chromium/chrome/browser/ui/views/side_panel/lens/lens_core_tab_side_panel_helper_browsertest.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 "chrome/browser/ui/views/side_panel/lens/lens_core_tab_side_panel_helper.h"

#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/search_test_utils.h"
#include "components/lens/buildflags.h"
#include "components/lens/lens_features.h"
#include "components/search_engines/template_url_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/buildflags/buildflags.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace lens {
class LensCoreTabSidePanelHelperBrowserTest : public InProcessBrowserTest {};

#if BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(LensCoreTabSidePanelHelperBrowserTest,
                       IsSidePanelEnabledForLensReturnsFalseForAndroid) {
  auto* web_contents = GetBrowserWebContents();

  EXPECT_FALSE(lens::IsSidePanelEnabledForLens(web_contents));
}
#endif  // BUILDFLAG(IS_ANDROID)

#if !BUILDFLAG(ENABLE_LENS_DESKTOP_GOOGLE_BRANDED_FEATURES)
IN_PROC_BROWSER_TEST_F(
    LensCoreTabSidePanelHelperBrowserTest,
    IsSidePanelEnabledForLensReturnsFalseIfGoogleBrandedFeaturesAreDisabled) {}
#endif  // !BUILDFLAG(ENABLE_LENS_DESKTOP_GOOGLE_BRANDED_FEATURES)

#if BUILDFLAG(ENABLE_LENS_DESKTOP_GOOGLE_BRANDED_FEATURES)
//  IsSidePanelEnabledForLens
IN_PROC_BROWSER_TEST_F(LensCoreTabSidePanelHelperBrowserTest,
                       IsSidePanelEnabledForLensReturnsFalseForPwa) {
  auto* pwa_web_contents = GetPwaWebContents();

  EXPECT_FALSE(lens::IsSidePanelEnabledForLens(pwa_web_contents));
}

IN_PROC_BROWSER_TEST_F(LensCoreTabSidePanelHelperBrowserTest,
                       IsSidePanelEnabledForLensReturnsTrueForBrowser) {
  auto* web_contents = GetBrowserWebContents();

  EXPECT_TRUE(lens::IsSidePanelEnabledForLens(web_contents));
}

IN_PROC_BROWSER_TEST_F(
    LensCoreTabSidePanelHelperBrowserTest,
    IsSidePanelEnabledForLensReturnsFalseForBrowserIfOptedOutInTemplateUrl) {
  SetGoogleToOptOutOfSidePanel();
  auto* web_contents = GetBrowserWebContents();

  EXPECT_FALSE(lens::IsSidePanelEnabledForLens(web_contents));
}

IN_PROC_BROWSER_TEST_F(LensCoreTabSidePanelHelperBrowserTest,
                       IsSidePanelEnabledForLensReturnsFalseFor3PDse) {
  Setup3PImageSearchEngine(/*support_side_panel*/ true);
  auto* web_contents = GetBrowserWebContents();

  EXPECT_FALSE(lens::IsSidePanelEnabledForLens(web_contents));
}
#endif  // BUILDFLAG(ENABLE_LENS_DESKTOP_GOOGLE_BRANDED_FEATURES)

class LensCoreTabSidePanelHelperLensStandaloneDisabled
    : public LensCoreTabSidePanelHelperBrowserTest {};

#if BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(LensCoreTabSidePanelHelperLensStandaloneDisabled,
                       IsSidePanelEnabledForLensReturnsFalseForAndroid) {
  auto* web_contents = GetBrowserWebContents();

  EXPECT_FALSE(lens::IsSidePanelEnabledForLens(web_contents));
}
#endif  // BUILDFLAG(IS_ANDROID)

#if !BUILDFLAG(ENABLE_LENS_DESKTOP_GOOGLE_BRANDED_FEATURES)
IN_PROC_BROWSER_TEST_F(
    LensCoreTabSidePanelHelperLensStandaloneDisabled,
    IsSidePanelEnabledForLensReturnsFalseIfGoogleBrandedFeaturesAreDisabled) {}
#endif  // !BUILDFLAG(ENABLE_LENS_DESKTOP_GOOGLE_BRANDED_FEATURES)

#if BUILDFLAG(ENABLE_LENS_DESKTOP_GOOGLE_BRANDED_FEATURES)
IN_PROC_BROWSER_TEST_F(LensCoreTabSidePanelHelperLensStandaloneDisabled,
                       IsSidePanelEnabledForLensReturnsFalse) {
  auto* web_contents = GetBrowserWebContents();

  EXPECT_FALSE(lens::IsSidePanelEnabledForLens(web_contents));
}

IN_PROC_BROWSER_TEST_F(LensCoreTabSidePanelHelperLensStandaloneDisabled,
                       IsSidePanelEnabledForLensReturnsFalseForPwa) {
  auto* web_contents = GetPwaWebContents();

  EXPECT_FALSE(lens::IsSidePanelEnabledForLens(web_contents));
}
#endif  // BUILDFLAG(ENABLE_LENS_DESKTOP_GOOGLE_BRANDED_FEATURES)

class LensCoreTabSidePanelHelperRegionSearchBrowserTest
    : public LensCoreTabSidePanelHelperBrowserTest {};

class LensCoreTabSidePanelHelper3PDseEnabled
    : public LensCoreTabSidePanelHelperBrowserTest {};

#if BUILDFLAG(IS_ANDROID)
IN_PROC_BROWSER_TEST_F(LensCoreTabSidePanelHelper3PDseEnabled,
                       IsSidePanelEnabledFor3PDseReturnsFalseForAndroid) {
  Setup3PImageSearchEngine(/*support_side_panel*/ true);
  auto* web_contents = GetBrowserWebContents();

  EXPECT_FALSE(lens::IsSidePanelEnabledFor3PDse(web_contents));
}
#endif  // BUILDFLAG(IS_ANDROID)

#if !BUILDFLAG(ENABLE_LENS_DESKTOP_GOOGLE_BRANDED_FEATURES)
IN_PROC_BROWSER_TEST_F(
    LensCoreTabSidePanelHelper3PDseEnabled,
    IsSidePanelEnabledFor3PDseReturnsFalseIfGoogleBrandedFeaturesAreDisabled) {}
#endif  // !BUILDFLAG(ENABLE_LENS_DESKTOP_GOOGLE_BRANDED_FEATURES)

#if BUILDFLAG(ENABLE_LENS_DESKTOP_GOOGLE_BRANDED_FEATURES)

//  IsSidePanelEnabledFor3PDse
IN_PROC_BROWSER_TEST_F(LensCoreTabSidePanelHelper3PDseEnabled,
                       IsSidePanelEnabledFor3PDseReturnsTrue) {
  Setup3PImageSearchEngine(/*support_side_panel*/ true);
  auto* web_contents = GetBrowserWebContents();

  EXPECT_TRUE(lens::IsSidePanelEnabledFor3PDse(web_contents));
}

IN_PROC_BROWSER_TEST_F(LensCoreTabSidePanelHelper3PDseEnabled,
                       IsSidePanelEnabledFor3PDseReturnsFalseIf3POptedOut) {
  Setup3PImageSearchEngine(/*support_side_panel*/ false);
  auto* web_contents = GetBrowserWebContents();

  EXPECT_FALSE(lens::IsSidePanelEnabledFor3PDse(web_contents));
}

IN_PROC_BROWSER_TEST_F(LensCoreTabSidePanelHelper3PDseEnabled,
                       IsSidePanelEnabledFor3PDseReturnsFalseForPwa) {
  Setup3PImageSearchEngine(/*support_side_panel*/ true);
  auto* pwa_web_contents = GetPwaWebContents();

  EXPECT_FALSE(lens::IsSidePanelEnabledFor3PDse(pwa_web_contents));
}

IN_PROC_BROWSER_TEST_F(LensCoreTabSidePanelHelper3PDseEnabled,
                       IsSidePanelEnabledFor3PDseReturnsFalseForGoogle) {
  auto* web_contents = GetBrowserWebContents();

  EXPECT_FALSE(lens::IsSidePanelEnabledFor3PDse(web_contents));
}
class LensCoreTabSidePanelHelper3PDseDisabledUsingFlag
    : public LensCoreTabSidePanelHelperBrowserTest {
 protected:
  void SetUp() override {
    // The test server must start first, so that we know the port that the
    // server is using.
    ASSERT_TRUE(embedded_test_server()->Start());

    base::test::ScopedFeatureList features;
    features.InitWithFeaturesAndParameters(
        {{}, {}}, {{lens::features::kEnableImageSearchSidePanelFor3PDse}});

    InProcessBrowserTest::SetUp();
  }
};

IN_PROC_BROWSER_TEST_F(LensCoreTabSidePanelHelper3PDseDisabledUsingFlag,
                       IsSidePanelEnabledFor3PDseReturnsFalse) {
  Setup3PImageSearchEngine(/*support_side_panel*/ true);
  auto* web_contents = GetBrowserWebContents();

  EXPECT_FALSE(lens::IsSidePanelEnabledFor3PDse(web_contents));
}
#endif  // BUILDFLAG(ENABLE_LENS_DESKTOP_GOOGLE_BRANDED_FEATURES)
}  // namespace lens