| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/app_mode/kiosk_app_update_service.h" | 5 #include "chrome/browser/chromeos/app_mode/kiosk_app_update_service.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/prefs/pref_service.h" | 16 #include "base/prefs/pref_service.h" |
| 17 #include "base/run_loop.h" | |
| 18 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/test/scoped_path_override.h" | 18 #include "base/test/scoped_path_override.h" |
| 20 #include "base/threading/sequenced_worker_pool.h" | 19 #include "base/threading/sequenced_worker_pool.h" |
| 21 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 22 #include "chrome/browser/apps/app_browsertest_util.h" | 21 #include "chrome/browser/apps/app_browsertest_util.h" |
| 23 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
| 24 #include "chrome/browser/browser_process_platform_part.h" | 23 #include "chrome/browser/browser_process_platform_part.h" |
| 25 #include "chrome/browser/chromeos/system/automatic_reboot_manager.h" | 24 #include "chrome/browser/chromeos/system/automatic_reboot_manager.h" |
| 26 #include "chrome/browser/chromeos/system/automatic_reboot_manager_observer.h" | |
| 27 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 28 #include "chrome/common/chrome_switches.h" | 26 #include "chrome/common/chrome_switches.h" |
| 29 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
| 30 #include "chromeos/chromeos_paths.h" | 28 #include "chromeos/chromeos_paths.h" |
| 31 #include "chromeos/dbus/update_engine_client.h" | 29 #include "chromeos/dbus/update_engine_client.h" |
| 32 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
| 31 #include "content/public/test/test_utils.h" |
| 33 #include "extensions/common/extension.h" | 32 #include "extensions/common/extension.h" |
| 34 #include "extensions/test/extension_test_message_listener.h" | 33 #include "extensions/test/extension_test_message_listener.h" |
| 35 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
| 36 | 35 |
| 37 namespace chromeos { | 36 namespace chromeos { |
| 38 | 37 |
| 39 class KioskAppUpdateServiceTest | 38 class KioskAppUpdateServiceTest : public extensions::PlatformAppBrowserTest { |
| 40 : public extensions::PlatformAppBrowserTest, | |
| 41 public system::AutomaticRebootManagerObserver { | |
| 42 public: | 39 public: |
| 43 KioskAppUpdateServiceTest() | 40 KioskAppUpdateServiceTest() : app_(NULL), update_service_(NULL) {} |
| 44 : app_(NULL), | 41 virtual ~KioskAppUpdateServiceTest() {} |
| 45 update_service_(NULL), | |
| 46 automatic_reboot_manager_(NULL) {} | |
| 47 | |
| 48 ~KioskAppUpdateServiceTest() override {} | |
| 49 | 42 |
| 50 // extensions::PlatformAppBrowserTest overrides: | 43 // extensions::PlatformAppBrowserTest overrides: |
| 51 void SetUpOnMainThread() override { | 44 virtual void SetUpOnMainThread() override { |
| 52 extensions::PlatformAppBrowserTest::SetUpOnMainThread(); | 45 extensions::PlatformAppBrowserTest::SetUpOnMainThread(); |
| 53 | 46 |
| 54 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 47 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 55 const base::FilePath& temp_dir = temp_dir_.path(); | 48 const base::FilePath& temp_dir = temp_dir_.path(); |
| 56 | 49 |
| 57 const base::TimeDelta uptime = base::TimeDelta::FromHours(1); | 50 const base::TimeDelta uptime = base::TimeDelta::FromHours(1); |
| 58 const std::string uptime_seconds = | 51 const std::string uptime_seconds = |
| 59 base::DoubleToString(uptime.InSecondsF()); | 52 base::DoubleToString(uptime.InSecondsF()); |
| 60 const base::FilePath uptime_file = temp_dir.Append("uptime"); | 53 const base::FilePath uptime_file = temp_dir.Append("uptime"); |
| 61 ASSERT_EQ(static_cast<int>(uptime_seconds.size()), | 54 ASSERT_EQ(static_cast<int>(uptime_seconds.size()), |
| 62 base::WriteFile( | 55 base::WriteFile( |
| 63 uptime_file, uptime_seconds.c_str(), uptime_seconds.size())); | 56 uptime_file, uptime_seconds.c_str(), uptime_seconds.size())); |
| 64 uptime_file_override_.reset( | 57 uptime_file_override_.reset( |
| 65 new base::ScopedPathOverride(chromeos::FILE_UPTIME, uptime_file)); | 58 new base::ScopedPathOverride(chromeos::FILE_UPTIME, uptime_file)); |
| 66 | 59 |
| 67 app_ = LoadExtension( | 60 app_ = LoadExtension( |
| 68 test_data_dir_.AppendASCII("api_test/runtime/on_restart_required")); | 61 test_data_dir_.AppendASCII("api_test/runtime/on_restart_required")); |
| 69 | 62 |
| 70 // Fake app mode command line. | 63 // Fake app mode command line. |
| 71 CommandLine* command = CommandLine::ForCurrentProcess(); | 64 CommandLine* command = CommandLine::ForCurrentProcess(); |
| 72 command->AppendSwitch(switches::kForceAppMode); | 65 command->AppendSwitch(switches::kForceAppMode); |
| 73 command->AppendSwitchASCII(switches::kAppId, app_->id()); | 66 command->AppendSwitchASCII(switches::kAppId, app_->id()); |
| 74 | 67 |
| 75 automatic_reboot_manager_ = | 68 update_service_ = KioskAppUpdateServiceFactory::GetForProfile(profile()); |
| 76 g_browser_process->platform_part()->automatic_reboot_manager(); | 69 update_service_->set_app_id(app_->id()); |
| 77 automatic_reboot_manager_->AddObserver(this); | |
| 78 } | |
| 79 | 70 |
| 80 // system::AutomaticRebootManagerObserver: | 71 content::RunAllBlockingPoolTasksUntilIdle(); |
| 81 void OnRebootRequested( | |
| 82 system::AutomaticRebootManagerObserver::Reason) override { | |
| 83 if (run_loop_) | |
| 84 run_loop_->Quit(); | |
| 85 } | |
| 86 | |
| 87 void WillDestroyAutomaticRebootManager() override { | |
| 88 automatic_reboot_manager_->RemoveObserver(this); | |
| 89 } | |
| 90 | |
| 91 void CreateKioskAppUpdateService() { | |
| 92 EXPECT_FALSE(update_service_); | |
| 93 update_service_ = KioskAppUpdateServiceFactory::GetForProfile(profile()); | |
| 94 update_service_->Init(app_->id()); | |
| 95 } | 72 } |
| 96 | 73 |
| 97 void FireAppUpdateAvailable() { | 74 void FireAppUpdateAvailable() { |
| 98 update_service_->OnAppUpdateAvailable(app_); | 75 update_service_->OnAppUpdateAvailable(app_); |
| 99 } | 76 } |
| 100 | 77 |
| 101 void FireUpdatedNeedReboot() { | 78 void FireUpdatedNeedReboot() { |
| 102 UpdateEngineClient::Status status; | 79 UpdateEngineClient::Status status; |
| 103 status.status = UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT; | 80 status.status = UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT; |
| 104 run_loop_.reset(new base::RunLoop); | 81 g_browser_process->platform_part()->automatic_reboot_manager()-> |
| 105 automatic_reboot_manager_->UpdateStatusChanged(status); | 82 UpdateStatusChanged(status); |
| 106 run_loop_->Run(); | |
| 107 } | |
| 108 | |
| 109 void RequestPeriodicReboot() { | |
| 110 run_loop_.reset(new base::RunLoop); | |
| 111 g_browser_process->local_state()->SetInteger( | |
| 112 prefs::kUptimeLimit, base::TimeDelta::FromMinutes(30).InSeconds()); | |
| 113 run_loop_->Run(); | |
| 114 } | 83 } |
| 115 | 84 |
| 116 private: | 85 private: |
| 117 base::ScopedTempDir temp_dir_; | 86 base::ScopedTempDir temp_dir_; |
| 118 scoped_ptr<base::ScopedPathOverride> uptime_file_override_; | 87 scoped_ptr<base::ScopedPathOverride> uptime_file_override_; |
| 119 const extensions::Extension* app_; // Not owned. | 88 const extensions::Extension* app_; // Not owned. |
| 120 KioskAppUpdateService* update_service_; // Not owned. | 89 KioskAppUpdateService* update_service_; // Not owned. |
| 121 system::AutomaticRebootManager* automatic_reboot_manager_; // Not owned. | |
| 122 scoped_ptr<base::RunLoop> run_loop_; | |
| 123 | 90 |
| 124 DISALLOW_COPY_AND_ASSIGN(KioskAppUpdateServiceTest); | 91 DISALLOW_COPY_AND_ASSIGN(KioskAppUpdateServiceTest); |
| 125 }; | 92 }; |
| 126 | 93 |
| 127 // Verifies that the app is notified a reboot is required when an app update | |
| 128 // becomes available. | |
| 129 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, AppUpdate) { | 94 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, AppUpdate) { |
| 130 CreateKioskAppUpdateService(); | 95 FireAppUpdateAvailable(); |
| 131 | 96 |
| 132 ExtensionTestMessageListener listener("app_update", false); | 97 ExtensionTestMessageListener listener("app_update", false); |
| 133 FireAppUpdateAvailable(); | |
| 134 listener.WaitUntilSatisfied(); | 98 listener.WaitUntilSatisfied(); |
| 135 } | 99 } |
| 136 | 100 |
| 137 // Verifies that the app is notified a reboot is required when an OS update is | |
| 138 // applied while Chrome is running and the policy to reboot after update is | |
| 139 // enabled. | |
| 140 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, OsUpdate) { | 101 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, OsUpdate) { |
| 141 CreateKioskAppUpdateService(); | |
| 142 | |
| 143 g_browser_process->local_state()->SetBoolean(prefs::kRebootAfterUpdate, true); | |
| 144 ExtensionTestMessageListener listener("os_update", false); | |
| 145 FireUpdatedNeedReboot(); | |
| 146 listener.WaitUntilSatisfied(); | |
| 147 } | |
| 148 | |
| 149 // Verifies that the app is notified a reboot is required when a periodic reboot | |
| 150 // is requested while Chrome is running. | |
| 151 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, Periodic) { | |
| 152 CreateKioskAppUpdateService(); | |
| 153 | |
| 154 ExtensionTestMessageListener listener("periodic", false); | |
| 155 RequestPeriodicReboot(); | |
| 156 listener.WaitUntilSatisfied(); | |
| 157 } | |
| 158 | |
| 159 // Verifies that the app is notified a reboot is required when an OS update was | |
| 160 // applied before Chrome was started and the policy to reboot after update is | |
| 161 // enabled. | |
| 162 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, StartAfterOsUpdate) { | |
| 163 g_browser_process->local_state()->SetBoolean(prefs::kRebootAfterUpdate, true); | 102 g_browser_process->local_state()->SetBoolean(prefs::kRebootAfterUpdate, true); |
| 164 FireUpdatedNeedReboot(); | 103 FireUpdatedNeedReboot(); |
| 165 | 104 |
| 166 ExtensionTestMessageListener listener("os_update", false); | 105 ExtensionTestMessageListener listener("os_update", false); |
| 167 CreateKioskAppUpdateService(); | |
| 168 listener.WaitUntilSatisfied(); | 106 listener.WaitUntilSatisfied(); |
| 169 } | 107 } |
| 170 | 108 |
| 171 // Verifies that the app is notified a reboot is required when a periodic reboot | 109 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, Periodic) { |
| 172 // was requested before Chrome was started. | 110 g_browser_process->local_state()->SetInteger( |
| 173 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, StartAfterPeriodic) { | 111 prefs::kUptimeLimit, base::TimeDelta::FromMinutes(30).InSeconds()); |
| 174 RequestPeriodicReboot(); | |
| 175 | 112 |
| 176 ExtensionTestMessageListener listener("periodic", false); | 113 ExtensionTestMessageListener listener("periodic", false); |
| 177 CreateKioskAppUpdateService(); | |
| 178 listener.WaitUntilSatisfied(); | 114 listener.WaitUntilSatisfied(); |
| 179 } | 115 } |
| 180 | 116 |
| 181 } // namespace chromeos | 117 } // namespace chromeos |
| OLD | NEW |