1 package com.android.settings;
2 
3 import android.content.Intent;
4 import android.content.Context;
5 
6 import android.app.Instrumentation;
7 import android.os.Bundle;
8 import android.support.v7.preference.Preference;
9 import android.support.v7.preference.PreferenceGroupAdapter;
10 import com.android.settings.accessibility.AccessibilitySettings;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import android.support.test.InstrumentationRegistry;
15 import android.support.test.filters.SmallTest;
16 import android.support.test.runner.AndroidJUnit4;
17 
18 import static android.support.test.espresso.Espresso.onView;
19 import static android.support.test.espresso.assertion.ViewAssertions.matches;
20 import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
21 import static android.support.test.espresso.matcher.ViewMatchers.withText;
22 import static com.google.common.truth.Truth.assertThat;
23 
24 @RunWith(AndroidJUnit4.class)
25 @SmallTest
26 public class SettingsPreferenceFragmentTest {
27 
28     private Instrumentation mInstrumentation;
29     private Context mTargetContext;
30 
31     @Before
setUp()32     public void setUp() throws Exception {
33         mInstrumentation = InstrumentationRegistry.getInstrumentation();
34         mTargetContext = mInstrumentation.getTargetContext();
35     }
36 
37     @Test
testHighlightCaptions()38     public void testHighlightCaptions() throws InterruptedException {
39         final String prefKey = "captioning_preference_screen";
40         Bundle args = new Bundle();
41         args.putString(SettingsActivity.EXTRA_FRAGMENT_ARG_KEY, prefKey);
42 
43         Intent intent = new Intent(Intent.ACTION_MAIN);
44         intent.setClass(mTargetContext, SubSettings.class);
45         intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT,
46                 "com.android.settings.accessibility.AccessibilitySettings");
47         intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
48 
49         SettingsActivity activity  = (SettingsActivity) mInstrumentation.startActivitySync(intent);
50         AccessibilitySettings fragment = (AccessibilitySettings)
51                 activity.getFragmentManager().getFragments().get(0);
52 
53         // Allow time for highlight from post-delay.
54         Thread.sleep(SettingsPreferenceFragment.DELAY_HIGHLIGHT_DURATION_MILLIS);
55         if (!fragment.mPreferenceHighlighted) {
56             Thread.sleep(SettingsPreferenceFragment.DELAY_HIGHLIGHT_DURATION_MILLIS);
57         }
58 
59         int prefPosition = -1;
60         PreferenceGroupAdapter adapter = (PreferenceGroupAdapter)
61                 fragment.getListView().getAdapter();
62         for (int n = 0, count = adapter.getItemCount(); n < count; n++) {
63             final Preference preference = adapter.getItem(n);
64             final String preferenceKey = preference.getKey();
65             if (preferenceKey.equals(prefKey)) {
66                 prefPosition = n;
67                 break;
68             }
69         }
70 
71         assertThat(fragment.mAdapter.initialHighlightedPosition).isEqualTo(prefPosition);
72     }
73 }
74