Android Open Source - droidcon Reddit Tests






From Project

Back to project page droidcon.

License

The source code is released under:

Apache License

If you think the Android project droidcon 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.timehop.droidcon2014retrofitsample.data.reddit;
/*from   www. j  av a 2  s . co  m*/
import android.app.Application;
import android.test.ApplicationTestCase;

import com.timehop.droidcon2014retrofitsample.data.SynchronousExecutor;
import com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditComment;
import com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditLink;
import com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditListing;
import com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditMore;
import com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditObject;
import com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditResponse;

import junit.framework.Assert;

import java.util.List;

import retrofit.Callback;
import retrofit.MockRestAdapter;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class RedditTests extends ApplicationTestCase<Application> {
  private RedditService mService;

  public RedditTests() {
    super(Application.class);
  }

  /**
   * Sets up the Reddit service with a synchronous executor and full logging
   * @throws Exception
   */
  @Override
  protected void setUp() throws Exception {
    super.setUp();
    mService = RedditService.Implementation.getBuilder()
        .setExecutors(new SynchronousExecutor(), new SynchronousExecutor())
        .setLogLevel(RestAdapter.LogLevel.FULL)
        .build()
        .create(RedditService.class);
  }

  /**
   * Example of getting Reddit comments synchronously with Retrofit
   */
  public void testGetComments() {
    List<RedditResponse<RedditListing>> responses = mService.getComments("AskReddit", "2gp9iv");
    assertComments(responses);
  }

  /**
   * Example of getting Reddit comments asynchronously with Retrofit
   */
  public void testGetCommentsAsync() {
    mService.getComments("AskReddit", "2gp9iv", new Callback<List<RedditResponse<RedditListing>>>() {
      @Override
      public void success(List<RedditResponse<RedditListing>> responses, Response response) {
        assertComments(responses);
      }

      @Override
      public void failure(RetrofitError error) {
        // request failed, fail the test
        fail();
      }
    });
  }

  /**
   * Example demonstrating a mock client
   */
  public void testMockClient() {
    MockRedditService mockService = new MockRedditService();
    List<RedditResponse<RedditListing>> response = mockService.getComments("AskReddit", "2gp9iv");
    assertComments(response);
  }

  /**
   * Example demonstrating a mock client asynchronously
   */
  public void testMockClientAsync() {
    MockRedditService mockService = new MockRedditService();
    mockService.getComments("AskReddit", "2gp9iv", new Callback<List<RedditResponse<RedditListing>>>() {
      @Override
      public void success(List<RedditResponse<RedditListing>> redditResponses, Response response) {
        assertComments(redditResponses);
      }

      @Override
      public void failure(RetrofitError error) {
        fail("Should not fail");
      }
    });
  }

  /**
   * Example using a mock client with delay
   */
  public void testMockClientWithDelay() {
    RestAdapter restAdapter = RedditService.Implementation.getBuilder().build();
    MockRestAdapter mockRestAdapter = MockRestAdapter.from(restAdapter);
    mockRestAdapter.setDelay(2500);
    RedditService service = mockRestAdapter.create(RedditService.class, new MockRedditService());
    long start = System.currentTimeMillis();
    List<RedditResponse<RedditListing>> response = service.getComments("AskReddit", "2gp9iv");
    long end = System.currentTimeMillis();
    assertTrue(end > start + 2000);
    assertComments(response);
  }

  /**
   * helper function to assert comments response is valid
   */
  private void assertComments(List<RedditResponse<RedditListing>> response) {
    // very the first listing only has links
    for (RedditObject child : response.get(0).getData().getChildren()) {
      Assert.assertTrue(child instanceof RedditLink);
    }

    // verify the second listing only has comments or 'more's
    for (RedditObject child : response.get(1).getData().getChildren()) {
      Assert.assertTrue(child instanceof RedditComment || child instanceof RedditMore);
    }
  }
}




Java Source Code List

com.timehop.droidcon2014retrofitsample.CommentsActivity.java
com.timehop.droidcon2014retrofitsample.MainActivity.java
com.timehop.droidcon2014retrofitsample.SubredditActivity.java
com.timehop.droidcon2014retrofitsample.VenueSearchActivity.java
com.timehop.droidcon2014retrofitsample.data.SynchronousExecutor.java
com.timehop.droidcon2014retrofitsample.data.foursquare.FoursquareService.java
com.timehop.droidcon2014retrofitsample.data.foursquare.FoursquareTests.java
com.timehop.droidcon2014retrofitsample.data.foursquare.api.FoursquareCredentials.java
com.timehop.droidcon2014retrofitsample.data.foursquare.api.FoursquareErrorHandler.java
com.timehop.droidcon2014retrofitsample.data.foursquare.api.FoursquareException.java
com.timehop.droidcon2014retrofitsample.data.foursquare.api.FoursquareRequestInterceptor.java
com.timehop.droidcon2014retrofitsample.data.foursquare.api.FoursquareResponse.java
com.timehop.droidcon2014retrofitsample.data.foursquare.legacy.VenueSearchTask.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Category.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Contact.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Icon.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Location.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Menu.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Meta.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Price.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.ResponseWrapper.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Stats.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Venue.java
com.timehop.droidcon2014retrofitsample.data.reddit.DateTimeDeserializer.java
com.timehop.droidcon2014retrofitsample.data.reddit.MockRedditService.java
com.timehop.droidcon2014retrofitsample.data.reddit.RedditObjectDeserializer.java
com.timehop.droidcon2014retrofitsample.data.reddit.RedditService.java
com.timehop.droidcon2014retrofitsample.data.reddit.RedditTests.java
com.timehop.droidcon2014retrofitsample.data.reddit.RedditType.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.MockModelHelper.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditAccount.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditComment.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditLink.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditListing.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditMore.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditObjectWrapper.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditObject.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditResponse.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditSubmission.java
com.timehop.droidcon2014retrofitsample.ui.CommentsAdapter.java
com.timehop.droidcon2014retrofitsample.ui.LinkAdapter.java
com.timehop.droidcon2014retrofitsample.ui.VenueAdapter.java