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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <utility>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "components/arc/arc_bridge_service_impl.h"
#include "components/arc/test/fake_arc_bridge_instance.h"
#include "ipc/mojo/scoped_ipc_support.h"
#include "mojo/public/cpp/system/message_pipe.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace arc {
namespace {
// A fake ArcBridgeBootstrap that creates a local connection.
class FakeArcBridgeBootstrap : public ArcBridgeBootstrap {
public:
explicit FakeArcBridgeBootstrap(FakeArcBridgeInstance* instance)
: instance_(instance) {}
~FakeArcBridgeBootstrap() override {}
void Start() override {
DCHECK(delegate_);
ArcBridgeInstancePtr instance;
instance_->Bind(mojo::GetProxy(&instance));
delegate_->OnConnectionEstablished(std::move(instance));
}
void Stop() override {
DCHECK(delegate_);
instance_->Unbind();
delegate_->OnStopped();
}
private:
// Owned by the caller.
FakeArcBridgeInstance* instance_;
DISALLOW_COPY_AND_ASSIGN(FakeArcBridgeBootstrap);
};
} // namespace
class ArcBridgeTest : public testing::Test, public ArcBridgeService::Observer {
public:
ArcBridgeTest() : ready_(false) {}
~ArcBridgeTest() override {}
void OnStateChanged(ArcBridgeService::State state) override {
state_ = state;
switch (state) {
case ArcBridgeService::State::READY:
ready_ = true;
break;
case ArcBridgeService::State::STOPPED:
message_loop_.PostTask(FROM_HERE, message_loop_.QuitWhenIdleClosure());
break;
default:
break;
}
}
bool ready() const { return ready_; }
ArcBridgeService::State state() const { return state_; }
protected:
scoped_ptr<ArcBridgeServiceImpl> service_;
scoped_ptr<FakeArcBridgeInstance> instance_;
private:
void SetUp() override {
chromeos::DBusThreadManager::Initialize();
ready_ = false;
state_ = ArcBridgeService::State::STOPPED;
instance_.reset(new FakeArcBridgeInstance());
service_.reset(new ArcBridgeServiceImpl(
make_scoped_ptr(new FakeArcBridgeBootstrap(instance_.get()))));
service_->AddObserver(this);
}
void TearDown() override {
service_->RemoveObserver(this);
instance_.reset();
service_.reset();
chromeos::DBusThreadManager::Shutdown();
}
bool ready_;
ArcBridgeService::State state_;
base::MessageLoopForUI message_loop_;
DISALLOW_COPY_AND_ASSIGN(ArcBridgeTest);
};
// Exercises the basic functionality of the ARC Bridge Service. A message from
// within the instance should cause the observer to be notified.
TEST_F(ArcBridgeTest, Basic) {
ASSERT_FALSE(ready());
ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
service_->SetAvailable(true);
service_->HandleStartup();
instance_->WaitForInitCall();
ASSERT_EQ(ArcBridgeService::State::READY, state());
service_->Shutdown();
ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
}
// If not all pre-requisites are met, the instance is not started.
TEST_F(ArcBridgeTest, Prerequisites) {
ASSERT_FALSE(ready());
ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
service_->SetAvailable(true);
ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
service_->SetAvailable(false);
service_->HandleStartup();
ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
}
// If the ArcBridgeService is shut down, it should be stopped, even
// mid-startup.
TEST_F(ArcBridgeTest, ShutdownMidStartup) {
ASSERT_FALSE(ready());
service_->SetAvailable(true);
service_->HandleStartup();
// WaitForInitCall() omitted.
ASSERT_EQ(ArcBridgeService::State::READY, state());
service_->Shutdown();
ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
}
// If the channel is disconnected, it should be re-established.
TEST_F(ArcBridgeTest, Restart) {
ASSERT_FALSE(ready());
ASSERT_EQ(0, instance_->init_calls());
service_->SetAvailable(true);
service_->HandleStartup();
instance_->WaitForInitCall();
ASSERT_EQ(ArcBridgeService::State::READY, state());
ASSERT_EQ(1, instance_->init_calls());
// Simulate a connection loss.
service_->OnChannelClosed();
instance_->WaitForInitCall();
ASSERT_EQ(ArcBridgeService::State::READY, state());
ASSERT_EQ(2, instance_->init_calls());
service_->Shutdown();
ASSERT_EQ(ArcBridgeService::State::STOPPED, state());
}
} // namespace arc
|