Skip to content

Commit

Permalink
Update test
Browse files Browse the repository at this point in the history
  • Loading branch information
afterxleep committed Dec 19, 2024
1 parent cdeddba commit 0d1b484
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
18 changes: 11 additions & 7 deletions DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,13 @@ final class DuckPlayerNavigationHandler: NSObject {
duckPlayerModeCancellable = nil
}

/// Checks if a URL contains a hash
///
/// - Parameter url: The `URL` used to determine the tab type.
private func urlContainsHash(_ url: URL) -> Bool {
return url.fragment != nil && !url.fragment!.isEmpty
}

}

extension DuckPlayerNavigationHandler: DuckPlayerNavigationHandling {
Expand Down Expand Up @@ -937,13 +944,10 @@ extension DuckPlayerNavigationHandler: DuckPlayerNavigationHandling {
}

// Allow Youtube's internal navigation when DuckPlayer is enabled and user is watching on Youtube
// This is to prevent DuckPlayer from interfering with Youtube's internal navigation for search and settings
// https://app.asana.com/0/1204099484721401/1208930843675395/f
if let (destinationVideoID, _) = url.youtubeVideoParams,
let (originVideoID, _) = webView.url?.youtubeVideoParams,
destinationVideoID == originVideoID,
duckPlayerMode == .enabled {
return false
// Youtube uses hashes to navigate within some settings
// This allows any navigation that includes a hash # (#searching, #bottom-sheet, etc)
if urlContainsHash(url), url.isYoutubeWatch {
return false
}

// Redirect to Duck Player if enabled
Expand Down
27 changes: 22 additions & 5 deletions DuckDuckGoTests/YoutublePlayerNavigationHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -583,20 +583,37 @@ class DuckPlayerNavigationHandlerTests: XCTestCase {
}

@MainActor
func testHandleDelegateNavigation_YoutubeWatchURLWithDuckPlayerEnabledAndSameVideoNavigation_ReturnsFalse() async {
func testHandleDelegateNavigation_YoutubeInternalNavigation_ReturnsFalse() async {
// Arrange
let youtubeURL = URL(string: "https://www.youtube.com/watch?v=abc123")!
let youtubeInternalURL = URL(string: "https://www.youtube.com/watch?v=abc123&settings")!
let youtubeURL = URL(string: "https://www.youtube.com/watch?v=abc123#searching")!
let request = URLRequest(url: youtubeURL)
let mockFrameInfo = MockFrameInfo(isMainFrame: true)
let navigationAction = MockNavigationAction(request: request, targetFrame: mockFrameInfo)
playerSettings.mode = .enabled
featureFlagger.enabledFeatures = [.duckPlayer, .duckPlayerOpenInNewTab]

mockWebView.setCurrentURL(youtubeInternalURL)
mockWebView.setCurrentURL(youtubeURL)

// Act
let shouldCancel = handler.handleDelegateNavigation(navigationAction: navigationAction, webView: mockWebView)
var shouldCancel = handler.handleDelegateNavigation(navigationAction: navigationAction, webView: mockWebView)

// Assert
XCTAssertFalse(shouldCancel, "Expected navigation NOT to be cancelled as it's Youtube Internal navigation")

// Arrange
playerSettings.mode = .disabled

// Act
shouldCancel = handler.handleDelegateNavigation(navigationAction: navigationAction, webView: mockWebView)

// Assert
XCTAssertFalse(shouldCancel, "Expected navigation NOT to be cancelled as it's Youtube Internal navigation")

// Arrange
featureFlagger.enabledFeatures = [.duckPlayer]

// Act
shouldCancel = handler.handleDelegateNavigation(navigationAction: navigationAction, webView: mockWebView)

// Assert
XCTAssertFalse(shouldCancel, "Expected navigation NOT to be cancelled as it's Youtube Internal navigation")
Expand Down

0 comments on commit 0d1b484

Please sign in to comment.