Refine search
/** * Asserts that the media source reported completed loads via {@link * MediaSourceEventListener#onLoadCompleted(int, MediaPeriodId, LoadEventInfo, MediaLoadData)} for * each specified window index and a null period id. Also asserts that no other loads with media * period id null are reported. */ public void assertCompletedManifestLoads(Integer... windowIndices) { List<Integer> expectedWindowIndices = new ArrayList<>(Arrays.asList(windowIndices)); for (Pair<Integer, MediaPeriodId> windowIndexAndMediaPeriodId : completedLoads) { if (windowIndexAndMediaPeriodId.second == null) { boolean loadExpected = expectedWindowIndices.remove(windowIndexAndMediaPeriodId.first); assertThat(loadExpected).isTrue(); } } assertWithMessage("Not all expected media source loads have been completed.") .that(expectedWindowIndices) .isEmpty(); }
/** Fails if the multimap is not empty. */ @CanIgnoreReturnValue public Ordered containsExactly() { return check().about(iterableEntries()).that(actual().entries()).containsExactly(); }
/** Returns a {@code StringSubject} to make assertions about the throwable's message. */ public final StringSubject hasMessageThat() { StandardSubjectBuilder check = check("getMessage()"); if (actual() instanceof ErrorWithFacts && ((ErrorWithFacts) actual()).facts().size() > 1) { check = check.withMessage( "(Note from Truth: When possible, instead of asserting on the full message, assert" + " about individual facts by using ExpectFailure.assertThat.)"); } return check.that(actual().getMessage()); }
/** * Returns a new {@code ThrowableSubject} that supports assertions on this throwable's direct * cause. This method can be invoked repeatedly (e.g. {@code * assertThat(e).hasCauseThat().hasCauseThat()....} to assert on a particular indirect cause. */ public final ThrowableSubject hasCauseThat() { // provides a more helpful error message if hasCauseThat() methods are chained too deep // e.g. assertThat(new Exception()).hCT().hCT().... // TODO(diamondm) in keeping with other subjects' behavior this should still NPE if the subject // *itself* is null, since there's no context to lose. See also b/37645583 if (actual() == null) { check("getCause()").fail("Causal chain is not deep enough - add a .isNotNull() check?"); return ignoreCheck() .that( new Throwable() { @Override public Throwable fillInStackTrace() { setStackTrace(new StackTraceElement[0]); // for old versions of Android return this; } }); } return check("getCause()").that(actual().getCause()); } }
public void test() { assertUsedAsRule(); isUsedWithoutCallingTest = false; tester.test(); assertThat(regressionTests).isNotEmpty(); int i = 1; for (Entry<Key, String> entry : regressionTests.entrySet()) { assert_() .withMessage( "Unexpected digest for regression test [" + i + "]: with key: " + entry.getKey()) .that(sha256.getStringDigest(entry.getKey())).isEqualTo(entry.getValue()); i++; } }
@Test public void testReadWriteLongerStringOddLength() { String val = "0123456789abcde"; parcel.writeString(val); parcel.setDataPosition(0); assertThat(parcel.readString()).isEqualTo(val); assertWithMessage("4B length + 15*2B data + 2B null char") .that(parcel.dataSize()) .isEqualTo(36); }
@Test public void testExceptionDuringEvictionByLeastRecentlyUsedCacheEvictorNotHang() throws Exception { CachedContentIndex index = Mockito.spy(new CachedContentIndex(cacheDir)); SimpleCache simpleCache = new SimpleCache(cacheDir, new LeastRecentlyUsedCacheEvictor(20), index); // Add some content. CacheSpan cacheSpan = simpleCache.startReadWrite(KEY_1, 0); addCache(simpleCache, KEY_1, 0, 15); // Make index.store() throw exception from now on. doAnswer( invocation -> { throw new CacheException("SimpleCacheTest"); }) .when(index) .store(); // Adding more content will make LeastRecentlyUsedCacheEvictor evict previous content. try { addCache(simpleCache, KEY_1, 15, 15); assertWithMessage("Exception was expected").fail(); } catch (CacheException e) { // do nothing. } simpleCache.releaseHoleSpan(cacheSpan); // Although store() has failed, it should remove the first span and add the new one. NavigableSet<CacheSpan> cachedSpans = simpleCache.getCachedSpans(KEY_1); assertThat(cachedSpans).isNotEmpty(); assertThat(cachedSpans).hasSize(1); assertThat(cachedSpans.pollFirst().position).isEqualTo(15); }
public void testMultipleQuantile() { ImmutableSet<Integer> indexes = ImmutableSet.of(50, 90, 99); Map<Integer, Double> referenceQuantiles = REFERENCE_ALGORITHM.multipleQuantiles(indexes, 100, dataset.clone()); assertThat(referenceQuantiles.keySet()).isEqualTo(indexes); for (QuantilesAlgorithm algorithm : NON_REFERENCE_ALGORITHMS) { Map<Integer, Double> quantiles = algorithm.multipleQuantiles(indexes, 100, dataset.clone()); assertWithMessage("Wrong keys from " + algorithm).that(quantiles.keySet()).isEqualTo(indexes); for (int i : indexes) { assertWithMessage("Mismatch between %s and %s at %s", algorithm, REFERENCE_ALGORITHM, i) .that(quantiles.get(i)) .isWithin(ALLOWED_ERROR) .of(referenceQuantiles.get(i)); } } } }
/** * Fails if the subject does not contain exactly the given elements. * * <p>Multiplicity is respected. For example, an object duplicated exactly 3 times in the * parameters asserts that the object must likewise be duplicated exactly 3 times in the subject. * * <p>To also test that the contents appear in the given order, make a call to {@code inOrder()} * on the object returned by this method. */ @CanIgnoreReturnValue public Ordered containsExactly(long... varargs) { return check().that(actualList).containsExactly(box(varargs)); }
private void searchSite(TorrentSite torrentSite) throws Exception { // Set test user and password if (torrentSite.getAdapter().getAuthType() != NONE) { String user = getResourceString(torrentSite.name() + "_user"); String pass = getResourceString(torrentSite.name() + "_pass"); String token = getResourceString(torrentSite.name() + "_token"); if (!(has(user, pass) || has(token))) assert_().fail(torrentSite.name() + " is private but no credentials found: untestable"); prefs.edit() .putString("pref_key_user_" + torrentSite.name(), user) .putString("pref_key_pass_" + torrentSite.name(), pass) .putString("pref_key_token_" + torrentSite.name(), token) .commit(); } List<SearchResult> results = torrentSite.search(prefs, QUERY, ORDER, RESULTS); assertThat(results).isNotEmpty(); for (SearchResult result : results) { assertThat(result.getTitle()).isNotEmpty(); assertThat(result.getTorrentUri()).isNotNull(); assertThat(result.getTorrentUri().toString()).isNotEmpty(); assertThat(result.getDetailsUrl()).isNotEmpty(); assertThat(result.getSize()).isNotEmpty(); assertThat(result.getAddedDate()).isNotNull(); assertThat(result.getAddedDate().getTime()).isGreaterThan(THE_YEAR_2000); assertThat(result.getSeeds()).isAtLeast(0); assertThat(result.getLeechers()).isAtLeast(0); } }
/** * Fails if the subject does not contain exactly the given elements. * * <p>Multiplicity is respected. For example, an object duplicated exactly 3 times in the * parameters asserts that the object must likewise be duplicated exactly 3 times in the subject. * * <p>To also test that the contents appear in the given order, make a call to {@code inOrder()} * on the object returned by this method. */ @CanIgnoreReturnValue public Ordered containsExactlyElementsIn(Iterable<?> expected) { return check().that(actualList).containsExactlyElementsIn(expected); }
/** * Fails if the subject does not contain all of the given elements. If an element appears more * than once in the given elements, then it must appear at least that number of times in the * actual elements. * * <p>To also test that the contents appear in the given order, make a call to {@code inOrder()} * on the object returned by this method. The expected elements must appear in the given order * within the actual elements, but they are not required to be consecutive. */ @CanIgnoreReturnValue public Ordered containsAllOf(long first, long second, long... rest) { return check().that(actualList).containsAllOf(first, second, box(rest)); }
/** * Fails if the subject does not contain all of the given elements. If an element appears more * than once in the given elements, then it must appear at least that number of times in the * actual elements. * * <p>To also test that the contents appear in the given order, make a call to {@code inOrder()} * on the object returned by this method. The expected elements must appear in the given order * within the actual elements, but they are not required to be consecutive. */ @CanIgnoreReturnValue public Ordered containsAllIn(Iterable<?> expected) { return check().that(actualList).containsAllIn(expected); }
public void testSuccessfulAsList_resultCancelledRacingInputDone() throws Exception { TestLogHandler listenerLoggerHandler = new TestLogHandler(); Logger exceptionLogger = Logger.getLogger(AbstractFuture.class.getName()); exceptionLogger.addHandler(listenerLoggerHandler); try { doTestSuccessfulAsList_resultCancelledRacingInputDone(); assertWithMessage("Nothing should be logged") .that(listenerLoggerHandler.getStoredLogRecords()) .isEmpty(); } finally { exceptionLogger.removeHandler(listenerLoggerHandler); } }
@CanIgnoreReturnValue @Override public T withWarningCount(int warningCount) { check().about(compilations()).that(compilation).hadWarningCount(warningCount); return thisObject(); }
public static void assertSkippedOutputBufferCount(String name, DecoderCounters counters, int expected) { counters.ensureUpdated(); int actual = counters.skippedOutputBufferCount; assertWithMessage( "Codec(" + name + ") skipped " + actual + " buffers. Expected " + expected + ".") .that(actual) .isEqualTo(expected); }
/** * Fails if the map does not contain exactly the keys in the given multimap, mapping to values * that correspond to the values of the given multimap. * * <p>A subsequent call to {@link Ordered#inOrder} may be made if the caller wishes to verify * that the two Multimaps iterate fully in the same order. That is, their key sets iterate in * the same order, and the corresponding value collections for each key iterate in the same * order. */ @CanIgnoreReturnValue public <K, V extends E> Ordered containsExactlyEntriesIn(Multimap<K, V> expectedMultimap) { // Note: The non-fuzzy MultimapSubject.containsExactlyEntriesIn has a custom implementation // and produces somewhat better failure messages simply asserting about the iterables of // entries would: it formats the expected values as k=[v1, v2] rather than k=v1, k=v2; and in // the case where inOrder() fails it says the keys and/or the values for some keys are out of // order. We don't bother with that here. It would be nice, but it would be a lot of added // complexity for little gain. return check() .about(iterableEntries()) .that(actual().entries()) .comparingElementsUsing(new EntryCorrespondence<K, A, V>(correspondence)) .containsExactlyElementsIn(expectedMultimap.entries()); }
/** * Fails if the subject is not strictly ordered, according to the given comparator. Strictly * ordered means that each element in the stream is <i>strictly</i> greater than the element that * preceded it. * * @throws ClassCastException if any pair of elements is not mutually Comparable */ // TODO(kak): Make this public once go/truth-stream-isinorder is decided private void isInStrictOrder(Comparator<?> comparator) { check().that(actualList).isStrictlyOrdered(comparator); }