@Test
 public void shouldNotAttemptReconnectionIfHandlerHasQueuedAttempts() {
   Robolectric.pauseMainLooper();
   xmppReconnectionHandler.reconnectAfter(xmppCommunication, 1000);
   startService();
   Robolectric.idleMainLooper(500);
   assertFalse(xmppConnection.isConnected());
 }
  @Test
  public void shouldPostInvalidateDelayed() throws Exception {
    Robolectric.pauseMainLooper();

    view.postInvalidateDelayed(100);
    ShadowView shadowView = shadowOf(view);
    assertFalse(shadowView.wasInvalidated());

    Robolectric.unPauseMainLooper();
    assertTrue(shadowView.wasInvalidated());
  }
  @Test
  public void shouldPostActionsToTheMessageQueue() throws Exception {
    Robolectric.pauseMainLooper();

    TestRunnable runnable = new TestRunnable();
    view.post(runnable);
    assertFalse(runnable.wasRun);

    Robolectric.unPauseMainLooper();
    assertTrue(runnable.wasRun);
  }
  @Test
  public void shouldPostActionsToTheMessageQueueWithDelay() throws Exception {
    Robolectric.pauseMainLooper();

    TestRunnable runnable = new TestRunnable();
    view.postDelayed(runnable, 1);
    assertFalse(runnable.wasRun);

    Robolectric.getUiThreadScheduler().advanceBy(1);
    assertTrue(runnable.wasRun);
  }
  @Test
  public void clearPendingHttpResponses() throws Exception {
    Robolectric.addPendingHttpResponse(200, "earlier");
    Robolectric.clearPendingHttpResponses();
    Robolectric.addPendingHttpResponse(500, "later");

    HttpResponse response = requestDirector.execute(null, new HttpGet("http://some.uri"), null);

    assertNotNull(response);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(500));
    assertThat(Strings.fromStream(response.getEntity().getContent()), equalTo("later"));
  }
  @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(), equalTo(404));
  }
  @Test
  public void shouldRecordExtendedRequestData() throws Exception {
    Robolectric.addPendingHttpResponse(200, "a happy response body");
    HttpGet httpGet = new HttpGet("http://example.com");
    requestDirector.execute(null, httpGet, null);

    assertSame(Robolectric.getSentHttpRequestInfo(0).getHttpRequest(), httpGet);
    ConnectionKeepAliveStrategy strategy =
        shadowOf(
                (DefaultRequestDirector) Robolectric.getSentHttpRequestInfo(0).getRequestDirector())
            .getConnectionKeepAliveStrategy();
    assertSame(strategy, connectionKeepAliveStrategy);
  }
  @Test
  public void clearHttpResponseRules_shouldRemoveAllRules() throws Exception {
    Robolectric.addHttpResponseRule("http://some.uri", "a cheery response body");
    Robolectric.clearHttpResponseRules();
    Robolectric.addHttpResponseRule("http://some.uri", "a gloomy response body");

    HttpResponse response = requestDirector.execute(null, new HttpGet("http://some.uri"), null);

    assertNotNull(response);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
    assertThat(
        Strings.fromStream(response.getEntity().getContent()), equalTo("a gloomy response body"));
  }
  @Before
  public void setUp() throws Exception {
    Robolectric.bindDefaultShadowClasses();

    transcript = new Transcript();
    listView = new ListView(null);
  }
 @Before
 public void setUp() throws Exception {
   connectivityManager =
       (ConnectivityManager)
           Robolectric.application.getSystemService(Context.CONNECTIVITY_SERVICE);
   networkInfo = Robolectric.shadowOf(connectivityManager.getActiveNetworkInfo());
 }
  @Before
  public void setUp_EnsureStaticStateIsReset() {
    FakeHttpLayer fakeHttpLayer = Robolectric.getFakeHttpLayer();
    assertTrue(fakeHttpLayer.pendingHttpResponses.isEmpty());
    assertTrue(fakeHttpLayer.httpRequestInfos.isEmpty());
    assertTrue(fakeHttpLayer.httpResponseRules.isEmpty());
    assertNull(fakeHttpLayer.defaultHttpResponse);

    connectionKeepAliveStrategy =
        new ConnectionKeepAliveStrategy() {
          @Override
          public long getKeepAliveDuration(HttpResponse httpResponse, HttpContext httpContext) {
            return 0;
          }
        };
    requestDirector =
        new DefaultRequestDirector(
            null,
            null,
            null,
            connectionKeepAliveStrategy,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null);
  }
 private void setupCursor() throws Exception {
   Statement statement =
       connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
   resultSet = statement.executeQuery("SELECT * FROM table_name;");
   cursor = new SQLiteCursor(null, null, null, null);
   Robolectric.shadowOf(cursor).setResultSet(resultSet);
 }
  @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(), equalTo(200));
    assertThat(
        Strings.fromStream(response.getEntity().getContent()), equalTo("a happy response body"));
  }
  @Test
  public void shouldSupportBasicResponseHandlerHandleResponse() throws Exception {
    Robolectric.addPendingHttpResponseWithContentType(
        200, "OK", new BasicHeader("Content-Type", "text/plain"));

    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(new HttpGet("http://www.nowhere.org"));

    assertThat(
        ((HttpUriRequest) Robolectric.getSentHttpRequest(0)).getURI(),
        equalTo(URI.create("http://www.nowhere.org")));

    Assert.assertNotNull(response);
    String responseStr = new BasicResponseHandler().handleResponse(response);
    Assert.assertEquals("OK", responseStr);
  }
  @Test
  public void shouldHandleMultipleInvocationsOfExecute() throws Exception {
    Robolectric.addPendingHttpResponse(200, "a happy response body");
    Robolectric.addPendingHttpResponse(201, "another happy response body");

    requestDirector.execute(null, new HttpGet("http://example.com"), null);
    requestDirector.execute(null, new HttpGet("www.example.com"), null);

    HttpUriRequest request1 = (HttpUriRequest) Robolectric.getSentHttpRequest(0);
    assertThat(request1.getMethod(), equalTo(HttpGet.METHOD_NAME));
    assertThat(request1.getURI(), equalTo(URI.create("http://example.com")));

    HttpUriRequest request2 = (HttpUriRequest) Robolectric.getSentHttpRequest(1);
    assertThat(request2.getMethod(), equalTo(HttpGet.METHOD_NAME));
    assertThat(request2.getURI(), equalTo(URI.create("www.example.com")));
  }
 private void setupCursor() throws Exception {
   Statement statement =
       connection.createStatement(DatabaseConfig.getResultSetType(), ResultSet.CONCUR_READ_ONLY);
   String sql = "SELECT * FROM table_name;";
   resultSet = statement.executeQuery("SELECT * FROM table_name;");
   cursor = new SQLiteCursor(null, null, null, null);
   Robolectric.shadowOf(cursor).setResultSet(resultSet, sql);
 }
 @Test
 public void shouldUnregisterInterestInNetworkChangesOnDestroy() {
   startService();
   xmppService.onDestroy();
   List<ShadowApplication.Wrapper> wrappers =
       Robolectric.getShadowApplication().getRegisteredReceivers();
   Assert.assertEquals(0, wrappers.size());
 }
 @Test
 public void setCurrentItem_shouldSetListener() throws Exception {
   TestOnPageChangeListener listener = new TestOnPageChangeListener();
   pager.setOnPageChangeListener(listener);
   ViewPager.OnPageChangeListener onPageChangeListener =
       Robolectric.shadowOf(pager).getOnPageChangeListener();
   assertEquals(listener, onPageChangeListener);
 }
 @Test
 public void receiveShouldStartForwardService() {
   Context context = Robolectric.application.getApplicationContext();
   this.receiver.onReceive(context, null);
   Intent service = Robolectric.getShadowApplication().getNextStartedService();
   assertNotNull(service);
   assertEquals(ForwardService.class.getName(), service.getComponent().getClassName());
 }
  @Test
  public void shouldHandleMultipleInvocations() throws Exception {
    Robolectric.addPendingHttpResponse(200, "a happy response body");
    Robolectric.addPendingHttpResponse(201, "another happy response body");

    HttpResponse response1 = requestDirector.execute(null, new HttpGet("http://example.com"), null);
    HttpResponse response2 = requestDirector.execute(null, new HttpGet("www.example.com"), null);

    assertThat(response1.getStatusLine().getStatusCode(), equalTo(200));
    assertThat(
        Strings.fromStream(response1.getEntity().getContent()), equalTo("a happy response body"));

    assertThat(response2.getStatusLine().getStatusCode(), equalTo(201));
    assertThat(
        Strings.fromStream(response2.getEntity().getContent()),
        equalTo("another happy response body"));
  }
  @Test
  public void testMarketLaunch() {
    ShadowActivity shadowActivity = Robolectric.shadowOf(helloAndroidActivity);
    helloAndroidActivity.launchMarket();
    Intent startedIntent = shadowActivity.getNextStartedActivity();

    Uri marketUri = Uri.parse("market://details?id=com.example");
    assertEquals(marketUri, startedIntent.getData());
  }
  @Test
  public void shouldSendXMPPMessageWithCorrectIntent() {
    Robolectric.pauseMainLooper();
    startService();
    ShadowHandler.runMainLooperOneTask();

    sendXmppMessageViaIntent(TEST_MESSAGE);
    assertTrue(getShadowChatForXmppRecipient().hasSentMessage(TEST_MESSAGE));
  }
  @Test
  public void shouldGetHttpResponseFromExecuteSimpleApi() throws Exception {
    Robolectric.addPendingHttpResponse(200, "a happy response body");
    HttpResponse response = requestDirector.execute(null, new HttpGet("http://example.com"), null);

    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
    assertThat(
        Strings.fromStream(response.getEntity().getContent()), equalTo("a happy response body"));
  }
 @Test
 public void shouldThrowUnauthorizedErrorForInvalidUsernameAndPassword() throws IOException {
   Robolectric.getFakeHttpLayer().setDefaultHttpResponse(401, "some response body");
   loginButton.performClick();
   ShadowHandler.idleMainLooper();
   assertThat(
       ShadowToast.getTextOfLatestToast(),
       equalTo(loginActivity.getString(R.string.unauthorized)));
 }
  @Test
  public void putStringArrayListExtra_addsListToExtras() {
    Intent intent = new Intent();
    final ArrayList<String> strings = new ArrayList<String>(Arrays.asList("hi", "there"));

    intent.putStringArrayListExtra("KEY", strings);
    assertThat(intent.getStringArrayListExtra("KEY"), equalTo(strings));
    assertThat(
        Robolectric.shadowOf(intent.getExtras()).getStringArrayList("KEY"), equalTo(strings));
  }
  @Test
  public void putIntegerArrayListExtra_addsListToExtras() {
    Intent intent = new Intent();
    final ArrayList<Integer> integers = new ArrayList<Integer>(Arrays.asList(100, 200, 300));

    intent.putIntegerArrayListExtra("KEY", integers);
    assertThat(intent.getIntegerArrayListExtra("KEY"), equalTo(integers));
    assertThat(
        Robolectric.shadowOf(intent.getExtras()).getIntegerArrayList("KEY"), equalTo(integers));
  }
  @Test
  public void shouldLoginSuccessfullyForValidUserAndUrl() {
    Robolectric.getFakeHttpLayer().setDefaultHttpResponse(201, "some response body");

    loginButton.performClick();
    ShadowHandler.idleMainLooper();
    assertThat(
        ShadowToast.getTextOfLatestToast(),
        equalTo(loginActivity.getString(R.string.login_successful)));
  }
 @Test
 public void shouldThrowConnectionRefusedIfServerIsNotAvailable() throws IOException {
   serverUrl.setText("rapidftr.com:abcd");
   Robolectric.getFakeHttpLayer().setDefaultHttpResponse(404, "some response body");
   loginButton.performClick();
   ShadowHandler.idleMainLooper();
   assertThat(
       ShadowToast.getTextOfLatestToast(),
       equalTo(loginActivity.getString(R.string.server_not_reachable)));
 }
Exemple #29
0
 @Implementation
 public void postInvalidateDelayed(long delayMilliseconds) {
   Robolectric.getUiThreadScheduler()
       .postDelayed(
           new Runnable() {
             @Override
             public void run() {
               realView.invalidate();
             }
           },
           delayMilliseconds);
 }
  @Test
  public void shouldSaveServerUrlAfterSuccessfulLogin() {
    SharedPreferences sharedPreferences =
        Robolectric.application.getSharedPreferences("RAPIDFTR_PREFERENCES", Context.MODE_PRIVATE);
    sharedPreferences.edit().putString("SERVER_URL", "").commit();

    serverUrl.setText("http://dev.rapidftr.com:3000");
    Robolectric.getFakeHttpLayer().setDefaultHttpResponse(201, "some response body");

    loginButton.performClick();
    assertThat(
        sharedPreferences.getString("SERVER_URL", ""), equalTo(serverUrl.getText().toString()));
  }