| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/signin/signin_create_profile_handler.h" | 5 #include "chrome/browser/ui/webui/signin/signin_create_profile_handler.h" |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/prefs/browser_prefs.h" | 8 #include "chrome/browser/prefs/browser_prefs.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/profiles/profile_attributes_entry.h" | 10 #include "chrome/browser/profiles/profile_attributes_entry.h" |
| 11 #include "chrome/browser/profiles/profile_attributes_storage.h" | 11 #include "chrome/browser/profiles/profile_attributes_storage.h" |
| 12 #include "chrome/browser/profiles/profile_avatar_icon_util.h" | 12 #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
| 13 #include "chrome/browser/signin/fake_signin_manager_builder.h" | 13 #include "chrome/browser/signin/fake_signin_manager_builder.h" |
| 14 #include "chrome/browser/signin/signin_error_controller_factory.h" | 14 #include "chrome/browser/signin/signin_error_controller_factory.h" |
| 15 #include "chrome/browser/signin/signin_manager_factory.h" | 15 #include "chrome/browser/signin/signin_manager_factory.h" |
| 16 #include "chrome/browser/supervised_user/legacy/supervised_user_sync_service.h" | |
| 17 #include "chrome/browser/supervised_user/legacy/supervised_user_sync_service_fac
tory.h" | |
| 18 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 19 #include "chrome/grit/generated_resources.h" | 17 #include "chrome/grit/generated_resources.h" |
| 20 #include "chrome/test/base/browser_with_test_window_test.h" | 18 #include "chrome/test/base/browser_with_test_window_test.h" |
| 21 #include "chrome/test/base/testing_browser_process.h" | 19 #include "chrome/test/base/testing_browser_process.h" |
| 22 #include "chrome/test/base/testing_profile_manager.h" | 20 #include "chrome/test/base/testing_profile_manager.h" |
| 23 #include "components/signin/core/browser/fake_auth_status_provider.h" | 21 #include "components/signin/core/browser/fake_auth_status_provider.h" |
| 24 #include "components/syncable_prefs/testing_pref_service_syncable.h" | 22 #include "components/syncable_prefs/testing_pref_service_syncable.h" |
| 25 #include "content/public/test/test_web_ui.h" | 23 #include "content/public/test/test_web_ui.h" |
| 26 #include "sync/api/fake_sync_change_processor.h" | 24 #include "sync/api/fake_sync_change_processor.h" |
| 27 #include "sync/api/sync_data.h" | 25 #include "sync/api/sync_data.h" |
| 28 #include "sync/api/sync_error_factory_mock.h" | 26 #include "sync/api/sync_error_factory_mock.h" |
| 29 #include "sync/internal_api/public/attachments/attachment_service_proxy_for_test
.h" | 27 #include "sync/internal_api/public/attachments/attachment_service_proxy_for_test
.h" |
| 30 #include "sync/protocol/sync.pb.h" | 28 #include "sync/protocol/sync.pb.h" |
| 31 #include "testing/gmock/include/gmock/gmock.h" | 29 #include "testing/gmock/include/gmock/gmock.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 33 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
| 34 | 32 |
| 33 #if defined(ENABLE_SUPERVISED_USERS) |
| 34 #include "chrome/browser/supervised_user/legacy/supervised_user_sync_service.h" |
| 35 #include "chrome/browser/supervised_user/legacy/supervised_user_sync_service_fac
tory.h" |
| 36 #endif |
| 37 |
| 35 // Gmock matchers and actions. | 38 // Gmock matchers and actions. |
| 36 using testing::_; | 39 using testing::_; |
| 37 using testing::Invoke; | 40 using testing::Invoke; |
| 38 | 41 |
| 39 namespace { | 42 namespace { |
| 40 | 43 |
| 41 const char kTestProfileName[] = "test-profile-name"; | 44 const char kTestProfileName[] = "test-profile-name"; |
| 42 | 45 |
| 43 const char kTestGaiaId1[] = "test-gaia-id-1"; | 46 const char kTestGaiaId1[] = "test-gaia-id-1"; |
| 44 const char kTestEmail1[] = "foo1@bar.com"; | 47 const char kTestEmail1[] = "foo1@bar.com"; |
| 45 | 48 |
| 46 const char kTestGaiaId2[] = "test-gaia-id-2"; | 49 const char kTestGaiaId2[] = "test-gaia-id-2"; |
| 47 const char kTestEmail2[] = "foo2@bar.com"; | 50 const char kTestEmail2[] = "foo2@bar.com"; |
| 48 | 51 |
| 49 const char kTestWebUIResponse[] = "cr.webUIListenerCallback"; | 52 const char kTestWebUIResponse[] = "cr.webUIListenerCallback"; |
| 50 | 53 |
| 54 #if defined(ENABLE_SUPERVISED_USERS) |
| 51 const char kSupervisedUserId1[] = "test-supervised-id-1"; | 55 const char kSupervisedUserId1[] = "test-supervised-id-1"; |
| 52 const char kSupervisedUserId2[] = "test-supervised-id-2"; | 56 const char kSupervisedUserId2[] = "test-supervised-id-2"; |
| 53 | 57 |
| 54 const char kSupervisedUsername1[] = "test-supervised-username-1"; | 58 const char kSupervisedUsername1[] = "test-supervised-username-1"; |
| 55 const char kSupervisedUsername2[] = "test-supervised-username-2"; | 59 const char kSupervisedUsername2[] = "test-supervised-username-2"; |
| 56 | 60 |
| 57 const char kSupervisedUserAvatarName1[] = "chrome-avatar-index:0"; | 61 const char kSupervisedUserAvatarName1[] = "chrome-avatar-index:0"; |
| 58 const char kSupervisedUserAvatarName2[] = "chrome-avatar-index:1"; | 62 const char kSupervisedUserAvatarName2[] = "chrome-avatar-index:1"; |
| 59 | 63 |
| 60 syncer::SyncData CreateSyncData(const std::string& id, | 64 syncer::SyncData CreateSyncData(const std::string& id, |
| 61 const std::string& name, | 65 const std::string& name, |
| 62 const std::string& chrome_avatar) { | 66 const std::string& chrome_avatar) { |
| 63 sync_pb::EntitySpecifics specifics; | 67 sync_pb::EntitySpecifics specifics; |
| 64 specifics.mutable_managed_user()->set_id(id); | 68 specifics.mutable_managed_user()->set_id(id); |
| 65 specifics.mutable_managed_user()->set_name(name); | 69 specifics.mutable_managed_user()->set_name(name); |
| 66 specifics.mutable_managed_user()->set_acknowledged(true); | 70 specifics.mutable_managed_user()->set_acknowledged(true); |
| 67 specifics.mutable_managed_user()->set_chrome_avatar(chrome_avatar); | 71 specifics.mutable_managed_user()->set_chrome_avatar(chrome_avatar); |
| 68 | 72 |
| 69 return syncer::SyncData::CreateRemoteData( | 73 return syncer::SyncData::CreateRemoteData( |
| 70 1, | 74 1, |
| 71 specifics, | 75 specifics, |
| 72 base::Time(), | 76 base::Time(), |
| 73 syncer::AttachmentIdList(), | 77 syncer::AttachmentIdList(), |
| 74 syncer::AttachmentServiceProxyForTest::Create()); | 78 syncer::AttachmentServiceProxyForTest::Create()); |
| 75 } | 79 } |
| 80 #endif |
| 76 | 81 |
| 77 } // namespace | 82 } // namespace |
| 78 | 83 |
| 79 class TestSigninCreateProfileHandler : public SigninCreateProfileHandler { | 84 class TestSigninCreateProfileHandler : public SigninCreateProfileHandler { |
| 80 public: | 85 public: |
| 81 explicit TestSigninCreateProfileHandler( | 86 explicit TestSigninCreateProfileHandler( |
| 82 content::WebUI* web_ui, | 87 content::WebUI* web_ui, |
| 83 TestingProfileManager* profile_manager) | 88 TestingProfileManager* profile_manager) |
| 84 : profile_manager_(profile_manager) { | 89 : profile_manager_(profile_manager) { |
| 85 set_web_ui(web_ui); | 90 set_web_ui(web_ui); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 void(Profile* profile, Profile::CreateStatus status)); | 133 void(Profile* profile, Profile::CreateStatus status)); |
| 129 | 134 |
| 130 // We don't actually need to register supervised users in the test. Mock this | 135 // We don't actually need to register supervised users in the test. Mock this |
| 131 // method to fake the registration part. | 136 // method to fake the registration part. |
| 132 MOCK_METHOD4(RegisterSupervisedUser, | 137 MOCK_METHOD4(RegisterSupervisedUser, |
| 133 void(bool create_shortcut, | 138 void(bool create_shortcut, |
| 134 const std::string& supervised_user_id, | 139 const std::string& supervised_user_id, |
| 135 Profile* custodian_profile, | 140 Profile* custodian_profile, |
| 136 Profile* new_profile)); | 141 Profile* new_profile)); |
| 137 | 142 |
| 143 #if defined(ENABLE_SUPERVISED_USERS) |
| 138 // Calls the callback method to resume profile creation flow. | 144 // Calls the callback method to resume profile creation flow. |
| 139 void RealRegisterSupervisedUser(bool create_shortcut, | 145 void RealRegisterSupervisedUser(bool create_shortcut, |
| 140 const std::string& supervised_user_id, | 146 const std::string& supervised_user_id, |
| 141 Profile* custodian_profile, | 147 Profile* custodian_profile, |
| 142 Profile* new_profile) { | 148 Profile* new_profile) { |
| 143 // Call the callback method to resume profile creation flow. | 149 // Call the callback method to resume profile creation flow. |
| 144 SigninCreateProfileHandler::OnSupervisedUserRegistered( | 150 SigninCreateProfileHandler::OnSupervisedUserRegistered( |
| 145 create_shortcut, | 151 create_shortcut, |
| 146 custodian_profile, | 152 custodian_profile, |
| 147 new_profile, | 153 new_profile, |
| 148 GoogleServiceAuthError(GoogleServiceAuthError::NONE)); | 154 GoogleServiceAuthError(GoogleServiceAuthError::NONE)); |
| 149 } | 155 } |
| 156 #endif |
| 150 | 157 |
| 151 private: | 158 private: |
| 152 TestingProfileManager* profile_manager_; | 159 TestingProfileManager* profile_manager_; |
| 153 DISALLOW_COPY_AND_ASSIGN(TestSigninCreateProfileHandler); | 160 DISALLOW_COPY_AND_ASSIGN(TestSigninCreateProfileHandler); |
| 154 }; | 161 }; |
| 155 | 162 |
| 156 class SigninCreateProfileHandlerTest : public BrowserWithTestWindowTest { | 163 class SigninCreateProfileHandlerTest : public BrowserWithTestWindowTest { |
| 157 public: | 164 public: |
| 158 SigninCreateProfileHandlerTest() | 165 SigninCreateProfileHandlerTest() |
| 159 : web_ui_(new content::TestWebUI) {} | 166 : web_ui_(new content::TestWebUI) {} |
| (...skipping 18 matching lines...) Expand all Loading... |
| 178 0, | 185 0, |
| 179 std::string(), | 186 std::string(), |
| 180 factories); | 187 factories); |
| 181 | 188 |
| 182 // Authenticate the custodian profile. | 189 // Authenticate the custodian profile. |
| 183 fake_signin_manager_ = static_cast<FakeSigninManagerForTesting*>( | 190 fake_signin_manager_ = static_cast<FakeSigninManagerForTesting*>( |
| 184 SigninManagerFactory::GetForProfile(custodian_)); | 191 SigninManagerFactory::GetForProfile(custodian_)); |
| 185 fake_signin_manager_->SetAuthenticatedAccountInfo(kTestGaiaId1, | 192 fake_signin_manager_->SetAuthenticatedAccountInfo(kTestGaiaId1, |
| 186 kTestEmail1); | 193 kTestEmail1); |
| 187 | 194 |
| 195 #if defined(ENABLE_SUPERVISED_USERS) |
| 188 // Add supervised users to the custodian profile. | 196 // Add supervised users to the custodian profile. |
| 189 SupervisedUserSyncService* sync_service_ = | 197 SupervisedUserSyncService* sync_service_ = |
| 190 SupervisedUserSyncServiceFactory::GetForProfile(custodian_); | 198 SupervisedUserSyncServiceFactory::GetForProfile(custodian_); |
| 191 syncer::SyncDataList sync_data; | 199 syncer::SyncDataList sync_data; |
| 192 sync_data.push_back(CreateSyncData(kSupervisedUserId1, | 200 sync_data.push_back(CreateSyncData(kSupervisedUserId1, |
| 193 kSupervisedUsername1, | 201 kSupervisedUsername1, |
| 194 kSupervisedUserAvatarName1)); | 202 kSupervisedUserAvatarName1)); |
| 195 sync_data.push_back(CreateSyncData(kSupervisedUserId2, | 203 sync_data.push_back(CreateSyncData(kSupervisedUserId2, |
| 196 kSupervisedUsername2, | 204 kSupervisedUsername2, |
| 197 kSupervisedUserAvatarName2)); | 205 kSupervisedUserAvatarName2)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 209 profile_manager()->CreateTestingProfile( | 217 profile_manager()->CreateTestingProfile( |
| 210 kSupervisedUsername2, | 218 kSupervisedUsername2, |
| 211 std::unique_ptr<syncable_prefs::PrefServiceSyncable>(), | 219 std::unique_ptr<syncable_prefs::PrefServiceSyncable>(), |
| 212 base::UTF8ToUTF16(kSupervisedUsername2), | 220 base::UTF8ToUTF16(kSupervisedUsername2), |
| 213 0, | 221 0, |
| 214 kSupervisedUserId2, // supervised_user_id | 222 kSupervisedUserId2, // supervised_user_id |
| 215 TestingProfile::TestingFactories()); | 223 TestingProfile::TestingFactories()); |
| 216 | 224 |
| 217 EXPECT_EQ(2u, | 225 EXPECT_EQ(2u, |
| 218 profile_manager()->profile_attributes_storage()->GetNumberOfProfiles()); | 226 profile_manager()->profile_attributes_storage()->GetNumberOfProfiles()); |
| 227 #endif |
| 219 } | 228 } |
| 220 | 229 |
| 221 void TearDown() override { | 230 void TearDown() override { |
| 222 profile_manager_.reset(); | 231 profile_manager_.reset(); |
| 223 handler_.reset(); | 232 handler_.reset(); |
| 224 BrowserWithTestWindowTest::TearDown(); | 233 BrowserWithTestWindowTest::TearDown(); |
| 225 } | 234 } |
| 226 | 235 |
| 227 content::TestWebUI* web_ui() { | 236 content::TestWebUI* web_ui() { |
| 228 return web_ui_.get(); | 237 return web_ui_.get(); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 ASSERT_TRUE(profile->GetString("name", &profile_name)); | 368 ASSERT_TRUE(profile->GetString("name", &profile_name)); |
| 360 EXPECT_NE("", profile_name); | 369 EXPECT_NE("", profile_name); |
| 361 std::string profile_path; | 370 std::string profile_path; |
| 362 ASSERT_TRUE(profile->GetString("filePath", &profile_path)); | 371 ASSERT_TRUE(profile->GetString("filePath", &profile_path)); |
| 363 EXPECT_NE("", profile_path); | 372 EXPECT_NE("", profile_path); |
| 364 bool is_supervised; | 373 bool is_supervised; |
| 365 ASSERT_TRUE(profile->GetBoolean("isSupervised", &is_supervised)); | 374 ASSERT_TRUE(profile->GetBoolean("isSupervised", &is_supervised)); |
| 366 ASSERT_FALSE(is_supervised); | 375 ASSERT_FALSE(is_supervised); |
| 367 } | 376 } |
| 368 | 377 |
| 378 #if defined(ENABLE_SUPERVISED_USERS) |
| 379 |
| 369 TEST_F(SigninCreateProfileHandlerTest, CreateSupervisedUser) { | 380 TEST_F(SigninCreateProfileHandlerTest, CreateSupervisedUser) { |
| 370 // Expect the call to create the profile. | 381 // Expect the call to create the profile. |
| 371 EXPECT_CALL(*handler(), DoCreateProfile(_, _, _, _, _)) | 382 EXPECT_CALL(*handler(), DoCreateProfile(_, _, _, _, _)) |
| 372 .WillOnce(Invoke(handler(), | 383 .WillOnce(Invoke(handler(), |
| 373 &TestSigninCreateProfileHandler::RealDoCreateProfile)); | 384 &TestSigninCreateProfileHandler::RealDoCreateProfile)); |
| 374 | 385 |
| 375 // Expect the call to register the supervised user. | 386 // Expect the call to register the supervised user. |
| 376 EXPECT_CALL(*handler(), RegisterSupervisedUser(_, _, _, _)) | 387 EXPECT_CALL(*handler(), RegisterSupervisedUser(_, _, _, _)) |
| 377 .WillOnce(Invoke( | 388 .WillOnce(Invoke( |
| 378 handler(), | 389 handler(), |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 588 std::string callback_name; | 599 std::string callback_name; |
| 589 ASSERT_TRUE(web_ui()->call_data()[0]->arg1()->GetAsString(&callback_name)); | 600 ASSERT_TRUE(web_ui()->call_data()[0]->arg1()->GetAsString(&callback_name)); |
| 590 EXPECT_EQ("create-profile-error", callback_name); | 601 EXPECT_EQ("create-profile-error", callback_name); |
| 591 | 602 |
| 592 base::string16 expected_error_message = l10n_util::GetStringUTF16( | 603 base::string16 expected_error_message = l10n_util::GetStringUTF16( |
| 593 IDS_PROFILES_CREATE_SUPERVISED_NOT_ALLOWED_BY_POLICY); | 604 IDS_PROFILES_CREATE_SUPERVISED_NOT_ALLOWED_BY_POLICY); |
| 594 base::string16 error_message; | 605 base::string16 error_message; |
| 595 ASSERT_TRUE(web_ui()->call_data()[0]->arg2()->GetAsString(&error_message)); | 606 ASSERT_TRUE(web_ui()->call_data()[0]->arg2()->GetAsString(&error_message)); |
| 596 EXPECT_EQ(expected_error_message, error_message); | 607 EXPECT_EQ(expected_error_message, error_message); |
| 597 } | 608 } |
| 609 |
| 610 #endif |
| OLD | NEW |