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

Unified Diff: chrome/browser/chromeos/shutdown_policy_browsertest.cc

Issue 811033002: Add device policy to disallow shutdown - ash UI modifications (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: shutdown_allowed => reboot_on_shutdown Created 6 years 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/shutdown_policy_browsertest.cc
diff --git a/chrome/browser/chromeos/shutdown_policy_browsertest.cc b/chrome/browser/chromeos/shutdown_policy_browsertest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1b5fa0bc30f74b508d3263531f9b689d5f59271f
--- /dev/null
+++ b/chrome/browser/chromeos/shutdown_policy_browsertest.cc
@@ -0,0 +1,322 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <string>
+
+#include "ash/shell.h"
+#include "ash/system/date/date_default_view.h"
+#include "ash/system/date/tray_date.h"
+#include "ash/system/tray/system_tray.h"
+#include "ash/system/user/login_status.h"
+#include "base/command_line.h"
+#include "base/location.h"
+#include "base/macros.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/message_loop/message_loop.h"
+#include "base/run_loop.h"
+#include "base/strings/stringprintf.h"
+#include "chrome/browser/chrome_notification_types.h"
+#include "chrome/browser/chromeos/login/lock/screen_locker.h"
+#include "chrome/browser/chromeos/login/lock/screen_locker_tester.h"
+#include "chrome/browser/chromeos/login/lock/webui_screen_locker.h"
+#include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
+#include "chrome/browser/chromeos/login/ui/webui_login_view.h"
+#include "chrome/browser/chromeos/policy/device_policy_builder.h"
+#include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
+#include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
+#include "chrome/browser/chromeos/settings/device_settings_service.h"
+#include "chrome/browser/lifetime/application_lifetime.h"
+#include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
+#include "chromeos/chromeos_switches.h"
+#include "chromeos/dbus/dbus_thread_manager.h"
+#include "chromeos/dbus/fake_session_manager_client.h"
+#include "chromeos/dbus/session_manager_client.h"
+#include "content/public/browser/notification_service.h"
+#include "content/public/browser/web_contents.h"
+#include "content/public/browser/web_ui.h"
+#include "content/public/test/browser_test_utils.h"
+#include "content/public/test/test_utils.h"
+#include "ui/compositor/scoped_animation_duration_scale_mode.h"
+#include "ui/views/view.h"
stevenjb 2014/12/17 19:09:02 Does this really need all of these dependencies, o
+
+namespace em = enterprise_management;
+
+namespace chromeos {
+
+namespace {
+
+const char kWaitForHiddenStateScript[] =
+ "var screenElement = document.getElementById('%s');"
+ "var expectation = %s;"
+ "function SendReplyIfAsExpected() {"
+ " if (screenElement.hidden != expectation)"
+ " return false;"
+ " domAutomationController.send(screenElement.hidden);"
+ " observer.disconnect();"
+ " return true;"
+ "}"
+ "var observer = new MutationObserver(SendReplyIfAsExpected);"
+ "if (!SendReplyIfAsExpected()) {"
+ " var options = { attributes: true };"
+ " observer.observe(screenElement, options);"
+ "}";
+
+} // namespace
+
+class ShutdownPolicyBaseTest
+ : public policy::DevicePolicyCrosBrowserTest,
+ public DeviceSettingsService::Observer {
+ protected:
+ ShutdownPolicyBaseTest() : contents_(nullptr) {}
+ virtual ~ShutdownPolicyBaseTest() {}
stevenjb 2014/12/17 19:09:02 override instead of virtual
+
+ // DeviceSettingsService::Observer:
+ virtual void OwnershipStatusChanged() override {}
stevenjb 2014/12/17 19:09:02 no 'virtual' in overrides Blank line between imple
+ virtual void DeviceSettingsUpdated() override {
+ if (run_loop_)
+ run_loop_->Quit();
+ }
+ virtual void OnDeviceSettingsServiceShutdown() override {}
+
+ // policy::DevicePolicyCrosBrowserTest:
+ virtual void SetUpInProcessBrowserTestFixture() override {
+ policy::DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture();
+ InstallOwnerKey();
+ MarkAsEnterpriseOwned();
+ }
+
+ // A helper functions which prepares the script by injecting the element_id of
+ // the element whose hiddenness we want to check and the expectation.
+ std::string PrepareScript(const std::string& element_id, bool expectation) {
+ return base::StringPrintf(kWaitForHiddenStateScript, element_id.c_str(),
+ expectation ? "true" : "false");
+ }
+
+ // Checks whether the element identified by |element_id| is hidden and only
+ // returns if the expectation is fulfilled.
+ void PrepareAndRunScript(const std::string& element_id, bool expectation) {
+ ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
+ contents_, PrepareScript(element_id, expectation),
+ &result_));
+ }
+
+ // Updates the device shutdown policy and sets it to |reboot_on_shutdown|.
+ void UpdateRebootOnShutdownPolicy(bool reboot_on_shutdown) {
+ policy::DevicePolicyBuilder* builder = device_policy();
+ ASSERT_TRUE(builder);
+ em::ChromeDeviceSettingsProto& proto(builder->payload());
+ proto.mutable_reboot_on_shutdown()->set_reboot_on_shutdown(
+ reboot_on_shutdown);
+ }
+
+ // Refreshes device policy and waits for it to be applied.
+ virtual void SyncRefreshDevicePolicy() {
+ run_loop_.reset(new base::RunLoop());
+ DeviceSettingsService::Get()->AddObserver(this);
+ RefreshDevicePolicy();
+ run_loop_->Run();
+ DeviceSettingsService::Get()->RemoveObserver(this);
+ run_loop_.reset();
+ }
+
+ // Blocks until the OobeUI indicates that the javascript side has been
+ // initialized.
+ void WaitForOobeIO(OobeUI* oobe_ui) {
+ ASSERT_TRUE(oobe_ui);
+ base::RunLoop run_loop;
+ const bool oobe_ui_ready = oobe_ui->IsJSReady(run_loop.QuitClosure());
+ if (!oobe_ui_ready)
+ run_loop.Run();
+ }
+
+ content::WebContents* contents_;
+ bool result_;
+ scoped_ptr<base::RunLoop> run_loop_;
+};
+
+class ShutdownPolicyInSessionTest
+ : public ShutdownPolicyBaseTest {
+ protected:
+ ShutdownPolicyInSessionTest() {}
+ virtual ~ShutdownPolicyInSessionTest() {}
+
+ virtual void SetUpOnMainThread() override {
+ ShutdownPolicyBaseTest::SetUpOnMainThread();
+ ash::TrayDate* tray_date = ash::Shell::GetInstance()
+ ->GetPrimarySystemTray()
+ ->GetTrayDateForTesting();
+ ASSERT_TRUE(tray_date);
+ date_default_view_.reset(
+ static_cast<ash::DateDefaultView*>(
+ tray_date->CreateDefaultViewForTesting(ash::user::LOGGED_IN_USER)));
+ ASSERT_TRUE(date_default_view_);
+ }
+
+ void TearDownOnMainThread() override {
+ date_default_view_.reset();
+ ShutdownPolicyBaseTest::TearDownOnMainThread();
+ }
+
+ // Get the shutdown and reboot button view from the date default view.
+ void GetShutdownAndRebootButton(views::View** shutdown_button,
+ views::View** reboot_button) {
+ *shutdown_button = date_default_view_->GetShutdownButtonView();
+ ASSERT_TRUE(*shutdown_button);
+ *reboot_button = date_default_view_->GetRebootButtonView();
+ ASSERT_TRUE(*reboot_button);
+ }
+ scoped_ptr<ash::DateDefaultView> date_default_view_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(ShutdownPolicyInSessionTest);
+};
+
+IN_PROC_BROWSER_TEST_F(ShutdownPolicyInSessionTest, TestBasic) {
+ views::View *shutdown_button, *reboot_button;
+ GetShutdownAndRebootButton(&shutdown_button, &reboot_button);
+ EXPECT_TRUE(shutdown_button->visible());
+ EXPECT_FALSE(reboot_button->visible());
+}
+
+IN_PROC_BROWSER_TEST_F(ShutdownPolicyInSessionTest, PolicyChange) {
+ views::View *shutdown_button, *reboot_button;
+ GetShutdownAndRebootButton(&shutdown_button, &reboot_button);
+
+ UpdateRebootOnShutdownPolicy(true);
+ SyncRefreshDevicePolicy();
+ EXPECT_FALSE(shutdown_button->visible());
+ EXPECT_TRUE(reboot_button->visible());
+
+ UpdateRebootOnShutdownPolicy(false);
+ SyncRefreshDevicePolicy();
+ EXPECT_TRUE(shutdown_button->visible());
+ EXPECT_FALSE(reboot_button->visible());
+}
+
+class ShutdownPolicyLockerTest : public ShutdownPolicyBaseTest {
+ protected:
+ ShutdownPolicyLockerTest() : fake_session_manager_client_(nullptr) {}
+ virtual ~ShutdownPolicyLockerTest() {}
+
+ virtual void SetUpInProcessBrowserTestFixture() override {
+ fake_session_manager_client_ = new FakeSessionManagerClient;
+ DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient(
+ scoped_ptr<SessionManagerClient>(fake_session_manager_client_));
+
+ ShutdownPolicyBaseTest::SetUpInProcessBrowserTestFixture();
+ zero_duration_mode_.reset(new ui::ScopedAnimationDurationScaleMode(
+ ui::ScopedAnimationDurationScaleMode::ZERO_DURATION));
+ InstallOwnerKey();
+ MarkAsEnterpriseOwned();
+ }
+
+ virtual void SetUpOnMainThread() override {
+ ShutdownPolicyBaseTest::SetUpOnMainThread();
+
+ // Bring up the locker screen.
+ ScreenLocker::Show();
+ scoped_ptr<test::ScreenLockerTester> tester(ScreenLocker::GetTester());
+ tester->EmulateWindowManagerReady();
+ content::WindowedNotificationObserver lock_state_observer(
+ chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
+ content::NotificationService::AllSources());
+ if (!tester->IsLocked())
+ lock_state_observer.Wait();
+ ScreenLocker* screen_locker = ScreenLocker::default_screen_locker();
+ WebUIScreenLocker* web_ui_screen_locker =
+ static_cast<WebUIScreenLocker*>(screen_locker->delegate());
+ ASSERT_TRUE(web_ui_screen_locker);
+ content::WebUI* web_ui = web_ui_screen_locker->GetWebUI();
+ ASSERT_TRUE(web_ui);
+ contents_ = web_ui->GetWebContents();
+ ASSERT_TRUE(contents_);
+
+ // Wait for the login UI to be ready.
+ WaitForOobeIO(
+ static_cast<OobeUI*>(web_ui->GetController()));
+ }
+
+ scoped_ptr<ui::ScopedAnimationDurationScaleMode> zero_duration_mode_;
+ FakeSessionManagerClient* fake_session_manager_client_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(ShutdownPolicyLockerTest);
+};
+
+IN_PROC_BROWSER_TEST_F(ShutdownPolicyLockerTest, TestBasic) {
+ PrepareAndRunScript("restart-header-bar-item", true);
+ PrepareAndRunScript("shutdown-header-bar-item", false);
+}
+
+IN_PROC_BROWSER_TEST_F(ShutdownPolicyLockerTest, PolicyChange) {
+ UpdateRebootOnShutdownPolicy(true);
+ RefreshDevicePolicy();
+ PrepareAndRunScript("restart-header-bar-item", false);
+ PrepareAndRunScript("shutdown-header-bar-item", true);
+
+ UpdateRebootOnShutdownPolicy(false);
+ RefreshDevicePolicy();
+ PrepareAndRunScript("restart-header-bar-item", true);
+ PrepareAndRunScript("shutdown-header-bar-item", false);
+}
+
+class ShutdownPolicyLoginTest : public ShutdownPolicyBaseTest {
+ protected:
+ ShutdownPolicyLoginTest() {}
+
+ // ShutdownPolicyBaseTest:
+ virtual void SetUpCommandLine(CommandLine* command_line) override {
+ command_line->AppendSwitch(switches::kLoginManager);
+ command_line->AppendSwitch(switches::kForceLoginManagerInTests);
+ }
+
+ virtual void SetUpInProcessBrowserTestFixture() override {
+ ShutdownPolicyBaseTest::SetUpInProcessBrowserTestFixture();
+ InstallOwnerKey();
+ MarkAsEnterpriseOwned();
+ }
+
+ virtual void SetUpOnMainThread() override {
+ ShutdownPolicyBaseTest::SetUpOnMainThread();
+
+ content::WindowedNotificationObserver(
+ chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
+ content::NotificationService::AllSources()).Wait();
+ LoginDisplayHostImpl* host =
+ static_cast<LoginDisplayHostImpl*>(
+ LoginDisplayHostImpl::default_host());
+ ASSERT_TRUE(host);
+ WebUILoginView* web_ui_login_view = host->GetWebUILoginView();
+ ASSERT_TRUE(web_ui_login_view);
+ content::WebUI* web_ui = web_ui_login_view->GetWebUI();
+ ASSERT_TRUE(web_ui);
+ contents_ = web_ui->GetWebContents();
+ ASSERT_TRUE(contents_);
+
+ // Wait for the login UI to be ready.
+ WaitForOobeIO(host->GetOobeUI());
+ }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(ShutdownPolicyLoginTest);
+};
+
+IN_PROC_BROWSER_TEST_F(ShutdownPolicyLoginTest, PolicyNotSet) {
+ PrepareAndRunScript("restart-header-bar-item", true);
+ PrepareAndRunScript("shutdown-header-bar-item", false);
+}
+
+IN_PROC_BROWSER_TEST_F(ShutdownPolicyLoginTest, PolicyChange) {
+ UpdateRebootOnShutdownPolicy(true);
+ RefreshDevicePolicy();
+ PrepareAndRunScript("restart-header-bar-item", false);
+ PrepareAndRunScript("shutdown-header-bar-item", true);
+
+ UpdateRebootOnShutdownPolicy(false);
+ RefreshDevicePolicy();
+ PrepareAndRunScript("restart-header-bar-item", true);
+ PrepareAndRunScript("shutdown-header-bar-item", false);
+}
+
+} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698