Bug 1261713 - (Part 1) Add unit test for DeviceUuidFactory.java. r=sebastian draft
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Sun, 03 Apr 2016 17:58:12 -0400
changeset 347226 32030b578478639907541689b4f6af97793ffc0a
parent 347225 6ada4aeb9ce898057f6d66ac95b63bf8606abb21
child 347227 5413dc0d35c3a7f59780a835eed3236a27e3c8f4
push id14518
push usermleibovic@mozilla.com
push dateSun, 03 Apr 2016 22:28:38 +0000
reviewerssebastian
bugs1261713
milestone48.0a1
Bug 1261713 - (Part 1) Add unit test for DeviceUuidFactory.java. r=sebastian MozReview-Commit-ID: 16iy9tvgg8x
mobile/android/tests/background/junit4/src/com/keepsafe/switchboard/TestSwitchboard.java
mobile/android/thirdparty/com/keepsafe/switchboard/DeviceUuidFactory.java
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/background/junit4/src/com/keepsafe/switchboard/TestSwitchboard.java
@@ -0,0 +1,25 @@
+package com.keepsafe.switchboard;
+
+import android.content.Context;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mozilla.gecko.background.testhelpers.TestRunner;
+import org.robolectric.RuntimeEnvironment;
+
+import java.util.UUID;
+
+import static org.junit.Assert.*;
+
+@RunWith(TestRunner.class)
+public class TestSwitchboard {
+
+    @Test
+    public void testDeviceUuidFactory() {
+        final Context c = RuntimeEnvironment.application;
+        final DeviceUuidFactory df = new DeviceUuidFactory(c);
+        final UUID uuid = df.getDeviceUuid();
+        assertNotNull("UUID is not null", uuid);
+        assertEquals("DeviceUuidFactory always returns the same UUID", df.getDeviceUuid(), uuid);
+    }
+}
--- a/mobile/android/thirdparty/com/keepsafe/switchboard/DeviceUuidFactory.java
+++ b/mobile/android/thirdparty/com/keepsafe/switchboard/DeviceUuidFactory.java
@@ -14,54 +14,44 @@
    limitations under the License.
 */
 package com.keepsafe.switchboard;
 
 import java.util.UUID;
 
 import android.content.Context;
 import android.content.SharedPreferences;
-import android.preference.Preference;
-
 
 /**
  * Generates a UUID and stores is persistent as in the apps shared preferences.
  * 
  * @author Philipp Berner
  */
 public class DeviceUuidFactory {
     protected static final String PREFS_FILE = "com.keepsafe.switchboard.uuid";
     protected static final String PREFS_DEVICE_ID = "device_id";
 
     private static UUID uuid = null;
 
     public DeviceUuidFactory(Context context) {
-
         if (uuid == null) {
             synchronized (DeviceUuidFactory.class) {
                 if (uuid == null) {
                     final SharedPreferences prefs = context
                             .getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);
                     final String id = prefs.getString(PREFS_DEVICE_ID, null);
 
                     if (id != null) {
-                        // Use the ids previously computed and stored in the
-                        // prefs file
+                        // Use the ids previously computed and stored in the prefs file
                         uuid = UUID.fromString(id);
-
                     } else {
-
-                        UUID newId = UUID.randomUUID();
-                        uuid = newId;
+                        uuid = UUID.randomUUID();
 
                         // Write the value out to the prefs file
-                        prefs.edit()
-                                .putString(PREFS_DEVICE_ID, newId.toString())
-                                .commit();
-
+                        prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString()).apply();
                     }
                 }
             }
         }
     }
 
     /**
      * Returns a unique UUID for the current android device. As with all UUIDs,