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

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: Adressed bartfab's comments 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/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/test/base/testing_browser_process.h"
bartfab (slow) 2014/12/16 10:24:42 Nit: No longer used.
cschuet (SLOW) 2014/12/16 10:53:00 Done.
15 #include "chromeos/chromeos_switches.h"
16 #include "chromeos/dbus/dbus_thread_manager.h"
17 #include "chromeos/settings/cros_settings_names.h"
18 #include "chromeos/settings/cros_settings_provider.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace chromeos {
23
24 class ShutdownPolicyObserverTest : public testing::Test,
25 public ShutdownPolicyObserver::Delegate {
26 public:
27 void ResultCallback(bool actual_reboot_on_shutdown) {
28 reboot_on_shutdown_ = actual_reboot_on_shutdown;
29 callback_called_ = true;
30 }
31
32 bool WasCallbackCalled() const {
33 return callback_called_;
34 }
35
36 protected:
37 ShutdownPolicyObserverTest()
38 : cros_settings_(nullptr),
39 callback_called_(false),
40 reboot_on_shutdown_(false),
41 delegate_invocations_count_(0) {
42 base::CommandLine::ForCurrentProcess()->AppendSwitch(
43 switches::kStubCrosSettings);
44 test_cros_settings_.reset(new ScopedTestCrosSettings);
45 }
46
47 // testing::Test:
48 virtual void SetUp() override {
49 testing::Test::SetUp();
50 cros_settings_ = CrosSettings::Get();
51 DBusThreadManager::Initialize();
52 }
53
54 virtual void TearDown() override {
55 DBusThreadManager::Shutdown();
56 }
57
58 void SetRebootOnShutdown(bool reboot_on_shutdown) {
59 const base::FundamentalValue reboot_on_shutdown_value(reboot_on_shutdown);
60 CrosSettings::Get()
61 ->GetProvider(kRebootOnShutdown)
62 ->Set(kRebootOnShutdown, reboot_on_shutdown_value);
63 base::RunLoop().RunUntilIdle();
64 }
65
66 // ShutdownPolicyObserver::Delegate:
67 void OnShutdownPolicyChanged(bool reboot_on_shutdown) override {
68 reboot_on_shutdown_ = reboot_on_shutdown;
69 delegate_invocations_count_++;
70 }
71
72 protected:
73 content::TestBrowserThreadBundle thread_bundle_;
74
75 CrosSettings* cros_settings_;
76 scoped_ptr<CrosSettingsProvider> device_settings_provider_;
77
78 ScopedTestDeviceSettingsService test_device_settings_service_;
79 scoped_ptr<ScopedTestCrosSettings> test_cros_settings_;
80
81 bool callback_called_;
82 bool reboot_on_shutdown_;
83 int delegate_invocations_count_;
84 };
85
86 TEST_F(ShutdownPolicyObserverTest, RetrieveTrustedDevicePolicies) {
87 ShutdownPolicyObserver shutdown_policy_observer(cros_settings_, this);
88 base::RunLoop().RunUntilIdle();
89 EXPECT_EQ(0, delegate_invocations_count_);
90
91 SetRebootOnShutdown(true);
92 base::RunLoop().RunUntilIdle();
93 EXPECT_EQ(1, delegate_invocations_count_);
94 EXPECT_TRUE(reboot_on_shutdown_);
95
96 SetRebootOnShutdown(false);
97 base::RunLoop().RunUntilIdle();
98 EXPECT_EQ(2, delegate_invocations_count_);
99 EXPECT_FALSE(reboot_on_shutdown_);
100
101 shutdown_policy_observer.Shutdown();
102
103 SetRebootOnShutdown(true);
104 base::RunLoop().RunUntilIdle();
105 EXPECT_EQ(2, delegate_invocations_count_);
106 EXPECT_FALSE(reboot_on_shutdown_);
107 }
108
109 TEST_F(ShutdownPolicyObserverTest, CheckIfRebootOnShutdown) {
110 ShutdownPolicyObserver shutdown_policy_observer(cros_settings_, this);
111 base::RunLoop().RunUntilIdle();
112
113 // Allow shutdown.
114 SetRebootOnShutdown(true);
115 callback_called_ = false;
bartfab (slow) 2014/12/16 10:24:42 Optional nit: You write |callback_called_| directl
cschuet (SLOW) 2014/12/16 10:53:00 Done.
116 EXPECT_FALSE(this->WasCallbackCalled());
117 shutdown_policy_observer.CheckIfRebootOnShutdown(
118 base::Bind(&ShutdownPolicyObserverTest::ResultCallback,
119 base::Unretained(this)));
120 base::RunLoop().RunUntilIdle();
121 EXPECT_TRUE(this->WasCallbackCalled());
122 EXPECT_EQ(true, reboot_on_shutdown_);
123 // Forbid shutdown.
124 SetRebootOnShutdown(false);
125 callback_called_ = false;
126 EXPECT_FALSE(this->WasCallbackCalled());
127 shutdown_policy_observer.CheckIfRebootOnShutdown(
128 base::Bind(&ShutdownPolicyObserverTest::ResultCallback,
129 base::Unretained(this)));
130 base::RunLoop().RunUntilIdle();
131 EXPECT_TRUE(this->WasCallbackCalled());
132 EXPECT_EQ(false, reboot_on_shutdown_);
133 }
134
135 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698