| 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..4136f994f68276b88b44640de398e3b37412f45a
|
| --- /dev/null
|
| +++ b/chrome/browser/ui/webui/chromeos/login/shutdown_policy_observer_unittest.cc
|
| @@ -0,0 +1,128 @@
|
| +// 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 "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 reboot_on_shutdown) {
|
| + reboot_on_shutdown_ = reboot_on_shutdown;
|
| + callback_called_ = true;
|
| + }
|
| +
|
| + 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;
|
| + shutdown_policy_observer.CheckIfRebootOnShutdown(
|
| + base::Bind(&ShutdownPolicyObserverTest::ResultCallback,
|
| + base::Unretained(this)));
|
| + base::RunLoop().RunUntilIdle();
|
| + EXPECT_TRUE(callback_called_);
|
| + EXPECT_EQ(true, reboot_on_shutdown_);
|
| + // Forbid shutdown.
|
| + SetRebootOnShutdown(false);
|
| + callback_called_ = false;
|
| + shutdown_policy_observer.CheckIfRebootOnShutdown(
|
| + base::Bind(&ShutdownPolicyObserverTest::ResultCallback,
|
| + base::Unretained(this)));
|
| + base::RunLoop().RunUntilIdle();
|
| + EXPECT_TRUE(callback_called_);
|
| + EXPECT_EQ(false, reboot_on_shutdown_);
|
| +}
|
| +
|
| +} // namespace chromeos
|
|
|