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.
4
5#include "chrome/browser/chromeos/policy/server_backed_state_keys_broker.h"
6
7#include "base/bind.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"
15
16namespace policy {
17
18class ServerBackedStateKeysBrokerTest : public testing::Test {
19 public:
20  ServerBackedStateKeysBrokerTest()
21      : task_runner_(new base::TestSimpleTaskRunner()),
22        broker_(&fake_session_manager_client_, task_runner_),
23        first_boot_(false),
24        updated_(false),
25        callback_invoked_(false) {
26    state_keys_.push_back("1");
27    state_keys_.push_back("2");
28    state_keys_.push_back("3");
29    fake_session_manager_client_.set_server_backed_state_keys(state_keys_);
30  }
31  virtual ~ServerBackedStateKeysBrokerTest() {}
32
33  void StateKeysUpdated() {
34    updated_ = true;
35  }
36
37  void ExpectGood() {
38    EXPECT_FALSE(broker_.pending());
39    EXPECT_TRUE(broker_.available());
40    EXPECT_EQ(state_keys_, broker_.state_keys());
41    EXPECT_EQ(state_keys_.front(), broker_.current_state_key());
42  }
43
44  void HandleStateKeysCallback(const std::vector<std::string>& state_keys,
45                               bool first_boot) {
46    callback_invoked_ = true;
47    callback_state_keys_ = state_keys;
48    first_boot_ = first_boot;
49  }
50
51 protected:
52  base::MessageLoop loop_;
53  scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
54  chromeos::FakeSessionManagerClient fake_session_manager_client_;
55  ServerBackedStateKeysBroker broker_;
56  std::vector<std::string> state_keys_;
57  bool first_boot_;
58  bool updated_;
59  std::vector<std::string> callback_state_keys_;
60  bool callback_invoked_;
61
62 private:
63  DISALLOW_COPY_AND_ASSIGN(ServerBackedStateKeysBrokerTest);
64};
65
66TEST_F(ServerBackedStateKeysBrokerTest, Load) {
67  EXPECT_TRUE(broker_.pending());
68  EXPECT_FALSE(broker_.available());
69  EXPECT_TRUE(broker_.state_keys().empty());
70  EXPECT_TRUE(broker_.current_state_key().empty());
71
72  ServerBackedStateKeysBroker::Subscription subscription =
73      broker_.RegisterUpdateCallback(
74          base::Bind(&ServerBackedStateKeysBrokerTest::StateKeysUpdated,
75                     base::Unretained(this)));
76  base::RunLoop().RunUntilIdle();
77  EXPECT_TRUE(updated_);
78  ExpectGood();
79}
80
81TEST_F(ServerBackedStateKeysBrokerTest, Retry) {
82  fake_session_manager_client_.set_server_backed_state_keys(
83      std::vector<std::string>());
84
85  ServerBackedStateKeysBroker::Subscription subscription =
86      broker_.RegisterUpdateCallback(
87          base::Bind(&ServerBackedStateKeysBrokerTest::StateKeysUpdated,
88                     base::Unretained(this)));
89  base::RunLoop().RunUntilIdle();
90  EXPECT_TRUE(updated_);
91
92  EXPECT_FALSE(broker_.pending());
93  EXPECT_FALSE(broker_.available());
94  EXPECT_TRUE(broker_.state_keys().empty());
95  EXPECT_TRUE(broker_.current_state_key().empty());
96
97  fake_session_manager_client_.set_server_backed_state_keys(state_keys_);
98  updated_ = false;
99  ServerBackedStateKeysBroker::Subscription subscription2 =
100      broker_.RegisterUpdateCallback(base::Bind(&base::DoNothing));
101  base::RunLoop().RunUntilIdle();
102  EXPECT_TRUE(updated_);
103  ExpectGood();
104}
105
106TEST_F(ServerBackedStateKeysBrokerTest, Refresh) {
107  ServerBackedStateKeysBroker::Subscription subscription =
108      broker_.RegisterUpdateCallback(
109          base::Bind(&ServerBackedStateKeysBrokerTest::StateKeysUpdated,
110                     base::Unretained(this)));
111  base::RunLoop().RunUntilIdle();
112  EXPECT_TRUE(updated_);
113  ExpectGood();
114
115  // Update callbacks get fired if the keys change.
116  state_keys_.erase(state_keys_.begin());
117  state_keys_.push_back("4");
118  fake_session_manager_client_.set_server_backed_state_keys(state_keys_);
119  updated_ = false;
120  task_runner_->RunPendingTasks();
121  base::RunLoop().RunUntilIdle();
122  EXPECT_TRUE(updated_);
123  ExpectGood();
124
125  // No update callback if the keys are unchanged.
126  updated_ = false;
127  task_runner_->RunPendingTasks();
128  base::RunLoop().RunUntilIdle();
129  EXPECT_FALSE(updated_);
130  ExpectGood();
131}
132
133TEST_F(ServerBackedStateKeysBrokerTest, Request) {
134  broker_.RequestStateKeys(
135      base::Bind(&ServerBackedStateKeysBrokerTest::HandleStateKeysCallback,
136                 base::Unretained(this)));
137  base::RunLoop().RunUntilIdle();
138  ExpectGood();
139  EXPECT_TRUE(callback_invoked_);
140  EXPECT_FALSE(first_boot_);
141  EXPECT_EQ(state_keys_, callback_state_keys_);
142}
143
144TEST_F(ServerBackedStateKeysBrokerTest, RequestFailure) {
145  fake_session_manager_client_.set_server_backed_state_keys(
146      std::vector<std::string>());
147
148  broker_.RequestStateKeys(
149      base::Bind(&ServerBackedStateKeysBrokerTest::HandleStateKeysCallback,
150                 base::Unretained(this)));
151  base::RunLoop().RunUntilIdle();
152  EXPECT_TRUE(callback_invoked_);
153  EXPECT_TRUE(callback_state_keys_.empty());
154}
155
156}  // namespace policy
157