Android Open Source - AudioBook Book Test






From Project

Back to project page AudioBook.

License

The source code is released under:

Creative Commons Legal Code Attribution-NonCommercial 3.0 Unported CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE LEGAL SERVICES. DISTRIBUTION OF THIS LICENSE DOES NOT C...

If you think the Android project AudioBook 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

/**
 *  This work is licensed under the Creative Commons Attribution-NonCommercial-
 *  NoDerivs 3.0 Unported License. To view a copy of this license, visit
 *  http://creativecommons.org/licenses/by-nc-nd/3.0/ or send a letter to 
 *  Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 
 *  94041, USA.//w  w  w  . j a va2s  . com
 * 
 *  Use of this work is permitted only in accordance with license rights granted.
 *  Materials provided "AS IS"; no representations or warranties provided.
 * 
 *  Copyright ? 2012 Marcus Parkkinen, Aki K?kel?, Fredrik ?hs.
 **/

package edu.chalmers.dat255.audiobookplayer.model;

import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

/**
 * Tests the Book class, by testing cloning a book,
 * removing/adding/swapping/moving tracks, selecting tracks and setting the
 * title of the book.
 * 
 * @author Marcus Parkkinen, Aki K?kel?
 * @version 0.2
 */
public class BookTest extends TestCase {
  private List<Track> bList;
  private String bookName = "MyTestBook";
  private String bookAuthor = "MyTestBookAuthor";
  private Book b;

  private static final int DURATION0 = 5;
  private static final int DURATION1 = 10;
  private static final int DURATION2 = 15;
  private static final int DURATION3 = 20;

  private static final int TOTAL_DURATION = DURATION0 + DURATION1 + DURATION2
      + DURATION3;

  // Tracks to test the book with
  private Track t0 = new Track("/audiobooks/huckleberry finn/huck_finn_chap01-text.mp3", DURATION0);
  private Track t1 = new Track("/audiobooks/huckleberry finn/huck_finn_chap02-text.mp3", DURATION1);
  private Track t2 = new Track("/audiobooks/huckleberry finn/huck_finn_chap03-text.mp3", DURATION2);
  private Track t3 = new Track("/audiobooks/huckleberry finn/huck_finn_chap04-text.mp3", DURATION3);

  private static final int TOTAL_NUMBER_OF_TRACKS = 4;

  private Track[] tracks = { t0, t1, t2, t3 };

  /*
   * (non-Javadoc)
   * 
   * @see junit.framework.TestCase#setUp()
   */
  @Override
  protected void setUp() {
    try {
      super.setUp();
    } catch (Exception e) {
      // catch exceptions from super.setUp() and fail
      fail("setUp failed + " + e.getMessage());
    }

    bList = new ArrayList<Track>();

    for (int i = 0; i < tracks.length; i++) {
      bList.add(tracks[i]);
    }

    b = new Book(bList, bookName, bookAuthor);
  }

  /**
   * Tests the constructor.
   */
  public void testConstructor() {

    // assert that four tracks have been added
    b = new Book(bList, bookName, bookAuthor);
    assertEquals(TOTAL_NUMBER_OF_TRACKS, b.getNumberOfTracks());

    // add a null track to the list
    bList.add(null);

    // assert that the null track doesn't get added to the book
    b = new Book(bList, bookName, bookAuthor);
    assertEquals(TOTAL_NUMBER_OF_TRACKS, b.getNumberOfTracks());

    // assert that the duration of the book has been calculated correctly
    assertEquals(TOTAL_DURATION, b.getDuration());
  }

  /**
   * Tests the copy constructor.
   */
  public void testCopy() {
    // create a new copy of the book
    Book newBook = new Book(b);

    // assert that we have two separate objects
    assertNotSame(newBook, b);

    // assert that the track objects are deep copies as well
    assertFalse(newBook.getSelectedTrack() == b.getSelectedTrack());

    // assert that both books are equal
    assertTrue(newBook.equals(b));

    // also try this with books that are empty
    b = new Book(bookName);
    newBook = new Book(b);

    // assert that we have two separate objects
    assertNotSame(newBook, b);

    // assert that both books are equal
    assertTrue(newBook.equals(b));
  }

  /**
   * Tests removing tracks.
   */
  public void testRemoveTrack() {
    // remove the first track
    int duration = b.getDuration();

    // for all tracks in the book..
    for (int i = 0; i < tracks.length; i++) {
      // assert that the current track index adjusts accordingly
      assertTrue(b.getSelectedTrack().equals(tracks[i]));

      // remove the track that is on the first index
      b.removeTrack(0);

      // assert that the amount of tracks is correct
      assertEquals(TOTAL_NUMBER_OF_TRACKS - i - 1, b.getNumberOfTracks());

      // assert that the duration adjusts accordingly
      duration -= tracks[i].getDuration();
      assertEquals(duration, b.getDuration());
    }

    // assert that no track is selected if the book is lacking tracks
    assertEquals(-1, b.getSelectedTrackIndex());
  }

  /**
   * Tests adding tracks.
   */
  public void testAddTrack() {
    int duration = 0;
    b = new Book(bookName);

    for (int i = 0; i < tracks.length - 1; i++) {

      // add a new track to the beginning of the book
      b.addTrack(tracks[i]);

      // assert that the selected track index does not change even
      // when adding tracks to indices before it
      assertTrue(b.getSelectedTrack().equals(tracks[0]));

      // assert that the amount of tracks changes accordingly
      assertEquals(i + 1, b.getNumberOfTracks());

      // assert that the duration is correct
      duration += tracks[i].getDuration();
      assertEquals(duration, b.getDuration());
    }

  }

  /**
   * Tests swapping tracks.
   */
  public void testSwap() {
    b.setSelectedTrackIndex(1);

    // swap tracks 0 and 1
    b.swapTracks(0, 1);
    assertTrue(b.getSelectedTrack().equals(t0));

    // assert that elapsed time of the book adjusts accordingly
    assertEquals(t1.getDuration(), b.getBookElapsedTime());

    // swap tracks 0 and 1 again
    b.swapTracks(0, 1);
    assertTrue(b.getSelectedTrack().equals(t1));

    assertEquals(t0.getDuration(), b.getBookElapsedTime());
  }

  /**
   * Tests moving tracks.
   */
  public void testMoveTrack() {
    b.setSelectedTrackIndex(1);

    // assert that the current track is t1
    assertTrue(b.getSelectedTrack().equals(tracks[1]));

    // swap t0 and t1
    b.moveTrack(0, 1);

    // assert that the current track now is t0
    assertTrue(b.getSelectedTrack().equals(tracks[0]));

    // also assert that the elapsed time has adjusted accordingly
    assertEquals(tracks[1].getDuration(), b.getBookElapsedTime());
  }

  /**
   * Tests selecting tracks.
   */
  public void setCurrentTrackIndex() {
    b.setSelectedTrackIndex(tracks.length - 1);

    // assert that the index is set correctly
    assertEquals(tracks.length - 1, b.getSelectedTrackIndex());

    // assert that the elapsed time of the book is adjusted
    // accordingly
    int elapsedTime = 0;
    for (int i = 0; i < tracks.length - 1; i++) {
      elapsedTime += tracks[i].getDuration();
    }

    assertEquals(elapsedTime, b.getBookElapsedTime());
  }

  /**
   * Tests setting the title of a book.
   */
  public void testSetBookTitle() {
    // assert that we cannot set the book name with a null string
    try {
      b.setSelectedBookTitle(null);
      fail("managed to set book title with null string");
    } catch (IllegalArgumentException e) {
      // assert that the old name still applies
      assertEquals(bookName, b.getSelectedBookTitle());
    }

    // but that we can set it to a new valid name
    String anotherTitle = "e";
    b.setSelectedBookTitle(anotherTitle);
    assertEquals(anotherTitle, b.getSelectedBookTitle());
  }

}




Java Source Code List

edu.chalmers.dat255.audiobookplayer.constants.Constants.java
edu.chalmers.dat255.audiobookplayer.constants.PlaybackStatus.java
edu.chalmers.dat255.audiobookplayer.ctrl.BookshelfControllerTest.java
edu.chalmers.dat255.audiobookplayer.ctrl.BookshelfController.java
edu.chalmers.dat255.audiobookplayer.ctrl.PlayerControllerTest.java
edu.chalmers.dat255.audiobookplayer.ctrl.PlayerController.java
edu.chalmers.dat255.audiobookplayer.instrumentation.AllTests.java
edu.chalmers.dat255.audiobookplayer.interfaces.IBookUpdates.java
edu.chalmers.dat255.audiobookplayer.interfaces.IBookshelfEvents.java
edu.chalmers.dat255.audiobookplayer.interfaces.IBookshelfGUIEvents.java
edu.chalmers.dat255.audiobookplayer.interfaces.IPlayerEvents.java
edu.chalmers.dat255.audiobookplayer.interfaces.ITrackUpdates.java
edu.chalmers.dat255.audiobookplayer.model.BookTest.java
edu.chalmers.dat255.audiobookplayer.model.Book.java
edu.chalmers.dat255.audiobookplayer.model.BookshelfTest.java
edu.chalmers.dat255.audiobookplayer.model.Bookshelf.java
edu.chalmers.dat255.audiobookplayer.model.TagTest.java
edu.chalmers.dat255.audiobookplayer.model.Tag.java
edu.chalmers.dat255.audiobookplayer.model.TrackTest.java
edu.chalmers.dat255.audiobookplayer.model.Track.java
edu.chalmers.dat255.audiobookplayer.util.BookCreatorTest.java
edu.chalmers.dat255.audiobookplayer.util.BookCreator.java
edu.chalmers.dat255.audiobookplayer.util.BookshelfHandlerTest.java
edu.chalmers.dat255.audiobookplayer.util.BookshelfHandler.java
edu.chalmers.dat255.audiobookplayer.util.FileParserTest.java
edu.chalmers.dat255.audiobookplayer.util.FileParser.java
edu.chalmers.dat255.audiobookplayer.util.JsonParserTest.java
edu.chalmers.dat255.audiobookplayer.util.JsonParser.java
edu.chalmers.dat255.audiobookplayer.util.TextFormatterTest.java
edu.chalmers.dat255.audiobookplayer.util.TextFormatter.java
edu.chalmers.dat255.audiobookplayer.util.TrackCreatorTest.java
edu.chalmers.dat255.audiobookplayer.util.TrackCreator.java
edu.chalmers.dat255.audiobookplayer.view.BookshelfFragment.java
edu.chalmers.dat255.audiobookplayer.view.BrowserActivityTest.java
edu.chalmers.dat255.audiobookplayer.view.BrowserActivity.java
edu.chalmers.dat255.audiobookplayer.view.MainActivity.java
edu.chalmers.dat255.audiobookplayer.view.PlayerFragment.java
edu.chalmers.dat255.audiobookplayer.view.ViewPagerAdapter.java