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 #include "mock/test_fixture.h"
32 #include "mock/smack_mock.h"
33 #include "mock/app_common_mock.h"
35 #define MY_ITEM_TYPE AbstractItem::Type::Custom + 1
37 using namespace notification;
38 using namespace tizen_base;
39 using namespace notification::item;
42 using ::testing::Invoke;
45 class TestItem : public AbstractItem {
47 TestItem(std::shared_ptr<AbstractAction> action = std::shared_ptr<AbstractAction>({}))
48 : AbstractItem(action) {
50 TestItem(std::string id, std::shared_ptr<AbstractAction> action = std::shared_ptr<AbstractAction>({}))
51 : AbstractItem(id, action) {
53 virtual ~TestItem() {}
55 Bundle Serialize() const override {
57 b = AbstractItem::Serialize();
60 void Deserialize(Bundle b) override {
61 AbstractItem::Deserialize(b);
63 AbstractItem& FindByID(std::string id) override {
66 bool IsItemTypeExist(int type) {
67 if (GetType() == type)
71 int GetType() const override {
76 class MyFactory : public IItemFactory {
79 virtual ~MyFactory() {}
81 std::unique_ptr<AbstractItem> CreateItem(int type) override {
82 if (type == MY_ITEM_TYPE)
83 return std::unique_ptr<AbstractItem>(new TestItem(""));
90 virtual public ::testing::NiceMock<SmackMock>,
91 virtual public ::testing::NiceMock<AppCommonMock> {};
94 class AbstractItemTest : public TestFixture {
96 AbstractItemTest() : TestFixture(std::make_unique<::Mocks>()) {}
98 virtual void SetUp() {
99 FactoryManager::GetInst().RegisterFactory(std::unique_ptr<IItemFactory>(new MyFactory()));
101 virtual void TearDown() {
102 FactoryManager::GetInst().RegisterFactory(std::unique_ptr<IItemFactory>(new DefaultItemFactory()));
106 TEST_F(AbstractItemTest, SerializeDeserialize) {
108 app_control_h app_control, app_control_1;
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);
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);
123 TestItem item("test_id", action);
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);
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");
149 std::static_pointer_cast<IItemInfoInternal>(item.GetInfo())->SetTime(current_time);
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);
156 item.SetLEDInfo(led);
157 item.SetOnGoingState(true);
159 extension_b.Add("test_key", "test_value");
160 item.SetExtensionData("extension_key", extension_b);
163 Bundle b = item.Serialize();
165 std::shared_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
166 TestItem* gen_test = static_cast<TestItem*>(gen_item.get());
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);
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);
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);
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);
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);
204 ASSERT_EQ(gen_test->GetStyle()->GetBackgroundImage(), "bg path");
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);
213 std::list<std::string> receiver1 = item.GetReceiverList();
214 std::list<std::string> receiver2 = gen_test->GetReceiverList();
216 ASSERT_EQ(receiver1.size(), receiver2.size());
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();
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();
229 ASSERT_EQ(hide1.size(), hide2.size());
231 for (unsigned int i = 0; i < hide1.size(); i++) {
232 ASSERT_EQ(hide1.front(), hide2.front());
237 ASSERT_EQ(gen_test->GetAction()->GetType(), AbstractAction::Type::AppControl);
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);
243 ASSERT_STREQ(app_id, "new_appid");
245 item.SetAction(action_1);
247 b = item.Serialize();
248 gen_item = ItemInflator::Create(b);
249 gen_test = static_cast<TestItem*>(gen_item.get());
251 ac = std::static_pointer_cast<AppControlAction>(gen_test->GetAction());
252 app_control_get_app_id(ac->GetAppControl(), &app_id);
254 ASSERT_STREQ(app_id, "new_appid_1");
255 ASSERT_EQ(gen_test->GetOnGoingState(), true);
257 Bundle extension_b2 = gen_test->GetExtensionData("extension_key");
258 ASSERT_EQ(extension_b2.GetString("test_key"), "test_value");
261 TEST_F(AbstractItemTest, SerializeDeserialize2) {
263 TestItem item("test_id");
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);
269 item.SetStyle(std::make_shared<Style>(color, padding, geometry, nullptr, ""));
272 Bundle b = item.Serialize();
274 std::shared_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
275 TestItem* gen_test = static_cast<TestItem*>(gen_item.get());
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);
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);
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);
292 ASSERT_EQ(gen_test->GetStyle()->GetBackgroundColor(), nullptr);
293 ASSERT_EQ(gen_test->GetStyle()->GetBackgroundImage(), "");
296 TEST_F(AbstractItemTest, ItemInfoCanReceive) {
297 TestItem item("test_id");
299 item.AddReceiver(ReceiverGroup::Panel);
300 item.AddReceiver(ReceiverGroup::LockScreen);
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));
307 TEST_F(AbstractItemTest, SetGetOnGoingState) {
308 TestItem item("test_id");
310 ASSERT_EQ(item.GetOnGoingState(), false);
312 item.SetOnGoingState(true);
314 ASSERT_EQ(item.GetOnGoingState(), true);
317 int __fake_app_get_name(char** app_name) {
318 *app_name = strdup("unittest_appname");
322 TEST_F(AbstractItemTest, SetGetFindMainType) {
323 EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
324 .WillRepeatedly(Invoke(__fake_app_get_name));
326 auto root = std::make_shared<GroupItem>("test_group");
327 auto item = std::make_shared<ButtonItem>("test_id", "title");
328 root->AddChild(item);
330 bool ret = root->SetMainType("test_id", AbstractItem::MainButton);
332 EXPECT_EQ(item->GetMainType(), AbstractItem::MainButton);
333 auto& i = root->FindByMainType(AbstractItem::MainButton);
334 EXPECT_EQ(i.GetId(), "test_id");
337 TEST_F(AbstractItemTest, SetInvalidMainType) {
338 EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
339 .WillRepeatedly(Invoke(__fake_app_get_name));
341 auto root = std::make_shared<GroupItem>("test_group");
342 auto item = std::make_shared<ButtonItem>("test_id", "title");
343 root->AddChild(item);
345 bool ret = root->SetMainType("test_id", AbstractItem::MainTitle);