summaryrefslogtreecommitdiff
path: root/sw/qa/uitest/librelogo/run.py
blob: 6bcca4ade8a88207643133ad29305c11ef808f5e (plain)
1
2
3
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

from uitest.framework import UITestCase
from uitest.uihelper.common import type_text
from com.sun.star.awt.FontSlant import NONE as __Slant_NONE__
from com.sun.star.awt.FontSlant import ITALIC as __Slant_ITALIC__
from com.sun.star.awt.FontUnderline import NONE as __Underline_NONE__
from com.sun.star.awt.FontUnderline import SINGLE as __Underline_SINGLE__
from com.sun.star.awt.FontStrikeout import NONE as __Strikeout_NONE__
from com.sun.star.awt.FontStrikeout import SINGLE as __Strikeout_SINGLE__

class LibreLogoTest(UITestCase):
   LIBRELOGO_PATH = "vnd.sun.star.script:LibreLogo|LibreLogo.py$%s?language=Python&location=share"

   def createMasterScriptProviderFactory(self):
       xServiceManager = self.xContext.ServiceManager
       return xServiceManager.createInstanceWithContext(
           "com.sun.star.script.provider.MasterScriptProviderFactory",
           self.xContext)

   def getScript(self, command):
       xMasterScriptProviderFactory = self.createMasterScriptProviderFactory()
       document = self.ui_test.get_component()
       xScriptProvider = xMasterScriptProviderFactory.createScriptProvider(document)
       xScript = xScriptProvider.getScript(self.LIBRELOGO_PATH %command)
       self.assertIsNotNone(xScript, "xScript was not loaded")
       return xScript

   def logo(self, command):
        self.xUITest.executeCommand(self.LIBRELOGO_PATH %command)

   def test_librelogo(self):
        with self.ui_test.create_doc_in_start_center("writer") as document:
            xWriterDoc = self.xUITest.getTopFocusWindow()
            xWriterEdit = xWriterDoc.getChild("writer_edit")
            # to check the state of LibreLogo program execution
            xIsAlive = self.getScript("__is_alive__")

            # run a program with basic drawing commands FORWARD and RIGHT
            # using their abbreviated names FD and RT
            type_text(xWriterEdit, "fd 100 rt 45 fd 100")
            self.logo("run")
            # wait for LibreLogo program termination
            while xIsAlive.invoke((), (), ())[0]:
                pass
            # check shape count for
            # a) program running:
            # - turtle shape: result of program start
            # - line shape: result of turtle drawing
            # b) continuous line drawing (the regression
            # related to the fix of tdf#106792 resulted shorter line
            # segments than the turtle path and non-continuous line
            # drawing, ie. in this example, three line shapes
            # instead of a single one. See its fix in
            # commit 502e8785085f9e8b54ee383080442c2dcaf95b15)
            self.assertEqual(document.DrawPage.getCount(), 2)

            # check formatting by "magic wand"
            self.logo("__translate__")
            # a) check expansion of abbreviated commands : fd -> FORWARD, rt -> RIGHT,
            # b) check line breaking (fix for tdf#100941: new line instead of the text "\" and "n")
            self.assertEqual(document.Text.String.replace('\r\n', '\n'), "\nFORWARD 100 RIGHT 45 FORWARD 100")
            # c) check usage of real paragraphs instead of line break (tdf#120422)
            # first paragraph is empty (for working page break)
            self.assertEqual(document.Text.createEnumeration().nextElement().String, "")

            # function definitions and calls can be in arbitrary order
            document.Text.String = """
; dragon curve
TO x n
IF n = 0 [ STOP ]
x n-1
RIGHT 90
y n-1 ; it worked only as "y(n-1)"
FORWARD 10
END

TO y n
IF n = 0 [ STOP ]
FORWARD 10
x n-1
LEFT 90
y n-1
END

PICTURE ; start new line draw
x 3 ; draw only a few levels
"""
            self.logo("run")
            # wait for LibreLogo program termination
            while xIsAlive.invoke((), (), ())[0]:
                pass
            # new shape + previous two ones = 3
            self.assertEqual(document.DrawPage.getCount(), 3)

   def test_LABEL(self):
        with self.ui_test.create_doc_in_start_center("writer") as document:
            xWriterDoc = self.xUITest.getTopFocusWindow()
            xWriterEdit = xWriterDoc.getChild("writer_edit")
            # to check the state of LibreLogo program execution
            xIsAlive = self.getScript("__is_alive__")

            #1 run a program with basic LABEL command

            type_text(xWriterEdit, "LABEL 'Hello, World!'")
            self.logo("run")
            # wait for LibreLogo program termination
            while xIsAlive.invoke((), (), ())[0]:
                pass

            # turtle and text shape
            self.assertEqual(document.DrawPage.getCount(), 2)
            textShape = document.DrawPage.getByIndex(1)
            # text in the text shape
            self.assertEqual(textShape.getString(), "Hello, World!")

            #2 check italic, bold, underline + red and blue formatting

            document.Text.String = "CLEARSCREEN LABEL '<i><red>Hello</red>, <bold><blue>W<u>orld</blue></bold>!</i></u>'"
            self.logo("run")
            # wait for LibreLogo program termination
            while xIsAlive.invoke((), (), ())[0]:
                pass

            # turtle and text shape
            self.assertEqual(document.DrawPage.getCount(), 2)
            textShape = document.DrawPage.getByIndex(1)
            # text in the text shape
            self.assertEqual(textShape.getString(), "Hello, World!")
            # check portion formatting
            c = textShape.createTextCursor()
            c.gotoStart(False)
            # before character "H"
            self.assertEqual(c.CharPosture, __Slant_ITALIC__) # cursive
            self.assertEqual(c.CharUnderline, __Underline_NONE__) # no underline
            self.assertEqual(c.CharWeight, 100) # normal weight
            self.assertEqual(c.CharColor, 0xFF0000) # red color
            # after character " "
            c.goRight(6, False)
            self.assertEqual(c.CharPosture, __Slant_ITALIC__) # cursive
            self.assertEqual(c.CharUnderline, __Underline_NONE__) # no underline
            self.assertEqual(c.CharWeight, 100) # normal weight
            self.assertEqual(c.CharColor, 0x000000) # black color
            # after character "W"
            c.goRight(2, False)
            self.assertEqual(c.CharPosture, __Slant_ITALIC__) # cursive
            self.assertEqual(c.CharUnderline, __Underline_NONE__) # no underline
            self.assertEqual(c.CharWeight, 150) # bold
            self.assertEqual(c.CharColor, 0x0000FF) # blue color
            # 9th: after character "o"
            c.goRight(1, False)
            self.assertEqual(c.CharPosture, __Slant_ITALIC__) # cursive
            self.assertEqual(c.CharUnderline, __Underline_SINGLE__) # underline
            self.assertEqual(c.CharWeight, 150) # bold
            self.assertEqual(c.CharColor, 0x0000FF) # blue color
            # last: after character "!"
            c.gotoEnd(False)
            self.assertEqual(c.CharPosture, __Slant_ITALIC__) # cursive
            self.assertEqual(c.CharUnderline, __Underline_SINGLE__) # underline
            self.assertEqual(c.CharWeight, 100) # normal weight
            self.assertEqual(c.CharColor, 0x000000) # black color

            #2 check strike out, sub, sup, font name and font size formatting

            document.Text.String = (
                "CLEARSCREEN FONTFAMILY 'Linux Biolinum G' FONTSIZE 12 " +
                "LABEL '<s>x</s>, <sub>x</sub>, <sup>x</sup>, " +
                    "<FONTFAMILY Liberation Sans>x</FONTFAMILY>, " +
                    "<FONTHEIGHT 20>x</FONTHEIGHT>...'" )

            self.logo("run")
            # wait for LibreLogo program termination
            while xIsAlive.invoke((), (), ())[0]:
                pass

            # turtle and text shape
            self.assertEqual(document.DrawPage.getCount(), 2)
            textShape = document.DrawPage.getByIndex(1)
            # text in the text shape
            self.assertEqual(textShape.getString(), "x, x, x, x, x...")
            # check portion formatting
            c = textShape.createTextCursor()
            c.gotoStart(False)
            # check portion formatting
            c = textShape.createTextCursor()
            c.gotoStart(False)

            # strike out
            self.assertEqual(c.CharStrikeout, __Strikeout_SINGLE__) # strike out
            c.goRight(4, False)

            # subscript
            self.assertEqual(c.CharStrikeout, __Strikeout_NONE__) # no strike out
            self.assertEqual(c.CharEscapement, -14000) # magic number for default subscript, see DFLT_ESC_AUTO_SUB
            self.assertEqual(c.CharEscapementHeight, 58) # size in percent
            c.goRight(3, False)

            # superscript
            self.assertEqual(c.CharEscapement, 14000) # magic number for default superscript, see DFLT_ESC_AUTO_SUPER
            self.assertEqual(c.CharEscapementHeight, 58) # size in percent
            c.goRight(3, False)

            # font family
            self.assertEqual(c.CharEscapement, 0) # no superscript
            self.assertEqual(c.CharEscapementHeight, 100) # no superscript
            self.assertEqual(c.CharFontName, "Liberation Sans") # new font family
            c.goRight(3, False)

            # font size
            self.assertEqual(c.CharFontName, "Linux Biolinum G") # default font family
            self.assertEqual(c.CharHeight, 20) # new font size
            c.goRight(3, False)

            # default font size
            self.assertEqual(c.CharHeight, 12)

            #3 check colors

            document.Text.String = (
                "CLEARSCREEN " +
                "LABEL '<red>x</red>, <BLUE>x</BLUE>, " +  # check ignoring case
                    "<FONTCOLOR GREEN>x</FONTCOLOR>, " +   # check with command
                    "<FONTCOLOR 0x0000FF>x, " +            # check with hexa code
                    "<FILLCOLOR ORANGE>x</FILLCOLOR>, " +  # blue text with orange highlighting
                    "<FILLCOLOR 0xFF00FF>x</FILLCOLOR>" +  # blue text with purple highlighting
                    "...</FONTCOLOR>'" )

            self.logo("run")
            # wait for LibreLogo program termination
            while xIsAlive.invoke((), (), ())[0]:
                pass

            # turtle and text shape
            self.assertEqual(document.DrawPage.getCount(), 2)
            textShape = document.DrawPage.getByIndex(1)
            # text in the text shape
            self.assertEqual(textShape.getString(), "x, x, x, x, x, x...")
            # check portion formatting
            c = textShape.createTextCursor()
            c.gotoStart(False)
            # check portion formatting
            c = textShape.createTextCursor()
            c.gotoStart(False)

            self.assertEqual(c.CharColor, 0xFF0000) # red
            self.assertEqual(c.CharBackColor, -1) # transparent highlight
            c.goRight(4, False)

            self.assertEqual(c.CharColor, 0x0000FF) # blue
            self.assertEqual(c.CharBackColor, -1) # transparent highlight
            c.goRight(3, False)

            self.assertEqual(c.CharColor, 0x008000) # green
            self.assertEqual(c.CharBackColor, -1) # transparent highlight
            c.goRight(3, False)

            self.assertEqual(c.CharColor, 0x0000FF) # blue
            self.assertEqual(c.CharBackColor, -1) # transparent highlight
            c.goRight(3, False)

            self.assertEqual(c.CharColor, 0x0000FF) # blue
            self.assertEqual(c.CharBackColor, 0xFFA500) # orange highlight
            c.goRight(3, False)

            self.assertEqual(c.CharColor, 0x0000FF) # blue
            self.assertEqual(c.CharBackColor, 0xFF00FF) # purple highlight
            c.goRight(3, False)

            self.assertEqual(c.CharColor, 0x0000FF) # blue
            self.assertEqual(c.CharBackColor, -1) # transparent highlight

            #4 check font features

            document.Text.String = (
                "CLEARSCREEN FONTFAMILY 'Linux Biolinum G' " +
                "LABEL 'a <smcp>smcp <pnum>1<onum>1</pnum> 1</onum>1</smcp>...'" )

            self.logo("run")
            # wait for LibreLogo program termination
            while xIsAlive.invoke((), (), ())[0]:
                pass

            # turtle and text shape
            self.assertEqual(document.DrawPage.getCount(), 2)
            textShape = document.DrawPage.getByIndex(1)
            # text in the text shape
            self.assertEqual(textShape.getString(), "a smcp 11 11...")
            # check portion formatting
            c = textShape.createTextCursor()
            c.gotoStart(False)
            # check portion formatting
            c = textShape.createTextCursor()
            c.gotoStart(False)

            self.assertEqual(c.CharFontName, "Linux Biolinum G")
            c.goRight(3, False)
            self.assertEqual(c.CharFontName, "Linux Biolinum G:smcp")
            c.goRight(5, False)
            self.assertEqual(c.CharFontName, "Linux Biolinum G:smcp&pnum")
            c.goRight(1, False)
            self.assertEqual(c.CharFontName, "Linux Biolinum G:smcp&pnum&onum")
            c.goRight(2, False)
            self.assertEqual(c.CharFontName, "Linux Biolinum G:smcp&onum")
            c.goRight(1, False)
            self.assertEqual(c.CharFontName, "Linux Biolinum G:smcp")

# vim: set shiftwidth=4 softtabstop=4 expandtab: