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/message_loop/message_loop_proxy.h"
21 #include "base/prefs/pref_service.h"
22 #include "base/run_loop.h"
23 #include "base/single_thread_task_runner.h"
24 #include "base/strings/string_number_conversions.h"
25 #include "base/test/scoped_path_override.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"),
107 base::Bind(&RunCallback
,
108 base::MessageLoopProxy::current(),
109 run_loop
.QuitClosure()));
111 // Ensure that the |automatic_reboot_manager_| has had a chance to fully
112 // process the result of the task posted to the blocking pool.
113 base::RunLoop().RunUntilIdle();
115 automatic_reboot_manager_
->AddObserver(this);
118 // system::AutomaticRebootManagerObserver:
119 void OnRebootRequested(
120 system::AutomaticRebootManagerObserver::Reason
) override
{
125 void WillDestroyAutomaticRebootManager() override
{
126 automatic_reboot_manager_
->RemoveObserver(this);
129 void CreateKioskAppUpdateService() {
130 EXPECT_FALSE(update_service_
);
131 update_service_
= KioskAppUpdateServiceFactory::GetForProfile(profile());
132 update_service_
->Init(app_
->id());
135 void FireAppUpdateAvailable() {
136 update_service_
->OnAppUpdateAvailable(app_
);
139 void FireUpdatedNeedReboot() {
140 UpdateEngineClient::Status status
;
141 status
.status
= UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT
;
142 run_loop_
.reset(new base::RunLoop
);
143 automatic_reboot_manager_
->UpdateStatusChanged(status
);
147 void RequestPeriodicReboot() {
148 run_loop_
.reset(new base::RunLoop
);
149 g_browser_process
->local_state()->SetInteger(
150 prefs::kUptimeLimit
, base::TimeDelta::FromHours(2).InSeconds());
155 base::ScopedTempDir temp_dir_
;
156 scoped_ptr
<base::ScopedPathOverride
> uptime_file_override_
;
157 const extensions::Extension
* app_
; // Not owned.
158 KioskAppUpdateService
* update_service_
; // Not owned.
159 system::AutomaticRebootManager
* automatic_reboot_manager_
; // Not owned.
160 scoped_ptr
<base::RunLoop
> run_loop_
;
162 DISALLOW_COPY_AND_ASSIGN(KioskAppUpdateServiceTest
);
165 // Verifies that the app is notified a reboot is required when an app update
166 // becomes available.
167 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest
, AppUpdate
) {
168 CreateKioskAppUpdateService();
170 ExtensionTestMessageListener
listener("app_update", false);
171 FireAppUpdateAvailable();
172 listener
.WaitUntilSatisfied();
175 // Verifies that the app is notified a reboot is required when an OS update is
176 // applied while Chrome is running and the policy to reboot after update is
178 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest
, OsUpdate
) {
179 CreateKioskAppUpdateService();
181 g_browser_process
->local_state()->SetBoolean(prefs::kRebootAfterUpdate
, true);
182 ExtensionTestMessageListener
listener("os_update", false);
183 FireUpdatedNeedReboot();
184 listener
.WaitUntilSatisfied();
187 // Verifies that the app is notified a reboot is required when a periodic reboot
188 // is requested while Chrome is running.
189 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest
, Periodic
) {
190 CreateKioskAppUpdateService();
192 ExtensionTestMessageListener
listener("periodic", false);
193 RequestPeriodicReboot();
194 listener
.WaitUntilSatisfied();
197 // Verifies that the app is notified a reboot is required when an OS update was
198 // applied before Chrome was started and the policy to reboot after update is
200 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest
, StartAfterOsUpdate
) {
201 g_browser_process
->local_state()->SetBoolean(prefs::kRebootAfterUpdate
, true);
202 FireUpdatedNeedReboot();
204 ExtensionTestMessageListener
listener("os_update", false);
205 CreateKioskAppUpdateService();
206 listener
.WaitUntilSatisfied();
209 // Verifies that the app is notified a reboot is required when a periodic reboot
210 // was requested before Chrome was started.
211 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest
, StartAfterPeriodic
) {
212 RequestPeriodicReboot();
214 ExtensionTestMessageListener
listener("periodic", false);
215 CreateKioskAppUpdateService();
216 listener
.WaitUntilSatisfied();
219 } // namespace chromeos