@Before public void setup() { activity = Robolectric.buildActivity(MockMainActivity.class).create().get(); // activity = Robolectric.setupActivity(MockMainActivity.class); shadowOf(activity).grantPermissions("android.permission.INTERNET"); server = new MockWebServer(); try { server.start(); successfulMockServerStarted = true; } catch (IOException e) { e.printStackTrace(); } FakeHttp.getFakeHttpLayer().interceptHttpRequests(true); FakeHttp.getFakeHttpLayer().interceptResponseContent(true); bgScheduler = Robolectric.getBackgroundThreadScheduler(); uiScheduler = Robolectric.getForegroundThreadScheduler(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); bgScheduler.pause(); uiScheduler.pause(); }
@Test public void testResponseCodeNot200() throws Exception{ final boolean[] responseCalled = {false}; FakeHttp.getFakeHttpLayer().addPendingHttpResponse(404, "some content that isn't JSON"); String theURL = "dummyURL"; byte[] optionalContentData = null; SKSimpleHttpToJsonQuery simpleHttpToJsonQuery = new SKSimpleHttpToJsonQuery(theURL, optionalContentData, new SKSimpleHttpToJsonQuery.QueryCompletion() { @Override public void OnQueryCompleted(boolean queryWasSuccessful, JSONObject jsonResponse) { responseCalled[0] = true; XCT.Assert(queryWasSuccessful == false); XCT.Assert(jsonResponse == null); } }); simpleHttpToJsonQuery.doPerformQuery(); XCT.Assert(responseCalled[0]); }
@Test public void testBadJSONData() throws Exception{ final boolean[] responseCalled = {false}; FakeHttp.getFakeHttpLayer().addPendingHttpResponse(200, "some content that isn't JSON"); String theURL = "dummyURL"; byte[] optionalContentData = null; SKSimpleHttpToJsonQuery simpleHttpToJsonQuery = new SKSimpleHttpToJsonQuery(theURL, optionalContentData, new SKSimpleHttpToJsonQuery.QueryCompletion() { @Override public void OnQueryCompleted(boolean queryWasSuccessful, JSONObject jsonResponse) { responseCalled[0] = true; XCT.Assert(queryWasSuccessful == false); XCT.Assert(jsonResponse == null); } }); simpleHttpToJsonQuery.doPerformQuery(); XCT.Assert(responseCalled[0]); }
@Test public void testResponseCodeNot200GoodJSONData() throws Exception{ final boolean[] responseCalled = {false}; FakeHttp.getFakeHttpLayer().addPendingHttpResponse(400, validJSONString); String theURL = "dummyURL"; byte[] optionalContentData = null; SKSimpleHttpToJsonQuery simpleHttpToJsonQuery = new SKSimpleHttpToJsonQuery(theURL, optionalContentData, new SKSimpleHttpToJsonQuery.QueryCompletion() { @Override public void OnQueryCompleted(boolean queryWasSuccessful, JSONObject jsonResponse) { responseCalled[0] = true; // Note that the CURRENT Implementation has this query treated with success, EVEN THOUGH the code is not 200! XCT.Assert(queryWasSuccessful == true); XCT.Assert(jsonResponse != null); } }); simpleHttpToJsonQuery.doPerformQuery(); XCT.Assert(responseCalled[0]); }
@Test public void testResponseCode200GoodJSONData() throws Exception{ final boolean[] responseCalled = {false}; FakeHttp.getFakeHttpLayer().addPendingHttpResponse(200, validJSONString); String theURL = "dummyURL"; byte[] optionalContentData = null; SKSimpleHttpToJsonQuery simpleHttpToJsonQuery = new SKSimpleHttpToJsonQuery(theURL, optionalContentData, new SKSimpleHttpToJsonQuery.QueryCompletion() { @Override public void OnQueryCompleted(boolean queryWasSuccessful, JSONObject jsonResponse) { responseCalled[0] = true; XCT.Assert(queryWasSuccessful == true); XCT.Assert(jsonResponse != null); } }); simpleHttpToJsonQuery.doPerformQuery(); XCT.Assert(responseCalled[0]); }
@Test public void testPostResponseCode200GoodJSONData() throws Exception{ final boolean[] responseCalled = {false}; FakeHttp.getFakeHttpLayer().addPendingHttpResponse(200, validJSONString); String theURL = "dummyURL"; byte[] optionalContentData = null; SKSimpleHttpToJsonQuery simpleHttpToJsonQuery = new SKSimpleHttpToJsonQuery(theURL, optionalContentData, new SKSimpleHttpToJsonQuery.QueryCompletion() { @Override public void OnQueryCompleted(boolean queryWasSuccessful, JSONObject jsonResponse) { responseCalled[0] = true; XCT.Assert(queryWasSuccessful == true); XCT.Assert(jsonResponse != null); } }); simpleHttpToJsonQuery.doPerformPost(); XCT.Assert(responseCalled[0]); }
public static VinliApp getVinliApp(){ if(vinliApp == null){ VinliApp.clientBuilder = generateUnsafeBuilder(); Endpoint.setDomain(Endpoint.DOMAIN_DEV); FakeHttp.getFakeHttpLayer().interceptHttpRequests(false); vinliApp = new VinliApp(getAccessToken()); Vinli.setCurrentApp(vinliApp); } return vinliApp; }
public static VinliApp getVehicleVinliApp(){ if(vehicleVinliApp == null){ VinliApp.clientBuilder = generateUnsafeBuilder(); Endpoint.setDomain(Endpoint.DOMAIN_DEV); FakeHttp.getFakeHttpLayer().interceptHttpRequests(false); vehicleVinliApp = new VinliApp(getVehicleAccessToken()); } return vehicleVinliApp; }
@org.junit.Before public void setUp() throws Exception { FakeHttp.getFakeHttpLayer().interceptHttpRequests(true); requestMatcherBuilder = new FakeHttpLayer.RequestMatcherBuilder(); }