#!/usr/bin/env python3 """Tests the per-tab "Never park this tab" exception. Run with: DISPLAY=:99 python3 test/harness/protect.py """ import sys import threading import time from http.server import ThreadingHTTPServer from driver import (BASE, BUILD, CHECKS, PORT, Handler, build, check, hook, launch, state, wait_uuid) def active_id(driver): return next(tab['id'] for tab in state(driver) if tab['active']) def by_id(tabs, tab_id): return next(tab for tab in tabs if tab['id'] == tab_id) def main(): build() server = ThreadingHTTPServer(('127.0.0.1', PORT), Handler) threading.Thread(target=server.serve_forever, daemon=True).start() driver = launch() handles = {} ids = {} try: addon_id = driver.install_addon(str(BUILD), temporary=True) wait_uuid(driver, addon_id) driver.get(BASE + '/p1') handles['/p1'] = driver.current_window_handle ids['/p1'] = active_id(driver) for i in (2, 3): driver.switch_to.new_window('tab') driver.get(BASE + f'/p{i}') handles[f'/p{i}'] = driver.current_window_handle ids[f'/p{i}'] = active_id(driver) time.sleep(0.5) hook(driver, 'protect', tabId=ids['/p2'], value=True) check('p2 is marked protected', ids['/p2'] in hook(driver, 'protected')) # Call from p2: it is the active tab and protected, so it survives and # the other two become candidates. driver.switch_to.window(handles['/p2']) hook(driver, 'unload-others', tabId=ids['/p2']) time.sleep(0.5) tabs = state(driver) check('unload-others skips the protected tab', not by_id(tabs, ids['/p2'])['discarded']) check('unload-others still discards the rest', by_id(tabs, ids['/p1'])['discarded'] and by_id(tabs, ids['/p3'])['discarded']) hook(driver, 'settings', values={'autoUnload': True, 'idleSeconds': 10, 'skipAudible': True}) driver.switch_to.window(handles['/p1']) time.sleep(11) hook(driver, 'sweep') time.sleep(0.5) tabs = state(driver) check('the idle sweep skips the protected tab', not by_id(tabs, ids['/p2'])['discarded']) hook(driver, 'protect', tabId=ids['/p2'], value=False) check('p2 is no longer protected', ids['/p2'] not in hook(driver, 'protected')) hook(driver, 'sweep') time.sleep(0.5) tabs = state(driver) check('the sweep discards it once unprotected', by_id(tabs, ids['/p2'])['discarded']) finally: driver.quit() server.shutdown() failed = [name for name, ok in CHECKS if not ok] print(f'\n{len(CHECKS) - len(failed)}/{len(CHECKS)} checks passed', flush=True) if failed: print('failed: ' + ', '.join(failed), flush=True) return 1 return 0 if __name__ == '__main__': sys.exit(main())