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 <gmock/gmock.h>
19 #include "notification-ex/group_item.h"
20 #include "notification-ex/button_item.h"
21 #include "notification-ex/text_item.h"
22 #include "notification-ex/item_inflator.h"
23 #include "mock/app_common_mock.h"
24 #include "mock/test_fixture.h"
26 using namespace tizen_base;
27 using namespace notification;
28 using namespace notification::item;
32 using ::testing::Invoke;
35 int __fake_app_get_name(char** app_name) {
36 *app_name = strdup("unittest_appname");
40 virtual public ::testing::NiceMock<AppCommonMock> {};
43 class GroupItemTest : public TestFixture {
45 GroupItemTest() : TestFixture(std::make_unique<::Mocks>()) {}
47 void SetUp() override {}
48 void TearDown() override {}
51 TEST_F(GroupItemTest, AddChild) {
52 EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
53 .WillRepeatedly(Invoke(__fake_app_get_name));
55 GroupItem item("GROUP1");
56 item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
57 item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
58 ASSERT_EQ(item.GetChildren().size(), 2);
61 TEST_F(GroupItemTest, RemoveChild) {
62 EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
63 .WillRepeatedly(Invoke(__fake_app_get_name));
65 GroupItem item("GROUP1");
66 item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
67 item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
68 ASSERT_EQ(item.GetChildren().size(), 2);
70 item.RemoveChild("btn1");
71 ASSERT_EQ(item.GetChildren().size(), 1);
74 TEST_F(GroupItemTest, SerializeDeserialize) {
75 EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
76 .WillRepeatedly(Invoke(__fake_app_get_name));
78 GroupItem item("GROUP1");
79 item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
80 item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
82 Bundle b = item.Serialize();
83 shared_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
84 ASSERT_EQ(gen_item->GetType(), item.GetType());
86 auto gen_group = static_pointer_cast<GroupItem>(gen_item);
87 list<shared_ptr<AbstractItem>> gen_children_list = gen_group->GetChildren();
88 list<shared_ptr<AbstractItem>> origin_children_list = item.GetChildren();
90 list<shared_ptr<AbstractItem>>::iterator it = origin_children_list.begin();
91 for (auto& i : gen_children_list) {
92 ASSERT_EQ(i->GetType(), (*it)->GetType());
93 if (i->GetType() == AbstractItem::Button) {
94 ButtonItem* btn1 = static_cast<ButtonItem*>(i.get());
95 ButtonItem* btn2 = static_cast<ButtonItem*>((*it).get());
96 ASSERT_EQ(btn1->GetTitle(), btn2->GetTitle());
100 ASSERT_EQ(item.GetChildren().size(), 2);
103 TEST_F(GroupItemTest, FindByID) {
104 EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
105 .WillRepeatedly(Invoke(__fake_app_get_name));
107 GroupItem item("GROUP1");
108 item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
109 item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
110 ASSERT_EQ(item.GetChildren().size(), 2);
112 AbstractItem& child = item.FindByID("btn2");
113 ButtonItem& btn = static_cast<ButtonItem&>(child);
114 ASSERT_EQ(btn.GetTitle(), "test2");
117 TEST_F(GroupItemTest, FindByIDGroupItem) {
118 EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
119 .WillRepeatedly(Invoke(__fake_app_get_name));
121 GroupItem item("GROUP1");
122 shared_ptr<GroupItem> gr2 = shared_ptr<GroupItem>(new GroupItem("GROUP2"));
123 shared_ptr<GroupItem> gr3 = shared_ptr<GroupItem>(new GroupItem("GROUP3"));
124 gr2->AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
125 gr2->AddChild(std::make_shared<ButtonItem>("btn3", "test3"));
126 gr2->AddChild(std::make_shared<ButtonItem>("btn4", "test4"));
127 gr3->AddChild(std::make_shared<ButtonItem>("btn6", "test6"));
130 item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
132 item.AddChild(std::make_shared<ButtonItem>("btn5", "test5"));
133 ASSERT_EQ(item.GetChildren().size(), 3);
135 AbstractItem& child = item.FindByID("btn3");
136 ButtonItem& btn = static_cast<ButtonItem&>(child);
137 ASSERT_EQ(btn.GetTitle(), "test3");
139 AbstractItem& child2 = item.FindByID("GROUP3");
140 GroupItem& ret_gr = static_cast<GroupItem&>(child2);
141 ASSERT_EQ(ret_gr.GetChildren().size(), 1);
145 TEST_F(GroupItemTest, IsItemTypeExist) {
146 EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
147 .WillRepeatedly(Invoke(__fake_app_get_name));
149 GroupItem item("GROUP1");
150 shared_ptr<GroupItem> gr2 = shared_ptr<GroupItem>(new GroupItem("GROUP2"));
151 shared_ptr<GroupItem> gr3 = shared_ptr<GroupItem>(new GroupItem("GROUP3"));
152 gr2->AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
153 gr2->AddChild(std::make_shared<ButtonItem>("btn3", "test3"));
154 gr2->AddChild(std::make_shared<ButtonItem>("btn4", "test4"));
155 gr3->AddChild(std::make_shared<ButtonItem>("btn6", "test6"));
156 gr3->AddChild(std::make_shared<TextItem>("text1", "text1"));
159 item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
161 item.AddChild(std::make_shared<ButtonItem>("btn5", "test5"));
162 ASSERT_EQ(item.GetChildren().size(), 3);
164 ASSERT_EQ(item.IsItemTypeExist(AbstractItem::Text), true);
165 ASSERT_EQ(item.IsItemTypeExist(AbstractItem::Image), false);
168 TEST_F(GroupItemTest, FindByIDNullItemReturn) {
169 EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
170 .WillRepeatedly(Invoke(__fake_app_get_name));
172 GroupItem item("GROUP1");
173 item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
174 item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
175 ASSERT_EQ(item.GetChildren().size(), 2);
177 AbstractItem& child = item.FindByID("not_exist_button");
178 ASSERT_EQ(child.GetType(), AbstractItem::NullObject);
181 TEST_F(GroupItemTest, GetAppLabel) {
182 EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
183 .WillRepeatedly(Invoke(__fake_app_get_name));
185 GroupItem item("GROUP1");
186 string app_label = item.GetAppLabel();
188 ASSERT_EQ(app_label, "unittest_appname");
191 TEST_F(GroupItemTest, SetAppLabel) {
192 EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
193 .WillRepeatedly(Invoke(__fake_app_get_name));
195 GroupItem item("GROUP1");
196 item.SetAppLabel("test");
197 string app_label = item.GetAppLabel();
199 ASSERT_EQ(app_label, "test");
202 TEST_F(GroupItemTest, SetDirection) {
203 EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
204 .WillRepeatedly(Invoke(__fake_app_get_name));
206 GroupItem item("GROUP1");
207 item.SetDirection(true);
209 ASSERT_TRUE(item.IsVertical());