Skip to content

Commit

Permalink
Add tests for GetMenuPath and owner drawn items.
Browse files Browse the repository at this point in the history
  • Loading branch information
vasily-v-ryabov committed Sep 21, 2015
1 parent db0868c commit 7c7525e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 30 deletions.
Binary file added apps/MFC_samples/BCDialogMenu.exe
Binary file not shown.
Binary file added apps/MFC_samples/x64/BCDialogMenu.exe
Binary file not shown.
2 changes: 1 addition & 1 deletion pywinauto/controls/menuwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def GetMenuPath(self, path, path_items = None, appdata = None, exact=False):
"Tools -> #0 -> Configure"
Text matching is done using a 'best match' fuzzy algorithm, so you don't
have to add all puntuation, elipses, etc.
have to add all punctuation, ellipses, etc.
"""

if path_items is None:
Expand Down
27 changes: 8 additions & 19 deletions pywinauto/unittests/test_HwndWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,25 @@
#import pdb
#import warnings

#import ctypes
import ctypes
import locale
import re

import sys, os
sys.path.append(".")
from pywinauto.application import Application
from pywinauto.controls.HwndWrapper import HwndWrapper
from pywinauto.controls.HwndWrapper import HwndWrapper, \
InvalidWindowHandle, GetDialogPropsFromHandle
from pywinauto import win32structures, win32defines
from pywinauto.findwindows import WindowNotFoundError
from pywinauto.sysinfo import is_x64_Python, is_x64_OS
from pywinauto.RemoteMemoryBlock import RemoteMemoryBlock
from pywinauto.timings import Timings, TimeoutError
from pywinauto import clipboard


__revision__ = "$Revision: 234 $"

try:
from pywinauto.controls.HwndWrapper import *
except ImportError:
# allow it to be imported in a dev environment
import sys

pywinauto_imp = "\\".join(__file__.split('\\')[:-3])
print("sdfdsf", pywinauto_imp)
sys.path.append(pywinauto_imp)
from pywinauto.controls.HwndWrapper import *

import unittest


mfc_samples_folder = os.path.join(
os.path.dirname(__file__), r"..\..\apps\MFC_samples")
if is_x64_Python():
Expand Down Expand Up @@ -178,7 +167,7 @@ def testCloseClick_bug(self):
Timings.closeclick_dialog_close_wait = .7
try:
self.app.AboutCalculator.CloseClick()
except timings.TimeoutError:
except TimeoutError:
pass

self.app.AboutCalculator.Close()
Expand Down Expand Up @@ -486,7 +475,7 @@ def tearDown(self):
# close the application
try:
self.dlg.Close(0.5)
except Exception: # timings.TimeoutError:
except Exception: # TimeoutError:
pass
finally:
self.app.kill_()
Expand Down Expand Up @@ -577,7 +566,7 @@ def tearDown(self):
if self.app.Notepad["Do&n't Save"].Exists():
self.app.Notepad["Do&n't Save"].Click()
self.app.Notepad["Do&n't Save"].WaitNot('visible')
except Exception: # timings.TimeoutError:
except Exception: # TimeoutError:
pass
finally:
self.app.kill_()
Expand Down
63 changes: 53 additions & 10 deletions pywinauto/unittests/test_menuwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@

"""Tests for Menu"""

import sys
import sys, os
sys.path.append(".")
from pywinauto.application import Application
from pywinauto.sysinfo import is_x64_Python, is_x64_OS
#from pywinauto.controls.HwndWrapper import HwndWrapper
#from pywinauto import win32structures, win32defines
#from pywinauto.controls import menuwrapper
from pywinauto.controls.menuwrapper import MenuItemNotEnabled

import unittest

mfc_samples_folder = os.path.join(
os.path.dirname(__file__), r"..\..\apps\MFC_samples")
if is_x64_Python():
mfc_samples_folder = os.path.join(mfc_samples_folder, 'x64')


class MenuWrapperTests(unittest.TestCase):
"Unit tests for the TreeViewWrapper class"
"Unit tests for the Menu and the MenuItem classes"

def setUp(self):
"""Start the application set some data and ensure the application
Expand All @@ -45,8 +52,6 @@ def setUp(self):

def tearDown(self):
"Close the application after tests"
# close the application
#self.dlg.TypeKeys("%{F4}")
self.app.kill_()


Expand All @@ -60,16 +65,54 @@ def testItemCount(self):
self.assertEquals(5, self.dlg.Menu().ItemCount())

def testItem(self):
pass
self.assertEquals(u'&File', self.dlg.Menu().Item(0).Text())

def testItems(self):
pass
self.assertEquals([u'&File', u'&Edit', u'F&ormat', u'&View', u'&Help'],
[item.Text() for item in self.dlg.Menu().Items()])

def testFriendlyClassName(self):
self.assertEquals('MenuItem', self.dlg.Menu().Item(0).FriendlyClassName())

def testMenuItemNotEnabled(self):
self.assertRaises(MenuItemNotEnabled, self.dlg.MenuSelect, 'Edit->Find Next')
self.assertRaises(MenuItemNotEnabled, self.dlg.MenuItem('Edit->Find Next').Click)

def testGetProperties(self):
pass
self.assertEquals({u'MenuItems': [{u'Index': 0, u'State': 0, u'Type': 0, u'ID': 64, u'Text': u'View &Help'},
{u'Index': 1, u'State': 3, u'Type': 2048, u'ID': 0, u'Text': u''},
{u'Index': 2, u'State': 0, u'Type': 0, u'ID': 65, u'Text': u'&About Notepad'}]},
self.dlg.Menu().GetMenuPath('Help')[0].SubMenu().GetProperties())

def testGetMenuPath(self):
pass
#print('ID = ' + str(self.dlg.Menu().GetMenuPath('Help->#3')[0].ID()))
self.assertEquals(u'&About Notepad', self.dlg.Menu().GetMenuPath('Help->#2')[-1].Text())
self.assertEquals(u'&About Notepad', self.dlg.Menu().GetMenuPath('Help->$65')[-1].Text())
self.assertEquals(u'&About Notepad', self.dlg.Menu().GetMenuPath('&Help->&About Notepad', exact=True)[-1].Text())

def test__repr__(self):
pass
print(self.dlg.Menu())
print(self.dlg.Menu().GetMenuPath('&Help->&About Notepad', exact=True)[-1])


class OwnerDrawnMenuTests(unittest.TestCase):
"Unit tests for the OWNERDRAW menu items"

def setUp(self):
"""Start the application set some data and ensure the application
is in the state we want it."""

self.app = Application().Start(os.path.join(mfc_samples_folder, u"BCDialogMenu.exe"))

self.dlg = self.app.BCDialogMenu

def tearDown(self):
"Close the application after tests"
self.app.kill_()

def testCorrectText(self):
self.assertEquals(u'&New \tCtrl+N', self.dlg.Menu().GetMenuPath('&File->#0')[-1].Text())
self.assertEquals(u'&Open... \tCtrl+O', self.dlg.Menu().GetMenuPath('&File->#1')[-1].Text())



Expand Down

0 comments on commit 7c7525e

Please sign in to comment.