1 /* 2 * Copyright (C) 2008 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 17 package com.android.email; 18 19 import com.android.email.provider.EmailContent.Mailbox; 20 21 import android.content.Context; 22 import android.graphics.drawable.Drawable; 23 import android.telephony.TelephonyManager; 24 import android.test.AndroidTestCase; 25 import android.test.MoreAsserts; 26 import android.test.suitebuilder.annotation.SmallTest; 27 import android.util.Log; 28 import android.widget.TextView; 29 30 import java.util.HashSet; 31 import java.util.Set; 32 33 /** 34 * This is a series of unit tests for the Utility class. These tests must be locally 35 * complete - no server(s) required. 36 */ 37 @SmallTest 38 public class UtilityUnitTests extends AndroidTestCase { 39 40 /** 41 * Tests of the IMAP quoting rules function. 42 */ testImapQuote()43 public void testImapQuote() { 44 45 // Simple strings should come through with simple quotes 46 assertEquals("\"abcd\"", Utility.imapQuoted("abcd")); 47 48 // Quoting internal double quotes with \ 49 assertEquals("\"ab\\\"cd\"", Utility.imapQuoted("ab\"cd")); 50 51 // Quoting internal \ with \\ 52 assertEquals("\"ab\\\\cd\"", Utility.imapQuoted("ab\\cd")); 53 } 54 55 /** 56 * Tests of the syncronization of array and types of the display folder names 57 */ testGetDisplayName()58 public void testGetDisplayName() { 59 Context context = getContext(); 60 String expect, name; 61 expect = context.getString(R.string.mailbox_name_display_inbox); 62 name = Utility.FolderProperties.getInstance(context).getDisplayName(Mailbox.TYPE_INBOX); 63 assertEquals(expect, name); 64 expect = null; 65 name = Utility.FolderProperties.getInstance(context).getDisplayName(Mailbox.TYPE_MAIL); 66 assertEquals(expect, name); 67 expect = null; 68 name = Utility.FolderProperties.getInstance(context).getDisplayName(Mailbox.TYPE_PARENT); 69 assertEquals(expect, name); 70 expect = context.getString(R.string.mailbox_name_display_drafts); 71 name = Utility.FolderProperties.getInstance(context).getDisplayName(Mailbox.TYPE_DRAFTS); 72 assertEquals(expect, name); 73 expect = context.getString(R.string.mailbox_name_display_outbox); 74 name = Utility.FolderProperties.getInstance(context).getDisplayName(Mailbox.TYPE_OUTBOX); 75 assertEquals(expect, name); 76 expect = context.getString(R.string.mailbox_name_display_sent); 77 name = Utility.FolderProperties.getInstance(context).getDisplayName(Mailbox.TYPE_SENT); 78 assertEquals(expect, name); 79 expect = context.getString(R.string.mailbox_name_display_trash); 80 name = Utility.FolderProperties.getInstance(context).getDisplayName(Mailbox.TYPE_TRASH); 81 assertEquals(expect, name); 82 expect = context.getString(R.string.mailbox_name_display_junk); 83 name = Utility.FolderProperties.getInstance(context).getDisplayName(Mailbox.TYPE_JUNK); 84 assertEquals(expect, name); 85 // Testing illegal index 86 expect = null; 87 name = Utility.FolderProperties.getInstance(context).getDisplayName(8); 88 assertEquals(expect, name); 89 } 90 91 /** 92 * Confirm that all of the special icons are available and unique 93 */ testSpecialIcons()94 public void testSpecialIcons() { 95 Utility.FolderProperties fp = Utility.FolderProperties.getInstance(mContext); 96 97 // Make sure they're available 98 Drawable inbox = fp.getIconIds(Mailbox.TYPE_INBOX); 99 Drawable mail = fp.getIconIds(Mailbox.TYPE_MAIL); 100 Drawable parent = fp.getIconIds(Mailbox.TYPE_PARENT); 101 Drawable drafts = fp.getIconIds(Mailbox.TYPE_DRAFTS); 102 Drawable outbox = fp.getIconIds(Mailbox.TYPE_OUTBOX); 103 Drawable sent = fp.getIconIds(Mailbox.TYPE_SENT); 104 Drawable trash = fp.getIconIds(Mailbox.TYPE_TRASH); 105 Drawable junk = fp.getIconIds(Mailbox.TYPE_JUNK); 106 107 // Make sure they're unique 108 Set<Drawable> set = new HashSet<Drawable>(); 109 set.add(inbox); 110 set.add(mail); 111 set.add(parent); 112 set.add(drafts); 113 set.add(outbox); 114 set.add(sent); 115 set.add(trash); 116 set.add(junk); 117 assertEquals(8, set.size()); 118 } 119 b(int... array)120 private static byte[] b(int... array) { 121 return TestUtils.b(array); 122 } 123 testToUtf8()124 public void testToUtf8() { 125 assertNull(Utility.toUtf8(null)); 126 MoreAsserts.assertEquals(new byte[] {}, Utility.toUtf8("")); 127 MoreAsserts.assertEquals(b('a'), Utility.toUtf8("a")); 128 MoreAsserts.assertEquals(b('A', 'B', 'C'), Utility.toUtf8("ABC")); 129 MoreAsserts.assertEquals(b(0xE6, 0x97, 0xA5, 0xE6, 0x9C, 0xAC, 0xE8, 0xAA, 0x9E), 130 Utility.toUtf8("\u65E5\u672C\u8A9E")); 131 } 132 testFromUtf8()133 public void testFromUtf8() { 134 assertNull(Utility.fromUtf8(null)); 135 assertEquals("", Utility.fromUtf8(new byte[] {})); 136 assertEquals("a", Utility.fromUtf8(b('a'))); 137 assertEquals("ABC", Utility.fromUtf8(b('A', 'B', 'C'))); 138 assertEquals("\u65E5\u672C\u8A9E", 139 Utility.fromUtf8(b(0xE6, 0x97, 0xA5, 0xE6, 0x9C, 0xAC, 0xE8, 0xAA, 0x9E))); 140 } 141 testIsFirstUtf8Byte()142 public void testIsFirstUtf8Byte() { 143 // 1 byte in UTF-8. 144 checkIsFirstUtf8Byte("0"); // First 2 bits: 00 145 checkIsFirstUtf8Byte("A"); // First 2 bits: 01 146 147 checkIsFirstUtf8Byte("\u00A2"); // 2 bytes in UTF-8. 148 checkIsFirstUtf8Byte("\u20AC"); // 3 bytes in UTF-8. 149 checkIsFirstUtf8Byte("\uD852\uDF62"); // 4 bytes in UTF-8. (surrogate pair) 150 } 151 checkIsFirstUtf8Byte(String aChar)152 private void checkIsFirstUtf8Byte(String aChar) { 153 byte[] bytes = Utility.toUtf8(aChar); 154 assertTrue("0", Utility.isFirstUtf8Byte(bytes[0])); 155 for (int i = 1; i < bytes.length; i++) { 156 assertFalse(Integer.toString(i), Utility.isFirstUtf8Byte(bytes[i])); 157 } 158 } 159 testByteToHex()160 public void testByteToHex() { 161 for (int i = 0; i <= 0xFF; i++) { 162 String hex = Utility.byteToHex((byte) i); 163 assertEquals("val=" + i, 2, hex.length()); 164 assertEquals("val=" + i, i, Integer.parseInt(hex, 16)); 165 } 166 } 167 testReplaceBareLfWithCrlf()168 public void testReplaceBareLfWithCrlf() { 169 assertEquals("", Utility.replaceBareLfWithCrlf("")); 170 assertEquals("", Utility.replaceBareLfWithCrlf("\r")); 171 assertEquals("\r\n", Utility.replaceBareLfWithCrlf("\r\n")); 172 assertEquals("\r\n", Utility.replaceBareLfWithCrlf("\n")); 173 assertEquals("\r\n\r\n\r\n", Utility.replaceBareLfWithCrlf("\n\n\n")); 174 assertEquals("A\r\nB\r\nC\r\nD", Utility.replaceBareLfWithCrlf("A\nB\r\nC\nD")); 175 } 176 testGetConsistentDeviceId()177 public void testGetConsistentDeviceId() { 178 TelephonyManager tm = 179 (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE); 180 if (tm == null) { 181 Log.w(Email.LOG_TAG, "TelephonyManager not supported. Skipping."); 182 return; 183 } 184 final String deviceId = Utility.getConsistentDeviceId(getContext()); 185 assertNotNull(deviceId); 186 187 final String deviceId2 = Utility.getConsistentDeviceId(getContext()); 188 // Should be consistent. 189 assertEquals(deviceId, deviceId2); 190 } 191 testGetSmallSha1()192 public void testGetSmallSha1() { 193 byte[] sha1 = new byte[20]; 194 195 // White box test. Not so great, but to make sure it may detect careless mistakes... 196 assertEquals(0, Utility.getSmallHashFromSha1(sha1)); 197 198 for (int i = 0; i < sha1.length; i++) { 199 sha1[i] = (byte) 0xFF; 200 } 201 assertEquals(Integer.MAX_VALUE, Utility.getSmallHashFromSha1(sha1)); 202 203 // Boundary check 204 for (int i = 0; i < 16; i++) { 205 sha1[19] = (byte) i; 206 Utility.getSmallHashFromSha1(sha1); 207 } 208 } 209 testCleanUpMimeDate()210 public void testCleanUpMimeDate() { 211 assertNull(Utility.cleanUpMimeDate(null)); 212 assertEquals("", Utility.cleanUpMimeDate("")); 213 assertEquals("abc", Utility.cleanUpMimeDate("abc")); 214 assertEquals("GMT", Utility.cleanUpMimeDate("GMT")); 215 assertEquals("0000", Utility.cleanUpMimeDate("0000")); 216 assertEquals("-0000", Utility.cleanUpMimeDate("-0000")); 217 assertEquals("+1234", Utility.cleanUpMimeDate("GMT+1234")); 218 assertEquals("-1234", Utility.cleanUpMimeDate("GMT-1234")); 219 assertEquals("gmt-1234", Utility.cleanUpMimeDate("gmt-1234")); 220 assertEquals("GMT-123", Utility.cleanUpMimeDate("GMT-123")); 221 222 assertEquals("Thu, 10 Dec 09 15:08:08 -0700", 223 Utility.cleanUpMimeDate("Thu, 10 Dec 09 15:08:08 GMT-0700")); 224 assertEquals("Thu, 10 Dec 09 15:08:08 -0700", 225 Utility.cleanUpMimeDate("Thu, 10 Dec 09 15:08:08 -0700")); 226 } 227 testIsPortFieldValid()228 public void testIsPortFieldValid() { 229 TextView view = new TextView(getContext()); 230 // null, empty, negative, and non integer strings aren't valid 231 view.setText(null); 232 assertFalse(Utility.isPortFieldValid(view)); 233 view.setText(""); 234 assertFalse(Utility.isPortFieldValid(view)); 235 view.setText("-1"); 236 assertFalse(Utility.isPortFieldValid(view)); 237 view.setText("1403.75"); 238 assertFalse(Utility.isPortFieldValid(view)); 239 view.setText("0"); 240 assertFalse(Utility.isPortFieldValid(view)); 241 view.setText("65536"); 242 assertFalse(Utility.isPortFieldValid(view)); 243 view.setText("i'm not valid"); 244 assertFalse(Utility.isPortFieldValid(view)); 245 // These next values are valid 246 view.setText("1"); 247 assertTrue(Utility.isPortFieldValid(view)); 248 view.setText("65535"); 249 assertTrue(Utility.isPortFieldValid(view)); 250 } 251 } 252