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