project.py 32.4 KB
Newer Older
1
2
#############################################################################
##
3
## Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
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
## Contact: http://www.qt-project.org/legal
##
## This file is part of Qt Creator.
##
## 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 Digia.  For licensing terms and
## conditions see http://qt.digia.com/licensing.  For further information
## use the contact form at http://qt.digia.com/contact-us.
##
## GNU Lesser General Public License Usage
## Alternatively, this file may be used under the terms of the GNU Lesser
## General Public License version 2.1 as published by the Free Software
## Foundation and appearing in the file LICENSE.LGPL included in the
## packaging of this file.  Please review the following information to
## ensure the GNU Lesser General Public License version 2.1 requirements
## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
##
## In addition, as a special exception, Digia gives you certain additional
## rights.  These rights are described in the Digia Qt LGPL Exception
## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
##
#############################################################################

30
import __builtin__
31
32
import re

Christian Stenger's avatar
Christian Stenger committed
33
def openQmakeProject(projectPath, targets=Targets.desktopTargetClasses(), fromWelcome=False):
34
    cleanUpUserFiles(projectPath)
35
36
37
    if fromWelcome:
        mouseClick(waitForObject(":OpenProject_QStyleItem"), 5, 5, 0, Qt.LeftButton)
        if not platform.system() == "Darwin":
38
            waitFor("waitForObject(':fileNameEdit_QLineEdit', 1000).focus == True", 3000)
39
40
    else:
        invokeMenuItem("File", "Open File or Project...")
41
    selectFromFileDialog(projectPath)
42
43
    try:
        # handle update generated files dialog
44
45
46
        waitForObject("{type='QLabel' name='qt_msgbox_label' visible='1' "
                      "text?='The following files are either outdated or have been modified*' "
                      "window={type='QMessageBox' unnamed='1' visible='1'}}", 3000)
47
48
49
        clickButton(waitForObject("{text='Yes' type='QPushButton' unnamed='1' visible='1'}"))
    except:
        pass
50
    checkedTargets = __chooseTargets__(targets)
51
    configureButton = waitForObject("{text='Configure Project' type='QPushButton' unnamed='1' visible='1'"
52
                                    "window=':Qt Creator_Core::Internal::MainWindow'}")
53
    clickButton(configureButton)
54
    return checkedTargets
55

56
def openCmakeProject(projectPath, buildDir):
57
    invokeMenuItem("File", "Open File or Project...")
58
    selectFromFileDialog(projectPath)
59
60
    replaceEditorContent("{type='Utils::BaseValidatingLineEdit' unnamed='1' visible='1'"
                         "window=':CMake Wizard_CMakeProjectManager::Internal::CMakeOpenProjectWizard'}", buildDir)
61
    clickButton(waitForObject(":CMake Wizard.Next_QPushButton"))
62
63
64
    return __handleCmakeWizardPage__()

def __handleCmakeWizardPage__():
65
    generatorCombo = waitForObject(":Generator:_QComboBox")
66
67
68
    mkspec = __getMkspecFromQmake__("qmake")
    test.log("Using mkspec '%s'" % mkspec)

69
    generatorText = "Unix Generator (Desktop 474 GCC)"
70
    if "win32-" in mkspec:
71
        generatorName = {"win32-g++" : "MinGW Generator (Desktop 474 GCC)",
72
                         "win32-msvc2010" : "NMake Generator (Desktop 480 MSVC2010)"}
73
        if mkspec in generatorName:
74
75
76
77
78
79
            generatorText = generatorName[mkspec]
    index = generatorCombo.findText(generatorText)
    if index == -1:
        test.warning("No matching CMake generator for mkspec '%s' found." % mkspec)
    else:
        generatorCombo.setCurrentIndex(index)
80

81
    clickButton(waitForObject(":CMake Wizard.Run CMake_QPushButton"))
Christian Stenger's avatar
Christian Stenger committed
82
83
84
85
86
87
88
89
90
91
    try:
        clickButton(waitForObject(":CMake Wizard.Finish_QPushButton", 60000))
    except LookupError:
        cmakeOutput = waitForObject("{type='QPlainTextEdit' unnamed='1' visible='1' "
                                    "window=':CMake Wizard_CMakeProjectManager::Internal::CMakeOpenProjectWizard'}")
        test.warning("Error while executing cmake - see details for cmake output.",
                     str(cmakeOutput.plainText))
        clickButton(waitForObject(":CMake Wizard.Cancel_QPushButton"))
        return False
    return True
92

93
94
95
96
# this function returns a list of available targets - this is not 100% error proof
# because the Simulator target is added for some cases even when Simulator has not
# been set up inside Qt versions/Toolchains
# this list can be used in __chooseTargets__()
97
def __createProjectOrFileSelectType__(category, template, fromWelcome = False, isProject=True):
98
99
100
101
    if fromWelcome:
        mouseClick(waitForObject(":CreateProject_QStyleItem"), 5, 5, 0, Qt.LeftButton)
    else:
        invokeMenuItem("File", "New File or Project...")
102
    categoriesView = waitForObject("{type='QTreeView' name='templateCategoryView'}")
103
104
105
106
    if isProject:
        clickItem(categoriesView, "Projects." + category, 5, 5, 0, Qt.LeftButton)
    else:
        clickItem(categoriesView, "Files and Classes." + category, 5, 5, 0, Qt.LeftButton)
107
    templatesView = waitForObject("{name='templatesView' type='QListView'}")
108
    clickItem(templatesView, template, 5, 5, 0, Qt.LeftButton)
109
    text = waitForObject("{type='QTextBrowser' name='templateDescription' visible='1'}").plainText
110
    clickButton(waitForObject("{text='Choose...' type='QPushButton' unnamed='1' visible='1'}"))
111
    return __getSupportedPlatforms__(str(text), template)[0]
112

113
def __createProjectSetNameAndPath__(path, projectName = None, checks = True):
114
    directoryEdit = waitForObject("{type='Utils::BaseValidatingLineEdit' unnamed='1' visible='1'}")
115
116
    replaceEditorContent(directoryEdit, path)
    projectNameEdit = waitForObject("{name='nameLineEdit' visible='1' "
117
                                    "type='Utils::ProjectNameValidatingLineEdit'}")
118
119
120
121
122
123
124
125
126
    if projectName == None:
        projectName = projectNameEdit.text
    else:
        replaceEditorContent(projectNameEdit, projectName)
    if checks:
        stateLabel = findObject("{type='QLabel' name='stateLabel'}")
        labelCheck = stateLabel.text=="" and stateLabel.styleSheet == ""
        test.verify(labelCheck, "Project name and base directory without warning or error")
    # make sure this is not set as default location
127
    ensureChecked("{type='QCheckBox' name='projectsDirectoryCheckBox' visible='1'}", False)
128
    clickButton(waitForObject(":Next_QPushButton"))
129
    return str(projectName)
130

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
def __createProjectHandleQtQuickSelection__(qtQuickVersion, withControls):
    comboBox = waitForObject("{type='QComboBox' unnamed='1' visible='1' "
                             "leftWidget={text='Qt Quick component set:' type='QLabel' unnamed='1' "
                             "visible='1'}}")
    if qtQuickVersion == 1:
        selectFromCombo(comboBox, "Qt Quick 1.1")
        if withControls:
            test.warning("Controls are not available for Quick 1.")
    elif qtQuickVersion == 2:
        if withControls:
            selectFromCombo(comboBox, "Qt Quick Controls 1.0")
        else:
            selectFromCombo(comboBox, "Qt Quick 2.0")
    else:
        test.fatal("Got unknown Qt Quick version: %s - trying to continue." % str(qtQuickVersion))
146
147
148
149
    label = waitForObject("{type='QLabel' unnamed='1' visible='1' text?='Creates a *' }")
    requires = re.match(".*Requires Qt (\d\.\d).*", str(label.text))
    if requires:
        requires = requires.group(1)
150
    clickButton(waitForObject(":Next_QPushButton"))
151
    return requires
152

153
154
# Selects the Qt versions for a project
# param checks turns tests in the function on if set to True
155
# param available a list holding the available targets
156
def __selectQtVersionDesktop__(checks, available=None):
Christian Stenger's avatar
Christian Stenger committed
157
    checkedTargets = __chooseTargets__(Targets.desktopTargetClasses(), available)
158
    if checks:
159
160
161
162
        cbObject = ("{type='QCheckBox' text='%s' unnamed='1' visible='1' "
                    "container={type='Utils::DetailsWidget' visible='1' unnamed='1'}}")
        verifyChecked(cbObject % "Debug")
        verifyChecked(cbObject % "Release")
163
    clickButton(waitForObject(":Next_QPushButton"))
164
    return checkedTargets
165

166
def __createProjectHandleLastPage__(expectedFiles = None, addToVersionControl = "<None>", addToProject = None):
167
    if expectedFiles != None:
168
169
170
171
172
173
        summary = waitForObject("{name='filesLabel' text?='<qt>Files to be added in<pre>*</pre>' "
                                "type='QLabel' visible='1'}").text
        verifyItemOrder(expectedFiles, summary)
    if addToProject:
        selectFromCombo(":projectComboBox_QComboBox", addToProject)
    selectFromCombo(":addToVersionControlComboBox_QComboBox", addToVersionControl)
174
    clickButton(waitForObject("{type='QPushButton' text~='(Finish|Done)' visible='1'}"))
175

176
177
178
179
180
181
def __verifyFileCreation__(path, expectedFiles):
    for filename in expectedFiles:
        if filename != path:
            filename = os.path.join(path, filename)
        test.verify(os.path.exists(filename), "Checking if '" + filename + "' was created")

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
def __modifyAvailableTargets__(available, requiredQt, asStrings=False):
    threeDigits = re.compile("\d{3}")
    requiredQtVersion = requiredQt.replace(".", "") + "0"
    tmp = list(available) # we need a deep copy
    for currentItem in tmp:
        if asStrings:
            item = currentItem
        else:
            item = Targets.getStringForTarget(currentItem)
        found = threeDigits.search(item)
        if found:
            if found.group(0) < requiredQtVersion:
                # Quick 1.1 supports 4.7.4 only for running, debugging is unsupported
                # so the least required version is 4.8, but 4.7.4 will be still listed
                if not (requiredQtVersion == "480" and found.group(0) == "474"):
                    available.remove(currentItem)
            if requiredQtVersion > "480":
                toBeRemoved = [Targets.EMBEDDED_LINUX, Targets.SIMULATOR]
                if asStrings:
                    toBeRemoved = Targets.getTargetsAsStrings(toBeRemoved)
                for t in toBeRemoved:
                    if t in available:
                        available.remove(t)

206
207
208
209
# Creates a Qt GUI project
# param path specifies where to create the project
# param projectName is the name for the new project
# param checks turns tests in the function on if set to True
210
def createProject_Qt_GUI(path, projectName, checks = True, addToVersionControl = "<None>"):
211
    template = "Qt Widgets Application"
212
    available = __createProjectOrFileSelectType__("  Applications", template)
213
    __createProjectSetNameAndPath__(path, projectName, checks)
214
    checkedTargets = __selectQtVersionDesktop__(checks, available)
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

    if checks:
        exp_filename = "mainwindow"
        h_file = exp_filename + ".h"
        cpp_file = exp_filename + ".cpp"
        ui_file = exp_filename + ".ui"
        pro_file = projectName + ".pro"

        waitFor("object.exists(':headerFileLineEdit_Utils::FileNameValidatingLineEdit')", 20000)
        waitFor("object.exists(':sourceFileLineEdit_Utils::FileNameValidatingLineEdit')", 20000)
        waitFor("object.exists(':formFileLineEdit_Utils::FileNameValidatingLineEdit')", 20000)

        test.compare(findObject(":headerFileLineEdit_Utils::FileNameValidatingLineEdit").text, h_file)
        test.compare(findObject(":sourceFileLineEdit_Utils::FileNameValidatingLineEdit").text, cpp_file)
        test.compare(findObject(":formFileLineEdit_Utils::FileNameValidatingLineEdit").text, ui_file)

231
    clickButton(waitForObject(":Next_QPushButton"))
232

233
    expectedFiles = None
234
    if checks:
235
236
        if platform.system() in ('Windows', 'Microsoft'):
            path = os.path.abspath(path)
Robert Loehning's avatar
Robert Loehning committed
237
        path = os.path.join(path, projectName)
238
        expectedFiles = [path]
239
        expectedFiles.extend(__sortFilenamesOSDependent__(["main.cpp", cpp_file, h_file, ui_file, pro_file]))
240
    __createProjectHandleLastPage__(expectedFiles, addToVersionControl)
241

242
    progressBarWait(20000)
243
    __verifyFileCreation__(path, expectedFiles)
244
    return checkedTargets
245

246
247
248
249
# Creates a Qt Console project
# param path specifies where to create the project
# param projectName is the name for the new project
# param checks turns tests in the function on if set to True
250
def createProject_Qt_Console(path, projectName, checks = True):
251
    available = __createProjectOrFileSelectType__("  Applications", "Qt Console Application")
252
    __createProjectSetNameAndPath__(path, projectName, checks)
253
    checkedTargets = __selectQtVersionDesktop__(checks, available)
254

255
256
257
258
259
260
261
    expectedFiles = None
    if checks:
        if platform.system() in ('Windows', 'Microsoft'):
            path = os.path.abspath(path)
        path = os.path.join(path, projectName)
        cpp_file = "main.cpp"
        pro_file = projectName + ".pro"
262
263
        expectedFiles = [path]
        expectedFiles.extend(__sortFilenamesOSDependent__([cpp_file, pro_file]))
264
    __createProjectHandleLastPage__(expectedFiles)
265

266
    progressBarWait(10000)
267
    __verifyFileCreation__(path, expectedFiles)
268
    return checkedTargets
269

270
def createNewQtQuickApplication(workingDir, projectName = None,
Christian Stenger's avatar
Christian Stenger committed
271
                                targets=Targets.desktopTargetClasses(), qtQuickVersion=1,
272
273
                                fromWelcome=False, withControls=False):
    available = __createProjectOrFileSelectType__("  Applications", "Qt Quick Application", fromWelcome)
274
    projectName = __createProjectSetNameAndPath__(workingDir, projectName)
275
276
    requiredQt = __createProjectHandleQtQuickSelection__(qtQuickVersion, withControls)
    __modifyAvailableTargets__(available, requiredQt)
277
    checkedTargets = __chooseTargets__(targets, available)
278
    snooze(1)
279
    clickButton(waitForObject(":Next_QPushButton"))
280
    __createProjectHandleLastPage__()
281

282
    progressBarWait(10000)
283
    return checkedTargets, projectName
284

285
286
def createNewQtQuickUI(workingDir, qtQuickVersion=1, withControls=False):
    __createProjectOrFileSelectType__("  Applications", "Qt Quick UI")
287
288
    if workingDir == None:
        workingDir = tempDir()
289
    projectName = __createProjectSetNameAndPath__(workingDir)
290
    __createProjectHandleQtQuickSelection__(qtQuickVersion, withControls)
291
    __createProjectHandleLastPage__()
292
    return projectName
293

294
295
296
def createNewQmlExtension(workingDir, targets=Targets.DESKTOP_474_GCC, qtQuickVersion=1):
    available = __createProjectOrFileSelectType__("  Libraries", "Qt Quick %d Extension Plugin"
                                                  % qtQuickVersion)
297
298
    if workingDir == None:
        workingDir = tempDir()
299
    __createProjectSetNameAndPath__(workingDir)
300
    checkedTargets = __chooseTargets__(targets, available)
301
    nextButton = waitForObject(":Next_QPushButton")
302
    clickButton(nextButton)
303
    nameLineEd = waitForObject("{buddy={type='QLabel' text='Object class-name:' unnamed='1' visible='1'} "
304
                               "type='QLineEdit' unnamed='1' visible='1'}")
305
306
    replaceEditorContent(nameLineEd, "TestItem")
    uriLineEd = waitForObject("{buddy={type='QLabel' text='URI:' unnamed='1' visible='1'} "
307
                              "type='QLineEdit' unnamed='1' visible='1'}")
Robert Loehning's avatar
Robert Loehning committed
308
    replaceEditorContent(uriLineEd, "org.qt-project.test.qmlcomponents")
309
    clickButton(nextButton)
310
    __createProjectHandleLastPage__()
311
    return checkedTargets
312

313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
def createEmptyQtProject(workingDir=None, projectName=None, targets=Targets.desktopTargetClasses()):
    __createProjectOrFileSelectType__("  Other Project", "Empty Qt Project")
    if workingDir == None:
        workingDir = tempDir()
    projectName = __createProjectSetNameAndPath__(workingDir, projectName)
    checkedTargets = __chooseTargets__(targets)
    snooze(1)
    clickButton(waitForObject(":Next_QPushButton"))
    __createProjectHandleLastPage__()
    return projectName, checkedTargets

def createNewNonQtProject(workingDir=None, projectName=None, target=Targets.DESKTOP_474_GCC,
                          plainC=False, cmake=False):
    if plainC:
        template = "Plain C Project"
    else:
        template = "Plain C++ Project"
    if cmake:
        template += " (CMake Build)"
    available = __createProjectOrFileSelectType__("  Non-Qt Project", template)
    if workingDir == None:
        workingDir = tempDir()
    projectName = __createProjectSetNameAndPath__(workingDir, projectName)
    if cmake:
        __createProjectHandleLastPage__()
        clickButton(waitForObject(":Next_QPushButton"))
        if not __handleCmakeWizardPage__():
            return None
    else:
        __chooseTargets__(target, availableTargets=available)
        clickButton(waitForObject(":Next_QPushButton"))
        __createProjectHandleLastPage__()
    return projectName

Christian Stenger's avatar
Christian Stenger committed
347
# parameter target can be an OR'd value of Targets
348
# parameter availableTargets should be the result of __createProjectOrFileSelectType__()
349
#           or use None as a fallback
350
def __chooseTargets__(targets=Targets.DESKTOP_474_GCC, availableTargets=None):
351
352
353
354
    if availableTargets != None:
        available = availableTargets
    else:
        # following targets depend on the build environment - added for further/later tests
355
356
        available = [Targets.DESKTOP_474_GCC, Targets.DESKTOP_480_GCC, Targets.DESKTOP_501_DEFAULT,
                     Targets.MAEMO5, Targets.EMBEDDED_LINUX, Targets.SIMULATOR, Targets.HARMATTAN]
357
        if platform.system() in ('Windows', 'Microsoft'):
Christian Stenger's avatar
Christian Stenger committed
358
            available.remove(Targets.EMBEDDED_LINUX)
359
            available.append(Targets.DESKTOP_480_MSVC2010)
360
361
362
    for target in filter(lambda x: x in available,
                         (Targets.MAEMO5, Targets.HARMATTAN)):
        available.remove(target)
363
    checkedTargets = []
364
365
366
    for current in available:
        mustCheck = targets & current == current
        try:
Christian Stenger's avatar
Christian Stenger committed
367
            ensureChecked("{type='QCheckBox' text='%s' visible='1'}" % Targets.getStringForTarget(current),
368
                          mustCheck, 3000)
369
370
            if (mustCheck):
                checkedTargets.append(current)
371
372
        except LookupError:
            if mustCheck:
Christian Stenger's avatar
Christian Stenger committed
373
                test.fail("Failed to check target '%s'." % Targets.getStringForTarget(current))
374
            else:
375
                # Simulator has been added without knowing whether configured or not - so skip warning here?
376
                if current != Targets.SIMULATOR:
Christian Stenger's avatar
Christian Stenger committed
377
                    test.warning("Target '%s' is not set up correctly." % Targets.getStringForTarget(current))
378
    return checkedTargets
379

380
def waitForProcessRunning(running=True):
381
382
383
384
385
386
387
    outputButton = waitForObject(":Qt Creator_AppOutput_Core::Internal::OutputPaneToggleButton")
    if not waitFor("outputButton.checked", 10000):
        ensureChecked(outputButton)
    waitFor("object.exists(':Qt Creator.ReRun_QToolButton')", 20000)
    reRunButton = findObject(":Qt Creator.ReRun_QToolButton")
    waitFor("object.exists(':Qt Creator.Stop_QToolButton')", 20000)
    stopButton = findObject(":Qt Creator.Stop_QToolButton")
388
    return waitFor("(reRunButton.enabled != running) and (stopButton.enabled == running)", 10000)
389

390
391
392
393
394
395
396
397
398
399
# run and close an application
# withHookInto - if set to True the function tries to attach to the sub-process instead of simply pressing Stop inside Creator
# executable - must be defined when using hook-into
# port - must be defined when using hook-into
# function - can be a string holding a function name or a reference to the function itself - this function will be called on
# the sub-process when hooking-into has been successful - if its missing simply closing the Qt Quick app will be done
# sType the SubprocessType - is nearly mandatory - except when using the function parameter
# userDefinedType - if you set sType to SubprocessType.USER_DEFINED you must(!) specify the WindowType for hooking into
# by yourself (or use the function parameter)
# ATTENTION! Make sure this function won't fail and the sub-process will end when the function returns
400
# returns None if the build failed, False if the subprocess did not start, and True otherwise
401
def runAndCloseApp(withHookInto=False, executable=None, port=None, function=None, sType=None, userDefinedType=None, quickVersion=1):
402
    runButton = waitForObject(":*Qt Creator.Run_Core::Internal::FancyToolButton")
403
404
    clickButton(runButton)
    if sType != SubprocessType.QT_QUICK_UI:
405
        waitForCompile(300000)
406
        buildSucceeded = checkLastBuild()
407
        ensureChecked(waitForObject(":Qt Creator_AppOutput_Core::Internal::OutputPaneToggleButton"))
408
409
        if not buildSucceeded:
            test.fatal("Build inside run wasn't successful - leaving test")
410
            return None
411
    if not waitForProcessRunning():
412
413
        test.fatal("Couldn't start application - leaving test")
        return False
414
    if sType == SubprocessType.QT_QUICK_UI and os.getenv("SYSTEST_QMLVIEWER_NO_HOOK_INTO", "0") == "1":
415
        withHookInto = False
416
    if withHookInto and not validType(sType, userDefinedType, quickVersion):
417
418
419
420
421
422
423
424
425
426
        if function != None:
            test.warning("You did not provide a valid value for the SubprocessType value - sType, but you have "
                         "provided a function to execute on the subprocess. Please ensure that your function "
                         "closes the subprocess before exiting, or this test will not complete.")
        else:
            test.warning("You did not provide a valid value for the SubprocessType value - sType, nor a "
                         "function to execute on the subprocess. Falling back to pushing the STOP button "
                         "inside creator to terminate execution of the subprocess.")
            withHookInto = False
    if withHookInto and not executable in ("", None):
427
        __closeSubprocessByHookingInto__(executable, port, function, sType, userDefinedType, quickVersion)
428
429
430
431
    else:
        __closeSubprocessByPushingStop__(sType)
    return True

432
def validType(sType, userDef, quickVersion):
433
434
    if sType == None:
        return False
435
    ty = SubprocessType.getWindowType(sType, quickVersion)
436
437
438
    return ty != None and not (ty == "user-defined" and (userDef == None or userDef.strip() == ""))

def __closeSubprocessByPushingStop__(sType):
439
    ensureChecked(":Qt Creator_AppOutput_Core::Internal::OutputPaneToggleButton")
440
441
442
443
    try:
        waitForObject(":Qt Creator.Stop_QToolButton", 5000)
    except:
        pass
444
445
446
447
    playButton = verifyEnabled(":Qt Creator.ReRun_QToolButton", False)
    stopButton = verifyEnabled(":Qt Creator.Stop_QToolButton")
    if stopButton.enabled:
        clickButton(stopButton)
448
449
        test.verify(waitFor("playButton.enabled", 5000), "Play button should be enabled")
        test.compare(stopButton.enabled, False, "Stop button should be disabled")
450
        if sType == SubprocessType.QT_QUICK_UI and platform.system() == "Darwin":
451
            waitFor("stopButton.enabled==False")
452
453
454
455
456
            snooze(2)
            nativeType("<Escape>")
    else:
        test.fatal("Subprocess does not seem to have been started.")

457
def __closeSubprocessByHookingInto__(executable, port, function, sType, userDefType, quickVersion):
458
    ensureChecked(":Qt Creator_AppOutput_Core::Internal::OutputPaneToggleButton")
459
    output = waitForObject("{type='Core::OutputWindow' visible='1' windowTitle='Application Output Window'}")
460
461
462
463
464
465
466
467
    if port == None:
        test.warning("I need a port number or attaching might fail.")
    else:
        waitFor("'Listening on port %d for incoming connectionsdone' in str(output.plainText)" % port, 5000)
    try:
        attachToApplication(executable)
    except:
        resetApplicationContextToCreator()
468
469
470
471
472
473
        if "Loading Qt Wrapper failed" in str(output.plainText):
            test.warning("Loading of Qt Wrapper failed - probably different Qt versions.",
                         "Resetting hook-into settings to continue.")
            # assuming we're still on the build settings of the current project (TODO)
            switchViewTo(ViewConstants.PROJECTS)
            if sType == SubprocessType.QT_QUICK_UI:
474
475
476
477
                if "qmlscene" in executable:
                    selectConfig = "QML Scene"
                else:
                    selectConfig = "QML Viewer"
478
479
480
481
482
483
            else:
                selectConfig = executable
            selectFromCombo(waitForObject("{buddy={text='Run configuration:' type='QLabel' "
                                          "unnamed='1' visible='1'} type='QComboBox' unnamed='1' "
                                          "visible='1'}"), selectConfig)
            switchViewTo(ViewConstants.EDIT)
484
            runButton = waitForObject(":*Qt Creator.Run_Core::Internal::FancyToolButton")
485
            clickButton(runButton)
486
            if not waitForProcessRunning():
487
488
489
490
491
492
493
                test.fatal("Something seems to be really wrong.", "Application output:"
                           % str(output.plainText))
                return False
            else:
                test.log("Application seems to be started without hooking-into.")
        else:
            test.warning("Could not attach to '%s' - using fallback of pushing STOP inside Creator." % executable)
494
495
496
497
498
499
        __closeSubprocessByPushingStop__(sType)
        return False
    if function == None:
        if sType==SubprocessType.USER_DEFINED:
            sendEvent("QCloseEvent", "{type='%s' unnamed='1' visible='1'}" % userDefType)
        else:
500
            sendEvent("QCloseEvent", "{type='%s' unnamed='1' visible='1'}" % SubprocessType.getWindowType(sType, quickVersion))
501
502
503
504
505
506
507
508
509
510
511
512
        resetApplicationContextToCreator()
    else:
        try:
            if isinstance(function, (str, unicode)):
                globals()[function]()
            else:
                function()
        except:
            test.fatal("Function to execute on sub-process could not be found.",
                       "Using fallback of pushing STOP inside Creator.")
            resetApplicationContextToCreator()
            __closeSubprocessByPushingStop__(sType)
513
    resetApplicationContextToCreator()
514
    if not (waitForProcessRunning(False) and waitFor("'exited with code' in str(output.plainText)", 10000)):
515
516
517
518
519
        test.warning("Sub-process seems not to have closed properly.")
        try:
            __closeSubprocessByPushingStop__(sType)
        except:
            pass
520
521
522
        if (platform.system() in ('Microsoft', 'Windows') and
            'Listening on port %d for incoming connectionsdone' % port not in str(output.plainText)):
            checkForStillRunningQmlExecutable([executable + ".exe"])
523
524
525
526
527
528
529
530
531
    return True

# this helper tries to reset the current application context back
# to creator - this strange work-around is needed _sometimes_ on MacOS
def resetApplicationContextToCreator():
    appCtxt = applicationContext("qtcreator")
    if appCtxt.name == "":
        appCtxt = applicationContext("Qt Creator")
    setApplicationContext(appCtxt)
532
533
534
535
536

# helper that examines the text (coming from the create project wizard)
# to figure out which available targets we have
# Simulator must be handled in a special way, because this depends on the
# configured Qt versions and Toolchains and cannot be looked up the same way
537
# if you set getAsStrings to True this function returns a list of strings instead
Christian Stenger's avatar
Christian Stenger committed
538
# of the constants defined in Targets
539
def __getSupportedPlatforms__(text, templateName, getAsStrings=False):
540
541
542
543
544
545
    reqPattern = re.compile("requires qt (?P<version>\d+\.\d+(\.\d+)?)", re.IGNORECASE)
    res = reqPattern.search(text)
    if res:
        version = res.group("version")
    else:
        version = None
546
547
548
549
    if 'Supported Platforms' in text:
        supports = text[text.find('Supported Platforms'):].split(":")[1].strip().split(" ")
        result = []
        if 'Desktop' in supports:
550
551
552
553
554
555
556
            if version == None or version < "5.0":
                result.append(Targets.DESKTOP_474_GCC)
                result.append(Targets.DESKTOP_480_GCC)
                if platform.system() in ("Linux", "Darwin"):
                    result.append(Targets.EMBEDDED_LINUX)
                elif platform.system() in ('Windows', 'Microsoft'):
                    result.append(Targets.DESKTOP_480_MSVC2010)
Christian Stenger's avatar
Christian Stenger committed
557
            result.append(Targets.DESKTOP_501_DEFAULT)
558
        if 'MeeGo/Harmattan' in supports:
Christian Stenger's avatar
Christian Stenger committed
559
            result.append(Targets.HARMATTAN)
560
        if 'Maemo/Fremantle' in supports:
Christian Stenger's avatar
Christian Stenger committed
561
            result.append(Targets.MAEMO5)
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
562
        if not ("BlackBerry" in templateName or re.search("custom Qt Creator plugin", text)) and (version == None or version < "5.0"):
Christian Stenger's avatar
Christian Stenger committed
563
            result.append(Targets.SIMULATOR)
564
    elif 'Platform independent' in text:
565
        # MAEMO5 and HARMATTAN could be wrong here - depends on having Madde plugin enabled or not
566
567
        result = [Targets.DESKTOP_474_GCC, Targets.DESKTOP_480_GCC, Targets.DESKTOP_501_DEFAULT,
                  Targets.MAEMO5, Targets.SIMULATOR, Targets.HARMATTAN]
Robert Loehning's avatar
Robert Loehning committed
568
        if platform.system() in ('Windows', 'Microsoft'):
569
            result.append(Targets.DESKTOP_480_MSVC2010)
570
571
572
    else:
        test.warning("Returning None (__getSupportedPlatforms__())",
                     "Parsed text: '%s'" % text)
573
574
        return None, None
    if getAsStrings:
Christian Stenger's avatar
Christian Stenger committed
575
        result = Targets.getTargetsAsStrings(result)
576
    return result, version
577
578
579
580

# copy example project (sourceExample is path to project) to temporary directory inside repository
def prepareTemplate(sourceExample):
    templateDir = os.path.abspath(tempDir() + "/template")
581
582
583
584
585
    try:
        shutil.copytree(sourceExample, templateDir)
    except:
        test.fatal("Error while copying '%s' to '%s'" % (sourceExample, templateDir))
        return None
586
    return templateDir
587
588
589
590
591
592
593

def __sortFilenamesOSDependent__(filenames):
    if platform.system() in ('Windows', 'Microsoft'):
        filenames.sort(key=str.lower)
    else:
        filenames.sort()
    return filenames
594

595
596
def __iterateChildren__(model, parent, nestingLevel=0):
    children = []
597
    for currentIndex in dumpIndices(model, parent):
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
        children.append([str(currentIndex.text), nestingLevel])
        if model.hasChildren(currentIndex):
            children.extend(__iterateChildren__(model, currentIndex, nestingLevel + 1))
    return children

# This will write the data to a file which can then be used for comparing
def __writeProjectTreeFile__(projectTree, filename):
    f = open(filename, "w+")
    f.write('"text"\t"nestinglevel"\n')
    for elem in projectTree:
        f.write('"%s"\t"%s"\n' % (elem[0], elem[1]))
    f.close()

def __getTestData__(record):
    return [testData.field(record, "text"),
            __builtin__.int(testData.field(record, "nestinglevel"))]

def compareProjectTree(rootObject, dataset):
    root = waitForObject(rootObject)
    tree = __iterateChildren__(root.model(), root)

    # __writeProjectTreeFile__(tree, dataset)

    for i, current in enumerate(map(__getTestData__, testData.dataset(dataset))):
        try:
            # Just removing everything up to the found item
            # Writing a pass would result in truly massive logs
            tree = tree[tree.index(current) + 1:]
        except ValueError:
            test.fail('Could not find "%s" with nesting level %s' % tuple(current),
                      'Line %s in dataset' % str(i + 1))
            return
    test.passes("No errors found in project tree")
631

632
def addCPlusPlusFileToCurrentProject(name, template, forceOverwrite=False, addToVCS = "<None>"):
633
634
635
636
637
638
639
640
641
642
643
    if name == None:
        test.fatal("File must have a name - got None.")
        return
    __createProjectOrFileSelectType__("  C++", template, isProject=False)
    window = "{type='Utils::FileWizardDialog' unnamed='1' visible='1'}"
    basePath = str(waitForObject("{type='Utils::BaseValidatingLineEdit' unnamed='1' visible='1' "
                                 "window=%s}" % window).text)
    lineEdit = waitForObject("{name='nameLineEdit' type='Utils::FileNameValidatingLineEdit' "
                             "visible='1' window=%s}" % window)
    replaceEditorContent(lineEdit, name)
    clickButton(waitForObject(":Next_QPushButton"))
644
    fileExistedBefore = os.path.exists(os.path.join(basePath, name))
645
    __createProjectHandleLastPage__(addToVersionControl = addToVCS)
646
    if (fileExistedBefore):
647
648
649
650
651
652
653
654
        overwriteDialog = "{type='Core::Internal::PromptOverwriteDialog' unnamed='1' visible='1'}"
        waitForObject(overwriteDialog)
        if forceOverwrite:
            buttonToClick = 'OK'
        else:
            buttonToClick = 'Cancel'
        clickButton("{text='%s' type='QPushButton' unnamed='1' visible='1' window=%s}"
                    % (buttonToClick, overwriteDialog))
655
656
657
658
659
660
661
662
663
664

def qt5SDKPath():
    if platform.system() in ('Microsoft', 'Windows'):
        return os.path.abspath("C:/Qt/Qt5.0.1/5.0.1/msvc2010")
    elif platform.system() == 'Linux':
        if __is64BitOS__():
            return os.path.expanduser("~/Qt5.0.1/5.0.1/gcc_64")
        return os.path.expanduser("~/Qt5.0.1/5.0.1/gcc")
    else:
        return os.path.expanduser("~/Qt5.0.1/5.0.1/clang_64")