| 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..119e71bd8ae47bd868bbc2b28872706e0baed8d0
|
| --- /dev/null
|
| +++ b/chrome/browser/ui/webui/chromeos/login/shutdown_policy_observer_unittest.cc
|
| @@ -0,0 +1,148 @@
|
| +// 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/run_loop.h"
|
| +#include "base/values.h"
|
| +#include "chrome/browser/chromeos/settings/device_settings_service.h"
|
| +#include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h"
|
| +#include "chrome/test/base/testing_browser_process.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 ExpectationCheckerCallback(bool actual_shutdown_allowed) {
|
| + EXPECT_EQ(expected_shutdown_allowed_, actual_shutdown_allowed);
|
| + callback_called_ = true;
|
| + }
|
| +
|
| + bool WasCallbackCalled() const {
|
| + return callback_called_;
|
| + }
|
| +
|
| + protected:
|
| + ShutdownPolicyObserverTest()
|
| + : cros_settings_(nullptr),
|
| + stub_settings_provider_(new StubCrosSettingsProvider),
|
| + callback_called_(false),
|
| + expected_shutdown_allowed_(false),
|
| + shutdown_allowed_(false),
|
| + count_delegate_invocations_(0) {}
|
| +
|
| + virtual void SetUp() override {
|
| + cros_settings_ = CrosSettings::Get();
|
| +
|
| + // Replace the real DeviceSettingsProvider with a stub.
|
| + device_settings_provider_.reset(
|
| + cros_settings_->GetProvider(chromeos::kShutdownAllowed));
|
| + EXPECT_TRUE(device_settings_provider_);
|
| + EXPECT_TRUE(cros_settings_->RemoveSettingsProvider(
|
| + device_settings_provider_.get()));
|
| + cros_settings_->AddSettingsProvider(stub_settings_provider_);
|
| +
|
| + chromeos::DBusThreadManager::Initialize();
|
| + }
|
| +
|
| + virtual void TearDown() override {
|
| + // Restore the real DeviceSettingsProvider.
|
| + EXPECT_TRUE(
|
| + cros_settings_->RemoveSettingsProvider(stub_settings_provider_));
|
| + cros_settings_->AddSettingsProvider(device_settings_provider_.release());
|
| +
|
| + // Shut down the DeviceSettingsService.
|
| + DeviceSettingsService::Get()->UnsetSessionManager();
|
| + TestingBrowserProcess::GetGlobal()->SetProfileManager(nullptr);
|
| + base::RunLoop().RunUntilIdle();
|
| + chromeos::DBusThreadManager::Shutdown();
|
| + }
|
| +
|
| + void SetDeviceSettings(bool shutdown_allowed) {
|
| + const base::FundamentalValue shutdown_allowed_value(shutdown_allowed);
|
| + stub_settings_provider_->Set(kShutdownAllowed, shutdown_allowed_value);
|
| + base::RunLoop().RunUntilIdle();
|
| + }
|
| +
|
| + void SetExpectation(bool expected_shutdown_allowed) {
|
| + expected_shutdown_allowed_ = expected_shutdown_allowed;
|
| + callback_called_ = false;
|
| + }
|
| +
|
| + void OnShutdownPolicyChanged(bool shutdown_allowed) override {
|
| + shutdown_allowed_ = shutdown_allowed;
|
| + count_delegate_invocations_++;
|
| + }
|
| +
|
| + protected:
|
| + content::TestBrowserThreadBundle thread_bundle_;
|
| +
|
| + CrosSettings* cros_settings_;
|
| + scoped_ptr<CrosSettingsProvider> device_settings_provider_;
|
| + StubCrosSettingsProvider* stub_settings_provider_;
|
| +
|
| + ScopedTestDeviceSettingsService test_device_settings_service_;
|
| + ScopedTestCrosSettings test_cros_settings_;
|
| +
|
| + bool callback_called_;
|
| + bool expected_shutdown_allowed_;
|
| + bool shutdown_allowed_;
|
| + int count_delegate_invocations_;
|
| +};
|
| +
|
| +TEST_F(ShutdownPolicyObserverTest, RetrieveTrustedDevicePolicies) {
|
| + ShutdownPolicyObserver shutdown_policy_observer(cros_settings_, this);
|
| + base::RunLoop().RunUntilIdle();
|
| + EXPECT_EQ(0, count_delegate_invocations_);
|
| +
|
| + SetDeviceSettings(true);
|
| + base::RunLoop().RunUntilIdle();
|
| + EXPECT_EQ(1, count_delegate_invocations_);
|
| + EXPECT_TRUE(shutdown_allowed_);
|
| +
|
| + SetDeviceSettings(false);
|
| + base::RunLoop().RunUntilIdle();
|
| + EXPECT_EQ(2, count_delegate_invocations_);
|
| + EXPECT_FALSE(shutdown_allowed_);
|
| +
|
| + shutdown_policy_observer.Shutdown();
|
| +
|
| + SetDeviceSettings(true);
|
| + base::RunLoop().RunUntilIdle();
|
| + EXPECT_EQ(2, count_delegate_invocations_);
|
| + EXPECT_FALSE(shutdown_allowed_);
|
| +}
|
| +
|
| +TEST_F(ShutdownPolicyObserverTest, CheckIfShutdownAllowedTest) {
|
| + ShutdownPolicyObserver shutdown_policy_observer(cros_settings_, this);
|
| + base::RunLoop().RunUntilIdle();
|
| +
|
| + // Allow shutdown.
|
| + SetDeviceSettings(true);
|
| + SetExpectation(true);
|
| + EXPECT_FALSE(this->WasCallbackCalled());
|
| + shutdown_policy_observer.CheckIfShutdownAllowed(
|
| + base::Bind(&ShutdownPolicyObserverTest::ExpectationCheckerCallback,
|
| + base::Unretained(this)));
|
| + base::RunLoop().RunUntilIdle();
|
| + EXPECT_TRUE(this->WasCallbackCalled());
|
| +
|
| + // Forbid shutdown.
|
| + SetDeviceSettings(false);
|
| + SetExpectation(false);
|
| + EXPECT_FALSE(this->WasCallbackCalled());
|
| + shutdown_policy_observer.CheckIfShutdownAllowed(
|
| + base::Bind(&ShutdownPolicyObserverTest::ExpectationCheckerCallback,
|
| + base::Unretained(this)));
|
| + base::RunLoop().RunUntilIdle();
|
| + EXPECT_TRUE(this->WasCallbackCalled());
|
| +}
|
| +
|
| +} // namespace chromeos
|
|
|