From 4e1da3d42f3a9746664227ca38302ed4b8e6f201 Mon Sep 17 00:00:00 2001 From: Markus Mohrhard Date: Fri, 6 May 2016 00:42:16 +0200 Subject: [PATCH] uitest: add initial support sending special key events Currently supported are: CTRL ALT SHIFT Change-Id: Ibf90f5a8b74f33f1ca69c66d18eb86889c0c52ea --- vcl/source/uitest/uiobject.cxx | 92 ++++++++++++++++++++++++++++++---- 1 file changed, 82 insertions(+), 10 deletions(-) diff --git a/vcl/source/uitest/uiobject.cxx b/vcl/source/uitest/uiobject.cxx index 214f69105321..285241502ae9 100644 --- a/vcl/source/uitest/uiobject.cxx +++ b/vcl/source/uitest/uiobject.cxx @@ -19,6 +19,8 @@ #include #include +#include + #include #include @@ -62,7 +64,6 @@ void UIObject::dumpHierarchy() const { } - namespace { bool isDialogWindow(vcl::Window* pWindow) @@ -106,6 +107,64 @@ std::vector generate_key_events_from_text(const OUString& rStr) return aEvents; } +sal_uInt16 get_key(sal_Unicode cChar, bool& bShift) +{ + bShift = false; + if (cChar >= 'a' && cChar <= 'z') + return KEY_A + (cChar - 'a'); + else if (cChar >= 'A' && cChar <= 'Z') + { + bShift = true; + return KEY_A + (cChar - 'A'); + } + else if (cChar >= '0' && cChar <= '9') + return KEY_0 + (cChar - 'A'); + + return cChar; +} + +std::vector generate_key_events_from_keycode(const OUString& rStr) +{ + std::vector aEvents; + + // split string along '+' + // then translate to keycodes + bool bShift = false; + bool bMod1 = false; + bool bMod2 = false; + OUString aRemainingText; + + std::vector aTokens = comphelper::string::split(rStr, '+'); + for (auto itr = aTokens.begin(), itrEnd = aTokens.end(); itr != itrEnd; ++itr) + { + OUString aToken = itr->trim(); + if (aToken == "CTRL") + { + bMod1 = true; + } + else if (aToken == "SHIFT") + { + bShift = true; + } + else if (aToken == "ALT") + { + bMod2 = true; + } + else + aRemainingText = aToken; + } + + for (sal_Int32 i = 0; i < aRemainingText.getLength(); ++i) + { + bool bShiftThroughKey = false; + sal_uInt16 nKey = get_key(aRemainingText[i], bShiftThroughKey); + vcl::KeyCode aCode(nKey, bShift || bShiftThroughKey, bMod1, bMod2, false); + aEvents.push_back(KeyEvent(aRemainingText[i], aCode)); + } + + return aEvents; +} + OUString to_string(const Point& rPos) { OUStringBuffer aBuffer; @@ -187,19 +246,32 @@ void WindowUIObject::execute(const OUString& rAction, else if (rAction == "TYPE") { auto it = rParameters.find("TEXT"); - if (it == rParameters.end()) + if (it != rParameters.end()) + { + const OUString& rText = it->second; + auto aKeyEvents = generate_key_events_from_text(rText); + for (auto itr = aKeyEvents.begin(), itrEnd = aKeyEvents.end(); + itr != itrEnd; ++itr) + { + mxWindow->KeyInput(*itr); + } + } + else if (rParameters.find("KEYCODE") != rParameters.end()) + { + auto itr = rParameters.find("KEYCODE"); + const OUString rText = itr->second; + auto aKeyEvents = generate_key_events_from_keycode(rText); + for (auto itrKey = aKeyEvents.begin(), itrKeyEnd = aKeyEvents.end(); + itrKey != itrKeyEnd; ++itrKey) + { + mxWindow->KeyInput(*itrKey); + } + } + else { SAL_WARN("vcl.uitest", "missing parameter TEXT to action TYPE"); return; } - - const OUString& rText = it->second; - auto aKeyEvents = generate_key_events_from_text(rText); - for (auto itr = aKeyEvents.begin(), itrEnd = aKeyEvents.end(); - itr != itrEnd; ++itr) - { - mxWindow->KeyInput(*itr); - } } else {