1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.accessibility;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.anyBoolean;
22 import static org.mockito.ArgumentMatchers.anyInt;
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
26 
27 import android.content.Context;
28 import android.content.res.Configuration;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.widget.FrameLayout;
32 import android.widget.LinearLayout;
33 
34 import androidx.preference.PreferenceViewHolder;
35 import androidx.test.core.app.ApplicationProvider;
36 import androidx.viewpager.widget.ViewPager;
37 
38 import com.android.settings.R;
39 import com.android.settings.display.PreviewPagerAdapter;
40 
41 import org.junit.Assume;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.robolectric.RobolectricTestRunner;
46 
47 /**
48  * Tests for {@link TextReadingPreferenceFragment}.
49  */
50 @RunWith(RobolectricTestRunner.class)
51 public class TextReadingPreviewPreferenceTest {
52 
53     private TextReadingPreviewPreference mTextReadingPreviewPreference;
54     private PreferenceViewHolder mHolder;
55     private ViewPager mViewPager;
56     private PreviewPagerAdapter mPreviewPagerAdapter;
57     private int mPreviewSampleCount;
58 
59     @Before
setUp()60     public void setUp() {
61         final Context context = ApplicationProvider.getApplicationContext();
62         final int[] previewSamples = TextReadingPreviewController.getPreviewSampleLayouts(context);
63         mPreviewSampleCount = previewSamples.length;
64         final Configuration[] configurations = createConfigurations(mPreviewSampleCount);
65         mTextReadingPreviewPreference = new TextReadingPreviewPreference(context);
66         mPreviewPagerAdapter =
67                 spy(new PreviewPagerAdapter(context, /* isLayoutRtl= */ false,
68                         previewSamples, configurations));
69         final LayoutInflater inflater = LayoutInflater.from(context);
70         final View view =
71                 inflater.inflate(mTextReadingPreviewPreference.getLayoutResource(),
72                         new LinearLayout(context), false);
73         mHolder = PreferenceViewHolder.createInstanceForTests(view);
74         mViewPager = view.findViewById(R.id.preview_pager);
75     }
76 
77     @Test
setPreviewerAdapter_success()78     public void setPreviewerAdapter_success() {
79         mTextReadingPreviewPreference.setPreviewAdapter(mPreviewPagerAdapter);
80         mTextReadingPreviewPreference.onBindViewHolder(mHolder);
81 
82         assertThat(mViewPager.getAdapter()).isEqualTo(mPreviewPagerAdapter);
83     }
84 
85     @Test
setPreviewAdapterWithNull_resetCurrentItem()86     public void setPreviewAdapterWithNull_resetCurrentItem() {
87         final int currentItem = mPreviewSampleCount - 1;
88         mTextReadingPreviewPreference.setPreviewAdapter(mPreviewPagerAdapter);
89         mTextReadingPreviewPreference.setCurrentItem(currentItem);
90         mTextReadingPreviewPreference.onBindViewHolder(mHolder);
91 
92         mTextReadingPreviewPreference.setPreviewAdapter(null);
93         mTextReadingPreviewPreference.onBindViewHolder(mHolder);
94 
95         assertThat(mTextReadingPreviewPreference.getCurrentItem()).isEqualTo(0);
96     }
97 
98     @Test
setCurrentItem_success()99     public void setCurrentItem_success() {
100         final int currentItem = mPreviewSampleCount - 1;
101         mTextReadingPreviewPreference.setPreviewAdapter(mPreviewPagerAdapter);
102         mTextReadingPreviewPreference.onBindViewHolder(mHolder);
103 
104         mTextReadingPreviewPreference.setCurrentItem(currentItem);
105         mTextReadingPreviewPreference.onBindViewHolder(mHolder);
106 
107         assertThat(mViewPager.getCurrentItem()).isEqualTo(currentItem);
108     }
109 
110     @Test(expected = NullPointerException.class)
setCurrentItemBeforeSetPreviewAdapter_throwNPE()111     public void setCurrentItemBeforeSetPreviewAdapter_throwNPE() {
112         final int currentItem = mPreviewSampleCount + 2;
113 
114         mTextReadingPreviewPreference.setCurrentItem(currentItem);
115     }
116 
117     @Test(expected = NullPointerException.class)
updatePagerWithoutPreviewAdapter_throwNPE()118     public void updatePagerWithoutPreviewAdapter_throwNPE() {
119         final int index = mPreviewSampleCount - 1;
120 
121         mTextReadingPreviewPreference.notifyPreviewPagerChanged(index);
122     }
123 
124     @Test
notifyPreviewPager_setPreviewLayer()125     public void notifyPreviewPager_setPreviewLayer() {
126         // The preview pager cannot switch page if there is only one preview layout, so skip the
127         // test if so
128         Assume.assumeTrue(mPreviewSampleCount > 1);
129 
130         final int index = mPreviewSampleCount - 1;
131         mTextReadingPreviewPreference.setPreviewAdapter(mPreviewPagerAdapter);
132         mTextReadingPreviewPreference.onBindViewHolder(mHolder);
133 
134         mTextReadingPreviewPreference.notifyPreviewPagerChanged(index);
135 
136         verify(mPreviewPagerAdapter).setPreviewLayer(eq(index), anyInt(), anyInt(), anyBoolean());
137     }
138 
139     @Test
afterPagerChange_updateCurrentItem()140     public void afterPagerChange_updateCurrentItem() {
141         final int currentItem = mPreviewSampleCount - 1;
142         mTextReadingPreviewPreference.setPreviewAdapter(mPreviewPagerAdapter);
143         mTextReadingPreviewPreference.onBindViewHolder(mHolder);
144 
145         mViewPager.setCurrentItem(currentItem);
146 
147         assertThat(mTextReadingPreviewPreference.getCurrentItem()).isEqualTo(currentItem);
148     }
149 
150     @Test
adjustPaddings_setMinPaddingsLessThanXMLValue_paddingsNotIncreased()151     public void adjustPaddings_setMinPaddingsLessThanXMLValue_paddingsNotIncreased() {
152         // get the default xml padding value
153         mTextReadingPreviewPreference.onBindViewHolder(mHolder);
154         final FrameLayout previewLayout = (FrameLayout) mHolder.itemView;
155         final LinearLayout backgroundView = previewLayout.findViewById(R.id.preview_background);
156 
157         final int currentLayoutPaddingStart = previewLayout.getPaddingStart();
158         final int currentBackgroundPaddingStart = backgroundView.getPaddingStart();
159         mTextReadingPreviewPreference.setLayoutMinHorizontalPadding(
160                 currentLayoutPaddingStart - 10);
161         mTextReadingPreviewPreference.setBackgroundMinHorizontalPadding(
162                 currentBackgroundPaddingStart - 10);
163         mTextReadingPreviewPreference.adjustPaddings(previewLayout, backgroundView);
164 
165         assertThat(previewLayout.getPaddingStart()).isEqualTo(currentLayoutPaddingStart);
166         assertThat(backgroundView.getPaddingStart()).isEqualTo(currentBackgroundPaddingStart);
167     }
168 
169     @Test
adjustPaddings_setMinPaddingsLargerThanXMLValue_paddingsIncreased()170     public void adjustPaddings_setMinPaddingsLargerThanXMLValue_paddingsIncreased() {
171         // get the default xml padding value
172         mTextReadingPreviewPreference.onBindViewHolder(mHolder);
173         final FrameLayout previewLayout = (FrameLayout) mHolder.itemView;
174         final LinearLayout backgroundView = previewLayout.findViewById(R.id.preview_background);
175 
176         final int currentLayoutPaddingStart = previewLayout.getPaddingStart();
177         final int currentBackgroundPaddingStart = backgroundView.getPaddingStart();
178         mTextReadingPreviewPreference.setLayoutMinHorizontalPadding(
179                 currentLayoutPaddingStart + 10);
180         mTextReadingPreviewPreference.setBackgroundMinHorizontalPadding(
181                 currentBackgroundPaddingStart + 10);
182         mTextReadingPreviewPreference.adjustPaddings(previewLayout, backgroundView);
183 
184         assertThat(previewLayout.getPaddingStart()).isEqualTo(currentLayoutPaddingStart + 10);
185         assertThat(backgroundView.getPaddingStart()).isEqualTo(currentBackgroundPaddingStart + 10);
186     }
187 
createConfigurations(int count)188     private static Configuration[] createConfigurations(int count) {
189         final Configuration[] configurations = new Configuration[count];
190         for (int i = 0; i < count; i++) {
191             configurations[i] = new Configuration();
192         }
193 
194         return configurations;
195     }
196 }
197