Skip to content

Commit

Permalink
Refactor shortcut key configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
yorkxin committed Aug 29, 2024
1 parent 684353c commit 303be93
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected static void runShortcutKeys(int[] modifiers, int key) throws AWTExcept
robot.delay(500);
}

protected void setShortcutKeyInChrome(String commandName, CharSequence[] modifiers, CharSequence key) throws AWTException, InterruptedException {
protected void setShortcutKeyInChrome(CommandDescriptor cmd) throws AWTException, InterruptedException {
// Max 4 shortcut keys can be specified in the manifest.json file,
// so we have to navigate to chrome://extension/shortcuts and configure them in runtime.

Expand All @@ -66,14 +66,14 @@ protected void setShortcutKeyInChrome(String commandName, CharSequence[] modifie
WebElement cmdEntry = null;
for (WebElement entry : commandEntries) {
String name = entry.findElement(By.className("command-name")).getText() ;
if (Objects.equals(name, commandName)){
if (Objects.equals(name, cmd.name)){
cmdEntry = entry;
break;
}
}

if (cmdEntry == null) {
throw new IllegalArgumentException("no such command: "+commandName);
throw new IllegalArgumentException("no such command: "+cmd.name);
}

WebElement editButton = shadow.findElement(cmdEntry, "#edit");
Expand All @@ -86,32 +86,38 @@ protected void setShortcutKeyInChrome(String commandName, CharSequence[] modifie
.click(editButton)
.click(inputBox);

for (CharSequence modifier : modifiers) {
for (CharSequence modifier : cmd.modifiers) {
actions = actions.keyDown(modifier);
}
actions.keyDown(key);
for (CharSequence modifier : modifiers) {
actions.keyDown(cmd.key);
for (CharSequence modifier : cmd.modifiers) {
actions = actions.keyUp(modifier);
}
actions.perform();
}

protected void setShortcutKeyInFirefox(String commandId, CharSequence[] modifiers, CharSequence key) {
WebElement cmdEntry = driver.findElement(By.xpath("//input[@name=\""+commandId+"\"]"));
protected void setShortcutKeyInFirefox(CommandDescriptor cmd) throws AWTException {
WebElement cmdEntry = driver.findElement(By.xpath("//input[@name=\""+cmd.command+"\"]"));

if (cmdEntry == null) {
throw new IllegalArgumentException("no such command: "+commandId);
throw new IllegalArgumentException("no such command: "+cmd.command);
}

Actions actions = new Actions(driver);
actions.scrollToElement(cmdEntry)
.click(cmdEntry);
if (!cmdEntry.isDisplayed()) {
driver.findElement(By.className("expand-button")).click();
}

// https://stackoverflow.com/a/63420553
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(false);", cmdEntry);

for (CharSequence modifier : modifiers) {
new Actions(driver).scrollToElement(cmdEntry).click(cmdEntry).perform();

Actions actions = new Actions(driver);
for (CharSequence modifier : cmd.modifiers) {
actions = actions.keyDown(modifier);
}
actions.keyDown(key);
for (CharSequence modifier : modifiers) {
actions.keyDown(cmd.key);
for (CharSequence modifier : cmd.modifiers) {
actions = actions.keyUp(modifier);
}
actions.perform();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.io.IOException;

import static org.testng.Assert.assertEquals;
import static org.yorkxin.copyasmarkdown.e2e.keyboardshortcut.tabs.BaseTest.getCommandDescriptor;
import static org.yorkxin.copyasmarkdown.e2e.keyboardshortcut.tabs.BaseTest.runShortcutKeys;

public class OnPageContentsTest extends BaseTest{
@BeforeClass
Expand All @@ -27,14 +29,14 @@ public void configureKeyboardShortcuts() throws InterruptedException, AWTExcepti

public void configureKeyboardShortcutsInChrome() throws InterruptedException, AWTException {
openChromeKeyboardShortcutsPage();
setShortcutKeyInChrome("current tab: [title](url)", new CharSequence[]{Keys.ALT, Keys.SHIFT}, "q");
setShortcutKeyInChrome("Copy Selection as Markdown", new CharSequence[]{Keys.ALT, Keys.SHIFT}, "p");
setShortcutKeyInChrome(getCommandDescriptor("current-tab-link"));
setShortcutKeyInChrome(getCommandDescriptor("selection-as-markdown"));
}

public void configureKeyboardShortcutsInFirefox() throws InterruptedException, AWTException {
openFirefoxKeyboardShortcutsPage();
setShortcutKeyInFirefox("current-tab-link", new CharSequence[]{Keys.CONTROL, Keys.SHIFT}, "q");
setShortcutKeyInFirefox("selection-as-markdown", new CharSequence[]{Keys.CONTROL, Keys.SHIFT}, "p");
setShortcutKeyInFirefox(getCommandDescriptor("current-tab-link"));
setShortcutKeyInFirefox(getCommandDescriptor("selection-as-markdown"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

public class BaseTest extends org.yorkxin.copyasmarkdown.e2e.keyboardshortcut.BaseTest {
public static CommandDescriptor[] allCommandDescriptors = new CommandDescriptor[]{
new CommandDescriptor("current tab: [title](url)", "current-tab-link", new CharSequence[]{Keys.ALT, Keys.SHIFT}, "q"),
new CommandDescriptor("Copy Selection as Markdown", "selection-as-markdown", new CharSequence[]{Keys.ALT, Keys.SHIFT}, "p"),
new CommandDescriptor("all tabs: - [title](url)", "all-tabs-link-as-list", new CharSequence[]{Keys.ALT, Keys.SHIFT}, "w"),
new CommandDescriptor("all tabs: - [ ] [title](url)", "all-tabs-link-as-task-list", new CharSequence[]{Keys.ALT, Keys.SHIFT}, "e"),
new CommandDescriptor("all tabs: - title", "all-tabs-title-as-list", new CharSequence[]{Keys.ALT, Keys.SHIFT}, "r"),
Expand Down Expand Up @@ -61,14 +63,14 @@ public void configureKeyboardShortcuts() throws InterruptedException, AWTExcepti
public void configureKeyboardShortcutsInChrome() throws InterruptedException, AWTException {
openChromeKeyboardShortcutsPage();
for (CommandDescriptor cmd : allCommandDescriptors) {
setShortcutKeyInChrome(cmd.name, cmd.modifiers, cmd.key);
setShortcutKeyInChrome(cmd);
}
}

public void configureKeyboardShortcutsInFirefox() throws InterruptedException, AWTException {
openFirefoxKeyboardShortcutsPage();
for (CommandDescriptor cmd : allCommandDescriptors) {
setShortcutKeyInFirefox(cmd.command, cmd.modifiers, cmd.key);
setShortcutKeyInFirefox(cmd);
}
}
}

0 comments on commit 303be93

Please sign in to comment.