2009-03-04 03:32:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 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.email;
|
|
|
|
|
2010-06-30 00:17:12 +00:00
|
|
|
import com.android.email.Utility.NewFileCreator;
|
2009-09-02 19:41:16 +00:00
|
|
|
import com.android.email.provider.EmailContent.Mailbox;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2009-10-07 23:30:41 +00:00
|
|
|
import android.graphics.drawable.Drawable;
|
2010-04-14 23:49:35 +00:00
|
|
|
import android.telephony.TelephonyManager;
|
2010-05-28 19:53:46 +00:00
|
|
|
import android.test.AndroidTestCase;
|
2010-03-12 21:30:26 +00:00
|
|
|
import android.test.MoreAsserts;
|
2009-03-04 03:32:22 +00:00
|
|
|
import android.test.suitebuilder.annotation.SmallTest;
|
2010-04-14 23:49:35 +00:00
|
|
|
import android.util.Log;
|
2009-03-04 03:32:22 +00:00
|
|
|
|
2010-06-30 00:17:12 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2009-10-07 23:30:41 +00:00
|
|
|
import java.util.HashSet;
|
2010-06-30 00:17:12 +00:00
|
|
|
import java.util.Locale;
|
2009-10-07 23:30:41 +00:00
|
|
|
import java.util.Set;
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a series of unit tests for the Utility class. These tests must be locally
|
|
|
|
* complete - no server(s) required.
|
2010-05-20 20:06:22 +00:00
|
|
|
*
|
|
|
|
* You can run this entire test case with:
|
|
|
|
* runtest -c com.android.email.UtilityUnitTests email
|
2009-03-04 03:32:22 +00:00
|
|
|
*/
|
|
|
|
@SmallTest
|
2010-05-28 19:53:46 +00:00
|
|
|
public class UtilityUnitTests extends AndroidTestCase {
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests of the IMAP quoting rules function.
|
|
|
|
*/
|
|
|
|
public void testImapQuote() {
|
2010-06-30 00:17:12 +00:00
|
|
|
|
2009-03-04 03:32:22 +00:00
|
|
|
// Simple strings should come through with simple quotes
|
|
|
|
assertEquals("\"abcd\"", Utility.imapQuoted("abcd"));
|
2010-06-30 00:17:12 +00:00
|
|
|
|
2009-03-04 03:32:22 +00:00
|
|
|
// Quoting internal double quotes with \
|
|
|
|
assertEquals("\"ab\\\"cd\"", Utility.imapQuoted("ab\"cd"));
|
2010-06-30 00:17:12 +00:00
|
|
|
|
2009-03-04 03:32:22 +00:00
|
|
|
// Quoting internal \ with \\
|
|
|
|
assertEquals("\"ab\\\\cd\"", Utility.imapQuoted("ab\\cd"));
|
|
|
|
}
|
2009-09-21 19:57:04 +00:00
|
|
|
|
2009-09-02 19:41:16 +00:00
|
|
|
/**
|
|
|
|
* Tests of the syncronization of array and types of the display folder names
|
|
|
|
*/
|
|
|
|
public void testGetDisplayName() {
|
|
|
|
Context context = getContext();
|
|
|
|
String expect, name;
|
2009-09-21 19:57:04 +00:00
|
|
|
expect = context.getString(R.string.mailbox_name_display_inbox);
|
2009-09-02 19:41:16 +00:00
|
|
|
name = Utility.FolderProperties.getInstance(context).getDisplayName(Mailbox.TYPE_INBOX);
|
|
|
|
assertEquals(expect, name);
|
|
|
|
expect = null;
|
|
|
|
name = Utility.FolderProperties.getInstance(context).getDisplayName(Mailbox.TYPE_MAIL);
|
|
|
|
assertEquals(expect, name);
|
|
|
|
expect = null;
|
|
|
|
name = Utility.FolderProperties.getInstance(context).getDisplayName(Mailbox.TYPE_PARENT);
|
|
|
|
assertEquals(expect, name);
|
2009-09-21 19:57:04 +00:00
|
|
|
expect = context.getString(R.string.mailbox_name_display_drafts);
|
2009-09-02 19:41:16 +00:00
|
|
|
name = Utility.FolderProperties.getInstance(context).getDisplayName(Mailbox.TYPE_DRAFTS);
|
|
|
|
assertEquals(expect, name);
|
2009-09-21 19:57:04 +00:00
|
|
|
expect = context.getString(R.string.mailbox_name_display_outbox);
|
2009-09-02 19:41:16 +00:00
|
|
|
name = Utility.FolderProperties.getInstance(context).getDisplayName(Mailbox.TYPE_OUTBOX);
|
|
|
|
assertEquals(expect, name);
|
2009-09-21 19:57:04 +00:00
|
|
|
expect = context.getString(R.string.mailbox_name_display_sent);
|
2009-09-02 19:41:16 +00:00
|
|
|
name = Utility.FolderProperties.getInstance(context).getDisplayName(Mailbox.TYPE_SENT);
|
|
|
|
assertEquals(expect, name);
|
2009-09-21 19:57:04 +00:00
|
|
|
expect = context.getString(R.string.mailbox_name_display_trash);
|
2009-09-02 19:41:16 +00:00
|
|
|
name = Utility.FolderProperties.getInstance(context).getDisplayName(Mailbox.TYPE_TRASH);
|
|
|
|
assertEquals(expect, name);
|
2009-09-21 19:57:04 +00:00
|
|
|
expect = context.getString(R.string.mailbox_name_display_junk);
|
2009-09-02 19:41:16 +00:00
|
|
|
name = Utility.FolderProperties.getInstance(context).getDisplayName(Mailbox.TYPE_JUNK);
|
|
|
|
assertEquals(expect, name);
|
|
|
|
// Testing illegal index
|
|
|
|
expect = null;
|
|
|
|
name = Utility.FolderProperties.getInstance(context).getDisplayName(8);
|
|
|
|
assertEquals(expect, name);
|
|
|
|
}
|
2009-10-07 23:30:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Confirm that all of the special icons are available and unique
|
|
|
|
*/
|
|
|
|
public void testSpecialIcons() {
|
|
|
|
Utility.FolderProperties fp = Utility.FolderProperties.getInstance(mContext);
|
|
|
|
|
|
|
|
// Make sure they're available
|
|
|
|
Drawable inbox = fp.getIconIds(Mailbox.TYPE_INBOX);
|
|
|
|
Drawable mail = fp.getIconIds(Mailbox.TYPE_MAIL);
|
|
|
|
Drawable parent = fp.getIconIds(Mailbox.TYPE_PARENT);
|
|
|
|
Drawable drafts = fp.getIconIds(Mailbox.TYPE_DRAFTS);
|
|
|
|
Drawable outbox = fp.getIconIds(Mailbox.TYPE_OUTBOX);
|
|
|
|
Drawable sent = fp.getIconIds(Mailbox.TYPE_SENT);
|
|
|
|
Drawable trash = fp.getIconIds(Mailbox.TYPE_TRASH);
|
|
|
|
Drawable junk = fp.getIconIds(Mailbox.TYPE_JUNK);
|
|
|
|
|
|
|
|
// Make sure they're unique
|
|
|
|
Set<Drawable> set = new HashSet<Drawable>();
|
|
|
|
set.add(inbox);
|
|
|
|
set.add(mail);
|
|
|
|
set.add(parent);
|
|
|
|
set.add(drafts);
|
|
|
|
set.add(outbox);
|
|
|
|
set.add(sent);
|
|
|
|
set.add(trash);
|
|
|
|
set.add(junk);
|
|
|
|
assertEquals(8, set.size());
|
|
|
|
}
|
2010-03-12 21:30:26 +00:00
|
|
|
|
2010-03-24 18:05:14 +00:00
|
|
|
private static byte[] b(int... array) {
|
|
|
|
return TestUtils.b(array);
|
|
|
|
}
|
2010-03-12 21:30:26 +00:00
|
|
|
|
2010-03-24 18:05:14 +00:00
|
|
|
public void testToUtf8() {
|
2010-03-12 21:30:26 +00:00
|
|
|
assertNull(Utility.toUtf8(null));
|
|
|
|
MoreAsserts.assertEquals(new byte[] {}, Utility.toUtf8(""));
|
2010-03-24 18:05:14 +00:00
|
|
|
MoreAsserts.assertEquals(b('a'), Utility.toUtf8("a"));
|
|
|
|
MoreAsserts.assertEquals(b('A', 'B', 'C'), Utility.toUtf8("ABC"));
|
|
|
|
MoreAsserts.assertEquals(b(0xE6, 0x97, 0xA5, 0xE6, 0x9C, 0xAC, 0xE8, 0xAA, 0x9E),
|
2010-03-12 21:30:26 +00:00
|
|
|
Utility.toUtf8("\u65E5\u672C\u8A9E"));
|
|
|
|
}
|
2010-03-16 18:08:46 +00:00
|
|
|
|
2010-03-24 18:05:14 +00:00
|
|
|
public void testFromUtf8() {
|
|
|
|
assertNull(Utility.fromUtf8(null));
|
|
|
|
assertEquals("", Utility.fromUtf8(new byte[] {}));
|
|
|
|
assertEquals("a", Utility.fromUtf8(b('a')));
|
|
|
|
assertEquals("ABC", Utility.fromUtf8(b('A', 'B', 'C')));
|
|
|
|
assertEquals("\u65E5\u672C\u8A9E",
|
|
|
|
Utility.fromUtf8(b(0xE6, 0x97, 0xA5, 0xE6, 0x9C, 0xAC, 0xE8, 0xAA, 0x9E)));
|
|
|
|
}
|
|
|
|
|
2010-03-16 18:08:46 +00:00
|
|
|
public void testIsFirstUtf8Byte() {
|
|
|
|
// 1 byte in UTF-8.
|
|
|
|
checkIsFirstUtf8Byte("0"); // First 2 bits: 00
|
|
|
|
checkIsFirstUtf8Byte("A"); // First 2 bits: 01
|
|
|
|
|
|
|
|
checkIsFirstUtf8Byte("\u00A2"); // 2 bytes in UTF-8.
|
|
|
|
checkIsFirstUtf8Byte("\u20AC"); // 3 bytes in UTF-8.
|
|
|
|
checkIsFirstUtf8Byte("\uD852\uDF62"); // 4 bytes in UTF-8. (surrogate pair)
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkIsFirstUtf8Byte(String aChar) {
|
|
|
|
byte[] bytes = Utility.toUtf8(aChar);
|
|
|
|
assertTrue("0", Utility.isFirstUtf8Byte(bytes[0]));
|
|
|
|
for (int i = 1; i < bytes.length; i++) {
|
|
|
|
assertFalse(Integer.toString(i), Utility.isFirstUtf8Byte(bytes[i]));
|
|
|
|
}
|
|
|
|
}
|
2010-03-17 18:26:57 +00:00
|
|
|
|
|
|
|
public void testByteToHex() {
|
|
|
|
for (int i = 0; i <= 0xFF; i++) {
|
|
|
|
String hex = Utility.byteToHex((byte) i);
|
|
|
|
assertEquals("val=" + i, 2, hex.length());
|
|
|
|
assertEquals("val=" + i, i, Integer.parseInt(hex, 16));
|
|
|
|
}
|
|
|
|
}
|
2010-03-25 19:12:44 +00:00
|
|
|
|
|
|
|
public void testReplaceBareLfWithCrlf() {
|
|
|
|
assertEquals("", Utility.replaceBareLfWithCrlf(""));
|
|
|
|
assertEquals("", Utility.replaceBareLfWithCrlf("\r"));
|
|
|
|
assertEquals("\r\n", Utility.replaceBareLfWithCrlf("\r\n"));
|
|
|
|
assertEquals("\r\n", Utility.replaceBareLfWithCrlf("\n"));
|
|
|
|
assertEquals("\r\n\r\n\r\n", Utility.replaceBareLfWithCrlf("\n\n\n"));
|
|
|
|
assertEquals("A\r\nB\r\nC\r\nD", Utility.replaceBareLfWithCrlf("A\nB\r\nC\nD"));
|
|
|
|
}
|
2010-04-14 23:49:35 +00:00
|
|
|
|
|
|
|
public void testGetConsistentDeviceId() {
|
|
|
|
TelephonyManager tm =
|
|
|
|
(TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
|
|
|
|
if (tm == null) {
|
|
|
|
Log.w(Email.LOG_TAG, "TelephonyManager not supported. Skipping.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final String deviceId = Utility.getConsistentDeviceId(getContext());
|
|
|
|
assertNotNull(deviceId);
|
|
|
|
|
|
|
|
final String deviceId2 = Utility.getConsistentDeviceId(getContext());
|
|
|
|
// Should be consistent.
|
|
|
|
assertEquals(deviceId, deviceId2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetSmallSha1() {
|
|
|
|
byte[] sha1 = new byte[20];
|
|
|
|
|
|
|
|
// White box test. Not so great, but to make sure it may detect careless mistakes...
|
|
|
|
assertEquals(0, Utility.getSmallHashFromSha1(sha1));
|
|
|
|
|
|
|
|
for (int i = 0; i < sha1.length; i++) {
|
|
|
|
sha1[i] = (byte) 0xFF;
|
|
|
|
}
|
|
|
|
assertEquals(Integer.MAX_VALUE, Utility.getSmallHashFromSha1(sha1));
|
|
|
|
|
|
|
|
// Boundary check
|
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
sha1[19] = (byte) i;
|
|
|
|
Utility.getSmallHashFromSha1(sha1);
|
|
|
|
}
|
|
|
|
}
|
2010-04-23 01:03:00 +00:00
|
|
|
|
|
|
|
public void testCleanUpMimeDate() {
|
|
|
|
assertNull(Utility.cleanUpMimeDate(null));
|
|
|
|
assertEquals("", Utility.cleanUpMimeDate(""));
|
|
|
|
assertEquals("abc", Utility.cleanUpMimeDate("abc"));
|
|
|
|
assertEquals("GMT", Utility.cleanUpMimeDate("GMT"));
|
|
|
|
assertEquals("0000", Utility.cleanUpMimeDate("0000"));
|
|
|
|
assertEquals("-0000", Utility.cleanUpMimeDate("-0000"));
|
|
|
|
assertEquals("+1234", Utility.cleanUpMimeDate("GMT+1234"));
|
|
|
|
assertEquals("-1234", Utility.cleanUpMimeDate("GMT-1234"));
|
|
|
|
assertEquals("gmt-1234", Utility.cleanUpMimeDate("gmt-1234"));
|
|
|
|
assertEquals("GMT-123", Utility.cleanUpMimeDate("GMT-123"));
|
|
|
|
|
|
|
|
assertEquals("Thu, 10 Dec 09 15:08:08 -0700",
|
|
|
|
Utility.cleanUpMimeDate("Thu, 10 Dec 09 15:08:08 GMT-0700"));
|
|
|
|
assertEquals("Thu, 10 Dec 09 15:08:08 -0700",
|
|
|
|
Utility.cleanUpMimeDate("Thu, 10 Dec 09 15:08:08 -0700"));
|
|
|
|
}
|
2010-06-30 00:17:12 +00:00
|
|
|
|
|
|
|
public void testFormatSize() {
|
|
|
|
if (!"en".equalsIgnoreCase(Locale.getDefault().getLanguage())) {
|
|
|
|
return; // Only works on the EN locale.
|
|
|
|
}
|
|
|
|
assertEquals("0B", Utility.formatSize(getContext(), 0));
|
|
|
|
assertEquals("1B", Utility.formatSize(getContext(), 1));
|
|
|
|
assertEquals("1023B", Utility.formatSize(getContext(), 1023));
|
|
|
|
assertEquals("1KB", Utility.formatSize(getContext(), 1024));
|
|
|
|
assertEquals("1023KB", Utility.formatSize(getContext(), 1024 * 1024 - 1));
|
|
|
|
assertEquals("1MB", Utility.formatSize(getContext(), 1024 * 1024));
|
|
|
|
assertEquals("1023MB", Utility.formatSize(getContext(), 1024 * 1024 * 1024 - 1));
|
|
|
|
assertEquals("1GB", Utility.formatSize(getContext(), 1024 * 1024 * 1024));
|
|
|
|
assertEquals("5GB", Utility.formatSize(getContext(), 5L * 1024 * 1024 * 1024));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class MyNewFileCreator implements NewFileCreator {
|
|
|
|
private final HashSet<String> mExistingFileNames;
|
|
|
|
|
|
|
|
public MyNewFileCreator(String... fileNames) {
|
|
|
|
mExistingFileNames = new HashSet<String>();
|
|
|
|
for (String f : fileNames) {
|
|
|
|
mExistingFileNames.add(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override public boolean createNewFile(File f) {
|
|
|
|
return !mExistingFileNames.contains(f.getAbsolutePath());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testCreateUniqueFile() throws Exception {
|
|
|
|
final MyNewFileCreator noFiles = new MyNewFileCreator();
|
|
|
|
|
|
|
|
// Case 1: Files don't exist.
|
|
|
|
checkCreateUniqueFile("/a", noFiles, "/", "a");
|
|
|
|
checkCreateUniqueFile("/a.txt", noFiles, "/", "a.txt");
|
|
|
|
|
|
|
|
checkCreateUniqueFile("/a/b/a", noFiles, "/a/b", "a");
|
|
|
|
checkCreateUniqueFile("/a/b/a.txt", noFiles, "/a/b", "a.txt");
|
|
|
|
|
|
|
|
// Case 2: Files exist already.
|
|
|
|
final MyNewFileCreator files = new MyNewFileCreator(
|
|
|
|
"/a", "/a.txt", "/a/b/a", "/a/b/a.txt",
|
|
|
|
"/a-2.txt",
|
|
|
|
"/a/b/a-2", "/a/b/a-3",
|
|
|
|
"/a/b/a-2.txt", "/a/b/a-3.txt", "/a/b/a-4.txt"
|
|
|
|
);
|
|
|
|
|
|
|
|
checkCreateUniqueFile("/a-2", files, "/", "a");
|
|
|
|
checkCreateUniqueFile("/a-3.txt", files, "/", "a.txt");
|
|
|
|
|
|
|
|
checkCreateUniqueFile("/a/b/a-4", files, "/a/b", "a");
|
|
|
|
checkCreateUniqueFile("/a/b/a-5.txt", files, "/a/b", "a.txt");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkCreateUniqueFile(String expectedFileName, NewFileCreator nfc,
|
|
|
|
String dir, String fileName) throws Exception {
|
|
|
|
assertEquals(expectedFileName,
|
|
|
|
Utility.createUniqueFileInternal(nfc, new File(dir), fileName).toString());
|
|
|
|
}
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|