File: tab_matcher_desktop_browsertest.cc

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,192,880 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (158 lines) | stat: -rw-r--r-- 6,814 bytes parent folder | download | duplicates (4)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2022 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/autocomplete/tab_matcher_desktop.h"

#include <array>
#include <memory>
#include <string>

#include "build/build_config.h"
#include "build/buildflag.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "chrome/browser/search_engines/template_url_service_test_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/search_engines/template_url_service.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/window_open_disposition.h"
#include "url/gurl.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "ash/constants/ash_switches.h"
#include "base/command_line.h"
#endif

class TabMatcherDesktopTest : public InProcessBrowserTest {
 public:
#if BUILDFLAG(IS_CHROMEOS)
  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitch(
        ash::switches::kIgnoreUserProfileMappingForTests);
  }
#endif  // BUILDFLAG(IS_CHROMEOS)
};

constexpr auto kServiceInitializers =
    std::to_array<TemplateURLService::Initializer>({
        {"kwa", "https://a.chromium.org/?a={searchTerms}", "ca"},
        {"kwb", "https://b.chromium.org/?b={searchTerms}", "cb"},
    });

IN_PROC_BROWSER_TEST_F(TabMatcherDesktopTest,
                       IsTabOpenWithURLNeverReturnsActiveTab) {
  std::unique_ptr<TemplateURLService> service =
      TemplateURLServiceTestUtil::CreateTemplateURLServiceForTesting(
          GetProfile(), kServiceInitializers);
  TabMatcherDesktop matcher(service.get(), GetProfile());

  GURL foo("https://foo.chromium.org");
  GURL bar("https://bar.chromium.org");
  GURL baz("https://baz.chromium.org");

  for (auto url : {foo, bar, baz}) {
    ui_test_utils::NavigateToURLWithDisposition(
        browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
        ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
  }

  EXPECT_TRUE(matcher.IsTabOpenWithURL(foo, nullptr));
  EXPECT_TRUE(matcher.IsTabOpenWithURL(bar, nullptr));
  EXPECT_FALSE(matcher.IsTabOpenWithURL(baz, nullptr));
  EXPECT_FALSE(matcher.IsTabOpenWithURL(GURL("https://chromium.org"), nullptr));
}

IN_PROC_BROWSER_TEST_F(TabMatcherDesktopTest, GetOpenTabsOnlyWithinProfile) {
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(),
                                           GURL("https://bar.chromium.org")));
  ui_test_utils::NavigateToURLWithDisposition(
      browser(), GURL("https://foo.chromium.org"),
      WindowOpenDisposition::NEW_FOREGROUND_TAB,
      ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);

  ProfileManager* profile_manager = g_browser_process->profile_manager();
  Profile* second_profile = &profiles::testing::CreateProfileSync(
      profile_manager, profile_manager->GenerateNextProfileDirectoryPath());
  Browser* browser_with_second_profile = CreateBrowser(second_profile);
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser_with_second_profile,
                                           GURL("https://baz.chromium.org")));

  std::unique_ptr<TemplateURLService> service =
      TemplateURLServiceTestUtil::CreateTemplateURLServiceForTesting(
          GetProfile(), kServiceInitializers);
  TabMatcherDesktop matcher(service.get(), GetProfile());

  AutocompleteInput input;
  const auto tabs = matcher.GetOpenTabs(&input);
  ASSERT_EQ(tabs.size(), 2U);
  EXPECT_EQ(tabs[0].url, GURL("https://bar.chromium.org"));
  EXPECT_EQ(tabs[1].url, GURL("https://foo.chromium.org"));
}

IN_PROC_BROWSER_TEST_F(TabMatcherDesktopTest, IsTabOpenUsesCanonicalSearchURL) {
  std::unique_ptr<TemplateURLService> turl_service =
      TemplateURLServiceTestUtil::CreateTemplateURLServiceForTesting(
          GetProfile(), kServiceInitializers);
  TabMatcherDesktop matcher(turl_service.get(), GetProfile());

  TemplateURLData data;
  data.SetURL("https://example.com/search?q={searchTerms}");
  data.search_intent_params = {"intent"};
  TemplateURL turl(data);
  auto* default_turl = turl_service->Add(std::make_unique<TemplateURL>(data));
  turl_service->SetUserSelectedDefaultSearchProvider(default_turl);

  {
    TemplateURLRef::SearchTermsArgs search_terms_args(u"foo");
    search_terms_args.additional_query_params = "wiz=baz";
    std::string foo_url = default_turl->url_ref().ReplaceSearchTerms(
        search_terms_args, turl_service->search_terms_data());
    EXPECT_EQ("https://example.com/search?wiz=baz&q=foo", foo_url);
    ui_test_utils::NavigateToURLWithDisposition(
        browser(), GURL(foo_url), WindowOpenDisposition::NEW_FOREGROUND_TAB,
        ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
    // The last tab is active. IsTabOpenWithURL() does not match the active tab.
    ui_test_utils::NavigateToURLWithDisposition(
        browser(), GURL("https://active.chromium.org"),
        WindowOpenDisposition::NEW_FOREGROUND_TAB,
        ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);

    EXPECT_TRUE(matcher.IsTabOpenWithURL(
        GURL("https://example.com/search?q=foo"), nullptr));
    EXPECT_TRUE(matcher.IsTabOpenWithURL(
        GURL("https://example.com/search?wiz=baz&q=foo"), nullptr));
    EXPECT_FALSE(matcher.IsTabOpenWithURL(
        GURL("https://example.com/search?wiz=baz&intent=INTENT&q=foo"),
        nullptr));
  }
  {
    TemplateURLRef::SearchTermsArgs search_terms_args(u"bar");
    search_terms_args.additional_query_params = "intent=INTENT";
    std::string bar_url = default_turl->url_ref().ReplaceSearchTerms(
        search_terms_args, turl_service->search_terms_data());
    EXPECT_EQ("https://example.com/search?intent=INTENT&q=bar", bar_url);
    ui_test_utils::NavigateToURLWithDisposition(
        browser(), GURL(bar_url), WindowOpenDisposition::NEW_FOREGROUND_TAB,
        ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
    // The last tab is active. IsTabOpenWithURL() does not match the active tab.
    ui_test_utils::NavigateToURLWithDisposition(
        browser(), GURL("https://active.chromium.org"),
        WindowOpenDisposition::NEW_FOREGROUND_TAB,
        ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);

    EXPECT_FALSE(matcher.IsTabOpenWithURL(
        GURL("https://example.com/search?q=bar"), nullptr));
    EXPECT_FALSE(matcher.IsTabOpenWithURL(
        GURL("https://example.com/search?wiz=baz&q=bar"), nullptr));
    EXPECT_TRUE(matcher.IsTabOpenWithURL(
        GURL("https://example.com/search?wiz=baz&intent=INTENT&q=bar"),
        nullptr));
  }
}