| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/webui/chromeos/login/shutdown_policy_observer.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "base/values.h" |
| 13 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 14 #include "chromeos/chromeos_switches.h" |
| 15 #include "chromeos/dbus/dbus_thread_manager.h" |
| 16 #include "chromeos/settings/cros_settings_names.h" |
| 17 #include "chromeos/settings/cros_settings_provider.h" |
| 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 namespace chromeos { |
| 22 |
| 23 class ShutdownPolicyObserverTest : public testing::Test, |
| 24 public ShutdownPolicyObserver::Delegate { |
| 25 public: |
| 26 void ResultCallback(bool reboot_on_shutdown) { |
| 27 reboot_on_shutdown_ = reboot_on_shutdown; |
| 28 callback_called_ = true; |
| 29 } |
| 30 |
| 31 protected: |
| 32 ShutdownPolicyObserverTest() |
| 33 : cros_settings_(nullptr), |
| 34 callback_called_(false), |
| 35 reboot_on_shutdown_(false), |
| 36 delegate_invocations_count_(0) { |
| 37 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 38 switches::kStubCrosSettings); |
| 39 test_cros_settings_.reset(new ScopedTestCrosSettings); |
| 40 } |
| 41 |
| 42 // testing::Test: |
| 43 virtual void SetUp() override { |
| 44 testing::Test::SetUp(); |
| 45 cros_settings_ = CrosSettings::Get(); |
| 46 DBusThreadManager::Initialize(); |
| 47 } |
| 48 |
| 49 virtual void TearDown() override { |
| 50 DBusThreadManager::Shutdown(); |
| 51 } |
| 52 |
| 53 void SetRebootOnShutdown(bool reboot_on_shutdown) { |
| 54 const base::FundamentalValue reboot_on_shutdown_value(reboot_on_shutdown); |
| 55 CrosSettings::Get() |
| 56 ->GetProvider(kRebootOnShutdown) |
| 57 ->Set(kRebootOnShutdown, reboot_on_shutdown_value); |
| 58 base::RunLoop().RunUntilIdle(); |
| 59 } |
| 60 |
| 61 // ShutdownPolicyObserver::Delegate: |
| 62 void OnShutdownPolicyChanged(bool reboot_on_shutdown) override { |
| 63 reboot_on_shutdown_ = reboot_on_shutdown; |
| 64 delegate_invocations_count_++; |
| 65 } |
| 66 |
| 67 protected: |
| 68 content::TestBrowserThreadBundle thread_bundle_; |
| 69 |
| 70 CrosSettings* cros_settings_; |
| 71 scoped_ptr<CrosSettingsProvider> device_settings_provider_; |
| 72 |
| 73 ScopedTestDeviceSettingsService test_device_settings_service_; |
| 74 scoped_ptr<ScopedTestCrosSettings> test_cros_settings_; |
| 75 |
| 76 bool callback_called_; |
| 77 bool reboot_on_shutdown_; |
| 78 int delegate_invocations_count_; |
| 79 }; |
| 80 |
| 81 TEST_F(ShutdownPolicyObserverTest, RetrieveTrustedDevicePolicies) { |
| 82 ShutdownPolicyObserver shutdown_policy_observer(cros_settings_, this); |
| 83 base::RunLoop().RunUntilIdle(); |
| 84 EXPECT_EQ(0, delegate_invocations_count_); |
| 85 |
| 86 SetRebootOnShutdown(true); |
| 87 base::RunLoop().RunUntilIdle(); |
| 88 EXPECT_EQ(1, delegate_invocations_count_); |
| 89 EXPECT_TRUE(reboot_on_shutdown_); |
| 90 |
| 91 SetRebootOnShutdown(false); |
| 92 base::RunLoop().RunUntilIdle(); |
| 93 EXPECT_EQ(2, delegate_invocations_count_); |
| 94 EXPECT_FALSE(reboot_on_shutdown_); |
| 95 |
| 96 shutdown_policy_observer.Shutdown(); |
| 97 |
| 98 SetRebootOnShutdown(true); |
| 99 base::RunLoop().RunUntilIdle(); |
| 100 EXPECT_EQ(2, delegate_invocations_count_); |
| 101 EXPECT_FALSE(reboot_on_shutdown_); |
| 102 } |
| 103 |
| 104 TEST_F(ShutdownPolicyObserverTest, CheckIfRebootOnShutdown) { |
| 105 ShutdownPolicyObserver shutdown_policy_observer(cros_settings_, this); |
| 106 base::RunLoop().RunUntilIdle(); |
| 107 |
| 108 // Allow shutdown. |
| 109 SetRebootOnShutdown(true); |
| 110 callback_called_ = false; |
| 111 shutdown_policy_observer.CheckIfRebootOnShutdown( |
| 112 base::Bind(&ShutdownPolicyObserverTest::ResultCallback, |
| 113 base::Unretained(this))); |
| 114 base::RunLoop().RunUntilIdle(); |
| 115 EXPECT_TRUE(callback_called_); |
| 116 EXPECT_EQ(true, reboot_on_shutdown_); |
| 117 // Forbid shutdown. |
| 118 SetRebootOnShutdown(false); |
| 119 callback_called_ = false; |
| 120 shutdown_policy_observer.CheckIfRebootOnShutdown( |
| 121 base::Bind(&ShutdownPolicyObserverTest::ResultCallback, |
| 122 base::Unretained(this))); |
| 123 base::RunLoop().RunUntilIdle(); |
| 124 EXPECT_TRUE(callback_called_); |
| 125 EXPECT_EQ(false, reboot_on_shutdown_); |
| 126 } |
| 127 |
| 128 } // namespace chromeos |
| OLD | NEW |