-
Notifications
You must be signed in to change notification settings - Fork 4k
/
CSharpWinForms.cs
169 lines (156 loc) · 9.82 KB
/
CSharpWinForms.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.VisualStudio.IntegrationTest.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
using ProjectUtils = Microsoft.VisualStudio.IntegrationTest.Utilities.Common.ProjectUtils;
namespace Roslyn.VisualStudio.IntegrationTests.CSharp
{
[Collection(nameof(SharedIntegrationHostFixture))]
public class CSharpWinForms : AbstractEditorTest
{
protected override string LanguageName => LanguageNames.CSharp;
public CSharpWinForms(VisualStudioInstanceFactory instanceFactory)
: base(instanceFactory, nameof(CSharpWinForms), WellKnownProjectTemplates.WinFormsApplication)
{
}
[WpfFact, Trait(Traits.Feature, Traits.Features.WinForms)]
public void AddControl()
{
var project = new ProjectUtils.Project(ProjectName);
VisualStudio.SolutionExplorer.OpenFileWithDesigner(project, "Form1.cs");
VisualStudio.Editor.AddWinFormButton("SomeButton");
VisualStudio.SolutionExplorer.SaveFile(project, "Form1.cs");
VisualStudio.SolutionExplorer.OpenFile(project, "Form1.Designer.cs");
var actualText = VisualStudio.Editor.GetText();
Assert.Contains(@"this.SomeButton.Name = ""SomeButton""", actualText);
Assert.Contains(@"private System.Windows.Forms.Button SomeButton;", actualText);
}
[WpfFact, Trait(Traits.Feature, Traits.Features.WinForms)]
public void ChangeControlProperty()
{
var project = new ProjectUtils.Project(ProjectName);
VisualStudio.SolutionExplorer.OpenFileWithDesigner(project, "Form1.cs");
VisualStudio.Editor.AddWinFormButton("SomeButton");
VisualStudio.Editor.EditWinFormButtonProperty(buttonName: "SomeButton", propertyName: "Text", propertyValue: "NewButtonText");
VisualStudio.SolutionExplorer.CloseDesignerFile(project, "Form1.cs", saveFile: true);
VisualStudio.SolutionExplorer.OpenFile(project, "Form1.Designer.cs");
var actualText = VisualStudio.Editor.GetText();
Assert.Contains(@"this.SomeButton.Text = ""NewButtonText""", actualText);
}
[WpfFact, Trait(Traits.Feature, Traits.Features.WinForms)]
public void ChangeControlPropertyInCode()
{
var project = new ProjectUtils.Project(ProjectName);
VisualStudio.SolutionExplorer.OpenFileWithDesigner(project, "Form1.cs");
VisualStudio.Editor.AddWinFormButton("SomeButton");
VisualStudio.Editor.EditWinFormButtonProperty(buttonName: "SomeButton", propertyName: "Text", propertyValue: "ButtonTextGoesHere");
var expectedPropertyValue = "ButtonTextGoesHere";
var actualPropertyValue = VisualStudio.Editor.GetWinFormButtonPropertyValue(buttonName: "SomeButton", propertyName: "Text");
Assert.Equal(expectedPropertyValue, actualPropertyValue);
VisualStudio.SolutionExplorer.CloseDesignerFile(project, "Form1.cs", saveFile: true);
// Change the control's text in designer.cs code
VisualStudio.SolutionExplorer.OpenFile(project, "Form1.Designer.cs");
// Verify that the control's property was set correctly. The following text should appear in InitializeComponent().
var actualText = VisualStudio.Editor.GetText();
Assert.Contains(@"this.SomeButton.Text = ""ButtonTextGoesHere"";", actualText);
// Replace text property with something else
VisualStudio.Editor.SelectTextInCurrentDocument(@"this.SomeButton.Text = ""ButtonTextGoesHere"";");
VisualStudio.Editor.SendKeys(@"this.SomeButton.Text = ""GibberishText"";");
VisualStudio.SolutionExplorer.CloseCodeFile(project, "Form1.Designer.cs", saveFile: true);
// Verify that the control text has changed in the designer
VisualStudio.SolutionExplorer.OpenFileWithDesigner(project, "Form1.cs");
expectedPropertyValue = "GibberishText";
actualPropertyValue = VisualStudio.Editor.GetWinFormButtonPropertyValue(buttonName: "SomeButton", propertyName: "Text");
Assert.Equal(expectedPropertyValue, actualPropertyValue);
}
[WpfFact, Trait(Traits.Feature, Traits.Features.WinForms)]
public void AddClickHandler()
{
var project = new ProjectUtils.Project(ProjectName);
VisualStudio.SolutionExplorer.OpenFileWithDesigner(project, "Form1.cs");
VisualStudio.Editor.AddWinFormButton("SomeButton");
VisualStudio.Editor.EditWinFormButtonEvent(buttonName: "SomeButton", eventName: "Click", eventHandlerName: "ExecuteWhenButtonClicked");
VisualStudio.SolutionExplorer.OpenFile(project, "Form1.Designer.cs");
var designerActualText = VisualStudio.Editor.GetText();
Assert.Contains(@"this.SomeButton.Click += new System.EventHandler(this.ExecuteWhenButtonClicked);", designerActualText);
VisualStudio.SolutionExplorer.OpenFile(project, "Form1.cs");
var codeFileActualText = VisualStudio.Editor.GetText();
Assert.Contains(@" public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ExecuteWhenButtonClicked(object sender, EventArgs e)
{
}
}", codeFileActualText);
}
[WpfFact, Trait(Traits.Feature, Traits.Features.WinForms)]
public void RenameControl()
{
var project = new ProjectUtils.Project(ProjectName);
VisualStudio.SolutionExplorer.OpenFileWithDesigner(project, "Form1.cs");
VisualStudio.Editor.AddWinFormButton("SomeButton");
// Add some control properties and events
VisualStudio.Editor.EditWinFormButtonProperty(buttonName: "SomeButton", propertyName: "Text", propertyValue: "ButtonTextValue");
VisualStudio.Editor.EditWinFormButtonEvent(buttonName: "SomeButton", eventName: "Click", eventHandlerName: "SomeButtonHandler");
// Rename the control
VisualStudio.Editor.EditWinFormButtonProperty(buttonName: "SomeButton", propertyName: "Name", propertyValue: "SomeNewButton");
VisualStudio.ErrorList.Verify.NoBuildErrors();
// Verify that the rename propagated in designer code
VisualStudio.SolutionExplorer.OpenFile(project, "Form1.Designer.cs");
var actualText = VisualStudio.Editor.GetText();
Assert.Contains(@"this.SomeNewButton.Name = ""SomeNewButton"";", actualText);
Assert.Contains(@"this.SomeNewButton.Text = ""ButtonTextValue"";", actualText);
Assert.Contains(@"this.SomeNewButton.Click += new System.EventHandler(this.SomeButtonHandler);", actualText);
Assert.Contains(@"private System.Windows.Forms.Button SomeNewButton;", actualText);
// Verify that the old button name goes away
Assert.DoesNotContain(@"private System.Windows.Forms.Button SomeButton;", actualText);
}
[WpfFact, Trait(Traits.Feature, Traits.Features.WinForms)]
public void RemoveEventHandler()
{
var project = new ProjectUtils.Project(ProjectName);
VisualStudio.SolutionExplorer.OpenFileWithDesigner(project, "Form1.cs");
VisualStudio.Editor.AddWinFormButton("SomeButton");
VisualStudio.Editor.EditWinFormButtonEvent(buttonName: "SomeButton", eventName: "Click", eventHandlerName: "GooHandler");
// Remove the event handler
VisualStudio.Editor.EditWinFormButtonEvent(buttonName: "SomeButton", eventName: "Click", eventHandlerName: "");
VisualStudio.ErrorList.Verify.NoBuildErrors();
// Verify that the handler is removed
VisualStudio.SolutionExplorer.OpenFile(project, "Form1.Designer.cs");
var actualText = VisualStudio.Editor.GetText();
Assert.DoesNotContain(@"VisualStudio.Editor.SomeButton.Click += new System.EventHandler(VisualStudio.Editor.GooHandler);", actualText);
}
[WpfFact, Trait(Traits.Feature, Traits.Features.WinForms)]
public void ChangeAccessibility()
{
var project = new ProjectUtils.Project(ProjectName);
VisualStudio.SolutionExplorer.OpenFileWithDesigner(project, "Form1.cs");
VisualStudio.Editor.AddWinFormButton("SomeButton");
VisualStudio.Editor.EditWinFormButtonProperty(buttonName: "SomeButton", propertyName: "Modifiers",
propertyTypeName: "System.CodeDom.MemberAttributes",
propertyValue: "Public");
VisualStudio.ErrorList.Verify.NoBuildErrors();
VisualStudio.SolutionExplorer.OpenFile(project, "Form1.Designer.cs");
var actualText = VisualStudio.Editor.GetText();
Assert.Contains(@"public System.Windows.Forms.Button SomeButton;", actualText);
}
[WpfFact, Trait(Traits.Feature, Traits.Features.WinForms)]
public void DeleteControl()
{
var project = new ProjectUtils.Project(ProjectName);
VisualStudio.SolutionExplorer.OpenFileWithDesigner(project, "Form1.cs");
VisualStudio.Editor.AddWinFormButton("SomeButton");
VisualStudio.Editor.DeleteWinFormButton("SomeButton");
VisualStudio.ErrorList.Verify.NoBuildErrors();
VisualStudio.SolutionExplorer.OpenFile(project, "Form1.Designer.cs");
var actualText = VisualStudio.Editor.GetText();
Assert.DoesNotContain(@"VisualStudio.Editor.SomeButton.Name = ""SomeButton"";", actualText);
Assert.DoesNotContain(@"private System.Windows.Forms.Button SomeButton;", actualText);
}
}
}