Commit 68593fb0 authored by Alice Wang's avatar Alice Wang Committed by Chromium LUCI CQ

[Android][Signin][Test] Add JUnit tests for ProfileDownloader

This CL adds a set of JUnit tests for the class ProfileDownloader.

Bug: 1161437
Change-Id: Iead7068b730b5ca6c62b7edb8bf893aa7457e60e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2607108
Commit-Queue: Alice Wang <aliceywang@chromium.org>
Reviewed-by: default avatarTanmoy Mollik <triploblastic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#840111}
parent 3b85aa06
......@@ -81,6 +81,7 @@ android_library("junit") {
bypass_platform_checks = true
testonly = true
sources = [
"junit/src/org/chromium/chrome/browser/signin/services/ProfileDownloaderTest.java",
"junit/src/org/chromium/chrome/browser/signin/services/SigninHelperTest.java",
"junit/src/org/chromium/chrome/browser/signin/services/SigninPreferencesManagerTest.java",
"junit/src/org/chromium/chrome/browser/signin/services/WebSigninBridgeTest.java",
......@@ -91,7 +92,9 @@ android_library("junit") {
"//base:base_junit_test_support",
"//chrome/browser/profiles/android:java",
"//chrome/test/android:chrome_java_test_support",
"//components/signin/core/browser/android:java",
"//components/signin/public/android:java",
"//third_party/android_deps:androidx_annotation_annotation_java",
"//third_party/junit",
"//third_party/mockito:mockito_java",
]
......
......@@ -140,17 +140,16 @@ public class ProfileDataCache implements ProfileDataSource.Observer, IdentityMan
* sent to observers of ProfileDownloader. The instance must have at least one observer (see
* {@link #addObserver}) when this method is called.
*/
public void update(List<String> accounts) {
public void update(List<String> accountEmails) {
ThreadUtils.assertOnUiThread();
assert !mObservers.isEmpty();
// ProfileDataSource is updated automatically.
if (mProfileDataSource != null) return;
for (int i = 0; i < accounts.size(); i++) {
if (mCachedProfileData.get(accounts.get(i)) == null) {
ProfileDownloader.get().startFetchingAccountInfoFor(
mContext, accounts.get(i), mImageSize, true);
for (String accountEmail : accountEmails) {
if (!mCachedProfileData.containsKey(accountEmail)) {
ProfileDownloader.get().startFetchingAccountInfoFor(accountEmail, mImageSize);
}
}
}
......
......@@ -4,9 +4,11 @@
package org.chromium.chrome.browser.signin.services;
import android.content.Context;
import android.graphics.Bitmap;
import androidx.annotation.Px;
import androidx.annotation.VisibleForTesting;
import org.chromium.base.ObserverList;
import org.chromium.base.ThreadUtils;
import org.chromium.base.annotations.CalledByNative;
......@@ -64,25 +66,27 @@ class ProfileDownloader {
}
/**
* Private class to pend profile download requests when system accounts have not been seeded
* into AccountTrackerService. It listens onSystemAccountsSeedingComplete to finish pending
* Private class (package private for tests) to pend profile download requests when system
* accounts have not been seeded into AccountTrackerService.
* It listens onSystemAccountsSeedingComplete to finish pending
* requests and onSystemAccountsChanged to clear outdated pending requests.
*/
private static class PendingProfileDownloads
@VisibleForTesting
static class PendingProfileDownloads
implements AccountTrackerService.OnSystemAccountsSeededListener {
private static PendingProfileDownloads sPendingProfileDownloads;
private final ArrayList<Profile> mProfiles;
private final ArrayList<String> mAccountEmails;
private final ArrayList<Integer> mImageSidePixels;
private final ArrayList<Integer> mImageSizes;
private PendingProfileDownloads() {
mProfiles = new ArrayList<>();
mAccountEmails = new ArrayList<>();
mImageSidePixels = new ArrayList<>();
mImageSizes = new ArrayList<>();
}
static PendingProfileDownloads get(Context context) {
static PendingProfileDownloads get() {
ThreadUtils.assertOnUiThread();
if (sPendingProfileDownloads == null) {
sPendingProfileDownloads = new PendingProfileDownloads();
......@@ -93,10 +97,10 @@ class ProfileDownloader {
return sPendingProfileDownloads;
}
public void pendProfileDownload(Profile profile, String accountEmail, int imageSidePixels) {
void pendProfileDownload(Profile profile, String accountEmail, int imageSize) {
mProfiles.add(profile);
mAccountEmails.add(accountEmail);
mImageSidePixels.add(imageSidePixels);
mImageSizes.add(imageSize);
}
@Override
......@@ -106,10 +110,10 @@ class ProfileDownloader {
// Pending requests here must be pre-signin request since SigninManager will wait
// system accounts been seeded into AccountTrackerService before finishing sign in.
ProfileDownloaderJni.get().startFetchingAccountInfoFor(
mProfiles.get(0), mAccountEmails.get(0), mImageSidePixels.get(0), true);
mProfiles.get(0), mAccountEmails.get(0), mImageSizes.get(0), true);
mProfiles.remove(0);
mAccountEmails.remove(0);
mImageSidePixels.remove(0);
mImageSizes.remove(0);
numberOfPendingRequests--;
}
}
......@@ -118,33 +122,31 @@ class ProfileDownloader {
public void onSystemAccountsChanged() {
mProfiles.clear();
mAccountEmails.clear();
mImageSidePixels.clear();
mImageSizes.clear();
}
}
/**
* Starts fetching the account information for a given account.
* @param context context associated with the request
* @param accountEmail Account email to fetch the information for
* @param imageSidePixels Request image side (in pixels)
* @param imageSize Request image side size (in pixels)
*/
public void startFetchingAccountInfoFor(
Context context, String accountEmail, int imageSidePixels, boolean isPreSignin) {
public void startFetchingAccountInfoFor(String accountEmail, @Px int imageSize) {
ThreadUtils.assertOnUiThread();
Profile profile = Profile.getLastUsedRegularProfile();
final Profile profile = Profile.getLastUsedRegularProfile();
if (!IdentityServicesProvider.get()
.getAccountTrackerService(profile)
.checkAndSeedSystemAccounts()) {
PendingProfileDownloads.get(context).pendProfileDownload(
profile, accountEmail, imageSidePixels);
PendingProfileDownloads.get().pendProfileDownload(profile, accountEmail, imageSize);
return;
}
ProfileDownloaderJni.get().startFetchingAccountInfoFor(
profile, accountEmail, imageSidePixels, isPreSignin);
profile, accountEmail, imageSize, /* isPreSignin= */ true);
}
@VisibleForTesting
@CalledByNative
private static void onProfileDownloadSuccess(
static void onProfileDownloadSuccess(
String accountEmail, String fullName, String givenName, Bitmap avatar) {
ThreadUtils.assertOnUiThread();
ProfileDownloader.get().notifyObservers(
......@@ -154,6 +156,6 @@ class ProfileDownloader {
@NativeMethods
interface Natives {
void startFetchingAccountInfoFor(
Profile profile, String accountEmail, int imageSidePixels, boolean isPreSignin);
Profile profile, String accountEmail, int imageSize, boolean isPreSignin);
}
}
// Copyright 2021 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.
package org.chromium.chrome.browser.signin.services;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.graphics.Bitmap;
import androidx.annotation.Px;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.base.test.util.JniMocker;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.signin.services.ProfileDownloader.PendingProfileDownloads;
import org.chromium.components.signin.AccountTrackerService;
import org.chromium.components.signin.ProfileDataSource;
import org.chromium.components.signin.ProfileDataSource.ProfileData;
/**
* Unit tests for {@link ProfileDownloader}.
*/
@RunWith(BaseRobolectricTestRunner.class)
public class ProfileDownloaderTest {
private static final String ACCOUNT_EMAIL = "test@gmail.com";
private static final @Px int IMAGE_SIZE = 64;
@Rule
public final JniMocker mocker = new JniMocker();
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
private ProfileDownloader.Natives mProfileDownloaderNativeMock;
@Mock
private Profile mProfileMock;
@Mock
private IdentityServicesProvider mIdentityServicesProviderMock;
@Mock
private AccountTrackerService mAccountTrackerServiceMock;
@Captor
private ArgumentCaptor<ProfileData> mProfileDataCaptor;
@Before
public void setUp() {
mocker.mock(ProfileDownloaderJni.TEST_HOOKS, mProfileDownloaderNativeMock);
Profile.setLastUsedProfileForTesting(mProfileMock);
IdentityServicesProvider.setInstanceForTests(mIdentityServicesProviderMock);
when(mIdentityServicesProviderMock.getAccountTrackerService(mProfileMock))
.thenReturn(mAccountTrackerServiceMock);
}
@Test
public void testFetchingAccountInfoWhenAccountsAreNotSeeded() {
when(mAccountTrackerServiceMock.checkAndSeedSystemAccounts()).thenReturn(false);
ProfileDownloader.get().startFetchingAccountInfoFor(ACCOUNT_EMAIL, IMAGE_SIZE);
final PendingProfileDownloads pendingProfileDownloads =
ProfileDownloader.PendingProfileDownloads.get();
verify(mAccountTrackerServiceMock).addSystemAccountsSeededListener(pendingProfileDownloads);
verify(mProfileDownloaderNativeMock, never())
.startFetchingAccountInfoFor(any(), anyString(), anyInt(), anyBoolean());
pendingProfileDownloads.onSystemAccountsSeedingComplete();
verify(mProfileDownloaderNativeMock)
.startFetchingAccountInfoFor(mProfileMock, ACCOUNT_EMAIL, IMAGE_SIZE, true);
}
@Test
public void testFetchingAccountInfoWhenAccountsAreSeeded() {
when(mAccountTrackerServiceMock.checkAndSeedSystemAccounts()).thenReturn(true);
ProfileDownloader.get().startFetchingAccountInfoFor(ACCOUNT_EMAIL, IMAGE_SIZE);
final PendingProfileDownloads pendingProfileDownloads =
ProfileDownloader.PendingProfileDownloads.get();
verify(mAccountTrackerServiceMock, never())
.addSystemAccountsSeededListener(pendingProfileDownloads);
verify(mProfileDownloaderNativeMock)
.startFetchingAccountInfoFor(mProfileMock, ACCOUNT_EMAIL, IMAGE_SIZE, true);
}
@Test
public void testOnProfileDownloadSuccess() {
final String fullName = "Full name";
final String givenName = "Given name";
final Bitmap avatar = mock(Bitmap.class);
final ProfileDataSource.Observer observer = mock(ProfileDataSource.Observer.class);
ProfileDownloader.get().addObserver(observer);
ProfileDownloader.onProfileDownloadSuccess(ACCOUNT_EMAIL, fullName, givenName, avatar);
verify(observer).onProfileDataUpdated(mProfileDataCaptor.capture());
final ProfileData profileData = mProfileDataCaptor.getValue();
Assert.assertEquals(ACCOUNT_EMAIL, profileData.getAccountEmail());
Assert.assertEquals(fullName, profileData.getFullName());
Assert.assertEquals(givenName, profileData.getGivenName());
Assert.assertEquals(avatar, profileData.getAvatar());
}
}
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