| 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/run_loop.h" |
| 8 #include "base/values.h" |
| 9 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
| 10 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" |
| 11 #include "chrome/test/base/testing_browser_process.h" |
| 12 #include "chromeos/dbus/dbus_thread_manager.h" |
| 13 #include "chromeos/settings/cros_settings_names.h" |
| 14 #include "chromeos/settings/cros_settings_provider.h" |
| 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace chromeos { |
| 19 |
| 20 class ShutdownPolicyObserverTest : public testing::Test, |
| 21 public ShutdownPolicyObserver::Delegate { |
| 22 public: |
| 23 void ExpectationCheckerCallback(bool actual_shutdown_allowed) { |
| 24 EXPECT_EQ(expected_shutdown_allowed_, actual_shutdown_allowed); |
| 25 callback_called_ = true; |
| 26 } |
| 27 |
| 28 bool WasCallbackCalled() const { |
| 29 return callback_called_; |
| 30 } |
| 31 |
| 32 protected: |
| 33 ShutdownPolicyObserverTest() |
| 34 : cros_settings_(nullptr), |
| 35 stub_settings_provider_(new StubCrosSettingsProvider), |
| 36 callback_called_(false), |
| 37 expected_shutdown_allowed_(false), |
| 38 shutdown_allowed_(false), |
| 39 count_delegate_invocations_(0) {} |
| 40 |
| 41 virtual void SetUp() override { |
| 42 cros_settings_ = CrosSettings::Get(); |
| 43 |
| 44 // Replace the real DeviceSettingsProvider with a stub. |
| 45 device_settings_provider_.reset( |
| 46 cros_settings_->GetProvider(chromeos::kShutdownAllowed)); |
| 47 EXPECT_TRUE(device_settings_provider_); |
| 48 EXPECT_TRUE(cros_settings_->RemoveSettingsProvider( |
| 49 device_settings_provider_.get())); |
| 50 cros_settings_->AddSettingsProvider(stub_settings_provider_); |
| 51 |
| 52 chromeos::DBusThreadManager::Initialize(); |
| 53 } |
| 54 |
| 55 virtual void TearDown() override { |
| 56 // Restore the real DeviceSettingsProvider. |
| 57 EXPECT_TRUE( |
| 58 cros_settings_->RemoveSettingsProvider(stub_settings_provider_)); |
| 59 cros_settings_->AddSettingsProvider(device_settings_provider_.release()); |
| 60 |
| 61 // Shut down the DeviceSettingsService. |
| 62 DeviceSettingsService::Get()->UnsetSessionManager(); |
| 63 TestingBrowserProcess::GetGlobal()->SetProfileManager(nullptr); |
| 64 base::RunLoop().RunUntilIdle(); |
| 65 chromeos::DBusThreadManager::Shutdown(); |
| 66 } |
| 67 |
| 68 void SetDeviceSettings(bool shutdown_allowed) { |
| 69 const base::FundamentalValue shutdown_allowed_value(shutdown_allowed); |
| 70 stub_settings_provider_->Set(kShutdownAllowed, shutdown_allowed_value); |
| 71 base::RunLoop().RunUntilIdle(); |
| 72 } |
| 73 |
| 74 void SetExpectation(bool expected_shutdown_allowed) { |
| 75 expected_shutdown_allowed_ = expected_shutdown_allowed; |
| 76 callback_called_ = false; |
| 77 } |
| 78 |
| 79 void OnShutdownPolicyChanged(bool shutdown_allowed) override { |
| 80 shutdown_allowed_ = shutdown_allowed; |
| 81 count_delegate_invocations_++; |
| 82 } |
| 83 |
| 84 protected: |
| 85 content::TestBrowserThreadBundle thread_bundle_; |
| 86 |
| 87 CrosSettings* cros_settings_; |
| 88 scoped_ptr<CrosSettingsProvider> device_settings_provider_; |
| 89 StubCrosSettingsProvider* stub_settings_provider_; |
| 90 |
| 91 ScopedTestDeviceSettingsService test_device_settings_service_; |
| 92 ScopedTestCrosSettings test_cros_settings_; |
| 93 |
| 94 bool callback_called_; |
| 95 bool expected_shutdown_allowed_; |
| 96 bool shutdown_allowed_; |
| 97 int count_delegate_invocations_; |
| 98 }; |
| 99 |
| 100 TEST_F(ShutdownPolicyObserverTest, RetrieveTrustedDevicePolicies) { |
| 101 ShutdownPolicyObserver shutdown_policy_observer(cros_settings_, this); |
| 102 base::RunLoop().RunUntilIdle(); |
| 103 EXPECT_EQ(0, count_delegate_invocations_); |
| 104 |
| 105 SetDeviceSettings(true); |
| 106 base::RunLoop().RunUntilIdle(); |
| 107 EXPECT_EQ(1, count_delegate_invocations_); |
| 108 EXPECT_TRUE(shutdown_allowed_); |
| 109 |
| 110 SetDeviceSettings(false); |
| 111 base::RunLoop().RunUntilIdle(); |
| 112 EXPECT_EQ(2, count_delegate_invocations_); |
| 113 EXPECT_FALSE(shutdown_allowed_); |
| 114 |
| 115 shutdown_policy_observer.Shutdown(); |
| 116 |
| 117 SetDeviceSettings(true); |
| 118 base::RunLoop().RunUntilIdle(); |
| 119 EXPECT_EQ(2, count_delegate_invocations_); |
| 120 EXPECT_FALSE(shutdown_allowed_); |
| 121 } |
| 122 |
| 123 TEST_F(ShutdownPolicyObserverTest, CheckIfShutdownAllowedTest) { |
| 124 ShutdownPolicyObserver shutdown_policy_observer(cros_settings_, this); |
| 125 base::RunLoop().RunUntilIdle(); |
| 126 |
| 127 // Allow shutdown. |
| 128 SetDeviceSettings(true); |
| 129 SetExpectation(true); |
| 130 EXPECT_FALSE(this->WasCallbackCalled()); |
| 131 shutdown_policy_observer.CheckIfShutdownAllowed( |
| 132 base::Bind(&ShutdownPolicyObserverTest::ExpectationCheckerCallback, |
| 133 base::Unretained(this))); |
| 134 base::RunLoop().RunUntilIdle(); |
| 135 EXPECT_TRUE(this->WasCallbackCalled()); |
| 136 |
| 137 // Forbid shutdown. |
| 138 SetDeviceSettings(false); |
| 139 SetExpectation(false); |
| 140 EXPECT_FALSE(this->WasCallbackCalled()); |
| 141 shutdown_policy_observer.CheckIfShutdownAllowed( |
| 142 base::Bind(&ShutdownPolicyObserverTest::ExpectationCheckerCallback, |
| 143 base::Unretained(this))); |
| 144 base::RunLoop().RunUntilIdle(); |
| 145 EXPECT_TRUE(this->WasCallbackCalled()); |
| 146 } |
| 147 |
| 148 } // namespace chromeos |
| OLD | NEW |