File: find_bar_host_interactive_uitest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (106 lines) | stat: -rw-r--r-- 4,036 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2014 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_view>

#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/find_bar/find_bar.h"
#include "chrome/browser/ui/find_bar/find_bar_controller.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/find_in_page/find_tab_helper.h"
#include "components/find_in_page/find_types.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/test/embedded_test_server/embedded_test_server.h"

using base::WideToUTF16;
using content::WebContents;

namespace {

const char kEndState[] = "/find_in_page/end_state.html";

class FindInPageInteractiveTest : public InProcessBrowserTest {
 public:
  FindInPageInteractiveTest() = default;

  // Platform independent FindInPage that takes |const wchar_t*|
  // as an input.
  int FindInPageASCII(WebContents* web_contents,
                      std::string_view search_str,
                      bool forward,
                      bool case_sensitive,
                      int* ordinal) {
    std::u16string search_str16(base::ASCIIToUTF16(search_str));
    Browser* browser = chrome::FindBrowserWithTab(web_contents);
    browser->GetFeatures()
        .GetFindBarController()
        ->find_bar()
        ->SetFindTextAndSelectedRange(search_str16, gfx::Range());
    return ui_test_utils::FindInPage(web_contents, search_str16, forward,
                                     case_sensitive, ordinal, nullptr);
  }
};

}  // namespace

[[nodiscard]] std::string FocusedOnPage(WebContents* web_contents) {
  return content::EvalJs(web_contents, "getFocusedElement();").ExtractString();
}

// This tests the FindInPage end-state, in other words: what is focused when you
// close the Find box (ie. if you find within a link the link should be
// focused).
IN_PROC_BROWSER_TEST_F(FindInPageInteractiveTest, FindInPageEndState) {
  ASSERT_TRUE(embedded_test_server()->Start());

  // Make sure Chrome is in the foreground, otherwise sending input
  // won't do anything and the test will hang.
  ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));

  // First we navigate to our special focus tracking page.
  GURL url = embedded_test_server()->GetURL(kEndState);
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));

  WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  ASSERT_TRUE(web_contents);
  find_in_page::FindTabHelper* find_tab_helper =
      find_in_page::FindTabHelper::FromWebContents(web_contents);

  // Verify that nothing has focus.
  ASSERT_EQ("{nothing focused}", FocusedOnPage(web_contents));

  // Search for a text that exists within a link on the page.
  int ordinal = 0;
  EXPECT_EQ(1, FindInPageASCII(web_contents, "nk", true, false, &ordinal));
  EXPECT_EQ(1, ordinal);

  // End the find session, which should set focus to the link.
  find_tab_helper->StopFinding(find_in_page::SelectionAction::kKeep);

  // Verify that the link is focused.
  EXPECT_EQ("link1", FocusedOnPage(web_contents));

  // Search for a text that exists within a link on the page.
  EXPECT_EQ(1, FindInPageASCII(web_contents, "Google", true, false, &ordinal));
  EXPECT_EQ(1, ordinal);

  // Move the selection to link 1, after searching.
  EXPECT_TRUE(content::ExecJs(web_contents, "selectLink1();"));

  // End the find session.
  find_tab_helper->StopFinding(find_in_page::SelectionAction::kKeep);

  // Verify that link2 is not focused.
  EXPECT_EQ("", FocusedOnPage(web_contents));
}