Repository Public License
1import Foundation2import NavigatorCore3import TestSupport4import Testing5import WebKit6@testable import WebRuntime78extension WebRuntimeTests {9 @Suite struct Profiles {10 /// Reads back the cookie header and a localStorage key as the fixture server / page see them.11 @MainActor12 private func read(_ engine: WebEngine, _ harness: Harness) async throws -> (cookies: String, storage: String) {13 engine.load(harness.server.url("/cookie/read"))14 try await waitUntil("cookie read") { engine.title.hasPrefix("cookies:") && !engine.isLoading }15 let cookies = String(engine.title.dropFirst("cookies:".count))16 engine.load(harness.server.url("/storage/read?key=token"))17 try await waitUntil("storage read") { engine.title.hasPrefix("storage:") && !engine.isLoading }18 return (cookies, String(engine.title.dropFirst("storage:".count)))19 }2021 @Test func cookiesAndStorageAreIsolatedBetweenProfilesAndGhost() async throws {22 try await withHarness { harness in23 let profileA = harness.makeProfile("A"), profileB = harness.makeProfile("B")24 let ghost = harness.makeProfile("Ghost", ghost: true)25 let a = harness.makeEngine(profile: profileA)26 try await harness.load(a, "/cookie/set?name=session&value=alpha&persistent=1", expectingTitle: "Cookie Set")27 try await harness.load(a, "/storage/set?key=token&value=alpha-storage", expectingTitle: "stored")2829 // Profile A sees its own data — also from a second tab.30 let a2 = harness.makeEngine(profile: profileA)31 let own = try await read(a2, harness)32 #expect(own.cookies == "session=alpha")33 #expect(own.storage == "alpha-storage")3435 // Profile B and a ghost session see nothing of it.36 let other = try await read(harness.makeEngine(profile: profileB), harness)37 #expect(other.cookies.isEmpty)38 #expect(other.storage == "null")39 let private_ = try await read(harness.makeEngine(profile: ghost), harness)40 #expect(private_.cookies.isEmpty)41 #expect(private_.storage == "null")4243 // Same through the store API.44 #expect(await harness.context(profileA).cookies().map(\.name) == ["session"])45 #expect(await harness.context(profileB).cookies().isEmpty)46 #expect(await harness.context(ghost).cookies().isEmpty)4748 // The stores are distinct objects with the identifiers of the profiles.49 #expect(harness.context(profileA).dataStore.identifier == profileA.dataStoreIdentifier)50 #expect(harness.context(profileB).dataStore.identifier == profileB.dataStoreIdentifier)51 #expect(harness.context(profileA).dataStore.isPersistent)52 #expect(!harness.context(ghost).dataStore.isPersistent)53 #expect(harness.context(ghost).dataStore.identifier == nil)54 }55 }5657 @Test func ghostSessionIsSharedByItsTabsAndGoneAfterItEnds() async throws {58 try await withHarness { harness in59 let ghost = harness.makeProfile("Ghost", ghost: true)60 let first = harness.makeEngine(profile: ghost)61 let sessionID = harness.context(ghost).sessionID62 try await harness.load(first, "/cookie/set?name=ghost&value=one", expectingTitle: "Cookie Set")63 try await harness.load(first, "/storage/set?key=token&value=ghost-storage", expectingTitle: "stored")6465 // A second tab of the same ghost session shares the store.66 let second = harness.makeEngine(profile: ghost)67 #expect(second.profile === first.profile)68 let shared = try await read(second, harness)69 #expect(shared.cookies == "ghost=one" && shared.storage == "ghost-storage")7071 // Ending the session and closing its tabs: the next ghost session starts empty.72 first.close(); second.close()73 harness.registry.endGhostSession(for: ghost.id)74 #expect(!harness.registry.hasOpenGhostSession(for: ghost.id))75 let fresh = harness.context(ghost)76 #expect(fresh.sessionID != sessionID)77 #expect(fresh.dataStore !== first.profile.dataStore)78 let after = try await read(harness.makeEngine(profile: ghost), harness)79 #expect(after.cookies.isEmpty && after.storage == "null")8081 // Nothing of a ghost session reaches history, and no persistent store was created for it.82 #expect(await harness.history.visits.isEmpty)83 #expect(!(await WebProfileRegistry.allStoreIdentifiers()).contains(ghost.dataStoreIdentifier))84 await #expect(throws: ProfileStoreError.ghostHasNoPersistentStore) {85 try await harness.registry.deleteStore(of: ghost)86 }87 }88 }8990 @Test func websiteDataListingAndRemoval() async throws {91 try await withHarness { harness in92 let profile = harness.makeProfile()93 let context = harness.context(profile)94 let engine = harness.makeEngine(profile: profile)95 try await harness.load(engine, "/cookie/set?name=a&value=1&persistent=1", expectingTitle: "Cookie Set")96 try await harness.load(engine, "/storage/set?key=token&value=x", expectingTitle: "stored")97 try await harness.load(engine, "/cookie/set?name=b&value=2&persistent=1", host: "localhost", expectingTitle: "Cookie Set")9899 try await waitUntilAsync("records for both hosts") {100 let domains = await context.websiteData(kinds: [.cookies]).map(\.domain)101 return domains.contains("127.0.0.1") && domains.contains("localhost")102 }103 let storage = await context.websiteData(kinds: [.localStorage])104 #expect(storage.contains { $0.domain == "127.0.0.1" && $0.kinds == [.localStorage] })105106 // Per host, per type: only the cookies of 127.0.0.1 go.107 #expect(await context.removeWebsiteData(kinds: [.cookies], hosts: ["127.0.0.1"]) == 1)108 #expect(await context.cookies().map(\.name) == ["b"])109 #expect(await context.websiteData(kinds: [.localStorage]).contains { $0.domain == "127.0.0.1" })110 #expect(await context.removeWebsiteData(kinds: [.cookies], hosts: ["nothing.invalid"]) == 0)111112 // Per date: nothing is older than a minute, so "since tomorrow" removes nothing and "since an113 // hour ago" removes the rest.114 await context.removeWebsiteData(kinds: WebsiteDataKind.all, modifiedSince: Date().addingTimeInterval(86_400))115 #expect(await context.cookies().count == 1)116 await context.removeWebsiteData(kinds: WebsiteDataKind.all, modifiedSince: Date().addingTimeInterval(-3600))117 #expect(await context.cookies().isEmpty)118 #expect(await context.websiteData().isEmpty)119120 engine.load(harness.server.url("/storage/read?key=token"))121 try await waitUntil("storage cleared") { engine.title == "storage:null" }122 }123 }124125 @Test func deletingAProfileStore() async throws {126 try await withHarness { harness in127 // Not registered with the harness: this test deletes the store itself.128 let profile = Profile(name: "Doomed")129 do {130 let engine = WebEngine(profile: harness.context(profile))131 engine.load(harness.server.url("/cookie/set?name=x&value=1&persistent=1"))132 try await waitUntil("cookie page") { engine.title == "Cookie Set" && !engine.isLoading }133 try await waitUntilAsync("store exists on disk") {134 await WebProfileRegistry.allStoreIdentifiers().contains(profile.dataStoreIdentifier)135 }136 // Refused while a web view of the profile is alive.137 await #expect(throws: ProfileStoreError.storeInUse(liveEngines: 1)) {138 try await harness.registry.deleteStore(of: profile)139 }140 engine.close()141 }142 // The closed engine is gone and with it the last reference to the context besides the143 // registry's: only now will WebKit let the store go.144 try await harness.registry.deleteStore(of: profile)145 #expect(!(await WebProfileRegistry.allStoreIdentifiers()).contains(profile.dataStoreIdentifier))146 }147 }148149 @Test func configurationFactory() async throws {150 try await withHarness { harness in151 let profile = harness.makeProfile()152 let context = harness.context(profile)153 #expect(harness.context(profile) === context, "one context per standard profile")154 var settings = WebEngineSettings()155 settings.autoplayRequiresUserGesture = false156 settings.elementFullscreenEnabled = true157 let first = context.makeConfiguration(settings: settings), second = context.makeConfiguration()158 #expect(first.websiteDataStore === context.dataStore && second.websiteDataStore === context.dataStore)159 #expect(first.userContentController !== second.userContentController, "script state is per tab")160 #expect(first.urlSchemeHandler(forURLScheme: "orix") != nil)161 #expect(first.preferences.isElementFullscreenEnabled)162 #expect(first.mediaTypesRequiringUserActionForPlayback == [])163 #expect(second.mediaTypesRequiringUserActionForPlayback == .all)164 #expect(first.applicationNameForUserAgent == settings.applicationNameForUserAgent)165 }166 }167 }168}169