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