1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.chrome.browser.tasks.tab_management;
6 
7 import static org.junit.Assert.assertEquals;
8 import static org.junit.Assert.assertFalse;
9 import static org.junit.Assert.assertNotEquals;
10 import static org.junit.Assert.assertNull;
11 import static org.junit.Assert.assertTrue;
12 
13 import android.content.res.ColorStateList;
14 import android.graphics.drawable.ColorDrawable;
15 import android.graphics.drawable.Drawable;
16 import android.view.LayoutInflater;
17 import android.view.View;
18 import android.view.ViewGroup;
19 import android.widget.FrameLayout;
20 import android.widget.ImageView;
21 
22 import androidx.recyclerview.widget.LinearLayoutManager;
23 import androidx.recyclerview.widget.RecyclerView;
24 import androidx.test.filters.SmallTest;
25 
26 import org.junit.Assert;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 import org.chromium.base.test.UiThreadTest;
31 import org.chromium.chrome.browser.toolbar.ToolbarColors;
32 import org.chromium.chrome.tab_ui.R;
33 import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
34 import org.chromium.ui.modelutil.PropertyModel;
35 import org.chromium.ui.modelutil.PropertyModelChangeProcessor;
36 import org.chromium.ui.test.util.DummyUiActivityTestCase;
37 
38 import java.util.concurrent.atomic.AtomicBoolean;
39 
40 /**
41  * Tests for {@link TabGroupUiViewBinder}.
42  */
43 @RunWith(ChromeJUnit4ClassRunner.class)
44 public class TabGroupUiViewBinderTest extends DummyUiActivityTestCase {
45     private ImageView mLeftButton;
46     private ImageView mRightButton;
47     private ViewGroup mContainerView;
48     private View mMainContent;
49 
50     private PropertyModel mModel;
51     private PropertyModelChangeProcessor mMCP;
52 
53     @Override
setUpTest()54     public void setUpTest() throws Exception {
55         super.setUpTest();
56 
57         ViewGroup parentView = new FrameLayout(getActivity());
58         TabGroupUiToolbarView toolbarView =
59                 (TabGroupUiToolbarView) LayoutInflater.from(getActivity())
60                         .inflate(R.layout.bottom_tab_strip_toolbar, parentView, false);
61         mLeftButton = toolbarView.findViewById(R.id.toolbar_left_button);
62         mRightButton = toolbarView.findViewById(R.id.toolbar_right_button);
63         mContainerView = toolbarView.findViewById(R.id.toolbar_container_view);
64         mMainContent = toolbarView.findViewById(R.id.main_content);
65         RecyclerView recyclerView =
66                 (TabListRecyclerView) LayoutInflater.from(getActivity())
67                         .inflate(R.layout.tab_list_recycler_view_layout, parentView, false);
68         recyclerView.setLayoutManager(
69                 new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
70 
71         mModel = new PropertyModel(TabGroupUiProperties.ALL_KEYS);
72         mMCP = PropertyModelChangeProcessor.create(mModel,
73                 new TabGroupUiViewBinder.ViewHolder(toolbarView, recyclerView),
74                 TabGroupUiViewBinder::bind);
75     }
76 
77     @Override
tearDownTest()78     public void tearDownTest() throws Exception {
79         mMCP.destroy();
80         super.tearDownTest();
81     }
82 
83     @Test
84     @UiThreadTest
85     @SmallTest
testSetLeftButtonOnClickListener()86     public void testSetLeftButtonOnClickListener() {
87         AtomicBoolean leftButtonClicked = new AtomicBoolean();
88         leftButtonClicked.set(false);
89         mLeftButton.performClick();
90         assertFalse(leftButtonClicked.get());
91 
92         mModel.set(TabGroupUiProperties.LEFT_BUTTON_ON_CLICK_LISTENER,
93                 (View view) -> leftButtonClicked.set(true));
94 
95         mLeftButton.performClick();
96         assertTrue(leftButtonClicked.get());
97     }
98 
99     @Test
100     @UiThreadTest
101     @SmallTest
testSetRightButtonOnClickListener()102     public void testSetRightButtonOnClickListener() {
103         AtomicBoolean rightButtonClicked = new AtomicBoolean();
104         rightButtonClicked.set(false);
105         mRightButton.performClick();
106         assertFalse(rightButtonClicked.get());
107 
108         mModel.set(TabGroupUiProperties.RIGHT_BUTTON_ON_CLICK_LISTENER,
109                 (View view) -> rightButtonClicked.set(true));
110 
111         mRightButton.performClick();
112         assertTrue(rightButtonClicked.get());
113     }
114 
115     @Test
116     @UiThreadTest
117     @SmallTest
testSetMainContentVisibility()118     public void testSetMainContentVisibility() {
119         View contentView = new View(getActivity());
120         mContainerView.addView(contentView);
121         contentView.setVisibility(View.GONE);
122 
123         mModel.set(TabGroupUiProperties.IS_MAIN_CONTENT_VISIBLE, true);
124         assertEquals(View.VISIBLE, contentView.getVisibility());
125 
126         mModel.set(TabGroupUiProperties.IS_MAIN_CONTENT_VISIBLE, false);
127         assertEquals(View.INVISIBLE, contentView.getVisibility());
128     }
129 
130     @Test
131     @UiThreadTest
132     @SmallTest
testSetLeftButtonDrawable()133     public void testSetLeftButtonDrawable() {
134         int expandLessDrawableId = R.drawable.ic_expand_less_black_24dp;
135         int expandMoreDrawableId = R.drawable.ic_expand_more_black_24dp;
136 
137         mModel.set(TabGroupUiProperties.LEFT_BUTTON_DRAWABLE_ID, expandLessDrawableId);
138         Drawable expandLessDrawable = mLeftButton.getDrawable();
139         mModel.set(TabGroupUiProperties.LEFT_BUTTON_DRAWABLE_ID, expandMoreDrawableId);
140         Drawable expandMoreDrawable = mLeftButton.getDrawable();
141 
142         assertNotEquals(expandLessDrawable, expandMoreDrawable);
143     }
144 
145     @Test
146     @UiThreadTest
147     @SmallTest
testSetTint()148     public void testSetTint() {
149         ColorStateList tint = ToolbarColors.getThemedToolbarIconTint(getActivity(), true);
150         Assert.assertNotEquals(tint, mLeftButton.getImageTintList());
151         Assert.assertNotEquals(tint, mRightButton.getImageTintList());
152 
153         mModel.set(TabGroupUiProperties.TINT, tint);
154 
155         Assert.assertEquals(tint, mLeftButton.getImageTintList());
156         Assert.assertEquals(tint, mRightButton.getImageTintList());
157     }
158 
159     @Test
160     @UiThreadTest
161     @SmallTest
testSetPrimaryColor()162     public void testSetPrimaryColor() {
163         int colorGrey = R.color.modern_grey_300;
164         int colorBlue = R.color.modern_blue_300;
165 
166         mModel.set(TabGroupUiProperties.PRIMARY_COLOR, colorGrey);
167         int greyDrawableId = ((ColorDrawable) mMainContent.getBackground()).getColor();
168         mModel.set(TabGroupUiProperties.PRIMARY_COLOR, colorBlue);
169         int blueDrawableId = ((ColorDrawable) mMainContent.getBackground()).getColor();
170 
171         assertNotEquals(greyDrawableId, blueDrawableId);
172     }
173 
174     @Test
175     @UiThreadTest
176     @SmallTest
testSetLeftButtonContentDescription()177     public void testSetLeftButtonContentDescription() {
178         assertNull(mLeftButton.getContentDescription());
179 
180         String string = "left button content";
181         mModel.set(TabGroupUiProperties.LEFT_BUTTON_CONTENT_DESCRIPTION, string);
182 
183         assertEquals(string, mLeftButton.getContentDescription());
184     }
185 
186     @Test
187     @UiThreadTest
188     @SmallTest
testSetRightButtonContentDescription()189     public void testSetRightButtonContentDescription() {
190         assertNull(mRightButton.getContentDescription());
191 
192         String string = "right button content";
193         mModel.set(TabGroupUiProperties.RIGHT_BUTTON_CONTENT_DESCRIPTION, string);
194 
195         assertEquals(string, mRightButton.getContentDescription());
196     }
197 }
198