Skip to content

Commit d3862d3

Browse files
authored
Add code coverage for OleDragDropHandler.cs file (#13022)
Related #10773 ## Proposed changes - Add unit test file: OleDragDropHandlerTests.cs for public methods of the OleDragDropHandler.cs.
1 parent 1a16af9 commit d3862d3

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.ComponentModel;
5+
using System.ComponentModel.Design;
6+
using System.Drawing.Design;
7+
using Moq;
8+
9+
namespace System.Windows.Forms.Design.Tests;
10+
11+
public class OleDragDropHandlerTests : IDisposable
12+
{
13+
private readonly Mock<SelectionUIHandler> _selectionHandlerMock;
14+
private readonly Mock<IServiceProvider> _serviceProviderMock;
15+
private readonly Mock<IOleDragClient> _clientMock;
16+
private readonly OleDragDropHandler _oleDragDropHandler;
17+
private readonly IComponent _component;
18+
19+
public OleDragDropHandlerTests()
20+
{
21+
_selectionHandlerMock = new() { CallBase = true };
22+
_serviceProviderMock = new();
23+
_clientMock = new();
24+
_component = new Component();
25+
_oleDragDropHandler = new(_selectionHandlerMock.Object, _serviceProviderMock.Object, _clientMock.Object);
26+
}
27+
28+
public void Dispose()
29+
{
30+
_component.Dispose();
31+
}
32+
33+
[Fact]
34+
public void DataFormat_ShouldReturnCF_CODE()
35+
{
36+
OleDragDropHandler.DataFormat.Should().Be(OleDragDropHandler.CF_CODE);
37+
}
38+
39+
[Fact]
40+
public void ExtraInfoFormat_ShouldReturnCF_COMPONENTTYPES()
41+
{
42+
OleDragDropHandler.ExtraInfoFormat.Should().Be(OleDragDropHandler.CF_COMPONENTTYPES);
43+
}
44+
45+
[Fact]
46+
public void NestedToolboxItemFormat_ShouldReturnCF_TOOLBOXITEM()
47+
{
48+
OleDragDropHandler.NestedToolboxItemFormat.Should().Be(OleDragDropHandler.CF_TOOLBOXITEM);
49+
}
50+
51+
[Fact]
52+
public void Dragging_ShouldReturnFalseInitially()
53+
{
54+
_oleDragDropHandler.Dragging.Should().BeFalse();
55+
}
56+
57+
[Fact]
58+
public void FreezePainting_ShouldReturnFalseInitially()
59+
{
60+
OleDragDropHandler.FreezePainting.Should().BeFalse();
61+
}
62+
63+
[Fact]
64+
public void CreateTool_ShouldReturnEmptyArray_WhenCheckoutExceptionIsCanceled()
65+
{
66+
ToolboxItem toolboxItem = new();
67+
_serviceProviderMock.Setup(sp => sp.GetService(typeof(IDesignerHost))).Returns((IDesignerHost?)null);
68+
_serviceProviderMock.Setup(sp => sp.GetService(typeof(IToolboxService))).Returns((IToolboxService?)null);
69+
70+
var result = _oleDragDropHandler.CreateTool(toolboxItem, null, 0, 0, 0, 0, true, true);
71+
72+
result.Should().BeEmpty();
73+
}
74+
75+
[Fact]
76+
public void DoBeginDrag_ShouldReturnTrue_WhenRulesAreNotSizeable()
77+
{
78+
object[] components = [_component];
79+
bool result = _oleDragDropHandler.DoBeginDrag(components, SelectionRules.Moveable, 0, 0);
80+
81+
result.Should().BeTrue();
82+
}
83+
84+
[Fact]
85+
public void DoBeginDrag_ShouldReturnTrue_WhenRulesAreSizeable()
86+
{
87+
object[] components = [_component];
88+
bool result = _oleDragDropHandler.DoBeginDrag(components, SelectionRules.AllSizeable, 0, 0);
89+
90+
result.Should().BeTrue();
91+
}
92+
93+
[Fact]
94+
public void DoEndDrag_ShouldSetDraggingToFalse()
95+
{
96+
_oleDragDropHandler.DoEndDrag();
97+
98+
_oleDragDropHandler.Dragging.Should().BeFalse();
99+
}
100+
101+
[Fact]
102+
public void DoOleDragDrop_ShouldSetEffectToNone_WhenSelectionHandlerIsNotNull()
103+
{
104+
OleDragDropHandler handler = new(_selectionHandlerMock.Object, _serviceProviderMock.Object, _clientMock.Object);
105+
DragEventArgs dragEventArgs = new(new DataObject(), 0, 0, 0, DragDropEffects.None, DragDropEffects.None);
106+
107+
handler.DoOleDragDrop(dragEventArgs);
108+
109+
dragEventArgs.Effect.Should().Be(DragDropEffects.None);
110+
}
111+
112+
[Fact]
113+
public void DoOleDragEnter_ShouldSetEffectToNone_WhenDraggingIsFalseAndCannotDropDataObject()
114+
{
115+
DragEventArgs dragEventArgs = new(new DataObject(), 0, 0, 0, DragDropEffects.None, DragDropEffects.None);
116+
117+
_oleDragDropHandler.DoOleDragEnter(dragEventArgs);
118+
119+
dragEventArgs.Effect.Should().Be(DragDropEffects.None);
120+
}
121+
122+
[Fact]
123+
public void DoOleDragEnter_ShouldSetEffectToNone_WhenCannotModifyComponents()
124+
{
125+
DragEventArgs dragEventArgs = new(new DataObject(), 0, 0, 0, DragDropEffects.Move, DragDropEffects.None);
126+
_clientMock.Setup(client => client.CanModifyComponents).Returns(false);
127+
128+
_oleDragDropHandler.DoOleDragEnter(dragEventArgs);
129+
130+
dragEventArgs.Effect.Should().Be(DragDropEffects.None);
131+
}
132+
133+
[Fact]
134+
public void DoOleDragLeave_ShouldSetDraggingToFalse()
135+
{
136+
_oleDragDropHandler.DoOleDragLeave();
137+
138+
_oleDragDropHandler.Dragging.Should().BeFalse();
139+
}
140+
141+
[Fact]
142+
public void DoOleDragOver_ShouldSetEffectToNone_WhenDraggingIsFalseAndDragOkIsFalse()
143+
{
144+
DragEventArgs dragEventArgs = new(new DataObject(), 0, 0, 0, DragDropEffects.None, DragDropEffects.None);
145+
146+
_oleDragDropHandler.DoOleDragOver(dragEventArgs);
147+
148+
dragEventArgs.Effect.Should().Be(DragDropEffects.None);
149+
}
150+
151+
[Fact]
152+
public void DoOleDragOver_ShouldSetEffectToNone_WhenNotDraggingAndAllowedEffectIsNone()
153+
{
154+
DragEventArgs dragEventArgs = new(new DataObject(), 0, 0, 0, DragDropEffects.None, DragDropEffects.None);
155+
156+
_oleDragDropHandler.DoOleDragOver(dragEventArgs);
157+
158+
dragEventArgs.Effect.Should().Be(DragDropEffects.None);
159+
}
160+
161+
[Fact]
162+
public void DoOleGiveFeedback_ShouldSetUseDefaultCursorsToTrue_WhenEffectIsNotNoneAndSelectionHandlerIsNotNull()
163+
{
164+
GiveFeedbackEventArgs giveFeedbackEventArgs = new(DragDropEffects.Move, false);
165+
166+
_oleDragDropHandler.DoOleGiveFeedback(giveFeedbackEventArgs);
167+
168+
giveFeedbackEventArgs.UseDefaultCursors.Should().BeTrue();
169+
_selectionHandlerMock.Verify(handler => handler.SetCursor(), Times.Never);
170+
}
171+
172+
[Fact]
173+
public void GetDraggingObjects_ShouldReturnNull_WhenDataObjectIsNull()
174+
{
175+
OleDragDropHandler.GetDraggingObjects((IDataObject?)null).Should().BeNull();
176+
}
177+
178+
[Fact]
179+
public void GetDraggingObjects_ShouldReturnNull_WhenDragEventArgsDataIsNull()
180+
{
181+
DragEventArgs dragEventArgs = new(null, 0, 0, 0, DragDropEffects.None, DragDropEffects.None);
182+
183+
OleDragDropHandler.GetDraggingObjects(dragEventArgs).Should().BeNull();
184+
}
185+
}

0 commit comments

Comments
 (0)