Improve unit TCs
[platform/core/api/notification.git] / unittest / src / test_app_control_action.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <gtest/gtest.h>
18 #include <gmock/gmock.h>
19
20 #include "notification-ex/app_control_action.h"
21 #include "notification-ex/action_inflator.h"
22
23 using namespace notification;
24 using namespace notification::item;
25
26 class AppControlActionTest : public ::testing::Test {
27  public:
28   AppControlAction* action;
29   app_control_h app_control;
30   const char* app_id = "test_appid";
31   std::string extra = "appcontrol_extra";
32
33   virtual void SetUp() {
34     app_control_create(&app_control);
35     app_control_set_app_id(app_control, app_id);
36
37     action = new AppControlAction(app_control, extra);
38   }
39   virtual void TearDown() {
40     delete action;
41     app_control_destroy(app_control);
42   }
43 };
44
45 TEST_F(AppControlActionTest, create) {
46   EXPECT_NE(AppControlActionTest::action, nullptr);
47 }
48
49 TEST_F(AppControlActionTest, IsLocal) {
50   ASSERT_EQ(AppControlActionTest::action->IsLocal(), 1);
51 }
52
53 TEST_F(AppControlActionTest, GetExtra) {
54   ASSERT_EQ(AppControlActionTest::action->GetExtra(),
55     AppControlActionTest::extra);
56 }
57
58 TEST_F(AppControlActionTest, SerializeDeserialize) {
59   Bundle b = AppControlActionTest::action->Serialize();
60
61   std::shared_ptr<AbstractAction> gen_action = ActionInflator::Create(b);
62   ASSERT_EQ(gen_action->GetType(), AbstractAction::AppControl);
63
64   AppControlAction* gen_appcontrol_action =
65     static_cast<AppControlAction*>(gen_action.get());
66
67   char* app_id = nullptr;
68   app_control_get_app_id(gen_appcontrol_action->GetAppControl(), &app_id);
69   std::unique_ptr<char, decltype(std::free)*> ptr(app_id, std::free);
70   ASSERT_STREQ(app_id, AppControlActionTest::app_id);
71 }
72
73 TEST_F(AppControlActionTest, AppControl) {
74   app_control_h app_control;
75   const char* app_id = "new_appid";
76
77   app_control_create(&app_control);
78   app_control_set_app_id(app_control, app_id);
79   AppControlActionTest::action->SetAppControl(app_control);
80   app_control_destroy(app_control);
81
82   Bundle b = AppControlActionTest::action->Serialize();
83
84   std::shared_ptr<AbstractAction> gen_action = ActionInflator::Create(b);
85   AppControlAction* gen_appcontrol_action =
86     static_cast<AppControlAction*>(gen_action.get());
87
88   char* app_id_ = nullptr;
89   app_control_get_app_id(gen_appcontrol_action->GetAppControl(), &app_id_);
90   std::unique_ptr<char, decltype(std::free)*> ptr(app_id_, std::free);
91   EXPECT_STREQ(app_id, app_id_);
92 }