chromium/chrome/browser/ui/android/webid/internal/java/src/org/chromium/chrome/browser/ui/android/webid/AccountSelectionIntegrationTestBase.java

// Copyright 2024 The Chromium Authors
// 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.ui.android.webid;

import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;

import static org.chromium.base.ThreadUtils.runOnUiThreadBlocking;

import android.graphics.Color;

import org.junit.Before;
import org.junit.Rule;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import org.chromium.base.test.util.CriteriaHelper;
import org.chromium.base.test.util.ScalableTimeout;
import org.chromium.blink.mojom.RpContext;
import org.chromium.blink.mojom.RpMode;
import org.chromium.chrome.browser.ui.android.webid.data.Account;
import org.chromium.chrome.browser.ui.android.webid.data.ClientIdMetadata;
import org.chromium.chrome.browser.ui.android.webid.data.IdentityProviderData;
import org.chromium.chrome.browser.ui.android.webid.data.IdentityProviderMetadata;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
import org.chromium.components.browser_ui.bottomsheet.BottomSheetController;
import org.chromium.components.browser_ui.bottomsheet.BottomSheetController.SheetState;
import org.chromium.components.browser_ui.bottomsheet.BottomSheetControllerProvider;
import org.chromium.components.image_fetcher.ImageFetcher;
import org.chromium.url.GURL;
import org.chromium.url.JUnitTestGURLs;

/** Common test fixtures for AccountSelectionIntegration Android Javatests. */
public class AccountSelectionIntegrationTestBase {
    protected static final String EXAMPLE_ETLD_PLUS_ONE = "example.com";
    protected static final String TEST_ETLD_PLUS_ONE_2 = "two.com";
    protected static final GURL TEST_PROFILE_PIC = JUnitTestGURLs.URL_1_WITH_PATH;
    protected static final GURL TEST_URL = JUnitTestGURLs.URL_1;

    protected static final Account RETURNING_ANA =
            new Account(
                    "Ana",
                    "[email protected]",
                    "Ana Doe",
                    "Ana",
                    TEST_PROFILE_PIC,
                    null,
                    /* isSignIn= */ true,
                    /* isBrowserTrustedSignIn= */ true);
    protected static final Account NEW_BOB =
            new Account(
                    "Bob",
                    "",
                    "Bob",
                    "",
                    TEST_PROFILE_PIC,
                    null,
                    /* isSignIn= */ false,
                    /* isBrowserTrustedSignIn= */ false);

    protected static final IdentityProviderMetadata IDP_METADATA =
            new IdentityProviderMetadata(
                    /* brandTextColor= */ Color.WHITE,
                    /* brandBackgroundColor= */ Color.BLACK,
                    /* brandIconUrl= */ EXAMPLE_ETLD_PLUS_ONE,
                    /* configUrl= */ null,
                    /* loginUrl= */ null,
                    /* supportsAddAccount= */ false);
    protected static final IdentityProviderMetadata IDP_METADATA_WITH_ADD_ACCOUNT =
            new IdentityProviderMetadata(
                    /* brandTextColor= */ Color.WHITE,
                    /* brandBackgroundColor= */ Color.BLACK,
                    /* brandIconUrl= */ EXAMPLE_ETLD_PLUS_ONE,
                    /* configUrl= */ null,
                    /* loginUrl= */ null,
                    /* supportsAddAccount= */ true);

    AccountSelectionCoordinator mAccountSelection;

    @Mock AccountSelectionComponent.Delegate mMockBridge;
    @Mock ImageFetcher mMockImageFetcher;

    @Rule
    public ChromeTabbedActivityTestRule mActivityTestRule = new ChromeTabbedActivityTestRule();

    BottomSheetController mBottomSheetController;

    String mTestUrlTermsOfService;
    String mTestUrlPrivacyPolicy;
    ClientIdMetadata mClientIdMetadata;
    IdentityProviderData mNewAccountsIdpReturningAna;
    IdentityProviderData mNewAccountsIdpNewBob;
    @RpMode.EnumType int mRpMode;

    @Before
    public void setUp() throws InterruptedException {
        MockitoAnnotations.initMocks(this);
        mActivityTestRule.startMainActivityOnBlankPage();

        mTestUrlTermsOfService =
                mActivityTestRule.getTestServer().getURL("/chrome/test/data/title1.html");
        mTestUrlPrivacyPolicy =
                mActivityTestRule.getTestServer().getURL("/chrome/test/data/title2.html");
        mClientIdMetadata =
                new ClientIdMetadata(
                        new GURL(mTestUrlTermsOfService),
                        new GURL(mTestUrlPrivacyPolicy),
                        EXAMPLE_ETLD_PLUS_ONE);
        mNewAccountsIdpReturningAna =
                new IdentityProviderData(
                        EXAMPLE_ETLD_PLUS_ONE,
                        new Account[] {RETURNING_ANA},
                        IDP_METADATA_WITH_ADD_ACCOUNT,
                        mClientIdMetadata,
                        RpContext.SIGN_IN,
                        /* requestPermission= */ true,
                        /* hasLoginStatusMismatch= */ false);
        mNewAccountsIdpNewBob =
                new IdentityProviderData(
                        EXAMPLE_ETLD_PLUS_ONE,
                        new Account[] {NEW_BOB},
                        IDP_METADATA_WITH_ADD_ACCOUNT,
                        mClientIdMetadata,
                        RpContext.SIGN_IN,
                        /* requestPermission= */ true,
                        /* hasLoginStatusMismatch= */ false);

        runOnUiThreadBlocking(
                () -> {
                    mBottomSheetController =
                            BottomSheetControllerProvider.from(
                                    mActivityTestRule.getActivity().getWindowAndroid());
                    mAccountSelection =
                            new AccountSelectionCoordinator(
                                    mActivityTestRule.getActivity().getActivityTab(),
                                    mActivityTestRule.getActivity().getWindowAndroid(),
                                    mBottomSheetController,
                                    mRpMode,
                                    mMockBridge);
                    mAccountSelection.getMediator().setImageFetcher(mMockImageFetcher);
                });
    }

    @SheetState
    int getBottomSheetState() {
        return mBottomSheetController.getSheetState();
    }

    public static <T> T waitForEvent(T mock) {
        return verify(
                mock,
                timeout(ScalableTimeout.scaleTimeout(CriteriaHelper.DEFAULT_MAX_TIME_TO_POLL)));
    }
}