| 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. |
| 4 | |
| 5 | package org.chromium.chrome.browser.identity; |
| 6 | |
| 7 | import android.test.InstrumentationTestCase; |
| 8 | import android.test.suitebuilder.annotation.SmallTest; |
| 9 | |
| 10 | import junit.framework.Assert; |
| 11 | |
| 12 | import org.chromium.base.test.util.AdvancedMockContext; |
| 13 | import org.chromium.base.test.util.Feature; |
| 14 | |
| 15 | public class UuidBasedUniqueIdentificationGeneratorTest extends InstrumentationTestCase { |
| 16 | private static final String FLAG_UUID = "uuid"; |
| 17 | |
| 18 | private AdvancedMockContext mContext; |
| 19 | |
| 20 | @Override |
| 21 | protected void setUp() throws Exception { |
| 22 | super.setUp(); |
| 23 | mContext = new AdvancedMockContext(getInstrumentation().getTargetContext()); |
| 24 | } |
| 25 | |
| 26 | @SmallTest |
| 27 | @Feature({"Sync"}) |
| 28 | public void testGenerationAndRestorationOfUuid() { |
| 29 | String preferenceKey = "some_preference_key"; |
| 30 | String expectedUniqueId = "myUuid"; |
| 31 | TestGenerator generator = new TestGenerator(mContext, preferenceKey, expectedUniqueId); |
| 32 | |
| 33 | // Get a unique ID and ensure it is as expected. |
| 34 | Assert.assertEquals(expectedUniqueId, generator.getUniqueId(null)); |
| 35 | |
| 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)); |
| 40 | |
| 41 | // After a restart, the TestGenerator should read the UUID from a preference, instead of |
| 42 | // asking for it. |
| 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)); |
| 47 | } |
| 48 | |
| 49 | @SmallTest |
| 50 | @Feature({"Sync"}) |
| 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); |
| 58 | |
| 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)); |
| 62 | |
| 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)); |
| 70 | } |
| 71 | |
| 72 | private static class TestGenerator extends UuidBasedUniqueIdentificationGenerator { |
| 73 | private final AdvancedMockContext mContext; |
| 74 | private final String mUuid; |
| 75 | |
| 76 | TestGenerator(AdvancedMockContext context, String preferenceKey, String uuid) { |
| 77 | super(context, preferenceKey); |
| 78 | mContext = context; |
| 79 | mUuid = uuid; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | String getUUID() { |
| 84 | mContext.setFlag(FLAG_UUID); |
| 85 | return mUuid; |
| 86 | } |
| 87 | } |
| 88 | } |