Get the style color and number just once
[LibreOffice.git] / sw / qa / uitest / librelogo / run.py
blob9f670e3d60edc55bf0ab77879ee650a2bc290a9b
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 from uitest.framework import UITestCase
11 from uitest.uihelper.common import type_text
12 from com.sun.star.awt.FontSlant import ITALIC as __Slant_ITALIC__
13 from com.sun.star.awt.FontUnderline import NONE as __Underline_NONE__
14 from com.sun.star.awt.FontUnderline import SINGLE as __Underline_SINGLE__
15 from com.sun.star.awt.FontStrikeout import NONE as __Strikeout_NONE__
16 from com.sun.star.awt.FontStrikeout import SINGLE as __Strikeout_SINGLE__
18 class LibreLogoTest(UITestCase):
19 LIBRELOGO_PATH = "vnd.sun.star.script:LibreLogo|LibreLogo.py$%s?language=Python&location=share"
21 def createMasterScriptProviderFactory(self):
22 xServiceManager = self.xContext.ServiceManager
23 return xServiceManager.createInstanceWithContext(
24 "com.sun.star.script.provider.MasterScriptProviderFactory",
25 self.xContext)
27 def getScript(self, command):
28 xMasterScriptProviderFactory = self.createMasterScriptProviderFactory()
29 document = self.ui_test.get_component()
30 xScriptProvider = xMasterScriptProviderFactory.createScriptProvider(document)
31 xScript = xScriptProvider.getScript(self.LIBRELOGO_PATH %command)
32 self.assertIsNotNone(xScript, "xScript was not loaded")
33 return xScript
35 def logo(self, command):
36 self.xUITest.executeCommand(self.LIBRELOGO_PATH %command)
38 def test_librelogo(self):
39 with self.ui_test.create_doc_in_start_center("writer") as document:
40 xWriterDoc = self.xUITest.getTopFocusWindow()
41 xWriterEdit = xWriterDoc.getChild("writer_edit")
42 # to check the state of LibreLogo program execution
43 xIsAlive = self.getScript("__is_alive__")
45 # run a program with basic drawing commands FORWARD and RIGHT
46 # using their abbreviated names FD and RT
47 type_text(xWriterEdit, "fd 100 rt 45 fd 100")
48 self.logo("run")
49 # wait for LibreLogo program termination
50 while xIsAlive.invoke((), (), ())[0]:
51 pass
52 # check shape count for
53 # a) program running:
54 # - turtle shape: result of program start
55 # - line shape: result of turtle drawing
56 # b) continuous line drawing (the regression
57 # related to the fix of tdf#106792 resulted shorter line
58 # segments than the turtle path and non-continuous line
59 # drawing, ie. in this example, three line shapes
60 # instead of a single one. See its fix in
61 # commit 502e8785085f9e8b54ee383080442c2dcaf95b15)
62 self.assertEqual(len(document.DrawPage), 2)
64 # check formatting by "magic wand"
65 self.logo("__translate__")
66 # a) check expansion of abbreviated commands : fd -> FORWARD, rt -> RIGHT,
67 # b) check line breaking (fix for tdf#100941: new line instead of the text "\" and "n")
68 self.assertEqual(document.Text.String.replace('\r\n', '\n'), "\nFORWARD 100 RIGHT 45 FORWARD 100")
69 # c) check usage of real paragraphs instead of line break (tdf#120422)
70 # first paragraph is empty (for working page break)
71 self.assertEqual(document.Text.createEnumeration().nextElement().String, "")
73 # function definitions and calls can be in arbitrary order
74 document.Text.String = """
75 ; dragon curve
76 TO x n
77 IF n = 0 [ STOP ]
78 x n-1
79 RIGHT 90
80 y n-1 ; it worked only as "y(n-1)"
81 FORWARD 10
82 END
84 TO y n
85 IF n = 0 [ STOP ]
86 FORWARD 10
87 x n-1
88 LEFT 90
89 y n-1
90 END
92 PICTURE ; start new line draw
93 x 3 ; draw only a few levels
94 """
95 self.logo("run")
96 # wait for LibreLogo program termination
97 while xIsAlive.invoke((), (), ())[0]:
98 pass
99 # new shape + previous two ones = 3
100 # disable unreliable test. Depending on how busy the machine is, this may produce 3 or 4
101 # self.assertEqual(len(document.DrawPage), 3)
103 def check_label(self, hasCustomLock):
104 sLock = "CLEARSCREEN "
105 if hasCustomLock:
106 sLock = sLock + "SLEEP -1 "
107 with self.ui_test.create_doc_in_start_center("writer") as document:
108 xWriterDoc = self.xUITest.getTopFocusWindow()
109 xWriterEdit = xWriterDoc.getChild("writer_edit")
110 # to check the state of LibreLogo program execution
111 xIsAlive = self.getScript("__is_alive__")
113 #1 run a program with basic LABEL command
115 type_text(xWriterEdit, sLock + "LABEL 'Hello, World!'")
116 self.logo("run")
117 # wait for LibreLogo program termination
118 while xIsAlive.invoke((), (), ())[0]:
119 pass
121 # turtle and text shape
122 self.assertEqual(len(document.DrawPage), 2)
123 textShape = document.DrawPage[1]
124 # text in the text shape
125 self.assertEqual(textShape.getString(), "Hello, World!")
127 #2 check italic, bold, underline + red and blue formatting
129 document.Text.String = sLock + "LABEL '<i><red>Hello</red>, <bold><blue>W<u>orld</blue></bold>!</i></u>'"
130 self.logo("run")
131 # wait for LibreLogo program termination
132 while xIsAlive.invoke((), (), ())[0]:
133 pass
135 # turtle and text shape
136 self.assertEqual(len(document.DrawPage), 2)
137 textShape = document.DrawPage[1]
138 # text in the text shape
139 self.assertEqual(textShape.getString(), "Hello, World!")
140 # check portion formatting
141 c = textShape.createTextCursor()
142 c.gotoStart(False)
143 # before character "H"
144 self.assertEqual(c.CharPosture, __Slant_ITALIC__) # cursive
145 self.assertEqual(c.CharUnderline, __Underline_NONE__) # no underline
146 self.assertEqual(c.CharWeight, 100) # normal weight
147 self.assertEqual(c.CharColor, 0xFF0000) # red color
148 # after character " "
149 c.goRight(6, False)
150 self.assertEqual(c.CharPosture, __Slant_ITALIC__) # cursive
151 self.assertEqual(c.CharUnderline, __Underline_NONE__) # no underline
152 self.assertEqual(c.CharWeight, 100) # normal weight
153 self.assertEqual(c.CharColor, 0x000000) # black color
154 # after character "W"
155 c.goRight(2, False)
156 self.assertEqual(c.CharPosture, __Slant_ITALIC__) # cursive
157 self.assertEqual(c.CharUnderline, __Underline_NONE__) # no underline
158 self.assertEqual(c.CharWeight, 150) # bold
159 self.assertEqual(c.CharColor, 0x0000FF) # blue color
160 # 9th: after character "o"
161 c.goRight(1, False)
162 self.assertEqual(c.CharPosture, __Slant_ITALIC__) # cursive
163 self.assertEqual(c.CharUnderline, __Underline_SINGLE__) # underline
164 self.assertEqual(c.CharWeight, 150) # bold
165 self.assertEqual(c.CharColor, 0x0000FF) # blue color
166 # last: after character "!"
167 c.gotoEnd(False)
168 self.assertEqual(c.CharPosture, __Slant_ITALIC__) # cursive
169 self.assertEqual(c.CharUnderline, __Underline_SINGLE__) # underline
170 self.assertEqual(c.CharWeight, 100) # normal weight
171 self.assertEqual(c.CharColor, 0x000000) # black color
173 #2 check strike out, sub, sup, font name and font size formatting
175 document.Text.String = (
176 sLock + "FONTFAMILY 'Linux Biolinum G' FONTSIZE 12 " +
177 "LABEL '<s>x</s>, <sub>x</sub>, <sup>x</sup>, " +
178 "<FONTFAMILY Liberation Sans>x</FONTFAMILY>, " +
179 "<FONTHEIGHT 20>x</FONTHEIGHT>...'" )
181 self.logo("run")
182 # wait for LibreLogo program termination
183 while xIsAlive.invoke((), (), ())[0]:
184 pass
186 # turtle and text shape
187 self.assertEqual(len(document.DrawPage), 2)
188 textShape = document.DrawPage[1]
189 # text in the text shape
190 self.assertEqual(textShape.getString(), "x, x, x, x, x...")
191 # check portion formatting
192 c = textShape.createTextCursor()
193 c.gotoStart(False)
194 # check portion formatting
195 c = textShape.createTextCursor()
196 c.gotoStart(False)
198 # strike out
199 self.assertEqual(c.CharStrikeout, __Strikeout_SINGLE__) # strike out
200 c.goRight(4, False)
202 # subscript
203 self.assertEqual(c.CharStrikeout, __Strikeout_NONE__) # no strike out
204 self.assertEqual(c.CharEscapement, -14000) # magic number for default subscript, see DFLT_ESC_AUTO_SUB
205 self.assertEqual(c.CharEscapementHeight, 58) # size in percent
206 c.goRight(3, False)
208 # superscript
209 self.assertEqual(c.CharEscapement, 14000) # magic number for default superscript, see DFLT_ESC_AUTO_SUPER
210 self.assertEqual(c.CharEscapementHeight, 58) # size in percent
211 c.goRight(3, False)
213 # font family
214 self.assertEqual(c.CharEscapement, 0) # no superscript
215 self.assertEqual(c.CharEscapementHeight, 100) # no superscript
216 self.assertEqual(c.CharFontName, "Liberation Sans") # new font family
217 c.goRight(3, False)
219 # font size
220 self.assertEqual(c.CharFontName, "Linux Biolinum G") # default font family
221 self.assertEqual(c.CharHeight, 20) # new font size
222 c.goRight(3, False)
224 # default font size
225 self.assertEqual(c.CharHeight, 12)
227 #3 check colors
229 document.Text.String = ( sLock +
230 "LABEL '<red>x</red>, <BLUE>x</BLUE>, " + # check ignoring case
231 "<FONTCOLOR GREEN>x</FONTCOLOR>, " + # check with command
232 "<FONTCOLOR 0x0000FF>x, " + # check with hexa code
233 "<FILLCOLOR ORANGE>x</FILLCOLOR>, " + # blue text with orange highlighting
234 "<FILLCOLOR 0xFF00FF>x</FILLCOLOR>" + # blue text with purple highlighting
235 "...</FONTCOLOR>'" )
237 self.logo("run")
238 # wait for LibreLogo program termination
239 while xIsAlive.invoke((), (), ())[0]:
240 pass
242 # turtle and text shape
243 self.assertEqual(len(document.DrawPage), 2)
244 textShape = document.DrawPage[1]
245 # text in the text shape
246 self.assertEqual(textShape.getString(), "x, x, x, x, x, x...")
247 # check portion formatting
248 c = textShape.createTextCursor()
249 c.gotoStart(False)
250 # check portion formatting
251 c = textShape.createTextCursor()
252 c.gotoStart(False)
254 self.assertEqual(c.CharColor, 0xFF0000) # red
255 self.assertEqual(c.CharBackColor, -1) # transparent highlight
256 c.goRight(4, False)
258 self.assertEqual(c.CharColor, 0x0000FF) # blue
259 self.assertEqual(c.CharBackColor, -1) # transparent highlight
260 c.goRight(3, False)
262 self.assertEqual(c.CharColor, 0x008000) # green
263 self.assertEqual(c.CharBackColor, -1) # transparent highlight
264 c.goRight(3, False)
266 self.assertEqual(c.CharColor, 0x0000FF) # blue
267 self.assertEqual(c.CharBackColor, -1) # transparent highlight
268 c.goRight(3, False)
270 self.assertEqual(c.CharColor, 0x0000FF) # blue
271 self.assertEqual(c.CharBackColor, 0xFFA500) # orange highlight
272 c.goRight(3, False)
274 self.assertEqual(c.CharColor, 0x0000FF) # blue
275 self.assertEqual(c.CharBackColor, 0xFF00FF) # purple highlight
276 c.goRight(3, False)
278 self.assertEqual(c.CharColor, 0x0000FF) # blue
279 self.assertEqual(c.CharBackColor, -1) # transparent highlight
281 #4 check font features
283 document.Text.String = (
284 sLock + "FONTFAMILY 'Linux Biolinum G' " +
285 "LABEL 'a <smcp>smcp <pnum>1<onum>1</pnum> 1</onum>1</smcp>...'" )
287 self.logo("run")
288 # wait for LibreLogo program termination
289 while xIsAlive.invoke((), (), ())[0]:
290 pass
292 # turtle and text shape
293 self.assertEqual(len(document.DrawPage), 2)
294 textShape = document.DrawPage[1]
295 # text in the text shape
296 self.assertEqual(textShape.getString(), "a smcp 11 11...")
297 # check portion formatting
298 c = textShape.createTextCursor()
299 c.gotoStart(False)
300 # check portion formatting
301 c = textShape.createTextCursor()
302 c.gotoStart(False)
304 self.assertEqual(c.CharFontName, "Linux Biolinum G")
305 c.goRight(3, False)
306 self.assertEqual(c.CharFontName, "Linux Biolinum G:smcp")
307 c.goRight(5, False)
308 self.assertEqual(c.CharFontName, "Linux Biolinum G:smcp&pnum")
309 c.goRight(1, False)
310 self.assertEqual(c.CharFontName, "Linux Biolinum G:smcp&pnum&onum")
311 c.goRight(2, False)
312 self.assertEqual(c.CharFontName, "Linux Biolinum G:smcp&onum")
313 c.goRight(1, False)
314 self.assertEqual(c.CharFontName, "Linux Biolinum G:smcp")
316 def test_LABEL(self):
317 self.check_label(False)
319 def test_custom_lock(self):
320 self.check_label(True)
322 # vim: set shiftwidth=4 softtabstop=4 expandtab: