|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: GPL-3.0-only |
| 3 | + * MuseScore-CLA-applies |
| 4 | + * |
| 5 | + * MuseScore |
| 6 | + * Music Composition & Notation |
| 7 | + * |
| 8 | + * Copyright (C) 2026 MuseScore Limited and others |
| 9 | + * |
| 10 | + * This program is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU General Public License version 3 as |
| 12 | + * published by the Free Software Foundation. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public License |
| 20 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 21 | + */ |
| 22 | +#include <gtest/gtest.h> |
| 23 | +#include <gmock/gmock.h> |
| 24 | + |
| 25 | +#include <QTemporaryDir> |
| 26 | + |
| 27 | +#include "modularity/ioc.h" |
| 28 | +#include "workspace/internal/workspacemanager.h" |
| 29 | +#include "workspace/iworkspaceconfiguration.h" |
| 30 | +#include "workspace/tests/mocks/workspaceconfigurationmock.h" |
| 31 | +#include "multiwindows/tests/mocks/multiwindowsprovidermock.h" |
| 32 | + |
| 33 | +using ::testing::Return; |
| 34 | + |
| 35 | +using namespace muse; |
| 36 | +using namespace muse::workspace; |
| 37 | + |
| 38 | +static const std::string BUILTIN_WORKSPACE_DIR = BUILTIN_WORKSPACES_DIR; |
| 39 | + |
| 40 | +namespace muse::workspace { |
| 41 | +class Workspace_WorkspaceManagerTests : public ::testing::Test |
| 42 | +{ |
| 43 | +public: |
| 44 | + void SetUp() override |
| 45 | + { |
| 46 | + m_userWorkspacesDir = std::make_unique<QTemporaryDir>(); |
| 47 | + ASSERT_TRUE(m_userWorkspacesDir->isValid()); |
| 48 | + |
| 49 | + m_multiWindowsProvider = std::make_shared<::testing::NiceMock<mi::MultiWindowsProviderMock> >(); |
| 50 | + modularity::globalIoc()->registerExport<mi::IMultiWindowsProvider>("utests", m_multiWindowsProvider); |
| 51 | + |
| 52 | + m_workspaceConfig = std::dynamic_pointer_cast<muse::workspace::WorkspaceConfigurationMock>( |
| 53 | + modularity::globalIoc()->resolve<IWorkspaceConfiguration>("utests")); |
| 54 | + ASSERT_NE(m_workspaceConfig, nullptr); |
| 55 | + |
| 56 | + m_userWorkspacesPath = m_userWorkspacesDir->path().toStdString(); |
| 57 | + } |
| 58 | + |
| 59 | + void TearDown() override |
| 60 | + { |
| 61 | + m_manager.reset(); |
| 62 | + modularity::globalIoc()->unregister<mi::IMultiWindowsProvider>("utests"); |
| 63 | + } |
| 64 | + |
| 65 | + void setupBuiltinPaths(const std::vector<std::string>& filenames) |
| 66 | + { |
| 67 | + io::paths_t paths; |
| 68 | + for (const auto& name : filenames) { |
| 69 | + paths.push_back(io::path_t(BUILTIN_WORKSPACE_DIR + "/" + name)); |
| 70 | + } |
| 71 | + |
| 72 | + ON_CALL(*m_workspaceConfig, builtinWorkspacesFilePaths()) |
| 73 | + .WillByDefault(Return(paths)); |
| 74 | + |
| 75 | + ON_CALL(*m_workspaceConfig, userWorkspacesPath()) |
| 76 | + .WillByDefault(Return(io::path_t(m_userWorkspacesPath))); |
| 77 | + } |
| 78 | + |
| 79 | + void initManager() |
| 80 | + { |
| 81 | + m_manager = std::make_unique<WorkspaceManager>(modularity::globalCtx()); |
| 82 | + m_manager->init(); |
| 83 | + } |
| 84 | + |
| 85 | +protected: |
| 86 | + std::unique_ptr<QTemporaryDir> m_userWorkspacesDir; |
| 87 | + std::string m_userWorkspacesPath; |
| 88 | + |
| 89 | + std::shared_ptr<::testing::NiceMock<mi::MultiWindowsProviderMock> > m_multiWindowsProvider; |
| 90 | + std::shared_ptr<muse::workspace::WorkspaceConfigurationMock> m_workspaceConfig; |
| 91 | + |
| 92 | + std::unique_ptr<WorkspaceManager> m_manager; |
| 93 | +}; |
| 94 | + |
| 95 | +TEST_F(Workspace_WorkspaceManagerTests, BuiltinWorkspaceLoadsOnInit) |
| 96 | +{ |
| 97 | + //! [GIVEN] Builtin workspace file exists |
| 98 | + setupBuiltinPaths({ "Default.mws" }); |
| 99 | + |
| 100 | + //! [GIVEN] Current workspace is "Default" |
| 101 | + ON_CALL(*m_workspaceConfig, currentWorkspaceName()) |
| 102 | + .WillByDefault(Return("Default")); |
| 103 | + |
| 104 | + //! [WHEN] WorkspaceManager is initialized |
| 105 | + initManager(); |
| 106 | + |
| 107 | + //! [THEN] Default workspace is loaded |
| 108 | + EXPECT_NE(m_manager->defaultWorkspace(), nullptr); |
| 109 | + EXPECT_EQ(m_manager->defaultWorkspace()->name(), "Default"); |
| 110 | + |
| 111 | + //! [THEN] Current workspace matches default |
| 112 | + EXPECT_NE(m_manager->currentWorkspace(), nullptr); |
| 113 | + EXPECT_EQ(m_manager->currentWorkspace()->name(), "Default"); |
| 114 | + |
| 115 | + //! [THEN] Builtin workspace is available |
| 116 | + EXPECT_EQ(m_manager->workspaces().size(), 1); |
| 117 | + |
| 118 | + m_manager->deinit(); |
| 119 | +} |
| 120 | + |
| 121 | +TEST_F(Workspace_WorkspaceManagerTests, FallbackToDefaultOnMissingCurrentWorkspace) |
| 122 | +{ |
| 123 | + //! [GIVEN] Builtin workspace file exists |
| 124 | + setupBuiltinPaths({ "Default.mws" }); |
| 125 | + |
| 126 | + //! [GIVEN] Current workspace name references a workspace that no longer exists |
| 127 | + ON_CALL(*m_workspaceConfig, currentWorkspaceName()) |
| 128 | + .WillByDefault(Return("MyDeletedWorkspace")); |
| 129 | + |
| 130 | + //! [GIVEN] Manager will correct the setting to default |
| 131 | + EXPECT_CALL(*m_workspaceConfig, setCurrentWorkspaceName("Default")) |
| 132 | + .Times(1); |
| 133 | + |
| 134 | + //! [WHEN] WorkspaceManager is initialized |
| 135 | + initManager(); |
| 136 | + |
| 137 | + //! [THEN] Falls back to default workspace |
| 138 | + EXPECT_NE(m_manager->currentWorkspace(), nullptr); |
| 139 | + EXPECT_EQ(m_manager->currentWorkspace()->name(), "Default"); |
| 140 | + EXPECT_NE(m_manager->defaultWorkspace(), nullptr); |
| 141 | + EXPECT_EQ(m_manager->defaultWorkspace()->name(), "Default"); |
| 142 | + |
| 143 | + m_manager->deinit(); |
| 144 | +} |
| 145 | + |
| 146 | +TEST_F(Workspace_WorkspaceManagerTests, EmptyUserDirUsesBuiltinWorkspaces) |
| 147 | +{ |
| 148 | + //! [GIVEN] Builtin workspace file exists, user dir is empty |
| 149 | + setupBuiltinPaths({ "Default.mws" }); |
| 150 | + |
| 151 | + ON_CALL(*m_workspaceConfig, currentWorkspaceName()) |
| 152 | + .WillByDefault(Return("Default")); |
| 153 | + |
| 154 | + //! [WHEN] WorkspaceManager is initialized |
| 155 | + initManager(); |
| 156 | + |
| 157 | + //! [THEN] Workspaces are loaded from builtins only |
| 158 | + EXPECT_EQ(m_manager->workspaces().size(), 1); |
| 159 | + |
| 160 | + //! [THEN] Current workspace is loaded and usable |
| 161 | + auto current = m_manager->currentWorkspace(); |
| 162 | + ASSERT_NE(current, nullptr); |
| 163 | + EXPECT_EQ(current->name(), "Default"); |
| 164 | + |
| 165 | + //! [THEN] Reading workspace data does not crash |
| 166 | + auto data = current->rawData("ui_settings"); |
| 167 | + EXPECT_TRUE(data.ret); |
| 168 | + |
| 169 | + m_manager->deinit(); |
| 170 | +} |
| 171 | +} |
0 commit comments