Skip to content

Commit

Permalink
fix(capture-sdk): Fix tests
Browse files Browse the repository at this point in the history
BSDK-258
  • Loading branch information
a-szotyori committed Jan 23, 2024
1 parent d1110c4 commit 77a7e0f
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGrou
return mFragmentImpl.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mFragmentImpl.onViewCreated(view, savedInstanceState);
}

/**
* Internal use only.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.FragmentActivity;

import net.gini.android.capture.Document;
import net.gini.android.capture.GiniCapture;
Expand All @@ -28,6 +30,8 @@
import net.gini.android.capture.internal.ui.FragmentImplCallback;
import net.gini.android.capture.internal.ui.IntervalClickListener;
import net.gini.android.capture.internal.util.Size;
import net.gini.android.capture.tracking.AnalysisScreenEvent;
import net.gini.android.capture.tracking.CameraScreenEvent;
import net.gini.android.capture.view.CustomLoadingIndicatorAdapter;
import net.gini.android.capture.view.InjectedViewAdapterHolder;
import net.gini.android.capture.view.InjectedViewContainer;
Expand All @@ -43,6 +47,8 @@
import kotlin.Unit;

import static net.gini.android.capture.internal.util.ActivityHelper.forcePortraitOrientationOnPhones;
import static net.gini.android.capture.tracking.EventTrackingHelper.trackAnalysisScreenEvent;
import static net.gini.android.capture.tracking.EventTrackingHelper.trackCameraScreenEvent;

/**
* Main logic implementation for analysis UI presented by {@link AnalysisFragment}
Expand Down Expand Up @@ -265,6 +271,26 @@ private void setLoadingIndicatorViewContainer() {
}
}

public void onViewCreated(View view, Bundle savedInstanceState) {
handleOnBackPressed();
}

private void handleOnBackPressed() {
final FragmentActivity activity = mFragment.getActivity();
if (activity == null) {
return;
}
activity.getOnBackPressedDispatcher().addCallback(mFragment.getViewLifecycleOwner(), new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
trackAnalysisScreenEvent(AnalysisScreenEvent.CANCEL);
setEnabled(false);
remove();
mFragment.getActivity().getOnBackPressedDispatcher().onBackPressed();
}
});
}

public void onResume() {
getPresenter().start();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package net.gini.android.capture.analysis

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentFactory
import androidx.fragment.app.testing.FragmentScenario
import androidx.lifecycle.Lifecycle
import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.spy
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import net.gini.android.capture.Amount
import net.gini.android.capture.Document
import net.gini.android.capture.GiniCapture
import net.gini.android.capture.R
import net.gini.android.capture.document.ImageDocument
import net.gini.android.capture.tracking.AnalysisScreenEvent
import net.gini.android.capture.tracking.Event
import net.gini.android.capture.tracking.EventTracker
import org.junit.After
import org.junit.Test
import org.junit.runner.RunWith

Expand All @@ -27,8 +27,7 @@ import org.junit.runner.RunWith
*/

@RunWith(AndroidJUnit4::class)
//@LooperMode(LooperMode.Mode.PAUSED)
class AnalysisActivityTest {
class AnalysisFragmentTest {

@Test
fun `triggers Cancel event when back was pressed`() {
Expand All @@ -37,17 +36,27 @@ class AnalysisActivityTest {
GiniCapture.Builder().setEventTracker(eventTracker).build()
GiniCapture.getInstance().internal().imageMultiPageDocumentMemoryStore.setMultiPageDocument(mock())

ActivityScenario.launch<AnalysisActivity>(Intent(getInstrumentation().targetContext, AnalysisActivity::class.java).apply {
putExtra(AnalysisActivity.EXTRA_IN_DOCUMENT, mock<ImageDocument>().apply {
val bundle = Bundle().apply {
putParcelable("GC_ARGS_DOCUMENT", mock<ImageDocument>().apply {
whenever(isReviewable).thenReturn(true)
whenever(type).thenReturn(Document.Type.IMAGE)
})
}).use { scenario ->
putString("GC_ARGS_DOCUMENT_ANALYSIS_ERROR_MESSAGE", "")
}
FragmentScenario.launchInContainer(fragmentClass = AnalysisFragment::class.java, fragmentArgs = bundle,
themeResId = R.style.GiniCaptureTheme,
factory = object : FragmentFactory() {
override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
return AnalysisFragment().apply {
setListener(mock())
}
}
}).use { scenario ->
scenario.moveToState(Lifecycle.State.STARTED)

// When
scenario.onActivity {activity ->
activity.onBackPressed()
scenario.onFragment { fragment ->
fragment.requireActivity().onBackPressedDispatcher.onBackPressed()

// Then
verify(eventTracker).onAnalysisScreenEvent(Event(AnalysisScreenEvent.CANCEL))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CameraFragmentImplTest {
val eventTracker = spy<EventTracker>()
GiniCapture.Builder().setEventTracker(eventTracker).build()

val fragmentImpl = object: CameraFragmentImpl(mock()) {
val fragmentImpl = object: CameraFragmentImpl(mock(), false) {
override fun createCameraController(activity: Activity?): CameraInterface {
return mock<CameraInterface>().apply {
whenever(isPreviewRunning).thenReturn(true)
Expand All @@ -57,7 +57,7 @@ class CameraFragmentImplTest {
// Given
GiniCaptureHelper.setGiniCaptureInstance(null)

val fragmentImpl = CameraFragmentImpl(mock())
val fragmentImpl = CameraFragmentImpl(mock(), false)

val listener = mock<CameraFragmentListener>()
fragmentImpl.setListener(listener)
Expand Down Expand Up @@ -109,7 +109,7 @@ class CameraFragmentImplTest {
})
whenever(fragmentCallbackStub.findNavController()).thenReturn(mock())

val fragmentImpl = CameraFragmentImpl(fragmentCallbackStub)
val fragmentImpl = CameraFragmentImpl(fragmentCallbackStub, false)

// When
fragmentImpl.startHelpActivity()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package net.gini.android.capture.camera

import android.view.MenuItem
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentFactory
import androidx.fragment.app.testing.FragmentScenario
import androidx.lifecycle.Lifecycle
import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.spy
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import net.gini.android.capture.Amount
import net.gini.android.capture.GiniCapture
import net.gini.android.capture.R
import net.gini.android.capture.tracking.CameraScreenEvent
import net.gini.android.capture.tracking.Event
import net.gini.android.capture.tracking.EventTracker
import org.junit.After
import org.junit.Test
import org.junit.runner.RunWith

Expand All @@ -26,20 +23,28 @@ import org.junit.runner.RunWith
*/

@RunWith(AndroidJUnit4::class)
class CameraActivityTest {
class CameraFragmentTest {

@Test
fun `triggers Exit event when back was pressed`() {
// Given
val eventTracker = spy<EventTracker>()
GiniCapture.Builder().setEventTracker(eventTracker).build()

ActivityScenario.launch(CameraActivity::class.java).use { scenario ->
FragmentScenario.launchInContainer(fragmentClass = CameraFragment::class.java,
themeResId = R.style.GiniCaptureTheme,
factory = object : FragmentFactory() {
override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
return CameraFragment().apply {
setListener(mock())
}
}
}).use { scenario ->
scenario.moveToState(Lifecycle.State.STARTED)

// When
scenario.onActivity { activity ->
activity.onBackPressed()
scenario.onFragment { fragment ->
fragment.requireActivity().onBackPressedDispatcher.onBackPressed()

// Then
verify(eventTracker).onCameraScreenEvent(Event(CameraScreenEvent.EXIT))
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package net.gini.android.capture.review.multipage

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentFactory
import androidx.fragment.app.testing.FragmentScenario
import androidx.fragment.app.testing.launchFragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Lifecycle.State.CREATED
import androidx.lifecycle.Lifecycle.State.RESUMED
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.google.common.truth.Truth
import com.nhaarman.mockitokotlin2.*
import jersey.repackaged.jsr166e.CompletableFuture
Expand Down Expand Up @@ -44,25 +47,69 @@ class MultipageReviewFragmentTest {
}

@Test
fun `triggers Next event`() {
fun `triggers Back event when back was pressed`() {
// Given
val giniCapture = mock<GiniCapture>()
GiniCaptureHelper.setGiniCaptureInstance(giniCapture)

val internal = mock<GiniCapture.Internal>()
`when`(giniCapture.internal()).thenReturn(internal)

val eventTracker = spy<EventTracker>()
`when`(giniCapture.internal().eventTracker).thenReturn(eventTracker)

val fragment = MultiPageReviewFragment()
fragment.setListener(mock())

// When
fragment.onNextButtonClicked()
GiniCapture.Builder().setEventTracker(eventTracker).build()
GiniCapture.getInstance().internal().imageMultiPageDocumentMemoryStore.setMultiPageDocument(mock())

FragmentScenario.launchInContainer(fragmentClass = MultiPageReviewFragment::class.java,
themeResId = R.style.GiniCaptureTheme,
factory = object : FragmentFactory() {
override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
return MultiPageReviewFragment().apply {
setListener(mock())
}
}
}).use { scenario ->
scenario.moveToState(Lifecycle.State.STARTED)

// When
scenario.onFragment { fragment ->
try {
fragment.requireActivity().onBackPressedDispatcher.onBackPressed()
} catch (e: IllegalStateException) {
// The only exception we can get must be related to the NavController
Truth.assertThat(e.message).contains("NavController")
}

// Then
verify(eventTracker).onReviewScreenEvent(Event(ReviewScreenEvent.BACK))
}
}
}

// Then
verify(eventTracker).onReviewScreenEvent(Event(ReviewScreenEvent.NEXT))
@Test
fun `triggers Next event`() {
// Given
val eventTracker = spy<EventTracker>()
GiniCapture.Builder().setEventTracker(eventTracker).build()
GiniCapture.getInstance().internal().imageMultiPageDocumentMemoryStore.setMultiPageDocument(mock())

FragmentScenario.launchInContainer(fragmentClass = MultiPageReviewFragment::class.java,
themeResId = R.style.GiniCaptureTheme,
factory = object : FragmentFactory() {
override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
return MultiPageReviewFragment().apply {
setListener(mock())
}
}
}).use { scenario ->
scenario.moveToState(Lifecycle.State.STARTED)

// When
scenario.onFragment { fragment ->
try {
fragment.onNextButtonClicked()
} catch (e: IllegalStateException) {
// The only exception we can get must be related to the NavController
Truth.assertThat(e.message).contains("NavController")
}

// Then
verify(eventTracker).onReviewScreenEvent(Event(ReviewScreenEvent.NEXT))
}
}
}

@Test
Expand Down

0 comments on commit 77a7e0f

Please sign in to comment.