aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorSamuel Ghinet <samuel.ghinet@qt.io>2022-03-21 23:18:59 +0200
committerSamuel Ghinet <samuel.ghinet@qt.io>2022-03-24 10:31:16 +0000
commit3935c671dd3adcdb0fad7e8b57735868c81082c5 (patch)
tree78cb56b9cc950a188c221734426b64d00668a14d /tests
parent4b96545a23486687aa04b91199beb302e4b68791 (diff)
QDS New Project Dialog fix: recents should include all project properties
Previously, only the wizard category, name, and size were saved for recent presets. Solved the problem by using the same kind of store (and struct type) for Recent presets as for User/Custom presets - this way we can save all properties. Other changes introduced: * After user creates custom preset C, then creates a project from it (resulting in the creation of a Recent preset R), if the user then deletes custom preset C, then the recent preset R will remain - previously, all recents of the custom preset were deleted * Now we can have multiple recent presets with the same name and size - so, no distinguishing feature inside the Presets view. User will have to look at Details and Styles panes to view differences. * Replaced .ini format with *.json file format. Change-Id: I500e9ac9378d4b9a393c3b0833ef6a34f785585c Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qml/qmldesigner/wizard/CMakeLists.txt2
-rw-r--r--tests/auto/qml/qmldesigner/wizard/presetmodel-test.cpp116
-rw-r--r--tests/auto/qml/qmldesigner/wizard/recentpresets-test.cpp303
-rw-r--r--tests/auto/qml/qmldesigner/wizard/userpresets-test.cpp174
4 files changed, 188 insertions, 407 deletions
diff --git a/tests/auto/qml/qmldesigner/wizard/CMakeLists.txt b/tests/auto/qml/qmldesigner/wizard/CMakeLists.txt
index c5040112ef..a2965b22f1 100644
--- a/tests/auto/qml/qmldesigner/wizard/CMakeLists.txt
+++ b/tests/auto/qml/qmldesigner/wizard/CMakeLists.txt
@@ -17,14 +17,12 @@ add_qtc_test(tst_qml_wizard
SOURCES
wizardfactories-test.cpp
stylemodel-test.cpp
- recentpresets-test.cpp
userpresets-test.cpp
presetmodel-test.cpp
test-utilities.h
test-main.cpp
"${StudioWelcomeDir}/wizardfactories.cpp"
"${StudioWelcomeDir}/stylemodel.cpp"
- "${StudioWelcomeDir}/recentpresets.cpp"
"${StudioWelcomeDir}/userpresets.cpp"
"${StudioWelcomeDir}/presetmodel.cpp"
)
diff --git a/tests/auto/qml/qmldesigner/wizard/presetmodel-test.cpp b/tests/auto/qml/qmldesigner/wizard/presetmodel-test.cpp
index 3c16688458..8452ca8222 100644
--- a/tests/auto/qml/qmldesigner/wizard/presetmodel-test.cpp
+++ b/tests/auto/qml/qmldesigner/wizard/presetmodel-test.cpp
@@ -100,6 +100,14 @@ UserPresetData aUserPreset(const QString &categId, const QString &wizardName, co
return preset;
}
+UserPresetData aRecentPreset(const QString &categId, const QString &wizardName, const QString &screenSizeName)
+{
+ UserPresetData preset = aUserPreset(categId, wizardName, wizardName);
+ preset.screenSize = screenSizeName;
+
+ return preset;
+}
+
MATCHER_P2(PresetIs, category, name, PrintToString(PresetItem{name, category}))
{
return arg->categoryId == category && arg->wizardName == name;
@@ -139,6 +147,7 @@ TEST(QdsPresetModel, haveSameArraySizeForPresetsAndCategories)
PresetData data;
data.setData(
+ /*wizard presets*/
{
aCategory("A.categ", "A", {"item a", "item b"}),
aCategory("B.categ", "B", {"item c", "item d"}),
@@ -157,6 +166,7 @@ TEST(QdsPresetModel, haveWizardPresetsNoRecents)
// When
data.setData(
+ /*wizard presets*/
{
aCategory("A.categ", "A", {"item a", "item b"}),
aCategory("B.categ", "B", {"item c", "item d"}),
@@ -179,6 +189,7 @@ TEST(QdsPresetModel, whenHaveUserPresetsButNoWizardPresetsReturnEmpty)
// When
data.setData({/*builtin presets*/},
+ /*user presets*/
{
aUserPreset("A.Mobile", "Scroll", "iPhone5"),
aUserPreset("B.Desktop", "Launcher", "MacBook"),
@@ -196,9 +207,10 @@ TEST(QdsPresetModel, haveRecentsNoWizardPresets)
data.setData({/*wizardPresets*/},
{/*user presets*/},
+ /*recent presets*/
{
- {"A.categ", "Desktop", "640 x 480"},
- {"B.categ", "Mobile", "800 x 600"},
+ aRecentPreset("A.categ", "Desktop", "640 x 480"),
+ aRecentPreset("B.categ", "Mobile", "800 x 600"),
});
ASSERT_THAT(data.categories(), IsEmpty());
@@ -220,8 +232,8 @@ TEST(QdsPresetModel, recentsAddedWithWizardPresets)
{/*user presets*/},
/*recents*/
{
- {"A.categ", "Desktop", "800 x 600"},
- {"B.categ", "Mobile", "640 x 480"},
+ aRecentPreset("A.categ", "Desktop", "800 x 600"),
+ aRecentPreset("B.categ", "Mobile", "640 x 480"),
});
// Then
@@ -247,6 +259,7 @@ TEST(QdsPresetModel, userPresetsAddedWithWizardPresets)
aCategory("A.categ", "A", {"Desktop", "item b"}),
aCategory("B.categ", "B", {"Mobile"}),
},
+ /*user presets*/
{
aUserPreset("A.categ", "Desktop", "Windows10"),
},
@@ -272,6 +285,7 @@ TEST(QdsPresetModel, doesNotAddUserPresetsOfNonExistingCategory)
{
aCategory("A.categ", "A", {"Desktop"}), // Only category "A.categ" exists
},
+ /*user presets*/
{
aUserPreset("Bad.Categ", "Desktop", "Windows8"), // Bad.Categ does not exist
},
@@ -293,6 +307,7 @@ TEST(QdsPresetModel, doesNotAddUserPresetIfWizardPresetItRefersToDoesNotExist)
{
aCategory("A.categ", "A", {"Desktop"}),
},
+ /*user presets*/
{
aUserPreset("B.categ", "BadWizard", "Tablet"), // BadWizard referenced does not exist
},
@@ -314,6 +329,7 @@ TEST(QdsPresetModel, userPresetWithSameNameAsWizardPreset)
{
aCategory("A.categ", "A", {"Desktop"}),
},
+ /*user presets*/
{
aUserPreset("A.categ", "Desktop", "Desktop"),
},
@@ -326,34 +342,7 @@ TEST(QdsPresetModel, userPresetWithSameNameAsWizardPreset)
ElementsAre(UserPresetIs("A.categ", "Desktop", "Desktop"))));
}
-TEST(QdsPresetModel, recentOfUserPresetReferringToExistingWizardPreset)
-{
- // Given
- PresetData data;
-
- // When
- data.setData(
- /*wizard presets*/
- {
- aCategory("A.categ", "A", {"Desktop"}),
- },
- {
- aUserPreset("A.categ", "Desktop", "Windows 7"),
- },
- /*recents*/
- {
- {"A.categ", "Windows 7", "200 x 300", /*is user*/true}
- });
-
- // Then
- ASSERT_THAT(data.categories(), ElementsAre("Recents", "A", "Custom"));
- ASSERT_THAT(data.presets(),
- ElementsAre(ElementsAre(UserPresetIs("A.categ", "Desktop", "Windows 7")),
- ElementsAre(PresetIs("A.categ", "Desktop")),
- ElementsAre(UserPresetIs("A.categ", "Desktop", "Windows 7"))));
-}
-
-TEST(QdsPresetModel, recentOfUserPresetReferringToNonexistingWizardPreset)
+TEST(QdsPresetModel, recentOfNonExistentWizardPreset)
{
// Given
PresetData data;
@@ -364,12 +353,10 @@ TEST(QdsPresetModel, recentOfUserPresetReferringToNonexistingWizardPreset)
{
aCategory("A.categ", "A", {"Desktop"}),
},
- {
- aUserPreset("A.categ", "Not-Desktop", "Windows 7"), // Non-existing Wizard Preset
- },
+ {/*user presets*/},
/*recents*/
{
- {"A.categ", "Windows 7", "200 x 300", /*is user*/true}
+ aRecentPreset("A.categ", "Windows 7", "200 x 300")
});
// Then
@@ -377,7 +364,7 @@ TEST(QdsPresetModel, recentOfUserPresetReferringToNonexistingWizardPreset)
ASSERT_THAT(data.presets(), ElementsAre(ElementsAre(PresetIs("A.categ", "Desktop"))));
}
-TEST(QdsPresetModel, recentOfNonExistentUserPreset)
+TEST(QdsPresetModel, recentsShouldNotBeSorted)
{
// Given
PresetData data;
@@ -386,20 +373,26 @@ TEST(QdsPresetModel, recentOfNonExistentUserPreset)
data.setData(
/*wizard presets*/
{
- aCategory("A.categ", "A", {"Desktop"}),
+ aCategory("A.categ", "A", {"Desktop", "item b"}),
+ aCategory("B.categ", "B", {"item c", "Mobile"}),
+ aCategory("Z.categ", "Z", {"Z.desktop"}),
},
{/*user presets*/},
/*recents*/
{
- {"A.categ", "Windows 7", "200 x 300", /*is user*/true}
+ aRecentPreset("Z.categ", "Z.desktop", "200 x 300"),
+ aRecentPreset("B.categ", "Mobile", "200 x 300"),
+ aRecentPreset("A.categ", "Desktop", "200 x 300"),
});
// Then
- ASSERT_THAT(data.categories(), ElementsAre("A"));
- ASSERT_THAT(data.presets(), ElementsAre(ElementsAre(PresetIs("A.categ", "Desktop"))));
+ ASSERT_THAT(data.presets()[0],
+ ElementsAre(PresetIs("Z.categ", "Z.desktop"),
+ PresetIs("B.categ", "Mobile"),
+ PresetIs("A.categ", "Desktop")));
}
-TEST(QdsPresetModel, recentsShouldNotBeSorted)
+TEST(QdsPresetModel, recentsOfSameWizardProjectButDifferentSizesAreRecognizedAsDifferentPresets)
{
// Given
PresetData data;
@@ -408,26 +401,25 @@ TEST(QdsPresetModel, recentsShouldNotBeSorted)
data.setData(
/*wizard presets*/
{
- aCategory("A.categ", "A", {"Desktop", "item b"}),
- aCategory("B.categ", "B", {"item c", "Mobile"}),
- aCategory("Z.categ", "Z", {"Z.desktop"}),
+ aCategory("A.categ", "A", {"Desktop"}),
+ aCategory("B.categ", "B", {"Mobile"}),
},
{/*user presets*/},
/*recents*/
{
- {"Z.categ", "Z.desktop", "200 x 300"},
- {"B.categ", "Mobile", "200 x 300"},
- {"A.categ", "Desktop", "200 x 300"},
+ aRecentPreset("B.categ", "Mobile", "400 x 400"),
+ aRecentPreset("B.categ", "Mobile", "200 x 300"),
+ aRecentPreset("A.categ", "Desktop", "640 x 480"),
});
// Then
ASSERT_THAT(data.presets()[0],
- ElementsAre(PresetIs("Z.categ", "Z.desktop"),
- PresetIs("B.categ", "Mobile"),
- PresetIs("A.categ", "Desktop")));
+ ElementsAre(PresetIs("B.categ", "Mobile", "400 x 400"),
+ PresetIs("B.categ", "Mobile", "200 x 300"),
+ PresetIs("A.categ", "Desktop", "640 x 480")));
}
-TEST(QdsPresetModel, recentsOfSameWizardProjectButDifferentSizesAreRecognizedAsDifferentPresets)
+TEST(QdsPresetModel, allowRecentsWithTheSameName)
{
// Given
PresetData data;
@@ -437,21 +429,23 @@ TEST(QdsPresetModel, recentsOfSameWizardProjectButDifferentSizesAreRecognizedAsD
/*wizard presets*/
{
aCategory("A.categ", "A", {"Desktop"}),
- aCategory("B.categ", "B", {"Mobile"}),
},
{/*user presets*/},
/*recents*/
{
- {"B.categ", "Mobile", "400 x 400"},
- {"B.categ", "Mobile", "200 x 300"},
- {"A.categ", "Desktop", "640 x 480"},
+ /* NOTE: it is assumed recents with the same name and size have other fields that
+ * distinguishes them from one another. It is the responsibility of the caller, who
+ * calls data.setData() to make sure that the recents do not contain duplicates. */
+ aRecentPreset("A.categ", "Desktop", "200 x 300"),
+ aRecentPreset("A.categ", "Desktop", "200 x 300"),
+ aRecentPreset("A.categ", "Desktop", "200 x 300"),
});
// Then
ASSERT_THAT(data.presets()[0],
- ElementsAre(PresetIs("B.categ", "Mobile", "400 x 400"),
- PresetIs("B.categ", "Mobile", "200 x 300"),
- PresetIs("A.categ", "Desktop", "640 x 480")));
+ ElementsAre(PresetIs("A.categ", "Desktop"),
+ PresetIs("A.categ", "Desktop"),
+ PresetIs("A.categ", "Desktop")));
}
TEST(QdsPresetModel, outdatedRecentsAreNotShown)
@@ -469,8 +463,8 @@ TEST(QdsPresetModel, outdatedRecentsAreNotShown)
{/*user presets*/},
/*recents*/
{
- {"B.categ", "NoLongerExists", "400 x 400"},
- {"A.categ", "Desktop", "640 x 480"},
+ aRecentPreset("B.categ", "NoLongerExists", "400 x 400"),
+ aRecentPreset("A.categ", "Desktop", "640 x 480"),
});
// Then
diff --git a/tests/auto/qml/qmldesigner/wizard/recentpresets-test.cpp b/tests/auto/qml/qmldesigner/wizard/recentpresets-test.cpp
deleted file mode 100644
index 745462f5c1..0000000000
--- a/tests/auto/qml/qmldesigner/wizard/recentpresets-test.cpp
+++ /dev/null
@@ -1,303 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2022 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of Qt Creator.
-**
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-****************************************************************************/
-
-#include "test-utilities.h"
-
-#include <QDir>
-#include <QRandomGenerator>
-#include <QTime>
-
-#include "recentpresets.h"
-#include "utils/filepath.h"
-#include "utils/temporarydirectory.h"
-
-using namespace StudioWelcome;
-
-constexpr char GROUP_NAME[] = "RecentPresets";
-constexpr char ITEMS[] = "Wizards";
-
-namespace StudioWelcome {
-void PrintTo(const RecentPresetData &recent, std::ostream *os)
-{
- *os << "{categId: " << recent.category << ", name: " << recent.presetName
- << ", size: " << recent.sizeName << ", isUser: " << recent.isUserPreset;
-
- *os << "}";
-}
-} // namespace StudioWelcome
-
-class QdsRecentPresets : public ::testing::Test
-{
-protected:
- RecentPresetsStore aStoreWithRecents(const QStringList &items)
- {
- settings.beginGroup(GROUP_NAME);
- settings.setValue(ITEMS, items);
- settings.endGroup();
-
- return RecentPresetsStore{&settings};
- }
-
- RecentPresetsStore aStoreWithOne(const QVariant &item)
- {
- settings.beginGroup(GROUP_NAME);
- settings.setValue(ITEMS, item);
- settings.endGroup();
-
- return RecentPresetsStore{&settings};
- }
-
-protected:
- Utils::TemporaryDirectory tempDir{"recentpresets-XXXXXX"};
- QSettings settings{tempDir.filePath("test").toString(), QSettings::IniFormat};
-
-private:
- QString settingsPath;
-};
-
-/******************* TESTS *******************/
-
-TEST_F(QdsRecentPresets, readFromEmptyStore)
-{
- RecentPresetsStore store{&settings};
-
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents, IsEmpty());
-}
-
-TEST_F(QdsRecentPresets, readEmptyRecentPresets)
-{
- RecentPresetsStore store = aStoreWithOne("");
-
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents, IsEmpty());
-}
-
-TEST_F(QdsRecentPresets, readOneRecentPresetAsList)
-{
- RecentPresetsStore store = aStoreWithRecents({"category/preset:640 x 480"});
-
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents, ElementsAre(RecentPresetData("category", "preset", "640 x 480")));
-}
-
-TEST_F(QdsRecentPresets, readOneRecentPresetAsString)
-{
- RecentPresetsStore store = aStoreWithOne("category/preset:200 x 300");
-
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents, ElementsAre(RecentPresetData("category", "preset", "200 x 300")));
-}
-
-TEST_F(QdsRecentPresets, readOneRecentUserPresetAsString)
-{
- RecentPresetsStore store = aStoreWithOne("category/[U]preset:200 x 300");
-
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents, ElementsAre(RecentPresetData("category", "preset", "200 x 300", true)));
-}
-
-TEST_F(QdsRecentPresets, readBadRecentPresetAsString)
-{
- RecentPresetsStore store = aStoreWithOne("no_category_only_preset");
-
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents, IsEmpty());
-}
-
-TEST_F(QdsRecentPresets, readBadRecentPresetAsInt)
-{
- RecentPresetsStore store = aStoreWithOne(32);
-
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents, IsEmpty());
-}
-
-TEST_F(QdsRecentPresets, readBadRecentPresetsInList)
-{
- RecentPresetsStore store = aStoreWithRecents({
- "bad1", // no category, no size
- "categ/name:800 x 600", // good
- "categ/bad2", //no size
- "categ/bad3:", //no size
- "categ 1/bad4:200 x 300", // category has space
- "categ/bad5: 400 x 300", // size starts with space
- "categ/bad6:400", // bad size
- "categ/[U]user:300 x 200", // good
- "categ/[u]user2:300 x 200", // small cap "U"
- "categ/[x]user3:300 x 200", // must be letter "U"
- "categ/[U] user4:300 x 200", // space
- });
-
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents,
- ElementsAre(RecentPresetData("categ", "name", "800 x 600", false),
- RecentPresetData("categ", "user", "300 x 200", true)));
-}
-
-TEST_F(QdsRecentPresets, readTwoRecentPresets)
-{
- RecentPresetsStore store = aStoreWithRecents(
- {"category_1/preset 1:640 x 480", "category_2/preset 2:320 x 200"});
-
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents,
- ElementsAre(RecentPresetData("category_1", "preset 1", "640 x 480"),
- RecentPresetData("category_2", "preset 2", "320 x 200")));
-}
-
-TEST_F(QdsRecentPresets, readRecentsToDifferentKindsOfPresets)
-{
- RecentPresetsStore store = aStoreWithRecents(
- {"category_1/preset 1:640 x 480", "category_2/[U]preset 2:320 x 200"});
-
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents,
- ElementsAre(RecentPresetData("category_1", "preset 1", "640 x 480", false),
- RecentPresetData("category_2", "preset 2", "320 x 200", true)));
-}
-
-TEST_F(QdsRecentPresets, addFirstRecentPreset)
-{
- RecentPresetsStore store{&settings};
-
- store.add("A.Category", "Normal Application", "400 x 600");
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents, ElementsAre(RecentPresetData("A.Category", "Normal Application", "400 x 600")));
-}
-
-TEST_F(QdsRecentPresets, addFirstRecentUserPreset)
-{
- RecentPresetsStore store{&settings};
-
- store.add("A.Category", "Normal Application", "400 x 600", /*user preset*/ true);
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents,
- ElementsAre(RecentPresetData("A.Category", "Normal Application", "400 x 600", true)));
-}
-
-TEST_F(QdsRecentPresets, addExistingFirstRecentPreset)
-{
- RecentPresetsStore store = aStoreWithRecents({"category/preset:200 x 300"});
- ASSERT_THAT(store.fetchAll(), ElementsAre(RecentPresetData("category", "preset", "200 x 300")));
-
- store.add("category", "preset", "200 x 300");
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents, ElementsAre(RecentPresetData("category", "preset", "200 x 300")));
-}
-
-TEST_F(QdsRecentPresets, addRecentUserPresetWithSameNameAsExistingRecentNormalPreset)
-{
- RecentPresetsStore store = aStoreWithRecents({"category/preset:200 x 300"});
- ASSERT_THAT(store.fetchAll(), ElementsAre(RecentPresetData("category", "preset", "200 x 300")));
-
- store.add("category", "preset", "200 x 300", /*user preset*/ true);
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents,
- ElementsAre(RecentPresetData("category", "preset", "200 x 300", true),
- RecentPresetData("category", "preset", "200 x 300", false)));
-}
-
-TEST_F(QdsRecentPresets, addSecondRecentPreset)
-{
- RecentPresetsStore store = aStoreWithRecents({"A.Category/Preset 1:800 x 600"});
-
- store.add("A.Category", "Preset 2", "640 x 480");
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents,
- ElementsAre(RecentPresetData("A.Category", "Preset 2", "640 x 480"),
- RecentPresetData("A.Category", "Preset 1", "800 x 600")));
-}
-
-TEST_F(QdsRecentPresets, addSecondRecentPresetSameKindButDifferentSize)
-{
- RecentPresetsStore store = aStoreWithRecents({"A.Category/Preset:800 x 600"});
-
- store.add("A.Category", "Preset", "640 x 480");
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents,
- ElementsAre(RecentPresetData("A.Category", "Preset", "640 x 480"),
- RecentPresetData("A.Category", "Preset", "800 x 600")));
-}
-
-TEST_F(QdsRecentPresets, fetchesRecentPresetsInTheReverseOrderTheyWereAdded)
-{
- RecentPresetsStore store{&settings};
-
- store.add("A.Category", "Preset 1", "640 x 480");
- store.add("A.Category", "Preset 2", "640 x 480");
- store.add("A.Category", "Preset 3", "800 x 600");
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents,
- ElementsAre(RecentPresetData("A.Category", "Preset 3", "800 x 600"),
- RecentPresetData("A.Category", "Preset 2", "640 x 480"),
- RecentPresetData("A.Category", "Preset 1", "640 x 480")));
-}
-
-TEST_F(QdsRecentPresets, addingAnExistingRecentPresetMakesItTheFirst)
-{
- RecentPresetsStore store = aStoreWithRecents({"A.Category/Preset 1:200 x 300",
- "A.Category/Preset 2:200 x 300",
- "A.Category/Preset 3:640 x 480"});
-
- store.add("A.Category", "Preset 3", "640 x 480");
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents,
- ElementsAre(RecentPresetData("A.Category", "Preset 3", "640 x 480"),
- RecentPresetData("A.Category", "Preset 1", "200 x 300"),
- RecentPresetData("A.Category", "Preset 2", "200 x 300")));
-}
-
-TEST_F(QdsRecentPresets, addingTooManyRecentPresetsRemovesTheOldestOne)
-{
- RecentPresetsStore store = aStoreWithRecents(
- {"A.Category/Preset 2:200 x 300", "A.Category/Preset 1:200 x 300"});
- store.setMaximum(2);
-
- store.add("A.Category", "Preset 3", "200 x 300");
- std::vector<RecentPresetData> recents = store.fetchAll();
-
- ASSERT_THAT(recents,
- ElementsAre(RecentPresetData("A.Category", "Preset 3", "200 x 300"),
- RecentPresetData("A.Category", "Preset 2", "200 x 300")));
-}
diff --git a/tests/auto/qml/qmldesigner/wizard/userpresets-test.cpp b/tests/auto/qml/qmldesigner/wizard/userpresets-test.cpp
index 8fa73402c0..6ed562621b 100644
--- a/tests/auto/qml/qmldesigner/wizard/userpresets-test.cpp
+++ b/tests/auto/qml/qmldesigner/wizard/userpresets-test.cpp
@@ -29,6 +29,10 @@
#include <utils/filepath.h>
#include <utils/temporarydirectory.h>
+#include <QJsonArray>
+#include <QJsonObject>
+#include <QJsonDocument>
+
namespace StudioWelcome {
void PrintTo(const UserPresetData &preset, std::ostream *os)
@@ -64,69 +68,85 @@ using namespace StudioWelcome;
constexpr char ARRAY_NAME[] = "UserPresets";
+class FakeStoreIo : public StoreIo
+{
+public:
+ QByteArray read() const override
+ {
+ return data.toUtf8();
+ }
+
+ void write(const QByteArray &bytes) override
+ {
+ data = bytes;
+ }
+
+ QString data;
+};
+
class QdsUserPresets : public ::testing::Test
{
protected:
void SetUp()
{
- settings = std::make_unique<QSettings>(tempDir.filePath("test").toString(),
- QSettings::IniFormat);
+ storeIo = std::make_unique<FakeStoreIo>();
}
- UserPresetsStore anEmptyStore() { return UserPresetsStore{std::move(settings)}; }
+ UserPresetsStore anEmptyStore()
+ {
+ return UserPresetsStore{std::move(storeIo), StorePolicy::UniqueNames};
+ }
UserPresetsStore aStoreWithZeroItems()
{
- settings->beginWriteArray(ARRAY_NAME, 0);
- settings->endArray();
+ storeIo->data = "[]";
- return UserPresetsStore{std::move(settings)};
+ return UserPresetsStore{std::move(storeIo), StorePolicy::UniqueNames};
}
- UserPresetsStore aStoreWithOne(const UserPresetData &preset)
+ UserPresetsStore aStoreWithOne(const UserPresetData &preset,
+ StorePolicy policy = StorePolicy::UniqueNames)
{
- settings->beginWriteArray(ARRAY_NAME, 1);
- settings->setArrayIndex(0);
-
- settings->setValue("categoryId", preset.categoryId);
- settings->setValue("wizardName", preset.wizardName);
- settings->setValue("name", preset.name);
- settings->setValue("screenSize", preset.screenSize);
- settings->setValue("useQtVirtualKeyboard", preset.useQtVirtualKeyboard);
- settings->setValue("qtVersion", preset.qtVersion);
- settings->setValue("styleName", preset.styleName);
-
- settings->endArray();
-
- return UserPresetsStore{std::move(settings)};
+ QJsonArray array({QJsonObject{{"categoryId", preset.categoryId},
+ {"wizardName", preset.wizardName},
+ {"name", preset.name},
+ {"screenSize", preset.screenSize},
+ {"useQtVirtualKeyboard", preset.useQtVirtualKeyboard},
+ {"qtVersion", preset.qtVersion},
+ {"styleName", preset.styleName}}});
+ QJsonDocument doc{array};
+ storeIo->data = doc.toJson();
+
+ return UserPresetsStore{std::move(storeIo), policy};
}
- UserPresetsStore aStoreWithPresets(const std::vector<UserPresetData> &presets)
+ UserPresetsStore aStoreWithPresets(const std::vector<UserPresetData> &presetItems)
{
- settings->beginWriteArray(ARRAY_NAME, presets.size());
-
- for (size_t i = 0; i < presets.size(); ++i) {
- settings->setArrayIndex(i);
- const auto &preset = presets[i];
-
- settings->setValue("categoryId", preset.categoryId);
- settings->setValue("wizardName", preset.wizardName);
- settings->setValue("name", preset.name);
- settings->setValue("screenSize", preset.screenSize);
- settings->setValue("useQtVirtualKeyboard", preset.useQtVirtualKeyboard);
- settings->setValue("qtVersion", preset.qtVersion);
- settings->setValue("styleName", preset.styleName);
+ QJsonArray array;
+
+ for (const auto &preset : presetItems) {
+ QJsonObject obj({{"categoryId", preset.categoryId},
+ {"wizardName", preset.wizardName},
+ {"name", preset.name},
+ {"screenSize", preset.screenSize},
+ {"useQtVirtualKeyboard", preset.useQtVirtualKeyboard},
+ {"qtVersion", preset.qtVersion},
+ {"styleName", preset.styleName}});
+
+ array.append(QJsonValue{obj});
}
- settings->endArray();
- return UserPresetsStore{std::move(settings)};
+ QJsonDocument doc{array};
+ storeIo->data = doc.toJson();
+
+ return UserPresetsStore{std::move(storeIo), StorePolicy::UniqueNames};
}
Utils::TemporaryDirectory tempDir{"userpresets-XXXXXX"};
- std::unique_ptr<QSettings> settings;
+ std::unique_ptr<FakeStoreIo> storeIo;
private:
- QString settingsPath;
+ QString storeIoPath;
};
/******************* TESTS *******************/
@@ -234,10 +254,10 @@ TEST_F(QdsUserPresets, saveIncompletePreset)
ASSERT_THAT(presets, ElementsAre(preset));
}
-TEST_F(QdsUserPresets, cannotSavePresetWithSameName)
+TEST_F(QdsUserPresets, cannotSavePresetWithSameNameForUniqueNamesPolicy)
{
UserPresetData existing{"B.categ", "3D App", "Same Name", "400 x 20", true, "Qt 5", "Material Dark"};
- auto store = aStoreWithOne(existing);
+ auto store = aStoreWithOne(existing, StorePolicy::UniqueNames);
UserPresetData newPreset{"C.categ", "Empty", "Same Name", "100 x 30", false, "Qt 6", "Fusion"};
bool saved = store.save(newPreset);
@@ -246,6 +266,30 @@ TEST_F(QdsUserPresets, cannotSavePresetWithSameName)
ASSERT_THAT(store.fetchAll(), ElementsAreArray({existing}));
}
+TEST_F(QdsUserPresets, canSavePresetWithSameNameForUniqueValuesPolicy)
+{
+ UserPresetData existing{"B.categ", "3D App", "Same Name", "400 x 20", true, "Qt 5", "Material Dark"};
+ auto store = aStoreWithOne(existing, StorePolicy::UniqueValues);
+
+ // NOTE: only Style is different
+ UserPresetData newPreset{"B.categ", "3D App", "Same Name", "400 x 20", true, "Qt 5", "Fusion"};
+ bool saved = store.save(newPreset);
+
+ ASSERT_TRUE(saved);
+ ASSERT_THAT(store.fetchAll(), ElementsAreArray({existing, newPreset}));
+}
+
+TEST_F(QdsUserPresets, cannotSaveExactCopyForUniqueValuesPolicy)
+{
+ UserPresetData existing{"B.categ", "3D App", "Same Name", "400 x 20", true, "Qt 5", "Material Dark"};
+ auto store = aStoreWithOne(existing, StorePolicy::UniqueNames);
+
+ bool saved = store.save(existing);
+
+ ASSERT_FALSE(saved);
+ ASSERT_THAT(store.fetchAll(), ElementsAreArray({existing}));
+}
+
TEST_F(QdsUserPresets, saveNewPreset)
{
UserPresetData existing{"A.categ", "3D App", "iPhone7", "400 x 20", true, "Qt 5", "Material Dark"};
@@ -258,6 +302,54 @@ TEST_F(QdsUserPresets, saveNewPreset)
ASSERT_THAT(presets, ElementsAre(existing, newPreset));
}
+TEST_F(QdsUserPresets, canLimitPresetsToAMaximum)
+{
+ std::vector<UserPresetData> existing{
+ {"A.categ", "AppA", "iPhone7", "400 x 20", true, "Qt 5", "Material Dark"},
+ {"B.categ", "AppB", "iPhone7", "400 x 20", true, "Qt 5", "Material Dark"},
+ {"C.categ", "AppC", "iPhone7", "400 x 20", true, "Qt 5", "Material Dark"},
+ };
+ auto store = aStoreWithPresets(existing);
+ store.setMaximum(3);
+
+ UserPresetData newPreset{"D.categ", "AppD", "Huawei", "100 x 30", true, "Qt 6", "Fusion"};
+ store.save(newPreset);
+
+ auto presets = store.fetchAll();
+ ASSERT_THAT(presets, ElementsAre(existing[1], existing[2], newPreset));
+}
+
+TEST_F(QdsUserPresets, canLimitPresetsToAMaximumForReverseOrder)
+{
+ std::vector<UserPresetData> existing{
+ {"A.categ", "AppA", "iPhone7", "400 x 20", true, "Qt 5", "Material Dark"},
+ {"B.categ", "AppB", "iPhone7", "400 x 20", true, "Qt 5", "Material Dark"},
+ {"C.categ", "AppC", "iPhone7", "400 x 20", true, "Qt 5", "Material Dark"},
+ };
+ auto store = aStoreWithPresets(existing);
+ store.setMaximum(3);
+ store.setReverseOrder();
+
+ UserPresetData newPreset{"D.categ", "AppD", "Huawei", "100 x 30", true, "Qt 6", "Fusion"};
+ store.save(newPreset);
+
+ auto presets = store.fetchAll();
+ ASSERT_THAT(presets, ElementsAre(newPreset, existing[0], existing[1]));
+}
+
+TEST_F(QdsUserPresets, canSavePresetsInReverseOrder)
+{
+ UserPresetData existing{"A.categ", "3D App", "iPhone7", "400 x 20", true, "Qt 5", "Material Dark"};
+ auto store = aStoreWithOne(existing, StorePolicy::UniqueNames);
+ store.setReverseOrder();
+
+ UserPresetData newPreset{"A.categ", "Empty", "Huawei", "100 x 30", true, "Qt 6", "Fusion"};
+ store.save(newPreset);
+
+ auto presets = store.fetchAll();
+ ASSERT_THAT(presets, ElementsAre(newPreset, existing));
+}
+
TEST_F(QdsUserPresets, removeUserPresetFromEmptyStore)
{
UserPresetData preset{"C.categ", "2D App", "Android", "", false, "", ""};