1 /* 2 * Copyright (C) 2014 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.emailcommon.utility; 18 19 import android.test.AndroidTestCase; 20 import android.test.IsolatedContext; 21 import android.test.mock.MockContentResolver; 22 import android.test.suitebuilder.annotation.SmallTest; 23 import android.text.TextUtils; 24 25 import com.android.emailcommon.utility.Utility.NewFileCreator; 26 27 import java.io.File; 28 import java.io.IOException; 29 import java.text.ParseException; 30 import java.util.Calendar; 31 import java.util.GregorianCalendar; 32 import java.util.TimeZone; 33 34 @SmallTest 35 public class UtilityTest extends AndroidTestCase { 36 private void testParseDateTimesHelper(String date, int year, int month, 37 int day, int hour, int minute, int second) throws Exception { 38 GregorianCalendar cal = Utility.parseDateTimeToCalendar(date); 39 assertEquals(year, cal.get(Calendar.YEAR)); 40 assertEquals(month, cal.get(Calendar.MONTH) + 1); 41 assertEquals(day, cal.get(Calendar.DAY_OF_MONTH)); 42 assertEquals(hour, cal.get(Calendar.HOUR_OF_DAY)); 43 assertEquals(minute, cal.get(Calendar.MINUTE)); 44 assertEquals(second, cal.get(Calendar.SECOND)); 45 } 46 47 @SmallTest 48 public void testParseDateTimes() throws Exception { 49 testParseDateTimesHelper("20090211T180303Z", 2009, 2, 11, 18, 3, 3); 50 testParseDateTimesHelper("20090211", 2009, 2, 11, 0, 0, 0); 51 try { 52 testParseDateTimesHelper("200902", 0, 0, 0, 0, 0, 0); 53 fail("Expected ParseException"); 54 } catch (ParseException e) { 55 // expected 56 } 57 } 58 59 private void testParseEmailDateTimeHelper(String date, int year, int month, 60 int day, int hour, int minute, int second, int millis) throws Exception { 61 GregorianCalendar cal = new GregorianCalendar(year, month - 1, day, 62 hour, minute, second); 63 cal.setTimeZone(TimeZone.getTimeZone("GMT")); 64 long timeInMillis = Utility.parseEmailDateTimeToMillis(date); 65 assertEquals(cal.getTimeInMillis() + millis, timeInMillis); 66 } 67 68 @SmallTest 69 public void testParseEmailDateTime() throws Exception { 70 testParseEmailDateTimeHelper("2010-02-23T16:01:05.000Z", 71 2010, 2, 23, 16, 1, 5, 0); 72 testParseEmailDateTimeHelper("2009-02-11T18:03:31.123Z", 73 2009, 2, 11, 18, 3, 31, 123); 74 testParseEmailDateTimeHelper("2009-02-11", 75 2009, 2, 11, 0, 0, 0, 0); 76 try { 77 testParseEmailDateTimeHelper("2010-02", 1970, 1, 1, 0, 0, 0, 0); 78 fail("Expected ParseException"); 79 } catch (ParseException e) { 80 // expected 81 } 82 } 83 84 private static NewFileCreator getCountdownFileCreator() { 85 return new NewFileCreator() { 86 private int mCountdown = 5; 87 @Override 88 public boolean createNewFile(File f) throws IOException { 89 return mCountdown-- <= 0; 90 } 91 }; 92 } 93 94 private static NewFileCreator getTrueFileCreator() { 95 return new NewFileCreator() { 96 @Override 97 public boolean createNewFile(File f) throws IOException { 98 return true; 99 } 100 }; 101 } 102 103 @SmallTest 104 public void testCreateUniqueFileCompare() throws Exception { 105 final File directory = 106 new IsolatedContext(new MockContentResolver(), getContext()).getFilesDir(); 107 108 final File created1 = 109 Utility.createUniqueFileInternal(getCountdownFileCreator(), directory, "file"); 110 assertNotNull(created1); 111 assertFalse(TextUtils.equals(created1.getName(), "file")); 112 113 final File created2 = 114 Utility.createUniqueFileInternal(getTrueFileCreator(), directory, "file"); 115 assertNotNull(created2); 116 assertTrue(TextUtils.equals(created2.getName(), "file")); 117 118 final File created3 = 119 Utility.createUniqueFileInternal(getCountdownFileCreator(), directory, "file.ext"); 120 assertNotNull(created3); 121 assertFalse(TextUtils.equals(created3.getName(), "file.ext")); 122 123 final File created4 = 124 Utility.createUniqueFileInternal(getTrueFileCreator(), directory, "file.ext"); 125 assertNotNull(created4); 126 assertTrue(TextUtils.equals(created4.getName(), "file.ext")); 127 } 128 129 @SmallTest 130 public void testCreateUniqueFileWithPercent() throws Exception { 131 final File directory = 132 new IsolatedContext(new MockContentResolver(), getContext()).getFilesDir(); 133 134 final File created1 = 135 Utility.createUniqueFileInternal(getTrueFileCreator(), directory, "file%s"); 136 assertNotNull(created1); 137 138 final File created2 = 139 Utility.createUniqueFileInternal(getTrueFileCreator(), directory, "file%s.ext"); 140 assertNotNull(created2); 141 } 142 143 @SmallTest 144 public void testCreateUniqueFile() throws Exception { 145 final File directory = 146 new IsolatedContext(new MockContentResolver(), getContext()).getFilesDir(); 147 148 final File created1 = 149 Utility.createUniqueFileInternal(getTrueFileCreator(), directory, "file"); 150 assertNotNull(created1); 151 152 final File created2 = 153 Utility.createUniqueFileInternal(getCountdownFileCreator(), directory, "file"); 154 assertNotNull(created2); 155 156 final File created3 = 157 Utility.createUniqueFileInternal(getTrueFileCreator(), directory, "file.ext"); 158 assertNotNull(created3); 159 160 final File created4 = 161 Utility.createUniqueFileInternal(getCountdownFileCreator(), directory, "file.ext"); 162 assertNotNull(created4); 163 164 final File created5 = 165 Utility.createUniqueFileInternal(getTrueFileCreator(), directory, ".ext"); 166 assertNotNull(created5); 167 168 final File created6 = 169 Utility.createUniqueFileInternal(getCountdownFileCreator(), directory, ".ext"); 170 assertNotNull(created6); 171 172 final File created7 = 173 Utility.createUniqueFileInternal(getTrueFileCreator(), directory, "."); 174 assertNotNull(created7); 175 176 final File created8 = 177 Utility.createUniqueFileInternal(getCountdownFileCreator(), directory, "."); 178 assertNotNull(created8); 179 } 180 181 @SmallTest 182 public void testCreateUniqueFileExtensions() throws Exception { 183 final File directory = 184 new IsolatedContext(new MockContentResolver(), getContext()).getFilesDir(); 185 186 final File created1 = 187 Utility.createUniqueFileInternal(getTrueFileCreator(), directory, "file"); 188 assertNotNull(created1); 189 assertEquals(created1.getName().indexOf('.'), -1); 190 191 final File created2 = 192 Utility.createUniqueFileInternal(getCountdownFileCreator(), directory, "file"); 193 assertNotNull(created2); 194 assertEquals(created2.getName().indexOf('.'), -1); 195 196 final File created3 = 197 Utility.createUniqueFileInternal(getTrueFileCreator(), directory, "file.ext"); 198 assertNotNull(created3); 199 assertEquals(created3.getName().length() - created3.getName().lastIndexOf('.'), 4); 200 201 final File created4 = 202 Utility.createUniqueFileInternal(getCountdownFileCreator(), directory, "file.ext"); 203 assertNotNull(created4); 204 assertEquals(created4.getName().length() - created4.getName().lastIndexOf('.'), 4); 205 } 206 } 207