Browse Source
Allow unit testing of view contents by decoupling OpenedFile from FileService and Workbench. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2663 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
13 changed files with 427 additions and 288 deletions
@ -0,0 +1,59 @@ |
|||||||
|
// <file>
|
||||||
|
// <copyright see="prj:///doc/copyright.txt"/>
|
||||||
|
// <license see="prj:///doc/license.txt"/>
|
||||||
|
// <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
|
||||||
|
// <version>$Revision: 2658$</version>
|
||||||
|
// </file>
|
||||||
|
|
||||||
|
using System; |
||||||
|
using ICSharpCode.SharpDevelop.Gui; |
||||||
|
using ICSharpCode.SharpDevelop; |
||||||
|
using System.Collections.Generic; |
||||||
|
|
||||||
|
namespace XmlEditor.Tests.Utils |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Description of MockOpenedFile.
|
||||||
|
/// </summary>
|
||||||
|
public class MockOpenedFile : OpenedFile |
||||||
|
{ |
||||||
|
public MockOpenedFile(string fileName) |
||||||
|
{ |
||||||
|
base.FileName = fileName; |
||||||
|
base.IsUntitled = true; |
||||||
|
SetData(new byte[0]); |
||||||
|
} |
||||||
|
|
||||||
|
List<IViewContent> registeredViews = new List<IViewContent>(); |
||||||
|
|
||||||
|
public override IList<IViewContent> RegisteredViewContents { |
||||||
|
get { return registeredViews.AsReadOnly(); } |
||||||
|
} |
||||||
|
|
||||||
|
public override void RegisterView(IViewContent view) |
||||||
|
{ |
||||||
|
if (view == null) |
||||||
|
throw new ArgumentNullException("view"); |
||||||
|
if (registeredViews.Contains(view)) |
||||||
|
throw new ArgumentException("registeredViews already contains view"); |
||||||
|
|
||||||
|
registeredViews.Add(view); |
||||||
|
|
||||||
|
if (registeredViews.Count == 1) |
||||||
|
SwitchedToView(registeredViews[0]); |
||||||
|
} |
||||||
|
|
||||||
|
public override void UnregisterView(IViewContent view) |
||||||
|
{ |
||||||
|
if (!registeredViews.Remove(view)) |
||||||
|
throw new ArgumentException("registeredViews does not contain view"); |
||||||
|
} |
||||||
|
|
||||||
|
public void SwitchToView(IViewContent view) |
||||||
|
{ |
||||||
|
if (!registeredViews.Contains(view)) |
||||||
|
throw new ArgumentException("registeredViews does not contain view"); |
||||||
|
base.SwitchedToView(view); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,126 @@ |
|||||||
|
// <file>
|
||||||
|
// <copyright see="prj:///doc/copyright.txt"/>
|
||||||
|
// <license see="prj:///doc/license.txt"/>
|
||||||
|
// <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
|
||||||
|
// <version>$Revision$</version>
|
||||||
|
// </file>
|
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Diagnostics; |
||||||
|
using System.IO; |
||||||
|
using ICSharpCode.SharpDevelop.Gui; |
||||||
|
using ICSharpCode.Core; |
||||||
|
|
||||||
|
namespace ICSharpCode.SharpDevelop |
||||||
|
{ |
||||||
|
#region FileChangeWatcher
|
||||||
|
internal sealed class FileChangeWatcher //: IDisposable
|
||||||
|
{ |
||||||
|
/*FileSystemWatcher watcher; |
||||||
|
bool wasChangedExternally = false; |
||||||
|
string fileName; |
||||||
|
AbstractViewContent viewContent; |
||||||
|
|
||||||
|
public FileChangeWatcher(AbstractViewContent viewContent) |
||||||
|
{ |
||||||
|
this.viewContent = viewContent; |
||||||
|
WorkbenchSingleton.MainForm.Activated += GotFocusEvent; |
||||||
|
} |
||||||
|
*/ |
||||||
|
|
||||||
|
public static bool DetectExternalChangesOption { |
||||||
|
get { |
||||||
|
return PropertyService.Get("SharpDevelop.FileChangeWatcher.DetectExternalChanges", true); |
||||||
|
} |
||||||
|
set { |
||||||
|
PropertyService.Set("SharpDevelop.FileChangeWatcher.DetectExternalChanges", value); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public static bool AutoLoadExternalChangesOption { |
||||||
|
get { |
||||||
|
return PropertyService.Get("SharpDevelop.FileChangeWatcher.AutoLoadExternalChanges", true); |
||||||
|
} |
||||||
|
set { |
||||||
|
PropertyService.Set("SharpDevelop.FileChangeWatcher.AutoLoadExternalChanges", value); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/* |
||||||
|
public void Dispose() |
||||||
|
{ |
||||||
|
WorkbenchSingleton.MainForm.Activated -= GotFocusEvent; |
||||||
|
if (watcher != null) { |
||||||
|
watcher.Dispose(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void Disable() |
||||||
|
{ |
||||||
|
if (watcher != null) { |
||||||
|
watcher.EnableRaisingEvents = false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public void SetWatcher(string fileName) |
||||||
|
{ |
||||||
|
this.fileName = fileName; |
||||||
|
if (DetectExternalChangesOption == false) |
||||||
|
return; |
||||||
|
try { |
||||||
|
if (this.watcher == null) { |
||||||
|
this.watcher = new FileSystemWatcher(); |
||||||
|
this.watcher.SynchronizingObject = WorkbenchSingleton.MainForm; |
||||||
|
this.watcher.Changed += new FileSystemEventHandler(this.OnFileChangedEvent); |
||||||
|
} else { |
||||||
|
this.watcher.EnableRaisingEvents = false; |
||||||
|
} |
||||||
|
this.watcher.Path = Path.GetDirectoryName(fileName); |
||||||
|
this.watcher.Filter = Path.GetFileName(fileName); |
||||||
|
this.watcher.NotifyFilter = NotifyFilters.LastWrite; |
||||||
|
this.watcher.EnableRaisingEvents = true; |
||||||
|
} catch (PlatformNotSupportedException) { |
||||||
|
if (watcher != null) { |
||||||
|
watcher.Dispose(); |
||||||
|
} |
||||||
|
watcher = null; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void OnFileChangedEvent(object sender, FileSystemEventArgs e) |
||||||
|
{ |
||||||
|
if (e.ChangeType != WatcherChangeTypes.Deleted) { |
||||||
|
wasChangedExternally = true; |
||||||
|
if (ICSharpCode.SharpDevelop.Gui.WorkbenchSingleton.Workbench.IsActiveWindow) { |
||||||
|
// delay showing message a bit, prevents showing two messages
|
||||||
|
// when the file changes twice in quick succession
|
||||||
|
WorkbenchSingleton.SafeThreadAsyncCall(GotFocusEvent, this, EventArgs.Empty); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void GotFocusEvent(object sender, EventArgs e) |
||||||
|
{ |
||||||
|
if (wasChangedExternally) { |
||||||
|
wasChangedExternally = false; |
||||||
|
|
||||||
|
string message = StringParser.Parse("${res:ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor.TextEditorDisplayBinding.FileAlteredMessage}", new string[,] {{"File", Path.GetFullPath(fileName)}}); |
||||||
|
if ((AutoLoadExternalChangesOption && viewContent.IsDirty == false) |
||||||
|
|| MessageBox.Show(message, |
||||||
|
StringParser.Parse("${res:MainWindow.DialogName}"), |
||||||
|
MessageBoxButtons.YesNo, |
||||||
|
MessageBoxIcon.Question) == DialogResult.Yes) |
||||||
|
{ |
||||||
|
if (File.Exists(fileName)) { |
||||||
|
viewContent.Load(fileName); |
||||||
|
} |
||||||
|
} else { |
||||||
|
viewContent.IsDirty = true; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
*/ |
||||||
|
} |
||||||
|
#endregion
|
||||||
|
} |
||||||
Loading…
Reference in new issue