From b30bae0abfe4079f22a4adc7f470bb1727fee8b1 Mon Sep 17 00:00:00 2001 From: Lars Brubaker Date: Tue, 25 Aug 2015 11:56:21 -0700 Subject: [PATCH] Made providers take a function to change the provider (used in searching) Improved tests (made functions to help create a clean run) --- Library/Provider/LibraryProviderFileSystem.cs | 2 +- Library/Provider/LibraryProviderHistory.cs | 2 +- Library/Provider/LibraryProviderPlugin.cs | 4 +- Library/Provider/LibraryProviderQueue.cs | 2 +- Library/Provider/LibraryProviderSelector.cs | 8 +- Library/Provider/LibraryProviderSqlite.cs | 84 ++++++++++++++----- MatterControl.sln | 31 ++++++- .../CheckBoxInLibraryIsClickable.cs | 6 +- .../LibraryProviderSqliteTests.cs | 34 +------- .../MatterControl/MatterControl.UI.cs | 57 ++++++++++++- 10 files changed, 165 insertions(+), 65 deletions(-) diff --git a/Library/Provider/LibraryProviderFileSystem.cs b/Library/Provider/LibraryProviderFileSystem.cs index 4db38937f..d0a12d620 100644 --- a/Library/Provider/LibraryProviderFileSystem.cs +++ b/Library/Provider/LibraryProviderFileSystem.cs @@ -59,7 +59,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider } } - public virtual LibraryProvider CreateLibraryProvider(LibraryProvider parentLibraryProvider) + public virtual LibraryProvider CreateLibraryProvider(LibraryProvider parentLibraryProvider, Action setCurrentLibraryProvider) { return new LibraryProviderFileSystem(rootPath, Description, parentLibraryProvider); } diff --git a/Library/Provider/LibraryProviderHistory.cs b/Library/Provider/LibraryProviderHistory.cs index 5af9ab752..0524cf3a6 100644 --- a/Library/Provider/LibraryProviderHistory.cs +++ b/Library/Provider/LibraryProviderHistory.cs @@ -49,7 +49,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { public class LibraryProviderHistoryCreator : ILibraryCreator { - public virtual LibraryProvider CreateLibraryProvider(LibraryProvider parentLibraryProvider) + public virtual LibraryProvider CreateLibraryProvider(LibraryProvider parentLibraryProvider, Action setCurrentLibraryProvider) { return new LibraryProviderHistory(null, parentLibraryProvider); } diff --git a/Library/Provider/LibraryProviderPlugin.cs b/Library/Provider/LibraryProviderPlugin.cs index f62637650..552194cb7 100644 --- a/Library/Provider/LibraryProviderPlugin.cs +++ b/Library/Provider/LibraryProviderPlugin.cs @@ -39,14 +39,14 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { public interface ILibraryCreator { - LibraryProvider CreateLibraryProvider(LibraryProvider parentLibraryProvider); + LibraryProvider CreateLibraryProvider(LibraryProvider parentLibraryProvider, Action setCurrentLibraryProvider); string ProviderKey { get; } } public class LibraryProviderPlugin : ILibraryCreator { - public virtual LibraryProvider CreateLibraryProvider(LibraryProvider parentLibraryProvider) + public virtual LibraryProvider CreateLibraryProvider(LibraryProvider parentLibraryProvider, Action setCurrentLibraryProvider) { throw new NotImplementedException(); } diff --git a/Library/Provider/LibraryProviderQueue.cs b/Library/Provider/LibraryProviderQueue.cs index 2bd0a0808..45032bedf 100644 --- a/Library/Provider/LibraryProviderQueue.cs +++ b/Library/Provider/LibraryProviderQueue.cs @@ -48,7 +48,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { public class LibraryProviderQueueCreator : ILibraryCreator { - public virtual LibraryProvider CreateLibraryProvider(LibraryProvider parentLibraryProvider) + public virtual LibraryProvider CreateLibraryProvider(LibraryProvider parentLibraryProvider, Action setCurrentLibraryProvider) { return new LibraryProviderQueue(null, parentLibraryProvider); } diff --git a/Library/Provider/LibraryProviderSelector.cs b/Library/Provider/LibraryProviderSelector.cs index eb35a3cff..3df8ffed8 100644 --- a/Library/Provider/LibraryProviderSelector.cs +++ b/Library/Provider/LibraryProviderSelector.cs @@ -160,7 +160,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider // If signing out, we need to force selection to this provider if(e != null && !e.IsAuthenticated) { - // Switch to the purchased library + // Switch to the selector setCurrentLibraryProvider(this); } @@ -233,7 +233,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public override PrintItemCollection GetCollectionItem(int collectionIndex) { - LibraryProvider provider = libraryCreators[collectionIndex].CreateLibraryProvider(this); + LibraryProvider provider = libraryCreators[collectionIndex].CreateLibraryProvider(this, setCurrentLibraryProvider); return new PrintItemCollection(provider.Name, provider.ProviderKey); } @@ -248,7 +248,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { if (collection.Key == libraryCreator.ProviderKey) { - return libraryCreator.CreateLibraryProvider(this); + return libraryCreator.CreateLibraryProvider(this, setCurrentLibraryProvider); } } @@ -271,7 +271,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public LibraryProvider GetPurchasedLibrary() { - return PurchasedLibraryCreator.CreateLibraryProvider(this); + return PurchasedLibraryCreator.CreateLibraryProvider(this, setCurrentLibraryProvider); } } } \ No newline at end of file diff --git a/Library/Provider/LibraryProviderSqlite.cs b/Library/Provider/LibraryProviderSqlite.cs index 072c6ba12..b78bd8a6d 100644 --- a/Library/Provider/LibraryProviderSqlite.cs +++ b/Library/Provider/LibraryProviderSqlite.cs @@ -48,9 +48,9 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { public class LibraryProviderSQLiteCreator : ILibraryCreator { - public virtual LibraryProvider CreateLibraryProvider(LibraryProvider parentLibraryProvider) + public virtual LibraryProvider CreateLibraryProvider(LibraryProvider parentLibraryProvider, Action setCurrentLibraryProvider) { - return new LibraryProviderSQLite(null, parentLibraryProvider, "Local Library"); + return new LibraryProviderSQLite(null, setCurrentLibraryProvider, parentLibraryProvider, "Local Library"); } public string ProviderKey @@ -66,26 +66,12 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { protected PrintItemCollection baseLibraryCollection; protected List childCollections = new List(); - private string keywordFilter = string.Empty; public ClassicSqliteStorageProvider(LibraryProvider parentLibraryProvider) : base(parentLibraryProvider) { } - public override string KeywordFilter - { - get - { - return keywordFilter; - } - - set - { - keywordFilter = value; - } - } - public override PrintItemCollection GetCollectionItem(int collectionIndex) { return childCollections[collectionIndex]; @@ -195,20 +181,23 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public class LibraryProviderSQLite : ClassicSqliteStorageProvider { + Action setCurrentLibraryProvider; public bool PreloadingCalibrationFiles = false; private static LibraryProviderSQLite instance = null; + private string keywordFilter = string.Empty; private List printItems = new List(); - public LibraryProviderSQLite(PrintItemCollection baseLibraryCollection, LibraryProvider parentLibraryProvider, string visibleName) + public LibraryProviderSQLite(PrintItemCollection baseLibraryCollection, Action setCurrentLibraryProvider, LibraryProvider parentLibraryProvider, string visibleName) : base(parentLibraryProvider) { + this.setCurrentLibraryProvider = setCurrentLibraryProvider; this.Name = visibleName; if (baseLibraryCollection == null) { - baseLibraryCollection = GetRootLibraryCollection2().Result; + baseLibraryCollection = GetRootLibraryCollection().Result; } this.baseLibraryCollection = baseLibraryCollection; @@ -221,7 +210,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider { if (instance == null) { - instance = new LibraryProviderSQLite(null, null, "Local Library"); + instance = new LibraryProviderSQLite(null, null, null, "Local Library"); } return instance; @@ -240,6 +229,59 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider LoadLibraryItems(); } + + bool ignoreNextKeywordFilter = false; + public override string KeywordFilter + { + get + { + return keywordFilter; + } + + set + { + if (ignoreNextKeywordFilter) + { + ignoreNextKeywordFilter = false; + return; + } + + PrintItemCollection rootLibraryCollection = GetRootLibraryCollection().Result; + if (value != "" + && this.baseLibraryCollection.Id != rootLibraryCollection.Id) + { + LibraryProviderSQLite currentProvider = this.ParentLibraryProvider as LibraryProviderSQLite; + while (currentProvider.ParentLibraryProvider != null + && currentProvider.baseLibraryCollection.Id != rootLibraryCollection.Id) + { + currentProvider = currentProvider.ParentLibraryProvider as LibraryProviderSQLite; + } + + if (currentProvider != null) + { + currentProvider.KeywordFilter = value; + currentProvider.ignoreNextKeywordFilter = true; + UiThread.RunOnIdle(() => setCurrentLibraryProvider(currentProvider)); + } + } + else // the search only shows for the cloud library root + { + if (keywordFilter != value) + { + keywordFilter = value; + + if (keywordFilter != null && keywordFilter != "") + { + } + else + { + OnDataReloaded(null); + } + } + } + } + } + public override void RenameItem(int itemIndexToRename, string newName) { printItems[itemIndexToRename].PrintItem.Name = newName; @@ -315,7 +357,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public override LibraryProvider GetProviderForCollection(PrintItemCollection collection) { - return new LibraryProviderSQLite(collection, this, collection.Name); + return new LibraryProviderSQLite(collection, setCurrentLibraryProvider, this, collection.Name); } public override void RemoveCollection(int collectionIndexToRemove) @@ -392,7 +434,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider return result; } - private async Task GetRootLibraryCollection2() + private async Task GetRootLibraryCollection() { // Attempt to initialize the library from the Datastore if null PrintItemCollection rootLibraryCollection = Datastore.Instance.dbSQLite.Table().Where(v => v.Name == "_library").Take(1).FirstOrDefault(); diff --git a/MatterControl.sln b/MatterControl.sln index 624f16d82..07f489d2c 100644 --- a/MatterControl.sln +++ b/MatterControl.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Blend for Visual Studio 14 -VisualStudioVersion = 14.0.23107.0 +# Visual Studio 2013 +VisualStudioVersion = 12.0.31101.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MatterControl", "MatterControl.csproj", "{0B8D6F56-BD7F-4426-B858-D9292B084656}" ProjectSection(ProjectDependencies) = postProject @@ -122,6 +122,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mono.Nat", "..\CloudService EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GuiAutomation", "Submodules\agg-sharp\GuiAutomation\GuiAutomation.csproj", "{E9102310-0029-4D8F-B1E9-88FBA6147D45}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudServices.Tests", "..\CloudServicesPlugin\CloudServices.Tests\CloudServices.Tests.csproj", "{66BAFF8D-3079-4912-B100-41140BAF4DDD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -1107,6 +1109,30 @@ Global {E9102310-0029-4D8F-B1E9-88FBA6147D45}.Release64|x64.Build.0 = Release64|x64 {E9102310-0029-4D8F-B1E9-88FBA6147D45}.Release64|x86.ActiveCfg = Release64|Any CPU {E9102310-0029-4D8F-B1E9-88FBA6147D45}.Release64|x86.Build.0 = Release64|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Debug|x64.ActiveCfg = Debug|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Debug|x86.ActiveCfg = Debug|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Debug64|Any CPU.ActiveCfg = Debug|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Debug64|Any CPU.Build.0 = Debug|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Debug64|Mixed Platforms.ActiveCfg = Debug|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Debug64|Mixed Platforms.Build.0 = Debug|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Debug64|x64.ActiveCfg = Debug|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Debug64|x86.ActiveCfg = Debug|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Release|Any CPU.Build.0 = Release|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Release|x64.ActiveCfg = Release|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Release|x86.ActiveCfg = Release|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Release64|Any CPU.ActiveCfg = Release|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Release64|Any CPU.Build.0 = Release|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Release64|Mixed Platforms.ActiveCfg = Release|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Release64|Mixed Platforms.Build.0 = Release|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Release64|x64.ActiveCfg = Release|Any CPU + {66BAFF8D-3079-4912-B100-41140BAF4DDD}.Release64|x86.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1143,6 +1169,7 @@ Global {26164CC3-29AD-4384-861C-181440934B00} = {4E79BE4E-275E-4901-9173-E9096B7318F0} {F5D74163-145F-47BF-83DC-D0E07249C6CA} = {4E79BE4E-275E-4901-9173-E9096B7318F0} {E9102310-0029-4D8F-B1E9-88FBA6147D45} = {2AB9B589-5C98-4C05-BBEA-F97DAE168EAB} + {66BAFF8D-3079-4912-B100-41140BAF4DDD} = {4E79BE4E-275E-4901-9173-E9096B7318F0} EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution StartupItem = MatterControl.csproj diff --git a/Tests/MatterControl.Tests/MatterControl/CheckBoxInLibraryIsClickable.cs b/Tests/MatterControl.Tests/MatterControl/CheckBoxInLibraryIsClickable.cs index 08c779c8e..4943e9e46 100644 --- a/Tests/MatterControl.Tests/MatterControl/CheckBoxInLibraryIsClickable.cs +++ b/Tests/MatterControl.Tests/MatterControl/CheckBoxInLibraryIsClickable.cs @@ -78,7 +78,7 @@ namespace MatterHackers.MatterControl.UI testRunner.Wait(.5); resultsHarness.AddTestResult(checkBoxWidget.Checked == false, "currently not checked"); - UITests.CloseMatterControl(testRunner); + MatterControlUITests.CloseMatterControl(testRunner); } }; @@ -91,7 +91,9 @@ namespace MatterHackers.MatterControl.UI MatterControlApplication matterControlWindow = MatterControlApplication.CreateInstance(out showWindow); AutomationTesterHarness testHarness = AutomationTesterHarness.ShowWindowAndExectueTests(matterControlWindow, testToRun, 1000); - Assert.IsTrue(testHarness.AllTestsPassed); + // NOTE: In the future we may want to make the "Local Library Row Item Collection" not clickable. + // If that is the case fix this test to click on a child of "Local Library Row Item Collection" instead. + Assert.IsTrue(testHarness.AllTestsPassed); Assert.IsTrue(testHarness.TestCount == 4); // make sure we ran all our tests } } diff --git a/Tests/MatterControl.Tests/MatterControl/LibraryProviderSqliteTests.cs b/Tests/MatterControl.Tests/MatterControl/LibraryProviderSqliteTests.cs index 272d19527..c3e1b95e2 100644 --- a/Tests/MatterControl.Tests/MatterControl/LibraryProviderSqliteTests.cs +++ b/Tests/MatterControl.Tests/MatterControl/LibraryProviderSqliteTests.cs @@ -31,6 +31,7 @@ using MatterHackers.Agg.PlatformAbstract; using MatterHackers.MatterControl.DataStorage; using MatterHackers.MatterControl.PrintLibrary.Provider; using MatterHackers.MatterControl.PrintQueue; +using MatterHackers.MatterControl.UI; using NUnit.Framework; using System; using System.Collections.Generic; @@ -62,24 +63,9 @@ namespace MatterControl.Tests [Test, RunInApplicationDomain] public void LibraryProviderSqlite_NavigationWorking() { - string userDataPath = MatterHackers.MatterControl.DataStorage.ApplicationDataStorage.ApplicationUserDataPath; - string renamedUserDataPath = Path.Combine(Path.GetDirectoryName(userDataPath), "-MatterControl"); - int testCount = 0; - while (Directory.Exists(renamedUserDataPath + testCount.ToString())) - { - testCount++; - } - renamedUserDataPath = renamedUserDataPath + testCount.ToString(); + MatterControlUITests.DataFolderState staticDataState = MatterControlUITests.MakeNewStaticDataForTesting(); - bool undoDataRename = false; - if (Directory.Exists(userDataPath)) - { - Directory.Move(userDataPath, renamedUserDataPath); - undoDataRename = true; - } - - Datastore.Instance.Initialize(); - LibraryProviderSQLite testProvider = new LibraryProviderSQLite(null, null, "Local Library"); + LibraryProviderSQLite testProvider = new LibraryProviderSQLite(null, null, null, "Local Library"); testProvider.DataReloaded += (sender, e) => { dataReloaded = true; }; Thread.Sleep(3000); // wait for the library to finish initializing Assert.IsTrue(testProvider.CollectionCount == 0, "Start with a new database for these tests."); @@ -127,19 +113,7 @@ namespace MatterControl.Tests Assert.IsTrue(testProvider.CollectionCount == 0); Assert.IsTrue(!NamedCollectionExists(collectionName)); // assert that the record does not exist in the DB - if (undoDataRename) - { - Datastore.Instance.Exit(); - Directory.Delete(userDataPath, true); - Stopwatch time = Stopwatch.StartNew(); - // Wait for up to some amount of time for the directory to be gone. - while (Directory.Exists(userDataPath) - && time.ElapsedMilliseconds < 100) - { - Thread.Sleep(1); // make sure we are not eating all the cpu time. - } - Directory.Move(renamedUserDataPath, userDataPath); - } + MatterControlUITests.RestoreStaticDataAfterTesting(staticDataState, true); } #endif diff --git a/Tests/MatterControl.Tests/MatterControl/MatterControl.UI.cs b/Tests/MatterControl.Tests/MatterControl/MatterControl.UI.cs index 8c81a1fd8..9c6bdf346 100644 --- a/Tests/MatterControl.Tests/MatterControl/MatterControl.UI.cs +++ b/Tests/MatterControl.Tests/MatterControl/MatterControl.UI.cs @@ -33,15 +33,18 @@ using MatterHackers.Agg.PlatformAbstract; using MatterHackers.Agg.UI; using MatterHackers.Agg.UI.Tests; using MatterHackers.GuiAutomation; +using MatterHackers.MatterControl.DataStorage; using NUnit.Framework; using System; +using System.Diagnostics; using System.IO; +using System.Threading; using System.Threading.Tasks; namespace MatterHackers.MatterControl.UI { [TestFixture, Category("MatterControl.UI")] - public class UITests + public class MatterControlUITests { private static bool saveImagesForDebug = true; @@ -168,5 +171,57 @@ namespace MatterHackers.MatterControl.UI OutputImage(control, "image-control.tga"); OutputImage(test, "image-test.tga"); } + + public class DataFolderState + { + internal bool undoDataRename; + internal string userDataPath; + internal string renamedUserDataPath; + } + + public static DataFolderState MakeNewStaticDataForTesting() + { + DataFolderState state = new DataFolderState(); + state.userDataPath = MatterHackers.MatterControl.DataStorage.ApplicationDataStorage.ApplicationUserDataPath; + state.renamedUserDataPath = Path.Combine(Path.GetDirectoryName(state.userDataPath), "-MatterControl"); + int testCount = 0; + while (Directory.Exists(state.renamedUserDataPath + testCount.ToString())) + { + testCount++; + } + state.renamedUserDataPath = state.renamedUserDataPath + testCount.ToString(); + + state.undoDataRename = false; + if (Directory.Exists(state.userDataPath)) + { + Directory.Move(state.userDataPath, state.renamedUserDataPath); + state.undoDataRename = true; + } + + Datastore.Instance.Initialize(); + + return state; + } + + public static void RestoreStaticDataAfterTesting(DataFolderState state, bool closeDataBase) + { + if (state.undoDataRename) + { + Thread.Sleep(500); + if (closeDataBase) + { + Datastore.Instance.Exit(); + } + Directory.Delete(state.userDataPath, true); + Stopwatch time = Stopwatch.StartNew(); + // Wait for up to some amount of time for the directory to be gone. + while (Directory.Exists(state.userDataPath) + && time.ElapsedMilliseconds < 100) + { + Thread.Sleep(1); // make sure we are not eating all the cpu time. + } + Directory.Move(state.renamedUserDataPath, state.userDataPath); + } + } } } \ No newline at end of file