From 4baa50b67586ca4966cf1e8202e3acf326f7e2ed Mon Sep 17 00:00:00 2001 From: Daniel Bernal Date: Tue, 17 Dec 2024 18:34:35 +0100 Subject: [PATCH] Bugfix: Fix Youtube Internal links when 'Watching on Youtube' from Duck Player (#3733) Task/Issue URL: https://app.asana.com/0/1204099484721401/1208930843675395/f Tech Design URL: CC: Description: Fixes an issue causing Youtube internal links to open Duck Player --- .../DuckPlayerNavigationHandler.swift | 10 ++++++++++ ...YoutublePlayerNavigationHandlerTests.swift | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift b/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift index 05160f8381..13afd90eb0 100644 --- a/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift +++ b/DuckDuckGo/DuckPlayer/DuckPlayerNavigationHandler.swift @@ -936,6 +936,16 @@ extension DuckPlayerNavigationHandler: DuckPlayerNavigationHandling { return false } + // 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 + } + // Redirect to Duck Player if enabled if url.isYoutubeWatch && duckPlayerMode == .enabled && !isDuckPlayerRedirect(url: url) { redirectToDuckPlayerVideo(url: url, webView: webView) diff --git a/DuckDuckGoTests/YoutublePlayerNavigationHandlerTests.swift b/DuckDuckGoTests/YoutublePlayerNavigationHandlerTests.swift index 4b968fcd68..3ae7bc73ea 100644 --- a/DuckDuckGoTests/YoutublePlayerNavigationHandlerTests.swift +++ b/DuckDuckGoTests/YoutublePlayerNavigationHandlerTests.swift @@ -582,4 +582,24 @@ class DuckPlayerNavigationHandlerTests: XCTestCase { XCTAssertNil(tabNavigator.openedURL, "No new tabs should open") } + @MainActor + func testHandleDelegateNavigation_YoutubeWatchURLWithDuckPlayerEnabledAndSameVideoNavigation_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 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) + + // Act + let shouldCancel = handler.handleDelegateNavigation(navigationAction: navigationAction, webView: mockWebView) + + // Assert + XCTAssertFalse(shouldCancel, "Expected navigation NOT to be cancelled as it's Youtube Internal navigation") + } + }