Testing Android Widgets with UI Automator
This is how you test your app widgets on Home Screen
Setup:
Add Test dependencies
//Testing
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:core:1.3.0-beta01';
androidTestImplementation 'androidx.test:runner:1.3.0-beta01';// UiAutomator Testing
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0';
androidTestImplementation 'org.hamcrest:hamcrest-integration:1.3'
Adding your Test class
In the Android Project View of Android Studio go to
src->androidTest-> java->packageName->
add a new test class
Testing
Now simply use UIAutomator to navigate to the Home Screen of the Launcher by pressing the Home Button.
On the home screen, if your widget is added you can use findObject
method to get views of the widget. And once you have the view object for the RemoteView
, you can do whatever you like.
Here is a simple widget test case example for checking if the widget is added and launches your app:
import androidx.test.filters.SdkSuppress;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;
import androidx.test.uiautomator.Until;import org.junit.Before;
import org.junit.Test;import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;@SdkSuppress(minSdkVersion = 18)
public class WidgetAddedTest {
private static final String APP_PACKAGE_NAME = "com.example.myweather";
private static final int LAUNCH_TIMEOUT = 5000;
private UiDevice device; @Before
public void startMainActivityFromHomeScreen() {
// Initialize UiDevice instance
device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); // Start from the home screen
device.pressHome(); // Wait for launcher
final String launcherPackage = device.getLauncherPackageName();
assertThat(launcherPackage, notNullValue());
device.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
LAUNCH_TIMEOUT);
}
@Test
public void widgetAddedTest() {
//Check text on Widget
UiObject2 titleTextView = device.findObject(By.res(APP_PACKAGE_NAME, "title"));
assertEquals("Weather", titleTextView.getText()); //CHeck correct value on Widget
UiObject2 temperatureTextView = device.findObject(By.res(APP_PACKAGE_NAME, "temperatureTextView"));
assertEquals("20", temperatureTextView.getText()); //Launch App From Widget Test
temperatureTextView.click(); // Wait for the app to appear
device.wait(Until.hasObject(By.pkg(APP_PACKAGE_NAME).depth(0)),
LAUNCH_TIMEOUT); //Check if the app is launched from the widget
UiObject2 toolBar = device.findObject(By.res(APP_PACKAGE_NAME, "toolBar"));
assertEquals("My Weather", toolBar.getText()); }
}
Clap if that helped you, Your response helps me keep motivated for sharing new stories.