Add progress item implementation 11/200311/1
authorhyunho <hhstark.kang@samsung.com>
Thu, 21 Feb 2019 10:13:24 +0000 (19:13 +0900)
committerhyunho <hhstark.kang@samsung.com>
Thu, 21 Feb 2019 10:13:24 +0000 (19:13 +0900)
Change-Id: Ibc606f47ab651d9bc5cfaf14ea286865651b1a2d
Signed-off-by: hyunho <hhstark.kang@samsung.com>
notification-ex/item.h
notification-ex/item_factory.cc
notification-ex/progress_item.cc [new file with mode: 0644]
notification-ex/progress_item.h [new file with mode: 0644]
notification-ex/progress_item_implementation.h [new file with mode: 0644]
unittest/src/test_progress_item.cc [new file with mode: 0644]

index 72b4ff6..a48054e 100644 (file)
@@ -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);
index a90dddd..6d882f8 100644 (file)
@@ -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<AbstractItem> ItemFactory::CreateItem(AbstractItem::Type type) {
   case AbstractItem::Effect :
     return make_shared<ButtonItem>("");
   case AbstractItem::Progress :
-    return make_shared<ButtonItem>("");
+    return make_shared<ProgressItem>(0.0, 0.0, 0.0);
   case AbstractItem::Custom :
     return make_shared<ButtonItem>("");
   }
diff --git a/notification-ex/progress_item.cc b/notification-ex/progress_item.cc
new file mode 100644 (file)
index 0000000..8868cfe
--- /dev/null
@@ -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 <dlog.h>
+
+#include <memory>
+
+#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<AbstractAction> 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<AbstractAction> 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 (file)
index 0000000..6060521
--- /dev/null
@@ -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 <string>
+#include <memory>
+#include <list>
+
+#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<AbstractAction> action = std::shared_ptr<AbstractAction>({}));
+  ProgressItem(std::string id, float min, float current, float max,
+      std::shared_ptr<AbstractAction> action = std::shared_ptr<AbstractAction>({}));
+  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> 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 (file)
index 0000000..865e96f
--- /dev/null
@@ -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 <string>
+#include <memory>
+#include <list>
+
+#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 (file)
index 0000000..435ffac
--- /dev/null
@@ -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 <gmock/gmock.h>
+
+#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<AbstractItem> gen_item = ItemInflator::Create(factory, b);
+  ASSERT_EQ(gen_item.get()->GetType(), item.GetType());
+
+  ProgressItem* gen_progress = static_cast<ProgressItem*>(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