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.android.settings.accessibility.TextReadingPreviewController.PREVIEW_SAMPLE_RES_IDS; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.ArgumentMatchers.anyBoolean; 24 import static org.mockito.ArgumentMatchers.anyInt; 25 import static org.mockito.ArgumentMatchers.eq; 26 import static org.mockito.Mockito.spy; 27 import static org.mockito.Mockito.verify; 28 29 import android.content.Context; 30 import android.content.res.Configuration; 31 import android.view.LayoutInflater; 32 import android.view.View; 33 import android.widget.LinearLayout; 34 35 import androidx.preference.PreferenceViewHolder; 36 import androidx.test.core.app.ApplicationProvider; 37 import androidx.viewpager.widget.ViewPager; 38 39 import com.android.settings.R; 40 import com.android.settings.display.PreviewPagerAdapter; 41 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 58 @Before setUp()59 public void setUp() { 60 final Context context = ApplicationProvider.getApplicationContext(); 61 final Configuration[] configurations = createConfigurations(PREVIEW_SAMPLE_RES_IDS.length); 62 mTextReadingPreviewPreference = new TextReadingPreviewPreference(context); 63 mPreviewPagerAdapter = 64 spy(new PreviewPagerAdapter(context, /* isLayoutRtl= */ false, 65 PREVIEW_SAMPLE_RES_IDS, configurations)); 66 final LayoutInflater inflater = LayoutInflater.from(context); 67 final View view = 68 inflater.inflate(mTextReadingPreviewPreference.getLayoutResource(), 69 new LinearLayout(context), false); 70 mHolder = PreferenceViewHolder.createInstanceForTests(view); 71 mViewPager = view.findViewById(R.id.preview_pager); 72 } 73 74 @Test setPreviewerAdapter_success()75 public void setPreviewerAdapter_success() { 76 mTextReadingPreviewPreference.setPreviewAdapter(mPreviewPagerAdapter); 77 mTextReadingPreviewPreference.onBindViewHolder(mHolder); 78 79 assertThat(mViewPager.getAdapter()).isEqualTo(mPreviewPagerAdapter); 80 } 81 82 @Test setPreviewAdapterWithNull_resetCurrentItem()83 public void setPreviewAdapterWithNull_resetCurrentItem() { 84 final int currentItem = 2; 85 mTextReadingPreviewPreference.setPreviewAdapter(mPreviewPagerAdapter); 86 mTextReadingPreviewPreference.setCurrentItem(currentItem); 87 mTextReadingPreviewPreference.onBindViewHolder(mHolder); 88 89 mTextReadingPreviewPreference.setPreviewAdapter(null); 90 mTextReadingPreviewPreference.onBindViewHolder(mHolder); 91 92 assertThat(mTextReadingPreviewPreference.getCurrentItem()).isEqualTo(0); 93 } 94 95 @Test setCurrentItem_success()96 public void setCurrentItem_success() { 97 final int currentItem = 1; 98 mTextReadingPreviewPreference.setPreviewAdapter(mPreviewPagerAdapter); 99 mTextReadingPreviewPreference.onBindViewHolder(mHolder); 100 101 mTextReadingPreviewPreference.setCurrentItem(currentItem); 102 mTextReadingPreviewPreference.onBindViewHolder(mHolder); 103 104 assertThat(mViewPager.getCurrentItem()).isEqualTo(currentItem); 105 } 106 107 @Test(expected = NullPointerException.class) setCurrentItemBeforeSetPreviewAdapter_throwNPE()108 public void setCurrentItemBeforeSetPreviewAdapter_throwNPE() { 109 final int currentItem = 5; 110 111 mTextReadingPreviewPreference.setCurrentItem(currentItem); 112 } 113 114 @Test(expected = NullPointerException.class) updatePagerWithoutPreviewAdapter_throwNPE()115 public void updatePagerWithoutPreviewAdapter_throwNPE() { 116 final int index = 1; 117 118 mTextReadingPreviewPreference.notifyPreviewPagerChanged(index); 119 } 120 121 @Test notifyPreviewPager_setPreviewLayer()122 public void notifyPreviewPager_setPreviewLayer() { 123 final int index = 2; 124 mTextReadingPreviewPreference.setPreviewAdapter(mPreviewPagerAdapter); 125 mTextReadingPreviewPreference.onBindViewHolder(mHolder); 126 127 mTextReadingPreviewPreference.notifyPreviewPagerChanged(index); 128 129 verify(mPreviewPagerAdapter).setPreviewLayer(eq(index), anyInt(), anyInt(), anyBoolean()); 130 } 131 132 @Test afterPagerChange_updateCurrentItem()133 public void afterPagerChange_updateCurrentItem() { 134 final int currentItem = 2; 135 mTextReadingPreviewPreference.setPreviewAdapter(mPreviewPagerAdapter); 136 mTextReadingPreviewPreference.onBindViewHolder(mHolder); 137 138 mViewPager.setCurrentItem(currentItem); 139 140 assertThat(mTextReadingPreviewPreference.getCurrentItem()).isEqualTo(currentItem); 141 } 142 createConfigurations(int count)143 private static Configuration[] createConfigurations(int count) { 144 final Configuration[] configurations = new Configuration[count]; 145 for (int i = 0; i < count; i++) { 146 configurations[i] = new Configuration(); 147 } 148 149 return configurations; 150 } 151 } 152