Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / consumer_management_notifier_unittest.cc
blob26c1d46ab2478c3194bb2e27a99452464b361eb4
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/policy/consumer_management_notifier.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/browser_process_platform_part.h"
10 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
11 #include "chrome/browser/chromeos/policy/consumer_management_service.h"
12 #include "chrome/browser/chromeos/policy/consumer_management_stage.h"
13 #include "chrome/browser/chromeos/policy/fake_consumer_management_service.h"
14 #include "chrome/browser/notifications/notification_ui_manager.h"
15 #include "chrome/test/base/browser_with_test_window_test.h"
16 #include "chrome/test/base/testing_browser_process.h"
17 #include "chrome/test/base/testing_profile_manager.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 namespace policy {
22 class ConsumerManagementNotifierTest : public BrowserWithTestWindowTest {
23 public:
24 ConsumerManagementNotifierTest()
25 : fake_service_(new FakeConsumerManagementService()) {
26 fake_service_->SetStatusAndStage(
27 ConsumerManagementService::STATUS_UNENROLLED,
28 ConsumerManagementStage::None());
30 // Inject objects.
31 BrowserPolicyConnectorChromeOS* connector =
32 g_browser_process->platform_part()->browser_policy_connector_chromeos();
33 connector->SetConsumerManagementServiceForTesting(
34 make_scoped_ptr(fake_service_));
37 void SetUp() override {
38 BrowserWithTestWindowTest::SetUp();
40 // Set up TestingProfileManager. This is required for NotificationUIManager.
41 testing_profile_manager_.reset(new TestingProfileManager(
42 TestingBrowserProcess::GetGlobal()));
43 ASSERT_TRUE(testing_profile_manager_->SetUp());
46 void TearDown() override {
47 if (notification_)
48 notification_->Shutdown();
49 notification_.reset();
50 g_browser_process->notification_ui_manager()->CancelAll();
51 testing_profile_manager_.reset();
53 BrowserWithTestWindowTest::TearDown();
56 void CreateConsumerManagementNotifier() {
57 notification_.reset(
58 new ConsumerManagementNotifier(profile(), fake_service_));
61 bool HasEnrollmentNotification() {
62 return g_browser_process->notification_ui_manager()->FindById(
63 "consumer_management.enroll",
64 NotificationUIManager::GetProfileID(profile()));
67 bool HasUnenrollmentNotification() {
68 return g_browser_process->notification_ui_manager()->FindById(
69 "consumer_management.unenroll",
70 NotificationUIManager::GetProfileID(profile()));
73 FakeConsumerManagementService* fake_service_;
74 scoped_ptr<TestingProfileManager> testing_profile_manager_;
75 scoped_ptr<ConsumerManagementNotifier> notification_;
78 TEST_F(ConsumerManagementNotifierTest,
79 ShowsEnrollmentNotificationWhenCreated) {
80 fake_service_->SetStatusAndStage(
81 ConsumerManagementService::STATUS_UNENROLLED,
82 ConsumerManagementStage::EnrollmentCanceled());
83 EXPECT_FALSE(HasEnrollmentNotification());
84 EXPECT_FALSE(HasUnenrollmentNotification());
86 CreateConsumerManagementNotifier();
88 EXPECT_EQ(ConsumerManagementStage::None(), fake_service_->GetStage());
89 EXPECT_TRUE(HasEnrollmentNotification());
90 EXPECT_FALSE(HasUnenrollmentNotification());
93 TEST_F(ConsumerManagementNotifierTest,
94 ShowsUnenrollmentNotificationWhenCreated) {
95 fake_service_->SetStatusAndStage(
96 ConsumerManagementService::STATUS_UNENROLLED,
97 ConsumerManagementStage::UnenrollmentSuccess());
98 EXPECT_FALSE(HasEnrollmentNotification());
99 EXPECT_FALSE(HasUnenrollmentNotification());
101 CreateConsumerManagementNotifier();
103 EXPECT_EQ(ConsumerManagementStage::None(), fake_service_->GetStage());
104 EXPECT_FALSE(HasEnrollmentNotification());
105 EXPECT_TRUE(HasUnenrollmentNotification());
108 TEST_F(ConsumerManagementNotifierTest,
109 ShowsEnrollmentNotificationWhenStatusChanged) {
110 fake_service_->SetStatusAndStage(
111 ConsumerManagementService::STATUS_ENROLLING,
112 ConsumerManagementStage::EnrollmentOwnerStored());
114 CreateConsumerManagementNotifier();
115 EXPECT_EQ(ConsumerManagementStage::EnrollmentOwnerStored(),
116 fake_service_->GetStage());
117 EXPECT_FALSE(HasEnrollmentNotification());
118 EXPECT_FALSE(HasUnenrollmentNotification());
120 fake_service_->SetStatusAndStage(
121 ConsumerManagementService::STATUS_ENROLLED,
122 ConsumerManagementStage::EnrollmentSuccess());
123 EXPECT_EQ(ConsumerManagementStage::None(), fake_service_->GetStage());
124 EXPECT_TRUE(HasEnrollmentNotification());
125 EXPECT_FALSE(HasUnenrollmentNotification());
128 TEST_F(ConsumerManagementNotifierTest,
129 ShowsUnenrollmentNotificationWhenStatusChanged) {
130 fake_service_->SetStatusAndStage(
131 ConsumerManagementService::STATUS_ENROLLED,
132 ConsumerManagementStage::None());
134 CreateConsumerManagementNotifier();
135 EXPECT_FALSE(HasEnrollmentNotification());
136 EXPECT_FALSE(HasUnenrollmentNotification());
138 fake_service_->SetStatusAndStage(
139 ConsumerManagementService::STATUS_UNENROLLING,
140 ConsumerManagementStage::UnenrollmentRequested());
141 EXPECT_FALSE(HasEnrollmentNotification());
142 EXPECT_FALSE(HasUnenrollmentNotification());
144 fake_service_->SetStatusAndStage(
145 ConsumerManagementService::STATUS_UNENROLLED,
146 ConsumerManagementStage::UnenrollmentSuccess());
147 EXPECT_EQ(ConsumerManagementStage::None(), fake_service_->GetStage());
148 EXPECT_FALSE(HasEnrollmentNotification());
149 EXPECT_TRUE(HasUnenrollmentNotification());
152 } // namespace policy