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 <string>
6
7#include "base/prefs/pref_service.h"
8#include "chrome/browser/browser_process.h"
9#include "chrome/browser/chromeos/login/login_manager_test.h"
10#include "chrome/browser/chromeos/login/startup_utils.h"
11#include "chrome/browser/chromeos/login/test/oobe_screen_waiter.h"
12#include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
13#include "chrome/browser/chromeos/login/ui/oobe_display.h"
14#include "chrome/browser/chromeos/login/ui/webui_login_view.h"
15#include "chrome/common/chrome_switches.h"
16#include "chrome/common/pref_names.h"
17#include "chromeos/chromeos_switches.h"
18#include "chromeos/dbus/dbus_thread_manager.h"
19#include "chromeos/dbus/fake_power_manager_client.h"
20#include "chromeos/dbus/fake_session_manager_client.h"
21#include "chromeos/dbus/fake_update_engine_client.h"
22#include "content/public/test/browser_test_utils.h"
23
24namespace chromeos {
25
26namespace {
27
28const char kTestUser1[] = "test-user1@gmail.com";
29
30}  // namespace
31
32class ResetTest : public LoginManagerTest {
33 public:
34  ResetTest() : LoginManagerTest(false),
35      update_engine_client_(NULL),
36      session_manager_client_(NULL),
37      power_manager_client_(NULL) {
38  }
39  virtual ~ResetTest() {}
40
41  virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE {
42    LoginManagerTest::SetUpCommandLine(command_line);
43    command_line->AppendSwitch(switches::kEnableRollbackOption);
44  }
45
46  // LoginManagerTest overrides:
47  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
48    scoped_ptr<DBusThreadManagerSetter> dbus_setter =
49        chromeos::DBusThreadManager::GetSetterForTesting();
50    session_manager_client_ = new FakeSessionManagerClient;
51    dbus_setter->SetSessionManagerClient(
52        scoped_ptr<SessionManagerClient>(session_manager_client_));
53    power_manager_client_ = new FakePowerManagerClient;
54    dbus_setter->SetPowerManagerClient(
55        scoped_ptr<PowerManagerClient>(power_manager_client_));
56    update_engine_client_ = new FakeUpdateEngineClient;
57    dbus_setter->SetUpdateEngineClient(
58        scoped_ptr<UpdateEngineClient>(update_engine_client_));
59
60    LoginManagerTest::SetUpInProcessBrowserTestFixture();
61  }
62
63  void RegisterSomeUser() {
64    RegisterUser(kTestUser1);
65    StartupUtils::MarkOobeCompleted();
66  }
67
68  bool JSExecuted(const std::string& script) {
69    return content::ExecuteScript(web_contents(), script);
70  }
71
72  void InvokeResetScreen() {
73    ASSERT_TRUE(JSExecuted("cr.ui.Oobe.handleAccelerator('reset');"));
74    OobeScreenWaiter(OobeDisplay::SCREEN_OOBE_RESET).Wait();
75  }
76
77  void InvokeRollbackOption() {
78    ASSERT_TRUE(JSExecuted(
79        "cr.ui.Oobe.handleAccelerator('show_rollback_on_reset_screen');"));
80  }
81
82  void CloseResetScreen() {
83    ASSERT_TRUE(JSExecuted("$('reset-cancel-button').click();"));
84  }
85
86  void ClickResetButton() {
87    ASSERT_TRUE(JSExecuted("$('reset-button').click();"));
88  }
89
90  void ClickRestartButton() {
91    ASSERT_TRUE(JSExecuted("$('reset-restart-button').click();"));
92  }
93  void ClickToConfirmButton() {
94    ASSERT_TRUE(JSExecuted("$('reset-toconfirm-button').click();"));
95  }
96
97  FakeUpdateEngineClient* update_engine_client_;
98  FakeSessionManagerClient* session_manager_client_;
99  FakePowerManagerClient* power_manager_client_;
100};
101
102class ResetFirstAfterBootTest : public ResetTest {
103 public:
104  virtual ~ResetFirstAfterBootTest() {}
105
106  virtual void SetUpCommandLine(base::CommandLine* command_line) OVERRIDE {
107    LoginManagerTest::SetUpCommandLine(command_line);
108    command_line->AppendSwitch(switches::kFirstExecAfterBoot);
109    command_line->AppendSwitch(switches::kEnableRollbackOption);
110  }
111};
112
113IN_PROC_BROWSER_TEST_F(ResetTest, PRE_ShowAndCancel) {
114  RegisterSomeUser();
115}
116
117IN_PROC_BROWSER_TEST_F(ResetTest, ShowAndCancel) {
118  JSExpect("!!document.querySelector('#reset.hidden')");
119  InvokeResetScreen();
120  JSExpect("!document.querySelector('#reset.hidden')");
121  CloseResetScreen();
122  JSExpect("!!document.querySelector('#reset.hidden')");
123}
124
125IN_PROC_BROWSER_TEST_F(ResetTest, PRE_RestartBeforePowerwash) {
126  RegisterSomeUser();
127}
128
129IN_PROC_BROWSER_TEST_F(ResetTest, RestartBeforePowerwash) {
130  PrefService* prefs = g_browser_process->local_state();
131
132  InvokeResetScreen();
133  EXPECT_EQ(0, power_manager_client_->num_request_restart_calls());
134  EXPECT_EQ(0, session_manager_client_->start_device_wipe_call_count());
135  ClickRestartButton();
136  ASSERT_EQ(1, power_manager_client_->num_request_restart_calls());
137  ASSERT_EQ(0, session_manager_client_->start_device_wipe_call_count());
138
139  EXPECT_TRUE(prefs->GetBoolean(prefs::kFactoryResetRequested));
140}
141
142IN_PROC_BROWSER_TEST_F(ResetFirstAfterBootTest, PRE_ShortcutInvokedCases) {
143  PrefService* prefs = g_browser_process->local_state();
144  prefs->SetBoolean(prefs::kFactoryResetRequested, true);
145  RegisterSomeUser();
146}
147
148IN_PROC_BROWSER_TEST_F(ResetFirstAfterBootTest, ShortcutInvokedCases) {
149  // rollback unavailable
150  update_engine_client_->set_can_rollback_check_result(false);
151  InvokeResetScreen();
152  EXPECT_EQ(0, power_manager_client_->num_request_restart_calls());
153  EXPECT_EQ(0, session_manager_client_->start_device_wipe_call_count());
154  EXPECT_EQ(0, update_engine_client_->rollback_call_count());
155  InvokeRollbackOption();
156  ClickToConfirmButton();
157  ClickResetButton();
158  EXPECT_EQ(0, power_manager_client_->num_request_restart_calls());
159  EXPECT_EQ(1, session_manager_client_->start_device_wipe_call_count());
160  EXPECT_EQ(0, update_engine_client_->rollback_call_count());
161  CloseResetScreen();
162  OobeScreenWaiter(OobeDisplay::SCREEN_ACCOUNT_PICKER).Wait();
163
164  PrefService* prefs = g_browser_process->local_state();
165  prefs->SetBoolean(prefs::kFactoryResetRequested, true);
166  update_engine_client_->set_can_rollback_check_result(true);
167  // rollback available and unchecked
168  InvokeResetScreen();
169  ClickToConfirmButton();
170  ClickResetButton();
171  EXPECT_EQ(0, power_manager_client_->num_request_restart_calls());
172  EXPECT_EQ(2, session_manager_client_->start_device_wipe_call_count());
173  EXPECT_EQ(0, update_engine_client_->rollback_call_count());
174  CloseResetScreen();
175  OobeScreenWaiter(OobeDisplay::SCREEN_ACCOUNT_PICKER).Wait();
176
177  prefs->SetBoolean(prefs::kFactoryResetRequested, true);
178  // rollback available and checked
179  InvokeResetScreen();
180  InvokeRollbackOption();
181  ClickToConfirmButton();
182  ClickResetButton();
183  EXPECT_EQ(0, power_manager_client_->num_request_restart_calls());
184  EXPECT_EQ(2, session_manager_client_->start_device_wipe_call_count());
185  EXPECT_EQ(1, update_engine_client_->rollback_call_count());
186}
187
188IN_PROC_BROWSER_TEST_F(ResetFirstAfterBootTest, PRE_PowerwashRequested) {
189  PrefService* prefs = g_browser_process->local_state();
190  prefs->SetBoolean(prefs::kFactoryResetRequested, true);
191  RegisterSomeUser();
192}
193
194IN_PROC_BROWSER_TEST_F(ResetFirstAfterBootTest, PowerwashRequested) {
195  OobeScreenWaiter(OobeDisplay::SCREEN_OOBE_RESET).Wait();
196  EXPECT_EQ(0, power_manager_client_->num_request_restart_calls());
197  EXPECT_EQ(0, session_manager_client_->start_device_wipe_call_count());
198  EXPECT_EQ(0, update_engine_client_->rollback_call_count());
199  ClickToConfirmButton();
200  EXPECT_EQ(0, power_manager_client_->num_request_restart_calls());
201  EXPECT_EQ(0, session_manager_client_->start_device_wipe_call_count());
202  EXPECT_EQ(0, update_engine_client_->rollback_call_count());
203  ClickResetButton();
204  EXPECT_EQ(0, power_manager_client_->num_request_restart_calls());
205  EXPECT_EQ(1, session_manager_client_->start_device_wipe_call_count());
206  EXPECT_EQ(0, update_engine_client_->rollback_call_count());
207}
208
209IN_PROC_BROWSER_TEST_F(ResetFirstAfterBootTest, PRE_ErrorOnRollbackRequested) {
210  PrefService* prefs = g_browser_process->local_state();
211  prefs->SetBoolean(prefs::kFactoryResetRequested, true);
212  RegisterSomeUser();
213}
214
215IN_PROC_BROWSER_TEST_F(ResetFirstAfterBootTest, ErrorOnRollbackRequested) {
216  update_engine_client_->set_can_rollback_check_result(true);
217  OobeScreenWaiter(OobeDisplay::SCREEN_OOBE_RESET).Wait();
218  EXPECT_EQ(0, power_manager_client_->num_request_restart_calls());
219  EXPECT_EQ(0, session_manager_client_->start_device_wipe_call_count());
220  EXPECT_EQ(0, update_engine_client_->rollback_call_count());
221  JSExpect("!$('reset').classList.contains('revert-promise-view')");
222  InvokeRollbackOption();
223  ClickToConfirmButton();
224  ClickResetButton();
225  EXPECT_EQ(0, power_manager_client_->num_request_restart_calls());
226  EXPECT_EQ(0, session_manager_client_->start_device_wipe_call_count());
227  EXPECT_EQ(1, update_engine_client_->rollback_call_count());
228  JSExpect("$('reset').classList.contains('revert-promise-view')");
229  UpdateEngineClient::Status error_update_status;
230  error_update_status.status = UpdateEngineClient::UPDATE_STATUS_ERROR;
231  update_engine_client_->NotifyObserversThatStatusChanged(
232      error_update_status);
233  OobeScreenWaiter(OobeDisplay::SCREEN_ERROR_MESSAGE).Wait();
234}
235
236IN_PROC_BROWSER_TEST_F(ResetFirstAfterBootTest,
237                       PRE_SuccessOnRollbackRequested) {
238  PrefService* prefs = g_browser_process->local_state();
239  prefs->SetBoolean(prefs::kFactoryResetRequested, true);
240  RegisterSomeUser();
241}
242
243IN_PROC_BROWSER_TEST_F(ResetFirstAfterBootTest, SuccessOnRollbackRequested) {
244  update_engine_client_->set_can_rollback_check_result(true);
245  OobeScreenWaiter(OobeDisplay::SCREEN_OOBE_RESET).Wait();
246  InvokeRollbackOption();
247  ClickToConfirmButton();
248  ClickResetButton();
249  EXPECT_EQ(0, power_manager_client_->num_request_restart_calls());
250  EXPECT_EQ(0, session_manager_client_->start_device_wipe_call_count());
251  EXPECT_EQ(1, update_engine_client_->rollback_call_count());
252  UpdateEngineClient::Status ready_for_reboot_status;
253  ready_for_reboot_status.status =
254      UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT;
255  update_engine_client_->NotifyObserversThatStatusChanged(
256      ready_for_reboot_status);
257  EXPECT_EQ(1, power_manager_client_->num_request_restart_calls());
258}
259
260
261}  // namespace chromeos
262