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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <gtest/gtest.h>
#include <glib-object.h>
#include <UnityCore/GLibWrapper.h>
#include <UnityCore/GLibDBusProxy.h>

using namespace std;
using namespace unity;

namespace
{

GMainLoop* loop_ = NULL;
glib::DBusProxy* proxy = NULL;

class TestGDBusProxy: public ::testing::Test
{
public:
  TestGDBusProxy()
    : connected_result(false)
    , got_signal_return(false)
    , got_result_return(false)
  {
  }
  bool connected_result;
  bool got_signal_return;
  bool got_result_return;
};

TEST_F(TestGDBusProxy, TestConstruction)
{
  loop_ = g_main_loop_new(NULL, FALSE);
  proxy = new glib::DBusProxy("com.canonical.Unity.Test", 
                              "/com/canonical/gdbus_wrapper", 
                              "com.canonical.gdbus_wrapper");
  // performs a check on the proxy, if the proxy is connected, report a sucess
  auto timeout_check = [] (gpointer data) -> gboolean
  {
    TestGDBusProxy* self = static_cast<TestGDBusProxy*>(data);
    if (proxy->IsConnected())
    {
      self->connected_result = true;
      g_main_loop_quit(loop_);
      return FALSE;
    }
    else
    {
      self->connected_result = false;
      return TRUE;
    }
  };
  

  // if the proxy is not connected when this lambda runs, fail.
  auto timeout_bailout = [] (gpointer data) -> gboolean 
  {
    TestGDBusProxy* self = static_cast<TestGDBusProxy*>(data);
    // reached timeout, failed testing
    self->connected_result = false;
    g_main_loop_quit(loop_);
    return FALSE;
  };
  
  guint timeout_source = g_timeout_add(1000, timeout_check, this); // check once a second
  guint bailout_source = g_timeout_add(10000, timeout_bailout, this); // bail out after ten

  g_main_loop_run(loop_);
  g_source_remove(timeout_source);
  g_source_remove(bailout_source);
  
  EXPECT_EQ(connected_result, true);
}

TEST_F(TestGDBusProxy, TestMethodReturn)
{
  // Our service is setup so that if you call the TestMethod method, it will emit the TestSignal method
  // with whatever string you pass in
  gchar* expected_return = (gchar *)"TestStringTestString☻☻☻"; // cast to get gcc to shut up
  gchar* returned_result = g_strdup("Not equal"); 
  gchar* returned_signal = g_strdup("Not equal"); 

  GVariant* param_value = g_variant_new_string(expected_return);
  GVariant* parameters = g_variant_new_tuple(&param_value, 1);
  // signal callback
  auto signal_connection = [&](GVariant *variant)
  {
    if (variant != nullptr)
    {
      g_free(returned_signal);
      returned_signal = g_strdup(g_variant_get_string(g_variant_get_child_value(variant, 0), NULL));
    }

    got_signal_return = true;
    if (got_signal_return && got_result_return)
      g_main_loop_quit(loop_);
  };

  // method callback
  auto method_connection = [&](GVariant *variant)
  {
    if (variant != nullptr)
    {
      g_free(returned_result);
      returned_result = g_strdup(g_variant_get_string(g_variant_get_child_value(variant, 0), NULL));
    }

    got_result_return = true;
    if (got_signal_return && got_result_return)
      g_main_loop_quit(loop_);
  };
  
  auto timeout_bailout = [] (gpointer data) -> gboolean // bail out after 10 seconds
  {
    g_main_loop_quit(loop_);
    return FALSE;
  };
   
  guint bailout_source = g_timeout_add(10000, timeout_bailout, this);

  EXPECT_EQ(proxy->IsConnected(), true); // fail if we are not connected
  proxy->Connect("TestSignal", signal_connection);
  proxy->Call("TestMethod", parameters, method_connection); 

 
  // next check we get 30 entries from this specific known callback
  g_main_loop_run(loop_);

  EXPECT_EQ(g_strcmp0(expected_return, returned_result), 0);
  EXPECT_EQ(g_strcmp0(expected_return, returned_signal), 0);

  g_free(returned_result);
  g_free(returned_signal);
  g_source_remove(bailout_source);
}


}