| 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/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h" | 10 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h" |
| 11 #include "chromeos/dbus/dbus_thread_manager.h" | 11 #include "chromeos/dbus/dbus_thread_manager.h" |
| 12 #include "chromeos/settings/cros_settings_names.h" | 12 #include "chromeos/settings/cros_settings_names.h" |
| 13 #include "content/public/test/test_browser_thread_bundle.h" | 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 namespace chromeos { | 16 namespace chromeos { |
| 17 | 17 |
| 18 class ShutdownPolicyHandlerTest : public testing::Test, | 18 class ShutdownPolicyHandlerTest : public testing::Test, |
| 19 public ShutdownPolicyHandler::Delegate { | 19 public ShutdownPolicyHandler::Delegate { |
| 20 public: | 20 public: |
| 21 void ResultCallback(bool reboot_on_shutdown) { | |
| 22 reboot_on_shutdown_ = reboot_on_shutdown; | |
| 23 callback_called_ = true; | |
| 24 } | |
| 25 | |
| 26 protected: | |
| 27 ShutdownPolicyHandlerTest() | 21 ShutdownPolicyHandlerTest() |
| 28 : callback_called_(false), | 22 : reboot_on_shutdown_(false), delegate_invocations_count_(0) {} |
| 29 reboot_on_shutdown_(false), | |
| 30 delegate_invocations_count_(0) {} | |
| 31 | 23 |
| 32 // testing::Test: | 24 // testing::Test: |
| 33 void SetUp() override { | 25 void SetUp() override { |
| 34 testing::Test::SetUp(); | 26 testing::Test::SetUp(); |
| 35 DBusThreadManager::Initialize(); | 27 DBusThreadManager::Initialize(); |
| 36 settings_helper_.ReplaceProvider(kRebootOnShutdown); | 28 settings_helper_.ReplaceProvider(kRebootOnShutdown); |
| 37 } | 29 } |
| 38 | 30 |
| 39 void TearDown() override { DBusThreadManager::Shutdown(); } | 31 void TearDown() override { DBusThreadManager::Shutdown(); } |
| 40 | 32 |
| 41 void SetRebootOnShutdown(bool reboot_on_shutdown) { | 33 void SetRebootOnShutdown(bool reboot_on_shutdown) { |
| 42 settings_helper_.SetBoolean(kRebootOnShutdown, reboot_on_shutdown); | 34 settings_helper_.SetBoolean(kRebootOnShutdown, reboot_on_shutdown); |
| 43 base::RunLoop().RunUntilIdle(); | 35 base::RunLoop().RunUntilIdle(); |
| 44 } | 36 } |
| 45 | 37 |
| 46 // ShutdownPolicyHandler::Delegate: | 38 // ShutdownPolicyHandler::Delegate: |
| 47 void OnShutdownPolicyChanged(bool reboot_on_shutdown) override { | 39 void OnShutdownPolicyChanged(bool reboot_on_shutdown) override { |
| 48 reboot_on_shutdown_ = reboot_on_shutdown; | 40 reboot_on_shutdown_ = reboot_on_shutdown; |
| 49 delegate_invocations_count_++; | 41 delegate_invocations_count_++; |
| 50 } | 42 } |
| 51 | 43 |
| 52 protected: | 44 protected: |
| 53 content::TestBrowserThreadBundle thread_bundle_; | 45 content::TestBrowserThreadBundle thread_bundle_; |
| 54 ScopedCrosSettingsTestHelper settings_helper_; | 46 ScopedCrosSettingsTestHelper settings_helper_; |
| 55 bool callback_called_; | |
| 56 bool reboot_on_shutdown_; | 47 bool reboot_on_shutdown_; |
| 57 int delegate_invocations_count_; | 48 int delegate_invocations_count_; |
| 58 }; | 49 }; |
| 59 | 50 |
| 60 TEST_F(ShutdownPolicyHandlerTest, RetrieveTrustedDevicePolicies) { | 51 TEST_F(ShutdownPolicyHandlerTest, RetrieveTrustedDevicePolicies) { |
| 61 ShutdownPolicyHandler shutdown_policy_observer(CrosSettings::Get(), this); | 52 ShutdownPolicyHandler shutdown_policy_handler(CrosSettings::Get(), this); |
| 62 base::RunLoop().RunUntilIdle(); | 53 base::RunLoop().RunUntilIdle(); |
| 63 EXPECT_EQ(0, delegate_invocations_count_); | 54 EXPECT_EQ(0, delegate_invocations_count_); |
| 64 | 55 |
| 65 SetRebootOnShutdown(true); | 56 SetRebootOnShutdown(true); |
| 66 base::RunLoop().RunUntilIdle(); | 57 base::RunLoop().RunUntilIdle(); |
| 67 EXPECT_EQ(1, delegate_invocations_count_); | 58 EXPECT_EQ(1, delegate_invocations_count_); |
| 68 EXPECT_TRUE(reboot_on_shutdown_); | 59 EXPECT_TRUE(reboot_on_shutdown_); |
| 69 | 60 |
| 70 SetRebootOnShutdown(false); | 61 SetRebootOnShutdown(false); |
| 71 base::RunLoop().RunUntilIdle(); | 62 base::RunLoop().RunUntilIdle(); |
| 72 EXPECT_EQ(2, delegate_invocations_count_); | 63 EXPECT_EQ(2, delegate_invocations_count_); |
| 73 EXPECT_FALSE(reboot_on_shutdown_); | 64 EXPECT_FALSE(reboot_on_shutdown_); |
| 74 | |
| 75 shutdown_policy_observer.Shutdown(); | |
| 76 | |
| 77 SetRebootOnShutdown(true); | |
| 78 base::RunLoop().RunUntilIdle(); | |
| 79 EXPECT_EQ(2, delegate_invocations_count_); | |
| 80 EXPECT_FALSE(reboot_on_shutdown_); | |
| 81 } | 65 } |
| 82 | 66 |
| 83 TEST_F(ShutdownPolicyHandlerTest, CheckIfRebootOnShutdown) { | 67 TEST_F(ShutdownPolicyHandlerTest, NotifyDelegateWithShutdownPolicy) { |
| 84 ShutdownPolicyHandler shutdown_policy_observer(CrosSettings::Get(), this); | 68 ShutdownPolicyHandler shutdown_policy_handler(CrosSettings::Get(), this); |
| 85 base::RunLoop().RunUntilIdle(); | 69 base::RunLoop().RunUntilIdle(); |
| 86 | 70 |
| 87 // Allow shutdown. | 71 // Allow shutdown. |
| 88 SetRebootOnShutdown(true); | 72 SetRebootOnShutdown(true); |
| 89 callback_called_ = false; | 73 delegate_invocations_count_ = 0; |
| 90 shutdown_policy_observer.CheckIfRebootOnShutdown( | 74 // Request a manual update. |
| 91 base::Bind(&ShutdownPolicyHandlerTest::ResultCallback, | 75 shutdown_policy_handler.NotifyDelegateWithShutdownPolicy(); |
| 92 base::Unretained(this))); | |
| 93 base::RunLoop().RunUntilIdle(); | 76 base::RunLoop().RunUntilIdle(); |
| 94 EXPECT_TRUE(callback_called_); | 77 EXPECT_EQ(1, delegate_invocations_count_); |
| 95 EXPECT_TRUE(reboot_on_shutdown_); | 78 EXPECT_TRUE(reboot_on_shutdown_); |
| 79 |
| 96 // Forbid shutdown. | 80 // Forbid shutdown. |
| 97 SetRebootOnShutdown(false); | 81 SetRebootOnShutdown(false); |
| 98 callback_called_ = false; | 82 delegate_invocations_count_ = 0; |
| 99 shutdown_policy_observer.CheckIfRebootOnShutdown( | 83 // Request a manual update. |
| 100 base::Bind(&ShutdownPolicyHandlerTest::ResultCallback, | 84 shutdown_policy_handler.NotifyDelegateWithShutdownPolicy(); |
| 101 base::Unretained(this))); | |
| 102 base::RunLoop().RunUntilIdle(); | 85 base::RunLoop().RunUntilIdle(); |
| 103 EXPECT_TRUE(callback_called_); | 86 EXPECT_EQ(1, delegate_invocations_count_); |
| 104 EXPECT_FALSE(reboot_on_shutdown_); | 87 EXPECT_FALSE(reboot_on_shutdown_); |
| 105 } | 88 } |
| 106 | 89 |
| 107 } // namespace chromeos | 90 } // namespace chromeos |
| OLD | NEW |