summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/settings/shutdown_policy_handler_unittest.cc
blob: eadd410c3cbb810478e1c915e872d0a25932db95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/settings/shutdown_policy_handler.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
#include "base/values.h"
#include "chrome/browser/chromeos/settings/device_settings_service.h"
#include "chromeos/chromeos_switches.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/settings/cros_settings_names.h"
#include "chromeos/settings/cros_settings_provider.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace chromeos {

class ShutdownPolicyHandlerTest : public testing::Test,
                                  public ShutdownPolicyHandler::Delegate {
 public:
  void ResultCallback(bool reboot_on_shutdown) {
    reboot_on_shutdown_ = reboot_on_shutdown;
    callback_called_ = true;
  }

 protected:
  ShutdownPolicyHandlerTest()
      : cros_settings_(nullptr),
        callback_called_(false),
        reboot_on_shutdown_(false),
        delegate_invocations_count_(0) {
    base::CommandLine::ForCurrentProcess()->AppendSwitch(
        switches::kStubCrosSettings);
    test_cros_settings_.reset(new ScopedTestCrosSettings);
}

  // testing::Test:
void SetUp() override {
    testing::Test::SetUp();
    cros_settings_ = CrosSettings::Get();
    DBusThreadManager::Initialize();
  }

  void TearDown() override { DBusThreadManager::Shutdown(); }

  void SetRebootOnShutdown(bool reboot_on_shutdown) {
    const base::FundamentalValue reboot_on_shutdown_value(reboot_on_shutdown);
    CrosSettings::Get()
        ->GetProvider(kRebootOnShutdown)
        ->Set(kRebootOnShutdown, reboot_on_shutdown_value);
    base::RunLoop().RunUntilIdle();
  }

  // ShutdownPolicyHandler::Delegate:
  void OnShutdownPolicyChanged(bool reboot_on_shutdown) override {
    reboot_on_shutdown_ = reboot_on_shutdown;
    delegate_invocations_count_++;
  }

 protected:
  content::TestBrowserThreadBundle thread_bundle_;

  CrosSettings* cros_settings_;
  scoped_ptr<CrosSettingsProvider> device_settings_provider_;

  ScopedTestDeviceSettingsService test_device_settings_service_;
  scoped_ptr<ScopedTestCrosSettings> test_cros_settings_;

  bool callback_called_;
  bool reboot_on_shutdown_;
  int delegate_invocations_count_;
};

TEST_F(ShutdownPolicyHandlerTest, RetrieveTrustedDevicePolicies) {
  ShutdownPolicyHandler shutdown_policy_observer(cros_settings_, this);
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(0, delegate_invocations_count_);

  SetRebootOnShutdown(true);
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(1, delegate_invocations_count_);
  EXPECT_TRUE(reboot_on_shutdown_);

  SetRebootOnShutdown(false);
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(2, delegate_invocations_count_);
  EXPECT_FALSE(reboot_on_shutdown_);

  shutdown_policy_observer.Shutdown();

  SetRebootOnShutdown(true);
  base::RunLoop().RunUntilIdle();
  EXPECT_EQ(2, delegate_invocations_count_);
  EXPECT_FALSE(reboot_on_shutdown_);
}

TEST_F(ShutdownPolicyHandlerTest, CheckIfRebootOnShutdown) {
  ShutdownPolicyHandler shutdown_policy_observer(cros_settings_, this);
  base::RunLoop().RunUntilIdle();

  // Allow shutdown.
  SetRebootOnShutdown(true);
  callback_called_ = false;
  shutdown_policy_observer.CheckIfRebootOnShutdown(
      base::Bind(&ShutdownPolicyHandlerTest::ResultCallback,
                 base::Unretained(this)));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(callback_called_);
  EXPECT_TRUE(reboot_on_shutdown_);
  // Forbid shutdown.
  SetRebootOnShutdown(false);
  callback_called_ = false;
  shutdown_policy_observer.CheckIfRebootOnShutdown(
      base::Bind(&ShutdownPolicyHandlerTest::ResultCallback,
                 base::Unretained(this)));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(callback_called_);
  EXPECT_FALSE(reboot_on_shutdown_);
}

}  // namespace chromeos