Skip to content

Commit

Permalink
#5 saving/loading library complete
Browse files Browse the repository at this point in the history
Added LibrarySaveAndLoadTest unit test
  • Loading branch information
dsarlo committed Oct 20, 2018
1 parent 2b1de1b commit a182181
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 43 deletions.
50 changes: 47 additions & 3 deletions WordByWord/WordByWord.Test/LibraryTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using CommonServiceLocator;
using System.Collections.Generic;
using System.IO;
using CommonServiceLocator;
using GalaSoft.MvvmLight.Ioc;
using MahApps.Metro.Controls.Dialogs;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WordByWord.Helpers;
using Newtonsoft.Json;
using WordByWord.Models;
using WordByWord.Services;

namespace WordByWord.Test
{
Expand All @@ -24,10 +28,50 @@ public static void TestSetup(TestContext testContext)
}

[TestMethod]
public void ImageCharacterRecognitionTest()
public void ImageCharacterRecognitionTest()//Must be run in debug!
{
string ocrResult = _viewModel.GetTextFromImage(@"TestFiles\LockeEssay.PNG");
Assert.IsTrue(ocrResult.Length > 0);
}

[TestMethod]
public void LibrarySaveAndLoadTest()
{
OcrDocument doc1 = new OcrDocument("g")
{
FileName = "Hello!",
IsBusy = false,
IsEditingFileName = false,
OcrText = "g"
};

OcrDocument doc2 = new OcrDocument("C:\\Users\\dan\\Pictures\\GDB.PNG")
{
FileName = "GDB.PNG",
OcrText = "00081 Oxbfffea68 --> 0x342\r\n\r\n00121 0xbfffea6c --> 0xbfffed24 --> 0xbfffef2b (\"/h0me/seed/Desktop/exploit\")\r\n00161 0xbfffea70 --> Oxb7fe3d39 (<check_match+9>: add ebx,0xlb2c7)\r\n00201 Oxbfffea74 --> Oxb7bf73d0 --> 0X94b90ca0\r\n\r\n00241 0xbfffea78 --> 0x53d\r\n\r\n00281 0xbfffea7c --> 0xb7ffd5b0 --> Oxb7bf3000 --> 0x464c457f\r\n\r\n[ ------------------------------------------------------------------------------ ]\r\n\r\nLegend: code, data, rodata, value\r\n\r\nBreakpoint 1, main (argc=0x1, argv=0xbfffed24) at exploit.c:25\r\n25 memset(&buffer, 0x90, 500);\r\n\r\ngdb-peda$ p &buffer\r\n\r\n$1 = (char (*)[500]) Oxbfffea78\r\n\r\ngdb-peda$ p $ebp\r\n\r\n$2 = (void *) 0xbfffec78\r\n\r\ngdb-peda$ p 0xbfffec78 .. 0xbfffea78\r\n\r\n$3 = OXZOO",
IsBusy = false,
IsEditingFileName = false
};

OcrDocument doc3 = new OcrDocument("C:\\Users\\dan\\Pictures\\GDB2.PNG")
{
FileName = "GDB2.PNG",
OcrText = "(gdb) info frame\r\nStack level. 0, frame at 0xbfffeae0:\r\n\r\neip = 0x80484c1 in bof (stack.c:11); saved eip = 0x804852e\r\ncalled by frame at OxbfffedIO\r\n\r\nsource language c.\r\nArglist at Oxbfffead8, args:\r\n\r\nstr=0xbfffeaf8 \"1N300Ph//shh/bin\\211N343PS\\211.'Nj\"\r\n\r\nLocals at Oxbfffead8, Previous frame's sp is Oxbfffeae0\r\nSaved registers:\r\n\r\nebp at Oxbfffead8, eip at Oxbfffeadc",
IsBusy = false,
IsEditingFileName = false
};

List<OcrDocument> testLibrary = new List<OcrDocument> { doc1, doc2, doc3 };

string serializedTestLibrary = JsonConvert.SerializeObject(testLibrary, Formatting.Indented);

Assert.AreEqual(File.ReadAllText(@"TestFiles\library.json"), serializedTestLibrary);

List<OcrDocument> deserializedLibrary =
JsonConvert.DeserializeObject<List<OcrDocument>>(File.ReadAllText(@"TestFiles\library.json"));

Assert.IsNotNull(deserializedLibrary);
Assert.AreEqual(testLibrary.Count, deserializedLibrary.Count);
}
}
}
2 changes: 1 addition & 1 deletion WordByWord/WordByWord.Test/ReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Threading.Tasks;
using WordByWord.Helpers;
using WordByWord.Services;

namespace WordByWord.Test
{
Expand Down
23 changes: 23 additions & 0 deletions WordByWord/WordByWord.Test/TestFiles/library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"FilePath": "g",
"FileName": "Hello!",
"OcrText": "g",
"IsBusy": false,
"IsEditingFileName": false
},
{
"FilePath": "C:\\Users\\dan\\Pictures\\GDB.PNG",
"FileName": "GDB.PNG",
"OcrText": "00081 Oxbfffea68 --> 0x342\r\n\r\n00121 0xbfffea6c --> 0xbfffed24 --> 0xbfffef2b (\"/h0me/seed/Desktop/exploit\")\r\n00161 0xbfffea70 --> Oxb7fe3d39 (<check_match+9>: add ebx,0xlb2c7)\r\n00201 Oxbfffea74 --> Oxb7bf73d0 --> 0X94b90ca0\r\n\r\n00241 0xbfffea78 --> 0x53d\r\n\r\n00281 0xbfffea7c --> 0xb7ffd5b0 --> Oxb7bf3000 --> 0x464c457f\r\n\r\n[ ------------------------------------------------------------------------------ ]\r\n\r\nLegend: code, data, rodata, value\r\n\r\nBreakpoint 1, main (argc=0x1, argv=0xbfffed24) at exploit.c:25\r\n25 memset(&buffer, 0x90, 500);\r\n\r\ngdb-peda$ p &buffer\r\n\r\n$1 = (char (*)[500]) Oxbfffea78\r\n\r\ngdb-peda$ p $ebp\r\n\r\n$2 = (void *) 0xbfffec78\r\n\r\ngdb-peda$ p 0xbfffec78 .. 0xbfffea78\r\n\r\n$3 = OXZOO",
"IsBusy": false,
"IsEditingFileName": false
},
{
"FilePath": "C:\\Users\\dan\\Pictures\\GDB2.PNG",
"FileName": "GDB2.PNG",
"OcrText": "(gdb) info frame\r\nStack level. 0, frame at 0xbfffeae0:\r\n\r\neip = 0x80484c1 in bof (stack.c:11); saved eip = 0x804852e\r\ncalled by frame at OxbfffedIO\r\n\r\nsource language c.\r\nArglist at Oxbfffead8, args:\r\n\r\nstr=0xbfffeaf8 \"1N300Ph//shh/bin\\211N343PS\\211.'Nj\"\r\n\r\nLocals at Oxbfffead8, Previous frame's sp is Oxbfffeae0\r\nSaved registers:\r\n\r\nebp at Oxbfffead8, eip at Oxbfffeadc",
"IsBusy": false,
"IsEditingFileName": false
}
]
6 changes: 6 additions & 0 deletions WordByWord/WordByWord.Test/WordByWord.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
Expand All @@ -76,6 +79,9 @@
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<Content Include="TestFiles\library.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WordByWord\WordByWord.csproj">
Expand Down
1 change: 1 addition & 0 deletions WordByWord/WordByWord.Test/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<package id="MSTest.TestAdapter" version="1.3.2" targetFramework="net461" />
<package id="MSTest.TestFramework" version="1.3.2" targetFramework="net461" />
<package id="MvvmLightLibs" version="5.4.1.1" targetFramework="net461" />
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net461" />
</packages>
2 changes: 1 addition & 1 deletion WordByWord/WordByWord/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using CommonServiceLocator;
using GalaSoft.MvvmLight.Ioc;
using MahApps.Metro.Controls.Dialogs;
using WordByWord.Helpers;
using WordByWord.Services;

namespace WordByWord
{
Expand Down
18 changes: 1 addition & 17 deletions WordByWord/WordByWord/InputText.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WordByWord
{
Expand All @@ -20,13 +7,10 @@ namespace WordByWord
/// </summary>
public partial class InputText : MetroWindow
{
ViewModel.ViewModel _viewModel;

public InputText(ViewModel.ViewModel viewModel)
{
InitializeComponent();
_viewModel = viewModel;
this.DataContext = _viewModel;
this.DataContext = viewModel;
}
}
}
2 changes: 1 addition & 1 deletion WordByWord/WordByWord/Library.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

<mah:ProgressRing Grid.Column="0" Margin="0 0 10 0" Visibility="{Binding IsBusy, Converter={StaticResource VisConverter}, Mode=OneWay}"/>
<TextBlock Grid.Column="1" FontSize="20" VerticalAlignment="Center" Text="{Binding FileName}" Margin="5 0 0 0" Visibility="Visible"/>
<TextBox Grid.Column="1" FontSize="20" VerticalAlignment="Center" Text="{Binding FileName}" Margin="5 0 0 0" Visibility="{Binding IsEditingFileName, Converter={StaticResource BooleanToVisibilityConverter}}"/>
<TextBox Grid.Column="1" FontSize="20" VerticalAlignment="Center" Text="{Binding FileName, UpdateSourceTrigger=PropertyChanged}" Margin="5 0 0 0" Visibility="{Binding IsEditingFileName, Converter={StaticResource BooleanToVisibilityConverter}}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
Expand Down
18 changes: 15 additions & 3 deletions WordByWord/WordByWord/Library.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Windows.Input;
using CommonServiceLocator;
using MahApps.Metro.Controls;
using WordByWord.Models;

namespace WordByWord
{
Expand Down Expand Up @@ -29,14 +30,25 @@ private void ListViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e

private void Rename_Click(object sender, System.Windows.RoutedEventArgs e)
{
_viewModel.SelectedDocument.IsEditingFileName = true;
if (_viewModel.SelectedDocument != null)
{
OcrDocument listViewDoc = (OcrDocument) LibraryListView.Items[LibraryListView.SelectedIndex];
if (!_viewModel.SelectedDocument.IsBusy && listViewDoc.FilePath == _viewModel.SelectedDocument.FilePath)
{
_viewModel.SelectedDocument.IsEditingFileName = true;
}
}
}

private void ListViewItem_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter && _viewModel.SelectedDocument.IsEditingFileName)
if (_viewModel.SelectedDocument != null)
{
_viewModel.SelectedDocument.IsEditingFileName = false;
if (e.Key == Key.Enter && _viewModel.SelectedDocument.IsEditingFileName)
{
_viewModel.SelectedDocument.IsEditingFileName = false;
_viewModel.SaveLibrary();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

namespace WordByWord.Helpers
namespace WordByWord.Services
{
public interface IWindowService
{
Expand Down
63 changes: 48 additions & 15 deletions WordByWord/WordByWord/ViewModel/ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
Expand All @@ -12,9 +13,10 @@
using Microsoft.Win32;
using WordByWord.Models;
using MahApps.Metro.Controls.Dialogs;
using WordByWord.Helpers;
using System.Text;
using WordByWord.Services;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using System.Text;

namespace WordByWord.ViewModel
{
Expand All @@ -36,6 +38,9 @@ public class ViewModel : ObservableObject
private int _numberOfSentences = 1;
private int _previousGrouping = 1;

private static readonly string SerializedDataFolderPath = $@"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\word-by-word\";
private readonly string _serializedLibraryPath = $"{SerializedDataFolderPath}library.json";

private readonly IDialogCoordinator _dialogService;
private readonly IWindowService _windowService;

Expand All @@ -54,6 +59,8 @@ public ViewModel(IDialogCoordinator dialogService, IWindowService windowService)
ConfirmEditCommand = new RelayCommand(ConfirmEdit);
ReadSelectedDocumentCommand = new RelayCommand(ReadSelectedDocument, () => !IsBusy);
CreateDocFromUserInputCommand = new RelayCommand(CreateDocFromUserInput);

LoadLibrary();
}

#region Properties
Expand Down Expand Up @@ -83,13 +90,16 @@ public int NumberOfSentences
set
{
Set(() => NumberOfSentences, ref _numberOfSentences, value);
switch(value)
switch (value)
{
case 1: ReaderFontSize = 30;
case 1:
ReaderFontSize = 30;
break;
case 2: ReaderFontSize = 20;
case 2:
ReaderFontSize = 20;
break;
case 3: ReaderFontSize = 20;
case 3:
ReaderFontSize = 20;
break;
}
}
Expand All @@ -111,7 +121,7 @@ public int NumberOfGroups
{
Set(() => NumberOfGroups, ref _numberOfGroups, value);
CurrentWord = string.Empty;
switch(value)
switch (value)
{
case 1:
ReaderFontSize = 50;
Expand Down Expand Up @@ -254,6 +264,7 @@ private async void UploadImage_Click(object sender, RoutedEventArgs e)
if (filePaths.Count > 0)
{
await RunOcrOnFiles(filePaths);
SaveLibrary();
}
}
}
Expand All @@ -262,24 +273,44 @@ private async void UploadImage_Click(object sender, RoutedEventArgs e)

#region Methods

public void SaveLibrary()
{
if (!Directory.Exists(SerializedDataFolderPath))
{
Directory.CreateDirectory(SerializedDataFolderPath);
}
string libraryAsJson = JsonConvert.SerializeObject(_library.Where(doc => !doc.IsBusy), Formatting.Indented);
File.WriteAllText(_serializedLibraryPath, libraryAsJson, Encoding.UTF8);
}

public void LoadLibrary()
{
string serializedLibraryFile = Directory.GetFiles(SerializedDataFolderPath).FirstOrDefault();
if (!string.IsNullOrEmpty(serializedLibraryFile))
{
Library = JsonConvert.DeserializeObject<ObservableCollection<OcrDocument>>(File.ReadAllText(serializedLibraryFile));
}
}

private void CreateDocFromUserInput()
{
if (!string.IsNullOrEmpty(UserInputTitle))
{
if (Library.All(doc => doc.FileName != UserInputTitle))
{
OcrDocument newDoc =
new OcrDocument(UserInputTitle) //All OcrDocuments must be created with a filePath!
{
OcrText = UserInputBody
};
OcrDocument newDoc = new OcrDocument(UserInputTitle) //All OcrDocuments must be created with a filePath!
{
OcrText = UserInputBody
};

Library.Add(newDoc);

UserInputTitle = string.Empty;
UserInputBody = string.Empty;

_windowService.CloseWindow("InputText", this);

SaveLibrary();
}
else
{
Expand Down Expand Up @@ -318,7 +349,7 @@ private async Task ReadSelectedDocumentWordsAsync()
if (!string.IsNullOrWhiteSpace(word))
{
CurrentWord = word;
await Task.Delay((int) ReaderDelay);
await Task.Delay(ReaderDelay);
}
}
IsBusy = false;
Expand All @@ -337,7 +368,7 @@ private async Task ReadSelectedDocumentSentencesAsync()
if (!string.IsNullOrWhiteSpace(sentence))
{
CurrentWord = sentence;
await Task.Delay((int) CalcDelay(sentence));
await Task.Delay((int)CalcDelay(sentence));
}
}
IsBusy = false;
Expand Down Expand Up @@ -382,7 +413,7 @@ await Task.Run(() =>
groups.Add(group);
}
});

return groups;
}

Expand All @@ -403,6 +434,8 @@ private void ConfirmEdit()
Library.Single(doc => doc.FilePath == SelectedDocument.FilePath).OcrText = EditorText;

_windowService.CloseWindow("Editor", this);

SaveLibrary();
}

internal void OpenReaderWindow()
Expand Down
Loading

0 comments on commit a182181

Please sign in to comment.