-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix]Thermal State not updating (#167)
- Loading branch information
1 parent
444b579
commit 81523c9
Showing
3 changed files
with
118 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// Copyright © 2023 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
@testable import StreamVideo | ||
import XCTest | ||
import Combine | ||
|
||
final class ThermalStateObserverTests: XCTestCase { | ||
|
||
private var stubThermalState: ProcessInfo.ThermalState = .nominal | ||
private lazy var subject: ThermalStateObserver! = .init { self.stubThermalState } | ||
|
||
override func tearDown() { | ||
subject = nil | ||
super.tearDown() | ||
} | ||
|
||
// MARK: - init | ||
|
||
func test_init_stateHasBeenCorrectlySetUp() { | ||
XCTAssertEqual(ThermalStateObserver.shared.state, ProcessInfo.processInfo.thermalState) | ||
} | ||
|
||
// MARK: - notificationObserver | ||
|
||
func test_notificationObserver_stateChangesWhenSystemPostsNotification() { | ||
func assertThermalState( | ||
_ expected: ProcessInfo.ThermalState, | ||
file: StaticString = #file, | ||
line: UInt = #line | ||
) { | ||
stubThermalState = expected | ||
|
||
let expectation = self.expectation(description: "Notification was received") | ||
var cancellable: AnyCancellable? | ||
cancellable = subject | ||
.$state | ||
.dropFirst() | ||
.sink { | ||
XCTAssertEqual($0, expected, file: file, line: line) | ||
expectation.fulfill() | ||
cancellable?.cancel() | ||
} | ||
|
||
NotificationCenter | ||
.default | ||
.post(.init(name: ProcessInfo.thermalStateDidChangeNotification)) | ||
|
||
wait(for: [expectation], timeout: defaultTimeout) | ||
} | ||
|
||
assertThermalState(.fair) | ||
assertThermalState(.serious) | ||
assertThermalState(.critical) | ||
assertThermalState(.nominal) | ||
} | ||
|
||
// MARK: - scale | ||
|
||
func test_scale_hasExpectedValueForEachThermalState() { | ||
func assertScale( | ||
_ thermalState: ProcessInfo.ThermalState, | ||
expected: CGFloat, | ||
file: StaticString = #file, | ||
line: UInt = #line | ||
) { | ||
stubThermalState = thermalState | ||
|
||
let expectation = self.expectation(description: "Notification was received") | ||
var cancellable: AnyCancellable? | ||
cancellable = subject | ||
.$state | ||
.dropFirst() | ||
.receive(on: DispatchQueue.main) | ||
.sink { [subject] _ in | ||
XCTAssertEqual(subject?.scale, expected, file: file, line: line) | ||
expectation.fulfill() | ||
cancellable?.cancel() | ||
} | ||
|
||
NotificationCenter | ||
.default | ||
.post(.init(name: ProcessInfo.thermalStateDidChangeNotification)) | ||
|
||
wait(for: [expectation], timeout: defaultTimeout) | ||
} | ||
|
||
assertScale(.nominal, expected: 1) | ||
assertScale(.fair, expected: 1.5) | ||
assertScale(.serious, expected: 2) | ||
assertScale(.critical, expected: 4) | ||
} | ||
} |