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.
5 #include "chrome/browser/chromeos/settings/shutdown_policy_handler.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 "chromeos/chromeos_switches.h"
15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #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"
19 #include "testing/gtest/include/gtest/gtest.h"
23 class ShutdownPolicyHandlerTest
: public testing::Test
,
24 public ShutdownPolicyHandler::Delegate
{
26 void ResultCallback(bool reboot_on_shutdown
) {
27 reboot_on_shutdown_
= reboot_on_shutdown
;
28 callback_called_
= true;
32 ShutdownPolicyHandlerTest()
33 : cros_settings_(nullptr),
34 callback_called_(false),
35 reboot_on_shutdown_(false),
36 delegate_invocations_count_(0) {
37 base::CommandLine::ForCurrentProcess()->AppendSwitch(
38 switches::kStubCrosSettings
);
39 test_cros_settings_
.reset(new ScopedTestCrosSettings
);
43 void SetUp() override
{
44 testing::Test::SetUp();
45 cros_settings_
= CrosSettings::Get();
46 DBusThreadManager::Initialize();
49 void TearDown() override
{ DBusThreadManager::Shutdown(); }
51 void SetRebootOnShutdown(bool reboot_on_shutdown
) {
52 const base::FundamentalValue
reboot_on_shutdown_value(reboot_on_shutdown
);
54 ->GetProvider(kRebootOnShutdown
)
55 ->Set(kRebootOnShutdown
, reboot_on_shutdown_value
);
56 base::RunLoop().RunUntilIdle();
59 // ShutdownPolicyHandler::Delegate:
60 void OnShutdownPolicyChanged(bool reboot_on_shutdown
) override
{
61 reboot_on_shutdown_
= reboot_on_shutdown
;
62 delegate_invocations_count_
++;
66 content::TestBrowserThreadBundle thread_bundle_
;
68 CrosSettings
* cros_settings_
;
69 scoped_ptr
<CrosSettingsProvider
> device_settings_provider_
;
71 ScopedTestDeviceSettingsService test_device_settings_service_
;
72 scoped_ptr
<ScopedTestCrosSettings
> test_cros_settings_
;
74 bool callback_called_
;
75 bool reboot_on_shutdown_
;
76 int delegate_invocations_count_
;
79 TEST_F(ShutdownPolicyHandlerTest
, RetrieveTrustedDevicePolicies
) {
80 ShutdownPolicyHandler
shutdown_policy_observer(cros_settings_
, this);
81 base::RunLoop().RunUntilIdle();
82 EXPECT_EQ(0, delegate_invocations_count_
);
84 SetRebootOnShutdown(true);
85 base::RunLoop().RunUntilIdle();
86 EXPECT_EQ(1, delegate_invocations_count_
);
87 EXPECT_TRUE(reboot_on_shutdown_
);
89 SetRebootOnShutdown(false);
90 base::RunLoop().RunUntilIdle();
91 EXPECT_EQ(2, delegate_invocations_count_
);
92 EXPECT_FALSE(reboot_on_shutdown_
);
94 shutdown_policy_observer
.Shutdown();
96 SetRebootOnShutdown(true);
97 base::RunLoop().RunUntilIdle();
98 EXPECT_EQ(2, delegate_invocations_count_
);
99 EXPECT_FALSE(reboot_on_shutdown_
);
102 TEST_F(ShutdownPolicyHandlerTest
, CheckIfRebootOnShutdown
) {
103 ShutdownPolicyHandler
shutdown_policy_observer(cros_settings_
, this);
104 base::RunLoop().RunUntilIdle();
107 SetRebootOnShutdown(true);
108 callback_called_
= false;
109 shutdown_policy_observer
.CheckIfRebootOnShutdown(
110 base::Bind(&ShutdownPolicyHandlerTest::ResultCallback
,
111 base::Unretained(this)));
112 base::RunLoop().RunUntilIdle();
113 EXPECT_TRUE(callback_called_
);
114 EXPECT_TRUE(reboot_on_shutdown_
);
116 SetRebootOnShutdown(false);
117 callback_called_
= false;
118 shutdown_policy_observer
.CheckIfRebootOnShutdown(
119 base::Bind(&ShutdownPolicyHandlerTest::ResultCallback
,
120 base::Unretained(this)));
121 base::RunLoop().RunUntilIdle();
122 EXPECT_TRUE(callback_called_
);
123 EXPECT_FALSE(reboot_on_shutdown_
);
126 } // namespace chromeos