Skip to content

Commit

Permalink
Merge pull request #44 from luigi-agosti/master
Browse files Browse the repository at this point in the history
improve test coverage partially fixing issue #25
  • Loading branch information
luigi-agosti committed Jun 26, 2012
2 parents c3c7e96 + 05c07ae commit a327d75
Show file tree
Hide file tree
Showing 12 changed files with 408 additions and 15 deletions.
12 changes: 12 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@
<version>2.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.novoda.imageloader.core.loader.Loader;
import com.novoda.imageloader.core.loader.SimpleLoader;
import com.novoda.imageloader.core.network.NetworkManager;
import com.novoda.imageloader.core.network.UrlNetworkLoader;
import com.novoda.imageloader.core.network.UrlNetworkManager;

/**
* ImageManager has the responsibility to provide a
Expand All @@ -49,7 +49,7 @@ public ImageManager(Context context, LoaderSettings settings) {
this.loaderContext = new LoaderContext();
loaderContext.setSettings(settings);
loaderContext.setFileManager(new BasicFileManager(settings));
loaderContext.setNetworkManager(new UrlNetworkLoader(settings));
loaderContext.setNetworkManager(new UrlNetworkManager(settings));
loaderContext.setResBitmapCache(new SoftMapCache());
cacheManager = settings.getCacheManager();
if (cacheManager == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ public ImageTagFactory(int width, int height, int defaultImageResId) {
}

public ImageTagFactory(Context context, int defaultImageResId) {
Display d = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
d.getMetrics(dm);
Display d = prepareDisplay(context);
this.width = d.getWidth();
this.height = d.getHeight();
this.defaultImageResId = defaultImageResId;
Expand Down Expand Up @@ -78,5 +76,12 @@ public ImageTag build(String url) {
it.setPreviewWidth(previewImageWidth);
return it;
}

protected Display prepareDisplay(Context context) {
Display d = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
d.getMetrics(dm);
return d;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
import java.io.File;
import java.io.InputStream;

/**
* A network manager is responsible of downloading images
* given an http resource of url string.
* At the moment requested resources can be retrieved as InputStream
* or saved directly to a file.
*/
public interface NetworkManager {

void retrieveImage(String url, File f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,32 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.novoda.imageloader.core.LoaderSettings;
import com.novoda.imageloader.core.exception.ImageNotFoundException;
import com.novoda.imageloader.core.file.util.FileUtil;

public class UrlNetworkLoader implements NetworkManager {
/**
* Basic implementation of the NetworkManager using URL connection.
*/
public class UrlNetworkManager implements NetworkManager {

private FileUtil fileUtil = new FileUtil();
private FileUtil fileUtil;
private LoaderSettings settings;

public UrlNetworkLoader(LoaderSettings settings) {
public UrlNetworkManager(LoaderSettings settings) {
this(settings, new FileUtil());
}

public UrlNetworkManager(LoaderSettings settings, FileUtil fileUtil) {
this.settings = settings;
this.fileUtil = fileUtil;
}

@Override
Expand All @@ -43,7 +53,7 @@ public void retrieveImage(String url, File f) {
HttpURLConnection conn = null;
applyChangeonSdkVersion(settings.getSdkVersion());
try {
conn = (HttpURLConnection) new URL(url).openConnection();
conn = openConnection(url);
conn.setConnectTimeout(settings.getConnectionTimeout());
conn.setReadTimeout(settings.getReadTimeout());
is = conn.getInputStream();
Expand All @@ -61,12 +71,12 @@ public void retrieveImage(String url, File f) {
fileUtil.closeSilently(os);
}
}

@Override
public InputStream retrieveInputStream(String url) {
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) new URL(url).openConnection();
conn = openConnection(url);
conn.setConnectTimeout(settings.getConnectionTimeout());
conn.setReadTimeout(settings.getReadTimeout());
return conn.getInputStream();
Expand All @@ -77,6 +87,10 @@ public InputStream retrieveInputStream(String url) {
return null;
}
}

protected HttpURLConnection openConnection(String url) throws IOException, MalformedURLException {
return (HttpURLConnection) new URL(url).openConnection();
}

private void applyChangeonSdkVersion(String sdkVersion) {
if (Integer.parseInt(sdkVersion) < 8) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.novoda.imageloader.core.LoaderSettings;
import com.novoda.imageloader.core.bitmap.BitmapUtil;
import com.novoda.imageloader.core.network.NetworkManager;
import com.novoda.imageloader.core.network.UrlNetworkLoader;
import com.novoda.imageloader.core.network.UrlNetworkManager;

/**
* Direct loader make use of the NetworkManager and the BitmapUtil
Expand All @@ -19,8 +19,12 @@ public class DirectLoader {
private BitmapUtil bitmapUtil;

public DirectLoader() {
networkManager = new UrlNetworkLoader(new LoaderSettings());
bitmapUtil = new BitmapUtil();
this(new UrlNetworkManager(new LoaderSettings()), new BitmapUtil());
}

public DirectLoader(NetworkManager networkManager, BitmapUtil bitmapUtil) {
this.networkManager = networkManager;
this.bitmapUtil = bitmapUtil;
}

public Bitmap download(String url) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.novoda.imageloader.core.file.util;

public class FileUtilTest {



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.novoda.imageloader.core.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.junit.Before;
import org.junit.Test;
import static org.mockito.Mockito.*;

import android.content.Context;
import android.view.Display;
import android.view.WindowManager;

public class ImageTagFactoryTest {

private ImageTagFactory imageTagFactory;
private int height = 12;
private int width = 9;
private int defaultResourceId = 1;
private String url = "google.com";

@Before
public void beforeEachTest() {
imageTagFactory = new ImageTagFactory(9, 12, 1);
}

@Test
public void shouldSetNormalPropertiesOnTheImageTag() {
ImageTag imageTag = imageTagFactory.build(url);

assertEquals(defaultResourceId, imageTag.getLoadingResourceId());
assertEquals(defaultResourceId, imageTag.getNotFoundResourceId());
assertEquals(height, imageTag.getHeight());
assertEquals(width, imageTag.getWidth());
assertEquals(url, imageTag.getUrl());
}

@Test
public void shouldSetPreviewProperties() {
int previewHeight = 1;
int previewWidth = 2;
imageTagFactory.usePreviewImage(previewWidth, previewHeight, true);
ImageTag imageTag = imageTagFactory.build(url);

assertEquals(previewHeight, imageTag.getPreviewHeight());
assertEquals(previewWidth, imageTag.getPreviewWidth());
}

@Test
public void shouldUseTheSameUrlForPreview() {
imageTagFactory.usePreviewImage(1, 1, true);
ImageTag imageTag = imageTagFactory.build(url);

assertEquals(url, imageTag.getPreviewUrl());
}

@Test
public void shouldNotUseTheSameUrlForPreview() {
imageTagFactory.usePreviewImage(1, 1, false);
ImageTag imageTag = imageTagFactory.build(url);

assertNull(imageTag.getPreviewUrl());
}

@Test
public void shouldUseDisplaySizes() {
final Display display = mock(Display.class);
when(display.getHeight()).thenReturn(21);
when(display.getWidth()).thenReturn(12);
imageTagFactory = new ImageTagFactory(null, 1) {
@Override
protected Display prepareDisplay(Context context) {
return display;
}
};
ImageTag imageTag = imageTagFactory.build(url);
assertEquals(21, imageTag.getHeight());
assertEquals(12, imageTag.getWidth());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.novoda.imageloader.core.model;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;

import android.widget.ImageView;


public class ImageWrapperTest {

private ImageWrapper imageWrapper;
private ImageView imageView;
private ImageTag imageTag;

@Before
public void beforeEachTest() {
imageView = mock(ImageView.class);
imageTag = mock(ImageTag.class);

}

@Test
public void shouldNotFailIfImageTagIsNull() {
imageWrapper = new ImageWrapper(imageView);
}

@Test
public void shouldSetLoadingResourceId() {
when(imageTag.getLoadingResourceId()).thenReturn(1);
when(imageView.getTag()).thenReturn(imageTag);

imageWrapper = new ImageWrapper(imageView);
assertEquals(1, imageWrapper.getLoadingResourceId());
}

@Test
public void shouldSetNotFoundResourceIdIfDefined() {
when(imageTag.getNotFoundResourceId()).thenReturn(2);
when(imageView.getTag()).thenReturn(imageTag);

imageWrapper = new ImageWrapper(imageView);
assertEquals(2, imageWrapper.getNotFoundResourceId());
}

@Test
public void shouldReturnTrueIfCurrentUrlHasChanged() {
when(imageTag.getUrl()).thenReturn("url1");
when(imageView.getTag()).thenReturn(imageTag);

imageWrapper = new ImageWrapper(imageView);
when(imageTag.getUrl()).thenReturn("url2");

assertTrue(imageWrapper.isUrlChanged());
}

@Test
public void shouldReturnFalseIfCurrentUrlHasNotChanged() {
when(imageTag.getUrl()).thenReturn("url1");
when(imageView.getTag()).thenReturn(imageTag);

imageWrapper = new ImageWrapper(imageView);
when(imageTag.getUrl()).thenReturn("url1");

assertFalse(imageWrapper.isUrlChanged());
}

}
Loading

0 comments on commit a327d75

Please sign in to comment.