Android Open Source - PhotoPicker Image Cursor Mapper Test






From Project

Back to project page PhotoPicker.

License

The source code is released under:

GNU General Public License

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

package com.thuytrinh.photopicker.controller;
/*from  w  w  w .  j a  v  a 2  s. c  o  m*/
import android.database.Cursor;
import android.test.AndroidTestCase;

import com.thuytrinh.photopicker.model.Photo;

import static android.provider.BaseColumns._ID;
import static android.provider.MediaStore.Images.Media.BUCKET_DISPLAY_NAME;
import static android.provider.MediaStore.Images.Media.BUCKET_ID;
import static android.provider.MediaStore.Images.Media.DATA;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ImageCursorMapperTest extends AndroidTestCase {
  public void testShouldGetIdProperly() {
    Cursor mockImageCursor = mock(Cursor.class);

    int mockIdIndex = 2;
    when(mockImageCursor.getColumnIndex(_ID))
        .thenReturn(mockIdIndex);

    long mockId = 12345L;
    when(mockImageCursor.getLong(mockIdIndex))
        .thenReturn(mockId);

    ImageCursorMapper imageCursorMapper = new ImageCursorMapper(mockImageCursor);

    assertThat(imageCursorMapper.getId())
        .isEqualTo(mockId);
  }

  public void testShouldGetBucketIdProperly() {
    Cursor mockImageCursor = mock(Cursor.class);

    int mockBucketIdIndex = 2;
    when(mockImageCursor.getColumnIndex(BUCKET_ID))
        .thenReturn(mockBucketIdIndex);

    long mockBucketId = 12345L;
    when(mockImageCursor.getLong(mockBucketIdIndex))
        .thenReturn(mockBucketId);

    ImageCursorMapper imageCursorMapper = new ImageCursorMapper(mockImageCursor);

    assertThat(imageCursorMapper.getBucketId())
        .isEqualTo(mockBucketId);
  }

  public void testShouldGetBucketDisplayNameProperly() {
    Cursor mockImageCursor = mock(Cursor.class);

    int mockBucketDisplayNameIndex = 2;
    when(mockImageCursor.getColumnIndex(BUCKET_DISPLAY_NAME))
        .thenReturn(mockBucketDisplayNameIndex);

    String mockBucketDisplayName = "Awesome album";
    when(mockImageCursor.getString(mockBucketDisplayNameIndex))
        .thenReturn(mockBucketDisplayName);

    ImageCursorMapper imageCursorMapper = new ImageCursorMapper(mockImageCursor);

    assertThat(imageCursorMapper.getBucketDisplayName())
        .isEqualTo(mockBucketDisplayName);
  }

  public void testShouldGetDataProperly() {
    Cursor mockImageCursor = mock(Cursor.class);

    int mockDataIndex = 2;
    when(mockImageCursor.getColumnIndex(DATA))
        .thenReturn(mockDataIndex);

    String mockData = "Matrix/secret.png";
    when(mockImageCursor.getString(mockDataIndex))
        .thenReturn(mockData);

    ImageCursorMapper imageCursorMapper = new ImageCursorMapper(mockImageCursor);

    assertThat(imageCursorMapper.getData())
        .isEqualTo(mockData);
  }

  public void testShouldConvertToPhotoProperly() {
    Cursor mockImageCursor = mock(Cursor.class);

    int mockIdIndex = 1;
    when(mockImageCursor.getColumnIndex(_ID))
        .thenReturn(mockIdIndex);

    long mockId = 123L;
    when(mockImageCursor.getLong(mockIdIndex))
        .thenReturn(mockId);

    int mockBucketIdIndex = 2;
    when(mockImageCursor.getColumnIndex(BUCKET_ID))
        .thenReturn(mockBucketIdIndex);

    long mockBucketId = 456L;
    when(mockImageCursor.getLong(mockBucketIdIndex))
        .thenReturn(mockBucketId);

    int mockDataIndex = 3;
    when(mockImageCursor.getColumnIndex(DATA))
        .thenReturn(mockDataIndex);

    String mockData = "Matrix/secret.png";
    when(mockImageCursor.getString(mockDataIndex))
        .thenReturn(mockData);

    ImageCursorMapper imageCursorMapper = new ImageCursorMapper(mockImageCursor);
    Photo photo = imageCursorMapper.toPhoto();

    assertNotNull(photo);
    assertThat(photo.getId()).isEqualTo(mockId);
    assertThat(photo.getAlbumId()).isEqualTo(mockBucketId);
    assertThat(photo.getPath()).isEqualTo(mockData);
  }
}




Java Source Code List

com.thuytrinh.photopicker.controller.GroupByBucketIdFunc.java
com.thuytrinh.photopicker.controller.ImageCursorMapperTest.java
com.thuytrinh.photopicker.controller.ImageCursorMapper.java
com.thuytrinh.photopicker.controller.SimpleLoaderListenerTest.java
com.thuytrinh.photopicker.controller.SimpleLoaderListener.java
com.thuytrinh.photopicker.controller.activity.PhotoChooserActivityTest.java
com.thuytrinh.photopicker.controller.activity.PhotoChooserActivity.java
com.thuytrinh.photopicker.controller.adapter.AlbumsAdapter.java
com.thuytrinh.photopicker.controller.adapter.PhotosAdapter.java
com.thuytrinh.photopicker.controller.fragment.AlbumsFragment.java
com.thuytrinh.photopicker.controller.fragment.PhotosFragment.java
com.thuytrinh.photopicker.controller.loader.AlbumsLoaderTest.java
com.thuytrinh.photopicker.controller.loader.AlbumsLoader.java
com.thuytrinh.photopicker.controller.loader.PhotosLoaderTest.java
com.thuytrinh.photopicker.controller.loader.PhotosLoader.java
com.thuytrinh.photopicker.model.PhotoTest.java
com.thuytrinh.photopicker.model.Photo.java
com.thuytrinh.photopicker.module.AppModule.java
com.thuytrinh.photopicker.module.ObjectLocator.java
com.thuytrinh.photopicker.view.AlbumView.java
com.thuytrinh.photopicker.view.PhotoItemLayoutTest.java
com.thuytrinh.photopicker.view.PhotoItemLayout.java