Use gmock instead of fff
[platform/core/api/notification.git] / tests / unittest / src / test_noti_ex_abstract_item.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 #include <app_control.h>
20 #include <app_common.h>
21
22 #include "notification-ex/item_inflator.h"
23 #include "notification-ex/app_control_action.h"
24 #include "notification-ex/item_info_internal.h"
25 #include "notification-ex/iitem_factory.h"
26 #include "notification-ex/factory_manager.h"
27 #include "notification-ex/default_item_factory.h"
28 #include "notification-ex/group_item.h"
29 #include "notification-ex/button_item.h"
30
31 #include "mock/test_fixture.h"
32 #include "mock/smack_mock.h"
33 #include "mock/app_common_mock.h"
34
35 #define MY_ITEM_TYPE AbstractItem::Type::Custom + 1
36
37 using namespace notification;
38 using namespace tizen_base;
39 using namespace notification::item;
40
41 using ::testing::_;
42 using ::testing::Invoke;
43
44 namespace {
45 class TestItem : public AbstractItem {
46   public:
47    TestItem(std::shared_ptr<AbstractAction> action = std::shared_ptr<AbstractAction>({}))
48      : AbstractItem(action) {
49    }
50    TestItem(std::string id, std::shared_ptr<AbstractAction> action = std::shared_ptr<AbstractAction>({}))
51      : AbstractItem(id, action) {
52    }
53    virtual ~TestItem() {}
54
55   Bundle Serialize() const override {
56     Bundle b;
57     b = AbstractItem::Serialize();
58     return b;
59   }
60   void Deserialize(Bundle b) override {
61     AbstractItem::Deserialize(b);
62   }
63   AbstractItem& FindByID(std::string id) override {
64     return *this;
65   }
66   bool IsItemTypeExist(int type) {
67   if (GetType() == type)
68       return true;
69     return false;
70   }
71   int GetType() const override {
72     return MY_ITEM_TYPE;
73   }
74 };
75
76 class MyFactory : public IItemFactory {
77  public:
78   MyFactory() {}
79   virtual ~MyFactory() {}
80
81   std::unique_ptr<AbstractItem> CreateItem(int type) override {
82     if (type == MY_ITEM_TYPE)
83       return std::unique_ptr<AbstractItem>(new TestItem(""));
84
85     return nullptr;
86   }
87 };
88
89 class Mocks :
90     virtual public ::testing::NiceMock<SmackMock>,
91     virtual public ::testing::NiceMock<AppCommonMock> {};
92 } // namespace
93
94 class AbstractItemTest : public TestFixture {
95  public:
96   AbstractItemTest() : TestFixture(std::make_unique<::Mocks>()) {}
97
98   virtual void SetUp() {
99     FactoryManager::GetInst().RegisterFactory(std::unique_ptr<IItemFactory>(new MyFactory()));
100   }
101   virtual void TearDown() {
102     FactoryManager::GetInst().RegisterFactory(std::unique_ptr<IItemFactory>(new DefaultItemFactory()));
103   }
104 };
105
106 TEST_F(AbstractItemTest, SerializeDeserialize) {
107   /* Serialize */
108   app_control_h app_control, app_control_1;
109   char* app_id = NULL;
110   time_t current_time;
111   Bundle extension_b;
112
113   app_control_create(&app_control);
114   app_control_set_app_id(app_control, "new_appid");
115   std::shared_ptr<AppControlAction> action = std::make_shared<AppControlAction>(app_control);
116   app_control_destroy(app_control);
117
118   app_control_create(&app_control_1);
119   app_control_set_app_id(app_control_1, "new_appid_1");
120   std::shared_ptr<AppControlAction> action_1 = std::make_shared<AppControlAction>(app_control_1);
121   app_control_destroy(app_control_1);
122
123   TestItem item("test_id", action);
124
125   std::shared_ptr<Color> color = std::make_shared<Color>(50, 100,150,200);
126   std::shared_ptr<Padding> padding = std::make_shared<Padding>(10, 20, 30, 40);
127   std::shared_ptr<Geometry> geometry = std::make_shared<Geometry>(110, 120, 130, 140);
128   std::shared_ptr<Color> bg_color = std::make_shared<Color>(71, 72, 73, 74);
129
130   item.SetStyle(std::make_shared<Style>(color, padding, geometry, bg_color, "bg path"));
131   item.SetVisible(false);
132   item.SetEnable(false);
133   item.AddReceiver("receiver_1");
134   item.AddReceiver("receiver_2");
135   std::static_pointer_cast<IItemInfoInternal>(item.GetInfo())->AddHideViewer("hide_1");
136   std::static_pointer_cast<IItemInfoInternal>(item.GetInfo())->AddHideViewer("hide_2");
137   item.SetPolicy(AbstractItem::Policy::OnBootClear);
138   std::static_pointer_cast<IItemInfoInternal>(item.GetInfo())->SetVersion(3);
139   item.GetInfo()->SetHideTime(5);
140   item.GetInfo()->SetDeleteTime(9);
141   item.SetChannel("channel99");
142   item.SetSoundPath("soundpath");
143   item.SetVibrationPath("vibrationpath");
144   std::static_pointer_cast<IItemInfoInternal>(item.GetInfo())->SetUid(3);
145   item.SetSenderAppId("sender");
146   item.SetTag("tag");
147
148   time(&current_time);
149   std::static_pointer_cast<IItemInfoInternal>(item.GetInfo())->SetTime(current_time);
150
151   std::shared_ptr<Color> color2 = std::make_shared<Color>(150, 160, 170, 180);
152   std::shared_ptr<LEDInfo> led = std::make_shared<LEDInfo>(color2);
153   led->SetOnPeriod(10);
154   led->SetOffPeriod(20);
155
156   item.SetLEDInfo(led);
157   item.SetOnGoingState(true);
158
159   extension_b.Add("test_key", "test_value");
160   item.SetExtensionData("extension_key", extension_b);
161
162   /* Deserialize */
163   Bundle b = item.Serialize();
164
165   std::shared_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
166   TestItem* gen_test = static_cast<TestItem*>(gen_item.get());
167
168   ASSERT_EQ(gen_test->GetId(), "test_id");
169   ASSERT_EQ(gen_test->GetType(), MY_ITEM_TYPE);
170   ASSERT_EQ(std::static_pointer_cast<IItemInfoInternal>(gen_test->GetInfo())->GetUid(), 3);
171   ASSERT_EQ(gen_test->GetEnable(), false);
172   ASSERT_EQ(gen_test->GetVisible(),false);
173   ASSERT_EQ(gen_test->GetPolicy(), AbstractItem::Policy::OnBootClear);
174   ASSERT_EQ(std::static_pointer_cast<IItemInfoInternal>(gen_test->GetInfo())->GetVersion(), 3);
175   ASSERT_EQ(gen_test->GetInfo()->GetHideTime(), 5);
176   ASSERT_EQ(gen_test->GetInfo()->GetDeleteTime(), 9);
177   ASSERT_EQ(gen_test->GetChannel(), "channel99");
178   ASSERT_EQ(gen_test->GetSoundPath(), "soundpath");
179   ASSERT_EQ(gen_test->GetVibrationPath(), "vibrationpath");
180   ASSERT_EQ(gen_test->GetSenderAppId(), "sender");
181   ASSERT_EQ(gen_test->GetTag(), "tag");
182   ASSERT_EQ(gen_test->GetInfo()->GetTime(), current_time);
183
184   ASSERT_EQ(gen_test->GetStyle()->GetPadding()->GetLeft(), 10);
185   ASSERT_EQ(gen_test->GetStyle()->GetPadding()->GetTop(), 20);
186   ASSERT_EQ(gen_test->GetStyle()->GetPadding()->GetRight(), 30);
187   ASSERT_EQ(gen_test->GetStyle()->GetPadding()->GetBottom(), 40);
188
189   ASSERT_EQ(gen_test->GetStyle()->GetColor()->GetAVal(), 50);
190   ASSERT_EQ(gen_test->GetStyle()->GetColor()->GetRVal(), 100);
191   ASSERT_EQ(gen_test->GetStyle()->GetColor()->GetGVal(), 150);
192   ASSERT_EQ(gen_test->GetStyle()->GetColor()->GetBVal(), 200);
193
194   ASSERT_EQ(gen_test->GetStyle()->GetGeometry()->GetX(), 110);
195   ASSERT_EQ(gen_test->GetStyle()->GetGeometry()->GetY(), 120);
196   ASSERT_EQ(gen_test->GetStyle()->GetGeometry()->GetWidth(), 130);
197   ASSERT_EQ(gen_test->GetStyle()->GetGeometry()->GetHeight(), 140);
198
199   ASSERT_EQ(gen_test->GetStyle()->GetBackgroundColor()->GetAVal(), 71);
200   ASSERT_EQ(gen_test->GetStyle()->GetBackgroundColor()->GetRVal(), 72);
201   ASSERT_EQ(gen_test->GetStyle()->GetBackgroundColor()->GetGVal(), 73);
202   ASSERT_EQ(gen_test->GetStyle()->GetBackgroundColor()->GetBVal(), 74);
203
204   ASSERT_EQ(gen_test->GetStyle()->GetBackgroundImage(), "bg path");
205
206   ASSERT_EQ(gen_test->GetLEDInfo()->GetColor()->GetAVal(), 150);
207   ASSERT_EQ(gen_test->GetLEDInfo()->GetColor()->GetRVal(), 160);
208   ASSERT_EQ(gen_test->GetLEDInfo()->GetColor()->GetGVal(), 170);
209   ASSERT_EQ(gen_test->GetLEDInfo()->GetColor()->GetBVal(), 180);
210   ASSERT_EQ(gen_test->GetLEDInfo()->GetOnPeriod(), 10);
211   ASSERT_EQ(gen_test->GetLEDInfo()->GetOffPeriod(), 20);
212
213   std::list<std::string> receiver1 = item.GetReceiverList();
214   std::list<std::string> receiver2 = gen_test->GetReceiverList();
215
216   ASSERT_EQ(receiver1.size(), receiver2.size());
217
218   for (unsigned int i = 0; i < receiver1.size(); i++) {
219     ASSERT_EQ(receiver1.front(), receiver2.front());
220     receiver1.pop_front();
221     receiver2.pop_front();
222   }
223
224   std::list<std::string> hide1 =
225       std::static_pointer_cast<IItemInfoInternal>(item.GetInfo())->GetHideViewerList();
226   std::list<std::string> hide2 =
227       std::static_pointer_cast<IItemInfoInternal>(gen_test->GetInfo())->GetHideViewerList();
228
229   ASSERT_EQ(hide1.size(), hide2.size());
230
231   for (unsigned int i = 0; i < hide1.size(); i++) {
232     ASSERT_EQ(hide1.front(), hide2.front());
233     hide1.pop_front();
234     hide2.pop_front();
235   }
236
237   ASSERT_EQ(gen_test->GetAction()->GetType(), AbstractAction::Type::AppControl);
238
239   std::shared_ptr<AppControlAction> ac =
240       std::static_pointer_cast<AppControlAction>(gen_test->GetAction());
241   app_control_get_app_id(ac->GetAppControl(), &app_id);
242
243   ASSERT_STREQ(app_id, "new_appid");
244
245   item.SetAction(action_1);
246
247   b = item.Serialize();
248   gen_item = ItemInflator::Create(b);
249   gen_test = static_cast<TestItem*>(gen_item.get());
250
251   ac = std::static_pointer_cast<AppControlAction>(gen_test->GetAction());
252   app_control_get_app_id(ac->GetAppControl(), &app_id);
253
254   ASSERT_STREQ(app_id, "new_appid_1");
255   ASSERT_EQ(gen_test->GetOnGoingState(), true);
256
257   Bundle extension_b2 = gen_test->GetExtensionData("extension_key");
258   ASSERT_EQ(extension_b2.GetString("test_key"), "test_value");
259 }
260
261 TEST_F(AbstractItemTest, SerializeDeserialize2) {
262   /* Serialize */
263   TestItem item("test_id");
264
265   std::shared_ptr<Color> color = std::make_shared<Color>(50, 100,150,200);
266   std::shared_ptr<Padding> padding = std::make_shared<Padding>(10, 20, 30, 40);
267   std::shared_ptr<Geometry> geometry = std::make_shared<Geometry>(110, 120, 130, 140);
268
269   item.SetStyle(std::make_shared<Style>(color, padding, geometry, nullptr, ""));
270
271   /* Deserialize */
272   Bundle b = item.Serialize();
273
274   std::shared_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
275   TestItem* gen_test = static_cast<TestItem*>(gen_item.get());
276
277   ASSERT_EQ(gen_test->GetStyle()->GetPadding()->GetLeft(), 10);
278   ASSERT_EQ(gen_test->GetStyle()->GetPadding()->GetTop(), 20);
279   ASSERT_EQ(gen_test->GetStyle()->GetPadding()->GetRight(), 30);
280   ASSERT_EQ(gen_test->GetStyle()->GetPadding()->GetBottom(), 40);
281
282   ASSERT_EQ(gen_test->GetStyle()->GetColor()->GetAVal(), 50);
283   ASSERT_EQ(gen_test->GetStyle()->GetColor()->GetRVal(), 100);
284   ASSERT_EQ(gen_test->GetStyle()->GetColor()->GetGVal(), 150);
285   ASSERT_EQ(gen_test->GetStyle()->GetColor()->GetBVal(), 200);
286
287   ASSERT_EQ(gen_test->GetStyle()->GetGeometry()->GetX(), 110);
288   ASSERT_EQ(gen_test->GetStyle()->GetGeometry()->GetY(), 120);
289   ASSERT_EQ(gen_test->GetStyle()->GetGeometry()->GetWidth(), 130);
290   ASSERT_EQ(gen_test->GetStyle()->GetGeometry()->GetHeight(), 140);
291
292   ASSERT_EQ(gen_test->GetStyle()->GetBackgroundColor(), nullptr);
293   ASSERT_EQ(gen_test->GetStyle()->GetBackgroundImage(), "");
294 }
295
296 TEST_F(AbstractItemTest, ItemInfoCanReceive) {
297   TestItem item("test_id");
298
299   item.AddReceiver(ReceiverGroup::Panel);
300   item.AddReceiver(ReceiverGroup::LockScreen);
301
302   ASSERT_TRUE(std::static_pointer_cast<IItemInfoInternal>(item.GetInfo())->CanReceive(ReceiverGroup::Panel));
303   ASSERT_TRUE(std::static_pointer_cast<IItemInfoInternal>(item.GetInfo())->CanReceive(ReceiverGroup::LockScreen));
304   ASSERT_FALSE(std::static_pointer_cast<IItemInfoInternal>(item.GetInfo())->CanReceive(ReceiverGroup::Popup));
305 }
306
307 TEST_F(AbstractItemTest, SetGetOnGoingState) {
308   TestItem item("test_id");
309
310   ASSERT_EQ(item.GetOnGoingState(), false);
311
312   item.SetOnGoingState(true);
313
314   ASSERT_EQ(item.GetOnGoingState(), true);
315 }
316
317 int __fake_app_get_name(char** app_name) {
318   *app_name = strdup("unittest_appname");
319   return 0;
320 }
321
322 TEST_F(AbstractItemTest, SetGetFindMainType) {
323   EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
324       .WillRepeatedly(Invoke(__fake_app_get_name));
325
326   auto root = std::make_shared<GroupItem>("test_group");
327   auto item = std::make_shared<ButtonItem>("test_id", "title");
328   root->AddChild(item);
329
330   bool ret = root->SetMainType("test_id", AbstractItem::MainButton);
331   EXPECT_TRUE(ret);
332   EXPECT_EQ(item->GetMainType(), AbstractItem::MainButton);
333   auto& i = root->FindByMainType(AbstractItem::MainButton);
334   EXPECT_EQ(i.GetId(), "test_id");
335 }
336
337 TEST_F(AbstractItemTest, SetInvalidMainType) {
338   EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
339       .WillRepeatedly(Invoke(__fake_app_get_name));
340
341   auto root = std::make_shared<GroupItem>("test_group");
342   auto item = std::make_shared<ButtonItem>("test_id", "title");
343   root->AddChild(item);
344
345   bool ret = root->SetMainType("test_id", AbstractItem::MainTitle);
346   EXPECT_FALSE(ret);
347 }