diff options
author | Samuel Ghinet <samuel.ghinet@qt.io> | 2022-02-23 15:32:34 +0200 |
---|---|---|
committer | Samuel Ghinet <samuel.ghinet@qt.io> | 2022-03-08 09:39:43 +0000 |
commit | 618eda3572a2df97d28ffdf603daa35b509fbcf1 (patch) | |
tree | f86be7777e702f7a13d9588c753ece1adb241b1a /tests | |
parent | ec02c157eec9aeece5cdbb6b5ab112d0eae4c4e2 (diff) | |
download | qt-creator-618eda3572a2df97d28ffdf603daa35b509fbcf1.tar.gz |
Implement custom presets
Task-number: QDS-4989
Change-Id: I95844ae97204ad3bb94905c89f8e16b79eed8f64
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
Diffstat (limited to 'tests')
5 files changed, 601 insertions, 56 deletions
diff --git a/tests/auto/qml/qmldesigner/wizard/CMakeLists.txt b/tests/auto/qml/qmldesigner/wizard/CMakeLists.txt index 0606be1a55..c5040112ef 100644 --- a/tests/auto/qml/qmldesigner/wizard/CMakeLists.txt +++ b/tests/auto/qml/qmldesigner/wizard/CMakeLists.txt @@ -18,12 +18,14 @@ add_qtc_test(tst_qml_wizard 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 dc855b67a3..f2233eee85 100644 --- a/tests/auto/qml/qmldesigner/wizard/presetmodel-test.cpp +++ b/tests/auto/qml/qmldesigner/wizard/presetmodel-test.cpp @@ -33,10 +33,30 @@ using ::testing::ElementsAreArray; using ::testing::PrintToString; namespace StudioWelcome { + +void PrintTo(const UserPresetItem &item, std::ostream *os); + void PrintTo(const PresetItem &item, std::ostream *os) { + if (typeid(item) == typeid(UserPresetItem)) { + PrintTo((UserPresetItem &) item, os); + return; + } + + *os << "{categId: " << item.categoryId << ", " + << "name: " << item.wizardName; + + if (!item.screenSizeName.isEmpty()) + *os << ", size: " << item.screenSizeName; + + *os << "}"; +} + +void PrintTo(const UserPresetItem &item, std::ostream *os) +{ *os << "{categId: " << item.categoryId << ", " - << "name: " << item.name; + << "name: " << item.wizardName << ", " + << "user name: " << item.userName; if (!item.screenSizeName.isEmpty()) *os << ", size: " << item.screenSizeName; @@ -44,6 +64,14 @@ void PrintTo(const PresetItem &item, std::ostream *os) *os << "}"; } +void PrintTo(const std::shared_ptr<PresetItem> &p, std::ostream *os) +{ + if (p) + PrintTo(*p, os); + else + *os << "{null}"; +} + } // namespace StudioWelcome namespace { @@ -51,20 +79,47 @@ std::pair<QString, WizardCategory> aCategory(const QString &categId, const QString &categName, const std::vector<QString> &names) { - std::vector<PresetItem> items = Utils::transform(names, [&categId](const QString &name) { - return PresetItem{name, categId}; - }); + std::vector<std::shared_ptr<PresetItem>> items + = Utils::transform(names, [&categId](const QString &name) { + std::shared_ptr<PresetItem> item{new PresetItem}; + item->wizardName = name; + item->categoryId = categId; + + return item; + }); return std::make_pair(categId, WizardCategory{categId, categName, items}); } +UserPresetData aUserPreset(const QString &categId, const QString &wizardName, const QString &userName) +{ + UserPresetData preset; + preset.categoryId = categId; + preset.wizardName = wizardName; + preset.name = userName; + + return preset; +} + MATCHER_P2(PresetIs, category, name, PrintToString(PresetItem{name, category})) { - return arg.categoryId == category && arg.name == name; + return arg->categoryId == category && arg->wizardName == name; +} + +MATCHER_P3(UserPresetIs, + category, + wizardName, + userName, + PrintToString(UserPresetItem{wizardName, userName, category})) +{ + auto userPreset = dynamic_cast<UserPresetItem *>(arg.get()); + + return userPreset->categoryId == category && userPreset->wizardName == wizardName + && userPreset->userName == userName; } MATCHER_P3(PresetIs, category, name, size, PrintToString(PresetItem{name, category, size})) { - return arg.categoryId == category && arg.name == name && size == arg.screenSizeName; + return arg->categoryId == category && arg->wizardName == name && size == arg->screenSizeName; } } // namespace @@ -88,6 +143,7 @@ TEST(QdsPresetModel, haveSameArraySizeForPresetsAndCategories) aCategory("A.categ", "A", {"item a", "item b"}), aCategory("B.categ", "B", {"item c", "item d"}), }, + {/*user presets*/}, {/*recents*/}); ASSERT_THAT(data.presets(), SizeIs(2)); @@ -105,6 +161,7 @@ TEST(QdsPresetModel, haveWizardPresetsNoRecents) aCategory("A.categ", "A", {"item a", "item b"}), aCategory("B.categ", "B", {"item c", "item d"}), }, + {/*user presets*/}, {/*recents*/}); // Then @@ -115,11 +172,30 @@ TEST(QdsPresetModel, haveWizardPresetsNoRecents) ElementsAre(PresetIs("B.categ", "item c"), PresetIs("B.categ", "item d"))); } +TEST(QdsPresetModel, whenHaveUserPresetsButNoWizardPresetsReturnEmpty) +{ + // Given + PresetData data; + + // When + data.setData({/*builtin presets*/}, + { + aUserPreset("A.Mobile", "Scroll", "iPhone5"), + aUserPreset("B.Desktop", "Launcher", "MacBook"), + }, + {/*recents*/}); + + // Then + ASSERT_THAT(data.categories(), IsEmpty()); + ASSERT_THAT(data.presets(), IsEmpty()); +} + TEST(QdsPresetModel, haveRecentsNoWizardPresets) { PresetData data; data.setData({/*wizardPresets*/}, + {/*user presets*/}, { {"A.categ", "Desktop", "640 x 480"}, {"B.categ", "Mobile", "800 x 600"}, @@ -129,7 +205,7 @@ TEST(QdsPresetModel, haveRecentsNoWizardPresets) ASSERT_THAT(data.presets(), IsEmpty()); } -TEST(QdsPresetModel, recentsAddedBeforeWizardPresets) +TEST(QdsPresetModel, recentsAddedWithWizardPresets) { // Given PresetData data; @@ -141,6 +217,7 @@ TEST(QdsPresetModel, recentsAddedBeforeWizardPresets) aCategory("A.categ", "A", {"Desktop", "item b"}), aCategory("B.categ", "B", {"item c", "Mobile"}), }, + {/*user presets*/}, /*recents*/ { {"A.categ", "Desktop", "800 x 600"}, @@ -158,7 +235,98 @@ TEST(QdsPresetModel, recentsAddedBeforeWizardPresets) ElementsAre(PresetIs("B.categ", "item c"), PresetIs("B.categ", "Mobile"))})); } -TEST(QdsPresetModel, recentsShouldNotSorted) +TEST(QdsPresetModel, userPresetsAddedWithWizardPresets) +{ + // Given + PresetData data; + + // When + data.setData( + /*wizard presets*/ + { + aCategory("A.categ", "A", {"Desktop", "item b"}), + aCategory("B.categ", "B", {"Mobile"}), + }, + { + aUserPreset("A.categ", "Desktop", "Windows10"), + }, + {/*recents*/}); + + // Then + ASSERT_THAT(data.categories(), ElementsAre("A", "B", "Custom")); + ASSERT_THAT(data.presets(), + ElementsAre(ElementsAre(PresetIs("A.categ", "Desktop"), + PresetIs("A.categ", "item b")), + ElementsAre(PresetIs("B.categ", "Mobile")), + ElementsAre(UserPresetIs("A.categ", "Desktop", "Windows10")))); +} + +TEST(QdsPresetModel, doesNotAddUserPresetsOfNonExistingCategory) +{ + // Given + PresetData data; + + // When + data.setData( + /*wizard presets*/ + { + aCategory("A.categ", "A", {"Desktop"}), // Only category "A.categ" exists + }, + { + aUserPreset("Bad.Categ", "Desktop", "Windows8"), // Bad.Categ does not exist + }, + {/*recents*/}); + + // Then + ASSERT_THAT(data.categories(), ElementsAre("A")); + ASSERT_THAT(data.presets(), ElementsAre(ElementsAre(PresetIs("A.categ", "Desktop")))); +} + +TEST(QdsPresetModel, doesNotAddUserPresetIfWizardPresetItRefersToDoesNotExist) +{ + // Given + PresetData data; + + // When + data.setData( + /*wizard presets*/ + { + aCategory("A.categ", "A", {"Desktop"}), + }, + { + aUserPreset("B.categ", "BadWizard", "Tablet"), // BadWizard referenced does not exist + }, + {/*recents*/}); + + // Then + ASSERT_THAT(data.categories(), ElementsAre("A")); + ASSERT_THAT(data.presets(), ElementsAre(ElementsAre(PresetIs("A.categ", "Desktop")))); +} + +TEST(QdsPresetModel, userPresetWithSameNameAsWizardPreset) +{ + // Given + PresetData data; + + // When + data.setData( + /*wizard presets*/ + { + aCategory("A.categ", "A", {"Desktop"}), + }, + { + aUserPreset("A.categ", "Desktop", "Desktop"), + }, + {/*recents*/}); + + // Then + ASSERT_THAT(data.categories(), ElementsAre("A", "Custom")); + ASSERT_THAT(data.presets(), + ElementsAre(ElementsAre(PresetIs("A.categ", "Desktop")), + ElementsAre(UserPresetIs("A.categ", "Desktop", "Desktop")))); +} + +TEST(QdsPresetModel, recentsShouldNotBeSorted) { // Given PresetData data; @@ -171,6 +339,7 @@ TEST(QdsPresetModel, recentsShouldNotSorted) aCategory("B.categ", "B", {"item c", "Mobile"}), aCategory("Z.categ", "Z", {"Z.desktop"}), }, + {/*user presets*/}, /*recents*/ { {"Z.categ", "Z.desktop", "200 x 300"}, @@ -197,6 +366,7 @@ TEST(QdsPresetModel, recentsOfSameWizardProjectButDifferentSizesAreRecognizedAsD aCategory("A.categ", "A", {"Desktop"}), aCategory("B.categ", "B", {"Mobile"}), }, + {/*user presets*/}, /*recents*/ { {"B.categ", "Mobile", "400 x 400"}, @@ -223,6 +393,7 @@ TEST(QdsPresetModel, outdatedRecentsAreNotShown) aCategory("A.categ", "A", {"Desktop"}), aCategory("B.categ", "B", {"Mobile"}), }, + {/*user presets*/}, /*recents*/ { {"B.categ", "NoLongerExists", "400 x 400"}, diff --git a/tests/auto/qml/qmldesigner/wizard/recentpresets-test.cpp b/tests/auto/qml/qmldesigner/wizard/recentpresets-test.cpp index b1bb0e0626..745462f5c1 100644 --- a/tests/auto/qml/qmldesigner/wizard/recentpresets-test.cpp +++ b/tests/auto/qml/qmldesigner/wizard/recentpresets-test.cpp @@ -38,6 +38,16 @@ 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: @@ -73,7 +83,7 @@ TEST_F(QdsRecentPresets, readFromEmptyStore) { RecentPresetsStore store{&settings}; - std::vector<RecentPreset> recents = store.fetchAll(); + std::vector<RecentPresetData> recents = store.fetchAll(); ASSERT_THAT(recents, IsEmpty()); } @@ -82,7 +92,7 @@ TEST_F(QdsRecentPresets, readEmptyRecentPresets) { RecentPresetsStore store = aStoreWithOne(""); - std::vector<RecentPreset> recents = store.fetchAll(); + std::vector<RecentPresetData> recents = store.fetchAll(); ASSERT_THAT(recents, IsEmpty()); } @@ -91,25 +101,34 @@ TEST_F(QdsRecentPresets, readOneRecentPresetAsList) { RecentPresetsStore store = aStoreWithRecents({"category/preset:640 x 480"}); - std::vector<RecentPreset> recents = store.fetchAll(); + std::vector<RecentPresetData> recents = store.fetchAll(); - ASSERT_THAT(recents, ElementsAre(RecentPreset("category", "preset", "640 x 480"))); + ASSERT_THAT(recents, ElementsAre(RecentPresetData("category", "preset", "640 x 480"))); } TEST_F(QdsRecentPresets, readOneRecentPresetAsString) { RecentPresetsStore store = aStoreWithOne("category/preset:200 x 300"); - std::vector<RecentPreset> recents = store.fetchAll(); + 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(RecentPreset("category", "preset", "200 x 300"))); + ASSERT_THAT(recents, ElementsAre(RecentPresetData("category", "preset", "200 x 300", true))); } TEST_F(QdsRecentPresets, readBadRecentPresetAsString) { RecentPresetsStore store = aStoreWithOne("no_category_only_preset"); - std::vector<RecentPreset> recents = store.fetchAll(); + std::vector<RecentPresetData> recents = store.fetchAll(); ASSERT_THAT(recents, IsEmpty()); } @@ -118,24 +137,32 @@ TEST_F(QdsRecentPresets, readBadRecentPresetAsInt) { RecentPresetsStore store = aStoreWithOne(32); - std::vector<RecentPreset> recents = store.fetchAll(); + 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 - - std::vector<RecentPreset> recents = store.fetchAll(); + 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(RecentPreset("categ", "name", "800 x 600"))); + ASSERT_THAT(recents, + ElementsAre(RecentPresetData("categ", "name", "800 x 600", false), + RecentPresetData("categ", "user", "300 x 200", true))); } TEST_F(QdsRecentPresets, readTwoRecentPresets) @@ -143,11 +170,23 @@ TEST_F(QdsRecentPresets, readTwoRecentPresets) RecentPresetsStore store = aStoreWithRecents( {"category_1/preset 1:640 x 480", "category_2/preset 2:320 x 200"}); - std::vector<RecentPreset> recents = store.fetchAll(); + std::vector<RecentPresetData> recents = store.fetchAll(); ASSERT_THAT(recents, - ElementsAre(RecentPreset("category_1", "preset 1", "640 x 480"), - RecentPreset("category_2", "preset 2", "320 x 200"))); + 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) @@ -155,19 +194,44 @@ TEST_F(QdsRecentPresets, addFirstRecentPreset) RecentPresetsStore store{&settings}; store.add("A.Category", "Normal Application", "400 x 600"); - std::vector<RecentPreset> recents = store.fetchAll(); + std::vector<RecentPresetData> recents = store.fetchAll(); - ASSERT_THAT(recents, ElementsAre(RecentPreset("A.Category", "Normal Application", "400 x 600"))); + 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"}); + 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<RecentPreset> recents = store.fetchAll(); + std::vector<RecentPresetData> recents = store.fetchAll(); + + ASSERT_THAT(recents, ElementsAre(RecentPresetData("category", "preset", "200 x 300"))); +} - ASSERT_THAT(recents, ElementsAre(RecentPreset("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) @@ -175,11 +239,11 @@ 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<RecentPreset> recents = store.fetchAll(); + std::vector<RecentPresetData> recents = store.fetchAll(); ASSERT_THAT(recents, - ElementsAre(RecentPreset("A.Category", "Preset 2", "640 x 480"), - RecentPreset("A.Category", "Preset 1", "800 x 600"))); + ElementsAre(RecentPresetData("A.Category", "Preset 2", "640 x 480"), + RecentPresetData("A.Category", "Preset 1", "800 x 600"))); } TEST_F(QdsRecentPresets, addSecondRecentPresetSameKindButDifferentSize) @@ -187,11 +251,11 @@ TEST_F(QdsRecentPresets, addSecondRecentPresetSameKindButDifferentSize) RecentPresetsStore store = aStoreWithRecents({"A.Category/Preset:800 x 600"}); store.add("A.Category", "Preset", "640 x 480"); - std::vector<RecentPreset> recents = store.fetchAll(); + std::vector<RecentPresetData> recents = store.fetchAll(); ASSERT_THAT(recents, - ElementsAre(RecentPreset("A.Category", "Preset", "640 x 480"), - RecentPreset("A.Category", "Preset", "800 x 600"))); + ElementsAre(RecentPresetData("A.Category", "Preset", "640 x 480"), + RecentPresetData("A.Category", "Preset", "800 x 600"))); } TEST_F(QdsRecentPresets, fetchesRecentPresetsInTheReverseOrderTheyWereAdded) @@ -201,12 +265,12 @@ TEST_F(QdsRecentPresets, fetchesRecentPresetsInTheReverseOrderTheyWereAdded) 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<RecentPreset> recents = store.fetchAll(); + std::vector<RecentPresetData> recents = store.fetchAll(); ASSERT_THAT(recents, - ElementsAre(RecentPreset("A.Category", "Preset 3", "800 x 600"), - RecentPreset("A.Category", "Preset 2", "640 x 480"), - RecentPreset("A.Category", "Preset 1", "640 x 480"))); + 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) @@ -216,12 +280,12 @@ TEST_F(QdsRecentPresets, addingAnExistingRecentPresetMakesItTheFirst) "A.Category/Preset 3:640 x 480"}); store.add("A.Category", "Preset 3", "640 x 480"); - std::vector<RecentPreset> recents = store.fetchAll(); + std::vector<RecentPresetData> recents = store.fetchAll(); ASSERT_THAT(recents, - ElementsAre(RecentPreset("A.Category", "Preset 3", "640 x 480"), - RecentPreset("A.Category", "Preset 1", "200 x 300"), - RecentPreset("A.Category", "Preset 2", "200 x 300"))); + 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) @@ -231,9 +295,9 @@ TEST_F(QdsRecentPresets, addingTooManyRecentPresetsRemovesTheOldestOne) store.setMaximum(2); store.add("A.Category", "Preset 3", "200 x 300"); - std::vector<RecentPreset> recents = store.fetchAll(); + std::vector<RecentPresetData> recents = store.fetchAll(); ASSERT_THAT(recents, - ElementsAre(RecentPreset("A.Category", "Preset 3", "200 x 300"), - RecentPreset("A.Category", "Preset 2", "200 x 300"))); + 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 new file mode 100644 index 0000000000..8fa73402c0 --- /dev/null +++ b/tests/auto/qml/qmldesigner/wizard/userpresets-test.cpp @@ -0,0 +1,308 @@ +/**************************************************************************** +** +** 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 "userpresets.h" + +#include <utils/filepath.h> +#include <utils/temporarydirectory.h> + +namespace StudioWelcome { + +void PrintTo(const UserPresetData &preset, std::ostream *os) +{ + *os << "UserPresetData{category = " << preset.categoryId; + *os << "; wizardName = " << preset.wizardName; + *os << "; name = " << preset.name; + *os << "; screenSize = " << preset.screenSize; + *os << "; keyboard = " << preset.useQtVirtualKeyboard; + *os << "; qt = " << preset.qtVersion; + *os << "; style = " << preset.styleName; + *os << "}"; +} + +void PrintTo(const std::vector<UserPresetData> &presets, std::ostream *os) +{ + if (presets.size() == 0) { + *os << "{}" << std::endl; + } else { + *os << "{" << std::endl; + for (size_t i = 0; i < presets.size(); ++i) { + *os << "#" << i << ": "; + PrintTo(presets[i], os); + *os << std::endl; + } + *os << "}" << std::endl; + } +} + +} // namespace StudioWelcome + +using namespace StudioWelcome; + +constexpr char ARRAY_NAME[] = "UserPresets"; + +class QdsUserPresets : public ::testing::Test +{ +protected: + void SetUp() + { + settings = std::make_unique<QSettings>(tempDir.filePath("test").toString(), + QSettings::IniFormat); + } + + UserPresetsStore anEmptyStore() { return UserPresetsStore{std::move(settings)}; } + + UserPresetsStore aStoreWithZeroItems() + { + settings->beginWriteArray(ARRAY_NAME, 0); + settings->endArray(); + + return UserPresetsStore{std::move(settings)}; + } + + UserPresetsStore aStoreWithOne(const UserPresetData &preset) + { + 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)}; + } + + UserPresetsStore aStoreWithPresets(const std::vector<UserPresetData> &presets) + { + 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); + } + settings->endArray(); + + return UserPresetsStore{std::move(settings)}; + } + + Utils::TemporaryDirectory tempDir{"userpresets-XXXXXX"}; + std::unique_ptr<QSettings> settings; + +private: + QString settingsPath; +}; + +/******************* TESTS *******************/ + +TEST_F(QdsUserPresets, readEmptyUserPresets) +{ + auto store = anEmptyStore(); + + auto presets = store.fetchAll(); + + ASSERT_THAT(presets, IsEmpty()); +} + +TEST_F(QdsUserPresets, readEmptyArrayOfUserPresets) +{ + auto store = aStoreWithZeroItems(); + + auto presets = store.fetchAll(); + + ASSERT_THAT(presets, IsEmpty()); +} + +TEST_F(QdsUserPresets, readOneUserPreset) +{ + UserPresetData preset{"A.categ", "3D App", "iPhone7", "400 x 20", true, "Qt 5", "Material Dark"}; + auto store = aStoreWithOne(preset); + + auto presets = store.fetchAll(); + + ASSERT_THAT(presets, ElementsAreArray({preset})); +} + +TEST_F(QdsUserPresets, readOneIncompleteUserPreset) +{ + // A preset entry that has the required entries, but not the others. + UserPresetData preset{"A.categ", "3D App", "iPhone7", "", false, "", ""}; + auto store = aStoreWithOne(preset); + + auto presets = store.fetchAll(); + + ASSERT_THAT(presets, ElementsAreArray({preset})); +} + +TEST_F(QdsUserPresets, doesNotReadPresetsThatLackRequiredEntries) +{ + // Required entries are: Category id, wizard name, preset name. + auto presetsInStore = std::vector<UserPresetData>{ + UserPresetData{"", "3D App", "iPhone7", "400 x 20", true, "Qt 5", "Material Dark"}, + UserPresetData{"A.categ", "", "iPhone7", "400 x 20", true, "Qt 5", "Material Dark"}, + UserPresetData{"A.categ", "3D App", "", "400 x 20", true, "Qt 5", "Material Dark"}, + }; + auto store = aStoreWithPresets(presetsInStore); + + auto presets = store.fetchAll(); + + ASSERT_THAT(presets, IsEmpty()); +} + +TEST_F(QdsUserPresets, readSeveralUserPresets) +{ + auto presetsInStore = std::vector<UserPresetData>{ + UserPresetData{"A.categ", "3D App", "iPhone7", "400 x 20", true, "Qt 5", "Material Dark"}, + UserPresetData{"B.categ", "2D App", "iPhone6", "200 x 50", false, "Qt 6", "Fusion"}, + UserPresetData{"C.categ", "Empty", "Some Other", "60 x 30", true, "Qt 7", "Material Light"}, + }; + auto store = aStoreWithPresets(presetsInStore); + + auto presets = store.fetchAll(); + + ASSERT_THAT(presets, ElementsAreArray(presetsInStore)); +} + +TEST_F(QdsUserPresets, cannotSaveInvalidPreset) +{ + // an invalid preset is a preset that lacks required fields. + UserPresetData invalidPreset{"", "3D App", "iPhone7", "400 x 20", true, "Qt 5", "Material Dark"}; + auto store = anEmptyStore(); + + bool saved = store.save(invalidPreset); + + auto presets = store.fetchAll(); + ASSERT_THAT(presets, IsEmpty()); + ASSERT_FALSE(saved); +} + +TEST_F(QdsUserPresets, savePresetInEmptyStore) +{ + UserPresetData preset{"B.categ", "3D App", "iPhone7", "400 x 20", true, "Qt 5", "Material Dark"}; + auto store = anEmptyStore(); + + store.save(preset); + + auto presets = store.fetchAll(); + ASSERT_THAT(presets, ElementsAre(preset)); +} + +TEST_F(QdsUserPresets, saveIncompletePreset) +{ + UserPresetData preset{"C.categ", "2D App", "Android", "", false, "", ""}; + auto store = anEmptyStore(); + + store.save(preset); + + auto presets = store.fetchAll(); + ASSERT_THAT(presets, ElementsAre(preset)); +} + +TEST_F(QdsUserPresets, cannotSavePresetWithSameName) +{ + UserPresetData existing{"B.categ", "3D App", "Same Name", "400 x 20", true, "Qt 5", "Material Dark"}; + auto store = aStoreWithOne(existing); + + UserPresetData newPreset{"C.categ", "Empty", "Same Name", "100 x 30", false, "Qt 6", "Fusion"}; + bool saved = store.save(newPreset); + + 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"}; + auto store = aStoreWithOne(existing); + + UserPresetData newPreset{"A.categ", "Empty", "Huawei", "100 x 30", true, "Qt 6", "Fusion"}; + store.save(newPreset); + + auto presets = store.fetchAll(); + ASSERT_THAT(presets, ElementsAre(existing, newPreset)); +} + +TEST_F(QdsUserPresets, removeUserPresetFromEmptyStore) +{ + UserPresetData preset{"C.categ", "2D App", "Android", "", false, "", ""}; + auto store = anEmptyStore(); + + store.remove("A.categ", "User preset name"); + + auto presets = store.fetchAll(); + ASSERT_THAT(presets, IsEmpty()); +} + +TEST_F(QdsUserPresets, removeExistingUserPreset) +{ + UserPresetData existing{"A.categ", "3D App", "iPhone7", "400 x 20", true, "Qt 5", "Material Dark"}; + auto store = aStoreWithOne(existing); + + store.remove("A.categ", "iPhone7"); + + auto presets = store.fetchAll(); + ASSERT_THAT(presets, IsEmpty()); +} + +TEST_F(QdsUserPresets, removeNonExistingUserPreset) +{ + UserPresetData existing{"A.categ", "3D App", "iPhone7", "400 x 20", true, "Qt 5", "Material Dark"}; + auto store = aStoreWithOne(existing); + + store.remove("A.categ", "Android"); + + auto presets = store.fetchAll(); + ASSERT_THAT(presets, ElementsAre(existing)); +} + +TEST_F(QdsUserPresets, removeExistingUserPresetInStoreWithManyPresets) +{ + auto presetsInStore = std::vector<UserPresetData>{ + UserPresetData{"A.categ", "3D App", "iPhone7", "400 x 20", true, "Qt 5", "Material Dark"}, + UserPresetData{"B.categ", "2D App", "iPhone6", "200 x 50", false, "Qt 6", "Fusion"}, + UserPresetData{"C.categ", "Empty", "Some Other", "60 x 30", true, "Qt 7", "Material Light"}, + }; + auto store = aStoreWithPresets(presetsInStore); + + store.remove("B.categ", "iPhone6"); + + auto presets = store.fetchAll(); + ASSERT_THAT(presets, ElementsAre(presetsInStore[0], presetsInStore[2])); +} + diff --git a/tests/auto/qml/qmldesigner/wizard/wizardfactories-test.cpp b/tests/auto/qml/qmldesigner/wizard/wizardfactories-test.cpp index 30c79e9090..6541e7a163 100644 --- a/tests/auto/qml/qmldesigner/wizard/wizardfactories-test.cpp +++ b/tests/auto/qml/qmldesigner/wizard/wizardfactories-test.cpp @@ -151,7 +151,7 @@ private: QStringList presetNames(const WizardCategory &cat) { - QStringList result = Utils::transform<QStringList>(cat.items, &PresetItem::name); + QStringList result = Utils::transform<QStringList>(cat.items, &PresetItem::wizardName); return result; } @@ -319,9 +319,9 @@ TEST_F(QdsWizardFactories, createsPresetItemAndCategoryCorrectlyFromWizardFactor ASSERT_EQ("myDisplayCategory", category.name); auto presetItem = presets["myCategoryId"].items[0]; - ASSERT_EQ("myName", presetItem.name); - ASSERT_EQ("myDescription", presetItem.description); - ASSERT_EQ("qrc:/my/qml/path", presetItem.qmlPath.toString()); - ASSERT_EQ("\uABCD", presetItem.fontIconCode); + ASSERT_EQ("myName", presetItem->wizardName); + ASSERT_EQ("myDescription", presetItem->description); + ASSERT_EQ("qrc:/my/qml/path", presetItem->qmlPath.toString()); + ASSERT_EQ("\uABCD", presetItem->fontIconCode); } |