Skip to content

Commit

Permalink
Added FXIOS-7438 [v120] xcode15 unit tests (mozilla-mobile#16619)
Browse files Browse the repository at this point in the history
* Fix URL interface

* Fix reader mode test

* Temporarily remove iOS 17 code
  • Loading branch information
OrlaM authored Sep 27, 2023
1 parent 1444349 commit c0445f2
Show file tree
Hide file tree
Showing 72 changed files with 112 additions and 105 deletions.
2 changes: 1 addition & 1 deletion BrowserKit/Sources/Common/Extensions/URLExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Foundation

extension URL {
/// Temporary init that will be removed with the update to XCode 15 where this URL API is available
public init?(string: String, encodingInvalidCharacters: Bool) {
public init?(string: String, invalidCharacters: Bool) {
self.init(string: string)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ actor DefaultFaviconURLCache: FaviconURLCache {

func getURLFromCache(cacheKey: String) async throws -> URL {
guard let favicon = urlCache[cacheKey],
let url = URL(string: favicon.faviconURL, encodingInvalidCharacters: false)
let url = URL(string: favicon.faviconURL, invalidCharacters: false)
else { throw SiteImageError.noURLInCache }

// Update the element in the cache so it's time to expire is reset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct DefaultFaviconURLFetcher: FaviconURLFetcher {
if let refresh = meta["http-equiv"], refresh == "Refresh",
let content = meta["content"],
let index = content.range(of: "URL="),
let url = URL(string: String(content[index.upperBound...]), encodingInvalidCharacters: false) {
let url = URL(string: String(content[index.upperBound...]), invalidCharacters: false) {
reloadURL = url
}
}
Expand All @@ -63,7 +63,7 @@ struct DefaultFaviconURLFetcher: FaviconURLFetcher {

// Fallback to the favicon at the root of the domain
// This is a fall back because it's generally low res
if let faviconURL = URL(string: siteURL.absoluteString + "/favicon.ico", encodingInvalidCharacters: false) {
if let faviconURL = URL(string: siteURL.absoluteString + "/favicon.ico", invalidCharacters: false) {
return faviconURL
}

Expand Down
4 changes: 2 additions & 2 deletions BrowserKit/Sources/SiteImageView/SiteImageHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public class DefaultSiteImageHandler: SiteImageHandler {
var imageModel = site

// urlStringRequest possibly cannot be a URL
if let siteURL = URL(string: site.siteURLString ?? "", encodingInvalidCharacters: false) {
if let siteURL = URL(string: site.siteURLString ?? "", invalidCharacters: false) {
let domain = generateDomainURL(siteURL: siteURL)
imageModel.siteURL = siteURL
imageModel.domain = domain
}

imageModel.cacheKey = generateCacheKey(siteURL: URL(string: site.siteURLString ?? "", encodingInvalidCharacters: false),
imageModel.cacheKey = generateCacheKey(siteURL: URL(string: site.siteURLString ?? "", invalidCharacters: false),
faviconURL: imageModel.faviconURL,
type: imageModel.expectedImageType)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ final class BundleImageFetcherTests: XCTestCase {
}

func testValidData_returnImage() {
let expectedImage = UIImage()
let expectedImage = mockImage()
bundleDataProvider.imageToReturn = expectedImage
bundleDataProvider.pathToReturn = "a/path/to/image"
bundleDataProvider.data = generateHTMLData(string: MockBundleData.validData)
Expand All @@ -92,15 +92,14 @@ final class BundleImageFetcherTests: XCTestCase {

do {
let result = try subject.getImageFromBundle(domain: domain)
XCTAssertEqual(expectedImage, result)
XCTAssertEqual(expectedImage.size, result.size)
} catch {
XCTFail("Should have succeeded")
}
}

func testValidData_whenDomainNotPresent_throwsError() {
let expectedImage = UIImage()
bundleDataProvider.imageToReturn = expectedImage
bundleDataProvider.imageToReturn = mockImage()
bundleDataProvider.pathToReturn = "a/path/to/image"
bundleDataProvider.data = generateHTMLData(string: MockBundleData.validData)
let subject = DefaultBundleImageFetcher(bundleDataProvider: bundleDataProvider)
Expand Down Expand Up @@ -134,6 +133,15 @@ final class BundleImageFetcherTests: XCTestCase {
XCTFail("Should have failed with BundleError type")
}
}

func mockImage() -> UIImage {
let rect = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 100, height: 100))
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image ?? UIImage()
}
}

private enum MockBundleData {
Expand Down
2 changes: 1 addition & 1 deletion Client/Coordinators/Router/RouteBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ final class RouteBuilder {
if userActivity.activityType == CSSearchableItemActionType {
guard let userInfo = userActivity.userInfo,
let urlString = userInfo[CSSearchableItemActivityIdentifier] as? String,
let url = URL(string: urlString, encodingInvalidCharacters: false)
let url = URL(string: urlString, invalidCharacters: false)
else {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion Client/Coordinators/Router/URLScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct URLScanner {
guard let scheme = urlComponents.scheme, urlSchemes.contains(scheme) else { return nil }
self.scheme = scheme
self.host = urlComponents.host ?? ""
self.components = URL(string: urlComponents.path, encodingInvalidCharacters: false)?.pathComponents ?? []
self.components = URL(string: urlComponents.path, invalidCharacters: false)?.pathComponents ?? []
self.queries = urlComponents.queryItems ?? []
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class GleanPlumbMessageManager: GleanPlumbMessageManagerProtocol {

// Create the message action URL.
let urlString = action.hasPrefix("://") ? URL.mozInternalScheme + action : action
guard let url = URL(string: urlString, encodingInvalidCharacters: false) else {
guard let url = URL(string: urlString, invalidCharacters: false) else {
self.onMalformedMessage(id: message.id, surface: message.surface)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class ExperimentsSettingsViewController: UIViewController {
private func loadRemoteExperiments(sender: AnyObject) {
guard
let text = experimentsView.customRemoteSettingsTextField.text,
let url = URL(string: text, encodingInvalidCharacters: false),
let url = URL(string: text, invalidCharacters: false),
let data = try? String(contentsOf: url)
else { return }

Expand Down
2 changes: 1 addition & 1 deletion Client/Extensions/UIPasteboard+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension UIPasteboard {

private var syncURL: URL? {
return UIPasteboard.general.string.flatMap {
guard let url = URL(string: $0, encodingInvalidCharacters: false),
guard let url = URL(string: $0, invalidCharacters: false),
url.isWebPage()
else { return nil }
return url
Expand Down
4 changes: 2 additions & 2 deletions Client/Frontend/Accessors/HomePageAccessors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class NewTabHomePageAccessors {
static func getHomePage(_ prefs: Prefs) -> URL? {
let string = prefs.stringForKey(PrefsKeys.NewTabCustomUrlPrefKey) ?? getDefaultHomePageString(prefs)
guard let urlString = string else { return nil }
return URL(string: urlString, encodingInvalidCharacters: false)
return URL(string: urlString, invalidCharacters: false)
}

static func getDefaultHomePageString(_ prefs: Prefs) -> String? {
Expand All @@ -26,6 +26,6 @@ class HomeButtonHomePageAccessors {
static func getHomePage(_ prefs: Prefs) -> URL? {
let string = prefs.stringForKey(PrefsKeys.HomeButtonHomePageURL)
guard let urlString = string else { return nil }
return URL(string: urlString, encodingInvalidCharacters: false)
return URL(string: urlString, invalidCharacters: false)
}
}
2 changes: 1 addition & 1 deletion Client/Frontend/Browser/BackForwardTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class BackForwardTableViewCell: UITableViewCell, ReusableCell, ThemeApplicable {
func configure(viewModel: BackForwardCellViewModel, theme: Theme) {
self.viewModel = viewModel

if let url = URL(string: viewModel.site.url, encodingInvalidCharacters: false),
if let url = URL(string: viewModel.site.url, invalidCharacters: false),
InternalURL(url)?.isAboutHomeURL == true {
faviconView.manuallySetImage(UIImage(named: ImageIdentifiers.firefoxFavicon) ?? UIImage())
} else {
Expand Down
2 changes: 1 addition & 1 deletion Client/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1961,7 +1961,7 @@ extension BrowserViewController: QRCodeViewControllerDelegate {
case .some(.link(let url)):
UIApplication.shared.open(url, options: [:], completionHandler: nil)
case .some(.phoneNumber(let phoneNumber)):
if let url = URL(string: "tel:\(phoneNumber)", encodingInvalidCharacters: false) {
if let url = URL(string: "tel:\(phoneNumber)", invalidCharacters: false) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
defaultAction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ extension BrowserViewController: URLBarDelegate {
let range = urlString.range(of: "%s") {
urlString.replaceSubrange(range, with: escapedQuery)

if let url = URL(string: urlString, encodingInvalidCharacters: false) {
if let url = URL(string: urlString, invalidCharacters: false) {
self.finishEditingAndSubmit(url, visitType: VisitType.typed, forTab: currentTab)
return
}
Expand Down
2 changes: 1 addition & 1 deletion Client/Frontend/Browser/ClipboardBarDisplayHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class ClipboardBarDisplayHandler: NSObject, URLChangeDelegate {
return true
}

if let url = URL(string: clipboardURL, encodingInvalidCharacters: false),
if let url = URL(string: clipboardURL, invalidCharacters: false),
tabManager?.getTabFor(url) != nil {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion Client/Frontend/Browser/MainMenuActionHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ class MainMenuActionHelper: PhotonActionSheetProtocol,

var iconURL: URL?
if let str = rustAccount.userProfile?.avatarUrl,
let url = URL(string: str, encodingInvalidCharacters: false) {
let url = URL(string: str, invalidCharacters: false) {
iconURL = url
}
let iconType: PhotonActionSheetIconType = needsReAuth ? .Image : .URL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class OpenWithSettingsViewController: ThemedTableViewController {
}

func canOpenMailScheme(_ scheme: String) -> Bool {
if let url = URL(string: scheme, encodingInvalidCharacters: false) {
if let url = URL(string: scheme, invalidCharacters: false) {
return UIApplication.shared.canOpenURL(url)
}
return false
Expand Down
4 changes: 2 additions & 2 deletions Client/Frontend/Browser/SearchEngines/OpenSearchEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class OpenSearchEngine: NSObject, NSSecureCoding {
private func isSearchURLForEngine(_ url: URL?) -> Bool {
guard let urlHost = url?.shortDisplayString,
let queryEndIndex = searchTemplate.range(of: "?")?.lowerBound,
let templateURL = URL(string: String(searchTemplate[..<queryEndIndex]), encodingInvalidCharacters: false)
let templateURL = URL(string: String(searchTemplate[..<queryEndIndex]), invalidCharacters: false)
else { return false }
return urlHost == templateURL.shortDisplayString
}
Expand All @@ -163,7 +163,7 @@ class OpenSearchEngine: NSObject, NSSecureCoding {
let urlString = encodedSearchTemplate
.replacingOccurrences(of: searchTermComponent, with: escapedQuery, options: .literal, range: nil)
.replacingOccurrences(of: localeTermComponent, with: localeString, options: .literal, range: nil)
return URL(string: urlString, encodingInvalidCharacters: false)
return URL(string: urlString, invalidCharacters: false)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class OpenSearchParser {

let uiImage: UIImage
if let imageElement = largestImageElement,
let imageURL = URL(string: imageElement.stringValue, encodingInvalidCharacters: false),
let imageURL = URL(string: imageElement.stringValue, invalidCharacters: false),
let imageData = try? Data(contentsOf: imageURL),
let image = UIImage(data: imageData) {
uiImage = image
Expand Down
4 changes: 2 additions & 2 deletions Client/Frontend/Browser/SearchViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -532,13 +532,13 @@ class SearchViewController: SiteTableViewController,
if let site = data[indexPath.row] {
recordSearchListSelectionTelemetry(type: .bookmarksAndHistory,
isBookmark: site.bookmarked ?? false)
if let url = URL(string: site.url, encodingInvalidCharacters: false) {
if let url = URL(string: site.url, invalidCharacters: false) {
searchDelegate?.searchViewController(self, didSelectURL: url, searchTerm: nil)
}
}
case .searchHighlights:
if let urlString = searchHighlights[indexPath.row].urlString,
let url = URL(string: urlString, encodingInvalidCharacters: false) {
let url = URL(string: urlString, invalidCharacters: false) {
recordSearchListSelectionTelemetry(type: .searchHighlights)
searchDelegate?.searchViewController(self, didSelectURL: url, searchTerm: nil)
}
Expand Down
6 changes: 3 additions & 3 deletions Client/Frontend/Browser/URIFixup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import Shared

class URIFixup {
static func getURL(_ entry: String) -> URL? {
if let url = URL(string: entry, encodingInvalidCharacters: false),
if let url = URL(string: entry, invalidCharacters: false),
InternalURL.isValid(url: url) {
return URL(string: entry, encodingInvalidCharacters: false)
return URL(string: entry, invalidCharacters: false)
}

let trimmed = entry.trimmingCharacters(in: .whitespacesAndNewlines)
Expand Down Expand Up @@ -49,7 +49,7 @@ class URIFixup {
string = replaceHashMarks(url: string)
}

guard let url = URL(string: string, encodingInvalidCharacters: false) else { return nil }
guard let url = URL(string: string, invalidCharacters: false) else { return nil }

var components = URLComponents(url: url, resolvingAgainstBaseURL: false)
if AppConstants.punyCode {
Expand Down
2 changes: 1 addition & 1 deletion Client/Frontend/Home/HistoryHighlights/HighlightItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extension HistoryHighlight: HighlightItem {
}

var siteUrl: URL? {
return URL(string: url, encodingInvalidCharacters: false)
return URL(string: url, invalidCharacters: false)
}

var urlString: String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ private let searchLimit = 1000

extension HistoryHighlight {
var urlFromString: URL? {
return URL(string: url, encodingInvalidCharacters: false)
return URL(string: url, invalidCharacters: false)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Client/Frontend/Home/HomePanelType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ enum HomePanelType: Int {
case topSites = 0

var internalUrl: URL {
let aboutUrl: URL! = URL(string: "\(InternalURL.baseUrl)/\(AboutHomeHandler.path)", encodingInvalidCharacters: false)
let aboutUrl: URL! = URL(string: "\(InternalURL.baseUrl)/\(AboutHomeHandler.path)", invalidCharacters: false)
return URL(string: "#panel=\(self.rawValue)", relativeTo: aboutUrl)!
}
}
2 changes: 1 addition & 1 deletion Client/Frontend/Home/HomepageContextMenuHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class HomepageContextMenuHelper: HomepageContextMenuProtocol {
/// - Returns: Share action
private func getShareAction(site: Site, sourceView: UIView?) -> PhotonRowActions {
return SingleActionViewModel(title: .ShareContextMenuTitle, iconString: ImageIdentifiers.share, tapHandler: { _ in
guard let url = URL(string: site.url, encodingInvalidCharacters: false) else { return }
guard let url = URL(string: site.url, invalidCharacters: false) else { return }

if CoordinatorFlagManager.isShareExtensionCoordinatorEnabled {
self.browserNavigationHandler?.showShareExtension(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ extension RecentlySavedViewModel: HomepageSectionHandler {
value: .recentlySavedBookmarkItemAction,
extras: TelemetryWrapper.getOriginExtras(isZeroSearch: isZeroSearch))
} else if let item = recentItems[safe: indexPath.row] as? ReadingListItem,
let url = URL(string: item.url, encodingInvalidCharacters: false),
let url = URL(string: item.url, invalidCharacters: false),
let encodedUrl = url.encodeReaderModeURL(WebServer.sharedInstance.baseReaderModeURL()) {
let visitType = VisitType.bookmark
libraryPanelDelegate?.libraryPanel(didSelectURL: encodedUrl, visitType: visitType)
Expand Down
2 changes: 1 addition & 1 deletion Client/Frontend/Home/TopSites/Cell/TopSiteItemCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class TopSiteItemCell: UICollectionViewCell, ReusableCell {
var imageURL: URL?

if let site = topSite.site as? SponsoredTile {
imageURL = URL(string: site.imageURL, encodingInvalidCharacters: false)
imageURL = URL(string: site.imageURL, invalidCharacters: false)
}
let viewModel = FaviconImageViewModel(siteURLString: urlRequest,
faviconURL: imageURL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ class ContileProvider: ContileProviderInterface, URLCaching, FeatureFlaggable {

private var resourceEndpoint: URL? {
if featureFlags.isCoreFeatureEnabled(.useStagingContileAPI) {
return URL(string: ContileProvider.contileStagingResourceEndpoint, encodingInvalidCharacters: false)
return URL(string: ContileProvider.contileStagingResourceEndpoint, invalidCharacters: false)
}
return URL(string: ContileProvider.contileProdResourceEndpoint, encodingInvalidCharacters: false)
return URL(string: ContileProvider.contileProdResourceEndpoint, invalidCharacters: false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct WallpaperCollection: Codable, Equatable {

var learnMoreUrl: URL? {
guard let urlString = learnMoreURLString else { return nil }
return URL(string: urlString, encodingInvalidCharacters: false)
return URL(string: urlString, invalidCharacters: false)
}

/// Wallpaper collections availability:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class BookmarkDetailPanel: SiteTableViewController {
}

private func isBookmarkItemURLValid() -> Bool {
let url = URL(string: bookmarkItemURL ?? "", encodingInvalidCharacters: false)
let url = URL(string: bookmarkItemURL ?? "", invalidCharacters: false)
return url?.schemeIsValid == true && url?.host != nil
}

Expand Down
2 changes: 1 addition & 1 deletion Client/Frontend/Library/Bookmarks/BookmarksPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ class BookmarksPanel: SiteTableViewController,
updatePanelState(newState: .bookmarks(state: .inFolder))
if CoordinatorFlagManager.isLibraryCoordinatorEnabled {
if let itemData = bookmarkCell as? BookmarkItemData,
let url = URL(string: itemData.url, encodingInvalidCharacters: false) {
let url = URL(string: itemData.url, invalidCharacters: false) {
libraryPanelDelegate?.libraryPanel(didSelectURL: url, visitType: .bookmark)
} else {
guard let folder = bookmarkCell as? FxBookmarkNode else { return }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class SearchGroupedItemsViewController: UIViewController, UITableViewDelegate, T
}

private func handleSiteItemTapped(site: Site) {
guard let url = URL(string: site.url, encodingInvalidCharacters: false) else {
guard let url = URL(string: site.url, invalidCharacters: false) else {
logger.log("Couldn't navigate to site",
level: .warning,
category: .library)
Expand Down
2 changes: 1 addition & 1 deletion Client/Frontend/Library/HistoryPanel/HistoryPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ extension HistoryPanel: UITableViewDelegate {
}

private func handleSiteItemTapped(site: Site) {
guard let url = URL(string: site.url, encodingInvalidCharacters: false) else {
guard let url = URL(string: site.url, invalidCharacters: false) else {
self.logger.log("Couldn't navigate to site",
level: .warning,
category: .library)
Expand Down
Loading

0 comments on commit c0445f2

Please sign in to comment.