下面列出了android.database.CharArrayBuffer#androidx.test.filters.MediumTest 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Suppress // not supporting localized collators
@MediumTest
@Test
public void testLocaleenUS() {
insertStrings();
Log.i("LocaleTest", "about to call setLocale en_US");
mDatabase.setLocale(new Locale("en", "US"));
String[] results;
results = query("SELECT data FROM test ORDER BY data COLLATE LOCALIZED ASC");
// The database code currently uses PRIMARY collation strength,
// meaning that all versions of a character compare equal (regardless
// of case or accents), leaving the "cote" flavors in database order.
assertEquals(results, new String[] {
STRINGS[4], // "boy"
STRINGS[0], // sundry forms of "cote"
STRINGS[1],
STRINGS[2],
STRINGS[3],
STRINGS[6], // "COTE"
STRINGS[5], // "dog"
});
}
@MediumTest
@Test
public void testCustomFunction() {
mDatabase.addCustomFunction("roundFunction", 1, new SQLiteDatabase.CustomFunction() {
@Override
public String callback(String[] args) {
String input = args[0];
double value = Double.parseDouble(input);
return String.valueOf(Math.round(value));
}
});
Cursor cursor = mDatabase.rawQuery("SELECT roundFunction(3.14)", null);
assertTrue(cursor.moveToFirst());
int result = cursor.getInt(0);
assertSame(3, result);
}
@MediumTest
@Test
public void testSupportUpdate() {
populateDefaultTable();
ContentValues values = new ContentValues(1);
values.put("data", "this is an updated test");
assertEquals(1, mDatabase.update("test", SQLiteDatabase.CONFLICT_NONE, values,
"_id=?", new Object[] { 1 }));
Cursor c = mDatabase.query("test", null, "_id=1", null, null, null, null);
assertNotNull(c);
assertEquals(1, c.getCount());
c.moveToFirst();
String value = c.getString(c.getColumnIndexOrThrow("data"));
assertEquals("this is an updated test", value);
}
@MediumTest
@Test
public void testSchemaChange1() throws Exception {
SQLiteDatabase db1 = mDatabase;
Cursor cursor;
db1.execSQL("CREATE TABLE db1 (_id INTEGER PRIMARY KEY, data TEXT);");
cursor = db1.query("db1", null, null, null, null, null, null);
assertNotNull("Cursor is null", cursor);
db1.execSQL("CREATE TABLE db2 (_id INTEGER PRIMARY KEY, data TEXT);");
assertEquals(0, cursor.getCount());
cursor.close();
}
@MediumTest
@Test
public void testSchemaChange3() {
mDatabase.execSQL("CREATE TABLE db1 (_id INTEGER PRIMARY KEY, data TEXT);");
mDatabase.execSQL("INSERT INTO db1 (data) VALUES ('test');");
mDatabase.execSQL("ALTER TABLE db1 ADD COLUMN blah int;");
Cursor c = null;
try {
c = mDatabase.rawQuery("select blah from db1", null);
} catch (SQLiteException e) {
fail("unexpected exception: " + e.getMessage());
} finally {
if (c != null) {
c.close();
}
}
}
@MediumTest
@Test
public void testSelectionArgs() {
mDatabase.execSQL("CREATE TABLE test (_id INTEGER PRIMARY KEY, data TEXT);");
ContentValues values = new ContentValues(1);
values.put("data", "don't forget to handled 's");
mDatabase.insert("test", "data", values);
values.clear();
values.put("data", "no apostrophes here");
mDatabase.insert("test", "data", values);
Cursor c = mDatabase.query(
"test", null, "data GLOB ?", new String[]{"*'*"}, null, null, null);
assertEquals(1, c.getCount());
assertTrue(c.moveToFirst());
assertEquals("don't forget to handled 's", c.getString(1));
c.close();
}
@MediumTest
@Test
public void testContentValues() {
ContentValues values = new ContentValues();
values.put("string", "value");
assertEquals("value", values.getAsString("string"));
byte[] bytes = new byte[42];
Arrays.fill(bytes, (byte) 0x28);
values.put("byteArray", bytes);
assertTrue(Arrays.equals(bytes, values.getAsByteArray("byteArray")));
// Write the ContentValues to a Parcel and then read them out
Parcel p = Parcel.obtain();
values.writeToParcel(p, 0);
p.setDataPosition(0);
values = ContentValues.CREATOR.createFromParcel(p);
// Read the values out again and make sure they're the same
assertTrue(Arrays.equals(bytes, values.getAsByteArray("byteArray")));
assertEquals("value", values.get("string"));
}
@MediumTest
@Test
public void testSemicolonsInStatements() {
mDatabase.execSQL("CREATE TABLE pragma_test (" +
"i INTEGER DEFAULT 1234, " +
"j INTEGER, " +
"s TEXT DEFAULT 'hello', " +
"t TEXT, " +
"'select' TEXT DEFAULT \"hello\")");
try {
// ending the sql statement with semicolons shouldn't be a problem.
Cursor cur = mDatabase.rawQuery("PRAGMA database_list;", null);
cur.close();
// two semicolons in the statement shouldn't be a problem.
cur = mDatabase.rawQuery("PRAGMA database_list;;", null);
cur.close();
} catch (Throwable t) {
fail("unexpected, of course");
}
}
@MediumTest
@Test
public void testRequeryWithSelection() {
populateDefaultTable();
Cursor c = mDatabase.rawQuery("SELECT data FROM test WHERE data = '" + sString1 + "'",
null);
assertNotNull(c);
assertEquals(1, c.getCount());
assertTrue(c.moveToFirst());
assertEquals(sString1, c.getString(0));
c.deactivate();
c.requery();
assertEquals(1, c.getCount());
assertTrue(c.moveToFirst());
assertEquals(sString1, c.getString(0));
c.close();
}
@MediumTest
@Test
public void testRequeryWithSelectionArgs() {
populateDefaultTable();
Cursor c = mDatabase.rawQuery("SELECT data FROM test WHERE data = ?",
new String[]{sString1});
assertNotNull(c);
assertEquals(1, c.getCount());
assertTrue(c.moveToFirst());
assertEquals(sString1, c.getString(0));
c.deactivate();
c.requery();
assertEquals(1, c.getCount());
assertTrue(c.moveToFirst());
assertEquals(sString1, c.getString(0));
c.close();
}
@MediumTest
@Test
public void testStatementLongBinding() {
mDatabase.execSQL("CREATE TABLE test (num INTEGER);");
SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)");
for (int i = 0; i < 10; i++) {
statement.bindLong(1, i);
statement.execute();
}
statement.close();
Cursor c = mDatabase.query("test", null, null, null, null, null, null);
int numCol = c.getColumnIndexOrThrow("num");
c.moveToFirst();
for (long i = 0; i < 10; i++) {
long num = c.getLong(numCol);
assertEquals(i, num);
c.moveToNext();
}
c.close();
}
@MediumTest
@Test
public void testStatementStringBinding() {
mDatabase.execSQL("CREATE TABLE test (num TEXT);");
SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)");
for (long i = 0; i < 10; i++) {
statement.bindString(1, Long.toHexString(i));
statement.execute();
}
statement.close();
Cursor c = mDatabase.query("test", null, null, null, null, null, null);
int numCol = c.getColumnIndexOrThrow("num");
c.moveToFirst();
for (long i = 0; i < 10; i++) {
String num = c.getString(numCol);
assertEquals(Long.toHexString(i), num);
c.moveToNext();
}
c.close();
}
@MediumTest
@Test
public void testStatementClearBindings() {
mDatabase.execSQL("CREATE TABLE test (num INTEGER);");
SQLiteStatement statement = mDatabase.compileStatement("INSERT INTO test (num) VALUES (?)");
for (long i = 0; i < 10; i++) {
statement.bindLong(1, i);
statement.clearBindings();
statement.execute();
}
statement.close();
Cursor c = mDatabase.query("test", null, null, null, null, null, "ROWID");
int numCol = c.getColumnIndexOrThrow("num");
assertTrue(c.moveToFirst());
for (long i = 0; i < 10; i++) {
assertTrue(c.isNull(numCol));
c.moveToNext();
}
c.close();
}
@MediumTest
@Test
public void testSimpleStringBinding() {
mDatabase.execSQL("CREATE TABLE test (num TEXT, value TEXT);");
String statement = "INSERT INTO test (num, value) VALUES (?,?)";
String[] args = new String[2];
for (int i = 0; i < 2; i++) {
args[i] = Integer.toHexString(i);
}
mDatabase.execSQL(statement, args);
Cursor c = mDatabase.query("test", null, null, null, null, null, null);
int numCol = c.getColumnIndexOrThrow("num");
int valCol = c.getColumnIndexOrThrow("value");
c.moveToFirst();
String num = c.getString(numCol);
assertEquals(Integer.toHexString(0), num);
String val = c.getString(valCol);
assertEquals(Integer.toHexString(1), val);
c.close();
}
@Test
@MediumTest
public void testSwipeDownToHide() {
Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
.perform(
DesignViewActions.withCustomConstraints(
ViewActions.swipeDown(), ViewMatchers.isDisplayingAtLeast(5)));
registerIdlingResourceCallback();
try {
Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
.check(ViewAssertions.matches(not(ViewMatchers.isDisplayed())));
assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_HIDDEN));
} finally {
unregisterIdlingResourceCallback();
}
}
@Test
@MediumTest
public void testSwipeUpToExpand() {
Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
.perform(
DesignViewActions.withCustomConstraints(
new GeneralSwipeAction(
Swipe.FAST,
GeneralLocation.VISIBLE_CENTER,
view -> new float[] {view.getWidth() / 2, 0},
Press.FINGER),
ViewMatchers.isDisplayingAtLeast(5)));
registerIdlingResourceCallback();
try {
Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_EXPANDED));
} finally {
unregisterIdlingResourceCallback();
}
}
@Test
@MediumTest
public void testNoSwipeUpToExpand() {
getBehavior().setDraggable(false);
Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
.perform(
DesignViewActions.withCustomConstraints(
new GeneralSwipeAction(
Swipe.FAST,
GeneralLocation.VISIBLE_CENTER,
view -> new float[] {view.getWidth() / 2, 0},
Press.FINGER),
ViewMatchers.isDisplayingAtLeast(5)));
Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_COLLAPSED));
}
@Test
@MediumTest
public void testNoDragging() {
getBehavior().setDraggable(false);
Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
// Drag (and not release)
.perform(
new DragAction(
GeneralLocation.VISIBLE_CENTER, GeneralLocation.TOP_CENTER, Press.FINGER))
// Check that the bottom sheet is NOT in STATE_DRAGGING
.check(
(view, e) -> {
assertThat(view, is(ViewMatchers.isDisplayed()));
BottomSheetBehavior<?> behavior = BottomSheetBehavior.from(view);
assertThat(behavior.getState(), not(is(BottomSheetBehavior.STATE_DRAGGING)));
});
}
@Test
@MediumTest
public void testHalfExpandedToExpanded() throws Throwable {
getBehavior().setFitToContents(false);
checkSetState(BottomSheetBehavior.STATE_HALF_EXPANDED, ViewMatchers.isDisplayed());
Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
.perform(
DesignViewActions.withCustomConstraints(
new GeneralSwipeAction(
Swipe.FAST,
GeneralLocation.VISIBLE_CENTER,
view -> new float[] {view.getWidth() / 2, 0},
Press.FINGER),
ViewMatchers.isDisplayingAtLeast(5)));
registerIdlingResourceCallback();
try {
Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_EXPANDED));
} finally {
unregisterIdlingResourceCallback();
}
}
@Test
@MediumTest
public void testCollapsedToExpanded() throws Throwable {
getBehavior().setFitToContents(false);
checkSetState(BottomSheetBehavior.STATE_COLLAPSED, ViewMatchers.isDisplayed());
Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
.perform(
DesignViewActions.withCustomConstraints(
new GeneralSwipeAction(
Swipe.FAST,
GeneralLocation.VISIBLE_CENTER,
view -> new float[] {view.getWidth() / 2, 0},
Press.FINGER),
ViewMatchers.isDisplayingAtLeast(5)));
registerIdlingResourceCallback();
try {
Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_EXPANDED));
} finally {
unregisterIdlingResourceCallback();
}
}
@Test
@MediumTest
public void testInvisible() throws Throwable {
// Make the bottomsheet invisible
activityTestRule.runOnUiThread(
() -> {
getBottomSheet().setVisibility(View.INVISIBLE);
assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_COLLAPSED));
});
// Swipe up as if to expand it
Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
.perform(
DesignViewActions.withCustomConstraints(
new GeneralSwipeAction(
Swipe.FAST,
GeneralLocation.VISIBLE_CENTER,
view -> new float[] {view.getWidth() / 2, 0},
Press.FINGER),
not(ViewMatchers.isDisplayed())));
// Check that the bottom sheet stays the same collapsed state
activityTestRule.runOnUiThread(
() -> assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_COLLAPSED)));
}
@Test
@MediumTest
public void testLayoutWhileDragging() {
Espresso.onView(ViewMatchers.withId(R.id.bottom_sheet))
// Drag (and not release)
.perform(
new DragAction(
GeneralLocation.VISIBLE_CENTER, GeneralLocation.TOP_CENTER, Press.FINGER))
// Check that the bottom sheet is in STATE_DRAGGING
.check(
(view, e) -> {
assertThat(view, is(ViewMatchers.isDisplayed()));
BottomSheetBehavior<?> behavior = BottomSheetBehavior.from(view);
assertThat(behavior.getState(), is(BottomSheetBehavior.STATE_DRAGGING));
})
// Add a new view
.perform(new AddViewAction(R.layout.frame_layout))
// Check that the newly added view is properly laid out
.check(
(view, e) -> {
ViewGroup parent = (ViewGroup) view;
assertThat(parent.getChildCount(), is(1));
View child = parent.getChildAt(0);
assertThat(ViewCompat.isLaidOut(child), is(true));
});
}
@Test
@MediumTest
public void testAutoPeekHeight() throws Throwable {
activityTestRule.runOnUiThread(
() -> getBehavior().setPeekHeight(BottomSheetBehavior.PEEK_HEIGHT_AUTO));
activityTestRule.runOnUiThread(
() -> {
CoordinatorLayout col = getCoordinatorLayout();
assertThat(
getBottomSheet().getTop(),
is(
Math.min(
col.getWidth() * 9 / 16,
col.getHeight() - getBehavior().getPeekHeightMin())));
});
}
@Test
@MediumTest
public void testHideThenShow() throws Throwable {
// Hide the bottom sheet and wait for the dialog to be canceled.
final DialogInterface.OnCancelListener onCancelListener =
mock(DialogInterface.OnCancelListener.class);
showDialog();
dialog.setOnCancelListener(onCancelListener);
onView(ViewMatchers.withId(R.id.design_bottom_sheet))
.perform(setState(BottomSheetBehavior.STATE_HIDDEN));
verify(onCancelListener, timeout(3000)).onCancel(any(DialogInterface.class));
// Reshow the same dialog instance and wait for the bottom sheet to be collapsed.
final BottomSheetBehavior.BottomSheetCallback callback =
mock(BottomSheetBehavior.BottomSheetCallback.class);
BottomSheetBehavior.from(dialog.findViewById(R.id.design_bottom_sheet))
.addBottomSheetCallback(callback);
// Show the same dialog again.
activityTestRule.runOnUiThread(() -> dialog.show());
verify(callback, timeout(3000))
.onStateChanged(any(View.class), eq(BottomSheetBehavior.STATE_SETTLING));
verify(callback, timeout(3000))
.onStateChanged(any(View.class), eq(BottomSheetBehavior.STATE_COLLAPSED));
}
@Test
public void isAnyTestSize_ContainsPlatformAndRunnerFilterAnnotations() {
assertThat(TestSize.isAnyTestSize(SmallTest.class), equalTo(true));
assertThat(TestSize.isAnyTestSize(MediumTest.class), equalTo(true));
assertThat(TestSize.isAnyTestSize(LargeTest.class), equalTo(true));
assertThat(
TestSize.isAnyTestSize(android.test.suitebuilder.annotation.SmallTest.class),
equalTo(true));
assertThat(
TestSize.isAnyTestSize(android.test.suitebuilder.annotation.MediumTest.class),
equalTo(true));
assertThat(
TestSize.isAnyTestSize(android.test.suitebuilder.annotation.LargeTest.class),
equalTo(true));
}
@Test
@MediumTest
public void testDismissViaAnotherSnackbar() throws Throwable {
final CustomSnackbar anotherSnackbar =
makeCustomSnackbar()
.setTitle("Different title")
.setSubtitle("Different subtitle")
.setDuration(Snackbar.LENGTH_SHORT);
// Our dismiss action is to show another snackbar (and verify that the original snackbar
// is now dismissed with CONSECUTIVE event)
verifyDismissCallback(
onView(isAssignableFrom(Snackbar.SnackbarLayout.class)),
null,
snackbar -> anotherSnackbar.show(),
Snackbar.LENGTH_INDEFINITE,
Snackbar.Callback.DISMISS_EVENT_CONSECUTIVE);
// And dismiss the second snackbar to get back to clean state
SnackbarUtils.dismissTransientBottomBarAndWaitUntilFullyDismissed(anotherSnackbar);
}
@Test
@MediumTest
public void testMultipleCallbacks() throws Throwable {
final CustomSnackbar snackbar =
makeCustomSnackbar()
.setTitle(TITLE_TEXT)
.setSubtitle(SUBTITLE_TEXT)
.setDuration(Snackbar.LENGTH_INDEFINITE);
final BaseTransientBottomBar.BaseCallback mockCallback1 =
mock(BaseTransientBottomBar.BaseCallback.class);
final BaseTransientBottomBar.BaseCallback mockCallback2 =
mock(BaseTransientBottomBar.BaseCallback.class);
snackbar.addCallback(mockCallback1);
snackbar.addCallback(mockCallback2);
SnackbarUtils.showTransientBottomBarAndWaitUntilFullyShown(snackbar);
verify(mockCallback1, times(1)).onShown(snackbar);
verify(mockCallback2, times(1)).onShown(snackbar);
SnackbarUtils.dismissTransientBottomBarAndWaitUntilFullyDismissed(snackbar);
verify(mockCallback1, times(1))
.onDismissed(snackbar, BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_MANUAL);
verify(mockCallback2, times(1))
.onDismissed(snackbar, BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_MANUAL);
}
@Test
@MediumTest
public void testRotationInExpandedState() {
ExpandableTransformationActivity activity = activityTestRule.getActivity();
int oldOrientation = TestUtils.getScreenOrientation(activity);
onView(withId(R.id.fab)).perform(setExpanded(true));
TestUtils.switchScreenOrientation(activity);
onView(isRoot()).perform(waitUntilIdle());
onView(withId(R.id.fab)).check(matches(not(isDisplayed())));
onView(withId(R.id.sheet)).check(matches(isDisplayed()));
onView(withId(R.id.scrim)).check(matches(isDisplayed()));
TestUtils.resetScreenOrientation(activity, oldOrientation);
}
/** Test provided multiple annotations to exclude. */
@Test
public void testTestSizeFilter_multipleNotAnnotation() {
Request request =
builder
.addAnnotationExclusionFilter(SmallTest.class.getName())
.addAnnotationExclusionFilter(MediumTest.class.getName())
.addTestClass(SampleMultipleAnnotation.class.getName())
.build();
JUnitCore testRunner = new JUnitCore();
Result result = testRunner.run(request);
// expect 1 test that failed
Assert.assertEquals(1, result.getRunCount());
Assert.assertEquals(
"testRunThis", result.getFailures().get(0).getDescription().getMethodName());
}
@MediumTest
@Test
public void testLocaleInsertOrder() {
insertStrings();
String[] results = query("SELECT data FROM test");
assertEquals(STRINGS, results);
}