From 8aeb721ca26acfa5a7bf7d40a71ef2e9df67c6aa Mon Sep 17 00:00:00 2001 From: David Schachter Date: Thu, 12 Nov 2026 23:18:11 -0800 Subject: [PATCH 1/9] ADFA-7141: Tell plugins the file that is shown, not the one left Plugins' FileChangeListener heard the previously active file. The editor's displayed-index observer reported getCurrentFile(), but setCurrentFile() and BaseEditorActivity.onTabSelected set the index before the current file, so the observer read the tab being left. A plugin tab (index -1) reported the last file instead of null, and closing the last file reported nothing. - EditorViewModel.displayedFile() is the file at the displayed index, or null for -1. The observer reports that. - removeFile() resets to -1 when the last file closes, as removeAllFiles() does, so listeners hear that no file is shown. EditorViewModelDisplayedFileTest replays the observer against the ViewModel: opening, switching tabs in onTabSelected's order, a plugin tab, closing the last file and closing all files. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH --- .../editor/EditorHandlerActivity.kt | 2 +- .../androidide/viewmodel/EditorViewModel.kt | 11 ++- .../EditorViewModelDisplayedFileTest.kt | 90 +++++++++++++++++++ 3 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt index eb62c37071..9063edc611 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt @@ -356,7 +356,7 @@ open class EditorHandlerActivity : if (tabPosition >= 0) { this.content.editorContainer.displayedChild = tabPosition } - EditorEvents.notifyFileChanged(editorViewModel.getCurrentFile()) + EditorEvents.notifyFileChanged(editorViewModel.displayedFile()) } pluginEditorProvider = diff --git a/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt b/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt index e8d602b5a4..2679b2878b 100644 --- a/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt +++ b/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt @@ -408,7 +408,9 @@ class EditorViewModel : ViewModel() { files.removeAt(index) if (files.isEmpty()) { - mCurrentFile.value = null + // As removeAllFiles does: no tab is left to select, so nothing else would tell the + // displayed-file observers that no file is shown. + setCurrentFile(-1, null) } } @@ -472,6 +474,13 @@ class EditorViewModel : ViewModel() { fun getCurrentFile(): File? = mCurrentFile.value?.second + /** + * The file at [displayedFileIndex], or null when it is -1 (a plugin tab, or no tab). Observers + * of the displayed index read this, not [getCurrentFile]: [setCurrentFile] and the tab-selected + * callback set the index first, so the current file still names the tab being left. + */ + fun displayedFile(): File? = files.getOrNull(displayedFileIndex) + /** * Get the [OpenedFilesCache] if it is already loaded, otherwise read the cache from the file system * and invoke the given callback. diff --git a/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt b/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt new file mode 100644 index 0000000000..2ecb79134e --- /dev/null +++ b/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt @@ -0,0 +1,90 @@ +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; reading the current file + * there named the tab being left, because the index is set before the current file. + */ +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() + + @Before + fun setUp() { + viewModel = EditorViewModel() + // The activity's observer, reduced to what it reports. + viewModel._displayedFile.observeForever { heard += viewModel.displayedFile() } + heard.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() + + // The order of BaseEditorActivity.onTabSelected: the index first, then setCurrentFile. + viewModel.displayedFileIndex = 0 + viewModel.setCurrentFile(0, a) + + assertThat(heard).containsExactly(a, 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) + } +} From 274b2a39740582602a0fe7f3ec0a0a3567bcb37b Mon Sep 17 00:00:00 2001 From: David Schachter Date: Fri, 13 Nov 2026 03:21:16 -0800 Subject: [PATCH 2/9] ADFA-7141: Set the current file before the shown index, for listeners Plugins heard the right file after the first fix. But a plugin that asked IdeEditorService for the current file, or its text, inside its callback still got the tab being left: the callback runs as the shown index is set, and setCurrentFile() set the index first. An independent review found it. - setCurrentFile() sets the current file, then the index. - onTabSelected and openFile, which set the index on its own, call setCurrentFile() instead. - displayedFile()'s KDoc says when -1 happens: a plugin tab just added, or no file open. aListenerReadingTheCurrentFileGetsTheShownFile fails with the old order (the listener reads B while A is shown). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH --- .../activities/editor/BaseEditorActivity.kt | 4 +++- .../editor/EditorHandlerActivity.kt | 2 +- .../androidide/viewmodel/EditorViewModel.kt | 10 ++++---- .../EditorViewModelDisplayedFileTest.kt | 24 ++++++++++++++++--- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/BaseEditorActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/BaseEditorActivity.kt index 812c443225..cb8ebf6ae5 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/BaseEditorActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/BaseEditorActivity.kt @@ -1347,7 +1347,9 @@ abstract class BaseEditorActivity : return } - editorViewModel.displayedFileIndex = fileIndex + // With the current file, not the index alone: plugins hear the switch as the index is set, and + // may ask for the current file. + editorViewModel.setCurrentFile(fileIndex, editorViewModel.getOpenedFiles().getOrNull(fileIndex)) val editorView = if (this is EditorHandlerActivity) { diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt index 9063edc611..a79295361a 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt @@ -1151,7 +1151,7 @@ open class EditorHandlerActivity : if (fileIndex < 0) return@withContext null editorViewModel.startDrawerOpened = false - editorViewModel.displayedFileIndex = fileIndex + editorViewModel.setCurrentFile(fileIndex, file) val tabPosition = getTabPositionForFileIndex(fileIndex) val tab = content.tabs.getTabAt(tabPosition) diff --git a/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt b/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt index 2679b2878b..10f3d0c1ed 100644 --- a/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt +++ b/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt @@ -424,8 +424,10 @@ class EditorViewModel : ViewModel() { index: Int, file: File?, ) { - displayedFileIndex = index + // The current file first: observers of the displayed index run as it is set, and the plugins + // they notify may ask for the current file. mCurrentFile.value = index to file + displayedFileIndex = index } fun updateFile( @@ -475,9 +477,9 @@ class EditorViewModel : ViewModel() { fun getCurrentFile(): File? = mCurrentFile.value?.second /** - * The file at [displayedFileIndex], or null when it is -1 (a plugin tab, or no tab). Observers - * of the displayed index read this, not [getCurrentFile]: [setCurrentFile] and the tab-selected - * callback set the index first, so the current file still names the tab being left. + * The file at [displayedFileIndex], or null when it is -1 (a plugin tab just added, or no file + * open). Observers of the displayed index read this rather than [getCurrentFile], which a new + * plugin tab leaves at the last file. */ fun displayedFile(): File? = files.getOrNull(displayedFileIndex) diff --git a/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt b/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt index 2ecb79134e..9dbb07e716 100644 --- a/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt +++ b/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt @@ -22,12 +22,19 @@ class EditorViewModelDisplayedFileTest { private lateinit var viewModel: EditorViewModel private val heard = mutableListOf() + // What a plugin reads with IdeEditorService.getCurrentFile() inside its callback. + private val currentInCallback = mutableListOf() + @Before fun setUp() { viewModel = EditorViewModel() // The activity's observer, reduced to what it reports. - viewModel._displayedFile.observeForever { heard += viewModel.displayedFile() } + viewModel._displayedFile.observeForever { + heard += viewModel.displayedFile() + currentInCallback += viewModel.getCurrentFile() + } heard.clear() + currentInCallback.clear() } private fun open(file: File) { @@ -49,13 +56,24 @@ class EditorViewModelDisplayedFileTest { open(b) heard.clear() - // The order of BaseEditorActivity.onTabSelected: the index first, then setCurrentFile. - viewModel.displayedFileIndex = 0 + // What BaseEditorActivity.onTabSelected does: the file at the index, then the editor's file. + viewModel.setCurrentFile(0, viewModel.getOpenedFiles().getOrNull(0)) viewModel.setCurrentFile(0, a) assertThat(heard).containsExactly(a, a) } + @Test + fun aListenerReadingTheCurrentFileGetsTheShownFile() { + open(a) + open(b) + currentInCallback.clear() + + viewModel.setCurrentFile(0, a) + + assertThat(currentInCallback).containsExactly(a) + } + @Test fun aPluginTabReportsNoFile() { open(a) From ec9e549b583ca0f9e9c533b2e443dbf1f3582856 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Fri, 13 Nov 2026 05:16:50 -0800 Subject: [PATCH 3/9] ADFA-7141: Keep the opened file current while a new editor loads A new editor learns its file only once its text has loaded, so the second setCurrentFile(fileIndex, editorView.file) in onTabSelected replaced the right file with null after a file was opened. The current file then stayed null until the user switched tabs, which could hide the install action and confuse "Close others". This was already the case on stage; the previous commit made it a one-line fix. - EditorViewModel.selectFile(index, editorFile) shows the opened file at index, falling back to it while the editor's own file is null. onTabSelected calls it twice, so the tests pin the logic the activity uses. - The tab-switching test now also checks the current file a listener reads in its callback, and a new test covers an editor that has not loaded yet. They fail with the fallback removed, and with the index set before the current file. - KDoc fixes: the test class, and displayedFile()'s null cases. Found by an independent review of 274b2a397. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH --- .../activities/editor/BaseEditorActivity.kt | 4 +-- .../androidide/viewmodel/EditorViewModel.kt | 17 ++++++++++--- .../EditorViewModelDisplayedFileTest.kt | 25 +++++++++++++++---- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/BaseEditorActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/BaseEditorActivity.kt index cb8ebf6ae5..59790ffb84 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/BaseEditorActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/BaseEditorActivity.kt @@ -1349,7 +1349,7 @@ abstract class BaseEditorActivity : // With the current file, not the index alone: plugins hear the switch as the index is set, and // may ask for the current file. - editorViewModel.setCurrentFile(fileIndex, editorViewModel.getOpenedFiles().getOrNull(fileIndex)) + editorViewModel.selectFile(fileIndex) val editorView = if (this is EditorHandlerActivity) { @@ -1364,7 +1364,7 @@ abstract class BaseEditorActivity : } editorView.onEditorSelected() - editorViewModel.setCurrentFile(fileIndex, editorView.file) + editorViewModel.selectFile(fileIndex, editorView.file) refreshSymbolInput(editorView) invalidateOptionsMenu() } diff --git a/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt b/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt index 10f3d0c1ed..664a341061 100644 --- a/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt +++ b/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt @@ -477,12 +477,23 @@ class EditorViewModel : ViewModel() { fun getCurrentFile(): File? = mCurrentFile.value?.second /** - * The file at [displayedFileIndex], or null when it is -1 (a plugin tab just added, or no file - * open). Observers of the displayed index read this rather than [getCurrentFile], which a new - * plugin tab leaves at the last file. + * The file at [displayedFileIndex], or null when there is none there: -1 (a plugin tab just + * added, or no file open), or briefly an index past the end while a tab is added or closed. + * Observers of the displayed index read this rather than [getCurrentFile], which a new plugin + * tab leaves at the last file. */ fun displayedFile(): File? = files.getOrNull(displayedFileIndex) + /** + * Shows the opened file at [index], as a selected tab does. [editorFile] is the tab's editor's + * own file, null until a new editor has loaded its text; the opened file stands in until then, + * so the current file is never null while a file is shown. + */ + fun selectFile( + index: Int, + editorFile: File? = null, + ) = setCurrentFile(index, editorFile ?: files.getOrNull(index)) + /** * Get the [OpenedFilesCache] if it is already loaded, otherwise read the cache from the file system * and invoke the given callback. diff --git a/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt b/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt index 9dbb07e716..447e9c1d17 100644 --- a/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt +++ b/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt @@ -10,8 +10,8 @@ import java.io.File /** * What plugins hear as the active file (ADFA-7141). EditorHandlerActivity reports - * [EditorViewModel.displayedFile] each time the displayed index changes; reading the current file - * there named the tab being left, because the index is set before the current file. + * [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 @@ -56,11 +56,26 @@ class EditorViewModelDisplayedFileTest { open(b) heard.clear() - // What BaseEditorActivity.onTabSelected does: the file at the index, then the editor's file. - viewModel.setCurrentFile(0, viewModel.getOpenedFiles().getOrNull(0)) - viewModel.setCurrentFile(0, a) + 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 From ca103380222f38b13021a45898003cc26a99efaf Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 22:03:59 -0800 Subject: [PATCH 4/9] ADFA-7141: Close others by the opened file, not the editor's From an independent review of ec9e549b5. Since that commit, the current file is set from the opened file as soon as its tab is shown. But a CodeEditorView's file stays null until its text has loaded, or for good if the read failed. closeOthers compared the current file with each editor's file, so the shown tab didn't match itself and was closed. Example: with [Shown, A, B] and Shown still loading, "Close others" closed Shown and A and kept B. Before ec9e549b5 both sides were null in that case, which matched by accident. closeOthers now compares against the view model's opened file at each index. That also removes its skip of a missing editor, which spun forever because the index never changed. CloseOthersTest drives the real closeOthers and closeFile on a built, uncreated activity, with the tab strip and editor container mocked in step with the opened files: - keeps a shown file whose editor hasn't loaded. Against ec9e549b5 this fails: it keeps B.kt; - keeps a loaded shown file in the middle. 210 editor and view-model tests pass, and spotlessCheck is clean. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH --- .../editor/EditorHandlerActivity.kt | 13 +-- .../activities/editor/CloseOthersTest.kt | 96 +++++++++++++++++++ 2 files changed, 100 insertions(+), 9 deletions(-) create mode 100644 app/src/test/java/com/itsaky/androidide/activities/editor/CloseOthersTest.kt diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt index a79295361a..724af78792 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt @@ -1795,16 +1795,11 @@ open class EditorHandlerActivity : // keep closing the file at index 0 // if openedFiles[0] == file, then keep closing files at index 1 while (editorViewModel.getOpenedFileCount() != 1) { - val editor = getEditorAtIndex(index) - - if (editor == null) { - log.error("Unable to save file at index {}", index) - continue - } - // Index of files changes as we keep close files - // So we compare the files instead of index - if (file != editor.file) { + // So we compare the files instead of index. The opened file, not the editor's: an + // editor's file is null until its text has loaded, or for good if the read failed, + // while the current file is set from the opened file as soon as its tab is shown. + if (file != editorViewModel.getOpenedFile(index)) { closeFile(index) } else { index = 1 diff --git a/app/src/test/java/com/itsaky/androidide/activities/editor/CloseOthersTest.kt b/app/src/test/java/com/itsaky/androidide/activities/editor/CloseOthersTest.kt new file mode 100644 index 0000000000..ae8eb98a2c --- /dev/null +++ b/app/src/test/java/com/itsaky/androidide/activities/editor/CloseOthersTest.kt @@ -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>): EditorHandlerActivity { + val activity = Robolectric.buildActivity(EditorHandlerActivity::class.java, Intent()).get() + val editors = + tabs + .map { (_, editorFile) -> + mockk(relaxed = true) { + every { file } returns editorFile + every { isModified } returns false + } + }.toMutableList() + val tabStrip = mockk(relaxed = true) { every { tabCount } answers { editors.size } } + val container = + mockk(relaxed = true) { + every { childCount } answers { editors.size } + every { getChildAt(any()) } answers { editors.getOrNull(firstArg()) } + every { removeViewAt(any()) } answers { editors.removeAt(firstArg()) } + } + // The binding's views are public Java fields, which mockk cannot stub. + val content = mockk(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(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 + } +} From 612f9ebe26abdc101227e2892089b97bf846c7ad Mon Sep 17 00:00:00 2001 From: David Schachter Date: Mon, 16 Nov 2026 23:05:41 -0800 Subject: [PATCH 5/9] ADFA-7141: Show no file when an open plugin tab is selected Selecting a plugin tab that was already open left the shown index at the previous file. Plugins kept hearing that file while a plugin's tab was on screen. Adding a plugin tab already set the index to -1. Now selecting one does too, so listeners hear null in both cases, and switching back to a file is heard again. CodeRabbit raised this on 8aeb721ca; the PR had listed it as a known limitation. Only the observer that notifies plugins reads the shown index. The current file is unchanged: getCurrentFile() and the plugin API's IdeEditorService.getCurrentFile(), which falls back to the last real file on purpose, still give the last file on a plugin tab. PluginTabSelectionTest calls the real onTabSelected on a built, uncreated activity, with a plugin tab at position 2. Without this change it fails: expected -1 but was 0. CloseOthersTest and EditorViewModelDisplayedFileTest still pass, and spotlessCheck is clean. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH --- .../activities/editor/BaseEditorActivity.kt | 2 + .../editor/PluginTabSelectionTest.kt | 65 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 app/src/test/java/com/itsaky/androidide/activities/editor/PluginTabSelectionTest.kt diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/BaseEditorActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/BaseEditorActivity.kt index 59790ffb84..bcde992290 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/BaseEditorActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/BaseEditorActivity.kt @@ -1328,6 +1328,8 @@ abstract class BaseEditorActivity : if (this is EditorHandlerActivity && isPluginTab(position)) { val pluginTabId = getPluginTabId(position) if (pluginTabId != null) { + // No file is shown: listeners hear null, as when a plugin tab is first added. + editorViewModel.displayedFileIndex = -1 val tabManager = PluginEditorTabManager.getInstance() tabManager.onTabSelected(pluginTabId) invalidateOptionsMenu() diff --git a/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTabSelectionTest.kt b/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTabSelectionTest.kt new file mode 100644 index 0000000000..a33fc30096 --- /dev/null +++ b/app/src/test/java/com/itsaky/androidide/activities/editor/PluginTabSelectionTest.kt @@ -0,0 +1,65 @@ +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 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 + +/** + * Selecting a plugin tab that is already open shows no file (ADFA-7141), as adding one does, so + * listeners hear null rather than keep the file shown before it. + */ +@RunWith(RobolectricTestRunner::class) +@Config(application = PluginTabSelectionTest.TestApp::class) +class PluginTabSelectionTest { + open class TestApp : BaseApplication() + + @Test + fun selectingAnOpenPluginTabShowsNoFile() { + val activity = Robolectric.buildActivity(EditorHandlerActivity::class.java, Intent()).get() + val content = mockk(relaxed = true) + // The binding's views are public Java fields, which mockk cannot stub. + ContentEditorBinding::class.java + .getDeclaredField("tabs") + .apply { isAccessible = true } + .set(content, mockk(relaxed = true) { every { tabCount } returns 3 }) + ContentEditorBinding::class.java + .getDeclaredField("editorContainer") + .apply { isAccessible = true } + .set(content, mockk(relaxed = true)) + val binding = mockk(relaxed = true) + ActivityEditorBinding::class.java + .getDeclaredField("content") + .apply { isAccessible = true } + .set(binding, content) + activity._binding = binding + // Two file tabs, then a plugin's tab at position 2. + @Suppress("UNCHECKED_CAST") + val pluginTabs = + EditorHandlerActivity::class.java + .getDeclaredField("tabIndexToPluginId") + .apply { isAccessible = true } + .get(activity) as MutableMap + pluginTabs[2] = "some.plugin.tab" + val a = File("/project/A.kt") + activity.editorViewModel.addFile(a) + activity.editorViewModel.addFile(File("/project/B.kt")) + activity.editorViewModel.selectFile(0, a) + + activity.onTabSelected(mockk { every { position } returns 2 }) + + assertThat(activity.editorViewModel.displayedFileIndex).isEqualTo(-1) + assertThat(activity.editorViewModel.displayedFile()).isNull() + } +} From 909616f701c3270fd8182aba95cb9400b985f986 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 08:25:16 -0800 Subject: [PATCH 6/9] ADFA-7141: Stop "Close others" when a close leaves the tab open From an independent review of ca1033802 and 612f9ebe2 (F05, a nitpick). closeFile can return without closing anything: it shows the unsaved-changes dialog and returns. closeOthers then tried the same tab again forever, spinning on the main thread. The check for unsaved files before the loop finds editors by their file, which is null until an editor loads, so in principle it could miss one. The review couldn't reach this, and the loop had this shape before ADFA-7141 too, but the cost would be a frozen app. The loop now stops when a close leaves the number of open files unchanged. CloseOthersTest gains closeOthersStopsWhenACloseAsksAboutUnsavedChanges: - A's editor has unsaved changes and no file yet, so closing it stops at the dialog. - Without the guard it fails: "closeOthers kept trying to close the same tab", raised after 20 checks. - With the guard, the check runs once and all three files stay open. 212 editor and view-model tests pass on a forced rerun, and spotlessCheck is clean. OutlineViewModelTest failed once in three fresh runs, with an uncaught Dispatchers.Main access from an earlier test. It passed in the other two, and nothing ties it to this change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH --- .../editor/EditorHandlerActivity.kt | 4 +++ .../activities/editor/CloseOthersTest.kt | 33 ++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt index 724af78792..84bcd1604d 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt @@ -1800,7 +1800,11 @@ open class EditorHandlerActivity : // editor's file is null until its text has loaded, or for good if the read failed, // while the current file is set from the opened file as soon as its tab is shown. if (file != editorViewModel.getOpenedFile(index)) { + val openedBefore = editorViewModel.getOpenedFileCount() closeFile(index) + // closeFile can return without closing, as when it asks about unsaved changes; trying + // again would spin on the main thread. + if (editorViewModel.getOpenedFileCount() == openedBefore) break } else { index = 1 } diff --git a/app/src/test/java/com/itsaky/androidide/activities/editor/CloseOthersTest.kt b/app/src/test/java/com/itsaky/androidide/activities/editor/CloseOthersTest.kt index ae8eb98a2c..a227d9e2dd 100644 --- a/app/src/test/java/com/itsaky/androidide/activities/editor/CloseOthersTest.kt +++ b/app/src/test/java/com/itsaky/androidide/activities/editor/CloseOthersTest.kt @@ -51,19 +51,44 @@ class CloseOthersTest { 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() + } + /** * 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. + * its editor reports: null while the editor loads. The editor at [modifiedAt], if any, has + * unsaved changes, and [onModifiedCheck] runs each time that is asked. */ - private fun activityWithTabs(tabs: List>): EditorHandlerActivity { + private fun activityWithTabs( + tabs: List>, + modifiedAt: Int = -1, + onModifiedCheck: () -> Unit = {}, + ): EditorHandlerActivity { val activity = Robolectric.buildActivity(EditorHandlerActivity::class.java, Intent()).get() val editors = tabs - .map { (_, editorFile) -> + .mapIndexed { i, (_, editorFile) -> mockk(relaxed = true) { every { file } returns editorFile - every { isModified } returns false + every { isModified } answers { + if (i == modifiedAt) onModifiedCheck() + i == modifiedAt + } } }.toMutableList() val tabStrip = mockk(relaxed = true) { every { tabCount } answers { editors.size } } From ac4b0caacc57a35729a916dedf3da7a34d2d7c7e Mon Sep 17 00:00:00 2001 From: David Schachter Date: Tue, 17 Nov 2026 10:45:37 -0800 Subject: [PATCH 7/9] ADFA-7141: Carry on with "Close others" once the unsaved-changes dialog is answered When a close stopped at the unsaved-changes dialog, "Close others" stopped too, so after the user discarded or saved the changes only that tab closed and the rest stayed open. The close now carries on with the remaining tabs once the dialog is answered. A close that finishes at once does not re-run it. Raised by CodeRabbit's review of 909616f70. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH --- .../editor/EditorHandlerActivity.kt | 9 +++++-- .../activities/editor/CloseOthersTest.kt | 24 +++++++++++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt index 84bcd1604d..41a114fa8c 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt @@ -1791,6 +1791,8 @@ open class EditorHandlerActivity : val file = editorViewModel.getCurrentFile() var index = 0 + // Set when a close waits for the unsaved-changes dialog; that close carries on once answered. + var waiting = false // keep closing the file at index 0 // if openedFiles[0] == file, then keep closing files at index 1 @@ -1801,10 +1803,13 @@ open class EditorHandlerActivity : // while the current file is set from the opened file as soon as its tab is shown. if (file != editorViewModel.getOpenedFile(index)) { val openedBefore = editorViewModel.getOpenedFileCount() - closeFile(index) + closeFile(index) { if (waiting) closeOthers() } // closeFile can return without closing, as when it asks about unsaved changes; trying // again would spin on the main thread. - if (editorViewModel.getOpenedFileCount() == openedBefore) break + if (editorViewModel.getOpenedFileCount() == openedBefore) { + waiting = true + break + } } else { index = 1 } diff --git a/app/src/test/java/com/itsaky/androidide/activities/editor/CloseOthersTest.kt b/app/src/test/java/com/itsaky/androidide/activities/editor/CloseOthersTest.kt index a227d9e2dd..d5a24644d8 100644 --- a/app/src/test/java/com/itsaky/androidide/activities/editor/CloseOthersTest.kt +++ b/app/src/test/java/com/itsaky/androidide/activities/editor/CloseOthersTest.kt @@ -1,7 +1,10 @@ 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 @@ -14,7 +17,9 @@ 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 /** @@ -68,11 +73,24 @@ class CloseOthersTest { 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) + } + /** * 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, and [onModifiedCheck] runs each time that is asked. + * unsaved changes until it is marked saved, and [onModifiedCheck] runs each time that is asked. */ private fun activityWithTabs( tabs: List>, @@ -80,6 +98,7 @@ class CloseOthersTest { onModifiedCheck: () -> Unit = {}, ): EditorHandlerActivity { val activity = Robolectric.buildActivity(EditorHandlerActivity::class.java, Intent()).get() + var saved = false val editors = tabs .mapIndexed { i, (_, editorFile) -> @@ -87,8 +106,9 @@ class CloseOthersTest { every { file } returns editorFile every { isModified } answers { if (i == modifiedAt) onModifiedCheck() - i == modifiedAt + i == modifiedAt && !saved } + every { markAsSaved() } answers { if (i == modifiedAt) saved = true } } }.toMutableList() val tabStrip = mockk(relaxed = true) { every { tabCount } answers { editors.size } } From 6669fb647f4596bfbc1a00412a3ac51a06acde16 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Wed, 18 Nov 2026 18:46:58 -0800 Subject: [PATCH 8/9] ADFA-7141: Keep the current and shown file indexes when an earlier tab closes removeFile left the current and displayed indexes stale when a tab before the selected one closed (TabLayout fires no selection event then). With tabs [A, B, S] and S shown, closing A and B left both at 2: displayedFile() was null. Now both shift down by one when the removed index is lower, current file first. Tests cover [A,B,S], [A,B,S,T] and a later tab; the first two fail without the fix. Raised by a code review relayed in ADFA-7141 (MEDIUM-2). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH --- .../androidide/viewmodel/EditorViewModel.kt | 15 +++++- .../EditorViewModelDisplayedFileTest.kt | 46 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt b/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt index 664a341061..132b92699f 100644 --- a/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt +++ b/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt @@ -399,7 +399,8 @@ class EditorViewModel : ViewModel() { } /** - * Remove the file at the given index from the list of opened files. + * Remove the file at the given index from the list of opened files. The current and shown + * indexes keep pointing at the same files when an earlier tab is removed. * * @param index The index of the closed file. */ @@ -411,6 +412,18 @@ class EditorViewModel : ViewModel() { // As removeAllFiles does: no tab is left to select, so nothing else would tell the // displayed-file observers that no file is shown. setCurrentFile(-1, null) + return@updateFiles + } + + // The tab bar fires no selection event when a tab before the selected one closes. + // Closing the shown tab itself is left to the tab selected next. + // Current file first, as setCurrentFile does: displayed-index observers may read it. + val current = mCurrentFile.value + if (current != null && index < current.first) { + mCurrentFile.value = (current.first - 1) to current.second + } + if (index < displayedFileIndex) { + displayedFileIndex -= 1 } } diff --git a/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt b/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt index 447e9c1d17..cae21bf699 100644 --- a/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt +++ b/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt @@ -19,6 +19,8 @@ class EditorViewModelDisplayedFileTest { private val a = File("/project/A.kt") private val b = File("/project/B.kt") + private val s = File("/project/S.kt") + private val t = File("/project/T.kt") private lateinit var viewModel: EditorViewModel private val heard = mutableListOf() @@ -120,4 +122,48 @@ class EditorViewModelDisplayedFileTest { assertThat(heard).containsExactly(null) } + + @Test + fun closingEarlierTabsKeepsTheShownFileCurrent() { + open(a) + open(b) + open(s) + + viewModel.removeFile(0) + viewModel.removeFile(0) + + assertThat(viewModel.displayedFile()).isEqualTo(s) + assertThat(viewModel.displayedFileIndex).isEqualTo(0) + assertThat(viewModel.getCurrentFileIndex()).isEqualTo(0) + assertThat(viewModel.getCurrentFile()).isEqualTo(s) + } + + @Test + fun closingAnEarlierTabDoesNotShowTheNextFile() { + open(a) + open(b) + open(s) + open(t) + viewModel.setCurrentFile(2, s) + + viewModel.removeFile(0) + + assertThat(viewModel.displayedFile()).isEqualTo(s) + assertThat(viewModel.displayedFileIndex).isEqualTo(1) + assertThat(viewModel.getCurrentFileIndex()).isEqualTo(1) + } + + @Test + fun closingALaterTabLeavesTheIndexesAlone() { + open(a) + open(s) + open(b) + viewModel.setCurrentFile(1, s) + + viewModel.removeFile(2) + + assertThat(viewModel.displayedFile()).isEqualTo(s) + assertThat(viewModel.displayedFileIndex).isEqualTo(1) + assertThat(viewModel.getCurrentFileIndex()).isEqualTo(1) + } } From ee8fd0fdb4e8b9532a847d365ac7a93a6af0d535 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Wed, 18 Nov 2026 20:22:29 -0800 Subject: [PATCH 9/9] ADFA-7141: Shift the editor indexes only after the closed tab's view is gone 6669fb647 shifted the shown index while the closed tab's view still existed, so with [A, B, S, T] and S shown, closing A showed B's text under S's tab. Confirmed on a device by deleting A.java from the file tree, and raised by CodeRabbit. Fix: closeFile now removes the tab and view inside removeFile, before the shift. Tests: a ViewModel test and a closeFile test in CloseOthersTest; both fail with the old order. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH --- .../editor/EditorHandlerActivity.kt | 12 +++--- .../androidide/viewmodel/EditorViewModel.kt | 39 +++++++++++++------ .../activities/editor/CloseOthersTest.kt | 31 ++++++++++++++- .../EditorViewModelDisplayedFileTest.kt | 21 ++++++++++ 4 files changed, 84 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt index 41a114fa8c..13f3d6ece3 100644 --- a/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt +++ b/app/src/main/java/com/itsaky/androidide/activities/editor/EditorHandlerActivity.kt @@ -1760,12 +1760,12 @@ open class EditorHandlerActivity : } val tabPosition = getTabPositionForFileIndex(index) - editorViewModel.removeFile(index) - - if (tabPosition >= 0) { - content.tabs.removeTabAt(tabPosition) - content.editorContainer.removeViewAt(tabPosition) - shiftPluginIndices(tabPosition + 1, -1) + editorViewModel.removeFile(index) { + if (tabPosition >= 0) { + content.tabs.removeTabAt(tabPosition) + content.editorContainer.removeViewAt(tabPosition) + shiftPluginIndices(tabPosition + 1, -1) + } } editorViewModel.areFilesModified = hasUnsavedFiles() diff --git a/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt b/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt index 132b92699f..700d5396d7 100644 --- a/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt +++ b/app/src/main/java/com/itsaky/androidide/viewmodel/EditorViewModel.kt @@ -402,9 +402,18 @@ class EditorViewModel : ViewModel() { * Remove the file at the given index from the list of opened files. The current and shown * indexes keep pointing at the same files when an earlier tab is removed. * + * The shift waits for [removeViews]: the displayed-index observer maps the index through the tab + * bar and the editor container, so shifting while the closed tab's view still exists would show + * the wrong view. + * * @param index The index of the closed file. + * @param removeViews Removes the closed file's tab and editor view. Runs after the `files` + * observers and before the indexes shift. */ - fun removeFile(index: Int) = + fun removeFile( + index: Int, + removeViews: () -> Unit = {}, + ) { updateFiles { files -> files.removeAt(index) @@ -412,20 +421,26 @@ class EditorViewModel : ViewModel() { // As removeAllFiles does: no tab is left to select, so nothing else would tell the // displayed-file observers that no file is shown. setCurrentFile(-1, null) - return@updateFiles } + } - // The tab bar fires no selection event when a tab before the selected one closes. - // Closing the shown tab itself is left to the tab selected next. - // Current file first, as setCurrentFile does: displayed-index observers may read it. - val current = mCurrentFile.value - if (current != null && index < current.first) { - mCurrentFile.value = (current.first - 1) to current.second - } - if (index < displayedFileIndex) { - displayedFileIndex -= 1 - } + removeViews() + + if (this.files.isEmpty()) { + return + } + + // The tab bar fires no selection event when a tab before the selected one closes. + // Closing the shown tab itself is left to the tab selected next. + // Current file first, as setCurrentFile does: displayed-index observers may read it. + val current = mCurrentFile.value + if (current != null && index < current.first) { + mCurrentFile.value = (current.first - 1) to current.second + } + if (index < displayedFileIndex) { + displayedFileIndex -= 1 } + } fun removeAllFiles() = updateFiles { files -> diff --git a/app/src/test/java/com/itsaky/androidide/activities/editor/CloseOthersTest.kt b/app/src/test/java/com/itsaky/androidide/activities/editor/CloseOthersTest.kt index d5a24644d8..f1f35d4040 100644 --- a/app/src/test/java/com/itsaky/androidide/activities/editor/CloseOthersTest.kt +++ b/app/src/test/java/com/itsaky/androidide/activities/editor/CloseOthersTest.kt @@ -35,6 +35,7 @@ class CloseOthersTest { 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() { @@ -86,15 +87,36 @@ class CloseOthersTest { 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() + 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>, modifiedAt: Int = -1, + events: MutableList? = null, onModifiedCheck: () -> Unit = {}, ): EditorHandlerActivity { val activity = Robolectric.buildActivity(EditorHandlerActivity::class.java, Intent()).get() @@ -116,7 +138,11 @@ class CloseOthersTest { mockk(relaxed = true) { every { childCount } answers { editors.size } every { getChildAt(any()) } answers { editors.getOrNull(firstArg()) } - every { removeViewAt(any()) } answers { editors.removeAt(firstArg()) } + every { removeViewAt(any()) } answers { + events?.add("removeViewAt:${firstArg()}") + editors.removeAt(firstArg()) + } + every { setDisplayedChild(any()) } answers { events?.add("displayedChild:${firstArg()}") } } // The binding's views are public Java fields, which mockk cannot stub. val content = mockk(relaxed = true) @@ -136,6 +162,9 @@ class CloseOthersTest { activity._binding = binding tabs.forEach { (opened, _) -> activity.editorViewModel.addFile(opened) } + if (events != null) { + activity.editorViewModel._displayedFile.observeForever { container.displayedChild = it } + } return activity } } diff --git a/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt b/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt index cae21bf699..647d1244bd 100644 --- a/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt +++ b/app/src/test/java/com/itsaky/androidide/viewmodel/EditorViewModelDisplayedFileTest.kt @@ -166,4 +166,25 @@ class EditorViewModelDisplayedFileTest { assertThat(viewModel.displayedFileIndex).isEqualTo(1) assertThat(viewModel.getCurrentFileIndex()).isEqualTo(1) } + + // The activity's observer maps the shown index to a tab and a view, so the index must not move + // while the closed tab's view still exists. + @Test + fun theShownIndexChangesOnlyAfterTheClosedTabsViewIsGone() { + open(a) + open(b) + open(s) + open(t) + viewModel.setCurrentFile(2, s) + var viewsRemoved = false + val removedAtEachFiring = mutableListOf() + viewModel._displayedFile.observeForever { removedAtEachFiring += viewsRemoved } + removedAtEachFiring.clear() + + viewModel.removeFile(0) { viewsRemoved = true } + + assertThat(removedAtEachFiring).isNotEmpty() + assertThat(removedAtEachFiring).doesNotContain(false) + assertThat(viewModel.displayedFile()).isEqualTo(s) + } }