Adding tests for App

WIP make VMs app agnostic
This commit is contained in:
BONNEVILLE Geoffroy
2017-11-22 18:54:03 +01:00
parent f2d97b4e7e
commit 5120c8177b
19 changed files with 467 additions and 40 deletions

View File

@@ -2,6 +2,7 @@
using Windows.Storage;
using Windows.UI.Xaml;
using ModernKeePass.Common;
using ModernKeePass.Interfaces;
using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Keys;
using ModernKeePassLib.Serialization;
@@ -18,7 +19,7 @@ namespace ModernKeePass.ViewModels
Success = 5
}
private readonly App _app = Application.Current as App;
//private readonly App _app = Application.Current as App;
private bool _hasPassword;
private bool _hasKeyFile;
private string _password = string.Empty;
@@ -27,6 +28,8 @@ namespace ModernKeePass.ViewModels
private StorageFile _keyFile;
private string _keyFileText = "Select key file from disk...";
public IDatabase Database { get; set; }
public bool HasPassword
{
get { return _hasPassword; }
@@ -90,15 +93,23 @@ namespace ModernKeePass.ViewModels
}
public GroupVm RootGroup { get; set; }
public double PasswordComplexityIndicator => QualityEstimation.EstimatePasswordBits(Password?.ToCharArray());
public CompositeKeyVm() : this((Application.Current as App)?.Database) { }
public CompositeKeyVm(IDatabase database)
{
Database = database;
}
public bool OpenDatabase(bool createNew)
{
_app.Database.Open(CreateCompositeKey(), createNew);
switch (_app.Database.Status)
Database.Open(CreateCompositeKey(), createNew);
switch ((DatabaseHelper.DatabaseStatus)Database.Status)
{
case DatabaseHelper.DatabaseStatus.Opened:
RootGroup = _app.Database.RootGroup;
RootGroup = Database.RootGroup;
return true;
case DatabaseHelper.DatabaseStatus.CompositeKeyError:
var errorMessage = new StringBuilder("Error: wrong ");
@@ -113,7 +124,7 @@ namespace ModernKeePass.ViewModels
public void UpdateKey()
{
_app.Database.UpdateCompositeKey(CreateCompositeKey());
Database.UpdateCompositeKey(CreateCompositeKey());
UpdateStatus("Database composite key updated.", StatusTypes.Success);
}

View File

@@ -14,6 +14,7 @@ namespace ModernKeePass.ViewModels
{
public class EntryVm : INotifyPropertyChanged, IPwEntity
{
public IDatabase Database { get; set; }
public GroupVm ParentGroup { get; private set; }
public GroupVm PreviousGroup { get; private set; }
@@ -150,7 +151,6 @@ namespace ModernKeePass.ViewModels
public event PropertyChangedEventHandler PropertyChanged;
private readonly PwEntry _pwEntry;
private readonly App _app = Application.Current as App;
private bool _isEditMode;
private bool _isRevealPassword;
private double _passwordLength = 25;
@@ -160,16 +160,20 @@ namespace ModernKeePass.ViewModels
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public EntryVm() { }
public EntryVm(PwEntry entry, GroupVm parent)
public EntryVm() : this(null, null) { }
internal EntryVm(PwEntry entry, GroupVm parent) : this(entry, parent, (Application.Current as App)?.Database) { }
public EntryVm(PwEntry entry, GroupVm parent, IDatabase database)
{
Database = database;
_pwEntry = entry;
ParentGroup = parent;
}
public void GeneratePassword()
{
var pwProfile = new PwProfile()
var pwProfile = new PwProfile
{
GeneratorType = PasswordGeneratorType.CharSet,
Length = (uint)PasswordLength,
@@ -208,9 +212,9 @@ namespace ModernKeePass.ViewModels
public void MarkForDelete()
{
if (_app.Database.RecycleBinEnabled && _app.Database.RecycleBin?.IdUuid == null)
_app.Database.CreateRecycleBin();
Move(_app.Database.RecycleBinEnabled && !ParentGroup.IsSelected ? _app.Database.RecycleBin : null);
if (Database.RecycleBinEnabled && Database.RecycleBin?.IdUuid == null)
Database.CreateRecycleBin();
Move(Database.RecycleBinEnabled && !ParentGroup.IsSelected ? Database.RecycleBin : null);
}
public void UndoDelete()
@@ -225,7 +229,7 @@ namespace ModernKeePass.ViewModels
PreviousGroup.RemovePwEntry(_pwEntry);
if (destination == null)
{
_app.Database.AddDeletedItem(IdUuid);
Database.AddDeletedItem(IdUuid);
return;
}
ParentGroup = destination;
@@ -236,13 +240,13 @@ namespace ModernKeePass.ViewModels
public void CommitDelete()
{
_pwEntry.ParentGroup.Entries.Remove(_pwEntry);
if (_app.Database.RecycleBinEnabled && !PreviousGroup.IsSelected) _app.Database.RecycleBin.AddPwEntry(_pwEntry);
else _app.Database.AddDeletedItem(IdUuid);
if (Database.RecycleBinEnabled && !PreviousGroup.IsSelected) Database.RecycleBin.AddPwEntry(_pwEntry);
else Database.AddDeletedItem(IdUuid);
}
public void Save()
{
_app.Database.Save();
Database.Save();
}
}
}

View File

@@ -49,14 +49,14 @@ namespace ModernKeePass.ViewModels
{
var app = (App)Application.Current;
var mru = StorageApplicationPermissions.MostRecentlyUsedList;
var isDatabaseOpen = app.Database != null && app.Database.Status == DatabaseHelper.DatabaseStatus.Opened;
var isDatabaseOpen = app.Database != null && app.Database.Status == (int) DatabaseHelper.DatabaseStatus.Opened;
var mainMenuItems = new ObservableCollection<MainMenuItemVm>
{
new MainMenuItemVm
{
Title = "Open", PageType = typeof(OpenDatabasePage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Page2,
IsSelected = app.Database.Status == DatabaseHelper.DatabaseStatus.Opening
IsSelected = app.Database.Status == (int) DatabaseHelper.DatabaseStatus.Opening
},
new MainMenuItemVm
{
@@ -69,7 +69,7 @@ namespace ModernKeePass.ViewModels
},
new MainMenuItemVm {
Title = "Recent" , PageType = typeof(RecentDatabasesPage), Destination = destinationFrame, Parameter = referenceFrame, SymbolIcon = Symbol.Copy,
IsSelected = (app.Database == null || app.Database.Status == DatabaseHelper.DatabaseStatus.Closed) && mru.Entries.Count > 0, IsEnabled = mru.Entries.Count > 0
IsSelected = (app.Database == null || app.Database.Status == (int) DatabaseHelper.DatabaseStatus.Closed) && mru.Entries.Count > 0, IsEnabled = mru.Entries.Count > 0
},
new MainMenuItemVm
{
@@ -78,7 +78,7 @@ namespace ModernKeePass.ViewModels
};
// Auto-select the Recent Items menu item if the conditions are met
SelectedItem = mainMenuItems.FirstOrDefault(m => m.IsSelected);
if (app.Database != null && app.Database.Status == DatabaseHelper.DatabaseStatus.Opened)
if (app.Database != null && app.Database.Status == (int) DatabaseHelper.DatabaseStatus.Opened)
mainMenuItems.Add(new MainMenuItemVm
{
Title = app.Database.Name,

View File

@@ -10,7 +10,7 @@ namespace ModernKeePass.ViewModels
{
public bool ShowPasswordBox
{
get { return ((App)Application.Current).Database.Status == DatabaseHelper.DatabaseStatus.Opening; }
get { return ((App)Application.Current).Database.Status == (int) DatabaseHelper.DatabaseStatus.Opening; }
}
public string Name
@@ -21,7 +21,7 @@ namespace ModernKeePass.ViewModels
public OpenVm()
{
var app = Application.Current as App;
if (app?.Database == null || app.Database.Status != DatabaseHelper.DatabaseStatus.Opening) return;
if (app?.Database == null || app.Database.Status != (int) DatabaseHelper.DatabaseStatus.Opening) return;
OpenFile(app.Database.DatabaseFile);
}