Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: dbus/bus_unittest.cc

Issue 15741025: Linux/CrOS: Retry connecting to mtpd. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: address nits Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « dbus/bus.cc ('k') | device/media_transfer_protocol/media_transfer_protocol_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "dbus/bus.h" 5 #include "dbus/bus.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/run_loop.h"
10 #include "base/threading/thread.h" 11 #include "base/threading/thread.h"
11 #include "dbus/exported_object.h" 12 #include "dbus/exported_object.h"
12 #include "dbus/object_path.h" 13 #include "dbus/object_path.h"
13 #include "dbus/object_proxy.h" 14 #include "dbus/object_proxy.h"
14 #include "dbus/scoped_dbus_error.h" 15 #include "dbus/scoped_dbus_error.h"
16 #include "dbus/test_service.h"
15 17
16 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
17 19
18 namespace { 20 namespace {
19 21
20 // Used to test AddFilterFunction(). 22 // Used to test AddFilterFunction().
21 DBusHandlerResult DummyHandler(DBusConnection* connection, 23 DBusHandlerResult DummyHandler(DBusConnection* connection,
22 DBusMessage* raw_message, 24 DBusMessage* raw_message,
23 void* user_data) { 25 void* user_data) {
24 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; 26 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
25 } 27 }
26 28
29 // Test helper for BusTest.ListenForServiceOwnerChange that wraps a
30 // base::RunLoop. At Run() time, the caller pass in the expected number of
31 // quit calls, and at QuitIfConditionIsSatisified() time, only quit the RunLoop
32 // if the expected number of quit calls have been reached.
33 class RunLoopWithExpectedCount {
34 public:
35 RunLoopWithExpectedCount() : expected_quit_calls_(0), actual_quit_calls_(0) {}
36 ~RunLoopWithExpectedCount() {}
37
38 void Run(int expected_quit_calls) {
39 DCHECK_EQ(0, expected_quit_calls_);
40 DCHECK_EQ(0, actual_quit_calls_);
41 expected_quit_calls_ = expected_quit_calls;
42 run_loop_.reset(new base::RunLoop());
43 run_loop_->Run();
44 }
45
46 void QuitIfConditionIsSatisified() {
47 if (++actual_quit_calls_ != expected_quit_calls_)
48 return;
49 run_loop_->Quit();
50 expected_quit_calls_ = 0;
51 actual_quit_calls_ = 0;
52 }
53
54 private:
55 scoped_ptr<base::RunLoop> run_loop_;
56 int expected_quit_calls_;
57 int actual_quit_calls_;
58
59 DISALLOW_COPY_AND_ASSIGN(RunLoopWithExpectedCount);
60 };
61
62 // Test helper for BusTest.ListenForServiceOwnerChange.
63 void OnServiceOwnerChanged(RunLoopWithExpectedCount* run_loop_state,
64 std::string* service_owner,
65 int* num_of_owner_changes,
66 const std::string& new_service_owner) {
67 *service_owner = new_service_owner;
68 ++(*num_of_owner_changes);
69 run_loop_state->QuitIfConditionIsSatisified();
70 }
71
27 } // namespace 72 } // namespace
28 73
29 TEST(BusTest, GetObjectProxy) { 74 TEST(BusTest, GetObjectProxy) {
30 dbus::Bus::Options options; 75 dbus::Bus::Options options;
31 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options); 76 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options);
32 77
33 dbus::ObjectProxy* object_proxy1 = 78 dbus::ObjectProxy* object_proxy1 =
34 bus->GetObjectProxy("org.chromium.TestService", 79 bus->GetObjectProxy("org.chromium.TestService",
35 dbus::ObjectPath("/org/chromium/TestObject")); 80 dbus::ObjectPath("/org/chromium/TestObject"));
36 ASSERT_TRUE(object_proxy1); 81 ASSERT_TRUE(object_proxy1);
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 error.get())); 334 error.get()));
290 ASSERT_FALSE(error.is_set()); 335 ASSERT_FALSE(error.is_set());
291 336
292 // A third attemp to remove the same rule should fail. 337 // A third attemp to remove the same rule should fail.
293 ASSERT_FALSE(bus->RemoveMatch( 338 ASSERT_FALSE(bus->RemoveMatch(
294 "type='signal',interface='org.chromium.TestService',path='/'", 339 "type='signal',interface='org.chromium.TestService',path='/'",
295 error.get())); 340 error.get()));
296 341
297 bus->ShutdownAndBlock(); 342 bus->ShutdownAndBlock();
298 } 343 }
344
345 TEST(BusTest, ListenForServiceOwnerChange) {
346 // Setup the current thread's MessageLoop. Must be of TYPE_IO for the
347 // listeners to work.
348 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO);
349 RunLoopWithExpectedCount run_loop_state;
350
351 // Create the bus.
352 dbus::Bus::Options bus_options;
353 scoped_refptr<dbus::Bus> bus = new dbus::Bus(bus_options);
354
355 // Add a listener.
356 std::string service_owner1;
357 int num_of_owner_changes1 = 0;
358 dbus::Bus::GetServiceOwnerCallback callback1 =
359 base::Bind(&OnServiceOwnerChanged,
360 &run_loop_state,
361 &service_owner1,
362 &num_of_owner_changes1);
363 bus->ListenForServiceOwnerChange("org.chromium.TestService", callback1);
364 // This should be a no-op.
365 bus->ListenForServiceOwnerChange("org.chromium.TestService", callback1);
366 base::RunLoop().RunUntilIdle();
367
368 // Nothing has happened yet. Check initial state.
369 EXPECT_TRUE(service_owner1.empty());
370 EXPECT_EQ(0, num_of_owner_changes1);
371
372 // Make an ownership change.
373 ASSERT_TRUE(bus->RequestOwnershipAndBlock("org.chromium.TestService"));
374 run_loop_state.Run(1);
375
376 {
377 // Get the current service owner and check to make sure the listener got
378 // the right value.
379 std::string current_service_owner =
380 bus->GetServiceOwnerAndBlock("org.chromium.TestService",
381 dbus::Bus::REPORT_ERRORS);
382 ASSERT_FALSE(current_service_owner.empty());
383
384 // Make sure the listener heard about the new owner.
385 EXPECT_EQ(current_service_owner, service_owner1);
386
387 // Test the second ListenForServiceOwnerChange() above is indeed a no-op.
388 EXPECT_EQ(1, num_of_owner_changes1);
389 }
390
391 // Add a second listener.
392 std::string service_owner2;
393 int num_of_owner_changes2 = 0;
394 dbus::Bus::GetServiceOwnerCallback callback2 =
395 base::Bind(&OnServiceOwnerChanged,
396 &run_loop_state,
397 &service_owner2,
398 &num_of_owner_changes2);
399 bus->ListenForServiceOwnerChange("org.chromium.TestService", callback2);
400 base::RunLoop().RunUntilIdle();
401
402 // Release the ownership and make sure the service owner listeners fire with
403 // the right values and the right number of times.
404 ASSERT_TRUE(bus->ReleaseOwnership("org.chromium.TestService"));
405 run_loop_state.Run(2);
406
407 EXPECT_TRUE(service_owner1.empty());
408 EXPECT_TRUE(service_owner2.empty());
409 EXPECT_EQ(2, num_of_owner_changes1);
410 EXPECT_EQ(1, num_of_owner_changes2);
411
412 // Unlisten so shutdown can proceed correctly.
413 bus->UnlistenForServiceOwnerChange("org.chromium.TestService", callback1);
414 bus->UnlistenForServiceOwnerChange("org.chromium.TestService", callback2);
415 base::RunLoop().RunUntilIdle();
416
417 // Shut down synchronously.
418 bus->ShutdownAndBlock();
419 EXPECT_TRUE(bus->shutdown_completed());
420 }
OLDNEW
« no previous file with comments | « dbus/bus.cc ('k') | device/media_transfer_protocol/media_transfer_protocol_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698