@Test
  public void testInitializeLastEventId() throws JSONException {
    DatabaseHelper dbHelper = DatabaseHelper.getDatabaseHelper(context);

    String sourceName = Constants.PACKAGE_NAME + "." + context.getPackageName();
    SharedPreferences prefs = context.getSharedPreferences(sourceName, Context.MODE_PRIVATE);
    prefs.edit().putLong(Constants.PREFKEY_LAST_EVENT_ID, 3L).commit();

    amplitude.initialize(context, apiKey);
    Shadows.shadowOf(amplitude.logThread.getLooper()).runOneTask();

    assertEquals(amplitude.lastEventId, 3L);
    assertEquals((long) dbHelper.getLongValue(AmplitudeClient.LAST_EVENT_ID_KEY), 3L);

    amplitude.logEvent("testEvent");
    Shadows.shadowOf(amplitude.logThread.getLooper()).runToEndOfTasks();
    Shadows.shadowOf(amplitude.logThread.getLooper()).runToEndOfTasks();

    RecordedRequest request = runRequest(amplitude);
    JSONArray events = getEventsFromRequest(request);

    assertEquals(events.getJSONObject(0).getLong("event_id"), 1L);

    assertEquals(amplitude.lastEventId, 1L);
    assertEquals((long) dbHelper.getLongValue(AmplitudeClient.LAST_EVENT_ID_KEY), 1L);

    // verify shared prefs deleted
    assertEquals(prefs.getLong(Constants.PREFKEY_LAST_EVENT_ID, -1), -1);
  }
 @Test
 public void testFitCenterHandlesBitmapsWithNullConfigs() {
   Bitmap toFit = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
   Shadows.shadowOf(toFit).setConfig(null);
   Bitmap transformed = TransformationUtils.fitCenter(bitmapPool, toFit, 50, 50);
   assertEquals(Bitmap.Config.ARGB_8888, transformed.getConfig());
 }
  @Test
  public void testInitializeOptOut() {
    ShadowLooper looper = Shadows.shadowOf(amplitude.logThread.getLooper());

    String sourceName = Constants.PACKAGE_NAME + "." + context.getPackageName();
    SharedPreferences prefs = context.getSharedPreferences(sourceName, Context.MODE_PRIVATE);
    prefs.edit().putBoolean(Constants.PREFKEY_OPT_OUT, true).commit();

    DatabaseHelper dbHelper = DatabaseHelper.getDatabaseHelper(context);
    assertNull(dbHelper.getLongValue(AmplitudeClient.OPT_OUT_KEY));

    amplitude.initialize(context, apiKey);
    looper.runOneTask();

    assertTrue(amplitude.isOptedOut());
    assertEquals((long) dbHelper.getLongValue(AmplitudeClient.OPT_OUT_KEY), 1L);

    amplitude.setOptOut(false);
    looper.runOneTask();
    assertFalse(amplitude.isOptedOut());
    assertEquals((long) dbHelper.getLongValue(AmplitudeClient.OPT_OUT_KEY), 0L);

    // verify shared prefs deleted
    assertFalse(prefs.getBoolean(Constants.PREFKEY_OPT_OUT, false));
  }
  @Test
  public void testInitializeUserId() {

    // the userId passed to initialize should override any existing values
    String sourceName = Constants.PACKAGE_NAME + "." + context.getPackageName();
    SharedPreferences prefs = context.getSharedPreferences(sourceName, Context.MODE_PRIVATE);
    prefs.edit().putString(Constants.PREFKEY_USER_ID, "oldestUserId").commit();

    DatabaseHelper dbHelper = DatabaseHelper.getDatabaseHelper(context);
    dbHelper.insertOrReplaceKeyValue(AmplitudeClient.USER_ID_KEY, "oldUserId");

    String userId = "newUserId";
    amplitude.initialize(context, apiKey, userId);
    Shadows.shadowOf(amplitude.logThread.getLooper()).runOneTask();

    // Test that the user id is set.
    assertEquals(userId, amplitude.userId);
    assertEquals(userId, dbHelper.getValue(AmplitudeClient.USER_ID_KEY));

    // Test that events are logged.
    RecordedRequest request = sendEvent(amplitude, "init_test_event", null);
    assertNotNull(request);

    // verified shared prefs not deleted
    assertEquals(prefs.getString(Constants.PREFKEY_USER_ID, null), "oldestUserId");
  }
  @Test
  public void testOnStop() {
    ShadowActivity shadowActivity = Shadows.shadowOf(activity);

    activity.onStop();
    assertFalse(shadowActivity.isFinishing());
  }
  @Test
  public void testJustFinishIfCreatedAgain() throws Exception {
    ActivityController<LauncherSettingsActivity> controller =
        Robolectric.buildActivity(LauncherSettingsActivity.class).attach().create().resume();
    ShadowActivity shadowActivity = Shadows.shadowOf(controller.get());
    Assert.assertFalse(shadowActivity.isFinishing());
    controller.pause().stop();
    Assert.assertFalse(shadowActivity.isFinishing());
    Bundle state = new Bundle();
    controller.saveInstanceState(state).destroy();

    controller = Robolectric.buildActivity(LauncherSettingsActivity.class).attach().create(state);
    shadowActivity = Shadows.shadowOf(controller.get());
    Assert.assertFalse(shadowActivity.isFinishing());
    controller.resume();
    Assert.assertTrue(shadowActivity.isFinishing());
  }
 @Test
 public void testRotateImageExifHandlesBitmapsWithNullConfigs() {
   Bitmap toRotate = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
   Shadows.shadowOf(toRotate).setConfig(null);
   Bitmap rotated =
       TransformationUtils.rotateImageExif(
           bitmapPool, toRotate, ExifInterface.ORIENTATION_ROTATE_180);
   assertEquals(Bitmap.Config.ARGB_8888, rotated.getConfig());
 }
 @Implementation
 public void set(Matrix src) {
   reset();
   if (src != null) {
     ShadowMatrix shadowMatrix = Shadows.shadowOf(src);
     preOps.addAll(shadowMatrix.preOps);
     postOps.addAll(shadowMatrix.postOps);
     setOps.putAll(shadowMatrix.setOps);
   }
 }
  @Test
  public void testRefreshBluetooth() {
    ShadowActivity shadowActivity = Shadows.shadowOf(activity);

    activity.findViewById(R.id.refresh_bluetooth).performClick();
    assertEquals(
        "Unexpected toast text",
        shadowActivity.getString(R.string.scan_for_devices),
        ShadowToast.getTextOfLatestToast());
  }
 @Implementation
 public Intent registerReceiver(
     BroadcastReceiver receiver,
     IntentFilter filter,
     String broadcastPermission,
     Handler scheduler) {
   return ((ShadowApplication) Shadows.shadowOf(getApplicationContext()))
       .registerReceiverWithContext(
           receiver, filter, broadcastPermission, scheduler, realContextWrapper);
 }
  @Test
  public void testRotateImage() {
    Bitmap toRotate = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888);

    Bitmap zero = TransformationUtils.rotateImage(toRotate, 0);
    assertTrue(toRotate == zero);

    Bitmap ninety = TransformationUtils.rotateImage(toRotate, 90);
    assertTrue(Shadows.shadowOf(ninety).getDescription().contains("rotate=90.0"));
    assertEquals(toRotate.getWidth(), toRotate.getHeight());
  }
  @Test
  public void testBluetoothSelected_nochange() {
    ShadowActivity shadowActivity = Shadows.shadowOf(activity);
    activity.selectedItemTV.setText("No Device Selected!");

    activity.findViewById(R.id.done_selecting).performClick();
    assertEquals(
        "Unexpected toast text",
        shadowActivity.getString(R.string.no_change_to_device),
        ShadowToast.getTextOfLatestToast());
    assertTrue("finish() was not called", shadowActivity.isFinishing());
  }
  @Test
  public void testGetView() {
    List<View> views = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
      views.add(new View(RuntimeEnvironment.application));
    }

    Shadows.shadowOf(adapter).setViews(views);

    for (int i = 0; i < 5; i++) {
      assertThat(adapter.getView(i, null, null)).isSameAs(views.get(i));
    }
  }
 @Test
 public void testOnItemClick_startsFullPhotoViewIntent() {
   subject.setupPhotoList();
   RecyclerView recyclerView = subject.getPhotoRecyclerView();
   recyclerView.measure(0, 0);
   recyclerView.layout(0, 0, 100, 1000);
   RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(0);
   View view = viewHolder.itemView;
   view.performClick();
   Intent expectedIntent = new Intent(subject, PhotoFullScreenActivity.class);
   ShadowActivity shadowActivity = Shadows.shadowOf(subject);
   Intent actualIntent = shadowActivity.getNextStartedActivity();
   Assertions.assertThat(actualIntent).isNotNull();
   assertTrue(actualIntent.filterEquals(expectedIntent));
 }
  @Test
  public void testInitializePreviousSessionId() {
    DatabaseHelper dbHelper = DatabaseHelper.getDatabaseHelper(context);

    String sourceName = Constants.PACKAGE_NAME + "." + context.getPackageName();
    SharedPreferences prefs = context.getSharedPreferences(sourceName, Context.MODE_PRIVATE);
    prefs.edit().putLong(Constants.PREFKEY_PREVIOUS_SESSION_ID, 4000L).commit();

    amplitude.initialize(context, apiKey);
    Shadows.shadowOf(amplitude.logThread.getLooper()).runOneTask();

    assertEquals(amplitude.sessionId, 4000L);
    assertEquals((long) dbHelper.getLongValue(AmplitudeClient.PREVIOUS_SESSION_ID_KEY), 4000L);

    // verify shared prefs deleted
    assertEquals(prefs.getLong(Constants.PREFKEY_PREVIOUS_SESSION_ID, -1), -1);
  }
  @Test
  public void testInitializeLastEventTime() {
    DatabaseHelper dbHelper = DatabaseHelper.getDatabaseHelper(context);
    dbHelper.insertOrReplaceKeyLongValue(AmplitudeClient.LAST_EVENT_TIME_KEY, 5000L);

    String sourceName = Constants.PACKAGE_NAME + "." + context.getPackageName();
    SharedPreferences prefs = context.getSharedPreferences(sourceName, Context.MODE_PRIVATE);
    prefs.edit().putLong(Constants.PREFKEY_LAST_EVENT_TIME, 4000L).commit();

    amplitude.initialize(context, apiKey);
    Shadows.shadowOf(amplitude.logThread.getLooper()).runOneTask();

    assertEquals(amplitude.lastEventTime, 5000L);
    assertEquals((long) dbHelper.getLongValue(AmplitudeClient.LAST_EVENT_TIME_KEY), 5000L);

    // verify shared prefs deleted
    assertEquals(prefs.getLong(Constants.PREFKEY_LAST_EVENT_TIME, -1), 4000L);
  }
  @Test
  public void testSkipSharedPrefsToDb() {
    DatabaseHelper dbHelper = DatabaseHelper.getDatabaseHelper(context);
    dbHelper.insertOrReplaceKeyValue(AmplitudeClient.DEVICE_ID_KEY, "testDeviceId");
    dbHelper.insertOrReplaceKeyLongValue(AmplitudeClient.PREVIOUS_SESSION_ID_KEY, 1000L);
    dbHelper.insertOrReplaceKeyLongValue(AmplitudeClient.LAST_EVENT_TIME_KEY, 2000L);

    assertNull(dbHelper.getValue(AmplitudeClient.USER_ID_KEY));
    assertNull(dbHelper.getLongValue(AmplitudeClient.LAST_EVENT_ID_KEY));
    assertNull(dbHelper.getLongValue(AmplitudeClient.LAST_IDENTIFY_ID_KEY));
    assertNull(dbHelper.getLongValue(AmplitudeClient.OPT_OUT_KEY));

    String sourceName = Constants.PACKAGE_NAME + "." + context.getPackageName();
    SharedPreferences prefs = context.getSharedPreferences(sourceName, Context.MODE_PRIVATE);
    prefs.edit().putString(Constants.PREFKEY_DEVICE_ID, "otherDeviceId").commit();
    prefs.edit().putString(Constants.PREFKEY_USER_ID, "testUserId").commit();
    prefs.edit().putBoolean(Constants.PREFKEY_OPT_OUT, true).commit();
    prefs.edit().putLong(Constants.PREFKEY_LAST_IDENTIFY_ID, 3000L).commit();

    amplitude.initialize(context, apiKey);
    ShadowLooper looper = Shadows.shadowOf(amplitude.logThread.getLooper());
    looper.runOneTask();
    looper.runToEndOfTasks();

    assertEquals(dbHelper.getValue(AmplitudeClient.DEVICE_ID_KEY), "testDeviceId");
    assertEquals((long) dbHelper.getLongValue(AmplitudeClient.PREVIOUS_SESSION_ID_KEY), 1000L);
    assertEquals((long) dbHelper.getLongValue(AmplitudeClient.LAST_EVENT_TIME_KEY), 2000L);
    assertNull(dbHelper.getValue(AmplitudeClient.USER_ID_KEY));
    assertNull(dbHelper.getLongValue(AmplitudeClient.LAST_EVENT_ID_KEY));
    assertNull(dbHelper.getLongValue(AmplitudeClient.LAST_IDENTIFY_ID_KEY));
    assertNull(dbHelper.getLongValue(AmplitudeClient.OPT_OUT_KEY));

    assertEquals(prefs.getString(Constants.PREFKEY_DEVICE_ID, null), "otherDeviceId");
    assertEquals(prefs.getString(Constants.PREFKEY_USER_ID, null), "testUserId");
    assertTrue(prefs.getBoolean(Constants.PREFKEY_OPT_OUT, false));
    assertEquals(prefs.getLong(Constants.PREFKEY_LAST_IDENTIFY_ID, -1), 3000L);

    // after upgrade, pref values still there since they weren't deleted
    assertEquals(amplitude.deviceId, "testDeviceId");
    assertEquals(amplitude.previousSessionId, 1000L);
    assertEquals(amplitude.lastEventTime, 2000L);
    assertNull(amplitude.userId);
  }
  @Test
  public void testInitializePreviousSessionIdLastEventTime() {
    // set a previous session id & last event time
    // log an event with timestamp such that same session is continued
    // log second event with timestamp such that new session is started

    amplitude.setSessionTimeoutMillis(5000); // 5s

    String sourceName = Constants.PACKAGE_NAME + "." + context.getPackageName();
    SharedPreferences prefs = context.getSharedPreferences(sourceName, Context.MODE_PRIVATE);
    prefs.edit().putString(Constants.PREFKEY_DEVICE_ID, "testDeviceId").commit();
    prefs.edit().putLong(Constants.PREFKEY_PREVIOUS_SESSION_ID, 6000L).commit();
    prefs.edit().putLong(Constants.PREFKEY_LAST_EVENT_TIME, 6000L).commit();

    DatabaseHelper dbHelper = DatabaseHelper.getDatabaseHelper(context);
    dbHelper.insertOrReplaceKeyLongValue(AmplitudeClient.LAST_EVENT_TIME_KEY, 7000L);

    long[] timestamps = {8000, 14000};
    clock.setTimestamps(timestamps);

    amplitude.initialize(context, apiKey);
    ShadowLooper looper = Shadows.shadowOf(amplitude.logThread.getLooper());
    looper.runOneTask();
    looper.runToEndOfTasks();

    assertEquals(amplitude.deviceId, "testDeviceId");
    assertEquals(amplitude.previousSessionId, 6000L);
    assertEquals(amplitude.lastEventTime, 7000L);
    assertNull(amplitude.userId);

    // log first event
    amplitude.logEvent("testEvent1");
    looper.runToEndOfTasks();
    assertEquals(amplitude.previousSessionId, 6000L);
    assertEquals(amplitude.lastEventTime, 8000L);

    // log second event
    amplitude.logEvent("testEvent2");
    looper.runToEndOfTasks();
    assertEquals(amplitude.previousSessionId, 14000L);
    assertEquals(amplitude.lastEventTime, 14000L);
  }
  @Test
  public void testInitializeUserIdFromSharedPrefs() {
    String userId = "testUserId";
    String sourceName = Constants.PACKAGE_NAME + "." + context.getPackageName();
    SharedPreferences prefs = context.getSharedPreferences(sourceName, Context.MODE_PRIVATE);
    prefs.edit().putString(Constants.PREFKEY_USER_ID, userId).commit();

    DatabaseHelper dbHelper = DatabaseHelper.getDatabaseHelper(context);
    assertNull(dbHelper.getValue(AmplitudeClient.USER_ID_KEY));

    amplitude.initialize(context, apiKey);
    Shadows.shadowOf(amplitude.logThread.getLooper()).runOneTask();

    // Test that the user id is set.
    assertEquals(amplitude.userId, userId);
    assertEquals(userId, dbHelper.getValue(AmplitudeClient.USER_ID_KEY));

    // verify shared prefs deleted
    assertNull(prefs.getString(Constants.PREFKEY_USER_ID, null));
  }
  @Test
  public void testInitializeUserIdFromDb() {
    // since user id already exists in database, ignore old value in shared prefs
    String userId = "testUserId";
    String sourceName = Constants.PACKAGE_NAME + "." + context.getPackageName();
    SharedPreferences prefs = context.getSharedPreferences(sourceName, Context.MODE_PRIVATE);
    prefs.edit().putString(Constants.PREFKEY_USER_ID, "oldUserId").commit();

    DatabaseHelper dbHelper = DatabaseHelper.getDatabaseHelper(context);
    dbHelper.insertOrReplaceKeyValue(AmplitudeClient.USER_ID_KEY, userId);

    amplitude.initialize(context, apiKey);
    Shadows.shadowOf(amplitude.logThread.getLooper()).runOneTask();

    // Test that the user id is set.
    assertEquals(amplitude.userId, userId);
    assertEquals(userId, dbHelper.getValue(AmplitudeClient.USER_ID_KEY));

    // verify that shared prefs not deleted
    assertEquals("oldUserId", prefs.getString(Constants.PREFKEY_USER_ID, null));
  }
 public ShadowApplication getShadowApplication() {
   return ((ShadowApplication) Shadows.shadowOf(getApplicationContext()));
 }
 @Implementation
 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
   return ((ShadowApplication) Shadows.shadowOf(getApplicationContext()))
       .registerReceiverWithContext(receiver, filter, null, null, realContextWrapper);
 }
 public static void grantPermission(final Application app, final String permission) {
   ShadowApplication shadowApp = Shadows.shadowOf(app);
   shadowApp.grantPermissions(permission);
 }
 @Test
 public void shouldNotRegisterObserversIfNoFlagsAreSet() throws Exception {
   adapter = new TestAdapterWithFlags(curs, 0);
   assertThat(Shadows.shadowOf(adapter).mChangeObserver).isNull();
   assertThat(Shadows.shadowOf(adapter).mDataSetObserver).isNull();
 }
Example #25
0
 @Before
 public void setUp() {
   dialog = new ProgressDialog(Robolectric.application);
   shadow = Shadows.shadowOf(dialog);
 }
 @Test
 public void shouldRegisterObserversWhenRegisterObserverFlagIsSet() throws Exception {
   adapter = new TestAdapterWithFlags(curs, FLAG_REGISTER_CONTENT_OBSERVER);
   assertThat(Shadows.shadowOf(adapter).mChangeObserver).isNotNull();
   assertThat(Shadows.shadowOf(adapter).mDataSetObserver).isNotNull();
 }
 public List<Intent> getBroadcastIntents() {
   return ((ShadowApplication) Shadows.shadowOf(getApplicationContext())).getBroadcastIntents();
 }
 @Test
 public void shouldRegisterObserversWhenAutoRequeryFlagIsSet() throws Exception {
   adapter = new TestAdapterWithFlags(curs, FLAG_AUTO_REQUERY);
   assertThat(Shadows.shadowOf(adapter).mChangeObserver).isNotNull();
   assertThat(Shadows.shadowOf(adapter).mDataSetObserver).isNotNull();
 }
 private void clickOnAnItem() {
   ShadowListView shadowListView = Shadows.shadowOf(view);
   shadowListView.performItemClick(indexToClick);
 }