Cotton code
41 lines · 1.7 KB · SwiftRaw
import Foundationimport XCTest@testable import CottonUIfinal class PagePlacesTests: XCTestCase {    func testEachDatabaseKeepsItsPositionAcrossNewInstances() {        let suite = "PagePlacesTests-\(UUID().uuidString)"        let defaults = UserDefaults(suiteName: suite)!        defer { defaults.removePersistentDomain(forName: suite) }        let first = PagePlaces(defaults: defaults)        first.keep(120, for: "personal")        first.keep(920, for: "garden")        let reopened = PagePlaces(defaults: defaults)        XCTAssertEqual(reopened.place(of: "personal"), 120)        XCTAssertEqual(reopened.place(of: "garden"), 920)        XCTAssertNil(reopened.place(of: "new"))    }    func testRestoreWaitsForLoadedContentAndNeverReplacesSavedPositionWithClampedOffset() {        var restore = PagePlaceRestoration()        restore.begin(key: "garden", kept: 920)        XCTAssertEqual(restore.target(contentHeight: 300, viewportHeight: 500), 0)        XCTAssertFalse(restore.acceptsOffset)        XCTAssertEqual(restore.target(contentHeight: 2000, viewportHeight: 500), 920)        restore.arrived(at: 900)        XCTAssertFalse(restore.acceptsOffset)        restore.arrived(at: 920)        XCTAssertTrue(restore.acceptsOffset)    }    func testUserScrollCanTakeOverBeforePendingContentArrives() {        var restore = PagePlaceRestoration()        restore.begin(key: "garden", kept: 920)        restore.userStartedScrolling()        XCTAssertTrue(restore.acceptsOffset)        XCTAssertNil(restore.target(contentHeight: 300, viewportHeight: 500))        restore.begin(key: "personal", kept: 120)        XCTAssertFalse(restore.acceptsOffset)        XCTAssertEqual(restore.target(contentHeight: 2000, viewportHeight: 500), 120)    }}