Home | History | Annotate | Download | only in calendar
      1 /*
      2  * Copyright (C) 2007 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.calendar;
     18 
     19 import android.content.res.Resources;
     20 import android.text.format.DateUtils;
     21 import android.text.format.Time;
     22 import android.test.AndroidTestCase;
     23 import android.test.suitebuilder.annotation.SmallTest;
     24 import android.util.Log;
     25 
     26 
     27 /**
     28  * Unit tests for {@link android.text.format.Time#getWeekNumber}.
     29  */
     30 public class WeekNumberTest extends AndroidTestCase {
     31 
     32     static private class DateAndWeekNumber {
     33         public Time date;
     34         public Time allDayDate;
     35         public int expectedWeekNumber;
     36 
     37         public DateAndWeekNumber(int year, int month, int day, int expectedWeekNumber) {
     38             date = new Time();
     39             allDayDate = new Time(Time.TIMEZONE_UTC);
     40 
     41             date.set(0, 0, 0, day, month, year);
     42             date.normalize(true /* ignore isDst */);
     43 
     44             allDayDate.set(day, month, year);
     45             allDayDate.normalize(true /* ignore isDst */);
     46 
     47             this.expectedWeekNumber = expectedWeekNumber;
     48         }
     49     }
     50 
     51     DateAndWeekNumber[] tests = {
     52             new DateAndWeekNumber(1998, 11, 28, 53),
     53             new DateAndWeekNumber(1998, 11, 29, 53),
     54             new DateAndWeekNumber(1998, 11, 30, 53),
     55             new DateAndWeekNumber(1998, 11, 31, 53),
     56             new DateAndWeekNumber(1999, 0, 1, 53),
     57             new DateAndWeekNumber(1999, 0, 2, 53),
     58             new DateAndWeekNumber(1999, 0, 3, 53),
     59             new DateAndWeekNumber(1999, 0, 4, 1),
     60             new DateAndWeekNumber(1999, 0, 10, 1),
     61             new DateAndWeekNumber(1999, 0, 20, 3),
     62             new DateAndWeekNumber(1999, 0, 30, 4),
     63 
     64             new DateAndWeekNumber(1999, 11, 28, 52),
     65             new DateAndWeekNumber(1999, 11, 29, 52),
     66             new DateAndWeekNumber(1999, 11, 30, 52),
     67             new DateAndWeekNumber(1999, 11, 31, 52),
     68             new DateAndWeekNumber(2000, 0, 1, 52),
     69             new DateAndWeekNumber(2000, 0, 2, 52),
     70             new DateAndWeekNumber(2000, 0, 3, 1),
     71             new DateAndWeekNumber(2000, 0, 4, 1),
     72             new DateAndWeekNumber(2000, 0, 10, 2),
     73             new DateAndWeekNumber(2000, 0, 20, 3),
     74             new DateAndWeekNumber(2000, 0, 30, 4),
     75 
     76             new DateAndWeekNumber(2000, 11, 28, 52),
     77             new DateAndWeekNumber(2000, 11, 29, 52),
     78             new DateAndWeekNumber(2000, 11, 30, 52),
     79             new DateAndWeekNumber(2000, 11, 31, 52),
     80             new DateAndWeekNumber(2001, 0, 1, 1),
     81             new DateAndWeekNumber(2001, 0, 2, 1),
     82             new DateAndWeekNumber(2001, 0, 3, 1),
     83             new DateAndWeekNumber(2001, 0, 4, 1),
     84             new DateAndWeekNumber(2001, 0, 10, 2),
     85             new DateAndWeekNumber(2001, 0, 20, 3),
     86             new DateAndWeekNumber(2001, 0, 30, 5),
     87 
     88             new DateAndWeekNumber(2001, 11, 28, 52),
     89             new DateAndWeekNumber(2001, 11, 29, 52),
     90             new DateAndWeekNumber(2001, 11, 30, 52),
     91             new DateAndWeekNumber(2001, 11, 31, 1),
     92             new DateAndWeekNumber(2002, 0, 1, 1),
     93             new DateAndWeekNumber(2002, 0, 2, 1),
     94             new DateAndWeekNumber(2002, 0, 3, 1),
     95             new DateAndWeekNumber(2002, 0, 4, 1),
     96             new DateAndWeekNumber(2002, 0, 10, 2),
     97             new DateAndWeekNumber(2002, 0, 20, 3),
     98             new DateAndWeekNumber(2002, 0, 30, 5),
     99 
    100             new DateAndWeekNumber(2002, 11, 28, 52),
    101             new DateAndWeekNumber(2002, 11, 29, 52),
    102             new DateAndWeekNumber(2002, 11, 30, 1),
    103             new DateAndWeekNumber(2002, 11, 31, 1),
    104             new DateAndWeekNumber(2003, 0, 1, 1),
    105             new DateAndWeekNumber(2003, 0, 2, 1),
    106             new DateAndWeekNumber(2003, 0, 3, 1),
    107             new DateAndWeekNumber(2003, 0, 4, 1),
    108             new DateAndWeekNumber(2003, 0, 10, 2),
    109             new DateAndWeekNumber(2003, 0, 20, 4),
    110             new DateAndWeekNumber(2003, 0, 30, 5),
    111 
    112             new DateAndWeekNumber(2003, 11, 28, 52),
    113             new DateAndWeekNumber(2003, 11, 29, 1),
    114             new DateAndWeekNumber(2003, 11, 30, 1),
    115             new DateAndWeekNumber(2003, 11, 31, 1),
    116             new DateAndWeekNumber(2004, 0, 1, 1),
    117             new DateAndWeekNumber(2004, 0, 2, 1),
    118             new DateAndWeekNumber(2004, 0, 3, 1),
    119             new DateAndWeekNumber(2004, 0, 4, 1),
    120             new DateAndWeekNumber(2004, 0, 10, 2),
    121             new DateAndWeekNumber(2004, 0, 20, 4),
    122             new DateAndWeekNumber(2004, 0, 30, 5),
    123 
    124             new DateAndWeekNumber(2004, 0, 1, 1),
    125             new DateAndWeekNumber(2004, 1, 1, 5),
    126             new DateAndWeekNumber(2004, 2, 1, 10),
    127             new DateAndWeekNumber(2004, 3, 1, 14),
    128             new DateAndWeekNumber(2004, 4, 1, 18),
    129             new DateAndWeekNumber(2004, 5, 1, 23),
    130             new DateAndWeekNumber(2004, 6, 1, 27),
    131             new DateAndWeekNumber(2004, 7, 1, 31),
    132             new DateAndWeekNumber(2004, 8, 1, 36),
    133             new DateAndWeekNumber(2004, 9, 1, 40),
    134             new DateAndWeekNumber(2004, 10, 1, 45),
    135             new DateAndWeekNumber(2004, 11, 1, 49),
    136 
    137             new DateAndWeekNumber(2004, 11, 28, 53),
    138             new DateAndWeekNumber(2004, 11, 29, 53),
    139             new DateAndWeekNumber(2004, 11, 30, 53),
    140             new DateAndWeekNumber(2004, 11, 31, 53),
    141             new DateAndWeekNumber(2005, 0, 1, 53),
    142             new DateAndWeekNumber(2005, 0, 2, 53),
    143             new DateAndWeekNumber(2005, 0, 3, 1),
    144             new DateAndWeekNumber(2005, 0, 4, 1),
    145             new DateAndWeekNumber(2005, 0, 10, 2),
    146             new DateAndWeekNumber(2005, 0, 20, 3),
    147             new DateAndWeekNumber(2005, 0, 30, 4),
    148 
    149             new DateAndWeekNumber(2005, 11, 28, 52),
    150             new DateAndWeekNumber(2005, 11, 29, 52),
    151             new DateAndWeekNumber(2005, 11, 30, 52),
    152             new DateAndWeekNumber(2005, 11, 31, 52),
    153             new DateAndWeekNumber(2006, 0, 1, 52),
    154             new DateAndWeekNumber(2006, 0, 2, 1),
    155             new DateAndWeekNumber(2006, 0, 3, 1),
    156             new DateAndWeekNumber(2006, 0, 4, 1),
    157             new DateAndWeekNumber(2006, 0, 10, 2),
    158             new DateAndWeekNumber(2006, 0, 20, 3),
    159             new DateAndWeekNumber(2006, 0, 30, 5),
    160 
    161             new DateAndWeekNumber(2006, 11, 28, 52),
    162             new DateAndWeekNumber(2006, 11, 29, 52),
    163             new DateAndWeekNumber(2006, 11, 30, 52),
    164             new DateAndWeekNumber(2006, 11, 31, 52),
    165             new DateAndWeekNumber(2007, 0, 1, 1),
    166             new DateAndWeekNumber(2007, 0, 2, 1),
    167             new DateAndWeekNumber(2007, 0, 3, 1),
    168             new DateAndWeekNumber(2007, 0, 4, 1),
    169             new DateAndWeekNumber(2007, 0, 10, 2),
    170             new DateAndWeekNumber(2007, 0, 20, 3),
    171             new DateAndWeekNumber(2007, 0, 30, 5),
    172 
    173             new DateAndWeekNumber(2007, 11, 28, 52),
    174             new DateAndWeekNumber(2007, 11, 29, 52),
    175             new DateAndWeekNumber(2007, 11, 30, 52),
    176             new DateAndWeekNumber(2007, 11, 31, 1),
    177             new DateAndWeekNumber(2008, 0, 1, 1),
    178             new DateAndWeekNumber(2008, 0, 2, 1),
    179             new DateAndWeekNumber(2008, 0, 3, 1),
    180             new DateAndWeekNumber(2008, 0, 4, 1),
    181             new DateAndWeekNumber(2008, 0, 10, 2),
    182             new DateAndWeekNumber(2008, 0, 20, 3),
    183             new DateAndWeekNumber(2008, 0, 30, 5),
    184 
    185             new DateAndWeekNumber(2008, 11, 28, 52),
    186             new DateAndWeekNumber(2008, 11, 29, 1),
    187             new DateAndWeekNumber(2008, 11, 30, 1),
    188             new DateAndWeekNumber(2008, 11, 31, 1),
    189             new DateAndWeekNumber(2009, 0, 1, 1),
    190             new DateAndWeekNumber(2009, 0, 2, 1),
    191             new DateAndWeekNumber(2009, 0, 3, 1),
    192             new DateAndWeekNumber(2009, 0, 4, 1),
    193             new DateAndWeekNumber(2009, 0, 10, 2),
    194             new DateAndWeekNumber(2009, 0, 20, 4),
    195             new DateAndWeekNumber(2009, 0, 30, 5),
    196     };
    197 
    198     @Override
    199     protected void setUp() throws Exception {
    200         super.setUp();
    201     }
    202 
    203     @SmallTest
    204     public void testAll() throws Exception {
    205         int len = tests.length;
    206         for (int index = 0; index < len; index++) {
    207             DateAndWeekNumber test = tests[index];
    208             int weekNumber = test.date.getWeekNumber();
    209             if (weekNumber != test.expectedWeekNumber) {
    210                 long millis = test.date.toMillis(false /* use isDst */);
    211                 int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NUMERIC_DATE;
    212                 String output = DateUtils.formatDateTime(mContext, millis, flags);
    213                 Log.i("WeekNumberTest", "index " + index
    214                         + " date: " + output
    215                         + " expected: " + test.expectedWeekNumber
    216                         + " actual: " + weekNumber);
    217             }
    218             assertEquals(weekNumber, test.expectedWeekNumber);
    219 
    220             weekNumber = test.allDayDate.getWeekNumber();
    221             if (weekNumber != test.expectedWeekNumber) {
    222                 long millis = test.date.toMillis(false /* use isDst */);
    223                 int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NUMERIC_DATE;
    224                 String output = DateUtils.formatDateTime(mContext, millis, flags);
    225                 Log.i("WeekNumberTest", "(all-day) index " + index
    226                         + " date: " + output
    227                         + " expected: " + test.expectedWeekNumber
    228                         + " actual: " + weekNumber);
    229             }
    230             assertEquals(weekNumber, test.expectedWeekNumber);
    231         }
    232     }
    233 }
    234