1+ package com .vogella .android .test .uiautomator ;
2+
3+ import android .content .Intent ;
4+ import android .content .pm .PackageManager ;
5+ import android .content .pm .ResolveInfo ;
6+ import android .support .test .InstrumentationRegistry ;
7+ import android .support .test .rule .ActivityTestRule ;
8+ import android .support .test .runner .AndroidJUnit4 ;
9+ import android .support .test .uiautomator .By ;
10+ import android .sucom .vogella .android .test .uiautomatorpport .test .uiautomator .UiDevice ;
11+ import android .support .test .uiautomator .UiObject ;
12+ import android .support .test .uiautomator .UiObjectNotFoundException ;
13+ import android .support .test .uiautomator .UiScrollable ;
14+ import android .support .test .uiautomator .UiSelector ;
15+ import android .support .test .uiautomator .Until ;
16+
17+ import org .hamcrest .Matchers ;
18+ import org .junit .Before ;
19+ import org .junit .Rule ;
20+ import org .junit .Test ;
21+ import org .junit .runner .RunWith ;
22+
23+ import static org .hamcrest .Matchers .*;
24+ import static org .junit .Assert .*;
25+
26+ @ RunWith (AndroidJUnit4 .class )
27+ public class MyUiAutomatorTest {
28+ @ Rule
29+ public ActivityTestRule <MainActivity > mActivityRule = new ActivityTestRule <MainActivity >(MainActivity .class );
30+
31+ private UiDevice mDevice ;
32+
33+ @ Before
34+ public void setUp () {
35+ // Initialize UiDevice instance
36+
37+ mDevice = UiDevice .getInstance (InstrumentationRegistry .getInstrumentation ());
38+ // Start from the home screen
39+ mDevice .pressHome ();
40+
41+ mDevice .wait (Until .hasObject (By .pkg (getLauncherPackageName ()).depth (0 )), 1000 );
42+ }
43+
44+ @ Test
45+ public void checkSettings () throws UiObjectNotFoundException {
46+
47+ // Simulate a short press on the HOME button.
48+ mDevice .pressHome ();
49+
50+ // We’re now in the home screen. Next, we want to simulate
51+ // a user bringing up the All Apps screen.
52+ // If you use the uiautomatorviewer tool to capture a snapshot
53+ // of the Home screen, notice that the All Apps button’s
54+ // content-description property has the value “Apps”. We can
55+ // use this property to create a UiSelector to find the button.
56+ UiObject allAppsButton = new UiObject (new UiSelector ().description ("Apps" ));
57+
58+ // Simulate a click to bring up the All Apps screen.
59+ allAppsButton .clickAndWaitForNewWindow ();
60+
61+ // In the All Apps screen, the Settings app is located in
62+ // the Apps tab. To simulate the user bringing up the Apps tab,
63+ // we create a UiSelector to find a tab with the text
64+ // label “Apps”.
65+ UiObject appsTab = new UiObject (new UiSelector ().text ("Apps" ));
66+
67+ // Simulate a click to enter the Apps tab.
68+ appsTab .click ();
69+
70+ // Next, in the apps tabs, we can simulate a user swiping until
71+ // they come to the Settings app icon. Since the container view
72+ // is scrollable, we can use a UiScrollable object.
73+ UiScrollable appViews = new UiScrollable (
74+ new UiSelector ().scrollable (true ));
75+
76+ // Set the swiping mode to horizontal (the default is vertical)
77+ appViews .setAsHorizontalList ();
78+
79+ // create a UiSelector to find the Settings app and simulate
80+ // a user click to launch the app.
81+ UiObject settingsApp = appViews
82+ .getChildByText (new UiSelector ()
83+ .className (android .widget .TextView .class .getName ()),
84+ "Settings" );
85+ settingsApp .clickAndWaitForNewWindow ();
86+
87+ // Validate that the package name is the expected one
88+ UiObject settingsValidation = new UiObject (
89+ new UiSelector ()
90+ .packageName ("com.android.settings" ));
91+ assertThat (settingsValidation .exists (), equalTo (true ));
92+ }
93+
94+ private String getLauncherPackageName () {
95+ // Create launcher Intent
96+ final Intent intent = new Intent (Intent .ACTION_MAIN );
97+ intent .addCategory (Intent .CATEGORY_HOME );
98+
99+ // Use PackageManager to get the launcher package name
100+ PackageManager pm = InstrumentationRegistry .getContext ().getPackageManager ();
101+ ResolveInfo resolveInfo = pm .resolveActivity (intent , PackageManager .MATCH_DEFAULT_ONLY );
102+ return resolveInfo .activityInfo .packageName ;
103+ }
104+ }
0 commit comments