mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 10:46:33 +02:00
whitespace forced changes (#6002)
This commit is contained in:
@@ -1,20 +1,20 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Diagnostics.Tracing;
|
||||
using Microsoft.PowerToys.Telemetry;
|
||||
using Microsoft.PowerToys.Telemetry.Events;
|
||||
|
||||
namespace MarkdownPreviewHandler.Telemetry.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// A telemetry event that is triggered when a markdown file is viewed in the preview pane.
|
||||
/// </summary>
|
||||
[EventData]
|
||||
public class MarkdownFileHandlerLoaded : EventBase, IEvent
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage;
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Diagnostics.Tracing;
|
||||
using Microsoft.PowerToys.Telemetry;
|
||||
using Microsoft.PowerToys.Telemetry.Events;
|
||||
|
||||
namespace MarkdownPreviewHandler.Telemetry.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// A telemetry event that is triggered when a markdown file is viewed in the preview pane.
|
||||
/// </summary>
|
||||
[EventData]
|
||||
public class MarkdownFileHandlerLoaded : EventBase, IEvent
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,115 +1,115 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
using MarkdownPreviewHandler;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using PreviewHandlerCommon;
|
||||
|
||||
namespace PreviewPaneUnitTests
|
||||
{
|
||||
[TestClass]
|
||||
public class MarkdownPreviewHandlerTest
|
||||
{
|
||||
[TestMethod]
|
||||
public void MarkdownPreviewHandlerControl__AddsBrowserToForm_WhenDoPreviewIsCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var markdownPreviewHandlerControl = new MarkdownPreviewHandlerControl())
|
||||
{
|
||||
// Act
|
||||
markdownPreviewHandlerControl.DoPreview<string>("HelperFiles/MarkdownWithExternalImage.txt");
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(markdownPreviewHandlerControl.Controls.Count, 2);
|
||||
Assert.IsInstanceOfType(markdownPreviewHandlerControl.Controls[0], typeof(WebBrowserExt));
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MarkdownPreviewHandlerControl__AddsInfoBarToFormIfExternalImageLinkPresent_WhenDoPreviewIsCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var markdownPreviewHandlerControl = new MarkdownPreviewHandlerControl())
|
||||
{
|
||||
// Act
|
||||
markdownPreviewHandlerControl.DoPreview<string>("HelperFiles/MarkdownWithExternalImage.txt");
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(markdownPreviewHandlerControl.Controls.Count, 2);
|
||||
Assert.IsInstanceOfType(markdownPreviewHandlerControl.Controls[1], typeof(RichTextBox));
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MarkdownPreviewHandlerControl__AddsInfoBarToFormIfHTMLImageTagIsPresent_WhenDoPreviewIsCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var markdownPreviewHandlerControl = new MarkdownPreviewHandlerControl())
|
||||
{
|
||||
// Act
|
||||
markdownPreviewHandlerControl.DoPreview<string>("HelperFiles/MarkdownWithHTMLImageTag.txt");
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(markdownPreviewHandlerControl.Controls.Count, 2);
|
||||
Assert.IsInstanceOfType(markdownPreviewHandlerControl.Controls[1], typeof(RichTextBox));
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MarkdownPreviewHandlerControl__DoesNotAddInfoBarToFormIfExternalImageLinkNotPresent_WhenDoPreviewIsCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var markdownPreviewHandlerControl = new MarkdownPreviewHandlerControl())
|
||||
{
|
||||
// Act
|
||||
markdownPreviewHandlerControl.DoPreview<string>("HelperFiles/MarkdownWithScript.txt");
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(markdownPreviewHandlerControl.Controls.Count, 1);
|
||||
Assert.IsInstanceOfType(markdownPreviewHandlerControl.Controls[0], typeof(WebBrowserExt));
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MarkdownPreviewHandlerControl__UpdatesWebBrowserSettings_WhenDoPreviewIsCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var markdownPreviewHandlerControl = new MarkdownPreviewHandlerControl())
|
||||
{
|
||||
// Act
|
||||
markdownPreviewHandlerControl.DoPreview<string>("HelperFiles/MarkdownWithExternalImage.txt");
|
||||
|
||||
// Assert
|
||||
Assert.IsInstanceOfType(markdownPreviewHandlerControl.Controls[0], typeof(WebBrowserExt));
|
||||
Assert.IsNotNull(((WebBrowser)markdownPreviewHandlerControl.Controls[0]).DocumentText);
|
||||
Assert.AreEqual(((WebBrowser)markdownPreviewHandlerControl.Controls[0]).Dock, DockStyle.Fill);
|
||||
Assert.AreEqual(((WebBrowser)markdownPreviewHandlerControl.Controls[0]).IsWebBrowserContextMenuEnabled, false);
|
||||
Assert.AreEqual(((WebBrowser)markdownPreviewHandlerControl.Controls[0]).ScriptErrorsSuppressed, true);
|
||||
Assert.AreEqual(((WebBrowser)markdownPreviewHandlerControl.Controls[0]).ScrollBarsEnabled, true);
|
||||
Assert.AreEqual(((WebBrowser)markdownPreviewHandlerControl.Controls[0]).AllowNavigation, false);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MarkdownPreviewHandlerControl__UpdateInfobarSettings_WhenDoPreviewIsCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var markdownPreviewHandlerControl = new MarkdownPreviewHandlerControl())
|
||||
{
|
||||
// Act
|
||||
markdownPreviewHandlerControl.DoPreview<string>("HelperFiles/MarkdownWithExternalImage.txt");
|
||||
|
||||
// Assert
|
||||
Assert.IsInstanceOfType(markdownPreviewHandlerControl.Controls[1], typeof(RichTextBox));
|
||||
Assert.IsNotNull(((RichTextBox)markdownPreviewHandlerControl.Controls[1]).Text);
|
||||
Assert.AreEqual(((RichTextBox)markdownPreviewHandlerControl.Controls[1]).Dock, DockStyle.Top);
|
||||
Assert.AreEqual(((RichTextBox)markdownPreviewHandlerControl.Controls[1]).BorderStyle, BorderStyle.None);
|
||||
Assert.AreEqual(((RichTextBox)markdownPreviewHandlerControl.Controls[1]).BackColor, Color.LightYellow);
|
||||
Assert.AreEqual(((RichTextBox)markdownPreviewHandlerControl.Controls[1]).Multiline, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
using MarkdownPreviewHandler;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using PreviewHandlerCommon;
|
||||
|
||||
namespace PreviewPaneUnitTests
|
||||
{
|
||||
[TestClass]
|
||||
public class MarkdownPreviewHandlerTest
|
||||
{
|
||||
[TestMethod]
|
||||
public void MarkdownPreviewHandlerControl__AddsBrowserToForm_WhenDoPreviewIsCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var markdownPreviewHandlerControl = new MarkdownPreviewHandlerControl())
|
||||
{
|
||||
// Act
|
||||
markdownPreviewHandlerControl.DoPreview<string>("HelperFiles/MarkdownWithExternalImage.txt");
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(markdownPreviewHandlerControl.Controls.Count, 2);
|
||||
Assert.IsInstanceOfType(markdownPreviewHandlerControl.Controls[0], typeof(WebBrowserExt));
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MarkdownPreviewHandlerControl__AddsInfoBarToFormIfExternalImageLinkPresent_WhenDoPreviewIsCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var markdownPreviewHandlerControl = new MarkdownPreviewHandlerControl())
|
||||
{
|
||||
// Act
|
||||
markdownPreviewHandlerControl.DoPreview<string>("HelperFiles/MarkdownWithExternalImage.txt");
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(markdownPreviewHandlerControl.Controls.Count, 2);
|
||||
Assert.IsInstanceOfType(markdownPreviewHandlerControl.Controls[1], typeof(RichTextBox));
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MarkdownPreviewHandlerControl__AddsInfoBarToFormIfHTMLImageTagIsPresent_WhenDoPreviewIsCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var markdownPreviewHandlerControl = new MarkdownPreviewHandlerControl())
|
||||
{
|
||||
// Act
|
||||
markdownPreviewHandlerControl.DoPreview<string>("HelperFiles/MarkdownWithHTMLImageTag.txt");
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(markdownPreviewHandlerControl.Controls.Count, 2);
|
||||
Assert.IsInstanceOfType(markdownPreviewHandlerControl.Controls[1], typeof(RichTextBox));
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MarkdownPreviewHandlerControl__DoesNotAddInfoBarToFormIfExternalImageLinkNotPresent_WhenDoPreviewIsCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var markdownPreviewHandlerControl = new MarkdownPreviewHandlerControl())
|
||||
{
|
||||
// Act
|
||||
markdownPreviewHandlerControl.DoPreview<string>("HelperFiles/MarkdownWithScript.txt");
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(markdownPreviewHandlerControl.Controls.Count, 1);
|
||||
Assert.IsInstanceOfType(markdownPreviewHandlerControl.Controls[0], typeof(WebBrowserExt));
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MarkdownPreviewHandlerControl__UpdatesWebBrowserSettings_WhenDoPreviewIsCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var markdownPreviewHandlerControl = new MarkdownPreviewHandlerControl())
|
||||
{
|
||||
// Act
|
||||
markdownPreviewHandlerControl.DoPreview<string>("HelperFiles/MarkdownWithExternalImage.txt");
|
||||
|
||||
// Assert
|
||||
Assert.IsInstanceOfType(markdownPreviewHandlerControl.Controls[0], typeof(WebBrowserExt));
|
||||
Assert.IsNotNull(((WebBrowser)markdownPreviewHandlerControl.Controls[0]).DocumentText);
|
||||
Assert.AreEqual(((WebBrowser)markdownPreviewHandlerControl.Controls[0]).Dock, DockStyle.Fill);
|
||||
Assert.AreEqual(((WebBrowser)markdownPreviewHandlerControl.Controls[0]).IsWebBrowserContextMenuEnabled, false);
|
||||
Assert.AreEqual(((WebBrowser)markdownPreviewHandlerControl.Controls[0]).ScriptErrorsSuppressed, true);
|
||||
Assert.AreEqual(((WebBrowser)markdownPreviewHandlerControl.Controls[0]).ScrollBarsEnabled, true);
|
||||
Assert.AreEqual(((WebBrowser)markdownPreviewHandlerControl.Controls[0]).AllowNavigation, false);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MarkdownPreviewHandlerControl__UpdateInfobarSettings_WhenDoPreviewIsCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var markdownPreviewHandlerControl = new MarkdownPreviewHandlerControl())
|
||||
{
|
||||
// Act
|
||||
markdownPreviewHandlerControl.DoPreview<string>("HelperFiles/MarkdownWithExternalImage.txt");
|
||||
|
||||
// Assert
|
||||
Assert.IsInstanceOfType(markdownPreviewHandlerControl.Controls[1], typeof(RichTextBox));
|
||||
Assert.IsNotNull(((RichTextBox)markdownPreviewHandlerControl.Controls[1]).Text);
|
||||
Assert.AreEqual(((RichTextBox)markdownPreviewHandlerControl.Controls[1]).Dock, DockStyle.Top);
|
||||
Assert.AreEqual(((RichTextBox)markdownPreviewHandlerControl.Controls[1]).BorderStyle, BorderStyle.None);
|
||||
Assert.AreEqual(((RichTextBox)markdownPreviewHandlerControl.Controls[1]).BackColor, Color.LightYellow);
|
||||
Assert.AreEqual(((RichTextBox)markdownPreviewHandlerControl.Controls[1]).Multiline, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,81 +1,81 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// This code was generated by a tool.
|
||||
// Runtime Version:4.0.30319.42000
|
||||
//
|
||||
// Changes to this file may cause incorrect behavior and will be lost if
|
||||
// the code is regenerated.
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
namespace SvgPreviewHandler {
|
||||
using System;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// A strongly-typed resource class, for looking up localized strings, etc.
|
||||
/// </summary>
|
||||
// This class was auto-generated by the StronglyTypedResourceBuilder
|
||||
// class via a tool like ResGen or Visual Studio.
|
||||
// To add or remove a member, edit your .ResX file then rerun ResGen
|
||||
// with the /str option, or rebuild your VS project.
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||
internal class Resource {
|
||||
|
||||
private static global::System.Resources.ResourceManager resourceMan;
|
||||
|
||||
private static global::System.Globalization.CultureInfo resourceCulture;
|
||||
|
||||
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
internal Resource() {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the cached ResourceManager instance used by this class.
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Resources.ResourceManager ResourceManager {
|
||||
get {
|
||||
if (object.ReferenceEquals(resourceMan, null)) {
|
||||
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("SvgPreviewHandler.Resource", typeof(Resource).Assembly);
|
||||
resourceMan = temp;
|
||||
}
|
||||
return resourceMan;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Overrides the current thread's CurrentUICulture property for all
|
||||
/// resource lookups using this strongly typed resource class.
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Globalization.CultureInfo Culture {
|
||||
get {
|
||||
return resourceCulture;
|
||||
}
|
||||
set {
|
||||
resourceCulture = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Some elements have been blocked to help prevent the sender from identifying your computer. Open this item to view all elements..
|
||||
/// </summary>
|
||||
internal static string BlockedElementInfoText {
|
||||
get {
|
||||
return ResourceManager.GetString("BlockedElementInfoText", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to The Svg could not be preview due to an internal error in Svg Preview Handler..
|
||||
/// </summary>
|
||||
internal static string SvgNotPreviewedError {
|
||||
get {
|
||||
return ResourceManager.GetString("SvgNotPreviewedError", resourceCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// This code was generated by a tool.
|
||||
// Runtime Version:4.0.30319.42000
|
||||
//
|
||||
// Changes to this file may cause incorrect behavior and will be lost if
|
||||
// the code is regenerated.
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
namespace SvgPreviewHandler {
|
||||
using System;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// A strongly-typed resource class, for looking up localized strings, etc.
|
||||
/// </summary>
|
||||
// This class was auto-generated by the StronglyTypedResourceBuilder
|
||||
// class via a tool like ResGen or Visual Studio.
|
||||
// To add or remove a member, edit your .ResX file then rerun ResGen
|
||||
// with the /str option, or rebuild your VS project.
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||
internal class Resource {
|
||||
|
||||
private static global::System.Resources.ResourceManager resourceMan;
|
||||
|
||||
private static global::System.Globalization.CultureInfo resourceCulture;
|
||||
|
||||
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
internal Resource() {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the cached ResourceManager instance used by this class.
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Resources.ResourceManager ResourceManager {
|
||||
get {
|
||||
if (object.ReferenceEquals(resourceMan, null)) {
|
||||
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("SvgPreviewHandler.Resource", typeof(Resource).Assembly);
|
||||
resourceMan = temp;
|
||||
}
|
||||
return resourceMan;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Overrides the current thread's CurrentUICulture property for all
|
||||
/// resource lookups using this strongly typed resource class.
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Globalization.CultureInfo Culture {
|
||||
get {
|
||||
return resourceCulture;
|
||||
}
|
||||
set {
|
||||
resourceCulture = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Some elements have been blocked to help prevent the sender from identifying your computer. Open this item to view all elements..
|
||||
/// </summary>
|
||||
internal static string BlockedElementInfoText {
|
||||
get {
|
||||
return ResourceManager.GetString("BlockedElementInfoText", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to The Svg could not be preview due to an internal error in Svg Preview Handler..
|
||||
/// </summary>
|
||||
internal static string SvgNotPreviewedError {
|
||||
get {
|
||||
return ResourceManager.GetString("SvgNotPreviewedError", resourceCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,144 +1,144 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
using System.Runtime.Versioning;
|
||||
using System.Windows.Forms;
|
||||
using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
using Common;
|
||||
using Common.Utilities;
|
||||
using Microsoft.PowerToys.Telemetry;
|
||||
using PreviewHandlerCommon;
|
||||
using SvgPreviewHandler.Telemetry.Events;
|
||||
|
||||
namespace SvgPreviewHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Implementation of Control for Svg Preview Handler.
|
||||
/// </summary>
|
||||
public class SvgPreviewControl : FormHandlerControl
|
||||
{
|
||||
/// <summary>
|
||||
/// Extended Browser Control to display Svg.
|
||||
/// </summary>
|
||||
private WebBrowserExt browser;
|
||||
|
||||
/// <summary>
|
||||
/// Text box to display the information about blocked elements from Svg.
|
||||
/// </summary>
|
||||
private RichTextBox textBox;
|
||||
|
||||
/// <summary>
|
||||
/// Represent if an text box info bar is added for showing message.
|
||||
/// </summary>
|
||||
private bool infoBarAdded = false;
|
||||
|
||||
/// <summary>
|
||||
/// Start the preview on the Control.
|
||||
/// </summary>
|
||||
/// <param name="dataSource">Stream reference to access source file.</param>
|
||||
public override void DoPreview<T>(T dataSource)
|
||||
{
|
||||
this.InvokeOnControlThread(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
this.infoBarAdded = false;
|
||||
string svgData = null;
|
||||
using (var stream = new StreamWrapper(dataSource as IStream))
|
||||
{
|
||||
using (var reader = new StreamReader(stream))
|
||||
{
|
||||
svgData = reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
|
||||
// Add a info bar on top of the Preview if any blocked element is present.
|
||||
if (SvgPreviewHandlerHelper.CheckBlockedElements(svgData))
|
||||
{
|
||||
this.infoBarAdded = true;
|
||||
this.AddTextBoxControl(Resource.BlockedElementInfoText);
|
||||
}
|
||||
|
||||
this.AddBrowserControl(svgData);
|
||||
this.Resize += this.FormResized;
|
||||
base.DoPreview(dataSource);
|
||||
PowerToysTelemetry.Log.WriteEvent(new SvgFilePreviewed());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PowerToysTelemetry.Log.WriteEvent(new SvgFilePreviewError { Message = ex.Message });
|
||||
this.Controls.Clear();
|
||||
this.infoBarAdded = true;
|
||||
this.AddTextBoxControl(Resource.SvgNotPreviewedError);
|
||||
base.DoPreview(dataSource);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when RichtextBox is resized.
|
||||
/// </summary>
|
||||
/// <param name="sender">Reference to resized control.</param>
|
||||
/// <param name="e">Provides data for the ContentsResized event.</param>
|
||||
private void RTBContentsResized(object sender, ContentsResizedEventArgs e)
|
||||
{
|
||||
var richTextBox = sender as RichTextBox;
|
||||
richTextBox.Height = e.NewRectangle.Height + 5;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when form is resized.
|
||||
/// </summary>
|
||||
/// <param name="sender">Reference to resized control.</param>
|
||||
/// <param name="e">Provides data for the resize event.</param>
|
||||
private void FormResized(object sender, EventArgs e)
|
||||
{
|
||||
if (this.infoBarAdded)
|
||||
{
|
||||
this.textBox.Width = this.Width;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a Web Browser Control to Control Collection.
|
||||
/// </summary>
|
||||
/// <param name="svgData">Svg to display on Browser Control.</param>
|
||||
private void AddBrowserControl(string svgData)
|
||||
{
|
||||
this.browser = new WebBrowserExt();
|
||||
this.browser.DocumentText = svgData;
|
||||
this.browser.Dock = DockStyle.Fill;
|
||||
this.browser.IsWebBrowserContextMenuEnabled = false;
|
||||
this.browser.ScriptErrorsSuppressed = true;
|
||||
this.browser.ScrollBarsEnabled = true;
|
||||
this.browser.AllowNavigation = false;
|
||||
this.Controls.Add(this.browser);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a Text Box in Controls for showing information about blocked elements.
|
||||
/// </summary>
|
||||
/// <param name="message">Message to be displayed in textbox.</param>
|
||||
private void AddTextBoxControl(string message)
|
||||
{
|
||||
this.textBox = new RichTextBox();
|
||||
this.textBox.Text = message;
|
||||
this.textBox.BackColor = Color.LightYellow;
|
||||
this.textBox.Multiline = true;
|
||||
this.textBox.Dock = DockStyle.Top;
|
||||
this.textBox.ReadOnly = true;
|
||||
this.textBox.ContentsResized += this.RTBContentsResized;
|
||||
this.textBox.ScrollBars = RichTextBoxScrollBars.None;
|
||||
this.textBox.BorderStyle = BorderStyle.None;
|
||||
this.Controls.Add(this.textBox);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
using System.Runtime.Versioning;
|
||||
using System.Windows.Forms;
|
||||
using System.Xml;
|
||||
using System.Xml.Linq;
|
||||
using Common;
|
||||
using Common.Utilities;
|
||||
using Microsoft.PowerToys.Telemetry;
|
||||
using PreviewHandlerCommon;
|
||||
using SvgPreviewHandler.Telemetry.Events;
|
||||
|
||||
namespace SvgPreviewHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Implementation of Control for Svg Preview Handler.
|
||||
/// </summary>
|
||||
public class SvgPreviewControl : FormHandlerControl
|
||||
{
|
||||
/// <summary>
|
||||
/// Extended Browser Control to display Svg.
|
||||
/// </summary>
|
||||
private WebBrowserExt browser;
|
||||
|
||||
/// <summary>
|
||||
/// Text box to display the information about blocked elements from Svg.
|
||||
/// </summary>
|
||||
private RichTextBox textBox;
|
||||
|
||||
/// <summary>
|
||||
/// Represent if an text box info bar is added for showing message.
|
||||
/// </summary>
|
||||
private bool infoBarAdded = false;
|
||||
|
||||
/// <summary>
|
||||
/// Start the preview on the Control.
|
||||
/// </summary>
|
||||
/// <param name="dataSource">Stream reference to access source file.</param>
|
||||
public override void DoPreview<T>(T dataSource)
|
||||
{
|
||||
this.InvokeOnControlThread(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
this.infoBarAdded = false;
|
||||
string svgData = null;
|
||||
using (var stream = new StreamWrapper(dataSource as IStream))
|
||||
{
|
||||
using (var reader = new StreamReader(stream))
|
||||
{
|
||||
svgData = reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
|
||||
// Add a info bar on top of the Preview if any blocked element is present.
|
||||
if (SvgPreviewHandlerHelper.CheckBlockedElements(svgData))
|
||||
{
|
||||
this.infoBarAdded = true;
|
||||
this.AddTextBoxControl(Resource.BlockedElementInfoText);
|
||||
}
|
||||
|
||||
this.AddBrowserControl(svgData);
|
||||
this.Resize += this.FormResized;
|
||||
base.DoPreview(dataSource);
|
||||
PowerToysTelemetry.Log.WriteEvent(new SvgFilePreviewed());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
PowerToysTelemetry.Log.WriteEvent(new SvgFilePreviewError { Message = ex.Message });
|
||||
this.Controls.Clear();
|
||||
this.infoBarAdded = true;
|
||||
this.AddTextBoxControl(Resource.SvgNotPreviewedError);
|
||||
base.DoPreview(dataSource);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when RichtextBox is resized.
|
||||
/// </summary>
|
||||
/// <param name="sender">Reference to resized control.</param>
|
||||
/// <param name="e">Provides data for the ContentsResized event.</param>
|
||||
private void RTBContentsResized(object sender, ContentsResizedEventArgs e)
|
||||
{
|
||||
var richTextBox = sender as RichTextBox;
|
||||
richTextBox.Height = e.NewRectangle.Height + 5;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when form is resized.
|
||||
/// </summary>
|
||||
/// <param name="sender">Reference to resized control.</param>
|
||||
/// <param name="e">Provides data for the resize event.</param>
|
||||
private void FormResized(object sender, EventArgs e)
|
||||
{
|
||||
if (this.infoBarAdded)
|
||||
{
|
||||
this.textBox.Width = this.Width;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a Web Browser Control to Control Collection.
|
||||
/// </summary>
|
||||
/// <param name="svgData">Svg to display on Browser Control.</param>
|
||||
private void AddBrowserControl(string svgData)
|
||||
{
|
||||
this.browser = new WebBrowserExt();
|
||||
this.browser.DocumentText = svgData;
|
||||
this.browser.Dock = DockStyle.Fill;
|
||||
this.browser.IsWebBrowserContextMenuEnabled = false;
|
||||
this.browser.ScriptErrorsSuppressed = true;
|
||||
this.browser.ScrollBarsEnabled = true;
|
||||
this.browser.AllowNavigation = false;
|
||||
this.Controls.Add(this.browser);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a Text Box in Controls for showing information about blocked elements.
|
||||
/// </summary>
|
||||
/// <param name="message">Message to be displayed in textbox.</param>
|
||||
private void AddTextBoxControl(string message)
|
||||
{
|
||||
this.textBox = new RichTextBox();
|
||||
this.textBox.Text = message;
|
||||
this.textBox.BackColor = Color.LightYellow;
|
||||
this.textBox.Multiline = true;
|
||||
this.textBox.Dock = DockStyle.Top;
|
||||
this.textBox.ReadOnly = true;
|
||||
this.textBox.ContentsResized += this.RTBContentsResized;
|
||||
this.textBox.ScrollBars = RichTextBoxScrollBars.None;
|
||||
this.textBox.BorderStyle = BorderStyle.None;
|
||||
this.Controls.Add(this.textBox);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,36 +1,36 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using Common;
|
||||
using Microsoft.PowerToys.Telemetry;
|
||||
|
||||
namespace SvgPreviewHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Extends <see cref="StreamBasedPreviewHandler"/> for Svg Preview Handler.
|
||||
/// </summary>
|
||||
[Guid("ddee2b8a-6807-48a6-bb20-2338174ff779")]
|
||||
[ClassInterface(ClassInterfaceType.None)]
|
||||
[ComVisible(true)]
|
||||
public class SvgPreviewHandler : StreamBasedPreviewHandler
|
||||
{
|
||||
private SvgPreviewControl svgPreviewControl;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void DoPreview()
|
||||
{
|
||||
this.svgPreviewControl.DoPreview(this.Stream);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override IPreviewHandlerControl CreatePreviewHandlerControl()
|
||||
{
|
||||
PowerToysTelemetry.Log.WriteEvent(new Telemetry.Events.SvgFileHandlerLoaded());
|
||||
this.svgPreviewControl = new SvgPreviewControl();
|
||||
return this.svgPreviewControl;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using Common;
|
||||
using Microsoft.PowerToys.Telemetry;
|
||||
|
||||
namespace SvgPreviewHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Extends <see cref="StreamBasedPreviewHandler"/> for Svg Preview Handler.
|
||||
/// </summary>
|
||||
[Guid("ddee2b8a-6807-48a6-bb20-2338174ff779")]
|
||||
[ClassInterface(ClassInterfaceType.None)]
|
||||
[ComVisible(true)]
|
||||
public class SvgPreviewHandler : StreamBasedPreviewHandler
|
||||
{
|
||||
private SvgPreviewControl svgPreviewControl;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void DoPreview()
|
||||
{
|
||||
this.svgPreviewControl.DoPreview(this.Stream);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override IPreviewHandlerControl CreatePreviewHandlerControl()
|
||||
{
|
||||
PowerToysTelemetry.Log.WriteEvent(new Telemetry.Events.SvgFileHandlerLoaded());
|
||||
this.svgPreviewControl = new SvgPreviewControl();
|
||||
return this.svgPreviewControl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Diagnostics.Tracing;
|
||||
using Microsoft.PowerToys.Telemetry;
|
||||
using Microsoft.PowerToys.Telemetry.Events;
|
||||
|
||||
namespace SvgPreviewHandler.Telemetry.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// A telemetry event to be raised when a svg file has been viewed in the preview pane.
|
||||
/// </summary>
|
||||
[EventData]
|
||||
public class SvgFileHandlerLoaded : EventBase, IEvent
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage;
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Diagnostics.Tracing;
|
||||
using Microsoft.PowerToys.Telemetry;
|
||||
using Microsoft.PowerToys.Telemetry.Events;
|
||||
|
||||
namespace SvgPreviewHandler.Telemetry.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// A telemetry event to be raised when a svg file has been viewed in the preview pane.
|
||||
/// </summary>
|
||||
[EventData]
|
||||
public class SvgFileHandlerLoaded : EventBase, IEvent
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Diagnostics.Tracing;
|
||||
using Microsoft.PowerToys.Telemetry;
|
||||
using Microsoft.PowerToys.Telemetry.Events;
|
||||
|
||||
namespace SvgPreviewHandler.Telemetry.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// A telemetry event to be raised when an error has occurred in the preview pane.
|
||||
/// </summary>
|
||||
[EventData]
|
||||
public class SvgFilePreviewError : EventBase, IEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the error message to log as part of the telemetry event.
|
||||
/// </summary>
|
||||
public string Message { get; set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServicePerformance;
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Diagnostics.Tracing;
|
||||
using Microsoft.PowerToys.Telemetry;
|
||||
using Microsoft.PowerToys.Telemetry.Events;
|
||||
|
||||
namespace SvgPreviewHandler.Telemetry.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// A telemetry event to be raised when an error has occurred in the preview pane.
|
||||
/// </summary>
|
||||
[EventData]
|
||||
public class SvgFilePreviewError : EventBase, IEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the error message to log as part of the telemetry event.
|
||||
/// </summary>
|
||||
public string Message { get; set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServicePerformance;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Diagnostics.Tracing;
|
||||
using Microsoft.PowerToys.Telemetry;
|
||||
using Microsoft.PowerToys.Telemetry.Events;
|
||||
|
||||
namespace SvgPreviewHandler.Telemetry.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// A telemetry event to be raised when a svg file has been viewed in the preview pane.
|
||||
/// </summary>
|
||||
[EventData]
|
||||
public class SvgFilePreviewed : EventBase, IEvent
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage;
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Diagnostics.Tracing;
|
||||
using Microsoft.PowerToys.Telemetry;
|
||||
using Microsoft.PowerToys.Telemetry.Events;
|
||||
|
||||
namespace SvgPreviewHandler.Telemetry.Events
|
||||
{
|
||||
/// <summary>
|
||||
/// A telemetry event to be raised when a svg file has been viewed in the preview pane.
|
||||
/// </summary>
|
||||
[EventData]
|
||||
public class SvgFilePreviewed : EventBase, IEvent
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +1,44 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using Common;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace UnitTests_PreviewHandlerCommon
|
||||
{
|
||||
[TestClass]
|
||||
public class FileBasedPreviewHandlerTests
|
||||
{
|
||||
public class TestFileBasedPreviewHandler : FileBasedPreviewHandler
|
||||
{
|
||||
public override void DoPreview()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected override IPreviewHandlerControl CreatePreviewHandlerControl()
|
||||
{
|
||||
return new Mock<IPreviewHandlerControl>().Object;
|
||||
}
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow(0U)]
|
||||
[DataRow(1U)]
|
||||
public void FileBasedPreviewHandler_ShouldSetFilePath_WhenInitializeCalled(uint grfMode)
|
||||
{
|
||||
// Arrange
|
||||
var fileBasedPreviewHandler = new TestFileBasedPreviewHandler();
|
||||
var filePath = "C:\\valid-path";
|
||||
|
||||
// Act
|
||||
fileBasedPreviewHandler.Initialize(filePath, grfMode);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(fileBasedPreviewHandler.FilePath, filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using Common;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace UnitTests_PreviewHandlerCommon
|
||||
{
|
||||
[TestClass]
|
||||
public class FileBasedPreviewHandlerTests
|
||||
{
|
||||
public class TestFileBasedPreviewHandler : FileBasedPreviewHandler
|
||||
{
|
||||
public override void DoPreview()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected override IPreviewHandlerControl CreatePreviewHandlerControl()
|
||||
{
|
||||
return new Mock<IPreviewHandlerControl>().Object;
|
||||
}
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow(0U)]
|
||||
[DataRow(1U)]
|
||||
public void FileBasedPreviewHandler_ShouldSetFilePath_WhenInitializeCalled(uint grfMode)
|
||||
{
|
||||
// Arrange
|
||||
var fileBasedPreviewHandler = new TestFileBasedPreviewHandler();
|
||||
var filePath = "C:\\valid-path";
|
||||
|
||||
// Act
|
||||
fileBasedPreviewHandler.Initialize(filePath, grfMode);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(fileBasedPreviewHandler.FilePath, filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,200 +1,200 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows.Forms;
|
||||
using Common;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace UnitTests_PreviewHandlerCommon
|
||||
{
|
||||
[TestClass]
|
||||
public class FormHandlerControlTests
|
||||
{
|
||||
private class TestFormControl : FormHandlerControl
|
||||
{
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldCreateHandle_OnInitialization()
|
||||
{
|
||||
// Arrange and act
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
// Assert
|
||||
Assert.IsTrue(testFormHandlerControl.IsHandleCreated);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldSetVisibleFalse_OnInitialization()
|
||||
{
|
||||
// Arrange and act
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
// Assert
|
||||
Assert.IsFalse(testFormHandlerControl.Visible);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldSetFormBorderStyle_OnInitialization()
|
||||
{
|
||||
// Arrange and act
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
// Assert
|
||||
Assert.AreEqual(FormBorderStyle.None, testFormHandlerControl.FormBorderStyle);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldReturnValidHandle_WhenGetHandleCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
// Act
|
||||
var handle = testFormHandlerControl.GetHandle();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(testFormHandlerControl.Handle, handle);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldSetBackgroundColor_WhenSetBackgroundColorCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
var color = Color.Navy;
|
||||
|
||||
// Act
|
||||
testFormHandlerControl.SetBackgroundColor(color);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(color, testFormHandlerControl.BackColor);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldSetFont_WhenSetFontCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
var font = new Font("Arial", 20);
|
||||
|
||||
// Act
|
||||
testFormHandlerControl.SetFont(font);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(font, testFormHandlerControl.Font);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldUpdateBounds_WhenSetRectCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
var bounds = new Rectangle(2, 2, 4, 4);
|
||||
|
||||
// Act
|
||||
testFormHandlerControl.SetRect(bounds);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(bounds, testFormHandlerControl.Bounds);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldSetTextColor_WhenSetTextColorCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
var color = Color.Navy;
|
||||
|
||||
// Act
|
||||
testFormHandlerControl.SetTextColor(color);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(color, testFormHandlerControl.ForeColor);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldClearAllControls_WhenUnloadCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
testFormHandlerControl.Controls.Add(new TextBox());
|
||||
testFormHandlerControl.Controls.Add(new RichTextBox());
|
||||
|
||||
// Act
|
||||
testFormHandlerControl.Unload();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(0, testFormHandlerControl.Controls.Count);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldSetVisibleFalse_WhenUnloadCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
// Act
|
||||
testFormHandlerControl.Unload();
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(testFormHandlerControl.Visible);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldSetVisibletrue_WhenDoPreviewCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
// Act
|
||||
testFormHandlerControl.DoPreview("valid-path");
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(testFormHandlerControl.Visible);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldSetParentHandle_WhenSetWindowCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
var parentFormWindow = new UserControl();
|
||||
var parentHwnd = parentFormWindow.Handle;
|
||||
var rect = new Rectangle(2, 2, 4, 4);
|
||||
|
||||
// Act
|
||||
testFormHandlerControl.SetWindow(parentHwnd, rect);
|
||||
var actualParentHwnd = GetAncestor(testFormHandlerControl.Handle, 1); // GA_PARENT 1
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(parentHwnd, actualParentHwnd);
|
||||
}
|
||||
}
|
||||
|
||||
// Gets the ancestor window: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getancestor
|
||||
[DllImport("user32.dll")]
|
||||
private static extern IntPtr GetAncestor(IntPtr hWnd, uint gaFlags);
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows.Forms;
|
||||
using Common;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace UnitTests_PreviewHandlerCommon
|
||||
{
|
||||
[TestClass]
|
||||
public class FormHandlerControlTests
|
||||
{
|
||||
private class TestFormControl : FormHandlerControl
|
||||
{
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldCreateHandle_OnInitialization()
|
||||
{
|
||||
// Arrange and act
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
// Assert
|
||||
Assert.IsTrue(testFormHandlerControl.IsHandleCreated);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldSetVisibleFalse_OnInitialization()
|
||||
{
|
||||
// Arrange and act
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
// Assert
|
||||
Assert.IsFalse(testFormHandlerControl.Visible);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldSetFormBorderStyle_OnInitialization()
|
||||
{
|
||||
// Arrange and act
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
// Assert
|
||||
Assert.AreEqual(FormBorderStyle.None, testFormHandlerControl.FormBorderStyle);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldReturnValidHandle_WhenGetHandleCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
// Act
|
||||
var handle = testFormHandlerControl.GetHandle();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(testFormHandlerControl.Handle, handle);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldSetBackgroundColor_WhenSetBackgroundColorCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
var color = Color.Navy;
|
||||
|
||||
// Act
|
||||
testFormHandlerControl.SetBackgroundColor(color);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(color, testFormHandlerControl.BackColor);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldSetFont_WhenSetFontCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
var font = new Font("Arial", 20);
|
||||
|
||||
// Act
|
||||
testFormHandlerControl.SetFont(font);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(font, testFormHandlerControl.Font);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldUpdateBounds_WhenSetRectCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
var bounds = new Rectangle(2, 2, 4, 4);
|
||||
|
||||
// Act
|
||||
testFormHandlerControl.SetRect(bounds);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(bounds, testFormHandlerControl.Bounds);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldSetTextColor_WhenSetTextColorCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
var color = Color.Navy;
|
||||
|
||||
// Act
|
||||
testFormHandlerControl.SetTextColor(color);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(color, testFormHandlerControl.ForeColor);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldClearAllControls_WhenUnloadCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
testFormHandlerControl.Controls.Add(new TextBox());
|
||||
testFormHandlerControl.Controls.Add(new RichTextBox());
|
||||
|
||||
// Act
|
||||
testFormHandlerControl.Unload();
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(0, testFormHandlerControl.Controls.Count);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldSetVisibleFalse_WhenUnloadCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
// Act
|
||||
testFormHandlerControl.Unload();
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(testFormHandlerControl.Visible);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldSetVisibletrue_WhenDoPreviewCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
// Act
|
||||
testFormHandlerControl.DoPreview("valid-path");
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(testFormHandlerControl.Visible);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void FormHandlerControl_ShouldSetParentHandle_WhenSetWindowCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var testFormHandlerControl = new TestFormControl())
|
||||
{
|
||||
var parentFormWindow = new UserControl();
|
||||
var parentHwnd = parentFormWindow.Handle;
|
||||
var rect = new Rectangle(2, 2, 4, 4);
|
||||
|
||||
// Act
|
||||
testFormHandlerControl.SetWindow(parentHwnd, rect);
|
||||
var actualParentHwnd = GetAncestor(testFormHandlerControl.Handle, 1); // GA_PARENT 1
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(parentHwnd, actualParentHwnd);
|
||||
}
|
||||
}
|
||||
|
||||
// Gets the ancestor window: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getancestor
|
||||
[DllImport("user32.dll")]
|
||||
private static extern IntPtr GetAncestor(IntPtr hWnd, uint gaFlags);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,394 +1,394 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Drawing;
|
||||
using Common;
|
||||
using Common.ComInterlop;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace UnitTests_PreviewHandlerCommon
|
||||
{
|
||||
[TestClass]
|
||||
public class PreviewHandlerBaseTests
|
||||
{
|
||||
private static IPreviewHandlerControl previewHandlerControl;
|
||||
|
||||
public class TestPreviewHandler : PreviewHandlerBase
|
||||
{
|
||||
public override void DoPreview()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected override IPreviewHandlerControl CreatePreviewHandlerControl()
|
||||
{
|
||||
return GetMockPreviewHandlerControl();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldCallPreviewControlSetWindow_WhenSetWindowCalled()
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
var handle = new IntPtr(5);
|
||||
var bounds = GetRectangle(2, 2, 4, 4);
|
||||
|
||||
var actualHandle = IntPtr.Zero;
|
||||
var actualBounds = Rectangle.Empty;
|
||||
mockPreviewControl
|
||||
.Setup(_ => _.SetWindow(It.IsAny<IntPtr>(), It.IsAny<Rectangle>()))
|
||||
.Callback<IntPtr, Rectangle>((hwnd, rect) =>
|
||||
{
|
||||
actualHandle = hwnd;
|
||||
actualBounds = rect;
|
||||
});
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
|
||||
// Act
|
||||
testPreviewHandler.SetWindow(handle, ref bounds);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(actualHandle, handle);
|
||||
Assert.AreEqual(actualBounds, bounds.ToRectangle());
|
||||
mockPreviewControl.Verify(_ => _.SetWindow(It.IsAny<IntPtr>(), It.IsAny<Rectangle>()), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldCallPreviewControlSetrect_WhenSetRectCalled()
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
var bounds = GetRectangle(2, 2, 4, 4);
|
||||
|
||||
var actualBounds = Rectangle.Empty;
|
||||
mockPreviewControl
|
||||
.Setup(_ => _.SetRect(It.IsAny<Rectangle>()))
|
||||
.Callback<Rectangle>((rect) =>
|
||||
{
|
||||
actualBounds = rect;
|
||||
});
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
|
||||
// Act
|
||||
testPreviewHandler.SetRect(ref bounds);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(actualBounds, bounds.ToRectangle());
|
||||
mockPreviewControl.Verify(_ => _.SetRect(It.IsAny<Rectangle>()), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldCallPreviewControlUnload_WhenUnloadCalled()
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
|
||||
// Act
|
||||
testPreviewHandler.Unload();
|
||||
|
||||
// Assert
|
||||
mockPreviewControl.Verify(_ => _.Unload(), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldCallPreviewControlSetBackgroundColor_WhenSetBackgroundColorCalled()
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
var color = default(COLORREF);
|
||||
|
||||
// Act
|
||||
testPreviewHandler.SetBackgroundColor(color);
|
||||
|
||||
// Assert
|
||||
mockPreviewControl.Verify(_ => _.SetBackgroundColor(It.Is<Color>(c => (c == color.Color))), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldCallPreviewControlSetTextColor_WhenSetTextColorCalled()
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
var color = default(COLORREF);
|
||||
|
||||
// Act
|
||||
testPreviewHandler.SetTextColor(color);
|
||||
|
||||
// Assert
|
||||
mockPreviewControl.Verify(_ => _.SetTextColor(It.Is<Color>(c => (c == color.Color))), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldCallPreviewControlSetFont_WhenSetFontCalled()
|
||||
{
|
||||
// Arrange
|
||||
Font actualFont = null;
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
mockPreviewControl
|
||||
.Setup(x => x.SetFont(It.IsAny<Font>()))
|
||||
.Callback<Font>((font) =>
|
||||
{
|
||||
actualFont = font;
|
||||
});
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
var logFont = GetLogFont();
|
||||
|
||||
// Act
|
||||
testPreviewHandler.SetFont(ref logFont);
|
||||
|
||||
// Assert
|
||||
mockPreviewControl.Verify(_ => _.SetFont(It.IsAny<Font>()), Times.Once);
|
||||
Assert.AreEqual(Font.FromLogFont(logFont), actualFont);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldCallPreviewControlSetFocus_WhenSetFocusCalled()
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
|
||||
// Act
|
||||
testPreviewHandler.SetFocus();
|
||||
|
||||
// Assert
|
||||
mockPreviewControl.Verify(_ => _.SetFocus(), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldSetHandleOnQueryFocus_WhenPreviewControlsReturnValidHandle()
|
||||
{
|
||||
// Arrange
|
||||
var hwnd = new IntPtr(5);
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
mockPreviewControl.Setup(x => x.QueryFocus(out hwnd));
|
||||
var actualHwnd = IntPtr.Zero;
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
|
||||
// Act
|
||||
testPreviewHandler.QueryFocus(out actualHwnd);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(actualHwnd, hwnd);
|
||||
mockPreviewControl.Verify(_ => _.QueryFocus(out hwnd), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldThrowOnQueryFocus_WhenPreviewControlsReturnNotValidHandle()
|
||||
{
|
||||
// Arrange
|
||||
var hwnd = IntPtr.Zero;
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
mockPreviewControl.Setup(x => x.QueryFocus(out hwnd));
|
||||
var actualHwnd = IntPtr.Zero;
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
Win32Exception exception = null;
|
||||
|
||||
// Act
|
||||
try
|
||||
{
|
||||
testPreviewHandler.QueryFocus(out actualHwnd);
|
||||
}
|
||||
catch (Win32Exception ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
mockPreviewControl.Verify(_ => _.QueryFocus(out hwnd), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldDirectKeyStrokesToIPreviewHandlerFrame_IfIPreviewHandlerFrameIsSet()
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
var mockPreviewHandlerFrame = new Mock<IPreviewHandlerFrame>();
|
||||
var msg = default(MSG);
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
testPreviewHandler.SetSite(mockPreviewHandlerFrame.Object);
|
||||
|
||||
// Act
|
||||
testPreviewHandler.TranslateAccelerator(ref msg);
|
||||
|
||||
// Assert
|
||||
mockPreviewHandlerFrame.Verify(_ => _.TranslateAccelerator(ref msg), Times.Once);
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow(0U)]
|
||||
[DataRow(1U)]
|
||||
public void PreviewHandlerBase_ShouldReturnIPreviewHandlerFrameResponse_IfIPreviewHandlerFrameIsSet(uint resultCode)
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
var mockPreviewHandlerFrame = new Mock<IPreviewHandlerFrame>();
|
||||
var msg = default(MSG);
|
||||
mockPreviewHandlerFrame
|
||||
.Setup(x => x.TranslateAccelerator(ref msg))
|
||||
.Returns(resultCode);
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
testPreviewHandler.SetSite(mockPreviewHandlerFrame.Object);
|
||||
|
||||
// Act
|
||||
var actualResultCode = testPreviewHandler.TranslateAccelerator(ref msg);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(resultCode, actualResultCode);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldReturnS_FALSE_IfIPreviewHandlerFrameIsNotSet()
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
var msg = default(MSG);
|
||||
uint sFalse = 1;
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
|
||||
// Act
|
||||
var result = testPreviewHandler.TranslateAccelerator(ref msg);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(result, sFalse);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldReturnPreviewControlHandle_IfGetWindowCalled()
|
||||
{
|
||||
// Arrange
|
||||
var previewControlHandle = new IntPtr(5);
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
mockPreviewControl.Setup(x => x.GetHandle())
|
||||
.Returns(previewControlHandle);
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
var hwndReceived = IntPtr.Zero;
|
||||
|
||||
// Act
|
||||
testPreviewHandler.GetWindow(out hwndReceived);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(hwndReceived, previewControlHandle);
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow(true)]
|
||||
[DataRow(false)]
|
||||
public void PreviewHandlerBase_ShouldThrowNotImplementedException_IfContextSensitiveHelpCalled(bool fEnterMode)
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
NotImplementedException exception = null;
|
||||
|
||||
// Act
|
||||
try
|
||||
{
|
||||
testPreviewHandler.ContextSensitiveHelp(fEnterMode);
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldReturnSite_WhenGetSiteCalled()
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
var site = new Mock<IPreviewHandlerFrame>().Object;
|
||||
testPreviewHandler.SetSite(site);
|
||||
object actualSite = null;
|
||||
var riid = Guid.Empty;
|
||||
|
||||
// Act
|
||||
testPreviewHandler.GetSite(ref riid, out actualSite);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(actualSite, site);
|
||||
}
|
||||
|
||||
private LOGFONT GetLogFont()
|
||||
{
|
||||
var logFont = new LOGFONT
|
||||
{
|
||||
LfHeight = 12,
|
||||
LfWidth = 0,
|
||||
LfEscapement = 0,
|
||||
LfWeight = 400, // FW_NORMAL
|
||||
LfItalic = Convert.ToByte(false),
|
||||
LfUnderline = Convert.ToByte(false),
|
||||
LfStrikeOut = Convert.ToByte(0),
|
||||
LfCharSet = Convert.ToByte(0), // ANSI_CHARSET
|
||||
LfOutPrecision = Convert.ToByte(0), // OUT_DEFAULT_PRECIS
|
||||
LfClipPrecision = Convert.ToByte(0),
|
||||
LfQuality = Convert.ToByte(0),
|
||||
LfPitchAndFamily = Convert.ToByte(0),
|
||||
LfFaceName = "valid-font",
|
||||
};
|
||||
|
||||
return logFont;
|
||||
}
|
||||
|
||||
private RECT GetRectangle(int left, int top, int right, int bottom)
|
||||
{
|
||||
var rect = new RECT
|
||||
{
|
||||
Left = left,
|
||||
Top = top,
|
||||
Right = right,
|
||||
Bottom = bottom,
|
||||
};
|
||||
|
||||
return rect;
|
||||
}
|
||||
|
||||
private static IPreviewHandlerControl GetMockPreviewHandlerControl()
|
||||
{
|
||||
return previewHandlerControl;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Drawing;
|
||||
using Common;
|
||||
using Common.ComInterlop;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace UnitTests_PreviewHandlerCommon
|
||||
{
|
||||
[TestClass]
|
||||
public class PreviewHandlerBaseTests
|
||||
{
|
||||
private static IPreviewHandlerControl previewHandlerControl;
|
||||
|
||||
public class TestPreviewHandler : PreviewHandlerBase
|
||||
{
|
||||
public override void DoPreview()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected override IPreviewHandlerControl CreatePreviewHandlerControl()
|
||||
{
|
||||
return GetMockPreviewHandlerControl();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldCallPreviewControlSetWindow_WhenSetWindowCalled()
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
var handle = new IntPtr(5);
|
||||
var bounds = GetRectangle(2, 2, 4, 4);
|
||||
|
||||
var actualHandle = IntPtr.Zero;
|
||||
var actualBounds = Rectangle.Empty;
|
||||
mockPreviewControl
|
||||
.Setup(_ => _.SetWindow(It.IsAny<IntPtr>(), It.IsAny<Rectangle>()))
|
||||
.Callback<IntPtr, Rectangle>((hwnd, rect) =>
|
||||
{
|
||||
actualHandle = hwnd;
|
||||
actualBounds = rect;
|
||||
});
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
|
||||
// Act
|
||||
testPreviewHandler.SetWindow(handle, ref bounds);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(actualHandle, handle);
|
||||
Assert.AreEqual(actualBounds, bounds.ToRectangle());
|
||||
mockPreviewControl.Verify(_ => _.SetWindow(It.IsAny<IntPtr>(), It.IsAny<Rectangle>()), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldCallPreviewControlSetrect_WhenSetRectCalled()
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
var bounds = GetRectangle(2, 2, 4, 4);
|
||||
|
||||
var actualBounds = Rectangle.Empty;
|
||||
mockPreviewControl
|
||||
.Setup(_ => _.SetRect(It.IsAny<Rectangle>()))
|
||||
.Callback<Rectangle>((rect) =>
|
||||
{
|
||||
actualBounds = rect;
|
||||
});
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
|
||||
// Act
|
||||
testPreviewHandler.SetRect(ref bounds);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(actualBounds, bounds.ToRectangle());
|
||||
mockPreviewControl.Verify(_ => _.SetRect(It.IsAny<Rectangle>()), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldCallPreviewControlUnload_WhenUnloadCalled()
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
|
||||
// Act
|
||||
testPreviewHandler.Unload();
|
||||
|
||||
// Assert
|
||||
mockPreviewControl.Verify(_ => _.Unload(), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldCallPreviewControlSetBackgroundColor_WhenSetBackgroundColorCalled()
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
var color = default(COLORREF);
|
||||
|
||||
// Act
|
||||
testPreviewHandler.SetBackgroundColor(color);
|
||||
|
||||
// Assert
|
||||
mockPreviewControl.Verify(_ => _.SetBackgroundColor(It.Is<Color>(c => (c == color.Color))), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldCallPreviewControlSetTextColor_WhenSetTextColorCalled()
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
var color = default(COLORREF);
|
||||
|
||||
// Act
|
||||
testPreviewHandler.SetTextColor(color);
|
||||
|
||||
// Assert
|
||||
mockPreviewControl.Verify(_ => _.SetTextColor(It.Is<Color>(c => (c == color.Color))), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldCallPreviewControlSetFont_WhenSetFontCalled()
|
||||
{
|
||||
// Arrange
|
||||
Font actualFont = null;
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
mockPreviewControl
|
||||
.Setup(x => x.SetFont(It.IsAny<Font>()))
|
||||
.Callback<Font>((font) =>
|
||||
{
|
||||
actualFont = font;
|
||||
});
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
var logFont = GetLogFont();
|
||||
|
||||
// Act
|
||||
testPreviewHandler.SetFont(ref logFont);
|
||||
|
||||
// Assert
|
||||
mockPreviewControl.Verify(_ => _.SetFont(It.IsAny<Font>()), Times.Once);
|
||||
Assert.AreEqual(Font.FromLogFont(logFont), actualFont);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldCallPreviewControlSetFocus_WhenSetFocusCalled()
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
|
||||
// Act
|
||||
testPreviewHandler.SetFocus();
|
||||
|
||||
// Assert
|
||||
mockPreviewControl.Verify(_ => _.SetFocus(), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldSetHandleOnQueryFocus_WhenPreviewControlsReturnValidHandle()
|
||||
{
|
||||
// Arrange
|
||||
var hwnd = new IntPtr(5);
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
mockPreviewControl.Setup(x => x.QueryFocus(out hwnd));
|
||||
var actualHwnd = IntPtr.Zero;
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
|
||||
// Act
|
||||
testPreviewHandler.QueryFocus(out actualHwnd);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(actualHwnd, hwnd);
|
||||
mockPreviewControl.Verify(_ => _.QueryFocus(out hwnd), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldThrowOnQueryFocus_WhenPreviewControlsReturnNotValidHandle()
|
||||
{
|
||||
// Arrange
|
||||
var hwnd = IntPtr.Zero;
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
mockPreviewControl.Setup(x => x.QueryFocus(out hwnd));
|
||||
var actualHwnd = IntPtr.Zero;
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
Win32Exception exception = null;
|
||||
|
||||
// Act
|
||||
try
|
||||
{
|
||||
testPreviewHandler.QueryFocus(out actualHwnd);
|
||||
}
|
||||
catch (Win32Exception ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
mockPreviewControl.Verify(_ => _.QueryFocus(out hwnd), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldDirectKeyStrokesToIPreviewHandlerFrame_IfIPreviewHandlerFrameIsSet()
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
var mockPreviewHandlerFrame = new Mock<IPreviewHandlerFrame>();
|
||||
var msg = default(MSG);
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
testPreviewHandler.SetSite(mockPreviewHandlerFrame.Object);
|
||||
|
||||
// Act
|
||||
testPreviewHandler.TranslateAccelerator(ref msg);
|
||||
|
||||
// Assert
|
||||
mockPreviewHandlerFrame.Verify(_ => _.TranslateAccelerator(ref msg), Times.Once);
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow(0U)]
|
||||
[DataRow(1U)]
|
||||
public void PreviewHandlerBase_ShouldReturnIPreviewHandlerFrameResponse_IfIPreviewHandlerFrameIsSet(uint resultCode)
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
var mockPreviewHandlerFrame = new Mock<IPreviewHandlerFrame>();
|
||||
var msg = default(MSG);
|
||||
mockPreviewHandlerFrame
|
||||
.Setup(x => x.TranslateAccelerator(ref msg))
|
||||
.Returns(resultCode);
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
testPreviewHandler.SetSite(mockPreviewHandlerFrame.Object);
|
||||
|
||||
// Act
|
||||
var actualResultCode = testPreviewHandler.TranslateAccelerator(ref msg);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(resultCode, actualResultCode);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldReturnS_FALSE_IfIPreviewHandlerFrameIsNotSet()
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
var msg = default(MSG);
|
||||
uint sFalse = 1;
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
|
||||
// Act
|
||||
var result = testPreviewHandler.TranslateAccelerator(ref msg);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(result, sFalse);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldReturnPreviewControlHandle_IfGetWindowCalled()
|
||||
{
|
||||
// Arrange
|
||||
var previewControlHandle = new IntPtr(5);
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
mockPreviewControl.Setup(x => x.GetHandle())
|
||||
.Returns(previewControlHandle);
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
var hwndReceived = IntPtr.Zero;
|
||||
|
||||
// Act
|
||||
testPreviewHandler.GetWindow(out hwndReceived);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(hwndReceived, previewControlHandle);
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow(true)]
|
||||
[DataRow(false)]
|
||||
public void PreviewHandlerBase_ShouldThrowNotImplementedException_IfContextSensitiveHelpCalled(bool fEnterMode)
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
NotImplementedException exception = null;
|
||||
|
||||
// Act
|
||||
try
|
||||
{
|
||||
testPreviewHandler.ContextSensitiveHelp(fEnterMode);
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void PreviewHandlerBase_ShouldReturnSite_WhenGetSiteCalled()
|
||||
{
|
||||
// Arrange
|
||||
var mockPreviewControl = new Mock<IPreviewHandlerControl>();
|
||||
|
||||
previewHandlerControl = mockPreviewControl.Object;
|
||||
var testPreviewHandler = new TestPreviewHandler();
|
||||
var site = new Mock<IPreviewHandlerFrame>().Object;
|
||||
testPreviewHandler.SetSite(site);
|
||||
object actualSite = null;
|
||||
var riid = Guid.Empty;
|
||||
|
||||
// Act
|
||||
testPreviewHandler.GetSite(ref riid, out actualSite);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(actualSite, site);
|
||||
}
|
||||
|
||||
private LOGFONT GetLogFont()
|
||||
{
|
||||
var logFont = new LOGFONT
|
||||
{
|
||||
LfHeight = 12,
|
||||
LfWidth = 0,
|
||||
LfEscapement = 0,
|
||||
LfWeight = 400, // FW_NORMAL
|
||||
LfItalic = Convert.ToByte(false),
|
||||
LfUnderline = Convert.ToByte(false),
|
||||
LfStrikeOut = Convert.ToByte(0),
|
||||
LfCharSet = Convert.ToByte(0), // ANSI_CHARSET
|
||||
LfOutPrecision = Convert.ToByte(0), // OUT_DEFAULT_PRECIS
|
||||
LfClipPrecision = Convert.ToByte(0),
|
||||
LfQuality = Convert.ToByte(0),
|
||||
LfPitchAndFamily = Convert.ToByte(0),
|
||||
LfFaceName = "valid-font",
|
||||
};
|
||||
|
||||
return logFont;
|
||||
}
|
||||
|
||||
private RECT GetRectangle(int left, int top, int right, int bottom)
|
||||
{
|
||||
var rect = new RECT
|
||||
{
|
||||
Left = left,
|
||||
Top = top,
|
||||
Right = right,
|
||||
Bottom = bottom,
|
||||
};
|
||||
|
||||
return rect;
|
||||
}
|
||||
|
||||
private static IPreviewHandlerControl GetMockPreviewHandlerControl()
|
||||
{
|
||||
return previewHandlerControl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +1,45 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
using Common;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace UnitTests_PreviewHandlerCommon
|
||||
{
|
||||
[TestClass]
|
||||
public class StreamBasedPreviewHandlerTests
|
||||
{
|
||||
public class TestStreamBasedPreviewHandler : StreamBasedPreviewHandler
|
||||
{
|
||||
public override void DoPreview()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected override IPreviewHandlerControl CreatePreviewHandlerControl()
|
||||
{
|
||||
return new Mock<IPreviewHandlerControl>().Object;
|
||||
}
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow(0U)]
|
||||
[DataRow(1U)]
|
||||
public void StreamBasedPreviewHandler_ShouldSetStream_WhenInitializeCalled(uint grfMode)
|
||||
{
|
||||
// Arrange
|
||||
var streamBasedPreviewHandler = new TestStreamBasedPreviewHandler();
|
||||
var stream = new Mock<IStream>().Object;
|
||||
|
||||
// Act
|
||||
streamBasedPreviewHandler.Initialize(stream, grfMode);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(streamBasedPreviewHandler.Stream, stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
using Common;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace UnitTests_PreviewHandlerCommon
|
||||
{
|
||||
[TestClass]
|
||||
public class StreamBasedPreviewHandlerTests
|
||||
{
|
||||
public class TestStreamBasedPreviewHandler : StreamBasedPreviewHandler
|
||||
{
|
||||
public override void DoPreview()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected override IPreviewHandlerControl CreatePreviewHandlerControl()
|
||||
{
|
||||
return new Mock<IPreviewHandlerControl>().Object;
|
||||
}
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow(0U)]
|
||||
[DataRow(1U)]
|
||||
public void StreamBasedPreviewHandler_ShouldSetStream_WhenInitializeCalled(uint grfMode)
|
||||
{
|
||||
// Arrange
|
||||
var streamBasedPreviewHandler = new TestStreamBasedPreviewHandler();
|
||||
var stream = new Mock<IStream>().Object;
|
||||
|
||||
// Act
|
||||
streamBasedPreviewHandler.Initialize(stream, grfMode);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(streamBasedPreviewHandler.Stream, stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,331 +1,331 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
using Common.Utilities;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace UnitTests_PreviewHandlerCommon
|
||||
{
|
||||
[TestClass]
|
||||
public class StreamWrapperTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldThrow_IfInitializeWithNullStream()
|
||||
{
|
||||
// Arrange
|
||||
IStream stream = null;
|
||||
ArgumentNullException exception = null;
|
||||
|
||||
// Act
|
||||
try
|
||||
{
|
||||
var streamWrapper = new StreamWrapper(stream);
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldReturnCanReadTrue()
|
||||
{
|
||||
// Arrange
|
||||
var streamMock = new Mock<IStream>();
|
||||
|
||||
// Act
|
||||
var streamWrapper = new StreamWrapper(streamMock.Object);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(streamWrapper.CanRead, true);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldReturnCanSeekTrue()
|
||||
{
|
||||
// Arrange
|
||||
var streamMock = new Mock<IStream>();
|
||||
|
||||
// Act
|
||||
var streamWrapper = new StreamWrapper(streamMock.Object);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(streamWrapper.CanSeek, true);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldReturnCanWriteFalse()
|
||||
{
|
||||
// Arrange
|
||||
var streamMock = new Mock<IStream>();
|
||||
|
||||
// Act
|
||||
var streamWrapper = new StreamWrapper(streamMock.Object);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(streamWrapper.CanWrite, false);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldReturnValidLength()
|
||||
{
|
||||
// Arrange
|
||||
long streamLength = 5;
|
||||
var stremMock = new Mock<IStream>();
|
||||
var stat = new System.Runtime.InteropServices.ComTypes.STATSTG
|
||||
{
|
||||
cbSize = streamLength,
|
||||
};
|
||||
|
||||
stremMock
|
||||
.Setup(x => x.Stat(out stat, It.IsAny<int>()));
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object);
|
||||
|
||||
// Act
|
||||
var actualLength = streamWrapper.Length;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(actualLength, streamLength);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldReturnValidPosition()
|
||||
{
|
||||
// Arrange
|
||||
int expectedDwOrigin = 1; // STREAM_SEEK_CUR
|
||||
long expectedOffset = 0;
|
||||
long currPosition = 5;
|
||||
var stremMock = new Mock<IStream>();
|
||||
|
||||
stremMock
|
||||
.Setup(x => x.Seek(It.IsAny<long>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
|
||||
.Callback<long, int, IntPtr>((dlibMove, dwOrigin, plibNewPosition) =>
|
||||
{
|
||||
Marshal.WriteInt64(plibNewPosition, currPosition);
|
||||
});
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object);
|
||||
|
||||
// Act
|
||||
var actualPosition = streamWrapper.Position;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(actualPosition, currPosition);
|
||||
stremMock.Verify(_ => _.Seek(It.Is<long>(offset => offset == expectedOffset), It.Is<int>(dworigin => dworigin == expectedDwOrigin), It.IsAny<IntPtr>()), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldCallIStreamSeek_WhenSetPosition()
|
||||
{
|
||||
// Arrange
|
||||
long positionToSet = 5;
|
||||
int expectedDwOrigin = 0; // STREAM_SEEK_SET
|
||||
var stremMock = new Mock<IStream>();
|
||||
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object)
|
||||
{
|
||||
// Act
|
||||
Position = positionToSet,
|
||||
};
|
||||
|
||||
// Assert
|
||||
stremMock.Verify(_ => _.Seek(It.Is<long>(offset => offset == positionToSet), It.Is<int>(dworigin => dworigin == expectedDwOrigin), It.IsAny<IntPtr>()), Times.Once);
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow(0L, SeekOrigin.Begin)]
|
||||
[DataRow(5L, SeekOrigin.Begin)]
|
||||
[DataRow(0L, SeekOrigin.Current)]
|
||||
[DataRow(5L, SeekOrigin.Current)]
|
||||
[DataRow(0L, SeekOrigin.End)]
|
||||
[DataRow(5L, SeekOrigin.End)]
|
||||
public void StreamWrapper_ShouldCallIStreamSeekWithValidArguments_WhenSeekCalled(long offset, SeekOrigin origin)
|
||||
{
|
||||
// Arrange
|
||||
int expectedDwOrigin = 0;
|
||||
switch (origin)
|
||||
{
|
||||
case SeekOrigin.Begin:
|
||||
expectedDwOrigin = 0;
|
||||
break;
|
||||
|
||||
case SeekOrigin.Current:
|
||||
expectedDwOrigin = 1;
|
||||
break;
|
||||
|
||||
case SeekOrigin.End:
|
||||
expectedDwOrigin = 2;
|
||||
break;
|
||||
}
|
||||
|
||||
var stremMock = new Mock<IStream>();
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object);
|
||||
|
||||
// Act
|
||||
streamWrapper.Seek(offset, origin);
|
||||
|
||||
// Assert
|
||||
stremMock.Verify(_ => _.Seek(It.Is<long>(actualOffset => actualOffset == offset), It.Is<int>(actualDwOrigin => actualDwOrigin == expectedDwOrigin), It.IsAny<IntPtr>()), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldReturnValidPosition_WhenSeekCalled()
|
||||
{
|
||||
// Arrange
|
||||
long position = 5;
|
||||
var stremMock = new Mock<IStream>();
|
||||
|
||||
stremMock
|
||||
.Setup(x => x.Seek(It.IsAny<long>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
|
||||
.Callback<long, int, IntPtr>((dlibMove, dwOrigin, plibNewPosition) =>
|
||||
{
|
||||
Marshal.WriteInt64(plibNewPosition, position);
|
||||
});
|
||||
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object);
|
||||
|
||||
// Act
|
||||
var actualPosition = streamWrapper.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(position, actualPosition);
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow(10, -1, 5)]
|
||||
[DataRow(10, 0, -5)]
|
||||
[DataRow(10, 0, 11)]
|
||||
[DataRow(10, 5, 6)]
|
||||
public void StreamWrapper_ShouldThrow_WhenReadCalledWithOutOfRangeArguments(int bufferLength, int offSet, int bytesToRead)
|
||||
{
|
||||
// Arrange
|
||||
var buffer = new byte[bufferLength];
|
||||
var stremMock = new Mock<IStream>();
|
||||
ArgumentOutOfRangeException exception = null;
|
||||
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object);
|
||||
|
||||
// Act
|
||||
try
|
||||
{
|
||||
streamWrapper.Read(buffer, offSet, bytesToRead);
|
||||
}
|
||||
catch (ArgumentOutOfRangeException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow(5, 0)]
|
||||
[DataRow(5, 5)]
|
||||
[DataRow(0, 5)]
|
||||
public void StreamWrapper_ShouldSetValidBuffer_WhenReadCalled(int count, int offset)
|
||||
{
|
||||
// Arrange
|
||||
var inputBuffer = new byte[1024];
|
||||
var streamBytes = new byte[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
streamBytes[i] = (byte)i;
|
||||
}
|
||||
|
||||
var stremMock = new Mock<IStream>();
|
||||
|
||||
stremMock
|
||||
.Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
|
||||
.Callback<byte[], int, IntPtr>((buffer, countToRead, bytesReadPtr) =>
|
||||
{
|
||||
Array.Copy(streamBytes, 0, buffer, 0, streamBytes.Length);
|
||||
Marshal.WriteInt32(bytesReadPtr, count);
|
||||
});
|
||||
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object);
|
||||
|
||||
// Act
|
||||
var bytesRead = streamWrapper.Read(inputBuffer, offset, count);
|
||||
|
||||
// Assert
|
||||
CollectionAssert.AreEqual(streamBytes, inputBuffer.Skip(offset).Take(count).ToArray());
|
||||
Assert.AreEqual(count, bytesRead);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldThrowNotImplementedException_WhenFlushCalled()
|
||||
{
|
||||
// Arrange
|
||||
var stremMock = new Mock<IStream>();
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object);
|
||||
NotImplementedException exception = null;
|
||||
|
||||
// Act
|
||||
try
|
||||
{
|
||||
streamWrapper.Flush();
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldThrowNotImplementedException_WhenSetLengthCalled()
|
||||
{
|
||||
// Arrange
|
||||
var stremMock = new Mock<IStream>();
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object);
|
||||
NotImplementedException exception = null;
|
||||
|
||||
// Act
|
||||
try
|
||||
{
|
||||
streamWrapper.SetLength(5);
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldThrowNotImplementedException_WhenWriteCalled()
|
||||
{
|
||||
// Arrange
|
||||
var stremMock = new Mock<IStream>();
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object);
|
||||
NotImplementedException exception = null;
|
||||
|
||||
// Act
|
||||
try
|
||||
{
|
||||
streamWrapper.Write(new byte[5], 0, 0);
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
using Common.Utilities;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
|
||||
namespace UnitTests_PreviewHandlerCommon
|
||||
{
|
||||
[TestClass]
|
||||
public class StreamWrapperTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldThrow_IfInitializeWithNullStream()
|
||||
{
|
||||
// Arrange
|
||||
IStream stream = null;
|
||||
ArgumentNullException exception = null;
|
||||
|
||||
// Act
|
||||
try
|
||||
{
|
||||
var streamWrapper = new StreamWrapper(stream);
|
||||
}
|
||||
catch (ArgumentNullException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldReturnCanReadTrue()
|
||||
{
|
||||
// Arrange
|
||||
var streamMock = new Mock<IStream>();
|
||||
|
||||
// Act
|
||||
var streamWrapper = new StreamWrapper(streamMock.Object);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(streamWrapper.CanRead, true);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldReturnCanSeekTrue()
|
||||
{
|
||||
// Arrange
|
||||
var streamMock = new Mock<IStream>();
|
||||
|
||||
// Act
|
||||
var streamWrapper = new StreamWrapper(streamMock.Object);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(streamWrapper.CanSeek, true);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldReturnCanWriteFalse()
|
||||
{
|
||||
// Arrange
|
||||
var streamMock = new Mock<IStream>();
|
||||
|
||||
// Act
|
||||
var streamWrapper = new StreamWrapper(streamMock.Object);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(streamWrapper.CanWrite, false);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldReturnValidLength()
|
||||
{
|
||||
// Arrange
|
||||
long streamLength = 5;
|
||||
var stremMock = new Mock<IStream>();
|
||||
var stat = new System.Runtime.InteropServices.ComTypes.STATSTG
|
||||
{
|
||||
cbSize = streamLength,
|
||||
};
|
||||
|
||||
stremMock
|
||||
.Setup(x => x.Stat(out stat, It.IsAny<int>()));
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object);
|
||||
|
||||
// Act
|
||||
var actualLength = streamWrapper.Length;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(actualLength, streamLength);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldReturnValidPosition()
|
||||
{
|
||||
// Arrange
|
||||
int expectedDwOrigin = 1; // STREAM_SEEK_CUR
|
||||
long expectedOffset = 0;
|
||||
long currPosition = 5;
|
||||
var stremMock = new Mock<IStream>();
|
||||
|
||||
stremMock
|
||||
.Setup(x => x.Seek(It.IsAny<long>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
|
||||
.Callback<long, int, IntPtr>((dlibMove, dwOrigin, plibNewPosition) =>
|
||||
{
|
||||
Marshal.WriteInt64(plibNewPosition, currPosition);
|
||||
});
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object);
|
||||
|
||||
// Act
|
||||
var actualPosition = streamWrapper.Position;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(actualPosition, currPosition);
|
||||
stremMock.Verify(_ => _.Seek(It.Is<long>(offset => offset == expectedOffset), It.Is<int>(dworigin => dworigin == expectedDwOrigin), It.IsAny<IntPtr>()), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldCallIStreamSeek_WhenSetPosition()
|
||||
{
|
||||
// Arrange
|
||||
long positionToSet = 5;
|
||||
int expectedDwOrigin = 0; // STREAM_SEEK_SET
|
||||
var stremMock = new Mock<IStream>();
|
||||
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object)
|
||||
{
|
||||
// Act
|
||||
Position = positionToSet,
|
||||
};
|
||||
|
||||
// Assert
|
||||
stremMock.Verify(_ => _.Seek(It.Is<long>(offset => offset == positionToSet), It.Is<int>(dworigin => dworigin == expectedDwOrigin), It.IsAny<IntPtr>()), Times.Once);
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow(0L, SeekOrigin.Begin)]
|
||||
[DataRow(5L, SeekOrigin.Begin)]
|
||||
[DataRow(0L, SeekOrigin.Current)]
|
||||
[DataRow(5L, SeekOrigin.Current)]
|
||||
[DataRow(0L, SeekOrigin.End)]
|
||||
[DataRow(5L, SeekOrigin.End)]
|
||||
public void StreamWrapper_ShouldCallIStreamSeekWithValidArguments_WhenSeekCalled(long offset, SeekOrigin origin)
|
||||
{
|
||||
// Arrange
|
||||
int expectedDwOrigin = 0;
|
||||
switch (origin)
|
||||
{
|
||||
case SeekOrigin.Begin:
|
||||
expectedDwOrigin = 0;
|
||||
break;
|
||||
|
||||
case SeekOrigin.Current:
|
||||
expectedDwOrigin = 1;
|
||||
break;
|
||||
|
||||
case SeekOrigin.End:
|
||||
expectedDwOrigin = 2;
|
||||
break;
|
||||
}
|
||||
|
||||
var stremMock = new Mock<IStream>();
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object);
|
||||
|
||||
// Act
|
||||
streamWrapper.Seek(offset, origin);
|
||||
|
||||
// Assert
|
||||
stremMock.Verify(_ => _.Seek(It.Is<long>(actualOffset => actualOffset == offset), It.Is<int>(actualDwOrigin => actualDwOrigin == expectedDwOrigin), It.IsAny<IntPtr>()), Times.Once);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldReturnValidPosition_WhenSeekCalled()
|
||||
{
|
||||
// Arrange
|
||||
long position = 5;
|
||||
var stremMock = new Mock<IStream>();
|
||||
|
||||
stremMock
|
||||
.Setup(x => x.Seek(It.IsAny<long>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
|
||||
.Callback<long, int, IntPtr>((dlibMove, dwOrigin, plibNewPosition) =>
|
||||
{
|
||||
Marshal.WriteInt64(plibNewPosition, position);
|
||||
});
|
||||
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object);
|
||||
|
||||
// Act
|
||||
var actualPosition = streamWrapper.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(position, actualPosition);
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow(10, -1, 5)]
|
||||
[DataRow(10, 0, -5)]
|
||||
[DataRow(10, 0, 11)]
|
||||
[DataRow(10, 5, 6)]
|
||||
public void StreamWrapper_ShouldThrow_WhenReadCalledWithOutOfRangeArguments(int bufferLength, int offSet, int bytesToRead)
|
||||
{
|
||||
// Arrange
|
||||
var buffer = new byte[bufferLength];
|
||||
var stremMock = new Mock<IStream>();
|
||||
ArgumentOutOfRangeException exception = null;
|
||||
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object);
|
||||
|
||||
// Act
|
||||
try
|
||||
{
|
||||
streamWrapper.Read(buffer, offSet, bytesToRead);
|
||||
}
|
||||
catch (ArgumentOutOfRangeException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow(5, 0)]
|
||||
[DataRow(5, 5)]
|
||||
[DataRow(0, 5)]
|
||||
public void StreamWrapper_ShouldSetValidBuffer_WhenReadCalled(int count, int offset)
|
||||
{
|
||||
// Arrange
|
||||
var inputBuffer = new byte[1024];
|
||||
var streamBytes = new byte[count];
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
streamBytes[i] = (byte)i;
|
||||
}
|
||||
|
||||
var stremMock = new Mock<IStream>();
|
||||
|
||||
stremMock
|
||||
.Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
|
||||
.Callback<byte[], int, IntPtr>((buffer, countToRead, bytesReadPtr) =>
|
||||
{
|
||||
Array.Copy(streamBytes, 0, buffer, 0, streamBytes.Length);
|
||||
Marshal.WriteInt32(bytesReadPtr, count);
|
||||
});
|
||||
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object);
|
||||
|
||||
// Act
|
||||
var bytesRead = streamWrapper.Read(inputBuffer, offset, count);
|
||||
|
||||
// Assert
|
||||
CollectionAssert.AreEqual(streamBytes, inputBuffer.Skip(offset).Take(count).ToArray());
|
||||
Assert.AreEqual(count, bytesRead);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldThrowNotImplementedException_WhenFlushCalled()
|
||||
{
|
||||
// Arrange
|
||||
var stremMock = new Mock<IStream>();
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object);
|
||||
NotImplementedException exception = null;
|
||||
|
||||
// Act
|
||||
try
|
||||
{
|
||||
streamWrapper.Flush();
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldThrowNotImplementedException_WhenSetLengthCalled()
|
||||
{
|
||||
// Arrange
|
||||
var stremMock = new Mock<IStream>();
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object);
|
||||
NotImplementedException exception = null;
|
||||
|
||||
// Act
|
||||
try
|
||||
{
|
||||
streamWrapper.SetLength(5);
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void StreamWrapper_ShouldThrowNotImplementedException_WhenWriteCalled()
|
||||
{
|
||||
// Arrange
|
||||
var stremMock = new Mock<IStream>();
|
||||
var streamWrapper = new StreamWrapper(stremMock.Object);
|
||||
NotImplementedException exception = null;
|
||||
|
||||
// Act
|
||||
try
|
||||
{
|
||||
streamWrapper.Write(new byte[5], 0, 0);
|
||||
}
|
||||
catch (NotImplementedException ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,268 +1,268 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using PreviewHandlerCommon;
|
||||
using SvgPreviewHandler;
|
||||
|
||||
namespace UnitTests_SvgPreviewHandler
|
||||
{
|
||||
[TestClass]
|
||||
public class SvgPreviewControlTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldAddExtendedBrowserControl_WhenDoPreviewCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream("<svg></svg>"));
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(svgPreviewControl.Controls.Count, 1);
|
||||
Assert.IsInstanceOfType(svgPreviewControl.Controls[0], typeof(WebBrowserExt));
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldSetDocumentStream_WhenDoPreviewCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream("<svg></svg>"));
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(((WebBrowser)svgPreviewControl.Controls[0]).DocumentStream);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldDisableWebBrowserContextMenu_WhenDoPreviewCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream("<svg></svg>"));
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(((WebBrowser)svgPreviewControl.Controls[0]).IsWebBrowserContextMenuEnabled, false);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldFillDockForWebBrowser_WhenDoPreviewCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream("<svg></svg>"));
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(((WebBrowser)svgPreviewControl.Controls[0]).Dock, DockStyle.Fill);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldSetScriptErrorsSuppressedProperty_WhenDoPreviewCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream("<svg></svg>"));
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(((WebBrowser)svgPreviewControl.Controls[0]).ScriptErrorsSuppressed, true);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldSetScrollBarsEnabledProperty_WhenDoPreviewCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream("<svg></svg>"));
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(((WebBrowser)svgPreviewControl.Controls[0]).ScrollBarsEnabled, true);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldDisableAllowNavigation_WhenDoPreviewCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream("<svg></svg>"));
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(((WebBrowser)svgPreviewControl.Controls[0]).AllowNavigation, false);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldAddValidInfoBar_IfSvgPreviewThrows()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
var mockStream = new Mock<IStream>();
|
||||
mockStream
|
||||
.Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
|
||||
.Throws(new Exception());
|
||||
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(mockStream.Object);
|
||||
var textBox = svgPreviewControl.Controls[0] as RichTextBox;
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(string.IsNullOrWhiteSpace(textBox.Text));
|
||||
Assert.AreEqual(svgPreviewControl.Controls.Count, 1);
|
||||
Assert.AreEqual(textBox.Dock, DockStyle.Top);
|
||||
Assert.AreEqual(textBox.BackColor, Color.LightYellow);
|
||||
Assert.IsTrue(textBox.Multiline);
|
||||
Assert.IsTrue(textBox.ReadOnly);
|
||||
Assert.AreEqual(textBox.ScrollBars, RichTextBoxScrollBars.None);
|
||||
Assert.AreEqual(textBox.BorderStyle, BorderStyle.None);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_InfoBarWidthShouldAdjustWithParentControlWidthChanges_IfSvgPreviewThrows()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
var mockStream = new Mock<IStream>();
|
||||
mockStream
|
||||
.Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
|
||||
.Throws(new Exception());
|
||||
svgPreviewControl.DoPreview(mockStream.Object);
|
||||
var textBox = svgPreviewControl.Controls[0] as RichTextBox;
|
||||
var incrementParentControlWidth = 5;
|
||||
var initialParentWidth = svgPreviewControl.Width;
|
||||
var initialTextBoxWidth = textBox.Width;
|
||||
var finalParentWidth = initialParentWidth + incrementParentControlWidth;
|
||||
|
||||
// Act
|
||||
svgPreviewControl.Width += incrementParentControlWidth;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(initialParentWidth, initialTextBoxWidth);
|
||||
Assert.AreEqual(finalParentWidth, textBox.Width);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldAddTextBox_IfBlockedElementsArePresent()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg width =\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">");
|
||||
svgBuilder.AppendLine("\t<script>alert(\"hello\")</script>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream(svgBuilder.ToString()));
|
||||
|
||||
// Assert
|
||||
Assert.IsInstanceOfType(svgPreviewControl.Controls[0], typeof(RichTextBox));
|
||||
Assert.IsInstanceOfType(svgPreviewControl.Controls[1], typeof(WebBrowserExt));
|
||||
Assert.AreEqual(svgPreviewControl.Controls.Count, 2);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldNotAddTextBox_IfNoBlockedElementsArePresent()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg viewBox=\"0 0 100 100\" xmlns=\"http://www.w3.org/2000/svg\">");
|
||||
svgBuilder.AppendLine("\t<circle cx=\"50\" cy=\"50\" r=\"50\">");
|
||||
svgBuilder.AppendLine("\t</circle>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream(svgBuilder.ToString()));
|
||||
|
||||
// Assert
|
||||
Assert.IsInstanceOfType(svgPreviewControl.Controls[0], typeof(WebBrowserExt));
|
||||
Assert.AreEqual(svgPreviewControl.Controls.Count, 1);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_InfoBarWidthShouldAdjustWithParentControlWidthChanges_IfBlockedElementsArePresent()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg width =\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">");
|
||||
svgBuilder.AppendLine("\t<script>alert(\"hello\")</script>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
svgPreviewControl.DoPreview(GetMockStream(svgBuilder.ToString()));
|
||||
var textBox = svgPreviewControl.Controls[0] as RichTextBox;
|
||||
var incrementParentControlWidth = 5;
|
||||
var initialParentWidth = svgPreviewControl.Width;
|
||||
var initialTextBoxWidth = textBox.Width;
|
||||
var finalParentWidth = initialParentWidth + incrementParentControlWidth;
|
||||
|
||||
// Act
|
||||
svgPreviewControl.Width += incrementParentControlWidth;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(initialParentWidth, initialTextBoxWidth);
|
||||
Assert.AreEqual(finalParentWidth, textBox.Width);
|
||||
}
|
||||
}
|
||||
|
||||
private IStream GetMockStream(string streamData)
|
||||
{
|
||||
var mockStream = new Mock<IStream>();
|
||||
var streamBytes = Encoding.UTF8.GetBytes(streamData);
|
||||
|
||||
var streamMock = new Mock<IStream>();
|
||||
var firstCall = true;
|
||||
streamMock
|
||||
.Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
|
||||
.Callback<byte[], int, IntPtr>((buffer, countToRead, bytesReadPtr) =>
|
||||
{
|
||||
if (firstCall)
|
||||
{
|
||||
Array.Copy(streamBytes, 0, buffer, 0, streamBytes.Length);
|
||||
Marshal.WriteInt32(bytesReadPtr, streamBytes.Length);
|
||||
firstCall = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Marshal.WriteInt32(bytesReadPtr, 0);
|
||||
}
|
||||
});
|
||||
|
||||
return streamMock.Object;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using PreviewHandlerCommon;
|
||||
using SvgPreviewHandler;
|
||||
|
||||
namespace UnitTests_SvgPreviewHandler
|
||||
{
|
||||
[TestClass]
|
||||
public class SvgPreviewControlTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldAddExtendedBrowserControl_WhenDoPreviewCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream("<svg></svg>"));
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(svgPreviewControl.Controls.Count, 1);
|
||||
Assert.IsInstanceOfType(svgPreviewControl.Controls[0], typeof(WebBrowserExt));
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldSetDocumentStream_WhenDoPreviewCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream("<svg></svg>"));
|
||||
|
||||
// Assert
|
||||
Assert.IsNotNull(((WebBrowser)svgPreviewControl.Controls[0]).DocumentStream);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldDisableWebBrowserContextMenu_WhenDoPreviewCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream("<svg></svg>"));
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(((WebBrowser)svgPreviewControl.Controls[0]).IsWebBrowserContextMenuEnabled, false);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldFillDockForWebBrowser_WhenDoPreviewCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream("<svg></svg>"));
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(((WebBrowser)svgPreviewControl.Controls[0]).Dock, DockStyle.Fill);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldSetScriptErrorsSuppressedProperty_WhenDoPreviewCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream("<svg></svg>"));
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(((WebBrowser)svgPreviewControl.Controls[0]).ScriptErrorsSuppressed, true);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldSetScrollBarsEnabledProperty_WhenDoPreviewCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream("<svg></svg>"));
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(((WebBrowser)svgPreviewControl.Controls[0]).ScrollBarsEnabled, true);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldDisableAllowNavigation_WhenDoPreviewCalled()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream("<svg></svg>"));
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(((WebBrowser)svgPreviewControl.Controls[0]).AllowNavigation, false);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldAddValidInfoBar_IfSvgPreviewThrows()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
var mockStream = new Mock<IStream>();
|
||||
mockStream
|
||||
.Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
|
||||
.Throws(new Exception());
|
||||
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(mockStream.Object);
|
||||
var textBox = svgPreviewControl.Controls[0] as RichTextBox;
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(string.IsNullOrWhiteSpace(textBox.Text));
|
||||
Assert.AreEqual(svgPreviewControl.Controls.Count, 1);
|
||||
Assert.AreEqual(textBox.Dock, DockStyle.Top);
|
||||
Assert.AreEqual(textBox.BackColor, Color.LightYellow);
|
||||
Assert.IsTrue(textBox.Multiline);
|
||||
Assert.IsTrue(textBox.ReadOnly);
|
||||
Assert.AreEqual(textBox.ScrollBars, RichTextBoxScrollBars.None);
|
||||
Assert.AreEqual(textBox.BorderStyle, BorderStyle.None);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_InfoBarWidthShouldAdjustWithParentControlWidthChanges_IfSvgPreviewThrows()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
var mockStream = new Mock<IStream>();
|
||||
mockStream
|
||||
.Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
|
||||
.Throws(new Exception());
|
||||
svgPreviewControl.DoPreview(mockStream.Object);
|
||||
var textBox = svgPreviewControl.Controls[0] as RichTextBox;
|
||||
var incrementParentControlWidth = 5;
|
||||
var initialParentWidth = svgPreviewControl.Width;
|
||||
var initialTextBoxWidth = textBox.Width;
|
||||
var finalParentWidth = initialParentWidth + incrementParentControlWidth;
|
||||
|
||||
// Act
|
||||
svgPreviewControl.Width += incrementParentControlWidth;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(initialParentWidth, initialTextBoxWidth);
|
||||
Assert.AreEqual(finalParentWidth, textBox.Width);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldAddTextBox_IfBlockedElementsArePresent()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg width =\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">");
|
||||
svgBuilder.AppendLine("\t<script>alert(\"hello\")</script>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream(svgBuilder.ToString()));
|
||||
|
||||
// Assert
|
||||
Assert.IsInstanceOfType(svgPreviewControl.Controls[0], typeof(RichTextBox));
|
||||
Assert.IsInstanceOfType(svgPreviewControl.Controls[1], typeof(WebBrowserExt));
|
||||
Assert.AreEqual(svgPreviewControl.Controls.Count, 2);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_ShouldNotAddTextBox_IfNoBlockedElementsArePresent()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg viewBox=\"0 0 100 100\" xmlns=\"http://www.w3.org/2000/svg\">");
|
||||
svgBuilder.AppendLine("\t<circle cx=\"50\" cy=\"50\" r=\"50\">");
|
||||
svgBuilder.AppendLine("\t</circle>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
|
||||
// Act
|
||||
svgPreviewControl.DoPreview(GetMockStream(svgBuilder.ToString()));
|
||||
|
||||
// Assert
|
||||
Assert.IsInstanceOfType(svgPreviewControl.Controls[0], typeof(WebBrowserExt));
|
||||
Assert.AreEqual(svgPreviewControl.Controls.Count, 1);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SvgPreviewControl_InfoBarWidthShouldAdjustWithParentControlWidthChanges_IfBlockedElementsArePresent()
|
||||
{
|
||||
// Arrange
|
||||
using (var svgPreviewControl = new SvgPreviewControl())
|
||||
{
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg width =\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">");
|
||||
svgBuilder.AppendLine("\t<script>alert(\"hello\")</script>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
svgPreviewControl.DoPreview(GetMockStream(svgBuilder.ToString()));
|
||||
var textBox = svgPreviewControl.Controls[0] as RichTextBox;
|
||||
var incrementParentControlWidth = 5;
|
||||
var initialParentWidth = svgPreviewControl.Width;
|
||||
var initialTextBoxWidth = textBox.Width;
|
||||
var finalParentWidth = initialParentWidth + incrementParentControlWidth;
|
||||
|
||||
// Act
|
||||
svgPreviewControl.Width += incrementParentControlWidth;
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(initialParentWidth, initialTextBoxWidth);
|
||||
Assert.AreEqual(finalParentWidth, textBox.Width);
|
||||
}
|
||||
}
|
||||
|
||||
private IStream GetMockStream(string streamData)
|
||||
{
|
||||
var mockStream = new Mock<IStream>();
|
||||
var streamBytes = Encoding.UTF8.GetBytes(streamData);
|
||||
|
||||
var streamMock = new Mock<IStream>();
|
||||
var firstCall = true;
|
||||
streamMock
|
||||
.Setup(x => x.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<IntPtr>()))
|
||||
.Callback<byte[], int, IntPtr>((buffer, countToRead, bytesReadPtr) =>
|
||||
{
|
||||
if (firstCall)
|
||||
{
|
||||
Array.Copy(streamBytes, 0, buffer, 0, streamBytes.Length);
|
||||
Marshal.WriteInt32(bytesReadPtr, streamBytes.Length);
|
||||
firstCall = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Marshal.WriteInt32(bytesReadPtr, 0);
|
||||
}
|
||||
});
|
||||
|
||||
return streamMock.Object;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,102 +1,102 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Text;
|
||||
using Common.Utilities;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace UnitTests_SvgPreviewHandler
|
||||
{
|
||||
[TestClass]
|
||||
public class SvgPreviewHandlerHelperTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void CheckBlockedElements_ShouldReturnTrue_IfABlockedElementIsPresent()
|
||||
{
|
||||
// Arrange
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg width =\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">");
|
||||
svgBuilder.AppendLine("\t<script>alert(\"hello\")</script>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
bool foundFilteredElement;
|
||||
|
||||
// Act
|
||||
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgBuilder.ToString());
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(foundFilteredElement);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CheckBlockedElements_ShouldReturnTrue_IfBlockedElementsIsPresentInNestedLevel()
|
||||
{
|
||||
// Arrange
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg viewBox=\"0 0 100 100\" xmlns=\"http://www.w3.org/2000/svg\">");
|
||||
svgBuilder.AppendLine("\t<circle cx=\"50\" cy=\"50\" r=\"50\">");
|
||||
svgBuilder.AppendLine("\t\t<script>alert(\"valid-message\")</script>");
|
||||
svgBuilder.AppendLine("\t</circle>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
bool foundFilteredElement;
|
||||
|
||||
// Act
|
||||
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgBuilder.ToString());
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(foundFilteredElement);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CheckBlockedElements_ShouldReturnTrue_IfMultipleBlockedElementsArePresent()
|
||||
{
|
||||
// Arrange
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg width =\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">");
|
||||
svgBuilder.AppendLine("\t<script>alert(\"valid-message\")</script>");
|
||||
svgBuilder.AppendLine("\t<image href=\"valid-url\" height=\"200\" width=\"200\"/>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
bool foundFilteredElement;
|
||||
|
||||
// Act
|
||||
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgBuilder.ToString());
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(foundFilteredElement);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CheckBlockedElements_ShouldReturnFalse_IfNoBlockedElementsArePresent()
|
||||
{
|
||||
// Arrange
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg viewBox=\"0 0 100 100\" xmlns=\"http://www.w3.org/2000/svg\">");
|
||||
svgBuilder.AppendLine("\t<circle cx=\"50\" cy=\"50\" r=\"50\">");
|
||||
svgBuilder.AppendLine("\t</circle>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
bool foundFilteredElement;
|
||||
|
||||
// Act
|
||||
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgBuilder.ToString());
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(foundFilteredElement);
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow("")]
|
||||
[DataRow(" ")]
|
||||
[DataRow(null)]
|
||||
public void CheckBlockedElements_ShouldReturnFalse_IfSvgDataIsNullOrWhiteSpaces(string svgData)
|
||||
{
|
||||
// Arrange
|
||||
bool foundFilteredElement;
|
||||
|
||||
// Act
|
||||
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgData);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(foundFilteredElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Text;
|
||||
using Common.Utilities;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace UnitTests_SvgPreviewHandler
|
||||
{
|
||||
[TestClass]
|
||||
public class SvgPreviewHandlerHelperTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void CheckBlockedElements_ShouldReturnTrue_IfABlockedElementIsPresent()
|
||||
{
|
||||
// Arrange
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg width =\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">");
|
||||
svgBuilder.AppendLine("\t<script>alert(\"hello\")</script>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
bool foundFilteredElement;
|
||||
|
||||
// Act
|
||||
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgBuilder.ToString());
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(foundFilteredElement);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CheckBlockedElements_ShouldReturnTrue_IfBlockedElementsIsPresentInNestedLevel()
|
||||
{
|
||||
// Arrange
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg viewBox=\"0 0 100 100\" xmlns=\"http://www.w3.org/2000/svg\">");
|
||||
svgBuilder.AppendLine("\t<circle cx=\"50\" cy=\"50\" r=\"50\">");
|
||||
svgBuilder.AppendLine("\t\t<script>alert(\"valid-message\")</script>");
|
||||
svgBuilder.AppendLine("\t</circle>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
bool foundFilteredElement;
|
||||
|
||||
// Act
|
||||
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgBuilder.ToString());
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(foundFilteredElement);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CheckBlockedElements_ShouldReturnTrue_IfMultipleBlockedElementsArePresent()
|
||||
{
|
||||
// Arrange
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg width =\"200\" height=\"200\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">");
|
||||
svgBuilder.AppendLine("\t<script>alert(\"valid-message\")</script>");
|
||||
svgBuilder.AppendLine("\t<image href=\"valid-url\" height=\"200\" width=\"200\"/>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
bool foundFilteredElement;
|
||||
|
||||
// Act
|
||||
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgBuilder.ToString());
|
||||
|
||||
// Assert
|
||||
Assert.IsTrue(foundFilteredElement);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CheckBlockedElements_ShouldReturnFalse_IfNoBlockedElementsArePresent()
|
||||
{
|
||||
// Arrange
|
||||
var svgBuilder = new StringBuilder();
|
||||
svgBuilder.AppendLine("<svg viewBox=\"0 0 100 100\" xmlns=\"http://www.w3.org/2000/svg\">");
|
||||
svgBuilder.AppendLine("\t<circle cx=\"50\" cy=\"50\" r=\"50\">");
|
||||
svgBuilder.AppendLine("\t</circle>");
|
||||
svgBuilder.AppendLine("</svg>");
|
||||
bool foundFilteredElement;
|
||||
|
||||
// Act
|
||||
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgBuilder.ToString());
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(foundFilteredElement);
|
||||
}
|
||||
|
||||
[DataTestMethod]
|
||||
[DataRow("")]
|
||||
[DataRow(" ")]
|
||||
[DataRow(null)]
|
||||
public void CheckBlockedElements_ShouldReturnFalse_IfSvgDataIsNullOrWhiteSpaces(string svgData)
|
||||
{
|
||||
// Arrange
|
||||
bool foundFilteredElement;
|
||||
|
||||
// Act
|
||||
foundFilteredElement = SvgPreviewHandlerHelper.CheckBlockedElements(svgData);
|
||||
|
||||
// Assert
|
||||
Assert.IsFalse(foundFilteredElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,242 +1,242 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
|
||||
namespace Common.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Wraps <see cref="IStream"/> interface into <see cref="Stream"/> Class.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Implements only read from the stream functionality.
|
||||
/// </remarks>
|
||||
public class StreamWrapper : Stream
|
||||
{
|
||||
private IStream stream;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StreamWrapper"/> class.
|
||||
/// </summary>
|
||||
/// <param name="stream">A pointer to an <see cref="IStream" /> interface that represents the stream source.</param>
|
||||
public StreamWrapper(IStream stream)
|
||||
{
|
||||
this.stream = stream ?? throw new ArgumentNullException(nameof(stream));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the current stream supports reading.
|
||||
/// </summary>
|
||||
public override bool CanRead
|
||||
{
|
||||
get
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the current stream supports seeking.
|
||||
/// </summary>
|
||||
public override bool CanSeek
|
||||
{
|
||||
get
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the current stream supports writing.
|
||||
/// </summary>
|
||||
public override bool CanWrite
|
||||
{
|
||||
get
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the length in bytes of the stream.
|
||||
/// </summary>
|
||||
public override long Length
|
||||
{
|
||||
get
|
||||
{
|
||||
this.CheckDisposed();
|
||||
System.Runtime.InteropServices.ComTypes.STATSTG stat;
|
||||
|
||||
// Stat called with STATFLAG_NONAME. The pwcsName is not required more details https://docs.microsoft.com/en-us/windows/win32/api/wtypes/ne-wtypes-statflag
|
||||
this.stream.Stat(out stat, 1); // STATFLAG_NONAME
|
||||
|
||||
return stat.cbSize;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or Sets the position within the current.
|
||||
/// </summary>
|
||||
public override long Position
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.Seek(0, SeekOrigin.Current);
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
this.Seek(value, SeekOrigin.Begin);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
|
||||
/// </summary>
|
||||
/// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
|
||||
/// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
|
||||
/// <param name="count">The maximum number of bytes to be read from the current stream.</param>
|
||||
/// <returns>The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero if the end of the stream has been reached.</returns>
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
this.CheckDisposed();
|
||||
|
||||
if (offset < 0 || count < 0 || offset + count > buffer.Length)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
byte[] localBuffer = buffer;
|
||||
|
||||
if (offset > 0)
|
||||
{
|
||||
localBuffer = new byte[count];
|
||||
}
|
||||
|
||||
IntPtr bytesReadPtr = Marshal.AllocCoTaskMem(sizeof(int));
|
||||
|
||||
try
|
||||
{
|
||||
this.stream.Read(localBuffer, count, bytesReadPtr);
|
||||
int bytesRead = Marshal.ReadInt32(bytesReadPtr);
|
||||
|
||||
if (offset > 0)
|
||||
{
|
||||
Array.Copy(localBuffer, 0, buffer, offset, bytesRead);
|
||||
}
|
||||
|
||||
return bytesRead;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeCoTaskMem(bytesReadPtr);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the position within the current stream.
|
||||
/// </summary>
|
||||
/// <param name="offset">A byte offset relative to the origin parameter.</param>
|
||||
/// <param name="origin">A value of type System.IO.SeekOrigin indicating the reference point used to obtain the new position.</param>
|
||||
/// <returns>The new position within the current stream.</returns>
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
this.CheckDisposed();
|
||||
int dwOrigin;
|
||||
|
||||
// Maps the SeekOrigin with dworigin more details: https://docs.microsoft.com/en-us/windows/win32/api/objidl/ne-objidl-stream_seek
|
||||
switch (origin)
|
||||
{
|
||||
case SeekOrigin.Begin:
|
||||
dwOrigin = 0; // STREAM_SEEK_SET
|
||||
break;
|
||||
|
||||
case SeekOrigin.Current:
|
||||
dwOrigin = 1; // STREAM_SEEK_CUR
|
||||
break;
|
||||
|
||||
case SeekOrigin.End:
|
||||
dwOrigin = 2; // STREAM_SEEK_END
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
IntPtr posPtr = Marshal.AllocCoTaskMem(sizeof(long));
|
||||
|
||||
try
|
||||
{
|
||||
this.stream.Seek(offset, dwOrigin, posPtr);
|
||||
return Marshal.ReadInt64(posPtr);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeCoTaskMem(posPtr);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Not implemented current implementation supports only read.
|
||||
/// </remarks>
|
||||
public override void Flush()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the length of the current stream.
|
||||
/// </summary>
|
||||
/// <param name="value">The desired length of the current stream in bytes.</param>
|
||||
/// /// <remarks>
|
||||
/// Not implemented current implementation supports only read.
|
||||
/// </remarks>
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
|
||||
/// </summary>
|
||||
/// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
|
||||
/// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
|
||||
/// <param name="count">The number of bytes to be written to the current stream.</param>
|
||||
/// <remarks>
|
||||
/// Not implemented current implementation supports only read.
|
||||
/// </remarks>
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (this.stream != null)
|
||||
{
|
||||
if (Marshal.IsComObject(this.stream))
|
||||
{
|
||||
Marshal.ReleaseComObject(this.stream);
|
||||
}
|
||||
|
||||
this.stream = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckDisposed()
|
||||
{
|
||||
if (this.stream == null)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(StreamWrapper));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
|
||||
namespace Common.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Wraps <see cref="IStream"/> interface into <see cref="Stream"/> Class.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Implements only read from the stream functionality.
|
||||
/// </remarks>
|
||||
public class StreamWrapper : Stream
|
||||
{
|
||||
private IStream stream;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="StreamWrapper"/> class.
|
||||
/// </summary>
|
||||
/// <param name="stream">A pointer to an <see cref="IStream" /> interface that represents the stream source.</param>
|
||||
public StreamWrapper(IStream stream)
|
||||
{
|
||||
this.stream = stream ?? throw new ArgumentNullException(nameof(stream));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the current stream supports reading.
|
||||
/// </summary>
|
||||
public override bool CanRead
|
||||
{
|
||||
get
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the current stream supports seeking.
|
||||
/// </summary>
|
||||
public override bool CanSeek
|
||||
{
|
||||
get
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the current stream supports writing.
|
||||
/// </summary>
|
||||
public override bool CanWrite
|
||||
{
|
||||
get
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the length in bytes of the stream.
|
||||
/// </summary>
|
||||
public override long Length
|
||||
{
|
||||
get
|
||||
{
|
||||
this.CheckDisposed();
|
||||
System.Runtime.InteropServices.ComTypes.STATSTG stat;
|
||||
|
||||
// Stat called with STATFLAG_NONAME. The pwcsName is not required more details https://docs.microsoft.com/en-us/windows/win32/api/wtypes/ne-wtypes-statflag
|
||||
this.stream.Stat(out stat, 1); // STATFLAG_NONAME
|
||||
|
||||
return stat.cbSize;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or Sets the position within the current.
|
||||
/// </summary>
|
||||
public override long Position
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.Seek(0, SeekOrigin.Current);
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
this.Seek(value, SeekOrigin.Begin);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
|
||||
/// </summary>
|
||||
/// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
|
||||
/// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
|
||||
/// <param name="count">The maximum number of bytes to be read from the current stream.</param>
|
||||
/// <returns>The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero if the end of the stream has been reached.</returns>
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
this.CheckDisposed();
|
||||
|
||||
if (offset < 0 || count < 0 || offset + count > buffer.Length)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
byte[] localBuffer = buffer;
|
||||
|
||||
if (offset > 0)
|
||||
{
|
||||
localBuffer = new byte[count];
|
||||
}
|
||||
|
||||
IntPtr bytesReadPtr = Marshal.AllocCoTaskMem(sizeof(int));
|
||||
|
||||
try
|
||||
{
|
||||
this.stream.Read(localBuffer, count, bytesReadPtr);
|
||||
int bytesRead = Marshal.ReadInt32(bytesReadPtr);
|
||||
|
||||
if (offset > 0)
|
||||
{
|
||||
Array.Copy(localBuffer, 0, buffer, offset, bytesRead);
|
||||
}
|
||||
|
||||
return bytesRead;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeCoTaskMem(bytesReadPtr);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the position within the current stream.
|
||||
/// </summary>
|
||||
/// <param name="offset">A byte offset relative to the origin parameter.</param>
|
||||
/// <param name="origin">A value of type System.IO.SeekOrigin indicating the reference point used to obtain the new position.</param>
|
||||
/// <returns>The new position within the current stream.</returns>
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
this.CheckDisposed();
|
||||
int dwOrigin;
|
||||
|
||||
// Maps the SeekOrigin with dworigin more details: https://docs.microsoft.com/en-us/windows/win32/api/objidl/ne-objidl-stream_seek
|
||||
switch (origin)
|
||||
{
|
||||
case SeekOrigin.Begin:
|
||||
dwOrigin = 0; // STREAM_SEEK_SET
|
||||
break;
|
||||
|
||||
case SeekOrigin.Current:
|
||||
dwOrigin = 1; // STREAM_SEEK_CUR
|
||||
break;
|
||||
|
||||
case SeekOrigin.End:
|
||||
dwOrigin = 2; // STREAM_SEEK_END
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
IntPtr posPtr = Marshal.AllocCoTaskMem(sizeof(long));
|
||||
|
||||
try
|
||||
{
|
||||
this.stream.Seek(offset, dwOrigin, posPtr);
|
||||
return Marshal.ReadInt64(posPtr);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.FreeCoTaskMem(posPtr);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Not implemented current implementation supports only read.
|
||||
/// </remarks>
|
||||
public override void Flush()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the length of the current stream.
|
||||
/// </summary>
|
||||
/// <param name="value">The desired length of the current stream in bytes.</param>
|
||||
/// /// <remarks>
|
||||
/// Not implemented current implementation supports only read.
|
||||
/// </remarks>
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
|
||||
/// </summary>
|
||||
/// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
|
||||
/// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
|
||||
/// <param name="count">The number of bytes to be written to the current stream.</param>
|
||||
/// <remarks>
|
||||
/// Not implemented current implementation supports only read.
|
||||
/// </remarks>
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (this.stream != null)
|
||||
{
|
||||
if (Marshal.IsComObject(this.stream))
|
||||
{
|
||||
Marshal.ReleaseComObject(this.stream);
|
||||
}
|
||||
|
||||
this.stream = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckDisposed()
|
||||
{
|
||||
if (this.stream == null)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(StreamWrapper));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +1,35 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Common.ComInterlop
|
||||
{
|
||||
/// <summary>
|
||||
/// The COLORREF value is used to specify an RGB color.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct COLORREF
|
||||
{
|
||||
/// <summary>
|
||||
/// Stores an RGB color value in a 32 bit integer.
|
||||
/// </summary>
|
||||
public uint Dword;
|
||||
|
||||
/// <summary>
|
||||
/// Gets RGB value stored in <see cref="Dword"/> in <see cref="Color"/> structure.
|
||||
/// </summary>
|
||||
public Color Color
|
||||
{
|
||||
get
|
||||
{
|
||||
return Color.FromArgb(
|
||||
(int)(0x000000FFU & this.Dword),
|
||||
(int)(0x0000FF00U & this.Dword) >> 8,
|
||||
(int)(0x00FF0000U & this.Dword) >> 16);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Common.ComInterlop
|
||||
{
|
||||
/// <summary>
|
||||
/// The COLORREF value is used to specify an RGB color.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct COLORREF
|
||||
{
|
||||
/// <summary>
|
||||
/// Stores an RGB color value in a 32 bit integer.
|
||||
/// </summary>
|
||||
public uint Dword;
|
||||
|
||||
/// <summary>
|
||||
/// Gets RGB value stored in <see cref="Dword"/> in <see cref="Color"/> structure.
|
||||
/// </summary>
|
||||
public Color Color
|
||||
{
|
||||
get
|
||||
{
|
||||
return Color.FromArgb(
|
||||
(int)(0x000000FFU & this.Dword),
|
||||
(int)(0x0000FF00U & this.Dword) >> 8,
|
||||
(int)(0x00FF0000U & this.Dword) >> 16);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Common.Cominterop
|
||||
{
|
||||
/// <summary>
|
||||
/// Exposes a method to initialize a handler, such as a property handler, thumbnail handler, or preview handler, with a file path.
|
||||
/// </summary>
|
||||
[ComImport]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
[Guid("b7d14566-0509-4cce-a71f-0a554233bd9b")]
|
||||
public interface IInitializeWithFile
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a handler with a file path.
|
||||
/// </summary>
|
||||
/// <param name="pszFilePath">File Path.</param>
|
||||
/// <param name="grfMode">Indicate the Access Mode either STGM_READ (Read Only Access) or STGM_READWRITE (Read and Write Access).</param>
|
||||
void Initialize([MarshalAs(UnmanagedType.LPWStr)] string pszFilePath, uint grfMode);
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Common.Cominterop
|
||||
{
|
||||
/// <summary>
|
||||
/// Exposes a method to initialize a handler, such as a property handler, thumbnail handler, or preview handler, with a file path.
|
||||
/// </summary>
|
||||
[ComImport]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
[Guid("b7d14566-0509-4cce-a71f-0a554233bd9b")]
|
||||
public interface IInitializeWithFile
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a handler with a file path.
|
||||
/// </summary>
|
||||
/// <param name="pszFilePath">File Path.</param>
|
||||
/// <param name="grfMode">Indicate the Access Mode either STGM_READ (Read Only Access) or STGM_READWRITE (Read and Write Access).</param>
|
||||
void Initialize([MarshalAs(UnmanagedType.LPWStr)] string pszFilePath, uint grfMode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +1,26 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
|
||||
namespace Common.ComInterlop
|
||||
{
|
||||
/// <summary>
|
||||
/// Exposes a method that initializes a handler, such as a property handler, thumbnail handler, or preview handler, with a stream.
|
||||
/// </summary>
|
||||
[ComImport]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
[Guid("b824b49d-22ac-4161-ac8a-9916e8fa3f7f")]
|
||||
public interface IInitializeWithStream
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a handler with a stream.
|
||||
/// </summary>
|
||||
/// <param name="pstream">A pointer to an <see cref="IStream" /> interface that represents the stream source.</param>
|
||||
/// <param name="grfMode">One of the <see href="https://docs.microsoft.com/en-us/windows/win32/stg/stgm-constants" >STGM</see> values that indicates the access mode for <paramref name="pstream"/>.</param>
|
||||
void Initialize(IStream pstream, uint grfMode);
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
|
||||
namespace Common.ComInterlop
|
||||
{
|
||||
/// <summary>
|
||||
/// Exposes a method that initializes a handler, such as a property handler, thumbnail handler, or preview handler, with a stream.
|
||||
/// </summary>
|
||||
[ComImport]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
[Guid("b824b49d-22ac-4161-ac8a-9916e8fa3f7f")]
|
||||
public interface IInitializeWithStream
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a handler with a stream.
|
||||
/// </summary>
|
||||
/// <param name="pstream">A pointer to an <see cref="IStream" /> interface that represents the stream source.</param>
|
||||
/// <param name="grfMode">One of the <see href="https://docs.microsoft.com/en-us/windows/win32/stg/stgm-constants" >STGM</see> values that indicates the access mode for <paramref name="pstream"/>.</param>
|
||||
void Initialize(IStream pstream, uint grfMode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +1,31 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Common.ComInterlop
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a simplified way to support communication between an object and its site in the container.
|
||||
/// </summary>
|
||||
[ComImport]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
[Guid("fc4801a3-2ba9-11cf-a229-00aa003d7352")]
|
||||
public interface IObjectWithSite
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides the site's pointer to the site object.
|
||||
/// </summary>
|
||||
/// <param name="pUnkSite">Address of an interface pointer to the site managing this object.</param>
|
||||
void SetSite([In, MarshalAs(UnmanagedType.IUnknown)] object pUnkSite);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the last site set using the <see cref="SetSite(object)" /> method. In cases where there is no known site, the object returns an exception.
|
||||
/// </summary>
|
||||
/// <param name="riid">Provides the IID of the interface pointer returned in the <paramref name="ppvSite"/> parameter.</param>
|
||||
/// <param name="ppvSite">The address of the caller's void variable in which the object stores the interface pointer of the site last seen in the <see cref="SetSite(object)" />.</param>
|
||||
void GetSite(ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out object ppvSite);
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Common.ComInterlop
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a simplified way to support communication between an object and its site in the container.
|
||||
/// </summary>
|
||||
[ComImport]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
[Guid("fc4801a3-2ba9-11cf-a229-00aa003d7352")]
|
||||
public interface IObjectWithSite
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides the site's pointer to the site object.
|
||||
/// </summary>
|
||||
/// <param name="pUnkSite">Address of an interface pointer to the site managing this object.</param>
|
||||
void SetSite([In, MarshalAs(UnmanagedType.IUnknown)] object pUnkSite);
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the last site set using the <see cref="SetSite(object)" /> method. In cases where there is no known site, the object returns an exception.
|
||||
/// </summary>
|
||||
/// <param name="riid">Provides the IID of the interface pointer returned in the <paramref name="ppvSite"/> parameter.</param>
|
||||
/// <param name="ppvSite">The address of the caller's void variable in which the object stores the interface pointer of the site last seen in the <see cref="SetSite(object)" />.</param>
|
||||
void GetSite(ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out object ppvSite);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +1,31 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Common.ComInterlop
|
||||
{
|
||||
/// <summary>
|
||||
/// The IOleWindow interface provides methods that allow an application to obtain the handle to the various windows that participate
|
||||
/// in in-place activation, and also to enter and exit context-sensitive help mode.
|
||||
/// </summary>
|
||||
[ComImport]
|
||||
[Guid("00000114-0000-0000-C000-000000000046")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
public interface IOleWindow
|
||||
{
|
||||
/// <summary>
|
||||
/// Retrieves a handle to one of the windows participating in in-place activation (frame, document, parent, or in-place object window).
|
||||
/// </summary>
|
||||
/// <param name="phwnd">A pointer to a variable that receives the window handle.</param>
|
||||
void GetWindow(out IntPtr phwnd);
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether context-sensitive help mode should be entered during an in-place activation session.
|
||||
/// </summary>
|
||||
/// <param name="fEnterMode">TRUE if help mode should be entered; FALSE if it should be exited.</param>
|
||||
void ContextSensitiveHelp([MarshalAs(UnmanagedType.Bool)] bool fEnterMode);
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Common.ComInterlop
|
||||
{
|
||||
/// <summary>
|
||||
/// The IOleWindow interface provides methods that allow an application to obtain the handle to the various windows that participate
|
||||
/// in in-place activation, and also to enter and exit context-sensitive help mode.
|
||||
/// </summary>
|
||||
[ComImport]
|
||||
[Guid("00000114-0000-0000-C000-000000000046")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
public interface IOleWindow
|
||||
{
|
||||
/// <summary>
|
||||
/// Retrieves a handle to one of the windows participating in in-place activation (frame, document, parent, or in-place object window).
|
||||
/// </summary>
|
||||
/// <param name="phwnd">A pointer to a variable that receives the window handle.</param>
|
||||
void GetWindow(out IntPtr phwnd);
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether context-sensitive help mode should be entered during an in-place activation session.
|
||||
/// </summary>
|
||||
/// <param name="fEnterMode">TRUE if help mode should be entered; FALSE if it should be exited.</param>
|
||||
void ContextSensitiveHelp([MarshalAs(UnmanagedType.Bool)] bool fEnterMode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,62 +1,62 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Common.ComInterlop
|
||||
{
|
||||
/// <summary>
|
||||
/// Exposes methods for the display of rich previews.
|
||||
/// </summary>
|
||||
[ComImport]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
[Guid("8895b1c6-b41f-4c1c-a562-0d564250836f")]
|
||||
public interface IPreviewHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the parent window of the previewer window, as well as the area within the parent to be used for the previewer window.
|
||||
/// </summary>
|
||||
/// <param name="hwnd">A handle to the parent window.</param>
|
||||
/// <param name="rect">A pointer to a <see cref="RECT"/> defining the area for the previewer.</param>
|
||||
void SetWindow(IntPtr hwnd, ref RECT rect);
|
||||
|
||||
/// <summary>
|
||||
/// Directs the preview handler to change the area within the parent hwnd that it draws into.
|
||||
/// </summary>
|
||||
/// <param name="rect">A pointer to a <see cref="RECT"/> to be used for the preview.</param>
|
||||
void SetRect(ref RECT rect);
|
||||
|
||||
/// <summary>
|
||||
/// Directs the preview handler to load data from the source specified in an earlier Initialize method call, and to begin rendering to the previewer window.
|
||||
/// </summary>
|
||||
void DoPreview();
|
||||
|
||||
/// <summary>
|
||||
/// Directs the preview handler to cease rendering a preview and to release all resources that have been allocated based on the item passed in during the initialization.
|
||||
/// </summary>
|
||||
void Unload();
|
||||
|
||||
/// <summary>
|
||||
/// Directs the preview handler to set focus to itself.
|
||||
/// </summary>
|
||||
void SetFocus();
|
||||
|
||||
/// <summary>
|
||||
/// Directs the preview handler to return the HWND from calling the GetFocus Function.
|
||||
/// </summary>
|
||||
/// <param name="phwnd">When this method returns, contains a pointer to the HWND returned from calling the GetFocus Function from the preview handler's foreground thread.</param>
|
||||
void QueryFocus(out IntPtr phwnd);
|
||||
|
||||
/// <summary>
|
||||
/// Directs the preview handler to handle a keystroke passed up from the message pump of the process in which the preview handler is running.
|
||||
/// </summary>
|
||||
/// <param name="pmsg">A pointer to a window message.</param>
|
||||
/// <returns>If the keystroke message can be processed by the preview handler, the handler will process it and return S_OK(0). If the preview handler cannot process the keystroke message, it
|
||||
/// will offer it to the host using <see cref="IPreviewHandlerFrame.TranslateAccelerator(ref MSG)"/>. If the host processes the message, this method will return S_OK(0). If the host does not process the message, this method will return S_FALSE(1).
|
||||
/// </returns>
|
||||
[PreserveSig]
|
||||
uint TranslateAccelerator(ref MSG pmsg);
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Common.ComInterlop
|
||||
{
|
||||
/// <summary>
|
||||
/// Exposes methods for the display of rich previews.
|
||||
/// </summary>
|
||||
[ComImport]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
[Guid("8895b1c6-b41f-4c1c-a562-0d564250836f")]
|
||||
public interface IPreviewHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the parent window of the previewer window, as well as the area within the parent to be used for the previewer window.
|
||||
/// </summary>
|
||||
/// <param name="hwnd">A handle to the parent window.</param>
|
||||
/// <param name="rect">A pointer to a <see cref="RECT"/> defining the area for the previewer.</param>
|
||||
void SetWindow(IntPtr hwnd, ref RECT rect);
|
||||
|
||||
/// <summary>
|
||||
/// Directs the preview handler to change the area within the parent hwnd that it draws into.
|
||||
/// </summary>
|
||||
/// <param name="rect">A pointer to a <see cref="RECT"/> to be used for the preview.</param>
|
||||
void SetRect(ref RECT rect);
|
||||
|
||||
/// <summary>
|
||||
/// Directs the preview handler to load data from the source specified in an earlier Initialize method call, and to begin rendering to the previewer window.
|
||||
/// </summary>
|
||||
void DoPreview();
|
||||
|
||||
/// <summary>
|
||||
/// Directs the preview handler to cease rendering a preview and to release all resources that have been allocated based on the item passed in during the initialization.
|
||||
/// </summary>
|
||||
void Unload();
|
||||
|
||||
/// <summary>
|
||||
/// Directs the preview handler to set focus to itself.
|
||||
/// </summary>
|
||||
void SetFocus();
|
||||
|
||||
/// <summary>
|
||||
/// Directs the preview handler to return the HWND from calling the GetFocus Function.
|
||||
/// </summary>
|
||||
/// <param name="phwnd">When this method returns, contains a pointer to the HWND returned from calling the GetFocus Function from the preview handler's foreground thread.</param>
|
||||
void QueryFocus(out IntPtr phwnd);
|
||||
|
||||
/// <summary>
|
||||
/// Directs the preview handler to handle a keystroke passed up from the message pump of the process in which the preview handler is running.
|
||||
/// </summary>
|
||||
/// <param name="pmsg">A pointer to a window message.</param>
|
||||
/// <returns>If the keystroke message can be processed by the preview handler, the handler will process it and return S_OK(0). If the preview handler cannot process the keystroke message, it
|
||||
/// will offer it to the host using <see cref="IPreviewHandlerFrame.TranslateAccelerator(ref MSG)"/>. If the host processes the message, this method will return S_OK(0). If the host does not process the message, this method will return S_FALSE(1).
|
||||
/// </returns>
|
||||
[PreserveSig]
|
||||
uint TranslateAccelerator(ref MSG pmsg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +1,33 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Common.ComInterlop
|
||||
{
|
||||
/// <summary>
|
||||
/// Enables preview handlers to pass keyboard shortcuts to the host. This interface retrieves a list of keyboard shortcuts and directs the host to handle a keyboard shortcut.
|
||||
/// </summary>
|
||||
[ComImport]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
[Guid("fec87aaf-35f9-447a-adb7-20234491401a")]
|
||||
public interface IPreviewHandlerFrame
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a list of the keyboard shortcuts for the preview host.
|
||||
/// </summary>
|
||||
/// <param name="pinfo">A pointer to a <see href="https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/ns-shobjidl_core-previewhandlerframeinfo">PREVIEWHANDLERFRAMEINFO</see> structure
|
||||
/// that receives accelerator table information.</param>
|
||||
void GetWindowContext(IntPtr pinfo);
|
||||
|
||||
/// <summary>
|
||||
/// Directs the host to handle an keyboard shortcut passed from the preview handler.
|
||||
/// </summary>
|
||||
/// <param name="pmsg">A reference to <see cref="MSG"/> that corresponds to a keyboard shortcut.</param>
|
||||
/// <returns>If the keyboard shortcut is one that the host intends to handle, the host will process it and return S_OK(0); otherwise, it returns S_FALSE(1).</returns>
|
||||
[PreserveSig]
|
||||
uint TranslateAccelerator(ref MSG pmsg);
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Common.ComInterlop
|
||||
{
|
||||
/// <summary>
|
||||
/// Enables preview handlers to pass keyboard shortcuts to the host. This interface retrieves a list of keyboard shortcuts and directs the host to handle a keyboard shortcut.
|
||||
/// </summary>
|
||||
[ComImport]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
[Guid("fec87aaf-35f9-447a-adb7-20234491401a")]
|
||||
public interface IPreviewHandlerFrame
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a list of the keyboard shortcuts for the preview host.
|
||||
/// </summary>
|
||||
/// <param name="pinfo">A pointer to a <see href="https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/ns-shobjidl_core-previewhandlerframeinfo">PREVIEWHANDLERFRAMEINFO</see> structure
|
||||
/// that receives accelerator table information.</param>
|
||||
void GetWindowContext(IntPtr pinfo);
|
||||
|
||||
/// <summary>
|
||||
/// Directs the host to handle an keyboard shortcut passed from the preview handler.
|
||||
/// </summary>
|
||||
/// <param name="pmsg">A reference to <see cref="MSG"/> that corresponds to a keyboard shortcut.</param>
|
||||
/// <returns>If the keyboard shortcut is one that the host intends to handle, the host will process it and return S_OK(0); otherwise, it returns S_FALSE(1).</returns>
|
||||
[PreserveSig]
|
||||
uint TranslateAccelerator(ref MSG pmsg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,36 +1,36 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Common.ComInterlop
|
||||
{
|
||||
/// <summary>
|
||||
/// Exposes methods for applying color and font information to preview handlers.
|
||||
/// </summary>
|
||||
[ComImport]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
[Guid("8327b13c-b63f-4b24-9b8a-d010dcc3f599")]
|
||||
public interface IPreviewHandlerVisuals
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the background color of the preview handler.
|
||||
/// </summary>
|
||||
/// <param name="color">A value of type <see cref="COLORREF"/> to use for the preview handler background.</param>
|
||||
void SetBackgroundColor(COLORREF color);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the font attributes to be used for text within the preview handler.
|
||||
/// </summary>
|
||||
/// <param name="plf">A pointer to a <see cref="LOGFONT"/> Structure containing the necessary attributes for the font to use.</param>
|
||||
void SetFont(ref LOGFONT plf);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the color of the text within the preview handler.
|
||||
/// </summary>
|
||||
/// <param name="color">A value of type <see cref="COLORREF"/> to use for the preview handler text color.</param>
|
||||
void SetTextColor(COLORREF color);
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Common.ComInterlop
|
||||
{
|
||||
/// <summary>
|
||||
/// Exposes methods for applying color and font information to preview handlers.
|
||||
/// </summary>
|
||||
[ComImport]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
[Guid("8327b13c-b63f-4b24-9b8a-d010dcc3f599")]
|
||||
public interface IPreviewHandlerVisuals
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the background color of the preview handler.
|
||||
/// </summary>
|
||||
/// <param name="color">A value of type <see cref="COLORREF"/> to use for the preview handler background.</param>
|
||||
void SetBackgroundColor(COLORREF color);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the font attributes to be used for text within the preview handler.
|
||||
/// </summary>
|
||||
/// <param name="plf">A pointer to a <see cref="LOGFONT"/> Structure containing the necessary attributes for the font to use.</param>
|
||||
void SetFont(ref LOGFONT plf);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the color of the text within the preview handler.
|
||||
/// </summary>
|
||||
/// <param name="color">A value of type <see cref="COLORREF"/> to use for the preview handler text color.</param>
|
||||
void SetTextColor(COLORREF color);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,88 +1,88 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Common.ComInterlop
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the attributes of a font.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
|
||||
public struct LOGFONT
|
||||
{
|
||||
/// <summary>
|
||||
/// Value of type INT that specifies the height, in logical units, of the font's character cell or character.
|
||||
/// </summary>
|
||||
public int LfHeight;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type INT that specifies the width, in logical units, of characters in the font.
|
||||
/// </summary>
|
||||
public int LfWidth;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type INT that contains the angle, in tenths of degrees, between the escapement vector and the x-axis of the device. The escapement
|
||||
/// vector is parallel to the base line of a row of text.
|
||||
/// </summary>
|
||||
public int LfEscapement;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type INT that specifies the angle, in tenths of degrees, between each character's base line and the x-axis of the device.
|
||||
/// </summary>
|
||||
public int LfOrientation;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type INT that specifies the weight of the font in the range from 0 through 1000.
|
||||
/// </summary>
|
||||
public int LfWeight;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type BYTE that specifies an italic font if set to TRUE.
|
||||
/// </summary>
|
||||
public byte LfItalic;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type BYTE that specifies an underlined font if set to TRUE.
|
||||
/// </summary>
|
||||
public byte LfUnderline;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type BYTE that specifies a strikeout font if set to TRUE.
|
||||
/// </summary>
|
||||
public byte LfStrikeOut;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type BYTE that specifies the character set.
|
||||
/// </summary>
|
||||
public byte LfCharSet;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type BYTE that specifies the output precision. The output precision defines how closely the output must match the requested
|
||||
/// font's height, width, character orientation, escapement, pitch, and font type.
|
||||
/// </summary>
|
||||
public byte LfOutPrecision;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type BYTE that specifies the clipping precision. The clipping precision defines how to clip characters that are partially outside the clipping region.
|
||||
/// </summary>
|
||||
public byte LfClipPrecision;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type BYTE that specifies the output quality. The output quality defines how carefully the GDI must attempt to match the logical-font attributes to those of an actual physical font.
|
||||
/// </summary>
|
||||
public byte LfQuality;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type BYTE that specifies the pitch and family of the font.
|
||||
/// </summary>
|
||||
public byte LfPitchAndFamily;
|
||||
|
||||
/// <summary>
|
||||
/// Array of wide characters that contains a null-terminated string that specifies the typeface name of the font. The length of the string must not exceed 32 characters, including the NULL terminator.
|
||||
/// </summary>
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
|
||||
public string LfFaceName;
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Common.ComInterlop
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the attributes of a font.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
|
||||
public struct LOGFONT
|
||||
{
|
||||
/// <summary>
|
||||
/// Value of type INT that specifies the height, in logical units, of the font's character cell or character.
|
||||
/// </summary>
|
||||
public int LfHeight;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type INT that specifies the width, in logical units, of characters in the font.
|
||||
/// </summary>
|
||||
public int LfWidth;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type INT that contains the angle, in tenths of degrees, between the escapement vector and the x-axis of the device. The escapement
|
||||
/// vector is parallel to the base line of a row of text.
|
||||
/// </summary>
|
||||
public int LfEscapement;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type INT that specifies the angle, in tenths of degrees, between each character's base line and the x-axis of the device.
|
||||
/// </summary>
|
||||
public int LfOrientation;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type INT that specifies the weight of the font in the range from 0 through 1000.
|
||||
/// </summary>
|
||||
public int LfWeight;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type BYTE that specifies an italic font if set to TRUE.
|
||||
/// </summary>
|
||||
public byte LfItalic;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type BYTE that specifies an underlined font if set to TRUE.
|
||||
/// </summary>
|
||||
public byte LfUnderline;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type BYTE that specifies a strikeout font if set to TRUE.
|
||||
/// </summary>
|
||||
public byte LfStrikeOut;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type BYTE that specifies the character set.
|
||||
/// </summary>
|
||||
public byte LfCharSet;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type BYTE that specifies the output precision. The output precision defines how closely the output must match the requested
|
||||
/// font's height, width, character orientation, escapement, pitch, and font type.
|
||||
/// </summary>
|
||||
public byte LfOutPrecision;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type BYTE that specifies the clipping precision. The clipping precision defines how to clip characters that are partially outside the clipping region.
|
||||
/// </summary>
|
||||
public byte LfClipPrecision;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type BYTE that specifies the output quality. The output quality defines how carefully the GDI must attempt to match the logical-font attributes to those of an actual physical font.
|
||||
/// </summary>
|
||||
public byte LfQuality;
|
||||
|
||||
/// <summary>
|
||||
/// Value of type BYTE that specifies the pitch and family of the font.
|
||||
/// </summary>
|
||||
public byte LfPitchAndFamily;
|
||||
|
||||
/// <summary>
|
||||
/// Array of wide characters that contains a null-terminated string that specifies the typeface name of the font. The length of the string must not exceed 32 characters, including the NULL terminator.
|
||||
/// </summary>
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
|
||||
public string LfFaceName;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,51 +1,51 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Common.ComInterlop
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains message information from a thread's message queue.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct MSG
|
||||
{
|
||||
/// <summary>
|
||||
/// A handle to the window whose window procedure receives the message. This member is NULL when the message is a thread message.
|
||||
/// </summary>
|
||||
public IntPtr Hwnd;
|
||||
|
||||
/// <summary>
|
||||
/// The message identifier. Applications can only use the low word; the high word is reserved by the system.
|
||||
/// </summary>
|
||||
public int Message;
|
||||
|
||||
/// <summary>
|
||||
/// Additional information about the message. The exact meaning depends on the value of the message member.
|
||||
/// </summary>
|
||||
public IntPtr WParam;
|
||||
|
||||
/// <summary>
|
||||
/// Additional information about the message. The exact meaning depends on the value of the message member.
|
||||
/// </summary>
|
||||
public IntPtr LParam;
|
||||
|
||||
/// <summary>
|
||||
/// The time at which the message was posted.
|
||||
/// </summary>
|
||||
public int Time;
|
||||
|
||||
/// <summary>
|
||||
/// The x coordinate of cursor position, in screen coordinates, when the message was posted.
|
||||
/// </summary>
|
||||
public int PtX;
|
||||
|
||||
/// <summary>
|
||||
/// The y coordinate of cursor position, in screen coordinates, when the message was posted.
|
||||
/// </summary>
|
||||
public int PtY;
|
||||
}
|
||||
}
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Common.ComInterlop
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains message information from a thread's message queue.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct MSG
|
||||
{
|
||||
/// <summary>
|
||||
/// A handle to the window whose window procedure receives the message. This member is NULL when the message is a thread message.
|
||||
/// </summary>
|
||||
public IntPtr Hwnd;
|
||||
|
||||
/// <summary>
|
||||
/// The message identifier. Applications can only use the low word; the high word is reserved by the system.
|
||||
/// </summary>
|
||||
public int Message;
|
||||
|
||||
/// <summary>
|
||||
/// Additional information about the message. The exact meaning depends on the value of the message member.
|
||||
/// </summary>
|
||||
public IntPtr WParam;
|
||||
|
||||
/// <summary>
|
||||
/// Additional information about the message. The exact meaning depends on the value of the message member.
|
||||
/// </summary>
|
||||
public IntPtr LParam;
|
||||
|
||||
/// <summary>
|
||||
/// The time at which the message was posted.
|
||||
/// </summary>
|
||||
public int Time;
|
||||
|
||||
/// <summary>
|
||||
/// The x coordinate of cursor position, in screen coordinates, when the message was posted.
|
||||
/// </summary>
|
||||
public int PtX;
|
||||
|
||||
/// <summary>
|
||||
/// The y coordinate of cursor position, in screen coordinates, when the message was posted.
|
||||
/// </summary>
|
||||
public int PtY;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user