2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
18 #include <gmock/gmock.h>
19 #include <app_control.h>
20 #include <app_common.h>
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"
31 #define MY_ITEM_TYPE AbstractItem::Type::Custom + 1
33 using namespace notification;
34 using namespace tizen_base;
35 using namespace notification::item;
38 class TestItem : public AbstractItem {
40 TestItem(std::shared_ptr<AbstractAction> action = std::shared_ptr<AbstractAction>({}))
41 : AbstractItem(action) {
43 TestItem(std::string id, std::shared_ptr<AbstractAction> action = std::shared_ptr<AbstractAction>({}))
44 : AbstractItem(id, action) {
46 virtual ~TestItem() {}
48 Bundle Serialize() const override {
50 b = AbstractItem::Serialize();
53 void Deserialize(Bundle b) override {
54 AbstractItem::Deserialize(b);
56 AbstractItem& FindByID(std::string id) override {
59 bool IsItemTypeExist(int type) {
60 if (GetType() == type)
64 int GetType() const override {
69 class MyFactory : public IItemFactory {
72 virtual ~MyFactory() {}
74 std::unique_ptr<AbstractItem> CreateItem(int type) override {
75 if (type == MY_ITEM_TYPE)
76 return std::unique_ptr<AbstractItem>(new TestItem(""));
82 class AbstractItemTest : public ::testing::Test {
84 virtual void SetUp() {
85 FactoryManager::GetInst().RegisterFactory(std::unique_ptr<IItemFactory>(new MyFactory()));
87 virtual void TearDown() {
88 FactoryManager::GetInst().RegisterFactory(std::unique_ptr<IItemFactory>(new DefaultItemFactory()));
92 TEST_F(AbstractItemTest, SerializeDeserialize) {
94 app_control_h app_control, app_control_1;
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);
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);
109 TestItem item("test_id", action);
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);
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");
135 std::static_pointer_cast<IItemInfoInternal>(item.GetInfo())->SetTime(current_time);
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);
142 item.SetLEDInfo(led);
143 item.SetOnGoingState(true);
145 extension_b.Add("test_key", "test_value");
146 item.SetExtensionData("extension_key", extension_b);
149 Bundle b = item.Serialize();
151 std::shared_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
152 TestItem* gen_test = static_cast<TestItem*>(gen_item.get());
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);
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);
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);
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);
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);
190 ASSERT_EQ(gen_test->GetStyle()->GetBackgroundImage(), "bg path");
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);
199 std::list<std::string> receiver1 = item.GetReceiverList();
200 std::list<std::string> receiver2 = gen_test->GetReceiverList();
202 ASSERT_EQ(receiver1.size(), receiver2.size());
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();
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();
215 ASSERT_EQ(hide1.size(), hide2.size());
217 for (unsigned int i = 0; i < hide1.size(); i++) {
218 ASSERT_EQ(hide1.front(), hide2.front());
223 ASSERT_EQ(gen_test->GetAction()->GetType(), AbstractAction::Type::AppControl);
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);
229 ASSERT_STREQ(app_id, "new_appid");
231 item.SetAction(action_1);
233 b = item.Serialize();
234 gen_item = ItemInflator::Create(b);
235 gen_test = static_cast<TestItem*>(gen_item.get());
237 ac = std::static_pointer_cast<AppControlAction>(gen_test->GetAction());
238 app_control_get_app_id(ac->GetAppControl(), &app_id);
240 ASSERT_STREQ(app_id, "new_appid_1");
241 ASSERT_EQ(gen_test->GetOnGoingState(), true);
243 Bundle extension_b2 = gen_test->GetExtensionData("extension_key");
244 ASSERT_EQ(extension_b2.GetString("test_key"), "test_value");
247 TEST_F(AbstractItemTest, SerializeDeserialize2) {
249 TestItem item("test_id");
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);
255 item.SetStyle(std::make_shared<Style>(color, padding, geometry, nullptr, ""));
258 Bundle b = item.Serialize();
260 std::shared_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
261 TestItem* gen_test = static_cast<TestItem*>(gen_item.get());
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);
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);
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);
278 ASSERT_EQ(gen_test->GetStyle()->GetBackgroundColor(), nullptr);
279 ASSERT_EQ(gen_test->GetStyle()->GetBackgroundImage(), "");
282 TEST_F(AbstractItemTest, ItemInfoCanReceive) {
283 TestItem item("test_id");
285 item.AddReceiver(ReceiverGroup::Panel);
286 item.AddReceiver(ReceiverGroup::LockScreen);
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));
293 TEST_F(AbstractItemTest, SetGetOnGoingState) {
294 TestItem item("test_id");
296 ASSERT_EQ(item.GetOnGoingState(), false);
298 item.SetOnGoingState(true);
300 ASSERT_EQ(item.GetOnGoingState(), true);
303 int __fake_app_get_name(char** app_name) {
304 *app_name = strdup("unittest_appname");
308 TEST_F(AbstractItemTest, SetGetFindMainType) {
309 app_get_name_fake.custom_fake = __fake_app_get_name;
311 auto root = std::make_shared<GroupItem>("test_group");
312 auto item = std::make_shared<ButtonItem>("test_id", "title");
313 root->AddChild(item);
315 bool ret = root->SetMainType("test_id", AbstractItem::MainButton);
317 EXPECT_EQ(item->GetMainType(), AbstractItem::MainButton);
318 auto& i = root->FindByMainType(AbstractItem::MainButton);
319 EXPECT_EQ(i.GetId(), "test_id");
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);
327 bool ret = root->SetMainType("test_id", AbstractItem::MainTitle);