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.
7 #include "base/basictypes.h"
8 #include "base/command_line.h"
9 #include "base/compiler_specific.h"
10 #include "base/file_util.h"
11 #include "base/files/file_path.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/path_service.h"
15 #include "base/prefs/pref_service.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/test/scoped_path_override.h"
18 #include "base/threading/sequenced_worker_pool.h"
19 #include "base/time/time.h"
20 #include "chrome/browser/apps/app_browsertest_util.h"
21 #include "chrome/browser/browser_process.h"
22 #include "chrome/browser/browser_process_platform_part.h"
23 #include "chrome/browser/chromeos/app_mode/kiosk_app_update_service.h"
24 #include "chrome/browser/chromeos/system/automatic_reboot_manager.h"
25 #include "chrome/browser/extensions/extension_test_message_listener.h"
26 #include "chrome/browser/profiles/profile.h"
27 #include "chrome/common/chrome_switches.h"
28 #include "chrome/common/pref_names.h"
29 #include "chromeos/chromeos_paths.h"
30 #include "chromeos/dbus/update_engine_client.h"
31 #include "content/public/browser/browser_thread.h"
32 #include "content/public/test/test_utils.h"
33 #include "extensions/common/extension.h"
34 #include "testing/gtest/include/gtest/gtest.h"
38 class KioskAppUpdateServiceTest
: public extensions::PlatformAppBrowserTest
{
40 KioskAppUpdateServiceTest() : app_(NULL
), update_service_(NULL
) {}
41 virtual ~KioskAppUpdateServiceTest() {}
43 // extensions::PlatformAppBrowserTest overrides:
44 virtual void SetUpOnMainThread() OVERRIDE
{
45 extensions::PlatformAppBrowserTest::SetUpOnMainThread();
47 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
48 const base::FilePath
& temp_dir
= temp_dir_
.path();
50 const base::TimeDelta uptime
= base::TimeDelta::FromHours(1);
51 const std::string uptime_seconds
=
52 base::DoubleToString(uptime
.InSecondsF());
53 const base::FilePath uptime_file
= temp_dir
.Append("uptime");
54 ASSERT_EQ(static_cast<int>(uptime_seconds
.size()),
56 uptime_file
, uptime_seconds
.c_str(), uptime_seconds
.size()));
57 uptime_file_override_
.reset(
58 new base::ScopedPathOverride(chromeos::FILE_UPTIME
, uptime_file
));
61 test_data_dir_
.AppendASCII("api_test/runtime/on_restart_required"));
63 // Fake app mode command line.
64 CommandLine
* command
= CommandLine::ForCurrentProcess();
65 command
->AppendSwitch(switches::kForceAppMode
);
66 command
->AppendSwitchASCII(switches::kAppId
, app_
->id());
68 update_service_
= KioskAppUpdateServiceFactory::GetForProfile(profile());
69 update_service_
->set_app_id(app_
->id());
71 content::BrowserThread::GetBlockingPool()->FlushForTesting();
72 content::RunAllPendingInMessageLoop();
75 void FireAppUpdateAvailable() {
76 update_service_
->OnAppUpdateAvailable(app_
);
79 void FireUpdatedNeedReboot() {
80 UpdateEngineClient::Status status
;
81 status
.status
= UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT
;
82 g_browser_process
->platform_part()->automatic_reboot_manager()->
83 UpdateStatusChanged(status
);
87 base::ScopedTempDir temp_dir_
;
88 scoped_ptr
<base::ScopedPathOverride
> uptime_file_override_
;
89 const extensions::Extension
* app_
; // Not owned.
90 KioskAppUpdateService
* update_service_
; // Not owned.
92 DISALLOW_COPY_AND_ASSIGN(KioskAppUpdateServiceTest
);
95 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest
, AppUpdate
) {
96 FireAppUpdateAvailable();
98 ExtensionTestMessageListener
listener("app_update", false);
99 listener
.WaitUntilSatisfied();
102 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest
, OsUpdate
) {
103 g_browser_process
->local_state()->SetBoolean(prefs::kRebootAfterUpdate
, true);
104 FireUpdatedNeedReboot();
106 ExtensionTestMessageListener
listener("os_update", false);
107 listener
.WaitUntilSatisfied();
110 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest
, Periodic
) {
111 g_browser_process
->local_state()->SetInteger(
112 prefs::kUptimeLimit
, base::TimeDelta::FromMinutes(30).InSeconds());
114 ExtensionTestMessageListener
listener("periodic", false);
115 listener
.WaitUntilSatisfied();
118 } // namespace chromeos