1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.chrome.browser.identity;
7 import android.test.InstrumentationTestCase;
8 import android.test.suitebuilder.annotation.SmallTest;
10 import junit.framework.Assert;
12 import org.chromium.base.test.util.AdvancedMockContext;
13 import org.chromium.base.test.util.Feature;
15 public class UuidBasedUniqueIdentificationGeneratorTest extends InstrumentationTestCase {
16 private static final String FLAG_UUID = "uuid";
18 private AdvancedMockContext mContext;
21 protected void setUp() throws Exception {
23 mContext = new AdvancedMockContext(getInstrumentation().getTargetContext());
28 public void testGenerationAndRestorationOfUuid() {
29 String preferenceKey = "some_preference_key";
30 String expectedUniqueId = "myUuid";
31 TestGenerator generator = new TestGenerator(mContext, preferenceKey, expectedUniqueId);
33 // Get a unique ID and ensure it is as expected.
34 Assert.assertEquals(expectedUniqueId, generator.getUniqueId(null));
36 // Asking for a unique ID again, should not try to regenerate it.
37 mContext.clearFlag(FLAG_UUID);
38 Assert.assertEquals(expectedUniqueId, generator.getUniqueId(null));
39 assertFalse(mContext.isFlagSet(FLAG_UUID));
41 // After a restart, the TestGenerator should read the UUID from a preference, instead of
43 mContext.clearFlag(FLAG_UUID);
44 generator = new TestGenerator(mContext, preferenceKey, null);
45 Assert.assertEquals(expectedUniqueId, generator.getUniqueId(null));
46 assertFalse(mContext.isFlagSet(FLAG_UUID));
51 public void testTwoDifferentGeneratorsShouldUseDifferentPreferences() {
52 String preferenceKey1 = "some_preference_key";
53 String preferenceKey2 = "some_other_preference_key";
54 String expectedUniqueId1 = "myUuid";
55 String expectedUniqueId2 = "myOtherUuid";
56 TestGenerator generator1 = new TestGenerator(mContext, preferenceKey1, expectedUniqueId1);
57 TestGenerator generator2 = new TestGenerator(mContext, preferenceKey2, expectedUniqueId2);
59 // Get a unique ID and ensure it is as expected.
60 Assert.assertEquals(expectedUniqueId1, generator1.getUniqueId(null));
61 Assert.assertEquals(expectedUniqueId2, generator2.getUniqueId(null));
63 // Asking for a unique ID again, should not try to regenerate it.
64 mContext.clearFlag(FLAG_UUID);
65 Assert.assertEquals(expectedUniqueId1, generator1.getUniqueId(null));
66 assertFalse(mContext.isFlagSet(FLAG_UUID));
67 mContext.clearFlag(FLAG_UUID);
68 Assert.assertEquals(expectedUniqueId2, generator2.getUniqueId(null));
69 assertFalse(mContext.isFlagSet(FLAG_UUID));
72 private static class TestGenerator extends UuidBasedUniqueIdentificationGenerator {
73 private final AdvancedMockContext mContext;
74 private final String mUuid;
76 TestGenerator(AdvancedMockContext context, String preferenceKey, String uuid) {
77 super(context, preferenceKey);
84 mContext.setFlag(FLAG_UUID);