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/item_inflator.h"
10 #include "unittest/mock/app_common.h"
11
12 using namespace notification;
13 using namespace notification::item;
14 using namespace std;
15
16 namespace {
17
18 class GroupItemTest : public ::testing::Test {
19  protected:
20   void SetUp() override {}
21   void TearDown() override {}
22 };
23
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);
29 }
30
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);
36
37   item.RemoveChild("btn1");
38   ASSERT_EQ(item.GetChildren().size(), 1);
39 }
40
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"));
45
46   Bundle b = item.Serialize();
47   shared_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
48   ASSERT_EQ(gen_item->GetType(), item.GetType());
49
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();
53
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());
61     }
62     it++;
63   }
64   ASSERT_EQ(item.GetChildren().size(), 2);
65 }
66
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);
72
73   AbstractItem& child = item.FindByID("btn2");
74   ButtonItem& btn = static_cast<ButtonItem&>(child);
75   ASSERT_EQ(btn.GetTitle(), "test2");
76 }
77
78 TEST_F(GroupItemTest, FindByIDNullItemReturn) {
79   GroupItem item("GROUP1");
80   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
81   item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
82   ASSERT_EQ(item.GetChildren().size(), 2);
83
84   AbstractItem& child = item.FindByID("not_exist_button");
85   ASSERT_EQ(child.GetType(), AbstractItem::NullObject);
86 }
87
88 int __fake_app_get_name(char** app_name) {
89   *app_name = (char*)"unittest_appname";
90   return 0;
91 }
92
93 TEST_F(GroupItemTest, GetAppLabel) {
94   app_get_name_fake.custom_fake = __fake_app_get_name;
95
96   GroupItem item("GROUP1");
97   string app_label = item.GetAppLabel();
98
99   ASSERT_EQ(app_label, "unittest_appname");
100 }
101
102 TEST_F(GroupItemTest, SetDirection) {
103   GroupItem item("GROUP1");
104   item.SetDirection(true);
105
106   ASSERT_TRUE(item.IsVertical());
107 }
108
109 }  // namespace