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
4
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.
+253
−15
Open
Changes from all commits
Commits
Show all changes
4 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 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
96 changes: 96 additions & 0 deletions
96
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,96 @@ | ||
| package com.itsaky.androidide.activities.editor | ||
|
|
||
| import android.content.Intent | ||
| import android.widget.ViewFlipper | ||
| 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.annotation.Config | ||
| 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") | ||
|
|
||
| @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) | ||
| } | ||
|
|
||
| /** | ||
| * 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. | ||
| */ | ||
| private fun activityWithTabs(tabs: List<Pair<File, File?>>): EditorHandlerActivity { | ||
| val activity = Robolectric.buildActivity(EditorHandlerActivity::class.java, Intent()).get() | ||
| val editors = | ||
| tabs | ||
| .map { (_, editorFile) -> | ||
| mockk<CodeEditorView>(relaxed = true) { | ||
| every { file } returns editorFile | ||
| every { isModified } returns false | ||
| } | ||
| }.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 { editors.removeAt(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) } | ||
| return activity | ||
| } | ||
| } |
123 changes: 123 additions & 0 deletions
123
app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.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,123 @@ | ||
| package com.itsaky.androidide.viewmodel | ||
|
|
||
| import androidx.arch.core.executor.testing.InstantTaskExecutorRule | ||
| import com.google.common.truth.Truth.assertThat | ||
| import org.junit.Before | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
| import org.junit.rules.TestRule | ||
| import java.io.File | ||
|
|
||
| /** | ||
| * What plugins hear as the active file (ADFA-7141). EditorHandlerActivity reports | ||
| * [EditorViewModel.displayedFile] each time the displayed index changes, and plugins may read the | ||
| * current file in that callback; both used to name the tab being left. | ||
| */ | ||
| class EditorViewModelDisplayedFileTest { | ||
| @get:Rule | ||
| val rule: TestRule = InstantTaskExecutorRule() | ||
|
|
||
| private val a = File("/project/A.kt") | ||
| private val b = File("/project/B.kt") | ||
| private lateinit var viewModel: EditorViewModel | ||
| private val heard = mutableListOf<File?>() | ||
|
|
||
| // What a plugin reads with IdeEditorService.getCurrentFile() inside its callback. | ||
| private val currentInCallback = mutableListOf<File?>() | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| viewModel = EditorViewModel() | ||
| // The activity's observer, reduced to what it reports. | ||
| viewModel._displayedFile.observeForever { | ||
| heard += viewModel.displayedFile() | ||
| currentInCallback += viewModel.getCurrentFile() | ||
| } | ||
| heard.clear() | ||
| currentInCallback.clear() | ||
| } | ||
|
|
||
| private fun open(file: File) { | ||
| viewModel.addFile(file) | ||
| viewModel.setCurrentFile(viewModel.getOpenedFileCount() - 1, file) | ||
| } | ||
|
|
||
| @Test | ||
| fun openingAFileReportsThatFile() { | ||
| open(a) | ||
| open(b) | ||
|
|
||
| assertThat(heard).containsExactly(a, b).inOrder() | ||
| } | ||
|
|
||
| @Test | ||
| fun switchingTabsReportsTheTabSwitchedTo() { | ||
| open(a) | ||
| open(b) | ||
| heard.clear() | ||
|
|
||
| currentInCallback.clear() | ||
|
|
||
| // What BaseEditorActivity.onTabSelected does: select the index, then again with the editor's file. | ||
| viewModel.selectFile(0) | ||
| viewModel.selectFile(0, a) | ||
|
|
||
| assertThat(heard).containsExactly(a, a) | ||
| assertThat(currentInCallback).containsExactly(a, a) | ||
| } | ||
|
|
||
| @Test | ||
| fun aTabWhoseEditorHasNotLoadedKeepsTheOpenedFileCurrent() { | ||
| open(a) | ||
| open(b) | ||
|
|
||
| // A new editor knows its file only once its text has loaded. | ||
| viewModel.selectFile(0) | ||
| viewModel.selectFile(0, editorFile = null) | ||
|
|
||
| assertThat(viewModel.getCurrentFile()).isEqualTo(a) | ||
| } | ||
|
|
||
| @Test | ||
| fun aListenerReadingTheCurrentFileGetsTheShownFile() { | ||
| open(a) | ||
| open(b) | ||
| currentInCallback.clear() | ||
|
|
||
| viewModel.setCurrentFile(0, a) | ||
|
|
||
| assertThat(currentInCallback).containsExactly(a) | ||
| } | ||
|
|
||
| @Test | ||
| fun aPluginTabReportsNoFile() { | ||
| open(a) | ||
| heard.clear() | ||
|
|
||
| viewModel.displayedFileIndex = -1 | ||
|
|
||
| assertThat(heard).containsExactly(null) | ||
| } | ||
|
|
||
| @Test | ||
| fun closingTheLastFileReportsNoFile() { | ||
| open(a) | ||
| heard.clear() | ||
|
|
||
| viewModel.removeFile(0) | ||
|
|
||
| assertThat(heard).containsExactly(null) | ||
| assertThat(viewModel.getCurrentFile()).isNull() | ||
| } | ||
|
|
||
| @Test | ||
| fun closingAllFilesReportsNoFile() { | ||
| open(a) | ||
| open(b) | ||
| heard.clear() | ||
|
|
||
| viewModel.removeAllFiles() | ||
|
|
||
| assertThat(heard).containsExactly(null) | ||
| } | ||
| } |
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.
Publish
-1when a plugin tab is selected.Selecting an existing plugin tab does not change
displayedFileIndex. The_displayedFileobserver then publishes no null notification. Plugins keep the file shown before the switch, although the plugin tab is now displayed. Set the index to-1in this branch. This matches the plugin-tab creation path atEditorHandlerActivity.kt:2313.EditorViewModelDisplayedFileTestsets-1directly at line 64. It does not run this selection handler, so it does not cover this transition.Suggested fix
if (pluginTabId != null) { + editorViewModel.displayedFileIndex = -1 val tabManager = PluginEditorTabManager.getInstance() tabManager.onTabSelected(pluginTabId)