1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/profiles/profile_downloader.h"
#include "base/functional/bind.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/task_environment.h"
#include "chrome/browser/profiles/profile_downloader_delegate.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
#include "chrome/browser/signin/test_signin_client_builder.h"
#include "chrome/test/base/testing_profile.h"
#include "components/signin/public/base/test_signin_client.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const char kTestEmail[] = "test@example.com";
const char kTestHostedDomain[] = "google.com";
const char kTestFullName[] = "full_name";
const char kTestGivenName[] = "given_name";
const char kTestLocale[] = "locale";
const char kTestValidPictureURL[] = "http://www.google.com/";
const char kTestInvalidPictureURL[] = "invalid_picture_url";
} // namespace
class ProfileDownloaderTest
: public testing::Test,
public ProfileDownloaderDelegate,
public signin::IdentityManager::DiagnosticsObserver {
protected:
ProfileDownloaderTest() : profile_downloader_(this) {
identity_test_env_.identity_manager()->AddDiagnosticsObserver(this);
}
~ProfileDownloaderTest() override {
identity_test_env_.identity_manager()->RemoveDiagnosticsObserver(this);
}
bool NeedsProfilePicture() const override { return true; }
int GetDesiredImageSideLength() const override { return 128; }
std::string GetCachedPictureURL() const override { return std::string(); }
signin::IdentityManager* GetIdentityManager() override {
return identity_test_env_.identity_manager();
}
network::mojom::URLLoaderFactory* GetURLLoaderFactory() override {
return &test_url_loader_factory_;
}
void OnProfileDownloadSuccess(ProfileDownloader* downloader) override {
}
void OnProfileDownloadFailure(
ProfileDownloader* downloader,
ProfileDownloaderDelegate::FailureReason reason) override {}
void SimulateUserInfoSuccess(const std::string& picture_url,
const AccountInfo& account_info) {
identity_test_env_.SimulateSuccessfulFetchOfAccountInfo(
account_info.account_id, account_info.email, account_info.gaia,
kTestHostedDomain, kTestFullName, kTestGivenName, kTestLocale,
picture_url);
}
// IdentityManager::DiagnosticsObserver:
void OnAccessTokenRequested(const CoreAccountId& account_id,
const std::string& consumer_id,
const signin::ScopeSet& scopes) override {
// This flow should be invoked only when a test has explicitly set up
// preconditions so that ProfileDownloader will request access tokens.
DCHECK(!on_access_token_request_callback_.is_null());
account_id_for_access_token_request_ = account_id;
std::move(on_access_token_request_callback_).Run();
}
void set_on_access_token_requested_callback(base::OnceClosure callback) {
on_access_token_request_callback_ = std::move(callback);
}
base::test::TaskEnvironment task_environment_;
network::TestURLLoaderFactory test_url_loader_factory_;
signin::IdentityTestEnvironment identity_test_env_;
ProfileDownloader profile_downloader_;
base::OnceClosure on_access_token_request_callback_;
CoreAccountId account_id_for_access_token_request_;
};
TEST_F(ProfileDownloaderTest, FetchAccessToken) {
AccountInfo account_info =
identity_test_env_.MakeAccountAvailable(kTestEmail);
identity_test_env_.SetRefreshTokenForAccount(account_info.account_id);
base::RunLoop run_loop;
set_on_access_token_requested_callback(run_loop.QuitClosure());
profile_downloader_.StartForAccount(account_info.account_id);
run_loop.Run();
EXPECT_EQ(account_info.account_id, account_id_for_access_token_request_);
}
TEST_F(ProfileDownloaderTest, AccountInfoReady) {
AccountInfo account_info =
identity_test_env_.MakeAccountAvailable(kTestEmail);
SimulateUserInfoSuccess(kTestValidPictureURL, account_info);
ASSERT_EQ(ProfileDownloader::PICTURE_FAILED,
profile_downloader_.GetProfilePictureStatus());
base::RunLoop run_loop;
set_on_access_token_requested_callback(run_loop.QuitClosure());
profile_downloader_.StartForAccount(account_info.account_id);
run_loop.Run();
profile_downloader_.StartFetchingImage();
ASSERT_EQ(kTestValidPictureURL, profile_downloader_.GetProfilePictureURL());
}
TEST_F(ProfileDownloaderTest, AccountInfoNotReady) {
AccountInfo account_info =
identity_test_env_.MakeAccountAvailable(kTestEmail);
ASSERT_EQ(ProfileDownloader::PICTURE_FAILED,
profile_downloader_.GetProfilePictureStatus());
base::RunLoop run_loop;
set_on_access_token_requested_callback(run_loop.QuitClosure());
profile_downloader_.StartForAccount(account_info.account_id);
run_loop.Run();
profile_downloader_.StartFetchingImage();
SimulateUserInfoSuccess(kTestValidPictureURL, account_info);
ASSERT_EQ(kTestValidPictureURL, profile_downloader_.GetProfilePictureURL());
}
// Regression test for http://crbug.com/854907
TEST_F(ProfileDownloaderTest, AccountInfoNoPictureDoesNotCrash) {
AccountInfo account_info =
identity_test_env_.MakeAccountAvailable(kTestEmail);
SimulateUserInfoSuccess(kNoPictureURLFound, account_info);
base::RunLoop run_loop;
set_on_access_token_requested_callback(run_loop.QuitClosure());
profile_downloader_.StartForAccount(account_info.account_id);
run_loop.Run();
profile_downloader_.StartFetchingImage();
EXPECT_TRUE(profile_downloader_.GetProfilePictureURL().empty());
ASSERT_EQ(ProfileDownloader::PICTURE_DEFAULT,
profile_downloader_.GetProfilePictureStatus());
}
// Regression test for http://crbug.com/854907
TEST_F(ProfileDownloaderTest, AccountInfoInvalidPictureURLDoesNotCrash) {
AccountInfo account_info =
identity_test_env_.MakeAccountAvailable(kTestEmail);
SimulateUserInfoSuccess(kTestInvalidPictureURL, account_info);
base::RunLoop run_loop;
set_on_access_token_requested_callback(run_loop.QuitClosure());
profile_downloader_.StartForAccount(account_info.account_id);
run_loop.Run();
profile_downloader_.StartFetchingImage();
EXPECT_TRUE(profile_downloader_.GetProfilePictureURL().empty());
ASSERT_EQ(ProfileDownloader::PICTURE_FAILED,
profile_downloader_.GetProfilePictureStatus());
}
|