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

Side by Side Diff: chrome/browser/chromeos/settings/shutdown_policy_handler_unittest.cc

Issue 811033002: Add device policy to disallow shutdown - ash UI modifications (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: reduced scope of CL (ash only now, removed webui changes) Created 5 years, 11 months 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
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/ui/webui/chromeos/login/shutdown_policy_observer.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/command_line.h" 9 #include "base/command_line.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "base/values.h" 12 #include "base/values.h"
13 #include "chrome/browser/chromeos/settings/device_settings_service.h" 13 #include "chrome/browser/chromeos/settings/device_settings_service.h"
14 #include "chromeos/chromeos_switches.h" 14 #include "chromeos/chromeos_switches.h"
15 #include "chromeos/dbus/dbus_thread_manager.h" 15 #include "chromeos/dbus/dbus_thread_manager.h"
16 #include "chromeos/settings/cros_settings_names.h" 16 #include "chromeos/settings/cros_settings_names.h"
17 #include "chromeos/settings/cros_settings_provider.h" 17 #include "chromeos/settings/cros_settings_provider.h"
18 #include "content/public/test/test_browser_thread_bundle.h" 18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 20
21 namespace chromeos { 21 namespace chromeos {
22 22
23 class ShutdownPolicyObserverTest : public testing::Test, 23 class ShutdownPolicyHandlerTest : public testing::Test,
24 public ShutdownPolicyObserver::Delegate { 24 public ShutdownPolicyHandler::Delegate {
Daniel Erat 2015/01/07 16:18:40 nit: fix indenting
cschuet (SLOW) 2015/01/13 10:38:28 Done.
25 public: 25 public:
26 void ResultCallback(bool reboot_on_shutdown) { 26 void ResultCallback(bool reboot_on_shutdown) {
27 reboot_on_shutdown_ = reboot_on_shutdown; 27 reboot_on_shutdown_ = reboot_on_shutdown;
28 callback_called_ = true; 28 callback_called_ = true;
29 } 29 }
30 30
31 protected: 31 protected:
32 ShutdownPolicyObserverTest() 32 ShutdownPolicyHandlerTest()
33 : cros_settings_(nullptr), 33 : cros_settings_(nullptr),
34 callback_called_(false), 34 callback_called_(false),
35 reboot_on_shutdown_(false), 35 reboot_on_shutdown_(false),
36 delegate_invocations_count_(0) { 36 delegate_invocations_count_(0) {
37 base::CommandLine::ForCurrentProcess()->AppendSwitch( 37 base::CommandLine::ForCurrentProcess()->AppendSwitch(
38 switches::kStubCrosSettings); 38 switches::kStubCrosSettings);
39 test_cros_settings_.reset(new ScopedTestCrosSettings); 39 test_cros_settings_.reset(new ScopedTestCrosSettings);
40 } 40 }
41 41
42 // testing::Test: 42 // testing::Test:
43 virtual void SetUp() override { 43 virtual void SetUp() override {
44 testing::Test::SetUp(); 44 testing::Test::SetUp();
45 cros_settings_ = CrosSettings::Get(); 45 cros_settings_ = CrosSettings::Get();
46 DBusThreadManager::Initialize(); 46 DBusThreadManager::Initialize();
47 } 47 }
48 48
49 virtual void TearDown() override { 49 virtual void TearDown() override {
50 DBusThreadManager::Shutdown(); 50 DBusThreadManager::Shutdown();
51 } 51 }
52 52
53 void SetRebootOnShutdown(bool reboot_on_shutdown) { 53 void SetRebootOnShutdown(bool reboot_on_shutdown) {
54 const base::FundamentalValue reboot_on_shutdown_value(reboot_on_shutdown); 54 const base::FundamentalValue reboot_on_shutdown_value(reboot_on_shutdown);
55 CrosSettings::Get() 55 CrosSettings::Get()
56 ->GetProvider(kRebootOnShutdown) 56 ->GetProvider(kRebootOnShutdown)
57 ->Set(kRebootOnShutdown, reboot_on_shutdown_value); 57 ->Set(kRebootOnShutdown, reboot_on_shutdown_value);
58 base::RunLoop().RunUntilIdle(); 58 base::RunLoop().RunUntilIdle();
59 } 59 }
60 60
61 // ShutdownPolicyObserver::Delegate: 61 // ShutdownPolicyHandler::Delegate:
62 void OnShutdownPolicyChanged(bool reboot_on_shutdown) override { 62 void OnShutdownPolicyChanged(bool reboot_on_shutdown) override {
63 reboot_on_shutdown_ = reboot_on_shutdown; 63 reboot_on_shutdown_ = reboot_on_shutdown;
64 delegate_invocations_count_++; 64 delegate_invocations_count_++;
65 } 65 }
66 66
67 protected: 67 protected:
68 content::TestBrowserThreadBundle thread_bundle_; 68 content::TestBrowserThreadBundle thread_bundle_;
69 69
70 CrosSettings* cros_settings_; 70 CrosSettings* cros_settings_;
71 scoped_ptr<CrosSettingsProvider> device_settings_provider_; 71 scoped_ptr<CrosSettingsProvider> device_settings_provider_;
72 72
73 ScopedTestDeviceSettingsService test_device_settings_service_; 73 ScopedTestDeviceSettingsService test_device_settings_service_;
74 scoped_ptr<ScopedTestCrosSettings> test_cros_settings_; 74 scoped_ptr<ScopedTestCrosSettings> test_cros_settings_;
75 75
76 bool callback_called_; 76 bool callback_called_;
77 bool reboot_on_shutdown_; 77 bool reboot_on_shutdown_;
78 int delegate_invocations_count_; 78 int delegate_invocations_count_;
79 }; 79 };
80 80
81 TEST_F(ShutdownPolicyObserverTest, RetrieveTrustedDevicePolicies) { 81 TEST_F(ShutdownPolicyHandlerTest, RetrieveTrustedDevicePolicies) {
82 ShutdownPolicyObserver shutdown_policy_observer(cros_settings_, this); 82 ShutdownPolicyHandler shutdown_policy_observer(cros_settings_, this);
83 base::RunLoop().RunUntilIdle(); 83 base::RunLoop().RunUntilIdle();
84 EXPECT_EQ(0, delegate_invocations_count_); 84 EXPECT_EQ(0, delegate_invocations_count_);
85 85
86 SetRebootOnShutdown(true); 86 SetRebootOnShutdown(true);
87 base::RunLoop().RunUntilIdle(); 87 base::RunLoop().RunUntilIdle();
88 EXPECT_EQ(1, delegate_invocations_count_); 88 EXPECT_EQ(1, delegate_invocations_count_);
89 EXPECT_TRUE(reboot_on_shutdown_); 89 EXPECT_TRUE(reboot_on_shutdown_);
90 90
91 SetRebootOnShutdown(false); 91 SetRebootOnShutdown(false);
92 base::RunLoop().RunUntilIdle(); 92 base::RunLoop().RunUntilIdle();
93 EXPECT_EQ(2, delegate_invocations_count_); 93 EXPECT_EQ(2, delegate_invocations_count_);
94 EXPECT_FALSE(reboot_on_shutdown_); 94 EXPECT_FALSE(reboot_on_shutdown_);
95 95
96 shutdown_policy_observer.Shutdown(); 96 shutdown_policy_observer.Shutdown();
97 97
98 SetRebootOnShutdown(true); 98 SetRebootOnShutdown(true);
99 base::RunLoop().RunUntilIdle(); 99 base::RunLoop().RunUntilIdle();
100 EXPECT_EQ(2, delegate_invocations_count_); 100 EXPECT_EQ(2, delegate_invocations_count_);
101 EXPECT_FALSE(reboot_on_shutdown_); 101 EXPECT_FALSE(reboot_on_shutdown_);
102 } 102 }
103 103
104 TEST_F(ShutdownPolicyObserverTest, CheckIfRebootOnShutdown) { 104 TEST_F(ShutdownPolicyHandlerTest, CheckIfRebootOnShutdown) {
105 ShutdownPolicyObserver shutdown_policy_observer(cros_settings_, this); 105 ShutdownPolicyHandler shutdown_policy_observer(cros_settings_, this);
106 base::RunLoop().RunUntilIdle(); 106 base::RunLoop().RunUntilIdle();
107 107
108 // Allow shutdown. 108 // Allow shutdown.
109 SetRebootOnShutdown(true); 109 SetRebootOnShutdown(true);
110 callback_called_ = false; 110 callback_called_ = false;
111 shutdown_policy_observer.CheckIfRebootOnShutdown( 111 shutdown_policy_observer.CheckIfRebootOnShutdown(
112 base::Bind(&ShutdownPolicyObserverTest::ResultCallback, 112 base::Bind(&ShutdownPolicyHandlerTest::ResultCallback,
113 base::Unretained(this))); 113 base::Unretained(this)));
114 base::RunLoop().RunUntilIdle(); 114 base::RunLoop().RunUntilIdle();
115 EXPECT_TRUE(callback_called_); 115 EXPECT_TRUE(callback_called_);
116 EXPECT_EQ(true, reboot_on_shutdown_); 116 EXPECT_EQ(true, reboot_on_shutdown_);
117 // Forbid shutdown. 117 // Forbid shutdown.
118 SetRebootOnShutdown(false); 118 SetRebootOnShutdown(false);
119 callback_called_ = false; 119 callback_called_ = false;
120 shutdown_policy_observer.CheckIfRebootOnShutdown( 120 shutdown_policy_observer.CheckIfRebootOnShutdown(
121 base::Bind(&ShutdownPolicyObserverTest::ResultCallback, 121 base::Bind(&ShutdownPolicyHandlerTest::ResultCallback,
122 base::Unretained(this))); 122 base::Unretained(this)));
123 base::RunLoop().RunUntilIdle(); 123 base::RunLoop().RunUntilIdle();
124 EXPECT_TRUE(callback_called_); 124 EXPECT_TRUE(callback_called_);
125 EXPECT_EQ(false, reboot_on_shutdown_); 125 EXPECT_EQ(false, reboot_on_shutdown_);
126 } 126 }
127 127
128 } // namespace chromeos 128 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698