• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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 android.text.format.cts;
18 
19 import dalvik.annotation.TestLevel;
20 import dalvik.annotation.TestTargetClass;
21 import dalvik.annotation.TestTargetNew;
22 import dalvik.annotation.TestTargets;
23 
24 import android.content.ContentResolver;
25 import android.content.Context;
26 import android.provider.Settings;
27 import android.test.AndroidTestCase;
28 import android.text.format.DateFormat;
29 
30 import java.text.ParseException;
31 import java.util.Calendar;
32 import java.util.Date;
33 import java.util.GregorianCalendar;
34 import java.util.Locale;
35 
36 @TestTargetClass(DateFormat.class)
37 public class DateFormatTest extends AndroidTestCase {
38 
39     private Context mContext;
40     private ContentResolver mContentResolver;
41     // Date: 12-18-2008 5:30AM
42     private static final int YEAR_FROM_1900 = 108;
43     private static final int YEAR = 2008;
44     private static final int MONTH = 11;
45     private static final int DAY = 18;
46     private static final int HOUR = 5;
47     private static final int MINUTE = 30;
48 
49     private boolean mIs24HourFormat;
50     private Locale mDefaultLocale;
51     private String mDefaultFormat;
52 
53     @Override
setUp()54     protected void setUp() throws Exception {
55         super.setUp();
56         mContext = getContext();
57         mContentResolver = mContext.getContentResolver();
58         mIs24HourFormat = DateFormat.is24HourFormat(mContext);
59         mDefaultLocale = Locale.getDefault();
60         mDefaultFormat = Settings.System.getString(mContext.getContentResolver(),
61                 Settings.System.DATE_FORMAT);
62     }
63 
64     @Override
tearDown()65     protected void tearDown() throws Exception {
66         if (!mIs24HourFormat) {
67             Settings.System.putString(mContentResolver, Settings.System.TIME_12_24, "12");
68         }
69         if (!Locale.getDefault().equals(mDefaultLocale)) {
70             Locale.setDefault(mDefaultLocale);
71         }
72         Settings.System.putString(mContentResolver, Settings.System.DATE_FORMAT, mDefaultFormat);
73         super.tearDown();
74     }
75 
76 
77     @TestTargetNew(
78         level = TestLevel.COMPLETE,
79         method = "is24HourFormat",
80         args = {Context.class}
81     )
testDateFormat()82     public void testDateFormat() {
83         Settings.System.putString(mContentResolver, Settings.System.TIME_12_24, "24");
84         assertTrue(DateFormat.is24HourFormat(mContext));
85         Settings.System.putString(mContentResolver, Settings.System.TIME_12_24, "12");
86         assertFalse(DateFormat.is24HourFormat(mContext));
87     }
88 
89     @TestTargets({
90         @TestTargetNew(
91             level = TestLevel.COMPLETE,
92             method = "getTimeFormat",
93             args = {Context.class}
94         ),
95         @TestTargetNew(
96             level = TestLevel.COMPLETE,
97             method = "getDateFormat",
98             args = {Context.class}
99         ),
100         @TestTargetNew(
101             level = TestLevel.COMPLETE,
102             method = "getLongDateFormat",
103             args = {Context.class}
104         ),
105         @TestTargetNew(
106             level = TestLevel.COMPLETE,
107             method = "getMediumDateFormat",
108             args = {Context.class}
109         ),
110         @TestTargetNew(
111             level = TestLevel.COMPLETE,
112             method = "getDateFormatOrder",
113             args = {Context.class}
114         ),
115         @TestTargetNew(
116             level = TestLevel.COMPLETE,
117             method = "format",
118             args = {CharSequence.class, Calendar.class}
119         ),
120         @TestTargetNew(
121             level = TestLevel.COMPLETE,
122             method = "format",
123             args = {CharSequence.class, Date.class}
124         ),
125         @TestTargetNew(
126             level = TestLevel.COMPLETE,
127             method = "format",
128             args = {CharSequence.class, long.class}
129         )
130     })
131     @SuppressWarnings("deprecation")
testFormatMethods()132     public void testFormatMethods() throws ParseException {
133         if (!mDefaultLocale.equals(Locale.US)) {
134             Locale.setDefault(Locale.US);
135         }
136 
137         java.text.DateFormat dateFormat = DateFormat.getDateFormat(mContext);
138         assertNotNull(dateFormat);
139         Date date = new Date(YEAR_FROM_1900, MONTH, DAY, HOUR, MINUTE);
140         String source = dateFormat.format(date);
141         Date parseDate = dateFormat.parse(source);
142         assertEquals(date.getYear(), parseDate.getYear());
143         assertEquals(date.getMonth(), parseDate.getMonth());
144         assertEquals(date.getDay(), date.getDay());
145 
146         dateFormat = DateFormat.getLongDateFormat(mContext);
147         assertNotNull(dateFormat);
148         source = dateFormat.format(date);
149         assertTrue(source.indexOf("December") >= 0);
150         dateFormat = DateFormat.getMediumDateFormat(mContext);
151         assertNotNull(dateFormat);
152         source = dateFormat.format(date);
153         assertTrue(source.indexOf("Dec") >= 0);
154         assertTrue(source.indexOf("December") < 0);
155         dateFormat = DateFormat.getTimeFormat(mContext);
156         source = dateFormat.format(date);
157         assertTrue(source.indexOf("5") >= 0);
158         assertTrue(source.indexOf("30") >= 0);
159 
160         String testFormat = "yyyy-MM-dd";
161         String testOrder = "yMd";
162         Settings.System.putString(mContentResolver, Settings.System.DATE_FORMAT, testFormat);
163         String actualOrder = String.valueOf(DateFormat.getDateFormatOrder(mContext));
164         assertEquals(testOrder, actualOrder);
165 
166         String format = "MM/dd/yy";
167         String expectedString = "12/18/08";
168         Calendar calendar = new GregorianCalendar(YEAR, MONTH, DAY);
169         CharSequence actual = DateFormat.format(format, calendar);
170         assertEquals(expectedString, actual.toString());
171         Date formatDate = new Date(YEAR_FROM_1900, MONTH, DAY);
172         actual = DateFormat.format(format, formatDate);
173         assertEquals(expectedString, actual.toString());
174         actual = DateFormat.format(format, formatDate.getTime());
175         assertEquals(expectedString, actual.toString());
176     }
177 }
178