下面列出了怎么用android.support.test.filters.SdkSuppress的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
@SdkSuppress(minSdkVersion=19)
public void loadVersionOffline() throws Exception {
CarretoGooglePlayVersionProvider provider = new CarretoGooglePlayVersionProvider("com.vocabularyminer.android");
TestButler.setGsmState(false);
TestButler.setWifiState(false);
try {
VersionResult result = provider.getVersionResult();
assertTrue(result.isError());
assertNotNull(result);
assertNull(result.getVersion());
assertNotNull(result.getErrorMessage());
assertNotNull(result.getErrorException());
} finally {
TestButler.setGsmState(true);
TestButler.setWifiState(true);
Thread.sleep(1000);
}
}
@Test
@SdkSuppress(minSdkVersion=19)
public void loadVersionOffline() throws Exception {
JsonHttpVersionProvider provider = new JsonHttpVersionProvider("https://raw.githubusercontent.com/skoumalcz/force-update/master/force-update/src/androidTest/java/net/skoumal/forceupdate/provider/JsonHttpVersionProviderTest.DefaultAttributes.json");
TestButler.setGsmState(false);
TestButler.setWifiState(false);
try {
VersionResult result = provider.getVersionResult();
assertTrue(result.isError());
assertNotNull(result);
assertNull(result.getVersion());
assertNull(result.getPayload());
assertNotNull(result.getErrorMessage());
assertNotNull(result.getErrorException());
} finally {
TestButler.setGsmState(true);
TestButler.setWifiState(true);
Thread.sleep(1000);
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.HONEYCOMB)
@Test public void executeUpdateDeleteAndTrigger() {
SupportSQLiteStatement statement = real.compileStatement(
"UPDATE " + TABLE_EMPLOYEE + " SET " + NAME + " = 'Zach'");
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(o);
o.assertCursor()
.hasRow("alice", "Alice Allison")
.hasRow("bob", "Bob Bobberson")
.hasRow("eve", "Eve Evenson")
.isExhausted();
db.executeUpdateDelete(TABLE_EMPLOYEE, statement);
o.assertCursor()
.hasRow("alice", "Zach")
.hasRow("bob", "Zach")
.hasRow("eve", "Zach")
.isExhausted();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.HONEYCOMB)
@Test public void executeUpdateDeleteAndDontTrigger() {
SupportSQLiteStatement statement = real.compileStatement(""
+ "UPDATE " + TABLE_EMPLOYEE
+ " SET " + NAME + " = 'Zach'"
+ " WHERE " + NAME + " = 'Rob'");
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(o);
o.assertCursor()
.hasRow("alice", "Alice Allison")
.hasRow("bob", "Bob Bobberson")
.hasRow("eve", "Eve Evenson")
.isExhausted();
db.executeUpdateDelete(TABLE_EMPLOYEE, statement);
o.assertNoMoreEvents();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.HONEYCOMB)
@Test public void executeUpdateDeleteAndTriggerWithNoTables() {
SupportSQLiteStatement statement = real.compileStatement(
"UPDATE " + TABLE_EMPLOYEE + " SET " + NAME + " = 'Zach'");
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(o);
o.assertCursor()
.hasRow("alice", "Alice Allison")
.hasRow("bob", "Bob Bobberson")
.hasRow("eve", "Eve Evenson")
.isExhausted();
db.executeUpdateDelete(Collections.<String>emptySet(), statement);
o.assertNoMoreEvents();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.HONEYCOMB)
@Test public void executeUpdateDeleteThrowsAndDoesNotTrigger() {
SupportSQLiteStatement statement = real.compileStatement(
"UPDATE " + TABLE_EMPLOYEE + " SET " + USERNAME + " = 'alice'");
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES)
.skip(1) // Skip initial
.subscribe(o);
try {
db.executeUpdateDelete(TABLE_EMPLOYEE, statement);
fail();
} catch (SQLException ignored) {
}
o.assertNoMoreEvents();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.HONEYCOMB)
@Test public void executeUpdateDeleteWithArgsAndTrigger() {
SupportSQLiteStatement statement = real.compileStatement(
"UPDATE " + TABLE_EMPLOYEE + " SET " + NAME + " = ?");
statement.bindString(1, "Zach");
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(o);
o.assertCursor()
.hasRow("alice", "Alice Allison")
.hasRow("bob", "Bob Bobberson")
.hasRow("eve", "Eve Evenson")
.isExhausted();
db.executeUpdateDelete(TABLE_EMPLOYEE, statement);
o.assertCursor()
.hasRow("alice", "Zach")
.hasRow("bob", "Zach")
.hasRow("eve", "Zach")
.isExhausted();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.HONEYCOMB)
@Test public void executeUpdateDeleteWithArgsThrowsAndDoesNotTrigger() {
SupportSQLiteStatement statement = real.compileStatement(
"UPDATE " + TABLE_EMPLOYEE + " SET " + USERNAME + " = ?");
statement.bindString(1, "alice");
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES)
.skip(1) // Skip initial
.subscribe(o);
try {
db.executeUpdateDelete(TABLE_EMPLOYEE, statement);
fail();
} catch (SQLException ignored) {
}
o.assertNoMoreEvents();
}
@Repeat(TIMES)
//@Test
@SdkSuppress(minSdkVersion = 23)
public void testFlingFPS() {
UiObject2 uiObject2 = loadPageForFPS();
if (uiObject2 != null) {
uiObject2.fling(Direction.DOWN, FLING_SPEED);
uiObject2.fling(Direction.DOWN, FLING_SPEED);
uiObject2.fling(Direction.DOWN, FLING_SPEED);
uiObject2.fling(Direction.DOWN, FLING_SPEED);
processGfxInfo(flingFrameSeconds);
}
}
@Repeat(TIMES)
//@Test
@SdkSuppress(minSdkVersion = 23)
public void testScrollFPS() {
UiObject2 uiObject2 = loadPageForFPS();
if (uiObject2 != null) {
uiObject2.scroll(Direction.DOWN, 6, SCROLL_SPEED);
processGfxInfo(scrollFrameSeconds);
}
}
@Test
@SdkSuppress(maxSdkVersion = Build.VERSION_CODES.M)
public void liveData_partiallyObscuredActivity_maxSdkM() throws Throwable {
CollectingSupportActivity activity = mActivityTestRule.getActivity();
liveData_partiallyObscuredLifecycleOwner_maxSdkM(activity);
}
@Test
@SdkSuppress(maxSdkVersion = Build.VERSION_CODES.M)
public void liveData_partiallyObscuredActivityWithFragment_maxSdkM() throws Throwable {
CollectingSupportActivity activity = mActivityTestRule.getActivity();
CollectingSupportFragment fragment = new CollectingSupportFragment();
mActivityTestRule.runOnUiThread(() -> activity.replaceFragment(fragment));
liveData_partiallyObscuredLifecycleOwner_maxSdkM(fragment);
}
@Test
@SdkSuppress(maxSdkVersion = Build.VERSION_CODES.M)
public void liveData_partiallyObscuredActivityFragmentInFragment_maxSdkM() throws Throwable {
CollectingSupportActivity activity = mActivityTestRule.getActivity();
CollectingSupportFragment fragment = new CollectingSupportFragment();
CollectingSupportFragment fragment2 = new CollectingSupportFragment();
mActivityTestRule.runOnUiThread(() -> {
activity.replaceFragment(fragment);
fragment.replaceFragment(fragment2);
});
liveData_partiallyObscuredLifecycleOwner_maxSdkM(fragment2);
}
@Test
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
public void liveData_partiallyObscuredActivity_minSdkN() throws Throwable {
CollectingSupportActivity activity = mActivityTestRule.getActivity();
liveData_partiallyObscuredLifecycleOwner_minSdkN(activity);
}
@Test
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
public void liveData_partiallyObscuredActivityWithFragment_minSdkN() throws Throwable {
CollectingSupportActivity activity = mActivityTestRule.getActivity();
CollectingSupportFragment fragment = new CollectingSupportFragment();
mActivityTestRule.runOnUiThread(() -> activity.replaceFragment(fragment));
liveData_partiallyObscuredLifecycleOwner_minSdkN(fragment);
}
@Test
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
public void liveData_partiallyObscuredActivityFragmentInFragment_minSdkN() throws Throwable {
CollectingSupportActivity activity = mActivityTestRule.getActivity();
CollectingSupportFragment fragment = new CollectingSupportFragment();
CollectingSupportFragment fragment2 = new CollectingSupportFragment();
mActivityTestRule.runOnUiThread(() -> {
activity.replaceFragment(fragment);
fragment.replaceFragment(fragment2);
});
liveData_partiallyObscuredLifecycleOwner_minSdkN(fragment2);
}
@SdkSuppress(maxSdkVersion = 27)
@Test
public void testOnStartCall() throws Throwable {
testSynchronousCall(Lifecycle.Event.ON_START,
activity -> getInstrumentation().callActivityOnCreate(activity, null),
SynchronousActivityLifecycleTest::performStart);
}
@SdkSuppress(maxSdkVersion = 27)
@Test
public void testOnResumeCall() throws Throwable {
testSynchronousCall(Lifecycle.Event.ON_RESUME,
activity -> {
getInstrumentation().callActivityOnCreate(activity, null);
performStart(activity);
},
SynchronousActivityLifecycleTest::performResume);
}
@SdkSuppress(maxSdkVersion = 27)
@Test
public void testOnStopCall() throws Throwable {
testSynchronousCall(Lifecycle.Event.ON_STOP,
activity -> {
getInstrumentation().callActivityOnCreate(activity, null);
performStart(activity);
},
SynchronousActivityLifecycleTest::performStop);
}
@SdkSuppress(minSdkVersion = 16)
@Ignore
@Test
public void testValidGetWithInvalidHeader () throws Exception {
final ConditionVariable cv = new ConditionVariable();
MockWebViewApp webViewApp = new MockWebViewApp() {
@Override
public boolean sendEvent(Enum eventCategory, Enum eventId, Object... params) {
EVENT_CATEGORY = eventCategory;
EVENT_ID = eventId;
EVENT_PARAMS = params;
cv.open();
return true;
}
};
JSONArray headers = new JSONArray("[[\"C,o-n#n#e*c*t*io*n\", \"close\"]]");
WebViewApp.setCurrentApp(webViewApp);
Invocation invocation = new Invocation();
WebViewCallback callback = new WebViewCallback("Test_Valid_Get_With_Invalid_Header", invocation.getId());
Request.get("1", validUrl, headers, connectTimeout, readTimeout, callback);
invocation.sendInvocationCallback();
boolean success = cv.block(30000);
assertTrue("ConditionVariable was not opened", success);
assertEquals("Callback status was not OK, successfull callback was expected", CallbackStatus.OK, CALLBACK_STATUS);
assertNull("Callback error was not NULL, successfull callback was expected", CALLBACK_ERROR);
assertEquals("Callback params list length was expected to be 2 (callback id and request id in params)", 2, CALLBACK_PARAMS.length);
assertEquals("Callback first param was expected to be Callback ID", callback.getCallbackId(), CALLBACK_ID);
assertEquals("Request has changed for some reason", validUrl, EVENT_PARAMS[1]);
assertEquals("Expected three (3) params in event parameters", 3, EVENT_PARAMS.length);
assertEquals("Event Category was incorrect", WebViewEventCategory.REQUEST, EVENT_CATEGORY);
assertEquals("Event ID was incorrect", WebRequestEvent.FAILED, EVENT_ID);
}
@SdkSuppress(minSdkVersion = 16)
@Ignore
@Test
public void testValidPostWithInvalidHeader () throws Exception {
final ConditionVariable cv = new ConditionVariable();
MockWebViewApp webViewApp = new MockWebViewApp() {
@Override
public boolean sendEvent(Enum eventCategory, Enum eventId, Object... params) {
EVENT_CATEGORY = eventCategory;
EVENT_ID = eventId;
EVENT_PARAMS = params;
cv.open();
return true;
}
};
JSONArray headers = new JSONArray("[[\"C,o-n#n#e*c*t*io*n\", \"close\"]]");
WebViewApp.setCurrentApp(webViewApp);
Invocation invocation = new Invocation();
WebViewCallback callback = new WebViewCallback("Test_Valid_Post_With_Invalid_Header", invocation.getId());
Request.post("1", validUrl, null, headers, connectTimeout, readTimeout, callback);
invocation.sendInvocationCallback();
boolean success = cv.block(30000);
assertTrue("ConditionVariable was not opened", success);
assertEquals("Callback status was not OK, successfull callback was expected", CallbackStatus.OK, CALLBACK_STATUS);
assertNull("Callback error was not NULL, successfull callback was expected", CALLBACK_ERROR);
assertEquals("Callback params list length was expected to be 2 (callback id and request id in params)", 2, CALLBACK_PARAMS.length);
assertEquals("Callback first param was expected to be Callback ID", callback.getCallbackId(), CALLBACK_ID);
assertEquals("Request has changed for some reason", validUrl, EVENT_PARAMS[1]);
assertEquals("Expected three (3) params in event parameters", 3, EVENT_PARAMS.length);
assertEquals("Event Category was incorrect", WebViewEventCategory.REQUEST, EVENT_CATEGORY);
assertEquals("Event ID was incorrect", WebRequestEvent.FAILED, EVENT_ID);
}
@Test
@SdkSuppress(minSdkVersion = 15)
public void testMinSdkVersion() {
Log.d("Test Filters", "Checking for min sdk >= 15");
Activity activity = activityTestRule.getActivity();
assertNotNull("MainActivity is not available", activity);
}
@Test
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.LOLLIPOP)
public void testMinBuild() {
Log.d("Test Filters", "Checking for min build > Lollipop");
Activity activity = activityTestRule.getActivity();
assertNotNull("MainActivity is not available", activity);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.HONEYCOMB)
@Test public void executeUpdateDeleteAndTriggerWithMultipleTables() {
SupportSQLiteStatement statement = real.compileStatement(
"UPDATE " + TABLE_EMPLOYEE + " SET " + NAME + " = 'Zach'");
final RecordingObserver managerObserver = new RecordingObserver();
db.createQuery(TABLE_MANAGER, SELECT_MANAGER_LIST).subscribe(managerObserver);
managerObserver.assertCursor()
.hasRow("Eve Evenson", "Alice Allison")
.isExhausted();
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES).subscribe(o);
o.assertCursor()
.hasRow("alice", "Alice Allison")
.hasRow("bob", "Bob Bobberson")
.hasRow("eve", "Eve Evenson")
.isExhausted();
final Set<String> employeeAndManagerTables = Collections.unmodifiableSet(new HashSet<>(BOTH_TABLES));
db.executeUpdateDelete(employeeAndManagerTables, statement);
o.assertCursor()
.hasRow("alice", "Zach")
.hasRow("bob", "Zach")
.hasRow("eve", "Zach")
.isExhausted();
managerObserver.assertCursor()
.hasRow("Zach", "Zach")
.isExhausted();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.HONEYCOMB)
@Test public void nonExclusiveTransactionWorks() throws InterruptedException {
final CountDownLatch transactionStarted = new CountDownLatch(1);
final CountDownLatch transactionProceed = new CountDownLatch(1);
final CountDownLatch transactionCompleted = new CountDownLatch(1);
new Thread() {
@Override public void run() {
Transaction transaction = db.newNonExclusiveTransaction();
transactionStarted.countDown();
try {
db.insert(TABLE_EMPLOYEE, CONFLICT_NONE, employee("hans", "Hans Hanson"));
transactionProceed.await(10, SECONDS);
} catch (InterruptedException e) {
throw new RuntimeException("Exception in transaction thread", e);
}
transaction.markSuccessful();
transaction.close();
transactionCompleted.countDown();
}
}.start();
assertThat(transactionStarted.await(10, SECONDS)).isTrue();
//Simple query
Employee employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 1")
.lift(Query.mapToOne(Employee.MAPPER))
.blockingFirst();
assertThat(employees).isEqualTo(new Employee("alice", "Alice Allison"));
transactionProceed.countDown();
assertThat(transactionCompleted.await(10, SECONDS)).isTrue();
}
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
@Test public void mapToOptional() {
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 1")
.lift(Query.mapToOptional(MAPPER))
.test()
.assertValue(Optional.of(new Employee("alice", "Alice Allison")));
}
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
@Test public void mapToOptionalThrowsWhenMapperReturnsNull() {
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 1")
.lift(Query.mapToOptional(new Function<Cursor, Employee>() {
@Override public Employee apply(Cursor cursor) throws Exception {
return null;
}
}))
.test()
.assertError(NullPointerException.class)
.assertErrorMessage("QueryToOne mapper returned null");
}
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
@Test public void mapToOptionalThrowsOnMultipleRows() {
db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 2") //
.lift(Query.mapToOptional(MAPPER))
.test()
.assertError(IllegalStateException.class)
.assertErrorMessage("Cursor returned more than 1 row");
}
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.N)
@Test public void mapToOptionalIgnoresNullCursor() {
Query nully = new Query() {
@Nullable @Override public Cursor run() {
return null;
}
};
Observable.just(nully)
.lift(Query.mapToOptional(MAPPER))
.test()
.assertValue(Optional.<Employee>empty());
}
@Test
@RequiresDevice
@SdkSuppress(minSdkVersion = 17)
public void testInfoListener () throws Exception {
final Activity activity = waitForActivityStart(null);
WebViewApp.setCurrentApp(new MockWebViewApp() {
@Override
public boolean sendEvent(Enum eventCategory, Enum eventId, Object... params) {
super.sendEvent(eventCategory, eventId, params);
if ("ON_FOCUS_GAINED".equals(eventId.name()) || "ON_FOCUS_LOST".equals(eventId.name())) {
return true;
}
EVENT_CATEGORIES.add(eventCategory);
EVENTS.add(eventId);
EVENT_PARAMS = params;
EVENT_COUNT++;
if (eventCategory.equals(WebViewEventCategory.VIDEOPLAYER) && eventId.equals(VideoPlayerEvent.PREPARED)) {
VIDEOPLAYER_VIEW.play();
}
if (eventCategory.equals(WebViewEventCategory.VIDEOPLAYER) && eventId.equals(VideoPlayerEvent.INFO)) {
INFO_EVENTS = new ArrayList<>();
INFO_EVENTS.add((Integer) params[1]);
}
if (eventCategory.equals(WebViewEventCategory.VIDEOPLAYER) && eventId.equals(VideoPlayerEvent.COMPLETED)) {
CONDITION_VARIABLE.open();
}
return true;
}
});
final String validUrl = TestUtilities.getTestServerAddress() + "/blue_test_trailer.mp4";
final Handler handler = new Handler(Looper.getMainLooper());
final ConditionVariable viewAddCV = new ConditionVariable();
handler.post(new Runnable() {
@Override
public void run() {
((MockWebViewApp)WebViewApp.getCurrentApp()).VIDEOPLAYER_VIEW = new VideoPlayerView(getInstrumentation().getTargetContext());
activity.addContentView(((MockWebViewApp)WebViewApp.getCurrentApp()).VIDEOPLAYER_VIEW, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
viewAddCV.open();
}
});
boolean success = viewAddCV.block(3000);
assertTrue("ConditionVariable did not open in view add", success);
final MockWebViewApp mockWebViewApp = (MockWebViewApp)WebViewApp.getCurrentApp();
ConditionVariable cv = new ConditionVariable();
mockWebViewApp.CONDITION_VARIABLE = cv;
((MockWebViewApp)WebViewApp.getCurrentApp()).VIDEOPLAYER_VIEW.prepare(validUrl, 1f, 0);
success = cv.block(30000);
assertTrue("Condition Variable was not opened: COMPLETED or PREPARE ERROR event was not received", success);
assertEquals("Event category should be videoplayer category", WebViewEventCategory.VIDEOPLAYER, mockWebViewApp.EVENT_CATEGORIES.get(0));
assertEquals("Event ID should be completed", VideoPlayerEvent.COMPLETED, mockWebViewApp.EVENTS.get(mockWebViewApp.EVENTS.size() - 1));
assertEquals("The video url and the url received from the completed event should be the same", validUrl, mockWebViewApp.EVENT_PARAMS[0]);
assertNotNull("Info events should not be NULL", mockWebViewApp.INFO_EVENTS);
assertTrue("There should be at least one INFO event received", mockWebViewApp.INFO_EVENTS.size() > 0);
assertTrue("Didn't get activity finish", waitForActivityFinish(activity));
}