Commit 44342369 authored by Marc Treib's avatar Marc Treib Committed by Commit Bot

Sync integration tests: Move secondary account helpers into own files

This moves a bunch of helper functions around secondary account support
from single_client_secondary_account_sync_test.cc into their own files.
This is in preparation for adding more integration tests that will make
use of these.

Bug: 885211
Change-Id: I1f30b2425951933f6f9251f35c4aafb7e1cdb663
Reviewed-on: https://chromium-review.googlesource.com/1233704Reviewed-by: default avatarJan Krcal <jkrcal@chromium.org>
Commit-Queue: Marc Treib <treib@chromium.org>
Cr-Commit-Position: refs/heads/master@{#592761}
parent dad5078a
// Copyright 2018 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/sync/test/integration/secondary_account_helper.h"
#include "base/bind.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/account_tracker_service_factory.h"
#include "chrome/browser/signin/fake_gaia_cookie_manager_service_builder.h"
#include "chrome/browser/signin/gaia_cookie_manager_service_factory.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/signin/core/browser/fake_gaia_cookie_manager_service.h"
#include "components/signin/core/browser/signin_manager.h"
#include "services/identity/public/cpp/identity_test_utils.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/net/network_portal_detector_test_impl.h"
#include "chromeos/network/network_handler.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h"
#include "chromeos/network/portal_detector/network_portal_detector.h"
#endif // defined(OS_CHROMEOS)
namespace secondary_account_helper {
namespace {
void OnWillCreateBrowserContextServices(content::BrowserContext* context) {
GaiaCookieManagerServiceFactory::GetInstance()->SetTestingFactory(
context, &BuildFakeGaiaCookieManagerService);
}
} // namespace
ScopedFakeGaiaCookieManagerServiceFactory SetUpFakeGaiaCookieManagerService() {
return BrowserContextDependencyManager::GetInstance()
->RegisterWillCreateBrowserContextServicesCallbackForTesting(
base::BindRepeating(&OnWillCreateBrowserContextServices));
}
#if defined(OS_CHROMEOS)
void InitNetwork() {
auto* portal_detector = new chromeos::NetworkPortalDetectorTestImpl();
const chromeos::NetworkState* default_network =
chromeos::NetworkHandler::Get()
->network_state_handler()
->DefaultNetwork();
portal_detector->SetDefaultNetworkForTesting(default_network->guid());
chromeos::NetworkPortalDetector::CaptivePortalState online_state;
online_state.status =
chromeos::NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
online_state.response_code = 204;
portal_detector->SetDetectionResultsForTesting(default_network->guid(),
online_state);
// Takes ownership.
chromeos::network_portal_detector::InitializeForTesting(portal_detector);
}
#endif // defined(OS_CHROMEOS)
void SignInSecondaryAccount(Profile* profile, const std::string& email) {
identity::IdentityManager* identity_manager =
IdentityManagerFactory::GetForProfile(profile);
AccountInfo account_info = identity::MakeAccountAvailable(
AccountTrackerServiceFactory::GetForProfile(profile),
ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
identity_manager, email);
FakeGaiaCookieManagerService* fake_cookie_service =
static_cast<FakeGaiaCookieManagerService*>(
GaiaCookieManagerServiceFactory::GetForProfile(profile));
identity::SetCookieAccounts(fake_cookie_service, identity_manager,
{{account_info.email, account_info.gaia}});
}
#if !defined(OS_CHROMEOS)
void MakeAccountPrimary(Profile* profile, const std::string& email) {
// This is implemented in the same way as in
// DiceTurnSyncOnHelper::SigninAndShowSyncConfirmationUI.
// TODO(blundell): IdentityManager should support this use case, so we don't
// have to go through the legacy API.
SigninManagerFactory::GetForProfile(profile)->OnExternalSigninCompleted(
email);
}
#endif // !defined(OS_CHROMEOS)
} // namespace secondary_account_helper
// Copyright 2018 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.
#ifndef CHROME_BROWSER_SYNC_TEST_INTEGRATION_SECONDARY_ACCOUNT_HELPER_H_
#define CHROME_BROWSER_SYNC_TEST_INTEGRATION_SECONDARY_ACCOUNT_HELPER_H_
#include <memory>
#include <string>
#include "base/callback_list.h"
#include "build/build_config.h"
class Profile;
namespace content {
class BrowserContext;
} // namespace content
namespace secondary_account_helper {
using ScopedFakeGaiaCookieManagerServiceFactory = std::unique_ptr<
base::CallbackList<void(content::BrowserContext*)>::Subscription>;
// Sets up a factory to create a FakeGaiaCookieManagerService. Meant to be
// called from SetUpInProcessBrowserTestFixture. The caller should hold on to
// the returned object for the duration of the test, e.g. store it in a member
// of the test fixture class.
ScopedFakeGaiaCookieManagerServiceFactory SetUpFakeGaiaCookieManagerService();
#if defined(OS_CHROMEOS)
// Sets up necessary fakes for fake network responses to work. Meant to be
// called from SetUpOnMainThread.
// TODO(crbug.com/882770): On ChromeOS, we need to set up a fake
// NetworkPortalDetector, otherwise chromeos::DelayNetworkCall will think it's
// behind a captive portal and delay all network requests forever, which means
// the ListAccounts requests (i.e. getting cookie accounts) will never make it
// far enough to even request our fake response.
void InitNetwork();
#endif // defined(OS_CHROMEOS)
// Makes a non-primary account available with both a refresh token and cookie.
void SignInSecondaryAccount(Profile* profile, const std::string& email);
#if !defined(OS_CHROMEOS)
// Makes the given account Chrome's primary one. The account must already be
// signed in (per SignInSecondaryAccount).
void MakeAccountPrimary(Profile* profile, const std::string& email);
#endif // !defined(OS_CHROMEOS)
} // namespace secondary_account_helper
#endif // CHROME_BROWSER_SYNC_TEST_INTEGRATION_SECONDARY_ACCOUNT_HELPER_H_
...@@ -2,37 +2,19 @@ ...@@ -2,37 +2,19 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "base/bind.h"
#include "base/callback_list.h" #include "base/callback_list.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/browser/defaults.h" #include "chrome/browser/defaults.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/account_tracker_service_factory.h"
#include "chrome/browser/signin/fake_gaia_cookie_manager_service_builder.h"
#include "chrome/browser/signin/gaia_cookie_manager_service_factory.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/sync/test/integration/profile_sync_service_harness.h" #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
#include "chrome/browser/sync/test/integration/secondary_account_helper.h"
#include "chrome/browser/sync/test/integration/single_client_status_change_checker.h" #include "chrome/browser/sync/test/integration/single_client_status_change_checker.h"
#include "chrome/browser/sync/test/integration/sync_test.h" #include "chrome/browser/sync/test/integration/sync_test.h"
#include "components/browser_sync/profile_sync_service.h" #include "components/browser_sync/profile_sync_service.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/signin/core/browser/fake_gaia_cookie_manager_service.h"
#include "components/signin/core/browser/signin_manager.h"
#include "components/sync/base/model_type.h" #include "components/sync/base/model_type.h"
#include "components/sync/driver/sync_driver_switches.h" #include "components/sync/driver/sync_driver_switches.h"
#include "services/identity/public/cpp/identity_test_utils.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/net/network_portal_detector_test_impl.h"
#include "chromeos/network/network_handler.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h"
#include "chromeos/network/portal_detector/network_portal_detector.h"
#endif // defined(OS_CHROMEOS)
namespace { namespace {
...@@ -56,89 +38,23 @@ class SingleClientSecondaryAccountSyncTest : public SyncTest { ...@@ -56,89 +38,23 @@ class SingleClientSecondaryAccountSyncTest : public SyncTest {
~SingleClientSecondaryAccountSyncTest() override {} ~SingleClientSecondaryAccountSyncTest() override {}
void SetUpInProcessBrowserTestFixture() override { void SetUpInProcessBrowserTestFixture() override {
will_create_browser_context_services_subscription_ = fake_gaia_cookie_manager_factory_ =
BrowserContextDependencyManager::GetInstance() secondary_account_helper::SetUpFakeGaiaCookieManagerService();
->RegisterWillCreateBrowserContextServicesCallbackForTesting(
base::BindRepeating(&SingleClientSecondaryAccountSyncTest::
OnWillCreateBrowserContextServices,
base::Unretained(this)));
} }
void SetUpOnMainThread() override { void SetUpOnMainThread() override {
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
InitNetwork(); secondary_account_helper::InitNetwork();
#endif // defined(OS_CHROMEOS) #endif // defined(OS_CHROMEOS)
} }
Profile* profile() { return GetProfile(0); } Profile* profile() { return GetProfile(0); }
// Makes a non-primary account available with both a refresh token and cookie.
void SignInSecondaryAccount(const std::string& email) {
identity::IdentityManager* identity_manager =
IdentityManagerFactory::GetForProfile(profile());
AccountInfo account_info = identity::MakeAccountAvailable(
AccountTrackerServiceFactory::GetForProfile(profile()),
ProfileOAuth2TokenServiceFactory::GetForProfile(profile()),
identity_manager, email);
FakeGaiaCookieManagerService* fake_cookie_service =
static_cast<FakeGaiaCookieManagerService*>(
GaiaCookieManagerServiceFactory::GetForProfile(profile()));
identity::SetCookieAccounts(fake_cookie_service, identity_manager,
{{account_info.email, account_info.gaia}});
}
#if !defined(OS_CHROMEOS)
// Makes the given account Chrome's primary one. The account must already be
// signed in (per SignInSecondaryAccount).
void MakeAccountPrimary(const std::string& email) {
// This is implemented in the same way as in
// DiceTurnSyncOnHelper::SigninAndShowSyncConfirmationUI.
// TODO(blundell): IdentityManager should support this use case, so we don't
// have to go through the legacy API.
SigninManagerFactory::GetForProfile(profile())->OnExternalSigninCompleted(
email);
}
#endif // !defined(OS_CHROMEOS)
private: private:
void OnWillCreateBrowserContextServices(content::BrowserContext* context) {
GaiaCookieManagerServiceFactory::GetInstance()->SetTestingFactory(
context, &BuildFakeGaiaCookieManagerService);
}
#if defined(OS_CHROMEOS)
// TODO(crbug.com/882770): On ChromeOS, we need to set up a fake
// NetworkPortalDetector, otherwise chromeos::DelayNetworkCall will think it's
// behind a captive portal and delay all network requests forever, which means
// the ListAccounts requests (i.e. getting cookie accounts) will never make it
// far enough to even request our fake response.
void InitNetwork() {
auto* portal_detector = new chromeos::NetworkPortalDetectorTestImpl();
const chromeos::NetworkState* default_network =
chromeos::NetworkHandler::Get()
->network_state_handler()
->DefaultNetwork();
portal_detector->SetDefaultNetworkForTesting(default_network->guid());
chromeos::NetworkPortalDetector::CaptivePortalState online_state;
online_state.status =
chromeos::NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
online_state.response_code = 204;
portal_detector->SetDetectionResultsForTesting(default_network->guid(),
online_state);
// Takes ownership.
chromeos::network_portal_detector::InitializeForTesting(portal_detector);
}
#endif // defined(OS_CHROMEOS)
base::test::ScopedFeatureList features_; base::test::ScopedFeatureList features_;
std::unique_ptr< secondary_account_helper::ScopedFakeGaiaCookieManagerServiceFactory
base::CallbackList<void(content::BrowserContext*)>::Subscription> fake_gaia_cookie_manager_factory_;
will_create_browser_context_services_subscription_;
DISALLOW_COPY_AND_ASSIGN(SingleClientSecondaryAccountSyncTest); DISALLOW_COPY_AND_ASSIGN(SingleClientSecondaryAccountSyncTest);
}; };
...@@ -153,7 +69,7 @@ IN_PROC_BROWSER_TEST_F(SingleClientSecondaryAccountSyncTest, ...@@ -153,7 +69,7 @@ IN_PROC_BROWSER_TEST_F(SingleClientSecondaryAccountSyncTest,
// Since standalone transport is disabled, just signing in (without making the // Since standalone transport is disabled, just signing in (without making the
// account Chrome's primary one) should *not* start the Sync machinery. // account Chrome's primary one) should *not* start the Sync machinery.
SignInSecondaryAccount("user@email.com"); secondary_account_helper::SignInSecondaryAccount(profile(), "user@email.com");
EXPECT_EQ(syncer::SyncService::TransportState::DISABLED, EXPECT_EQ(syncer::SyncService::TransportState::DISABLED,
GetSyncService(0)->GetTransportState()); GetSyncService(0)->GetTransportState());
} }
...@@ -169,7 +85,7 @@ IN_PROC_BROWSER_TEST_F(SingleClientSecondaryAccountSyncTest, ...@@ -169,7 +85,7 @@ IN_PROC_BROWSER_TEST_F(SingleClientSecondaryAccountSyncTest,
// Since secondary account support is disabled, just signing in (without // Since secondary account support is disabled, just signing in (without
// making the account Chrome's primary one) should *not* start the Sync // making the account Chrome's primary one) should *not* start the Sync
// machinery. // machinery.
SignInSecondaryAccount("user@email.com"); secondary_account_helper::SignInSecondaryAccount(profile(), "user@email.com");
EXPECT_EQ(syncer::SyncService::TransportState::DISABLED, EXPECT_EQ(syncer::SyncService::TransportState::DISABLED,
GetSyncService(0)->GetTransportState()); GetSyncService(0)->GetTransportState());
} }
...@@ -181,7 +97,7 @@ IN_PROC_BROWSER_TEST_F(SingleClientSecondaryAccountSyncTest, ...@@ -181,7 +97,7 @@ IN_PROC_BROWSER_TEST_F(SingleClientSecondaryAccountSyncTest,
// Signing in (without making the account Chrome's primary one or explicitly // Signing in (without making the account Chrome's primary one or explicitly
// setting up Sync) should trigger starting the Sync machinery in standalone // setting up Sync) should trigger starting the Sync machinery in standalone
// transport mode. // transport mode.
SignInSecondaryAccount("user@email.com"); secondary_account_helper::SignInSecondaryAccount(profile(), "user@email.com");
if (browser_defaults::kSyncAutoStarts) { if (browser_defaults::kSyncAutoStarts) {
EXPECT_EQ(syncer::SyncService::TransportState::INITIALIZING, EXPECT_EQ(syncer::SyncService::TransportState::INITIALIZING,
GetSyncService(0)->GetTransportState()); GetSyncService(0)->GetTransportState());
...@@ -219,7 +135,7 @@ IN_PROC_BROWSER_TEST_F(SingleClientSecondaryAccountSyncTest, ...@@ -219,7 +135,7 @@ IN_PROC_BROWSER_TEST_F(SingleClientSecondaryAccountSyncTest,
ASSERT_TRUE(SetupClients()) << "SetupClients() failed."; ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
// Set up Sync in transport mode for a non-primary account. // Set up Sync in transport mode for a non-primary account.
SignInSecondaryAccount("user@email.com"); secondary_account_helper::SignInSecondaryAccount(profile(), "user@email.com");
ASSERT_TRUE(GetClient(0)->AwaitSyncSetupCompletion( ASSERT_TRUE(GetClient(0)->AwaitSyncSetupCompletion(
/*skip_passphrase_verification=*/false)); /*skip_passphrase_verification=*/false));
ASSERT_EQ(syncer::SyncService::TransportState::ACTIVE, ASSERT_EQ(syncer::SyncService::TransportState::ACTIVE,
...@@ -229,7 +145,7 @@ IN_PROC_BROWSER_TEST_F(SingleClientSecondaryAccountSyncTest, ...@@ -229,7 +145,7 @@ IN_PROC_BROWSER_TEST_F(SingleClientSecondaryAccountSyncTest,
// Simulate the user opting in to full Sync: Make the account primary, and // Simulate the user opting in to full Sync: Make the account primary, and
// set first-time setup to complete. // set first-time setup to complete.
MakeAccountPrimary("user@email.com"); secondary_account_helper::MakeAccountPrimary(profile(), "user@email.com");
GetSyncService(0)->SetFirstSetupComplete(); GetSyncService(0)->SetFirstSetupComplete();
EXPECT_TRUE(GetClient(0)->AwaitSyncSetupCompletion( EXPECT_TRUE(GetClient(0)->AwaitSyncSetupCompletion(
......
...@@ -5354,6 +5354,8 @@ if (!is_android && !is_fuchsia) { ...@@ -5354,6 +5354,8 @@ if (!is_android && !is_fuchsia) {
"../browser/sync/test/integration/retry_verifier.h", "../browser/sync/test/integration/retry_verifier.h",
"../browser/sync/test/integration/search_engines_helper.cc", "../browser/sync/test/integration/search_engines_helper.cc",
"../browser/sync/test/integration/search_engines_helper.h", "../browser/sync/test/integration/search_engines_helper.h",
"../browser/sync/test/integration/secondary_account_helper.cc",
"../browser/sync/test/integration/secondary_account_helper.h",
"../browser/sync/test/integration/session_hierarchy_match_checker.cc", "../browser/sync/test/integration/session_hierarchy_match_checker.cc",
"../browser/sync/test/integration/session_hierarchy_match_checker.h", "../browser/sync/test/integration/session_hierarchy_match_checker.h",
"../browser/sync/test/integration/sessions_helper.cc", "../browser/sync/test/integration/sessions_helper.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment