8cf8c79321682b49de804d1c13baa95c6c73ffd0
[platform/core/api/notification.git] / unittest / src / test_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.h"
24
25 using namespace tizen_base;
26 using namespace notification;
27 using namespace notification::item;
28 using namespace std;
29
30 namespace {
31
32 class GroupItemTest : public ::testing::Test {
33  protected:
34   void SetUp() override {}
35   void TearDown() override {}
36 };
37
38 TEST_F(GroupItemTest, AddChild) {
39   GroupItem item("GROUP1");
40   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
41   item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
42   ASSERT_EQ(item.GetChildren().size(), 2);
43 }
44
45 TEST_F(GroupItemTest, RemoveChild) {
46   GroupItem item("GROUP1");
47   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
48   item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
49   ASSERT_EQ(item.GetChildren().size(), 2);
50
51   item.RemoveChild("btn1");
52   ASSERT_EQ(item.GetChildren().size(), 1);
53 }
54
55 int __fake_app_get_name(char** app_name) {
56   *app_name = strdup("unittest_appname");
57   return 0;
58 }
59
60 TEST_F(GroupItemTest, SerializeDeserialize) {
61   app_get_name_fake.custom_fake = __fake_app_get_name;
62
63   GroupItem item("GROUP1");
64   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
65   item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
66
67   Bundle b = item.Serialize();
68   shared_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
69   ASSERT_EQ(gen_item->GetType(), item.GetType());
70
71   auto gen_group = static_pointer_cast<GroupItem>(gen_item);
72   list<shared_ptr<AbstractItem>> gen_children_list = gen_group->GetChildren();
73   list<shared_ptr<AbstractItem>> origin_children_list = item.GetChildren();
74
75   list<shared_ptr<AbstractItem>>::iterator it = origin_children_list.begin();
76   for (auto& i : gen_children_list) {
77     ASSERT_EQ(i->GetType(), (*it)->GetType());
78     if (i->GetType() == AbstractItem::Button) {
79       ButtonItem* btn1 = static_cast<ButtonItem*>(i.get());
80       ButtonItem* btn2 = static_cast<ButtonItem*>((*it).get());
81       ASSERT_EQ(btn1->GetTitle(), btn2->GetTitle());
82     }
83     it++;
84   }
85   ASSERT_EQ(item.GetChildren().size(), 2);
86 }
87
88 TEST_F(GroupItemTest, FindByID) {
89   GroupItem item("GROUP1");
90   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
91   item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
92   ASSERT_EQ(item.GetChildren().size(), 2);
93
94   AbstractItem& child = item.FindByID("btn2");
95   ButtonItem& btn = static_cast<ButtonItem&>(child);
96   ASSERT_EQ(btn.GetTitle(), "test2");
97 }
98
99 TEST_F(GroupItemTest, FindByIDGroupItem) {
100   GroupItem item("GROUP1");
101   shared_ptr<GroupItem> gr2 = shared_ptr<GroupItem>(new GroupItem("GROUP2"));
102   shared_ptr<GroupItem> gr3 = shared_ptr<GroupItem>(new GroupItem("GROUP3"));
103   gr2->AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
104   gr2->AddChild(std::make_shared<ButtonItem>("btn3", "test3"));
105   gr2->AddChild(std::make_shared<ButtonItem>("btn4", "test4"));
106   gr3->AddChild(std::make_shared<ButtonItem>("btn6", "test6"));
107   gr2->AddChild(gr3);
108
109   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
110   item.AddChild(gr2);
111   item.AddChild(std::make_shared<ButtonItem>("btn5", "test5"));
112   ASSERT_EQ(item.GetChildren().size(), 3);
113
114   AbstractItem& child = item.FindByID("btn3");
115   ButtonItem& btn = static_cast<ButtonItem&>(child);
116   ASSERT_EQ(btn.GetTitle(), "test3");
117
118   AbstractItem& child2 = item.FindByID("GROUP3");
119   GroupItem& ret_gr = static_cast<GroupItem&>(child2);
120   ASSERT_EQ(ret_gr.GetChildren().size(), 1);
121 }
122
123
124 TEST_F(GroupItemTest, IsItemTypeExist) {
125   GroupItem item("GROUP1");
126   shared_ptr<GroupItem> gr2 = shared_ptr<GroupItem>(new GroupItem("GROUP2"));
127   shared_ptr<GroupItem> gr3 = shared_ptr<GroupItem>(new GroupItem("GROUP3"));
128   gr2->AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
129   gr2->AddChild(std::make_shared<ButtonItem>("btn3", "test3"));
130   gr2->AddChild(std::make_shared<ButtonItem>("btn4", "test4"));
131   gr3->AddChild(std::make_shared<ButtonItem>("btn6", "test6"));
132   gr3->AddChild(std::make_shared<TextItem>("text1", "text1"));
133   gr2->AddChild(gr3);
134
135   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
136   item.AddChild(gr2);
137   item.AddChild(std::make_shared<ButtonItem>("btn5", "test5"));
138   ASSERT_EQ(item.GetChildren().size(), 3);
139
140   ASSERT_EQ(item.IsItemTypeExist(AbstractItem::Text), true);
141   ASSERT_EQ(item.IsItemTypeExist(AbstractItem::Image), false);
142 }
143
144 TEST_F(GroupItemTest, FindByIDNullItemReturn) {
145   GroupItem item("GROUP1");
146   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
147   item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
148   ASSERT_EQ(item.GetChildren().size(), 2);
149
150   AbstractItem& child = item.FindByID("not_exist_button");
151   ASSERT_EQ(child.GetType(), AbstractItem::NullObject);
152 }
153
154 TEST_F(GroupItemTest, GetAppLabel) {
155   app_get_name_fake.custom_fake = __fake_app_get_name;
156
157   GroupItem item("GROUP1");
158   string app_label = item.GetAppLabel();
159
160   ASSERT_EQ(app_label, "unittest_appname");
161 }
162
163 TEST_F(GroupItemTest, SetAppLabel) {
164   GroupItem item("GROUP1");
165   item.SetAppLabel("test");
166   string app_label = item.GetAppLabel();
167
168   ASSERT_EQ(app_label, "test");
169 }
170
171 TEST_F(GroupItemTest, SetDirection) {
172   GroupItem item("GROUP1");
173   item.SetDirection(true);
174
175   ASSERT_TRUE(item.IsVertical());
176 }
177
178 }  // namespace