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
|
// Copyright 2014 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 "chrome/browser/chromeos/policy/server_backed_state_keys_broker.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/test/test_simple_task_runner.h"
#include "chromeos/dbus/fake_session_manager_client.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace policy {
class ServerBackedStateKeysBrokerTest : public testing::Test {
public:
ServerBackedStateKeysBrokerTest()
: task_runner_(new base::TestSimpleTaskRunner()),
broker_(&fake_session_manager_client_, task_runner_),
first_boot_(false),
updated_(false),
callback_invoked_(false) {
state_keys_.push_back("1");
state_keys_.push_back("2");
state_keys_.push_back("3");
fake_session_manager_client_.set_server_backed_state_keys(state_keys_);
}
virtual ~ServerBackedStateKeysBrokerTest() {}
void StateKeysUpdated() {
updated_ = true;
}
void ExpectGood() {
EXPECT_FALSE(broker_.pending());
EXPECT_TRUE(broker_.available());
EXPECT_EQ(state_keys_, broker_.state_keys());
EXPECT_EQ(state_keys_.front(), broker_.current_state_key());
}
void HandleStateKeysCallback(const std::vector<std::string>& state_keys,
bool first_boot) {
callback_invoked_ = true;
callback_state_keys_ = state_keys;
first_boot_ = first_boot;
}
protected:
base::MessageLoop loop_;
scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
chromeos::FakeSessionManagerClient fake_session_manager_client_;
ServerBackedStateKeysBroker broker_;
std::vector<std::string> state_keys_;
bool first_boot_;
bool updated_;
std::vector<std::string> callback_state_keys_;
bool callback_invoked_;
private:
DISALLOW_COPY_AND_ASSIGN(ServerBackedStateKeysBrokerTest);
};
TEST_F(ServerBackedStateKeysBrokerTest, Load) {
EXPECT_TRUE(broker_.pending());
EXPECT_FALSE(broker_.available());
EXPECT_TRUE(broker_.state_keys().empty());
EXPECT_TRUE(broker_.current_state_key().empty());
ServerBackedStateKeysBroker::Subscription subscription =
broker_.RegisterUpdateCallback(
base::Bind(&ServerBackedStateKeysBrokerTest::StateKeysUpdated,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(updated_);
ExpectGood();
}
TEST_F(ServerBackedStateKeysBrokerTest, Retry) {
fake_session_manager_client_.set_server_backed_state_keys(
std::vector<std::string>());
ServerBackedStateKeysBroker::Subscription subscription =
broker_.RegisterUpdateCallback(
base::Bind(&ServerBackedStateKeysBrokerTest::StateKeysUpdated,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(updated_);
EXPECT_FALSE(broker_.pending());
EXPECT_FALSE(broker_.available());
EXPECT_TRUE(broker_.state_keys().empty());
EXPECT_TRUE(broker_.current_state_key().empty());
fake_session_manager_client_.set_server_backed_state_keys(state_keys_);
updated_ = false;
ServerBackedStateKeysBroker::Subscription subscription2 =
broker_.RegisterUpdateCallback(base::Bind(&base::DoNothing));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(updated_);
ExpectGood();
}
TEST_F(ServerBackedStateKeysBrokerTest, Refresh) {
ServerBackedStateKeysBroker::Subscription subscription =
broker_.RegisterUpdateCallback(
base::Bind(&ServerBackedStateKeysBrokerTest::StateKeysUpdated,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(updated_);
ExpectGood();
// Update callbacks get fired if the keys change.
state_keys_.erase(state_keys_.begin());
state_keys_.push_back("4");
fake_session_manager_client_.set_server_backed_state_keys(state_keys_);
updated_ = false;
task_runner_->RunPendingTasks();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(updated_);
ExpectGood();
// No update callback if the keys are unchanged.
updated_ = false;
task_runner_->RunPendingTasks();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(updated_);
ExpectGood();
}
TEST_F(ServerBackedStateKeysBrokerTest, Request) {
broker_.RequestStateKeys(
base::Bind(&ServerBackedStateKeysBrokerTest::HandleStateKeysCallback,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
ExpectGood();
EXPECT_TRUE(callback_invoked_);
EXPECT_FALSE(first_boot_);
EXPECT_EQ(state_keys_, callback_state_keys_);
}
TEST_F(ServerBackedStateKeysBrokerTest, RequestFailure) {
fake_session_manager_client_.set_server_backed_state_keys(
std::vector<std::string>());
broker_.RequestStateKeys(
base::Bind(&ServerBackedStateKeysBrokerTest::HandleStateKeysCallback,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(callback_invoked_);
EXPECT_TRUE(callback_state_keys_.empty());
}
} // namespace policy
|