Add implementation of some classes
[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::item;
13 using namespace std;
14
15 namespace {
16
17 class GroupItemTest : public ::testing::Test {
18  protected:
19   void SetUp() override {}
20   void TearDown() override {}
21 };
22
23 TEST_F(GroupItemTest, AddChild) {
24   GroupItem item("GROUP1");
25   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
26   item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
27   ASSERT_EQ(item.GetChildren().size(), 2);
28 }
29
30 TEST_F(GroupItemTest, RemoveChild) {
31   GroupItem item("GROUP1");
32   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
33   item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
34   ASSERT_EQ(item.GetChildren().size(), 2);
35
36   item.RemoveChild("btn1");
37   ASSERT_EQ(item.GetChildren().size(), 1);
38 }
39
40 TEST_F(GroupItemTest, SerializeDeserialize) {
41   GroupItem item("GROUP1");
42   item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
43   item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
44
45   Bundle b = item.Serialize();
46   ItemFactory factory;
47   shared_ptr<AbstractItem> gen_item = ItemInflator::Create(factory, b);
48   ASSERT_EQ(gen_item.get()->GetType(), item.GetType());
49
50   GroupItem* gen_group = static_cast<GroupItem*>(gen_item.get());
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.get()->GetType(), (*it).get()->GetType());
57     if (i.get()->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   shared_ptr<AbstractItem> child = item.FindByID("btn2");
74   ButtonItem* btn = static_cast<ButtonItem*>(child.get());
75   ASSERT_EQ(btn->GetTitle(), "test2");
76 }
77
78 int __fake_app_get_name(char** app_name) {
79   *app_name = (char*)"unittest_appname";
80   return 0;
81 }
82
83 TEST_F(GroupItemTest, GetAppLabel) {
84   app_get_name_fake.custom_fake = __fake_app_get_name;
85
86   GroupItem item("GROUP1");
87   string app_label = item.GetAppLabel();
88
89   ASSERT_EQ(app_label, "unittest_appname");
90 }
91
92 TEST_F(GroupItemTest, SetDirection) {
93   GroupItem item("GROUP1");
94   item.SetDirection(true);
95
96   ASSERT_TRUE(item.IsVertical());
97 }
98
99 }  // namespace