Bug 1261713 - (Part 1) Add unit test for DeviceUuidFactory.java. r=sebastian
MozReview-Commit-ID: 16iy9tvgg8x
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,