Android Open Source - watchme Date Time Utils Test






From Project

Back to project page watchme.

License

The source code is released under:

Copyright (c) 2012 Johan Brook, Robin Andersson, Lisa Stenberg, Mattias Henriksson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documen...

If you think the Android project watchme listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
*  DateTimeUtilsTest.java/*www  .  java  2s.  com*/
*
*  @author Johan Brook
*  @copyright (c) 2012 Johan Brook
*  @license MIT
*/

package se.chalmers.watchmetest.util;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;

import android.test.suitebuilder.annotation.SmallTest;

import se.chalmers.watchme.utils.DateTimeUtils;
import se.chalmers.watchme.utils.MovieHelper;
import junit.framework.TestCase;

public class DateTimeUtilsTest extends TestCase {
  
  public void testToSimpleDate() {
    Calendar cal = Calendar.getInstance();
    // The month really is October (10)
    cal.set(2012, 9, 14);
    
    // Don't forget the correct locale
    assertEquals("14 Oct, 2012", DateTimeUtils.toSimpleDate(cal));
  }
  
  public void testToSimpleDateFormat() {
    String format = "yyyy-MM-dd";
    Calendar cal = Calendar.getInstance();
    cal.set(2012, 9, 14);
    
    assertEquals("2012-10-14", DateTimeUtils.toSimpleDate(cal, format));
  }
  
  public void testMinutesToHuman() {
    int minutes1 = 124;
    int minutes2 = 119;
    int minutes3 = 120;
    int minutes4 = 59;
    
    assertEquals("2:04", DateTimeUtils.minutesToHuman(minutes1));
    assertEquals("1:59", DateTimeUtils.minutesToHuman(minutes2));
    assertEquals("2:00", DateTimeUtils.minutesToHuman(minutes3));
    assertEquals("0:59", DateTimeUtils.minutesToHuman(minutes4));
  }
  
  public void testParseYearFromDate() {
    String correctDate = "2012-03-03";
    assertEquals(DateTimeUtils.parseYearFromDate(correctDate), "2012");
    
    String incorrectDate = "2012/03/03";
    assertEquals(DateTimeUtils.parseYearFromDate(incorrectDate), incorrectDate);
    
    assertNull(DateTimeUtils.parseYearFromDate(null));
  }
  
  /**
   * Test the relative time function in order
   */
  public void testToHumanDate() {
    Calendar date = GregorianCalendar.getInstance();
    
    assertEquals("today", DateTimeUtils.toHumanDate(date));

    date.add(Calendar.DAY_OF_MONTH, 1);
    assertEquals("tomorrow", DateTimeUtils.toHumanDate(date));
    date.add(Calendar.DAY_OF_MONTH, 3);
    assertEquals("4 days", DateTimeUtils.toHumanDate(date));
    
    date.add(Calendar.MONTH, 1);
    assertEquals("next month", DateTimeUtils.toHumanDate(date));
    date.add(Calendar.MONTH, 1);
    assertEquals("2 months", DateTimeUtils.toHumanDate(date));
    
    date.add(Calendar.YEAR, 1);
    assertEquals("next year", DateTimeUtils.toHumanDate(date));
    date.add(Calendar.YEAR, 3);
    assertEquals("4 years", DateTimeUtils.toHumanDate(date));
  }
  
  /**
   * Now test the relative time function with month before day
   */
  public void testToHumanDateMixedOrder() {
    Calendar date = GregorianCalendar.getInstance();
    
    date.add(Calendar.MONTH, 1);
    assertEquals("next month", DateTimeUtils.toHumanDate(date));
    date.add(Calendar.MONTH, 1);
    assertEquals("2 months", DateTimeUtils.toHumanDate(date));
    
    // Return string should still be the same
    date.add(Calendar.DAY_OF_MONTH, 3);
    assertEquals("2 months", DateTimeUtils.toHumanDate(date));
  }
  
  public void testToHumanDateOverlap() {
    Calendar date = GregorianCalendar.getInstance();
    
    date.add(Calendar.MONTH, 6);
    assertEquals("next year", DateTimeUtils.toHumanDate(date));
  }
  
  
  public void testIsDateInInterval() {
    Calendar dateWithin = GregorianCalendar.getInstance();
    Calendar dateOutside = GregorianCalendar.getInstance();
    
    dateWithin.add(Calendar.DAY_OF_MONTH, 3);
    dateOutside.add(Calendar.DAY_OF_MONTH, 10);
    
    assertTrue(DateTimeUtils.isDateInInterval(dateWithin, 5, TimeUnit.DAYS));
    assertTrue(DateTimeUtils.isDateInInterval(GregorianCalendar.getInstance(), 5, TimeUnit.DAYS));
    
    assertFalse(DateTimeUtils.isDateInInterval(dateOutside, 5, TimeUnit.DAYS));
  }
}




Java Source Code List

se.chalmers.watchme.activity.AddMovieActivity.java
se.chalmers.watchme.activity.AutoCompleteAdapter.java
se.chalmers.watchme.activity.MainActivity.java
se.chalmers.watchme.activity.MovieDetailsActivity.java
se.chalmers.watchme.activity.SearchableActivity.java
se.chalmers.watchme.activity.TabsAdapter.java
se.chalmers.watchme.activity.TagMovieListActivity.java
se.chalmers.watchme.database.DatabaseAdapter.java
se.chalmers.watchme.database.DatabaseHelper.java
se.chalmers.watchme.database.GenericCursorLoader.java
se.chalmers.watchme.database.HasTagTable.java
se.chalmers.watchme.database.ICursorHelper.java
se.chalmers.watchme.database.MovieAlreadyExistsException.java
se.chalmers.watchme.database.MoviesTable.java
se.chalmers.watchme.database.TagsTable.java
se.chalmers.watchme.database.WatchMeContentProvider.java
se.chalmers.watchme.model.Movie.java
se.chalmers.watchme.model.Tag.java
se.chalmers.watchme.net.HttpRetriever.java
se.chalmers.watchme.net.IMDBHandler.java
se.chalmers.watchme.net.ImageDownloadTask.java
se.chalmers.watchme.net.MovieSource.java
se.chalmers.watchme.net.NoEntityException.java
se.chalmers.watchme.notifications.Notifiable.java
se.chalmers.watchme.notifications.NotificationClient.java
se.chalmers.watchme.notifications.NotificationService.java
se.chalmers.watchme.notifications.NotifyService.java
se.chalmers.watchme.ui.ContentListFragment.java
se.chalmers.watchme.ui.DatePickerFragment.java
se.chalmers.watchme.ui.ImageDialog.java
se.chalmers.watchme.ui.MovieListFragment.java
se.chalmers.watchme.ui.TagListFragment.java
se.chalmers.watchme.utils.DateTimeUtils.java
se.chalmers.watchme.utils.ImageCache.java
se.chalmers.watchme.utils.MenuUtils.java
se.chalmers.watchme.utils.MovieHelper.java
se.chalmers.watchmetest.Constants.java
se.chalmers.watchmetest.activity.MainActivityTest.java
se.chalmers.watchmetest.activity.MovieDetailsActivityTest.java
se.chalmers.watchmetest.activity.SearchableActivityTest.java
se.chalmers.watchmetest.activity.TabsAdapterTest.java
se.chalmers.watchmetest.activity.TagMovieListActivityTest.java
se.chalmers.watchmetest.database.WatchMeContentProviderTest.java
se.chalmers.watchmetest.model.MovieTest.java
se.chalmers.watchmetest.model.TagTest.java
se.chalmers.watchmetest.net.HttpRetrieverTest.java
se.chalmers.watchmetest.net.IMDBHandlerTest.java
se.chalmers.watchmetest.ui.MovieListFragmentTest.java
se.chalmers.watchmetest.ui.TagListFragmentTest.java
se.chalmers.watchmetest.util.DateTimeUtilsTest.java
se.chalmers.watchmetest.util.MovieHelperTest.java