From c14a4756a92586424f0f1a00c6134e441db50afd Mon Sep 17 00:00:00 2001
From: Robert Loehning <robert.loehning@nokia.com>
Date: Tue, 24 Jan 2012 17:47:12 +0100
Subject: [PATCH] Squish: Refactored openContextMenuOnTextCursorPosition()

Change-Id: Ie7a903794c11ed32cc1786c66ee0fa8d654a93f2
Reviewed-by: Christian Stenger <christian.stenger@nokia.com>
---
 tests/system/shared/editor_utils.py           | 24 +++++++++++++++++++
 .../suite_qtquick/tst_qml_editor/test.py      | 16 ++++---------
 2 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/tests/system/shared/editor_utils.py b/tests/system/shared/editor_utils.py
index c0260de7c13..9942ee8dee8 100644
--- a/tests/system/shared/editor_utils.py
+++ b/tests/system/shared/editor_utils.py
@@ -40,11 +40,35 @@ def placeCursorToLine(editor,line,isRegex=False):
     editor.setTextCursor(cursor)
     return True
 
+# this function returns True if a QMenu is
+# popped up above the given editor
+# param editor is the editor where the menu should appear
+# param menuInList is a list containing one item. This item will be assigned the menu if there is one.
+#                  THIS IS A HACK to get a pass-by-reference
+def menuVisibleAtEditor(editor, menuInList):
+    menuInList[0] = None
+    try:
+        menu = waitForObject("{type='QMenu' unnamed='1' visible='1'}", 200)
+        success = menu.visible and widgetContainsPoint(editor, menu.mapToGlobal(QPoint(0, 0)))
+        if success:
+            menuInList[0] = menu
+        return success
+    except:
+        return False
+
+# this function checks whether the given global point (QPoint)
+# is contained in the given widget
+def widgetContainsPoint(widget, point):
+    return QRect(widget.mapToGlobal(QPoint(0, 0)), widget.size).contains(point)
+
 # this function simply opens the context menu inside the given editor
 # at the same position where the text cursor is located at
 def openContextMenuOnTextCursorPosition(editor):
     rect = editor.cursorRect(editor.textCursor())
     openContextMenu(editor, rect.x+rect.width/2, rect.y+rect.height/2, 0)
+    menuInList = [None]
+    waitFor("menuVisibleAtEditor(editor, menuInList)", 5000)
+    return menuInList[0]
 
 # this function marks/selects the text inside the given editor from position
 # startPosition to endPosition (both inclusive)
diff --git a/tests/system/suite_qtquick/tst_qml_editor/test.py b/tests/system/suite_qtquick/tst_qml_editor/test.py
index 209a8ab18d6..45e39b948f2 100644
--- a/tests/system/suite_qtquick/tst_qml_editor/test.py
+++ b/tests/system/suite_qtquick/tst_qml_editor/test.py
@@ -63,12 +63,8 @@ def testRenameId():
         return False
     type(editor, "<Down>")
     searchFinished = False
-    if platform.system() == "Darwin":
-        invokeMenuItem("Tools", "QML/JS", "Rename Symbol Under Cursor")
-    else:
-        openContextMenuOnTextCursorPosition(editor)
-        ctxtMenu = waitForObject("{type='QMenu' visible='1' unnamed='1'}")
-        activateItem(waitForObjectItem(objectMap.realName(ctxtMenu), "Rename Symbol Under Cursor"))
+    ctxtMenu = openContextMenuOnTextCursorPosition(editor)
+    activateItem(waitForObjectItem(objectMap.realName(ctxtMenu), "Rename Symbol Under Cursor"))
     waitFor("searchFinished")
     type(waitForObject("{leftWidget={text='Replace with:' type='QLabel' unnamed='1' visible='1'} "
                        "type='Find::Internal::WideEnoughLineEdit' unnamed='1' visible='1' "
@@ -100,12 +96,8 @@ def __invokeFindUsage__(treeView, filename, line, additionalKeyPresses, expected
     for ty in additionalKeyPresses:
         type(editor, ty)
     searchFinished = False
-    if platform.system() == "Darwin":
-        invokeMenuItem("Tools", "QML/JS", "Find Usages")
-    else:
-        openContextMenuOnTextCursorPosition(editor)
-        ctxtMenu = waitForObject("{type='QMenu' visible='1' unnamed='1'}")
-        activateItem(waitForObjectItem(objectMap.realName(ctxtMenu), "Find Usages"))
+    ctxtMenu = openContextMenuOnTextCursorPosition(editor)
+    activateItem(waitForObjectItem(objectMap.realName(ctxtMenu), "Find Usages"))
     waitFor("searchFinished")
     validateSearchResult(expectedCount)
 
-- 
GitLab