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

Unified Diff: chrome/browser/ui/webui/chromeos/login/shutdown_policy_observer_unittest.cc

Issue 776093004: Add device policy to disallow shutdown (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adressed bartfab's comments 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/ui/webui/chromeos/login/shutdown_policy_observer_unittest.cc
diff --git a/chrome/browser/ui/webui/chromeos/login/shutdown_policy_observer_unittest.cc b/chrome/browser/ui/webui/chromeos/login/shutdown_policy_observer_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..7bec7abf06229b868b1129a65e69594687ab2e18
--- /dev/null
+++ b/chrome/browser/ui/webui/chromeos/login/shutdown_policy_observer_unittest.cc
@@ -0,0 +1,135 @@
+// 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 "chrome/browser/ui/webui/chromeos/login/shutdown_policy_observer.h"
+
+#include "base/bind.h"
+#include "base/bind_helpers.h"
+#include "base/command_line.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/run_loop.h"
+#include "base/values.h"
+#include "chrome/browser/chromeos/settings/device_settings_service.h"
+#include "chrome/test/base/testing_browser_process.h"
bartfab (slow) 2014/12/16 10:24:42 Nit: No longer used.
cschuet (SLOW) 2014/12/16 10:53:00 Done.
+#include "chromeos/chromeos_switches.h"
+#include "chromeos/dbus/dbus_thread_manager.h"
+#include "chromeos/settings/cros_settings_names.h"
+#include "chromeos/settings/cros_settings_provider.h"
+#include "content/public/test/test_browser_thread_bundle.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace chromeos {
+
+class ShutdownPolicyObserverTest : public testing::Test,
+ public ShutdownPolicyObserver::Delegate {
+ public:
+ void ResultCallback(bool actual_reboot_on_shutdown) {
+ reboot_on_shutdown_ = actual_reboot_on_shutdown;
+ callback_called_ = true;
+ }
+
+ bool WasCallbackCalled() const {
+ return callback_called_;
+ }
+
+ protected:
+ ShutdownPolicyObserverTest()
+ : cros_settings_(nullptr),
+ callback_called_(false),
+ reboot_on_shutdown_(false),
+ delegate_invocations_count_(0) {
+ base::CommandLine::ForCurrentProcess()->AppendSwitch(
+ switches::kStubCrosSettings);
+ test_cros_settings_.reset(new ScopedTestCrosSettings);
+}
+
+ // testing::Test:
+ virtual void SetUp() override {
+ testing::Test::SetUp();
+ cros_settings_ = CrosSettings::Get();
+ DBusThreadManager::Initialize();
+ }
+
+ virtual void TearDown() override {
+ DBusThreadManager::Shutdown();
+ }
+
+ void SetRebootOnShutdown(bool reboot_on_shutdown) {
+ const base::FundamentalValue reboot_on_shutdown_value(reboot_on_shutdown);
+ CrosSettings::Get()
+ ->GetProvider(kRebootOnShutdown)
+ ->Set(kRebootOnShutdown, reboot_on_shutdown_value);
+ base::RunLoop().RunUntilIdle();
+ }
+
+ // ShutdownPolicyObserver::Delegate:
+ void OnShutdownPolicyChanged(bool reboot_on_shutdown) override {
+ reboot_on_shutdown_ = reboot_on_shutdown;
+ delegate_invocations_count_++;
+ }
+
+ protected:
+ content::TestBrowserThreadBundle thread_bundle_;
+
+ CrosSettings* cros_settings_;
+ scoped_ptr<CrosSettingsProvider> device_settings_provider_;
+
+ ScopedTestDeviceSettingsService test_device_settings_service_;
+ scoped_ptr<ScopedTestCrosSettings> test_cros_settings_;
+
+ bool callback_called_;
+ bool reboot_on_shutdown_;
+ int delegate_invocations_count_;
+};
+
+TEST_F(ShutdownPolicyObserverTest, RetrieveTrustedDevicePolicies) {
+ ShutdownPolicyObserver shutdown_policy_observer(cros_settings_, this);
+ base::RunLoop().RunUntilIdle();
+ EXPECT_EQ(0, delegate_invocations_count_);
+
+ SetRebootOnShutdown(true);
+ base::RunLoop().RunUntilIdle();
+ EXPECT_EQ(1, delegate_invocations_count_);
+ EXPECT_TRUE(reboot_on_shutdown_);
+
+ SetRebootOnShutdown(false);
+ base::RunLoop().RunUntilIdle();
+ EXPECT_EQ(2, delegate_invocations_count_);
+ EXPECT_FALSE(reboot_on_shutdown_);
+
+ shutdown_policy_observer.Shutdown();
+
+ SetRebootOnShutdown(true);
+ base::RunLoop().RunUntilIdle();
+ EXPECT_EQ(2, delegate_invocations_count_);
+ EXPECT_FALSE(reboot_on_shutdown_);
+}
+
+TEST_F(ShutdownPolicyObserverTest, CheckIfRebootOnShutdown) {
+ ShutdownPolicyObserver shutdown_policy_observer(cros_settings_, this);
+ base::RunLoop().RunUntilIdle();
+
+ // Allow shutdown.
+ SetRebootOnShutdown(true);
+ callback_called_ = false;
bartfab (slow) 2014/12/16 10:24:42 Optional nit: You write |callback_called_| directl
cschuet (SLOW) 2014/12/16 10:53:00 Done.
+ EXPECT_FALSE(this->WasCallbackCalled());
+ shutdown_policy_observer.CheckIfRebootOnShutdown(
+ base::Bind(&ShutdownPolicyObserverTest::ResultCallback,
+ base::Unretained(this)));
+ base::RunLoop().RunUntilIdle();
+ EXPECT_TRUE(this->WasCallbackCalled());
+ EXPECT_EQ(true, reboot_on_shutdown_);
+ // Forbid shutdown.
+ SetRebootOnShutdown(false);
+ callback_called_ = false;
+ EXPECT_FALSE(this->WasCallbackCalled());
+ shutdown_policy_observer.CheckIfRebootOnShutdown(
+ base::Bind(&ShutdownPolicyObserverTest::ResultCallback,
+ base::Unretained(this)));
+ base::RunLoop().RunUntilIdle();
+ EXPECT_TRUE(this->WasCallbackCalled());
+ EXPECT_EQ(false, reboot_on_shutdown_);
+}
+
+} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698