package com.jraska.dialog;

import android.app.Dialog;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class LambdaDialogsDelegateTest {
  TestActivity activity;

  @Mock ActivityDialogMethodParam<TestActivity, Object> verificationFactory;

  @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);

    activity = Robolectric.setupActivity(TestActivity.class);
    activity.verificationFactory = verificationFactory;
    when(verificationFactory.onCreateDialog(any(), any()))
        .thenReturn(new Dialog(RuntimeEnvironment.application));
  }

  @Test
  public void whenShown_thenMethodCalled() {
    LambdaDialogs.delegate(activity)
        .method(TestActivity::createDialog)
        .show();

    verify(verificationFactory).onCreateDialog(any(), eq(null));
  }

  @Test
  public void whenShown_thenStringParameterProvided() {
    LambdaDialogs.delegate(activity)
        .parameter("text")
        .method(TestActivity::createDialog)
        .show();

    verify(verificationFactory).onCreateDialog(any(), eq("text"));
  }

  @Test
  public void whenShown_thenIntentParameterProvided() {
    Intent intent = new Intent(activity, TestActivity.class);
    LambdaDialogs.delegate(activity)
        .parameter(intent)
        .method(TestActivity::createDialog)
        .show();

    verify(verificationFactory).onCreateDialog(any(), eq(intent));
  }

  static class TestActivity extends FragmentActivity {
    ActivityDialogMethodParam<TestActivity, Object> verificationFactory;

    Dialog createDialog(Intent parcelableIntent) {
      return verificationFactory.onCreateDialog(this, parcelableIntent);
    }

    Dialog createDialog(String serializableString) {
      return verificationFactory.onCreateDialog(this, serializableString);
    }

    Dialog createDialog() {
      return verificationFactory.onCreateDialog(this, null);
    }
  }
}
@@ -439,16 +439,119 @@
 Config;%0A
+import org.robolectric.shadows.ShadowDialog;%0A%0Aimport static org.assertj.core.api.Assertions.assertThat;
 %0Aimport 
@@ -898,35 +898,104 @@
 Object%3E 
-verificationFactory
+mockActivityWithParameterMethod;%0A  @Mock ActivityMethod%3CTestActivity%3E mockActivityMethod
 ;%0A%0A  @Be
@@ -1165,57 +1165,81 @@
 y = 
-verificationFactory;%0A    when(verificationFactory
+mockActivityWithParameterMethod;%0A    when(mockActivityWithParameterMethod
 .onC
@@ -1329,16 +1329,58 @@
 ation));
+%0A%0A    LambdaDialogs.validateEagerly(true);
 %0A  %7D%0A%0A  
@@ -1540,35 +1540,47 @@
  verify(
-verificationFactory
+mockActivityWithParameterMethod
 ).onCrea
@@ -1812,35 +1812,47 @@
  verify(
-verificationFactory
+mockActivityWithParameterMethod
 ).onCrea
@@ -2152,60 +2152,853 @@
 ify(
-verificationFactory).onCre
+mockActivityWithParameterMethod).onCreateDialog(any(), eq(intent));%0A  %7D%0A%0A  @Test%0A  public void whenCanceled_thenCancelMethodCalled() %7B%0A    DelegateDialog dialog = LambdaDialogs.delegate(activity)%0A        .method(TestActivity::createDialog)%0A        .cancelMethod(mockActivityMethod)%0A        .cancelable(true)%0A        .build();%0A%0A    assertThat(dialog.isCancelable()).isTrue();%0A%0A    dialog.show(activity.getSupportFragmentManager(), %22tag%22);%0A    ShadowDialog.getL
 ate
+st
 Dialog(
-any(), eq(intent
+).cancel();%0A%0A    verify(mockActivityMethod).call(eq(activity));%0A  %7D%0A%0A  @Test%0A  public void whenDismissed_thenDismissMethodCalled() %7B%0A    LambdaDialogs.delegate(activity)%0A        .method(TestActivity::createDialog)%0A        .dismissMethod(mockActivityMethod)%0A        .show(%22Tag%22);%0A%0A    ShadowDialog.getLatestDialog().dismiss();%0A%0A    verify(mockActivityMethod).call(eq(activity
 ));%0A

Add tests. Resolves #7