chromium/chrome/browser/ui/find_bar/find_bar_host_browsertest.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 <stddef.h>

#include <string>

#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "build/chromeos_buildflags.h"
#include "chrome/app/chrome_command_ids.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/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/find_bar/find_bar.h"
#include "chrome/browser/ui/find_bar/find_bar_controller.h"
#include "chrome/browser/ui/find_bar/find_bar_host_unittest_util.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/views/toolbar/pinned_toolbar_actions_container.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/find_result_waiter.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/find_in_page/find_notification_details.h"
#include "components/find_in_page/find_tab_helper.h"
#include "components/find_in_page/find_types.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/render_view_host.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/base/filename_util.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/views/layout/animating_layout_manager_test_util.h"

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

ASCIIToUTF16;
WideToUTF16;
NavigationController;
WebContents;

namespace {

const char kAnchorPage[] =;
const char kAnchor[] =;
const char kFramePage[] =;
const char kFrameData[] =;
const char kUserSelectPage[] =;
const char kCrashPage[] =;
const char kTooFewMatchesPage[] =;
const char kLongTextareaPage[] =;
const char kPrematureEnd[] =;
const char kMoveIfOver[] =;
const char kBitstackCrash[] =;
const char kSelectChangesOrdinal[] =;
const char kStartAfterSelection[] =;
const char kSimple[] =;
const char kLinkPage[] =;

const bool kBack =;
const bool kFwd =;

const bool kIgnoreCase =;
const bool kCaseSensitive =;

const int kMoveIterations =;

}  // namespace

class FindInPageControllerTest : public InProcessBrowserTest {};

// This test loads a page with frames and starts FindInPage requests.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageFrames) {}

// Verify search for text within various forms and text areas.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageFormsTextAreas) {}

// This test removes a frame after a search comes up empty, then navigates to
// a different page and verifies this doesn't cause any extraneous dings.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, NoAudibleAlertOnFrameChange) {}

// This test navigates to a different page after a successful search and
// verifies this doesn't cause any extraneous dings.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, NoAudibleAlertOnNavigation) {}

// See http://crbug.com/1131780
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
                       AudibleAlertsWithPrepopulatedFind) {}

// Verify search selection coordinates. The data file used is set-up such that
// the text occurs on the same line, and we verify their positions by verifying
// their relative positions.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageSpecialURLs) {}

// This tests the following bug that used to exist:
// 1) Do a find that has 0 results
// 2) Navigate to a new page (on the same domain) that contains the search text.
// 3) Open the find bar. It will be prepopulated with the previous search text
// and should show the number of matches for that text. The bug caused it to
// show 0 matches instead.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, StaleCountAfterNoResults) {}

// Verifies that comments and meta data are not searchable.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
                       CommentsAndMetaDataNotSearchable) {}

// Verifies that span are searchable.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, SpanSearchable) {}

// Find in a very large page.
// TODO(crbug.com/40129326): Test is flaky on Mac debug builds.
#if BUILDFLAG(IS_MAC) && !defined(NDEBUG)
#define MAYBE_LargePage
#elif BUILDFLAG(IS_LINUX) && (!defined(NDEBUG) || defined(ADDRESS_SANITIZER))
// TODO(crbug.com/40751034): Test is flaky on Linux debug builds.
// TODO(crbug.com/40760850): Test is flaky on Linux ASAN builds.
#define MAYBE_LargePage
#else
#define MAYBE_LargePage
#endif
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, MAYBE_LargePage) {}

// Find a very long string in a large page.
// TODO(crbug.com/40700976): Test is flaky on Mac debug builds and Linux asan.
#if (BUILDFLAG(IS_MAC) && !defined(NDEBUG)) || defined(ADDRESS_SANITIZER)
#define MAYBE_FindLongString
#elif BUILDFLAG(IS_LINUX) && !defined(NDEBUG)
// TODO(crbug.com/40751034): Test is flaky on Linux debug builds.
#define MAYBE_FindLongString
#else
#define MAYBE_FindLongString
#endif
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, MAYBE_FindLongString) {}

// Find a big font string in a page.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, BigString) {}

// Search Back and Forward on a single occurrence.
// TODO(crbug.com/40714133): Test is flaky on ChromeOS.
#if BUILDFLAG(IS_CHROMEOS_ASH) || BUILDFLAG(IS_MAC)
#define MAYBE_SingleOccurrence
#else
#define MAYBE_SingleOccurrence
#endif
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, MAYBE_SingleOccurrence) {}

// Find the whole text file page and find count should be 1.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindWholeFileContent) {}

// This test loads a single-frame page and makes sure the ordinal returned makes
// sense as we FindNext over all the items.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageOrdinal) {}

// This tests that the ordinal is correctly adjusted after a selection
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
                       SelectChangesOrdinal_Issue20883) {}

// This tests that we start searching after selected text.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
                       StartSearchAfterSelection) {}

// This test loads a page with frames and makes sure the ordinal returned makes
// sense.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPageMultiFramesOrdinal) {}

// We could get ordinals out of whack when restarting search in subframes.
// See http://crbug.com/5132.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPage_Issue5132) {}

// This tests that the ordinal and match count is cleared after a navigation,
// as reported in issue http://crbug.com/126468.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, NavigateClearsOrdinal) {}

// Load a page with no selectable text and make sure we don't crash.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindUnselectableText) {}

// Try to reproduce the crash seen in issue 1341577.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindCrash_Issue1341577) {}

// Try to reproduce the crash seen in http://crbug.com/14491, where an assert
// hits in the BitStack size comparison in WebKit.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindCrash_Issue14491) {}

// Test to make sure Find does the right thing when restarting from a timeout.
// We used to have a problem where we'd stop finding matches when all of the
// following conditions were true:
// 1) The page has a lot of text to search.
// 2) The page contains more than one match.
// 3) It takes longer than the time-slice given to each Find operation (100
//    ms) to find one or more of those matches (so Find times out and has to try
//    again from where it left off).
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindRestarts_Issue1155639) {}

// Make sure we don't get into an infinite loop when text box contains very
// large amount of text.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindRestarts_Issue70505) {}

// This tests bug 11761: FindInPage terminates search prematurely.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindInPagePrematureEnd) {}

// Verify that the find bar is hidden on reload and navigation.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
                       HideFindBarOnNavigateAndReload) {}

IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindStayVisibleOnAnchorLoad) {}

// Make sure Find box disappears when History/Downloads page is opened, and
// when a New Tab is opened.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
                       FindDisappearOnNewTabAndHistory) {}

// Make sure Find box moves out of the way if it is obscuring the active match.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FindMovesWhenObscuring) {}

// Make sure F3 in a new tab works if Find has previous string to search for.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
                       FindNextInNewTabUsesPrepopulate) {}

// Make sure Find box does not become UI-inactive when no text is in the box as
// we switch to a tab contents with an empty find string. See issue 13570.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, StayActive) {}

// Make sure F3 works after you FindNext a couple of times and end the Find
// session. See issue http://crbug.com/28306.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, RestartSearchFromF3) {}

// When re-opening the find bar with F3, the find bar should be re-populated
// with the last search from the same tab rather than the last overall search.
// The only exception is if there is a global pasteboard (for example on Mac).
// http://crbug.com/30006
#if BUILDFLAG(IS_MAC)
#define MAYBE_PreferPreviousSearch
#else
#define MAYBE_PreferPreviousSearch
#endif
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, MAYBE_PreferPreviousSearch) {}

// This tests that whenever you close and reopen the Find bar, it should show
// the last search entered in that tab. http://crbug.com/40121.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, PrepopulateSameTab) {}

// This tests that whenever you open Find in a new tab it should prepopulate
// with a previous search term (in any tab), if a search has not been issued in
// this tab before.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, PrepopulateInNewTab) {}

// This makes sure that we can search for A in tabA, then for B in tabB and
// when we come back to tabA we should still see A (because that was the last
// search in that tab).
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, PrepopulatePreserveLast) {}

// This tests that search terms entered into an incognito find bar are not used
// as prepopulate terms for non-incognito windows.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, NoIncognitoPrepopulate) {}

// This makes sure that dismissing the find bar with kActivateSelection works.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, ActivateLinkNavigatesPage) {}

IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, FitWindow) {}

#if BUILDFLAG(IS_CHROMEOS_LACROS)
// TODO(crbug.com/40230732): Flakily crashes on Lacros.
#define MAYBE_FindMovesOnTabClose_Issue1343052
#else
#define MAYBE_FindMovesOnTabClose_Issue1343052
#endif
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
                       MAYBE_FindMovesOnTabClose_Issue1343052) {}

// Verify that if there's a global pasteboard (for example on Mac) then doing
// a search on one tab will clear the matches label on the other tabs.
#if BUILDFLAG(IS_MAC)
// TODO(http://crbug.com/843878): Remove the interactive UI test
// FindBarPlatformHelperMacInteractiveUITest.GlobalPasteBoardClearMatches
// once http://crbug.com/843878 is fixed.
#define MAYBE_GlobalPasteBoardClearMatches
#else
#define MAYBE_GlobalPasteBoardClearMatches
#endif
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
                       MAYBE_GlobalPasteBoardClearMatches) {}

// Verify that Incognito window doesn't propagate find string to other widows.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, GlobalPasteboardIncognito) {}

// Find text in regular window, find different text in incognito, send
// IDC_FIND_NEXT to incognito. It should search for the second phrase.
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest, IncognitoFindNextSecret) {}

// Find text in regular window, send IDC_FIND_NEXT to incognito. It should
// search for the first phrase.
#if BUILDFLAG(IS_MAC)
#define MAYBE_IncognitoFindNextShared
#else
#define MAYBE_IncognitoFindNextShared
#endif
IN_PROC_BROWSER_TEST_F(FindInPageControllerTest,
                       MAYBE_IncognitoFindNextShared) {}