From a8feb7b88f2f67d8a80762dc54d336f1ea3a22d3 Mon Sep 17 00:00:00 2001 From: Chiao Cheng Date: Wed, 31 Oct 2012 15:18:29 -0700 Subject: Moving dependencies of PhoneFavoriteFragment. Move common classes from Contacts to Contacts Common. Bug: 6993891 Change-Id: Idd0b3115df810090340d1b10b04eb698d5facfb6 --- .../contacts/common/format/FormatUtilsTests.java | 114 +++++++++++++++++++++ .../common/format/PrefixHighligherTest.java | 85 +++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 tests/src/com/android/contacts/common/format/FormatUtilsTests.java create mode 100644 tests/src/com/android/contacts/common/format/PrefixHighligherTest.java (limited to 'tests') diff --git a/tests/src/com/android/contacts/common/format/FormatUtilsTests.java b/tests/src/com/android/contacts/common/format/FormatUtilsTests.java new file mode 100644 index 00000000..b38019dc --- /dev/null +++ b/tests/src/com/android/contacts/common/format/FormatUtilsTests.java @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.contacts.common.format; + +import android.database.CharArrayBuffer; +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.SmallTest; + +/** + * Test cases for format utility methods. + */ +@SmallTest +public class FormatUtilsTests extends AndroidTestCase { + + public void testOverlapPoint() throws Exception { + assertEquals(2, FormatUtils.overlapPoint("abcde", "cdefg")); + assertEquals(-1, FormatUtils.overlapPoint("John Doe", "John Doe")); + assertEquals(5, FormatUtils.overlapPoint("John Doe", "Doe, John")); + assertEquals(-1, FormatUtils.overlapPoint("Mr. John Doe", "Mr. Doe, John")); + assertEquals(13, FormatUtils.overlapPoint("John Herbert Doe", "Doe, John Herbert")); + } + + public void testCopyToCharArrayBuffer() { + CharArrayBuffer charArrayBuffer = new CharArrayBuffer(20); + checkCopyToCharArrayBuffer(charArrayBuffer, null, 0); + checkCopyToCharArrayBuffer(charArrayBuffer, "", 0); + checkCopyToCharArrayBuffer(charArrayBuffer, "test", 4); + // Check that it works after copying something into it. + checkCopyToCharArrayBuffer(charArrayBuffer, "", 0); + checkCopyToCharArrayBuffer(charArrayBuffer, "test", 4); + checkCopyToCharArrayBuffer(charArrayBuffer, null, 0); + // This requires a resize of the actual buffer. + checkCopyToCharArrayBuffer(charArrayBuffer, "test test test test test", 24); + } + + public void testCharArrayBufferToString() { + checkCharArrayBufferToString(""); + checkCharArrayBufferToString("test"); + checkCharArrayBufferToString("test test test test test"); + } + + /** Checks that copying a string into a {@link CharArrayBuffer} and back works correctly. */ + private void checkCharArrayBufferToString(String text) { + CharArrayBuffer buffer = new CharArrayBuffer(20); + FormatUtils.copyToCharArrayBuffer(text, buffer); + assertEquals(text, FormatUtils.charArrayBufferToString(buffer)); + } + + /** + * Checks that copying into the char array buffer copies the values correctly. + */ + private void checkCopyToCharArrayBuffer(CharArrayBuffer buffer, String value, int length) { + FormatUtils.copyToCharArrayBuffer(value, buffer); + assertEquals(length, buffer.sizeCopied); + for (int index = 0; index < length; ++index) { + assertEquals(value.charAt(index), buffer.data[index]); + } + } + + public void testIndexOfWordPrefix_NullPrefix() { + assertEquals(-1, FormatUtils.indexOfWordPrefix("test", null)); + } + + public void testIndexOfWordPrefix_NullText() { + assertEquals(-1, FormatUtils.indexOfWordPrefix(null, "TE".toCharArray())); + } + + public void testIndexOfWordPrefix_MatchingPrefix() { + checkIndexOfWordPrefix("test", "TE", 0); + checkIndexOfWordPrefix("Test", "TE", 0); + checkIndexOfWordPrefix("TEst", "TE", 0); + checkIndexOfWordPrefix("TEST", "TE", 0); + checkIndexOfWordPrefix("a test", "TE", 2); + checkIndexOfWordPrefix("test test", "TE", 0); + checkIndexOfWordPrefix("a test test", "TE", 2); + } + + public void testIndexOfWordPrefix_NotMatchingPrefix() { + checkIndexOfWordPrefix("test", "TA", -1); + checkIndexOfWordPrefix("test type theme", "TA", -1); + checkIndexOfWordPrefix("atest retest pretest", "TEST", -1); + checkIndexOfWordPrefix("tes", "TEST", -1); + } + + public void testIndexOfWordPrefix_LowerCase() { + // The prefix match only works if the prefix is un upper case. + checkIndexOfWordPrefix("test", "te", -1); + } + + /** + * Checks that getting the index of a word prefix in the given text returns the expected index. + * + * @param text the text in which to look for the word + * @param wordPrefix the word prefix to look for + * @param expectedIndex the expected value to be returned by the function + */ + private void checkIndexOfWordPrefix(String text, String wordPrefix, int expectedIndex) { + assertEquals(expectedIndex, FormatUtils.indexOfWordPrefix(text, wordPrefix.toCharArray())); + } +} diff --git a/tests/src/com/android/contacts/common/format/PrefixHighligherTest.java b/tests/src/com/android/contacts/common/format/PrefixHighligherTest.java new file mode 100644 index 00000000..d57e5951 --- /dev/null +++ b/tests/src/com/android/contacts/common/format/PrefixHighligherTest.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.contacts.common.format; + +import android.test.suitebuilder.annotation.SmallTest; + +import junit.framework.TestCase; + +/** + * Unit tests for {@link com.android.contacts.common.format.PrefixHighlighter}. + */ +@SmallTest +public class PrefixHighligherTest extends TestCase { + private static final int TEST_PREFIX_HIGHLIGHT_COLOR = 0xFF0000; + + /** The object under test. */ + private PrefixHighlighter mPrefixHighlighter; + + @Override + protected void setUp() throws Exception { + super.setUp(); + mPrefixHighlighter = new PrefixHighlighter(TEST_PREFIX_HIGHLIGHT_COLOR); + } + + public void testApply_EmptyPrefix() { + CharSequence seq = mPrefixHighlighter.apply("", new char[0]); + SpannedTestUtils.assertNotSpanned(seq, ""); + + seq = mPrefixHighlighter.apply("test", new char[0]); + SpannedTestUtils.assertNotSpanned(seq, "test"); + } + + public void testSetText_MatchingPrefix() { + final char[] charArray = "TE".toCharArray(); + + CharSequence seq = mPrefixHighlighter.apply("test", charArray); + SpannedTestUtils.assertPrefixSpan(seq, 0, 1); + + seq = mPrefixHighlighter.apply("Test", charArray); + SpannedTestUtils.assertPrefixSpan(seq, 0, 1); + + seq = mPrefixHighlighter.apply("TEst", charArray); + SpannedTestUtils.assertPrefixSpan(seq, 0, 1); + + seq = mPrefixHighlighter.apply("a test", charArray); + SpannedTestUtils.assertPrefixSpan(seq, 2, 3); + } + + public void testSetText_NotMatchingPrefix() { + final CharSequence seq = mPrefixHighlighter.apply("test", "TA".toCharArray()); + SpannedTestUtils.assertNotSpanned(seq, "test"); + } + + public void testSetText_FirstMatch() { + final CharSequence seq = mPrefixHighlighter.apply("a test's tests are not tests", + "TE".toCharArray()); + SpannedTestUtils.assertPrefixSpan(seq, 2, 3); + } + + public void testSetText_NoMatchingMiddleOfWord() { + final char[] charArray = "TE".toCharArray(); + CharSequence seq = mPrefixHighlighter.apply("atest", charArray); + SpannedTestUtils.assertNotSpanned(seq, "atest"); + + seq = mPrefixHighlighter.apply("atest otest", charArray); + SpannedTestUtils.assertNotSpanned(seq, "atest otest"); + + seq = mPrefixHighlighter.apply("atest test", charArray); + SpannedTestUtils.assertPrefixSpan(seq, 6, 7); + } +} -- cgit v1.2.3