Improve coverage and add notification unittest
[platform/core/api/notification.git] / notification-ex / manager.cc
index a408dd7..3c483fa 100644 (file)
@@ -27,6 +27,8 @@
 #include "notification-ex/dbus_connection_manager.h"
 #include "notification-ex/ex_util.h"
 #include "notification-ex/item_info_internal.h"
+#include "notification-ex/null_item.h"
+#include "notification-ex/exception.h"
 
 #ifdef LOG_TAG
 #undef LOG_TAG
 
 #define MAX_PACKAGE_STR_SIZE 512
 #define NOTIFICATION_EX_MANAGER_OBJECT_PATH "/org/tizen/notification_ex_manager"
+#define DATA_PROVIDER_MASTER_ID "data-provider-master"
 
 using namespace std;
+using namespace tizen_base;
 using namespace notification::item;
 namespace notification {
 
+/* LCOV_EXCL_START */
 Manager::Manager(unique_ptr<IEventSender> sender,
     unique_ptr<IEventListener> listener, string receiver_group)
   : impl_(new Impl(this, move(sender), move(listener), receiver_group)) {
@@ -49,6 +54,8 @@ Manager::~Manager() = default;
 
 Manager::Impl::~Impl() {
   listener_->UnRegisterObserver(parent_);
+  EventInfo info(EventInfo::Unregister, util::GetAppId(), "", "");
+  sender_->RequestReturnValue(info);
 }
 Manager::Impl::Impl(Manager* parent,
     unique_ptr<IEventSender> sender,
@@ -58,17 +65,12 @@ Manager::Impl::Impl(Manager* parent,
     parent_(parent) {
   LOGI("impl created");
   listener_->RegisterObserver(parent_);
-}
 
-int Manager::Impl::SendNotify(shared_ptr<item::AbstractItem> noti,
-    IEventInfo::EventType type) {
-  Bundle serialized = noti->Serialize();
-  EventInfo info(type, util::GetAppId(), noti->GetChannel());
-  list<Bundle> serialized_list {serialized};
+  if (DBusConnectionManager::GetInst().IsDataProviderMaster(util::GetAppId()))
+    return;
 
-  /* Reply to Sender */
-  sender_->Notify(info, serialized_list, noti->GetSenderAppId());
-  return info.GetRequestId();
+  EventInfo info(EventInfo::Register, util::GetAppId(), receiver_group, "");
+  sender_->RequestReturnValue(info);
 }
 
 void Manager::SendError(const IEventInfo& info, NotificationError error) {
@@ -80,53 +82,83 @@ void Manager::SendError(const IEventInfo& info, NotificationError error) {
 }
 
 int Manager::Update(shared_ptr<item::AbstractItem> noti) {
-  return impl_->SendNotify(noti, EventInfo::Update);
+  Bundle serialized = noti->Serialize();
+  EventInfo info(
+      EventInfo::Update, util::GetAppId(), noti->GetChannel(), noti->GetId());
+  list<Bundle> serialized_list {serialized};
+  impl_->sender_->SendMessage(
+      info, serialized_list, noti->GetSenderAppId());
+  return info.GetRequestId();
 }
 
 int Manager::Delete(shared_ptr<item::AbstractItem> noti) {
-  return impl_->SendNotify(noti, EventInfo::Delete);
+  Bundle serialized = noti->Serialize();
+  EventInfo info(
+      EventInfo::Delete, util::GetAppId(), noti->GetChannel(), noti->GetId());
+  list<Bundle> serialized_list {serialized};
+  impl_->sender_->SendMessage(
+      info, serialized_list, noti->GetSenderAppId());
+  return info.GetRequestId();
 }
 
 int Manager::DeleteAll() {
-  //TODO
-  return -1;
+  EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "");
+  impl_->sender_->RequestReturnValue(info);
+  return info.GetRequestId();
+}
+
+int Manager::DeleteByChannel(string channel) {
+  EventInfo info(EventInfo::DeleteAll, util::GetAppId(), channel);
+  impl_->sender_->RequestReturnValue(info);
+  return info.GetRequestId();
+}
+
+int Manager::DeleteByAppId(string appId) {
+  EventInfo info(EventInfo::DeleteAll, util::GetAppId(), "", appId);
+  impl_->sender_->RequestReturnValue(info);
+  return info.GetRequestId();
 }
 
 int Manager::GetCount() const {
-  //TODO
-  return -1;
+  EventInfo info(EventInfo::Count, util::GetAppId(), "");
+  return impl_->sender_->RequestReturnValue(info);
 }
 
 int Manager::Hide(shared_ptr<item::AbstractItem> noti) {
-  ((IItemInfoInternal*)noti->GetInfo().get())->AddHideViewer(util::GetAppId());
-  return impl_->SendNotify(noti, EventInfo::Update);
+  (reinterpret_cast<IItemInfoInternal*>(noti->GetInfo().get()))
+      ->AddHideViewer(util::GetAppId());
+
+  EventInfo info(
+      EventInfo::Update, util::GetAppId(), noti->GetChannel(), noti->GetId());
+  Bundle serialized = noti->Serialize();
+  list<Bundle> serialized_list {serialized};
+  impl_->sender_->SendMessage(
+      info, serialized_list, noti->GetSenderAppId());
+  return info.GetRequestId();
 }
 
-shared_ptr<item::AbstractItem> Manager::FindByRootID(string id) {
+unique_ptr<item::AbstractItem> Manager::FindByRootID(string id) {
   EventInfo info(EventInfo::Get, util::GetAppId(), "", id);
   list<Bundle> result = impl_->sender_->Request(info);
-  if (result.size() == 0) {
-    LOGE("Fail to get noti");
-    return shared_ptr<item::AbstractItem>({});
-  }
   Bundle b = result.front();
-  shared_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
+  unique_ptr<AbstractItem> gen_item = ItemInflator::Create(b);
   return gen_item;
 }
 
-list<shared_ptr<item::AbstractItem>> Manager::Get() {
-  EventInfo info(EventInfo::Get, util::GetAppId(), "");
+list<unique_ptr<item::AbstractItem>> Manager::Get(string channel) {
+  EventInfo info(EventInfo::Get, util::GetAppId(), channel);
   list<Bundle> result = impl_->sender_->Request(info);
-  list<shared_ptr<item::AbstractItem>> gen_list;
+  list<unique_ptr<item::AbstractItem>> gen_list;
   for (auto& i : result) {
-    shared_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
-    gen_list.emplace_back(gen_item);
+    unique_ptr<AbstractItem> gen_item = ItemInflator::Create(i);
+    gen_list.push_back(move(gen_item));
   }
   return gen_list;
 }
 
 int Manager::SendEvent(const IEventInfo& info,
     shared_ptr<item::AbstractItem> noti) {
+  LOGI("manager-sendevent");
   Bundle serialized = noti->Serialize();
   Bundle serialized_info = info.Serialize();
   list<Bundle> serialized_list {serialized};
@@ -134,16 +166,33 @@ int Manager::SendEvent(const IEventInfo& info,
   return info.GetRequestId();
 }
 
+int Manager::SendEvent(const IEventInfo& info,
+    list<shared_ptr<item::AbstractItem>> notiList) {
+  LOGI("manager-sendevent - list");
+  list<Bundle> serialized_list;
+  for (auto& i : notiList) {
+    Bundle b = i->Serialize();
+    serialized_list.push_back(b);
+  }
+  impl_->sender_->Notify(info, serialized_list);
+  return info.GetRequestId();
+}
+
 list<Bundle> Manager::OnRequest(const IEventInfo& info) {
   list<shared_ptr<item::AbstractItem>> item_list = OnRequestEvent(info);
   list<Bundle> serialized_list;
   for (auto& i : item_list) {
-    if (i->CanReceive(impl_->receiver_group_))
+    if (std::static_pointer_cast<IItemInfoInternal>(i->GetInfo())
+          ->CanReceive(impl_->receiver_group_))
       serialized_list.push_back(i->Serialize());
   }
   return serialized_list;
 }
 
+int Manager::OnRequestNumber(const IEventInfo& info) {
+  return OnRequestNumberEvent(info);
+}
+
 void Manager::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
   shared_ptr<AbstractItem> gen_item;
   int type = info.GetEventType();
@@ -155,12 +204,13 @@ void Manager::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
     return;
   }
 
-  switch(type) {
+  switch (type) {
     case EventInfo::Post: {
       list<shared_ptr<item::AbstractItem>> added;
       for (auto& i : serialized) {
         gen_item = ItemInflator::Create(i);
-        if (gen_item->CanReceive(impl_->receiver_group_))
+        if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
+              ->CanReceive(impl_->receiver_group_))
           added.emplace_back(gen_item);
       }
       if (added.size() > 0)
@@ -168,51 +218,73 @@ void Manager::OnEvent(const IEventInfo& info, list<Bundle> serialized) {
       break;
     }
     case EventInfo::Update: {
+      list<shared_ptr<item::AbstractItem>> updated;
       for (auto& i : serialized) {
         gen_item = ItemInflator::Create(i);
-        if (gen_item->CanReceive(impl_->receiver_group_))
-          OnUpdate(info, gen_item);
+        if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
+              ->CanReceive(impl_->receiver_group_))
+          updated.emplace_back(gen_item);
       }
+      if (updated.size() > 0)
+        OnUpdate(info, updated);
       break;
     }
     case EventInfo::Delete: {
+      list<shared_ptr<item::AbstractItem>> deleted;
       for (auto& i : serialized) {
         gen_item = ItemInflator::Create(i);
-        if (gen_item->CanReceive(impl_->receiver_group_))
-          OnDelete(info, gen_item);
+        if (std::static_pointer_cast<IItemInfoInternal>(gen_item->GetInfo())
+              ->CanReceive(impl_->receiver_group_))
+          deleted.emplace_back(gen_item);
       }
+      if (deleted.size() > 0)
+        OnDelete(info, deleted);
       break;
     }
     case EventInfo::Get:
       break;
     case EventInfo::Error:
       break;
+    case EventInfo::Count:
+      break;
+    case EventInfo::DeleteAll: {
+      list<shared_ptr<item::AbstractItem>> deleted;
+      deleted.emplace_back(new NullItem());
+      OnDelete(info, deleted);
+      break;
+    }
     case EventInfo::Custom:
       break;
   }
 }
 
 void Manager::OnAdd(const IEventInfo& info,
-      list<shared_ptr<item::AbstractItem>> addedItem) {
+      list<shared_ptr<item::AbstractItem>> addedList) {
 }
 
 void Manager::OnUpdate(const IEventInfo& info,
-      shared_ptr<item::AbstractItem> updatedItem) {
+      list<shared_ptr<item::AbstractItem>> updatedList) {
 }
 
 void Manager::OnDelete(const IEventInfo& info,
-      shared_ptr<item::AbstractItem> deletedItem) {
+      list<shared_ptr<item::AbstractItem>> deletedList) {
 }
 
 void Manager::OnError(NotificationError error, int requestId) {
 }
 
-list<shared_ptr<item::AbstractItem>> Manager::OnRequestEvent(const IEventInfo& info) {
+list<shared_ptr<item::AbstractItem>> Manager::OnRequestEvent(
+    const IEventInfo& info) {
   return list<shared_ptr<item::AbstractItem>>({});
 }
 
+int Manager::OnRequestNumberEvent(const IEventInfo& info) {
+  return 0;
+}
+
 string Manager::GetPath() {
   return NOTIFICATION_EX_MANAGER_OBJECT_PATH;
 }
+/* LCOV_EXCL_STOP */
 
 }  // namespace notification