Наша сборка Qt VS Tools
giy
2022-09-02 ca47896204482bf4a6979e3838bf7f09f61cebeb
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt VS Tools.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
 
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Win32;
 
namespace QtVsTools.Test.Package
{
    [TestClass]
    public class Test_QtVersionsPage
    {
        // UI automation property conditions
        string SetGlobals => @"
//# using System.IO
var elementSubtree = (TreeScope.Element | TreeScope.Subtree);
var isButton = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button);
var isDataGrid = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.DataGrid);
var isEdit = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit);
var isText = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text);
var isWindow = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window);";
 
        // Open menu: Tools > Options...
        string OpenVsOptions => @"
//# ui context VSROOT => ""MenuBar"", ""Tools""
//# ui pattern Invoke
//# ui context => ""Options...""
//# ui pattern Invoke";
 
        // Select options: page Qt > Versions
        string SelectQtVersionsPage => @"
//# ui context VSROOT => ""Options"", ""Qt""
//# ui pattern ExpandCollapse qtOptions
qtOptions.Expand();
//# ui context => ""Versions""
//# ui pattern SelectionItem qtVersions
qtVersions.Select();";
 
        // Get reference to data grid with Qt versions
        string GetQtVersionsTable => @"
//# ui context VSROOT => ""Options""
//# ui find => elementSubtree, isDataGrid
//# ui pattern Grid qtVersionsTable";
 
        // Add new row to versions table
        string AddNewRow => @"
var lastRow = qtVersionsTable.Current.RowCount - 1;
UiContext = qtVersionsTable.GetItem(lastRow, 1);
//# ui find => elementSubtree, isButton
//# ui pattern Invoke
{
    //# ui context VSROOT => ""Options""
    //# ui find => elementSubtree, isDataGrid
    //# ui pattern Grid qtVersionsTableAux
    qtVersionsTable = qtVersionsTableAux;
}
UiContext = qtVersionsTable.GetItem(lastRow, 1);
//# ui find => elementSubtree, isEdit
//# ui pattern Value newVersionName
newVersionName.SetValue(""TEST_"" + Path.GetRandomFileName());";
 
        // Set UI context to the path field of the new row
        string SelectNewRowPath => @"
UiContext = qtVersionsTable.GetItem(lastRow, 3);
//# ui find => elementSubtree, isEdit";
 
        // Save changes to the versions table and close the VS options dialog
        //  * Any error message will be copied to 'Result'
        string SaveChanges => @"
//# ui context VSROOT => ""Options"", ""OK""
//# ui pattern Invoke
//# thread ui
try {
    //# ui context VSROOT 100 => ""Options""
    //# ui find => elementSubtree, isWindow
} catch (TimeoutException) {
    return;
}
if (UiContext == null)
    return;
//# ui find => elementSubtree, isText
Result = UiContext.Current.Name;
//# ui context VSROOT => ""Options""
//# ui find => elementSubtree, isWindow
//# ui context => ""OK""
//# ui pattern Invoke
//# ui context VSROOT => ""Options"", ""Cancel""
//# ui pattern Invoke";
 
        // Add new variable 'qtPath' with the path to the Qt version in the top row
        //  * This is assumed to be a valid path to an existing Qt version
        string GetFirstRowPath => @"
if (qtVersionsTable.Current.RowCount <= 1) {
    Result = MACRO_ERROR_MSG(""No Qt version registered."");
    return;
}
UiContext = qtVersionsTable.GetItem(0, 3);
//# ui find => elementSubtree, isEdit
//# ui pattern Value path
string qtPath = path.Current.Value;
if (Path.GetFileName(qtPath).Equals(""qmake.exe"", StringComparison.InvariantCultureIgnoreCase))
    qtPath = Path.GetDirectoryName(qtPath);
if (Path.GetFileName(qtPath).Equals(""bin"", StringComparison.InvariantCultureIgnoreCase))
    qtPath = Path.GetDirectoryName(qtPath);";
 
        [TestMethod]
        // Add new (empty) row => error
        public void Test_EmptyVersion()
        {
            string result;
            using (var vs = QtVsTestClient.Attach()) {
                result = vs.RunMacro($@"
                    {SetGlobals}
                    {OpenVsOptions}
                    {SelectQtVersionsPage}
                    {GetQtVersionsTable}
                    {AddNewRow}
                    {SaveChanges}");
            }
            Assert.IsTrue(result.Contains("Invalid Qt versions"), result);
        }
 
        [TestMethod]
        // Add new row and copy the path from the top row => OK
        public void Test_AddNewVersion()
        {
            string result;
            using (var vs = QtVsTestClient.Attach()) {
                result = vs.RunMacro($@"
                    {SetGlobals}
                    {OpenVsOptions}
                    {SelectQtVersionsPage}
                    {GetQtVersionsTable}
                    {GetFirstRowPath}
                    {AddNewRow}
                    {SelectNewRowPath}
                    //# ui pattern Value newVersionPath
                    newVersionPath.SetValue(qtPath);
                    {SaveChanges}");
            }
            Assert.IsTrue(result.StartsWith(QtVsTestClient.MacroOk), result);
        }
 
        [TestMethod]
        // Add new row, copy the path from the top row, and append "qmake.exe" => OK
        public void Test_AddBinToPath()
        {
            string result;
            using (var vs = QtVsTestClient.Attach()) {
                result = vs.RunMacro($@"
                    {SetGlobals}
                    {OpenVsOptions}
                    {SelectQtVersionsPage}
                    {GetQtVersionsTable}
                    {GetFirstRowPath}
                    {AddNewRow}
                    {SelectNewRowPath}
                    //# ui pattern Value newVersionPath
                    newVersionPath.SetValue(Path.Combine(qtPath, ""bin""));
                    {SaveChanges}");
            }
            Assert.IsTrue(result.StartsWith(QtVsTestClient.MacroOk), result);
        }
 
        [TestMethod]
        // Add new row, copy the path from the top row, and append "bin\qmake.exe" => OK
        public void Test_AddBinQMakeToPath()
        {
            string result;
            using (var vs = QtVsTestClient.Attach()) {
                result = vs.RunMacro($@"
                    {SetGlobals}
                    {OpenVsOptions}
                    {SelectQtVersionsPage}
                    {GetQtVersionsTable}
                    {GetFirstRowPath}
                    {AddNewRow}
                    {SelectNewRowPath}
                    //# ui pattern Value newVersionPath
                    newVersionPath.SetValue(Path.Combine(qtPath, ""bin"", ""qmake.exe""));
                    {SaveChanges}");
            }
            Assert.IsTrue(result.StartsWith(QtVsTestClient.MacroOk), result);
        }
 
        [TestMethod]
        // Add new row, copy the path from the top row, and append "include" => ERROR
        public void Test_AddIncludeToPath()
        {
            string result;
            using (var vs = QtVsTestClient.Attach()) {
                result = vs.RunMacro($@"
                    {SetGlobals}
                    {OpenVsOptions}
                    {SelectQtVersionsPage}
                    {GetQtVersionsTable}
                    {GetFirstRowPath}
                    {AddNewRow}
                    {SelectNewRowPath}
                    //# ui pattern Value newVersionPath
                    newVersionPath.SetValue(Path.Combine(qtPath, ""include""));
                    {SaveChanges}");
            }
            Assert.IsTrue(result.Contains("Invalid Qt versions"), result);
        }
 
        [ClassCleanup]
        // Remove registry keys created during tests
        public static void RemoveTestKeys()
        {
            var qtVersions = Registry.CurrentUser
                .OpenSubKey(@"Software\Digia\Versions", writable: true);
            using (qtVersions) {
                var allVersions = qtVersions.GetSubKeyNames();
                var testVersions = allVersions.Where(k => k.StartsWith("TEST"));
                foreach (var testVersion in testVersions)
                    qtVersions.DeleteSubKey(testVersion);
                qtVersions.Close();
            }
        }
    }
}