Index: chrome/browser/chromeos/login/enrollment/enrollment_screen_unittest.cc |
diff --git a/chrome/browser/chromeos/login/enrollment/enrollment_screen_unittest.cc b/chrome/browser/chromeos/login/enrollment/enrollment_screen_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..da39d3c5fe768b993fdcc8139fdb596db4fe649b |
--- /dev/null |
+++ b/chrome/browser/chromeos/login/enrollment/enrollment_screen_unittest.cc |
@@ -0,0 +1,172 @@ |
+// Copyright (c) 2017 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/login/enrollment/enrollment_screen.h" |
+#include "base/bind.h" |
+#include "base/command_line.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/test/scoped_mock_time_message_loop_task_runner.h" |
+#include "base/threading/thread_task_runner_handle.h" |
+#include "chrome/browser/chromeos/login/enrollment/enterprise_enrollment_helper.h" |
+#include "chrome/browser/chromeos/login/enrollment/enterprise_enrollment_helper_mock.h" |
+#include "chrome/browser/chromeos/login/enrollment/mock_enrollment_screen.h" |
+#include "chrome/browser/chromeos/login/screens/mock_base_screen_delegate.h" |
+#include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h" |
+#include "chrome/browser/chromeos/policy/enrollment_config.h" |
+#include "chrome/test/base/testing_browser_process.h" |
+#include "chromeos/chromeos_switches.h" |
+#include "chromeos/dbus/dbus_thread_manager.h" |
+#include "components/pairing/fake_controller_pairing_controller.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using testing::AnyNumber; |
+using testing::Invoke; |
+ |
+namespace chromeos { |
+ |
+class EnrollmentScreenUnitTest : public testing::Test { |
+ public: |
+ EnrollmentScreenUnitTest() : fake_controller_("") { |
+ enrollment_config_.mode = policy::EnrollmentConfig::MODE_ATTESTATION_FORCED; |
+ enrollment_config_.auth_mechanism = |
+ policy::EnrollmentConfig::AUTH_MECHANISM_ATTESTATION; |
+ } |
+ |
+ // Closure passed to EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock |
+ // which creates the EnterpriseEnrollmentHelperMock object that will |
+ // eventually be tied to the EnrollmentScreen. It also sets up the |
+ // appropriate expectations for testing with the Google Mock framework. |
+ template <bool SHOULD_ENROLL> |
xiyuan
2017/02/03 23:34:14
SHOULD_ENROLL -> should_enroll
kumarniranjan
2017/02/06 21:38:25
Done.
|
+ static EnterpriseEnrollmentHelper* MockEnrollmentHelperCreator( |
+ EnterpriseEnrollmentHelper::EnrollmentStatusConsumer* status_consumer, |
+ const policy::EnrollmentConfig& enrollment_config, |
+ const std::string& enrolling_user_domain) { |
+ EnterpriseEnrollmentHelperMock* mock = |
+ new EnterpriseEnrollmentHelperMock(status_consumer); |
+ if (SHOULD_ENROLL) { |
+ EXPECT_CALL(*mock, EnrollUsingAttestation()) |
+ .Times(AnyNumber()) |
+ .WillRepeatedly( |
+ Invoke(mock, &EnterpriseEnrollmentHelperMock::SucceedEnrollment)); |
+ } else { |
+ EXPECT_CALL(*mock, EnrollUsingAttestation()) |
+ .Times(AnyNumber()) |
+ .WillRepeatedly( |
+ Invoke(mock, &EnterpriseEnrollmentHelperMock::FailEnrollment)); |
+ } |
+ return mock; |
+ } |
+ |
+ // testing::Test: |
+ void SetUp() override { |
+ // Replace the existing BrowserProcess with a TestingBrowserProcess. |
+ 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
|
+ TestingBrowserProcess::CreateInstance(); |
+ |
+ // Initialize the thread manager. |
+ DBusThreadManager::Initialize(); |
+ |
+ // Configure the browser to use Hands-Off Enrollment. |
+ base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
+ switches::kEnterpriseEnableZeroTouchEnrollment, "hands-off"); |
+ } |
+ |
+ // 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.
|
+ void TearDown() override { DBusThreadManager::Shutdown(); } |
+ |
+ // Creates the EnrollmentScreen and sets required parameters. |
+ void SetUpEnrollmentScreen() { |
+ enrollment_screen_.reset( |
+ new EnrollmentScreen(&mock_delegate_, &mock_actor_)); |
+ enrollment_screen_->SetParameters(enrollment_config_, &fake_controller_); |
+ } |
+ |
+ // Replace main thread's task runner with a mock for duration of test. |
+ 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.
|
+ base::ScopedMockTimeMessageLoopTaskRunner runner_; |
+ |
+ // A pointer to the EnrollmentScreen. |
+ std::unique_ptr<EnrollmentScreen> enrollment_screen_; |
+ |
+ // Objects required by the EnrollmentScreen that can be re-used. |
+ policy::EnrollmentConfig enrollment_config_; |
+ pairing_chromeos::FakeControllerPairingController fake_controller_; |
+ MockBaseScreenDelegate mock_delegate_; |
+ MockEnrollmentScreenActor mock_actor_; |
+}; |
xiyuan
2017/02/03 23:34:14
DISALLOW_COPY_AND_ASSIGN(EnrollmentScreenUnitTest)
kumarniranjan
2017/02/06 21:38:25
Done.
|
+ |
+TEST_F(EnrollmentScreenUnitTest, Retries) { |
+ // Define behavior of EnterpriseEnrollmentHelperMock to always fail |
+ // enrollment. |
+ EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( |
+ &EnrollmentScreenUnitTest::MockEnrollmentHelperCreator<false>); |
+ |
+ SetUpEnrollmentScreen(); |
+ |
+ // Remove jitter to enable deterministic testing. |
+ enrollment_screen_->retry_policy_.jitter_factor = 0; |
+ |
+ // Start zero-touch enrollment. |
+ enrollment_screen_->Show(); |
+ |
+ // Fast forward time by 1 minute. |
+ runner_.task_runner()->FastForwardBy(base::TimeDelta::FromMinutes(1)); |
+ |
+ // Check that we have retried 4 times. |
+ EXPECT_EQ(enrollment_screen_->num_retries_, 4); |
+ |
+ // Required for proper destruction of EnrollmentScreen. |
+ TestingBrowserProcess::GetGlobal()->SetShuttingDown(true); |
+} |
+ |
+TEST_F(EnrollmentScreenUnitTest, DoesNotRetryOnTopOfUser) { |
+ // Define behavior of EnterpriseEnrollmentHelperMock to always fail |
+ // enrollment. |
+ EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( |
+ &EnrollmentScreenUnitTest::MockEnrollmentHelperCreator<false>); |
+ |
+ SetUpEnrollmentScreen(); |
+ |
+ // Remove jitter to enable deterministic testing. |
+ enrollment_screen_->retry_policy_.jitter_factor = 0; |
+ |
+ // Start zero-touch enrollment. |
+ enrollment_screen_->Show(); |
+ |
+ // Schedule user retry button click after 30 sec. |
+ base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
+ FROM_HERE, base::Bind(&EnrollmentScreen::OnRetry, |
+ enrollment_screen_->weak_ptr_factory_.GetWeakPtr()), |
+ base::TimeDelta::FromSeconds(30)); |
+ |
+ // Fast forward time by 1 minute. |
+ runner_.task_runner()->FastForwardBy(base::TimeDelta::FromMinutes(1)); |
+ |
+ // Check that the number of retries is still 4. |
+ EXPECT_EQ(enrollment_screen_->num_retries_, 4); |
+ |
+ // Required for proper destruction of EnrollmentScreen. |
+ TestingBrowserProcess::GetGlobal()->SetShuttingDown(true); |
+} |
+ |
+TEST_F(EnrollmentScreenUnitTest, DoesNotRetryAfterSuccess) { |
+ // Define behavior of EnterpriseEnrollmentHelperMock to successfully enroll. |
+ EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( |
+ &EnrollmentScreenUnitTest::MockEnrollmentHelperCreator<true>); |
+ |
+ SetUpEnrollmentScreen(); |
+ |
+ // Start zero-touch enrollment. |
+ enrollment_screen_->Show(); |
+ |
+ // Fast forward time by 1 minute. |
+ runner_.task_runner()->FastForwardBy(base::TimeDelta::FromMinutes(1)); |
+ |
+ // Check that we do not retry. |
+ EXPECT_EQ(enrollment_screen_->num_retries_, 0); |
+ |
+ // Required for proper destruction of EnrollmentScreen. |
+ TestingBrowserProcess::GetGlobal()->SetShuttingDown(true); |
+} |
+} // namespace chromeos |