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

Side by Side Diff: components/arc/arc_bridge_service_unittest.cc

Issue 2031143002: Add unit tests for ArcBridgeService::RemoveObserver() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 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
« no previous file with comments | « components/arc/arc_bridge_service.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 <memory> 5 #include <memory>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "chromeos/dbus/dbus_thread_manager.h" 12 #include "chromeos/dbus/dbus_thread_manager.h"
13 #include "components/arc/arc_bridge_service_impl.h" 13 #include "components/arc/arc_bridge_service_impl.h"
14 #include "components/arc/test/fake_arc_bridge_bootstrap.h" 14 #include "components/arc/test/fake_arc_bridge_bootstrap.h"
15 #include "components/arc/test/fake_arc_bridge_instance.h" 15 #include "components/arc/test/fake_arc_bridge_instance.h"
16 #include "ipc/mojo/scoped_ipc_support.h" 16 #include "ipc/mojo/scoped_ipc_support.h"
17 #include "mojo/public/cpp/system/message_pipe.h" 17 #include "mojo/public/cpp/system/message_pipe.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 19
20 namespace arc { 20 namespace arc {
21 21
22 namespace {
23
22 class ArcBridgeTest : public testing::Test, public ArcBridgeService::Observer { 24 class ArcBridgeTest : public testing::Test, public ArcBridgeService::Observer {
23 public: 25 public:
24 ArcBridgeTest() : ready_(false) {} 26 ArcBridgeTest() : ready_(false) {}
25 ~ArcBridgeTest() override {} 27 ~ArcBridgeTest() override {}
26 28
27 void OnStateChanged(ArcBridgeService::State state) override { 29 void OnStateChanged(ArcBridgeService::State state) override {
28 state_ = state; 30 state_ = state;
29 switch (state) { 31 switch (state) {
30 case ArcBridgeService::State::READY: 32 case ArcBridgeService::State::READY:
31 ready_ = true; 33 ready_ = true;
(...skipping 17 matching lines...) Expand all
49 51
50 private: 52 private:
51 void SetUp() override { 53 void SetUp() override {
52 chromeos::DBusThreadManager::Initialize(); 54 chromeos::DBusThreadManager::Initialize();
53 55
54 ready_ = false; 56 ready_ = false;
55 state_ = ArcBridgeService::State::STOPPED; 57 state_ = ArcBridgeService::State::STOPPED;
56 58
57 instance_.reset(new FakeArcBridgeInstance()); 59 instance_.reset(new FakeArcBridgeInstance());
58 service_.reset(new ArcBridgeServiceImpl( 60 service_.reset(new ArcBridgeServiceImpl(
59 base::WrapUnique(new FakeArcBridgeBootstrap(instance_.get())))); 61 base::MakeUnique<FakeArcBridgeBootstrap>(instance_.get())));
60 62
61 service_->AddObserver(this); 63 service_->AddObserver(this);
62 } 64 }
63 65
64 void TearDown() override { 66 void TearDown() override {
65 service_->RemoveObserver(this); 67 service_->RemoveObserver(this);
66 instance_.reset(); 68 instance_.reset();
67 service_.reset(); 69 service_.reset();
68 70
69 chromeos::DBusThreadManager::Shutdown(); 71 chromeos::DBusThreadManager::Shutdown();
70 } 72 }
71 73
72 bool ready_; 74 bool ready_;
73 ArcBridgeService::State state_; 75 ArcBridgeService::State state_;
74 base::MessageLoopForUI message_loop_; 76 base::MessageLoopForUI message_loop_;
75 77
76 DISALLOW_COPY_AND_ASSIGN(ArcBridgeTest); 78 DISALLOW_COPY_AND_ASSIGN(ArcBridgeTest);
77 }; 79 };
78 80
81 class DummyObserver : public ArcBridgeService::Observer {};
82
83 } // namespace
84
79 // Exercises the basic functionality of the ARC Bridge Service. A message from 85 // Exercises the basic functionality of the ARC Bridge Service. A message from
80 // within the instance should cause the observer to be notified. 86 // within the instance should cause the observer to be notified.
81 TEST_F(ArcBridgeTest, Basic) { 87 TEST_F(ArcBridgeTest, Basic) {
82 ASSERT_FALSE(ready()); 88 ASSERT_FALSE(ready());
83 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 89 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
84 90
85 service_->SetAvailable(true); 91 service_->SetAvailable(true);
86 service_->HandleStartup(); 92 service_->HandleStartup();
87 instance_->WaitForInitCall(); 93 instance_->WaitForInitCall();
88 ASSERT_EQ(ArcBridgeService::State::READY, state()); 94 ASSERT_EQ(ArcBridgeService::State::READY, state());
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 service_->DisableReconnectDelayForTesting(); 137 service_->DisableReconnectDelayForTesting();
132 service_->OnChannelClosed(); 138 service_->OnChannelClosed();
133 instance_->WaitForInitCall(); 139 instance_->WaitForInitCall();
134 ASSERT_EQ(ArcBridgeService::State::READY, state()); 140 ASSERT_EQ(ArcBridgeService::State::READY, state());
135 ASSERT_EQ(2, instance_->init_calls()); 141 ASSERT_EQ(2, instance_->init_calls());
136 142
137 service_->Shutdown(); 143 service_->Shutdown();
138 ASSERT_EQ(ArcBridgeService::State::STOPPED, state()); 144 ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
139 } 145 }
140 146
147 // Removing the same observer more than once should be okay.
148 TEST_F(ArcBridgeTest, RemoveObserverTwice) {
149 ASSERT_FALSE(ready());
150 service_->RemoveObserver(this);
151 // The teardown method will also remove |this|.
152 }
153
154 // Removing an unknown observer should be allowed.
155 TEST_F(ArcBridgeTest, RemoveUnknownObserver) {
156 ASSERT_FALSE(ready());
157 auto dummy_observer = base::MakeUnique<DummyObserver>();
158 service_->RemoveObserver(dummy_observer.get());
159 }
160
141 } // namespace arc 161 } // namespace arc
OLDNEW
« no previous file with comments | « components/arc/arc_bridge_service.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698