Upstream version 10.38.208.0
[platform/framework/web/crosswalk.git] / src / dbus / property_unittest.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "dbus/property.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/logging.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/threading/thread.h"
15 #include "base/threading/thread_restrictions.h"
16 #include "dbus/bus.h"
17 #include "dbus/object_path.h"
18 #include "dbus/object_proxy.h"
19 #include "dbus/test_service.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace dbus {
23
24 // The property test exerises the asynchronous APIs in PropertySet and
25 // Property<>.
26 class PropertyTest : public testing::Test {
27  public:
28   PropertyTest() {
29   }
30
31   struct Properties : public PropertySet {
32     Property<std::string> name;
33     Property<int16> version;
34     Property<std::vector<std::string> > methods;
35     Property<std::vector<ObjectPath> > objects;
36     Property<std::vector<uint8> > bytes;
37
38     Properties(ObjectProxy* object_proxy,
39                PropertyChangedCallback property_changed_callback)
40         : PropertySet(object_proxy,
41                       "org.chromium.TestInterface",
42                       property_changed_callback) {
43       RegisterProperty("Name", &name);
44       RegisterProperty("Version", &version);
45       RegisterProperty("Methods", &methods);
46       RegisterProperty("Objects", &objects);
47       RegisterProperty("Bytes", &bytes);
48     }
49   };
50
51   virtual void SetUp() {
52     // Make the main thread not to allow IO.
53     base::ThreadRestrictions::SetIOAllowed(false);
54
55     // Start the D-Bus thread.
56     dbus_thread_.reset(new base::Thread("D-Bus Thread"));
57     base::Thread::Options thread_options;
58     thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
59     ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
60
61     // Start the test service, using the D-Bus thread.
62     TestService::Options options;
63     options.dbus_task_runner = dbus_thread_->message_loop_proxy();
64     test_service_.reset(new TestService(options));
65     ASSERT_TRUE(test_service_->StartService());
66     ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
67     ASSERT_TRUE(test_service_->HasDBusThread());
68
69     // Create the client, using the D-Bus thread.
70     Bus::Options bus_options;
71     bus_options.bus_type = Bus::SESSION;
72     bus_options.connection_type = Bus::PRIVATE;
73     bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy();
74     bus_ = new Bus(bus_options);
75     object_proxy_ = bus_->GetObjectProxy(
76         "org.chromium.TestService",
77         ObjectPath("/org/chromium/TestObject"));
78     ASSERT_TRUE(bus_->HasDBusThread());
79
80     // Create the properties structure
81     properties_.reset(new Properties(
82         object_proxy_,
83         base::Bind(&PropertyTest::OnPropertyChanged,
84                    base::Unretained(this))));
85
86     // GetAll is called once the signals are connected.
87     properties_->ConnectSignals();
88   }
89
90   virtual void TearDown() {
91     bus_->ShutdownOnDBusThreadAndBlock();
92
93     // Shut down the service.
94     test_service_->ShutdownAndBlock();
95
96     // Reset to the default.
97     base::ThreadRestrictions::SetIOAllowed(true);
98
99     // Stopping a thread is considered an IO operation, so do this after
100     // allowing IO.
101     test_service_->Stop();
102   }
103
104   // Generic callback, bind with a string |id| for passing to
105   // WaitForCallback() to ensure the callback for the right method is
106   // waited for.
107   void PropertyCallback(const std::string& id, bool success) {
108     last_callback_ = id;
109     message_loop_.Quit();
110   }
111
112  protected:
113   // Called when a property value is updated.
114   void OnPropertyChanged(const std::string& name) {
115     updated_properties_.push_back(name);
116     message_loop_.Quit();
117   }
118
119   // Waits for the given number of updates.
120   void WaitForUpdates(size_t num_updates) {
121     while (updated_properties_.size() < num_updates)
122       message_loop_.Run();
123     for (size_t i = 0; i < num_updates; ++i)
124       updated_properties_.erase(updated_properties_.begin());
125   }
126
127   // Name, Version, Methods, Objects
128   static const int kExpectedSignalUpdates = 5;
129
130   // Waits for initial values to be set.
131   void WaitForGetAll() {
132     WaitForUpdates(kExpectedSignalUpdates);
133   }
134
135   // Waits for the callback. |id| is the string bound to the callback when
136   // the method call is made that identifies it and distinguishes from any
137   // other; you can set this to whatever you wish.
138   void WaitForCallback(const std::string& id) {
139     while (last_callback_ != id) {
140       message_loop_.Run();
141     }
142   }
143
144   base::MessageLoop message_loop_;
145   scoped_ptr<base::Thread> dbus_thread_;
146   scoped_refptr<Bus> bus_;
147   ObjectProxy* object_proxy_;
148   scoped_ptr<Properties> properties_;
149   scoped_ptr<TestService> test_service_;
150   // Properties updated.
151   std::vector<std::string> updated_properties_;
152   // Last callback received.
153   std::string last_callback_;
154 };
155
156 TEST_F(PropertyTest, InitialValues) {
157   WaitForGetAll();
158
159   EXPECT_EQ("TestService", properties_->name.value());
160   EXPECT_EQ(10, properties_->version.value());
161
162   std::vector<std::string> methods = properties_->methods.value();
163   ASSERT_EQ(4U, methods.size());
164   EXPECT_EQ("Echo", methods[0]);
165   EXPECT_EQ("SlowEcho", methods[1]);
166   EXPECT_EQ("AsyncEcho", methods[2]);
167   EXPECT_EQ("BrokenMethod", methods[3]);
168
169   std::vector<ObjectPath> objects = properties_->objects.value();
170   ASSERT_EQ(1U, objects.size());
171   EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
172
173   std::vector<uint8> bytes = properties_->bytes.value();
174   ASSERT_EQ(4U, bytes.size());
175   EXPECT_EQ('T', bytes[0]);
176   EXPECT_EQ('e', bytes[1]);
177   EXPECT_EQ('s', bytes[2]);
178   EXPECT_EQ('t', bytes[3]);
179 }
180
181 TEST_F(PropertyTest, UpdatedValues) {
182   WaitForGetAll();
183
184   // Update the value of the "Name" property, this value should not change.
185   properties_->name.Get(base::Bind(&PropertyTest::PropertyCallback,
186                                    base::Unretained(this),
187                                    "Name"));
188   WaitForCallback("Name");
189   WaitForUpdates(1);
190
191   EXPECT_EQ("TestService", properties_->name.value());
192
193   // Update the value of the "Version" property, this value should be changed.
194   properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback,
195                                       base::Unretained(this),
196                                       "Version"));
197   WaitForCallback("Version");
198   WaitForUpdates(1);
199
200   EXPECT_EQ(20, properties_->version.value());
201
202   // Update the value of the "Methods" property, this value should not change
203   // and should not grow to contain duplicate entries.
204   properties_->methods.Get(base::Bind(&PropertyTest::PropertyCallback,
205                                       base::Unretained(this),
206                                       "Methods"));
207   WaitForCallback("Methods");
208   WaitForUpdates(1);
209
210   std::vector<std::string> methods = properties_->methods.value();
211   ASSERT_EQ(4U, methods.size());
212   EXPECT_EQ("Echo", methods[0]);
213   EXPECT_EQ("SlowEcho", methods[1]);
214   EXPECT_EQ("AsyncEcho", methods[2]);
215   EXPECT_EQ("BrokenMethod", methods[3]);
216
217   // Update the value of the "Objects" property, this value should not change
218   // and should not grow to contain duplicate entries.
219   properties_->objects.Get(base::Bind(&PropertyTest::PropertyCallback,
220                                       base::Unretained(this),
221                                       "Objects"));
222   WaitForCallback("Objects");
223   WaitForUpdates(1);
224
225   std::vector<ObjectPath> objects = properties_->objects.value();
226   ASSERT_EQ(1U, objects.size());
227   EXPECT_EQ(ObjectPath("/TestObjectPath"), objects[0]);
228
229   // Update the value of the "Bytes" property, this value should not change
230   // and should not grow to contain duplicate entries.
231   properties_->bytes.Get(base::Bind(&PropertyTest::PropertyCallback,
232                                     base::Unretained(this),
233                                    "Bytes"));
234   WaitForCallback("Bytes");
235   WaitForUpdates(1);
236
237   std::vector<uint8> bytes = properties_->bytes.value();
238   ASSERT_EQ(4U, bytes.size());
239   EXPECT_EQ('T', bytes[0]);
240   EXPECT_EQ('e', bytes[1]);
241   EXPECT_EQ('s', bytes[2]);
242   EXPECT_EQ('t', bytes[3]);
243 }
244
245 TEST_F(PropertyTest, Get) {
246   WaitForGetAll();
247
248   // Ask for the new Version property.
249   properties_->version.Get(base::Bind(&PropertyTest::PropertyCallback,
250                                       base::Unretained(this),
251                                       "Get"));
252   WaitForCallback("Get");
253
254   // Make sure we got a property update too.
255   WaitForUpdates(1);
256
257   EXPECT_EQ(20, properties_->version.value());
258 }
259
260 TEST_F(PropertyTest, Set) {
261   WaitForGetAll();
262
263   // Set a new name.
264   properties_->name.Set("NewService",
265                         base::Bind(&PropertyTest::PropertyCallback,
266                                    base::Unretained(this),
267                                    "Set"));
268   WaitForCallback("Set");
269
270   // TestService sends a property update.
271   WaitForUpdates(1);
272
273   EXPECT_EQ("NewService", properties_->name.value());
274 }
275
276 }  // namespace dbus