Send signal when current theme changed 68/237168/5
authorSangyoon Jang <jeremy.jang@samsung.com>
Thu, 25 Jun 2020 11:26:48 +0000 (20:26 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Fri, 26 Jun 2020 09:37:41 +0000 (18:37 +0900)
Send serialized theme which is changed theme when current theme changed.
Add test for theme changed event.

Change-Id: I5bb068445cad8cfb7f7fd7fe8bf488aebd9af43a
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/theme/loader/theme_info_loader.cc
src/theme_provider/control_request_handler.cc
test/unit_tests/test_request_handlers.cc

index c223402..c3bfb47 100644 (file)
@@ -30,13 +30,11 @@ tizen_base::Bundle ThemeInfoLoader::OnRequest(dbus::Command cmd,
     const tizen_base::Bundle& args) {
   switch (cmd) {
     case dbus::Command::CHANGED: {
-      tizen_base::Bundle b(args.GetString(dbus::kCmdDataKey));
-      const ThemeInfo* info = new ThemeInfo(b);
+      ThemeInfo info(args);
       for (auto& listener : listeners_) {
         std::shared_ptr<IThemeEvent> ev = listener.second;
-        ev->OnThemeChanged(info, args);
+        ev->OnThemeChanged(&info, args);
       }
-      delete info;
       break;
     }
     default: {
index 7b3c7fd..ee105e8 100644 (file)
@@ -7,6 +7,8 @@
 #include <bundle_cpp.h>
 
 #include "theme/dbus/command.h"
+#include "theme/dbus/request_broker.h"
+#include "theme/utils/logging.h"
 #include "theme_provider/theme_info_proxy.h"
 
 namespace ttm {
@@ -21,7 +23,21 @@ tizen_base::Bundle ControlRequestHandler::OnRequest(dbus::Command cmd,
     return b;
   }
   bool r = proxy_->SetCurrentTheme(args.GetString(dbus::kCmdDataKey));
-  b.Add(dbus::kCmdResultKey, r ? "ok" : "error");
+  if (!r) {
+    b.Add(dbus::kCmdResultKey, "error");
+    return b;
+  }
+
+  std::shared_ptr<loader::ThemeInfo> theme = proxy_->GetLoadedTheme();
+  if (!theme) {
+    LOG(ERROR) << "Failed to get loaded theme";
+  } else {
+    if (!dbus::RequestBroker::GetInst().
+        EmitSignal("changed", dbus::Command::CHANGED, theme->Serialize()))
+      LOG(ERROR) << "Failed to send themd changed signal";
+  }
+
+  b.Add(dbus::kCmdResultKey, "ok");
   return b;
 }
 
index 8cd687d..d20ae2d 100644 (file)
  */
 
 #include <bundle_cpp.h>
+#include <glib.h>
+#include <gmock/gmock.h>
 #include <gtest/gtest.h>
 
 #include <cstdio>
 #include <memory>
 
 #include "theme/dbus/command.h"
+#include "theme/dbus/request_broker.h"
 #include "theme/dbus/request_handler.h"
+#include "theme/loader/theme_info_loader.h"
 #include "theme_provider/control_request_handler.h"
 #include "theme_provider/package_event_request_handler.h"
 #include "theme_provider/selection_request_handler.h"
 #include "theme_provider/theme_info_proxy.h"
+#include "unit_tests/mock/gio_mock.h"
+#include "unit_tests/mock/test_fixture.h"
 
+using ::testing::_;
+using ::testing::Return;
+using ::testing::Invoke;
+using ttm::loader::IThemeEvent;
 using ttm::loader::ThemeInfo;
+using ttm::loader::ThemeInfoLoader;
 using ttm::provider::ThemeInfoProxy;
 
 namespace {
 
 const char kTestDb[] = "request_handler_test.db";
 
+class Timer {
+ public:
+  explicit Timer(int timeout_msec)
+      : loop_(nullptr), timeout_(timeout_msec) {}
+  void WaitForResult() {
+    loop_ = g_main_loop_new(nullptr, FALSE);
+    g_timeout_add(timeout_,
+        [](gpointer user_data) -> gboolean {
+          Timer* t = static_cast<Timer*>(user_data);
+          g_main_loop_quit(t->loop_);
+          return FALSE;
+        }, this);
+    g_main_loop_run(loop_);
+    g_main_loop_unref(loop_);
+  }
+
+  GMainLoop* loop_;
+  int timeout_;
+};
+
 }  // namespace
 
-class RequestHandlersTest : public testing::Test {
+class Mocks : public ::testing::NiceMock<GioMock> {};
+
+class RequestHandlersTest : public TestFixture {
  public:
+  RequestHandlersTest() : TestFixture(std::make_unique<Mocks>()) {}
   virtual ~RequestHandlersTest() {}
 
   virtual void SetUp() {
@@ -60,7 +94,53 @@ class RequestHandlersTest : public testing::Test {
   tizen_base::Bundle b_;
 };
 
+class TestEventMock : public IThemeEvent {
+ public:
+  MOCK_METHOD2(OnThemeChanged, void(const ThemeInfo*,
+      const tizen_base::Bundle& args));
+};
+
 TEST_F(RequestHandlersTest, ControlRequestHandler_OnRequest) {
+  auto loader = std::make_shared<ThemeInfoLoader>();
+  auto event = std::make_shared<TestEventMock>();
+
+  EXPECT_CALL(*event, OnThemeChanged(_, _));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_emit_signal(_, _, _, _, _, _, _)).
+          WillOnce(Return(true));
+  EXPECT_CALL(GetMock<GioMock>(),
+      g_dbus_connection_signal_subscribe(_, _, _, _, _, _, _, _, _, _)).
+          WillOnce(Invoke(
+              [this](GDBusConnection* arg0, const gchar* arg1,
+                    const gchar* arg2, const gchar* arg3, const gchar* arg4,
+                    const gchar* arg5, GDBusSignalFlags arg6,
+                    GDBusSignalCallback arg7, gpointer arg8,
+                    GDestroyNotify arg9) -> guint {
+                g_timeout_add(1000,
+                    [](gpointer user_data) -> gboolean {
+                      GDBusSignalCallback cb =
+                          reinterpret_cast<GDBusSignalCallback>(user_data);
+                      tizen_base::Bundle b;
+                      b.Add("id", "testid");
+                      b.Add("version", "1.0");
+                      b.Add("tool_version", "1.1");
+                      b.Add("title", "Test");
+                      b.Add("resolution", "360X360");
+                      b.Add("preview", "shared/res/preview.png");
+                      b.Add("description", "test");
+                      GVariant* gv = g_variant_new("(is)",
+                          static_cast<int>(ttm::dbus::Command::CHANGED),
+                          reinterpret_cast<char*>(b.ToRaw().first.get()));
+                      cb(nullptr, nullptr, nullptr, nullptr, nullptr, gv,
+                          &ttm::dbus::RequestBroker::GetInst());
+                      g_variant_unref(gv);
+                      return FALSE;
+                    }, reinterpret_cast<gpointer>(arg7));
+                return 1;
+              }));
+
+  std::string key = loader->AddEvent(event);
+
   using ttm::provider::ControlRequestHandler;
   ControlRequestHandler handler(proxy_);
 
@@ -74,6 +154,11 @@ TEST_F(RequestHandlersTest, ControlRequestHandler_OnRequest) {
   auto cur = proxy_->GetLoadedTheme();
   EXPECT_NE(cur, nullptr);
   EXPECT_EQ(cur->GetId(), "testid");
+
+  Timer timer(2000);
+  timer.WaitForResult();
+
+  loader->RemoveEvent(key);
 }
 
 TEST_F(RequestHandlersTest, PackageRequestHandler_OnRequest) {