Bug 1246816 - Add unit tests for profile creation date. r=sebastian
authorMichael Comella <michael.l.comella@gmail.com>
Thu, 31 Mar 2016 15:22:09 -0700
changeset 292107 0bdc00213180d671c58bd10e2e7e79853d481532
parent 292106 6653113a8093393a68ce21899a0ab698e89a8d1c
child 292108 163ec1ec0f25e482a9c5156581887744f1f3fbb7
push id74762
push usercbook@mozilla.com
push dateThu, 07 Apr 2016 09:56:20 +0000
treeherdermozilla-inbound@772253c53374 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian
bugs1246816
milestone48.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1246816 - Add unit tests for profile creation date. r=sebastian MozReview-Commit-ID: 5ONkG2MzHfc
mobile/android/tests/background/junit4/src/org/mozilla/gecko/TestGeckoProfile.java
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/TestGeckoProfile.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/TestGeckoProfile.java
@@ -26,32 +26,35 @@ import static org.junit.Assert.*;
 /**
  * Unit test methods of the GeckoProfile class.
  */
 @RunWith(TestRunner.class)
 public class TestGeckoProfile {
     private static final String PROFILE_NAME = "profileName";
 
     private static final String CLIENT_ID_JSON_ATTR = "clientID";
+    private static final String PROFILE_CREATION_DATE_JSON_ATTR = "created";
 
     @Rule
     public TemporaryFolder dirContainingProfile = new TemporaryFolder();
 
     private File profileDir;
     private GeckoProfile profile;
 
     private File clientIdFile;
+    private File timesFile;
 
     @Before
     public void setUp() throws IOException {
         final Context context = RuntimeEnvironment.application;
         profileDir = dirContainingProfile.newFolder();
         profile = GeckoProfile.get(context, PROFILE_NAME, profileDir);
 
         clientIdFile = new File(profileDir, "datareporting/state.json");
+        timesFile = new File(profileDir, "times.json");
     }
 
     public void assertValidClientId(final String clientId) {
         // This isn't the method we use in the main GeckoProfile code, but it should be equivalent.
         UUID.fromString(clientId); // assert: will throw if null or invalid UUID.
     }
 
     @Test
@@ -196,16 +199,53 @@ public class TestGeckoProfile {
         // We generate client IDs using UUID - better make sure they're valid.
         for (int i = 0; i < 30; ++i) {
             final String generatedClientId = UUID.randomUUID().toString();
             assertTrue("Generated client ID from UUID, " + generatedClientId + ", is valid",
                     profile.isClientIdValid(generatedClientId));
         }
     }
 
+    @Test
+    public void testGetProfileCreationDateFromTimesFile() throws Exception {
+        final long expectedDate = System.currentTimeMillis();
+        final JSONObject expectedObj = new JSONObject();
+        expectedObj.put(PROFILE_CREATION_DATE_JSON_ATTR, expectedDate);
+        FileUtil.writeJSONObjectToFile(timesFile, expectedObj);
+
+        final Context context = RuntimeEnvironment.application;
+        final long actualDate = profile.getAndPersistProfileCreationDate(context);
+        assertEquals("Date from disk equals date inserted to disk", expectedDate, actualDate);
+
+        final long actualDateFromDisk = readProfileCreationDateFromFile(timesFile);
+        assertEquals("Date in times.json has not changed after accessing profile creation date",
+                expectedDate, actualDateFromDisk);
+    }
+
+    @Test
+    public void testGetProfileCreationDateTimesFileDoesNotExist() throws Exception {
+        assertFalse("Times.json does not already exist", timesFile.exists());
+
+        final Context context = RuntimeEnvironment.application;
+        final long actualDate = profile.getAndPersistProfileCreationDate(context);
+        // I'd prefer to mock so we can return and verify a specific value but we can't mock
+        // GeckoProfile because it's final. Instead, we check if the value is at least reasonable.
+        assertTrue("Date from method is positive", actualDate >= 0);
+        assertTrue("Date from method is less than current time", actualDate < System.currentTimeMillis());
+
+        assertTrue("Times.json exists after getting profile", timesFile.exists());
+        final long actualDateFromDisk = readProfileCreationDateFromFile(timesFile);
+        assertEquals("Date from disk equals returned value", actualDate, actualDateFromDisk);
+    }
+
+    private static long readProfileCreationDateFromFile(final File file) throws Exception {
+        final JSONObject actualObj = FileUtil.readJSONObjectFromFile(file);
+        return actualObj.getLong(PROFILE_CREATION_DATE_JSON_ATTR);
+    }
+
     private String readClientIdFromFile(final File file) throws Exception {
         final JSONObject obj = FileUtil.readJSONObjectFromFile(file);
         return obj.getString(CLIENT_ID_JSON_ATTR);
     }
 
     private void writeClientIdToFile(final File file, final String clientId) throws Exception {
         final JSONObject obj = new JSONObject();
         obj.put(CLIENT_ID_JSON_ATTR, clientId);