From 93e4a37cdf6cdc89ed8b3b7082e7350585aa709d Mon Sep 17 00:00:00 2001 From: Astariul Date: Thu, 9 May 2024 17:11:00 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20support=20for=20KeyboardKit?= =?UTF-8?q?=20open-source=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kebbie/cmd.py | 2 +- kebbie/emulator.py | 57 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/kebbie/cmd.py b/kebbie/cmd.py index 17fef7b..111c415 100644 --- a/kebbie/cmd.py +++ b/kebbie/cmd.py @@ -68,7 +68,7 @@ def common_args(parser: argparse.ArgumentParser): dest="keyboard", type=str, required=True, - choices=["gboard", "ios", "kbkit", "tappa", "fleksy"], + choices=["gboard", "ios", "kbkitpro", "kbkitoss", "tappa", "fleksy"], help="Which keyboard, to be tested, is currently installed on the emulator.", ) diff --git a/kebbie/emulator.py b/kebbie/emulator.py index 43293cd..730c2d3 100644 --- a/kebbie/emulator.py +++ b/kebbie/emulator.py @@ -30,7 +30,8 @@ GBOARD = "gboard" TAPPA = "tappa" FLEKSY = "fleksy" -KBKIT = "kbkit" +KBKITPRO = "kbkitpro" +KBKITOSS = "kbkitoss" ANDROID_CAPABILITIES = { "platformName": "android", "automationName": "UiAutomator2", @@ -84,6 +85,7 @@ "And", "Are", "“A”", + "🚀", ] CONTENT_TO_RENAME = { "Shift": "shift", @@ -304,13 +306,16 @@ def __init__( elif self.keyboard == IOS: self.detected = IosLayoutDetector(self.driver, self._tap) self.layout = self.detected.layout - elif self.keyboard == KBKIT: - self.detected = KbkitLayoutDetector(self.driver, self._tap) + elif self.keyboard == KBKITPRO: + self.detected = KbkitproLayoutDetector(self.driver, self._tap) + self.layout = self.detected.layout + elif self.keyboard == KBKITOSS: + self.detected = KbkitossLayoutDetector(self.driver, self._tap) self.layout = self.detected.layout else: raise ValueError( - f"Unknown keyboard : {self.keyboard}. Please specify `{GBOARD}`, `{TAPPA}`, `{FLEKSY}`, `{KBKIT}` " - f" or `{IOS}`." + f"Unknown keyboard : {self.keyboard}. Please specify `{GBOARD}`, `{TAPPA}`, `{FLEKSY}`, `{KBKITPRO}`, " + f"`{KBKITOSS}` or `{IOS}`." ) self.typing_field.clear() @@ -393,7 +398,7 @@ def _paste(self, text: str): # (which is what we want). On iOS it will not, we need to do it "manually" if self.platform == IOS: self.typing_field.clear() - if self.keyboard == KBKIT: + if self.keyboard == KBKITPRO or self.keyboard == KBKITOSS: # In the case of KeyboardKit, after pasting the content, typing a space # trigger a punctuation (because previous context may end with a space) # To avoid this behavior, break the cycle by typing a backspace @@ -933,9 +938,9 @@ def get_suggestions(self) -> List[str]: return suggestions -class KbkitLayoutDetector(LayoutDetector): - """Layout detector for the KeyboardKit demo keyboard. See `LayoutDetector` - for more information. +class KbkitproLayoutDetector(LayoutDetector): + """Layout detector for the KeyboardKit Pro demo keyboard. See + `LayoutDetector` for more information. """ def __init__(self, *args, **kwargs): @@ -970,6 +975,40 @@ def get_suggestions(self) -> List[str]: return suggestions +class KbkitossLayoutDetector(LayoutDetector): + """Layout detector for the KeyboardKit OSS demo keyboard. See + `LayoutDetector` for more information. + """ + + def __init__(self, *args, **kwargs): + super().__init__( + *args, + xpath_root=".//XCUIElementTypeOther[XCUIElementTypeButton and XCUIElementTypeStaticText]", + xpath_keys=".//XCUIElementTypeButton", + android=False, + **kwargs, + ) + + def get_suggestions(self) -> List[str]: + """Method to retrieve the keyboard suggestions from the XML tree. + + Returns: + List of suggestions from the keyboard. + """ + suggestions = [] + + for data in self.driver.page_source.split("")[1:]: + m = re.search(r"name=\"([^\"]*)\"?", elem) + if m: + name = m.group(1) + suggestions.append(name.replace("“", "").replace("”", "")) + + return suggestions + + class TappaLayoutDetector(LayoutDetector): """Layout detector for the Tappa keyboard. See `LayoutDetector` for more information.