Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(275)

Side by Side Diff: chrome/browser/ui/webui/chromeos/login/shutdown_policy_observer_unittest.cc

Issue 776093004: Add device policy to disallow shutdown (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: modifications suggested by stevenjb Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/run_loop.h"
8 #include "base/values.h"
9 #include "chrome/browser/chromeos/settings/device_settings_service.h"
10 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h"
11 #include "chrome/test/base/testing_browser_process.h"
12 #include "chromeos/dbus/dbus_thread_manager.h"
13 #include "chromeos/settings/cros_settings_names.h"
14 #include "chromeos/settings/cros_settings_provider.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace chromeos {
19
20 class ShutdownPolicyObserverTest : public testing::Test,
21 public ShutdownPolicyObserver::Delegate {
22 public:
23 void ExpectationCheckerCallback(bool actual_shutdown_is_reboot) {
bartfab (slow) 2014/12/08 16:49:46 This is not ideal for the following reason: If an
cschuet (SLOW) 2014/12/08 18:57:05 Done.
24 EXPECT_EQ(expected_shutdown_is_reboot_, actual_shutdown_is_reboot);
25 callback_called_ = true;
26 }
27
28 bool WasCallbackCalled() const {
29 return callback_called_;
30 }
31
32 protected:
33 ShutdownPolicyObserverTest()
34 : cros_settings_(nullptr),
35 stub_settings_provider_(new StubCrosSettingsProvider),
bartfab (slow) 2014/12/08 16:49:46 Nit: Ownership should be held by a scoped_ptr unti
cschuet (SLOW) 2014/12/08 18:57:05 Done.
36 callback_called_(false),
37 expected_shutdown_is_reboot_(false),
38 shutdown_is_reboot_(false),
39 count_delegate_invocations_(0) {}
40
41 virtual void SetUp() override {
bartfab (slow) 2014/12/08 16:49:46 Nit: Add //testing::Test:
cschuet (SLOW) 2014/12/08 18:57:05 Done.
42 cros_settings_ = CrosSettings::Get();
43
44 // Replace the real DeviceSettingsProvider with a stub.
45 device_settings_provider_.reset(
bartfab (slow) 2014/12/08 16:49:45 1: How about setting the |switches::kStubCrosSetti
cschuet (SLOW) 2014/12/08 18:57:05 Done.
46 cros_settings_->GetProvider(chromeos::kShutdownIsReboot));
bartfab (slow) 2014/12/08 16:49:45 Nit: No need for chromeos::.
cschuet (SLOW) 2014/12/08 18:57:04 Done.
47 EXPECT_TRUE(device_settings_provider_);
48 EXPECT_TRUE(cros_settings_->RemoveSettingsProvider(
49 device_settings_provider_.get()));
50 cros_settings_->AddSettingsProvider(stub_settings_provider_);
51
52 chromeos::DBusThreadManager::Initialize();
bartfab (slow) 2014/12/08 16:49:46 Nit: No need for chromeos::.
cschuet (SLOW) 2014/12/08 18:57:05 Done.
53 }
54
55 virtual void TearDown() override {
56 // Restore the real DeviceSettingsProvider.
57 EXPECT_TRUE(
58 cros_settings_->RemoveSettingsProvider(stub_settings_provider_));
59 cros_settings_->AddSettingsProvider(device_settings_provider_.release());
60
61 // Shut down the DeviceSettingsService.
62 DeviceSettingsService::Get()->UnsetSessionManager();
63 TestingBrowserProcess::GetGlobal()->SetProfileManager(nullptr);
64 base::RunLoop().RunUntilIdle();
65 chromeos::DBusThreadManager::Shutdown();
bartfab (slow) 2014/12/08 16:49:46 Nit: No need for chromeos::.
cschuet (SLOW) 2014/12/08 18:57:05 Done.
66 }
67
68 void SetDeviceSettings(bool shutdown_is_reboot) {
bartfab (slow) 2014/12/08 16:49:46 Nit: When this method is called, e.g. as SetDevice
cschuet (SLOW) 2014/12/08 18:57:05 Done.
69 const base::FundamentalValue shutdown_is_reboot_value(shutdown_is_reboot);
70 stub_settings_provider_->Set(kShutdownIsReboot, shutdown_is_reboot_value);
71 base::RunLoop().RunUntilIdle();
72 }
73
74 void SetExpectation(bool expected_shutdown_is_reboot) {
bartfab (slow) 2014/12/08 16:49:46 Nit: When this method is called, e.g. as SetExpect
cschuet (SLOW) 2014/12/08 18:57:04 Done.
75 expected_shutdown_is_reboot_ = expected_shutdown_is_reboot;
76 callback_called_ = false;
77 }
78
79 void OnShutdownPolicyChanged(bool shutdown_is_reboot) override {
bartfab (slow) 2014/12/08 16:49:46 Nit: Add //ShutdownPolicyObserver::Delegate:
cschuet (SLOW) 2014/12/08 18:57:05 Done.
80 shutdown_is_reboot_ = shutdown_is_reboot;
81 count_delegate_invocations_++;
82 }
83
84 protected:
85 content::TestBrowserThreadBundle thread_bundle_;
86
87 CrosSettings* cros_settings_;
88 scoped_ptr<CrosSettingsProvider> device_settings_provider_;
bartfab (slow) 2014/12/08 16:49:46 Nit: #include "base/memory/scoped_ptr.h"
cschuet (SLOW) 2014/12/08 18:57:04 Done.
89 StubCrosSettingsProvider* stub_settings_provider_;
90
91 ScopedTestDeviceSettingsService test_device_settings_service_;
92 ScopedTestCrosSettings test_cros_settings_;
93
94 bool callback_called_;
95 bool expected_shutdown_is_reboot_;
96 bool shutdown_is_reboot_;
97 int count_delegate_invocations_;
bartfab (slow) 2014/12/08 16:49:45 Nit: The word oder does not match Chrome style. s/
cschuet (SLOW) 2014/12/08 18:57:05 Done.
98 };
99
100 TEST_F(ShutdownPolicyObserverTest, RetrieveTrustedDevicePolicies) {
101 ShutdownPolicyObserver shutdown_policy_observer(cros_settings_, this);
102 base::RunLoop().RunUntilIdle();
103 EXPECT_EQ(0, count_delegate_invocations_);
104
105 SetDeviceSettings(true);
106 base::RunLoop().RunUntilIdle();
107 EXPECT_EQ(1, count_delegate_invocations_);
108 EXPECT_TRUE(shutdown_is_reboot_);
109
110 SetDeviceSettings(false);
111 base::RunLoop().RunUntilIdle();
112 EXPECT_EQ(2, count_delegate_invocations_);
113 EXPECT_FALSE(shutdown_is_reboot_);
114
115 shutdown_policy_observer.Shutdown();
116
117 SetDeviceSettings(true);
118 base::RunLoop().RunUntilIdle();
119 EXPECT_EQ(2, count_delegate_invocations_);
120 EXPECT_FALSE(shutdown_is_reboot_);
121 }
122
123 TEST_F(ShutdownPolicyObserverTest, CheckIfShutdownIsRebootTest) {
bartfab (slow) 2014/12/08 16:49:46 Nit: No need for |Test| at the end of a test case
cschuet (SLOW) 2014/12/08 18:57:05 Done.
124 ShutdownPolicyObserver shutdown_policy_observer(cros_settings_, this);
125 base::RunLoop().RunUntilIdle();
126
127 // Allow shutdown.
128 SetDeviceSettings(true);
129 SetExpectation(true);
130 EXPECT_FALSE(this->WasCallbackCalled());
131 shutdown_policy_observer.CheckIfShutdownIsReboot(
132 base::Bind(&ShutdownPolicyObserverTest::ExpectationCheckerCallback,
bartfab (slow) 2014/12/08 16:49:46 Nit: #include "base/bind.h"
cschuet (SLOW) 2014/12/08 18:57:05 Done.
133 base::Unretained(this)));
bartfab (slow) 2014/12/08 16:49:45 Nit: #include "base/bind_helpers.h"
cschuet (SLOW) 2014/12/08 18:57:05 Done.
134 base::RunLoop().RunUntilIdle();
135 EXPECT_TRUE(this->WasCallbackCalled());
136
137 // Forbid shutdown.
138 SetDeviceSettings(false);
139 SetExpectation(false);
140 EXPECT_FALSE(this->WasCallbackCalled());
141 shutdown_policy_observer.CheckIfShutdownIsReboot(
142 base::Bind(&ShutdownPolicyObserverTest::ExpectationCheckerCallback,
143 base::Unretained(this)));
144 base::RunLoop().RunUntilIdle();
145 EXPECT_TRUE(this->WasCallbackCalled());
146 }
147
148 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698