| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- //
- // NetworkReachabilityManagerTests.swift
- //
- // Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #if canImport(SystemConfiguration)
- @testable import Alamofire
- import Foundation
- import SystemConfiguration
- import XCTest
- final class NetworkReachabilityManagerTestCase: BaseTestCase {
- // MARK: - Tests - Initialization
- func testThatManagerCanBeInitializedFromHost() {
- // Given, When
- let manager = NetworkReachabilityManager(host: "localhost")
- // Then
- XCTAssertNotNil(manager)
- }
- func testThatManagerCanBeInitializedFromAddress() {
- // Given, When
- let manager = NetworkReachabilityManager()
- // Then
- XCTAssertNotNil(manager)
- }
- func testThatHostManagerIsReachableOnWiFi() {
- // Given, When
- let manager = NetworkReachabilityManager(host: "localhost")
- // Then
- XCTAssertEqual(manager?.status, .reachable(.ethernetOrWiFi))
- XCTAssertEqual(manager?.isReachable, true)
- XCTAssertEqual(manager?.isReachableOnCellular, false)
- XCTAssertEqual(manager?.isReachableOnEthernetOrWiFi, true)
- }
- func testThatHostManagerStartsWithReachableStatus() {
- // Given, When
- let manager = NetworkReachabilityManager(host: "localhost")
- // Then
- XCTAssertEqual(manager?.status, .reachable(.ethernetOrWiFi))
- XCTAssertEqual(manager?.isReachable, true)
- XCTAssertEqual(manager?.isReachableOnCellular, false)
- XCTAssertEqual(manager?.isReachableOnEthernetOrWiFi, true)
- }
- func testThatAddressManagerStartsWithReachableStatus() {
- // Given, When
- let manager = NetworkReachabilityManager()
- // Then
- XCTAssertEqual(manager?.status, .reachable(.ethernetOrWiFi))
- XCTAssertEqual(manager?.isReachable, true)
- XCTAssertEqual(manager?.isReachableOnCellular, false)
- XCTAssertEqual(manager?.isReachableOnEthernetOrWiFi, true)
- }
- func testThatZeroManagerCanBeProperlyRestarted() {
- // Given
- let manager = NetworkReachabilityManager()
- let first = expectation(description: "first listener notified")
- let second = expectation(description: "second listener notified")
- // When
- manager?.startListening { _ in
- first.fulfill()
- }
- wait(for: [first], timeout: timeout)
- manager?.stopListening()
- manager?.startListening { _ in
- second.fulfill()
- }
- wait(for: [second], timeout: timeout)
- // Then
- XCTAssertEqual(manager?.status, .reachable(.ethernetOrWiFi))
- }
- func testThatHostManagerCanBeProperlyRestarted() {
- // Given
- let manager = NetworkReachabilityManager(host: "localhost")
- let first = expectation(description: "first listener notified")
- let second = expectation(description: "second listener notified")
- // When
- manager?.startListening { _ in
- first.fulfill()
- }
- wait(for: [first], timeout: timeout)
- manager?.stopListening()
- manager?.startListening { _ in
- second.fulfill()
- }
- wait(for: [second], timeout: timeout)
- // Then
- XCTAssertEqual(manager?.status, .reachable(.ethernetOrWiFi))
- }
- func testThatHostManagerCanBeDeinitialized() {
- // Given
- let expect = expectation(description: "reachability queue should clear")
- var manager: NetworkReachabilityManager? = NetworkReachabilityManager(host: "localhost")
- weak var weakManager = manager
- // When
- manager?.startListening(onUpdatePerforming: { _ in })
- manager?.stopListening()
- manager?.reachabilityQueue.async { expect.fulfill() }
- manager = nil
- waitForExpectations(timeout: timeout)
- // Then
- XCTAssertNil(manager, "strong reference should be nil")
- XCTAssertNil(weakManager, "weak reference should be nil")
- }
- func testThatAddressManagerCanBeDeinitialized() {
- // Given
- let expect = expectation(description: "reachability queue should clear")
- var manager: NetworkReachabilityManager? = NetworkReachabilityManager()
- weak var weakManager = manager
- // When
- manager?.startListening(onUpdatePerforming: { _ in })
- manager?.stopListening()
- manager?.reachabilityQueue.async { expect.fulfill() }
- manager = nil
- waitForExpectations(timeout: timeout)
- // Then
- XCTAssertNil(manager, "strong reference should be nil")
- XCTAssertNil(weakManager, "weak reference should be nil")
- }
- // MARK: - Listener
- func testThatHostManagerIsNotifiedWhenStartListeningIsCalled() {
- // Given
- guard let manager = NetworkReachabilityManager(host: "store.apple.com") else {
- XCTFail("manager should NOT be nil")
- return
- }
- let expectation = expectation(description: "listener closure should be executed")
- var networkReachabilityStatus: NetworkReachabilityManager.NetworkReachabilityStatus?
- // When
- manager.startListening { status in
- guard networkReachabilityStatus == nil else { return }
- networkReachabilityStatus = status
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout)
- // Then
- XCTAssertEqual(networkReachabilityStatus, .reachable(.ethernetOrWiFi))
- }
- func testThatAddressManagerIsNotifiedWhenStartListeningIsCalled() {
- // Given
- let manager = NetworkReachabilityManager()
- let expectation = expectation(description: "listener closure should be executed")
- var networkReachabilityStatus: NetworkReachabilityManager.NetworkReachabilityStatus?
- // When
- manager?.startListening { status in
- networkReachabilityStatus = status
- expectation.fulfill()
- }
- waitForExpectations(timeout: timeout)
- // Then
- XCTAssertEqual(networkReachabilityStatus, .reachable(.ethernetOrWiFi))
- }
- // MARK: - NetworkReachabilityStatus
- func testThatStatusIsNotReachableStatusWhenReachableFlagIsAbsent() {
- // Given
- let flags: SCNetworkReachabilityFlags = [.connectionOnDemand]
- // When
- let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
- // Then
- XCTAssertEqual(status, .notReachable)
- }
- func testThatStatusIsNotReachableStatusWhenConnectionIsRequired() {
- // Given
- let flags: SCNetworkReachabilityFlags = [.reachable, .connectionRequired]
- // When
- let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
- // Then
- XCTAssertEqual(status, .notReachable)
- }
- func testThatStatusIsNotReachableStatusWhenInterventionIsRequired() {
- // Given
- let flags: SCNetworkReachabilityFlags = [.reachable, .connectionRequired, .interventionRequired]
- // When
- let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
- // Then
- XCTAssertEqual(status, .notReachable)
- }
- func testThatStatusIsReachableOnWiFiStatusWhenConnectionIsNotRequired() {
- // Given
- let flags: SCNetworkReachabilityFlags = [.reachable]
- // When
- let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
- // Then
- XCTAssertEqual(status, .reachable(.ethernetOrWiFi))
- }
- func testThatStatusIsReachableOnWiFiStatusWhenConnectionIsOnDemand() {
- // Given
- let flags: SCNetworkReachabilityFlags = [.reachable, .connectionRequired, .connectionOnDemand]
- // When
- let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
- // Then
- XCTAssertEqual(status, .reachable(.ethernetOrWiFi))
- }
- func testThatStatusIsReachableOnWiFiStatusWhenConnectionIsOnTraffic() {
- // Given
- let flags: SCNetworkReachabilityFlags = [.reachable, .connectionRequired, .connectionOnTraffic]
- // When
- let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
- // Then
- XCTAssertEqual(status, .reachable(.ethernetOrWiFi))
- }
- #if os(iOS) || os(tvOS)
- func testThatStatusIsReachableOnCellularStatusWhenIsWWAN() {
- // Given
- let flags: SCNetworkReachabilityFlags = [.reachable, .isWWAN]
- // When
- let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
- // Then
- XCTAssertEqual(status, .reachable(.cellular))
- }
- func testThatStatusIsNotReachableOnCellularStatusWhenIsWWANAndConnectionIsRequired() {
- // Given
- let flags: SCNetworkReachabilityFlags = [.reachable, .isWWAN, .connectionRequired]
- // When
- let status = NetworkReachabilityManager.NetworkReachabilityStatus(flags)
- // Then
- XCTAssertEqual(status, .notReachable)
- }
- #endif
- }
- #endif
|