~macslow/unity/unity.remote-add

« back to all changes in this revision

Viewing changes to tests/test_gdbus_proxy.cpp

  • Committer: Gord Allott
  • Date: 2012-02-21 15:17:18 UTC
  • mto: This revision was merged to the branch mainline in revision 2000.
  • Revision ID: gord.allott@canonical.com-20120221151718-lalghyc5wr33ye70
adds a gdbus test and service

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <gtest/gtest.h>
 
2
#include <glib-object.h>
 
3
#include <UnityCore/GLibWrapper.h>
 
4
#include <UnityCore/GLibDBusProxy.h>
 
5
 
 
6
using namespace std;
 
7
using namespace unity;
 
8
 
 
9
namespace
 
10
{
 
11
 
 
12
GMainLoop* loop_ = g_main_loop_new(NULL, FALSE);
 
13
glib::DBusProxy proxy("com.canonical.Unity.Test", 
 
14
                      "/com/canonical/gdbus_wrapper", 
 
15
                      "com.canonical.gdbus_wrapper");
 
16
 
 
17
class TestGDBusProxy: public ::testing::Test
 
18
{
 
19
public:
 
20
  TestGDBusProxy()
 
21
    : connected_result(false)
 
22
  {
 
23
  }
 
24
  bool connected_result;
 
25
};
 
26
 
 
27
TEST_F(TestGDBusProxy, TestConstruction)
 
28
{
 
29
  
 
30
  // performs a check on the proxy, if the proxy is connected, report a sucess
 
31
  auto timeout_check = [] (gpointer data) -> gboolean
 
32
  {
 
33
    TestGDBusProxy* self = static_cast<TestGDBusProxy*>(data);
 
34
    if (proxy.IsConnected())
 
35
    {
 
36
      self->connected_result = true;
 
37
      g_main_loop_quit(loop_);
 
38
      return FALSE;
 
39
    }
 
40
    else
 
41
    {
 
42
      self->connected_result = false;
 
43
      return TRUE;
 
44
    }
 
45
  };
 
46
  
 
47
 
 
48
  // if the proxy is not connected when this lambda runs, fail.
 
49
  auto timeout_bailout = [] (gpointer data) -> gboolean 
 
50
  {
 
51
    TestGDBusProxy* self = static_cast<TestGDBusProxy*>(data);
 
52
    // reached timeout, failed testing
 
53
    self->connected_result = false;
 
54
    g_main_loop_quit(loop_);
 
55
    return FALSE;
 
56
  };
 
57
  
 
58
  g_timeout_add_seconds(1, timeout_check, this); // check once a second
 
59
  g_timeout_add_seconds(10, timeout_bailout, this); // bail out after ten
 
60
 
 
61
  g_main_loop_run(loop_);
 
62
  
 
63
  EXPECT_EQ(connected_result, true);
 
64
}
 
65
 
 
66
TEST_F(TestGDBusProxy, TestMethodReturn)
 
67
{
 
68
  glib::String expected_return((gchar *)"TestStringTestString☻☻☻"); // cast to get gcc to shut up
 
69
  gchar* returned_result = (gchar*)"Not equal"; // cast to get gcc to shut up
 
70
  gchar* returned_signal = (gchar*)"Not equal"; // cast to get gcc to shut up
 
71
 
 
72
  GVariant* param_value = g_variant_new_string(expected_return);
 
73
  GVariant* parameters = g_variant_new_tuple(&param_value, 1);
 
74
  // signal callback
 
75
  auto signal_connection = [&](GVariant *variant)
 
76
  {
 
77
    if (variant != nullptr)
 
78
    {
 
79
      returned_signal = (gchar*)g_variant_get_string(g_variant_get_child_value(variant, 0), NULL);
 
80
    }
 
81
  };
 
82
 
 
83
  // method callback
 
84
  auto method_connection = [&](GVariant *variant)
 
85
  {
 
86
    if (variant != nullptr)
 
87
    {
 
88
      returned_result = (gchar*)g_variant_get_string(g_variant_get_child_value(variant, 0), NULL);
 
89
    }
 
90
  };
 
91
  
 
92
  auto timeout_bailout = [] (gpointer data) -> gboolean // bail out after 10 seconds
 
93
  {
 
94
    g_main_loop_quit(loop_);
 
95
    return FALSE;
 
96
  };
 
97
   
 
98
  g_timeout_add_seconds(10, timeout_bailout, this);
 
99
 
 
100
  EXPECT_EQ(proxy.IsConnected(), true); // fail if we are not connected
 
101
  proxy.Connect("TestSignal", signal_connection);
 
102
  proxy.Call("TestMethod", parameters, method_connection); 
 
103
 
 
104
 
 
105
  // next check we get 30 entries from this specific known callback
 
106
  g_main_loop_run(loop_);
 
107
 
 
108
  EXPECT_EQ(g_strcmp0(expected_return, returned_result), 0);
 
109
  EXPECT_EQ(g_strcmp0(expected_return, returned_signal), 0);
 
110
}
 
111
 
 
112
 
 
113
}