Revert "Revert "Use tizen_base::Bundle""
[platform/core/api/notification.git] / unittest / src / test_group_item.cc
1 // Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #include <gmock/gmock.h>
6
7 #include "notification-ex/group_item.h"
8 #include "notification-ex/button_item.h"
9 #include "notification-ex/text_item.h"
10 #include "notification-ex/item_inflator.h"
11 #include "unittest/mock/app_common.h"
12
13 using namespace tizen_base;
14 using namespace notification;
15 using namespace notification::item;
16 using namespace std;
17
18 namespace {
19
20 class GroupItemTest : public ::testing::Test {
21  protected:
22   void SetUp() override {}
23   void TearDown() override {}
24 };
25
26 TEST_F(GroupItemTest, AddChild) {
27   GroupItem item("GROUP1");
28   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
29   item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
30   ASSERT_EQ(item.GetChildren().size(), 2);
31 }
32
33 TEST_F(GroupItemTest, RemoveChild) {
34   GroupItem item("GROUP1");
35   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
36   item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
37   ASSERT_EQ(item.GetChildren().size(), 2);
38
39   item.RemoveChild("btn1");
40   ASSERT_EQ(item.GetChildren().size(), 1);
41 }
42
43 TEST_F(GroupItemTest, SerializeDeserialize) {
44   GroupItem item("GROUP1");
45   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
46   item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
47
48   Bundle b = item.Serialize();
49   shared_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
50   ASSERT_EQ(gen_item->GetType(), item.GetType());
51
52   auto gen_group = static_pointer_cast<GroupItem>(gen_item);
53   list<shared_ptr<AbstractItem>> gen_children_list = gen_group->GetChildren();
54   list<shared_ptr<AbstractItem>> origin_children_list = item.GetChildren();
55
56   list<shared_ptr<AbstractItem>>::iterator it = origin_children_list.begin();
57   for (auto& i : gen_children_list) {
58     ASSERT_EQ(i->GetType(), (*it)->GetType());
59     if (i->GetType() == AbstractItem::Button) {
60       ButtonItem* btn1 = static_cast<ButtonItem*>(i.get());
61       ButtonItem* btn2 = static_cast<ButtonItem*>((*it).get());
62       ASSERT_EQ(btn1->GetTitle(), btn2->GetTitle());
63     }
64     it++;
65   }
66   ASSERT_EQ(item.GetChildren().size(), 2);
67 }
68
69 TEST_F(GroupItemTest, FindByID) {
70   GroupItem item("GROUP1");
71   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
72   item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
73   ASSERT_EQ(item.GetChildren().size(), 2);
74
75   AbstractItem& child = item.FindByID("btn2");
76   ButtonItem& btn = static_cast<ButtonItem&>(child);
77   ASSERT_EQ(btn.GetTitle(), "test2");
78 }
79
80 TEST_F(GroupItemTest, FindByIDGroupItem) {
81   GroupItem item("GROUP1");
82   shared_ptr<GroupItem> gr2 = shared_ptr<GroupItem>(new GroupItem("GROUP2"));
83   shared_ptr<GroupItem> gr3 = shared_ptr<GroupItem>(new GroupItem("GROUP3"));
84   gr2->AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
85   gr2->AddChild(std::make_shared<ButtonItem>("btn3", "test3"));
86   gr2->AddChild(std::make_shared<ButtonItem>("btn4", "test4"));
87   gr3->AddChild(std::make_shared<ButtonItem>("btn6", "test6"));
88   gr2->AddChild(gr3);
89
90   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
91   item.AddChild(gr2);
92   item.AddChild(std::make_shared<ButtonItem>("btn5", "test5"));
93   ASSERT_EQ(item.GetChildren().size(), 3);
94
95   AbstractItem& child = item.FindByID("btn3");
96   ButtonItem& btn = static_cast<ButtonItem&>(child);
97   ASSERT_EQ(btn.GetTitle(), "test3");
98
99   AbstractItem& child2 = item.FindByID("GROUP3");
100   GroupItem& ret_gr = static_cast<GroupItem&>(child2);
101   ASSERT_EQ(ret_gr.GetChildren().size(), 1);
102 }
103
104
105 TEST_F(GroupItemTest, IsItemTypeExist) {
106   GroupItem item("GROUP1");
107   shared_ptr<GroupItem> gr2 = shared_ptr<GroupItem>(new GroupItem("GROUP2"));
108   shared_ptr<GroupItem> gr3 = shared_ptr<GroupItem>(new GroupItem("GROUP3"));
109   gr2->AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
110   gr2->AddChild(std::make_shared<ButtonItem>("btn3", "test3"));
111   gr2->AddChild(std::make_shared<ButtonItem>("btn4", "test4"));
112   gr3->AddChild(std::make_shared<ButtonItem>("btn6", "test6"));
113   gr3->AddChild(std::make_shared<TextItem>("text1", "text1"));
114   gr2->AddChild(gr3);
115
116   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
117   item.AddChild(gr2);
118   item.AddChild(std::make_shared<ButtonItem>("btn5", "test5"));
119   ASSERT_EQ(item.GetChildren().size(), 3);
120
121   ASSERT_EQ(item.IsItemTypeExist(AbstractItem::Text), true);
122   ASSERT_EQ(item.IsItemTypeExist(AbstractItem::Image), false);
123 }
124
125 TEST_F(GroupItemTest, FindByIDNullItemReturn) {
126   GroupItem item("GROUP1");
127   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
128   item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
129   ASSERT_EQ(item.GetChildren().size(), 2);
130
131   AbstractItem& child = item.FindByID("not_exist_button");
132   ASSERT_EQ(child.GetType(), AbstractItem::NullObject);
133 }
134
135 int __fake_app_get_name(char** app_name) {
136   *app_name = (char*)"unittest_appname";
137   return 0;
138 }
139
140 TEST_F(GroupItemTest, GetAppLabel) {
141   app_get_name_fake.custom_fake = __fake_app_get_name;
142
143   GroupItem item("GROUP1");
144   string app_label = item.GetAppLabel();
145
146   ASSERT_EQ(app_label, "unittest_appname");
147 }
148
149 TEST_F(GroupItemTest, SetAppLabel) {
150   GroupItem item("GROUP1");
151   item.SetAppLabel("test");
152   string app_label = item.GetAppLabel();
153
154   ASSERT_EQ(app_label, "test");
155 }
156
157 TEST_F(GroupItemTest, SetDirection) {
158   GroupItem item("GROUP1");
159   item.SetDirection(true);
160
161   ASSERT_TRUE(item.IsVertical());
162 }
163
164 }  // namespace