namespace notification {
namespace item {
-AbstractItem::AbstractItem()
- : impl_(new Impl(this)) {
-}
-
AbstractItem::AbstractItem(AbstractItem::Type type,
std::shared_ptr<AbstractAction> action)
: impl_(new Impl(this, type, action)) {
LOGI("GroupItem created");
}
-AbstractItem::Impl::Impl(AbstractItem* parent)
- : parent_(parent) {
- LOGI("GroupItem created");
-}
-
AbstractItem::~AbstractItem() = default;
Bundle AbstractItem::Serialize() {
class EXPORT_API AbstractItem {
public:
enum Type {
+ NullObject,
Text,
Image,
Icon,
virtual Bundle Serialize() = 0;
virtual void Deserialize(Bundle b) = 0;
- virtual std::shared_ptr<AbstractItem> FindByID(std::string id) = 0;
+ virtual AbstractItem& FindByID(std::string id) = 0;
std::string GetId() const;
void SetId(std::string id);
std::shared_ptr<AbstractAction> GetAction() const;
#include <memory>
#include "notification-ex/button_item.h"
+#include "notification-ex/item_factory.h"
#ifdef LOG_TAG
#undef LOG_TAG
title_ = b.GetString(BUTTON_TITLE_KEY);
}
-shared_ptr<AbstractItem> ButtonItem::FindByID(std::string id) {
+AbstractItem& ButtonItem::FindByID(std::string id) {
if (GetId() == id)
- return shared_ptr<AbstractItem>(this);
- return nullptr;
+ return *this;
+ return ItemFactory::GetNullItem();
}
std::string ButtonItem::GetTitle() const {
Bundle Serialize() override;
void Deserialize(Bundle b) override;
- std::shared_ptr<AbstractItem> FindByID(std::string id) override;
+ AbstractItem& FindByID(std::string id) override;
std::string GetTitle() const;
private:
Bundle serialized = i.get()->Serialize();
serialized.Add(
GROUP_CHILDREN_TYPE_KEY, to_string((int)i.get()->GetType()));
- char* str = strdup(reinterpret_cast<char*>(serialized.ToRaw().first.get()));
- arr.push_back(str);
+ arr.push_back(reinterpret_cast<char*>(serialized.ToRaw().first.get()));
}
b.Add(GROUP_CHILDREN_KEY, arr);
}
}
-shared_ptr<AbstractItem> GroupItem::FindByID(std::string id) {
+AbstractItem& GroupItem::FindByID(std::string id) {
for (auto& i : impl_->children_list_) {
if (i.get()->GetId() == id)
- return i;
+ return *i;
}
- return nullptr;
+ return ItemFactory::GetNullItem();
}
void GroupItem::AddChild(shared_ptr<AbstractItem> child) {
public:
virtual Bundle Serialize() override;
virtual void Deserialize(Bundle b) override;
- virtual std::shared_ptr<AbstractItem> FindByID(std::string id) override;
+ virtual AbstractItem& FindByID(std::string id) override;
void SetDirection(bool vertical);
bool IsVertical();
Bundle Serialize() override;
void Deserialize(Bundle b) override;
- std::shared_ptr<AbstractItem> FindByID(std::string id) override;
+ AbstractItem& FindByID(std::string id) override;
void SetContents(std::string contents);
std::string GetContents() const;
std::string GetHyperLink() const;
Bundle Serialize() override;
void Deserialize(Bundle b) override;
- std::shared_ptr<AbstractItem> FindByID(std::string id) override;
+ AbstractItem& FindByID(std::string id) override;
private:
std::string iconPath_ = nullptr;
Bundle Serialize() override;
void Deserialize(Bundle b) override;
- std::shared_ptr<AbstractItem> FindByID(std::string id) override;
+ AbstractItem& FindByID(std::string id) override;
IconItem GetIconItem() const;
TextItem GetTextItem() const;
Bundle Serialize() override;
void Deserialize(Bundle b) override;
- std::shared_ptr<AbstractItem> FindByID(std::string id) override;
+ AbstractItem& FindByID(std::string id) override;
std::string GetImagePath() const;
private:
Bundle Serialize() override;
void Deserialize(Bundle b) override;
- std::shared_ptr<AbstractItem> FindByID(std::string id) override;
+ AbstractItem& FindByID(std::string id) override;
float GetCurrent() const;
void SetCurrent(float current);
float GetMin() const;
Bundle Serialize() override;
void Deserialize(Bundle b) override;
- std::shared_ptr<AbstractItem> FindByID(std::string id) override;
+ AbstractItem& FindByID(std::string id) override;
bool IsChecked() const;
private:
Bundle Serialize() override;
void Deserialize(Bundle b) override;
- std::shared_ptr<AbstractItem> FindByID(std::string id) override;
+ AbstractItem& FindByID(std::string id) override;
TextItem GetTextItem() const;
TextItem GetDataItem() const;
time_t GetTimeItem() const;
Bundle Serialize() override;
void Deserialize(Bundle b) override;
- std::shared_ptr<AbstractItem> FindByID(std::string id) override;
+ AbstractItem& FindByID(std::string id) override;
std::list<std::string> GetContents() const;
void SetContents(std::list<std::string> contents);
Bundle Serialize() override;
void Deserialize(Bundle b) override;
- std::shared_ptr<AbstractItem> FindByID(std::string id) override;
+ AbstractItem& FindByID(std::string id) override;
std::string GetText() const;
void SetText(std::string text);
void SetTextLimit(int size);
Bundle Serialize() override;
void Deserialize(Bundle b) override;
- std::shared_ptr<AbstractItem> FindByID(std::string id) override;
+ AbstractItem& FindByID(std::string id) override;
std::string GetSoundPath() const;
std::string GetVibrationPath() const;
Bundle Serialize() override;
void Deserialize(Bundle b) override;
- std::shared_ptr<AbstractItem> FindByID(std::string id) override;
+ AbstractItem& FindByID(std::string id) override;
}; // class CustomItem
} // namespace item
#include "notification-ex/item_factory.h"
#include "notification-ex/button_item.h"
#include "notification-ex/group_item.h"
+#include "notification-ex/null_item.h"
+#include "notification-ex/exception.h"
#ifdef LOG_TAG
#undef LOG_TAG
shared_ptr<AbstractItem> ItemFactory::CreateItem(AbstractItem::Type type) {
switch (type) {
+ case AbstractItem::NullObject :
+ THROW(NOTIFICATION_ERROR_INVALID_PARAMETER);
case AbstractItem::Text :
return make_shared<ButtonItem>("");
case AbstractItem::Icon :
return nullptr;
}
+AbstractItem& ItemFactory::GetNullItem() {
+ static NullItem item;
+ return item;
+}
} // namespace item
} // namespace notification
class EXPORT_API ItemFactory {
public:
std::shared_ptr<AbstractItem> CreateItem(AbstractItem::Type type);
+ static AbstractItem& GetNullItem();
};
} // namespace item
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <dlog.h>
+
+#include <memory>
+
+#include "notification-ex/null_item.h"
+
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+
+#define LOG_TAG "NOTIFICATION_EX"
+
+using namespace std;
+namespace notification {
+namespace item {
+
+NullItem::NullItem(shared_ptr<AbstractAction> action)
+ : AbstractItem(AbstractItem::NullObject, action) {
+}
+
+NullItem::NullItem(string id,
+ shared_ptr<AbstractAction> action)
+ : AbstractItem(id, AbstractItem::NullObject, action) {
+}
+NullItem::~NullItem() = default;
+
+Bundle NullItem::Serialize() {
+ Bundle b;
+ b = AbstractItem::Serialize();
+ return b;
+}
+
+void NullItem::Deserialize(Bundle b) {
+ AbstractItem::Deserialize(b);
+}
+
+AbstractItem& NullItem::FindByID(std::string id) {
+ return *this;
+}
+
+} // namespace item
+} // namespace notification
--- /dev/null
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef NOTIFICATION_EX_NULL_ITEM_H_
+#define NOTIFICATION_EX_NULL_ITEM_H_
+
+#include <string>
+#include <memory>
+#include <list>
+
+#include "notification-ex/abstract_item.h"
+
+namespace notification {
+namespace item {
+
+class EXPORT_API NullItem : public AbstractItem {
+ public:
+ NullItem(std::shared_ptr<AbstractAction> action = std::shared_ptr<AbstractAction>({}));
+ NullItem(std::string id,
+ std::shared_ptr<AbstractAction> action = std::shared_ptr<AbstractAction>({}));
+ virtual ~NullItem();
+
+ public:
+ virtual Bundle Serialize() override;
+ virtual void Deserialize(Bundle b) override;
+ virtual AbstractItem& FindByID(std::string id) override;
+};
+
+} // namespace item
+} // namespace notification
+
+#endif // NOTIFICATION_EX_NULL_ITEM_H_
void TearDown() override {}
};
+
+TEST_F(ButtonItemTest, FindByID) {
+ ButtonItem item("btn_id", "title");
+
+ AbstractItem& child = item.FindByID("btn_id");
+ ButtonItem& btn = static_cast<ButtonItem&>(child);
+ ASSERT_EQ(btn.GetTitle(), "title");
+}
+
+TEST_F(ButtonItemTest, FindByIDNullItemReturn) {
+ ButtonItem item("btn_id", "title");
+
+ AbstractItem& child = item.FindByID("not_exist_button");
+ ASSERT_EQ(child.GetType(), AbstractItem::NullObject);
+}
+
TEST_F(ButtonItemTest, SerializeDeserializeGetTitle) {
ButtonItem item("title");
Bundle b = item.Serialize();
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, 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) {