Bug 1246816 - Add unit tests for profile creation date. r=sebastian a=ritu
authorMichael Comella <michael.l.comella@gmail.com>
Thu, 31 Mar 2016 15:22:09 -0700
changeset 324006 f96dd0b45c033d59e596f1ed1097ed3a71dc9eaf
parent 324005 6eb56cc6d62fc03c8129db68a4ade7b3ebc2e012
child 324007 5b11886851efd1ab4e012508f6b7151dc71972f6
push id5913
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 16:57:49 +0000
treeherdermozilla-beta@dcaf0a6fa115 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian, ritu
bugs1246816
milestone47.0a2
Bug 1246816 - Add unit tests for profile creation date. r=sebastian a=ritu 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);