Files
fbca2cae81 core, ui: resize images in the core for sharper previews and images (#7941)
* WIP: Haskell impl of resizeImageToStrSize

* WIP: add FFI export wrapper

* WIP: switch ios resizeImageToStrSizeSync to backend

* WIP: fix resizeImageToStrSizeSync

* WIP: add JNI wrappers

* Add non-base64 resize and cWriteImage

* WIP: add chat_write_image

* fix resizeToStr

* fixes

* fix syntax, raise jpeg quality back

* add chatWriteImage

* Fix cfArgs in tests

* Use jpeg-turbo encoder

* add nix pins

* bump jpeg-turbo

* add CI deps

* add encoding funs to android lib

* bump jpeg-turbo for 20.04 fix

* jpeg-turbo fix for 8.10

* android fixes

* Bump jpeg-turbo

* set static flag for jpeg-turbo

* Fix nix for aarch64/android

* Fix android C api

* Fix resize in android

* Bump hs-libjpeg-turbo

* target correct override

* override android lib from android pkgs

* add macos build tweaks

* bump hs-jpeg-turbo for 8.10 fixes

* more build fixes

* make ios compile

* Always use pkgconfig for libjpeg-turbo

* WIP: debug libjpeg in ubuntu pkg-config CI

* Add pkg-config to dockerfiles

* Add libturbojpeg to dockerfiles

* Use -dev version of libturbojpeg in dockerfiles

* Install pkg-config in windows CI

* Disable flag for custom libturbojpeg linking options

Workarounds relocation error on x86_64-linux with gcc:
  /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/libturbojpeg.a(turbojpeg.c.o): relocation R_X86_64_TPOFF32 against `errStr' can not be used when making a shared object; recompile with -fPIC
  /usr/bin/ld: failed to set dynamic section sizes: bad value

* WIP: debug libjpeg in windows pkg-config CI

* WIP: debug libjpeg package in windows CI

* core: image resize module and buffer FFI

* ci: link libjpeg-turbo statically on all platforms

* ui: resize images in the core

* core: rename image resize functions to chat_resize_image and chat_resize_image_uri

* ci: restore whitespace in build.yml

* ci: build libjpeg-turbo 3.1.4.1 from a submodule

* core: hs-jpeg-turbo from github.com/simplex-chat

* nodejs: resize images

* python: resize images

* core: hs-jpeg-turbo from gitlab.com, the GitHub import lacks LFS objects

* core: generate /img previews

* ci: link libjpeg-turbo on Windows without pkg-config

* ci: build libjpeg-turbo on Windows with the GHC toolchain

* docs: plan for image resizing in the core

* docs: update image resizing plan

* ios: prevent crash when wallpaper image cannot be resized

* android, desktop: raise an error when an image cannot be resized

* docs: update spec references for image resizing

* docker: build the CLI image from a clone without submodules

* docs: apply review fixes to image resizing plan

* docker: build the CLI from a git worktree with the submodule present

* docs: apply second review to image resizing plan

* docs: point image resizing plan at the rewritten commit

* core: choose image format by transparency in chat_resize_image

* nodejs: resize images to the app sizes

* python: resize images to the app sizes

* nodejs, python: describe the core modules as used for image resizing

* docs: add license notices of the image libraries

* ci: describe the avatar method without attributing it to the apps

* ci, ios, nix: remove the libjpeg-turbo build outside cabal

* python: name resize_image in the core module text

* nodejs, python: describe resizing to the app image sizes

* core: refuse images larger than the supported size

* core: resize to JPEG images with a side above 65500 pixels

* docs: record image resizing decisions for the bundled libjpeg-turbo

* core, docs: build libjpeg-turbo and stb from the fork submodules

* docs: record the fork pins in the image resizing plan

* core: resize images to at most 4320 pixels per side, refuse above 16384

* docs: record the image side limits in the image resizing plan

* core: pin the rebased hs-jpeg-turbo fork

* core: pin the hs-jpeg-turbo fork without package.yaml

* docs: plan scaled JPEG decoding and EXIF orientation of images

* docs: copy the JuicyPixels-stbir licence of the pinned fork

* core: pin the hs-jpeg-turbo fork with scaled JPEG decoding

* core: decode JPEG at the display size, up to 65500 pixels per side

* core: rotate JPEG images by their EXIF orientation

* docs: refer the image plan to the JPEG decoding code

* docs: record the approved image processing decisions

* core: limit JPEG decoding memory to 1024 MB outside iOS and Android

* docs: name JPEG decoding in the libjpeg-turbo notice

* docs: plan image resizing from files and the decisions of 26 Oct

* core: scale images in one Catmull-Rom pass with sharpening

* docs: plan image sizes at a fixed quality

* core: resize images at a fixed quality to fill the size limit

* core: resize images from a file path or a file descriptor

* ios, android, desktop: resize picked images from their files

* core: encode resized images with mozjpeg

* tests: check descriptor closing by the file it refers to

* core: raise JPEG quality only for a full-size image below 97% of the limit

* ci: name the mozjpeg submodule in the Windows cleanup comments

* docs: refer the image plan to the current code

* docs: refer the image plan to the code merged with master

* core: export every image resizing function from each library

* core: close the file descriptor when its handle cannot be created

* docs: count the C sources of mozjpeg with the arithmetic decoder

* core: use the reviewed hs-jpeg-turbo and JuicyPixels-stbir forks

* ci: build the armv7a Android core library on request

* core: round image heights half up in the size search

* ios: describe the file image resizing functions in SimpleX.h

* ci: pick, preview and send images in the Claude app tests

* core: decode PNG images with libspng and refuse other formats

* core: encode PNG with libspng, keeping the smaller filter choice

* ci: pick, preview and send images in the scripted app smoke tests

* flake: build the PNG decoder package in nix builds

* ios: remove the copy of a picked image after sending it

* core: refuse image files larger than the decode memory limit

* core: scale a JPEG image to its largest size once per resize

* core: bisect JPEG quality without a step function

* core: reduce the memory used to sharpen and to decode PNG images

* android, desktop: pass the image format to the core as an integer

* android, desktop: fuse the preview conditions of picked media

* android, desktop, ci: check that the core resized the smoke test images

* python: remove the docstrings of the explicit image resizing functions

* docs: add the second review fixes to the image plan

* ci: tap the iOS attachment button by its accessibility label

* core: keep the colour profiles of resized images

* docs: add the colour profiles to the image plan

* ci: wait up to 30 s for the new group screens in the iOS smoke test

* ci: pick the Android smoke test images from their own photo folders

* ci: tap the iOS photo picker by position, as its tree is not shown

* ci: pick the test card and the logo by their iOS picker positions

* core: update JuicyPixels-stbir with the stb_image_resize licence terms

* core: write a fixed Display P3 profile for Display P3 images only

* core: do not keep the input image in memory for its colour profile

* core: read the JPEG segments once for the orientation and the profile

* core: read an image file into one buffer of its size

* docs: add the third review fixes to the image plan

* core: resize images with Display P3 or no profile, not profile bytes

* ci: check the pixel sizes of the images sent in the iOS smoke test

* core: update hs-jpeg-turbo with PNG chunk limits and encoder fixes

* docs: list Compact ICC Profiles, libspng and miniz in the dependencies

* android, desktop: reword the picked media preview comment

* docs: refer the image plan to the code merged with master

* ci: render the iOS snapshots before the images case

* ci: dismiss the snapshot tests' camera alert before the iOS images case

* core: update hs-jpeg-turbo with tests and the JuicyPixels-spng synopsis

* core: pin hs-jpeg-turbo and JuicyPixels-stbir in simplex-chat repositories

* update ios core library

* core, ui: accept only JPEG and PNG image data

* core: update JuicyPixels-stbir

* docs: plan for the iOS copy of picked images

---------

Co-authored-by: IC Rainbow <aenor.realm@gmail.com>
Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com>
Co-authored-by: Alexandre Esteves <alexfmpe@proton.me>
2026-10-30 12:14:51 +00:00

363 lines
13 KiB
Bash
Executable File

#!/bin/bash
# Installs the APK in the booted emulator (emulator.sh) and runs the scripted UI cases with the ui helper: onboarding to
# the chat list, a local group, then the chat list with the reachable toolbar (the default) and with the top bar, then
# tests/fixtures/test.jpg and logo-large-rgba.png picked in the system's image picker and sent in the group.
# Usage: scripts/ci/android/smoke-test.sh <apk> <output dir> [start|onboarding|layouts|backend]
# With "start" it stops at the first screen, for an agent to go on from; with "onboarding", at the chat list. "backend"
# also adds the test SMP server (NAMES_SERVER, from backend.sh, reached through emulator.sh reverse) under Your servers
# and tests it, before both layouts.
# Each step saves a numbered screenshot, the accessibility tree and the text on screen to the output dir, prints the
# text to the log, and adds a line to steps.log there; logcat is in app.log. What the screens show is checked without
# stopping: every check is listed in checks.txt and the job summary, and the test fails at the end if any failed.
# The app keeps running for later steps of the job, which get UI_OUT.
set -euo pipefail
apk=$1
export UI_OUT=$2
cases=${3:-layouts}
case "$cases" in
start | onboarding | layouts | backend) ;;
*)
echo "error: unknown case \"$cases\": start, onboarding, layouts (the default) or backend"
exit 1
;;
esac
package=chat.simplex.app
local_chat="seeded notes"
search_field="Search or paste SimpleX link"
ui=$(dirname "$0")/ui
fixtures=$(dirname "$0")/../../../tests/fixtures
checks=$UI_OUT/checks.txt
mkdir -p "$UI_OUT"
: > "$checks"
if [ -n "${GITHUB_ENV:-}" ]; then
echo "UI_OUT=$UI_OUT" >> "$GITHUB_ENV"
fi
report_checks() {
[ -s "$checks" ] || return 0
echo "::group::checks"
cat "$checks"
echo "::endgroup::"
if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then
{
echo "### Smoke test checks"
echo
sed 's/^/- /' "$checks"
} >> "$GITHUB_STEP_SUMMARY"
fi
}
finish() {
local status=$?
if [ "$status" -ne 0 ]; then
"$ui" shot final || true
fi
echo "::group::the core and crashes in logcat"
grep -E 'ndk_translation|SIMPLEX *: (start|App crashed)|AndroidRuntime: (FATAL EXCEPTION|Process: )|Fatal signal' "$UI_OUT/app.log" | head -n 100 || true
echo "::endgroup::"
report_checks
exit "$status"
}
trap finish EXIT
fail() {
echo "error: $*"
exit 1
}
check() {
local what=$1
shift
if "$@"; then
echo "PASS $what" | tee -a "$checks"
else
echo "FAIL $what" | tee -a "$checks"
echo "::error::$what"
fi
}
# A crash: the app logs its own under SIMPLEX ("App crashed"), Android's FATAL EXCEPTION covers one before the app's
# handler is set, and a native fatal signal names the process by the last 15 characters of its name. Not any line
# with "error" or from AndroidRuntime, which logs every uiautomator dump.
app_running() {
! grep -q 'SIMPLEX *: App crashed' "$UI_OUT/app.log" &&
! grep -A 1 'FATAL EXCEPTION' "$UI_OUT/app.log" | grep -q "Process: $package," &&
! grep -q "Fatal signal .*(${package: -15})" "$UI_OUT/app.log" &&
adb shell pidof "$package" > /dev/null
}
y_of() {
local pos
pos=$("$ui" find "$1") || return 1
cut -d ' ' -f 2 <<< "$pos"
}
above() {
local a b
a=$(y_of "$1") || return 1
b=$(y_of "$2") || return 1
[ "$a" -lt "$b" ]
}
screen_height() {
adb shell wm size | sed -n 's/^Physical size: [0-9]*x//p'
}
# Back closes one screen at a time, here between the screens of Settings, which cover the chat list without removing it
# from the tree: a restart is what returns to the chat list.
back_to() {
for _ in 1 2 3 4 5; do
"$ui" find "$1" > /dev/null 2>&1 && return 0
"$ui" key BACK
done
"$ui" find "$1" > /dev/null
}
# On a first start the chat list can show for a moment before onboarding, long enough to open its What's new sheet over
# the first screen: it is closed and noted, not failed.
first_screen() {
local deadline=$((SECONDS + 180))
until "$ui" find "Get started" > /dev/null 2>&1; do
app_running || return 1
if "$ui" find "New in" > /dev/null 2>&1; then
echo "NOTE the What's new sheet covered the first onboarding screen" | tee -a "$checks"
"$ui" shot whats-new-over-onboarding
"$ui" click-text OK
fi
[ "$SECONDS" -lt "$deadline" ] || return 1
sleep 1
done
}
# After Your network Android asks about instant notifications, in front of the conditions: the app's alert, then the
# system's battery dialog, each with "Allow". The conditions show for a moment between the two.
allow_notifications_and_accept() {
local deadline=$((SECONDS + 120))
until "$ui" find "Talk to someone" > /dev/null 2>&1; do
if "$ui" find "Allow" > /dev/null 2>&1; then
"$ui" shot notifications
"$ui" click-text "Allow"
elif "$ui" find "Accept" > /dev/null 2>&1; then
"$ui" shot network-commitments
"$ui" click-text "Accept"
fi
[ "$SECONDS" -lt "$deadline" ] || return 1
sleep 1
done
}
# The centre "x y" of the first text field below the text, which may have neither a text nor a placeholder.
field_below() {
local y
y=$(y_of "$1") || return 1
"$ui" tree | sed -n -E 's/^EditText .* at ([0-9]+),([0-9]+) size ([0-9]+)x([0-9]+).*/\1 \2 \3 \4/p' |
awk -v y="$y" '$2 > y { print $1 + int($3 / 2), $2 + int($4 / 2); exit }'
}
# The centre "x y" of the switch in the row of the text.
switch_of() {
local y
y=$(y_of "$1") || return 1
"$ui" tree | sed -n -E 's/.* at ([0-9]+),([0-9]+) size ([0-9]+)x([0-9]+) .*checkable.*/\1 \2 \3 \4/p' |
awk -v y="$y" '$2 <= y && y <= $2 + $4 { print $1 + int($3 / 2), $2 + int($4 / 2); exit }'
}
# The user picker's button in the toolbar, before the list has chats: chats show the same profile image placeholder.
open_settings() {
"$ui" click-text "profile image placeholder" 10
"$ui" click-text "Settings" 10
}
# Green pixels of the test status left of a server's host name: SimplexGreen, the tick of a server that passed its test.
green_before() {
local x y l t r b png
read -r x y l t r b < <("$ui" find "$1") || return 1
"$ui" shot "$2" > /dev/null || return 1
png=$(find "$UI_OUT" -maxdepth 1 -name "*-$2.png" | sort | tail -n 1)
convert "$png" -alpha off -crop "${l}x$((b - t))+0+$t" +repage -fuzz 20% -fill black +opaque 'rgb(77,218,103)' \
-fill white -opaque 'rgb(77,218,103)' -colorspace Gray -format '%[fx:round(mean*w*h)]' info:
}
# Waits up to 60 s for Test servers to finish: the test status turns green, or the app shows that the test failed.
server_tested() {
local deadline=$((SECONDS + 60)) green
while [ "$SECONDS" -lt "$deadline" ]; do
if "$ui" find "Server test failed" > /dev/null 2>&1; then
"$ui" shot server-test-failed
return 1
fi
green=$(green_before 127.0.0.1 server-test) || return 1
echo "green pixels before the test server's host: $green"
if [ "$green" -ge 50 ]; then
return 0
fi
sleep 2
done
return 1
}
# Settings > Network & servers > Your servers: the test SMP server added, tested and saved. Its address has 127.0.0.1,
# which the emulator forwards to the runner's.
test_server() {
local x y
open_settings
"$ui" click-text "Network & servers" 10
"$ui" click-text "Your servers" 10
"$ui" click-text "Add server" 10
"$ui" click-text "Enter server manually" 10
"$ui" wait "Your server address" 10
read -r x y < <(field_below "Your server address") || fail "no address field for the new server"
"$ui" click "$x" "$y"
"$ui" type "$NAMES_SERVER"
# a server entered by hand is off, and Test servers tests only the servers that are on
read -r x y < <(switch_of "Use for new connections") || fail "no \"Use for new connections\" switch"
"$ui" click "$x" "$y"
"$ui" shot new-server
back_to "Test servers" || fail "no Your servers after adding the server"
"$ui" wait 127.0.0.1 10
"$ui" click-text "Test servers"
check "the test SMP server passes Test servers" server_tested
back_to "Save servers" || fail "no Network & servers after Your servers"
"$ui" click-text "Save servers"
"$ui" shot servers-saved
"$ui" relaunch
}
# A group of the user's own: without chats the chat list shows the onboarding pages, not the search field. The new
# group's name field has the focus.
create_local_chat() {
"$ui" click-text "Start new chat" 10
"$ui" click-text "Create group" 10
"$ui" wait "Enter group name" 10
"$ui" type "$local_chat"
"$ui" wait "$local_chat" 10
"$ui" shot new-group
"$ui" click-text "Create group" 10
# the app offers to invite members once it has created the group; without contacts it has no "Skip inviting members"
"$ui" wait "Invite members" 30
"$ui" shot group-created
"$ui" relaunch
"$ui" wait "$search_field" 30
"$ui" shot local-chat
}
# The search field and "Add list" next to it: at the bottom of the chat list, "Add list" above the field; at the top, below it.
check_layout() {
local layout=$1 field half
half=$(($(screen_height) / 2))
field=$(y_of "$search_field") || field=""
"$ui" shot "$layout-layout"
check "$layout: \"$local_chat\" listed" "$ui" find "$local_chat"
if [ "$layout" = reachable ]; then
check "reachable: the search field is in the bottom half" [ "${field:-0}" -gt "$half" ]
check "reachable: \"Add list\" above the search field" above "Add list" "$search_field"
else
check "top: the search field is in the top half" [ "${field:-$((half * 2))}" -lt "$half" ]
check "top: \"Add list\" below the search field" above "$search_field" "Add list"
fi
}
add_images() {
adb push "$fixtures/logo-large-rgba.png" /sdcard/Pictures/simplex-logo/simplex-logo.png
adb push "$fixtures/test.jpg" /sdcard/Pictures/test-card/test-card.jpg
for image in simplex-logo/simplex-logo.png test-card/test-card.jpg; do
adb shell am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d "file:///sdcard/Pictures/$image" > /dev/null
done
}
sent_files() {
adb shell run-as "$package" ls files/app_files 2> /dev/null | grep -c "^IMG_.*\.$1\$" || true
}
sent_file() {
local deadline=$((SECONDS + 30))
until [ "$(sent_files "$1")" -gt "$2" ]; do
[ "$SECONDS" -lt "$deadline" ] || return 1
sleep 1
done
}
core_returned_no_image() {
grep -c "core returned no image" "$UI_OUT/app.log" || true
}
send_image() {
local folder=$1 ext=$2 sent no_image
sent=$(sent_files "$ext")
no_image=$(core_returned_no_image)
"$ui" click-text Attach 10
"$ui" click-text Image 10
sleep 3
"$ui" shot "picker-$ext"
"$ui" click-text "$folder" 30 || fail "no $folder folder in the image picker"
sleep 2
"$ui" shot "picker-folder-$ext"
"$ui" click-text Photo 30 || fail "no photo in the $folder folder of the image picker"
if ! "$ui" wait "Cancel image preview" 10; then
"$ui" shot "picker-selected-$ext"
"$ui" click-text Done 5 || "$ui" click-text Add 5 || true
fi
check "$ext: the compose area shows the picked image" "$ui" wait "Cancel image preview" 30
"$ui" shot "preview-$ext"
"$ui" click-text "Send Message" 10
check "$ext: the image is sent as a .$ext file" sent_file "$ext" "$sent"
"$ui" shot "sent-$ext"
check "$ext: the core resized the image" [ "$(core_returned_no_image)" -eq "$no_image" ]
}
send_images() {
add_images
"$ui" click-text "$local_chat" 10
"$ui" wait Attach 10
send_image test-card jpg
send_image simplex-logo png
}
# private routing to unknown servers only: the preset servers that would proxy to the test SMP server can't reach the
# runner's 127.0.0.1; developer tools at the debug level put the app's debug log in logcat
started=$SECONDS
"$ui" start "$apk" NetworkSMPProxyMode=Unknown DeveloperTools=true LogLevel=DEBUG
first_screen || fail "no first onboarding screen 180 s after the start, or the app stopped: its crashes in logcat are below"
echo "first screen after $((SECONDS - started)) s"
"$ui" shot first-screen
if [ "$cases" = start ]; then
echo "smoke test: the app shows its first screen"
exit 0
fi
"$ui" click-text "Get started"
"$ui" click-text "Enter profile name" 30
"$ui" type citester
"$ui" wait citester 10
"$ui" shot profile-name
"$ui" click-text "Create profile"
"$ui" wait "Continue" 60
"$ui" shot your-network
"$ui" click-text "Continue"
allow_notifications_and_accept || fail "no chat list 120 s after Your network"
"$ui" shot chat-list
echo "onboarding reached the chat list $((SECONDS - started)) s after the start"
if [ "$cases" != onboarding ]; then
if [ "$cases" = backend ]; then
: "${NAMES_SERVER:?NAMES_SERVER must be the test SMP server address}"
test_server
fi
create_local_chat
check_layout reachable
"$ui" layout top
check_layout top
send_images
fi
check "the app is running and logged no crash" app_running
if grep -q '^FAIL' "$checks"; then
fail "the smoke test failed checks: $(grep -c '^FAIL' "$checks") of $(grep -cE '^(PASS|FAIL)' "$checks")"
fi
echo "smoke test passed: $cases"