-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
arch(dev-only): handle 'no storage decision' for all activities in ActivityList
#21323
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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
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
74 changes: 74 additions & 0 deletions
74
AnkiDroid/src/test/java/com/ichi2/anki/ActivityStartupUndecidedStorageTest.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,74 @@ | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
|
|
||
| package com.ichi2.anki | ||
|
|
||
| import com.ichi2.anki.storage.StorageDecision | ||
| import com.ichi2.testutils.ActivityList | ||
| import com.ichi2.testutils.ActivityList.ActivityLaunchParam | ||
| import org.junit.After | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.ParameterizedRobolectricTestRunner | ||
|
|
||
| /** | ||
| * All activities start crash-free when [CollectionHelper.storageDecision] returns | ||
| * [StorageDecision.Undecided]. | ||
| * | ||
| * Unlike [ExternalEntryPointsUndecidedStorageTest], this covers in-app navigation and pinned | ||
| * shortcuts. | ||
| * | ||
| * Collection-requiring activities are expected to finish via [ensureStorageIsReady][com.ichi2.anki.startup.ensureStorageIsReady]. | ||
| * | ||
| * If this fails for a new activity, add the following after `super.onCreate`: | ||
| * ``` | ||
| * if (!ensureStorageIsReady()) { | ||
| * return | ||
| * } | ||
| * ``` | ||
| */ | ||
| @RunWith(ParameterizedRobolectricTestRunner::class) | ||
| class ActivityStartupUndecidedStorageTest : RobolectricTest() { | ||
| @ParameterizedRobolectricTestRunner.Parameter | ||
| @JvmField // required for Parameter | ||
| var launcher: ActivityLaunchParam? = null | ||
|
|
||
| // Only used for display, but needs to be defined | ||
| @ParameterizedRobolectricTestRunner.Parameter(1) | ||
| @JvmField // required for Parameter | ||
| @Suppress("unused") | ||
| var activityName: String? = null | ||
|
|
||
| @Before | ||
| fun setStorageUndecided() { | ||
| CollectionHelper.storageDecisionTestOverride = StorageDecision.Undecided | ||
| } | ||
|
|
||
| @After | ||
| fun resetStorageDecision() { | ||
| CollectionHelper.storageDecisionTestOverride = null | ||
| } | ||
|
|
||
| @Test | ||
| fun `startup is crash-free when storage is undecided`() { | ||
| val controller = launcher!!.build(targetContext) | ||
| // mirrors Android: an activity which finishes during onCreate (e.g. redirectToMainEntryPoint) | ||
| // does not receive the remaining lifecycle callbacks; controller.setup() would force them | ||
| controller.create() | ||
| if (!controller.get().isFinishing) { | ||
| controller | ||
| .start() | ||
| .postCreate(null) | ||
| .resume() | ||
| .visible() | ||
| } | ||
| advanceRobolectricLooper() | ||
| } | ||
|
|
||
| companion object { | ||
| @Suppress("unused") | ||
| @ParameterizedRobolectricTestRunner.Parameters(name = "{1}") | ||
| @JvmStatic // required for initParameters | ||
| fun initParameters(): Collection<Array<Any>> = ActivityList.allActivitiesAndIntents().map { arrayOf(it, it.simpleName) } | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
AnkiDroid/src/test/java/com/ichi2/anki/CoroutineHelpersRobolectricTest.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,35 @@ | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
|
|
||
| package com.ichi2.anki | ||
|
|
||
| import androidx.fragment.app.FragmentActivity | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import com.ichi2.anki.exception.StorageNotConfiguredException | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.Robolectric | ||
| import org.robolectric.Shadows.shadowOf | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertNotNull | ||
| import kotlin.test.assertTrue | ||
|
|
||
| @RunWith(AndroidJUnit4::class) | ||
| class CoroutineHelpersRobolectricTest : RobolectricTest() { | ||
| /** | ||
| * A [StorageNotConfiguredException] escaping a coroutine means an activity raced or | ||
| * outlived its [ensureStorageIsReady][com.ichi2.anki.startup.ensureStorageIsReady] check: | ||
| * the user should be sent to the main entry point, which handles storage setup. | ||
| */ | ||
| @Test | ||
| fun `launchCatchingTask redirects to main entry point when storage is not configured`() { | ||
| val activity = Robolectric.buildActivity(FragmentActivity::class.java).create().get() | ||
|
|
||
| activity.launchCatchingTask { throw StorageNotConfiguredException() } | ||
| advanceRobolectricLooper() | ||
|
|
||
| assertTrue(activity.isFinishing, "activity should finish") | ||
| val redirect = shadowOf(activity).nextStartedActivity | ||
| assertNotNull(redirect, "the main entry point should be opened") | ||
| assertEquals(IntentHandler::class.qualifiedName, redirect.component?.className) | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function is
ensureStorageIsReady