| 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/scoped_cros_settings_test_helper.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, |
| 24 public ShutdownPolicyHandler::Delegate { | 21 public ShutdownPolicyHandler::Delegate { |
| 25 public: | 22 public: |
| 26 void ResultCallback(bool reboot_on_shutdown) { | 23 void ResultCallback(bool reboot_on_shutdown) { |
| 27 reboot_on_shutdown_ = reboot_on_shutdown; | 24 reboot_on_shutdown_ = reboot_on_shutdown; |
| 28 callback_called_ = true; | 25 callback_called_ = true; |
| 29 } | 26 } |
| 30 | 27 |
| 31 protected: | 28 protected: |
| 32 ShutdownPolicyHandlerTest() | 29 ShutdownPolicyHandlerTest() |
| 33 : cros_settings_(nullptr), | 30 : callback_called_(false), |
| 34 callback_called_(false), | |
| 35 reboot_on_shutdown_(false), | 31 reboot_on_shutdown_(false), |
| 36 delegate_invocations_count_(0) { | 32 delegate_invocations_count_(0) {} |
| 37 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 38 switches::kStubCrosSettings); | |
| 39 test_cros_settings_.reset(new ScopedTestCrosSettings); | |
| 40 } | |
| 41 | 33 |
| 42 // testing::Test: | 34 // testing::Test: |
| 43 void SetUp() override { | 35 void SetUp() override { |
| 44 testing::Test::SetUp(); | 36 testing::Test::SetUp(); |
| 45 cros_settings_ = CrosSettings::Get(); | |
| 46 DBusThreadManager::Initialize(); | 37 DBusThreadManager::Initialize(); |
| 38 settings_helper_.ReplaceProvider(kRebootOnShutdown); |
| 47 } | 39 } |
| 48 | 40 |
| 49 void TearDown() override { DBusThreadManager::Shutdown(); } | 41 void TearDown() override { DBusThreadManager::Shutdown(); } |
| 50 | 42 |
| 51 void SetRebootOnShutdown(bool reboot_on_shutdown) { | 43 void SetRebootOnShutdown(bool reboot_on_shutdown) { |
| 52 const base::FundamentalValue reboot_on_shutdown_value(reboot_on_shutdown); | 44 settings_helper_.SetBoolean(kRebootOnShutdown, reboot_on_shutdown); |
| 53 CrosSettings::Get() | |
| 54 ->GetProvider(kRebootOnShutdown) | |
| 55 ->Set(kRebootOnShutdown, reboot_on_shutdown_value); | |
| 56 base::RunLoop().RunUntilIdle(); | 45 base::RunLoop().RunUntilIdle(); |
| 57 } | 46 } |
| 58 | 47 |
| 59 // ShutdownPolicyHandler::Delegate: | 48 // ShutdownPolicyHandler::Delegate: |
| 60 void OnShutdownPolicyChanged(bool reboot_on_shutdown) override { | 49 void OnShutdownPolicyChanged(bool reboot_on_shutdown) override { |
| 61 reboot_on_shutdown_ = reboot_on_shutdown; | 50 reboot_on_shutdown_ = reboot_on_shutdown; |
| 62 delegate_invocations_count_++; | 51 delegate_invocations_count_++; |
| 63 } | 52 } |
| 64 | 53 |
| 65 protected: | 54 protected: |
| 66 content::TestBrowserThreadBundle thread_bundle_; | 55 content::TestBrowserThreadBundle thread_bundle_; |
| 67 | 56 ScopedCrosSettingsTestHelper settings_helper_; |
| 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_; | 57 bool callback_called_; |
| 75 bool reboot_on_shutdown_; | 58 bool reboot_on_shutdown_; |
| 76 int delegate_invocations_count_; | 59 int delegate_invocations_count_; |
| 77 }; | 60 }; |
| 78 | 61 |
| 79 TEST_F(ShutdownPolicyHandlerTest, RetrieveTrustedDevicePolicies) { | 62 TEST_F(ShutdownPolicyHandlerTest, RetrieveTrustedDevicePolicies) { |
| 80 ShutdownPolicyHandler shutdown_policy_observer(cros_settings_, this); | 63 ShutdownPolicyHandler shutdown_policy_observer(CrosSettings::Get(), this); |
| 81 base::RunLoop().RunUntilIdle(); | 64 base::RunLoop().RunUntilIdle(); |
| 82 EXPECT_EQ(0, delegate_invocations_count_); | 65 EXPECT_EQ(0, delegate_invocations_count_); |
| 83 | 66 |
| 84 SetRebootOnShutdown(true); | 67 SetRebootOnShutdown(true); |
| 85 base::RunLoop().RunUntilIdle(); | 68 base::RunLoop().RunUntilIdle(); |
| 86 EXPECT_EQ(1, delegate_invocations_count_); | 69 EXPECT_EQ(1, delegate_invocations_count_); |
| 87 EXPECT_TRUE(reboot_on_shutdown_); | 70 EXPECT_TRUE(reboot_on_shutdown_); |
| 88 | 71 |
| 89 SetRebootOnShutdown(false); | 72 SetRebootOnShutdown(false); |
| 90 base::RunLoop().RunUntilIdle(); | 73 base::RunLoop().RunUntilIdle(); |
| 91 EXPECT_EQ(2, delegate_invocations_count_); | 74 EXPECT_EQ(2, delegate_invocations_count_); |
| 92 EXPECT_FALSE(reboot_on_shutdown_); | 75 EXPECT_FALSE(reboot_on_shutdown_); |
| 93 | 76 |
| 94 shutdown_policy_observer.Shutdown(); | 77 shutdown_policy_observer.Shutdown(); |
| 95 | 78 |
| 96 SetRebootOnShutdown(true); | 79 SetRebootOnShutdown(true); |
| 97 base::RunLoop().RunUntilIdle(); | 80 base::RunLoop().RunUntilIdle(); |
| 98 EXPECT_EQ(2, delegate_invocations_count_); | 81 EXPECT_EQ(2, delegate_invocations_count_); |
| 99 EXPECT_FALSE(reboot_on_shutdown_); | 82 EXPECT_FALSE(reboot_on_shutdown_); |
| 100 } | 83 } |
| 101 | 84 |
| 102 TEST_F(ShutdownPolicyHandlerTest, CheckIfRebootOnShutdown) { | 85 TEST_F(ShutdownPolicyHandlerTest, CheckIfRebootOnShutdown) { |
| 103 ShutdownPolicyHandler shutdown_policy_observer(cros_settings_, this); | 86 ShutdownPolicyHandler shutdown_policy_observer(CrosSettings::Get(), this); |
| 104 base::RunLoop().RunUntilIdle(); | 87 base::RunLoop().RunUntilIdle(); |
| 105 | 88 |
| 106 // Allow shutdown. | 89 // Allow shutdown. |
| 107 SetRebootOnShutdown(true); | 90 SetRebootOnShutdown(true); |
| 108 callback_called_ = false; | 91 callback_called_ = false; |
| 109 shutdown_policy_observer.CheckIfRebootOnShutdown( | 92 shutdown_policy_observer.CheckIfRebootOnShutdown( |
| 110 base::Bind(&ShutdownPolicyHandlerTest::ResultCallback, | 93 base::Bind(&ShutdownPolicyHandlerTest::ResultCallback, |
| 111 base::Unretained(this))); | 94 base::Unretained(this))); |
| 112 base::RunLoop().RunUntilIdle(); | 95 base::RunLoop().RunUntilIdle(); |
| 113 EXPECT_TRUE(callback_called_); | 96 EXPECT_TRUE(callback_called_); |
| 114 EXPECT_TRUE(reboot_on_shutdown_); | 97 EXPECT_TRUE(reboot_on_shutdown_); |
| 115 // Forbid shutdown. | 98 // Forbid shutdown. |
| 116 SetRebootOnShutdown(false); | 99 SetRebootOnShutdown(false); |
| 117 callback_called_ = false; | 100 callback_called_ = false; |
| 118 shutdown_policy_observer.CheckIfRebootOnShutdown( | 101 shutdown_policy_observer.CheckIfRebootOnShutdown( |
| 119 base::Bind(&ShutdownPolicyHandlerTest::ResultCallback, | 102 base::Bind(&ShutdownPolicyHandlerTest::ResultCallback, |
| 120 base::Unretained(this))); | 103 base::Unretained(this))); |
| 121 base::RunLoop().RunUntilIdle(); | 104 base::RunLoop().RunUntilIdle(); |
| 122 EXPECT_TRUE(callback_called_); | 105 EXPECT_TRUE(callback_called_); |
| 123 EXPECT_FALSE(reboot_on_shutdown_); | 106 EXPECT_FALSE(reboot_on_shutdown_); |
| 124 } | 107 } |
| 125 | 108 |
| 126 } // namespace chromeos | 109 } // namespace chromeos |
| OLD | NEW |