我基本上是从一个 Activity 启动一个前台媒体服务,然后将该 Activity 移动到
Lifecycle.State.DESTROYED
:
@RunWith(AndroidJUnit4.class)
public class MainActivityStateTest extends BaseActivityTestCase {
protected ActivityScenario<MainActivity> mScenario;
@Before
public void setupTest() {
this.setScenario(this.activityScenarioRule.getScenario());
this.getScenario().onActivity(activity -> { /* ... */ });
}
@Test
public void testActivityLifecycle() {
/* Destroy the Activity. */
this.getScenario().moveToState(Lifecycle.State.DESTROYED);
/* TODO: Use UiAutomator to click media-controls. */
}
@NonNull
protected ActivityScenario<MainActivity> getScenario() {
return this.mScenario;
}
protected void setScenario(@NonNull ActivityScenario<MainActivity> scenario) {
this.mScenario = scenario;
}
@Override
public void tearDown() throws Exception {
this.getScenario().close();
super.tearDown();
}
}
如何使用 UiAutomator 访问媒体通知控件?
我编写了一些方法,以便获取媒体服务通知的句柄:
@RunWith(AndroidJUnit4.class)
public class MainActivityStateTest extends BaseActivityTestCase {
protected UiObject openNotificationPanel() {
synchronized (this.mDevice) {
if (this.mDevice.openNotification()) {
this.mDevice.wait(Until.hasObject(By.pkg("com.android.systemui")), 10000);
UiObject notificationStackScroller = this.mDevice.findObject(
new UiSelector()
.packageName("com.android.systemui")
.className("android.widget.ScrollView")
.resourceId("com.android.systemui:id/notification_stack_scroller")
);
assertTrue(notificationStackScroller.exists());
return notificationStackScroller;
} else {
return null;
}
}
}
protected UiObject getMediaServiceNotification(@NonNull UiObject scrollView) {
try {
UiObject item = scrollView
.getChild(new UiSelector().resourceId("com.android.systemui:id/expanded"))
.getChild(new UiSelector().resourceId("android:id/notification_media_content"));
assertTrue(item.exists());
return item;
} catch (UiObjectNotFoundException e) {
Log.e(LOG_TAG,"UiObjectNotFoundException: " + e.getMessage());
return null;
}
}
protected void clickMediaActionAtIndex(@NonNull UiObject notification, int index) {
try {
UiObject mediaActions = notification.getChild(new UiSelector().resourceId("android:id/media_actions"));
assertTrue(mediaActions.exists());
UiObject mainColumn = notification.getChild(new UiSelector().resourceId("android:id/notification_main_column"));
assertTrue(mainColumn.exists());
UiObject mediaProgress = notification.getChild(new UiSelector().resourceId("android:id/notification_media_progress"));
assertTrue(mediaProgress.exists());
if (index < mediaActions.getChildCount()) {
UiObject mediaAction = mediaActions.getChild(new UiSelector().className("android.widget.ImageButton").index(index));
assertTrue(mediaAction.exists());
mediaAction.click();
}
} catch (UiObjectNotFoundException e) {
Log.e(LOG_TAG,"UiObjectNotFoundException: " + e.getMessage());
}
}
}
测试用例看起来像这样:
@Test
public void testActivityLifecycle() {
/* Destroy the Activity. */
this.getScenario().moveToState(Lifecycle.State.DESTROYED);
/* Click Media-Notification Action at index 2. */
UiObject notificationStackScroller = this.openNotificationPanel();
UiObject mediaNotification = this.getMediaServiceNotification(notificationStackScroller);
this.clickMediaActionAtIndex(mediaNotification, 2); /* eg. 0: rewind, 2: play/pause */
}