Revision control

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
import UIKit
6
import XCTest
7
8
import Shared
9
import Storage
10
import WebKit
11
@testable import Client
12
13
class ClientTests: XCTestCase {
14
15
func testSyncUA() {
16
let ua = UserAgent.syncUserAgent
17
let device = DeviceInfo.deviceModel()
18
let systemVersion = UIDevice.current.systemVersion
19
let expectedRegex = "^Firefox-iOS-Sync/[0-9\\.]+b[0-9]* \\(\(device); iPhone OS \(systemVersion)\\) \\([-_A-Za-z0-9= \\(\\)]+\\)$"
20
let loc = ua.range(of: expectedRegex, options: .regularExpression)
21
XCTAssertTrue(loc != nil, "Sync UA is as expected. Was \(ua)")
22
}
23
24
// Simple test to make sure the WKWebView UA matches the expected FxiOS pattern.
25
func testUserAgent() {
26
let compare: (String) -> Bool = { ua in
27
let range = ua.range(of: "^Mozilla/5\\.0 \\(.+\\) AppleWebKit/[0-9\\.]+ \\(KHTML, like Gecko\\)", options: .regularExpression)
28
return range != nil
29
}
30
31
XCTAssertTrue(compare(UserAgent.defaultUserAgent()), "User agent computes correctly.")
32
//XCTAssertTrue(compare(UserAgent.cachedUserAgent(checkiOSVersion: true)!), "User agent is cached correctly.")
33
34
let expectation = self.expectation(description: "Found Firefox user agent")
35
36
let webView = WKWebView()
37
webView.evaluateJavaScript("navigator.userAgent") { result, error in
38
let userAgent = result as! String
39
if compare(userAgent) {
40
expectation.fulfill()
41
} else {
42
XCTFail("User agent did not match expected pattern! \(userAgent)")
43
}
44
}
45
46
waitForExpectations(timeout: 5, handler: nil)
47
}
48
49
func testDesktopUserAgent() {
50
let compare: (String) -> Bool = { ua in
51
let range = ua.range(of: "^Mozilla/5\\.0 \\(Macintosh; Intel Mac OS X [0-9\\.]+\\)", options: .regularExpression)
52
return range != nil
53
}
54
55
XCTAssertTrue(compare(UserAgent.desktopUserAgent()), "Desktop user agent computes correctly.")
56
}
57
}