1 /* 2 * Copyright (C) 2020 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 package com.android.settings.widget; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import android.os.Parcelable; 21 import android.text.TextUtils; 22 import android.view.View; 23 import android.view.ViewGroup; 24 25 import androidx.test.core.app.ApplicationProvider; 26 import androidx.test.ext.junit.runners.AndroidJUnit4; 27 import androidx.viewpager.widget.PagerAdapter; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 33 import java.util.Locale; 34 35 @RunWith(AndroidJUnit4.class) 36 public class RtlCompatibleViewPagerTest { 37 38 private Locale mLocaleEn; 39 private Locale mLocaleHe; 40 private RtlCompatibleViewPager mViewPager; 41 42 @Before setUp()43 public void setUp() { 44 mViewPager = new RtlCompatibleViewPager(ApplicationProvider.getApplicationContext()); 45 mViewPager.setAdapter(new ViewPagerAdapter()); 46 mLocaleEn = new Locale("en"); 47 mLocaleHe = new Locale("he"); 48 } 49 50 @Test testGetCurrentItem_shouldMaintainIndexDuringLocaleChange()51 public void testGetCurrentItem_shouldMaintainIndexDuringLocaleChange() { 52 testRtlCompatibleInner(0); 53 testRtlCompatibleInner(1); 54 } 55 testRtlCompatibleInner(int currentItem)56 private void testRtlCompatibleInner(int currentItem) { 57 // Set up the environment 58 Locale.setDefault(mLocaleEn); 59 mViewPager.setCurrentItem(currentItem); 60 61 assertThat(TextUtils.getLayoutDirectionFromLocale(Locale.getDefault())) 62 .isEqualTo(View.LAYOUT_DIRECTION_LTR); 63 assertThat(mViewPager.getCurrentItem()).isEqualTo(currentItem); 64 65 // Simulate to change the language to RTL 66 Parcelable savedInstance = mViewPager.onSaveInstanceState(); 67 Locale.setDefault(mLocaleHe); 68 mViewPager.onRestoreInstanceState(savedInstance); 69 70 assertThat(TextUtils.getLayoutDirectionFromLocale(Locale.getDefault())) 71 .isEqualTo(View.LAYOUT_DIRECTION_RTL); 72 assertThat(mViewPager.getCurrentItem()).isEqualTo(currentItem); 73 } 74 75 /** 76 * Test viewpager adapter, uses 2 view to test it 77 */ 78 private static final class ViewPagerAdapter extends PagerAdapter { 79 80 private static final int ITEM_COUNT = 2; 81 ViewPagerAdapter()82 private ViewPagerAdapter() { 83 } 84 85 @Override getCount()86 public int getCount() { 87 return ITEM_COUNT; 88 } 89 90 @Override isViewFromObject(View view, Object object)91 public boolean isViewFromObject(View view, Object object) { 92 return view == object; 93 } 94 95 @Override instantiateItem(ViewGroup container, int position)96 public Object instantiateItem(ViewGroup container, int position) { 97 return null; 98 } 99 100 @Override getPageTitle(int position)101 public CharSequence getPageTitle(int position) { 102 return null; 103 } 104 } 105 } 106