Code example for Calendar

Methods: add, get, getTime

0
 
    pressOK(); 
     
    Calendar newDate = getDatePreference(activity).getDate();
    Calendar expected = defaultDate();
    expected.add(Calendar.DAY_OF_MONTH, 1);
    String newDateSummary = getDatePreference(activity).getSummary().toString();
     
    assertCalendarDateEquals(expected, newDate);
    assertSummary(expected, newDateSummary);
  } 
   
  public void testDateCanceled() { 
    DatePreferenceActivity activity = getRestoredActivity();
    assertNotNull(activity);
     
    activateDodPreference(); 
 
    // increment the day 
    sendKeys(KeyEvent.KEYCODE_DPAD_DOWN,
        KeyEvent.KEYCODE_DPAD_UP,
        KeyEvent.KEYCODE_DPAD_RIGHT,
        KeyEvent.KEYCODE_DPAD_UP,
        KeyEvent.KEYCODE_DPAD_UP,
        KeyEvent.KEYCODE_DPAD_CENTER);
     
    pressCancel(); 
     
    Calendar newDate = getDatePreference(activity).getDate();
    Calendar expected = defaultDate();
     
    assertCalendarDateEquals(expected, newDate);
  } 
   
  public void testDateEdited() { 
    DatePreferenceActivity activity = getRestoredActivity();
    assertNotNull(activity);
     
    activateDodPreference(); 
 
    // change the day 
    sendKeys(KeyEvent.KEYCODE_DPAD_DOWN,
        KeyEvent.KEYCODE_DPAD_UP,
        KeyEvent.KEYCODE_DPAD_RIGHT,
        KeyEvent.KEYCODE_DPAD_UP,
        KeyEvent.KEYCODE_DEL,
        KeyEvent.KEYCODE_DEL,
        KeyEvent.KEYCODE_0,
        KeyEvent.KEYCODE_2);
 
 
    final DatePreference datePreference = getDatePreference(activity);
    activity.runOnUiThread(new Runnable() {
      public void run() { 
        clickOK(datePreference);
      } 
    }); 
    getInstrumentation().waitForIdleSync();
     
    Calendar newDate = datePreference.getDate();
    Calendar expected = defaultDate();
    expected.add(Calendar.DAY_OF_MONTH, 1);
     
    assertCalendarDateEquals(expected, newDate);
  } 
   
  public void testCancelThenOK() { 
    DatePreferenceActivity activity = getRestoredActivity();
    assertNotNull(activity);
     
    activateDodPreference(); 
 
    // increment the day 
    sendKeys(KeyEvent.KEYCODE_DPAD_DOWN,
        KeyEvent.KEYCODE_DPAD_UP,
        KeyEvent.KEYCODE_DPAD_RIGHT,
        KeyEvent.KEYCODE_DPAD_UP,
        KeyEvent.KEYCODE_DPAD_UP,
        KeyEvent.KEYCODE_DPAD_CENTER);
     
    pressCancel(); 
    activateDodPreference(); 
    pressOK(); 
    activateDodPreference(); 
    pressOK(); 
 
    Calendar newDate = getDatePreference(activity).getDate();
    Calendar expected = defaultDate();
     
    assertCalendarDateEquals(expected, newDate);
  } 
   
  public void testSharedPreferences() { 
    DatePreferenceActivity activity = getRestoredActivity();
    assertNotNull(activity);
     
    activateDodPreference(); 
    // increment the day 
    sendKeys(KeyEvent.KEYCODE_DPAD_DOWN,
        KeyEvent.KEYCODE_DPAD_UP,
        KeyEvent.KEYCODE_DPAD_RIGHT,
        KeyEvent.KEYCODE_DPAD_UP,
        KeyEvent.KEYCODE_DPAD_UP,
        KeyEvent.KEYCODE_DPAD_CENTER);
    pressOK(); 
 
    Calendar expected = defaultDate();
    expected.add(Calendar.DAY_OF_MONTH, 1);
    assertCalendarDateEquals(expected, activity.getCalendarOfDeath());
  } 
   
  public void testDestroyedAndPresistence() { 
    DatePreferenceActivity activity = getRestoredActivity();
    assertNotNull(activity);
     
    activateDodPreference(); 
    // increment the day 
    sendKeys(KeyEvent.KEYCODE_DPAD_DOWN,
        KeyEvent.KEYCODE_DPAD_UP,
        KeyEvent.KEYCODE_DPAD_RIGHT,
        KeyEvent.KEYCODE_DPAD_UP,
        KeyEvent.KEYCODE_DPAD_UP,
        KeyEvent.KEYCODE_DPAD_CENTER);
    pressOK(); 
    activateDoaPreference(); // android:persistent=false 
    // increment the day 
    sendKeys(KeyEvent.KEYCODE_DPAD_DOWN,
        KeyEvent.KEYCODE_DPAD_UP,
        KeyEvent.KEYCODE_DPAD_RIGHT,
        KeyEvent.KEYCODE_DPAD_UP,
        KeyEvent.KEYCODE_DPAD_UP,
        KeyEvent.KEYCODE_DPAD_CENTER);
    pressOK(); 
    activity.finish();
    activity = getActivity();
  
    Calendar expected = defaultDate();
    expected.add(Calendar.DAY_OF_MONTH, 1);
 
    assertCalendarDateEquals(expected, getDatePreference(activity, "dod").getDate());
//    assertCalendarDateEquals(defaultDate(), getDatePreference(activity, "doa").getDate()); // not yet possible to test 
  } 
   
  private Calendar defaultDate() {
    return new GregorianCalendar(1970, 0, 1);
  } 
   
  protected void assertCalendarDateEquals(Calendar expected, Calendar actual) {
    String msg = "Expected: " + formatter().format(expected.getTime()) + " but got: " + formatter().format(actual.getTime());
    assertEquals(msg, expected.get(Calendar.YEAR), actual.get(Calendar.YEAR));
    assertEquals(msg, expected.get(Calendar.MONTH), actual.get(Calendar.MONTH));
    assertEquals(msg, expected.get(Calendar.DAY_OF_MONTH), actual.get(Calendar.DAY_OF_MONTH));
  } 
   
  protected void assertDateEquals(Date expected, Date actual) {
    String msg = "Expected: " + formatter().format(expected.getTime()) + " but got: " + formatter().format(actual.getTime());
    assertEquals(msg, expected.getYear(), actual.getYear());
    assertEquals(msg, expected.getMonth(), actual.getMonth());
    assertEquals(msg, expected.getDay(), actual.getDay());
  } 
   
  protected void assertSummary(Calendar expected, String actual) {
    assertEquals(summaryFormatter().format(expected.getTime()), actual);
  } 
 
   
  private SimpleDateFormat formatter() {
    return new SimpleDateFormat("yyyy.MM.dd");