Fix resouce leak
[platform/core/api/notification.git] / unittest / src / test_group_item.cc
index 67fb14d..caa46b0 100644 (file)
@@ -6,9 +6,12 @@
 
 #include "notification-ex/group_item.h"
 #include "notification-ex/button_item.h"
+#include "notification-ex/text_item.h"
 #include "notification-ex/item_inflator.h"
 #include "unittest/mock/app_common.h"
 
+using namespace tizen_base;
+using namespace notification;
 using namespace notification::item;
 using namespace std;
 
@@ -43,18 +46,17 @@ TEST_F(GroupItemTest, SerializeDeserialize) {
   item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
 
   Bundle b = item.Serialize();
-  ItemFactory factory;
-  shared_ptr<AbstractItem> gen_item = ItemInflator::Create(factory, b);
-  ASSERT_EQ(gen_item.get()->GetType(), item.GetType());
+  shared_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
+  ASSERT_EQ(gen_item->GetType(), item.GetType());
 
-  GroupItem* gen_group = static_cast<GroupItem*>(gen_item.get());
+  auto gen_group = static_pointer_cast<GroupItem>(gen_item);
   list<shared_ptr<AbstractItem>> gen_children_list = gen_group->GetChildren();
   list<shared_ptr<AbstractItem>> origin_children_list = item.GetChildren();
 
   list<shared_ptr<AbstractItem>>::iterator it = origin_children_list.begin();
   for (auto& i : gen_children_list) {
-    ASSERT_EQ(i.get()->GetType(), (*it).get()->GetType());
-    if (i.get()->GetType() == AbstractItem::Button) {
+    ASSERT_EQ(i->GetType(), (*it)->GetType());
+    if (i->GetType() == AbstractItem::Button) {
       ButtonItem* btn1 = static_cast<ButtonItem*>(i.get());
       ButtonItem* btn2 = static_cast<ButtonItem*>((*it).get());
       ASSERT_EQ(btn1->GetTitle(), btn2->GetTitle());
@@ -70,13 +72,68 @@ TEST_F(GroupItemTest, FindByID) {
   item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
   ASSERT_EQ(item.GetChildren().size(), 2);
 
-  shared_ptr<AbstractItem> child = item.FindByID("btn2");
-  ButtonItem* btn = static_cast<ButtonItem*>(child.get());
-  ASSERT_EQ(btn->GetTitle(), "test2");
+  AbstractItem& child = item.FindByID("btn2");
+  ButtonItem& btn = static_cast<ButtonItem&>(child);
+  ASSERT_EQ(btn.GetTitle(), "test2");
+}
+
+TEST_F(GroupItemTest, FindByIDGroupItem) {
+  GroupItem item("GROUP1");
+  shared_ptr<GroupItem> gr2 = shared_ptr<GroupItem>(new GroupItem("GROUP2"));
+  shared_ptr<GroupItem> gr3 = shared_ptr<GroupItem>(new GroupItem("GROUP3"));
+  gr2->AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
+  gr2->AddChild(std::make_shared<ButtonItem>("btn3", "test3"));
+  gr2->AddChild(std::make_shared<ButtonItem>("btn4", "test4"));
+  gr3->AddChild(std::make_shared<ButtonItem>("btn6", "test6"));
+  gr2->AddChild(gr3);
+
+  item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
+  item.AddChild(gr2);
+  item.AddChild(std::make_shared<ButtonItem>("btn5", "test5"));
+  ASSERT_EQ(item.GetChildren().size(), 3);
+
+  AbstractItem& child = item.FindByID("btn3");
+  ButtonItem& btn = static_cast<ButtonItem&>(child);
+  ASSERT_EQ(btn.GetTitle(), "test3");
+
+  AbstractItem& child2 = item.FindByID("GROUP3");
+  GroupItem& ret_gr = static_cast<GroupItem&>(child2);
+  ASSERT_EQ(ret_gr.GetChildren().size(), 1);
+}
+
+
+TEST_F(GroupItemTest, IsItemTypeExist) {
+  GroupItem item("GROUP1");
+  shared_ptr<GroupItem> gr2 = shared_ptr<GroupItem>(new GroupItem("GROUP2"));
+  shared_ptr<GroupItem> gr3 = shared_ptr<GroupItem>(new GroupItem("GROUP3"));
+  gr2->AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
+  gr2->AddChild(std::make_shared<ButtonItem>("btn3", "test3"));
+  gr2->AddChild(std::make_shared<ButtonItem>("btn4", "test4"));
+  gr3->AddChild(std::make_shared<ButtonItem>("btn6", "test6"));
+  gr3->AddChild(std::make_shared<TextItem>("text1", "text1"));
+  gr2->AddChild(gr3);
+
+  item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
+  item.AddChild(gr2);
+  item.AddChild(std::make_shared<ButtonItem>("btn5", "test5"));
+  ASSERT_EQ(item.GetChildren().size(), 3);
+
+  ASSERT_EQ(item.IsItemTypeExist(AbstractItem::Text), true);
+  ASSERT_EQ(item.IsItemTypeExist(AbstractItem::Image), false);
+}
+
+TEST_F(GroupItemTest, FindByIDNullItemReturn) {
+  GroupItem item("GROUP1");
+  item.AddChild(std::make_shared<ButtonItem>("btn1", "test1"));
+  item.AddChild(std::make_shared<ButtonItem>("btn2", "test2"));
+  ASSERT_EQ(item.GetChildren().size(), 2);
+
+  AbstractItem& child = item.FindByID("not_exist_button");
+  ASSERT_EQ(child.GetType(), AbstractItem::NullObject);
 }
 
 int __fake_app_get_name(char** app_name) {
-  *app_name = (char*)"unittest_appname";
+  *app_name = strdup("unittest_appname");
   return 0;
 }
 
@@ -89,6 +146,14 @@ TEST_F(GroupItemTest, GetAppLabel) {
   ASSERT_EQ(app_label, "unittest_appname");
 }
 
+TEST_F(GroupItemTest, SetAppLabel) {
+  GroupItem item("GROUP1");
+  item.SetAppLabel("test");
+  string app_label = item.GetAppLabel();
+
+  ASSERT_EQ(app_label, "test");
+}
+
 TEST_F(GroupItemTest, SetDirection) {
   GroupItem item("GROUP1");
   item.SetDirection(true);