From ccbd6f31f6a258d72a41e37674aff639a7a3732a Mon Sep 17 00:00:00 2001 From: Junghoon Park Date: Tue, 13 Aug 2019 13:54:23 +0900 Subject: [PATCH] Check main type validation Change-Id: If9833ecc37734ae57bd8b3877a3557b8b62fe966 Signed-off-by: Junghoon Park --- notification-ex/abstract_item.cc | 30 ++++++++++++++++++++++++++ notification-ex/abstract_item_implementation.h | 1 + unittest/src/test_abstract_item.cc | 9 ++++++++ 3 files changed, 40 insertions(+) diff --git a/notification-ex/abstract_item.cc b/notification-ex/abstract_item.cc index 61fc8cd..ff25fd5 100644 --- a/notification-ex/abstract_item.cc +++ b/notification-ex/abstract_item.cc @@ -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; diff --git a/notification-ex/abstract_item_implementation.h b/notification-ex/abstract_item_implementation.h index 4e6b9d2..e6a4a64 100644 --- a/notification-ex/abstract_item_implementation.h +++ b/notification-ex/abstract_item_implementation.h @@ -37,6 +37,7 @@ class AbstractItem::Impl { std::shared_ptr action = std::shared_ptr({})); Impl(AbstractItem* parent, std::string id, std::shared_ptr action = std::shared_ptr({})); + bool IsValidMainType(AbstractItem& target, MainType type); private: friend class AbstractItem; diff --git a/unittest/src/test_abstract_item.cc b/unittest/src/test_abstract_item.cc index 89a5ed3..b922d85 100644 --- a/unittest/src/test_abstract_item.cc +++ b/unittest/src/test_abstract_item.cc @@ -304,4 +304,13 @@ TEST_F(AbstractItemTest, SetGetFindMainType) { EXPECT_EQ(i.GetId(), "test_id"); } +TEST_F(AbstractItemTest, SetInvalidMainType) { + auto root = std::make_shared("test_group"); + auto item = std::make_shared("test_id", "title"); + root->AddChild(item); + + bool ret = root->SetMainType("test_id", AbstractItem::MainTitle); + EXPECT_FALSE(ret); +} + } // namespace \ No newline at end of file -- 2.7.4