OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2017 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/chromeos/login/enrollment/enrollment_screen.h" | |
6 #include "base/bind.h" | |
7 #include "base/command_line.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "base/test/scoped_mock_time_message_loop_task_runner.h" | |
10 #include "base/threading/thread_task_runner_handle.h" | |
11 #include "chrome/browser/chromeos/login/enrollment/enterprise_enrollment_helper. h" | |
12 #include "chrome/browser/chromeos/login/enrollment/enterprise_enrollment_helper_ mock.h" | |
13 #include "chrome/browser/chromeos/login/enrollment/mock_enrollment_screen.h" | |
14 #include "chrome/browser/chromeos/login/screens/mock_base_screen_delegate.h" | |
15 #include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h" | |
16 #include "chrome/browser/chromeos/policy/enrollment_config.h" | |
17 #include "chrome/test/base/testing_browser_process.h" | |
18 #include "chromeos/chromeos_switches.h" | |
19 #include "chromeos/dbus/dbus_thread_manager.h" | |
20 #include "components/pairing/fake_controller_pairing_controller.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | |
22 | |
23 using testing::AnyNumber; | |
24 using testing::Invoke; | |
25 | |
26 namespace chromeos { | |
27 | |
28 class EnrollmentScreenUnitTest : public testing::Test { | |
29 public: | |
30 EnrollmentScreenUnitTest() : fake_controller_("") { | |
31 enrollment_config_.mode = policy::EnrollmentConfig::MODE_ATTESTATION_FORCED; | |
32 enrollment_config_.auth_mechanism = | |
33 policy::EnrollmentConfig::AUTH_MECHANISM_ATTESTATION; | |
34 } | |
35 | |
36 // Closure passed to EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock | |
37 // which creates the EnterpriseEnrollmentHelperMock object that will | |
38 // eventually be tied to the EnrollmentScreen. It also sets up the | |
39 // appropriate expectations for testing with the Google Mock framework. | |
40 template <bool SHOULD_ENROLL> | |
xiyuan
2017/02/03 23:34:14
SHOULD_ENROLL -> should_enroll
kumarniranjan
2017/02/06 21:38:25
Done.
| |
41 static EnterpriseEnrollmentHelper* MockEnrollmentHelperCreator( | |
42 EnterpriseEnrollmentHelper::EnrollmentStatusConsumer* status_consumer, | |
43 const policy::EnrollmentConfig& enrollment_config, | |
44 const std::string& enrolling_user_domain) { | |
45 EnterpriseEnrollmentHelperMock* mock = | |
46 new EnterpriseEnrollmentHelperMock(status_consumer); | |
47 if (SHOULD_ENROLL) { | |
48 EXPECT_CALL(*mock, EnrollUsingAttestation()) | |
49 .Times(AnyNumber()) | |
50 .WillRepeatedly( | |
51 Invoke(mock, &EnterpriseEnrollmentHelperMock::SucceedEnrollment)); | |
52 } else { | |
53 EXPECT_CALL(*mock, EnrollUsingAttestation()) | |
54 .Times(AnyNumber()) | |
55 .WillRepeatedly( | |
56 Invoke(mock, &EnterpriseEnrollmentHelperMock::FailEnrollment)); | |
57 } | |
58 return mock; | |
59 } | |
60 | |
61 // testing::Test: | |
62 void SetUp() override { | |
63 // Replace the existing BrowserProcess with a TestingBrowserProcess. | |
64 TestingBrowserProcess::DeleteInstance(); | |
xiyuan
2017/02/03 23:34:14
Think DeleteInstance() is not necessary because th
kumarniranjan
2017/02/06 21:38:24
I tried removing this line, but my test fails beca
xiyuan
2017/02/06 23:05:33
Can you remind me why we need to re-create Testing
kumarniranjan
2017/02/06 23:35:12
The only reason I put TestingBrowserProcess::Delet
| |
65 TestingBrowserProcess::CreateInstance(); | |
66 | |
67 // Initialize the thread manager. | |
68 DBusThreadManager::Initialize(); | |
69 | |
70 // Configure the browser to use Hands-Off Enrollment. | |
71 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
72 switches::kEnterpriseEnableZeroTouchEnrollment, "hands-off"); | |
73 } | |
74 | |
75 // testing::Test: | |
xiyuan
2017/02/03 23:34:14
nit: Remove this. Only need one at the beginning o
kumarniranjan
2017/02/06 21:38:25
Done.
| |
76 void TearDown() override { DBusThreadManager::Shutdown(); } | |
77 | |
78 // Creates the EnrollmentScreen and sets required parameters. | |
79 void SetUpEnrollmentScreen() { | |
80 enrollment_screen_.reset( | |
81 new EnrollmentScreen(&mock_delegate_, &mock_actor_)); | |
82 enrollment_screen_->SetParameters(enrollment_config_, &fake_controller_); | |
83 } | |
84 | |
85 // Replace main thread's task runner with a mock for duration of test. | |
86 base::MessageLoop loop_; | |
xiyuan
2017/02/03 23:34:14
Make member vars private and provide getters to th
kumarniranjan
2017/02/06 21:38:25
Done.
| |
87 base::ScopedMockTimeMessageLoopTaskRunner runner_; | |
88 | |
89 // A pointer to the EnrollmentScreen. | |
90 std::unique_ptr<EnrollmentScreen> enrollment_screen_; | |
91 | |
92 // Objects required by the EnrollmentScreen that can be re-used. | |
93 policy::EnrollmentConfig enrollment_config_; | |
94 pairing_chromeos::FakeControllerPairingController fake_controller_; | |
95 MockBaseScreenDelegate mock_delegate_; | |
96 MockEnrollmentScreenActor mock_actor_; | |
97 }; | |
xiyuan
2017/02/03 23:34:14
DISALLOW_COPY_AND_ASSIGN(EnrollmentScreenUnitTest)
kumarniranjan
2017/02/06 21:38:25
Done.
| |
98 | |
99 TEST_F(EnrollmentScreenUnitTest, Retries) { | |
100 // Define behavior of EnterpriseEnrollmentHelperMock to always fail | |
101 // enrollment. | |
102 EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( | |
103 &EnrollmentScreenUnitTest::MockEnrollmentHelperCreator<false>); | |
104 | |
105 SetUpEnrollmentScreen(); | |
106 | |
107 // Remove jitter to enable deterministic testing. | |
108 enrollment_screen_->retry_policy_.jitter_factor = 0; | |
109 | |
110 // Start zero-touch enrollment. | |
111 enrollment_screen_->Show(); | |
112 | |
113 // Fast forward time by 1 minute. | |
114 runner_.task_runner()->FastForwardBy(base::TimeDelta::FromMinutes(1)); | |
115 | |
116 // Check that we have retried 4 times. | |
117 EXPECT_EQ(enrollment_screen_->num_retries_, 4); | |
118 | |
119 // Required for proper destruction of EnrollmentScreen. | |
120 TestingBrowserProcess::GetGlobal()->SetShuttingDown(true); | |
121 } | |
122 | |
123 TEST_F(EnrollmentScreenUnitTest, DoesNotRetryOnTopOfUser) { | |
124 // Define behavior of EnterpriseEnrollmentHelperMock to always fail | |
125 // enrollment. | |
126 EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( | |
127 &EnrollmentScreenUnitTest::MockEnrollmentHelperCreator<false>); | |
128 | |
129 SetUpEnrollmentScreen(); | |
130 | |
131 // Remove jitter to enable deterministic testing. | |
132 enrollment_screen_->retry_policy_.jitter_factor = 0; | |
133 | |
134 // Start zero-touch enrollment. | |
135 enrollment_screen_->Show(); | |
136 | |
137 // Schedule user retry button click after 30 sec. | |
138 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
139 FROM_HERE, base::Bind(&EnrollmentScreen::OnRetry, | |
140 enrollment_screen_->weak_ptr_factory_.GetWeakPtr()), | |
141 base::TimeDelta::FromSeconds(30)); | |
142 | |
143 // Fast forward time by 1 minute. | |
144 runner_.task_runner()->FastForwardBy(base::TimeDelta::FromMinutes(1)); | |
145 | |
146 // Check that the number of retries is still 4. | |
147 EXPECT_EQ(enrollment_screen_->num_retries_, 4); | |
148 | |
149 // Required for proper destruction of EnrollmentScreen. | |
150 TestingBrowserProcess::GetGlobal()->SetShuttingDown(true); | |
151 } | |
152 | |
153 TEST_F(EnrollmentScreenUnitTest, DoesNotRetryAfterSuccess) { | |
154 // Define behavior of EnterpriseEnrollmentHelperMock to successfully enroll. | |
155 EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( | |
156 &EnrollmentScreenUnitTest::MockEnrollmentHelperCreator<true>); | |
157 | |
158 SetUpEnrollmentScreen(); | |
159 | |
160 // Start zero-touch enrollment. | |
161 enrollment_screen_->Show(); | |
162 | |
163 // Fast forward time by 1 minute. | |
164 runner_.task_runner()->FastForwardBy(base::TimeDelta::FromMinutes(1)); | |
165 | |
166 // Check that we do not retry. | |
167 EXPECT_EQ(enrollment_screen_->num_retries_, 0); | |
168 | |
169 // Required for proper destruction of EnrollmentScreen. | |
170 TestingBrowserProcess::GetGlobal()->SetShuttingDown(true); | |
171 } | |
172 } // namespace chromeos | |
OLD | NEW |