Writing unit tests for the library provider stuff.

Got the file folder provider acting like nodes rather than a static set.
This commit is contained in:
Lars Brubaker 2015-06-29 18:03:56 -07:00
parent 70a8460af7
commit 593aee44f9
16 changed files with 599 additions and 624 deletions

View file

@ -40,49 +40,60 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
{
public class LibraryProviderSelector : LibraryProvider
{
private static LibraryProviderSelector instance = null;
private List<LibraryProvider> libraryProviders = new List<LibraryProvider>();
private int selectedLibraryProvider = -1;
public LibraryProviderSelector(string parentProviderKey)
: base(parentProviderKey)
private LibraryProviderSelector()
: base(null)
{
// put in the sqlite provider
libraryProviders.Add(LibraryProviderSQLite.Instance);
libraryProviders.Add(new LibraryProviderSQLite(this));
// and any directory providers (sd card provider, etc...)
libraryProviders.Add(new LibraryProviderFileSystem(Path.Combine("C:\\", "Users", "LarsBrubaker", "Downloads"), "Downloads", this.ProviderKey));
//libraryProviders.Add(new LibraryProviderFileSystem(Path.Combine("C:\\", "Users", "LarsBrubaker", "Downloads"), "Downloads", this));
//#if __ANDROID__
//libraryProviders.Add(new LibraryProviderFileSystem(ApplicationDataStorage.Instance.PublicDataStoragePath, "Downloads", this.ProviderKey));
PrintItemCollection libraryCollection = new PrintItemCollection("Library Folder1", Path.Combine("C:\\", "Users", "LarsBrubaker", "AppData", "Local", "MatterControl", "Library"));
//libraryProviders.Add(new LibraryProviderFileSystem(libraryCollection, "Library Folder2", this.ProviderKey));
// Check for LibraryProvider factories and put them in the list too.
PluginFinder<LibraryProviderPlugin> libraryProviderPlugins = new PluginFinder<LibraryProviderPlugin>();
foreach (LibraryProviderPlugin libraryProviderPlugin in libraryProviderPlugins.Plugins)
{
libraryProviders.Add(libraryProviderPlugin.CreateLibraryProvider(this.ProviderKey));
libraryProviders.Add(libraryProviderPlugin.CreateLibraryProvider(this));
}
providerLocationStack.Add(new PrintItemCollection("..", ProviderKey));
}
public static LibraryProviderSelector Instance
{
get
{
if (instance == null)
{
instance = new LibraryProviderSelector();
}
return instance;
}
}
#region Overriden Abstract Methods
private List<PrintItemCollection> providerLocationStack = new List<PrintItemCollection>();
public static string LibraryProviderSelectorKey
{
get
{
return "ProviderSelectorKey";
}
}
public override int CollectionCount
{
get
{
if (selectedLibraryProvider == -1)
{
return libraryProviders.Count;
}
else
{
return libraryProviders[selectedLibraryProvider].CollectionCount;
}
return libraryProviders.Count;
}
}
@ -90,14 +101,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
{
get
{
if (selectedLibraryProvider == -1)
{
return 0;
}
else
{
return libraryProviders[selectedLibraryProvider].ItemCount;
}
return 0;
}
}
@ -105,25 +109,11 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
{
get
{
if (selectedLibraryProvider == -1)
{
return "";
}
else
{
return libraryProviders[selectedLibraryProvider].KeywordFilter;
}
return "";
}
set
{
if (selectedLibraryProvider == -1)
{
}
else
{
libraryProviders[selectedLibraryProvider].KeywordFilter = value;
}
}
}
@ -135,6 +125,11 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public override string ProviderData
{
get { return ""; }
}
public override string ProviderKey
{
get
@ -143,24 +138,9 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
}
public static string LibraryProviderSelectorKey
{
get
{
return "ProviderSelectorKey";
}
}
public override void AddCollectionToLibrary(string collectionName)
{
if (selectedLibraryProvider == -1)
{
throw new NotImplementedException();
}
else
{
libraryProviders[selectedLibraryProvider].AddCollectionToLibrary(collectionName);
}
throw new NotImplementedException();
}
public override void AddFilesToLibrary(IList<string> files, List<ProviderLocatorNode> providerSavePath, ReportProgressRatio reportProgress = null, RunWorkerCompletedEventHandler callback = null)
@ -178,85 +158,35 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
public override PrintItemCollection GetCollectionItem(int collectionIndex)
{
if (selectedLibraryProvider == -1)
{
LibraryProvider provider = libraryProviders[collectionIndex];
return new PrintItemCollection(provider.Name, provider.ProviderKey);
}
else
{
return libraryProviders[selectedLibraryProvider].GetCollectionItem(collectionIndex);
}
}
public override PrintItemCollection GetParentCollectionItem()
{
if (selectedLibraryProvider == -1)
{
return null;
}
else
{
return libraryProviders[selectedLibraryProvider].GetParentCollectionItem();
}
LibraryProvider provider = libraryProviders[collectionIndex];
return new PrintItemCollection(provider.Name, provider.ProviderKey);
}
public override PrintItemWrapper GetPrintItemWrapper(int itemIndex)
{
if (selectedLibraryProvider == -1)
if (libraryProviders[0].ProviderKey != LibraryProviderSQLite.StaticProviderKey)
{
if (libraryProviders[0].ProviderKey != LibraryProviderSQLite.StaticProviderKey)
{
throw new Exception("It is expected these are the same.");
}
return libraryProviders[0].GetPrintItemWrapper(itemIndex);
}
else
{
return libraryProviders[selectedLibraryProvider].GetPrintItemWrapper(itemIndex);
throw new Exception("It is expected these are the same.");
}
return libraryProviders[0].GetPrintItemWrapper(itemIndex);
}
// A key,value list that threads into the current collection loos like "key0,displayName0|key1,displayName1|key2,displayName2|...|keyN,displayNameN".
public override List<ProviderLocatorNode> GetProviderLocator()
public override LibraryProvider GetProviderForItem(PrintItemCollection collection)
{
if (selectedLibraryProvider == -1)
foreach (LibraryProvider libraryProvider in libraryProviders)
{
return new List<ProviderLocatorNode>();
}
else
{
List<ProviderLocatorNode> providerPathNodes = new List<ProviderLocatorNode>();
bool first = true;
for (int i = 0; i < providerLocationStack.Count; i++)
if (collection.Key == libraryProvider.ProviderKey)
{
PrintItemCollection collection = providerLocationStack[i];
if (first)
{
providerPathNodes.Add(new ProviderLocatorNode(collection.Key, collection.Name));
first = false;
}
else
{
providerPathNodes.Add(new ProviderLocatorNode(collection.Key, collection.Name));
}
return libraryProvider;
}
return providerPathNodes;
}
throw new NotImplementedException();
}
public override void RemoveCollection(string collectionName)
{
if (selectedLibraryProvider == -1)
{
throw new NotImplementedException();
}
else
{
libraryProviders[selectedLibraryProvider].RemoveCollection(collectionName);
}
throw new NotImplementedException();
}
public override void RemoveItem(PrintItemWrapper printItemWrapper)
@ -269,61 +199,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
public override void SaveToLibrary(PrintItemWrapper printItemWrapper, List<MeshGroup> meshGroupsToSave, List<ProviderLocatorNode> providerSavePath = null)
{
if (selectedLibraryProvider == -1)
{
throw new NotImplementedException();
}
else
{
List<ProviderLocatorNode> subProviderSavePath;
int libraryProviderToUseIndex = GetProviderIndex(printItemWrapper, out subProviderSavePath);
libraryProviders[libraryProviderToUseIndex].SaveToLibrary(printItemWrapper, meshGroupsToSave, subProviderSavePath);
}
}
public override void SetCollectionBase(PrintItemCollection collectionBase)
{
// This logic may need to be move legitamately into the virtual functions of the providers rather than all
// gathered up here. If you find that this is not working the way you want ask me. LBB
if ((providerLocationStack.Count > 2
&& collectionBase.Key == providerLocationStack[providerLocationStack.Count - 2].Key)
|| (providerLocationStack.Count > 1
&& selectedLibraryProvider != -1
&& collectionBase.Key == libraryProviders[selectedLibraryProvider].GetParentCollectionItem().Key)
)
{
providerLocationStack.RemoveAt(providerLocationStack.Count - 1);
}
else
{
providerLocationStack.Add(collectionBase);
}
if (collectionBase.Key == this.ProviderKey)
{
selectedLibraryProvider = -1;
}
else
{
bool wasSet = false;
for (int i = 0; i < libraryProviders.Count; i++)
{
if (libraryProviders[i].ProviderKey == collectionBase.Key)
{
selectedLibraryProvider = i;
wasSet = true;
break;
}
}
if (!wasSet)
{
libraryProviders[selectedLibraryProvider].SetCollectionBase(collectionBase);
}
}
CollectionChanged.CallEvents(this, null);
throw new NotImplementedException();
}
private int GetProviderIndex(PrintItemWrapper printItemWrapper, out List<ProviderLocatorNode> subProviderSavePath)
@ -355,5 +231,10 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider
}
#endregion Overriden Abstract Methods
public static LibraryProvider GetProviderForItem(PrintItemWrapper printItemWrapper)
{
throw new NotImplementedException();
}
}
}