mobile/android/tests/background/junit3/src/db/TestCachedSQLiteOpenHelper.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/TestCachedSQLiteOpenHelper.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,56 @@
     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.mozilla.gecko.background.sync.helpers.HistoryHelpers;
    1.10 +import org.mozilla.gecko.sync.repositories.NullCursorException;
    1.11 +import org.mozilla.gecko.sync.repositories.android.AndroidBrowserHistoryDataExtender;
    1.12 +import org.mozilla.gecko.sync.repositories.android.ClientsDatabase;
    1.13 +import org.mozilla.gecko.sync.repositories.domain.ClientRecord;
    1.14 +import org.mozilla.gecko.sync.repositories.domain.HistoryRecord;
    1.15 +import org.mozilla.gecko.sync.setup.Constants;
    1.16 +
    1.17 +import android.database.Cursor;
    1.18 +import android.test.AndroidTestCase;
    1.19 +
    1.20 +public class TestCachedSQLiteOpenHelper extends AndroidTestCase {
    1.21 +
    1.22 +  protected ClientsDatabase clientsDB;
    1.23 +  protected AndroidBrowserHistoryDataExtender extender;
    1.24 +
    1.25 +  public void setUp() {
    1.26 +    clientsDB = new ClientsDatabase(mContext);
    1.27 +    extender = new AndroidBrowserHistoryDataExtender(mContext);
    1.28 +  }
    1.29 +
    1.30 +  public void tearDown() {
    1.31 +    clientsDB.close();
    1.32 +    extender.close();
    1.33 +  }
    1.34 +
    1.35 +  public void testUnclosedDatabasesDontInteract() throws NullCursorException {
    1.36 +    // clientsDB gracefully does its thing and closes.
    1.37 +    clientsDB.wipeClientsTable();
    1.38 +    ClientRecord record = new ClientRecord();
    1.39 +    String profileConst = Constants.DEFAULT_PROFILE;
    1.40 +    clientsDB.store(profileConst, record);
    1.41 +    clientsDB.close();
    1.42 +
    1.43 +    // extender does its thing but still hasn't closed.
    1.44 +    HistoryRecord h = HistoryHelpers.createHistory1();
    1.45 +    extender.store(h.guid, h.visits);
    1.46 +
    1.47 +    // Ensure items in the clientsDB are still accessible nonetheless.
    1.48 +    Cursor cur = null;
    1.49 +    try {
    1.50 +      cur = clientsDB.fetchAllClients();
    1.51 +      assertTrue(cur.moveToFirst());
    1.52 +      assertEquals(1, cur.getCount());
    1.53 +    } finally {
    1.54 +      if (cur != null) {
    1.55 +        cur.close();
    1.56 +      }
    1.57 +    }
    1.58 +  }
    1.59 +}

mercurial