@Before public void setUp() throws Exception { context = Robolectric.buildActivity(Activity.class).create().get(); bundle = new Bundle(); baseVideoViewControllerListener = mock(BaseVideoViewControllerListener.class); bundle.putString(VIDEO_URL, "http://video_url"); Robolectric.getUiThreadScheduler().pause(); Robolectric.getBackgroundScheduler().pause(); Robolectric.addHttpResponseRule(new RequestMatcher() { @Override public boolean matches(HttpRequest request) { return true; } }, new TestHttpResponse(200, "body")); ShadowLocalBroadcastManager.getInstance(context).registerReceiver(broadcastReceiver, getHtmlInterstitialIntentFilter()); }
@Before public void setup() { context = Robolectric.buildActivity(Activity.class).create().get(); userAgent = new WebView(context).getSettings().getUserAgentString(); Robolectric.addHttpResponseRule(new RequestMatcher() { @Override public boolean matches(HttpRequest request) { return true; } }, new TestHttpResponse(200, "body")); HttpClient.setWebViewUserAgent(null); Robolectric.getBackgroundScheduler().pause(); Robolectric.clearPendingHttpResponses(); }
@Test public void shouldReturnRequestsByRule_KeepsTrackOfOpenContentStreams() throws Exception { TestHttpResponse testHttpResponse = new TestHttpResponse(200, "a cheery response body"); Robolectric.addHttpResponseRule("http://some.uri", testHttpResponse); assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed()).isTrue(); HttpResponse getResponse = requestDirector.execute(null, new HttpGet("http://some.uri"), null); InputStream getResponseStream = getResponse.getEntity().getContent(); assertThat(Strings.fromStream(getResponseStream)).isEqualTo("a cheery response body"); assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed()).isFalse(); HttpResponse postResponse = requestDirector.execute(null, new HttpPost("http://some.uri"), null); InputStream postResponseStream = postResponse.getEntity().getContent(); assertThat(Strings.fromStream(postResponseStream)).isEqualTo("a cheery response body"); assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed()).isFalse(); getResponseStream.close(); assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed()).isFalse(); postResponseStream.close(); assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed()).isTrue(); }
@Test public void shouldReturnRequestsByRule_WithCustomRequestMatcher() throws Exception { Robolectric.setDefaultHttpResponse(404, "no such page"); Robolectric.addHttpResponseRule(new RequestMatcher() { @Override public boolean matches(HttpRequest request) { return request.getRequestLine().getUri().equals("http://matching.uri"); } }, new TestHttpResponse(200, "a cheery response body")); HttpResponse response = requestDirector.execute(null, new HttpGet("http://matching.uri"), null); assertNotNull(response); assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); assertThat(Strings.fromStream(response.getEntity().getContent())).isEqualTo("a cheery response body"); response = requestDirector.execute(null, new HttpGet("http://non-matching.uri"), null); assertNotNull(response); assertThat(response.getStatusLine().getStatusCode()).isEqualTo(404); assertThat(Strings.fromStream(response.getEntity().getContent())).isEqualTo("no such page"); }
@Test public void shouldSupportConnectionTimeoutWithExceptions() throws Exception { Robolectric.setDefaultHttpResponse(new TestHttpResponse() { @Override public HttpParams getParams() { HttpParams httpParams = super.getParams(); HttpConnectionParams.setConnectionTimeout(httpParams, -1); return httpParams; } }); DefaultHttpClient client = new DefaultHttpClient(); try { client.execute(new HttpGet("http://www.nowhere.org")); } catch (ConnectTimeoutException x) { return; } fail("Exception should have been thrown"); }
@Test public void shouldSupportSocketTimeoutWithExceptions() throws Exception { Robolectric.setDefaultHttpResponse(new TestHttpResponse() { @Override public HttpParams getParams() { HttpParams httpParams = super.getParams(); HttpConnectionParams.setSoTimeout(httpParams, -1); return httpParams; } }); DefaultHttpClient client = new DefaultHttpClient(); try { client.execute(new HttpGet("http://www.nowhere.org")); } catch (ConnectTimeoutException x) { return; } fail("Exception should have been thrown"); }
@Test public void createTokenReturnsConnectionError() throws Exception { HttpResponseStub response = new TestHttpResponse(200, "") { @Override public HttpEntity getEntity() { return new InputStreamEntity(new StringInputStream("foo"), 3) { @Override public InputStream getContent() throws IOException { throw new IOException("Test exception"); } }; } }; Robolectric.addPendingHttpResponse(response); Throwable throwable = createTokenThenError(ApiSample.testCard); assertThat(throwable, instanceOf(IOException.class)); assertEquals(throwable.getMessage(), "Test exception"); }
/** * <p>Test for a request which expects the raw {@link HttpResponse}.</p> * * @since 1.3.0 */ @Test public final void testRawResponse() throws ParseException, IOException { String subpath = "/rawresponse"; String url = "http://0.0.0.0:8080" + subpath; String body = "Welcome to the Republic of Genosha"; Robolectric.addHttpResponseRule(HttpGet.METHOD_NAME, url, new TestHttpResponse(200, body)); Object response = responseEndpoint.rawResponse(); assertNotNull(response); assertTrue(response instanceof HttpResponse); assertEquals(EntityUtils.toString(((HttpResponse)response).getEntity()), body); }
/** * <p>Test for a request which expects the raw {@link HttpEntity}.</p> * * @since 1.3.0 */ @Test public final void testRawEntity() throws ParseException, IOException { String subpath = "/rawentity"; String url = "http://0.0.0.0:8080" + subpath; String body = "Hulk, make me a sandwich"; Robolectric.addHttpResponseRule(HttpGet.METHOD_NAME, url, new TestHttpResponse(200, body)); Object response = responseEndpoint.rawEntity(); assertNotNull(response); assertTrue(response instanceof HttpEntity); assertEquals(EntityUtils.toString(((HttpEntity)response)), body); }
@Test public void execute_whenResponseContentLengthIsLargerThan25MiB_shouldNotPutDataInCacheAndShouldSignalDownloadFailed() throws Exception { Robolectric.clearPendingHttpResponses(); final String randomString = createRandomString(25 * 1024 * 1024 + 1); Robolectric.addPendingHttpResponse(new TestHttpResponse(200, randomString)); subject.execute(videoUrl); semaphore.acquire(); CacheServiceTest.assertDiskCacheIsEmpty(); verify(mVastVideoDownloadTaskListener).onComplete(false); }
@Test public void shouldGetHttpResponseFromExecute() throws Exception { Robolectric.addPendingHttpResponse(new TestHttpResponse(200, "a happy response body")); HttpResponse response = requestDirector.execute(null, new HttpGet("http://example.com"), null); assertNotNull(response); assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); assertThat(Strings.fromStream(response.getEntity().getContent())).isEqualTo("a happy response body"); }
@Test public void shouldPreferPendingResponses() throws Exception { Robolectric.addPendingHttpResponse(new TestHttpResponse(200, "a happy response body")); Robolectric.addHttpResponseRule(HttpGet.METHOD_NAME, "http://some.uri", new TestHttpResponse(200, "a cheery response body")); HttpResponse response = requestDirector.execute(null, new HttpGet("http://some.uri"), null); assertNotNull(response); assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); assertThat(Strings.fromStream(response.getEntity().getContent())).isEqualTo("a happy response body"); }
@Test public void shouldReturnRequestsByRule() throws Exception { Robolectric.addHttpResponseRule(HttpGet.METHOD_NAME, "http://some.uri", new TestHttpResponse(200, "a cheery response body")); HttpResponse response = requestDirector.execute(null, new HttpGet("http://some.uri"), null); assertNotNull(response); assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); assertThat(Strings.fromStream(response.getEntity().getContent())).isEqualTo("a cheery response body"); }
@Test public void shouldReturnRequestsByRule_MatchingMethod() throws Exception { Robolectric.setDefaultHttpResponse(404, "no such page"); Robolectric.addHttpResponseRule(HttpPost.METHOD_NAME, "http://some.uri", new TestHttpResponse(200, "a cheery response body")); HttpResponse response = requestDirector.execute(null, new HttpGet("http://some.uri"), null); assertNotNull(response); assertThat(response.getStatusLine().getStatusCode()).isEqualTo(404); }
@Test public void shouldReturnRequestsByRule_AnyMethod() throws Exception { Robolectric.addHttpResponseRule("http://some.uri", new TestHttpResponse(200, "a cheery response body")); HttpResponse getResponse = requestDirector.execute(null, new HttpGet("http://some.uri"), null); assertNotNull(getResponse); assertThat(getResponse.getStatusLine().getStatusCode()).isEqualTo(200); assertThat(Strings.fromStream(getResponse.getEntity().getContent())).isEqualTo("a cheery response body"); HttpResponse postResponse = requestDirector.execute(null, new HttpPost("http://some.uri"), null); assertNotNull(postResponse); assertThat(postResponse.getStatusLine().getStatusCode()).isEqualTo(200); assertThat(Strings.fromStream(postResponse.getEntity().getContent())).isEqualTo("a cheery response body"); }
@Test public void shouldReturnResponseFromHttpResponseGenerator() throws Exception { Robolectric.addPendingHttpResponse(new HttpResponseGenerator() { @Override public HttpResponse getResponse(HttpRequest request) { return new TestHttpResponse(200, "a happy response body"); } }); HttpResponse response = requestDirector.execute(null, new HttpGet("http://example.com"), null); assertNotNull(response); assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200); assertThat(Strings.fromStream(response.getEntity().getContent())).isEqualTo("a happy response body"); }
@Test public void createTokenReturnsJSONException() throws Exception { TestHttpResponse response = new TestHttpResponse(201, "{:}", new BasicHeader("Content-Type", "application/json")); Robolectric.addPendingHttpResponse(response); Throwable throwable = createTokenThenError(ApiSample.testCard); assertThat(throwable, instanceOf(JSONException.class)); assertEquals(throwable.getMessage(), "Expected literal value at character 1 of {:}"); }
/** * <p>Test for a request which expects the raw {@link HttpEntity}.</p> * * @since 1.3.0 */ @Test public final void testNoDeserializer() { String subpath = "/nodeserializer"; String url = "http://0.0.0.0:8080" + subpath; String body = new Gson().toJson(new User(1, "Cain", "Marko", 37, false)); Robolectric.addHttpResponseRule(HttpGet.METHOD_NAME, url, new TestHttpResponse(200, body)); expectedException.expect(Is.isA(InvocationException.class)); responseEndpoint.noDeserializer(); }
@TargetApi(VERSION_CODES.GINGERBREAD_MR1) @Before public void setUp() throws Exception { Networking.setRequestQueueForTesting(mockRequestQueue); Networking.setImageLoaderForTesting(mockImageLoader); context = Robolectric.buildActivity(Activity.class).create().get(); bundle = new Bundle(); savedInstanceState = new Bundle(); testBroadcastIdentifier = 1111; VastVideoConfiguration vastVideoConfiguration = new VastVideoConfiguration(); vastVideoConfiguration.setNetworkMediaFileUrl("video_url"); vastVideoConfiguration.setDiskMediaFileUrl("disk_video_path"); vastVideoConfiguration.addAbsoluteTrackers( Arrays.asList(new VastAbsoluteProgressTracker("start" + MACRO_TAGS, 2000))); vastVideoConfiguration.addFractionalTrackers( Arrays.asList(new VastFractionalProgressTracker("first" + MACRO_TAGS, 0.25f), new VastFractionalProgressTracker("mid" + MACRO_TAGS, 0.5f), new VastFractionalProgressTracker("third" + MACRO_TAGS, 0.75f))); vastVideoConfiguration.addPauseTrackers( Arrays.asList(new VastTracker("pause" + MACRO_TAGS, true))); vastVideoConfiguration.addResumeTrackers( Arrays.asList(new VastTracker("resume" + MACRO_TAGS, true))); vastVideoConfiguration.addCompleteTrackers( VastUtils.stringsToVastTrackers("complete" + MACRO_TAGS)); vastVideoConfiguration.addCloseTrackers( VastUtils.stringsToVastTrackers("close" + MACRO_TAGS)); vastVideoConfiguration.addSkipTrackers(VastUtils.stringsToVastTrackers("skip" + MACRO_TAGS)); vastVideoConfiguration.addImpressionTrackers( VastUtils.stringsToVastTrackers("imp" + MACRO_TAGS)); vastVideoConfiguration.addErrorTrackers( Collections.singletonList(new VastTracker("error" + MACRO_TAGS))); vastVideoConfiguration.setClickThroughUrl(CLICKTHROUGH_URL); vastVideoConfiguration.addClickTrackers( VastUtils.stringsToVastTrackers("click_1" + MACRO_TAGS, "click_2" + MACRO_TAGS)); VastCompanionAd vastCompanionAd = new VastCompanionAd( 300, 250, new VastResource(COMPANION_IMAGE_URL, VastResource.Type.STATIC_RESOURCE, VastResource.CreativeType.IMAGE, 300, 250), COMPANION_CLICK_DESTINATION_URL, VastUtils.stringsToVastTrackers(COMPANION_CLICK_TRACKING_URL_1, COMPANION_CLICK_TRACKING_URL_2), VastUtils.stringsToVastTrackers(COMPANION_CREATIVE_VIEW_URL_1, COMPANION_CREATIVE_VIEW_URL_2) ); vastVideoConfiguration.setVastCompanionAd(vastCompanionAd); when(mockVastIcon.getWidth()).thenReturn(40); when(mockVastIcon.getHeight()).thenReturn(40); VastResource vastResource = mock(VastResource.class); when(vastResource.getType()).thenReturn(VastResource.Type.STATIC_RESOURCE); when(vastResource.getResource()).thenReturn("static"); when(vastResource.getCreativeType()).thenReturn(VastResource.CreativeType.IMAGE); when(mockVastIcon.getVastResource()).thenReturn(vastResource); vastVideoConfiguration.setVastIcon(mockVastIcon); when(mockMediaMetadataRetriever.getFrameAtTime(anyLong(), anyInt())).thenReturn(mockBitmap); bundle.putSerializable(VAST_VIDEO_CONFIGURATION, vastVideoConfiguration); expectedBrowserRequestCode = 1; Robolectric.getUiThreadScheduler().pause(); Robolectric.getBackgroundScheduler().pause(); Robolectric.clearPendingHttpResponses(); // Used to give responses to Vast Download Tasks. Robolectric.addHttpResponseRule(new RequestMatcher() { @Override public boolean matches(HttpRequest request) { return true; } }, new TestHttpResponse(200, "body")); ShadowLocalBroadcastManager.getInstance(context).registerReceiver(broadcastReceiver, getHtmlInterstitialIntentFilter()); expectedUserAgent = new WebView(context).getSettings().getUserAgentString(); }
protected void setUpSuccessHttpGetRequest(String uri, String fixtureFile) throws IOException { String rootDataSetJson = readFixtureFile(fixtureFile); Robolectric.addHttpResponseRule("GET", String.format("%s%s", dhis2BaseUrl, uri), new TestHttpResponse(200, rootDataSetJson)); }
protected void setUpSuccessHttpPostRequest(String uri, String fixtureFile) throws IOException { String rootDataSetJson = readFixtureFile(fixtureFile); Robolectric.addHttpResponseRule("POST", String.format("%s%s", dhis2BaseUrl, uri), new TestHttpResponse(200, rootDataSetJson)); }