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