1 // Copyright 2014 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.
5 #include "chrome/browser/chromeos/policy/server_backed_state_keys_broker.h"
8 #include "base/bind_helpers.h"
9 #include "base/macros.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/test/test_simple_task_runner.h"
13 #include "chromeos/dbus/fake_session_manager_client.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 class ServerBackedStateKeysBrokerTest
: public testing::Test
{
20 ServerBackedStateKeysBrokerTest()
21 : task_runner_(new base::TestSimpleTaskRunner()),
22 broker_(&fake_session_manager_client_
, task_runner_
),
24 callback_invoked_(false) {
25 state_keys_
.push_back("1");
26 state_keys_
.push_back("2");
27 state_keys_
.push_back("3");
28 fake_session_manager_client_
.set_server_backed_state_keys(state_keys_
);
30 ~ServerBackedStateKeysBrokerTest() override
{}
32 void StateKeysUpdated() {
37 EXPECT_FALSE(broker_
.pending());
38 EXPECT_TRUE(broker_
.available());
39 EXPECT_EQ(state_keys_
, broker_
.state_keys());
40 EXPECT_EQ(state_keys_
.front(), broker_
.current_state_key());
43 void HandleStateKeysCallback(const std::vector
<std::string
>& state_keys
) {
44 callback_invoked_
= true;
45 callback_state_keys_
= state_keys
;
49 base::MessageLoop loop_
;
50 scoped_refptr
<base::TestSimpleTaskRunner
> task_runner_
;
51 chromeos::FakeSessionManagerClient fake_session_manager_client_
;
52 ServerBackedStateKeysBroker broker_
;
53 std::vector
<std::string
> state_keys_
;
55 std::vector
<std::string
> callback_state_keys_
;
56 bool callback_invoked_
;
59 DISALLOW_COPY_AND_ASSIGN(ServerBackedStateKeysBrokerTest
);
62 TEST_F(ServerBackedStateKeysBrokerTest
, Load
) {
63 EXPECT_TRUE(broker_
.pending());
64 EXPECT_FALSE(broker_
.available());
65 EXPECT_TRUE(broker_
.state_keys().empty());
66 EXPECT_TRUE(broker_
.current_state_key().empty());
68 ServerBackedStateKeysBroker::Subscription subscription
=
69 broker_
.RegisterUpdateCallback(
70 base::Bind(&ServerBackedStateKeysBrokerTest::StateKeysUpdated
,
71 base::Unretained(this)));
72 base::RunLoop().RunUntilIdle();
73 EXPECT_TRUE(updated_
);
77 TEST_F(ServerBackedStateKeysBrokerTest
, Retry
) {
78 fake_session_manager_client_
.set_server_backed_state_keys(
79 std::vector
<std::string
>());
81 ServerBackedStateKeysBroker::Subscription subscription
=
82 broker_
.RegisterUpdateCallback(
83 base::Bind(&ServerBackedStateKeysBrokerTest::StateKeysUpdated
,
84 base::Unretained(this)));
85 base::RunLoop().RunUntilIdle();
86 EXPECT_TRUE(updated_
);
88 EXPECT_FALSE(broker_
.pending());
89 EXPECT_FALSE(broker_
.available());
90 EXPECT_TRUE(broker_
.state_keys().empty());
91 EXPECT_TRUE(broker_
.current_state_key().empty());
93 fake_session_manager_client_
.set_server_backed_state_keys(state_keys_
);
95 ServerBackedStateKeysBroker::Subscription subscription2
=
96 broker_
.RegisterUpdateCallback(base::Bind(&base::DoNothing
));
97 base::RunLoop().RunUntilIdle();
98 EXPECT_TRUE(updated_
);
102 TEST_F(ServerBackedStateKeysBrokerTest
, Refresh
) {
103 ServerBackedStateKeysBroker::Subscription subscription
=
104 broker_
.RegisterUpdateCallback(
105 base::Bind(&ServerBackedStateKeysBrokerTest::StateKeysUpdated
,
106 base::Unretained(this)));
107 base::RunLoop().RunUntilIdle();
108 EXPECT_TRUE(updated_
);
111 // Update callbacks get fired if the keys change.
112 state_keys_
.erase(state_keys_
.begin());
113 state_keys_
.push_back("4");
114 fake_session_manager_client_
.set_server_backed_state_keys(state_keys_
);
116 task_runner_
->RunPendingTasks();
117 base::RunLoop().RunUntilIdle();
118 EXPECT_TRUE(updated_
);
121 // No update callback if the keys are unchanged.
123 task_runner_
->RunPendingTasks();
124 base::RunLoop().RunUntilIdle();
125 EXPECT_FALSE(updated_
);
129 TEST_F(ServerBackedStateKeysBrokerTest
, Request
) {
130 broker_
.RequestStateKeys(
131 base::Bind(&ServerBackedStateKeysBrokerTest::HandleStateKeysCallback
,
132 base::Unretained(this)));
133 base::RunLoop().RunUntilIdle();
135 EXPECT_TRUE(callback_invoked_
);
136 EXPECT_EQ(state_keys_
, callback_state_keys_
);
139 TEST_F(ServerBackedStateKeysBrokerTest
, RequestFailure
) {
140 fake_session_manager_client_
.set_server_backed_state_keys(
141 std::vector
<std::string
>());
143 broker_
.RequestStateKeys(
144 base::Bind(&ServerBackedStateKeysBrokerTest::HandleStateKeysCallback
,
145 base::Unretained(this)));
146 base::RunLoop().RunUntilIdle();
147 EXPECT_TRUE(callback_invoked_
);
148 EXPECT_TRUE(callback_state_keys_
.empty());
151 } // namespace policy