Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Remove explicit device selection in Fastlane definition & iPad run (uplift to 1.74.x) #27178

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions ios/brave-ios/App/Brave_iPad.xctestplan

This file was deleted.

2 changes: 0 additions & 2 deletions ios/brave-ios/App/Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@
0A5E04F823FEADA800E5A3E9 /* LaunchScreen.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = LaunchScreen.storyboard; sourceTree = "<group>"; };
0A5E04FA23FEB53700E5A3E9 /* LaunchScreen.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = LaunchScreen.xcassets; sourceTree = "<group>"; };
0A60A1882358AF9E00953CA8 /* Brave.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; name = Brave.xctestplan; path = Client.xcodeproj/Brave.xctestplan; sourceTree = "<group>"; };
0A66550D23E9EA540047EF2A /* Brave_iPad.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = Brave_iPad.xctestplan; sourceTree = "<group>"; };
0A6AC4E824484FBC003D1ED7 /* StoreKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = StoreKit.framework; path = System/Library/Frameworks/StoreKit.framework; sourceTree = SDKROOT; };
2703FC14284FDB6300970FD2 /* ru */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ru; path = Shortcuts/ru.lproj/BrowserIntents.strings; sourceTree = "<group>"; };
270ECFC628381E270089B8B7 /* brave-ios */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = "brave-ios"; path = ..; sourceTree = "<group>"; };
Expand Down Expand Up @@ -790,7 +789,6 @@
270ECFC628381E270089B8B7 /* brave-ios */,
27D67621282DAD3700BCE16E /* BrowserIntents.intentdefinition */,
0A60A1882358AF9E00953CA8 /* Brave.xctestplan */,
0A66550D23E9EA540047EF2A /* Brave_iPad.xctestplan */,
0A24F836233EB5B4004D2F3A /* Configuration */,
270ECFC9283824F00089B8B7 /* iOS */,
2FE7D51E2B509DA50039FBA4 /* ActionExtension */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@
reference = "container:Client.xcodeproj/Brave.xctestplan"
default = "YES">
</TestPlanReference>
<TestPlanReference
reference = "container:Brave_iPad.xctestplan">
</TestPlanReference>
</TestPlans>
</TestAction>
<LaunchAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@
reference = "container:Client.xcodeproj/Brave.xctestplan"
default = "YES">
</TestPlanReference>
<TestPlanReference
reference = "container:Brave_iPad.xctestplan">
</TestPlanReference>
</TestPlans>
</TestAction>
<LaunchAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@
reference = "container:Client.xcodeproj/Brave.xctestplan"
default = "YES">
</TestPlanReference>
<TestPlanReference
reference = "container:Brave_iPad.xctestplan">
</TestPlanReference>
</TestPlans>
</TestAction>
<LaunchAction
Expand Down
2 changes: 1 addition & 1 deletion ios/brave-ios/App/iOS/Delegates/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

fileprivate func setUserAgent() {
let userAgent = UserAgent.userAgentForDesktopMode
let userAgent = UserAgent.userAgentForIdiom()

// Set the favicon fetcher, and the image loader.
// This only needs to be done once per runtime. Note that we use defaults here that are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class BraveWebView: WKWebView {

isFindInteractionEnabled = true

customUserAgent = UserAgent.userAgentForDesktopMode
customUserAgent = UserAgent.userAgentForIdiom()
if #available(iOS 16.4, *) {
isInspectable = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class BraveSearchManager: NSObject {
request.setValue($0.value, forHTTPHeaderField: $0.key)
}

request.setValue(UserAgent.userAgentForDesktopMode, forHTTPHeaderField: "User-Agent")
request.setValue(UserAgent.userAgentForIdiom(), forHTTPHeaderField: "User-Agent")

let session = URLSession(configuration: .ephemeral, delegate: authManager, delegateQueue: .main)

Expand Down Expand Up @@ -203,7 +203,7 @@ class BraveSearchManager: NSObject {
)

// Must be set, without it the fallback results may be not retrieved correctly.
request.addValue(UserAgent.userAgentForDesktopMode, forHTTPHeaderField: "User-Agent")
request.addValue(UserAgent.userAgentForIdiom(), forHTTPHeaderField: "User-Agent")

request.addValue(
"text/html;charset=UTF-8, text/plain;charset=UTF-8",
Expand Down
4 changes: 2 additions & 2 deletions ios/brave-ios/Sources/Brave/Frontend/Browser/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ class Tab: NSObject {
return
}

let desktopMode = userAgentOverrides[baseDomain] ?? UserAgent.shouldUseDesktopMode
let desktopMode = userAgentOverrides[baseDomain] ?? UserAgent.shouldUseDesktopMode()
webView.customUserAgent = desktopMode ? UserAgent.desktop : UserAgent.mobile
}

Expand Down Expand Up @@ -954,7 +954,7 @@ class Tab: NSObject {
userAgentOverrides[urlString] = !siteOverride
} else {
// First time switch, adding the basedomain to dictionary with flipped value.
userAgentOverrides[urlString] = !UserAgent.shouldUseDesktopMode
userAgentOverrides[urlString] = !UserAgent.shouldUseDesktopMode()
}
}

Expand Down
2 changes: 1 addition & 1 deletion ios/brave-ios/Sources/Playlist/AVKitExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ extension AVAsset {
}

public static var defaultOptions: [String: Any] {
let userAgent = UserAgent.shouldUseDesktopMode ? UserAgent.desktop : UserAgent.mobile
let userAgent = UserAgent.userAgentForIdiom()
var options: [String: Any] = [:]
options[AVURLAssetHTTPUserAgentKey] = userAgent
return options
Expand Down
10 changes: 2 additions & 8 deletions ios/brave-ios/Sources/Playlist/PlaylistDownloadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -715,10 +715,7 @@ private class PlaylistFileDownloadManager: NSObject, URLSessionDownloadDelegate
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range
request.addValue("bytes=0-", forHTTPHeaderField: "Range")
request.addValue(UUID().uuidString, forHTTPHeaderField: "X-Playback-Session-Id")
request.addValue(
UserAgent.shouldUseDesktopMode ? UserAgent.desktop : UserAgent.mobile,
forHTTPHeaderField: "User-Agent"
)
request.addValue(UserAgent.userAgentForIdiom(), forHTTPHeaderField: "User-Agent")
return request
}()

Expand Down Expand Up @@ -1036,10 +1033,7 @@ private class PlaylistDataDownloadManager: NSObject, URLSessionDataDelegate {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range
request.addValue("bytes=0-", forHTTPHeaderField: "Range")
request.addValue(UUID().uuidString, forHTTPHeaderField: "X-Playback-Session-Id")
request.addValue(
UserAgent.shouldUseDesktopMode ? UserAgent.desktop : UserAgent.mobile,
forHTTPHeaderField: "User-Agent"
)
request.addValue(UserAgent.userAgentForIdiom(), forHTTPHeaderField: "User-Agent")
return request
}()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,7 @@ public class PlaylistMediaStreamer {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range
request.addValue("bytes=0-1", forHTTPHeaderField: "Range")
request.addValue(UUID().uuidString, forHTTPHeaderField: "X-Playback-Session-Id")
request.addValue(
UserAgent.shouldUseDesktopMode ? UserAgent.desktop : UserAgent.mobile,
forHTTPHeaderField: "User-Agent"
)
request.addValue(UserAgent.userAgentForIdiom(), forHTTPHeaderField: "User-Agent")
return request
}()

Expand Down
15 changes: 8 additions & 7 deletions ios/brave-ios/Sources/UserAgent/UserAgent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ public struct UserAgent {
public static let mobile = UserAgentBuilder().build(desktopMode: false)
public static let desktop = UserAgentBuilder().build(desktopMode: true)

public static var userAgentForDesktopMode: String {
UserAgent.shouldUseDesktopMode ? UserAgent.desktop : UserAgent.mobile
public static func userAgentForIdiom(
_ idiom: UIUserInterfaceIdiom = UIDevice.current.userInterfaceIdiom
) -> String {
return shouldUseDesktopMode(idiom: idiom) ? UserAgent.desktop : UserAgent.mobile
}

public static var shouldUseDesktopMode: Bool {
if UIDevice.current.userInterfaceIdiom == .pad {
return Preferences.UserAgent.alwaysRequestDesktopSite.value
}
return false
public static func shouldUseDesktopMode(
idiom: UIUserInterfaceIdiom = UIDevice.current.userInterfaceIdiom
) -> Bool {
return idiom == .pad ? Preferences.UserAgent.alwaysRequestDesktopSite.value : false
}
}
55 changes: 9 additions & 46 deletions ios/brave-ios/Tests/UserAgentTests/UserAgentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,63 +109,26 @@ class UserAgentTests: XCTestCase {
waitForExpectations(timeout: 60, handler: nil)
}

// MARK: iPad only tests - must run on iPad

func testDesktopUserAgent() {
// Must run on iPad iOS 13+
if UIDevice.current.userInterfaceIdiom != .pad
|| ProcessInfo().operatingSystemVersion.majorVersion < 13
{
return
}
func testDesktopUserAgentOnPad() {
Preferences.UserAgent.alwaysRequestDesktopSite.value = true

XCTAssertTrue(desktopUARegex(UserAgent.desktop), "User agent computes correctly.")

let expectation = self.expectation(description: "Found Firefox user agent")
let webView = BraveWebView(frame: .zero, isPrivate: false)
let userAgent = UserAgent.userAgentForIdiom(.pad)

webView.evaluateSafeJavaScript(
functionName: "navigator.userAgent",
contentWorld: .page,
asFunction: false
) { result, error in
let userAgent = result as! String
if self.mobileUARegex(userAgent) || !self.desktopUARegex(userAgent) {
XCTFail("User agent did not match expected pattern! \(userAgent)")
}
expectation.fulfill()
if self.mobileUARegex(userAgent) || !self.desktopUARegex(userAgent) {
XCTFail("User agent did not match expected pattern! \(userAgent)")
}

waitForExpectations(timeout: 60, handler: nil)
}

func testIpadMobileUserAgent() {
// Must run on iPad iOS 13+
if UIDevice.current.userInterfaceIdiom != .pad
|| ProcessInfo().operatingSystemVersion.majorVersion < 13
{
return
}

func testMobileUserAgentOnPad() {
Preferences.UserAgent.alwaysRequestDesktopSite.value = false

XCTAssertTrue(mobileUARegex(UserAgent.mobile), "User agent computes correctly.")
let userAgent = UserAgent.userAgentForIdiom(.pad)

let expectation = self.expectation(description: "Found Firefox user agent")
let webView = BraveWebView(frame: .zero, isPrivate: false)

webView.evaluateSafeJavaScript(
functionName: "navigator.userAgent",
contentWorld: .page,
asFunction: false
) { result, error in
let userAgent = result as! String
if !self.mobileUARegex(userAgent) || self.desktopUARegex(userAgent) {
XCTFail("User agent did not match expected pattern! \(userAgent)")
}
expectation.fulfill()
if !self.mobileUARegex(userAgent) || self.desktopUARegex(userAgent) {
XCTFail("User agent did not match expected pattern! \(userAgent)")
}

waitForExpectations(timeout: 60, handler: nil)
}
}
10 changes: 0 additions & 10 deletions ios/brave-ios/fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,8 @@ platform :ios do
defaultScanParams = scan_params()
run_tests(defaultScanParams.merge!({
test_without_building: true,
devices: ["iPhone 16 (18.0)"],
ensure_devices_found: true,
skip_testing: skipped_tests()
}))

run_tests(defaultScanParams.merge!({
test_without_building: true,
devices: ["iPad (10th generation) (18.0)"],
ensure_devices_found: true,
output_files: "junit-ipad.xml",
testplan: "Brave_iPad"
}))
end

desc "Creates a Brave Beta build for TestFlight."
Expand Down
Loading