From a756a49988e5dd2a94d09aaa4661802de237ef52 Mon Sep 17 00:00:00 2001 From: Serg Date: Tue, 10 Feb 2026 16:24:58 -0500 Subject: [PATCH] [Android] Fix for NTP idle snackbar shows when opening external links from other apps. (#33847) [Android] Fix for NTP idle snackbar shows when opening external links from other apps. When the NTP idle experiment (variants B/C) is active and the inactivity threshold has been reached, opening a link from an external app should not show an NTP+snackbar. Resolves: https://github.com/brave/brave-browser/issues/52746 --- .../tasks/BraveReturnToChromeUtil.java | 8 +- android/junit/BUILD.gn | 6 ++ .../BraveReturnToChromeUtilUnitTest.java | 81 +++++++++++++++++++ test/BUILD.gn | 1 + 4 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 android/junit/src/org/chromium/chrome/browser/tasks/BraveReturnToChromeUtilUnitTest.java diff --git a/android/java/org/chromium/chrome/browser/tasks/BraveReturnToChromeUtil.java b/android/java/org/chromium/chrome/browser/tasks/BraveReturnToChromeUtil.java index 9b4c0b5db9d..597080b8253 100644 --- a/android/java/org/chromium/chrome/browser/tasks/BraveReturnToChromeUtil.java +++ b/android/java/org/chromium/chrome/browser/tasks/BraveReturnToChromeUtil.java @@ -9,6 +9,7 @@ import android.content.Intent; import android.os.Bundle; import org.chromium.base.BravePreferenceKeys; +import org.chromium.base.IntentUtils; import org.chromium.build.annotations.NullMarked; import org.chromium.build.annotations.Nullable; import org.chromium.chrome.browser.ChromeInactivityTracker; @@ -30,8 +31,11 @@ public final class BraveReturnToChromeUtil { /** Returns whether should show a NTP as the home surface at startup. */ public static boolean shouldShowNtpAsHomeSurfaceAtStartup( Intent intent, Bundle bundle, ChromeInactivityTracker inactivityTracker) { - // When feature is disabled, use Brave's default behavior - if (!BraveFreshNtpHelper.isEnabled()) { + // Only show NTP when launched from the main launcher icon. This prevents showing + // NTP + snackbar when the app is opened via an external link (e.g., clicking a URL + // in other apps or sharing a link). + // When feature is disabled, use Brave's default behavior. + if (!IntentUtils.isMainIntentFromLauncher(intent) || !BraveFreshNtpHelper.isEnabled()) { return false; } diff --git a/android/junit/BUILD.gn b/android/junit/BUILD.gn index d543838d02b..4c54987bca9 100644 --- a/android/junit/BUILD.gn +++ b/android/junit/BUILD.gn @@ -122,4 +122,10 @@ if (is_android) { deps = [ "//chrome/android/junit:chrome_junit_tests_helper" ] } + + robolectric_library("brave_junit_tests_org.chromium.chrome.browser.tasks") { + sources = [ "src/org/chromium/chrome/browser/tasks/BraveReturnToChromeUtilUnitTest.java" ] + + deps = [ "//chrome/android/junit:chrome_junit_tests_helper" ] + } } diff --git a/android/junit/src/org/chromium/chrome/browser/tasks/BraveReturnToChromeUtilUnitTest.java b/android/junit/src/org/chromium/chrome/browser/tasks/BraveReturnToChromeUtilUnitTest.java new file mode 100644 index 00000000000..937b82facfa --- /dev/null +++ b/android/junit/src/org/chromium/chrome/browser/tasks/BraveReturnToChromeUtilUnitTest.java @@ -0,0 +1,81 @@ +/* Copyright (c) 2026 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. */ + +package org.chromium.chrome.browser.tasks; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import android.content.Intent; +import android.net.Uri; + +import androidx.test.filters.SmallTest; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; +import org.robolectric.annotation.Config; + +import org.chromium.base.BravePreferenceKeys; +import org.chromium.base.IntentUtils; +import org.chromium.base.test.BaseRobolectricTestRunner; +import org.chromium.chrome.browser.ChromeInactivityTracker; +import org.chromium.chrome.browser.preferences.ChromeSharedPreferences; + +/** Unit tests for {@link BraveReturnToChromeUtil} class. */ +@RunWith(BaseRobolectricTestRunner.class) +@Config(manifest = Config.NONE) +public class BraveReturnToChromeUtilUnitTest { + @Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule(); + @Mock private ChromeInactivityTracker mInactivityTracker; + + @Test + @SmallTest + public void testShouldNotShowNtpWithExternalViewIntent() { + // Simulate an ACTION_VIEW intent from an external app (e.g., clicking a link in Gmail). + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://example.com")); + + assertFalse(IntentUtils.isMainIntentFromLauncher(intent)); + assertFalse( + BraveReturnToChromeUtil.shouldShowNtpAsHomeSurfaceAtStartup( + intent, null, mInactivityTracker)); + + // Verify snackbar flag was NOT set. + assertFalse( + ChromeSharedPreferences.getInstance() + .readBoolean(BravePreferenceKeys.BRAVE_SHOW_RECENT_TABS_SNACKBAR, false)); + } + + @Test + @SmallTest + public void testShouldNotShowNtpWithSendIntent() { + // Simulate an ACTION_SEND intent (e.g., sharing a link to Brave). + Intent intent = new Intent(Intent.ACTION_SEND); + intent.putExtra(Intent.EXTRA_TEXT, "https://example.com"); + + assertFalse(IntentUtils.isMainIntentFromLauncher(intent)); + assertFalse( + BraveReturnToChromeUtil.shouldShowNtpAsHomeSurfaceAtStartup( + intent, null, mInactivityTracker)); + } + + @Test + @SmallTest + public void testMainLauncherIntentIsRecognized() { + // Verify that a proper main launcher intent passes the intent check. + Intent intent = createMainIntentFromLauncher(); + assertTrue(IntentUtils.isMainIntentFromLauncher(intent)); + } + + private Intent createMainIntentFromLauncher() { + Intent intent = new Intent(); + intent.setAction(Intent.ACTION_MAIN); + intent.addCategory(Intent.CATEGORY_LAUNCHER); + return intent; + } +} diff --git a/test/BUILD.gn b/test/BUILD.gn index d4380e96bef..df3f45bdd5e 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -1563,6 +1563,7 @@ if (is_android) { "//brave/android/junit:brave_junit_tests_org.chromium.chrome.browser.referrer", "//brave/android/junit:brave_junit_tests_org.chromium.chrome.browser.settings", "//brave/android/junit:brave_junit_tests_org.chromium.chrome.browser.tabbed_mode", + "//brave/android/junit:brave_junit_tests_org.chromium.chrome.browser.tasks", "//brave/android/junit:brave_junit_tests_org.chromium.chrome.browser.toolbar", "//brave/android/junit:brave_junit_tests_org.chromium.chrome.browser.vpn", "//brave/browser/customize_menu/android:junit",