Fix disable/uninstall recovery logic bug 50/191050/4
authorhyunho <hhstark.kang@samsung.com>
Thu, 11 Oct 2018 04:55:53 +0000 (13:55 +0900)
committerhyunho <hhstark.kang@samsung.com>
Fri, 12 Oct 2018 01:48:23 +0000 (10:48 +0900)
Change-Id: I7126784e610513e30aa1f0259419fcc8d2b6cb9a
Signed-off-by: hyunho <hhstark.kang@samsung.com>
unittest/src/test-complication.cc
watchface-complication/complication-implementation.h
watchface-complication/complication.cc

index a393ecc..a24e800 100644 (file)
@@ -32,6 +32,7 @@ using namespace std;
 using namespace watchface_complication;
 
 GMainLoop *mainloop = NULL;
+unsigned int loop_timer = 0;
 class WatchComplication : public Complication {
  public:
   WatchComplication(int id, int supported_types, int supported_event_types,
@@ -43,13 +44,21 @@ class WatchComplication : public Complication {
   void OnProviderError(const std::string& provider_id,
       ComplicationType type, int error) override {
     cout << "provider error : " << provider_id << "(" << error << ")" << endl;
-    g_main_loop_quit(mainloop);
+    if (loop_timer == 0) {
+      loop_timer = g_timeout_add(1000, [](gpointer user_data)->gboolean {
+            g_main_loop_quit(mainloop);
+            loop_timer = 0;
+            return G_SOURCE_REMOVE;
+          }, this);
+    }
+
     if (error == WATCHFACE_COMPLICATION_ERROR_PROVIDER_NOT_AVAILABLE)
       not_available_ = true;
   }
   virtual ~WatchComplication() = default;
 
  public:
+  unsigned int event_timer_ = 0;
   bool not_available_ = false;
 };
 
@@ -81,6 +90,7 @@ class WC : public ::testing::Test {
   }
   virtual void TearDown() {
     delete complication;
+    delete complication2;
     g_main_loop_unref(mainloop);
     mainloop = NULL;
   }
@@ -160,11 +170,12 @@ TEST_F(WC, Disabled)
 {
   std::list<std::unique_ptr<Bundle>> const& list = complication2->GetCandidates();
   for (unsigned int i = 0; i < list.size(); i++) {
-    complication2->SetCurDataIdx(i);
-    if (strcmp(complication2->GetCurProviderId(), "org.tizen.gmock_comp_provider/test") == 0)
+    complication->SetCurDataIdx(i);
+    if (strcmp(complication->GetCurProviderId(), "org.tizen.gmock_comp_provider/test") == 0)
       break;
   }
   WC::complication->not_available_ = false;
+  complication->UpdateLastData();
   RunMainLoop();
   EXPECT_EQ(WC::complication->not_available_, true);
 }
@@ -174,13 +185,10 @@ TEST_F(WC, DisabledRecovery)
   std::list<std::unique_ptr<Bundle>> const& list = complication2->GetCandidates();
   for (unsigned int i = 0; i < list.size(); i++) {
     complication2->SetCurDataIdx(i);
-    if (strcmp(complication2->GetCurProviderId(), WC::providerId.c_str()) == 0) {
-      RunMainLoop();
+    if (strcmp(complication2->GetCurProviderId(), WC::providerId.c_str()) == 0)
       break;
-    }
   }
-
-  cout << "??" << complication2->GetCurProviderId() << endl;
+  RunMainLoop();
   EXPECT_EQ(strcmp(complication2->GetCurProviderId(), WC::providerId2.c_str()), 0);
 }
 
index 15232be..d2794d1 100644 (file)
@@ -83,6 +83,7 @@ class Complication::Impl : IGDBus::IGDBusEvent, IPackageManager::IPackageEvent {
                     const std::string& sender_name);
   void UpdatedProcess(GVariant* parameters);
   void NotifyDataUpdateProcess(GVariant* parameters);
+  int UpdateCurProvider(std::string new_provider_id, ComplicationType type);
 
  private:
   Complication* parent_;
index 3040dee..24c76cb 100644 (file)
@@ -84,54 +84,27 @@ int Complication::Impl::LoadCurProviderFromPrev() {
 
   LOGI("get setting from bundle %s, %s", prev_provider_id, prev_provider_type);
 
-  std::string appid = DBManager::GetProviderAppId(prev_provider_id);
-  if (appid.empty() || !DBManager::IsProviderExist(prev_provider_id_str,
-          strtol((const char*)prev_provider_type, NULL, 10))) {
-    LOGE("Fail to get provider appid");
-    return WATCHFACE_COMPLICATION_ERROR_PROVIDER_NOT_AVAILABLE;
-  }
-  cur_provider_appid_ = appid;
-
-  int ret = CheckNotSupported(prev_provider_id_str);
+  int ret = UpdateCurProvider(prev_provider_id_str, static_cast<ComplicationType>(
+    strtol((const char*)prev_provider_type, NULL, 10)));
   if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
     return ret;
 
-  cur_provider_id_ = prev_provider_id_str;
-  cur_type_ = static_cast<ComplicationType>(
-    strtol((const char*)prev_provider_type, NULL, 10));
   LOGI("Successfully get previous provider info");
   return WATCHFACE_COMPLICATION_ERROR_NONE;
 }
 
 int Complication::Impl::LoadCurProviderFromDefault() {
-  cur_type_ = default_type_;
-  cur_provider_id_ = default_provider_id_;
-
-  if (!DBManager::IsProviderExist(default_provider_id_,
-          static_cast<int>(default_type_))) {
-    LOGE("Default provider do not exist (%s)(%d)",
-        default_provider_id_.c_str(), default_type_);
-    return WATCHFACE_COMPLICATION_ERROR_PROVIDER_NOT_AVAILABLE;
-  }
-
-  int ret = CheckNotSupported(default_provider_id_);
+  int ret = UpdateCurProvider(default_provider_id_, default_type_);
   if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
     return ret;
 
-  default_provider_appid_ =
-          DBManager::GetProviderAppId(default_provider_id_.c_str());
-  if (default_provider_appid_.empty()) {
-    LOGE("fail to get appid");
-    return WATCHFACE_COMPLICATION_ERROR_PROVIDER_NOT_AVAILABLE;
-  }
-
   LOGI("Successfully get default provider info");
   return WATCHFACE_COMPLICATION_ERROR_NONE;
 }
 
 int Complication::Init() {
   int ret = impl_->LoadCurProviderFromPrev();
-  if (ret == WATCHFACE_COMPLICATION_ERROR_PROVIDER_NOT_AVAILABLE) {
+  if (ret != WATCHFACE_COMPLICATION_ERROR_NONE) {
     ret = impl_->LoadCurProviderFromDefault();
     if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
       LOGE("cannot load default provider info (%d)", ret);
@@ -145,6 +118,13 @@ int Complication::Init() {
 
   impl_->package_.get()->Watch(impl_.get());
 
+  impl_->default_provider_appid_ =
+          DBManager::GetProviderAppId(impl_->default_provider_id_.c_str());
+  if (impl_->default_provider_appid_.empty()) {
+    LOGE("fail to get appid");
+    return WATCHFACE_COMPLICATION_ERROR_PROVIDER_NOT_AVAILABLE;
+  }
+
   return impl_->UpdateProviderInfo();
 }
 
@@ -504,6 +484,29 @@ void Complication::Impl::UpdateLastDataFields() {
   last_data_idx_ = cur_data_idx_;
 }
 
+int Complication::Impl::UpdateCurProvider(std::string new_provider_id,
+    ComplicationType type) {
+  if (new_provider_id.empty()) {
+    LOGE("empty provider id");
+    return WATCHFACE_COMPLICATION_ERROR_PROVIDER_NOT_AVAILABLE;
+  }
+
+  std::string appid = DBManager::GetProviderAppId(new_provider_id.c_str());
+  if (appid.empty() || !DBManager::IsProviderExist(new_provider_id, (int)type)) {
+    LOGE("Fail to get provider from DB");
+    return WATCHFACE_COMPLICATION_ERROR_PROVIDER_NOT_AVAILABLE;
+  }
+
+  int ret = CheckNotSupported(new_provider_id);
+  if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+    return ret;
+
+  cur_type_ = type;
+  cur_provider_id_ = new_provider_id;
+  cur_provider_appid_ = appid;
+  return WATCHFACE_COMPLICATION_ERROR_NONE;
+}
+
 /*
 Store current provider info in DB & update periodic timer
 */
@@ -536,9 +539,9 @@ int Complication::SetCurDataIdx(int cur_data_idx) {
   if (cur_data_idx < 0 ||
     cur_data_idx >= static_cast<int>(impl_->candidates_list_.size())) {
     LOGE("Invalid index");
-    impl_->cur_data_idx_ = -1;
-    impl_->cur_provider_id_ = impl_->last_provider_id_;
-    impl_->cur_type_ = impl_->last_type_;
+    ret = impl_->UpdateCurProvider(impl_->last_provider_id_, impl_->last_type_);
+    if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+      return ret;
   } else {
     const Bundle* data = GetNthData(cur_data_idx);
     if (data == NULL) {
@@ -549,13 +552,18 @@ int Complication::SetCurDataIdx(int cur_data_idx) {
         impl_->provider_id_key_.c_str(), &provider_id);
     bundle_get_str(const_cast<Bundle*>(data)->GetRaw(),
         impl_->provider_type_key_.c_str(), &type);
-    if (provider_id != NULL)
-      impl_->cur_provider_id_ = std::string(provider_id);
-    if (type != NULL)
-      impl_->cur_type_ = static_cast<ComplicationType>(strtol(type, NULL, 10));
+
+    if (provider_id == NULL || type == NULL) {
+      LOGI("GetCurData failed");
+      return WATCHFACE_COMPLICATION_ERROR_NO_DATA;
+    }
+    ret = impl_->UpdateCurProvider(std::string(provider_id),
+        static_cast<ComplicationType>(strtol(type, NULL, 10)));
+    if (ret != WATCHFACE_COMPLICATION_ERROR_NONE)
+      return ret;
+    impl_->cur_data_idx_ = cur_data_idx;
   }
 
-  impl_->cur_data_idx_ = cur_data_idx;
   LOGI("cur idx %d, cur provider %s, cur type %d", impl_->cur_data_idx_,
     impl_->cur_provider_id_.c_str(), impl_->cur_type_);
   impl_->gdbus_.get()->UnSubscribeSignal(impl_->subscribe_id_);