public void testEventsNotBuffered() {
    Context context = activity.getApplicationContext();

    // starting from a clean slate
    mockLogger.test(
        "Was AdjustActivityState deleted? " + ActivityHandler.deleteActivityState(context));

    ActivityHandler activityHandler = new ActivityHandler(activity);
    // start the first session
    activityHandler.trackSubsessionStart();

    // construct the parameters of the the event
    Map<String, String> eventParameters = new HashMap<String, String>();
    eventParameters.put("key", "value");
    eventParameters.put("foo", "bar");

    // the first event has parameters, the second none
    activityHandler.trackEvent("abc123", null);

    activityHandler.trackRevenue(0, null, null);
    SystemClock.sleep(2000);

    // check that the package added the session, event and revenue package
    assertEquals(mockLogger.toString(), 3, mockPackageHandler.queue.size());

    // check the first event
    ActivityPackage activityPackage = mockPackageHandler.queue.get(1);
    Map<String, String> packageParameters = activityPackage.getParameters();

    // check that it contains the information of the tracking being enabled
    assertNotNull(activityPackage.getExtendedString(), packageParameters.get("tracking_enabled"));

    // check the event count in the package parameters
    assertEquals(
        activityPackage.getExtendedString(),
        1,
        Integer.parseInt(packageParameters.get("event_count")));

    // check the event token
    assertEquals(
        activityPackage.getExtendedString(), "abc123", packageParameters.get("event_token"));

    // check the that the parameters were not injected
    assertNull(activityPackage.getExtendedString(), packageParameters.get("params"));

    // check the event path
    assertEquals(activityPackage.getExtendedString(), "/event", activityPackage.getPath());

    // check the event suffix
    assertEquals(activityPackage.getExtendedString(), " 'abc123'", activityPackage.getSuffix());

    // check that the package handler was called
    assertTrue(
        mockLogger.toString(), mockLogger.containsTestMessage("PackageHandler sendFirstPackage"));

    // check the event count in the written activity state
    assertTrue(
        mockLogger.toString(),
        mockLogger.containsMessage(LogLevel.DEBUG, "Wrote activity state: ec:1"));

    // check the event count in the logger
    assertTrue(mockLogger.toString(), mockLogger.containsMessage(LogLevel.DEBUG, "Event 1"));

    // check the fourth event/ second revenue
    activityPackage = mockPackageHandler.queue.get(2);
    packageParameters = activityPackage.getParameters();

    // check the event count in the package parameters
    assertEquals(
        activityPackage.getExtendedString(),
        2,
        Integer.parseInt(packageParameters.get("event_count")));

    // check the amount, transforming cents into rounded decimal cents
    // note that the 4.45 cents ~> 45 decimal cents
    assertEquals(
        activityPackage.getExtendedString(), 0, Long.parseLong(packageParameters.get("amount")));

    // check that the event token is null
    assertNull(activityPackage.getExtendedString(), packageParameters.get("event_token"));

    // check the that the parameters were not injected
    assertNull(activityPackage.getExtendedString(), packageParameters.get("params"));

    // check the revenue path
    assertEquals(activityPackage.getExtendedString(), "/revenue", activityPackage.getPath());

    // check the revenue suffix
    // note that the amount was rounded to the decimal cents
    assertEquals(activityPackage.getExtendedString(), " (0.0 cent)", activityPackage.getSuffix());

    // check that the package handler was called
    assertTrue(
        mockLogger.toString(), mockLogger.containsTestMessage("PackageHandler sendFirstPackage"));

    // check the event count in the written activity state
    assertTrue(
        mockLogger.toString(),
        mockLogger.containsMessage(LogLevel.DEBUG, "Wrote activity state: ec:2"));

    // check the event count in the logger
    assertTrue(
        mockLogger.toString(), mockLogger.containsMessage(LogLevel.DEBUG, "Event 2 (revenue)"));
  }
  public void testOpenUrl() {
    Context context = activity.getApplicationContext();

    // starting from a clean slate
    mockLogger.test(
        "Was AdjustActivityState deleted? " + ActivityHandler.deleteActivityState(context));

    ActivityHandler activityHandler = new ActivityHandler(activity);
    activityHandler.trackSubsessionStart();

    Uri normal =
        Uri.parse(
            "AdjustTests://example.com/path/inApp?adjust_foo=bar&other=stuff&adjust_key=value");
    Uri emptyQueryString = Uri.parse("AdjustTests://");
    Uri emptyString = Uri.parse("");
    Uri nullString = null;
    Uri single = Uri.parse("AdjustTests://example.com/path/inApp?adjust_foo");
    Uri prefix = Uri.parse("AdjustTests://example.com/path/inApp?adjust_=bar");
    Uri incomplete = Uri.parse("AdjustTests://example.com/path/inApp?adjust_foo=");

    activityHandler.readOpenUrl(normal);
    activityHandler.readOpenUrl(emptyQueryString);
    activityHandler.readOpenUrl(emptyString);
    activityHandler.readOpenUrl(nullString);
    activityHandler.readOpenUrl(single);
    activityHandler.readOpenUrl(prefix);
    activityHandler.readOpenUrl(incomplete);

    SystemClock.sleep(1000);

    // check that all supposed packages were sent
    // 1 session + 1 reattributions
    assertEquals(2, mockPackageHandler.queue.size());

    // check that the normal url was parsed and sent
    ActivityPackage activityPackage = mockPackageHandler.queue.get(1);

    // testing the activity kind is the correct one
    ActivityKind activityKind = activityPackage.getActivityKind();
    assertEquals(activityPackage.getExtendedString(), ActivityKind.REATTRIBUTION, activityKind);

    // testing the conversion from activity kind to string
    String activityKindString = activityKind.toString();
    assertEquals(activityPackage.getExtendedString(), "reattribution", activityKindString);

    // testing the conversion from string to activity kind
    activityKind = ActivityKind.fromString(activityKindString);
    assertEquals(activityPackage.getExtendedString(), ActivityKind.REATTRIBUTION, activityKind);

    // package type should be reattribute
    assertEquals(activityPackage.getExtendedString(), "/reattribute", activityPackage.getPath());

    // suffix should be empty
    assertEquals(activityPackage.getExtendedString(), "", activityPackage.getSuffix());

    Map<String, String> parameters = activityPackage.getParameters();

    // check that deep link parameters contains the base64 with the 2 keys
    assertEquals(
        activityPackage.getExtendedString(),
        "{\"foo\":\"bar\",\"key\":\"value\"}",
        parameters.get("deeplink_parameters"));

    // check that added and set both session and reattribution package
    assertTrue(mockLogger.toString(), mockLogger.containsTestMessage("PackageHandler addPackage"));
    assertTrue(
        mockLogger.toString(), mockLogger.containsTestMessage("PackageHandler sendFirstPackage"));
    assertTrue(mockLogger.toString(), mockLogger.containsTestMessage("PackageHandler addPackage"));
    assertTrue(
        mockLogger.toString(), mockLogger.containsTestMessage("PackageHandler sendFirstPackage"));

    // check that sent the reattribution package
    assertTrue(
        mockLogger.toString(),
        mockLogger.containsMessage(LogLevel.DEBUG, "Reattribution {key=value, foo=bar}"));
  }
  public void testEventsBuffered() {
    Context context = activity.getApplicationContext();

    // starting from a clean slate
    mockLogger.test(
        "Was AdjustActivityState deleted? " + ActivityHandler.deleteActivityState(context));

    ActivityHandler activityHandler =
        new ActivityHandler(activity, "123456789012", "sandbox", "verbose", true);

    // start the first session
    activityHandler.trackSubsessionStart();

    // construct the parameters of the the event
    Map<String, String> eventParameters = new HashMap<String, String>();
    eventParameters.put("key", "value");
    eventParameters.put("foo", "bar");

    // the first event has parameters, the second none
    activityHandler.trackEvent("abc123", eventParameters);
    activityHandler.trackRevenue(4.45, "abc123", eventParameters);
    SystemClock.sleep(2000);

    // check that event buffering is enabled
    assertTrue(
        mockLogger.toString(),
        mockLogger.containsMessage(LogLevel.INFO, "Event buffering is enabled"));

    // check that the package builder added the session, event and revenue package
    assertEquals(mockLogger.toString(), 3, mockPackageHandler.queue.size());

    // check the first event
    ActivityPackage activityPackage = mockPackageHandler.queue.get(1);
    Map<String, String> packageParameters = activityPackage.getParameters();

    // check the event count in the package parameters
    assertEquals(
        activityPackage.getExtendedString(),
        1,
        Integer.parseInt(packageParameters.get("event_count")));

    // check the event token
    assertEquals(
        activityPackage.getExtendedString(), "abc123", packageParameters.get("event_token"));

    // check the injected parameters
    assertEquals(
        activityPackage.getExtendedString(),
        "eyJrZXkiOiJ2YWx1ZSIsImZvbyI6ImJhciJ9",
        packageParameters.get("params"));

    // check the event path
    assertEquals(activityPackage.getExtendedString(), "/event", activityPackage.getPath());

    // check the event suffix
    assertEquals(activityPackage.getExtendedString(), " 'abc123'", activityPackage.getSuffix());

    // check that the event was buffered
    assertTrue(
        mockLogger.toString(),
        mockLogger.containsMessage(LogLevel.INFO, "Buffered event  'abc123'"));

    // check the event count in the written activity state
    assertTrue(
        mockLogger.toString(),
        mockLogger.containsMessage(LogLevel.DEBUG, "Wrote activity state: ec:1"));

    // check the event count in the logger
    assertTrue(mockLogger.toString(), mockLogger.containsMessage(LogLevel.DEBUG, "Event 1"));

    // check the second event/ first revenue
    activityPackage = mockPackageHandler.queue.get(2);
    packageParameters = activityPackage.getParameters();

    // check the event count in the package parameters
    assertEquals(
        activityPackage.getExtendedString(),
        2,
        Integer.parseInt(packageParameters.get("event_count")));

    // check the amount, transforming cents into rounded decimal cents
    // note that the 4.45 cents ~> 45 decimal cents
    assertEquals(
        activityPackage.getExtendedString(), 45, Long.parseLong(packageParameters.get("amount")));

    // check the event token
    assertEquals(
        activityPackage.getExtendedString(), "abc123", packageParameters.get("event_token"));

    // check the injected parameters
    assertEquals(
        activityPackage.getExtendedString(),
        "eyJrZXkiOiJ2YWx1ZSIsImZvbyI6ImJhciJ9",
        packageParameters.get("params"));

    // check the revenue path
    assertEquals(activityPackage.getExtendedString(), "/revenue", activityPackage.getPath());

    // check the revenue suffix
    // note that the amount was rounded to the decimal cents
    assertEquals(
        activityPackage.getExtendedString(), " (4.5 cent, 'abc123')", activityPackage.getSuffix());

    // check that the revenue was buffered
    assertTrue(
        mockLogger.toString(),
        mockLogger.containsMessage(LogLevel.INFO, "Buffered revenue  (4.5 cent, 'abc123')"));

    // check the event count in the written activity state
    assertTrue(
        mockLogger.toString(),
        mockLogger.containsMessage(LogLevel.DEBUG, "Wrote activity state: ec:2"));

    // check the event count in the logger
    assertTrue(
        mockLogger.toString(), mockLogger.containsMessage(LogLevel.DEBUG, "Event 2 (revenue)"));
  }