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