-
Notifications
You must be signed in to change notification settings - Fork 1
PP-1700: Unit and Integration related to feature flag #837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
obaidgini
wants to merge
11
commits into
main
Choose a base branch
from
PP-1700-unit-and-integration-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
22fd9f9
feat(capture-sdk): Adding the unit and integration tests for the user…
obaidgini 7210f71
feat(capture-sdk): accommodating the co-pilot comments
obaidgini 811afd4
Merge branch 'main' of https://github.com/gini/gini-mobile-android in…
obaidgini 401bf92
feat(capture-sdk): refactored the methods of UserAnalyticsEventTracke…
obaidgini 9312a30
feat(capture-sdk): refactored the methods of AmplitudeUserAnalyticsEv…
obaidgini 2463adb
feat(capture-sdk): refactored the methods of BufferedUserAnalyticsEve…
obaidgini e565aea
feat(capture-sdk): Changed the comments as per PR comments
obaidgini b4fd428
feat(capture-sdk): Changed the tests, according to new refactor and b…
obaidgini 025d397
feat(capture-sdk): Changed the setup according to new return type of …
obaidgini c2ff476
feat(capture-sdk): Addressing the detekt issues
obaidgini 56bf21f
feat(capture-sdk): Resolving the test failures because we changed the…
obaidgini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...c/androidTest/java/net/gini/android/capture/GiniCaptureHelperForInstrumentationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package net.gini.android.capture; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
/** | ||
* Helper class to set the {@link GiniCapture} instance for instrumentation tests. | ||
*/ | ||
public class GiniCaptureHelperForInstrumentationTests { | ||
public static void setGiniCaptureInstance(@Nullable final GiniCapture giniCapture) { | ||
GiniCapture.setInstance(giniCapture); | ||
} | ||
|
||
} |
185 changes: 185 additions & 0 deletions
185
.../sdk/src/androidTest/java/net/gini/android/capture/ginicapture/GiniCaptureFragmentTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package net.gini.android.capture.ginicapture | ||
|
||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentFactory | ||
import androidx.fragment.app.testing.FragmentScenario | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.google.common.truth.Truth.assertThat | ||
import jersey.repackaged.jsr166e.CompletableFuture | ||
import net.gini.android.capture.DocumentImportEnabledFileTypes | ||
import net.gini.android.capture.EntryPoint | ||
import net.gini.android.capture.GiniCapture | ||
import net.gini.android.capture.GiniCaptureFragment | ||
import net.gini.android.capture.GiniCaptureHelperForInstrumentationTests | ||
import net.gini.android.capture.di.CaptureSdkIsolatedKoinContext | ||
import net.gini.android.capture.internal.document.ImageMultiPageDocumentMemoryStore | ||
import net.gini.android.capture.internal.network.Configuration | ||
import net.gini.android.capture.internal.network.ConfigurationNetworkResult | ||
import net.gini.android.capture.internal.network.NetworkRequestsManager | ||
import net.gini.android.capture.internal.provider.GiniBankConfigurationProvider | ||
import net.gini.android.capture.tracking.useranalytics.BufferedUserAnalyticsEventTracker | ||
import net.gini.android.capture.tracking.useranalytics.UserAnalytics | ||
import net.gini.android.capture.view.DefaultLoadingIndicatorAdapter | ||
import net.gini.android.capture.view.DefaultNavigationBarTopAdapter | ||
import net.gini.android.capture.view.InjectedViewAdapterInstance | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.koin.dsl.module | ||
import com.nhaarman.mockitokotlin2.any | ||
import com.nhaarman.mockitokotlin2.mock | ||
import com.nhaarman.mockitokotlin2.whenever | ||
import java.util.UUID | ||
|
||
/** | ||
obaidgini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Integration test to verify the correct behavior of Analytics. | ||
* Classes involved | ||
* - [GiniCaptureFragment] | ||
* - [GiniCapture] | ||
* - [GiniCapture.Internal] | ||
* - [NetworkRequestsManager] | ||
* - [BufferedUserAnalyticsEventTracker] | ||
* */ | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class GiniCaptureFragmentTest { | ||
private lateinit var networkRequestsManager: NetworkRequestsManager | ||
private lateinit var giniCapture: GiniCapture | ||
private lateinit var giniInternal: GiniCapture.Internal | ||
private lateinit var memoryStore: ImageMultiPageDocumentMemoryStore | ||
private val koinTestModule = module { | ||
single { GiniBankConfigurationProvider() } | ||
} | ||
|
||
/** | ||
* We are using multiple dependencies in different classes, so many mocks are needed | ||
* to fully test the functionality of AnalyticsTracker, by running the GiniCaptureFragment | ||
* in Isolation. | ||
* Mock was needed for [NetworkRequestsManager], [GiniCapture], [GiniCapture.Internal], | ||
* [ImageMultiPageDocumentMemoryStore], [GiniBankConfigurationProvider] | ||
* Also, in [GiniCaptureFragment], we are using koin to update | ||
* the [GiniBankConfigurationProvider], for that we need to load and unload the module, | ||
* other wise an exception will be thrown from koin for not loaded module. | ||
* In the end, we need to set the mocked [GiniCapture] instance, and we have a helper class | ||
* [GiniCaptureHelperForInstrumentationTests] for that. | ||
* | ||
* */ | ||
|
||
@Before | ||
fun setUp() { | ||
CaptureSdkIsolatedKoinContext.koin.loadModules(listOf(koinTestModule)) | ||
|
||
networkRequestsManager = mock() | ||
giniCapture = mock() | ||
giniInternal = mock() | ||
memoryStore = mock() | ||
|
||
whenever(giniInternal.networkRequestsManager).thenReturn(networkRequestsManager) | ||
whenever(giniCapture.internal()).thenReturn(giniInternal) | ||
whenever(giniCapture.entryPoint).thenReturn(EntryPoint.BUTTON) | ||
whenever(giniInternal.imageMultiPageDocumentMemoryStore).thenReturn(memoryStore) | ||
whenever(giniInternal.navigationBarTopAdapterInstance).thenReturn( | ||
InjectedViewAdapterInstance(DefaultNavigationBarTopAdapter()) | ||
) | ||
whenever(giniCapture.documentImportEnabledFileTypes).thenReturn( | ||
DocumentImportEnabledFileTypes.NONE | ||
) | ||
whenever(giniCapture.internal().loadingIndicatorAdapterInstance).thenReturn( | ||
InjectedViewAdapterInstance(DefaultLoadingIndicatorAdapter()) | ||
) | ||
|
||
GiniCaptureHelperForInstrumentationTests.setGiniCaptureInstance(giniCapture) | ||
} | ||
|
||
|
||
/** | ||
* Unload the koin modules which were loaded in the [setUp]. | ||
* */ | ||
|
||
@After | ||
fun tearDown() = CaptureSdkIsolatedKoinContext.koin.unloadModules(listOf(koinTestModule)) | ||
|
||
|
||
@Test | ||
fun analyticsTracker_shouldBeEmpty_whenUserJourneyDisabled() { | ||
|
||
whenever(networkRequestsManager.getConfigurations(any())).thenReturn( | ||
CompletableFuture.completedFuture(getMockedConfiguration(userJourneyEnabled = false)) | ||
) | ||
|
||
launchGiniCaptureFragment().use { scenario -> | ||
|
||
scenario.moveToState(Lifecycle.State.STARTED) | ||
|
||
scenario.onFragment { _ -> | ||
assertThat(getAnalyticsTracker().getTrackers()).isEmpty() | ||
} | ||
} | ||
} | ||
|
||
|
||
@Test | ||
fun analyticsTracker_shouldNotBeEmpty_whenUserJourneyEnabled() { | ||
|
||
whenever(networkRequestsManager.getConfigurations(any())).thenReturn( | ||
CompletableFuture.completedFuture(getMockedConfiguration(userJourneyEnabled = true)) | ||
) | ||
|
||
launchGiniCaptureFragment().use { scenario -> | ||
|
||
scenario.moveToState(Lifecycle.State.STARTED) | ||
|
||
scenario.onFragment { _ -> | ||
assertThat(getAnalyticsTracker().getTrackers()).isNotEmpty() | ||
} | ||
} | ||
} | ||
|
||
|
||
private fun getMockedConfiguration(userJourneyEnabled: Boolean): ConfigurationNetworkResult { | ||
val testConfig = Configuration( | ||
id = UUID.randomUUID(), | ||
clientID = TEST_CLIENT_ID, | ||
isUserJourneyAnalyticsEnabled = userJourneyEnabled, | ||
isSkontoEnabled = false, | ||
isReturnAssistantEnabled = false, | ||
isTransactionDocsEnabled = false, | ||
isQrCodeEducationEnabled = false, | ||
isInstantPaymentEnabled = false, | ||
isEInvoiceEnabled = false, | ||
amplitudeApiKey = TEST_API_KEY | ||
) | ||
|
||
return ConfigurationNetworkResult(testConfig, UUID.randomUUID()) | ||
} | ||
|
||
private fun getAnalyticsTracker(): BufferedUserAnalyticsEventTracker { | ||
return UserAnalytics.getAnalyticsEventTracker() as BufferedUserAnalyticsEventTracker | ||
} | ||
|
||
/** | ||
* Helper method to launch the [GiniCaptureFragment] in a container, | ||
* needed in all the tests. | ||
* | ||
* */ | ||
|
||
private fun launchGiniCaptureFragment(): FragmentScenario<GiniCaptureFragment> { | ||
return FragmentScenario.launchInContainer( | ||
fragmentClass = GiniCaptureFragment::class.java, | ||
factory = object : FragmentFactory() { | ||
override fun instantiate(classLoader: ClassLoader, className: String): Fragment { | ||
return GiniCaptureFragment.createInstance().apply { | ||
setListener(mock()) | ||
} | ||
} | ||
} | ||
) | ||
} | ||
|
||
companion object { | ||
private const val TEST_CLIENT_ID = "test-client-id" | ||
private const val TEST_API_KEY = "test-api-key" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.