图库: 书库改文件管理器模型 + 关闭缩略图 + 根锁定/无卡容错(M18)

用户反馈四件事一起改:
- 「目录里有一张图就被当成最终目录」→ 取消「目录含图=一本书」全部链路
  (探测/页数/封面/续读角标/懒算 worker/sr2 补位),列表 = 目录 + 图片 +
  .cbz/.zip(目录优先、组内自然序)。点目录=进入(空目录也进),点图片=
  mr_reader_open_dir_at(当前目录, 图片自然序下标),点档案册=打开档案册。
- 「文件夹浏览特别卡」→ 删除缩略图线程/磁盘 bmp 缓存/LRU/解码路径与行内
  缩略图;首帧只做一次 opendir/readdir+lstat,副标题(类型·大小)扫描期
  生成一次,行合成/滚动/点击零 IO 零格式化;去掉逐行 snap(自由滚动手感)。
  2000 项目录扫描 ~2.8ms(opendir=1, lstat=2000),行合成 2.78ms/200 万次
  (宿主实测;旧实现 2000 项 entry 含 512B cover,内存 2.7MB→1.7MB)。
- 「不要访问 SD 卡上级目录」→ 浏览根锁定 MR_ROOT_DIR:删除越根的顶栏
  「上级」/library_go_parent,根隐藏顶栏返回,左缘返回到根=退出 App。
- 「没有 SD 卡不要报错」→ 根不可访问(未插卡/挂载失败/根是文件)时友好空态
  「未检测到 SD 卡」+ 顶栏刷新/重进页面重试;子目录不可读才弹提示横幅。

集成陷阱:书库图片下标与阅读器目录册共享同一套过滤器(隐藏项/图片扩展名/
普通文件/符号链接)与 mr_natural_cmp,test_browse_lazy 直接对比
mr_dir_book_init 顺序,test_open_book_host 断言「点 img10 就开 img10」。

reader 侧 M17 遗留:mr_reader_open_dir_at 在阅读页已激活时先 pop 再 push,
走完整 leave/enter(SDK 对同 view 重复 push 是 no-op 且会压重复 activity)。

回归:run_tests.sh / run_webtoon_tests.sh 全 PASS;交叉构建零警告。
This commit is contained in:
AskaEth committed 2026-11-05 20:26:40 +08:00
1 parent d07b99056f
commit 64336f95d9
16 files changed
+940 -1927

No files matched your search

+8 -6
View File
@@ -1,9 +1,9 @@
#!/bin/bash
# run_tests.sh — 宿主 gcc 构建并运行图库测试(不链 SDK 静态库、不需要设备)。
# - test_library:单层目录扫描/自然排序/父目录/阅读方向
# - test_browse_nav:访问放开(系统/空/无图目录可进入)、上溯到 /、不可读目录、截断
# - test_browse_lazy:单层探测/懒判定补位/mtime 失效缓存/行合成 O(1)/M7 册间语义
# - test_browse_perf:500 目录×8 层嵌套+2000 文件夹具,断言扫描单层且有界
# - test_library:单层目录扫描(目录+图片+档案册)/自然排序/图片下标/父目录/阅读方向
# - test_browse_nav:访问放开(系统/空/无图目录可进入)、图片与档案册成行、截断、不可读
# - test_browse_lazy:行合成零 IO/副标题预生成/图片下标 == 阅读器目录册下标(集成陷阱)
# - test_browse_perf:500 目录×8 层嵌套 + 2000 项大目录,断言单层扫描与行合成成本
# - test_settings_reader_persist:阅读方向 meow_config 持久化(默认/落盘/重启恢复/
# 坏值/非法值/只读一次/写失败只告警)
# - test_pin:启动 PIN 格式/设置/校验/修改/禁用/残缺配置/落盘失败(logic/pin.c 为纯 C,
@@ -33,7 +33,8 @@ gcc -Wall -Wextra -O1 -DMR_LIBRARY_TEST_HOOKS -I. -I"$SDK" \
"$OUT"/test_browse_nav
gcc -Wall -Wextra -O1 -DMR_LIBRARY_TEST_HOOKS -I. -I"$SDK" \
test/test_browse_lazy.c logic/library.c logic/reader.c test/settings_reader_stub.c \
test/test_browse_lazy.c logic/library.c logic/reader.c \
logic/reader/page_source.c test/settings_reader_stub.c \
-pthread -o "$OUT/test_browse_lazy"
"$OUT"/test_browse_lazy
@@ -73,8 +74,9 @@ if [ -f "$SDK/meow_ui_view.c" ] && [ -f "$SDK/meow_ui_config.c" ]; then
-pthread -lm -o "$OUT"/test_settings_lock_host
"$OUT"/test_settings_lock_host
# 书库浏览页集成(src2 懒算补位/行语义/上溯至 / /不可读提示);同样需要 SDK 源码树。
# 书库浏览页集成(文件管理器行模型/点击语义/根锁定/无 SD 卡空态/零 IO 行合成);同样需要 SDK 源码树。
gcc -Wall -Wextra -O1 -DMEOW_UI_HOST_TEST -DMR_LIBRARY_UI_TEST_HOOKS \
-DMR_LIBRARY_TEST_HOOKS \
-I. -I"$SDK" -I"$SDK/include" \
test/test_browse_ui_host.c ui/library/library.c logic/library.c \
logic/reader.c logic/reader/page_source.c logic/reader/archive_source.c \
+90 -306
View File
@@ -1,24 +1,22 @@
/* test_browse_lazy.c — 单层探测 / 懒判定 / mtime 失效缓存 / 行合成(纯 C,无 SDK)。
/* test_browse_lazy.c — 浏览行模型 / 零 IO 行合成 / 图片下标一致性(纯 C,无 SDK)。
*
* 覆盖:
* L1 单层探测:直接图片数、自然序首图、空目录标记;不递归(只有子目录含图的目录不算书);
* L2 懒判定接线:scan 后行全部 PENDING(占位副标题)→ apply_probe 后页数/首图/副标题/
* 书标记;空目录「空目录」;无图目录「无图片」;探测失败「无法读取」且视为非书;
* L3 缓存:mtime 失效(同 mtime 命中不读盘;目录变化后重扫并刷新结果);并发调用线程安全;
* L3b 评审 P2:探测期间 mtime/size 变化的结果不写缓存(避免「不完整结果绑定新 mtime」);
* L4 行合成 O(1):mr_library_row_at 纯内存(IO 计数不变)、截断提示行;
* L5 M7 兼容语义:「直接含图兄弟册」+ 自然序册间顺序(M7 mr_book_adjacent 的口径)。
* 文件名沿用历史(旧的「懒判定」已随 M18 文件管理器模型删除);本测试现在覆盖:
* L1 行合成 O(1):mr_library_row_at 纯内存(IO 计数不变)、截断提示行;
* L2 **书库→阅读器下标记号契约**:mr_library_image_index 数出的下标 == 阅读器目录册
* (logic/reader/page_source.c: mr_dir_book_init)里同一张图的下标。
* 夹具含大小写混排(A.jpg/a.jpg)、数字序号(img2/img10)、大写扩展名(IMG3.JPG)、
* 前导零(page01/page1)、夹在图片之间的档案册与普通文件;
* L3 扫描期副标题:每行都有内容(不再是占位「…」,渲染期不做格式化)。
*
* 编译(在 tuku/ 下):
* gcc -Wall -Wextra -O1 -I. -I../sdk -DMR_LIBRARY_TEST_HOOKS \
* test/test_browse_lazy.c logic/library.c logic/reader.c test/settings_reader_stub.c \
* test/test_browse_lazy.c logic/library.c logic/reader/page_source.c \
* -pthread -o build/host/test_browse_lazy
*/
#include "logic/library.h"
#include "logic/reader/page_source.h"
#include <dirent.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -81,255 +79,47 @@ static void joinp(char *out, int outsz, const char *a, const char *b)
snprintf(out, (size_t)outsz, "%s/%s", a, b);
}
/* 把目录 mtime 显式推后 3 秒(避免同秒内变化被缓存误命中)。 */
static void bump_mtime(const char *path)
static long long now_us(void)
{
struct timespec ts[2];
ts[0].tv_sec = time(NULL) + 3;
ts[0].tv_nsec = 0;
ts[1] = ts[0];
utimensat(AT_FDCWD, path, ts, 0);
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (long long)ts.tv_sec * 1000000LL + ts.tv_nsec / 1000LL;
}
static const mr_library_entry *find_entry(const mr_library *lib, const char *name)
/* 阅读器目录册用的假解码回调(本测试不打开任何页)。 */
static meow_img *fake_load(const char *path, void *ud, int maxdim)
{
for (int i = 0; i < lib->count; i++)
if (strcmp(lib->items[i].name, name) == 0) return &lib->items[i];
(void)path; (void)ud; (void)maxdim;
return NULL;
}
static void fake_unload(meow_img *img) { (void)img; }
static int find_index(const mr_library *lib, const char *name, int *out)
{
for (int i = 0; i < lib->count; i++)
if (strcmp(lib->items[i].name, name) == 0) { *out = i; return 1; }
*out = -1;
return 0;
}
/* ---- L1 + L2 ---- */
static void test_probe_and_apply(const char *root)
{
char book[MR_PATH_MAX], noimg[MR_PATH_MAX], empty[MR_PATH_MAX], dirs_only[MR_PATH_MAX];
char deep[MR_PATH_MAX];
joinp(book, sizeof book, root, "book");
joinp(noimg, sizeof noimg, root, "noimg");
joinp(empty, sizeof empty, root, "empty");
joinp(dirs_only, sizeof dirs_only, root, "dirs_only");
joinp(deep, sizeof deep, root, "dirs_only/deep");
mr_library_probe p;
CHECK(mr_library_probe_dir(book, &p) == 0, "probe book");
CHECK(p.pages == 2, "book pages=%d", p.pages);
CHECK(p.empty == 0, "book not empty");
CHECK(strstr(p.cover, "page2.png") != NULL, "cover natural-first: %s", p.cover);
CHECK(mr_library_probe_dir(noimg, &p) == 0 && p.pages == 0 && p.empty == 0,
"noimg: 0 pages, not empty");
CHECK(mr_library_probe_dir(empty, &p) == 0 && p.pages == 0 && p.empty == 1,
"empty dir flagged");
CHECK(mr_library_probe_dir(dirs_only, &p) == 0 && p.pages == 0 && p.empty == 0,
"dirs_only has no direct images (no recursion)");
CHECK(mr_library_probe_dir(deep, &p) == 0 && p.pages == 1, "deep is a book");
char slash[MR_PATH_MAX];
snprintf(slash, sizeof slash, "%s/", book);
CHECK(mr_library_probe_dir(slash, &p) == 0 && p.pages == 2, "trailing slash");
CHECK(mr_library_probe_dir("/no/such/dir", &p) == -1, "missing dir -> -1");
CHECK(mr_library_probe_dir(NULL, &p) == -1, "NULL -> -1");
mr_library lib;
memset(&lib, 0, sizeof lib);
CHECK(mr_library_scan(&lib, root, MR_LIB_MAX_DEPTH) == 5, "scan 5 dirs (got %d)", lib.count);
const mr_library_entry *e = find_entry(&lib, "book");
CHECK(e && e->probe_state == MR_LIB_PENDING && e->pages == -1,
"book pending after scan");
int idx_book, idx_noimg, idx_empty, idx_dirs;
CHECK(find_index(&lib, "book", &idx_book), "book index");
CHECK(find_index(&lib, "noimg", &idx_noimg), "noimg index");
CHECK(find_index(&lib, "empty", &idx_empty), "empty index");
CHECK(find_index(&lib, "dirs_only", &idx_dirs), "dirs_only index");
if (idx_book < 0 || idx_noimg < 0 || idx_empty < 0 || idx_dirs < 0) {
mr_library_free(&lib);
return;
}
CHECK(mr_library_probe_dir(lib.items[idx_book].path, &p) == 0, "probe row book");
CHECK(mr_library_apply_probe(&lib, idx_book, &p) == 1, "apply book");
e = &lib.items[idx_book];
CHECK(e->has_images == 1 && e->pages == 2 && e->probe_state == MR_LIB_READY,
"book ready");
CHECK(strcmp(e->subtitle, "2 pages") == 0, "book subtitle=%s", e->subtitle);
CHECK(strstr(e->cover, "page2.png") != NULL, "book row cover=%s", e->cover);
CHECK(mr_library_probe_dir(lib.items[idx_noimg].path, &p) == 0 &&
mr_library_apply_probe(&lib, idx_noimg, &p) == 1, "apply noimg");
e = &lib.items[idx_noimg];
CHECK(e->has_images == 0 && strcmp(e->subtitle, "无图片") == 0,
"noimg subtitle=%s", e->subtitle);
CHECK(mr_library_probe_dir(lib.items[idx_empty].path, &p) == 0 &&
mr_library_apply_probe(&lib, idx_empty, &p) == 1, "apply empty");
e = &lib.items[idx_empty];
CHECK(e->has_images == 0 && e->empty == 1 && strcmp(e->subtitle, "空目录") == 0,
"empty subtitle=%s", e->subtitle);
CHECK(mr_library_mark_error(&lib, idx_dirs) == 1, "mark error");
e = &lib.items[idx_dirs];
CHECK(e->probe_state == MR_LIB_FAILED && e->has_images == 0 &&
strcmp(e->subtitle, "无法读取") == 0, "failed subtitle=%s", e->subtitle);
CHECK(mr_library_apply_probe(&lib, 999, &p) == 0, "apply out of range");
CHECK(mr_library_mark_error(&lib, -1) == 0, "mark_error out of range");
mr_library_free(&lib);
}
/* 档案册探测回调(P2 用例):可注入「探测期间文件变化」。 */
typedef struct arch_probe_ctx {
int calls;
int bump_times; /* 前 N 次调用里改动文件 mtime(模拟探测期间变化) */
} arch_probe_ctx;
static int arch_probe(const char *path, void *ud)
{
arch_probe_ctx *c = (arch_probe_ctx *)ud;
c->calls++;
if (c->calls <= c->bump_times) bump_mtime(path);
return 1;
}
/* ---- L3b:评审 P2——探测期间 mtime 变化不得写入缓存(消除假新鲜) ---- */
static void test_cache_race(const char *root)
{
char arc[MR_PATH_MAX];
joinp(arc, sizeof arc, root, "race.cbz");
CHECK(touch_file(arc) == 0, "create race.cbz");
mr_library_probe_cache_clear();
mr_library_probe p;
/* 稳定文件:第二次走缓存,回调只调一次。 */
arch_probe_ctx stable = { 0, 0 };
CHECK(mr_library_probe_path(arc, 1, arch_probe, &stable, &p) == 0 && p.pages == 1,
"archive probe");
CHECK(mr_library_probe_path(arc, 1, arch_probe, &stable, &p) == 0 && p.pages == 1,
"archive probe (cached)");
CHECK(stable.calls == 1, "stable archive cached (calls=%d)", stable.calls);
/* 探测期间文件变化:本次结果不得入缓存 → 下一次仍重新探测。 */
mr_library_probe_cache_clear();
arch_probe_ctx race = { 0, 1 };
CHECK(mr_library_probe_path(arc, 1, arch_probe, &race, &p) == 0 && p.pages == 1,
"racing archive probe");
CHECK(race.calls == 1, "racing probe called once (calls=%d)", race.calls);
CHECK(mr_library_probe_path(arc, 1, arch_probe, &race, &p) == 0 && p.pages == 1,
"racing archive probe again");
CHECK(race.calls == 2, "changed-during-probe result NOT cached (calls=%d)", race.calls);
/* 第二次稳定后落缓存,第三次命中。 */
CHECK(mr_library_probe_path(arc, 1, arch_probe, &race, &p) == 0 && p.pages == 1,
"racing archive probe third");
CHECK(race.calls == 2, "stable result cached after third call (calls=%d)", race.calls);
mr_library_probe_cache_clear();
}
/* ---- L3:mtime 失效缓存 + 并发 ---- */
#define THREADS 6
typedef struct th_arg {
const char *dir;
int want;
int *fail;
pthread_mutex_t *mu;
} th_arg;
static void *probe_thread(void *arg)
{
th_arg *a = (th_arg *)arg;
for (int i = 0; i < 200; i++) {
mr_library_probe p;
int ok = (mr_library_probe_cached(a->dir, &p) == 0 && p.pages == a->want);
if (!ok) {
pthread_mutex_lock(a->mu);
(*a->fail)++;
pthread_mutex_unlock(a->mu);
}
}
return NULL;
}
static void test_cache(const char *root)
{
char book[MR_PATH_MAX];
joinp(book, sizeof book, root, "book");
mr_library_probe_cache_clear();
mr_library_io_stats st;
mr_library_probe p;
mr_library_test_io_reset();
CHECK(mr_library_probe_cached(book, &p) == 0 && p.pages == 2, "cache miss probe");
mr_library_test_io_get(&st);
CHECK(st.opendirs == 1, "cache miss opens once (got %ld)", st.opendirs);
mr_library_test_io_reset();
CHECK(mr_library_probe_cached(book, &p) == 0 && p.pages == 2, "cache hit");
mr_library_test_io_get(&st);
CHECK(st.opendirs == 0 && st.lstats == 0, "cache hit does no IO (open=%ld stat=%ld)",
st.opendirs, st.lstats);
char img[MR_PATH_MAX];
joinp(img, sizeof img, book, "page30.png");
CHECK(touch_file(img) == 0, "add image");
bump_mtime(book);
mr_library_test_io_reset();
CHECK(mr_library_probe_cached(book, &p) == 0 && p.pages == 3,
"cache invalidated by mtime (pages=%d)", p.pages);
mr_library_test_io_get(&st);
CHECK(st.opendirs == 1, "invalidation re-opens (got %ld)", st.opendirs);
CHECK(mr_library_probe_cached("/no/such/dir", &p) == -1, "missing dir -> -1");
pthread_t th[THREADS];
th_arg args[THREADS];
int failures = 0;
pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;
int started = 0;
for (int t = 0; t < THREADS; t++) {
args[t].dir = book;
args[t].want = 3;
args[t].fail = &failures;
args[t].mu = &mu;
if (pthread_create(&th[t], NULL, probe_thread, &args[t]) == 0) started++;
}
for (int t = 0; t < started; t++) pthread_join(th[t], NULL);
CHECK(started == THREADS, "threads started (%d)", started);
CHECK(failures == 0, "concurrent probe failures=%d", failures);
pthread_mutex_destroy(&mu);
}
/* ---- L4:行合成 O(1) + 截断提示行 ---- */
static void test_rows(const char *root)
/* ---- L1 + L3:行合成零 IO / 副标题预生成 ---- */
static void test_rows_zero_io(const char *root)
{
mr_library lib;
memset(&lib, 0, sizeof lib);
mr_library_scan(&lib, root, MR_LIB_MAX_DEPTH);
CHECK(mr_library_scan(&lib, root, MR_LIB_MAX_DEPTH) > 0, "scan fixture");
for (int i = 0; i < lib.count; i++) {
mr_library_row row;
CHECK(mr_library_row_at(&lib, i, &row) == 1, "row %d", i);
CHECK(row.title && row.title[0], "row %d title", i);
CHECK(row.subtitle && row.subtitle[0] && strcmp(row.subtitle, "…") != 0,
"row %d subtitle pre-generated (%s)", i, row.subtitle ? row.subtitle : "");
}
mr_library_io_stats st;
mr_library_test_io_reset();
struct timespec t0, t1;
clock_gettime(CLOCK_MONOTONIC, &t0);
long long t0 = now_us();
int rows = 0;
for (int r = 0; r < 2000; r++)
for (int i = 0; i < lib.count; i++) {
mr_library_row row;
if (mr_library_row_at(&lib, i, &row)) rows++;
}
clock_gettime(CLOCK_MONOTONIC, &t1);
long long us = now_us() - t0;
mr_library_test_io_get(&st);
long long us = (long long)(t1.tv_sec - t0.tv_sec) * 1000000LL +
(t1.tv_nsec - t0.tv_nsec) / 1000LL;
CHECK(rows == 2000 * lib.count, "rows synthesized (%d)", rows);
CHECK(st.opendirs == 0 && st.readdirs == 0 && st.lstats == 0,
"row synth is pure memory (open=%ld rd=%ld stat=%ld)",
@@ -343,99 +133,93 @@ static void test_rows(const char *root)
"truncation info row");
CHECK(strcmp(row.title, "已截断") == 0, "truncation title=%s", row.title);
CHECK(strstr(row.subtitle, "2000") != NULL, "truncation subtitle=%s", row.subtitle);
mr_library_free(&lib);
}
/* ---- L5:M7 兼容语义(直接含图兄弟册 + 自然序) ---- */
static void collect_books(const char *parent, char names[][MR_NAME_MAX], int max, int *out_n)
/* ---- L2:书库图片下标 == 阅读器目录册下标 ---- */
static void test_image_index_matches_reader(const char *dir)
{
mr_library lib;
memset(&lib, 0, sizeof lib);
int n = 0;
if (mr_library_scan(&lib, parent, MR_LIB_MAX_DEPTH) > 0) {
for (int i = 0; i < lib.count && n < max; i++) {
mr_library_probe p;
if (mr_library_probe_cached(lib.items[i].path, &p) == 0 && p.pages > 0) {
snprintf(names[n], MR_NAME_MAX, "%s", lib.items[i].name);
n++;
}
}
CHECK(mr_library_scan(&lib, dir, MR_LIB_MAX_DEPTH) > 0, "scan mixed dir");
mr_dir_book db;
memset(&db, 0, sizeof db);
int pages = mr_dir_book_init(&db, dir, 720, fake_load, fake_unload, NULL);
CHECK(pages > 0, "reader dir book pages=%d", pages);
/* 书库里的图片行按出现顺序,其下标必须依次是 0..n-1(无空洞/无重排)。 */
int expect = 0;
for (int i = 0; i < lib.count; i++) {
if (lib.items[i].kind != MR_LIB_KIND_IMAGE) continue;
int idx = mr_library_image_index(&lib, i);
CHECK(idx == expect, "image row %s index=%d (want %d)",
lib.items[i].name, idx, expect);
expect++;
}
CHECK(expect == pages, "library images=%d vs reader pages=%d", expect, pages);
if (expect != pages) {
mr_library_free(&lib);
mr_dir_book_free(&db);
return;
}
/* 逐张对齐:书库第 k 张图 == 阅读器目录册第 k 页(下标即起始页)。 */
int k = 0;
for (int i = 0; i < lib.count; i++) {
if (lib.items[i].kind != MR_LIB_KIND_IMAGE) continue;
char page_path[MR_PATH_MAX];
mr_dir_book_path(&db, k, page_path, (int)sizeof page_path);
CHECK(strcmp(page_path, lib.items[i].path) == 0,
"page[%d]: library=%s reader=%s", k, lib.items[i].path, page_path);
k++;
}
/* 档案册/目录不占图片下标。 */
for (int i = 0; i < lib.count; i++) {
if (lib.items[i].kind == MR_LIB_KIND_IMAGE) continue;
CHECK(mr_library_image_index(&lib, i) == -1,
"non-image %s has no image index", lib.items[i].name);
}
mr_dir_book_free(&db);
mr_library_free(&lib);
*out_n = n;
}
static int book_index(char names[][MR_NAME_MAX], int n, const char *cur)
{
for (int i = 0; i < n; i++)
if (strcmp(names[i], cur) == 0) return i;
return -1;
}
static void test_m7_semantics(const char *root)
{
char parent[MR_PATH_MAX];
joinp(parent, sizeof parent, root, "series");
char names[16][MR_NAME_MAX];
int n = 0;
collect_books(parent, names, 16, &n);
CHECK(n == 3, "series books=%d (vol1/vol2/vol10; vol6 is not a book)", n);
if (n == 3) {
CHECK(strcmp(names[0], "vol1") == 0, "book[0]=%s", names[0]);
CHECK(strcmp(names[1], "vol2") == 0, "book[1]=%s", names[1]);
CHECK(strcmp(names[2], "vol10") == 0, "book[2]=%s", names[2]);
}
CHECK(book_index(names, n, "vol6") < 0, "dirs-only vol6 skipped (direct images only)");
CHECK(book_index(names, n, ".hidden") < 0, "hidden skipped");
CHECK(book_index(names, n, "MeowOS") < 0, "system dir without direct images skipped");
int i2 = book_index(names, n, "vol2");
CHECK(i2 >= 0 && i2 + 1 < n && strcmp(names[i2 + 1], "vol10") == 0,
"vol2 next = vol10 (natural order, vol6 skipped)");
CHECK(book_index(names, n, "vol10") == n - 1, "vol10 is last (no next)");
CHECK(book_index(names, n, "vol1") == 0, "vol1 is first (no prev)");
}
int main(void)
{
char tmpl[] = "./mr_lazy_XXXXXX";
char tmpl[] = "./mr_rows_XXXXXX";
char *root = mkdtemp(tmpl);
CHECK(root != NULL, "mkdtemp");
if (!root) return 1;
char p[MR_PATH_MAX];
const char *dirs[] = { "book", "noimg", "empty", "dirs_only/deep", "series" };
const char *dirs[] = { "book", "mixed", "mixed/sub", "empty" };
for (unsigned i = 0; i < sizeof dirs / sizeof dirs[0]; i++) {
joinp(p, sizeof p, root, dirs[i]);
CHECK(mkdir_p(p) == 0, "mkdir %s", p);
}
joinp(p, sizeof p, root, "book/page2.png"); CHECK(touch_file(p) == 0, "page2");
joinp(p, sizeof p, root, "book/page10.jpg"); CHECK(touch_file(p) == 0, "page10");
joinp(p, sizeof p, root, "book/notes.txt"); CHECK(touch_file(p) == 0, "notes");
joinp(p, sizeof p, root, "noimg/readme.txt"); CHECK(touch_file(p) == 0, "readme");
joinp(p, sizeof p, root, "dirs_only/deep/a.png"); CHECK(touch_file(p) == 0, "deep a");
joinp(p, sizeof p, root, "mixed/sub/zz.png");CHECK(touch_file(p) == 0, "nested");
/* M7 语义夹具:vol1/vol2/vol10 直接含图;vol6 只有子目录含图;.hidden / MeowOS 干扰项 */
const char *mk[] = { "series/vol1/001.png", "series/vol2/001.png",
"series/vol10/001.png", "series/vol6/sub/001.png",
"series/.hidden/001.png", "series/MeowOS/deep/001.png" };
for (unsigned i = 0; i < sizeof mk / sizeof mk[0]; i++) {
joinp(p, sizeof p, root, mk[i]);
/* 先建父目录,再落文件 */
char dir[MR_PATH_MAX];
snprintf(dir, sizeof dir, "%s", p);
char *slash = strrchr(dir, '/');
if (slash) { *slash = '\0'; CHECK(mkdir_p(dir) == 0, "mkdir %s", dir); }
/* 大小写混排 + 数字序号 + 大写扩展名 + 前导零 + 夹档案/普通文件。 */
const char *mixed[] = {
"A.jpg", "a.jpg", "img2.png", "IMG3.JPG", "img10.png",
"page01.bmp", "page1.jpeg", "cover.cbz", "notes.txt", ".hidden.png"
};
for (unsigned i = 0; i < sizeof mixed / sizeof mixed[0]; i++) {
joinp(p, sizeof p, root, "mixed");
size_t dl = strlen(p);
snprintf(p + dl, sizeof p - dl, "/%s", mixed[i]);
CHECK(touch_file(p) == 0, "image %s", p);
}
test_probe_and_apply(root);
test_cache(root);
test_cache_race(root);
test_rows(root);
test_m7_semantics(root);
test_rows_zero_io(root);
joinp(p, sizeof p, root, "mixed");
test_image_index_matches_reader(p);
rm_rf(root);
printf("%s: %d passed, %d failed\n", g_fail ? "FAIL" : "PASS", g_pass, g_fail);
+41 -26
View File
@@ -1,17 +1,16 @@
/* test_browse_nav.c — 浏览访问放开 / 上溯父目录 / 截断标注(纯 C,无 SDK)。
/* test_browse_nav.c — 浏览模型 / 进入目录 / 截断(纯 C,无 SDK)。
*
* 覆盖(M13 用户要求②):
* N1 访问放开:系统目录(MeowAPPs/MeowData/MeowOS/lost+found)不再隐藏,
* 空目录/无图目录都列出且可进入(scan 返回 0 而不是 -1);
* N2 隐藏项/普通文件/符号链接仍不列(防环、防噪声);
* N3 不可读目录/不存在/不是目录:scan 返回 -1,调用方据此提示并停留;
* N4 上溯父目录:mr_path_parent 可一路走到文件系统 "/"(不再以 TF 卡根为界);
* N5 单目录条目上限:达到 MR_LIB_SCAN_MAX 后截断并置 truncated(行合成给出提示行)。
* 覆盖(M18 文件管理器模型):
* N1 单层列表:目录 + 受支持图片 + 档案册(cbz/zip)都成行,目录优先+自然序;
* 系统目录/空目录/无图目录都列出且可进入;隐藏项/普通文件/符号链接不列;
* N2 不可读目录/不存在/不是目录:scan 返回 -1,调用方据此显示空态或提示;
* N3 父目录路径推导:mr_path_parent 是纯路径工具(可算到文件系统 "/";
* 浏览越界的限制在 UI 导航层,见 test_browse_ui_host 的根锁定用例);
* N4 单目录条目上限:达到 MR_LIB_SCAN_MAX 后截断并置 truncated(行合成给出提示行)。
*
* 编译(在 tuku/ 下):
* gcc -Wall -Wextra -O1 -I. -I../sdk -DMR_LIBRARY_TEST_HOOKS \
* test/test_browse_nav.c logic/library.c logic/reader.c test/settings_reader_stub.c \
* -pthread -o build/host/test_browse_nav
* test/test_browse_nav.c logic/library.c -pthread -o build/host/test_browse_nav
*/
#include "logic/library.h"
@@ -85,19 +84,20 @@ static const mr_library_entry *find_entry(const mr_library *lib, const char *nam
return NULL;
}
/* ---- N1/N2 ---- */
/* ---- N1 ---- */
static void test_open_access(const char *root)
{
mr_library lib;
memset(&lib, 0, sizeof lib);
int n = mr_library_scan(&lib, root, MR_LIB_MAX_DEPTH);
CHECK(n == 7, "root lists 7 dirs (got %d)", n);
CHECK(n == 9, "root lists 9 rows (got %d)", n);
CHECK(lib.truncated == 0, "root not truncated");
static const char *want[7] = {
"book", "empty", "lost+found", "MeowAPPs", "MeowData", "MeowOS", "noimg"
static const char *want[9] = {
"book", "empty", "lost+found", "MeowAPPs", "MeowData", "MeowOS", "noimg",
"album.cbz", "loose.png"
};
for (int i = 0; i < 7 && i < lib.count; i++)
for (int i = 0; i < 9 && i < lib.count; i++)
CHECK(strcmp(lib.items[i].name, want[i]) == 0,
"sort[%d]=%s got %s", i, want[i], lib.items[i].name);
@@ -105,11 +105,13 @@ static void test_open_access(const char *root)
CHECK(find_entry(&lib, want[i]) != NULL, "system/empty/noimg dir listed: %s", want[i]);
CHECK(find_entry(&lib, ".hidden") == NULL, "hidden dir excluded");
CHECK(find_entry(&lib, "linkdir") == NULL, "symlink dir excluded");
CHECK(find_entry(&lib, "loose.png") == NULL, "file excluded");
CHECK(find_entry(&lib, "loose.png") != NULL, "loose image listed (clickable)");
CHECK(find_entry(&lib, "album.cbz") != NULL, "archive listed");
CHECK(find_entry(&lib, "readme.txt") == NULL, "plain file excluded");
/* 可进入:每个列出的目录都能被 scan(0 项也算成功,不等于 -1)。 */
char p[MR_PATH_MAX];
for (int i = 0; i < lib.count; i++) {
if (lib.items[i].kind != MR_LIB_KIND_DIR) continue;
mr_library child;
memset(&child, 0, sizeof child);
CHECK(mr_library_scan(&child, lib.items[i].path, MR_LIB_MAX_DEPTH) >= 0,
@@ -117,11 +119,20 @@ static void test_open_access(const char *root)
mr_library_free(&child);
}
char p[MR_PATH_MAX];
joinp(p, sizeof p, root, "book");
mr_library book;
memset(&book, 0, sizeof book);
CHECK(mr_library_scan(&book, p, MR_LIB_MAX_DEPTH) == 1 &&
find_entry(&book, "001.png") != NULL, "dir with images lists the image");
mr_library_free(&book);
joinp(p, sizeof p, root, "MeowOS");
mr_library syslib;
memset(&syslib, 0, sizeof syslib);
CHECK(mr_library_scan(&syslib, p, MR_LIB_MAX_DEPTH) == 1 &&
find_entry(&syslib, "sub") != NULL, "system dir browsable");
CHECK(mr_library_scan(&syslib, p, MR_LIB_MAX_DEPTH) == 2 &&
find_entry(&syslib, "sub") != NULL &&
find_entry(&syslib, "001.png") != NULL, "system dir browsable");
mr_library_free(&syslib);
joinp(p, sizeof p, root, "empty");
@@ -133,13 +144,13 @@ static void test_open_access(const char *root)
mr_library_free(&lib);
}
/* ---- N3 ---- */
/* ---- N2 ---- */
static void test_unreadable(const char *root)
{
mr_library lib;
char p[MR_PATH_MAX];
memset(&lib, 0, sizeof lib);
CHECK(mr_library_scan(&lib, "/no/such/dir/m13", MR_LIB_MAX_DEPTH) == -1, "missing -> -1");
CHECK(mr_library_scan(&lib, "/no/such/dir/m18", MR_LIB_MAX_DEPTH) == -1, "missing -> -1");
CHECK(lib.count == 0 && lib.truncated == 0, "failed scan leaves empty lib");
mr_library_free(&lib);
@@ -160,8 +171,8 @@ static void test_unreadable(const char *root)
}
}
/* ---- N4 ---- */
static void test_up_to_root(void)
/* ---- N3 ---- */
static void test_parent_path(void)
{
char cur[MR_PATH_MAX];
snprintf(cur, sizeof cur, "/usr/data/mnt/sd_0/a/b/c");
@@ -183,10 +194,10 @@ static void test_up_to_root(void)
"root parent stays /");
CHECK(mr_path_parent("/usr/data/mnt/sd_0", up, (int)sizeof up) == 0 &&
strcmp(up, "/usr/data/mnt") == 0,
"TF root can go above mount point: %s", up);
"path helper is root-agnostic: %s", up);
}
/* ---- N5 ---- */
/* ---- N4 ---- */
static void test_truncation(void)
{
CHECK(MR_LIB_SCAN_MAX == 2000, "default cap is 2000 (got %d)", MR_LIB_SCAN_MAX);
@@ -241,6 +252,10 @@ int main(void)
touch_file(p);
joinp(p, sizeof p, root, "loose.png");
touch_file(p);
joinp(p, sizeof p, root, "album.cbz");
touch_file(p);
joinp(p, sizeof p, root, "readme.txt");
touch_file(p);
/* 符号链接目录不参与浏览(有权限才建;建不了不影响其余断言)。 */
char target[MR_PATH_MAX], link[MR_PATH_MAX];
joinp(target, sizeof target, root, "book");
@@ -249,7 +264,7 @@ int main(void)
test_open_access(root);
test_unreadable(root);
test_up_to_root();
test_parent_path();
rm_rf(root);
test_truncation();
+67 -43
View File
@@ -1,17 +1,16 @@
/* test_browse_perf.c — 单层扫描性能/有界性验收(纯 C,无 SDK)。
/* test_browse_perf.c — 单层扫描 / 行合成性能与有界性验收(纯 C,无 SDK)。
*
* 夹具:500 个顶层目录 × 8 层嵌套(4000 个子目录)+ 根目录 2000 个文件。
* 断言(对应 M13 任务⑤):
* P1 首帧扫描只做单层:opendir 次数 == 1(旧实现会递归打开数千个目录);
* lstat 次数 == 本层条目数;耗时有界;
* P2 后台探测同样只读一层:每个目录一次 opendir,不进入其嵌套子树;
* P3 行合成(UI src/bind 的数据面)纯内存、零 IO、O(1);
* P4 深目录直接扫描也只打开它自己。
* 夹具:500 个顶层目录 × 8 层嵌套(4000 个子目录)+ 根目录 2000 个普通文件
* + 一个 big/ 目录(2000 张图片,验证 2000 项大目录)。
* 断言(对应 M18 任务③):
* P1 首帧扫描只做单层:opendir 次数 == 1,lstat 次数 == 本层条目数,耗时有界;
* P2 2000 项大目录:一次扫描出全部 2000 行,opendir=1、lstat=2000,耗时有界;
* P3 行合成(UI src 的数据面)纯内存、零 IO、耗时有界(2000 行 × 1000 遍);
* P4 进入深层/普通子目录同样只打开它自己(进入目录成本 = 该目录一层)。
*
* 编译(在 tuku/ 下):
* gcc -Wall -Wextra -O1 -I. -I../sdk -DMR_LIBRARY_TEST_HOOKS \
* test/test_browse_perf.c logic/library.c logic/reader.c test/settings_reader_stub.c \
* -pthread -o build/host/test_browse_perf
* test/test_browse_perf.c logic/library.c -pthread -o build/host/test_browse_perf
*/
#include "logic/library.h"
@@ -26,6 +25,7 @@
#define PERF_DIRS 500
#define PERF_DEPTH 8
#define PERF_FILES 2000
#define PERF_BIG 2000
static int g_pass, g_fail;
#define CHECK(cond, ...) do { \
@@ -92,7 +92,7 @@ int main(void)
if (!root) return 1;
char p[MR_PATH_MAX];
/* 根目录:2000 个文件 + 500 个顶层目录(每个顶层目录内是 8 层嵌套 + 1 张图)。 */
/* 根目录:2000 个普通文件(不成行)+ 500 个顶层目录(每个顶层目录内是 8 层嵌套 + 1 张图)。 */
for (int f = 0; f < PERF_FILES; f++) {
snprintf(p, sizeof p, "%s/f%04d.txt", root, f);
if (touch_file(p) != 0) { CHECK(0, "file %s", p); break; }
@@ -101,7 +101,7 @@ int main(void)
char deep[MR_PATH_MAX];
snprintf(deep, sizeof deep, "%s/d%04d", root, d);
if (mkdir_p(deep) != 0) { CHECK(0, "mkdir %s", deep); break; }
snprintf(p, sizeof p, "%s/001.png", deep); /* 顶层目录自身直接含图(=一本书) */
snprintf(p, sizeof p, "%s/001.png", deep);
if (touch_file(p) != 0) { CHECK(0, "touched top image"); break; }
for (int l = 1; l <= PERF_DEPTH; l++) {
char sub[MR_PATH_MAX];
@@ -113,6 +113,15 @@ int main(void)
if (touch_file(p) != 0) { CHECK(0, "touched deep image"); break; }
}
/* 2000 项大目录(图片)。 */
char big[MR_PATH_MAX];
snprintf(big, sizeof big, "%s/big", root);
CHECK(mkdir_p(big) == 0, "mkdir big");
for (int i = 0; i < PERF_BIG; i++) {
snprintf(p, sizeof p, "%s/img%04d.png", big, i);
if (touch_file(p) != 0) { CHECK(0, "big image %s", p); break; }
}
/* ---- P1:首帧扫描只做单层 ---- */
mr_library lib;
memset(&lib, 0, sizeof lib);
@@ -123,61 +132,67 @@ int main(void)
mr_library_io_stats st;
mr_library_test_io_get(&st);
CHECK(n == PERF_DIRS, "scan lists %d dirs (got %d)", PERF_DIRS, n);
CHECK(n == PERF_DIRS + 1, "scan lists %d dirs (got %d)", PERF_DIRS + 1, n);
CHECK(lib.truncated == 0, "not truncated");
CHECK(st.opendirs == 1, "single-layer scan: opendir=%ld (want 1)", st.opendirs);
CHECK(st.lstats == PERF_FILES + PERF_DIRS,
"lstat only current level: %ld (want %d)", st.lstats, PERF_FILES + PERF_DIRS);
CHECK(st.lstats == PERF_FILES + PERF_DIRS + 1,
"lstat only current level: %ld (want %d)", st.lstats, PERF_FILES + PERF_DIRS + 1);
CHECK(scan_us < 1500000LL, "scan bounded: %lldus", scan_us);
if (lib.count > 1) {
CHECK(strcmp(lib.items[0].name, "d0000") == 0, "first=%s", lib.items[0].name);
if (lib.count == PERF_DIRS + 1) {
CHECK(strcmp(lib.items[0].name, "big") == 0, "first=%s", lib.items[0].name);
CHECK(strcmp(lib.items[lib.count - 1].name, "d0499") == 0,
"last=%s", lib.items[lib.count - 1].name);
}
printf(" scan: %lldus, %d dirs, opendir=%ld lstat=%ld\n",
printf(" root scan: %lldus, %d rows, opendir=%ld lstat=%ld\n",
scan_us, n, st.opendirs, st.lstats);
mr_library_free(&lib);
/* ---- P2:后台探测每目录只开一次,不进入嵌套子树 ---- */
/* ---- P2:2000 项大目录(进入目录 = 单层扫描) ---- */
mr_library biglib;
memset(&biglib, 0, sizeof biglib);
mr_library_test_io_reset();
long long p0 = now_us();
int bad = 0;
for (int i = 0; i < lib.count; i++) {
mr_library_probe probe;
if (mr_library_probe_cached(lib.items[i].path, &probe) != 0 || probe.pages != 1)
bad++;
}
long long probe_us = now_us() - p0;
long long b0 = now_us();
int bn = mr_library_scan(&biglib, big, MR_LIB_MAX_DEPTH);
long long big_us = now_us() - b0;
mr_library_test_io_get(&st);
CHECK(bad == 0, "all top dirs have 1 direct image (bad=%d)", bad);
CHECK(st.opendirs == PERF_DIRS,
"probe stays shallow: opendir=%ld (want %d, not %d)",
st.opendirs, PERF_DIRS, PERF_DIRS * (PERF_DEPTH + 1));
CHECK(st.lstats == PERF_DIRS,
"probe lstats only image candidates: %ld (want %d)", st.lstats, PERF_DIRS);
CHECK(probe_us < 5000000LL, "probe bounded: %lldus", probe_us);
printf(" probe: %lldus, opendir=%ld lstat=%ld\n", probe_us, st.opendirs, st.lstats);
CHECK(bn == PERF_BIG, "big dir rows=%d (want %d)", bn, PERF_BIG);
CHECK(biglib.truncated == 0, "big dir not truncated at cap");
CHECK(st.opendirs == 1, "big dir scan: opendir=%ld", st.opendirs);
CHECK(st.lstats == PERF_BIG, "big dir scan: lstat=%ld (want %d)", st.lstats, PERF_BIG);
CHECK(big_us < 1500000LL, "big dir scan bounded: %lldus", big_us);
if (biglib.count == PERF_BIG) {
CHECK(strcmp(biglib.items[0].name, "img0000.png") == 0, "first=%s", biglib.items[0].name);
CHECK(strcmp(biglib.items[PERF_BIG - 1].name, "img1999.png") == 0,
"last=%s", biglib.items[PERF_BIG - 1].name);
CHECK(biglib.items[PERF_BIG - 1].kind == MR_LIB_KIND_IMAGE, "last is image");
CHECK(mr_library_image_index(&biglib, PERF_BIG - 1) == PERF_BIG - 1,
"image index at end");
}
printf(" big dir scan: %lldus, %d rows, opendir=%ld lstat=%ld\n",
big_us, bn, st.opendirs, st.lstats);
/* ---- P3:行合成(bind/src 数据面)纯内存 O(1) ---- */
/* ---- P3:行合成(UI src 数据面)纯内存、2000 行 × 1000 遍 ---- */
mr_library_test_io_reset();
long long r0 = now_us();
int rows = 0;
for (int r = 0; r < 1000; r++)
for (int i = 0; i < lib.count; i++) {
for (int i = 0; i < biglib.count; i++) {
mr_library_row row;
if (mr_library_row_at(&lib, i, &row)) rows++;
if (mr_library_row_at(&biglib, i, &row)) rows++;
}
long long row_us = now_us() - r0;
mr_library_test_io_get(&st);
CHECK(rows == 1000 * lib.count, "rows synthesized (%d)", rows);
CHECK(rows == 1000 * biglib.count, "rows synthesized (%d)", rows);
CHECK(st.opendirs == 0 && st.readdirs == 0 && st.lstats == 0,
"row synthesis does no IO (open=%ld rd=%ld stat=%ld)",
st.opendirs, st.readdirs, st.lstats);
CHECK(row_us < 1000000LL, "row synthesis bounded: %lldus", row_us);
printf(" rows: %lldus for %d rows, zero IO\n", row_us, rows);
mr_library_free(&biglib);
/* ---- P4:直接扫描深层目录同样只打开它自己 ---- */
/* ---- P4:进入子目录只打开它自己(不递归子树) ---- */
char deep[MR_PATH_MAX];
snprintf(deep, sizeof deep, "%s", lib.items[0].path);
snprintf(deep, sizeof deep, "%s/d0000", root);
for (int l = 1; l <= PERF_DEPTH; l++) {
char sub[MR_PATH_MAX];
snprintf(sub, sizeof sub, "%s/l%d", deep, l);
@@ -186,13 +201,22 @@ int main(void)
mr_library_test_io_reset();
mr_library deep_lib;
memset(&deep_lib, 0, sizeof deep_lib);
CHECK(mr_library_scan(&deep_lib, deep, MR_LIB_MAX_DEPTH) == 0, "deep leaf has no subdir");
CHECK(mr_library_scan(&deep_lib, deep, MR_LIB_MAX_DEPTH) == 1, "deep leaf has one image");
mr_library_test_io_get(&st);
CHECK(st.opendirs == 1, "deep scan opens once (got %ld)", st.opendirs);
CHECK(st.lstats == 1, "deep scan lstats only its image (got %ld)", st.lstats);
mr_library_free(&deep_lib);
mr_library_free(&lib);
/* 顶层目录:一个子目录 + 一张图 = 2 行,只打开它自己。 */
snprintf(deep, sizeof deep, "%s/d0000", root);
memset(&deep_lib, 0, sizeof deep_lib);
mr_library_test_io_reset();
CHECK(mr_library_scan(&deep_lib, deep, MR_LIB_MAX_DEPTH) == 2, "top dir rows");
mr_library_test_io_get(&st);
CHECK(st.opendirs == 1 && st.lstats == 2, "top dir scan: open=%ld stat=%ld",
st.opendirs, st.lstats);
mr_library_free(&deep_lib);
rm_rf(root);
printf("%s: %d passed, %d failed\n", g_fail ? "FAIL" : "PASS", g_pass, g_fail);
return g_fail ? 1 : 0;
+222 -195
View File
@@ -1,33 +1,35 @@
/* test_browse_ui_host.c — 书库浏览页宿主集成测试(可选,需 SDK 源码树)。
*
* 目的(M13):
* U1 列表用 src2 模式(每帧重取行):扫描后行副标题是占位「…」,后台探测完成、
* on_tick 补位后同一行立即显示页数——验证懒算结果能上屏(固定池 vsource 做不到);
* U2 行语义:书行显示 "N pages"(无进入箭头),空目录「空目录」,无图目录「无图片」;
* U3 导航:可逐级上溯父目录;文件系统 "/" 处 library_go_up 返回 0(app.c 据此退出);
* 「TF卡」回根动作回到书库根;
* U4 不可读目录:load 失败 → 停留原目录 + 顶部提示横幅;
* U5 启动根不可读:create 时仍建空列表(不是只有横幅);
* U6 缩略图 LRU 跳过解压页缓存(M11 P2-1 回归):cache_prune 只统计/删除缩略图,
* mrpage_*(page_cache 的解压页)必须保留且不计入 64 张额度。
* 目的(M18 文件管理器模型):
* U1 行模型:目录 + 图片 + 档案册混排(目录优先+自然序),行副标题是扫描期生成的
* 类型·大小(不再有占位/后台补位),图标/右侧箭头按类型区分;
* U2 行合成零 IO:反复取可见行不触发 opendir/readdir/lstat(渲染期无文件访问);
* U3 点击语义:点目录=进入(空目录也进);点图片=从该图开始读
* (mr_reader_open_dir_at(当前目录, 图片下标));点档案册=打开档案册;
* U4 根目录锁定:根不显示顶栏返回、library_go_up 在根返回 0(app.c 退出);
* 子目录返回=回根;不存在越过浏览根的导航;
* U5 无 SD 卡容错:浏览根不可访问(不存在/不是目录)→ 友好空态「未检测到 SD 卡」,
* 不弹错误横幅;顶栏刷新/重扫可重试成功;
* U6 子目录不可读:停留原目录 + 顶部提示横幅;
* U7 书库不再碰缓存目录:CBZ 解压页缓存 mrpage_* 原样保留,且不写任何 bmp。
*
* 编译:见 test/run_tests.sh(与 test_settings_lock_host 同口径:链 SDK 源码树 + 打桩;
* 只发布 meow_ui.a 的交付环境自动 SKIP)。ui/library/library.c 以
* -DMR_LIBRARY_UI_TEST_HOOKS 编译出测试接缝;本测试另打桩 mr_reader_open_dir/mr_nav_report。
* 编译:见 test/run_tests.sh(链 SDK 源码树 + 打桩;只发布 meow_ui.a 的环境自动 SKIP)。
* ui/library/library.c 以 -DMR_LIBRARY_UI_TEST_HOOKS 编译出测试接缝;logic/library.c
* 以 -DMR_LIBRARY_TEST_HOOKS 编译出 IO 计数。
*/
#include "tuku_internal.h"
#include "logic/library.h"
#include "logic/reader/page_cache.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <dirent.h>
/* ---- ui/library/library.c 的测试接缝(仅 -DMR_LIBRARY_UI_TEST_HOOKS 时定义) ---- */
/* ui/library/library.c 的测试接缝(仅 -DMR_LIBRARY_UI_TEST_HOOKS 时定义) */
int library_test_load(const char *dir);
void library_test_set_root(const char *root);
const char *library_test_root(void);
@@ -37,14 +39,26 @@ int library_test_rows(void);
int library_test_src(int idx, meow_list_item *item);
int library_test_hint_active(void);
const char *library_test_hint(void);
int library_test_go_root(void);
int library_test_topbar_back(void);
const char *library_test_empty_text(void);
int library_test_sd_missing(void);
int library_test_retry(void);
int library_test_go_up(void);
int library_test_go_parent(void);
int library_test_drag_active(void);
int library_test_has_list(void);
const char *library_test_cache_dir(void);
int library_test_cache_on(void);
void library_test_cache_prune(void);
/* 阅读器入口打桩:只记录被打开的册路径与起始页(本测试不建 reader 页)。 */
#define MR_READER_START_RESUME (-1)
void mr_reader_open_dir_at(const char *dir, int start_index);
static char g_opened[MR_PATH_MAX];
static int g_opened_start;
static int g_opened_n;
void mr_reader_open_dir_at(const char *dir, int start_index)
{
snprintf(g_opened, sizeof g_opened, "%s", dir ? dir : "");
g_opened_start = start_index;
g_opened_n++;
}
static int g_fail;
#define CHECK(cond, ...) do { \
@@ -55,15 +69,6 @@ static int g_fail;
/* app.c 的导航上报在测试里不需要网络侧实现。 */
void mr_nav_report(void) {}
/* reader 打桩:只记录被打开的册路径(本测试不建 reader 页)。 */
static char g_opened[MR_PATH_MAX];
static int g_opened_n;
void mr_reader_open_dir(const char *path)
{
snprintf(g_opened, sizeof g_opened, "%s", path ? path : "");
g_opened_n++;
}
static int mkdir_p(const char *path)
{
char tmp[MR_PATH_MAX];
@@ -125,16 +130,15 @@ static int row_index_for(const char *name)
return -1;
}
static void set_mtime(const char *path, time_t t)
static void tap_row(const char *name)
{
struct timespec ts[2];
ts[0].tv_sec = t;
ts[0].tv_nsec = 0;
ts[1] = ts[0];
utimensat(AT_FDCWD, path, ts, 0);
int idx = row_index_for(name);
if (idx < 0) { CHECK(0, "row %s not found", name); return; }
int list_top = meow_ui_statusbar_height() + 56;
library_on_tap(120, list_top + idx * 56 + 28);
}
/* 统计缓存目录里的缩略图(*.bmp 且非 mrpage_*)与解压页(mrpage_*.bmp)。 */
/* 统计缓存目录里的 bmp(缩略图/解压页)。 */
static int count_bmp(const char *dir, int *pages)
{
DIR *d = opendir(dir);
@@ -144,7 +148,7 @@ static int count_bmp(const char *dir, int *pages)
while ((de = readdir(d)) != NULL) {
size_t len = strlen(de->d_name);
if (len <= 4 || strcmp(de->d_name + len - 4, ".bmp") != 0) continue;
if (strncmp(de->d_name, "mrpage_", 7) == 0) pg++;
if (mr_cache_is_page_name(de->d_name)) pg++;
else thumbs++;
}
closedir(d);
@@ -152,71 +156,6 @@ static int count_bmp(const char *dir, int *pages)
return thumbs;
}
static void wait_ready(int ms)
{
for (int t = 0; t < ms / 10; t++) {
library_on_tick();
int ready = 1;
int rows = library_test_rows();
for (int i = 0; i < rows; i++) {
meow_list_item it;
if (!library_test_src(i, &it)) continue;
if (it.subtitle && strcmp(it.subtitle, "…") == 0) { ready = 0; break; }
}
if (ready) return;
usleep(10000);
}
}
/* U6:缩略图 LRU 必须跳过解压页缓存(M11 P2-1 回归;MR_CACHE_MAX_FILES=64)。 */
static void test_cache_prune_skips_pages(void)
{
const char *dir = library_test_cache_dir();
CHECK(library_test_cache_on() && dir && dir[0], "thumbnail cache enabled: %s",
dir ? dir : "");
if (!dir || !dir[0]) return;
/* 清空之前可能写入的 bmp(本测试独占 MEOW_DATA_DIR)。 */
DIR *d = opendir(dir);
if (d) {
struct dirent *de;
while ((de = readdir(d)) != NULL) {
size_t len = strlen(de->d_name);
if (len > 4 && strcmp(de->d_name + len - 4, ".bmp") == 0) {
char full[MR_PATH_MAX + 96];
snprintf(full, sizeof full, "%s/%s", dir, de->d_name);
unlink(full);
}
}
closedir(d);
}
char page[MR_PATH_MAX + 96], thumb[MR_PATH_MAX + 96];
snprintf(page, sizeof page, "%s/mrpage_0123456789abcdef.bmp", dir);
CHECK(touch_file(page) == 0, "create page cache file");
set_mtime(page, time(NULL) - 7200); /* 最旧:若被误删一定先删它 */
time_t base = time(NULL) - 3600;
for (int i = 0; i < 65; i++) { /* 65 张缩略图 > MR_CACHE_MAX_FILES */
snprintf(thumb, sizeof thumb, "%s/%016x_%ld_%d.bmp", dir,
(unsigned)i, (long)(base + i), i);
CHECK(touch_file(thumb) == 0, "create thumbnail %d", i);
set_mtime(thumb, base + i);
}
int pages_before = 0;
CHECK(count_bmp(dir, &pages_before) == 65, "precondition 65 thumbnails");
CHECK(pages_before == 1, "precondition 1 page (got %d)", pages_before);
library_test_cache_prune();
int pages_after = 0;
int thumbs_after = count_bmp(dir, &pages_after);
CHECK(access(page, R_OK) == 0, "mrpage_* survives thumbnail LRU");
CHECK(pages_after == 1, "mrpage_* not counted as thumbnail (got %d)", pages_after);
CHECK(thumbs_after == 64, "thumbnails trimmed to 64 (got %d)", thumbs_after);
}
int main(void)
{
const char *base = getenv("TMPDIR");
@@ -227,129 +166,192 @@ int main(void)
if (!data) { printf("SKIP: mkdtemp failed\n"); return 0; }
setenv("MEOW_DATA_DIR", data, 1);
char root[MR_PATH_MAX];
char root[MR_PATH_MAX], p[MR_PATH_MAX];
snprintf(root, sizeof root, "%s/library", data);
char p[MR_PATH_MAX];
const char *dirs[] = { "book", "empty", "noimg", "deep/sub", "MeowOS/sub" };
const char *dirs[] = { "album", "empty", "noimg", "deep/sub", "Pictures", "Pictures/sub" };
for (unsigned i = 0; i < sizeof dirs / sizeof dirs[0]; i++) {
joinp(p, sizeof p, root, dirs[i]);
if (mkdir_p(p) != 0) { printf("SKIP: mkdir %s failed\n", p); return 0; }
}
joinp(p, sizeof p, root, "book/page2.png"); touch_file(p);
joinp(p, sizeof p, root, "book/page10.jpg"); touch_file(p);
joinp(p, sizeof p, root, "album/001.png"); touch_file(p);
joinp(p, sizeof p, root, "album/002.png"); touch_file(p);
joinp(p, sizeof p, root, "noimg/readme.txt"); touch_file(p);
joinp(p, sizeof p, root, "deep/sub/001.png"); touch_file(p);
joinp(p, sizeof p, root, "MeowOS/sub/m.png"); touch_file(p);
joinp(p, sizeof p, root, "root.png"); touch_file(p);
joinp(p, sizeof p, root, "book.cbz"); touch_file(p);
/* Pictures:大小写混排 + 数字序号 + 大写扩展名 + 档案册(图片排序契约夹具)。 */
const char *pics[] = { "A.jpg", "a.jpg", "img2.png", "IMG3.JPG", "img10.png",
"page01.bmp", "page1.jpeg", "cover.cbz", "notes.txt" };
for (unsigned i = 0; i < sizeof pics / sizeof pics[0]; i++) {
joinp(p, sizeof p, root, "Pictures");
size_t dl = strlen(p);
snprintf(p + dl, sizeof p - dl, "/%s", pics[i]);
touch_file(p);
}
meow_ui_opts o = { 480, 720 };
if (meow_ui_init(&o) != 0) { printf("SKIP: meow_ui_init failed (no host backend)\n"); return 0; }
/* U5:先指向一个不存在的根,再 create —— 真实走「启动时根目录不可读」分支。 */
const char *bad_root = "/no/such/tuku/m13_root";
/* ---- U5:浏览根不可访问 —— 先指向不存在的根,再 create ---- */
const char *bad_root = "/no/such/tuku/m18_root";
library_test_set_root(bad_root);
CHECK(strcmp(library_test_root(), bad_root) == 0, "bad root installed");
meow_view *lib = library_view_create();
CHECK(library_test_hint_active(), "startup unreadable root shows hint: %s",
library_test_hint());
CHECK(library_test_rows() == 0, "startup empty list rows=%d", library_test_rows());
CHECK(library_test_has_list(), "startup built listview for empty list");
CHECK(library_test_sd_missing() == 1, "unreadable root -> SD missing state");
CHECK(library_test_rows() == 0, "empty list rows=%d", library_test_rows());
CHECK(library_test_has_list(), "startup still builds a listview");
CHECK(strcmp(library_test_dir(), bad_root) == 0, "startup dir=%s", library_test_dir());
CHECK(strstr(library_test_empty_text(), "SD") != NULL,
"friendly empty state: %s", library_test_empty_text());
CHECK(library_test_hint_active() == 0, "no error banner for missing SD");
CHECK(library_test_topbar_back() == 0, "no back arrow at storage root");
CHECK(library_test_retry() == 0 && library_test_sd_missing() == 1,
"retry while still missing stays in empty state (no crash)");
/* 根路径不是目录(普通文件)同样走 SD 空态。 */
char notadir[MR_PATH_MAX];
joinp(notadir, sizeof notadir, data, "notadir");
touch_file(notadir);
library_test_set_root(notadir);
library_test_set_dir(notadir);
CHECK(library_test_retry() == 0 && library_test_sd_missing() == 1,
"root pointing at a file -> SD missing state");
/* 插卡/挂载恢复:同一路径变成可读目录后,刷新即恢复列表。 */
library_test_set_root(root);
CHECK(strcmp(library_test_root(), root) == 0, "test root installed");
library_test_set_dir(root);
CHECK(library_test_retry() > 0, "retry after storage available loads rows");
CHECK(library_test_sd_missing() == 0, "SD missing state cleared");
CHECK(strcmp(library_test_dir(), root) == 0, "now browsing root");
CHECK(library_test_topbar_back() == 0, "still no back arrow at root");
meow_view_page_push(ACT_LIBRARY);
CHECK(meow_ui_activity_active() == ACT_LIBRARY, "library active");
/* ---- U1:src2 行 + 懒算补位 ---- */
CHECK(library_test_load(root) == 1, "load fixture");
CHECK(library_test_rows() == 5, "5 rows (got %d)", library_test_rows());
static const char *want[5] = { "book", "deep", "empty", "MeowOS", "noimg" };
for (int i = 0; i < 5; i++) {
/* ---- U1:行模型(目录优先 + 自然序 + 类型图标/箭头) ---- */
CHECK(library_test_load(root) == 1, "load fixture root");
CHECK(library_test_rows() == 7, "7 rows (got %d)", library_test_rows());
static const char *want[7] = {
"album", "deep", "empty", "noimg", "Pictures", "book.cbz", "root.png"
};
for (int i = 0; i < 7; i++) {
meow_list_item it;
CHECK(library_test_src(i, &it) == 1, "src row %d", i);
CHECK(it.title && strcmp(it.title, want[i]) == 0, "row[%d]=%s (want %s)",
i, it.title ? it.title : "(null)", want[i]);
CHECK(it.subtitle && strcmp(it.subtitle, "…") == 0,
"row[%d] starts with placeholder (got %s)", i, it.subtitle ? it.subtitle : "");
CHECK(it.right == MEOW_LIST_RIGHT_ARROW, "row[%d] has enter arrow while pending", i);
CHECK(it.subtitle && it.subtitle[0] && strcmp(it.subtitle, "…") != 0,
"row[%d] subtitle pre-generated (%s)", i, it.subtitle ? it.subtitle : "");
if (i < 5) {
CHECK(it.icon_kind == MEOW_LIST_ICON_FOLDER && it.right == MEOW_LIST_RIGHT_ARROW,
"row[%d] is a folder row with enter arrow", i);
} else {
CHECK(it.icon_kind == MEOW_LIST_ICON_FILE && it.right == MEOW_LIST_RIGHT_NONE,
"row[%d] is a file row without child arrow", i);
}
}
wait_ready(3000);
/* U2:补位后的行语义 */
for (int i = 0; i < 5; i++) {
meow_list_item it;
library_test_src(i, &it);
CHECK(it.subtitle && strcmp(it.subtitle, "…") != 0,
"row[%d] subtitle filled (got %s)", i, it.subtitle ? it.subtitle : "");
}
int ib = row_index_for("book");
int ie = row_index_for("empty");
int in = row_index_for("noimg");
int id = row_index_for("deep");
int ic = row_index_for("book.cbz");
meow_list_item it;
CHECK(ib >= 0 && library_test_src(ib, &it) && strcmp(it.subtitle, "2 pages") == 0,
"book subtitle=%s", it.subtitle ? it.subtitle : "");
CHECK(it.right == MEOW_LIST_RIGHT_NONE, "book row has no enter arrow (opens reader)");
CHECK(ie >= 0 && library_test_src(ie, &it) && strcmp(it.subtitle, "空目录") == 0,
"empty subtitle=%s", it.subtitle ? it.subtitle : "");
CHECK(in >= 0 && library_test_src(in, &it) && strcmp(it.subtitle, "无图片") == 0,
"noimg subtitle=%s", it.subtitle ? it.subtitle : "");
CHECK(id >= 0 && library_test_src(id, &it) && strcmp(it.subtitle, "无图片") == 0,
"deep (images only in subdir) is not a book: %s", it.subtitle ? it.subtitle : "");
CHECK(ic >= 0 && library_test_src(ic, &it) &&
strncmp(it.subtitle, "档案册 · ", strlen("档案册 · ")) == 0,
"archive subtitle=%s", it.subtitle ? it.subtitle : "");
int ir = row_index_for("root.png");
CHECK(ir >= 0 && library_test_src(ir, &it) &&
strncmp(it.subtitle, "图片 · ", strlen("图片 · ")) == 0,
"image subtitle=%s", it.subtitle ? it.subtitle : "");
/* ---- 行点击:书 → reader;空目录 → 进入 ---- */
int list_top = meow_ui_statusbar_height() + 56;
/* ---- U2:行合成零 IO(渲染/滚动路径不访问文件系统) ---- */
mr_library_test_io_reset();
for (int pass = 0; pass < 500; pass++)
for (int i = 0; i < library_test_rows(); i++) {
meow_list_item tmp;
library_test_src(i, &tmp);
}
mr_library_io_stats io;
mr_library_test_io_get(&io);
CHECK(io.opendirs == 0 && io.readdirs == 0 && io.lstats == 0,
"row source does no IO (open=%ld rd=%ld stat=%ld)",
io.opendirs, io.readdirs, io.lstats);
/* ---- U3:点击语义 ---- */
char pics_dir[MR_PATH_MAX];
joinp(pics_dir, sizeof pics_dir, root, "Pictures");
CHECK(library_test_load(pics_dir) == 1, "entered Pictures");
CHECK(strcmp(library_test_dir(), pics_dir) == 0, "current dir=%s", library_test_dir());
/* 图片下标 = 该目录里排在该图之前的图片行数(阅读器目录册同序)。 */
const char *target = "img10.png";
int ti = row_index_for(target);
CHECK(ti >= 0, "target image row present");
int expect_idx = 0;
for (int i = 0; i < ti; i++) {
meow_list_item tmp;
if (!library_test_src(i, &tmp)) continue;
if (tmp.subtitle && strncmp(tmp.subtitle, "图片 · ", strlen("图片 · ")) == 0)
expect_idx++;
}
g_opened_n = 0;
library_on_tap(120, list_top + ib * 56 + 28);
CHECK(g_opened_n == 1 && strstr(g_opened, "/book") != NULL,
"tap book opens reader (%s)", g_opened);
CHECK(strcmp(library_test_dir(), root) == 0, "tap book does not navigate");
tap_row(target);
CHECK(g_opened_n == 1 && strcmp(g_opened, pics_dir) == 0,
"tap image opens reader for current dir (%s)", g_opened);
CHECK(g_opened_start == expect_idx, "start index=%d (want %d)", g_opened_start, expect_idx);
CHECK(strcmp(library_test_dir(), pics_dir) == 0, "tap image does not navigate");
library_on_tap(120, list_top + ie * 56 + 28);
char empty_path[MR_PATH_MAX];
joinp(empty_path, sizeof empty_path, root, "empty");
CHECK(strcmp(library_test_dir(), empty_path) == 0,
"tap empty dir enters it (%s)", library_test_dir());
g_opened_n = 0;
tap_row("cover.cbz");
char archive[MR_PATH_MAX];
joinp(archive, sizeof archive, pics_dir, "cover.cbz");
CHECK(g_opened_n == 1 && strcmp(g_opened, archive) == 0,
"tap archive opens archive book (%s)", g_opened);
CHECK(g_opened_start == MR_READER_START_RESUME, "archive uses resume start (%d)",
g_opened_start);
/* 点目录 = 进入;空目录也进(不打开阅读器)。 */
char sub_dir[MR_PATH_MAX];
joinp(sub_dir, sizeof sub_dir, pics_dir, "sub");
g_opened_n = 0;
tap_row("sub");
CHECK(g_opened_n == 0, "tap dir does not open reader");
CHECK(strcmp(library_test_dir(), sub_dir) == 0, "entered empty sub dir (%s)",
library_test_dir());
CHECK(library_test_rows() == 0, "empty dir shows empty list");
CHECK(strcmp(library_test_empty_text(), "空文件夹") == 0,
"empty dir state: %s", library_test_empty_text());
/* ---- U3:导航(逐级上溯 / 到 / 退出 / 回 TF 根) ---- */
CHECK(library_test_go_up() == 1, "go_up from empty consumed");
CHECK(strcmp(library_test_dir(), root) == 0, "go_up to fixture root");
/* ---- U4:根目录锁定 ---- */
char album_dir[MR_PATH_MAX];
joinp(album_dir, sizeof album_dir, root, "album");
CHECK(library_test_load(album_dir) == 1, "load album");
CHECK(library_test_topbar_back() == 1, "back arrow shown in subdir");
CHECK(library_test_go_up() == 1, "go_up from subdir consumed");
CHECK(strcmp(library_test_dir(), root) == 0, "back to root");
char deep[MR_PATH_MAX];
joinp(deep, sizeof deep, root, "deep/sub");
CHECK(library_test_load(deep) == 1, "load deep/sub");
CHECK(library_test_go_up() == 1, "go_up from deep/sub");
joinp(p, sizeof p, root, "deep");
CHECK(strcmp(library_test_dir(), p) == 0, "now at deep");
/* 根:系统返回 = 退出(返回 0),并保持列表不变。 */
CHECK(library_test_go_up() == 0, "go_up at root returns 0 (app exits)");
CHECK(strcmp(library_test_dir(), root) == 0, "still at root after exit gesture");
CHECK(library_test_topbar_back() == 0, "no back arrow at root");
/* 「TF卡」回根 */
CHECK(library_test_load(deep) == 1, "reload deep/sub");
CHECK(library_test_go_root() == 1 && strcmp(library_test_dir(), root) == 0,
"TF卡 action returns to library root");
/* 顶栏返回:子目录点击返回箭头 = 回根;根上该区域不响应。 */
CHECK(library_test_load(album_dir) == 1, "load album again");
library_on_tap(20, meow_ui_statusbar_height() + 28);
CHECK(strcmp(library_test_dir(), root) == 0, "topbar back returns to root");
int root_rows_before = library_test_rows();
library_on_tap(20, meow_ui_statusbar_height() + 28);
CHECK(strcmp(library_test_dir(), root) == 0 && library_test_rows() == root_rows_before,
"topbar back is inert at root");
/* 上溯到文件系统 /:go_up 在 / 返回 0(app.c 据此退出)。
* 宿主(Termux)读不了 "/":从 /usr 返回时 load("/") 失败,仍应切到 "/" 的空列表
* (否则会卡在 /usr 无法退出),下一次返回=退出 App。 */
library_test_set_dir("/usr");
CHECK(library_test_go_up() == 1, "go_up toward / consumed");
CHECK(strcmp(library_test_dir(), "/") == 0, "reached / (unreadable root fallback)");
CHECK(library_test_go_up() == 0, "go_up at / returns 0 (app exits)");
CHECK(strcmp(library_test_dir(), "/") == 0, "still at / after exit gesture");
/* 嵌套:deep/sub 两级返回。 */
tap_row("deep");
tap_row("sub");
char deep_sub[MR_PATH_MAX], deep_dir[MR_PATH_MAX];
joinp(deep_dir, sizeof deep_dir, root, "deep");
joinp(deep_sub, sizeof deep_sub, root, "deep/sub");
CHECK(strcmp(library_test_dir(), deep_sub) == 0, "entered deep/sub (%s)", library_test_dir());
CHECK(library_test_go_up() == 1 && strcmp(library_test_dir(), deep_dir) == 0, "up to deep");
CHECK(library_test_go_up() == 1 && strcmp(library_test_dir(), root) == 0, "up to root");
/* ---- U3b:系统返回语义 = TF 根退出 App;顶栏「上级」保留越界浏览 ---- */
CHECK(library_test_load(root) == 1, "load fixture root");
CHECK(library_test_go_up() == 0, "go_up at TF root returns 0 (app exits)");
CHECK(strcmp(library_test_dir(), root) == 0, "still at TF root after exit gesture");
/* 顶栏显式「上级」:访问放开,TF 根 → 其父目录(fixture root 的 parent = data)。 */
CHECK(library_test_go_parent() == 1, "explicit up from TF root consumed");
CHECK(strcmp(library_test_dir(), data) == 0,
"explicit up goes above TF root (%s)", library_test_dir());
CHECK(library_test_load(root) == 1, "back to fixture root");
/* ---- U3c:左缘 24px 系统返回手势带不启动列表拖拽 ---- */
/* ---- U3c:左缘 24px / 屏底热区不启动列表拖拽 ---- */
int list_top = meow_ui_statusbar_height() + 56;
int row_y = list_top + 28;
library_on_touch_down(120, row_y);
CHECK(library_test_drag_active() == 1, "touch inside list starts drag");
@@ -362,11 +364,10 @@ int main(void)
library_on_touch_down(120, meow_ui_height() - 4);
CHECK(library_test_drag_active() == 0, "bottom hotzone does not start drag");
/* ---- U4:不可读目录 → 停留 + 提示 ---- */
CHECK(library_test_load(root) == 1, "back to fixture root");
char noread[MR_PATH_MAX];
joinp(noread, sizeof noread, root, "noread");
/* ---- U6:不可读子目录 → 停留 + 提示 ---- */
if (geteuid() != 0) {
char noread[MR_PATH_MAX];
joinp(noread, sizeof noread, root, "noread");
CHECK(mkdir(noread, 0755) == 0, "mkdir noread");
chmod(noread, 0000);
int ok = library_test_load(noread);
@@ -381,7 +382,33 @@ int main(void)
printf(" SKIP unreadable-dir case (running as root)\n");
}
test_cache_prune_skips_pages();
/* ---- U7:书库不碰缓存目录(CBZ 解压页缓存原样保留,不写 bmp) ---- */
CHECK(mr_cache_init(data) == 0, "page cache init");
const char *cdir = mr_cache_dir();
CHECK(cdir && cdir[0], "page cache dir: %s", cdir ? cdir : "");
if (cdir && cdir[0]) {
char page[MR_PATH_MAX + 96];
snprintf(page, sizeof page, "%s/mrpage_0123456789abcdef.bmp", cdir);
CHECK(touch_file(page) == 0, "create page cache file");
int pages_before = 0;
count_bmp(cdir, &pages_before);
CHECK(pages_before == 1, "precondition 1 page (got %d)", pages_before);
CHECK(library_test_load(root) == 1, "reload root");
for (int i = 0; i < library_test_rows(); i++) {
meow_list_item tmp;
library_test_src(i, &tmp);
}
tap_row("album");
tap_row("001.png"); /* 打开阅读器入口(打桩) */
library_test_go_up();
int pages_after = 0;
int thumbs_after = count_bmp(cdir, &pages_after);
CHECK(access(page, R_OK) == 0, "mrpage_* survives book browsing");
CHECK(pages_after == 1 && thumbs_after == 0,
"library writes no bmp to cache (pages=%d thumbs=%d)", pages_after, thumbs_after);
}
meow_view_free(lib);
meow_ui_shutdown();
+32 -94
View File
@@ -15,13 +15,12 @@
* T6 坏档容错:非 zip/截断/无图片页/声明 96MiB 的 zip 炸弹 → 打开失败或 0 页,不崩。
* T7 统一开册契约:目录册与档案册走同一个 mr_book(count/meta/open/close/destroy),
* 越界 open 返回 NULL,close_auto 幂等。
* T8 书库列档(M13 单层 + 懒判定):.cbz 与文件夹册同列自然序(bookA < box < ... <
* Vol10.cbz);扫描期只成行、副标题占位,探测补位后才有页数/书标记;
* 坏档/无图片页档案仍列出但为非书(访问放开,不再隐藏);
* 不注入探测回调时档案文件不成行(M7 语义),目录仍按单层列出。
* T8 书库列档(M18 文件管理器模型):目录 + .cbz/.zip 同列(目录优先、组内自然序,
* bookA < box < … < Vol10.cbz);扫描期一次成行,副标题是「文件夹/档案册 · 大小」,
* 不再探测页数/封面/续读角标;坏档/无图片页档案也照常列出(点开时才由 reader 判)。
* T9 册间衔接:同父目录自然序混排(bookA → vol2.cbz → Vol10.cbz),
* 首/末册返回 0;不注入探测回调时档案册不参与(M7 语义)。
* T10 续读角标(M13 两段式):档案路径作为册标识,probe 补位后副标题后缀「· 续读 Pn」。
* T10 封面解压 + 无续读角标:档案首图仍可解到缓存;书库行不再带「· 续读 Pn」。
*
* 编译(在 tuku/ 下):
* gcc -Wall -Wextra -Werror -O1 -I. test/test_cbz.c logic/library.c logic/reader.c \
@@ -716,28 +715,15 @@ static const mr_library_entry *lib_find(const mr_library *lib, const char *name)
return NULL;
}
/* M13:模拟 UI 后台探测补位(目录单层探测 / 档案册注入页数回调)。 */
static void lib_probe_all(mr_library *lib)
{
for (int i = 0; i < lib->count; i++) {
mr_library_probe p;
if (mr_library_probe_path(lib->items[i].path, lib->items[i].is_archive,
archive_pages_probe, NULL, &p) == 0)
mr_library_apply_probe(lib, i, &p);
else
mr_library_mark_error(lib, i);
}
}
static void test_library(void)
{
mr_test_cfg_clear();
/* M13:扫描只做单层枚举——目录(全部列出,含无图的 box)与档案文件(.cbz/.zip)
* 都先成行、副标题占位;书/非书与页数由后台 probe 补位。 */
/* 文件管理器模型:目录 + 档案册同列(目录优先、组内自然序),扫描期一次成行;
* 档案册只按扩展名成行,不探测页数/封面(打开时才由 reader 解档)。 */
mr_library lib;
memset(&lib, 0, sizeof lib);
int n = mr_library_scan_ex(&lib, g_lib, MR_LIB_MAX_DEPTH, archive_pages_probe, NULL);
int n = mr_library_scan(&lib, g_lib, MR_LIB_MAX_DEPTH);
CHECK(n == 8, "library rows=%d (want 2 dirs + 6 archives)", n);
static const char *want[8] = {
"bookA", "box", "broken.cbz", "emptypages.cbz",
@@ -746,68 +732,33 @@ static void test_library(void)
for (int i = 0; i < 8 && i < lib.count; i++) {
CHECK(strcmp(lib.items[i].name, want[i]) == 0,
"row[%d]=%s (want %s)", i, lib.items[i].name, want[i]);
CHECK(lib.items[i].probe_state == MR_LIB_PENDING && lib.items[i].pages == -1,
"%s pending after scan", lib.items[i].name);
CHECK(strcmp(lib.items[i].subtitle, "…") == 0,
"%s placeholder subtitle=%s", lib.items[i].name, lib.items[i].subtitle);
}
/* 后台补位(等价 UI 的 on_tick):目录走单层目录探测,档案走注入的 zip 页数回调。 */
for (int i = 0; i < lib.count; i++) {
mr_library_probe p;
int ok = (mr_library_probe_path(lib.items[i].path, lib.items[i].is_archive,
archive_pages_probe, NULL, &p) == 0);
CHECK(ok, "%s probed", lib.items[i].name);
if (ok) mr_library_apply_probe(&lib, i, &p);
}
for (int i = 0; i < 2 && i < lib.count; i++)
CHECK(lib.items[i].kind == MR_LIB_KIND_DIR &&
strcmp(lib.items[i].subtitle, "文件夹") == 0,
"%s dir row subtitle=%s", lib.items[i].name, lib.items[i].subtitle);
for (int i = 2; i < 8 && i < lib.count; i++)
CHECK(lib.items[i].kind == MR_LIB_KIND_ARCHIVE &&
strncmp(lib.items[i].subtitle, "档案册 · ", strlen("档案册 · ")) == 0,
"%s archive row subtitle=%s", lib.items[i].name, lib.items[i].subtitle);
const mr_library_entry *e = lib_find(&lib, "bookA");
CHECK(e && e->pages == 2 && e->has_images == 1 && e->subdirs == 0, "bookA fields");
CHECK(e && strcmp(e->subtitle, "2 pages") == 0, "bookA subtitle=%s",
e ? e->subtitle : "");
CHECK(e && strstr(e->cover, "001.png") != NULL, "bookA cover=%s",
e ? e->cover : "");
/* box 只含档案册:目录本身不直接含图 → 不是书(M13 不做递归/子表统计)。 */
e = lib_find(&lib, "box");
CHECK(e && e->has_images == 0 && e->subdirs == 0 && e->is_archive == 0,
"box fields");
CHECK(e && strcmp(e->subtitle, "无图片") == 0, "box subtitle=%s",
e ? e->subtitle : "");
CHECK(e && e->cover[0] == '\0', "box cover should be empty");
CHECK(e && e->kind == MR_LIB_KIND_DIR && e->size == 0, "bookA dir entry");
e = lib_find(&lib, "vol2.cbz");
CHECK(e && e->pages == 3 && e->has_images == 1 && e->subdirs == 0 &&
e->is_archive == 1, "vol2.cbz fields");
CHECK(e && strcmp(e->subtitle, "3 pages") == 0, "vol2.cbz subtitle=%s",
e ? e->subtitle : "");
CHECK(e && e->cover[0] == '\0', "archive cover should be empty (UI extracts)");
CHECK(e && strstr(e->path, "vol2.cbz") != NULL, "vol2.cbz path=%s",
e ? e->path : "");
CHECK(e && e->kind == MR_LIB_KIND_ARCHIVE && e->size > 0 &&
strstr(e->path, "vol2.cbz") != NULL,
"vol2.cbz archive entry (size=%lld)", e ? e->size : -1);
e = lib_find(&lib, "Vol10.cbz");
CHECK(e && e->pages == 1 && strcmp(e->subtitle, "1 page") == 0,
"Vol10.cbz pages/subtitle");
/* 坏档/无图片页档案:列出但探测为非书(M13 访问放开,不再隐藏)。 */
static const char *not_book[] = { "notabook.zip", "emptypages.cbz",
/* 坏档/无图片页档案也照常列出(书库不预判,点开时 reader 才判可读性)。 */
static const char *all_arch[] = { "notabook.zip", "emptypages.cbz",
"broken.cbz", "truncated.cbz" };
for (unsigned i = 0; i < sizeof not_book / sizeof not_book[0]; i++) {
e = lib_find(&lib, not_book[i]);
CHECK(e && e->is_archive == 1 && e->has_images == 0 && e->pages == 0 &&
strcmp(e->subtitle, "无图片") == 0,
"%s: not a book (subtitle=%s)", not_book[i], e ? e->subtitle : "(missing)");
for (unsigned i = 0; i < sizeof all_arch / sizeof all_arch[0]; i++) {
e = lib_find(&lib, all_arch[i]);
CHECK(e && e->kind == MR_LIB_KIND_ARCHIVE, "%s listed as archive", all_arch[i]);
}
CHECK(lib_find(&lib, ".hidden.cbz") == NULL, "hidden cbz not listed");
mr_library_free(&lib);
/* 不注入探测回调:M7 纯目录书库(档案文件不成行,目录仍按 M13 单层列出)。 */
memset(&lib, 0, sizeof lib);
CHECK(mr_library_scan(&lib, g_lib, MR_LIB_MAX_DEPTH) == 2,
"legacy scan rows=%d (want 2: bookA, box)", lib.count);
CHECK(lib.count >= 2 && strcmp(lib.items[0].name, "bookA") == 0 &&
strcmp(lib.items[1].name, "box") == 0, "legacy scan rows");
mr_library_free(&lib);
}
/* ============================================================ */
@@ -863,7 +814,7 @@ static void test_adjacent(void)
}
/* ============================================================ */
/* T10 续读角标 + 封面 */
/* T10 封面解压 + 无续读角标 */
/* ============================================================ */
static void test_badge_cover(void)
{
@@ -873,31 +824,18 @@ static void test_badge_cover(void)
mr_test_cfg_clear();
mr_cont_save(vol2, 2, MR_MODE_WEBTOON);
/* M13 两段式:扫描期只有占位副标题;后台 probe 补位后才出现页数与续读角标。 */
/* 文件管理器模型:书库行只有「档案册 · 大小」,不再有「· 续读 Pn」角标
* (续读仍由 reader 打开该册时恢复,见 test_mode_switch_host)。 */
mr_library lib;
memset(&lib, 0, sizeof lib);
CHECK(mr_library_scan_ex(&lib, g_lib, MR_LIB_MAX_DEPTH, archive_pages_probe,
NULL) == 8, "rescan for badge (single-layer rows)");
CHECK(mr_library_scan(&lib, g_lib, MR_LIB_MAX_DEPTH) == 8, "rescan rows");
const mr_library_entry *e = lib_find(&lib, "vol2.cbz");
CHECK(e && strcmp(e->subtitle, "…") == 0, "vol2 placeholder before probe: %s",
e ? e->subtitle : "");
lib_probe_all(&lib);
e = lib_find(&lib, "vol2.cbz");
CHECK(e && strstr(e->subtitle, "续读 P3") != NULL, "badge subtitle=%s",
e ? e->subtitle : "");
CHECK(e && strstr(e->subtitle, "3 pages") != NULL, "badge keeps page text: %s",
CHECK(e && strstr(e->subtitle, "续读") == NULL,
"no continuation badge in file-manager rows: %s", e ? e->subtitle : "");
CHECK(e && strstr(e->subtitle, "档案册") != NULL, "archive subtitle=%s",
e ? e->subtitle : "");
e = lib_find(&lib, "Vol10.cbz");
CHECK(e && strstr(e->subtitle, "续读") == NULL, "Vol10 should have no badge");
mr_library_free(&lib);
/* 清除记录后角标消失(重扫 + 再探测;探测缓存只存页数,角标按当前配置重算)。 */
mr_cont_save(vol2, 0, MR_MODE_WEBTOON);
memset(&lib, 0, sizeof lib);
mr_library_scan_ex(&lib, g_lib, MR_LIB_MAX_DEPTH, archive_pages_probe, NULL);
lib_probe_all(&lib);
e = lib_find(&lib, "vol2.cbz");
CHECK(e && strstr(e->subtitle, "续读") == NULL, "cleared badge gone");
CHECK(e && strstr(e->subtitle, "续读") == NULL, "Vol10 no badge either");
mr_library_free(&lib);
/* 封面 = 档内自然序首图(ch01/p2.png),解压到缓存 */
+22 -63
View File
@@ -6,8 +6,8 @@
* A3 边界:末页前进→下一册、首页后退→上一册(调用方语义);路径末尾斜杠等价;
* A4 排除:隐藏/系统目录、符号链接、普通文件不参与兄弟册;
* A5 参数:delta 非 ±1、NULL/小缓冲、不存在的目录;
* A6 书库扫描接线(M13 两段式):scan 只列单层、副标题占位「…」;
* probe/on_tick 补位后出现页数与「· 续读 Pn」角标。
* A6 书库行模型(M18):目录+图片+档案册同列、目录优先;副标题扫描期生成,
* 不再有「· 续读 Pn」角标(角标随「目录自动当书」链路删除,续读仍由 reader 恢复)。
*
* 编译(在 tuku/ 下):
* gcc -Wall -Wextra -O1 -I. test/test_cont_adjacent.c logic/library.c logic/reader.c \
@@ -159,84 +159,43 @@ static void test_adjacent(const char *root)
CHECK(mr_book_adjacent("/", 1, out, (int)sizeof out) == -1, "root path invalid");
}
/* 模拟 UI 后台探测:逐行单层 probe 并补位(page>0 的书行由 format_subtitle 拼续读角标)。 */
static void probe_all_rows(mr_library *lib)
{
for (int i = 0; i < lib->count; i++) {
mr_library_probe p;
if (mr_library_probe_cached(lib->items[i].path, &p) == 0)
mr_library_apply_probe(lib, i, &p);
else
mr_library_mark_error(lib, i);
}
}
/* A6 两段式(M13):scan 只做单层枚举、行副标题是占位「…」;后台 probe/on_tick 补位后
* 才有页数与「· 续读 Pn」角标。 */
static void test_library_badge(const char *root)
/* A6 文件管理器行模型:扫描即出最终行(无占位/无后台补位/无续读角标);
* 目录优先;当前层的图片行带图片下标(点图 → reader 起始页)。 */
static void test_library_rows(const char *root)
{
mr_test_cfg_clear();
char vol1[MR_PATH_MAX], vol2[MR_PATH_MAX];
char vol1[MR_PATH_MAX];
joinp(vol1, sizeof vol1, root, "vol1");
joinp(vol2, sizeof vol2, root, "vol2");
/* vol1 有续读记录(第 5 页),vol2 没有 */
/* vol1 有续读记录(第 5 页)——书库行不应出现角标。 */
mr_cont_save(vol1, 4, MR_MODE_WEBTOON);
mr_library lib;
memset(&lib, 0, sizeof lib);
int n = mr_library_scan(&lib, root, MR_LIB_MAX_DEPTH);
CHECK(n > 0, "library scan n=%d", n);
if (n > 0)
CHECK(lib.items[0].kind == MR_LIB_KIND_DIR, "dirs first: %s", lib.items[0].name);
/* 第一段:扫描期不判定,占位副标题。 */
int seen1 = 0, seen2 = 0;
int seen1 = 0, seen_img = 0;
for (int i = 0; i < lib.count; i++) {
mr_library_entry *e = &lib.items[i];
const mr_library_entry *e = &lib.items[i];
CHECK(e->subtitle[0] && strcmp(e->subtitle, "…") != 0,
"%s subtitle pre-generated: %s", e->name, e->subtitle);
CHECK(strstr(e->subtitle, "续读") == NULL, "%s has no badge: %s",
e->name, e->subtitle);
if (strcmp(e->name, "vol1") == 0) {
seen1 = 1;
CHECK(e->probe_state == MR_LIB_PENDING && e->pages == -1,
"vol1 pending after scan");
CHECK(strcmp(e->subtitle, "…") == 0, "vol1 placeholder subtitle=%s", e->subtitle);
CHECK(e->kind == MR_LIB_KIND_DIR, "vol1 is a dir row");
}
if (strcmp(e->name, "vol2") == 0) {
seen2 = 1;
CHECK(e->probe_state == MR_LIB_PENDING && strcmp(e->subtitle, "…") == 0,
"vol2 placeholder subtitle=%s", e->subtitle);
if (strcmp(e->name, "loose.png") == 0) {
seen_img = 1;
CHECK(e->kind == MR_LIB_KIND_IMAGE, "loose.png is an image row");
CHECK(mr_library_image_index(&lib, i) == 0, "loose.png is image #0");
}
}
CHECK(seen1 == 1, "vol1 listed");
CHECK(seen2 == 1, "vol2 listed");
/* 第二段:后台探测补位后,页数与续读角标出现。 */
probe_all_rows(&lib);
seen1 = seen2 = 0;
for (int i = 0; i < lib.count; i++) {
mr_library_entry *e = &lib.items[i];
if (strcmp(e->name, "vol1") == 0) {
seen1 = 1;
CHECK(e->has_images == 1 && e->pages == 1, "vol1 probed pages=%d", e->pages);
CHECK(strstr(e->subtitle, "page") != NULL, "vol1 keeps pages text: %s", e->subtitle);
CHECK(strstr(e->subtitle, "续读 P5") != NULL, "vol1 subtitle=%s", e->subtitle);
}
if (strcmp(e->name, "vol2") == 0) {
seen2 = 1;
CHECK(e->has_images == 1 && e->pages == 1, "vol2 probed pages=%d", e->pages);
CHECK(strstr(e->subtitle, "续读") == NULL, "vol2 no badge: %s", e->subtitle);
CHECK(strcmp(e->subtitle, "1 page") == 0, "vol2 subtitle=%s", e->subtitle);
}
}
CHECK(seen1 == 1, "vol1 listed after probe");
CHECK(seen2 == 1, "vol2 listed after probe");
mr_library_free(&lib);
/* 记录清除后角标消失(重扫 + 再探测;探测缓存只存页数,角标按当前配置重算)。 */
mr_cont_save(vol1, 0, MR_MODE_WEBTOON);
memset(&lib, 0, sizeof lib);
CHECK(mr_library_scan(&lib, root, MR_LIB_MAX_DEPTH) > 0, "rescan");
probe_all_rows(&lib);
for (int i = 0; i < lib.count; i++)
if (strcmp(lib.items[i].name, "vol1") == 0)
CHECK(strstr(lib.items[i].subtitle, "续读") == NULL, "cleared badge gone");
CHECK(seen_img == 1, "loose image listed");
mr_library_free(&lib);
}
@@ -280,7 +239,7 @@ int main(void)
test_has_images(root);
test_adjacent(root);
test_library_badge(root);
test_library_rows(root);
rm_rf(root);
printf("%s: %d passed, %d failed\n", g_fail ? "FAIL" : "PASS", g_pass, g_fail);
+84 -27
View File
@@ -1,11 +1,11 @@
/* test_library.c — 图库 logic 层宿主测试(纯 C,无 SDK/硬件)。
*
* 覆盖:
* T1 自然排序:数字段数值序(page2<page10)、前导零、数字/字母混合、大小写;
* T2 扩展名过滤:png/jpg/jpeg/bmp 大小写不敏感,gif/txt 不支持;
* T3 目录扫描(M13 单层语义):只列当前层直接子目录(自然升序),不递归探测子树;
* 系统目录/空目录/无图目录都列出;隐藏项/普通文件/符号链接不列;
* max_depth 兼容参数被忽略;行初始为「待探测」占位;父目录路径推导;
* 覆盖(M18 文件管理器模型):
* T1 自然排序:数字段数值序(page2<page10)、前导零、数字/字母混合、大小写确定性;
* T2 扩展名过滤:图片 png/jpg/jpeg/bmp 与档案册 cbz/zip 大小写不敏感;gif/txt 不支持;
* T3 单层扫描:目录 + 受支持图片 + 档案册混排(目录优先、组内自然序);
* 其它普通文件/隐藏项/符号链接不列;副标题扫描期生成一次(无占位/无懒判定);
* 图片行下标(mr_library_image_index)与行类型;不可读路径 -> -1;max_depth 忽略;
* T4 阅读方向状态(logic/reader.c)。
*
* 编译(在 tuku/ 下):
@@ -80,6 +80,13 @@ static const mr_library_entry *find_entry(const mr_library *lib, const char *nam
return NULL;
}
static int find_index(const mr_library *lib, const char *name)
{
for (int i = 0; i < lib->count; i++)
if (strcmp(lib->items[i].name, name) == 0) return i;
return -1;
}
static void test_natural(void)
{
CHECK(mr_natural_cmp("page2", "page10") < 0, "page2 < page10");
@@ -91,6 +98,7 @@ static void test_natural(void)
CHECK(mr_natural_cmp("a", "a1") < 0, "prefix shorter first");
CHECK(mr_natural_cmp("abc", "abc") == 0, "identical equal");
CHECK(mr_natural_cmp("ABC", "abc") != 0, "case-only tie is deterministic");
CHECK(mr_natural_cmp("a.jpg", "A.jpg") > 0, "case tie falls back to raw bytes");
CHECK(mr_natural_cmp("A2", "a10") < 0, "case-insensitive primary then numeric");
}
@@ -104,6 +112,10 @@ static void test_ext(void)
CHECK(mr_path_is_image("a.txt") == 0, "txt unsupported");
CHECK(mr_path_is_image("png") == 0, "no extension");
CHECK(mr_path_is_image(".png") == 0, "dotfile-ish no stem");
CHECK(mr_path_is_archive("a.cbz") == 1, "cbz");
CHECK(mr_path_is_archive("a.ZIP") == 1, "ZIP uppercase");
CHECK(mr_path_is_archive("a.7z") == 0, "7z unsupported");
CHECK(mr_path_is_archive("cbz") == 0, "no extension archive");
}
static void test_parent(void)
@@ -150,54 +162,99 @@ static void test_scan(void)
snprintf(p, sizeof p, "%s/MeowAPPs", root); mkdir_p(p);
snprintf(p, sizeof p, "%s/MeowAPPs/m.png", root); touch_file(p);
snprintf(p, sizeof p, "%s/root.png", root); touch_file(p); /* 普通文件不成为行 */
/* 当前层文件:图片与档案册都成行;txt/隐藏文件不成行。 */
snprintf(p, sizeof p, "%s/root.png", root); touch_file(p);
snprintf(p, sizeof p, "%s/cover.JPG", root); touch_file(p);
snprintf(p, sizeof p, "%s/book.cbz", root); touch_file(p);
snprintf(p, sizeof p, "%s/bundle.zip", root);touch_file(p);
snprintf(p, sizeof p, "%s/notes.txt", root); touch_file(p);
snprintf(p, sizeof p, "%s/.hidden.png", root); touch_file(p);
char link_path[MR_PATH_MAX], link_target[MR_PATH_MAX];
snprintf(link_target, sizeof link_target, "%s/root.png", root);
snprintf(link_path, sizeof link_path, "%s/link.png", root);
symlink(link_target, link_path); /* 有权限才建;不列 */
mr_library lib;
memset(&lib, 0, sizeof lib);
int n = mr_library_scan(&lib, root, MR_LIB_MAX_DEPTH);
CHECK(n == 7, "scan count=%d (want 7: all direct subdirs)", n);
CHECK(n == 11, "scan count=%d (want 11: 7 dirs + 4 files)", n);
CHECK(lib.count == n, "count matches");
CHECK(lib.truncated == 0, "not truncated");
if (lib.count == 7) {
static const char *want[7] = {
"Empty", "MeowAPPs", "Nested", "Series 2", "Series 02", "Series 3", "Series 10"
if (lib.count == 11) {
static const char *want[11] = {
"Empty", "MeowAPPs", "Nested", "Series 2", "Series 02", "Series 3", "Series 10",
"book.cbz", "bundle.zip", "cover.JPG", "root.png"
};
for (int i = 0; i < 7; i++)
for (int i = 0; i < 11; i++)
CHECK(strcmp(lib.items[i].name, want[i]) == 0,
"sort[%d]=%s got %s", i, want[i], lib.items[i].name);
for (int i = 0; i < 7; i++)
CHECK(lib.items[i].kind == MR_LIB_KIND_DIR, "dirs first: %s", lib.items[i].name);
}
/* 访问放开:系统目录/空目录/无图目录都列出;隐藏项/文件不列。 */
CHECK(find_entry(&lib, "MeowAPPs") != NULL, "system dir listed");
CHECK(find_entry(&lib, "Empty") != NULL, "empty dir listed");
CHECK(find_entry(&lib, "Series 3") != NULL, "no-image dir listed");
CHECK(find_entry(&lib, "Nested") != NULL, "intermediate dir listed");
/* 行类型与副标题(扫描期生成一次,不是占位)。 */
const mr_library_entry *e = find_entry(&lib, "Empty");
CHECK(e && e->kind == MR_LIB_KIND_DIR && strcmp(e->subtitle, "文件夹") == 0,
"dir subtitle=%s", e ? e->subtitle : "");
e = find_entry(&lib, "root.png");
CHECK(e && e->kind == MR_LIB_KIND_IMAGE && e->size == 1 &&
strncmp(e->subtitle, "图片 · ", strlen("图片 · ")) == 0,
"image subtitle=%s size=%lld", e ? e->subtitle : "", e ? e->size : -1);
e = find_entry(&lib, "book.cbz");
CHECK(e && e->kind == MR_LIB_KIND_ARCHIVE &&
strncmp(e->subtitle, "档案册 · ", strlen("档案册 · ")) == 0,
"archive subtitle=%s", e ? e->subtitle : "");
/* 排除项:普通文件/隐藏项/隐藏目录/符号链接。 */
CHECK(find_entry(&lib, "notes.txt") == NULL, "plain text file excluded");
CHECK(find_entry(&lib, ".hidden") == NULL, "hidden dir excluded");
CHECK(find_entry(&lib, "root.png") == NULL, "loose image file excluded");
CHECK(find_entry(&lib, ".hidden.png") == NULL, "hidden file excluded");
CHECK(find_entry(&lib, "link.png") == NULL, "symlink excluded");
CHECK(find_entry(&lib, "a.png") == NULL, "no recursion into subdirs");
CHECK(find_entry(&lib, "Deeper") == NULL, "no recursion into subdirs (deeper)");
/* 首帧不做懒判定:所有行待探测(副标题占位)。 */
for (int i = 0; i < lib.count; i++) {
CHECK(lib.items[i].pages == -1, "%s pages pending", lib.items[i].name);
CHECK(lib.items[i].has_images == -1, "%s book state pending", lib.items[i].name);
CHECK(lib.items[i].probe_state == MR_LIB_PENDING, "%s probe pending", lib.items[i].name);
CHECK(strcmp(lib.items[i].subtitle, "…") == 0, "%s subtitle placeholder", lib.items[i].name);
}
/* 图片下标 = 本层图片自然序(书库点图 → 阅读器起始页的契约)。 */
int i_cover = find_index(&lib, "cover.JPG");
int i_root = find_index(&lib, "root.png");
CHECK(mr_library_image_index(&lib, i_cover) == 0, "cover.JPG is image #0");
CHECK(mr_library_image_index(&lib, i_root) == 1, "root.png is image #1");
CHECK(mr_library_image_index(&lib, find_index(&lib, "book.cbz")) == -1,
"archive has no image index");
CHECK(mr_library_image_index(&lib, find_index(&lib, "Empty")) == -1,
"dir has no image index");
CHECK(mr_library_image_index(&lib, -1) == -1 && mr_library_image_index(&lib, 99) == -1,
"out-of-range image index");
/* 行合成:纯内存,返回扫描期生成的字符串。 */
mr_library_row row;
CHECK(mr_library_row_at(&lib, 0, &row) == 1 && row.index == 0 &&
row.kind == MR_LIB_KIND_DIR && row.title && row.subtitle &&
strcmp(row.subtitle, "文件夹") == 0, "row 0 = first dir");
CHECK(mr_library_row_at(&lib, lib.count, &row) == 0, "no extra row when not truncated");
CHECK(mr_library_row_at(&lib, -1, &row) == 0, "row -1 invalid");
/* max_depth 参数只为兼容旧调用保留:不再递归,结果一致。 */
mr_library lib1;
memset(&lib1, 0, sizeof lib1);
CHECK(mr_library_scan(&lib1, root, 1) == 7, "max_depth ignored (single layer)");
CHECK(mr_library_scan(&lib1, root, 1) == 11, "max_depth ignored (single layer)");
mr_library_free(&lib1);
mr_library_free(&lib);
/* 不可读目录:返回 -1,lib 可安全 free。 */
/* 不可读目录 / 不是目录:返回 -1,lib 可安全 free。 */
mr_library bad;
memset(&bad, 0, sizeof bad);
CHECK(mr_library_scan(&bad, "/no/such/dir/tuku", MR_LIB_MAX_DEPTH) == -1, "missing dir -> -1");
CHECK(bad.count == 0, "missing dir empty");
mr_library_free(&bad);
snprintf(p, sizeof p, "%s/root.png", root);
memset(&bad, 0, sizeof bad);
CHECK(mr_library_scan(&bad, p, MR_LIB_MAX_DEPTH) == -1, "file (not dir) -> -1");
CHECK(bad.count == 0, "file scan empty");
mr_library_free(&bad);
rm_rf(root);
}
+80 -71
View File
@@ -1,11 +1,14 @@
/* test_open_book_host.c — 书库点击 → 阅读器打开的宿主集成回归(需 SDK 源码树)。
*
* 复现用户报告:「父文件夹有封面图片、子文件夹也有图片,点开父文件夹报无法打开此册」。
* 用合成夹具(真实 BMP 当封面/子图,不读用户相册)走完整链路:
* library 列表 → 行点击 → mr_reader_open_dir → reader on_enter → mr_book_open_auto
* 并断言父目录以「书」打开(count=1)且没有进入 open_err。
* M18 文件管理器模型:点目录=进入;点图片=从该图开始读(mr_reader_open_dir_at(目录, 下标));
* 点档案册=打开档案册。本测试用合成夹具(真实 BMP,不读用户相册)走完整链路:
* library 列表 → 点图片行 → mr_reader_open_dir_at → reader on_enter → mr_book_open_auto
* 并断言「点哪张就开哪张」:阅读器当前页 == 该书库行在目录图片自然序里的下标。
*
* 另覆盖:封面图很大时打开仍成功(打开只枚举,不解码);同目录二次打开/离开重开。
* 另覆盖:
* - 目录行只进目录,不打开阅读器;
* - 阅读页已在本页时再次 mr_reader_open_dir_at 能换册/换页重进(SDK 忽略重复 push 的坑);
* - 离开阅读器回书库后再次点图(同目录二次打开)。
*
* 编译:见 test/run_tests.sh(与 test_mode_switch_host 同口径)。
*/
@@ -25,7 +28,6 @@ void library_test_set_root(const char *root);
const char *library_test_dir(void);
int library_test_rows(void);
int library_test_src(int idx, meow_list_item *item);
int library_test_worker_on(void);
/* ui/reader/reader.c 的测试接缝(-DMR_READER_HOST_TEST) */
int mr_reader_ui_mode(void);
@@ -116,22 +118,6 @@ static int rm_rf(const char *path)
return rmdir(path);
}
static void wait_ready(int ms)
{
for (int t = 0; t < ms / 10; t++) {
library_on_tick();
int ready = 1;
int rows = library_test_rows();
for (int i = 0; i < rows; i++) {
meow_list_item it;
if (!library_test_src(i, &it)) continue;
if (it.subtitle && strcmp(it.subtitle, "…") == 0) { ready = 0; break; }
}
if (ready) return;
usleep(10000);
}
}
static int row_index_for(const char *name)
{
int rows = library_test_rows();
@@ -147,10 +133,25 @@ static int row_index_for(const char *name)
static void tap_row(const char *name)
{
int idx = row_index_for(name);
if (idx < 0) { CHECK(0, "row %s not found", name); return; }
int list_top = meow_ui_statusbar_height() + 56;
library_on_tap(120, list_top + idx * 56 + 28);
}
/* 某张图片行在本目录图片自然序里的下标 = 排在前面的「图片 · 」行数。 */
static int expected_image_index(const char *name)
{
int ti = row_index_for(name);
if (ti < 0) return -1;
int n = 0;
for (int i = 0; i < ti; i++) {
meow_list_item it;
if (!library_test_src(i, &it)) continue;
if (it.subtitle && strncmp(it.subtitle, "图片 · ", strlen("图片 · ")) == 0) n++;
}
return n;
}
int main(void)
{
const char *base = getenv("TMPDIR");
@@ -161,25 +162,27 @@ int main(void)
if (!data) { printf("SKIP: mkdtemp failed\n"); return 0; }
setenv("MEOW_DATA_DIR", data, 1);
char root[MR_PATH_MAX], parent[MR_PATH_MAX], sub[MR_PATH_MAX], p[MR_PATH_MAX];
char root[MR_PATH_MAX], pics[MR_PATH_MAX], other[MR_PATH_MAX], p[MR_PATH_MAX];
snprintf(root, sizeof root, "%s/library", data);
snprintf(parent, sizeof parent, "%s/Pictures", root);
if (mkdir_p(parent) != 0) { printf("SKIP: mkdir failed\n"); return 0; }
snprintf(pics, sizeof pics, "%s/Pictures", root);
snprintf(other, sizeof other, "%s/Other", root);
if (mkdir_p(pics) != 0 || mkdir_p(other) != 0) { printf("SKIP: mkdir failed\n"); return 0; }
/* 父目录:一张封面图 + 两个含图子目录(用户报告的结构)。 */
snprintf(p, sizeof p, "%s/cover.jpg", parent); /* 扩展名即可;打开只枚举不解码 */
FILE *f = fopen(p, "wb");
if (!f) { printf("SKIP: cover write failed\n"); return 0; }
fputs("x", f);
fclose(f);
snprintf(sub, sizeof sub, "%s/2023", parent);
mkdir_p(sub);
snprintf(p, sizeof p, "%s/a.bmp", sub);
write_bmp24(p, 40, 60);
snprintf(sub, sizeof sub, "%s/2024", parent);
mkdir_p(sub);
snprintf(p, sizeof p, "%s/b.bmp", sub);
write_bmp24(p, 40, 60);
/* Pictures:6 张图(数字序号/大小写/前导零混排)+ 1 个普通文件(不成行)。 */
const char *imgs[] = { "A.bmp", "a.bmp", "img2.bmp", "img10.bmp",
"page3.bmp", "Page03.bmp" };
for (unsigned i = 0; i < sizeof imgs / sizeof imgs[0]; i++) {
snprintf(p, sizeof p, "%s/%s", pics, imgs[i]);
if (write_bmp24(p, 40, 60) != 0) { printf("SKIP: image write failed\n"); return 0; }
}
snprintf(p, sizeof p, "%s/notes.txt", pics);
{ FILE *f = fopen(p, "wb"); if (f) { fputs("x", f); fclose(f); } }
/* Other:4 张图,用于「已在本页时换册重进」。 */
for (int i = 0; i < 4; i++) {
snprintf(p, sizeof p, "%s/o%d.bmp", other, i + 1);
if (write_bmp24(p, 40, 60) != 0) { printf("SKIP: image write failed\n"); return 0; }
}
meow_ui_opts o = { 480, 720 };
if (meow_ui_init(&o) != 0) { printf("SKIP: meow_ui_init failed (no host backend)\n"); return 0; }
@@ -190,46 +193,52 @@ int main(void)
meow_view_page_push(ACT_LIBRARY);
CHECK(library_test_load(root) == 1, "load fixture root");
wait_ready(3000);
int ip = row_index_for("Pictures");
meow_list_item it;
CHECK(ip >= 0 && library_test_src(ip, &it), "Pictures row present");
CHECK(it.subtitle && strcmp(it.subtitle, "1 page") == 0,
"Pictures probed as book with direct cover (subtitle=%s)",
it.subtitle ? it.subtitle : "");
/* 点击父目录 → 进阅读器;cover 是唯一直接子图 → count=1,不能是 open_err。 */
/* 点目录 = 进入目录,不打开阅读器。 */
tap_row("Pictures");
CHECK(meow_ui_activity_active() == ACT_READER, "tap opens reader");
CHECK(strcmp(mr_reader_dir(), parent) == 0, "reader dir=%s", mr_reader_dir());
CHECK(mr_reader_ui_open_err() == 0, "parent folder opens without open_err");
CHECK(mr_reader_ui_count() == 1, "parent book pages=%d (want 1)",
mr_reader_ui_count());
CHECK(library_test_worker_on() == 0,
"library background decode stopped while reader is active");
for (int i = 0; i < 30; i++) { usleep(10 * 1000); }
CHECK(meow_ui_activity_active() == ACT_LIBRARY, "tap dir stays in library");
CHECK(strcmp(library_test_dir(), pics) == 0, "entered Pictures (%s)", library_test_dir());
CHECK(library_test_rows() == 6, "Pictures rows=%d (6 images, txt excluded)",
library_test_rows());
/* 点 img10.bmp:阅读器从该图开始(count=6, page=它在该目录的自然序下标)。 */
int want = expected_image_index("img10.bmp");
CHECK(want == 3, "img10.bmp natural image index=%d (want 3)", want);
tap_row("img10.bmp");
CHECK(meow_ui_activity_active() == ACT_READER, "tap image opens reader");
CHECK(strcmp(mr_reader_dir(), pics) == 0, "reader dir=%s", mr_reader_dir());
CHECK(mr_reader_ui_open_err() == 0, "open has no error");
CHECK(mr_reader_ui_count() == 6, "reader pages=%d (want 6)", mr_reader_ui_count());
CHECK(mr_reader_ui_page() == want, "reader starts at selected image: page=%d (want %d)",
mr_reader_ui_page(), want);
/* 让首帧解码 tick 跑一遍,确认不会因坏夹具进入 open_err。 */
for (int i = 0; i < 20; i++) usleep(10 * 1000);
CHECK(mr_reader_ui_open_err() == 0, "still no open_err after decode tick");
/* 返回书库 → 后台线程恢复,再次点开(同目录二次打开 + 续读恢复)。 */
/* 已在本页:直接换册重进(M17 遗留修复,SDK 会忽略重复 push)。 */
mr_reader_open_dir_at(other, 2);
CHECK(meow_ui_activity_active() == ACT_READER, "switch keeps reader active");
CHECK(strcmp(mr_reader_dir(), other) == 0, "switched reader dir=%s", mr_reader_dir());
CHECK(mr_reader_ui_count() == 4, "switched book pages=%d (want 4)", mr_reader_ui_count());
CHECK(mr_reader_ui_page() == 2, "switched book page=%d (want 2)", mr_reader_ui_page());
/* 返回书库 → 再点另一张图(同目录二次打开 + 显式起始页优先于续读)。 */
meow_view_page_pop();
CHECK(meow_ui_activity_active() == ACT_LIBRARY, "back to library");
CHECK(library_test_worker_on() == 1, "library background decode restarted on return");
tap_row("Pictures");
CHECK(mr_reader_ui_open_err() == 0, "reopen parent without open_err");
CHECK(mr_reader_ui_count() == 1, "reopen pages=%d", mr_reader_ui_count());
CHECK(strcmp(library_test_dir(), pics) == 0, "library still in Pictures");
tap_row("page3.bmp");
CHECK(meow_ui_activity_active() == ACT_READER, "reopen reader");
CHECK(mr_reader_ui_open_err() == 0, "reopen no error");
CHECK(mr_reader_ui_count() == 6, "reopen pages=%d", mr_reader_ui_count());
int want2 = expected_image_index("page3.bmp");
CHECK(want2 == 4, "page3.bmp natural image index=%d (want 4)", want2);
CHECK(mr_reader_ui_page() == want2, "reopen starts at page=%d (want %d)",
mr_reader_ui_page(), want2);
meow_view_page_pop();
/* 封面是大图也不影响打开(打开只枚举文件名)。 */
snprintf(p, sizeof p, "%s/cover.bmp", parent);
CHECK(write_bmp24(p, 400, 600) == 0, "write large cover");
CHECK(library_test_load(root) == 1, "reload after cover change");
wait_ready(3000);
tap_row("Pictures");
CHECK(mr_reader_ui_open_err() == 0, "open with large cover has no open_err");
CHECK(mr_reader_ui_count() == 2, "cover.jpg + cover.bmp pages=%d (want 2)",
mr_reader_ui_count());
meow_view_page_pop();
/* 档案册路径(无 zip 夹具时只验证入口不被当成目录):点 txt 不存在行;跳过。 */
meow_view_free(reader);
meow_view_free(lib);