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