Bug 1260478 - Part 2: Duplicate TestTabsRecord into TestTabsProvider unit test. r=mcomella

MozReview-Commit-ID: 4V8Pfll5MfN

--HG--
extra : rebase_source : 9beb3573974f5045b79f643174457a04b228c5da
extra : histedit_source : b4f1b5e21aecd24aaf0379e121303eebdbde6d08
This commit is contained in:
Nick Alexander 2016-03-27 14:24:22 -07:00
parent f14934994f
commit bdec48be3f

View File

@ -19,6 +19,8 @@ import org.mozilla.gecko.background.testhelpers.TestRunner;
import org.mozilla.gecko.db.BrowserContract;
import org.mozilla.gecko.db.TabsProvider;
import org.mozilla.gecko.sync.repositories.android.BrowserContractHelpers;
import org.mozilla.gecko.sync.repositories.android.FennecTabsRepository;
import org.mozilla.gecko.sync.repositories.domain.TabsRecord;
import org.robolectric.shadows.ShadowContentResolver;
@RunWith(TestRunner.class)
@ -246,4 +248,91 @@ public class TestTabsProvider {
cursor.close();
}
}
@Test
public void testTabsRecordFromCursor() throws Exception {
final ContentProviderClient tabsClient = getTabsClient();
deleteAllTestTabs(tabsClient);
insertTestClient(getClientsClient());
insertSomeTestTabs(tabsClient);
final String positionAscending = BrowserContract.Tabs.POSITION + " ASC";
Cursor cursor = null;
try {
cursor = tabsClient.query(BrowserContractHelpers.TABS_CONTENT_URI, null, TABS_CLIENT_GUID_IS, new String[] { TEST_CLIENT_GUID }, positionAscending);
Assert.assertEquals(3, cursor.getCount());
cursor.moveToPosition(1);
final TabsRecord tabsRecord = FennecTabsRepository.tabsRecordFromCursor(cursor, TEST_CLIENT_GUID, TEST_CLIENT_NAME);
// Make sure we clean up after ourselves.
Assert.assertEquals(1, cursor.getPosition());
Assert.assertEquals(TEST_CLIENT_GUID, tabsRecord.guid);
Assert.assertEquals(TEST_CLIENT_NAME, tabsRecord.clientName);
Assert.assertEquals(3, tabsRecord.tabs.size());
Assert.assertEquals(testTab1, tabsRecord.tabs.get(0));
Assert.assertEquals(testTab2, tabsRecord.tabs.get(1));
Assert.assertEquals(testTab3, tabsRecord.tabs.get(2));
Assert.assertEquals(Math.max(Math.max(testTab1.lastUsed, testTab2.lastUsed), testTab3.lastUsed), tabsRecord.lastModified);
} finally {
cursor.close();
}
}
// Verify that we can fetch a record when there are no local tabs at all.
@Test
public void testEmptyTabsRecordFromCursor() throws Exception {
final ContentProviderClient tabsClient = getTabsClient();
deleteAllTestTabs(tabsClient);
final String positionAscending = BrowserContract.Tabs.POSITION + " ASC";
Cursor cursor = null;
try {
cursor = tabsClient.query(BrowserContractHelpers.TABS_CONTENT_URI, null, TABS_CLIENT_GUID_IS, new String[] { TEST_CLIENT_GUID }, positionAscending);
Assert.assertEquals(0, cursor.getCount());
final TabsRecord tabsRecord = FennecTabsRepository.tabsRecordFromCursor(cursor, TEST_CLIENT_GUID, TEST_CLIENT_NAME);
Assert.assertEquals(TEST_CLIENT_GUID, tabsRecord.guid);
Assert.assertEquals(TEST_CLIENT_NAME, tabsRecord.clientName);
Assert.assertNotNull(tabsRecord.tabs);
Assert.assertEquals(0, tabsRecord.tabs.size());
Assert.assertEquals(0, tabsRecord.lastModified);
} finally {
cursor.close();
}
}
// Not much of a test, but verifies the tabs record at least agrees with the
// disk data and doubles as a database inspector.
@Test
public void testLocalTabs() throws Exception {
final ContentProviderClient tabsClient = getTabsClient();
final String positionAscending = BrowserContract.Tabs.POSITION + " ASC";
Cursor cursor = null;
try {
// Keep this in sync with the Fennec schema.
cursor = tabsClient.query(BrowserContractHelpers.TABS_CONTENT_URI, null, BrowserContract.Tabs.CLIENT_GUID + " IS NULL", null, positionAscending);
CursorDumper.dumpCursor(cursor);
final TabsRecord tabsRecord = FennecTabsRepository.tabsRecordFromCursor(cursor, TEST_CLIENT_GUID, TEST_CLIENT_NAME);
Assert.assertEquals(TEST_CLIENT_GUID, tabsRecord.guid);
Assert.assertEquals(TEST_CLIENT_NAME, tabsRecord.clientName);
Assert.assertNotNull(tabsRecord.tabs);
Assert.assertEquals(cursor.getCount(), tabsRecord.tabs.size());
} finally {
cursor.close();
}
}
}