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 static org.junit.Assert.assertEquals; 8 import static org.junit.Assert.assertFalse; 9 import static org.junit.Assert.assertTrue; 10 11 import androidx.test.filters.SmallTest; 12 13 import org.junit.Test; 14 import org.junit.runner.RunWith; 15 16 import org.chromium.base.test.BaseRobolectricTestRunner; 17 18 /** Unit tests for {@link KeyPrefix}. */ 19 @RunWith(BaseRobolectricTestRunner.class) 20 public class KeyPrefixTest { 21 @Test 22 @SmallTest testSuccess_validPattern()23 public void testSuccess_validPattern() { 24 KeyPrefix prefix = new KeyPrefix("Chrome.Feature.KP.*"); 25 26 assertEquals(prefix.pattern(), "Chrome.Feature.KP.*"); 27 28 assertEquals(prefix.createKey("DynamicKey"), "Chrome.Feature.KP.DynamicKey"); 29 assertEquals(prefix.createKey("Level.DynamicKey"), "Chrome.Feature.KP.Level.DynamicKey"); 30 assertEquals(prefix.createKey(42), "Chrome.Feature.KP.42"); 31 32 assertTrue(prefix.hasGenerated("Chrome.Feature.KP.DynamicKey")); 33 assertTrue(prefix.hasGenerated("Chrome.Feature.KP.Level.DynamicKey")); 34 assertTrue(prefix.hasGenerated("Chrome.Feature.KP.42")); 35 assertFalse(prefix.hasGenerated("OtherKey")); 36 } 37 38 @Test 39 @SmallTest testSuccess_validLegacyPattern()40 public void testSuccess_validLegacyPattern() { 41 KeyPrefix prefix = new KeyPrefix("legacy_pattern_*"); 42 43 assertEquals(prefix.pattern(), "legacy_pattern_*"); 44 assertEquals(prefix.createKey("DynamicKey"), "legacy_pattern_DynamicKey"); 45 46 assertTrue(prefix.hasGenerated("legacy_pattern_DynamicKey")); 47 assertFalse(prefix.hasGenerated("OtherKey")); 48 } 49 50 @Test(expected = AssertionError.class) 51 @SmallTest testError_missingPeriod()52 public void testError_missingPeriod() { 53 new KeyPrefix("Chrome.Feature.KP"); 54 } 55 56 @Test(expected = AssertionError.class) 57 @SmallTest testError_missingStar()58 public void testError_missingStar() { 59 new KeyPrefix("Chrome.Feature.KP."); 60 } 61 } 62