Repository navigation
ADFA-6894: Offer Install for .cgt files in the editor file tab #2213
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
5 commits
Select commit
Hold shift + click to select a range
23f84d4
ADFA-6894: Offer Install for .cgt files in the editor file tab
davidschachterADFA d3c94e4
ADFA-6894: Test .cgt install kind and the template install intent
davidschachterADFA a2f401a
ADFA-6894: Guard the .cgt install launch, narrow visibility, tighten …
davidschachterADFA c5194a0
ADFA-6894: Test the Install action's wiring and log launch failures
davidschachterADFA bcb3727
Merge branch 'stage' into ADFA-6894-cgt-install
davidschachterADFA 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
131 changes: 131 additions & 0 deletions
131
app/src/test/java/com/itsaky/androidide/actions/file/InstallFileActionTest.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,131 @@ | ||
| package com.itsaky.androidide.actions.file | ||
|
|
||
| import android.app.Activity | ||
| import android.content.ActivityNotFoundException | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.net.Uri | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import com.google.common.truth.Truth.assertThat | ||
| import com.itsaky.androidide.actions.ActionData | ||
| import com.itsaky.androidide.activities.ExternalFileInstallActivity | ||
| import com.itsaky.androidide.activities.editor.EditorHandlerActivity | ||
| import com.itsaky.androidide.utils.fileProviderAuthority | ||
| import com.itsaky.androidide.utils.fileProviderUriFor | ||
| import com.itsaky.androidide.utils.flashError | ||
| import io.mockk.every | ||
| import io.mockk.just | ||
| import io.mockk.mockk | ||
| import io.mockk.mockkStatic | ||
| import io.mockk.runs | ||
| import io.mockk.slot | ||
| import io.mockk.unmockkStatic | ||
| import io.mockk.verify | ||
| import kotlinx.coroutines.runBlocking | ||
| import org.junit.After | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
| import java.io.File | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| class InstallFileActionTest { | ||
|
davidschachterADFA marked this conversation as resolved.
Resolved
|
||
| private val activity = mockk<EditorHandlerActivity>(relaxed = true) | ||
| private val data = ActionData.create(activity) | ||
| private val context = ApplicationProvider.getApplicationContext<Context>() | ||
| private val uri = Uri.parse("content://test.providers.fileprovider/a.cgt") | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| unmockkStatic(FILE_PROVIDER_UTILS, FLASHBAR_UTILS) | ||
| } | ||
|
|
||
| private fun currentFile(file: File?) { | ||
| every { activity.editorViewModel.getOpenedFiles() } returns listOfNotNull(file) | ||
| every { activity.editorViewModel.getCurrentFile() } returns file | ||
| } | ||
|
|
||
| @Test | ||
| fun `cgt maps to TEMPLATE in any case`() { | ||
| assertThat(installKindFor(File("a.cgt"))).isEqualTo(InstallKind.TEMPLATE) | ||
| assertThat(installKindFor(File("a.CGT"))).isEqualTo(InstallKind.TEMPLATE) | ||
| } | ||
|
|
||
| @Test | ||
| fun `cgp and apk keep their kinds`() { | ||
| assertThat(installKindFor(File("a.cgp"))).isEqualTo(InstallKind.PLUGIN) | ||
| assertThat(installKindFor(File("a.Cgp"))).isEqualTo(InstallKind.PLUGIN) | ||
| assertThat(installKindFor(File("a.apk"))).isEqualTo(InstallKind.APK) | ||
| assertThat(installKindFor(File("a.APK"))).isEqualTo(InstallKind.APK) | ||
| } | ||
|
|
||
| @Test | ||
| fun `other extensions, no extension and no file are not installable`() { | ||
| assertThat(installKindFor(File("a.zip"))).isNull() | ||
| assertThat(installKindFor(File("a.txt"))).isNull() | ||
| assertThat(installKindFor(File("cgt"))).isNull() | ||
| assertThat(installKindFor(File("a.cgt.bak"))).isNull() | ||
| assertThat(installKindFor(null)).isNull() | ||
| } | ||
|
|
||
| @Test | ||
| fun `template install intent targets the install activity with a FileProvider uri and read grant`() { | ||
| val context = ApplicationProvider.getApplicationContext<android.content.Context>() | ||
| val file = File(context.filesDir, "my.cgt").apply { writeText("x") } | ||
|
|
||
| val intent = templateInstallIntent(context, file) | ||
|
|
||
| assertThat(intent.action).isEqualTo(Intent.ACTION_VIEW) | ||
| assertThat(intent.component?.className).isEqualTo(ExternalFileInstallActivity::class.java.name) | ||
| assertThat(intent.data?.scheme).isEqualTo("content") | ||
| assertThat(intent.data?.authority).isEqualTo(context.fileProviderAuthority()) | ||
| assertThat(intent.flags and Intent.FLAG_GRANT_READ_URI_PERMISSION) | ||
| .isEqualTo(Intent.FLAG_GRANT_READ_URI_PERMISSION) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Install is offered for a cgt tab in any case and hidden for other files`() { | ||
| for ((name, visible) in listOf("a.cgt" to true, "a.CGT" to true, "a.cgp" to true, "a.apk" to true, "a.txt" to false)) { | ||
| currentFile(File(name)) | ||
|
|
||
| val action = InstallFileAction(context, 0).apply { prepare(data) } | ||
|
|
||
| assertThat(action.visible).isEqualTo(visible) | ||
| assertThat(action.enabled).isEqualTo(visible) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `tapping Install on a cgt starts the install activity with the file's FileProvider uri`() { | ||
| mockkStatic(FILE_PROVIDER_UTILS) | ||
| every { any<Context>().fileProviderUriFor(any()) } returns uri | ||
| currentFile(File("a.cgt")) | ||
|
|
||
| val result = runBlocking { InstallFileAction(context, 0).execAction(data) } | ||
|
|
||
| val started = slot<Intent>() | ||
| verify(exactly = 1) { activity.startActivity(capture(started)) } | ||
| assertThat(result).isEqualTo(true) | ||
| assertThat(started.captured.component?.className).isEqualTo(ExternalFileInstallActivity::class.java.name) | ||
| assertThat(started.captured.data).isEqualTo(uri) | ||
| } | ||
|
|
||
| @Test | ||
| fun `a failed launch flashes the error and reports failure instead of crashing`() { | ||
| mockkStatic(FILE_PROVIDER_UTILS, FLASHBAR_UTILS) | ||
| every { any<Context>().fileProviderUriFor(any()) } returns uri | ||
| every { any<Activity>().flashError(any<String>()) } just runs | ||
| every { activity.startActivity(any<Intent>()) } throws ActivityNotFoundException() | ||
| currentFile(File("a.cgt")) | ||
|
|
||
| val result = runBlocking { InstallFileAction(context, 0).execAction(data) } | ||
|
|
||
| assertThat(result).isEqualTo(false) | ||
| verify(exactly = 1) { activity.flashError(any<String>()) } | ||
| } | ||
|
|
||
| private companion object { | ||
| const val FILE_PROVIDER_UTILS = "com.itsaky.androidide.utils.FileProviderUtilsKt" | ||
| const val FLASHBAR_UTILS = "com.itsaky.androidide.utils.FlashbarActivityUtilsKt" | ||
| } | ||
| } | ||
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.