Skip to content

Commit

Permalink
Added unit test for url generation
Browse files Browse the repository at this point in the history
  • Loading branch information
cfraz89 committed Apr 2, 2014
1 parent 7161266 commit e095dfb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,48 @@
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.thoughtworks.androidbootcamp.Treasure;
import com.thoughtworks.androidbootcamp.util.Properties;

import java.util.List;

/**
* Created by trogdor on 5/03/14.
*/
public class TreasureListAdapter extends BaseAdapter{
Context context;
String[] treasurePaths;
List<Treasure> treasures;

public TreasureListAdapter(Context context, String[] paths) {
public TreasureListAdapter(Context context, List<Treasure> treasures) {
this.context = context;
this.treasurePaths = paths;
this.treasures = treasures;
}

@Override
public int getCount() {
return treasurePaths.length;
return treasures.size();
}

@Override
public Object getItem(int i) {
return treasurePaths[i];
return treasures.get(i);
}

@Override
public long getItemId(int i) {
return treasurePaths[i].hashCode();
return treasures.get(i).hashCode();
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ImageView imageView = (view == null) ? new ImageView(context) : (ImageView) view;
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Picasso.with(context).load("file:https://" + treasurePaths[i]).resize(640, 480).centerCrop().into(imageView);
String url = getUrlForTreasure(treasures.get(i));
Picasso.with(context).load(url).resize(640, 480).centerCrop().into(imageView);
return imageView;
}

public String getUrlForTreasure(Treasure treasure) {
return Properties.SERVICE_URL + "/" + treasure.getUrl();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
import android.widget.Toast;

import com.thoughtworks.androidbootcamp.R;
import com.thoughtworks.androidbootcamp.Treasure;
import com.thoughtworks.androidbootcamp.controller.adapter.TreasureListAdapter;
import com.thoughtworks.androidbootcamp.util.FileUtils;
import com.thoughtworks.androidbootcamp.util.Properties;
import com.thoughtworks.androidbootcamp.util.TreasureLoader;
import com.thoughtworks.androidbootcamp.util.TreasureService;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import retrofit.RestAdapter;

Expand Down Expand Up @@ -56,7 +59,10 @@ public TreasureListFragment() {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
treasureLoader = new TreasureLoader(getActivity());
treasureService = new RestAdapter.Builder()
.setEndpoint(Properties.SERVICE_URL)
.build()
.create(TreasureService.class);
}

@Override
Expand All @@ -70,7 +76,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
GridView gridView = (GridView) view.findViewById(R.id.treasure_list);
mTreasureListAdapter = new TreasureListAdapter(getActivity(), treasureLoader.getSampleImagePaths());
mTreasureListAdapter = new TreasureListAdapter(getActivity(), new ArrayList<Treasure>());
gridView.setAdapter(mTreasureListAdapter);

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
* Created by trogdor on 2/04/14.
*/
public class Properties {
static String SERVICE_URL = "http:https://android-bootcamp-rest-server.herokuapp.com";
public static String SERVICE_URL = "http:https://android-bootcamp-rest-server.herokuapp.com";
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Activity;
import android.content.Context;

import com.thoughtworks.androidbootcamp.Treasure;
import com.thoughtworks.androidbootcamp.controller.HelloAndroid;

import org.junit.Assert;
Expand All @@ -15,6 +16,9 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

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

import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

Expand All @@ -25,7 +29,24 @@ public class TreasureListAdapterTest {
@Test
public void shouldReturnItemCountAsNumberOfImagesProvided() throws Exception {
int numTreasures = 10;
TreasureListAdapter adapter = new TreasureListAdapter(Mockito.mock(Context.class), new String[numTreasures]);
List<Treasure> treasureList = new ArrayList<Treasure>();
for (int i=0; i< numTreasures; i++) {
treasureList.add(new Treasure());
}
TreasureListAdapter adapter = new TreasureListAdapter(Mockito.mock(Context.class), treasureList);
Assert.assertEquals(10, adapter.getCount());
}

@Config(emulateSdk = 18)
@Test
public void shouldReturnAbsoluteURLOfTreasure() throws Exception {
String expectedUrl = "http:https://android-bootcamp-rest-server.herokuapp.com/images/treasure.jpg";
Treasure treasure = new Treasure();
treasure.setUrl("images/treasure.jpg");
TreasureListAdapter adapter = new TreasureListAdapter(Mockito.mock(Context.class), null);

String generatedUrl = adapter.getUrlForTreasure(treasure);

Assert.assertEquals(expectedUrl, generatedUrl);
}
}

0 comments on commit e095dfb

Please sign in to comment.