OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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. | |
4 | |
5 #include <string> | |
6 | |
7 #include "ash/shell.h" | |
8 #include "ash/system/date/date_default_view.h" | |
9 #include "ash/system/date/tray_date.h" | |
10 #include "ash/system/tray/system_tray.h" | |
11 #include "ash/system/user/login_status.h" | |
12 #include "base/command_line.h" | |
13 #include "base/location.h" | |
14 #include "base/macros.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/message_loop/message_loop.h" | |
17 #include "base/run_loop.h" | |
18 #include "base/strings/stringprintf.h" | |
19 #include "chrome/browser/chrome_notification_types.h" | |
20 #include "chrome/browser/chromeos/login/lock/screen_locker.h" | |
21 #include "chrome/browser/chromeos/login/lock/screen_locker_tester.h" | |
22 #include "chrome/browser/chromeos/login/lock/webui_screen_locker.h" | |
23 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h" | |
24 #include "chrome/browser/chromeos/login/ui/webui_login_view.h" | |
25 #include "chrome/browser/chromeos/policy/device_policy_builder.h" | |
26 #include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h" | |
27 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" | |
28 #include "chrome/browser/chromeos/settings/device_settings_service.h" | |
29 #include "chrome/browser/lifetime/application_lifetime.h" | |
30 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h" | |
31 #include "chromeos/chromeos_switches.h" | |
32 #include "chromeos/dbus/dbus_thread_manager.h" | |
33 #include "chromeos/dbus/fake_session_manager_client.h" | |
34 #include "chromeos/dbus/session_manager_client.h" | |
35 #include "content/public/browser/notification_service.h" | |
36 #include "content/public/browser/web_contents.h" | |
37 #include "content/public/browser/web_ui.h" | |
38 #include "content/public/test/browser_test_utils.h" | |
39 #include "content/public/test/test_utils.h" | |
40 #include "ui/compositor/scoped_animation_duration_scale_mode.h" | |
41 #include "ui/views/view.h" | |
stevenjb
2014/12/17 19:09:02
Does this really need all of these dependencies, o
| |
42 | |
43 namespace em = enterprise_management; | |
44 | |
45 namespace chromeos { | |
46 | |
47 namespace { | |
48 | |
49 const char kWaitForHiddenStateScript[] = | |
50 "var screenElement = document.getElementById('%s');" | |
51 "var expectation = %s;" | |
52 "function SendReplyIfAsExpected() {" | |
53 " if (screenElement.hidden != expectation)" | |
54 " return false;" | |
55 " domAutomationController.send(screenElement.hidden);" | |
56 " observer.disconnect();" | |
57 " return true;" | |
58 "}" | |
59 "var observer = new MutationObserver(SendReplyIfAsExpected);" | |
60 "if (!SendReplyIfAsExpected()) {" | |
61 " var options = { attributes: true };" | |
62 " observer.observe(screenElement, options);" | |
63 "}"; | |
64 | |
65 } // namespace | |
66 | |
67 class ShutdownPolicyBaseTest | |
68 : public policy::DevicePolicyCrosBrowserTest, | |
69 public DeviceSettingsService::Observer { | |
70 protected: | |
71 ShutdownPolicyBaseTest() : contents_(nullptr) {} | |
72 virtual ~ShutdownPolicyBaseTest() {} | |
stevenjb
2014/12/17 19:09:02
override instead of virtual
| |
73 | |
74 // DeviceSettingsService::Observer: | |
75 virtual void OwnershipStatusChanged() override {} | |
stevenjb
2014/12/17 19:09:02
no 'virtual' in overrides
Blank line between imple
| |
76 virtual void DeviceSettingsUpdated() override { | |
77 if (run_loop_) | |
78 run_loop_->Quit(); | |
79 } | |
80 virtual void OnDeviceSettingsServiceShutdown() override {} | |
81 | |
82 // policy::DevicePolicyCrosBrowserTest: | |
83 virtual void SetUpInProcessBrowserTestFixture() override { | |
84 policy::DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture(); | |
85 InstallOwnerKey(); | |
86 MarkAsEnterpriseOwned(); | |
87 } | |
88 | |
89 // A helper functions which prepares the script by injecting the element_id of | |
90 // the element whose hiddenness we want to check and the expectation. | |
91 std::string PrepareScript(const std::string& element_id, bool expectation) { | |
92 return base::StringPrintf(kWaitForHiddenStateScript, element_id.c_str(), | |
93 expectation ? "true" : "false"); | |
94 } | |
95 | |
96 // Checks whether the element identified by |element_id| is hidden and only | |
97 // returns if the expectation is fulfilled. | |
98 void PrepareAndRunScript(const std::string& element_id, bool expectation) { | |
99 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | |
100 contents_, PrepareScript(element_id, expectation), | |
101 &result_)); | |
102 } | |
103 | |
104 // Updates the device shutdown policy and sets it to |reboot_on_shutdown|. | |
105 void UpdateRebootOnShutdownPolicy(bool reboot_on_shutdown) { | |
106 policy::DevicePolicyBuilder* builder = device_policy(); | |
107 ASSERT_TRUE(builder); | |
108 em::ChromeDeviceSettingsProto& proto(builder->payload()); | |
109 proto.mutable_reboot_on_shutdown()->set_reboot_on_shutdown( | |
110 reboot_on_shutdown); | |
111 } | |
112 | |
113 // Refreshes device policy and waits for it to be applied. | |
114 virtual void SyncRefreshDevicePolicy() { | |
115 run_loop_.reset(new base::RunLoop()); | |
116 DeviceSettingsService::Get()->AddObserver(this); | |
117 RefreshDevicePolicy(); | |
118 run_loop_->Run(); | |
119 DeviceSettingsService::Get()->RemoveObserver(this); | |
120 run_loop_.reset(); | |
121 } | |
122 | |
123 // Blocks until the OobeUI indicates that the javascript side has been | |
124 // initialized. | |
125 void WaitForOobeIO(OobeUI* oobe_ui) { | |
126 ASSERT_TRUE(oobe_ui); | |
127 base::RunLoop run_loop; | |
128 const bool oobe_ui_ready = oobe_ui->IsJSReady(run_loop.QuitClosure()); | |
129 if (!oobe_ui_ready) | |
130 run_loop.Run(); | |
131 } | |
132 | |
133 content::WebContents* contents_; | |
134 bool result_; | |
135 scoped_ptr<base::RunLoop> run_loop_; | |
136 }; | |
137 | |
138 class ShutdownPolicyInSessionTest | |
139 : public ShutdownPolicyBaseTest { | |
140 protected: | |
141 ShutdownPolicyInSessionTest() {} | |
142 virtual ~ShutdownPolicyInSessionTest() {} | |
143 | |
144 virtual void SetUpOnMainThread() override { | |
145 ShutdownPolicyBaseTest::SetUpOnMainThread(); | |
146 ash::TrayDate* tray_date = ash::Shell::GetInstance() | |
147 ->GetPrimarySystemTray() | |
148 ->GetTrayDateForTesting(); | |
149 ASSERT_TRUE(tray_date); | |
150 date_default_view_.reset( | |
151 static_cast<ash::DateDefaultView*>( | |
152 tray_date->CreateDefaultViewForTesting(ash::user::LOGGED_IN_USER))); | |
153 ASSERT_TRUE(date_default_view_); | |
154 } | |
155 | |
156 void TearDownOnMainThread() override { | |
157 date_default_view_.reset(); | |
158 ShutdownPolicyBaseTest::TearDownOnMainThread(); | |
159 } | |
160 | |
161 // Get the shutdown and reboot button view from the date default view. | |
162 void GetShutdownAndRebootButton(views::View** shutdown_button, | |
163 views::View** reboot_button) { | |
164 *shutdown_button = date_default_view_->GetShutdownButtonView(); | |
165 ASSERT_TRUE(*shutdown_button); | |
166 *reboot_button = date_default_view_->GetRebootButtonView(); | |
167 ASSERT_TRUE(*reboot_button); | |
168 } | |
169 scoped_ptr<ash::DateDefaultView> date_default_view_; | |
170 | |
171 private: | |
172 DISALLOW_COPY_AND_ASSIGN(ShutdownPolicyInSessionTest); | |
173 }; | |
174 | |
175 IN_PROC_BROWSER_TEST_F(ShutdownPolicyInSessionTest, TestBasic) { | |
176 views::View *shutdown_button, *reboot_button; | |
177 GetShutdownAndRebootButton(&shutdown_button, &reboot_button); | |
178 EXPECT_TRUE(shutdown_button->visible()); | |
179 EXPECT_FALSE(reboot_button->visible()); | |
180 } | |
181 | |
182 IN_PROC_BROWSER_TEST_F(ShutdownPolicyInSessionTest, PolicyChange) { | |
183 views::View *shutdown_button, *reboot_button; | |
184 GetShutdownAndRebootButton(&shutdown_button, &reboot_button); | |
185 | |
186 UpdateRebootOnShutdownPolicy(true); | |
187 SyncRefreshDevicePolicy(); | |
188 EXPECT_FALSE(shutdown_button->visible()); | |
189 EXPECT_TRUE(reboot_button->visible()); | |
190 | |
191 UpdateRebootOnShutdownPolicy(false); | |
192 SyncRefreshDevicePolicy(); | |
193 EXPECT_TRUE(shutdown_button->visible()); | |
194 EXPECT_FALSE(reboot_button->visible()); | |
195 } | |
196 | |
197 class ShutdownPolicyLockerTest : public ShutdownPolicyBaseTest { | |
198 protected: | |
199 ShutdownPolicyLockerTest() : fake_session_manager_client_(nullptr) {} | |
200 virtual ~ShutdownPolicyLockerTest() {} | |
201 | |
202 virtual void SetUpInProcessBrowserTestFixture() override { | |
203 fake_session_manager_client_ = new FakeSessionManagerClient; | |
204 DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( | |
205 scoped_ptr<SessionManagerClient>(fake_session_manager_client_)); | |
206 | |
207 ShutdownPolicyBaseTest::SetUpInProcessBrowserTestFixture(); | |
208 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode( | |
209 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION)); | |
210 InstallOwnerKey(); | |
211 MarkAsEnterpriseOwned(); | |
212 } | |
213 | |
214 virtual void SetUpOnMainThread() override { | |
215 ShutdownPolicyBaseTest::SetUpOnMainThread(); | |
216 | |
217 // Bring up the locker screen. | |
218 ScreenLocker::Show(); | |
219 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester()); | |
220 tester->EmulateWindowManagerReady(); | |
221 content::WindowedNotificationObserver lock_state_observer( | |
222 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, | |
223 content::NotificationService::AllSources()); | |
224 if (!tester->IsLocked()) | |
225 lock_state_observer.Wait(); | |
226 ScreenLocker* screen_locker = ScreenLocker::default_screen_locker(); | |
227 WebUIScreenLocker* web_ui_screen_locker = | |
228 static_cast<WebUIScreenLocker*>(screen_locker->delegate()); | |
229 ASSERT_TRUE(web_ui_screen_locker); | |
230 content::WebUI* web_ui = web_ui_screen_locker->GetWebUI(); | |
231 ASSERT_TRUE(web_ui); | |
232 contents_ = web_ui->GetWebContents(); | |
233 ASSERT_TRUE(contents_); | |
234 | |
235 // Wait for the login UI to be ready. | |
236 WaitForOobeIO( | |
237 static_cast<OobeUI*>(web_ui->GetController())); | |
238 } | |
239 | |
240 scoped_ptr<ui::ScopedAnimationDurationScaleMode> zero_duration_mode_; | |
241 FakeSessionManagerClient* fake_session_manager_client_; | |
242 | |
243 private: | |
244 DISALLOW_COPY_AND_ASSIGN(ShutdownPolicyLockerTest); | |
245 }; | |
246 | |
247 IN_PROC_BROWSER_TEST_F(ShutdownPolicyLockerTest, TestBasic) { | |
248 PrepareAndRunScript("restart-header-bar-item", true); | |
249 PrepareAndRunScript("shutdown-header-bar-item", false); | |
250 } | |
251 | |
252 IN_PROC_BROWSER_TEST_F(ShutdownPolicyLockerTest, PolicyChange) { | |
253 UpdateRebootOnShutdownPolicy(true); | |
254 RefreshDevicePolicy(); | |
255 PrepareAndRunScript("restart-header-bar-item", false); | |
256 PrepareAndRunScript("shutdown-header-bar-item", true); | |
257 | |
258 UpdateRebootOnShutdownPolicy(false); | |
259 RefreshDevicePolicy(); | |
260 PrepareAndRunScript("restart-header-bar-item", true); | |
261 PrepareAndRunScript("shutdown-header-bar-item", false); | |
262 } | |
263 | |
264 class ShutdownPolicyLoginTest : public ShutdownPolicyBaseTest { | |
265 protected: | |
266 ShutdownPolicyLoginTest() {} | |
267 | |
268 // ShutdownPolicyBaseTest: | |
269 virtual void SetUpCommandLine(CommandLine* command_line) override { | |
270 command_line->AppendSwitch(switches::kLoginManager); | |
271 command_line->AppendSwitch(switches::kForceLoginManagerInTests); | |
272 } | |
273 | |
274 virtual void SetUpInProcessBrowserTestFixture() override { | |
275 ShutdownPolicyBaseTest::SetUpInProcessBrowserTestFixture(); | |
276 InstallOwnerKey(); | |
277 MarkAsEnterpriseOwned(); | |
278 } | |
279 | |
280 virtual void SetUpOnMainThread() override { | |
281 ShutdownPolicyBaseTest::SetUpOnMainThread(); | |
282 | |
283 content::WindowedNotificationObserver( | |
284 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | |
285 content::NotificationService::AllSources()).Wait(); | |
286 LoginDisplayHostImpl* host = | |
287 static_cast<LoginDisplayHostImpl*>( | |
288 LoginDisplayHostImpl::default_host()); | |
289 ASSERT_TRUE(host); | |
290 WebUILoginView* web_ui_login_view = host->GetWebUILoginView(); | |
291 ASSERT_TRUE(web_ui_login_view); | |
292 content::WebUI* web_ui = web_ui_login_view->GetWebUI(); | |
293 ASSERT_TRUE(web_ui); | |
294 contents_ = web_ui->GetWebContents(); | |
295 ASSERT_TRUE(contents_); | |
296 | |
297 // Wait for the login UI to be ready. | |
298 WaitForOobeIO(host->GetOobeUI()); | |
299 } | |
300 | |
301 private: | |
302 DISALLOW_COPY_AND_ASSIGN(ShutdownPolicyLoginTest); | |
303 }; | |
304 | |
305 IN_PROC_BROWSER_TEST_F(ShutdownPolicyLoginTest, PolicyNotSet) { | |
306 PrepareAndRunScript("restart-header-bar-item", true); | |
307 PrepareAndRunScript("shutdown-header-bar-item", false); | |
308 } | |
309 | |
310 IN_PROC_BROWSER_TEST_F(ShutdownPolicyLoginTest, PolicyChange) { | |
311 UpdateRebootOnShutdownPolicy(true); | |
312 RefreshDevicePolicy(); | |
313 PrepareAndRunScript("restart-header-bar-item", false); | |
314 PrepareAndRunScript("shutdown-header-bar-item", true); | |
315 | |
316 UpdateRebootOnShutdownPolicy(false); | |
317 RefreshDevicePolicy(); | |
318 PrepareAndRunScript("restart-header-bar-item", true); | |
319 PrepareAndRunScript("shutdown-header-bar-item", false); | |
320 } | |
321 | |
322 } // namespace chromeos | |
OLD | NEW |