Check main type validation 75/211975/4
authorJunghoon Park <jh9216.park@samsung.com>
Tue, 13 Aug 2019 04:54:23 +0000 (13:54 +0900)
committerJunghoon Park <jh9216.park@samsung.com>
Tue, 13 Aug 2019 09:38:55 +0000 (18:38 +0900)
Change-Id: If9833ecc37734ae57bd8b3877a3557b8b62fe966
Signed-off-by: Junghoon Park <jh9216.park@samsung.com>
notification-ex/abstract_item.cc
notification-ex/abstract_item_implementation.h
unittest/src/test_abstract_item.cc

index 61fc8cd..ff25fd5 100644 (file)
@@ -515,10 +515,40 @@ bool AbstractItem::GetOnGoingState() const {
   return impl_->ongoing_;
 }
 
+bool AbstractItem::Impl::IsValidMainType(AbstractItem& target, MainType type) {
+  int item_type = target.GetType();
+
+  switch (type) {
+    case MainTitle:
+    case MainContents:
+      if (item_type != Text)
+        return false;
+      break;
+    case MainIcon:
+      if (item_type != Image)
+        return false;
+      break;
+    case MainButton:
+      if (item_type != Button)
+        return false;
+      break;
+    default:
+      return false;
+  }
+
+  return true;
+}
+
 bool AbstractItem::SetMainType(std::string target_id, MainType type) {
   AbstractItem& target = FindByID(target_id);
   if (target.GetType() == NullObject)
     return false;
+  if (!impl_->IsValidMainType(target,type)) {
+    LOGE("Main type and item type are not matched (%d, %d)",
+        type, target.GetType());
+    return false;
+  }
+
   AbstractItem& old = FindByMainType(type);
   if (target.GetType() != NullObject)
     old.impl_->main_type_ = MainNone;
index 4e6b9d2..e6a4a64 100644 (file)
@@ -37,6 +37,7 @@ class AbstractItem::Impl {
       std::shared_ptr<AbstractAction> action = std::shared_ptr<AbstractAction>({}));
   Impl(AbstractItem* parent, std::string id,
       std::shared_ptr<AbstractAction> action = std::shared_ptr<AbstractAction>({}));
+  bool IsValidMainType(AbstractItem& target, MainType type);
 
  private:
   friend class AbstractItem;
index 89a5ed3..b922d85 100644 (file)
@@ -304,4 +304,13 @@ TEST_F(AbstractItemTest, SetGetFindMainType) {
   EXPECT_EQ(i.GetId(), "test_id");
 }
 
+TEST_F(AbstractItemTest, SetInvalidMainType) {
+  auto root = std::make_shared<GroupItem>("test_group");
+  auto item = std::make_shared<ButtonItem>("test_id", "title");
+  root->AddChild(item);
+
+  bool ret = root->SetMainType("test_id", AbstractItem::MainTitle);
+  EXPECT_FALSE(ret);
+}
+
 }  // namespace
\ No newline at end of file