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.
5 #include <gmock/gmock.h>
7 #include "notification-ex/group_item.h"
8 #include "notification-ex/button_item.h"
9 #include "notification-ex/item_inflator.h"
10 #include "unittest/mock/app_common.h"
12 using namespace notification;
13 using namespace notification::item;
18 class GroupItemTest : public ::testing::Test {
20 void SetUp() override {}
21 void TearDown() override {}
24 TEST_F(GroupItemTest, AddChild) {
25 GroupItem item("GROUP1");
26 item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
27 item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
28 ASSERT_EQ(item.GetChildren().size(), 2);
31 TEST_F(GroupItemTest, RemoveChild) {
32 GroupItem item("GROUP1");
33 item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
34 item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
35 ASSERT_EQ(item.GetChildren().size(), 2);
37 item.RemoveChild("btn1");
38 ASSERT_EQ(item.GetChildren().size(), 1);
41 TEST_F(GroupItemTest, SerializeDeserialize) {
42 GroupItem item("GROUP1");
43 item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
44 item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
46 Bundle b = item.Serialize();
47 shared_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
48 ASSERT_EQ(gen_item->GetType(), item.GetType());
50 auto gen_group = static_pointer_cast<GroupItem>(gen_item);
51 list<shared_ptr<AbstractItem>> gen_children_list = gen_group->GetChildren();
52 list<shared_ptr<AbstractItem>> origin_children_list = item.GetChildren();
54 list<shared_ptr<AbstractItem>>::iterator it = origin_children_list.begin();
55 for (auto& i : gen_children_list) {
56 ASSERT_EQ(i->GetType(), (*it)->GetType());
57 if (i->GetType() == AbstractItem::Button) {
58 ButtonItem* btn1 = static_cast<ButtonItem*>(i.get());
59 ButtonItem* btn2 = static_cast<ButtonItem*>((*it).get());
60 ASSERT_EQ(btn1->GetTitle(), btn2->GetTitle());
64 ASSERT_EQ(item.GetChildren().size(), 2);
67 TEST_F(GroupItemTest, FindByID) {
68 GroupItem item("GROUP1");
69 item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
70 item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
71 ASSERT_EQ(item.GetChildren().size(), 2);
73 AbstractItem& child = item.FindByID("btn2");
74 ButtonItem& btn = static_cast<ButtonItem&>(child);
75 ASSERT_EQ(btn.GetTitle(), "test2");
78 TEST_F(GroupItemTest, FindByIDGroupItem) {
79 GroupItem item("GROUP1");
80 shared_ptr<GroupItem> gr2 = shared_ptr<GroupItem>(new GroupItem("GROUP2"));
81 shared_ptr<GroupItem> gr3 = shared_ptr<GroupItem>(new GroupItem("GROUP3"));
82 gr2->AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
83 gr2->AddChild(std::make_shared<ButtonItem>("btn3", "test3"));
84 gr2->AddChild(std::make_shared<ButtonItem>("btn4", "test4"));
85 gr3->AddChild(std::make_shared<ButtonItem>("btn6", "test6"));
88 item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
90 item.AddChild(std::make_shared<ButtonItem>("btn5", "test5"));
91 ASSERT_EQ(item.GetChildren().size(), 3);
93 AbstractItem& child = item.FindByID("btn3");
94 ButtonItem& btn = static_cast<ButtonItem&>(child);
95 ASSERT_EQ(btn.GetTitle(), "test3");
97 AbstractItem& child2 = item.FindByID("GROUP3");
98 GroupItem& ret_gr = static_cast<GroupItem&>(child2);
99 ASSERT_EQ(ret_gr.GetChildren().size(), 1);
102 TEST_F(GroupItemTest, FindByIDNullItemReturn) {
103 GroupItem item("GROUP1");
104 item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
105 item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
106 ASSERT_EQ(item.GetChildren().size(), 2);
108 AbstractItem& child = item.FindByID("not_exist_button");
109 ASSERT_EQ(child.GetType(), AbstractItem::NullObject);
112 int __fake_app_get_name(char** app_name) {
113 *app_name = (char*)"unittest_appname";
117 TEST_F(GroupItemTest, GetAppLabel) {
118 app_get_name_fake.custom_fake = __fake_app_get_name;
120 GroupItem item("GROUP1");
121 string app_label = item.GetAppLabel();
123 ASSERT_EQ(app_label, "unittest_appname");
126 TEST_F(GroupItemTest, SetAppLabel) {
127 GroupItem item("GROUP1");
128 item.SetAppLabel("test");
129 string app_label = item.GetAppLabel();
131 ASSERT_EQ(app_label, "test");
134 TEST_F(GroupItemTest, SetDirection) {
135 GroupItem item("GROUP1");
136 item.SetDirection(true);
138 ASSERT_TRUE(item.IsVertical());