LibreOffice Module vcl (master)  1
logger.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  */
9 
10 #include <config_folders.h>
11 
12 #include <vcl/uitest/logger.hxx>
13 
14 #include <rtl/bootstrap.hxx>
15 #include <rtl/ustrbuf.hxx>
16 #include <osl/file.hxx>
17 #include <vcl/ctrl.hxx>
18 #include <vcl/event.hxx>
19 #include <vcl/uitest/uiobject.hxx>
21 #include <svdata.hxx>
22 #include <com/sun/star/beans/PropertyValue.hpp>
23 #include <memory>
24 
25 namespace
26 {
27 bool isDialogWindow(vcl::Window const* pWindow)
28 {
29  WindowType nType = pWindow->GetType();
30  if (nType == WindowType::DIALOG || nType == WindowType::MODELESSDIALOG)
31  return true;
32 
33  // MESSBOX, INFOBOX, WARNINGBOX, ERRORBOX, QUERYBOX
34  if (nType >= WindowType::MESSBOX && nType <= WindowType::QUERYBOX)
35  return true;
36 
37  if (nType == WindowType::TABDIALOG)
38  return true;
39 
40  return false;
41 }
42 
43 bool isTopWindow(vcl::Window const* pWindow)
44 {
45  WindowType eType = pWindow->GetType();
46  if (eType == WindowType::FLOATINGWINDOW)
47  {
48  return pWindow->GetStyle() & WB_SYSTEMFLOATWIN;
49  }
50  return false;
51 }
52 
53 vcl::Window* get_top_parent(vcl::Window* pWindow)
54 {
55  if (isDialogWindow(pWindow) || isTopWindow(pWindow))
56  return pWindow;
57 
58  vcl::Window* pParent = pWindow->GetParent();
59  if (!pParent)
60  return pWindow;
61 
62  return get_top_parent(pParent);
63 }
64 }
66  : maStream()
67  , mbValid(false)
68 {
69  static const char* pFile = std::getenv("LO_COLLECT_UIINFO");
70  if (pFile)
71  {
72  OUString aDirPath("${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER
73  "/" SAL_CONFIGFILE("bootstrap") ":UserInstallation}/uitest/");
74  rtl::Bootstrap::expandMacros(aDirPath);
75  osl::Directory::createPath(aDirPath);
76  OUString aFilePath = aDirPath + OUString::fromUtf8(pFile);
77 
78  maStream.Open(aFilePath, StreamMode::READWRITE | StreamMode::TRUNC);
79  mbValid = true;
80  }
81 }
82 
83 void UITestLogger::logCommand(const OUString& rAction,
84  const css::uno::Sequence<css::beans::PropertyValue>& rArgs)
85 {
86  if (!mbValid)
87  return;
88 
89  OUStringBuffer aBuffer(rAction);
90 
91  if (rArgs.hasElements())
92  {
93  aBuffer.append(" {");
94  for (const css::beans::PropertyValue& rProp : rArgs)
95  {
96  OUString aTypeName = rProp.Value.getValueTypeName();
97 
98  if (aTypeName == "long" || aTypeName == "short")
99  {
100  sal_Int32 nValue = 0;
101  rProp.Value >>= nValue;
102  aBuffer.append("\"").append(rProp.Name).append("\": ");
103  aBuffer.append(OUString::number(nValue)).append(", ");
104  }
105  else if (aTypeName == "unsigned long")
106  {
107  sal_uInt32 nValue = 0;
108  rProp.Value >>= nValue;
109  aBuffer.append("\"").append(rProp.Name).append("\": ");
110  aBuffer.append(OUString::number(nValue)).append(", ");
111  }
112  else if (aTypeName == "boolean")
113  {
114  bool bValue = false;
115  rProp.Value >>= bValue;
116  aBuffer.append("\"").append(rProp.Name).append("\": ");
117  if (bValue)
118  aBuffer.append("True, ");
119  else
120  aBuffer.append("False, ");
121  }
122  }
123  aBuffer.append("}");
124  }
125 
126  OUString aCommand(aBuffer.makeStringAndClear());
127  maStream.WriteLine(OUStringToOString(aCommand, RTL_TEXTENCODING_UTF8));
128 }
129 
130 namespace
131 {
132 // most likely this should be recursive
133 bool child_windows_have_focus(VclPtr<vcl::Window> const& xUIElement)
134 {
135  sal_Int32 nCount = xUIElement->GetChildCount();
136  for (sal_Int32 i = 0; i < nCount; ++i)
137  {
138  vcl::Window* pChild = xUIElement->GetChild(i);
139  if (pChild->HasFocus())
140  {
141  return true;
142  }
143  if (child_windows_have_focus(VclPtr<vcl::Window>(pChild)))
144  return true;
145  }
146  return false;
147 }
148 }
149 
150 void UITestLogger::logAction(VclPtr<Control> const& xUIElement, VclEventId nEvent)
151 {
152  if (!mbValid)
153  return;
154 
155  if (xUIElement->get_id().isEmpty())
156  return;
157 
158  std::unique_ptr<UIObject> pUIObject = xUIElement->GetUITestFactory()(xUIElement.get());
159  OUString aAction = pUIObject->get_action(nEvent);
160  if (!xUIElement->HasFocus() && !child_windows_have_focus(xUIElement))
161  {
162  return;
163  }
164 
165  if (!aAction.isEmpty())
166  maStream.WriteLine(OUStringToOString(aAction, RTL_TEXTENCODING_UTF8));
167 }
168 
169 void UITestLogger::log(const OUString& rString)
170 {
171  if (!mbValid)
172  return;
173 
174  if (rString.isEmpty())
175  return;
176 
177  maStream.WriteLine(OUStringToOString(rString, RTL_TEXTENCODING_UTF8));
178 }
179 
180 void UITestLogger::logKeyInput(VclPtr<vcl::Window> const& xUIElement, const KeyEvent& rEvent)
181 {
182  if (!mbValid)
183  return;
184 
185  //We need to check for Parent's ID in case the UI Element is SubEdit of Combobox/SpinField
186  const OUString& rID
187  = xUIElement->get_id().isEmpty() ? xUIElement->GetParent()->get_id() : xUIElement->get_id();
188  if (rID.isEmpty())
189  return;
190 
191  sal_Unicode nChar = rEvent.GetCharCode();
192  sal_uInt16 nKeyCode = rEvent.GetKeyCode().GetCode();
193  bool bShift = rEvent.GetKeyCode().IsShift();
194  bool bMod1 = rEvent.GetKeyCode().IsMod1();
195  bool bMod2 = rEvent.GetKeyCode().IsMod2();
196  bool bMod3 = rEvent.GetKeyCode().IsMod3();
197 
198  std::map<OUString, sal_uInt16> aKeyMap
199  = { { "ESC", KEY_ESCAPE }, { "TAB", KEY_TAB }, { "DOWN", KEY_DOWN },
200  { "UP", KEY_UP }, { "LEFT", KEY_LEFT }, { "RIGHT", KEY_RIGHT },
201  { "DELETE", KEY_DELETE }, { "INSERT", KEY_INSERT }, { "BACKSPACE", KEY_BACKSPACE },
202  { "RETURN", KEY_RETURN }, { "HOME", KEY_HOME }, { "END", KEY_END },
203  { "PAGEUP", KEY_PAGEUP }, { "PAGEDOWN", KEY_PAGEDOWN } };
204 
205  OUString aFound;
206  for (const auto& itr : aKeyMap)
207  {
208  if (itr.second == nKeyCode)
209  {
210  aFound = itr.first;
211  break;
212  }
213  }
214 
215  OUString aKeyCode;
216  if (!aFound.isEmpty() || bShift || bMod1 || bMod2 || bMod3)
217  {
218  aKeyCode = "{\"KEYCODE\": \"";
219  if (bShift)
220  aKeyCode += "SHIFT+";
221 
222  if (bMod1)
223  aKeyCode += "CTRL+";
224 
225  if (bMod2)
226  aKeyCode += "ALT+";
227 
228  if (aFound.isEmpty())
229  aKeyCode += OUStringChar(nChar) + "\"}";
230  else
231  aKeyCode += aFound + "\"}";
232  }
233  else
234  {
235  aKeyCode = "{\"TEXT\": \"" + OUStringChar(nChar) + "\"}";
236  }
237 
238  std::unique_ptr<UIObject> pUIObject = xUIElement->GetUITestFactory()(xUIElement.get());
239 
240  VclPtr<vcl::Window> pParent = xUIElement->GetParent();
241 
242  while (!pParent->IsTopWindow())
243  {
244  pParent = pParent->GetParent();
245  }
246 
247  OUString aParentID = pParent->get_id();
248 
249  OUString aContent;
250 
251  if (pUIObject->get_type() == "EditUIObject")
252  {
253  if (aParentID.isEmpty())
254  {
255  VclPtr<vcl::Window> pParent_top = get_top_parent(xUIElement);
256  aParentID = pParent_top->get_id();
257  }
258  if (aParentID.isEmpty())
259  {
260  aContent += "Type on '" + rID + "' " + aKeyCode;
261  }
262  else
263  {
264  aContent += "Type on '" + rID + "' " + aKeyCode + " from " + aParentID;
265  }
266  }
267  else if (pUIObject->get_type() == "SwEditWinUIObject" && rID == "writer_edit")
268  {
269  aContent = "Type on writer " + aKeyCode;
270  }
271  else if (pUIObject->get_type() == "ScGridWinUIObject" && rID == "grid_window")
272  {
273  aContent = "Type on current cell " + aKeyCode;
274  }
275  else if (pUIObject->get_type() == "ImpressWindowUIObject" && rID == "impress_win")
276  {
277  aContent = "Type on impress " + aKeyCode;
278  }
279  else if (pUIObject->get_type() == "WindowUIObject" && rID == "math_edit")
280  {
281  aContent = "Type on math " + aKeyCode;
282  }
283  else if (rID == "draw_win")
284  {
285  aContent = "Type on draw " + aKeyCode;
286  }
287  else
288  {
289  if (aParentID.isEmpty())
290  {
291  VclPtr<vcl::Window> pParent_top = get_top_parent(xUIElement);
292  aParentID = pParent_top->get_id();
293  }
294  if (aParentID.isEmpty())
295  {
296  aContent = "Type on '" + rID + "' " + aKeyCode;
297  }
298  else
299  {
300  aContent = "Type on '" + rID + "' " + aKeyCode + " from " + aParentID;
301  }
302  }
303  maStream.WriteLine(OUStringToOString(aContent, RTL_TEXTENCODING_UTF8));
304 }
305 
306 namespace
307 {
308 OUString StringMapToOUString(const std::map<OUString, OUString>& rParameters)
309 {
310  if (rParameters.empty())
311  return "";
312 
313  OUStringBuffer aParameterString(static_cast<int>(rParameters.size()*32));
314  aParameterString.append(" {");
315 
316  for (std::map<OUString, OUString>::const_iterator itr = rParameters.begin();
317  itr != rParameters.end(); ++itr)
318  {
319  if (itr != rParameters.begin())
320  aParameterString.append(", ");
321  aParameterString.append("\"")
322  .append(itr->first)
323  .append("\": \"")
324  .append(itr->second)
325  .append("\"");
326  }
327 
328  aParameterString.append("}");
329 
330  return aParameterString.makeStringAndClear();
331 }
332 
333 OUString GetValueInMapWithIndex(const std::map<OUString, OUString>& rParameters, sal_Int32 index)
334 {
335  sal_Int32 j = 0;
336 
337  std::map<OUString, OUString>::const_iterator itr = rParameters.begin();
338 
339  for (; itr != rParameters.end() && j < index; ++itr, ++j)
340  ;
341 
342  assert(itr != rParameters.end());
343 
344  return itr->second;
345 }
346 
347 OUString GetKeyInMapWithIndex(const std::map<OUString, OUString>& rParameters, sal_Int32 index)
348 {
349  sal_Int32 j = 0;
350 
351  std::map<OUString, OUString>::const_iterator itr = rParameters.begin();
352 
353  for (; itr != rParameters.end() && j < index; ++itr, ++j)
354  ;
355 
356  assert(itr != rParameters.end());
357 
358  return itr->first;
359 }
360 }
361 
362 void UITestLogger::logEvent(const EventDescription& rDescription)
363 {
364  OUString aParameterString = StringMapToOUString(rDescription.aParameters);
365 
366  //here we will customize our statements depending on the caller of this function
367  OUString aLogLine;
368  //first check on general commands
369  if (rDescription.aAction == "SET")
370  {
371  aLogLine = "Set Zoom to " + GetValueInMapWithIndex(rDescription.aParameters, 0);
372  }
373  else if (rDescription.aAction == "SIDEBAR")
374  {
375  aLogLine = "From SIDEBAR Choose " + aParameterString;
376  }
377  else if (rDescription.aAction == "SELECT" && rDescription.aID.isEmpty())
378  {
379  aLogLine = "Select " + aParameterString;
380  }
381  else if (rDescription.aID == "writer_edit")
382  {
383  if (rDescription.aAction == "GOTO")
384  {
385  aLogLine = "GOTO page number " + GetValueInMapWithIndex(rDescription.aParameters, 0);
386  }
387  else if (rDescription.aAction == "SELECT")
388  {
389  OUString to = GetValueInMapWithIndex(rDescription.aParameters, 0);
390  OUString from = GetValueInMapWithIndex(rDescription.aParameters, 1);
391  aLogLine = "Select from Pos " + from + " to Pos " + to;
392  }
393  else if (rDescription.aAction == "CREATE_TABLE")
394  {
395  OUString size = GetValueInMapWithIndex(rDescription.aParameters, 0);
396  aLogLine = "Create Table with " + size;
397  ;
398  }
399  else if (rDescription.aAction == "COPY")
400  {
401  aLogLine = "Copy the Selected Text";
402  }
403  else if (rDescription.aAction == "CUT")
404  {
405  aLogLine = "Cut the Selected Text";
406  }
407  else if (rDescription.aAction == "PASTE")
408  {
409  aLogLine = "Paste in the Current Cursor Location";
410  }
411  else if (rDescription.aAction == "BREAK_PAGE")
412  {
413  aLogLine = "Insert Break Page";
414  }
415  }
416  else if (rDescription.aID == "grid_window")
417  {
418  if (rDescription.aAction == "SELECT")
419  {
420  OUString type = GetKeyInMapWithIndex(rDescription.aParameters, 0);
421  if (type == "CELL" || type == "RANGE")
422  {
423  aLogLine = "Select from calc" + aParameterString;
424  }
425  else if (type == "TABLE")
426  {
427  aLogLine = "Switch to sheet number "
428  + GetValueInMapWithIndex(rDescription.aParameters, 0);
429  }
430  }
431  else if (rDescription.aAction == "LAUNCH")
432  {
433  aLogLine = "Launch AutoFilter from Col "
434  + GetValueInMapWithIndex(rDescription.aParameters, 2) + " and Row "
435  + GetValueInMapWithIndex(rDescription.aParameters, 1);
436  }
437  else if (rDescription.aAction == "DELETE_CONTENT")
438  {
439  aLogLine = "Remove Content from This " + aParameterString;
440  }
441  else if (rDescription.aAction == "DELETE_CELLS")
442  {
443  aLogLine = "Delete The Cells in" + aParameterString;
444  }
445  else if (rDescription.aAction == "INSERT_CELLS")
446  {
447  aLogLine = "Insert Cell around the " + aParameterString;
448  }
449  else if (rDescription.aAction == "CUT")
450  {
451  aLogLine = "CUT the selected " + aParameterString;
452  }
453  else if (rDescription.aAction == "COPY")
454  {
455  aLogLine = "COPY the selected " + aParameterString;
456  }
457  else if (rDescription.aAction == "PASTE")
458  {
459  aLogLine = "Paste in the " + aParameterString;
460  }
461  else if (rDescription.aAction == "MERGE_CELLS")
462  {
463  aLogLine = "Merge " + aParameterString;
464  }
465  else if (rDescription.aAction == "UNMERGE_CELL")
466  {
467  aLogLine = "Delete the merged " + aParameterString;
468  }
469  else if (rDescription.aAction == "Rename_Sheet")
470  {
471  aLogLine = "Rename The Selected Tab to \""
472  + GetValueInMapWithIndex(rDescription.aParameters, 0) + "\"";
473  }
474  else if (rDescription.aAction == "InsertTab")
475  {
476  aLogLine = "Insert New Tab ";
477  }
478  }
479  else if (rDescription.aID == "impress_win_or_draw_win")
480  {
481  if (rDescription.aAction == "Insert_New_Page_or_Slide")
482  {
483  if (UITestLogger::getInstance().getAppName() == "impress")
484  {
485  aLogLine = "Insert New Slide at Position "
486  + GetValueInMapWithIndex(rDescription.aParameters, 0);
487  }
488  else if (UITestLogger::getInstance().getAppName() == "draw")
489  {
490  aLogLine = "Insert New Page at Position "
491  + GetValueInMapWithIndex(rDescription.aParameters, 0);
492  }
493  }
494  else if (rDescription.aAction == "Delete_Slide_or_Page")
495  {
496  if (UITestLogger::getInstance().getAppName() == "impress")
497  {
498  aLogLine
499  = "Delete Slide number " + GetValueInMapWithIndex(rDescription.aParameters, 0);
500  }
501  else if (UITestLogger::getInstance().getAppName() == "draw")
502  {
503  aLogLine
504  = "Delete Page number " + GetValueInMapWithIndex(rDescription.aParameters, 0);
505  }
506  }
507  else if (rDescription.aAction == "Duplicate")
508  {
509  aLogLine = "Duplicate The Selected Slide ";
510  }
511  else if (rDescription.aAction == "RENAME")
512  {
513  if (UITestLogger::getInstance().getAppName() == "impress")
514  {
515  aLogLine = "Rename The Selected Slide from \""
516  + GetValueInMapWithIndex(rDescription.aParameters, 1) + "\" to \""
517  + GetValueInMapWithIndex(rDescription.aParameters, 0) + "\"";
518  }
519  else if (UITestLogger::getInstance().getAppName() == "draw")
520  {
521  aLogLine = "Rename The Selected Page from \""
522  + GetValueInMapWithIndex(rDescription.aParameters, 1) + "\" to \""
523  + GetValueInMapWithIndex(rDescription.aParameters, 0) + "\"";
524  }
525  }
526  }
527  else if (rDescription.aParent == "element_selector")
528  {
529  aLogLine = "Select element no " + rDescription.aID + " From " + rDescription.aParent;
530  }
531  else
532  {
533  aLogLine = rDescription.aKeyWord + " Action:" + rDescription.aAction + " Id:"
534  + rDescription.aID + " Parent:" + rDescription.aParent + aParameterString;
535  }
536  log(aLogLine);
537 }
538 
540 {
541  ImplSVData* const pSVData = ImplGetSVData();
542  assert(pSVData);
543 
544  if (!pSVData->maFrameData.m_pUITestLogger)
545  {
546  pSVData->maFrameData.m_pUITestLogger.reset(new UITestLogger);
547  }
548 
549  return *pSVData->maFrameData.m_pUITestLogger;
550 }
551 
552 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual FactoryFunction GetUITestFactory() const
Definition: window.cxx:3910
sal_uInt16 GetCode() const
Definition: keycod.hxx:53
void log(const OUString &rString)
Definition: logger.cxx:169
constexpr sal_uInt16 KEY_UP
Definition: keycodes.hxx:111
void logAction(VclPtr< Control > const &xUIElement, VclEventId nEvent)
Definition: logger.cxx:150
constexpr sal_uInt16 KEY_END
Definition: keycodes.hxx:115
static UITestLogger & getInstance()
Definition: logger.cxx:539
void logEvent(const EventDescription &rDescription)
Definition: logger.cxx:362
sal_uInt16 sal_Unicode
const OUString & get_id() const
Get the ID of the window.
Definition: window.cxx:3905
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
int nCount
constexpr sal_uInt16 KEY_PAGEUP
Definition: keycodes.hxx:116
UBlockCode from
VclEventId
Definition: vclevent.hxx:37
UBlockCode to
bool IsTopWindow() const
Definition: stacking.cxx:612
WinBits const WB_SYSTEMFLOATWIN
void Open(const OUString &rFileName, StreamMode eOpenMode)
UITestLogger()
Definition: logger.cxx:65
DocumentType eType
constexpr sal_uInt16 KEY_DOWN
Definition: keycodes.hxx:110
ImplSVData * ImplGetSVData()
Definition: svdata.cxx:76
#define SAL_CONFIGFILE(name)
void logCommand(const OUString &rAction, const css::uno::Sequence< css::beans::PropertyValue > &rArgs)
Definition: logger.cxx:83
int i
bool WriteLine(const OString &rStr)
SvFileStream maStream
Definition: logger.hxx:33
constexpr sal_uInt16 KEY_HOME
Definition: keycodes.hxx:114
void logKeyInput(VclPtr< vcl::Window > const &xUIElement, const KeyEvent &rEvent)
Definition: logger.cxx:180
constexpr sal_uInt16 KEY_PAGEDOWN
Definition: keycodes.hxx:117
size
vcl::Window * GetParent() const
Definition: window2.cxx:1097
constexpr sal_uInt16 KEY_RETURN
Definition: keycodes.hxx:119
constexpr sal_uInt16 KEY_RIGHT
Definition: keycodes.hxx:113
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
sal_uInt16 GetChildCount() const
Definition: stacking.cxx:1005
const vcl::KeyCode & GetKeyCode() const
Definition: event.hxx:54
std::unique_ptr< char[]> aBuffer
bool IsShift() const
Definition: keycod.hxx:58
WindowType
constexpr sal_uInt16 KEY_ESCAPE
Definition: keycodes.hxx:120
ImplSVFrameData maFrameData
Definition: svdata.hxx:394
std::unique_ptr< UITestLogger > m_pUITestLogger
Definition: svdata.hxx:241
sal_Unicode GetCharCode() const
Definition: event.hxx:53
bool IsMod1() const
Definition: keycod.hxx:60
OUString getAppName() const
Definition: logger.hxx:57
QPRO_FUNC_TYPE nType
bool mbValid
Definition: logger.hxx:35
constexpr sal_uInt16 KEY_BACKSPACE
Definition: keycodes.hxx:122
WindowType GetType() const
Definition: window2.cxx:974
reference_type * get() const
Get the body.
Definition: vclptr.hxx:143
ResultType type
OUString aCommand
WinBits GetStyle() const
Definition: window2.cxx:953
constexpr sal_uInt16 KEY_DELETE
Definition: keycodes.hxx:125
constexpr sal_uInt16 KEY_LEFT
Definition: keycodes.hxx:112
std::map< OUString, OUString > aParameters
constexpr sal_uInt16 KEY_INSERT
Definition: keycodes.hxx:124
bool HasFocus() const
Definition: window.cxx:3011
bool IsMod3() const
Definition: keycod.hxx:64
bool mbValid
constexpr sal_uInt16 KEY_TAB
Definition: keycodes.hxx:121
sal_Int16 nValue
vcl::Window * GetChild(sal_uInt16 nChild) const
Definition: stacking.cxx:1021
bool IsMod2() const
Definition: keycod.hxx:62