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