Fix the duplicatedly commited composition text when switching IME.
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / consumer_enrollment_handler_unittest.cc
blob7116c61b0ceed9ce1fc19189fcf5cb8e06271171
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_enrollment_handler.h"
7 #include <utility>
9 #include "base/memory/scoped_ptr.h"
10 #include "base/run_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/browser_process_platform_part.h"
14 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
15 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
16 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
17 #include "chrome/browser/chromeos/policy/consumer_management_service.h"
18 #include "chrome/browser/chromeos/policy/consumer_management_stage.h"
19 #include "chrome/browser/chromeos/policy/enrollment_status_chromeos.h"
20 #include "chrome/browser/chromeos/policy/fake_consumer_management_service.h"
21 #include "chrome/browser/chromeos/policy/fake_device_cloud_policy_initializer.h"
22 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
23 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
24 #include "chrome/browser/signin/signin_manager_factory.h"
25 #include "chrome/test/base/testing_browser_process.h"
26 #include "chrome/test/base/testing_profile_manager.h"
27 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
28 #include "components/signin/core/browser/profile_oauth2_token_service.h"
29 #include "components/signin/core/browser/signin_manager_base.h"
30 #include "components/syncable_prefs/pref_service_syncable.h"
31 #include "content/public/test/test_browser_thread_bundle.h"
32 #include "google_apis/gaia/google_service_auth_error.h"
33 #include "testing/gtest/include/gtest/gtest.h"
35 namespace {
36 const char* kTestOwner = "test.owner@chromium.org.test";
37 const char* kTestUser = "test.user@chromium.org.test";
40 namespace policy {
42 class ConsumerEnrollmentHandlerTest : public testing::Test {
43 public:
44 ConsumerEnrollmentHandlerTest()
45 : fake_service_(new FakeConsumerManagementService()),
46 fake_initializer_(new FakeDeviceCloudPolicyInitializer()),
47 fake_user_manager_(new chromeos::FakeChromeUserManager()),
48 scoped_user_manager_enabler_(fake_user_manager_),
49 testing_profile_manager_(
50 new TestingProfileManager(TestingBrowserProcess::GetGlobal())) {
51 // Set up FakeConsumerManagementService.
52 fake_service_->SetStatusAndStage(
53 ConsumerManagementService::STATUS_ENROLLING,
54 ConsumerManagementStage::EnrollmentOwnerStored());
56 // Inject fake objects.
57 BrowserPolicyConnectorChromeOS* connector =
58 g_browser_process->platform_part()->browser_policy_connector_chromeos();
59 connector->SetConsumerManagementServiceForTesting(
60 make_scoped_ptr(fake_service_));
61 connector->SetDeviceCloudPolicyInitializerForTesting(
62 make_scoped_ptr(fake_initializer_));
64 // Set up FakeChromeUserManager.
65 fake_user_manager_->AddUser(kTestOwner);
66 fake_user_manager_->AddUser(kTestUser);
67 fake_user_manager_->set_owner_email(kTestOwner);
70 void SetUp() override {
71 ASSERT_TRUE(testing_profile_manager_->SetUp());
72 TestingProfile::TestingFactories factories;
73 factories.push_back(
74 std::make_pair(ProfileOAuth2TokenServiceFactory::GetInstance(),
75 BuildAutoIssuingFakeProfileOAuth2TokenService));
76 profile_ = testing_profile_manager_->CreateTestingProfile(
77 kTestUser, scoped_ptr<syncable_prefs::PrefServiceSyncable>(),
78 base::UTF8ToUTF16(kTestUser), 0, std::string(), factories);
80 // Set up the authenticated user name and ID.
81 SigninManagerFactory::GetForProfile(profile_)
82 ->SetAuthenticatedAccountInfo(kTestOwner, kTestOwner);
84 // Issue a fake refresh token.
85 GetFakeProfileOAuth2TokenService()->UpdateCredentials(kTestOwner,
86 "fake_token");
89 FakeProfileOAuth2TokenService* GetFakeProfileOAuth2TokenService() {
90 return static_cast<FakeProfileOAuth2TokenService*>(
91 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_));
94 void RunEnrollmentTest() {
95 handler_.reset(
96 new ConsumerEnrollmentHandler(profile_, fake_service_, NULL));
97 base::RunLoop().RunUntilIdle();
100 content::TestBrowserThreadBundle thread_bundle;
101 FakeConsumerManagementService* fake_service_;
102 FakeDeviceCloudPolicyInitializer* fake_initializer_;
103 chromeos::FakeChromeUserManager* fake_user_manager_;
104 chromeos::ScopedUserManagerEnabler scoped_user_manager_enabler_;
105 scoped_ptr<TestingProfileManager> testing_profile_manager_;
106 Profile* profile_;
107 scoped_ptr<ConsumerEnrollmentHandler> handler_;
110 TEST_F(ConsumerEnrollmentHandlerTest, EnrollsSuccessfully) {
111 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called());
113 RunEnrollmentTest();
115 EXPECT_TRUE(fake_initializer_->was_start_enrollment_called());
116 EXPECT_EQ(ConsumerManagementStage::EnrollmentSuccess(),
117 fake_service_->GetStage());
120 TEST_F(ConsumerEnrollmentHandlerTest, FailsToGetAccessToken) {
121 // Disable auto-posting so that RunEnrollmentTest() should stop and wait for
122 // the access token to be available.
123 GetFakeProfileOAuth2TokenService()->
124 set_auto_post_fetch_response_on_message_loop(false);
126 RunEnrollmentTest();
128 // The service should have a pending token request.
129 OAuth2TokenService::Request* token_request =
130 handler_->GetTokenRequestForTesting();
131 EXPECT_TRUE(token_request);
133 // Tell the service that the access token is not available because of some
134 // backend issue.
135 handler_->OnGetTokenFailure(
136 token_request,
137 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR));
139 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called());
140 EXPECT_EQ(ConsumerManagementStage::EnrollmentGetTokenFailed(),
141 fake_service_->GetStage());
144 TEST_F(ConsumerEnrollmentHandlerTest, FailsToRegister) {
145 EXPECT_FALSE(fake_initializer_->was_start_enrollment_called());
146 fake_initializer_->set_enrollment_status(EnrollmentStatus::ForStatus(
147 EnrollmentStatus::STATUS_REGISTRATION_FAILED));
149 RunEnrollmentTest();
151 EXPECT_TRUE(fake_initializer_->was_start_enrollment_called());
152 EXPECT_EQ(ConsumerManagementStage::EnrollmentDMServerFailed(),
153 fake_service_->GetStage());
156 } // namespace policy