chromium/chrome/android/features/tab_ui/javatests/src/org/chromium/chrome/browser/tasks/tab_management/TabGroupUiViewBinderTest.java

// Copyright 2019 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.tasks.tab_management;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.test.annotation.UiThreadTest;
import androidx.test.filters.SmallTest;

import org.junit.Test;
import org.junit.runner.RunWith;

import org.chromium.base.ThreadUtils;
import org.chromium.chrome.tab_ui.R;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.ui.modelutil.PropertyModel;
import org.chromium.ui.modelutil.PropertyModelChangeProcessor;
import org.chromium.ui.test.util.BlankUiTestActivityTestCase;

import java.util.concurrent.atomic.AtomicBoolean;

/** Tests for {@link TabGroupUiViewBinder}. */
@RunWith(ChromeJUnit4ClassRunner.class)
public class TabGroupUiViewBinderTest extends BlankUiTestActivityTestCase {
    private ImageView mShowGroupDialogButton;
    private ImageView mNewTabButton;
    private ViewGroup mContainerView;
    private View mMainContent;

    private PropertyModel mModel;
    private PropertyModelChangeProcessor mMCP;

    @Override
    public void setUpTest() throws Exception {
        super.setUpTest();

        ThreadUtils.runOnUiThreadBlocking(
                () -> {
                    ViewGroup parentView = new FrameLayout(getActivity());
                    TabGroupUiToolbarView toolbarView =
                            (TabGroupUiToolbarView)
                                    LayoutInflater.from(getActivity())
                                            .inflate(
                                                    R.layout.bottom_tab_strip_toolbar,
                                                    parentView,
                                                    false);
                    mShowGroupDialogButton =
                            toolbarView.findViewById(R.id.toolbar_show_group_dialog_button);
                    mNewTabButton = toolbarView.findViewById(R.id.toolbar_new_tab_button);
                    mContainerView = toolbarView.findViewById(R.id.toolbar_container_view);
                    mMainContent = toolbarView.findViewById(R.id.main_content);
                    RecyclerView recyclerView =
                            (TabListRecyclerView)
                                    LayoutInflater.from(getActivity())
                                            .inflate(
                                                    R.layout.tab_list_recycler_view_layout,
                                                    parentView,
                                                    false);
                    recyclerView.setLayoutManager(
                            new LinearLayoutManager(
                                    getActivity(), LinearLayoutManager.HORIZONTAL, false));

                    mModel =
                            new PropertyModel.Builder(TabGroupUiProperties.ALL_KEYS)
                                    .with(TabGroupUiProperties.BACKGROUND_COLOR, Color.WHITE)
                                    .build();
                    mMCP =
                            PropertyModelChangeProcessor.create(
                                    mModel,
                                    new TabGroupUiViewBinder.ViewHolder(toolbarView, recyclerView),
                                    TabGroupUiViewBinder::bind);
                });
    }

    @Override
    public void tearDownTest() throws Exception {
        ThreadUtils.runOnUiThreadBlocking(mMCP::destroy);
        super.tearDownTest();
    }

    @Test
    @UiThreadTest
    @SmallTest
    public void testSetShowGroupDialogButtonOnClickListener() {
        AtomicBoolean leftButtonClicked = new AtomicBoolean();
        leftButtonClicked.set(false);
        mShowGroupDialogButton.performClick();
        assertFalse(leftButtonClicked.get());

        mModel.set(
                TabGroupUiProperties.SHOW_GROUP_DIALOG_BUTTON_ON_CLICK_LISTENER,
                (View view) -> leftButtonClicked.set(true));

        mShowGroupDialogButton.performClick();
        assertTrue(leftButtonClicked.get());
    }

    @Test
    @UiThreadTest
    @SmallTest
    public void testSetNewTabButtonOnClickListener() {
        AtomicBoolean rightButtonClicked = new AtomicBoolean();
        rightButtonClicked.set(false);
        mNewTabButton.performClick();
        assertFalse(rightButtonClicked.get());

        mModel.set(
                TabGroupUiProperties.NEW_TAB_BUTTON_ON_CLICK_LISTENER,
                (View view) -> rightButtonClicked.set(true));

        mNewTabButton.performClick();
        assertTrue(rightButtonClicked.get());
    }

    @Test
    @UiThreadTest
    @SmallTest
    public void testSetMainContentVisibility() {
        View contentView = new View(getActivity());
        mContainerView.addView(contentView);
        contentView.setVisibility(View.GONE);

        mModel.set(TabGroupUiProperties.IS_MAIN_CONTENT_VISIBLE, true);
        assertEquals(View.VISIBLE, contentView.getVisibility());

        mModel.set(TabGroupUiProperties.IS_MAIN_CONTENT_VISIBLE, false);
        assertEquals(View.INVISIBLE, contentView.getVisibility());
    }

    @Test
    @UiThreadTest
    @SmallTest
    public void testSetIncognito() {
        mModel.set(TabGroupUiProperties.IS_INCOGNITO, false);
        ColorStateList lightNewTabImageTint = mNewTabButton.getImageTintList();
        ColorStateList lightShowGroupDialogImageTint = mShowGroupDialogButton.getImageTintList();

        mModel.set(TabGroupUiProperties.IS_INCOGNITO, true);
        assertNotEquals(lightNewTabImageTint, mShowGroupDialogButton.getImageTintList());
        assertNotEquals(lightShowGroupDialogImageTint, mNewTabButton.getImageTintList());
    }

    @Test
    @UiThreadTest
    @SmallTest
    public void testSetBackgroundColor() {
        mModel.set(TabGroupUiProperties.BACKGROUND_COLOR, Color.WHITE);
        assertEquals(Color.WHITE, ((ColorDrawable) mMainContent.getBackground()).getColor());

        mModel.set(TabGroupUiProperties.BACKGROUND_COLOR, Color.BLACK);
        assertEquals(Color.BLACK, ((ColorDrawable) mMainContent.getBackground()).getColor());
    }

    @Test
    @UiThreadTest
    @SmallTest
    public void testSetShowGroupDialogButtonContentDescription() {
        assertNull(mShowGroupDialogButton.getContentDescription());

        String string = "show group dialog button content";
        mModel.set(TabGroupUiProperties.SHOW_GROUP_DIALOG_BUTTON_CONTENT_DESCRIPTION, string);

        assertEquals(string, mShowGroupDialogButton.getContentDescription());
    }
}