1/* 2 * Copyright (C) 2010 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 17package com.android.email.mail.store.imap; 18 19import static com.android.email.mail.store.imap.ImapTestUtils.*; 20 21import com.android.email.mail.store.imap.ImapElement; 22import com.android.email.mail.store.imap.ImapList; 23import com.android.email.mail.store.imap.ImapSimpleString; 24import com.android.email.mail.store.imap.ImapString; 25 26import android.test.suitebuilder.annotation.SmallTest; 27 28import junit.framework.TestCase; 29 30@SmallTest 31public class ImapListTest extends TestCase { 32 33 /** 34 * Test for small functions. (isList, isString, isEmpty and size) 35 */ 36 public void testBasics() { 37 ImapList list = new ImapList(); 38 39 assertTrue(list.isList()); 40 assertFalse(list.isString()); 41 42 assertTrue(list.isEmpty()); 43 assertEquals(0, list.size()); 44 45 list.add(STRING_1); 46 assertFalse(list.isEmpty()); 47 assertEquals(1, list.size()); 48 49 list.add(STRING_2); 50 assertEquals(2, list.size()); 51 52 list.add(LIST_1); 53 assertEquals(3, list.size()); 54 } 55 56 /** 57 * Test for {@link ImapList#EMPTY}. 58 */ 59 public void testEmpty() { 60 assertTrue(ImapList.EMPTY.isEmpty()); 61 } 62 63 public void testIs() { 64 final ImapString ABC = new ImapSimpleString("AbC"); 65 ImapList list = buildList(ImapList.EMPTY, ABC, LIST_1, ImapString.EMPTY); 66 67 assertFalse(list.is(0, "abc")); 68 assertFalse(list.is(1, "ab")); 69 assertTrue (list.is(1, "abc")); 70 assertFalse(list.is(2, "abc")); 71 assertFalse(list.is(3, "abc")); 72 assertFalse(list.is(4, "abc")); 73 74 assertFalse(list.is(0, "ab", false)); 75 assertFalse(list.is(1, "ab", false)); 76 assertTrue (list.is(1, "abc", false)); 77 assertFalse(list.is(2, "ab", false)); 78 assertFalse(list.is(3, "ab", false)); 79 assertFalse(list.is(4, "ab", false)); 80 81 assertFalse(list.is(0, "ab", true)); 82 assertTrue (list.is(1, "ab", true)); 83 assertTrue (list.is(1, "abc", true)); 84 assertFalse(list.is(2, "ab", true)); 85 assertFalse(list.is(3, "ab", true)); 86 assertFalse(list.is(4, "ab", true)); 87 88 // Make sure null is okay 89 assertFalse(list.is(0, null, false)); 90 91 // Make sure won't crash with empty list 92 assertFalse(ImapList.EMPTY.is(0, "abc")); 93 } 94 95 public void testGetElementOrNone() { 96 ImapList list = buildList(ImapList.EMPTY, STRING_1, LIST_1, ImapString.EMPTY); 97 98 assertElement(ImapList.EMPTY, list.getElementOrNone(0)); 99 assertElement(STRING_1, list.getElementOrNone(1)); 100 assertElement(LIST_1, list.getElementOrNone(2)); 101 assertElement(ImapString.EMPTY, list.getElementOrNone(3)); 102 assertElement(ImapElement.NONE, list.getElementOrNone(4)); // Out of index. 103 104 // Make sure won't crash with empty list 105 assertElement(ImapElement.NONE, ImapList.EMPTY.getElementOrNone(0)); 106 } 107 108 public void testGetListOrEmpty() { 109 ImapList list = buildList(ImapList.EMPTY, STRING_1, LIST_1, ImapString.EMPTY); 110 111 assertElement(ImapList.EMPTY, list.getListOrEmpty(0)); 112 assertElement(ImapList.EMPTY, list.getListOrEmpty(1)); 113 assertElement(LIST_1, list.getListOrEmpty(2)); 114 assertElement(ImapList.EMPTY, list.getListOrEmpty(3)); 115 assertElement(ImapList.EMPTY, list.getListOrEmpty(4)); // Out of index. 116 117 // Make sure won't crash with empty list 118 assertElement(ImapList.EMPTY, ImapList.EMPTY.getListOrEmpty(0)); 119 } 120 121 public void testGetStringOrEmpty() { 122 ImapList list = buildList(ImapList.EMPTY, STRING_1, LIST_1, ImapString.EMPTY); 123 124 assertElement(ImapString.EMPTY, list.getStringOrEmpty(0)); 125 assertElement(STRING_1, list.getStringOrEmpty(1)); 126 assertElement(ImapString.EMPTY, list.getStringOrEmpty(2)); 127 assertElement(ImapString.EMPTY, list.getStringOrEmpty(3)); 128 assertElement(ImapString.EMPTY, list.getStringOrEmpty(4)); // Out of index. 129 130 // Make sure won't crash with empty list 131 assertElement(ImapString.EMPTY, ImapList.EMPTY.getStringOrEmpty(0)); 132 } 133 134 public void testGetKeyedElementOrNull() { 135 final ImapString K1 = new ImapSimpleString("aBCd"); 136 final ImapString K2 = new ImapSimpleString("Def"); 137 final ImapString K3 = new ImapSimpleString("abC"); 138 139 ImapList list = buildList( 140 K1, STRING_1, 141 K2, K3, 142 K3, STRING_2); 143 144 assertElement(null, list.getKeyedElementOrNull("ab", false)); 145 assertElement(STRING_1, list.getKeyedElementOrNull("abcd", false)); 146 assertElement(K3, list.getKeyedElementOrNull("def", false)); 147 assertElement(STRING_2, list.getKeyedElementOrNull("abc", false)); 148 149 assertElement(STRING_1, list.getKeyedElementOrNull("ab", true)); 150 assertElement(STRING_1, list.getKeyedElementOrNull("abcd", true)); 151 assertElement(K3, list.getKeyedElementOrNull("def", true)); 152 assertElement(STRING_1, list.getKeyedElementOrNull("abc", true)); 153 154 // Make sure null is okay 155 assertElement(null, list.getKeyedElementOrNull(null, false)); 156 157 // Make sure won't crash with empty list 158 assertNull(ImapList.EMPTY.getKeyedElementOrNull("ab", false)); 159 160 // Shouldn't crash with a list with an odd number of elements. 161 assertElement(null, buildList(K1).getKeyedElementOrNull("abcd", false)); 162 } 163 164 public void getKeyedListOrEmpty() { 165 final ImapString K1 = new ImapSimpleString("Key"); 166 ImapList list = buildList(K1, LIST_1); 167 168 assertElement(LIST_1, list.getKeyedListOrEmpty("key", false)); 169 assertElement(LIST_1, list.getKeyedListOrEmpty("key", true)); 170 assertElement(ImapList.EMPTY, list.getKeyedListOrEmpty("ke", false)); 171 assertElement(LIST_1, list.getKeyedListOrEmpty("ke", true)); 172 173 assertElement(ImapList.EMPTY, list.getKeyedListOrEmpty("ke")); 174 assertElement(LIST_1, list.getKeyedListOrEmpty("key")); 175 } 176 177 public void getKeyedStringOrEmpty() { 178 final ImapString K1 = new ImapSimpleString("Key"); 179 ImapList list = buildList(K1, STRING_1); 180 181 assertElement(STRING_1, list.getKeyedListOrEmpty("key", false)); 182 assertElement(STRING_1, list.getKeyedListOrEmpty("key", true)); 183 assertElement(ImapString.EMPTY, list.getKeyedListOrEmpty("ke", false)); 184 assertElement(STRING_1, list.getKeyedListOrEmpty("ke", true)); 185 186 assertElement(ImapString.EMPTY, list.getKeyedListOrEmpty("ke")); 187 assertElement(STRING_1, list.getKeyedListOrEmpty("key")); 188 } 189 190 public void testContains() { 191 final ImapString K1 = new ImapSimpleString("aBCd"); 192 final ImapString K2 = new ImapSimpleString("Def"); 193 final ImapString K3 = new ImapSimpleString("abC"); 194 195 ImapList list = buildList(K1, K2, K3); 196 197 assertTrue(list.contains("abc")); 198 assertTrue(list.contains("abcd")); 199 assertTrue(list.contains("def")); 200 assertFalse(list.contains("")); 201 assertFalse(list.contains("a")); 202 assertFalse(list.contains(null)); 203 204 // Make sure null is okay 205 assertFalse(list.contains(null)); 206 207 // Make sure won't crash with empty list 208 assertFalse(ImapList.EMPTY.contains(null)); 209 } 210 211 public void testFlatten() { 212 assertEquals("[]", ImapList.EMPTY.flatten()); 213 assertEquals("[aBc]", buildList(STRING_1).flatten()); 214 assertEquals("[[]]", buildList(ImapList.EMPTY).flatten()); 215 assertEquals("[aBc,[,X y z],aBc]", 216 buildList(STRING_1, buildList(ImapString.EMPTY, STRING_2), STRING_1).flatten()); 217 } 218} 219