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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/app_mode/kiosk_app_update_service.h"
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/command_line.h"
13 #include "base/compiler_specific.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/files/scoped_temp_dir.h"
17 #include "base/location.h"
18 #include "base/memory/ref_counted.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "base/prefs/pref_service.h"
21 #include "base/run_loop.h"
22 #include "base/single_thread_task_runner.h"
23 #include "base/strings/string_number_conversions.h"
24 #include "base/test/scoped_path_override.h"
25 #include "base/thread_task_runner_handle.h"
26 #include "base/threading/sequenced_worker_pool.h"
27 #include "base/time/time.h"
28 #include "chrome/browser/apps/app_browsertest_util.h"
29 #include "chrome/browser/browser_process.h"
30 #include "chrome/browser/browser_process_platform_part.h"
31 #include "chrome/browser/chromeos/system/automatic_reboot_manager.h"
32 #include "chrome/browser/chromeos/system/automatic_reboot_manager_observer.h"
33 #include "chrome/browser/profiles/profile.h"
34 #include "chrome/common/chrome_switches.h"
35 #include "chrome/common/pref_names.h"
36 #include "chromeos/chromeos_paths.h"
37 #include "chromeos/dbus/update_engine_client.h"
38 #include "content/public/browser/browser_thread.h"
39 #include "extensions/common/extension.h"
40 #include "extensions/test/extension_test_message_listener.h"
41 #include "testing/gtest/include/gtest/gtest.h"
47 void RunCallback(scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
,
48 const base::Closure
& callback
) {
49 task_runner
->PostTask(FROM_HERE
, callback
);
54 class KioskAppUpdateServiceTest
55 : public extensions::PlatformAppBrowserTest
,
56 public system::AutomaticRebootManagerObserver
{
58 KioskAppUpdateServiceTest()
60 update_service_(NULL
),
61 automatic_reboot_manager_(NULL
) {}
63 ~KioskAppUpdateServiceTest() override
{}
65 // extensions::PlatformAppBrowserTest overrides:
66 void SetUpInProcessBrowserTestFixture() override
{
67 extensions::PlatformAppBrowserTest::SetUpInProcessBrowserTestFixture();
69 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
70 const base::FilePath
& temp_dir
= temp_dir_
.path();
72 const base::TimeDelta uptime
= base::TimeDelta::FromHours(3);
73 const std::string uptime_seconds
=
74 base::DoubleToString(uptime
.InSecondsF());
75 const base::FilePath uptime_file
= temp_dir
.Append("uptime");
76 ASSERT_EQ(static_cast<int>(uptime_seconds
.size()),
78 uptime_file
, uptime_seconds
.c_str(), uptime_seconds
.size()));
79 uptime_file_override_
.reset(
80 new base::ScopedPathOverride(chromeos::FILE_UPTIME
, uptime_file
));
83 void SetUpOnMainThread() override
{
84 extensions::PlatformAppBrowserTest::SetUpOnMainThread();
87 test_data_dir_
.AppendASCII("api_test/runtime/on_restart_required"));
89 // Fake app mode command line.
90 base::CommandLine
* command
= base::CommandLine::ForCurrentProcess();
91 command
->AppendSwitch(switches::kForceAppMode
);
92 command
->AppendSwitchASCII(switches::kAppId
, app_
->id());
94 automatic_reboot_manager_
=
95 g_browser_process
->platform_part()->automatic_reboot_manager();
97 // Wait for the |automatic_reboot_manager_| to finish initializing.
98 base::RunLoop run_loop
;
99 base::SequencedWorkerPool
* worker_pool
=
100 content::BrowserThread::GetBlockingPool();
101 // Ensure that the initialization task the |automatic_reboot_manager_| posts
102 // to the blocking pool has finished by posting another task with the same
103 // |SequenceToken| and waiting for it to finish.
104 worker_pool
->PostSequencedWorkerTask(
105 worker_pool
->GetNamedSequenceToken("automatic-reboot-manager"),
106 FROM_HERE
, base::Bind(&RunCallback
, base::ThreadTaskRunnerHandle::Get(),
107 run_loop
.QuitClosure()));
109 // Ensure that the |automatic_reboot_manager_| has had a chance to fully
110 // process the result of the task posted to the blocking pool.
111 base::RunLoop().RunUntilIdle();
113 automatic_reboot_manager_
->AddObserver(this);
116 // system::AutomaticRebootManagerObserver:
117 void OnRebootRequested(
118 system::AutomaticRebootManagerObserver::Reason
) override
{
123 void WillDestroyAutomaticRebootManager() override
{
124 automatic_reboot_manager_
->RemoveObserver(this);
127 void CreateKioskAppUpdateService() {
128 EXPECT_FALSE(update_service_
);
129 update_service_
= KioskAppUpdateServiceFactory::GetForProfile(profile());
130 update_service_
->Init(app_
->id());
133 void FireAppUpdateAvailable() {
134 update_service_
->OnAppUpdateAvailable(app_
);
137 void FireUpdatedNeedReboot() {
138 UpdateEngineClient::Status status
;
139 status
.status
= UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT
;
140 run_loop_
.reset(new base::RunLoop
);
141 automatic_reboot_manager_
->UpdateStatusChanged(status
);
145 void RequestPeriodicReboot() {
146 run_loop_
.reset(new base::RunLoop
);
147 g_browser_process
->local_state()->SetInteger(
148 prefs::kUptimeLimit
, base::TimeDelta::FromHours(2).InSeconds());
153 base::ScopedTempDir temp_dir_
;
154 scoped_ptr
<base::ScopedPathOverride
> uptime_file_override_
;
155 const extensions::Extension
* app_
; // Not owned.
156 KioskAppUpdateService
* update_service_
; // Not owned.
157 system::AutomaticRebootManager
* automatic_reboot_manager_
; // Not owned.
158 scoped_ptr
<base::RunLoop
> run_loop_
;
160 DISALLOW_COPY_AND_ASSIGN(KioskAppUpdateServiceTest
);
163 // Verifies that the app is notified a reboot is required when an app update
164 // becomes available.
165 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest
, AppUpdate
) {
166 CreateKioskAppUpdateService();
168 ExtensionTestMessageListener
listener("app_update", false);
169 FireAppUpdateAvailable();
170 listener
.WaitUntilSatisfied();
173 // Verifies that the app is notified a reboot is required when an OS update is
174 // applied while Chrome is running and the policy to reboot after update is
176 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest
, OsUpdate
) {
177 CreateKioskAppUpdateService();
179 g_browser_process
->local_state()->SetBoolean(prefs::kRebootAfterUpdate
, true);
180 ExtensionTestMessageListener
listener("os_update", false);
181 FireUpdatedNeedReboot();
182 listener
.WaitUntilSatisfied();
185 // Verifies that the app is notified a reboot is required when a periodic reboot
186 // is requested while Chrome is running.
187 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest
, Periodic
) {
188 CreateKioskAppUpdateService();
190 ExtensionTestMessageListener
listener("periodic", false);
191 RequestPeriodicReboot();
192 listener
.WaitUntilSatisfied();
195 // Verifies that the app is notified a reboot is required when an OS update was
196 // applied before Chrome was started and the policy to reboot after update is
198 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest
, StartAfterOsUpdate
) {
199 g_browser_process
->local_state()->SetBoolean(prefs::kRebootAfterUpdate
, true);
200 FireUpdatedNeedReboot();
202 ExtensionTestMessageListener
listener("os_update", false);
203 CreateKioskAppUpdateService();
204 listener
.WaitUntilSatisfied();
207 // Verifies that the app is notified a reboot is required when a periodic reboot
208 // was requested before Chrome was started.
209 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest
, StartAfterPeriodic
) {
210 RequestPeriodicReboot();
212 ExtensionTestMessageListener
listener("periodic", false);
213 CreateKioskAppUpdateService();
214 listener
.WaitUntilSatisfied();
217 } // namespace chromeos