1 // Copyright 2019 The Chromium Authors 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.base.shared_preferences; 6 7 import androidx.test.filters.SmallTest; 8 9 import org.junit.Before; 10 import org.junit.Test; 11 import org.junit.runner.RunWith; 12 13 import org.chromium.base.test.BaseRobolectricTestRunner; 14 15 import java.util.Arrays; 16 import java.util.List; 17 18 /** Unit tests for {@link StrictPreferenceKeyChecker}. */ 19 @RunWith(BaseRobolectricTestRunner.class) 20 public class StrictPreferenceKeyCheckerTest { 21 private static final String KEY1_IN_USE = "Chrome.Feature.Key1"; 22 private static final String KEY2_IN_USE = "Chrome.Feature.Key2"; 23 private static final String KEY3_NOT_IN_USE = "Chrome.Feature.Key3"; 24 private static final KeyPrefix KEY_PREFIX1_IN_USE = 25 new KeyPrefix("Chrome.Feature.KeyPrefix1.*"); 26 private static final KeyPrefix KEY_PREFIX2_IN_USE = 27 new KeyPrefix("Chrome.Feature.KeyPrefix2.*"); 28 private static final KeyPrefix KEY_PREFIX3_NOT_IN_USE = 29 new KeyPrefix("Chrome.Feature.KeyPrefix3.*"); 30 private static final String LEGACY_KEY_IN_USE = "legacykey"; 31 private static final String LEGACY_PREFIX_IN_USE = "legacyprefix_"; 32 33 private StrictPreferenceKeyChecker mSubject; 34 35 @Before setUp()36 public void setUp() { 37 List<String> keysInUse = 38 Arrays.asList( 39 KEY1_IN_USE, 40 KEY2_IN_USE, 41 KEY_PREFIX1_IN_USE.pattern(), 42 KEY_PREFIX2_IN_USE.pattern()); 43 List<String> legacyKeys = Arrays.asList(LEGACY_KEY_IN_USE); 44 List<KeyPrefix> legacyPrefixes = Arrays.asList(new KeyPrefix(LEGACY_PREFIX_IN_USE + "*")); 45 PreferenceKeyRegistry registry = 46 new PreferenceKeyRegistry("testModule", keysInUse, legacyKeys, legacyPrefixes); 47 mSubject = new StrictPreferenceKeyChecker(registry); 48 } 49 50 @Test 51 @SmallTest testRegularKeys_registered_noException()52 public void testRegularKeys_registered_noException() { 53 mSubject.checkIsKeyInUse(KEY1_IN_USE); 54 mSubject.checkIsKeyInUse(KEY2_IN_USE); 55 mSubject.checkIsKeyInUse(LEGACY_KEY_IN_USE); 56 mSubject.checkIsKeyInUse(LEGACY_PREFIX_IN_USE + "restofkey"); 57 } 58 59 @Test(expected = RuntimeException.class) 60 @SmallTest testRegularKeys_notRegistered_throwsException()61 public void testRegularKeys_notRegistered_throwsException() { 62 mSubject.checkIsKeyInUse(KEY3_NOT_IN_USE); 63 } 64 65 @Test 66 @SmallTest testPrefixedKeys_noException()67 public void testPrefixedKeys_noException() { 68 mSubject.checkIsKeyInUse(KEY_PREFIX1_IN_USE.createKey("restofkey")); 69 } 70 71 @Test 72 @SmallTest testPrefixedKeys_multipleLevels_noException()73 public void testPrefixedKeys_multipleLevels_noException() { 74 mSubject.checkIsKeyInUse( 75 KEY_PREFIX2_IN_USE.createKey("ExtraLevel.DynamicallyGenerated98765")); 76 } 77 78 @Test(expected = RuntimeException.class) 79 @SmallTest testPrefixedKeys_noPrefixMatch_throwsException()80 public void testPrefixedKeys_noPrefixMatch_throwsException() { 81 mSubject.checkIsKeyInUse(KEY_PREFIX3_NOT_IN_USE.createKey("restofkey")); 82 } 83 84 @Test(expected = RuntimeException.class) 85 @SmallTest testPrefixedKeys_matchOnlyPrefix_throwsException()86 public void testPrefixedKeys_matchOnlyPrefix_throwsException() { 87 mSubject.checkIsKeyInUse(KEY_PREFIX1_IN_USE.createKey("")); 88 } 89 90 @Test(expected = RuntimeException.class) 91 @SmallTest testPrefixedKeys_matchPattern_throwsException()92 public void testPrefixedKeys_matchPattern_throwsException() { 93 mSubject.checkIsKeyInUse(KEY_PREFIX1_IN_USE.createKey("*")); 94 } 95 96 @Test 97 @SmallTest testPrefix_inUse_noException()98 public void testPrefix_inUse_noException() { 99 mSubject.checkIsPrefixInUse(KEY_PREFIX2_IN_USE); 100 } 101 102 @Test(expected = RuntimeException.class) 103 @SmallTest testPrefix_notInUse_throwsException()104 public void testPrefix_notInUse_throwsException() { 105 mSubject.checkIsPrefixInUse(KEY_PREFIX3_NOT_IN_USE); 106 } 107 } 108