Chromium Code Reviews| 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 "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" | |
|
bartfab (slow)
2014/12/15 18:01:56
Nit: No longer used.
cschuet (SLOW)
2014/12/16 10:02:08
Done.
| |
| 15 #include "chrome/test/base/testing_browser_process.h" | |
| 16 #include "chromeos/chromeos_switches.h" | |
| 17 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 18 #include "chromeos/settings/cros_settings_names.h" | |
| 19 #include "chromeos/settings/cros_settings_provider.h" | |
| 20 #include "content/public/test/test_browser_thread_bundle.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 | |
| 23 namespace chromeos { | |
| 24 | |
| 25 class ShutdownPolicyObserverTest : public testing::Test, | |
| 26 public ShutdownPolicyObserver::Delegate { | |
| 27 public: | |
| 28 void ResultCallback(bool actual_reboot_on_shutdown) { | |
|
bartfab (slow)
2014/12/15 18:01:57
Nit: You can drop the |actual_| prefix now.
cschuet (SLOW)
2014/12/16 10:02:08
Done.
bartfab (slow)
2014/12/16 10:24:42
No, not done.
cschuet (SLOW)
2014/12/16 10:53:00
Done.
| |
| 29 reboot_on_shutdown_ = actual_reboot_on_shutdown; | |
| 30 callback_called_ = true; | |
| 31 } | |
| 32 | |
| 33 bool WasCallbackCalled() const { | |
| 34 return callback_called_; | |
| 35 } | |
| 36 | |
| 37 protected: | |
| 38 ShutdownPolicyObserverTest() | |
| 39 : cros_settings_(nullptr), | |
| 40 callback_called_(false), | |
| 41 expected_reboot_on_shutdown_(false), | |
|
bartfab (slow)
2014/12/15 18:01:56
Nit: This member is no longer needed as you set an
cschuet (SLOW)
2014/12/16 10:02:07
Done.
| |
| 42 reboot_on_shutdown_(false), | |
| 43 delegate_invocations_count_(0) { | |
| 44 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 45 switches::kStubCrosSettings); | |
| 46 test_cros_settings_.reset(new ScopedTestCrosSettings); | |
| 47 } | |
| 48 | |
| 49 // testing::Test: | |
| 50 virtual void SetUp() override { | |
| 51 testing::Test::SetUp(); | |
| 52 cros_settings_ = CrosSettings::Get(); | |
| 53 DBusThreadManager::Initialize(); | |
|
bartfab (slow)
2014/12/15 18:01:56
Nit: Could you check whether this is still necessa
cschuet (SLOW)
2014/12/16 10:02:07
Done.
| |
| 54 } | |
| 55 | |
| 56 virtual void TearDown() override { | |
| 57 // Shut down the DeviceSettingsService. | |
| 58 DeviceSettingsService::Get()->UnsetSessionManager(); | |
|
bartfab (slow)
2014/12/15 18:01:57
Nit: Could you check whether this is still necessa
cschuet (SLOW)
2014/12/16 10:02:07
Done.
| |
| 59 TestingBrowserProcess::GetGlobal()->SetProfileManager(nullptr); | |
|
bartfab (slow)
2014/12/15 18:01:57
Nit: Could you check whether this is still necessa
cschuet (SLOW)
2014/12/16 10:02:08
Done.
| |
| 60 base::RunLoop().RunUntilIdle(); | |
|
bartfab (slow)
2014/12/15 18:01:56
Nit: Could you check whether this is still necessa
cschuet (SLOW)
2014/12/16 10:02:07
Done.
| |
| 61 DBusThreadManager::Shutdown(); | |
|
bartfab (slow)
2014/12/15 18:01:57
Nit: Could you check whether this is still necessa
cschuet (SLOW)
2014/12/16 10:02:07
Done.
| |
| 62 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
|
bartfab (slow)
2014/12/15 18:01:56
You should remove, not re-add, the switch in TearD
cschuet (SLOW)
2014/12/16 10:02:07
Done.
| |
| 63 switches::kStubCrosSettings); | |
| 64 } | |
| 65 | |
| 66 void SetRebootOnShutdown(bool reboot_on_shutdown) { | |
| 67 const base::FundamentalValue reboot_on_shutdown_value(reboot_on_shutdown); | |
| 68 CrosSettings::Get() | |
| 69 ->GetProvider(kRebootOnShutdown) | |
| 70 ->Set(kRebootOnShutdown, reboot_on_shutdown_value); | |
| 71 base::RunLoop().RunUntilIdle(); | |
| 72 } | |
| 73 | |
| 74 void SetExpectation(bool expected_reboot_on_shutdown) { | |
| 75 expected_reboot_on_shutdown_ = expected_reboot_on_shutdown; | |
| 76 callback_called_ = false; | |
| 77 } | |
| 78 | |
| 79 // ShutdownPolicyObserver::Delegate: | |
| 80 void OnShutdownPolicyChanged(bool reboot_on_shutdown) override { | |
| 81 reboot_on_shutdown_ = reboot_on_shutdown; | |
| 82 delegate_invocations_count_++; | |
| 83 } | |
| 84 | |
| 85 protected: | |
| 86 content::TestBrowserThreadBundle thread_bundle_; | |
| 87 | |
| 88 CrosSettings* cros_settings_; | |
| 89 scoped_ptr<CrosSettingsProvider> device_settings_provider_; | |
| 90 | |
| 91 ScopedTestDeviceSettingsService test_device_settings_service_; | |
| 92 scoped_ptr<ScopedTestCrosSettings> test_cros_settings_; | |
| 93 | |
| 94 bool callback_called_; | |
| 95 bool expected_reboot_on_shutdown_; | |
| 96 bool reboot_on_shutdown_; | |
| 97 int delegate_invocations_count_; | |
| 98 }; | |
| 99 | |
| 100 TEST_F(ShutdownPolicyObserverTest, RetrieveTrustedDevicePolicies) { | |
| 101 ShutdownPolicyObserver shutdown_policy_observer(cros_settings_, this); | |
| 102 base::RunLoop().RunUntilIdle(); | |
| 103 EXPECT_EQ(0, delegate_invocations_count_); | |
| 104 | |
| 105 SetRebootOnShutdown(true); | |
| 106 base::RunLoop().RunUntilIdle(); | |
| 107 EXPECT_EQ(1, delegate_invocations_count_); | |
| 108 EXPECT_TRUE(reboot_on_shutdown_); | |
| 109 | |
| 110 SetRebootOnShutdown(false); | |
| 111 base::RunLoop().RunUntilIdle(); | |
| 112 EXPECT_EQ(2, delegate_invocations_count_); | |
| 113 EXPECT_FALSE(reboot_on_shutdown_); | |
| 114 | |
| 115 shutdown_policy_observer.Shutdown(); | |
| 116 | |
| 117 SetRebootOnShutdown(true); | |
| 118 base::RunLoop().RunUntilIdle(); | |
| 119 EXPECT_EQ(2, delegate_invocations_count_); | |
| 120 EXPECT_FALSE(reboot_on_shutdown_); | |
| 121 } | |
| 122 | |
| 123 TEST_F(ShutdownPolicyObserverTest, CheckIfRebootOnShutdown) { | |
| 124 ShutdownPolicyObserver shutdown_policy_observer(cros_settings_, this); | |
| 125 base::RunLoop().RunUntilIdle(); | |
| 126 | |
| 127 // Allow shutdown. | |
| 128 SetRebootOnShutdown(true); | |
| 129 SetExpectation(true); | |
| 130 EXPECT_FALSE(this->WasCallbackCalled()); | |
| 131 shutdown_policy_observer.CheckIfRebootOnShutdown( | |
| 132 base::Bind(&ShutdownPolicyObserverTest::ResultCallback, | |
| 133 base::Unretained(this))); | |
| 134 base::RunLoop().RunUntilIdle(); | |
| 135 EXPECT_TRUE(this->WasCallbackCalled()); | |
| 136 EXPECT_EQ(expected_reboot_on_shutdown_, reboot_on_shutdown_); | |
| 137 // Forbid shutdown. | |
| 138 SetRebootOnShutdown(false); | |
| 139 SetExpectation(false); | |
| 140 EXPECT_FALSE(this->WasCallbackCalled()); | |
| 141 shutdown_policy_observer.CheckIfRebootOnShutdown( | |
| 142 base::Bind(&ShutdownPolicyObserverTest::ResultCallback, | |
| 143 base::Unretained(this))); | |
| 144 base::RunLoop().RunUntilIdle(); | |
| 145 EXPECT_TRUE(this->WasCallbackCalled()); | |
| 146 EXPECT_EQ(expected_reboot_on_shutdown_, reboot_on_shutdown_); | |
| 147 } | |
| 148 | |
| 149 } // namespace chromeos | |
| OLD | NEW |