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

Side by Side Diff: chrome/browser/chromeos/shutdown_policy_browsertest.cc

Issue 871483003: Add device policy to disallow shutdown - WebUI modifications (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase ygorshenin's proposed changes Created 5 years, 11 months 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
(Empty)
1 // Copyright 2015 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/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 ~ShutdownPolicyBaseTest() override {}
76
77 // DeviceSettingsService::Observer:
78 void OwnershipStatusChanged() override {}
79 void DeviceSettingsUpdated() override {
80 if (run_loop_)
81 run_loop_->Quit();
82 }
83 void OnDeviceSettingsServiceShutdown() override {}
84
85 // policy::DevicePolicyCrosBrowserTest:
86 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 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 WaitUntilOobeUIIsReady(OobeUI* oobe_ui) {
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 ~ShutdownPolicyInSessionTest() override {}
146
147 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 const ash::TrayPopupHeaderButton* GetShutdownButton() {
166 return static_cast<const ash::TrayPopupHeaderButton*>(
167 date_default_view_->GetShutdownButtonViewForTest());
168 }
169
170 bool HasButtonTooltipText(const ash::TrayPopupHeaderButton* button,
171 int message_id) {
oshima 2015/01/23 21:43:37 nit: const method?
cschuet (SLOW) 2015/01/24 10:18:53 Done.
172 base::string16 actual_tooltip;
173 button->GetTooltipText(gfx::Point(), &actual_tooltip);
174 return l10n_util::GetStringUTF16(message_id) == actual_tooltip;
175 }
oshima 2015/01/23 21:43:37 new line between } and private:
cschuet (SLOW) 2015/01/24 10:18:53 Done.
176 private:
177 scoped_ptr<ash::DateDefaultView> date_default_view_;
178
179 DISALLOW_COPY_AND_ASSIGN(ShutdownPolicyInSessionTest);
180 };
181
182 IN_PROC_BROWSER_TEST_F(ShutdownPolicyInSessionTest, TestBasic) {
183 const ash::TrayPopupHeaderButton *shutdown_button = GetShutdownButton();
184 EXPECT_TRUE(
185 HasButtonTooltipText(shutdown_button, IDS_ASH_STATUS_TRAY_SHUTDOWN));
186 }
187
188 IN_PROC_BROWSER_TEST_F(ShutdownPolicyInSessionTest, PolicyChange) {
189 const ash::TrayPopupHeaderButton *shutdown_button = GetShutdownButton();
190
191 UpdateRebootOnShutdownPolicy(true);
192 SyncRefreshDevicePolicy();
193 EXPECT_TRUE(
194 HasButtonTooltipText(shutdown_button, IDS_ASH_STATUS_TRAY_REBOOT));
195
196 UpdateRebootOnShutdownPolicy(false);
197 SyncRefreshDevicePolicy();
198 EXPECT_TRUE(
199 HasButtonTooltipText(shutdown_button, IDS_ASH_STATUS_TRAY_SHUTDOWN));
200 }
201
202 class ShutdownPolicyLockerTest : public ShutdownPolicyBaseTest {
203 protected:
204 ShutdownPolicyLockerTest() : fake_session_manager_client_(nullptr) {}
205 ~ShutdownPolicyLockerTest() override {}
206
207 void SetUpInProcessBrowserTestFixture() override {
208 fake_session_manager_client_ = new FakeSessionManagerClient;
209 DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient(
210 scoped_ptr<SessionManagerClient>(fake_session_manager_client_));
211
212 ShutdownPolicyBaseTest::SetUpInProcessBrowserTestFixture();
213 zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
214 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
215 InstallOwnerKey();
216 MarkAsEnterpriseOwned();
217 }
218
219 void SetUpOnMainThread() override {
220 ShutdownPolicyBaseTest::SetUpOnMainThread();
221
222 // Bring up the locker screen.
223 ScreenLocker::Show();
224 scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
225 tester->EmulateWindowManagerReady();
226 content::WindowedNotificationObserver lock_state_observer(
227 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
228 content::NotificationService::AllSources());
229 if (!tester->IsLocked())
230 lock_state_observer.Wait();
231 ScreenLocker* screen_locker = ScreenLocker::default_screen_locker();
232 WebUIScreenLocker* web_ui_screen_locker =
233 static_cast<WebUIScreenLocker*>(screen_locker->delegate());
234 ASSERT_TRUE(web_ui_screen_locker);
235 content::WebUI* web_ui = web_ui_screen_locker->GetWebUI();
236 ASSERT_TRUE(web_ui);
237 contents_ = web_ui->GetWebContents();
238 ASSERT_TRUE(contents_);
239
240 // Wait for the login UI to be ready.
241 WaitUntilOobeUIIsReady(
242 static_cast<OobeUI*>(web_ui->GetController()));
243 }
244
245 scoped_ptr<ui::ScopedAnimationDurationScaleMode> zero_duration_mode_;
246 FakeSessionManagerClient* fake_session_manager_client_;
247
248 private:
oshima 2015/01/23 21:43:37 nit: move his before member variables
cschuet (SLOW) 2015/01/24 10:18:53 Done.
249 DISALLOW_COPY_AND_ASSIGN(ShutdownPolicyLockerTest);
250 };
251
252 IN_PROC_BROWSER_TEST_F(ShutdownPolicyLockerTest, TestBasic) {
253 PrepareAndRunScript("restart-header-bar-item", true);
254 PrepareAndRunScript("shutdown-header-bar-item", false);
255 }
256
257 IN_PROC_BROWSER_TEST_F(ShutdownPolicyLockerTest, PolicyChange) {
258 UpdateRebootOnShutdownPolicy(true);
259 RefreshDevicePolicy();
260 PrepareAndRunScript("restart-header-bar-item", false);
261 PrepareAndRunScript("shutdown-header-bar-item", true);
262
263 UpdateRebootOnShutdownPolicy(false);
264 RefreshDevicePolicy();
265 PrepareAndRunScript("restart-header-bar-item", true);
266 PrepareAndRunScript("shutdown-header-bar-item", false);
267 }
268
269 class ShutdownPolicyLoginTest : public ShutdownPolicyBaseTest {
270 protected:
271 ShutdownPolicyLoginTest() {}
272 ~ShutdownPolicyLoginTest() override {}
oshima 2015/01/23 21:43:37 nit: new line
cschuet (SLOW) 2015/01/24 10:18:53 Done.
273 // ShutdownPolicyBaseTest:
274 void SetUpCommandLine(base::CommandLine* command_line) override {
275 command_line->AppendSwitch(switches::kLoginManager);
276 command_line->AppendSwitch(switches::kForceLoginManagerInTests);
277 }
278
279 void SetUpInProcessBrowserTestFixture() override {
280 ShutdownPolicyBaseTest::SetUpInProcessBrowserTestFixture();
281 InstallOwnerKey();
282 MarkAsEnterpriseOwned();
283 }
284
285 void SetUpOnMainThread() override {
286 ShutdownPolicyBaseTest::SetUpOnMainThread();
287
288 content::WindowedNotificationObserver(
289 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
290 content::NotificationService::AllSources()).Wait();
291 LoginDisplayHostImpl* host =
292 static_cast<LoginDisplayHostImpl*>(
293 LoginDisplayHostImpl::default_host());
294 ASSERT_TRUE(host);
295 WebUILoginView* web_ui_login_view = host->GetWebUILoginView();
296 ASSERT_TRUE(web_ui_login_view);
297 content::WebUI* web_ui = web_ui_login_view->GetWebUI();
298 ASSERT_TRUE(web_ui);
299 contents_ = web_ui->GetWebContents();
300 ASSERT_TRUE(contents_);
301
302 // Wait for the login UI to be ready.
303 WaitUntilOobeUIIsReady(host->GetOobeUI());
304 }
305
306 void TearDownOnMainThread() override {
307 // If the login display is still showing, exit gracefully.
308 if (LoginDisplayHostImpl::default_host()) {
309 base::MessageLoop::current()->PostTask(FROM_HERE,
310 base::Bind(&chrome::AttemptExit));
311 content::RunMessageLoop();
312 }
313 }
314
315 private:
316 DISALLOW_COPY_AND_ASSIGN(ShutdownPolicyLoginTest);
317 };
318
319 IN_PROC_BROWSER_TEST_F(ShutdownPolicyLoginTest, PolicyNotSet) {
320 PrepareAndRunScript("restart-header-bar-item", true);
321 PrepareAndRunScript("shutdown-header-bar-item", false);
322 }
323
324 IN_PROC_BROWSER_TEST_F(ShutdownPolicyLoginTest, PolicyChange) {
325 UpdateRebootOnShutdownPolicy(true);
326 RefreshDevicePolicy();
327 PrepareAndRunScript("restart-header-bar-item", false);
328 PrepareAndRunScript("shutdown-header-bar-item", true);
329
330 UpdateRebootOnShutdownPolicy(false);
331 RefreshDevicePolicy();
332 PrepareAndRunScript("restart-header-bar-item", true);
333 PrepareAndRunScript("shutdown-header-bar-item", false);
334 }
335
336 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/settings/shutdown_policy_handler.cc ('k') | chrome/browser/resources/chromeos/login/header_bar.css » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698