下面列出了怎么用android.test.MoreAsserts的API类实例代码及写法,或者点击链接到github查看源代码。
public void assertEquals(FakeTrackOutput expected) {
Assert.assertEquals(expected.format, format);
Assert.assertEquals(expected.sampleTimesUs.size(), sampleTimesUs.size());
MoreAsserts.assertEquals(expected.sampleData, sampleData);
for (int i = 0; i < sampleTimesUs.size(); i++) {
Assert.assertEquals(expected.sampleTimesUs.get(i), sampleTimesUs.get(i));
Assert.assertEquals(expected.sampleFlags.get(i), sampleFlags.get(i));
Assert.assertEquals(expected.sampleStartOffsets.get(i), sampleStartOffsets.get(i));
Assert.assertEquals(expected.sampleEndOffsets.get(i), sampleEndOffsets.get(i));
if (expected.sampleEncryptionKeys.get(i) == null) {
Assert.assertNull(sampleEncryptionKeys.get(i));
} else {
MoreAsserts.assertEquals(expected.sampleEncryptionKeys.get(i), sampleEncryptionKeys.get(i));
}
}
}
@MediumTest
public void testGetSpans() {
Spannable spannable = newSpannableWithText("abcdef");
Object emptySpan = new Object();
spannable.setSpan(emptySpan, 1, 1, 0);
Object unemptySpan = new Object();
spannable.setSpan(unemptySpan, 1, 2, 0);
Object[] spans;
// Empty spans are included when they merely abut the query region
// but other spans are not, unless the query region is empty, in
// in which case any abutting spans are returned.
spans = spannable.getSpans(0, 1, Object.class);
MoreAsserts.assertEquals(new Object[]{emptySpan}, spans);
spans = spannable.getSpans(0, 2, Object.class);
MoreAsserts.assertEquals(new Object[]{emptySpan, unemptySpan}, spans);
spans = spannable.getSpans(1, 2, Object.class);
MoreAsserts.assertEquals(new Object[]{emptySpan, unemptySpan}, spans);
spans = spannable.getSpans(2, 2, Object.class);
MoreAsserts.assertEquals(new Object[]{unemptySpan}, spans);
}
public void assertSample(int index, byte[] data, long timeUs, int flags, byte[] encryptionKey) {
byte[] actualData = getSampleData(index);
MoreAsserts.assertEquals(data, actualData);
Assert.assertEquals(timeUs, (long) sampleTimesUs.get(index));
Assert.assertEquals(flags, (int) sampleFlags.get(index));
byte[] sampleEncryptionKey = sampleEncryptionKeys.get(index);
if (encryptionKey == null) {
Assert.assertEquals(null, sampleEncryptionKey);
} else {
MoreAsserts.assertEquals(encryptionKey, sampleEncryptionKey);
}
}
private void stringSplitterTestHelper(String string, String[] expectedStrings) {
TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(',');
splitter.setString(string);
List<String> strings = Lists.newArrayList();
for (String s : splitter) {
strings.add(s);
}
MoreAsserts.assertEquals(expectedStrings, strings.toArray(new String[]{}));
}
public void testIntersectPayloads() {
List<RtcPayload> emptyIntersection = Utils.intersectPayloads(sDefaultVideoPayloads, Collections.<RtcPayload>emptyList());
MoreAsserts.assertEmpty(emptyIntersection);
}
public void testCorrectVersion() throws Exception {
PackageInfo info = application.getPackageManager().getPackageInfo(application.getPackageName(), 0);
assertNotNull(info);
MoreAsserts.assertMatchesRegex("\\d\\.\\d", info.versionName);
}
public void testCorrectVersion() throws Exception {
PackageInfo info = application.getPackageManager().getPackageInfo(application.getPackageName(), 0);
assertNotNull(info);
MoreAsserts.assertMatchesRegex("\\d\\.\\d", info.versionName);
}
public void testDateFormatter_formatRelativeDatetime() {
// We'll force the use of a date, otherwise tests will fail if run
// between midnight and 1am (because there'll be no time that's "greater
// than an hour" but also "within the same day".
Calendar calNow = new GregorianCalendar(TimeZone.getDefault());
calNow.set(2013, 10, 22, 23, 01, 02); // Friday 2013-11-22T23:01:02 (local TZ)
Date within1Minute = new Date(calNow.getTimeInMillis() - 30000);
Date within1Hour = new Date(calNow.getTimeInMillis() - 1800000);
Date withinSameDay = new Date(calNow.getTimeInMillis() - 9000000);
Date withinSameWeek = new Date(calNow.getTimeInMillis() - 259200000);
Date withinSameYear = new Date(calNow.getTimeInMillis() - 1036800000);
Date moreThanSameYear = new Date(calNow.getTimeInMillis() - 34560000000L);
// TODO: Make these locale-friendly? (Without just duplicating the function being tested...)
// ago = false
MoreAsserts.assertContainsRegex(
"^\\d{1,2} secs$",
Utils.DateFormatter.formatRelativeDatetime(getContext(), within1Minute, calNow.getTime(), false));
MoreAsserts.assertContainsRegex(
"^\\d{1,2} mins$",
Utils.DateFormatter.formatRelativeDatetime(getContext(), within1Hour, calNow.getTime(), false));
MoreAsserts.assertContainsRegex(
"^\\d{1,2}:\\d{2} (AM|PM)$",
Utils.DateFormatter.formatRelativeDatetime(getContext(), withinSameDay, calNow.getTime(), false));
MoreAsserts.assertContainsRegex(
"^\\w{3}, \\d{1,2}:\\d{2} (AM|PM)$",
Utils.DateFormatter.formatRelativeDatetime(getContext(), withinSameWeek, calNow.getTime(), false));
MoreAsserts.assertContainsRegex(
"^\\w{3} \\d{1,2}, \\d{1,2}:\\d{2} (AM|PM)$",
Utils.DateFormatter.formatRelativeDatetime(getContext(), withinSameYear, calNow.getTime(), false));
MoreAsserts.assertContainsRegex(
"^\\w{3} \\d{1,2} \\d{4}, \\d{1,2}:\\d{2} (AM|PM)$",
Utils.DateFormatter.formatRelativeDatetime(getContext(), moreThanSameYear, calNow.getTime(), false));
// ago = true
MoreAsserts.assertContainsRegex(
"^\\d{1,2} secs ago$",
Utils.DateFormatter.formatRelativeDatetime(getContext(), within1Minute, calNow.getTime(), true));
MoreAsserts.assertContainsRegex(
"^\\d{1,2} mins ago$",
Utils.DateFormatter.formatRelativeDatetime(getContext(), within1Hour, calNow.getTime(), true));
MoreAsserts.assertContainsRegex(
"^\\d{1,2}:\\d{2} (AM|PM)$",
Utils.DateFormatter.formatRelativeDatetime(getContext(), withinSameDay, calNow.getTime(), true));
MoreAsserts.assertContainsRegex(
"^\\w{3}, \\d{1,2}:\\d{2} (AM|PM)$",
Utils.DateFormatter.formatRelativeDatetime(getContext(), withinSameWeek, calNow.getTime(), true));
MoreAsserts.assertContainsRegex(
"^\\w{3} \\d{1,2}, \\d{1,2}:\\d{2} (AM|PM)$",
Utils.DateFormatter.formatRelativeDatetime(getContext(), withinSameYear, calNow.getTime(), true));
MoreAsserts.assertContainsRegex(
"^\\w{3} \\d{1,2} \\d{4}, \\d{1,2}:\\d{2} (AM|PM)$",
Utils.DateFormatter.formatRelativeDatetime(getContext(), moreThanSameYear, calNow.getTime(), true));
/*
Date start = new Date();
int i = 0;
for (; i < 100; i++) {
Utils.DateFormatter.formatRelativeDatetime(getContext(), within1Minute, calNow.getTime(), true);
Utils.DateFormatter.formatRelativeDatetime(getContext(), within1Hour, calNow.getTime(), true);
Utils.DateFormatter.formatRelativeDatetime(getContext(), withinSameDay, calNow.getTime(), true);
Utils.DateFormatter.formatRelativeDatetime(getContext(), withinSameWeek, calNow.getTime(), true);
Utils.DateFormatter.formatRelativeDatetime(getContext(), withinSameYear, calNow.getTime(), true);
Utils.DateFormatter.formatRelativeDatetime(getContext(), moreThanSameYear, calNow.getTime(), true);
}
Date end = new Date();
Log.i(this.getName(), String.format("%d iterations: %d ms", i, end.getTime() - start.getTime()));
*/
}