Fix touchscreen navigation and add runtime touch diagnostics
This commit is contained in:
1 parent
1a40a6420c
commit
9d58d66fb7
32 files changed
+1785
-123
No files matched your search
+70
-2
@@ -62,9 +62,18 @@ public:
|
||||
std::vector<Line> lines;
|
||||
std::vector<uint8_t> pixels;
|
||||
Display(int w, int h) : DisplayDriver(w, h), pixels(w * h, 0) {}
|
||||
int bodyY() const { return width() < 64 ? 22 : 12; }
|
||||
int bodyY() const {
|
||||
const int content_height = width() < 64 ? 20 : 10;
|
||||
#if UI_READER_TOUCH_BAR
|
||||
return mesh::ui::readerTouchHeaderHeight(content_height) + 2;
|
||||
#else
|
||||
return content_height + 2;
|
||||
#endif
|
||||
}
|
||||
int bodyBottom() {
|
||||
#if UI_BUTTON_READER_HINT
|
||||
#if UI_READER_TOUCH_BAR
|
||||
return mesh::ui::makeReaderTouchBar(*this, height()).top;
|
||||
#elif UI_BUTTON_READER_HINT
|
||||
mesh::ui::SmallMessageText compact(*this);
|
||||
const bool small = useSmallMessageFont();
|
||||
DisplayDriver& hint_text = small ? static_cast<DisplayDriver&>(compact) : *this;
|
||||
@@ -164,8 +173,12 @@ int main(int argc, char** argv) {
|
||||
{128,64,6}, {64,128,6}, {160,80,0}, {80,160,0}, {240,135,0},
|
||||
{159,80,6}, {80,159,6}, {320,240,0}, {240,320,0}, {250,122,0}, {200,200,0},
|
||||
{72,40,5}, {40,72,5}, {128,32,5}, {32,128,5}, {64,48,5}, {48,64,5},
|
||||
{160,160,0},
|
||||
};
|
||||
for (const auto& dimensions : cases) {
|
||||
#if UI_READER_TOUCH_BAR
|
||||
if (dimensions[0] != 160 || dimensions[1] != 160) continue;
|
||||
#endif
|
||||
Display display(dimensions[0], dimensions[1]);
|
||||
Display expected(dimensions[0], dimensions[1]);
|
||||
const int top = expected.bodyY();
|
||||
@@ -196,9 +209,14 @@ int main(int argc, char** argv) {
|
||||
const bool small_hint = footer.useSmallMessageFont();
|
||||
DisplayDriver& hint_text = small_hint
|
||||
? static_cast<DisplayDriver&>(hint_compact) : footer;
|
||||
#if UI_READER_TOUCH_BAR
|
||||
const auto hint = mesh::ui::makeReaderTouchBar(hint_text, footer.height());
|
||||
mesh::ui::drawReaderTouchBar(hint_text, hint);
|
||||
#else
|
||||
const auto hint = mesh::ui::makeButtonReaderHintLayout(hint_text,
|
||||
small_hint ? hint_compact.glyphHeight() : 10, footer.height());
|
||||
mesh::ui::drawButtonReaderHint(hint_text, hint);
|
||||
#endif
|
||||
if (small_hint) {
|
||||
// Compare the real reader's hint pixels independently of the body.
|
||||
auto first = footer.pixels.begin() + hint.top * footer.width();
|
||||
@@ -262,7 +280,57 @@ int main(int argc, char** argv) {
|
||||
assert(display.body() == expected_pages.front()); // no wrap before start
|
||||
screen.handleInput(KEY_ENTER);
|
||||
assert(task.closed);
|
||||
#if UI_READER_TOUCH_BAR
|
||||
task.closed = false;
|
||||
mesh::ui::TouchInput input(true, true, 70, true, false);
|
||||
auto touch_gesture = [&](int sx, int sy, int ex, int ey) {
|
||||
display.clear(); screen.render(display);
|
||||
const auto* bar = screen.readerTouchBar();
|
||||
input.update(true, display.width()-1-sx, sy, display.width(), display.height(), false, nullptr, bar);
|
||||
input.update(true, display.width()-1-ex, ey, display.width(), display.height(), false, nullptr, bar);
|
||||
input.update(false, -1, -1, display.width(), display.height(), false, nullptr, bar);
|
||||
const auto action = input.update(false, -1, -1, display.width(), display.height(), false, nullptr, bar);
|
||||
switch (action) {
|
||||
case mesh::ui::TouchAction::Next: screen.handleInput(KEY_NEXT); break;
|
||||
case mesh::ui::TouchAction::Previous: screen.handleInput(KEY_PREV); break;
|
||||
case mesh::ui::TouchAction::VerticalNext: screen.handleInput(KEY_DOWN); break;
|
||||
case mesh::ui::TouchAction::VerticalPrevious: screen.handleInput(KEY_UP); break;
|
||||
case mesh::ui::TouchAction::Select: screen.handleInput(KEY_ENTER); break;
|
||||
default: assert(false);
|
||||
}
|
||||
};
|
||||
auto tap = [&](int cell) {
|
||||
const int x = display.width() * (2 * cell + 1) / 10;
|
||||
touch_gesture(x, screen.readerTouchBar()->top, x, screen.readerTouchBar()->top);
|
||||
};
|
||||
tap(2); assert(screen.flush());
|
||||
Position saved; assert(the_mesh.loadJohnBookmark(saved) && saved.verse == 1);
|
||||
tap(1); assert(screen.flush());
|
||||
assert(!the_mesh.loadJohnBookmark(saved) && saved.atStart());
|
||||
tap(3); assert(screen.flush());
|
||||
assert(the_mesh.loadJohnBookmark(saved) && referenceAt(saved.verse).chapter == 2);
|
||||
tap(0); assert(screen.flush());
|
||||
assert(!the_mesh.loadJohnBookmark(saved) && saved.atStart());
|
||||
tap(4); assert(task.closed);
|
||||
task.closed = false;
|
||||
touch_gesture(120,70,120,70); assert(screen.flush());
|
||||
assert(the_mesh.loadJohnBookmark(saved) && saved.verse == 1);
|
||||
touch_gesture(40,70,40,70); assert(screen.flush());
|
||||
assert(!the_mesh.loadJohnBookmark(saved) && saved.atStart());
|
||||
touch_gesture(120,70,40,70); assert(screen.flush());
|
||||
assert(the_mesh.loadJohnBookmark(saved) && saved.verse == 1);
|
||||
touch_gesture(40,70,120,70); assert(screen.flush());
|
||||
assert(!the_mesh.loadJohnBookmark(saved) && saved.atStart());
|
||||
touch_gesture(80,100,80,50); assert(screen.flush());
|
||||
assert(the_mesh.loadJohnBookmark(saved) && referenceAt(saved.verse).chapter == 2);
|
||||
touch_gesture(80,50,80,100); assert(screen.flush());
|
||||
assert(!the_mesh.loadJohnBookmark(saved) && saved.atStart());
|
||||
touch_gesture(80,0,80,0); assert(task.closed);
|
||||
#endif
|
||||
}
|
||||
#if UI_READER_TOUCH_BAR
|
||||
return 0; // The remaining fault-injection cases use tiny non-touch viewports.
|
||||
#endif
|
||||
|
||||
// Group navigation starts at verse one of the adjacent chapter, resets
|
||||
// the page offset, stops at book boundaries, and checkpoints normally.
|
||||
|
||||
@@ -130,10 +130,11 @@ class CompanionJohnTest(unittest.TestCase):
|
||||
self.run_checked([cc, "-Os", *flags, "-c", str(ROOT / "src/helpers/ota/OtaTinf.c"),
|
||||
"-o", str(obj)])
|
||||
for small_font in (0, 1):
|
||||
for button_hint in (0, 1):
|
||||
for button_hint, touch_bar in ((0, 0), (1, 0), (1, 1)):
|
||||
self.run_checked([cxx, "-std=c++11", "-Os", "-Wall", "-Wextra", "-Werror", "-Wno-unused-parameter",
|
||||
*flags, f"-DUI_SMALL_MESSAGE_FONT={small_font}",
|
||||
f"-DUI_BUTTON_READER_HINT={button_hint}",
|
||||
f"-DUI_READER_TOUCH_BAR={touch_bar}",
|
||||
"-I" + str(ROOT / "src"), "-I" + str(FIXTURE),
|
||||
str(FIXTURE / "test_reader.cpp"), str(ROOT / "src/helpers/CompanionJohn.cpp"),
|
||||
str(obj), "-o", str(binary)])
|
||||
|
||||
@@ -94,9 +94,16 @@ assert re.search(
|
||||
|
||||
# Terminal-specific handlers keep precedence, then both the generic `set`
|
||||
# fallthrough and the final unknown-command fallthrough delegate to the shared
|
||||
# framed/rescue command surface. This keeps get/set radio, get name, and
|
||||
# variant commands available without routing terminal-only commands twice.
|
||||
assert terminal.count("handleCommand(command, 0, local_reply)") == 2
|
||||
# framed/rescue command surface. The explicit frequency branch also delegates
|
||||
# to the strict shared frequency parser; it is not a third fallthrough.
|
||||
freq_branch = re.search(
|
||||
r'else if \(strncmp\(config, "freq ", 5\) == 0\) \{([^{}]*)\}', terminal
|
||||
)
|
||||
assert freq_branch is not None
|
||||
assert "handleCommand(command, 0, local_reply)" in freq_branch.group(1)
|
||||
assert terminal.replace(freq_branch.group(0), "").count(
|
||||
"handleCommand(command, 0, local_reply)"
|
||||
) == 2
|
||||
assert terminal.index('strncmp(config, "tx ", 3)') < terminal.index(
|
||||
"handleCommand(command, 0, local_reply)"
|
||||
)
|
||||
|
||||
@@ -5,12 +5,81 @@ import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from test_reader_touch_coordinates import PREAMBLE
|
||||
from test_replay_reset_integration import extract_braced
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
UI = ROOT / "examples" / "companion_radio" / "ui-new" / "UITask.cpp"
|
||||
|
||||
|
||||
class CompanionTransportSelectorTest(unittest.TestCase):
|
||||
def test_centered_choices_fit_and_preserve_full_height_navigation(self):
|
||||
actual = extract_braced(UI.read_text(), "static void drawCompanionTransportChoice(")
|
||||
generic = extract_braced(
|
||||
(ROOT / "src/helpers/ui/LGFXDisplay.cpp").read_text(),
|
||||
"bool LGFXDisplay::getTouch(")
|
||||
scenarios = r'''
|
||||
#include <helpers/ui/CompanionTransportSelectorLayout.h>
|
||||
using namespace mesh::ui;
|
||||
struct MeasuredDisplay : LGFXDisplay {
|
||||
int size=1,bx=0,by=0,bw=160,bh=160,last_bottom=0;
|
||||
void setTextSize(int s) override { size=s; }
|
||||
uint16_t getTextWidth(const char* text) override { return strlen(text)*6*size; }
|
||||
void print(const char* text) override {
|
||||
assert(cx>=bx+2 && cx+getTextWidth(text)<=bx+bw-2);
|
||||
assert(cy>=by && cy+8*size<=by+bh);
|
||||
assert(cy>=last_bottom);
|
||||
last_bottom=cy+8*size;
|
||||
LGFXDisplay::print(text);
|
||||
}
|
||||
};
|
||||
int main() {
|
||||
const auto layout=makeCompanionTransportSelectorLayout(160,160,70);
|
||||
assert(layout.side_nav_width==24);
|
||||
assert(layout.wifi.x==26 && layout.wifi.width==52);
|
||||
assert(layout.bluetooth.x==82 && layout.bluetooth.width==52);
|
||||
const TouchSplitSelector selector={layout.wifi.x,layout.wifi.width,
|
||||
layout.bluetooth.x,layout.bluetooth.width,layout.wifi.y,
|
||||
layout.wifi.height,layout.side_nav_width};
|
||||
for(bool mirror : {false,true}) for(int y=0;y<160;++y) for(int x=0;x<160;++x) {
|
||||
TouchAction expected=TouchAction::None;
|
||||
if(x<24) expected=TouchAction::Previous;
|
||||
else if(x>=136) expected=TouchAction::Next;
|
||||
else if(y>=40 && y<140) {
|
||||
if(x>=26 && x<78) expected=TouchAction::SelectLeft;
|
||||
if(x>=82 && x<134) expected=TouchAction::SelectRight;
|
||||
}
|
||||
TouchInput input(true,true,70,mirror,false);
|
||||
const int tx=mirror ? 159-x : x;
|
||||
input.update(true,tx,y,160,160,false,&selector);
|
||||
input.update(false,-1,-1,160,160,false,&selector);
|
||||
assert(input.update(false,-1,-1,160,160,false,&selector)==expected);
|
||||
}
|
||||
for(int canvas : {320,480}) for(bool wifi : {false,true})
|
||||
for(bool active : {false,true}) for(bool selected : {false,true}) {
|
||||
MeasuredDisplay d;
|
||||
d._coordinateScale=canvas/160; d._outputZoom=480.0f/canvas;
|
||||
const auto& box=wifi ? layout.wifi : layout.bluetooth;
|
||||
d.bx=box.x; d.by=box.y; d.bw=box.width; d.bh=box.height;
|
||||
drawCompanionTransportChoice(d,box.x,box.y,box.width,box.height,
|
||||
wifi ? "WiFi" : "BLE",active,selected);
|
||||
assert(d.labels.size()==(active || selected ? 2 : 1));
|
||||
}
|
||||
assert(4*6*3<=160-2*layout.side_nav_width); // MODE
|
||||
assert(9*6*2<=160-2*layout.side_nav_width); // TAP A BOX
|
||||
}
|
||||
'''
|
||||
with tempfile.TemporaryDirectory() as temp:
|
||||
binary = Path(temp) / "centered_transport"
|
||||
compiled = subprocess.run(["c++", "-std=c++11", "-O1",
|
||||
"-fsanitize=address,undefined", "-fno-pie", "-no-pie",
|
||||
f"-I{ROOT / 'src'}", "-x", "c++", "-", "-o", str(binary)],
|
||||
input=PREAMBLE + generic + actual + scenarios,
|
||||
text=True, capture_output=True)
|
||||
self.assertEqual(compiled.returncode, 0, compiled.stderr)
|
||||
tested = subprocess.run([str(binary)], text=True, capture_output=True)
|
||||
self.assertEqual(tested.returncode, 0, tested.stderr)
|
||||
|
||||
def test_indicator_layout_has_room_for_large_transport_text(self):
|
||||
source = r'''
|
||||
#include <helpers/ui/CompanionTransportSelectorLayout.h>
|
||||
@@ -195,12 +264,12 @@ int main() {
|
||||
self.assertIn('large_transport_text ? "NEXT" : "NEXT BOOT"', source)
|
||||
self.assertIn('display.setTextSize(3);', source)
|
||||
self.assertIn('display.width() / 2, layout.title_y, "MODE"', source)
|
||||
self.assertIn('layout.show_title ? "TAP SIDE" : "tap a box"', source)
|
||||
self.assertIn('layout.show_title ? "TAP A BOX" : "tap a box"', source)
|
||||
self.assertIn("makeCompanionTransportSelectorLayout", source)
|
||||
|
||||
handler_start = source.index(
|
||||
"if (_page == HomePage::TRANSPORT\n"
|
||||
" && (c == KEY_ENTER || c == KEY_UP || c == KEY_DOWN))"
|
||||
" && (key == KEY_ENTER || key == KEY_UP || key == KEY_DOWN))"
|
||||
)
|
||||
handler_end = source.index("#else", handler_start)
|
||||
handler = source[handler_start:handler_end]
|
||||
@@ -239,7 +308,8 @@ int main() {
|
||||
"->isTransportSelectorPage()",
|
||||
source,
|
||||
)
|
||||
self.assertIn("curr == msg_preview, split_transport_selector", source)
|
||||
self.assertIn("curr == msg_preview && UI_MESSAGE_CHANNEL_FOOTER == 1,", source)
|
||||
self.assertIn("split_transport_selector, reader_touch_bar);", source)
|
||||
self.assertIn(
|
||||
"case mesh::ui::TouchAction::SelectLeft:\n"
|
||||
" c = checkDisplayOn(KEY_UP);",
|
||||
|
||||
@@ -12,6 +12,25 @@ UI = ROOT / "examples" / "companion_radio" / "ui-new" / "UITask.cpp"
|
||||
|
||||
|
||||
class IndicatorMessagesProfileTest(unittest.TestCase):
|
||||
def test_touch_profile_has_one_navigation_bar_and_no_hidden_channel_targets(self):
|
||||
profile = PROFILE.read_text()
|
||||
ui = UI.read_text()
|
||||
for flag in ("UI_BUTTON_READER_HINT", "UI_READER_TOUCH_BAR"):
|
||||
self.assertIn(f"-D {flag}=1", profile)
|
||||
self.assertIn("-D UI_MESSAGE_CHANNEL_FOOTER=0", profile)
|
||||
self.assertIn("curr == msg_preview && UI_MESSAGE_CHANNEL_FOOTER == 1", ui)
|
||||
# Compile the real gate: HAS_TOUCH must not overwrite the explicit
|
||||
# physical-button footer setting with zero.
|
||||
start = ui.index("#ifndef UI_BUTTON_READER_HINT")
|
||||
gate = ui[start:ui.index("#if COMPANION_FEATURE_JOHN", start)]
|
||||
result = subprocess.run(["c++", "-E", "-P", "-x", "c++", "-"],
|
||||
input="#define HAS_TOUCH 1\n#define UI_BUTTON_READER_HINT 1\n" + gate +
|
||||
"\n#if UI_BUTTON_READER_HINT != 1\n#error reader hint disabled\n#endif\n",
|
||||
text=True, capture_output=True)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertIn("static_cast<MsgPreviewScreen*>(msg_preview)->readerTouchBar()", ui)
|
||||
self.assertIn("static_cast<JohnReaderScreen*>(john_reader)->readerTouchBar()", ui)
|
||||
|
||||
def test_indicator_enables_messages_home_page(self):
|
||||
profile = PROFILE.read_text()
|
||||
ui = UI.read_text()
|
||||
|
||||
@@ -200,7 +200,11 @@ int main() {
|
||||
self.assertIn("isCompanionWiFiConnected()", page)
|
||||
self.assertIn("hasCompanionWiFiCredentials()", page)
|
||||
self.assertIn("CompanionWiFiDisplayState::NotConfigured", page)
|
||||
self.assertIn('"TAP TO START"', page)
|
||||
# The shared page uses the input-specific label; Indicator's touch
|
||||
# profile must still resolve it to TAP, not the button-only HOLD.
|
||||
self.assertIn('SELECT_LABEL " TO START"', page)
|
||||
touch_labels = ui.split("#ifdef HAS_TOUCH", 1)[1].split("#elif", 1)[0]
|
||||
self.assertIn('#define SELECT_LABEL "TAP"', touch_labels)
|
||||
self.assertIn("wifi_connected != _wifi_was_connected", ui)
|
||||
self.assertIn("CompanionWiFiDisplayState::Ready", page)
|
||||
self.assertIn("display.clear()", page)
|
||||
|
||||
+225
-28
@@ -2,6 +2,8 @@
|
||||
"""Exercise actual button routing and message filters with a host display."""
|
||||
|
||||
from pathlib import Path
|
||||
from itertools import product
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
@@ -21,16 +23,22 @@ PREAMBLE = r'''
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#define UI_SMALL_MESSAGE_FONT 0
|
||||
#ifndef UI_BUTTON_READER_HINT
|
||||
#define UI_BUTTON_READER_HINT 1
|
||||
#endif
|
||||
#ifndef UI_MESSAGE_CHANNEL_FOOTER
|
||||
#define UI_MESSAGE_CHANNEL_FOOTER 0
|
||||
#endif
|
||||
#ifndef UI_COMPACT_MESSAGE_STATUS
|
||||
#define UI_COMPACT_MESSAGE_STATUS 0
|
||||
#endif
|
||||
#define UI_MSG_PREVIEW_SIZE 161
|
||||
#define MAX_GROUP_CHANNELS 4
|
||||
#define AUTO_OFF_MILLIS 15000
|
||||
#define MESH_DEBUG_PRINTLN(...)
|
||||
ColorVal UIColor::window_bkg=0, UIColor::title_bkg=0, UIColor::title_txt=1;
|
||||
ColorVal UIColor::primary_txt=1, UIColor::secondary_txt=1, UIColor::warning_txt=1;
|
||||
ColorVal UIColor::popup_bkg=0, UIColor::popup_txt=1, UIColor::corp_blue=1;
|
||||
ColorVal UIColor::popup_bkg=0, UIColor::popup_txt=1, UIColor::corp_blue=2;
|
||||
uint64_t companionMessageNowMillis() { return millis(); }
|
||||
uint64_t companionMessageElapsedMillis(uint64_t when) { return millis()-when; }
|
||||
struct StrHelper {
|
||||
@@ -46,30 +54,50 @@ struct Mesh {
|
||||
}
|
||||
} the_mesh;
|
||||
struct Display : DisplayDriver {
|
||||
struct Line { int x,y; std::string text; };
|
||||
struct Line { int x,y; std::string text; ColorVal color; };
|
||||
struct Rect { int x,y,w,h; ColorVal color; };
|
||||
std::vector<Line> lines;
|
||||
std::vector<Rect> rectangles;
|
||||
ColorVal color=1;
|
||||
int x=0,y=0;
|
||||
bool on=true;
|
||||
bool on=true, compact=false;
|
||||
#ifdef TEST_INDICATOR_CANVAS
|
||||
Display() : DisplayDriver(160,160) {}
|
||||
int renderWidth() const override { return TEST_INDICATOR_CANVAS; }
|
||||
int renderHeight() const override { return TEST_INDICATOR_CANVAS; }
|
||||
#else
|
||||
Display() : DisplayDriver(128,64) {}
|
||||
#endif
|
||||
bool isOn() override { return on; }
|
||||
void turnOn() override { on=true; }
|
||||
void turnOff() override { on=false; }
|
||||
void clear() override { lines.clear(); }
|
||||
void clear() override { lines.clear(); rectangles.clear(); }
|
||||
void startFrame(ColorVal=0) override { clear(); }
|
||||
void endFrame() override {}
|
||||
void setTextSize(int) override {}
|
||||
void setColor(ColorVal) override {}
|
||||
void setCompactText(bool value) override { compact=value; }
|
||||
void setColor(ColorVal value) override { color=value; }
|
||||
void setCursor(int a,int b) override { x=a;y=b; }
|
||||
uint16_t getTextWidth(const char* text) override { return strlen(text)*4; }
|
||||
uint16_t getTextWidth(const char* text) override {
|
||||
#ifdef TEST_INDICATOR_CANVAS
|
||||
// Indicator font: 18x24 physical pixels at the ordinary 3x scale;
|
||||
// native non-compact text is enlarged 20%. Round bounds outwards.
|
||||
return (strlen(text)*(TEST_INDICATOR_CANVAS==480 && !compact ? 72 : 60)+9)/10;
|
||||
#else
|
||||
return strlen(text)*4;
|
||||
#endif
|
||||
}
|
||||
void print(const char* text) override {
|
||||
assert(x>=0 && x+getTextWidth(text)<=width());
|
||||
assert(y>=0 && y+8<=height());
|
||||
lines.push_back({x,y,text});
|
||||
lines.push_back({x,y,text,color});
|
||||
}
|
||||
void fillRect(int a,int b,int w,int h) override {
|
||||
assert(a>=0 && b>=0 && a+w<=width() && b+h<=height());
|
||||
}
|
||||
void drawRect(int a,int b,int w,int h) override { fillRect(a,b,w,h); }
|
||||
void drawRect(int a,int b,int w,int h) override {
|
||||
fillRect(a,b,w,h); rectangles.push_back({a,b,w,h,color});
|
||||
}
|
||||
void drawXbm(int,int,const uint8_t*,int,int) override {}
|
||||
bool contains(const char* text) const {
|
||||
for (const auto& line:lines) if (line.text==text) return true;
|
||||
@@ -78,8 +106,9 @@ struct Display : DisplayDriver {
|
||||
};
|
||||
struct Screen : UIScreen {
|
||||
uint8_t key=0;
|
||||
int events=0;
|
||||
int render(DisplayDriver&) override { return 0; }
|
||||
bool handleInput(char c) override { key=static_cast<uint8_t>(c); return true; }
|
||||
bool handleInput(char c) override { ++events; key=static_cast<uint8_t>(c); return true; }
|
||||
};
|
||||
MomentaryButton user_btn(7,1000,true,true,true);
|
||||
class UITask {
|
||||
@@ -95,11 +124,15 @@ public:
|
||||
bool isJohnReaderActive() const { return john_reader && curr==john_reader; }
|
||||
void gotoHomeScreen() { curr=home; }
|
||||
void toggleBuzzer() { ++buzzer_changes; }
|
||||
void showJohnReader() { curr=john_reader; }
|
||||
void showAlert(const char*,int) {}
|
||||
char handleLongPress(char c) { return c; }
|
||||
char handleMultiClick(char,bool);
|
||||
char handleDoubleClick(char);
|
||||
char checkDisplayOn(char);
|
||||
void pollButton();
|
||||
void routeTouch(mesh::ui::TouchAction action);
|
||||
bool isButtonGesturePending() const;
|
||||
};
|
||||
'''
|
||||
|
||||
@@ -112,7 +145,7 @@ void gesture(UITask& task,int count) {
|
||||
g_mock_millis+=25; task.pollButton();
|
||||
if (tap+1<count) { g_mock_millis+=80; task.pollButton(); }
|
||||
}
|
||||
g_mock_millis+=280; task.pollButton();
|
||||
g_mock_millis+=MOMENTARY_BUTTON_MULTI_CLICK_MS; task.pollButton();
|
||||
}
|
||||
int main() {
|
||||
resetArduinoMock();
|
||||
@@ -120,7 +153,8 @@ int main() {
|
||||
user_btn.begin(); user_btn.enableQuadrupleClick();
|
||||
Display display;
|
||||
UITask task(display);
|
||||
Screen home,group;
|
||||
HomeScreen home(&task);
|
||||
Screen group;
|
||||
MsgPreviewScreen messages(&task);
|
||||
task.home=&home; task.msg_preview=task.curr=&messages; task.john_reader=&group;
|
||||
messages.addPreview(1,"Alice","public old",0,"Public");
|
||||
@@ -130,7 +164,58 @@ int main() {
|
||||
auto expect=[&](const char* header,const char* body) {
|
||||
assert(task.curr==&messages);
|
||||
display.clear(); messages.render(display);
|
||||
#if UI_MESSAGE_CHANNEL_FOOTER
|
||||
std::string status="Message "; status+=strrchr(header,' ')+1;
|
||||
assert(display.contains(status.c_str()) && display.contains(body));
|
||||
#else
|
||||
assert(display.contains(header) && display.contains(body));
|
||||
#endif
|
||||
display.setCompactText(UI_COMPACT_MESSAGE_STATUS);
|
||||
const int bottom=display.height()-(UI_MESSAGE_CHANNEL_FOOTER ?
|
||||
mesh::ui::makeCompanionMessageChromeLayout(UI_COMPACT_MESSAGE_STATUS).filter_height : 0);
|
||||
#if UI_READER_TOUCH_BAR
|
||||
const auto hint=mesh::ui::makeReaderTouchBar(display,bottom);
|
||||
const int exit_height=messages.readerTouchBar()->exit_height;
|
||||
assert(exit_height>=24);
|
||||
for(const auto& line:display.lines) {
|
||||
if(line.text==header) assert(line.y>0 && line.y+8<exit_height);
|
||||
if(line.text==body) assert(line.y>=exit_height);
|
||||
}
|
||||
bool header_divider=false;
|
||||
for(const auto& rect:display.rectangles)
|
||||
if(rect.y==exit_height-1 && rect.h==1 && rect.w==display.width()) header_divider=true;
|
||||
assert(header_divider);
|
||||
const char* labels[]={"4<<","2<",">1",">>3","X"};
|
||||
for(int cell=0;cell<5;++cell) {
|
||||
assert(display.contains(labels[cell]));
|
||||
for(const auto& line:display.lines) if(line.text==labels[cell]) {
|
||||
assert(line.x>=display.width()*cell/5);
|
||||
assert(line.x+display.getTextWidth(labels[cell])<=display.width()*(cell+1)/5);
|
||||
assert(line.y>=hint.top && line.y+8<=hint.top+hint.height);
|
||||
assert(line.color==UIColor::corp_blue);
|
||||
}
|
||||
}
|
||||
bool divider=false;
|
||||
for(const auto& rect:display.rectangles)
|
||||
if(rect.x==0 && rect.y==hint.top && rect.w==display.width()
|
||||
&& rect.h==1 && rect.color==UIColor::corp_blue) divider=true;
|
||||
assert(divider);
|
||||
#if !UI_MESSAGE_CHANNEL_FOOTER
|
||||
assert(hint.top+hint.height==display.height());
|
||||
assert(!display.contains("<") && !display.contains(">"));
|
||||
#endif
|
||||
for(const auto& line:display.lines)
|
||||
if(line.text==body) assert(line.y+8<=hint.top);
|
||||
#else
|
||||
const auto hint=mesh::ui::makeButtonReaderHintLayout(display,display.textLineHeight(),bottom);
|
||||
for(int row=0;row<hint.line_count;++row) assert(display.contains(hint.lines[row]));
|
||||
for(const auto& line:display.lines) {
|
||||
if(line.text==body) assert(line.y+8<=hint.top);
|
||||
for(int row=0;row<hint.line_count;++row)
|
||||
if(line.text==hint.lines[row]) assert(line.y==hint.top+row*hint.line_height);
|
||||
}
|
||||
#endif
|
||||
display.setCompactText(false);
|
||||
assert(task.buzzer_changes==0);
|
||||
};
|
||||
expect("DM 1/1","direct message");
|
||||
@@ -151,6 +236,47 @@ int main() {
|
||||
assert(display.on); expect("DM 1/1","direct message");
|
||||
gesture(task,4); expect("Ch 3 0/0","No buffered messages");
|
||||
gesture(task,4); expect("Ch 2 1/1","second");
|
||||
#if UI_READER_TOUCH_BAR
|
||||
// Taps use the same freshly rendered rectangles and actual UI action route.
|
||||
mesh::ui::TouchInput touch(true,true,70,true,false);
|
||||
auto tap=[&](int cell) {
|
||||
display.clear(); messages.render(display);
|
||||
const auto* bar=messages.readerTouchBar();
|
||||
const int visual_x=display.width()*(2*cell+1)/10;
|
||||
const int raw_x=display.width()-1-visual_x;
|
||||
const int y=bar->top+bar->height/2;
|
||||
touch.update(true,raw_x,y,display.width(),display.height(),UI_MESSAGE_CHANNEL_FOOTER,nullptr,bar);
|
||||
touch.update(true,raw_x,y,display.width(),display.height(),UI_MESSAGE_CHANNEL_FOOTER,nullptr,bar);
|
||||
touch.update(false,-1,-1,display.width(),display.height(),UI_MESSAGE_CHANNEL_FOOTER,nullptr,bar);
|
||||
task.routeTouch(touch.update(false,-1,-1,display.width(),display.height(),UI_MESSAGE_CHANNEL_FOOTER,nullptr,bar));
|
||||
};
|
||||
tap(0); expect("Ch 0 1/2","public new");
|
||||
tap(2); expect("Ch 0 2/2","public old");
|
||||
tap(1); expect("Ch 0 1/2","public new");
|
||||
tap(3); expect("Ch 2 1/1","second");
|
||||
display.on=false; tap(4); assert(display.on && task.curr==&messages);
|
||||
tap(4); assert(task.curr==&home);
|
||||
task.curr=&messages;
|
||||
auto body_gesture=[&](int sx,int sy,int ex,int ey) {
|
||||
display.clear(); messages.render(display);
|
||||
const auto* bar=messages.readerTouchBar();
|
||||
touch.update(true,display.width()-1-sx,sy,display.width(),display.height(),false,nullptr,bar);
|
||||
touch.update(true,display.width()-1-ex,ey,display.width(),display.height(),false,nullptr,bar);
|
||||
touch.update(false,-1,-1,display.width(),display.height(),false,nullptr,bar);
|
||||
task.routeTouch(touch.update(false,-1,-1,display.width(),display.height(),false,nullptr,bar));
|
||||
};
|
||||
tap(0); expect("Ch 0 1/2","public new");
|
||||
body_gesture(120,70,120,70); expect("Ch 0 2/2","public old");
|
||||
body_gesture(40,70,40,70); expect("Ch 0 1/2","public new");
|
||||
body_gesture(120,70,40,70); expect("Ch 0 2/2","public old");
|
||||
body_gesture(40,70,120,70); expect("Ch 0 1/2","public new");
|
||||
body_gesture(80,100,80,50); expect("Ch 2 1/1","second");
|
||||
body_gesture(80,50,80,100); expect("Ch 0 1/2","public new");
|
||||
body_gesture(48,messages.readerTouchBar()->top-1,48,messages.readerTouchBar()->top-1);
|
||||
expect("Ch 0 1/2","public new"); // just missing 2< cannot close the reader
|
||||
body_gesture(80,0,80,0); assert(task.curr==&home);
|
||||
task.curr=&messages;
|
||||
#endif
|
||||
// Long press exits, without a delayed tap changing the home screen.
|
||||
g_mock_pin_levels[7]=LOW; task.pollButton();
|
||||
g_mock_millis+=25; task.pollButton();
|
||||
@@ -158,15 +284,58 @@ int main() {
|
||||
assert(task.curr==&home);
|
||||
g_mock_pin_levels[7]=HIGH; task.pollButton();
|
||||
g_mock_millis+=25; task.pollButton();
|
||||
g_mock_millis+=280; task.pollButton();
|
||||
g_mock_millis+=MOMENTARY_BUTTON_MULTI_CLICK_MS; task.pollButton();
|
||||
assert(home.key==0);
|
||||
#if COMPANION_FEATURE_JOHN
|
||||
task.curr=&group;
|
||||
gesture(task,1); assert(group.key==KEY_NEXT && group.events==1);
|
||||
gesture(task,2); assert(group.key==KEY_PREV && group.events==2);
|
||||
gesture(task,3); assert(group.key==KEY_DOWN);
|
||||
gesture(task,4); assert(group.key==KEY_UP);
|
||||
assert(task.buzzer_changes==0);
|
||||
#ifdef TEST_INDICATOR_CANVAS
|
||||
// Poll real pin waveforms while periodic synchronous RGB work is due.
|
||||
// A redraw between clicks must wait, not erase a short press/release from
|
||||
// the poller's history. Cover quick and deliberate multi-click rhythms.
|
||||
for(int clicks : {3,4}) for(int gap : {80,320,430}) {
|
||||
group.events=0; group.key=0;
|
||||
uint32_t elapsed=0;
|
||||
const uint32_t active_until=(clicks-1)*(70+gap)+70;
|
||||
uint32_t redraw_at=35; int redraws=0;
|
||||
while(elapsed<active_until+MOMENTARY_BUTTON_MULTI_CLICK_MS+300) {
|
||||
g_mock_pin_levels[7]=(elapsed<active_until && elapsed%(70+gap)<70) ? LOW : HIGH;
|
||||
task.pollButton();
|
||||
if(elapsed>=redraw_at && !task.isButtonGesturePending()) {
|
||||
// Simulate a slow synchronous frame, including the time the poller
|
||||
// cannot run. With the old unconditional render this loses clicks.
|
||||
elapsed+=200; g_mock_millis+=200; redraw_at=elapsed+1000; ++redraws;
|
||||
}
|
||||
elapsed+=5; g_mock_millis+=5;
|
||||
}
|
||||
assert(group.events==1 && group.key==(clicks==3 ? KEY_DOWN : KEY_UP));
|
||||
assert(redraws>0 && !task.isButtonGesturePending());
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
task.curr=&home;
|
||||
// Exercise the actual home-page navigation, not just a recorded key.
|
||||
gesture(task,1); assert(home._page==1 && home.events==1);
|
||||
gesture(task,2); assert(home._page==0 && home.events==2);
|
||||
gesture(task,2); assert(home._page==HomeScreen::Count-1 && home.events==3);
|
||||
gesture(task,1); assert(home._page==0 && home.events==4);
|
||||
// No premature single-click while a second press is still possible.
|
||||
g_mock_pin_levels[7]=LOW; task.pollButton();
|
||||
g_mock_millis+=25; task.pollButton();
|
||||
g_mock_pin_levels[7]=HIGH; task.pollButton();
|
||||
g_mock_millis+=25; task.pollButton();
|
||||
g_mock_millis+=200; task.pollButton();
|
||||
assert(home._page==0 && home.events==4);
|
||||
g_mock_pin_levels[7]=LOW; task.pollButton();
|
||||
g_mock_millis+=25; task.pollButton();
|
||||
g_mock_pin_levels[7]=HIGH; task.pollButton();
|
||||
g_mock_millis+=25; task.pollButton();
|
||||
g_mock_millis+=MOMENTARY_BUTTON_MULTI_CLICK_MS; task.pollButton();
|
||||
assert(home._page==HomeScreen::Count-1 && home.events==5);
|
||||
gesture(task,3); assert(task.buzzer_changes==1);
|
||||
gesture(task,4); assert(task.buzzer_changes==2);
|
||||
}
|
||||
@@ -182,25 +351,53 @@ class MessageNavigationTest(unittest.TestCase):
|
||||
button_route = source[start:source.index(" #endif", start)]
|
||||
implementation = "\n".join(extract_braced(source, signature) for signature in (
|
||||
"char UITask::checkDisplayOn(", "char UITask::handleDoubleClick(",
|
||||
"char UITask::handleMultiClick("))
|
||||
"char UITask::handleMultiClick(", "bool UITask::isButtonGesturePending("))
|
||||
self.assertIn("millis() >= _next_refresh && curr && !isButtonGesturePending()", source)
|
||||
implementation += "\n" + extract_braced(source, "class MsgPreviewScreen :") + ";\n"
|
||||
implementation += "void UITask::pollButton() { char c=0; int ev=user_btn.check();\n"
|
||||
implementation += button_route + "\nif(c && curr) curr->handleInput(c);\n}\n"
|
||||
for enabled in (0, 1):
|
||||
for signedness in ("-fsigned-char", "-funsigned-char"):
|
||||
with self.subTest(feature=enabled, signedness=signedness), tempfile.TemporaryDirectory() as temp:
|
||||
binary = Path(temp) / "navigation"
|
||||
result = subprocess.run([
|
||||
"c++", "-std=c++17", signedness,
|
||||
f"-DCOMPANION_FEATURE_JOHN={enabled}",
|
||||
"-I" + str(ROOT / "src"), "-I" + str(ROOT / "test/mocks"),
|
||||
"-fsanitize=address,undefined", "-fno-pie", "-no-pie",
|
||||
"-x", "c++", "-", str(ROOT / "src/helpers/ui/MomentaryButton.cpp"),
|
||||
"-o", str(binary),
|
||||
], input=PREAMBLE + implementation + SCENARIOS, text=True, capture_output=True)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
result = subprocess.run([str(binary)], text=True, capture_output=True)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
touch_route = extract_braced(source[source.index("void UITask::loop()"):], "switch (action)")
|
||||
implementation += ("void UITask::routeTouch(mesh::ui::TouchAction action) { "
|
||||
"char c=0; bool on_transport_selector=false;\n" + touch_route +
|
||||
"\nif(c && curr) curr->handleInput(c); }\n")
|
||||
home = source[source.index("class HomeScreen :"):]
|
||||
home_navigation = home[home.index(" bool handleInput(char c) override {"):]
|
||||
home_navigation = home_navigation.split("#ifdef COMPANION_EXCLUSIVE_WIFI_BLE", 1)[0]
|
||||
home_navigation = home_navigation.replace("{\n", "{\n Screen::handleInput(c);\n", 1)
|
||||
implementation += ("class HomeScreen : public Screen { public: UITask* _task; "
|
||||
"enum HomePage {FIRST,MESSAGES,RECENT,RADIO,Count}; int _page=0; "
|
||||
"HomeScreen(UITask* task):_task(task){}\n" + home_navigation +
|
||||
"return false; } };\n")
|
||||
target = (ROOT / "variants/sensecap_indicator-espnow/target.cpp").read_text()
|
||||
button = re.search(r"MomentaryButton user_btn\([^;]+;", target).group(0)
|
||||
profile = (ROOT / "variants/sensecap_indicator-espnow/platformio.ini").read_text()
|
||||
indicator_flags = ["-DHAS_TOUCH=1", "-DPIN_USER_BTN=7"]
|
||||
for name in ("UI_BUTTON_READER_HINT", "UI_READER_TOUCH_BAR", "UI_MESSAGE_CHANNEL_FOOTER", "UI_COMPACT_MESSAGE_STATUS",
|
||||
"MOMENTARY_BUTTON_MULTI_CLICK_MS", "UI_DEFER_RENDER_DURING_BUTTON_GESTURE"):
|
||||
value = re.search(r"^\s*-D " + name + r"=(\d+)\s*$", profile, re.M)
|
||||
self.assertIsNotNone(value, name)
|
||||
indicator_flags.append(f"-D{name}={value.group(1)}")
|
||||
for enabled, signedness, canvas in product(
|
||||
(0, 1), ("-fsigned-char", "-funsigned-char"), (0, 320, 480)):
|
||||
with self.subTest(feature=enabled, signedness=signedness, canvas=canvas), tempfile.TemporaryDirectory() as temp:
|
||||
preamble = PREAMBLE
|
||||
flags = []
|
||||
if canvas:
|
||||
preamble = preamble.replace("MomentaryButton user_btn(7,1000,true,true,true);", button)
|
||||
flags = indicator_flags + [f"-DTEST_INDICATOR_CANVAS={canvas}"]
|
||||
binary = Path(temp) / "navigation"
|
||||
result = subprocess.run([
|
||||
"c++", "-std=c++17", signedness,
|
||||
f"-DCOMPANION_FEATURE_JOHN={enabled}",
|
||||
*flags,
|
||||
"-I" + str(ROOT / "src"), "-I" + str(ROOT / "test/mocks"),
|
||||
"-fsanitize=address,undefined", "-fno-pie", "-no-pie",
|
||||
"-x", "c++", "-", str(ROOT / "src/helpers/ui/MomentaryButton.cpp"),
|
||||
"-o", str(binary),
|
||||
], input=preamble + implementation + SCENARIOS, text=True, capture_output=True)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
result = subprocess.run([str(binary)], text=True, capture_output=True)
|
||||
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -0,0 +1,302 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Exercise the real LGFX touch conversion before the rendered reader targets."""
|
||||
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from test_replay_reset_integration import extract_braced
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
PREAMBLE = r'''
|
||||
#include <cassert>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <helpers/ui/ReaderNavigationHint.h>
|
||||
#include <helpers/ui/DisplayTouchCoordinates.h>
|
||||
ColorVal UIColor::window_bkg=0, UIColor::title_bkg=0, UIColor::title_txt=1;
|
||||
ColorVal UIColor::primary_txt=1, UIColor::secondary_txt=1, UIColor::warning_txt=1;
|
||||
ColorVal UIColor::popup_bkg=0, UIColor::popup_txt=1, UIColor::corp_blue=2;
|
||||
namespace lgfx { namespace v1 { struct touch_point_t { int x,y; }; } }
|
||||
struct Panel {
|
||||
int w=480,h=480,x=0,y=0;
|
||||
bool touched=true;
|
||||
int width() const { return w; }
|
||||
int height() const { return h; }
|
||||
int getTouch(lgfx::v1::touch_point_t* point) {
|
||||
point->x=x; point->y=y; return touched ? 1 : 0;
|
||||
}
|
||||
};
|
||||
struct LGFXDisplay : DisplayDriver {
|
||||
Panel panel;
|
||||
Panel* display=&panel;
|
||||
// Retain these fields so this test also compiles the pre-fix implementation.
|
||||
int _coordinateScale=3;
|
||||
float _outputZoom=1.0f;
|
||||
struct Label { int x,y; std::string text; };
|
||||
std::vector<Label> labels;
|
||||
int cx=0,cy=0,glyph_width=6,divider=-1;
|
||||
LGFXDisplay() : DisplayDriver(160,160) {}
|
||||
bool isOn() override { return true; }
|
||||
void turnOn() override {}
|
||||
void turnOff() override {}
|
||||
void clear() override { labels.clear(); }
|
||||
void startFrame(ColorVal=0) override { clear(); }
|
||||
void endFrame() override {}
|
||||
void setTextSize(int) override {}
|
||||
void setColor(ColorVal) override {}
|
||||
void setCursor(int x,int y) override { cx=x; cy=y; }
|
||||
uint16_t getTextWidth(const char* text) override { return strlen(text)*glyph_width; }
|
||||
void print(const char* text) override { labels.push_back({cx,cy,text}); }
|
||||
void fillRect(int x,int y,int w,int h) override {
|
||||
assert(x>=0 && y>=0 && x+w<=width() && y+h<=height());
|
||||
}
|
||||
void drawRect(int x,int y,int w,int h) override {
|
||||
fillRect(x,y,w,h); if(h==1 && w==width()) divider=y;
|
||||
}
|
||||
void drawXbm(int,int,const uint8_t*,int,int) override {}
|
||||
bool getTouch(int*,int*) override;
|
||||
};
|
||||
'''
|
||||
|
||||
SCENARIOS = r'''
|
||||
using mesh::ui::TouchAction;
|
||||
TouchAction tap(LGFXDisplay& d,const mesh::ui::TouchNavigationBar& bar,
|
||||
int visual_x,int physical_y,bool mirror,int samples=2) {
|
||||
// The Indicator's controller X is mirrored; TouchInput applies the board
|
||||
// correction. Feed physical 0..479 samples, NOT pre-scaled 0..159 samples.
|
||||
d.panel.x=mirror ? d.panel.width()-1-visual_x : visual_x;
|
||||
d.panel.y=physical_y;
|
||||
mesh::ui::TouchInput input(true,true,70,mirror,false);
|
||||
int x=-1,y=-1;
|
||||
assert(d.getTouch(&x,&y));
|
||||
for(int i=0;i<samples;++i)
|
||||
assert(input.update(true,x,y,d.width(),d.height(),false,nullptr,&bar)==TouchAction::None);
|
||||
assert(input.update(false,-1,-1,d.width(),d.height(),false,nullptr,&bar)==TouchAction::None);
|
||||
return input.update(false,-1,-1,d.width(),d.height(),false,nullptr,&bar);
|
||||
}
|
||||
void test_rendered_targets() {
|
||||
const TouchAction expected[]={TouchAction::VerticalPrevious,TouchAction::Previous,
|
||||
TouchAction::Next,TouchAction::VerticalNext,TouchAction::Select};
|
||||
for(int canvas : {320,480}) for(int font : {6,8}) for(bool mirror : {false,true}) {
|
||||
LGFXDisplay d;
|
||||
d._coordinateScale=canvas/160; d._outputZoom=480.0f/canvas; d.glyph_width=font;
|
||||
const auto bar=mesh::ui::makeReaderTouchBar(d,d.height(),d.textLineHeight()+1);
|
||||
mesh::ui::drawReaderTouchBar(d,bar);
|
||||
assert(d.labels.size()==5 && d.divider==bar.top);
|
||||
assert(bar.top+bar.height==d.height());
|
||||
// Full rendered row, including every column boundary and final panel pixel.
|
||||
for(int py=d.divider*3;py<480;++py) for(int px=0;px<480;++px)
|
||||
assert(tap(d,bar,px,py,mirror,1)==expected[px/96]);
|
||||
// Actual drawn label centers must activate their own controls too.
|
||||
for(int i=0;i<5;++i) {
|
||||
const auto& label=d.labels[i];
|
||||
int px=(label.x+d.getTextWidth(label.text.c_str())/2)*3;
|
||||
int py=(label.y+d.textLineHeight()/2)*3;
|
||||
assert(tap(d,bar,px,py,mirror)==expected[i]);
|
||||
}
|
||||
// Header Exit uses exactly the rendered header height, not a broad zone.
|
||||
for(int px=0;px<480;++px) {
|
||||
assert(tap(d,bar,px,0,mirror,1)==TouchAction::Select);
|
||||
assert(tap(d,bar,px,bar.exit_height*3-1,mirror,1)==TouchAction::Select);
|
||||
assert(tap(d,bar,px,bar.exit_height*3,mirror)==
|
||||
(px<240 ? TouchAction::Previous : TouchAction::Next));
|
||||
}
|
||||
}
|
||||
}
|
||||
void test_missed_arrow_does_not_exit() {
|
||||
LGFXDisplay d;
|
||||
const auto bar=mesh::ui::makeReaderTouchBar(d,d.height());
|
||||
for(int canvas : {320,480}) {
|
||||
d._coordinateScale=canvas/160; d._outputZoom=480.0f/canvas;
|
||||
// Reported failure: a tap near 2< falls above the footer and becomes the
|
||||
// generic center/select action, which closes the message reader.
|
||||
assert(tap(d,bar,144,bar.top*3-1,true)==TouchAction::Previous);
|
||||
assert(tap(d,bar,240,bar.top*3-12,true)==TouchAction::Next);
|
||||
assert(tap(d,bar,144,bar.top*3,true)==TouchAction::Previous);
|
||||
assert(tap(d,bar,432,479,true)==TouchAction::Select);
|
||||
}
|
||||
}
|
||||
void test_panel_bounds() {
|
||||
LGFXDisplay d;
|
||||
int x=-1,y=-1;
|
||||
for(int bad : {-2,-1,480,481}) {
|
||||
d.panel.x=bad; d.panel.y=460; assert(!d.getTouch(&x,&y));
|
||||
d.panel.x=144; d.panel.y=bad; assert(!d.getTouch(&x,&y));
|
||||
}
|
||||
d.panel.x=479; d.panel.y=479; assert(d.getTouch(&x,&y)); assert(x==159 && y==159);
|
||||
d.panel.touched=false; assert(!d.getTouch(&x,&y));
|
||||
assert(x==-1 && y==-1);
|
||||
assert(!d.getTouch(nullptr,&y) && !d.getTouch(&x,nullptr));
|
||||
// The shared driver uses the live, rotated output dimensions, including
|
||||
// non-square outputs. No stale compile-time zoom can move edge targets.
|
||||
d.panel.touched=true; d.panel.w=240; d.panel.h=320;
|
||||
d.panel.x=239; d.panel.y=319; assert(d.getTouch(&x,&y));
|
||||
assert(x==159 && y==159);
|
||||
d.panel.x=120; d.panel.y=160; assert(d.getTouch(&x,&y));
|
||||
assert(x==80 && y==80);
|
||||
d.panel.w=0; assert(!d.getTouch(&x,&y));
|
||||
assert(x==-1 && y==-1);
|
||||
}
|
||||
void test_body_gestures_and_chrome_drags() {
|
||||
for(int canvas : {320,480}) {
|
||||
LGFXDisplay d;
|
||||
d._coordinateScale=canvas/160; d._outputZoom=480.0f/canvas;
|
||||
const auto bar=mesh::ui::makeReaderTouchBar(d,d.height(),12);
|
||||
auto drag=[&](int sx,int sy,int ex,int ey) {
|
||||
mesh::ui::TouchInput input(true,true,70,true,false);
|
||||
int x,y;
|
||||
d.panel.x=479-sx; d.panel.y=sy; assert(d.getTouch(&x,&y));
|
||||
input.update(true,x,y,160,160,false,nullptr,&bar);
|
||||
d.panel.x=479-ex; d.panel.y=ey; assert(d.getTouch(&x,&y));
|
||||
input.update(true,x,y,160,160,false,nullptr,&bar);
|
||||
input.update(false,-1,-1,160,160,false,nullptr,&bar);
|
||||
auto action=input.update(false,-1,-1,160,160,false,nullptr,&bar);
|
||||
assert(input.update(false,-1,-1,160,160,false,nullptr,&bar)==TouchAction::None);
|
||||
return action;
|
||||
};
|
||||
assert(drag(360,220,120,220)==TouchAction::Next); // swipe left
|
||||
assert(drag(120,220,360,220)==TouchAction::Previous);
|
||||
assert(drag(240,300,240,120)==TouchAction::VerticalNext); // up = >>
|
||||
assert(drag(240,120,240,300)==TouchAction::VerticalPrevious); // down = <<
|
||||
// Starting in the body and ending over X/header remains a swipe, not Exit.
|
||||
assert(drag(432,200,432,479)==TouchAction::VerticalPrevious);
|
||||
assert(drag(240,200,240,0)==TouchAction::VerticalNext);
|
||||
assert(drag(120,460,432,460)==TouchAction::None);
|
||||
assert(drag(120,12,360,12)==TouchAction::None);
|
||||
// Small movement out of a button's row/cell must not select a neighbor.
|
||||
assert(drag(144,bar.top*3,144,bar.top*3-3)==TouchAction::None);
|
||||
assert(drag(382,460,386,460)==TouchAction::None);
|
||||
assert(drag(144,35,144,39)==TouchAction::None);
|
||||
}
|
||||
}
|
||||
int main(int argc,char** argv) {
|
||||
assert(argc==2);
|
||||
if(std::string(argv[1])=="targets") test_rendered_targets();
|
||||
else if(std::string(argv[1])=="miss") test_missed_arrow_does_not_exit();
|
||||
else if(std::string(argv[1])=="gestures") test_body_gestures_and_chrome_drags();
|
||||
else test_panel_bounds();
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
class ReaderTouchCoordinatesTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.temp = tempfile.TemporaryDirectory()
|
||||
cls.addClassCleanup(cls.temp.cleanup)
|
||||
cls.executable = Path(cls.temp.name) / "reader_touch_coordinates"
|
||||
implementation = (ROOT / "src/helpers/ui/LGFXDisplay.cpp").read_text()
|
||||
actual_get_touch = extract_braced(implementation, "bool LGFXDisplay::getTouch(")
|
||||
result = subprocess.run(
|
||||
["c++", "-std=c++17", "-O1", "-fsanitize=address,undefined",
|
||||
"-fno-pie", "-no-pie", f"-I{ROOT / 'src'}", "-x", "c++", "-",
|
||||
"-o", str(cls.executable)],
|
||||
input=PREAMBLE + actual_get_touch + SCENARIOS,
|
||||
text=True, capture_output=True)
|
||||
if result.returncode:
|
||||
raise AssertionError(result.stderr)
|
||||
|
||||
def run_scenario(self, scenario):
|
||||
result = subprocess.run([str(self.executable), scenario], text=True, capture_output=True)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
|
||||
def test_actual_panel_coordinates_match_rendered_footer(self):
|
||||
self.run_scenario("targets")
|
||||
|
||||
def test_missed_arrow_never_becomes_exit(self):
|
||||
self.run_scenario("miss")
|
||||
|
||||
def test_invalid_panel_points_do_not_round_into_edge_targets(self):
|
||||
self.run_scenario("bounds")
|
||||
|
||||
def test_body_swipes_and_chrome_drags(self):
|
||||
self.run_scenario("gestures")
|
||||
|
||||
def test_indicator_keeps_vertical_direction_independent_of_mirrored_x(self):
|
||||
profile = (ROOT / "variants/sensecap_indicator-espnow/platformio.ini").read_text()
|
||||
task = (ROOT / "examples/companion_radio/ui-new/UITask.h").read_text()
|
||||
self.assertIn("-D TOUCH_REVERSE_VERTICAL_SWIPE=0", profile)
|
||||
self.assertIn("TOUCH_REVERSE_VERTICAL_SWIPE != 0", task)
|
||||
source = (ROOT / "examples/companion_radio/ui-new/UITask.cpp").read_text()
|
||||
self.assertIn("layout.header_divider_y + 1", source)
|
||||
self.assertIn("if (!_display->isOn()) reader_touch_bar = nullptr;", source)
|
||||
reader = (ROOT / "examples/companion_radio/ui-new/JohnReaderScreen.h").read_text()
|
||||
self.assertIn("makeReaderTouchBar(hint_text, bottom, top - 1)", reader)
|
||||
|
||||
|
||||
class IndicatorTouchOrientationTest(unittest.TestCase):
|
||||
def test_actual_board_driver_corrects_bottom_to_top_reports(self):
|
||||
generic = extract_braced(
|
||||
(ROOT / "src/helpers/ui/LGFXDisplay.cpp").read_text(),
|
||||
"bool LGFXDisplay::getTouch(")
|
||||
board = extract_braced(
|
||||
(ROOT / "variants/sensecap_indicator-espnow/SCIndicatorDisplay.h").read_text(),
|
||||
"bool getTouch(int* x, int* y) override")
|
||||
scenarios = r'''
|
||||
#include <helpers/ui/TouchDebugOverlay.h>
|
||||
using namespace mesh::ui;
|
||||
int main() {
|
||||
for(int canvas : {320,480}) {
|
||||
SCIndicatorDisplay d;
|
||||
d._coordinateScale=canvas/160; d._outputZoom=480.0f/canvas;
|
||||
const auto bar=makeReaderTouchBar(d,160,readerTouchHeaderHeight(12));
|
||||
const auto regions=makeTouchDebugAreas(160,160,70,false,nullptr,&bar);
|
||||
assert(bar.exit_height==24 && regions.count==8);
|
||||
// Actual Indicator reports BOTH axes opposite to the visible panel.
|
||||
// Exercise every physical pixel through the real board + generic drivers,
|
||||
// the gesture handler and the region used for green press feedback.
|
||||
for(int py=0;py<480;++py) for(int px=0;px<480;++px) {
|
||||
d.panel.x=479-px; d.panel.y=479-py;
|
||||
int x=-1,y=-1;
|
||||
assert(d.getTouch(&x,&y));
|
||||
assert(159-x==px/3 && y==py/3);
|
||||
const int region=touchDebugAreaAt(regions,159-x,y);
|
||||
const int expected=py<72 ? 0 : py<bar.top*3 ? (px<240 ? 1 : 2) : 3+px/96;
|
||||
assert(region==expected);
|
||||
TouchInput input(true,true,70,true,false);
|
||||
input.update(true,x,y,160,160,false,nullptr,&bar);
|
||||
input.update(true,x,y,160,160,false,nullptr,&bar);
|
||||
input.update(false,-1,-1,160,160,false,nullptr,&bar);
|
||||
assert(input.update(false,-1,-1,160,160,false,nullptr,&bar)==regions.areas[expected].action);
|
||||
}
|
||||
auto drag=[&](int sx,int sy,int ex,int ey) {
|
||||
TouchInput input(true,true,70,true,false);
|
||||
int x,y;
|
||||
d.panel.x=479-sx; d.panel.y=479-sy; assert(d.getTouch(&x,&y));
|
||||
input.update(true,x,y,160,160,false,nullptr,&bar);
|
||||
d.panel.x=479-ex; d.panel.y=479-ey; assert(d.getTouch(&x,&y));
|
||||
input.update(true,x,y,160,160,false,nullptr,&bar);
|
||||
input.update(false,-1,-1,160,160,false,nullptr,&bar);
|
||||
return input.update(false,-1,-1,160,160,false,nullptr,&bar);
|
||||
};
|
||||
assert(drag(240,300,240,120)==TouchAction::VerticalNext);
|
||||
assert(drag(240,120,240,300)==TouchAction::VerticalPrevious);
|
||||
assert(drag(360,220,120,220)==TouchAction::Next);
|
||||
assert(drag(120,220,360,220)==TouchAction::Previous);
|
||||
for(int bad : {-1,480}) {
|
||||
int x=0,y=0;
|
||||
d.panel.y=bad; assert(!d.getTouch(&x,&y));
|
||||
assert(x==-1 && y==-1);
|
||||
}
|
||||
int x,y;
|
||||
assert(!d.getTouch(nullptr,&y) && !d.getTouch(&x,nullptr));
|
||||
}
|
||||
}
|
||||
'''
|
||||
source = PREAMBLE + generic + "struct SCIndicatorDisplay : LGFXDisplay {\n" + board + "\n};\n" + scenarios
|
||||
with tempfile.TemporaryDirectory() as temp:
|
||||
binary = Path(temp) / "indicator_touch_orientation"
|
||||
compiled = subprocess.run(["c++", "-std=c++17", "-O1",
|
||||
"-fsanitize=address,undefined", "-fno-pie", "-no-pie",
|
||||
f"-I{ROOT / 'src'}", "-x", "c++", "-", "-o", str(binary)],
|
||||
input=source, text=True, capture_output=True)
|
||||
self.assertEqual(compiled.returncode, 0, compiled.stderr)
|
||||
tested = subprocess.run([str(binary)], text=True, capture_output=True)
|
||||
self.assertEqual(tested.returncode, 0, tested.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,169 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compile actual UI hooks and CLI branches for touch, legacy and absent UIs."""
|
||||
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from test_replay_reset_integration import extract_braced
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
UI = ROOT / "examples/companion_radio/ui-new"
|
||||
MESH = ROOT / "examples/companion_radio/MyMesh.cpp"
|
||||
|
||||
|
||||
class TouchDebugCliTest(unittest.TestCase):
|
||||
def test_runtime_default_toggle_redraw_and_unsupported_targets(self):
|
||||
abstract = (ROOT / "examples/companion_radio/AbstractUITask.h").read_text()
|
||||
header = (UI / "UITask.h").read_text()
|
||||
hooks = ("supportsTouchDebug() const", "isTouchDebugEnabled() const",
|
||||
"setTouchDebugEnabled(bool enabled)")
|
||||
base = "\n".join(extract_braced(abstract, "virtual bool " + hook) for hook in hooks)
|
||||
overrides = "\n".join(extract_braced(header, "bool " + hook) for hook in hooks)
|
||||
fields = re.search(r" bool _touch_debug_enabled[^\n]*\n"
|
||||
r" int _touch_debug_x[^\n]*\n"
|
||||
r" UIScreen\* _touch_debug_screen[^\n]*", header).group()
|
||||
mesh = MESH.read_text()
|
||||
getter = extract_braced(mesh, 'if (strcmp(command, "get display.touch") == 0)')
|
||||
setter = extract_braced(mesh, 'if (strncmp(command, "set display.touch", 17) == 0')
|
||||
source = r'''
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <initializer_list>
|
||||
struct UIScreen {};
|
||||
struct Display {};
|
||||
struct AbstractUITask {
|
||||
BASE_HOOKS
|
||||
};
|
||||
struct UITask : AbstractUITask {
|
||||
Display* _display=nullptr;
|
||||
unsigned long _next_refresh=999;
|
||||
int wakes=0;
|
||||
char checkDisplayOn(char c) { assert(c==0); ++wakes; return 0; }
|
||||
FIELDS
|
||||
#ifdef HAS_TOUCH
|
||||
OVERRIDES
|
||||
#endif
|
||||
};
|
||||
struct MyMesh {
|
||||
AbstractUITask* _ui=nullptr;
|
||||
bool command(const char* command, char* reply, size_t reply_size) {
|
||||
if(!command || !reply || !reply_size) return false;
|
||||
GETTER
|
||||
SETTER
|
||||
return false;
|
||||
}
|
||||
};
|
||||
int main() {
|
||||
Display display;
|
||||
UITask ui;
|
||||
MyMesh mesh;
|
||||
mesh._ui=&ui;
|
||||
auto command=[&](const char* text) {
|
||||
char reply[160]={};
|
||||
assert(mesh.command(text,reply,sizeof(reply)));
|
||||
return std::string(reply);
|
||||
};
|
||||
assert(!ui.isTouchDebugEnabled() && !ui.supportsTouchDebug());
|
||||
assert(command("get display.touch").find("unsupported")!=std::string::npos);
|
||||
assert(command("set display.touch on").find("unsupported")!=std::string::npos);
|
||||
ui._display=&display;
|
||||
#ifdef HAS_TOUCH
|
||||
assert(ui.supportsTouchDebug());
|
||||
assert(command("get display.touch")=="display.touch off (off after reboot)");
|
||||
assert(command("set display.touch on")=="OK - display.touch on (off after reboot)");
|
||||
assert(ui.isTouchDebugEnabled() && ui.wakes==1 && ui._next_refresh==0);
|
||||
assert(command("get display.touch")=="display.touch on (off after reboot)");
|
||||
// Invalid inputs must not mutate state or accidentally accept a suffix.
|
||||
for(const char* input : {"set display.touch", "set display.touch ",
|
||||
"set display.touch toggle", "set display.touch only", "set display.touch OFF",
|
||||
"set display.touch 1", "set display.touch off extra", "set display.touch on reboot"}) {
|
||||
assert(command(input)=="Error: use set display.touch on|off");
|
||||
assert(ui.isTouchDebugEnabled() && ui.wakes==1);
|
||||
}
|
||||
UIScreen screen;
|
||||
ui._touch_debug_x=48; ui._touch_debug_y=145;
|
||||
ui._touch_debug_area=4; ui._touch_debug_screen=&screen;
|
||||
ui._next_refresh=999;
|
||||
assert(command("set display.touch\t off")=="OK - display.touch off (off after reboot)");
|
||||
assert(!ui.isTouchDebugEnabled() && ui.wakes==1 && ui._next_refresh==0);
|
||||
assert(ui._touch_debug_x==-1 && ui._touch_debug_y==-1 && ui._touch_debug_area==-1);
|
||||
assert(ui._touch_debug_screen==nullptr);
|
||||
assert(command("set display.touch on").find("OK")==0);
|
||||
UITask rebooted;
|
||||
rebooted._display=&display;
|
||||
assert(!rebooted.isTouchDebugEnabled());
|
||||
#else
|
||||
assert(!ui.supportsTouchDebug());
|
||||
assert(command("set display.touch on").find("unsupported")!=std::string::npos);
|
||||
assert(!ui.isTouchDebugEnabled() && ui.wakes==0);
|
||||
#endif
|
||||
char reply[160]={};
|
||||
for(const char* input : {"get display.touching", "set display.touching on", "get role"})
|
||||
assert(!mesh.command(input,reply,sizeof(reply)));
|
||||
assert(!mesh.command(nullptr,reply,sizeof(reply)));
|
||||
assert(!mesh.command("get display.touch",nullptr,sizeof(reply)));
|
||||
assert(!mesh.command("get display.touch",reply,0));
|
||||
// No missing/legacy UI may crash or claim it enabled an invisible feature.
|
||||
AbstractUITask legacy;
|
||||
for(AbstractUITask* target : {&legacy,static_cast<AbstractUITask*>(nullptr)}) {
|
||||
mesh._ui=target;
|
||||
assert(command("get display.touch").find("unsupported")!=std::string::npos);
|
||||
assert(command("set display.touch off").find("unsupported")!=std::string::npos);
|
||||
}
|
||||
// Short binary reply capacities remain bounded, including a one-byte reply.
|
||||
mesh._ui=&ui;
|
||||
for(size_t capacity : {size_t(1),size_t(8),size_t(20)}) {
|
||||
char tiny[21]; memset(tiny,'!',sizeof(tiny));
|
||||
assert(mesh.command("get display.touch",tiny,capacity));
|
||||
assert(tiny[capacity-1]==0 && tiny[capacity]=='!');
|
||||
}
|
||||
}
|
||||
'''.replace("BASE_HOOKS", base).replace("OVERRIDES", overrides).replace(
|
||||
"FIELDS", fields).replace("GETTER", getter).replace("SETTER", setter)
|
||||
for touch in (False, True):
|
||||
with self.subTest(touch=touch), tempfile.TemporaryDirectory() as temp:
|
||||
binary = Path(temp) / "touch_debug_cli"
|
||||
compiled = subprocess.run(["c++", "-std=c++11", "-O1",
|
||||
"-fsanitize=address,undefined", "-fno-pie", "-no-pie",
|
||||
*(["-DHAS_TOUCH=1"] if touch else []),
|
||||
"-x", "c++", "-", "-o", str(binary)],
|
||||
input=source, text=True, capture_output=True)
|
||||
self.assertEqual(compiled.returncode, 0, compiled.stderr)
|
||||
tested = subprocess.run([str(binary)], text=True, capture_output=True)
|
||||
self.assertEqual(tested.returncode, 0, tested.stderr)
|
||||
|
||||
def test_shared_runtime_feature_is_wired_to_cli_and_rendering(self):
|
||||
source = (UI / "UITask.cpp").read_text()
|
||||
header = (UI / "UITask.h").read_text()
|
||||
mesh = MESH.read_text()
|
||||
self.assertNotIn("UI_TOUCH_DEBUG_OUTLINES", source + header)
|
||||
self.assertIn('#ifdef HAS_TOUCH\n #include <helpers/ui/TouchDebugOverlay.h>', source)
|
||||
self.assertIn('#ifdef HAS_TOUCH\n bool supportsTouchDebug()', header)
|
||||
self.assertIn("bool _touch_debug_enabled = false;", header)
|
||||
loop = extract_braced(source, "void UITask::loop()")
|
||||
feedback = extract_braced(loop, "if (_touch_debug_enabled)")
|
||||
self.assertIn("touchDebugAreaAt", feedback)
|
||||
draw = extract_braced(loop, "if (_touch_debug_enabled && !isPairingScreenActive())")
|
||||
self.assertIn("drawTouchDebugAreas", draw)
|
||||
local = extract_braced(mesh, "bool MyMesh::handleLocalControlCommand(")
|
||||
self.assertIn('"get display.touch"', local)
|
||||
handler = extract_braced(mesh, "bool MyMesh::handleCommand(const char* command,")
|
||||
self.assertIn("handleLocalControlCommand(command, reply, reply_capacity)", handler)
|
||||
terminal = extract_braced(mesh, "void MyMesh::handleTerminalCommand(")
|
||||
self.assertIn("handleLocalControlCommand(command, local_reply, sizeof(local_reply))", terminal)
|
||||
self.assertIn('get display.touch\\r\\n', terminal)
|
||||
# No persistent preference schema or write is involved.
|
||||
setter = extract_braced(local, 'if (strncmp(command, "set display.touch", 17) == 0')
|
||||
self.assertNotIn("savePrefs", setter)
|
||||
self.assertNotIn("_prefs", setter)
|
||||
for profile in (ROOT / "variants").rglob("platformio.ini"):
|
||||
self.assertNotIn("UI_TOUCH_DEBUG_OUTLINES", profile.read_text())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,167 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Yellow outlines must describe real touch actions, not a second guessed UI."""
|
||||
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from test_reader_touch_coordinates import PREAMBLE
|
||||
from test_replay_reset_integration import extract_braced
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
TEST = r'''
|
||||
#include <helpers/ui/CompanionTransportSelectorLayout.h>
|
||||
using namespace mesh::ui;
|
||||
struct DebugDisplay : LGFXDisplay {
|
||||
ColorVal color=0;
|
||||
std::vector<ColorVal> pixels=std::vector<ColorVal>(160*160,0);
|
||||
void setColor(ColorVal c) override { color=c; }
|
||||
void fillRect(int x,int y,int w,int h) override {
|
||||
LGFXDisplay::fillRect(x,y,w,h);
|
||||
for(int py=y;py<y+h;++py) for(int px=x;px<x+w;++px)
|
||||
pixels[py*160+px]=color;
|
||||
}
|
||||
};
|
||||
void verify_actions(int width,int center,bool bottom,
|
||||
const TouchSplitSelector* split,const TouchNavigationBar* reader) {
|
||||
const auto regions=makeTouchDebugAreas(width,width,center,bottom,split,reader);
|
||||
for(bool mirrored : {false,true}) for(int y=0;y<width;++y) for(int x=0;x<width;++x) {
|
||||
TouchAction expected=TouchAction::None;
|
||||
int matches=0;
|
||||
for(int i=0;i<regions.count;++i) {
|
||||
const auto& r=regions.areas[i];
|
||||
if(x>=r.x && x<r.x+r.width && y>=r.y && y<r.y+r.height) {
|
||||
expected=r.action; ++matches;
|
||||
}
|
||||
}
|
||||
assert(matches<=1);
|
||||
TouchInput input(true,true,center,mirrored,false);
|
||||
int tx=mirrored ? width-1-x : x;
|
||||
input.update(true,tx,y,width,width,bottom,split,reader);
|
||||
input.update(true,tx,y,width,width,bottom,split,reader);
|
||||
input.update(false,-1,-1,width,width,bottom,split,reader);
|
||||
assert(input.update(false,-1,-1,width,width,bottom,split,reader)==expected);
|
||||
}
|
||||
}
|
||||
int main() {
|
||||
// Home: three zones. Transport: choices plus page edges, no inert gaps.
|
||||
// Reader: header, two body halves, and all five footer cells.
|
||||
for(int width : {137,160,320,480}) {
|
||||
for(int center : {34,70,100}) {
|
||||
verify_actions(width,center,false,nullptr,nullptr);
|
||||
verify_actions(width,center,true,nullptr,nullptr);
|
||||
}
|
||||
TouchSplitSelector split={2,width/2-4,width/2+2,width/2-4,40,width-50};
|
||||
verify_actions(width,70,false,&split,nullptr);
|
||||
verify_actions(width,70,true,&split,nullptr);
|
||||
for(int center : {34,70,100}) {
|
||||
const auto layout=makeCompanionTransportSelectorLayout(width,width,center);
|
||||
TouchSplitSelector centered={layout.wifi.x,layout.wifi.width,
|
||||
layout.bluetooth.x,layout.bluetooth.width,layout.wifi.y,
|
||||
layout.wifi.height,layout.side_nav_width};
|
||||
verify_actions(width,center,false,¢ered,nullptr);
|
||||
verify_actions(width,center,true,¢ered,nullptr);
|
||||
const auto areas=makeTouchDebugAreas(width,width,center,false,¢ered,nullptr);
|
||||
assert(areas.count==(center==100 ? 2 : 4));
|
||||
}
|
||||
TouchNavigationBar bar;
|
||||
bar.top=width-24; bar.height=24; bar.exit_height=12;
|
||||
verify_actions(width,70,false,nullptr,&bar);
|
||||
bar.top-=10; // optional space below the footer remains truthfully marked
|
||||
verify_actions(width,70,true,nullptr,&bar);
|
||||
}
|
||||
for(int canvas : {320,480}) {
|
||||
DebugDisplay d;
|
||||
d._coordinateScale=canvas/160; d._outputZoom=480.0f/canvas;
|
||||
auto bar=makeReaderTouchBar(d,160,10);
|
||||
auto regions=makeTouchDebugAreas(160,160,70,false,nullptr,&bar);
|
||||
assert(regions.count==8);
|
||||
drawTouchDebugAreas(d,regions);
|
||||
int dots=0,empty_edges=0;
|
||||
for(int y=0;y<160;++y) for(int x=0;x<160;++x) {
|
||||
bool perimeter=false;
|
||||
for(int i=0;i<regions.count;++i) {
|
||||
const auto& r=regions.areas[i];
|
||||
if(x>=r.x && x<r.x+r.width && y>=r.y && y<r.y+r.height)
|
||||
perimeter |= x==r.x || x==r.x+r.width-1 || y==r.y || y==r.y+r.height-1;
|
||||
}
|
||||
if(d.pixels[y*160+x]) {
|
||||
assert(perimeter && d.pixels[y*160+x]==color_theme::TOUCH_OUTLINE); ++dots;
|
||||
} else if(perimeter) ++empty_edges;
|
||||
}
|
||||
assert(dots>100 && empty_edges>dots); // dotted, not solid or filled
|
||||
for(int i=0;i<regions.count;++i) {
|
||||
const auto& r=regions.areas[i];
|
||||
assert(d.pixels[r.y*160+r.x]==color_theme::TOUCH_OUTLINE);
|
||||
assert(d.pixels[(r.y+r.height-1)*160+r.x+r.width-1]==color_theme::TOUCH_OUTLINE);
|
||||
}
|
||||
for(int active=0;active<regions.count;++active) {
|
||||
const auto& target=regions.areas[active];
|
||||
const int x=target.x+target.width/2, y=target.y+target.height/2;
|
||||
assert(touchDebugAreaAt(regions,x,y)==active);
|
||||
drawTouchDebugAreas(d,regions,x,y);
|
||||
for(int i=0;i<regions.count;++i) {
|
||||
const auto& r=regions.areas[i];
|
||||
assert(d.pixels[r.y*160+r.x]==(i==active
|
||||
? color_theme::TOUCH_PRESSED : color_theme::TOUCH_OUTLINE));
|
||||
}
|
||||
drawTouchDebugAreas(d,regions); // release restores yellow immediately
|
||||
assert(d.pixels[target.y*160+target.x]==color_theme::TOUCH_OUTLINE);
|
||||
}
|
||||
assert(touchDebugAreaAt(regions,-1,-1)==-1);
|
||||
assert(touchDebugAreaAt(regions,160,159)==-1);
|
||||
}
|
||||
// RGB565 yellow used to fall through to palette index 0 (black). Exercise
|
||||
// the actual production indexed-colour conversion and palette entry.
|
||||
assert(actualRenderColor(color_theme::TOUCH_OUTLINE)==color_theme::INDEX_TOUCH_OUTLINE);
|
||||
assert(UI_PALETTE[color_theme::INDEX_TOUCH_OUTLINE]==0xFFFF00);
|
||||
assert(actualRenderColor(color_theme::TOUCH_PRESSED)==color_theme::INDEX_TOUCH_PRESSED);
|
||||
assert(UI_PALETTE[color_theme::INDEX_TOUCH_PRESSED]==0x00FF00);
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
class TouchDebugOverlayTest(unittest.TestCase):
|
||||
def test_outlines_match_every_tap_pixel_and_stay_yellow(self):
|
||||
display = (ROOT / "src/helpers/ui/LGFXDisplay.cpp").read_text()
|
||||
actual_touch = extract_braced(display, "bool LGFXDisplay::getTouch(")
|
||||
actual_color = extract_braced(display, "uint32_t LGFXDisplay::renderColor(").replace(
|
||||
"uint32_t LGFXDisplay::renderColor(ColorVal color) const",
|
||||
"uint32_t actualRenderColor(ColorVal color)")
|
||||
palette = extract_braced(display, "static const uint32_t UI_PALETTE[16]") + ";"
|
||||
source = '#define UI_BUFFER_COLOR_DEPTH 4\n#include <helpers/ui/TouchDebugOverlay.h>\n'
|
||||
source += PREAMBLE + actual_touch + actual_color + palette + TEST
|
||||
with tempfile.TemporaryDirectory() as temp:
|
||||
binary = Path(temp) / "touch_debug_overlay"
|
||||
compiled = subprocess.run(["c++", "-std=c++17", "-O1",
|
||||
"-fsanitize=address,undefined", "-fno-pie", "-no-pie",
|
||||
f"-I{ROOT / 'src'}", "-x", "c++", "-", "-o", str(binary)],
|
||||
input=source, text=True, capture_output=True)
|
||||
self.assertEqual(compiled.returncode, 0, compiled.stderr)
|
||||
tested = subprocess.run([str(binary)], text=True, capture_output=True)
|
||||
self.assertEqual(tested.returncode, 0, tested.stderr)
|
||||
|
||||
def test_overlay_uses_the_same_current_controls_after_content_render(self):
|
||||
source = (ROOT / "examples/companion_radio/ui-new/UITask.cpp").read_text()
|
||||
loop = extract_braced(source, "void UITask::loop()")
|
||||
self.assertIn("getTouchControls(transport_touch_selector, split_transport_selector, reader_touch_bar)", loop)
|
||||
self.assertIn("getTouchControls(transport, split, reader)", loop)
|
||||
render = loop.index("int delay_millis = curr->render(*_display);")
|
||||
outline = loop.index("mesh::ui::drawTouchDebugAreas", render)
|
||||
present = loop.index("_display->endFrame();", outline)
|
||||
self.assertLess(render, outline)
|
||||
self.assertLess(outline, present)
|
||||
self.assertIn("if (_touch_debug_enabled && !isPairingScreenActive())", loop[render:outline])
|
||||
feedback = extract_braced(loop, "if (_touch_debug_enabled)")
|
||||
self.assertIn("mesh::ui::touchDebugAreaAt", feedback)
|
||||
profile = (ROOT / "variants/sensecap_indicator-espnow/platformio.ini").read_text()
|
||||
self.assertNotIn("UI_TOUCH_DEBUG_OUTLINES", profile)
|
||||
self.assertIn("TOUCH_MIRROR_TAP_X_ENABLED ? _display->width() - 1 - touch_x : touch_x", loop)
|
||||
self.assertIn("if (debug_area != _touch_debug_area || curr != _touch_debug_screen) _next_refresh = 0;", loop)
|
||||
self.assertIn("_touch_debug_screen == curr ? _touch_debug_x : -1", loop)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -15,6 +15,76 @@ TouchAction release(TouchInput& input, int width = 137, int height = 137) {
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST(TouchInput, ReaderFooterHasFiveFullWidthTargetsIncludingExit) {
|
||||
using mesh::ui::TouchNavigationBar;
|
||||
// Same logical positions on the Indicator's 320 and 480 rendering profiles.
|
||||
for (int width : {160, 320, 480}) {
|
||||
for (bool mirrored : {false, true}) {
|
||||
TouchInput input(true, true, 70, mirrored);
|
||||
TouchNavigationBar bar;
|
||||
bar.top = width - 24;
|
||||
bar.height = 24;
|
||||
bar.exit_height = 12;
|
||||
const TouchAction expected[] = {TouchAction::VerticalPrevious,
|
||||
TouchAction::Previous, TouchAction::Next,
|
||||
TouchAction::VerticalNext, TouchAction::Select};
|
||||
// Every point of each cell is usable, not only the arrow glyph.
|
||||
for (int visual_x = 0; visual_x < width; ++visual_x) {
|
||||
const int x = mirrored ? width - 1 - visual_x : visual_x;
|
||||
input.update(true, x, bar.top, width, width, true, nullptr, &bar);
|
||||
EXPECT_EQ(input.update(false, -1, -1, width, width, true, nullptr, &bar), TouchAction::None);
|
||||
EXPECT_EQ(input.update(false, -1, -1, width, width, true, nullptr, &bar),
|
||||
expected[visual_x / (width / 5)]);
|
||||
EXPECT_EQ(input.update(false, -1, -1, width, width, true, nullptr, &bar), TouchAction::None);
|
||||
}
|
||||
// The top header is an explicit exit target across its entire width.
|
||||
for (int visual_x : {0, width / 2, width - 1}) {
|
||||
const int x = mirrored ? width - 1 - visual_x : visual_x;
|
||||
input.update(true, x, 0, width, width, true, nullptr, &bar);
|
||||
input.update(false, -1, -1, width, width, true, nullptr, &bar);
|
||||
EXPECT_EQ(input.update(false, -1, -1, width, width, true, nullptr, &bar),
|
||||
TouchAction::Select);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(TouchInput, ReaderFooterDragDoesNotActivateExitOrEmitAnEndpointTap) {
|
||||
mesh::ui::TouchNavigationBar bar;
|
||||
bar.top = 130; bar.height = 16;
|
||||
TouchInput input(true, true, 70, true);
|
||||
input.update(true, 10, 135, 160, 160, true, nullptr, &bar);
|
||||
input.update(true, 90, 135, 160, 160, true, nullptr, &bar);
|
||||
EXPECT_EQ(input.update(false, -1, -1, 160, 160, true, nullptr, &bar), TouchAction::None);
|
||||
// A transient lost sample cannot end the swipe.
|
||||
input.update(true, 120, 135, 160, 160, true, nullptr, &bar);
|
||||
input.update(false, -1, -1, 160, 160, true, nullptr, &bar);
|
||||
EXPECT_EQ(input.update(false, -1, -1, 160, 160, true, nullptr, &bar), TouchAction::None);
|
||||
EXPECT_EQ(input.update(false, -1, -1, 160, 160, true, nullptr, &bar), TouchAction::None);
|
||||
// A one-sample contact away from the new targets is still ignored.
|
||||
input.update(true, 80, 50, 160, 160, false, nullptr, &bar);
|
||||
input.update(false, -1, -1, 160, 160, false, nullptr, &bar);
|
||||
EXPECT_EQ(input.update(false, -1, -1, 160, 160, false, nullptr, &bar), TouchAction::None);
|
||||
}
|
||||
|
||||
TEST(TouchInput, RemovedChannelBarDoesNotLeaveInvisibleSelectorAboveNavigation) {
|
||||
mesh::ui::TouchNavigationBar bar;
|
||||
bar.top = 144; bar.height = 16;
|
||||
TouchInput input(true, true, 70, true);
|
||||
auto tap = [&](int visual_x, int y) {
|
||||
const int raw_x = 159 - visual_x;
|
||||
input.update(true, raw_x, y, 160, 160, false, nullptr, &bar);
|
||||
input.update(true, raw_x, y, 160, 160, false, nullptr, &bar);
|
||||
input.update(false, -1, -1, 160, 160, false, nullptr, &bar);
|
||||
return input.update(false, -1, -1, 160, 160, false, nullptr, &bar);
|
||||
};
|
||||
EXPECT_EQ(tap(0, 135), TouchAction::Previous);
|
||||
EXPECT_EQ(tap(159, 135), TouchAction::Next);
|
||||
EXPECT_EQ(tap(80, 135), TouchAction::Next);
|
||||
EXPECT_EQ(tap(0, 144), TouchAction::VerticalPrevious);
|
||||
EXPECT_EQ(tap(159, 159), TouchAction::Select); // X now owns the bottom-right.
|
||||
}
|
||||
|
||||
TEST(TouchInput, EmitsOnlyAfterRelease) {
|
||||
TouchInput input;
|
||||
EXPECT_EQ(input.update(true, 70, 50, 137, 137), TouchAction::None);
|
||||
|
||||
Reference in new issue
Block a user