Bug 1261713 - (Part 1) Add unit test for DeviceUuidFactory.java. r=sebastian

MozReview-Commit-ID: 16iy9tvgg8x

--HG--
extra : rebase_source : 2a10ae27b90738da6e39ec449c24590478302625
This commit is contained in:
Margaret Leibovic 2016-04-03 17:58:12 -04:00
parent c7d8ce7715
commit 895ef3571e
2 changed files with 28 additions and 13 deletions

View File

@ -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);
}
}

View File

@ -19,8 +19,6 @@ 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.
@ -34,7 +32,6 @@ public class DeviceUuidFactory {
private static UUID uuid = null;
public DeviceUuidFactory(Context context) {
if (uuid == null) {
synchronized (DeviceUuidFactory.class) {
if (uuid == null) {
@ -43,20 +40,13 @@ public class DeviceUuidFactory {
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();
}
}
}