package com.segment.analytics;

import com.bugsnag.android.Bugsnag;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import static org.powermock.api.mockito.PowerMockito.verifyNoMoreInteractions;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
import static org.powermock.api.mockito.PowerMockito.when;

@RunWith(RobolectricTestRunner.class) @Config(emulateSdk = 18, manifest = Config.NONE)
@PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" })
@PrepareForTest(Bugsnag.class)
public class BugsnagTest extends IntegrationExam {
  @Rule public PowerMockRule rule = new PowerMockRule();
  BugsnagIntegrationAdapter adapter;

  @Before @Override public void setUp() {
    super.setUp();
    PowerMockito.mockStatic(Bugsnag.class);
    adapter = new BugsnagIntegrationAdapter();
  }

  @Test public void initialize() throws InvalidConfigurationException {
    adapter.initialize(context, new JsonMap().putValue("apiKey", "foo").putValue("useSSL", true));
    verifyStatic();
    Bugsnag.register(context, "foo");
    Bugsnag.setUseSSL(true);
  }

  @Test
  public void onActivityCreated() {
    when(activity.getLocalClassName()).thenReturn("foo");
    adapter.onActivityCreated(activity, bundle);
    verifyStatic();
    Bugsnag.setContext("foo");
    Bugsnag.onActivityCreate(activity);
  }

  @Test public void onActivityResumed() {
    adapter.onActivityResumed(activity);
    verifyStatic();
    Bugsnag.onActivityResume(activity);
  }

  @Test public void onActivityPaused() {
    adapter.onActivityPaused(activity);
    verifyStatic();
    Bugsnag.onActivityPause(activity);
  }

  @Test public void onActivityDestroyed() {
    adapter.onActivityDestroyed(activity);
    verifyStatic();
    Bugsnag.onActivityDestroy(activity);
  }

  @Test public void activityLifecycle() {
    adapter.onActivityStopped(activity);
    adapter.onActivitySaveInstanceState(activity, bundle);
    adapter.onActivityStarted(activity);
    verifyStatic();
    verifyNoMoreInteractions(Bugsnag.class);
  }

  @Test public void identify() {
    traits.putUserId("foo").putEmail("bar").putName("baz");
    adapter.identify(identifyPayload("foo"));
    verifyStatic();
    Bugsnag.setUser("foo", "bar", "baz");
    Bugsnag.addToTab("User", "userId", "foo");
    Bugsnag.addToTab("User", "email", "bar");
    Bugsnag.addToTab("User", "name", "baz");
  }
}

@@ -1646,32 +1646,52 @@
 Context(%22foo%22);%0A
+    verifyStatic();%0A
     Bugsnag.onAc
@@ -2615,24 +2615,44 @@
 r%22, %22baz%22);%0A
+    verifyStatic();%0A
     Bugsnag.
@@ -2678,32 +2678,52 @@
 serId%22, %22foo%22);%0A
+    verifyStatic();%0A
     Bugsnag.addT
@@ -2748,24 +2748,44 @@
 l%22, %22bar%22);%0A
+    verifyStatic();%0A
     Bugsnag.

Update Bugsnag tests