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