test.py 7.74 KB
Newer Older
1
2
#############################################################################
##
Eike Ziller's avatar
Eike Ziller committed
3
4
## Copyright (C) 2015 The Qt Company Ltd.
## Contact: http://www.qt.io/licensing
5
6
7
8
9
10
11
##
## 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
Eike Ziller's avatar
Eike Ziller committed
12
13
## a written agreement between you and The Qt Company.  For licensing terms and
## conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
## use the contact form at http://www.qt.io/contact-us.
15
16
17
##
## GNU Lesser General Public License Usage
## Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
## General Public License version 2.1 or version 3 as published by the Free
## Software Foundation and appearing in the file LICENSE.LGPLv21 and
## LICENSE.LGPLv3 included in the packaging of this file.  Please review the
## following information to ensure the GNU Lesser General Public License
## requirements will be met: https://www.gnu.org/licenses/lgpl.html and
23
## http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
##
Eike Ziller's avatar
Eike Ziller committed
25
26
## In addition, as a special exception, The Qt Company gives you certain additional
## rights.  These rights are described in The Qt Company LGPL Exception
27
28
29
30
## version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
##
#############################################################################

31
32
33
34
35
source("../../shared/qtcreator.py")

def main():
    global testFolder
    cppEditorStr = ":Qt Creator_CppEditor::Internal::CPPEditorWidget"
36
    proEditorStr = ":Qt Creator_TextEditor::TextEditorWidget"
37
38
39
40
41
42
43
44
    testFolder = prepareTemplate(os.path.abspath(os.path.join(os.getcwd(), "..", "shared",
                                                          "simplePlainCPP")))
    if testFolder == None:
        test.fatal("Could not prepare test files - leaving test")
        return
    if not changeFilePermissions(testFolder, True, False, "testfiles.pro"):
        test.fatal("Could not set permissions for files to read-only - test will likely fail.")
    startApplication("qtcreator" + SettingsPath)
45
46
    if not startedWithoutPluginError():
        return
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
    openQmakeProject(os.path.join(testFolder, "testfiles.pro"))
    modifiedUnsaved = []
    readOnlyFiles = []
    currentFile = testModifyFile("testfiles.Sources.main\\.cpp", cppEditorStr, "{", True)
    modifiedUnsaved.append(currentFile)
    readOnlyFiles.append(currentFile)
    currentFile = testModifyFile("testfiles.testfiles\\.pro", proEditorStr, "CONFIG -= qt", False)
    modifiedUnsaved.append(currentFile)
    currentFile = testModifyFile("testfiles.Headers.testfile\\.h", cppEditorStr, "{", True)
    modifiedUnsaved.append(currentFile)
    readOnlyFiles.append(currentFile)
    invokeMenuItem("File", "Exit")
    testSaveChangesAndMakeWritable(modifiedUnsaved, readOnlyFiles)

def testModifyFile(fileName, editor, line, expectWarning):
    readOnlyWarningStr = ("{text='<b>Warning:</b> You are changing a read-only file.' type='QLabel'"
                      " unnamed='1' window=':Qt Creator_Core::Internal::MainWindow'}")
    simpleFName = simpleFileName(fileName)
    test.log("Opening file '%s'" % simpleFName)
    openDocument(fileName)
    fileNameCombo = waitForObject(":Qt Creator_FilenameQComboBox")
    test.compare(str(fileNameCombo.currentText), simpleFName,
                 "Verifying content of file name combo box.")
    checkOpenDocumentsContains(simpleFName)
    if not placeCursorToLine(editor, line):
        return
    type(editor, "<Return>")
    try:
        waitForObject(readOnlyWarningStr, 3000)
        if expectWarning:
            test.passes("Warning about changing a read-only file appeared.")
        else:
            test.fail("Warning about changing a read-only file appeared, although changing "
                      "a writable file. (%s)" % simpleFName)
    except:
        if expectWarning:
            test.fail("Warning about changing a read-only file missing. (%s)" % simpleFName)
        else:
            test.passes("Warning about changing a read-only file does not come up "
                        "(changing a writable file).")
    test.compare(str(fileNameCombo.currentText), "%s*" % simpleFName,
                 "Verifying content of file name combo box.")
    return checkOpenDocumentsContains("%s*" % simpleFName)

def testSaveChangesAndMakeWritable(modifiedFiles, readOnlyFiles):
    saveDlgStr = ("{name='Core__Internal__SaveItemsDialog' type='Core::Internal::SaveItemsDialog' "
                  "visible='1' windowTitle='Save Changes'}")
    try:
        waitForObject(saveDlgStr)
    except:
        test.fail("Save Changes dialog did not come up, but was expected to appear.")
        return
    treeWidget = waitForObject("{name='treeWidget' type='QTreeWidget' visible='1' window=%s}"
                               % saveDlgStr)
    checkUnsavedChangesContains(treeWidget.model(), modifiedFiles)
    clickButton(waitForObject("{text='Save All' type='QPushButton' unnamed='1' visible='1' "
                              "window=%s}" % saveDlgStr))
104
105
106
    try:
        filesTree = waitForObject("{name='treeWidget' type='QTreeWidget' visible='1' "
                                  "window=':WritePermissions_Core::Internal::ReadOnlyFilesDialog'}")
107
108
        items = map(os.path.expanduser, map(os.path.join, dumpItems(filesTree.model(), column=4),
                                            dumpItems(filesTree.model(), column=3)))
109
110
        test.compare(set(readOnlyFiles), set(items),
                     "Verifying whether all modified files without write permission are listed.")
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
        clickButton("{text='Change Permission' type='QPushButton' visible='1' unnamed='1' "
                    "window=':WritePermissions_Core::Internal::ReadOnlyFilesDialog'}")
    except:
        test.fatal("Missing dialog regarding missing permission on read only files.")
    exitCanceled = False
    try:
        mBoxStr = "{type='QMessageBox' unnamed='1' visible='1' text?='*Could not save the files.'}"
        msgBox = waitForObject(mBoxStr, 3000)
        test.fatal("Creator failed to set permissions.", str(msgBox.text))
        exitCanceled = True
        clickButton(waitForObject("{text='OK' type='QPushButton' unnamed='1' visible='1' "
                                  "window=%s}" % mBoxStr))
    except:
        for current in readOnlyFiles:
            test.verify(isWritable(current),
                        "Checking whether Creator made '%s' writable again." % current)
    if exitCanceled:
        invokeMenuItem("File", "Exit")
        test.log("Exiting without saving.")
        waitForObject(saveDlgStr)
        clickButton(waitForObject("{text='Do not Save' type='QPushButton' unnamed='1' "
                                  "visible='1' window=%s}" % saveDlgStr))
133

134
def checkOpenDocumentsContains(itemName):
135
    selectFromCombo(":Qt Creator_Core::Internal::NavComboBox", "Open Documents")
136
    openDocsTreeViewModel = waitForObject(":OpenDocuments_Widget").model()
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
    result = None
    found = False
    for index in dumpIndices(openDocsTreeViewModel):
        if str(index.data()) == itemName:
            found = True
            result = index.toolTip
            break
    test.verify(found, "Check whether 'Open Documents' contains '%s'" % itemName)
    return result

def checkUnsavedChangesContains(model, filePaths):
    foundItems = map(lambda x: os.path.join(x[0], x[1]), zip(dumpItems(model,column=1),
                                                             dumpItems(model, column=0)))
    test.compare(set(foundItems), set(filePaths),
                 "Verifying whether modified (unsaved) files do match expected.")

def cleanup():
    global testFolder
    if testFolder:
        changeFilePermissions(testFolder, True, True, "testfiles.pro")