Release version 0.7.2
[platform/core/api/notification.git] / tests / unittest / src / test_noti_ex_group_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 <gmock/gmock.h>
18
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"
25
26 using namespace tizen_base;
27 using namespace notification;
28 using namespace notification::item;
29 using namespace std;
30
31 using ::testing::_;
32 using ::testing::Invoke;
33
34 namespace {
35   int __fake_app_get_name(char** app_name) {
36     *app_name = strdup("unittest_appname");
37     return 0;
38   }
39 class Mocks :
40     virtual public ::testing::NiceMock<AppCommonMock> {};
41 } // namespace
42
43 class GroupItemTest : public TestFixture {
44  public:
45   GroupItemTest() : TestFixture(std::make_unique<::Mocks>()) {}
46  protected:
47   void SetUp() override {}
48   void TearDown() override {}
49 };
50
51 TEST_F(GroupItemTest, AddChild) {
52   EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
53       .WillRepeatedly(Invoke(__fake_app_get_name));
54
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);
59 }
60
61 TEST_F(GroupItemTest, RemoveChild) {
62   EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
63       .WillRepeatedly(Invoke(__fake_app_get_name));
64
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);
69
70   item.RemoveChild("btn1");
71   ASSERT_EQ(item.GetChildren().size(), 1);
72 }
73
74 TEST_F(GroupItemTest, SerializeDeserialize) {
75   EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
76       .WillRepeatedly(Invoke(__fake_app_get_name));
77
78   GroupItem item("GROUP1");
79   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
80   item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
81
82   Bundle b = item.Serialize();
83   shared_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
84   ASSERT_EQ(gen_item->GetType(), item.GetType());
85
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();
89
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());
97     }
98     it++;
99   }
100   ASSERT_EQ(item.GetChildren().size(), 2);
101 }
102
103 TEST_F(GroupItemTest, FindByID) {
104   EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
105       .WillRepeatedly(Invoke(__fake_app_get_name));
106
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);
111
112   AbstractItem& child = item.FindByID("btn2");
113   ButtonItem& btn = static_cast<ButtonItem&>(child);
114   ASSERT_EQ(btn.GetTitle(), "test2");
115 }
116
117 TEST_F(GroupItemTest, FindByIDGroupItem) {
118   EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
119       .WillRepeatedly(Invoke(__fake_app_get_name));
120
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"));
128   gr2->AddChild(gr3);
129
130   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
131   item.AddChild(gr2);
132   item.AddChild(std::make_shared<ButtonItem>("btn5", "test5"));
133   ASSERT_EQ(item.GetChildren().size(), 3);
134
135   AbstractItem& child = item.FindByID("btn3");
136   ButtonItem& btn = static_cast<ButtonItem&>(child);
137   ASSERT_EQ(btn.GetTitle(), "test3");
138
139   AbstractItem& child2 = item.FindByID("GROUP3");
140   GroupItem& ret_gr = static_cast<GroupItem&>(child2);
141   ASSERT_EQ(ret_gr.GetChildren().size(), 1);
142 }
143
144
145 TEST_F(GroupItemTest, IsItemTypeExist) {
146   EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
147       .WillRepeatedly(Invoke(__fake_app_get_name));
148
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"));
157   gr2->AddChild(gr3);
158
159   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
160   item.AddChild(gr2);
161   item.AddChild(std::make_shared<ButtonItem>("btn5", "test5"));
162   ASSERT_EQ(item.GetChildren().size(), 3);
163
164   ASSERT_EQ(item.IsItemTypeExist(AbstractItem::Text), true);
165   ASSERT_EQ(item.IsItemTypeExist(AbstractItem::Image), false);
166 }
167
168 TEST_F(GroupItemTest, FindByIDNullItemReturn) {
169   EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
170       .WillRepeatedly(Invoke(__fake_app_get_name));
171
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);
176
177   AbstractItem& child = item.FindByID("not_exist_button");
178   ASSERT_EQ(child.GetType(), AbstractItem::NullObject);
179 }
180
181 TEST_F(GroupItemTest, GetAppLabel) {
182   EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
183       .WillRepeatedly(Invoke(__fake_app_get_name));
184
185   GroupItem item("GROUP1");
186   string app_label = item.GetAppLabel();
187
188   ASSERT_EQ(app_label, "unittest_appname");
189 }
190
191 TEST_F(GroupItemTest, SetAppLabel) {
192   EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
193       .WillRepeatedly(Invoke(__fake_app_get_name));
194
195   GroupItem item("GROUP1");
196   item.SetAppLabel("test");
197   string app_label = item.GetAppLabel();
198
199   ASSERT_EQ(app_label, "test");
200 }
201
202 TEST_F(GroupItemTest, SetDirection) {
203   EXPECT_CALL(GetMock<AppCommonMock>(), app_get_name(_))
204       .WillRepeatedly(Invoke(__fake_app_get_name));
205
206   GroupItem item("GROUP1");
207   item.SetDirection(true);
208
209   ASSERT_TRUE(item.IsVertical());
210 }