Back to index

unity  6.0.0
test_main_dbus.cpp
Go to the documentation of this file.
00001 #include <gtest/gtest.h>
00002 #include <gio/gio.h>
00003 #include <NuxCore/Logger.h>
00004 #include <Nux/Nux.h>
00005 
00006 static bool wait_until_test_service_appears();
00007 static void tell_service_to_exit();
00008 
00009 int main(int argc, char** argv)
00010 {
00011   ::testing::InitGoogleTest(&argc, argv);
00012   g_type_init();
00013   
00014   nux::NuxInitialize (0);
00015 
00016   // We need the service to be ready before we are
00017   if (!wait_until_test_service_appears())
00018   {
00019     std::cerr << "FATAL: Unable to connect to test service";
00020     return EXIT_FAILURE;
00021   }
00022 
00023   // Slightly higher as we're more likely to test things we know will fail
00024   nux::logging::configure_logging("<root>=error");
00025 
00026   // but you can still change it if you're debugging ;)
00027   nux::logging::configure_logging(::getenv("UNITY_LOG_SEVERITY"));
00028 
00029   int ret = RUN_ALL_TESTS();
00030 
00031   tell_service_to_exit();
00032 
00033   return ret;
00034 }
00035 
00036 static bool wait_until_test_service_appears()
00037 {
00038   bool have_name = false;
00039   bool timeout_reached = false;
00040 
00041   auto timeout_cb = [](gpointer data) -> gboolean
00042   {
00043     *(bool*)data = true;
00044     return FALSE;
00045   };
00046 
00047   auto callback = [](GDBusConnection * conn,
00048                      const char * name,
00049                      const char * name_owner,
00050                      gpointer user_data)
00051   {
00052     *(bool*)user_data = true;
00053   };
00054 
00055   g_bus_watch_name(G_BUS_TYPE_SESSION,
00056                    "com.canonical.Unity.Test",
00057                    G_BUS_NAME_WATCHER_FLAGS_NONE,
00058                    callback,
00059                    NULL,
00060                    &have_name,
00061                    NULL);
00062   g_timeout_add(10000, timeout_cb, &timeout_reached);
00063 
00064   while (!have_name && !timeout_reached)
00065     g_main_context_iteration(g_main_context_get_thread_default(), TRUE);
00066 
00067   return (have_name && !timeout_reached);
00068 }
00069 
00070 static void tell_service_to_exit()
00071 {
00072   // Ask the service to exit
00073   GDBusConnection* connection = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
00074   g_dbus_connection_call_sync(connection,
00075                               "com.canonical.Unity.Test",
00076                               "/com/canonical/unity/test/controller",
00077                               "com.canonical.Unity.Test",
00078                               "Exit",
00079                               NULL,
00080                               NULL,
00081                               G_DBUS_CALL_FLAGS_NONE,
00082                               -1,
00083                               NULL, NULL);
00084   g_object_unref(connection);
00085 }