From 8d09364c950a84e1aa8f4ae42b105d56bf7f6ee3 Mon Sep 17 00:00:00 2001 From: hyunho Date: Thu, 21 Feb 2019 19:13:24 +0900 Subject: [PATCH] Add progress item implementation Change-Id: Ibc606f47ab651d9bc5cfaf14ea286865651b1a2d Signed-off-by: hyunho --- notification-ex/item.h | 19 ----- notification-ex/item_factory.cc | 3 +- notification-ex/progress_item.cc | 100 +++++++++++++++++++++++++ notification-ex/progress_item.h | 55 ++++++++++++++ notification-ex/progress_item_implementation.h | 51 +++++++++++++ unittest/src/test_progress_item.cc | 38 ++++++++++ 6 files changed, 246 insertions(+), 20 deletions(-) create mode 100644 notification-ex/progress_item.cc create mode 100644 notification-ex/progress_item.h create mode 100644 notification-ex/progress_item_implementation.h create mode 100644 unittest/src/test_progress_item.cc diff --git a/notification-ex/item.h b/notification-ex/item.h index 72b4ff6..a48054e 100644 --- a/notification-ex/item.h +++ b/notification-ex/item.h @@ -92,25 +92,6 @@ class EXPORT_API ImageItem : public AbstractItem { std::string imagePath_ = nullptr; }; // class ImageItem -class EXPORT_API ProgressItem : public AbstractItem { - public: - ProgressItem(float min, float max, float current); - virtual ~ProgressItem(); - - Bundle Serialize() override; - void Deserialize(Bundle b) override; - AbstractItem& FindByID(std::string id) override; - float GetCurrent() const; - void SetCurrent(float current); - float GetMin() const; - float GetMax() const; - - private: - float min_ = 0.0; - float max_ = 0.0; - float current_ = 0.0; -}; // class ProgressItem - class EXPORT_API CheckBoxItem : public AbstractItem { public: CheckBoxItem(bool checked); diff --git a/notification-ex/item_factory.cc b/notification-ex/item_factory.cc index a90dddd..6d882f8 100644 --- a/notification-ex/item_factory.cc +++ b/notification-ex/item_factory.cc @@ -24,6 +24,7 @@ #include "notification-ex/null_item.h" #include "notification-ex/exception.h" #include "notification-ex/input_selector_item.h" +#include "notification-ex/progress_item.h" #ifdef LOG_TAG #undef LOG_TAG @@ -60,7 +61,7 @@ shared_ptr ItemFactory::CreateItem(AbstractItem::Type type) { case AbstractItem::Effect : return make_shared(""); case AbstractItem::Progress : - return make_shared(""); + return make_shared(0.0, 0.0, 0.0); case AbstractItem::Custom : return make_shared(""); } diff --git a/notification-ex/progress_item.cc b/notification-ex/progress_item.cc new file mode 100644 index 0000000..8868cfe --- /dev/null +++ b/notification-ex/progress_item.cc @@ -0,0 +1,100 @@ +/* + * 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 + +#include + +#include "notification-ex/exception.h" +#include "notification-ex/progress_item.h" +#include "notification-ex/progress_item_implementation.h" +#include "notification-ex/item_factory.h" + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_TAG "NOTIFICATION_EX" +#define PROGRESS_MIN_KEY "__PROGRESS_MIN_KEY__" +#define PROGRESS_CURRENT_KEY "__PROGRESS_CURRENT_KEY__" +#define PROGRESS_MAX_KEY "__PROGRESS_MAX_KEY__" + +using namespace std; +namespace notification { +namespace item { + +ProgressItem::ProgressItem(float min_val, float current, float max_val, + std::shared_ptr action) + : AbstractItem(AbstractItem::Progress, action), + impl_(new Impl(min_val, current, max_val, this)) { +} + +ProgressItem::ProgressItem(string id, float min_val, float current, float max_val, + std::shared_ptr action) + : AbstractItem(id, AbstractItem::Progress, action), + impl_(new Impl(min_val, current, max_val, this)) { +} + +ProgressItem::~ProgressItem() = default; +ProgressItem::Impl::~Impl() = default; + +ProgressItem::Impl::Impl(float min_val, float current, float max_val, ProgressItem* parent) + : min_(min_val), current_(current), max_(max_val), parent_(parent) { + if (min_val > current || max_val < current) + THROW(NOTIFICATION_ERROR_INVALID_PARAMETER); + LOGI("ProgressItem impl created"); +} + +Bundle ProgressItem::Serialize() { + Bundle b; + b = AbstractItem::Serialize(); + b.Add(PROGRESS_MIN_KEY, to_string(impl_->min_)); + b.Add(PROGRESS_CURRENT_KEY, to_string(impl_->current_)); + b.Add(PROGRESS_MAX_KEY, to_string(impl_->max_)); + return b; +} + +void ProgressItem::Deserialize(Bundle b) { + AbstractItem::Deserialize(b); + impl_->min_ = strtof(b.GetString(PROGRESS_MIN_KEY).c_str(), 0); + impl_->current_ = strtof(b.GetString(PROGRESS_CURRENT_KEY).c_str(), 0); + impl_->max_ = strtof(b.GetString(PROGRESS_MAX_KEY).c_str(), 0); +} + +AbstractItem& ProgressItem::FindByID(std::string id) { + if (GetId() == id) + return *this; + return ItemFactory::GetNullItem(); +} + +float ProgressItem::GetCurrent() const { + return impl_->current_; +} + +void ProgressItem::SetCurrent(float current) { + impl_->current_ = current; +} + +float ProgressItem::GetMin() const { + return impl_->min_; +} + +float ProgressItem::GetMax() const { + return impl_->max_; +} + +} // namespace item +} // namespace notification diff --git a/notification-ex/progress_item.h b/notification-ex/progress_item.h new file mode 100644 index 0000000..6060521 --- /dev/null +++ b/notification-ex/progress_item.h @@ -0,0 +1,55 @@ +/* + * 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_PROGRESS_ITEM_H_ +#define NOTIFICATION_EX_PROGRESS_ITEM_H_ + +#include +#include +#include + +#include "notification-ex/abstract_item.h" + +namespace notification { +namespace item { + +class EXPORT_API ProgressItem : public AbstractItem { + public: + ProgressItem(float min, float current, float max, + std::shared_ptr action = std::shared_ptr({})); + ProgressItem(std::string id, float min, float current, float max, + std::shared_ptr action = std::shared_ptr({})); + virtual ~ProgressItem(); + + public: + virtual Bundle Serialize() override; + virtual void Deserialize(Bundle b) override; + virtual AbstractItem& FindByID(std::string id) override; + + float GetCurrent() const; + void SetCurrent(float current); + float GetMin() const; + float GetMax() const; + + private: + class Impl; + std::unique_ptr impl_; +}; + +} // namespace item +} // namespace notification + +#endif // NOTIFICATION_EX_PROGRESS_ITEM_H_ diff --git a/notification-ex/progress_item_implementation.h b/notification-ex/progress_item_implementation.h new file mode 100644 index 0000000..865e96f --- /dev/null +++ b/notification-ex/progress_item_implementation.h @@ -0,0 +1,51 @@ + + +/* + * 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_INPUT_SELECTOR_ITEM_IMPLEMENTATION_H_ +#define NOTIFICATION_EX_INPUT_SELECTOR_ITEM_IMPLEMENTATION_H_ + +#include +#include +#include + +#include "notification-ex/progress_item.h" + +namespace notification { +namespace item { + +class ProgressItem::Impl { + public: + virtual ~Impl(); + + private: + Impl(float min_val, float current, float max_val, + ProgressItem* parent); + + private: + friend class ProgressItem; + + float min_; + float current_; + float max_; + ProgressItem* parent_; +}; + +} // namespace item +} // namespace notification + +#endif // NOTIFICATION_EX_INPUT_SELECTOR_ITEM_IMPLEMENTATION_H_ diff --git a/unittest/src/test_progress_item.cc b/unittest/src/test_progress_item.cc new file mode 100644 index 0000000..435ffac --- /dev/null +++ b/unittest/src/test_progress_item.cc @@ -0,0 +1,38 @@ +// Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved +// Use of this source code is governed by a apache 2.0 license that can be +// found in the LICENSE file. + +#include + +#include "notification-ex/progress_item.h" +#include "notification-ex/item_inflator.h" + +using namespace notification::item; +using namespace std; + +namespace { + +class ProgressItemTest : public ::testing::Test { + protected: + void SetUp() override {} + void TearDown() override {} +}; + +TEST_F(ProgressItemTest, SerializeDeserializeGetTitle) { + ProgressItem item(1.0, 10.0, 100.0); + Bundle b = item.Serialize(); + ItemFactory factory; + shared_ptr gen_item = ItemInflator::Create(factory, b); + ASSERT_EQ(gen_item.get()->GetType(), item.GetType()); + + ProgressItem* gen_progress = static_cast(gen_item.get()); + ASSERT_EQ(item.GetCurrent(), gen_progress->GetCurrent()); + ASSERT_EQ(item.GetMin(), gen_progress->GetMin()); + ASSERT_EQ(item.GetMax(), gen_progress->GetMax()); + + ASSERT_EQ(gen_progress->GetMin(), 1.0); + ASSERT_EQ(gen_progress->GetCurrent(), 10.0); + ASSERT_EQ(gen_progress->GetMax(), 100.0); +} + +} // namespace -- 2.7.4