Repository navigation
ADFA-7141: Tell plugins the file that is shown, not the one left #2248
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
davidschachterADFA
wants to merge
9
commits into
stage
Choose a base branch
from
fix/ADFA-7141
base: stage
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
9 commits
Select commit
Hold shift + click to select a range
8aeb721
ADFA-7141: Tell plugins the file that is shown, not the one left
davidschachterADFA 274b2a3
ADFA-7141: Set the current file before the shown index, for listeners
davidschachterADFA ec9e549
ADFA-7141: Keep the opened file current while a new editor loads
davidschachterADFA ca10338
ADFA-7141: Close others by the opened file, not the editor's
davidschachterADFA 612f9eb
ADFA-7141: Show no file when an open plugin tab is selected
davidschachterADFA 909616f
ADFA-7141: Stop "Close others" when a close leaves the tab open
davidschachterADFA ac4b0ca
ADFA-7141: Carry on with "Close others" once the unsaved-changes dial…
davidschachterADFA 6669fb6
ADFA-7141: Keep the current and shown file indexes when an earlier ta…
davidschachterADFA ee8fd0f
ADFA-7141: Shift the editor indexes only after the closed tab's view …
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
170 changes: 170 additions & 0 deletions
170
app/src/test/java/com/itsaky/androidide/activities/editor/CloseOthersTest.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,170 @@ | ||
| package com.itsaky.androidide.activities.editor | ||
|
|
||
| import android.content.DialogInterface | ||
| import android.content.Intent | ||
| import android.os.Looper | ||
| import android.widget.ViewFlipper | ||
| import androidx.appcompat.app.AlertDialog | ||
| import com.google.android.material.tabs.TabLayout | ||
| import com.google.common.truth.Truth.assertThat | ||
| import com.itsaky.androidide.app.BaseApplication | ||
| import com.itsaky.androidide.databinding.ActivityEditorBinding | ||
| import com.itsaky.androidide.databinding.ContentEditorBinding | ||
| import com.itsaky.androidide.ui.CodeEditorView | ||
| import io.mockk.every | ||
| import io.mockk.mockk | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.Robolectric | ||
| import org.robolectric.RobolectricTestRunner | ||
| import org.robolectric.Shadows.shadowOf | ||
| import org.robolectric.annotation.Config | ||
| import org.robolectric.shadows.ShadowDialog | ||
| import java.io.File | ||
|
|
||
| /** | ||
| * "Close others" keeps the shown file (ADFA-7141). The current file is set from the opened file | ||
| * as soon as a tab is shown, while its editor's own file stays null until the text has loaded, or | ||
| * for good if the read failed. Comparing against the editor's file closed the shown tab instead. | ||
| */ | ||
| @RunWith(RobolectricTestRunner::class) | ||
| @Config(application = CloseOthersTest.TestApp::class) | ||
| class CloseOthersTest { | ||
| open class TestApp : BaseApplication() | ||
|
|
||
| private val shown = File("/project/Shown.kt") | ||
| private val a = File("/project/A.kt") | ||
| private val b = File("/project/B.kt") | ||
| private val t = File("/project/T.kt") | ||
|
|
||
| @Test | ||
| fun closeOthersKeepsAShownFileWhoseEditorHasNotLoaded() { | ||
| val activity = activityWithTabs(listOf(shown to null, a to a, b to b)) | ||
| activity.editorViewModel.selectFile(0) | ||
|
|
||
| activity.closeOthers() | ||
|
|
||
| assertThat(activity.editorViewModel.getOpenedFiles()).containsExactly(shown) | ||
| } | ||
|
|
||
| @Test | ||
| fun closeOthersKeepsALoadedShownFileInTheMiddle() { | ||
| val activity = activityWithTabs(listOf(a to a, shown to shown, b to b)) | ||
| activity.editorViewModel.selectFile(1, shown) | ||
|
|
||
| activity.closeOthers() | ||
|
|
||
| assertThat(activity.editorViewModel.getOpenedFiles()).containsExactly(shown) | ||
| } | ||
|
|
||
| // A's editor has unsaved changes but no file yet, so the check before the loop misses it, and | ||
| // closing it stops at the unsaved-changes dialog without closing anything. | ||
| @Test | ||
| fun closeOthersStopsWhenACloseAsksAboutUnsavedChanges() { | ||
| var checks = 0 | ||
| val activity = | ||
| activityWithTabs(listOf(shown to shown, a to null, b to b), modifiedAt = 1) { | ||
| check(++checks <= 20) { "closeOthers kept trying to close the same tab" } | ||
| } | ||
| activity.editorViewModel.selectFile(0, shown) | ||
|
|
||
| activity.closeOthers() | ||
|
|
||
| assertThat(checks).isEqualTo(1) | ||
| assertThat(activity.editorViewModel.getOpenedFiles()).containsExactly(shown, a, b).inOrder() | ||
| } | ||
|
|
||
| @Test | ||
| fun closeOthersCarriesOnOnceTheUnsavedChangesAreDiscarded() { | ||
| val activity = activityWithTabs(listOf(shown to shown, a to null, b to b), modifiedAt = 1) | ||
| activity.editorViewModel.selectFile(0, shown) | ||
| activity.closeOthers() | ||
|
|
||
| // "No": discard the changes and close. The dialog delivers the click through the main looper. | ||
| (ShadowDialog.getLatestDialog() as AlertDialog).getButton(DialogInterface.BUTTON_NEGATIVE).performClick() | ||
| shadowOf(Looper.getMainLooper()).idle() | ||
|
|
||
| assertThat(activity.editorViewModel.getOpenedFiles()).containsExactly(shown) | ||
| } | ||
|
|
||
| // ViewAnimator shows the child at displayedChild, and removeViewAt leaves the shown child alone. | ||
| // So the editor shows the wrong file unless the closed tab's view is gone before the shown | ||
| // index moves. | ||
| @Test | ||
| fun closingAnEarlierTabRemovesItsViewBeforeTheShownIndexMoves() { | ||
| val events = mutableListOf<String>() | ||
| val activity = activityWithTabs(listOf(a to a, b to b, shown to shown, t to t), events = events) | ||
| activity.editorViewModel.selectFile(2, shown) | ||
| events.clear() | ||
|
|
||
| activity.closeFile(0) | ||
|
|
||
| assertThat(events.first()).isEqualTo("removeViewAt:0") | ||
| assertThat(events.last()).isEqualTo("displayedChild:1") | ||
| assertThat(activity.editorViewModel.displayedFile()).isEqualTo(shown) | ||
| } | ||
|
|
||
| /** | ||
| * An editor activity, built but not created as in the other activity tests here, whose tab | ||
| * strip and editor container follow its opened files. Each pair is an opened file and the file | ||
| * its editor reports: null while the editor loads. The editor at [modifiedAt], if any, has | ||
| * unsaved changes until it is marked saved, and [onModifiedCheck] runs each time that is asked. | ||
| * With [events], the container's removeViewAt and displayedChild calls are recorded in it, and | ||
| * the displayed-index observer onCreate would register is stood in for: it sets displayedChild | ||
| * to the index, as there are no plugin tabs here. | ||
| */ | ||
| private fun activityWithTabs( | ||
| tabs: List<Pair<File, File?>>, | ||
| modifiedAt: Int = -1, | ||
| events: MutableList<String>? = null, | ||
| onModifiedCheck: () -> Unit = {}, | ||
| ): EditorHandlerActivity { | ||
| val activity = Robolectric.buildActivity(EditorHandlerActivity::class.java, Intent()).get() | ||
| var saved = false | ||
| val editors = | ||
| tabs | ||
| .mapIndexed { i, (_, editorFile) -> | ||
| mockk<CodeEditorView>(relaxed = true) { | ||
| every { file } returns editorFile | ||
| every { isModified } answers { | ||
| if (i == modifiedAt) onModifiedCheck() | ||
| i == modifiedAt && !saved | ||
| } | ||
| every { markAsSaved() } answers { if (i == modifiedAt) saved = true } | ||
| } | ||
| }.toMutableList() | ||
| val tabStrip = mockk<TabLayout>(relaxed = true) { every { tabCount } answers { editors.size } } | ||
| val container = | ||
| mockk<ViewFlipper>(relaxed = true) { | ||
| every { childCount } answers { editors.size } | ||
| every { getChildAt(any()) } answers { editors.getOrNull(firstArg()) } | ||
| every { removeViewAt(any()) } answers { | ||
| events?.add("removeViewAt:${firstArg<Int>()}") | ||
| editors.removeAt(firstArg<Int>()) | ||
| } | ||
| every { setDisplayedChild(any()) } answers { events?.add("displayedChild:${firstArg<Int>()}") } | ||
| } | ||
| // The binding's views are public Java fields, which mockk cannot stub. | ||
| val content = mockk<ContentEditorBinding>(relaxed = true) | ||
| ContentEditorBinding::class.java | ||
| .getDeclaredField("tabs") | ||
| .apply { isAccessible = true } | ||
| .set(content, tabStrip) | ||
| ContentEditorBinding::class.java | ||
| .getDeclaredField("editorContainer") | ||
| .apply { isAccessible = true } | ||
| .set(content, container) | ||
| val binding = mockk<ActivityEditorBinding>(relaxed = true) | ||
| ActivityEditorBinding::class.java | ||
| .getDeclaredField("content") | ||
| .apply { isAccessible = true } | ||
| .set(binding, content) | ||
| activity._binding = binding | ||
|
|
||
| tabs.forEach { (opened, _) -> activity.editorViewModel.addFile(opened) } | ||
| if (events != null) { | ||
| activity.editorViewModel._displayedFile.observeForever { container.displayedChild = it } | ||
| } | ||
| return activity | ||
| } | ||
| } |
Oops, something went wrong.
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.