Skip to content

Commit b1a092f

Browse files
authored
fix Color unnecessary setConfig on click hex input (#366)
1 parent 150f670 commit b1a092f

6 files changed

Lines changed: 103 additions & 23 deletions

File tree

appium/TestConfigView.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ struct TestConfigView: View {
77
Button("Input Method") {
88
NSApp.mainWindow?.close()
99
ConfigWindowController.openWindow("im", InputMethodConfigController.self)
10-
}
10+
}.accessibilityIdentifier("Input Method")
1111
Button("Global Config") {
1212
NSApp.mainWindow?.close()
1313
ConfigWindowController.openWindow("global", GlobalConfigController.self)
14-
}
14+
}.accessibilityIdentifier("Global Config")
1515
Button("Theme") {
1616
NSApp.mainWindow?.close()
1717
ConfigWindowController.openWindow("theme", ThemeEditorController.self)
18-
}
18+
}.accessibilityIdentifier("Theme")
1919
Button("Advanced") {
2020
NSApp.mainWindow?.close()
2121
ConfigWindowController.openWindow("advanced", AdvancedController.self)
22-
}
22+
}.accessibilityIdentifier("Advanced")
2323
}
2424
}
2525
}

appium/test_color.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from appium.webdriver.webdriver import WebDriver
2+
3+
from util.config import read_theme_config
4+
from util.button import get_undo_redo, is_enabled
5+
from util.color import get_color_value, set_color_value
6+
from util.message import (
7+
BUTTON_SHOULD_BE_DISABLED,
8+
BUTTON_SHOULD_BE_ENABLED,
9+
CHANGE_NOT_SAVED,
10+
UI_NOT_UPDATED,
11+
)
12+
from util.window import find_element_by_id, open_theme_config
13+
14+
15+
LIGHT_SECTION = "LightMode"
16+
COLOR_ID = "HighlightColor"
17+
18+
19+
def test_highlight_color(driver: WebDriver, app: str):
20+
open_theme_config(driver)
21+
find_element_by_id(driver, LIGHT_SECTION).click()
22+
23+
def read_config_value() -> str:
24+
cfg = read_theme_config(app)
25+
return cfg[LIGHT_SECTION][COLOR_ID].lstrip("#")
26+
27+
field = find_element_by_id(driver, COLOR_ID)
28+
initial_value = get_color_value(field)
29+
new_value = "abcdef"
30+
undo, _ = get_undo_redo(driver)
31+
32+
set_color_value(field, new_value)
33+
assert get_color_value(field) == new_value, UI_NOT_UPDATED
34+
assert is_enabled(undo) is True, BUTTON_SHOULD_BE_ENABLED
35+
assert read_config_value() == new_value, CHANGE_NOT_SAVED
36+
37+
undo.click()
38+
assert get_color_value(field) == initial_value, UI_NOT_UPDATED
39+
assert is_enabled(undo) is False, BUTTON_SHOULD_BE_DISABLED
40+
assert read_config_value() == initial_value, CHANGE_NOT_SAVED

appium/test_string.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
UI_NOT_UPDATED,
1010
)
1111
from util.string import get_string_value
12-
from util.window import find_button, find_element_by_id, reset_option
12+
from util.window import find_element_by_id, open_theme_config, reset_option
1313

1414
CARET_SECTION = "Caret"
1515
STRING_ID = "Text"
1616

1717

1818
def test_theme_caret(driver: WebDriver, app: str):
19-
find_button(driver, "Theme").click()
19+
open_theme_config(driver)
2020
find_element_by_id(driver, CARET_SECTION).click()
2121

2222
def read_config_value() -> str:

appium/util/color.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from selenium.webdriver.remote.webelement import WebElement
2+
from appium.webdriver.common.appiumby import AppiumBy
3+
from util.key import press
4+
from util.window import find_element_by_id
5+
6+
7+
def get_color_value(element: WebElement) -> str:
8+
"""Get the current color value from a text element."""
9+
raw = element.get_attribute("value") # rgb 0.0705882 0.203922 0.337255 1
10+
return "".join(
11+
"%.2x" % round(float(x) * 255) for x in raw.split()[1:-1]
12+
) # ignore alpha
13+
14+
15+
def set_color_value(element: WebElement, value: str):
16+
driver = element.parent
17+
element.click()
18+
19+
# Slider mode
20+
buttons = driver.find_elements(AppiumBy.CLASS_NAME, "XCUIElementTypeButton")
21+
for button in buttons:
22+
if button.get_attribute("label") == "Color Sliders":
23+
button.click()
24+
break
25+
26+
# RGB slider
27+
button = driver.find_element(AppiumBy.CLASS_NAME, "XCUIElementTypePopUpButton")
28+
button.click()
29+
find_element_by_id(driver, "showRGBView:").click()
30+
31+
# Type hex
32+
hex_field = find_element_by_id(driver, "hex")
33+
hex_field.send_keys(value)
34+
press(driver, "\n") # Without it, below may throw StaleElementReferenceException.
35+
36+
# Close
37+
buttons = driver.find_elements(AppiumBy.CLASS_NAME, "XCUIElementTypeButton")
38+
for button in buttons:
39+
if (
40+
button.tag_name == "_XCUI:CloseWindow"
41+
and button.size["height"] == 12
42+
and button.size["width"] == 12
43+
):
44+
button.click()
45+
break

appium/util/window.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
11
from appium.webdriver.common.appiumby import AppiumBy
22
from appium.webdriver.webdriver import WebDriver
3-
from selenium.webdriver.common.by import By
43
from selenium.webdriver.remote.webelement import WebElement
54

65

7-
def find_button(driver: WebDriver, label: str) -> WebElement | None:
8-
"""Find a button by its title or label attribute."""
9-
buttons = driver.find_elements(By.CLASS_NAME, "XCUIElementTypeButton")
10-
for btn in buttons:
11-
title = btn.get_attribute("title") or ""
12-
btn_label = btn.get_attribute("label") or ""
13-
name = title or btn_label
14-
if label in name:
15-
return btn
16-
return None
17-
18-
196
def find_element_by_id(driver: WebDriver, identifier: str) -> WebElement:
207
"""Find an element by its accessibility identifier."""
218
elements = driver.find_elements(AppiumBy.ACCESSIBILITY_ID, identifier)
@@ -24,12 +11,17 @@ def find_element_by_id(driver: WebDriver, identifier: str) -> WebElement:
2411

2512
def open_global_config(driver: WebDriver):
2613
"""Open the Global Config window."""
27-
find_button(driver, "Global Config").click()
14+
find_element_by_id(driver, "Global Config").click()
15+
16+
17+
def open_theme_config(driver: WebDriver):
18+
"""Open the Theme window."""
19+
find_element_by_id(driver, "Theme").click()
2820

2921

3022
def open_advanced_config(driver: WebDriver):
3123
"""Open the Advanced Config window."""
32-
find_button(driver, "Advanced").click()
24+
find_element_by_id(driver, "Advanced").click()
3325

3426

3527
def reset_option(driver: WebDriver, option_id: str):

src/config/ColorView.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,17 @@ struct ColorView: OptionViewProtocol {
5454
HStack {
5555
if #available(macOS 14.0, *) {
5656
ColorPicker("", selection: $rgb, supportsOpacity: true)
57+
.accessibilityIdentifier(data["Option"] as? String ?? "")
5758
} else {
5859
ColorPicker("", selection: $rgb, supportsOpacity: false)
5960
Text("Alpha (0-255)")
6061
TextField("", value: $alpha, formatter: numberFormatter)
6162
}
6263
}
63-
.onChange(of: rgb) { _ in
64-
if rgb != stringToColor(value as? String ?? "") {
64+
.onChange(of: rgb) { newValue in
65+
// value's initial value is affected by fcitx's serialization rule (lower case, omit 255 alpha).
66+
// Compare full string to avoid unnecessary setConfig.
67+
if colorToString(newValue) != colorToString(stringToColor(value as? String ?? "")) {
6568
colorSubject.send()
6669
}
6770
}

0 commit comments

Comments
 (0)