chromium/chrome/browser/ui/views/default_link_capturing_interactive_uitest.cc

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/test/metrics/user_action_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "chrome/browser/apps/link_capturing/link_capturing_feature_test_support.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/user_education/browser_feature_promo_controller.h"
#include "chrome/browser/ui/views/web_apps/web_app_link_capturing_test_utils.h"
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/ui/web_applications/test/web_app_navigation_browsertest.h"
#include "chrome/browser/web_applications/test/web_app_icon_test_utils.h"
#include "chrome/browser/web_applications/web_app_pref_guardrails.h"
#include "chrome/browser/web_applications/web_app_ui_manager.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chrome/test/user_education/interactive_feature_promo_test.h"
#include "components/user_education/views/help_bubble_factory_views.h"
#include "components/user_education/views/help_bubble_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/event.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/geometry/point.h"
#include "ui/views/interaction/interaction_test_util_views.h"
#include "ui/views/test/button_test_api.h"
#include "ui/views/widget/any_widget_observer.h"

#if BUILDFLAG(IS_MAC)
#include "chrome/browser/apps/link_capturing/mac_intent_picker_helpers.h"
#endif  // BUILDFLAG(IS_MAC)

// This test only runs on Windows, Mac and Linux platforms.
// Because some tests here rely on browser activation, an
// interactive_ui_test is preferred over a browser test.
class DefaultLinkCapturingInteractiveUiTest
    : public web_app::WebAppNavigationBrowserTest {};

IN_PROC_BROWSER_TEST_F(DefaultLinkCapturingInteractiveUiTest,
                       IntentPickerBubbleAcceptCorrectActions) {}

IN_PROC_BROWSER_TEST_F(DefaultLinkCapturingInteractiveUiTest,
                       IntentPickerBubbleCancel) {}

IN_PROC_BROWSER_TEST_F(DefaultLinkCapturingInteractiveUiTest,
                       IntentPickerBubbleIgnored) {}

#if BUILDFLAG(IS_MAC)
using IntentPickerInteractiveUiTest = DefaultLinkCapturingInteractiveUiTest;
// Test that if there is a Universal Link for a site, it shows the picker
// with the app icon.
IN_PROC_BROWSER_TEST_F(IntentPickerInteractiveUiTest,
                       ShowUniversalLinkAppInIntentChip) {
  const GURL url1(embedded_test_server()->GetURL("/title1.html"));
  const GURL url2(embedded_test_server()->GetURL("/title2.html"));

  const char* kFinderAppPath = "/System/Library/CoreServices/Finder.app";

  // Start with a page with no corresponding native app.
  apps::OverrideMacAppForUrlForTesting(true, "");
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url1));

  // Verify that no icon was shown.
  EXPECT_TRUE(web_app::AwaitIntentPickerTabHelperIconUpdateComplete(
      browser()->tab_strip_model()->GetActiveWebContents()));
  ASSERT_FALSE(web_app::GetIntentPickerIcon(browser())->GetVisible());

  // Load a different page while simulating it having a native app.
  apps::OverrideMacAppForUrlForTesting(true, kFinderAppPath);
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url2));

  // Verify app icon shows up in the intent picker.
  EXPECT_TRUE(web_app::AwaitIntentPickerTabHelperIconUpdateComplete(
      browser()->tab_strip_model()->GetActiveWebContents()));
  IntentChipButton* intent_picker_icon =
      web_app::GetIntentPickerIcon(browser());
  ASSERT_NE(intent_picker_icon, nullptr);

  ui::ImageModel app_icon = intent_picker_icon->GetAppIconForTesting();

  SkColor final_color = app_icon.GetImage().AsBitmap().getColor(8, 8);
  EXPECT_TRUE(
      web_app::AreColorsEqual(SK_ColorRED, final_color, /*threshold=*/50));
}
#endif  // BUILDFLAG(IS_MAC)

// TODO(b/338969664): The following tests are failing on Linux MSan/macOS 14, so
// disabling for now. These should be re-enabled.
#if (BUILDFLAG(IS_LINUX) &&                                       \
     (defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER))) || \
    BUILDFLAG(IS_MAC)
#define MAYBE_AcceptingBubbleMeasuresUserAccept
#define MAYBE_BubbleDismissMeasuresUserDismiss
#define MAYBE_ClosingAppWindowMeasuresDismiss
#define MAYBE_IPHShownOnLinkClick
#else
#define MAYBE_AcceptingBubbleMeasuresUserAccept
#define MAYBE_BubbleDismissMeasuresUserDismiss
#define MAYBE_ClosingAppWindowMeasuresDismiss
#define MAYBE_IPHShownOnLinkClick
#endif

// Test to verify that launching an app from the intent picker chip shows the
// IPH bubble if the feature flag is enabled.
class WebAppLinkCapturingIPHPromoTest
    : public InteractiveFeaturePromoTestT<
          DefaultLinkCapturingInteractiveUiTest>,
      public testing::WithParamInterface<bool> {};

IN_PROC_BROWSER_TEST_P(WebAppLinkCapturingIPHPromoTest,
                       AppLaunchFromIntentChipShowsIPH) {}

IN_PROC_BROWSER_TEST_P(WebAppLinkCapturingIPHPromoTest,
                       MAYBE_IPHShownOnLinkClick) {}

IN_PROC_BROWSER_TEST_P(WebAppLinkCapturingIPHPromoTest,
                       MAYBE_ClosingAppWindowMeasuresDismiss) {}

IN_PROC_BROWSER_TEST_P(WebAppLinkCapturingIPHPromoTest,
                       MAYBE_AcceptingBubbleMeasuresUserAccept) {}

IN_PROC_BROWSER_TEST_P(WebAppLinkCapturingIPHPromoTest,
                       MAYBE_BubbleDismissMeasuresUserDismiss) {}

INSTANTIATE_TEST_SUITE_P();