1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 package com.android.providers.contacts.util; 17 18 import android.test.suitebuilder.annotation.SmallTest; 19 20 import junit.framework.TestCase; 21 22 /** 23 * Run with: 24 atest /android/pi-dev/packages/providers/ContactsProvider/tests/src/com/android/providers/contacts/util/CappedStringBuilderTest.java 25 */ 26 @SmallTest 27 public class CappedStringBuilderTest extends TestCase { 28 public void testCappedChar() { 29 CappedStringBuilder csb = new CappedStringBuilder(8); 30 31 csb.append("abcd"); 32 csb.append("efgh"); 33 34 csb.append('x'); 35 assertEquals("abcdefgh", csb.toString()); 36 37 csb.append("y"); 38 csb.append("yz"); 39 40 assertEquals("abcdefgh", csb.toString()); 41 } 42 43 public void testCappedString() { 44 CappedStringBuilder csb = new CappedStringBuilder(8); 45 46 csb.append("abcd"); 47 csb.append("efgh"); 48 49 csb.append("x"); 50 assertEquals("abcdefgh", csb.toString()); 51 } 52 53 public void testClear() { 54 CappedStringBuilder csb = new CappedStringBuilder(8); 55 56 csb.append("abcd"); 57 csb.append("efgh"); 58 59 csb.append("x"); 60 61 assertEquals("abcdefgh", csb.toString()); 62 63 csb.clear(); 64 65 assertEquals("", csb.toString()); 66 67 csb.append("abcd"); 68 assertEquals("abcd", csb.toString()); 69 } 70 71 public void testAlreadyCapped() { 72 CappedStringBuilder csb = new CappedStringBuilder(4); 73 74 csb.append("abc"); 75 76 csb.append("xy"); 77 78 // Once capped, further append() will all be blocked. 79 csb.append('z'); 80 csb.append("z"); 81 82 assertEquals("abc", csb.toString()); 83 } 84 } 85