~ubuntu-branches/ubuntu/quantal/unity/quantal

55.1500.1 by Gord Allott
adds a gdbus test and service
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
55.1500.2 by Gord Allott
fix up the tests so they pass
12
GMainLoop* loop_ = NULL;
13
glib::DBusProxy* proxy = NULL;
55.1500.1 by Gord Allott
adds a gdbus test and service
14
15
class TestGDBusProxy: public ::testing::Test
16
{
17
public:
18
  TestGDBusProxy()
19
    : connected_result(false)
55.1500.2 by Gord Allott
fix up the tests so they pass
20
    , got_signal_return(false)
21
    , got_result_return(false)
55.1500.1 by Gord Allott
adds a gdbus test and service
22
  {
23
  }
24
  bool connected_result;
55.1500.2 by Gord Allott
fix up the tests so they pass
25
  bool got_signal_return;
26
  bool got_result_return;
55.1500.1 by Gord Allott
adds a gdbus test and service
27
};
28
29
TEST_F(TestGDBusProxy, TestConstruction)
30
{
55.1500.2 by Gord Allott
fix up the tests so they pass
31
  loop_ = g_main_loop_new(NULL, FALSE);
32
  proxy = new glib::DBusProxy("com.canonical.Unity.Test", 
33
                              "/com/canonical/gdbus_wrapper", 
34
                              "com.canonical.gdbus_wrapper");
55.1500.1 by Gord Allott
adds a gdbus test and service
35
  // performs a check on the proxy, if the proxy is connected, report a sucess
36
  auto timeout_check = [] (gpointer data) -> gboolean
37
  {
38
    TestGDBusProxy* self = static_cast<TestGDBusProxy*>(data);
55.1500.2 by Gord Allott
fix up the tests so they pass
39
    if (proxy->IsConnected())
55.1500.1 by Gord Allott
adds a gdbus test and service
40
    {
41
      self->connected_result = true;
42
      g_main_loop_quit(loop_);
43
      return FALSE;
44
    }
45
    else
46
    {
47
      self->connected_result = false;
48
      return TRUE;
49
    }
50
  };
51
  
52
53
  // if the proxy is not connected when this lambda runs, fail.
54
  auto timeout_bailout = [] (gpointer data) -> gboolean 
55
  {
56
    TestGDBusProxy* self = static_cast<TestGDBusProxy*>(data);
57
    // reached timeout, failed testing
58
    self->connected_result = false;
59
    g_main_loop_quit(loop_);
60
    return FALSE;
61
  };
62
  
55.1586.1 by Michal Hruby
Safer tests
63
  guint timeout_source = g_timeout_add(1000, timeout_check, this); // check once a second
64
  guint bailout_source = g_timeout_add(10000, timeout_bailout, this); // bail out after ten
55.1500.1 by Gord Allott
adds a gdbus test and service
65
66
  g_main_loop_run(loop_);
55.1500.4 by Gord Allott
and the other sources
67
  g_source_remove(timeout_source);
68
  g_source_remove(bailout_source);
55.1500.1 by Gord Allott
adds a gdbus test and service
69
  
70
  EXPECT_EQ(connected_result, true);
71
}
72
73
TEST_F(TestGDBusProxy, TestMethodReturn)
74
{
55.1500.2 by Gord Allott
fix up the tests so they pass
75
  // Our service is setup so that if you call the TestMethod method, it will emit the TestSignal method
76
  // with whatever string you pass in
77
  gchar* expected_return = (gchar *)"TestStringTestString☻☻☻"; // cast to get gcc to shut up
78
  gchar* returned_result = g_strdup("Not equal"); 
79
  gchar* returned_signal = g_strdup("Not equal"); 
55.1500.1 by Gord Allott
adds a gdbus test and service
80
81
  GVariant* param_value = g_variant_new_string(expected_return);
82
  GVariant* parameters = g_variant_new_tuple(&param_value, 1);
83
  // signal callback
84
  auto signal_connection = [&](GVariant *variant)
85
  {
86
    if (variant != nullptr)
87
    {
55.1500.2 by Gord Allott
fix up the tests so they pass
88
      g_free(returned_signal);
89
      returned_signal = g_strdup(g_variant_get_string(g_variant_get_child_value(variant, 0), NULL));
55.1500.1 by Gord Allott
adds a gdbus test and service
90
    }
55.1500.2 by Gord Allott
fix up the tests so they pass
91
92
    got_signal_return = true;
93
    if (got_signal_return && got_result_return)
94
      g_main_loop_quit(loop_);
55.1500.1 by Gord Allott
adds a gdbus test and service
95
  };
96
97
  // method callback
98
  auto method_connection = [&](GVariant *variant)
99
  {
100
    if (variant != nullptr)
101
    {
55.1500.2 by Gord Allott
fix up the tests so they pass
102
      g_free(returned_result);
103
      returned_result = g_strdup(g_variant_get_string(g_variant_get_child_value(variant, 0), NULL));
55.1500.1 by Gord Allott
adds a gdbus test and service
104
    }
55.1500.2 by Gord Allott
fix up the tests so they pass
105
106
    got_result_return = true;
107
    if (got_signal_return && got_result_return)
108
      g_main_loop_quit(loop_);
55.1500.1 by Gord Allott
adds a gdbus test and service
109
  };
110
  
111
  auto timeout_bailout = [] (gpointer data) -> gboolean // bail out after 10 seconds
112
  {
113
    g_main_loop_quit(loop_);
114
    return FALSE;
115
  };
116
   
55.1586.1 by Michal Hruby
Safer tests
117
  guint bailout_source = g_timeout_add(10000, timeout_bailout, this);
55.1500.1 by Gord Allott
adds a gdbus test and service
118
55.1500.2 by Gord Allott
fix up the tests so they pass
119
  EXPECT_EQ(proxy->IsConnected(), true); // fail if we are not connected
120
  proxy->Connect("TestSignal", signal_connection);
121
  proxy->Call("TestMethod", parameters, method_connection); 
55.1500.1 by Gord Allott
adds a gdbus test and service
122
123
 
124
  // next check we get 30 entries from this specific known callback
125
  g_main_loop_run(loop_);
126
127
  EXPECT_EQ(g_strcmp0(expected_return, returned_result), 0);
128
  EXPECT_EQ(g_strcmp0(expected_return, returned_signal), 0);
55.1500.2 by Gord Allott
fix up the tests so they pass
129
130
  g_free(returned_result);
131
  g_free(returned_signal);
55.1500.3 by Gord Allott
remove the source, oops
132
  g_source_remove(bailout_source);
55.1500.1 by Gord Allott
adds a gdbus test and service
133
}
134
135
136
}