# Tab lifecycle (S059): a tab that asked "Leave this page?" and stayed takes Reload again; Reload while the question
# is up is ignored with a log line; Nacre's pages hold no web view (navigating there frees it, Back brings the page
# back with its history); closing a tab whose page posts messages frees its view without a GLib CRITICAL; a shortcut
# being recorded in settings stops when its tab is left. Offline.
# Run: SMOKE_SIZE=1280x900 tools/smoke.sh tests/smoke/stuck.txt ; then `grep -c CRITICAL <out>/nacre.log` = 0.
wait 3
resize 1280 900
open @PAGES@/dialogs.html
waitload
# The page's beforeunload only asks after a click in it.
locate #t
wait 0.5
click @
wait 0.5
cmd close_tab
wait 1.2
expect /active/dialog true
expect /active/closing true
shot leave-ask
# Reload while the question is up: ignored (log line), the view isn't loaded into while WebKit closes it.
cmd reload
wait 0.5
expect /tab_count 1
key Escape
wait 0.5
expect /active/dialog false
expect /active/closing false
expect /tab_count 1
# Stayed: Reload works again (the marker set now is gone after it).
js window.__mark = 1
assert window.__mark === 1
# The page was clicked, so the Reload asks too: Leave (the dialog's second button).
cmd reload
wait 1.2
expect /active/dialog true
expect /active/closing false
shot reload-ask
tap 700 542
wait 1.5
waitfor 10 window.__mark === undefined && document.readyState === "complete"
expect /active/dialog false
js window.onbeforeunload = null
open @PAGES@/hello.html
waitload
expect /active/title "Smoke page"
# A web page navigating to one of Nacre's pages lets its view go; Back loads it again, history kept.
open @PAGES@/basics.html
waitload
expect /lifecycle/views 1
cmd navigate nacre://settings
wait 1
expect /active/has_web false
expect /active/history_state true
expect /active/can_back true
wait 2
expect /lifecycle/views 0
expect /lifecycle/views_leaked 0
status on-settings
cmd back
waitload
expect /active/has_web true
expect /active/title "Basics"
wait 1
expect /active/can_back true
cmd back
waitload
expect /active/title "Smoke page"
# A nacre:// tab closes at once and leaves nothing behind.
newtab nacre://history
wait 1
expect /tab_count 2
cmd close_tab
wait 1
expect /tab_count 1
# A tab whose page keeps posting messages closes and frees its view, and WebKit drops the late messages.
newtab @PAGES@/chatty.html
waitload
wait 1
expect /lifecycle/views 2
cmd close_tab
wait 3
expect /tab_count 1
expect /lifecycle/views 1
expect /lifecycle/views_leaked 0
# A shortcut being recorded stops when its tab is left (else every shortcut of the window stays dead).
newtab nacre://settings/keys
wait 1.5
tap 1091 313
wait 0.5
expect /lifecycle/capture_key true
shot keys
cmd select 0
wait 0.5
expect /lifecycle/capture_key false
# And a web page's question left on the tab goes with its view when the tab opens one of Nacre's pages.
open @PAGES@/dialogs.html
waitload
locate #t
wait 0.5
click @
wait 0.5
cmd reload
wait 1.2
expect /active/dialog true
cmd navigate nacre://settings
wait 2
expect /active/dialog false
expect /lifecycle/views_leaked 0
status done
quit
