|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014-2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import _Concurrency |
| 14 | +import XCTest |
| 15 | +import protocol TSCUtility.ProgressAnimationProtocol |
| 16 | + |
| 17 | +@_spi(SwiftPMInternal) |
| 18 | +@testable |
| 19 | +import Basics |
| 20 | + |
| 21 | +final class ProgressAnimationTests: XCTestCase { |
| 22 | + class TrackingProgressAnimation: ProgressAnimationProtocol { |
| 23 | + var steps: [Int] = [] |
| 24 | + |
| 25 | + func update(step: Int, total: Int, text: String) { |
| 26 | + steps.append(step) |
| 27 | + } |
| 28 | + |
| 29 | + func complete(success: Bool) {} |
| 30 | + func clear() {} |
| 31 | + } |
| 32 | + |
| 33 | + func testThrottledPercentProgressAnimation() { |
| 34 | + do { |
| 35 | + let tracking = TrackingProgressAnimation() |
| 36 | + var now = ContinuousClock().now |
| 37 | + let animation = ThrottledProgressAnimation( |
| 38 | + tracking, now: { now }, interval: .milliseconds(100), |
| 39 | + clock: ContinuousClock.self |
| 40 | + ) |
| 41 | + |
| 42 | + // Update the animation 10 times with a 50ms interval. |
| 43 | + let total = 10 |
| 44 | + for i in 0...total { |
| 45 | + animation.update(step: i, total: total, text: "") |
| 46 | + now += .milliseconds(50) |
| 47 | + } |
| 48 | + animation.complete(success: true) |
| 49 | + XCTAssertEqual(tracking.steps, [0, 2, 4, 6, 8, 10]) |
| 50 | + } |
| 51 | + |
| 52 | + do { |
| 53 | + // Check that the last animation update is sent even if |
| 54 | + // the interval has not passed. |
| 55 | + let tracking = TrackingProgressAnimation() |
| 56 | + var now = ContinuousClock().now |
| 57 | + let animation = ThrottledProgressAnimation( |
| 58 | + tracking, now: { now }, interval: .milliseconds(100), |
| 59 | + clock: ContinuousClock.self |
| 60 | + ) |
| 61 | + |
| 62 | + // Update the animation 10 times with a 50ms interval. |
| 63 | + let total = 10 |
| 64 | + for i in 0...total-1 { |
| 65 | + animation.update(step: i, total: total, text: "") |
| 66 | + now += .milliseconds(50) |
| 67 | + } |
| 68 | + // The next update is at 1000ms, but we are at 950ms, |
| 69 | + // so "step 9" is not sent yet. |
| 70 | + XCTAssertEqual(tracking.steps, [0, 2, 4, 6, 8]) |
| 71 | + // After explicit "completion", the last step is flushed out. |
| 72 | + animation.complete(success: true) |
| 73 | + XCTAssertEqual(tracking.steps, [0, 2, 4, 6, 8, 9]) |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments