Chromium Code Reviews| 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 |