mobile/android/tests/background/junit3/src/healthreport/TestProfileInformationCache.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 package org.mozilla.gecko.background.healthreport;
     6 import java.io.File;
     7 import java.io.IOException;
     9 import org.json.JSONException;
    10 import org.json.JSONObject;
    11 import org.mozilla.gecko.background.helpers.FakeProfileTestCase;
    13 public class TestProfileInformationCache extends FakeProfileTestCase {
    15   public final void testInitState() throws IOException {
    16     MockProfileInformationCache cache = new MockProfileInformationCache(this.fakeProfileDirectory.getAbsolutePath());
    17     assertFalse(cache.isInitialized());
    18     assertFalse(cache.needsWrite());
    20     try {
    21       cache.isBlocklistEnabled();
    22       fail("Should throw fetching isBlocklistEnabled.");
    23     } catch (IllegalStateException e) {
    24       // Great!
    25     }
    27     cache.beginInitialization();
    28     assertFalse(cache.isInitialized());
    29     assertTrue(cache.needsWrite());
    31     try {
    32       cache.isBlocklistEnabled();
    33       fail("Should throw fetching isBlocklistEnabled.");
    34     } catch (IllegalStateException e) {
    35       // Great!
    36     }
    38     cache.completeInitialization();
    39     assertTrue(cache.isInitialized());
    40     assertFalse(cache.needsWrite());
    41   }
    43   public final MockProfileInformationCache makeCache(final String suffix) {
    44     File subdir = new File(this.fakeProfileDirectory.getAbsolutePath() + File.separator + suffix);
    45     subdir.mkdir();
    46     return new MockProfileInformationCache(subdir.getAbsolutePath());
    47   }
    49   public final void testPersisting() throws IOException {
    50     MockProfileInformationCache cache = makeCache("testPersisting");
    51     // We start with no file.
    52     assertFalse(cache.getFile().exists());
    54     // Partially populate. Note that this doesn't happen in live code, but
    55     // apparently we can end up with null add-ons JSON on disk, so this
    56     // reproduces that scenario.
    57     cache.beginInitialization();
    58     cache.setBlocklistEnabled(true);
    59     cache.setTelemetryEnabled(true);
    60     cache.setProfileCreationTime(1234L);
    61     cache.completeInitialization();
    63     assertTrue(cache.getFile().exists());
    65     // But reading this from disk won't work, because we were only partially
    66     // initialized. We want to start over.
    67     cache = makeCache("testPersisting");
    68     assertFalse(cache.isInitialized());
    69     assertFalse(cache.restoreUnlessInitialized());
    70     assertFalse(cache.isInitialized());
    72     // Now fully populate, and try again...
    73     cache.beginInitialization();
    74     cache.setBlocklistEnabled(true);
    75     cache.setTelemetryEnabled(true);
    76     cache.setProfileCreationTime(1234L);
    77     cache.setJSONForAddons(new JSONObject());
    78     cache.completeInitialization();
    79     assertTrue(cache.getFile().exists());
    81     // ... and this time we succeed.
    82     cache = makeCache("testPersisting");
    83     assertFalse(cache.isInitialized());
    84     assertTrue(cache.restoreUnlessInitialized());
    85     assertTrue(cache.isInitialized());
    86     assertTrue(cache.isBlocklistEnabled());
    87     assertTrue(cache.isTelemetryEnabled());
    88     assertEquals(1234L, cache.getProfileCreationTime());
    90     // Mutate.
    91     cache.beginInitialization();
    92     assertFalse(cache.isInitialized());
    93     cache.setBlocklistEnabled(false);
    94     cache.setProfileCreationTime(2345L);
    95     cache.completeInitialization();
    96     assertTrue(cache.isInitialized());
    98     cache = makeCache("testPersisting");
    99     assertFalse(cache.isInitialized());
   100     assertTrue(cache.restoreUnlessInitialized());
   102     assertTrue(cache.isInitialized());
   103     assertFalse(cache.isBlocklistEnabled());
   104     assertTrue(cache.isTelemetryEnabled());
   105     assertEquals(2345L, cache.getProfileCreationTime());
   106   }
   108   public final void testVersioning() throws JSONException, IOException {
   109     MockProfileInformationCache cache = makeCache("testVersioning");
   110     final int currentVersion = ProfileInformationCache.FORMAT_VERSION;
   111     final JSONObject json = cache.toJSON();
   112     assertEquals(currentVersion, json.getInt("version"));
   114     // Initialize enough that we can re-load it.
   115     cache.beginInitialization();
   116     cache.setJSONForAddons(new JSONObject());
   117     cache.completeInitialization();
   118     cache.writeJSON(json);
   119     assertTrue(cache.restoreUnlessInitialized());
   120     cache.beginInitialization();     // So that we'll need to read again.
   121     json.put("version", currentVersion + 1);
   122     cache.writeJSON(json);
   124     // We can't restore a future version.
   125     assertFalse(cache.restoreUnlessInitialized());
   126   }
   128   public void testRestoreInvalidJSON() throws Exception {
   129     final MockProfileInformationCache cache = makeCache("invalid");
   131     final JSONObject invalidJSON = new JSONObject();
   132     invalidJSON.put("blocklist", true);
   133     invalidJSON.put("telemetry", false);
   134     invalidJSON.put("profileCreated", 1234567L);
   135     cache.writeJSON(invalidJSON);
   136     assertFalse(cache.restoreUnlessInitialized());
   137   }
   139   private JSONObject getValidCacheJSON() throws Exception {
   140     final JSONObject json = new JSONObject();
   141     json.put("blocklist", true);
   142     json.put("telemetry", false);
   143     json.put("profileCreated", 1234567L);
   144     json.put("addons", new JSONObject());
   145     json.put("version", ProfileInformationCache.FORMAT_VERSION);
   146     return json;
   147   }
   149   public void testRestoreImplicitV1() throws Exception {
   150     assertTrue(ProfileInformationCache.FORMAT_VERSION > 1);
   152     final MockProfileInformationCache cache = makeCache("implicitV1");
   153     final JSONObject json = getValidCacheJSON();
   154     json.remove("version");
   155     cache.writeJSON(json);
   156     // Can't restore v1 (which is implicitly set) since it is not the current version.
   157     assertFalse(cache.restoreUnlessInitialized());
   158   }
   160   public void testRestoreOldVersion() throws Exception {
   161     final int oldVersion = 1;
   162     assertTrue(ProfileInformationCache.FORMAT_VERSION > oldVersion);
   164     final MockProfileInformationCache cache = makeCache("oldVersion");
   165     final JSONObject json = getValidCacheJSON();
   166     json.put("version", oldVersion);
   167     cache.writeJSON(json);
   168     assertFalse(cache.restoreUnlessInitialized());
   169   }
   171   public void testRestoreCurrentVersion() throws Exception {
   172     final MockProfileInformationCache cache = makeCache("currentVersion");
   173     final JSONObject json = getValidCacheJSON();
   174     cache.writeJSON(json);
   175     cache.beginInitialization();
   176     cache.setTelemetryEnabled(true);
   177     cache.completeInitialization();
   178     assertEquals(ProfileInformationCache.FORMAT_VERSION, cache.readJSON().getInt("version"));
   179   }
   180 }

mercurial