mobile/android/tests/background/junit3/src/db/TestFennecTabsRepositorySession.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/tests/background/junit3/src/db/TestFennecTabsRepositorySession.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,205 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +package org.mozilla.gecko.background.db;
     1.8 +
     1.9 +import org.json.simple.JSONArray;
    1.10 +import org.mozilla.gecko.background.helpers.AndroidSyncTestCase;
    1.11 +import org.mozilla.gecko.background.sync.helpers.ExpectFetchDelegate;
    1.12 +import org.mozilla.gecko.background.sync.helpers.SessionTestHelper;
    1.13 +import org.mozilla.gecko.db.BrowserContract;
    1.14 +import org.mozilla.gecko.sync.repositories.NoContentProviderException;
    1.15 +import org.mozilla.gecko.sync.repositories.RepositorySession;
    1.16 +import org.mozilla.gecko.sync.repositories.android.BrowserContractHelpers;
    1.17 +import org.mozilla.gecko.sync.repositories.android.FennecTabsRepository;
    1.18 +import org.mozilla.gecko.sync.repositories.android.FennecTabsRepository.FennecTabsRepositorySession;
    1.19 +import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionCreationDelegate;
    1.20 +import org.mozilla.gecko.sync.repositories.domain.Record;
    1.21 +import org.mozilla.gecko.sync.repositories.domain.TabsRecord;
    1.22 +
    1.23 +import android.content.ContentProviderClient;
    1.24 +import android.content.ContentResolver;
    1.25 +import android.content.Context;
    1.26 +import android.database.Cursor;
    1.27 +import android.os.RemoteException;
    1.28 +
    1.29 +public class TestFennecTabsRepositorySession extends AndroidSyncTestCase {
    1.30 +  public static final String TEST_CLIENT_GUID = "test guid"; // Real GUIDs never contain spaces.
    1.31 +  public static final String TEST_CLIENT_NAME = "test client name";
    1.32 +
    1.33 +  // Override these to test against data that is not live.
    1.34 +  public static final String TEST_TABS_CLIENT_GUID_IS_LOCAL_SELECTION = BrowserContract.Tabs.CLIENT_GUID + " IS ?";
    1.35 +  public static final String[] TEST_TABS_CLIENT_GUID_IS_LOCAL_SELECTION_ARGS = new String[] { TEST_CLIENT_GUID };
    1.36 +
    1.37 +  protected ContentProviderClient tabsClient = null;
    1.38 +
    1.39 +  protected ContentProviderClient getTabsClient() {
    1.40 +    final ContentResolver cr = getApplicationContext().getContentResolver();
    1.41 +    return cr.acquireContentProviderClient(BrowserContractHelpers.TABS_CONTENT_URI);
    1.42 +  }
    1.43 +
    1.44 +  public TestFennecTabsRepositorySession() throws NoContentProviderException {
    1.45 +    super();
    1.46 +  }
    1.47 +
    1.48 +  @Override
    1.49 +  public void setUp() {
    1.50 +    if (tabsClient == null) {
    1.51 +      tabsClient = getTabsClient();
    1.52 +    }
    1.53 +  }
    1.54 +
    1.55 +  protected int deleteAllTestTabs(final ContentProviderClient tabsClient) throws RemoteException {
    1.56 +    if (tabsClient == null) {
    1.57 +      return -1;
    1.58 +    }
    1.59 +    return tabsClient.delete(BrowserContractHelpers.TABS_CONTENT_URI,
    1.60 +        TEST_TABS_CLIENT_GUID_IS_LOCAL_SELECTION, TEST_TABS_CLIENT_GUID_IS_LOCAL_SELECTION_ARGS);
    1.61 +  }
    1.62 +
    1.63 +  @Override
    1.64 +  protected void tearDown() throws Exception {
    1.65 +    if (tabsClient != null) {
    1.66 +      deleteAllTestTabs(tabsClient);
    1.67 +
    1.68 +      tabsClient.release();
    1.69 +      tabsClient = null;
    1.70 +    }
    1.71 +  }
    1.72 +
    1.73 +  protected FennecTabsRepository getRepository() {
    1.74 +    /**
    1.75 +     * Override this chain in order to avoid our test code having to create two
    1.76 +     * sessions all the time.
    1.77 +     */
    1.78 +    return new FennecTabsRepository(TEST_CLIENT_NAME, TEST_CLIENT_GUID) {
    1.79 +      @Override
    1.80 +      public void createSession(RepositorySessionCreationDelegate delegate,
    1.81 +                                Context context) {
    1.82 +        try {
    1.83 +          final FennecTabsRepositorySession session = new FennecTabsRepositorySession(this, context) {
    1.84 +            @Override
    1.85 +            protected synchronized void trackGUID(String guid) {
    1.86 +            }
    1.87 +
    1.88 +            @Override
    1.89 +            protected String localClientSelection() {
    1.90 +              return TEST_TABS_CLIENT_GUID_IS_LOCAL_SELECTION;
    1.91 +            }
    1.92 +
    1.93 +            @Override
    1.94 +            protected String[] localClientSelectionArgs() {
    1.95 +              return TEST_TABS_CLIENT_GUID_IS_LOCAL_SELECTION_ARGS;
    1.96 +            }
    1.97 +          };
    1.98 +          delegate.onSessionCreated(session);
    1.99 +        } catch (Exception e) {
   1.100 +          delegate.onSessionCreateFailed(e);
   1.101 +        }
   1.102 +      }
   1.103 +    };
   1.104 +  }
   1.105 +
   1.106 +  protected FennecTabsRepositorySession createSession() {
   1.107 +    return (FennecTabsRepositorySession) SessionTestHelper.createSession(
   1.108 +        getApplicationContext(),
   1.109 +        getRepository());
   1.110 +  }
   1.111 +
   1.112 +  protected FennecTabsRepositorySession createAndBeginSession() {
   1.113 +    return (FennecTabsRepositorySession) SessionTestHelper.createAndBeginSession(
   1.114 +        getApplicationContext(),
   1.115 +        getRepository());
   1.116 +  }
   1.117 +
   1.118 +  protected Runnable fetchSinceRunnable(final RepositorySession session, final long timestamp, final Record[] expectedRecords) {
   1.119 +    return new Runnable() {
   1.120 +      @Override
   1.121 +      public void run() {
   1.122 +        session.fetchSince(timestamp, new ExpectFetchDelegate(expectedRecords));
   1.123 +      }
   1.124 +    };
   1.125 +  }
   1.126 +
   1.127 +  protected Runnable fetchAllRunnable(final RepositorySession session, final Record[] expectedRecords) {
   1.128 +    return new Runnable() {
   1.129 +      @Override
   1.130 +      public void run() {
   1.131 +        session.fetchAll(new ExpectFetchDelegate(expectedRecords));
   1.132 +      }
   1.133 +    };
   1.134 +  }
   1.135 +
   1.136 +  protected Tab testTab1;
   1.137 +  protected Tab testTab2;
   1.138 +  protected Tab testTab3;
   1.139 +
   1.140 +  @SuppressWarnings("unchecked")
   1.141 +  private void insertSomeTestTabs(ContentProviderClient tabsClient) throws RemoteException {
   1.142 +    final JSONArray history1 = new JSONArray();
   1.143 +    history1.add("http://test.com/test1.html");
   1.144 +    testTab1 = new Tab("test title 1", "http://test.com/test1.png", history1, 1000);
   1.145 +
   1.146 +    final JSONArray history2 = new JSONArray();
   1.147 +    history2.add("http://test.com/test2.html#1");
   1.148 +    history2.add("http://test.com/test2.html#2");
   1.149 +    history2.add("http://test.com/test2.html#3");
   1.150 +    testTab2 = new Tab("test title 2", "http://test.com/test2.png", history2, 2000);
   1.151 +
   1.152 +    final JSONArray history3 = new JSONArray();
   1.153 +    history3.add("http://test.com/test3.html#1");
   1.154 +    history3.add("http://test.com/test3.html#2");
   1.155 +    testTab3 = new Tab("test title 3", "http://test.com/test3.png", history3, 3000);
   1.156 +
   1.157 +    tabsClient.insert(BrowserContractHelpers.TABS_CONTENT_URI, testTab1.toContentValues(TEST_CLIENT_GUID, 0));
   1.158 +    tabsClient.insert(BrowserContractHelpers.TABS_CONTENT_URI, testTab2.toContentValues(TEST_CLIENT_GUID, 1));
   1.159 +    tabsClient.insert(BrowserContractHelpers.TABS_CONTENT_URI, testTab3.toContentValues(TEST_CLIENT_GUID, 2));
   1.160 +  }
   1.161 +
   1.162 +  protected TabsRecord insertTestTabsAndExtractTabsRecord() throws RemoteException {
   1.163 +    insertSomeTestTabs(tabsClient);
   1.164 +
   1.165 +    final String positionAscending = BrowserContract.Tabs.POSITION + " ASC";
   1.166 +    Cursor cursor = null;
   1.167 +    try {
   1.168 +      cursor = tabsClient.query(BrowserContractHelpers.TABS_CONTENT_URI, null,
   1.169 +          TEST_TABS_CLIENT_GUID_IS_LOCAL_SELECTION, TEST_TABS_CLIENT_GUID_IS_LOCAL_SELECTION_ARGS, positionAscending);
   1.170 +      CursorDumper.dumpCursor(cursor);
   1.171 +
   1.172 +      final TabsRecord tabsRecord = FennecTabsRepository.tabsRecordFromCursor(cursor, TEST_CLIENT_GUID, TEST_CLIENT_NAME);
   1.173 +
   1.174 +      assertEquals(TEST_CLIENT_GUID, tabsRecord.guid);
   1.175 +      assertEquals(TEST_CLIENT_NAME, tabsRecord.clientName);
   1.176 +
   1.177 +      assertNotNull(tabsRecord.tabs);
   1.178 +      assertEquals(cursor.getCount(), tabsRecord.tabs.size());
   1.179 +
   1.180 +      return tabsRecord;
   1.181 +    } finally {
   1.182 +      cursor.close();
   1.183 +    }
   1.184 +  }
   1.185 +
   1.186 +  public void testFetchAll() throws NoContentProviderException, RemoteException {
   1.187 +    final TabsRecord tabsRecord = insertTestTabsAndExtractTabsRecord();
   1.188 +
   1.189 +    final FennecTabsRepositorySession session = createAndBeginSession();
   1.190 +    performWait(fetchAllRunnable(session, new Record[] { tabsRecord }));
   1.191 +
   1.192 +    session.abort();
   1.193 +  }
   1.194 +
   1.195 +  public void testFetchSince() throws NoContentProviderException, RemoteException {
   1.196 +    final TabsRecord tabsRecord = insertTestTabsAndExtractTabsRecord();
   1.197 +
   1.198 +    final FennecTabsRepositorySession session = createAndBeginSession();
   1.199 +
   1.200 +    // Not all tabs are modified after this, but the record should contain them all.
   1.201 +    performWait(fetchSinceRunnable(session, 1000, new Record[] { tabsRecord }));
   1.202 +
   1.203 +    // No tabs are modified after this, so we shouldn't get a record at all.
   1.204 +    performWait(fetchSinceRunnable(session, 4000, new Record[] { }));
   1.205 +
   1.206 +    session.abort();
   1.207 +  }
   1.208 +}

mercurial