|
| 1 | +// |
| 2 | +// CountlyScreenMetricsTests.swift |
| 3 | +// CountlyTests |
| 4 | +// |
| 5 | +// Covers screen-metric helpers refactored on the `mainscreen_dpc` branch: |
| 6 | +// - CountlyCommon.getWindowSize() — used for content/feedback view sizing. |
| 7 | +// - CountlyDeviceInfo.resolution() — used for the `_resolution` metric. |
| 8 | +// |
| 9 | +// These tests are written to work both in a hosted XCTest bundle (where |
| 10 | +// UIApplication has connected window scenes) and in a logic-test bundle |
| 11 | +// (where no scene is attached). They assert *property* invariants rather |
| 12 | +// than concrete sizes so they hold across hosts and devices. |
| 13 | +// |
| 14 | + |
| 15 | +import XCTest |
| 16 | +@testable import Countly |
| 17 | + |
| 18 | +#if os(iOS) |
| 19 | +class CountlyScreenMetricsTests: CountlyBaseTestCase { |
| 20 | + |
| 21 | + override func setUp() { |
| 22 | + super.setUp() |
| 23 | + Countly.sharedInstance().halt(true) |
| 24 | + } |
| 25 | + |
| 26 | + override func tearDown() { |
| 27 | + Countly.sharedInstance().halt(true) |
| 28 | + super.tearDown() |
| 29 | + } |
| 30 | + |
| 31 | + // MARK: - getWindowSize |
| 32 | + |
| 33 | + /** |
| 34 | + * Regression guard: getWindowSize must not crash and must return a |
| 35 | + * finite, non-negative CGSize. The `mainscreen_dpc` refactor previously |
| 36 | + * broke the build by referencing `size` after removing its declaration; |
| 37 | + * this test exists primarily so the build/runtime path stays exercised. |
| 38 | + */ |
| 39 | + func test_getWindowSize_returnsFiniteNonNegativeSize() { |
| 40 | + let size = CountlyCommon.sharedInstance().getWindowSize() |
| 41 | + |
| 42 | + XCTAssertFalse(size.width.isNaN, "width should not be NaN") |
| 43 | + XCTAssertFalse(size.height.isNaN, "height should not be NaN") |
| 44 | + XCTAssertGreaterThanOrEqual(size.width, 0, "width should be >= 0") |
| 45 | + XCTAssertGreaterThanOrEqual(size.height, 0, "height should be >= 0") |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * getWindowSize must be deterministic — repeated calls without any |
| 50 | + * window-scene churn should return the same value. |
| 51 | + */ |
| 52 | + func test_getWindowSize_isStableAcrossCalls() { |
| 53 | + let first = CountlyCommon.sharedInstance().getWindowSize() |
| 54 | + let second = CountlyCommon.sharedInstance().getWindowSize() |
| 55 | + let third = CountlyCommon.sharedInstance().getWindowSize() |
| 56 | + |
| 57 | + XCTAssertEqual(first.width, second.width, accuracy: 0.0001) |
| 58 | + XCTAssertEqual(first.height, second.height, accuracy: 0.0001) |
| 59 | + XCTAssertEqual(second.width, third.width, accuracy: 0.0001) |
| 60 | + XCTAssertEqual(second.height, third.height, accuracy: 0.0001) |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * If a UIWindowScene is present (hosted test), getWindowSize should not |
| 65 | + * exceed the underlying window's bounds — safe-area adjustments may |
| 66 | + * shrink the size but never grow it. If no scene is present, the |
| 67 | + * function returns CGSizeZero, which satisfies the same invariant. |
| 68 | + */ |
| 69 | + func test_getWindowSize_doesNotExceedWindowBounds() { |
| 70 | + let size = CountlyCommon.sharedInstance().getWindowSize() |
| 71 | + |
| 72 | + if let window = firstWindow() { |
| 73 | + XCTAssertLessThanOrEqual(size.width, window.bounds.width, |
| 74 | + "Reported width should not exceed window width") |
| 75 | + XCTAssertLessThanOrEqual(size.height, window.bounds.height, |
| 76 | + "Reported height should not exceed window height") |
| 77 | + } else { |
| 78 | + XCTAssertEqual(size.width, 0, accuracy: 0.0001, |
| 79 | + "Expected CGSizeZero when no window scene is attached") |
| 80 | + XCTAssertEqual(size.height, 0, accuracy: 0.0001, |
| 81 | + "Expected CGSizeZero when no window scene is attached") |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // MARK: - resolution |
| 86 | + |
| 87 | + /** |
| 88 | + * resolution() must return a "WIDTHxHEIGHT" formatted string with two |
| 89 | + * non-negative numeric components. This is the contract consumed by the |
| 90 | + * `_resolution` metric and by the content-builder query parameters. |
| 91 | + */ |
| 92 | + func test_resolution_isWellFormedString() { |
| 93 | + guard let resolution = CountlyDeviceInfo.resolution() else { |
| 94 | + XCTFail("resolution() returned nil on iOS") |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + let parts = resolution.components(separatedBy: "x") |
| 99 | + XCTAssertEqual(parts.count, 2, |
| 100 | + "Expected WIDTHxHEIGHT format, got: \(resolution)") |
| 101 | + guard parts.count == 2 else { return } |
| 102 | + |
| 103 | + guard let width = Double(parts[0]), let height = Double(parts[1]) else { |
| 104 | + XCTFail("resolution() components should be numeric, got: \(resolution)") |
| 105 | + return |
| 106 | + } |
| 107 | + XCTAssertGreaterThanOrEqual(width, 0, "resolution width should be >= 0 (got: \(resolution))") |
| 108 | + XCTAssertGreaterThanOrEqual(height, 0, "resolution height should be >= 0 (got: \(resolution))") |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * On iOS 13+ the refactor reads bounds/scale from the first |
| 113 | + * UIWindowScene's window. When a scene is connected, the returned |
| 114 | + * string must match `bounds.size * displayScale` for that window. |
| 115 | + * When no scene is connected, the implementation falls through with |
| 116 | + * zero values and emits "0x0" — locks in the iOS 13+ no-fallback |
| 117 | + * behavior so regressions are visible. |
| 118 | + */ |
| 119 | + func test_resolution_matchesFirstSceneOrIsZero() { |
| 120 | + guard let resolution = CountlyDeviceInfo.resolution() else { |
| 121 | + XCTFail("resolution() returned nil on iOS") |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + if let window = firstWindow() { |
| 126 | + let scale = window.traitCollection.displayScale |
| 127 | + let expected = "\(percentG(window.bounds.width * scale))x\(percentG(window.bounds.height * scale))" |
| 128 | + XCTAssertEqual(resolution, expected, |
| 129 | + "resolution should reflect first window scene's pixel size") |
| 130 | + } else { |
| 131 | + XCTAssertEqual(resolution, "0x0", |
| 132 | + "iOS 13+ without a window scene should report 0x0 (no UIScreen fallback)") |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * The metrics dictionary returned by CountlyDeviceInfo.metrics() |
| 138 | + * must include the `_resolution` key — this is the SDK-facing |
| 139 | + * contract that ends up in `/i` requests. |
| 140 | + */ |
| 141 | + func test_resolution_appearsInMetricsString() { |
| 142 | + guard let metricsString = CountlyDeviceInfo.metrics() else { |
| 143 | + XCTFail("metrics() returned nil") |
| 144 | + return |
| 145 | + } |
| 146 | + XCTAssertTrue(metricsString.contains("_resolution"), |
| 147 | + "metrics() should include the _resolution key, got: \(metricsString)") |
| 148 | + } |
| 149 | + |
| 150 | + // MARK: - Helpers |
| 151 | + |
| 152 | + private func firstWindow() -> UIWindow? { |
| 153 | + if #available(iOS 13.0, *) { |
| 154 | + for scene in UIApplication.shared.connectedScenes { |
| 155 | + if let windowScene = scene as? UIWindowScene, |
| 156 | + let window = windowScene.windows.first { |
| 157 | + return window |
| 158 | + } |
| 159 | + } |
| 160 | + return nil |
| 161 | + } else { |
| 162 | + return UIApplication.shared.delegate?.window ?? nil |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /// Mirrors Objective-C `%g` formatting used by `CountlyDeviceInfo.resolution`. |
| 167 | + /// `%g` uses the shorter of `%e` or `%f`, trims trailing zeros, and drops |
| 168 | + /// the decimal point when not needed. `String(format:)` with `%g` matches |
| 169 | + /// this behavior on Apple platforms. |
| 170 | + private func percentG(_ value: CGFloat) -> String { |
| 171 | + return String(format: "%g", Double(value)) |
| 172 | + } |
| 173 | +} |
| 174 | +#endif |
0 commit comments