pyWinAuto: c:\.projects\py_pywinauto\pywinauto\tests\translation.py

0001# GUI Application automation and testing library
0002# Copyright (C) 2006 Mark Mc Mahon
0003#
0004# This library is free software; you can redistribute it and/or
0005# modify it under the terms of the GNU Lesser General Public License
0006# as published by the Free Software Foundation; either version 2.1
0007# of the License, or (at your option) any later version.
0008#
0009# This library is distributed in the hope that it will be useful,
0010# but WITHOUT ANY WARRANTY; without even the implied warranty of
0011# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
0012# See the GNU Lesser General Public License for more details.
0013#
0014# You should have received a copy of the GNU Lesser General Public
0015# License along with this library; if not, write to the
0016#    Free Software Foundation, Inc.,
0017#    59 Temple Place,
0018#    Suite 330,
0019#    Boston, MA 02111-1307 USA
0020
0021
0022"""Translation Test
0023
0024**What is checked**
0025This checks for controls which appear not to be translated.
0026
0027**How is it checked**
0028It compares the text of the localised and reference controls.
0029
0030If there are more than string in the control then each item is searched for in
0031the US list of titles (so checking is not order dependent).
0032The indices for the untranslated strings are returned in a comma separated
0033string.
0034Also the untranslated strings themselves are returned (all as one string).
0035These strings are not escaped and are delimited as
0036"string1","string2",..."stringN".
0037
0038**When is a bug reported**
0039
0040 If the text of the localised control is identical to the reference control
0041 (in case, spacing i.e.  a binary compare) then it will be flagged as
0042 untranslated. Otherwise the control is treated as translated.
0043
0044Note: This is the method to return the least number of bugs. If there are
0045differences in any part of the string (e.g. a path or variable name) but the
0046rest of the string is untranslated then a bug will not be highlighted
0047
0048**Bug Extra Information**
0049The bug contains the following extra information
0050Name    Description
0051Strings         The list of the untranslated strings as explained above
0052StringIndices           The list of indices (0 based) that are untranslated.
0053This will usually be 0 but if there are many strings in the control
0054untranslated it will report ALL the strings e.g. 0,2,5,19,23
0055
0056**Is Reference dialog needed**
0057The reference dialog is always necessary.
0058
0059**False positive bug reports**
0060False positive bugs will be reported in the following cases.
0061-       The title of the control stays the same as the US because the
0062translation is the same as the English text(e.g. Name: in German)
0063-       The title of the control is not displayed (and not translated).
0064This can sometimes happen if the programmer displays something else on the
0065control after the dialog is created.
0066
0067**Test Identifier**
0068The identifier for this test/bug is "Translation" """
0069
0070__revision__ = "$Revision: 221 $"
0071
0072testname = "Translation"
0073
0074import re
0075
0076#-----------------------------------------------------------------------------
0077def TranslationTest(windows):
0078    "Returns just one bug for each control"
0079
0080    bugs = []
0081    for win in windows:
0082        if not win.ref:
0083            continue
0084
0085        # get if any items are untranslated
0086        untranTitles, untranIndices = _GetUntranslations(win)
0087
0088        if untranTitles:
0089            indicesAsString = ",".join([unicode(idx) for idx in untranIndices])
0090
0091            bugs.append((
0092                [win,],
0093                {
0094                    "StringIndices": indicesAsString,
0095                    "Strings": ('"%s"' % '","'.join(untranTitles))
0096                },
0097                testname,
0098                0)
0099            )
0100
0101
0102    return bugs
0103
0104def _GetUntranslations(win):
0105    "Find the text items that are not translated"
0106    # remove ampersands and other non translatable bits from the string
0107
0108    nonTransChars = re.compile(
0109        """(\&(?!\&)|   # ampersand not followed by an ampersand
0110            \.\.\.$|    # elipsis ...
0111            ^\s*|               # leading whitespace
0112            \s*$|               # trailing whitespace
0113            \s*:\s*$    # trailing colon (with/without whitespace)
0114            )*                  # repeated as often as necessary
0115            """, re.X)
0116
0117
0118    # clean each of the loc titles for comparison
0119    cleanedLocTitles = []
0120    for title in win.Texts():
0121        cleanedLocTitles.append(nonTransChars.sub("", title))
0122
0123    # clean each of the ref titles for comparison
0124    cleanedRefTitles = []
0125    for title in win.ref.Texts():
0126        cleanedRefTitles.append(nonTransChars.sub("", title))
0127
0128    untranslatedTitles = []
0129    untranslatedIndices = []
0130
0131    # loop over each of the cleaned loc title
0132    for index, title in enumerate(cleanedLocTitles):
0133
0134        # if the title is empty just skip it
0135        if not title:
0136            continue
0137
0138        # if that title is in the cleaned Ref Titles
0139        if title in cleanedRefTitles:
0140            # add this as one of the bugs
0141            untranslatedTitles.append(title)
0142            untranslatedIndices.append(index)
0143
0144    # return all the untranslated titles and thier indices
0145    return untranslatedTitles, untranslatedIndices
0146
0147
0148TranslationTest.TestsMenus = True