<NME> DialogComponentTest.kt
<BEF> package com.wealthfront.magellan.view

import android.app.Activity
import android.app.AlertDialog
import android.app.Dialog
import android.os.Looper
import com.google.common.truth.Truth.assertThat
import com.wealthfront.magellan.DialogCreator
import com.wealthfront.magellan.lifecycle.LifecycleState
import com.wealthfront.magellan.lifecycle.transition
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.MockitoAnnotations.initMocks
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows

@RunWith(RobolectricTestRunner::class)
internal class DialogComponentTest {

  lateinit var dialog1: Dialog
  lateinit var dialog2: Dialog

  private val context = Robolectric.buildActivity(Activity::class.java).get()
  private val dialogComponent = DialogComponent()

  private lateinit var dialogCreator1: DialogCreator
  private lateinit var dialogCreator2: DialogCreator

  @Before
  fun setUp() {
    initMocks(this)
    dialogCreator1 = DialogCreator {
      AlertDialog.Builder(context).create().also { dialog1 = it }
    }
    dialogCreator2 = DialogCreator {
    `when`(dialog1.isShowing).thenReturn(true)

    dialogComponent.create(context)
    dialogComponent.showDialog { context -> dialogCreator1.createDialog(context) }

    verify(dialog1).show()
    assertThat(dialogComponent.dialogIsShowing).isTrue()
    dialogComponent.transition(LifecycleState.Shown(context), LifecycleState.Resumed(context))
    dialogComponent.showDialog(dialogCreator1)
    assertThat(dialog1.isShowing).isTrue()
  }
    `when`(dialog1.isShowing).thenReturn(true)

    dialogComponent.create(context)
    dialogComponent.showDialog { context -> dialogCreator1.createDialog(context) }

    verify(dialog1).show()
    assertThat(dialogComponent.dialogIsShowing).isTrue()

    dialog1.dismiss()
    dialogComponent.transition(LifecycleState.Resumed(context), LifecycleState.Shown(context))
    assertThat(dialogComponent.shouldRestoreDialog).isFalse()
  }

  @Test
  fun showDialog_rotation() {
    dialogComponent.transition(LifecycleState.Created(context), LifecycleState.Shown(context))
  fun showDialog_rotation() {
    `when`(dialog1.isShowing).thenReturn(true)
    dialogComponent.create(context)
    dialogComponent.showDialog { context -> dialogCreator1.createDialog(context) }

    verify(dialog1).show()
    assertThat(dialogComponent.dialogIsShowing).isTrue()
    assertThat(dialogComponent.shouldRestoreDialog).isTrue()

    dialogComponent.transition(LifecycleState.Created(context), LifecycleState.Shown(context))
    dialogComponent.transition(LifecycleState.Shown(context), LifecycleState.Resumed(context))
    assertThat(dialog1.isShowing).isTrue()
  }

  @Test
  fun showDialog_diffDialog() {
    dialogComponent.transition(LifecycleState.Created(context), LifecycleState.Shown(context))
    dialogComponent.transition(LifecycleState.Shown(context), LifecycleState.Resumed(context))
    dialogComponent.showDialog(dialogCreator1)
    assertThat(dialog1.isShowing).isTrue()

    dialogComponent.showDialog(dialogCreator2)
    Shadows.shadowOf(Looper.getMainLooper()).idle()
    `when`(dialog2.isShowing).thenReturn(true)

    dialogComponent.create(context)
    dialogComponent.showDialog { context -> dialogCreator1.createDialog(context) }

    verify(dialog1).show()
    assertThat(dialogComponent.dialogIsShowing).isTrue()

    dialogComponent.showDialog(DialogCreator { context -> dialogCreator2.createDialog(context) })

    verify(dialog2).show()
    assertThat(dialogComponent.dialogIsShowing).isTrue()

<MSG> Address comments

<DFF> @@ -38,7 +38,7 @@ class DialogComponentTest {
     `when`(dialog1.isShowing).thenReturn(true)
 
     dialogComponent.create(context)
-    dialogComponent.showDialog { context -> dialogCreator1.createDialog(context) }
+    dialogComponent.showDialog(dialogCreator1)
 
     verify(dialog1).show()
     assertThat(dialogComponent.dialogIsShowing).isTrue()
@@ -49,7 +49,7 @@ class DialogComponentTest {
     `when`(dialog1.isShowing).thenReturn(true)
 
     dialogComponent.create(context)
-    dialogComponent.showDialog { context -> dialogCreator1.createDialog(context) }
+    dialogComponent.showDialog(dialogCreator1)
 
     verify(dialog1).show()
     assertThat(dialogComponent.dialogIsShowing).isTrue()
@@ -65,7 +65,7 @@ class DialogComponentTest {
   fun showDialog_rotation() {
     `when`(dialog1.isShowing).thenReturn(true)
     dialogComponent.create(context)
-    dialogComponent.showDialog { context -> dialogCreator1.createDialog(context) }
+    dialogComponent.showDialog(dialogCreator1)
 
     verify(dialog1).show()
     assertThat(dialogComponent.dialogIsShowing).isTrue()
@@ -88,12 +88,12 @@ class DialogComponentTest {
     `when`(dialog2.isShowing).thenReturn(true)
 
     dialogComponent.create(context)
-    dialogComponent.showDialog { context -> dialogCreator1.createDialog(context) }
+    dialogComponent.showDialog(dialogCreator1)
 
     verify(dialog1).show()
     assertThat(dialogComponent.dialogIsShowing).isTrue()
 
-    dialogComponent.showDialog(DialogCreator { context -> dialogCreator2.createDialog(context) })
+    dialogComponent.showDialog(dialogCreator2)
 
     verify(dialog2).show()
     assertThat(dialogComponent.dialogIsShowing).isTrue()

{'addition_count': 5, 'commit_subject': 'Address comments', 'deletion_count': 5, 'file_extension': '.kt', 'lang': 'kt', 'license': 'apache-2.0', 'repo_name': 'wealthfront/magellan'}