Skip to content

Commit

Permalink
Show error message when saving file in use
Browse files Browse the repository at this point in the history
Fixes #28
  • Loading branch information
fernandreu committed Mar 13, 2019
1 parent 93c8440 commit ad72d20
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
15 changes: 14 additions & 1 deletion CustomUIEditor/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,20 @@ private void FinishOpeningFile(string fileName)
private void Save()
{
this.ApplyCurrentText();
this.CurrentDocument?.Save(this.ReloadOnSave);

if (this.CurrentDocument == null)
{
return;
}

try
{
this.CurrentDocument.Save(this.ReloadOnSave);
}
catch (IOException ex)
{
this.messageBoxService.Show(ex.Message, "Error saving Office document", MessageBoxButton.OK, MessageBoxImage.Error);
}
}

private void SaveAll()
Expand Down
41 changes: 41 additions & 0 deletions CustomUIEditorTests/ViewModels/MainWindowViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,47 @@ public void InsertSampleTest()
Assert.AreEqual(contents, part.Contents, "Sample XML file not created with the expected contents");
}

/// <summary>
/// Checks whether a file being saved while opened in another program is detected correctly and does not cause any crash
/// </summary>
[Test]
public void SaveInUseTest()
{
File.Copy(this.sourceFile, this.destFile);
this.MockOpenFile(this.destFile);
this.viewModel.OpenCommand.Execute();
this.viewModel.SelectedItem = this.viewModel.DocumentList[0];

// Open the same file in exclusive mode
Assert.DoesNotThrow(() =>
{
using (File.Open(this.destFile, FileMode.Open, FileAccess.Read, FileShare.None))
{
this.viewModel.SaveCommand.Execute();
}
});
}

/// <summary>
/// Checks whether a file being saved with the Save As... option while opened in another program is detected correctly and does not cause any crash
/// </summary>
[Test]
public void SaveAsInUseTest()
{
this.viewModel.OpenCommand.Execute();
this.viewModel.SelectedItem = this.viewModel.DocumentList[0];
this.viewModel.SaveAsCommand.Execute();

// Open the same file in exclusive mode
Assert.DoesNotThrow(() =>
{
using (File.Open(this.destFile, FileMode.Open, FileAccess.Read, FileShare.None))
{
this.viewModel.SaveAsCommand.Execute();
}
});
}

private void MockOpenFile(string path)
{
this.fileSvc.Setup(x => x.OpenFileDialog(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Action<string>>(), It.IsAny<string>(), It.IsAny<int>()))
Expand Down

0 comments on commit ad72d20

Please sign in to comment.