Bug 1261713 - (Part 1) Add unit test for DeviceUuidFactory.java. r=sebastian
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Sun, 03 Apr 2016 17:58:12 -0400
changeset 347691 ac533e9899b77da4dcb3683440a789cdf06b4fab
parent 347690 43138b644a40880ef532d6fb8e43ef16b446d266
child 347692 ae1f2e223f22a0961192e4acf85280fdb445621e
push id14647
push userbmo:ahunt@mozilla.com
push dateTue, 05 Apr 2016 18:15: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,