SPB Git

Repository Public License

SessionTests.swift 7.8 KB 153 lines swift

1import Foundation2import NavigatorCore3import TestSupport4import Testing5import WebKit6@testable import WebRuntime78extension WebRuntimeTests {9    @Suite struct Session {10        @Test func interactionStateRoundTrip() async throws {11            try await withHarness { harness in12                let profile = harness.makeProfile()13                let first = harness.makeEngine(profile: profile)14                try await harness.load(first, "/", expectingTitle: "Fixture Home")15                try await harness.load(first, "/page/a", expectingTitle: "Fixture a")16                try await harness.load(first, "/page/b", expectingTitle: "Fixture b")17                first.goBack()18                try await waitUntil("back to a") { first.title == "Fixture a" && !first.isLoading }1920                let state = try #require(first.sessionStateData())21                #expect(!state.isEmpty)22                let saved = first.updating(Tab(windowID: WindowID(), spaceID: SpaceID()))23                #expect(saved.url == harness.server.url("/page/a"))24                #expect(saved.title == "Fixture a")25                #expect(saved.sessionState == state)2627                let second = harness.makeEngine(profile: profile)28                #expect(second.restoreSessionState(state))29                try await waitUntil("restored page") { second.title == "Fixture a" && !second.isLoading }30                #expect(second.url == harness.server.url("/page/a"))31                #expect(second.canGoBack && second.canGoForward)32                second.goForward()33                try await waitUntil("restored forward entry") { second.title == "Fixture b" && !second.isLoading }3435                // Garbage is refused instead of leaving the tab in a half-restored state.36                let third = harness.makeEngine(profile: profile)37                #expect(!third.restoreSessionState(Data("not a session".utf8)))38            }39        }4041        @Test func suspendReleasesWebViewAndResumeRestores() async throws {42            try await withHarness { harness in43                let engine = harness.makeEngine()44                try await harness.load(engine, "/", expectingTitle: "Fixture Home")45                try await harness.load(engine, "/page/a", expectingTitle: "Fixture a")46                engine.setPageZoom(1.25)47                #expect(engine.isSuspendable)48                weak let releasedView = engine.webView4950                let page = try #require(await engine.suspend())51                #expect(engine.webView == nil)52                #expect(engine.lifecycle == .suspended)53                #expect(engine.suspendedPage == page)54                #expect(page.url == harness.server.url("/page/a"))55                #expect(page.interactionState != nil)56                // The tab keeps what the tab strip shows.57                #expect(engine.title == "Fixture a")58                #expect(engine.url == harness.server.url("/page/a"))59                let thumbnail = try #require(page.thumbnailPNG)60                #expect(thumbnail.starts(with: [0x89, 0x50, 0x4E, 0x47]))61                #expect(engine.sessionStateData() == page.interactionState)62                await settle(milliseconds: 100)63                #expect(releasedView == nil, "the web view must actually be released")6465                engine.resume()66                #expect(engine.lifecycle == .live)67                #expect(engine.suspendedPage == nil)68                let view = try #require(engine.webView)69                try await waitUntil("page restored after resume") { engine.title == "Fixture a" && !engine.isLoading && engine.canGoBack }70                #expect(view.pageZoom == 1.25)71                engine.goBack()72                try await waitUntil("history survived suspension") { engine.title == "Fixture Home" && !engine.isLoading }73            }74        }7576        @Test func dormantTabCreatesNoWebViewUntilResumed() async throws {77            try await withHarness { harness in78                let profile = harness.makeProfile()79                let tab = Tab(windowID: WindowID(), spaceID: SpaceID(), url: harness.server.url("/page/dormant"),80                              title: "Saved Title", isMuted: true, zoom: 1.5, userAgentMode: .mobile)81                let engine = WebEngine(profile: harness.context(profile), restoring: tab)82                harness.adopt(engine)83                #expect(engine.id == tab.id)84                #expect(engine.webView == nil)85                #expect(engine.lifecycle == .dormant)86                #expect(engine.title == "Saved Title")87                #expect(engine.contentMode == .mobile)88                #expect(engine.isMuted)89                await settle(milliseconds: 150)90                #expect(harness.server.requests.isEmpty)9192                engine.resume()93                try await waitUntil("dormant tab loaded") { engine.title == "Fixture dormant" && !engine.isLoading }94                #expect(engine.lifecycle == .live)95                #expect(engine.webView?.pageZoom == 1.5)96                #expect(harness.server.requests(path: "/page/dormant").first?.header("user-agent")?.contains("iPhone") == true)97            }98        }99100        @Test func poolSuspendsLeastRecentlyUsedAndSparesProtectedTabs() async throws {101            try await withHarness { harness in102                let profile = harness.makeProfile()103                let pool = WebViewPool(maximumLiveWebViews: 1)104                var engines: [WebEngine] = []105                for name in ["one", "two", "three"] {106                    let engine = harness.makeEngine(profile: profile)107                    try await harness.load(engine, "/page/\(name)", expectingTitle: "Fixture \(name)")108                    pool.track(engine)109                    engines.append(engine)110                }111                engines[0].lastActiveAt = Date().addingTimeInterval(-300)112                engines[1].lastActiveAt = Date().addingTimeInterval(-200)113                engines[2].lastActiveAt = Date().addingTimeInterval(-100)114                #expect(pool.liveCount == 3)115116                // "one" is the oldest but protected (say, visible in a split): "two" goes first, then "three".117                let suspended = await pool.enforceLimit(protecting: [engines[0].id])118                #expect(suspended == [engines[1].id, engines[2].id])119                #expect(pool.liveCount == 1)120                #expect(engines[0].webView != nil)121                #expect(engines[1].lifecycle == .suspended)122123                pool.activate(engines[1])124                try await waitUntil("reactivated tab") { engines[1].title == "Fixture two" && !engines[1].isLoading }125                #expect(pool.liveCount == 2)126127                let idle = await pool.suspendIdle(olderThan: 250, protecting: [])128                #expect(idle == [engines[0].id])129            }130        }131132        @Test func processTerminationStateAndReload() async throws {133            try await withHarness { harness in134                let engine = harness.makeEngine()135                try await harness.load(engine, "/page/a", expectingTitle: "Fixture a")136                let view = try #require(engine.webView)137                // Public API offers no way to make a web content process die, so the delegate callback is138                // invoked directly: this verifies the state handling, not a real termination.139                engine.adapter.webViewWebContentProcessDidTerminate(view)140                #expect(engine.processState.isTerminated)141                if case .terminated(let count, _) = engine.processState { #expect(count == 1) }142143                let before = harness.server.requests(path: "/page/a").count144                engine.reload()145                try await waitUntil("reloaded after termination") {146                    harness.server.requests(path: "/page/a").count > before && !engine.isLoading && engine.processState == .running147                }148                #expect(engine.title == "Fixture a")149            }150        }151    }152}153
Files main
Menu

Theme