OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.identity; | 5 package org.chromium.chrome.browser.identity; |
6 | 6 |
| 7 import android.support.test.InstrumentationRegistry; |
7 import android.support.test.filters.SmallTest; | 8 import android.support.test.filters.SmallTest; |
8 import android.test.InstrumentationTestCase; | |
9 | 9 |
10 import junit.framework.Assert; | 10 import org.junit.Assert; |
| 11 import org.junit.Before; |
| 12 import org.junit.Test; |
| 13 import org.junit.runner.RunWith; |
11 | 14 |
12 import org.chromium.base.test.util.AdvancedMockContext; | 15 import org.chromium.base.test.util.AdvancedMockContext; |
13 import org.chromium.base.test.util.Feature; | 16 import org.chromium.base.test.util.Feature; |
| 17 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
14 | 18 |
15 public class UuidBasedUniqueIdentificationGeneratorTest extends InstrumentationT
estCase { | 19 @RunWith(ChromeJUnit4ClassRunner.class) |
| 20 public class UuidBasedUniqueIdentificationGeneratorTest { |
16 private static final String FLAG_UUID = "uuid"; | 21 private static final String FLAG_UUID = "uuid"; |
17 | 22 |
18 private AdvancedMockContext mContext; | 23 private AdvancedMockContext mContext; |
19 | 24 |
20 @Override | 25 @Before |
21 protected void setUp() throws Exception { | 26 public void setUp() throws Exception { |
22 super.setUp(); | 27 mContext = new AdvancedMockContext( |
23 mContext = new AdvancedMockContext(getInstrumentation().getTargetContext
()); | 28 InstrumentationRegistry.getInstrumentation().getTargetContext())
; |
24 } | 29 } |
25 | 30 |
| 31 @Test |
26 @SmallTest | 32 @SmallTest |
27 @Feature({"Sync"}) | 33 @Feature({"Sync"}) |
28 public void testGenerationAndRestorationOfUuid() { | 34 public void testGenerationAndRestorationOfUuid() { |
29 String preferenceKey = "some_preference_key"; | 35 String preferenceKey = "some_preference_key"; |
30 String expectedUniqueId = "myUuid"; | 36 String expectedUniqueId = "myUuid"; |
31 TestGenerator generator = new TestGenerator(mContext, preferenceKey, exp
ectedUniqueId); | 37 TestGenerator generator = new TestGenerator(mContext, preferenceKey, exp
ectedUniqueId); |
32 | 38 |
33 // Get a unique ID and ensure it is as expected. | 39 // Get a unique ID and ensure it is as expected. |
34 Assert.assertEquals(expectedUniqueId, generator.getUniqueId(null)); | 40 Assert.assertEquals(expectedUniqueId, generator.getUniqueId(null)); |
35 | 41 |
36 // Asking for a unique ID again, should not try to regenerate it. | 42 // Asking for a unique ID again, should not try to regenerate it. |
37 mContext.clearFlag(FLAG_UUID); | 43 mContext.clearFlag(FLAG_UUID); |
38 Assert.assertEquals(expectedUniqueId, generator.getUniqueId(null)); | 44 Assert.assertEquals(expectedUniqueId, generator.getUniqueId(null)); |
39 assertFalse(mContext.isFlagSet(FLAG_UUID)); | 45 Assert.assertFalse(mContext.isFlagSet(FLAG_UUID)); |
40 | 46 |
41 // After a restart, the TestGenerator should read the UUID from a prefer
ence, instead of | 47 // After a restart, the TestGenerator should read the UUID from a prefer
ence, instead of |
42 // asking for it. | 48 // asking for it. |
43 mContext.clearFlag(FLAG_UUID); | 49 mContext.clearFlag(FLAG_UUID); |
44 generator = new TestGenerator(mContext, preferenceKey, null); | 50 generator = new TestGenerator(mContext, preferenceKey, null); |
45 Assert.assertEquals(expectedUniqueId, generator.getUniqueId(null)); | 51 Assert.assertEquals(expectedUniqueId, generator.getUniqueId(null)); |
46 assertFalse(mContext.isFlagSet(FLAG_UUID)); | 52 Assert.assertFalse(mContext.isFlagSet(FLAG_UUID)); |
47 } | 53 } |
48 | 54 |
| 55 @Test |
49 @SmallTest | 56 @SmallTest |
50 @Feature({"Sync"}) | 57 @Feature({"Sync"}) |
51 public void testTwoDifferentGeneratorsShouldUseDifferentPreferences() { | 58 public void testTwoDifferentGeneratorsShouldUseDifferentPreferences() { |
52 String preferenceKey1 = "some_preference_key"; | 59 String preferenceKey1 = "some_preference_key"; |
53 String preferenceKey2 = "some_other_preference_key"; | 60 String preferenceKey2 = "some_other_preference_key"; |
54 String expectedUniqueId1 = "myUuid"; | 61 String expectedUniqueId1 = "myUuid"; |
55 String expectedUniqueId2 = "myOtherUuid"; | 62 String expectedUniqueId2 = "myOtherUuid"; |
56 TestGenerator generator1 = new TestGenerator(mContext, preferenceKey1, e
xpectedUniqueId1); | 63 TestGenerator generator1 = new TestGenerator(mContext, preferenceKey1, e
xpectedUniqueId1); |
57 TestGenerator generator2 = new TestGenerator(mContext, preferenceKey2, e
xpectedUniqueId2); | 64 TestGenerator generator2 = new TestGenerator(mContext, preferenceKey2, e
xpectedUniqueId2); |
58 | 65 |
59 // Get a unique ID and ensure it is as expected. | 66 // Get a unique ID and ensure it is as expected. |
60 Assert.assertEquals(expectedUniqueId1, generator1.getUniqueId(null)); | 67 Assert.assertEquals(expectedUniqueId1, generator1.getUniqueId(null)); |
61 Assert.assertEquals(expectedUniqueId2, generator2.getUniqueId(null)); | 68 Assert.assertEquals(expectedUniqueId2, generator2.getUniqueId(null)); |
62 | 69 |
63 // Asking for a unique ID again, should not try to regenerate it. | 70 // Asking for a unique ID again, should not try to regenerate it. |
64 mContext.clearFlag(FLAG_UUID); | 71 mContext.clearFlag(FLAG_UUID); |
65 Assert.assertEquals(expectedUniqueId1, generator1.getUniqueId(null)); | 72 Assert.assertEquals(expectedUniqueId1, generator1.getUniqueId(null)); |
66 assertFalse(mContext.isFlagSet(FLAG_UUID)); | 73 Assert.assertFalse(mContext.isFlagSet(FLAG_UUID)); |
67 mContext.clearFlag(FLAG_UUID); | 74 mContext.clearFlag(FLAG_UUID); |
68 Assert.assertEquals(expectedUniqueId2, generator2.getUniqueId(null)); | 75 Assert.assertEquals(expectedUniqueId2, generator2.getUniqueId(null)); |
69 assertFalse(mContext.isFlagSet(FLAG_UUID)); | 76 Assert.assertFalse(mContext.isFlagSet(FLAG_UUID)); |
70 } | 77 } |
71 | 78 |
72 private static class TestGenerator extends UuidBasedUniqueIdentificationGene
rator { | 79 private static class TestGenerator extends UuidBasedUniqueIdentificationGene
rator { |
73 private final AdvancedMockContext mContext; | 80 private final AdvancedMockContext mContext; |
74 private final String mUuid; | 81 private final String mUuid; |
75 | 82 |
76 TestGenerator(AdvancedMockContext context, String preferenceKey, String
uuid) { | 83 TestGenerator(AdvancedMockContext context, String preferenceKey, String
uuid) { |
77 super(context, preferenceKey); | 84 super(context, preferenceKey); |
78 mContext = context; | 85 mContext = context; |
79 mUuid = uuid; | 86 mUuid = uuid; |
80 } | 87 } |
81 | 88 |
82 @Override | 89 @Override |
83 String getUUID() { | 90 String getUUID() { |
84 mContext.setFlag(FLAG_UUID); | 91 mContext.setFlag(FLAG_UUID); |
85 return mUuid; | 92 return mUuid; |
86 } | 93 } |
87 } | 94 } |
88 } | 95 } |
OLD | NEW |