#!/usr/bin/env python3 """TEXT lane — EasyNotes (Kotlin/Compose note app). This lane owns CONTENT / OUTPUT text and is the SOLE owner of exact-string equality: strings an interaction PRODUCES (empty/no-match placeholders, the card that reflects a typed title/body, status/label strings on surfaces you open) and opaque rendered cores (markdown that renders to a surface the view tree cannot give as a formatted string). Every test drives the app through the user path, reads the produced string out of a FRESH ui(), and asserts it. Discriminating tests assert ABSENT-before / PRESENT-after where the string is produced by a trigger. Package comes ONLY from argv[1]; no literal package/coordinate/serial appears here. """ import os import re import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from android_testgen_kit import * # noqa: F403,E402 # ── local helpers ──────────────────────────────────────────────────────────── def all_text(nodes): """All visible `text` values joined — for substring/normalised comparison.""" return " | ".join(n.get("text", "") for n in nodes if n.get("text")) def all_desc(nodes): """All content-desc values joined — for content-desc string assertions.""" return " | ".join(n.get("content-desc", "") for n in nodes if n.get("content-desc")) def unescape_entities(s): """The kit's XML parser does NOT decode HTML entities, so a label whose visible text is 'Colors & Styles' arrives as the literal 'Colors & Styles'. Normalise both sides before an exact-string comparison so the entity never causes a false failure.""" return (s.replace("&", "&").replace("<", "<").replace(">", ">") .replace("'", "'").replace(""", '"')) def norm(s): """Entity-decode + strip + casefold — a faithful recreation may differ in case but not in the words.""" return unescape_entities(s).strip().casefold() def contains_text(nodes, needle): """True iff any node's (entity-decoded) text contains `needle`, case-insensitive.""" n = norm(needle) return any(n in norm(x) for x in (nd.get("text", "") for nd in nodes) if x) def contains_desc(nodes, needle): n = norm(needle) return any(n in norm(x) for x in (nd.get("content-desc", "") for nd in nodes) if x) def edittexts_by_y(nodes): """The Edit page has two unlabelled EditTexts (Name on top, Description below); document order is not reliable, so order by y.""" return sorted(find_by_class(nodes, "EditText"), key=lambda n: n.get("_bounds", (0, 0, 0, 0))[1]) def open_home(): """Fresh Home with app data intact.""" restart_app() wait_for_foreground() settle("launch") def open_empty_home(): """Fresh Home with NO notes (empty DB).""" clear_data() launch_app() wait_for_foreground() settle("launch") def create_note(title, body): """Create a note through the GUI (New Note FAB → Name → Description → Done). Returns True if it left the Edit surface back to Home.""" fab = find_node(ui(), content_desc="New Note") if not tap_node(fab): return False settle("tap") fields = edittexts_by_y(ui()) if not fields: return False if title: type_text(fields[0], title) settle("text") if body: fields = edittexts_by_y(ui()) if len(fields) > 1: type_text(fields[1], body) settle("text") done = find_node(ui(), content_desc="Done") if not tap_node(done): return False settle("tap") return True # ── depth-family helpers (name-backed so a declared depth>=1 is honest) ──────── def and_enter_settings(): """Home → Settings gear. Counts one surface.""" n = find_node(ui(), content_desc="Settings") ok = and_reach_surface(n, "Support", "New Note") if n else False if not ok and n: # fall back to a generic surface change before = and_showing() tap_node(n) if surface_changed(before, and_showing()): and_enter("Settings") ok = True return ok def and_enter_about(): """(assumes on Settings) Settings → About. Counts one surface.""" n = find_node_ci(ui(), "About") or scroll_into_view("About") if not n: return False before = and_showing() tap_node(n) if surface_changed(before, and_showing()): and_enter("About") return True return False def and_enter_preview(title): """Home → open a note card (opens on the Preview page). Counts one surface.""" n = find_node(ui(), text=title) if not n: return False before = and_showing() tap(n["_center"][0], n["_center"][1]) settle("tap") if surface_changed(before, and_showing()): and_enter("Preview") return True return False def and_enter_more_menu(): """(assumes on Preview) tap Info (⋮) → More dropdown. Counts one surface.""" n = find_node(ui(), content_desc="Info") if not n: return False before = and_showing() tap_node(n) settle("tap") if surface_changed(before, and_showing()): and_enter("More") return True return False def and_reach_note_info(): """(assumes More dropdown open) tap Information → Note Info bottom sheet.""" n = find_node_ci(ui(), "Information") if not n: return False return and_reach_surface(n, "Created Time", "Copy") # ══════════════════════════════════════════════════════════════════════════════ # Placeholder strings produced by empty / no-match states # ══════════════════════════════════════════════════════════════════════════════ @and_test(expects=("text_empty_home_placeholder",)) def test_empty_home_placeholder(): open_empty_home() nodes = ui() got = contains_text(nodes, "No created notes.") record("text_empty_home_placeholder", got, "" if got else "behavior_wrong: empty Home did not show the " "placeholder 'No created notes.' — got text=%r" % all_text(nodes), depth=0, jump_kind="value", expect="No created notes.") @and_test(expects=("text_search_no_match",)) def test_search_no_match(): open_empty_home() if not create_note("Groceries", "Milk"): record("text_search_no_match", False, "setup_failed: could not create a note to search over", depth=0, jump_kind="value", expect="No notes found.") return # Trigger: type a query that matches nothing. before = contains_text(ui(), "No notes found.") stext = find_node(ui(), text="Search") or find_node(ui(), content_desc="Search") if not tap_node(stext): record("text_search_no_match", False, "setup_failed: could not focus the search bar", depth=0, jump_kind="value", expect="No notes found.") return settle("tap") field = find_by_class(ui(), "EditText") if not field: record("text_search_no_match", False, "setup_failed: search bar did not become an editable field", depth=0, jump_kind="value", expect="No notes found.") return type_text(field[0], "zzqqxx", verify_focus=False) settle("text") after = contains_text(ui(), "No notes found.") ok = (not before) and after record("text_search_no_match", ok, "" if ok else "behavior_wrong: gibberish query did not produce the " "'No notes found.' placeholder (before=%s after=%s)" % (before, after), depth=0, jump_kind="value", expect="No notes found.") @and_test(expects=("text_empty_note_discard_placeholder",)) def test_empty_note_discard_placeholder(): # An empty note (no title, no body) is discarded on leave -> the produced # string after this no-op create is still the empty placeholder. The trigger # dips into Edit and back, but the OBSERVATION is read on Home, so depth=0. open_empty_home() if not create_note("", ""): record("text_empty_note_discard_placeholder", False, "setup_failed: could not open/leave a new empty note", depth=0, jump_kind="value", expect="No created notes.") return nodes = ui() ok = contains_text(nodes, "No created notes.") record("text_empty_note_discard_placeholder", ok, "" if ok else "behavior_wrong: after discarding an empty note the Home " "placeholder 'No created notes.' was absent — got %r" % all_text(nodes), depth=0, jump_kind="value", expect="No created notes.") # ══════════════════════════════════════════════════════════════════════════════ # The card content an ACTION produced — exact title / body strings # ══════════════════════════════════════════════════════════════════════════════ @and_test(expects=("text_card_title",)) def test_card_title_reflects_typed_title(): open_empty_home() absent = contains_text(ui(), "GroceryList") if not create_note("GroceryList", "Milk and eggs"): record("text_card_title", False, "setup_failed: could not create the note", depth=0, jump_kind="value", expect="GroceryList") return nodes = ui() # exact title string on the produced card title_node = find_node(nodes, text="GroceryList") ok = (not absent) and title_node is not None record("text_card_title", ok, "" if ok else "behavior_wrong: produced card title != 'GroceryList' " "(got text=%r)" % all_text(nodes), depth=0, jump_kind="value", expect="GroceryList") @and_test(expects=("text_card_body_preview",)) def test_card_body_reflects_typed_body(): open_empty_home() if not create_note("GroceryList", "Milk and eggs"): record("text_card_body_preview", False, "setup_failed: could not create the note", depth=0, jump_kind="value", expect="Milk and eggs") return nodes = ui() ok = contains_text(nodes, "Milk and eggs") record("text_card_body_preview", ok, "" if ok else "behavior_wrong: card body preview did not contain the " "typed body 'Milk and eggs' — got %r" % all_text(nodes), depth=0, jump_kind="value", expect="Milk and eggs") @and_test(expects=("text_home_card_strips_markdown",)) def test_home_card_body_strips_markdown(): # A body of "# Heading" renders on the Home card preview as "Heading" (the '#' # marker stripped) — a produced, markdown-rendered string. open_empty_home() if not create_note("MdCard", "# Heading"): record("text_home_card_strips_markdown", False, "setup_failed: could not create the markdown note", depth=0, jump_kind="value", expect="Heading (no '#')") return nodes = ui() has_heading = contains_text(nodes, "Heading") # the raw '#Heading'/'# Heading' marker must NOT survive in the preview text raw_hash = any("#" in (n.get("text", "")) for n in nodes) ok = has_heading and not raw_hash record("text_home_card_strips_markdown", ok, "" if ok else "behavior_wrong: Home card did not render '# Heading' as " "'Heading' without the '#' (has_heading=%s raw_hash=%s text=%r)" % (has_heading, raw_hash, all_text(nodes)), depth=0, jump_kind="value", expect="Heading (no '#')") # ══════════════════════════════════════════════════════════════════════════════ # content-desc strings PRODUCED on a state change (focus Description) # ══════════════════════════════════════════════════════════════════════════════ @and_test(expects=("text_desc_focus_toolbar_descs",)) def test_desc_focus_shows_markdown_toolbar_descs(): # Trigger: focus the Description field -> the markdown toolbar appears with the # content-desc strings "Bullet List", "Checkbox", "Insert Image". open_home() fab = find_node(ui(), content_desc="New Note") if not tap_node(fab): record("text_desc_focus_toolbar_descs", False, "setup_failed: New Note FAB not tappable", depth=1, jump_kind="value", expect="Checkbox, Insert Image") return settle("tap") before_desc = all_desc(ui()) absent = ("Checkbox" not in before_desc) and ("Insert Image" not in before_desc) fields = edittexts_by_y(ui()) if len(fields) < 2: record("text_desc_focus_toolbar_descs", False, "setup_failed: no Description field on the Edit surface", depth=1, jump_kind="value", expect="Checkbox, Insert Image") return and_enter("markdown toolbar") # focusing Desc reveals the toolbar surface tap_node(fields[1]) settle("keyboard") nodes = ui() present = contains_desc(nodes, "Checkbox") and contains_desc(nodes, "Insert Image") ok = absent and present record("text_desc_focus_toolbar_descs", ok, "" if ok else "behavior_wrong: focusing Description did not reveal the " "toolbar content-descs 'Checkbox'/'Insert Image' (absent=%s present=%s " "desc=%r)" % (absent, present, all_desc(nodes)), depth=1, jump_kind="value", expect="Checkbox, Insert Image") @and_test(expects=("text_desc_focus_undo_redo_descs",)) def test_desc_focus_shows_undo_redo_descs(): # Trigger: focus Description -> "Undo" and "Redo" content-desc appear in the top bar. open_home() fab = find_node(ui(), content_desc="New Note") if not tap_node(fab): record("text_desc_focus_undo_redo_descs", False, "setup_failed: New Note FAB not tappable", depth=1, jump_kind="value", expect="Undo, Redo") return settle("tap") before_desc = all_desc(ui()) absent = ("Undo" not in before_desc) and ("Redo" not in before_desc) fields = edittexts_by_y(ui()) if len(fields) < 2: record("text_desc_focus_undo_redo_descs", False, "setup_failed: no Description field on the Edit surface", depth=1, jump_kind="value", expect="Undo, Redo") return and_enter("edit-with-undo-redo") tap_node(fields[1]) settle("keyboard") nodes = ui() present = contains_desc(nodes, "Undo") and contains_desc(nodes, "Redo") ok = absent and present record("text_desc_focus_undo_redo_descs", ok, "" if ok else "behavior_wrong: focusing Description did not reveal the " "'Undo'/'Redo' content-descs (absent=%s present=%s desc=%r)" % (absent, present, all_desc(nodes)), depth=1, jump_kind="value", expect="Undo, Redo") # ══════════════════════════════════════════════════════════════════════════════ # Settings section label strings (depth 1) # ══════════════════════════════════════════════════════════════════════════════ @and_test(expects=("text_settings_section_labels",)) def test_settings_section_labels(): open_home() if not and_enter_settings(): record("text_settings_section_labels", False, "setup_failed: could not reach the Settings surface", depth=1, jump_kind="value", expect="Colors & Styles, Behavior, Language, Backup") return nodes = ui() # exact section strings; "Colors & Styles" arrives entity-encoded -> normalise wanted = ["Colors & Styles", "Behavior", "Language", "Backup", "Privacy", "Tools", "About"] missing = [w for w in wanted if not contains_text(nodes, w)] ok = not missing record("text_settings_section_labels", ok, "" if ok else "behavior_wrong: Settings missing section label(s) %r " "— got text=%r" % (missing, all_text(nodes)), depth=1, jump_kind="value", expect="Colors & Styles, Behavior, Language, Backup, Privacy, Tools, About") # ══════════════════════════════════════════════════════════════════════════════ # About label strings (depth 2) — assert the LABELS, not the version VALUE # ══════════════════════════════════════════════════════════════════════════════ @and_test(expects=("text_about_labels",)) def test_about_labels(): open_home() if not and_enter_settings(): record("text_about_labels", False, "setup_failed: could not reach Settings", depth=2, jump_kind="value", expect="Version, Build Type") return if not and_enter_about(): record("text_about_labels", False, "setup_failed: could not reach the About surface", depth=2, jump_kind="value", expect="Version, Build Type") return nodes = ui() # Assert the produced LABEL strings only — a recreation cannot know the value # "1.7-fdroid", so asserting it would be unfair rather than discriminating. has_version = contains_text(nodes, "Version") has_build = contains_text(nodes, "Build Type") ok = has_version and has_build record("text_about_labels", ok, "" if ok else "behavior_wrong: About missing label(s) " "'Version'/'Build Type' (version=%s build=%s text=%r)" % (has_version, has_build, all_text(nodes)), depth=2, jump_kind="value", expect="Version, Build Type") # ══════════════════════════════════════════════════════════════════════════════ # More dropdown label strings (depth 2) # ══════════════════════════════════════════════════════════════════════════════ @and_test(expects=("text_more_menu_labels",)) def test_more_menu_labels(): open_empty_home() if not create_note("MenuNote", "some body text"): record("text_more_menu_labels", False, "setup_failed: could not create a note to open", depth=2, jump_kind="value", expect="Pinned, Copy, Information") return if not and_enter_preview("MenuNote"): record("text_more_menu_labels", False, "setup_failed: could not open the note Preview", depth=2, jump_kind="value", expect="Pinned, Copy, Information") return if not and_enter_more_menu(): record("text_more_menu_labels", False, "setup_failed: could not open the More (Info) dropdown", depth=2, jump_kind="value", expect="Pinned, Copy, Information") return nodes = ui() # exact non-destructive menu label strings the dropdown produced wanted = ["Pinned", "Copy", "Information"] missing = [w for w in wanted if not contains_text(nodes, w)] ok = not missing record("text_more_menu_labels", ok, "" if ok else "behavior_wrong: More dropdown missing label(s) %r — " "got text=%r" % (missing, all_text(nodes)), depth=2, jump_kind="value", expect="Pinned, Copy, Information") # ══════════════════════════════════════════════════════════════════════════════ # Note Info modal label strings + Created Time FORMAT (depth 3) # ══════════════════════════════════════════════════════════════════════════════ @and_test(expects=("text_note_info_labels",)) def test_note_info_labels(): open_empty_home() if not create_note("InfoNote", "Milk Eggs Bread"): record("text_note_info_labels", False, "setup_failed: could not create the note", depth=3, jump_kind="value", expect="Created Time, Words, Characters") return if not and_enter_preview("InfoNote"): record("text_note_info_labels", False, "setup_failed: could not open Preview", depth=3, jump_kind="value", expect="Created Time, Words, Characters") return if not and_enter_more_menu(): record("text_note_info_labels", False, "setup_failed: could not open the More dropdown", depth=3, jump_kind="value", expect="Created Time, Words, Characters") return if not and_reach_note_info(): record("text_note_info_labels", False, "setup_failed: could not open the Note Info modal", depth=3, jump_kind="value", expect="Created Time, Words, Characters") return nodes = ui() wanted = ["Created Time", "Words", "Characters"] missing = [w for w in wanted if not contains_text(nodes, w)] ok = not missing record("text_note_info_labels", ok, "" if ok else "behavior_wrong: Note Info modal missing label(s) %r — " "got text=%r" % (missing, all_text(nodes)), depth=3, jump_kind="value", expect="Created Time, Words, Characters") @and_test(expects=("text_note_info_created_time_format",)) def test_note_info_created_time_format(): open_empty_home() if not create_note("TimeNote", "hello world"): record("text_note_info_created_time_format", False, "setup_failed: could not create the note", depth=3, jump_kind="value", expect="Created Time = yyyy-MM-dd HH:mm:ss") return if not (and_enter_preview("TimeNote") and and_enter_more_menu() and and_reach_note_info()): record("text_note_info_created_time_format", False, "setup_failed: could not reach the Note Info modal", depth=3, jump_kind="value", expect="Created Time = yyyy-MM-dd HH:mm:ss") return nodes = ui() # The Created Time VALUE is a produced dynamic string; assert its SHAPE # (yyyy-MM-dd HH:mm:ss), not any specific timestamp. pat = re.compile(r"\b\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\b") matched = any(pat.search(n.get("text", "")) for n in nodes) record("text_note_info_created_time_format", matched, "" if matched else "behavior_wrong: Created Time value did not match the " "'yyyy-MM-dd HH:mm:ss' format — got text=%r" % all_text(nodes), depth=3, jump_kind="value", expect="Created Time = yyyy-MM-dd HH:mm:ss") # ══════════════════════════════════════════════════════════════════════════════ # Markdown render — the '#' marker is stripped in Preview (dump-readable RULE side) # ══════════════════════════════════════════════════════════════════════════════ @and_test(expects=("text_preview_heading_stripped",)) def test_preview_heading_marker_stripped(): open_empty_home() if not create_note("MdNote", "# Heading"): record("text_preview_heading_stripped", False, "setup_failed: could not create the markdown note", depth=1, jump_kind="value", expect="Heading (no '#')") return if not and_enter_preview("MdNote"): record("text_preview_heading_stripped", False, "setup_failed: could not open Preview", depth=1, jump_kind="value", expect="Heading (no '#')") return nodes = ui() has_heading = contains_text(nodes, "Heading") raw_hash = any("#" in (n.get("text", "")) for n in nodes) ok = has_heading and not raw_hash record("text_preview_heading_stripped", ok, "" if ok else "behavior_wrong: Preview did not render '# Heading' as " "'Heading' without the '#' (has_heading=%s raw_hash=%s text=%r)" % (has_heading, raw_hash, all_text(nodes)), depth=1, jump_kind="value", expect="Heading (no '#')") # ══════════════════════════════════════════════════════════════════════════════ # VLM (budget = 2) — rendered visual properties markdown produces that the view # tree cannot expose: the HEADING is enlarged, and a **bold** span is heavier. # ══════════════════════════════════════════════════════════════════════════════ @and_test(expects=("text_preview_heading_render_vlm",)) def test_preview_heading_render_vlm(): open_empty_home() if not create_note("HeadNote", "# Heading"): record("text_preview_heading_render_vlm", False, "setup_failed: could not create the markdown note", depth=1, jump_kind="value", tag="size") return if not and_enter_preview("HeadNote"): record("text_preview_heading_render_vlm", False, "setup_failed: could not open Preview", depth=1, jump_kind="value", tag="size") return # The text 'Heading' is in the tree but its rendered SIZE (heading vs body) is # not — that rendered property is the VLM's job. vlm_assert("text_preview_heading_render", "Is the word 'Heading' shown as a large heading, clearly bigger " "than normal body text, with NO '#' hash character visible before " "it?", RESULTS_DIR, depth=1, jump_kind="value", tag="size") @and_test(expects=("text_preview_bold_render_vlm",)) def test_preview_bold_render_vlm(): open_empty_home() if not create_note("BoldNote", "word **strongword** end"): record("text_preview_bold_render_vlm", False, "setup_failed: could not create the markdown note", depth=1, jump_kind="value", tag="shape") return if not and_enter_preview("BoldNote"): record("text_preview_bold_render_vlm", False, "setup_failed: could not open Preview", depth=1, jump_kind="value", tag="shape") return # The '**' markers are stripped in the tree (text reads 'word strongword end'), # but the tree cannot say 'strongword' is BOLD — a rendered font-weight property. vlm_assert("text_preview_bold_render", "In the note body, is the single word 'strongword' shown in bold " "(noticeably thicker/heavier) while the surrounding words 'word' and " "'end' are in normal weight, with no '*' asterisk characters " "visible?", RESULTS_DIR, depth=1, jump_kind="value", tag="shape") TESTS = [ test_empty_home_placeholder, test_search_no_match, test_empty_note_discard_placeholder, test_card_title_reflects_typed_title, test_card_body_reflects_typed_body, test_home_card_body_strips_markdown, test_desc_focus_shows_markdown_toolbar_descs, test_desc_focus_shows_undo_redo_descs, test_settings_section_labels, test_about_labels, test_more_menu_labels, test_note_info_labels, test_note_info_created_time_format, test_preview_heading_marker_stripped, test_preview_heading_render_vlm, test_preview_bold_render_vlm, ] if __name__ == "__main__": if not device_online(): print("Device not reachable — the tunnel may have dropped.") run_suite(TESTS, "text")