chromium/chrome/browser/extensions/api/tabs/tabs_apitest.cc

// Copyright 2012 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/extensions/extension_apitest.h"

#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/extensions/api/tabs/tabs_api.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/prefs/incognito_mode_prefs.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "components/prefs/pref_service.h"
#include "content/public/common/content_features.h"
#include "content/public/test/back_forward_cache_util.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/prerender_test_util.h"
#include "extensions/test/result_catcher.h"
#include "extensions/test/test_extension_dir.h"
#include "net/dns/mock_host_resolver.h"

#if BUILDFLAG(IS_WIN)
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#endif

ContextType;

class ExtensionApiTabTest : public extensions::ExtensionApiTest {};

class ExtensionApiTabTestWithContextType
    : public ExtensionApiTabTest,
      public testing::WithParamInterface<ContextType> {};

INSTANTIATE_TEST_SUITE_P();

INSTANTIATE_TEST_SUITE_P();

class ExtensionApiTabBackForwardCacheTest
    : public ExtensionApiTabTestWithContextType {};

INSTANTIATE_TEST_SUITE_P();

INSTANTIATE_TEST_SUITE_P();

class ExtensionApiNewTabTest : public ExtensionApiTabTestWithContextType {};

INSTANTIATE_TEST_SUITE_P();

INSTANTIATE_TEST_SUITE_P();

IN_PROC_BROWSER_TEST_P(ExtensionApiNewTabTest, Tabs) {}

IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, TabAudible) {}

// TODO(crbug.com/40925613): Re-enable this test
#if BUILDFLAG(IS_MAC)
#define MAYBE_Muted
#else
#define MAYBE_Muted
#endif
IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, MAYBE_Muted) {}

IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, Tabs2) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Duplicate) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Size) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Update) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Pinned) {}

// Flakes reported on Linux debug and Mac, see crbug.com/40936001.
#if (BUILDFLAG(IS_LINUX) && !defined(NDEBUG)) || BUILDFLAG(IS_MAC)
#define MAYBE_Move
#else
#define MAYBE_Move
#endif
IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, MAYBE_Move) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Events) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, RelativeURLs) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Query) {}

// TODO(crbug.com/40254426): Move to tabs_interactive_test.cc
#if !BUILDFLAG(IS_CHROMEOS_LACROS)
// TODO(crbug.com/40890826): Re-enable once flakiness is fixed.
IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, DISABLED_Highlight) {}
#endif

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, LastAccessed) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, CrashBrowser) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Opener) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Remove) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, RemoveMultiple) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, GetCurrent) {}

// Disabled for being flaky. See crbug.com/1472144
IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, DISABLED_Connect) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, OnRemoved) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Reload) {}

class ExtensionApiCaptureTest
    : public ExtensionApiTabTest,
      public testing::WithParamInterface<ContextType> {};

INSTANTIATE_TEST_SUITE_P();
INSTANTIATE_TEST_SUITE_P();

// https://crbug.com/1450747 Flaky on Mac.
#if BUILDFLAG(IS_MAC)
#define MAYBE_CaptureVisibleTabJpeg
#else
#define MAYBE_CaptureVisibleTabJpeg
#endif
IN_PROC_BROWSER_TEST_P(ExtensionApiCaptureTest, MAYBE_CaptureVisibleTabJpeg) {}

// https://crbug.com/1450933 Flaky on Mac.
#if BUILDFLAG(IS_MAC)
#define MAYBE_CaptureVisibleTabPng
#else
#define MAYBE_CaptureVisibleTabPng
#endif
IN_PROC_BROWSER_TEST_P(ExtensionApiCaptureTest, MAYBE_CaptureVisibleTabPng) {}

// TODO(crbug.com/40168659) Re-enable test
IN_PROC_BROWSER_TEST_P(ExtensionApiCaptureTest,
                       DISABLED_CaptureVisibleTabRace) {}

// https://crbug.com/1107934 Flaky on Windows, Linux, ChromeOS.
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_CHROMEOS)
#define MAYBE_CaptureVisibleFile
#else
#define MAYBE_CaptureVisibleFile
#endif
IN_PROC_BROWSER_TEST_P(ExtensionApiCaptureTest, MAYBE_CaptureVisibleFile) {}

// TODO(crbug.com/40803947): Fix flakiness on Linux and Lacros then reenable.
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)
#define MAYBE_CaptureVisibleDisabled
#else
#define MAYBE_CaptureVisibleDisabled
#endif
IN_PROC_BROWSER_TEST_P(ExtensionApiCaptureTest, MAYBE_CaptureVisibleDisabled) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiCaptureTest, CaptureNullWindow) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, OnCreated) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType,
                       LazyBackgroundTabsOnCreated) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, OnUpdated) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabBackForwardCacheTest, OnUpdated) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, NoPermissions) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType,
                       DISABLED_HostPermission) {}

// Flaky on Windows, Mac and Linux. http://crbug.com/820110.
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || \
    BUILDFLAG(IS_CHROMEOS)
#define MAYBE_UpdateWindowResize
#else
#define MAYBE_UpdateWindowResize
#endif
IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, MAYBE_UpdateWindowResize) {}

#if BUILDFLAG(IS_WIN)
IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, FocusWindowDoesNotUnmaximize) {
  HWND window =
      browser()->window()->GetNativeWindow()->GetHost()->GetAcceleratedWidget();
  ::SendMessage(window, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
  ASSERT_TRUE(RunExtensionTest("window_update/focus")) << message_;
  ASSERT_TRUE(::IsZoomed(window));
}
#endif  // BUILDFLAG(IS_WIN)

#if defined(USE_AURA) || BUILDFLAG(IS_MAC)
// Maximizing/fullscreen popup window doesn't work on aura's managed mode.
// See bug crbug.com/116305.
// Mac: http://crbug.com/103912
#define MAYBE_UpdateWindowShowState
#else
#define MAYBE_UpdateWindowShowState
#endif  // defined(USE_AURA) || BUILDFLAG(IS_MAC)
IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, MAYBE_UpdateWindowShowState) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType,
                       IncognitoDisabledByPref) {}

IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, GetViewsOfCreatedPopup) {}

IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, GetViewsOfCreatedWindow) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType,
                       OnUpdatedDiscardedState) {}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, OpenerCraziness) {}

// Tests sending messages from an extension's service worker using
// chrome.tabs.sendMessage to a webpage in the extension listening for them
// using chrome.runtime.OnMessage.
IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, SendMessage) {}

// Tests that extension with "tabs" permission does not leak tab info to another
// extension without "tabs" permission.
//
// Regression test for https://crbug.com/1302959
IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType,
                       TabsPermissionDoesNotLeakTabInfo) {}

struct IncognitoTestParam {};

class IncognitoExtensionApiTabTest
    : public ExtensionApiTabTest,
      public testing::WithParamInterface<IncognitoTestParam> {};

IN_PROC_BROWSER_TEST_P(IncognitoExtensionApiTabTest, Tabs) {}

INSTANTIATE_TEST_SUITE_P();
INSTANTIATE_TEST_SUITE_P();
INSTANTIATE_TEST_SUITE_P();
INSTANTIATE_TEST_SUITE_P();

class ExtensionApiTabPrerenderingTest : public ExtensionApiTabTest {};

// TODO(crbug.com/40235049): Flaky on multiple platforms.
IN_PROC_BROWSER_TEST_F(ExtensionApiTabPrerenderingTest, DISABLED_Prerendering) {}

IN_PROC_BROWSER_TEST_F(ExtensionApiTabPrerenderingTest,
                       PrerenderingIntoANewTab) {}

// Adding a new test? Awesome. But API tests are the old hotness. The new
// hotness is api_test_utils. See tabs_test.cc for an example.
// We are trying to phase out many uses of API tests as they tend to be flaky.