Created
May 22, 2015 08:06
-
-
Save HugoGresse/9b6fbc0ec8e677235a73 to your computer and use it in GitHub Desktop.
Robolectric 3 rc2 asynchronous test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(RobolectricGradleTestRunner.class) | |
@Config(emulateSdk = 21, reportSdk = 21, constants = BuildConfig.class) | |
public class ServerClientTest extends GetAdResponseHandler { | |
private Transcript mTranscript; | |
public static final String LOG_TAG = "ServerClientTest"; | |
public static final String SERVER_URL = "/rich/"; | |
public static final String CONNECTION_NO_SETTINGS = "vastConnectionWithoutSettings.json"; | |
public static final String INLINE_NOTSETTINGS = "vastInlineWithoutSettings.json"; | |
static MockWebServer sMockServer; | |
@Before | |
public void setUp() throws IOException { | |
// This method is executed once, before the start of all tests | |
mTranscript = new Transcript(); | |
// Create a MockWebServer. These are lean enough that you can create a new | |
// instance for every unit test. | |
sMockServer = new MockWebServer(); | |
final Dispatcher dispatcher = new Dispatcher() { | |
@Override | |
public MockResponse dispatch(RecordedRequest request) throws InterruptedException { | |
String requestPath = request.getPath(); | |
switch (requestPath){ | |
case SERVER_URL + CONNECTION_NO_SETTINGS: | |
return new MockResponse().setResponseCode(200); | |
case SERVER_URL + INLINE_NOTSETTINGS: | |
return new MockResponse().setResponseCode(200); | |
} | |
return new MockResponse().setResponseCode(404); | |
} | |
}; | |
sMockServer.setDispatcher(dispatcher); | |
// Start the server. | |
sMockServer.play(); | |
} | |
@Test | |
public void isGooglePlayServicesAvailable_shouldGetSet() { | |
ShadowGooglePlayServicesUtil.setIsGooglePlayServicesAvailable(ConnectionResult.SUCCESS); | |
assertThat(GooglePlayServicesUtil.isGooglePlayServicesAvailable(RuntimeEnvironment.application)).isEqualTo(ConnectionResult.SUCCESS); | |
} | |
@Test | |
public void testLongTask() { | |
// Fails if the method takes longer than 7 seconds | |
ShadowGooglePlayServicesUtil.setIsGooglePlayServicesAvailable(ConnectionResult.SUCCESS); | |
ServerClient.setBaseUrl(sMockServer.getUrl("").toString()); | |
try { | |
ServerClient client = new ServerClient(RuntimeEnvironment.application); | |
client.setPlacement( | |
VAST_CONNECTION_NO_SETTINGS, | |
AdContent.PlacementAdType.PlacementAdTypeNativeVideo); | |
client.getAd(this); | |
} catch (Exception e){ | |
e.printStackTrace(); | |
} | |
/* wait for task code */ | |
ShadowApplication.runBackgroundTasks(); | |
mTranscript.assertEventsSoFar("onFailure"); | |
} | |
@After | |
public void tearDown() throws IOException { | |
// This method is executed once, after all tests have been finished | |
// Shut down the server. Instances cannot be reused. | |
sMockServer.shutdown(); | |
} | |
// ServerClient callbacks | |
@Override | |
public void onSuccess(AdContentData adContentData) { | |
mTranscript.add("onSuccess"); | |
assertTrue(false); | |
} | |
@Override | |
public void onFailure(TeadsError teadsError) { | |
mTranscript.add("onFailure"); | |
assertTrue(false); | |
} | |
// UTILS | |
public String getJsonsFiles(String file) throws IOException { | |
Application application = RuntimeEnvironment.application; | |
assertNotNull(application); | |
InputStream input = application.getAssets().open(file); | |
assertNotNull(input); | |
return ServerClientTest.convertStreamToString(input); | |
} | |
static String convertStreamToString(java.io.InputStream is) { | |
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); | |
return s.hasNext() ? s.next() : ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment