1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/tests/background/junit3/src/healthreport/TestProfileInformationCache.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,180 @@ 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.healthreport; 1.8 + 1.9 +import java.io.File; 1.10 +import java.io.IOException; 1.11 + 1.12 +import org.json.JSONException; 1.13 +import org.json.JSONObject; 1.14 +import org.mozilla.gecko.background.helpers.FakeProfileTestCase; 1.15 + 1.16 +public class TestProfileInformationCache extends FakeProfileTestCase { 1.17 + 1.18 + public final void testInitState() throws IOException { 1.19 + MockProfileInformationCache cache = new MockProfileInformationCache(this.fakeProfileDirectory.getAbsolutePath()); 1.20 + assertFalse(cache.isInitialized()); 1.21 + assertFalse(cache.needsWrite()); 1.22 + 1.23 + try { 1.24 + cache.isBlocklistEnabled(); 1.25 + fail("Should throw fetching isBlocklistEnabled."); 1.26 + } catch (IllegalStateException e) { 1.27 + // Great! 1.28 + } 1.29 + 1.30 + cache.beginInitialization(); 1.31 + assertFalse(cache.isInitialized()); 1.32 + assertTrue(cache.needsWrite()); 1.33 + 1.34 + try { 1.35 + cache.isBlocklistEnabled(); 1.36 + fail("Should throw fetching isBlocklistEnabled."); 1.37 + } catch (IllegalStateException e) { 1.38 + // Great! 1.39 + } 1.40 + 1.41 + cache.completeInitialization(); 1.42 + assertTrue(cache.isInitialized()); 1.43 + assertFalse(cache.needsWrite()); 1.44 + } 1.45 + 1.46 + public final MockProfileInformationCache makeCache(final String suffix) { 1.47 + File subdir = new File(this.fakeProfileDirectory.getAbsolutePath() + File.separator + suffix); 1.48 + subdir.mkdir(); 1.49 + return new MockProfileInformationCache(subdir.getAbsolutePath()); 1.50 + } 1.51 + 1.52 + public final void testPersisting() throws IOException { 1.53 + MockProfileInformationCache cache = makeCache("testPersisting"); 1.54 + // We start with no file. 1.55 + assertFalse(cache.getFile().exists()); 1.56 + 1.57 + // Partially populate. Note that this doesn't happen in live code, but 1.58 + // apparently we can end up with null add-ons JSON on disk, so this 1.59 + // reproduces that scenario. 1.60 + cache.beginInitialization(); 1.61 + cache.setBlocklistEnabled(true); 1.62 + cache.setTelemetryEnabled(true); 1.63 + cache.setProfileCreationTime(1234L); 1.64 + cache.completeInitialization(); 1.65 + 1.66 + assertTrue(cache.getFile().exists()); 1.67 + 1.68 + // But reading this from disk won't work, because we were only partially 1.69 + // initialized. We want to start over. 1.70 + cache = makeCache("testPersisting"); 1.71 + assertFalse(cache.isInitialized()); 1.72 + assertFalse(cache.restoreUnlessInitialized()); 1.73 + assertFalse(cache.isInitialized()); 1.74 + 1.75 + // Now fully populate, and try again... 1.76 + cache.beginInitialization(); 1.77 + cache.setBlocklistEnabled(true); 1.78 + cache.setTelemetryEnabled(true); 1.79 + cache.setProfileCreationTime(1234L); 1.80 + cache.setJSONForAddons(new JSONObject()); 1.81 + cache.completeInitialization(); 1.82 + assertTrue(cache.getFile().exists()); 1.83 + 1.84 + // ... and this time we succeed. 1.85 + cache = makeCache("testPersisting"); 1.86 + assertFalse(cache.isInitialized()); 1.87 + assertTrue(cache.restoreUnlessInitialized()); 1.88 + assertTrue(cache.isInitialized()); 1.89 + assertTrue(cache.isBlocklistEnabled()); 1.90 + assertTrue(cache.isTelemetryEnabled()); 1.91 + assertEquals(1234L, cache.getProfileCreationTime()); 1.92 + 1.93 + // Mutate. 1.94 + cache.beginInitialization(); 1.95 + assertFalse(cache.isInitialized()); 1.96 + cache.setBlocklistEnabled(false); 1.97 + cache.setProfileCreationTime(2345L); 1.98 + cache.completeInitialization(); 1.99 + assertTrue(cache.isInitialized()); 1.100 + 1.101 + cache = makeCache("testPersisting"); 1.102 + assertFalse(cache.isInitialized()); 1.103 + assertTrue(cache.restoreUnlessInitialized()); 1.104 + 1.105 + assertTrue(cache.isInitialized()); 1.106 + assertFalse(cache.isBlocklistEnabled()); 1.107 + assertTrue(cache.isTelemetryEnabled()); 1.108 + assertEquals(2345L, cache.getProfileCreationTime()); 1.109 + } 1.110 + 1.111 + public final void testVersioning() throws JSONException, IOException { 1.112 + MockProfileInformationCache cache = makeCache("testVersioning"); 1.113 + final int currentVersion = ProfileInformationCache.FORMAT_VERSION; 1.114 + final JSONObject json = cache.toJSON(); 1.115 + assertEquals(currentVersion, json.getInt("version")); 1.116 + 1.117 + // Initialize enough that we can re-load it. 1.118 + cache.beginInitialization(); 1.119 + cache.setJSONForAddons(new JSONObject()); 1.120 + cache.completeInitialization(); 1.121 + cache.writeJSON(json); 1.122 + assertTrue(cache.restoreUnlessInitialized()); 1.123 + cache.beginInitialization(); // So that we'll need to read again. 1.124 + json.put("version", currentVersion + 1); 1.125 + cache.writeJSON(json); 1.126 + 1.127 + // We can't restore a future version. 1.128 + assertFalse(cache.restoreUnlessInitialized()); 1.129 + } 1.130 + 1.131 + public void testRestoreInvalidJSON() throws Exception { 1.132 + final MockProfileInformationCache cache = makeCache("invalid"); 1.133 + 1.134 + final JSONObject invalidJSON = new JSONObject(); 1.135 + invalidJSON.put("blocklist", true); 1.136 + invalidJSON.put("telemetry", false); 1.137 + invalidJSON.put("profileCreated", 1234567L); 1.138 + cache.writeJSON(invalidJSON); 1.139 + assertFalse(cache.restoreUnlessInitialized()); 1.140 + } 1.141 + 1.142 + private JSONObject getValidCacheJSON() throws Exception { 1.143 + final JSONObject json = new JSONObject(); 1.144 + json.put("blocklist", true); 1.145 + json.put("telemetry", false); 1.146 + json.put("profileCreated", 1234567L); 1.147 + json.put("addons", new JSONObject()); 1.148 + json.put("version", ProfileInformationCache.FORMAT_VERSION); 1.149 + return json; 1.150 + } 1.151 + 1.152 + public void testRestoreImplicitV1() throws Exception { 1.153 + assertTrue(ProfileInformationCache.FORMAT_VERSION > 1); 1.154 + 1.155 + final MockProfileInformationCache cache = makeCache("implicitV1"); 1.156 + final JSONObject json = getValidCacheJSON(); 1.157 + json.remove("version"); 1.158 + cache.writeJSON(json); 1.159 + // Can't restore v1 (which is implicitly set) since it is not the current version. 1.160 + assertFalse(cache.restoreUnlessInitialized()); 1.161 + } 1.162 + 1.163 + public void testRestoreOldVersion() throws Exception { 1.164 + final int oldVersion = 1; 1.165 + assertTrue(ProfileInformationCache.FORMAT_VERSION > oldVersion); 1.166 + 1.167 + final MockProfileInformationCache cache = makeCache("oldVersion"); 1.168 + final JSONObject json = getValidCacheJSON(); 1.169 + json.put("version", oldVersion); 1.170 + cache.writeJSON(json); 1.171 + assertFalse(cache.restoreUnlessInitialized()); 1.172 + } 1.173 + 1.174 + public void testRestoreCurrentVersion() throws Exception { 1.175 + final MockProfileInformationCache cache = makeCache("currentVersion"); 1.176 + final JSONObject json = getValidCacheJSON(); 1.177 + cache.writeJSON(json); 1.178 + cache.beginInitialization(); 1.179 + cache.setTelemetryEnabled(true); 1.180 + cache.completeInitialization(); 1.181 + assertEquals(ProfileInformationCache.FORMAT_VERSION, cache.readJSON().getInt("version")); 1.182 + } 1.183 +}