Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Side by Side Diff: chrome/browser/chromeos/app_mode/kiosk_app_update_service_browsertest.cc

Issue 727363002: Fire notifications when reboot is requested, not scheduled (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@managed_cros
Patch Set: Removed debug logging. Ready for re-review. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/bind.h"
11 #include "base/callback.h"
10 #include "base/command_line.h" 12 #include "base/command_line.h"
11 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
12 #include "base/files/file_path.h" 14 #include "base/files/file_path.h"
13 #include "base/files/file_util.h" 15 #include "base/files/file_util.h"
14 #include "base/files/scoped_temp_dir.h" 16 #include "base/files/scoped_temp_dir.h"
17 #include "base/location.h"
18 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h" 19 #include "base/memory/scoped_ptr.h"
20 #include "base/message_loop/message_loop_proxy.h"
16 #include "base/prefs/pref_service.h" 21 #include "base/prefs/pref_service.h"
22 #include "base/run_loop.h"
23 #include "base/single_thread_task_runner.h"
17 #include "base/strings/string_number_conversions.h" 24 #include "base/strings/string_number_conversions.h"
18 #include "base/test/scoped_path_override.h" 25 #include "base/test/scoped_path_override.h"
19 #include "base/threading/sequenced_worker_pool.h" 26 #include "base/threading/sequenced_worker_pool.h"
20 #include "base/time/time.h" 27 #include "base/time/time.h"
21 #include "chrome/browser/apps/app_browsertest_util.h" 28 #include "chrome/browser/apps/app_browsertest_util.h"
22 #include "chrome/browser/browser_process.h" 29 #include "chrome/browser/browser_process.h"
23 #include "chrome/browser/browser_process_platform_part.h" 30 #include "chrome/browser/browser_process_platform_part.h"
24 #include "chrome/browser/chromeos/system/automatic_reboot_manager.h" 31 #include "chrome/browser/chromeos/system/automatic_reboot_manager.h"
32 #include "chrome/browser/chromeos/system/automatic_reboot_manager_observer.h"
25 #include "chrome/browser/profiles/profile.h" 33 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/common/chrome_switches.h" 34 #include "chrome/common/chrome_switches.h"
27 #include "chrome/common/pref_names.h" 35 #include "chrome/common/pref_names.h"
28 #include "chromeos/chromeos_paths.h" 36 #include "chromeos/chromeos_paths.h"
29 #include "chromeos/dbus/update_engine_client.h" 37 #include "chromeos/dbus/update_engine_client.h"
30 #include "content/public/browser/browser_thread.h" 38 #include "content/public/browser/browser_thread.h"
31 #include "content/public/test/test_utils.h"
32 #include "extensions/common/extension.h" 39 #include "extensions/common/extension.h"
33 #include "extensions/test/extension_test_message_listener.h" 40 #include "extensions/test/extension_test_message_listener.h"
34 #include "testing/gtest/include/gtest/gtest.h" 41 #include "testing/gtest/include/gtest/gtest.h"
35 42
36 namespace chromeos { 43 namespace chromeos {
37 44
38 class KioskAppUpdateServiceTest : public extensions::PlatformAppBrowserTest { 45 namespace {
46
47 void RunCallback(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
48 const base::Closure& callback) {
49 task_runner->PostTask(FROM_HERE, callback);
50 }
51
52 } // namespace
53
54 class KioskAppUpdateServiceTest
55 : public extensions::PlatformAppBrowserTest,
56 public system::AutomaticRebootManagerObserver {
39 public: 57 public:
40 KioskAppUpdateServiceTest() : app_(NULL), update_service_(NULL) {} 58 KioskAppUpdateServiceTest()
41 virtual ~KioskAppUpdateServiceTest() {} 59 : app_(NULL),
60 update_service_(NULL),
61 automatic_reboot_manager_(NULL) {}
62
63 ~KioskAppUpdateServiceTest() override {}
42 64
43 // extensions::PlatformAppBrowserTest overrides: 65 // extensions::PlatformAppBrowserTest overrides:
44 virtual void SetUpOnMainThread() override { 66 void SetUpInProcessBrowserTestFixture() override {
45 extensions::PlatformAppBrowserTest::SetUpOnMainThread(); 67 extensions::PlatformAppBrowserTest::SetUpInProcessBrowserTestFixture();
46 68
47 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 69 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
48 const base::FilePath& temp_dir = temp_dir_.path(); 70 const base::FilePath& temp_dir = temp_dir_.path();
49 71
50 const base::TimeDelta uptime = base::TimeDelta::FromHours(1); 72 const base::TimeDelta uptime = base::TimeDelta::FromHours(3);
51 const std::string uptime_seconds = 73 const std::string uptime_seconds =
52 base::DoubleToString(uptime.InSecondsF()); 74 base::DoubleToString(uptime.InSecondsF());
53 const base::FilePath uptime_file = temp_dir.Append("uptime"); 75 const base::FilePath uptime_file = temp_dir.Append("uptime");
54 ASSERT_EQ(static_cast<int>(uptime_seconds.size()), 76 ASSERT_EQ(static_cast<int>(uptime_seconds.size()),
55 base::WriteFile( 77 base::WriteFile(
56 uptime_file, uptime_seconds.c_str(), uptime_seconds.size())); 78 uptime_file, uptime_seconds.c_str(), uptime_seconds.size()));
57 uptime_file_override_.reset( 79 uptime_file_override_.reset(
58 new base::ScopedPathOverride(chromeos::FILE_UPTIME, uptime_file)); 80 new base::ScopedPathOverride(chromeos::FILE_UPTIME, uptime_file));
81 }
82
83 void SetUpOnMainThread() override {
84 extensions::PlatformAppBrowserTest::SetUpOnMainThread();
59 85
60 app_ = LoadExtension( 86 app_ = LoadExtension(
61 test_data_dir_.AppendASCII("api_test/runtime/on_restart_required")); 87 test_data_dir_.AppendASCII("api_test/runtime/on_restart_required"));
62 88
63 // Fake app mode command line. 89 // Fake app mode command line.
64 CommandLine* command = CommandLine::ForCurrentProcess(); 90 CommandLine* command = CommandLine::ForCurrentProcess();
65 command->AppendSwitch(switches::kForceAppMode); 91 command->AppendSwitch(switches::kForceAppMode);
66 command->AppendSwitchASCII(switches::kAppId, app_->id()); 92 command->AppendSwitchASCII(switches::kAppId, app_->id());
67 93
94 automatic_reboot_manager_ =
95 g_browser_process->platform_part()->automatic_reboot_manager();
96
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,
107 base::Bind(&RunCallback,
108 base::MessageLoopProxy::current(),
109 run_loop.QuitClosure()));
110 // Ensure that the |automatic_reboot_manager_| has had a chance to process
111 // the result of the task posted to the blocking pool.
112 base::RunLoop().RunUntilIdle();
xiyuan 2014/11/18 22:37:16 Should we be doing "run_loop.Run()" so that it blo
bartfab (slow) 2014/11/19 09:21:08 Oops, I was not spinning |run_loop| at all. My int
113
114 automatic_reboot_manager_->AddObserver(this);
115 }
116
117 // system::AutomaticRebootManagerObserver:
118 void OnRebootRequested(
119 system::AutomaticRebootManagerObserver::Reason) override {
120 if (run_loop_)
121 run_loop_->Quit();
122 }
123
124 void WillDestroyAutomaticRebootManager() override {
125 automatic_reboot_manager_->RemoveObserver(this);
126 }
127
128 void CreateKioskAppUpdateService() {
129 EXPECT_FALSE(update_service_);
68 update_service_ = KioskAppUpdateServiceFactory::GetForProfile(profile()); 130 update_service_ = KioskAppUpdateServiceFactory::GetForProfile(profile());
69 update_service_->set_app_id(app_->id()); 131 update_service_->Init(app_->id());
70
71 content::RunAllBlockingPoolTasksUntilIdle();
72 } 132 }
73 133
74 void FireAppUpdateAvailable() { 134 void FireAppUpdateAvailable() {
75 update_service_->OnAppUpdateAvailable(app_); 135 update_service_->OnAppUpdateAvailable(app_);
76 } 136 }
77 137
78 void FireUpdatedNeedReboot() { 138 void FireUpdatedNeedReboot() {
79 UpdateEngineClient::Status status; 139 UpdateEngineClient::Status status;
80 status.status = UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT; 140 status.status = UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT;
81 g_browser_process->platform_part()->automatic_reboot_manager()-> 141 run_loop_.reset(new base::RunLoop);
82 UpdateStatusChanged(status); 142 automatic_reboot_manager_->UpdateStatusChanged(status);
143 run_loop_->Run();
144 }
145
146 void RequestPeriodicReboot() {
147 run_loop_.reset(new base::RunLoop);
148 g_browser_process->local_state()->SetInteger(
149 prefs::kUptimeLimit, base::TimeDelta::FromHours(2).InSeconds());
150 run_loop_->Run();
83 } 151 }
84 152
85 private: 153 private:
86 base::ScopedTempDir temp_dir_; 154 base::ScopedTempDir temp_dir_;
87 scoped_ptr<base::ScopedPathOverride> uptime_file_override_; 155 scoped_ptr<base::ScopedPathOverride> uptime_file_override_;
88 const extensions::Extension* app_; // Not owned. 156 const extensions::Extension* app_; // Not owned.
89 KioskAppUpdateService* update_service_; // Not owned. 157 KioskAppUpdateService* update_service_; // Not owned.
158 system::AutomaticRebootManager* automatic_reboot_manager_; // Not owned.
159 scoped_ptr<base::RunLoop> run_loop_;
90 160
91 DISALLOW_COPY_AND_ASSIGN(KioskAppUpdateServiceTest); 161 DISALLOW_COPY_AND_ASSIGN(KioskAppUpdateServiceTest);
92 }; 162 };
93 163
164 // Verifies that the app is notified a reboot is required when an app update
165 // becomes available.
94 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, AppUpdate) { 166 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, AppUpdate) {
95 FireAppUpdateAvailable(); 167 CreateKioskAppUpdateService();
96 168
97 ExtensionTestMessageListener listener("app_update", false); 169 ExtensionTestMessageListener listener("app_update", false);
170 FireAppUpdateAvailable();
98 listener.WaitUntilSatisfied(); 171 listener.WaitUntilSatisfied();
99 } 172 }
100 173
174 // Verifies that the app is notified a reboot is required when an OS update is
175 // applied while Chrome is running and the policy to reboot after update is
176 // enabled.
101 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, OsUpdate) { 177 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, OsUpdate) {
178 CreateKioskAppUpdateService();
179
180 g_browser_process->local_state()->SetBoolean(prefs::kRebootAfterUpdate, true);
181 ExtensionTestMessageListener listener("os_update", false);
182 FireUpdatedNeedReboot();
183 listener.WaitUntilSatisfied();
184 }
185
186 // Verifies that the app is notified a reboot is required when a periodic reboot
187 // is requested while Chrome is running.
188 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, Periodic) {
189 CreateKioskAppUpdateService();
190
191 ExtensionTestMessageListener listener("periodic", false);
192 RequestPeriodicReboot();
193 listener.WaitUntilSatisfied();
194 }
195
196 // Verifies that the app is notified a reboot is required when an OS update was
197 // applied before Chrome was started and the policy to reboot after update is
198 // enabled.
199 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, StartAfterOsUpdate) {
102 g_browser_process->local_state()->SetBoolean(prefs::kRebootAfterUpdate, true); 200 g_browser_process->local_state()->SetBoolean(prefs::kRebootAfterUpdate, true);
103 FireUpdatedNeedReboot(); 201 FireUpdatedNeedReboot();
104 202
105 ExtensionTestMessageListener listener("os_update", false); 203 ExtensionTestMessageListener listener("os_update", false);
204 CreateKioskAppUpdateService();
106 listener.WaitUntilSatisfied(); 205 listener.WaitUntilSatisfied();
107 } 206 }
108 207
109 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, Periodic) { 208 // Verifies that the app is notified a reboot is required when a periodic reboot
110 g_browser_process->local_state()->SetInteger( 209 // was requested before Chrome was started.
111 prefs::kUptimeLimit, base::TimeDelta::FromMinutes(30).InSeconds()); 210 IN_PROC_BROWSER_TEST_F(KioskAppUpdateServiceTest, StartAfterPeriodic) {
211 RequestPeriodicReboot();
112 212
113 ExtensionTestMessageListener listener("periodic", false); 213 ExtensionTestMessageListener listener("periodic", false);
214 CreateKioskAppUpdateService();
114 listener.WaitUntilSatisfied(); 215 listener.WaitUntilSatisfied();
115 } 216 }
116 217
117 } // namespace chromeos 218 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698