mobile/android/tests/background/junit3/src/sync/helpers/DefaultFetchDelegate.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/sync/helpers/DefaultFetchDelegate.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,106 @@
     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.sync.helpers;
     1.8 +
     1.9 +import static junit.framework.Assert.assertEquals;
    1.10 +import static junit.framework.Assert.assertTrue;
    1.11 +import static junit.framework.Assert.fail;
    1.12 +
    1.13 +import java.util.ArrayList;
    1.14 +import java.util.HashMap;
    1.15 +import java.util.HashSet;
    1.16 +import java.util.Set;
    1.17 +import java.util.concurrent.ExecutorService;
    1.18 +
    1.19 +import junit.framework.AssertionFailedError;
    1.20 +
    1.21 +import org.mozilla.gecko.background.common.log.Logger;
    1.22 +import org.mozilla.gecko.sync.repositories.delegates.DeferredRepositorySessionFetchRecordsDelegate;
    1.23 +import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionFetchRecordsDelegate;
    1.24 +import org.mozilla.gecko.sync.repositories.domain.Record;
    1.25 +
    1.26 +public class DefaultFetchDelegate extends DefaultDelegate implements RepositorySessionFetchRecordsDelegate {
    1.27 +
    1.28 +  private static final String LOG_TAG = "DefaultFetchDelegate";
    1.29 +  public ArrayList<Record> records = new ArrayList<Record>();
    1.30 +  public Set<String> ignore = new HashSet<String>();
    1.31 +
    1.32 +  @Override
    1.33 +  public void onFetchFailed(Exception ex, Record record) {
    1.34 +    performNotify("Fetch failed.", ex);
    1.35 +  }
    1.36 +
    1.37 +  protected void onDone(ArrayList<Record> records, HashMap<String, Record> expected, long end) {
    1.38 +    Logger.debug(LOG_TAG, "onDone.");
    1.39 +    Logger.debug(LOG_TAG, "End timestamp is " + end);
    1.40 +    Logger.debug(LOG_TAG, "Expected is " + expected);
    1.41 +    Logger.debug(LOG_TAG, "Records is " + records);
    1.42 +    Set<String> foundGuids = new HashSet<String>();
    1.43 +    try {
    1.44 +      int expectedCount = 0;
    1.45 +      int expectedFound = 0;
    1.46 +      Logger.debug(LOG_TAG, "Counting expected keys.");
    1.47 +      for (String key : expected.keySet()) {
    1.48 +        if (!ignore.contains(key)) {
    1.49 +          expectedCount++;
    1.50 +        }
    1.51 +      }
    1.52 +      Logger.debug(LOG_TAG, "Expected keys: " + expectedCount);
    1.53 +      for (Record record : records) {
    1.54 +        Logger.debug(LOG_TAG, "Record.");
    1.55 +        Logger.debug(LOG_TAG, record.guid);
    1.56 +
    1.57 +        // Ignore special GUIDs (e.g., for bookmarks).
    1.58 +        if (!ignore.contains(record.guid)) {
    1.59 +          if (foundGuids.contains(record.guid)) {
    1.60 +            fail("Found duplicate guid " + record.guid);
    1.61 +          }
    1.62 +          Record expect = expected.get(record.guid);
    1.63 +          if (expect == null) {
    1.64 +            fail("Do not expect to get back a record with guid: " + record.guid); // Caught below
    1.65 +          }
    1.66 +          Logger.debug(LOG_TAG, "Checking equality.");
    1.67 +          try {
    1.68 +            assertTrue(expect.equalPayloads(record)); // Caught below
    1.69 +          } catch (Exception e) {
    1.70 +            Logger.error(LOG_TAG, "ONOZ!", e);
    1.71 +          }
    1.72 +          Logger.debug(LOG_TAG, "Checked equality.");
    1.73 +          expectedFound += 1;
    1.74 +          // Track record once we've found it.
    1.75 +          foundGuids.add(record.guid);
    1.76 +        }
    1.77 +      }
    1.78 +      assertEquals(expectedCount, expectedFound); // Caught below
    1.79 +      Logger.debug(LOG_TAG, "Notifying success.");
    1.80 +      performNotify();
    1.81 +    } catch (AssertionFailedError e) {
    1.82 +      Logger.error(LOG_TAG, "Notifying assertion failure.");
    1.83 +      performNotify(e);
    1.84 +    } catch (Exception e) {
    1.85 +      Logger.error(LOG_TAG, "No!");
    1.86 +      performNotify();
    1.87 +    }
    1.88 +  }
    1.89 +
    1.90 +  public int recordCount() {
    1.91 +    return (this.records == null) ? 0 : this.records.size();
    1.92 +  }
    1.93 +
    1.94 +  @Override
    1.95 +  public void onFetchedRecord(Record record) {
    1.96 +    Logger.debug(LOG_TAG, "onFetchedRecord(" + record.guid + ")");
    1.97 +    records.add(record);
    1.98 +  }
    1.99 +
   1.100 +  @Override
   1.101 +  public void onFetchCompleted(final long fetchEnd) {
   1.102 +    Logger.debug(LOG_TAG, "onFetchCompleted. Doing nothing.");
   1.103 +  }
   1.104 +
   1.105 +  @Override
   1.106 +  public RepositorySessionFetchRecordsDelegate deferredFetchDelegate(final ExecutorService executor) {
   1.107 +    return new DeferredRepositorySessionFetchRecordsDelegate(this, executor);
   1.108 +  }
   1.109 +}

mercurial