### Question:
InvalidationTracker { @WorkerThread public void addObserver(@NonNull Observer observer) { final String[] tableNames = observer.mTables; int[] tableIds = new int[tableNames.length]; final int size = tableNames.length; long[] versions = new long[tableNames.length]; for (int i = 0; i < size; i++) { Integer tableId = mTableIdLookup.get(tableNames[i].toLowerCase(Locale.US)); if (tableId == null) { throw new IllegalArgumentException("There is no table with name " + tableNames[i]); } tableIds[i] = tableId; versions[i] = mMaxVersion; } ObserverWrapper wrapper = new ObserverWrapper(observer, tableIds, tableNames, versions); ObserverWrapper currentObserver; synchronized (mObserverMap) { currentObserver = mObserverMap.putIfAbsent(observer, wrapper); } if (currentObserver == null && mObservedTableTracker.onAdded(tableIds)) { syncTriggers(); } } @SuppressWarnings("WeakerAccess") @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)  InvalidationTracker(RoomDatabase database, String... tableNames); @WorkerThread void addObserver(@NonNull Observer observer); @SuppressWarnings("unused") @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) void addWeakObserver(Observer observer); @SuppressWarnings("WeakerAccess") @WorkerThread void removeObserver(@NonNull final Observer observer); @SuppressWarnings("WeakerAccess") void refreshVersionsAsync(); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @WorkerThread void refreshVersionsSync();  }### Answer:
@Test(expected = IllegalArgumentException.class) public void badObserver() { InvalidationTracker.Observer observer = new LatchObserver(1, "x"); mTracker.addObserver(observer); }
@Test public void observe1Table() throws Exception { LatchObserver observer = new LatchObserver(1, "a"); mTracker.addObserver(observer); setVersions(1, 0, 2, 1); refreshSync(); assertThat(observer.await(), is(true)); assertThat(observer.getInvalidatedTables().size(), is(1)); assertThat(observer.getInvalidatedTables(), hasItem("a")); setVersions(3, 1); observer.reset(1); refreshSync(); assertThat(observer.await(), is(false)); setVersions(4, 0); refreshSync(); assertThat(observer.await(), is(true)); assertThat(observer.getInvalidatedTables().size(), is(1)); assertThat(observer.getInvalidatedTables(), hasItem("a")); }
@Test public void observe2Tables() throws Exception { LatchObserver observer = new LatchObserver(1, "A", "B"); mTracker.addObserver(observer); setVersions(1, 0, 2, 1); refreshSync(); assertThat(observer.await(), is(true)); assertThat(observer.getInvalidatedTables().size(), is(2)); assertThat(observer.getInvalidatedTables(), hasItems("A", "B")); setVersions(3, 1); observer.reset(1); refreshSync(); assertThat(observer.await(), is(true)); assertThat(observer.getInvalidatedTables().size(), is(1)); assertThat(observer.getInvalidatedTables(), hasItem("B")); setVersions(4, 0); observer.reset(1); refreshSync(); assertThat(observer.await(), is(true)); assertThat(observer.getInvalidatedTables().size(), is(1)); assertThat(observer.getInvalidatedTables(), hasItem("A")); observer.reset(1); refreshSync(); assertThat(observer.await(), is(false)); }
@Test public void locale() { LatchObserver observer = new LatchObserver(1, "I"); mTracker.addObserver(observer); }
@Test public void closedDb() { doReturn(false).when(mRoomDatabase).isOpen(); doThrow(new IllegalStateException("foo")).when(mOpenHelper).getWritableDatabase(); mTracker.addObserver(new LatchObserver(1, "a", "b")); mTracker.mRefreshRunnable.run(); }