@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 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);
  }