File: sunfish_browsertest.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 (217 lines) | stat: -rw-r--r-- 8,410 bytes parent folder | download | duplicates (5)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright 2024 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>

#include "ash/capture_mode/capture_mode_controller.h"
#include "ash/capture_mode/capture_mode_test_util.h"
#include "ash/capture_mode/capture_mode_types.h"
#include "ash/capture_mode/search_results_panel.h"
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_switches.h"
#include "ash/public/cpp/ash_web_view.h"
#include "ash/public/cpp/capture_mode/capture_mode_api.h"
#include "base/functional/callback_helpers.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/ui/ash/capture_mode/chrome_capture_mode_delegate.h"
#include "chrome/browser/ui/ash/capture_mode/search_results_view.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/lens/lens_overlay_permission_utils.h"
#include "content/public/browser/page_navigator.h"
#include "content/public/common/referrer.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_navigation_observer.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/page_transition_types.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget.h"

namespace {

constexpr char kSearchResultClickedHistogram[] =
    "Ash.CaptureModeController.SearchResultClicked.ClamshellMode";

std::unique_ptr<views::Widget> CreateWidget() {
  views::Widget::InitParams params(
      views::Widget::InitParams::CLIENT_OWNS_WIDGET,
      views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
  return std::make_unique<views::Widget>(std::move(params));
}

// Given an embedded script `body`, wraps it in a basic HTML structure, then
// returns a GURL accessible by the local test server.
GURL CreateDataUrlWithBody(const std::string& body) {
  return GURL(base::StringPrintf(R"(data:text/html,
      <html>
        <body>
          <style>
            * {
              margin: 0;
              padding: 0;
            }
          </style>
          %s
        </body>
      </html>
    )",
                                 body.c_str()));
}

}  // namespace

namespace ash {

class SunfishBrowserTest : public InProcessBrowserTest {
 public:
  SunfishBrowserTest() = default;
  SunfishBrowserTest(const SunfishBrowserTest&) = delete;
  SunfishBrowserTest& operator=(const SunfishBrowserTest&) = delete;
  ~SunfishBrowserTest() override = default;

 private:
  base::test::ScopedFeatureList scoped_feature_list_{features::kSunfishFeature};
};

// Tests the basic functionalities of `SearchResultsView`.
IN_PROC_BROWSER_TEST_F(SunfishBrowserTest, SearchResultsView) {
  auto widget = CreateWidget();
  ChromeCaptureModeDelegate* delegate = ChromeCaptureModeDelegate::Get();
  auto* contents_view =
      widget->SetContentsView(delegate->CreateSearchResultsView());
  auto* search_results_view =
      views::AsViewClass<SearchResultsView>(contents_view);
  ASSERT_TRUE(search_results_view);
}

// Tests that links are opened in new tabs.
IN_PROC_BROWSER_TEST_F(SunfishBrowserTest, OpenLinksInNewTabs) {
  base::HistogramTester histogram_tester;

  histogram_tester.ExpectTotalCount(kSearchResultClickedHistogram, 0);

  auto* controller = CaptureModeController::Get();
  controller->StartSunfishSession();
  VerifyActiveBehavior(BehaviorType::kSunfish);

  // Simulate showing the panel while the session is active.
  controller->ShowSearchResultsPanel();
  controller->NavigateSearchResultsPanel(GURL("kTestUrl1"));
  ASSERT_TRUE(controller->IsActive());
  auto* search_results_view =
      controller->GetSearchResultsPanel()->search_results_view();

  // Browser tests start out with 1 browser tab by default.
  EXPECT_EQ(1, browser()->tab_strip_model()->count());

  // Simulate clicking on a new URL in the web view.
  auto* child_view =
      search_results_view->GetViewByID(kAshWebViewChildWebViewId);
  ASSERT_TRUE(child_view);
  auto* web_view = views::AsViewClass<views::WebView>(child_view);
  ASSERT_TRUE(web_view);
  auto* web_contents = web_view->web_contents();
  web_contents->GetController().LoadURL(
      CreateDataUrlWithBody(R"(
      <script>
        // Wait until window has finished loading.
        window.addEventListener("load", () => {

          // Perform simple click on an anchor within the same target.
          const anchor = document.createElement("a");
          anchor.href = "https://google.com/";
          anchor.click();

          // Wait for first click event to be flushed.
          setTimeout(() => {

            // Perform simple click on an anchor with "_blank" target.
            const anchor = document.createElement("a");
            anchor.href = "https://assistant.google.com/";
            anchor.target = "_blank";
            anchor.click();
          }, 0);
        });
      </script>
    )"),
      content::Referrer(), ui::PAGE_TRANSITION_TYPED, std::string());
  content::TestNavigationObserver observer(web_contents);
  observer.Wait();
  EXPECT_TRUE(observer.last_navigation_succeeded());

  // Test it opens a new tab and ends the session.
  EXPECT_EQ(2, browser()->tab_strip_model()->count());
  EXPECT_FALSE(controller->IsActive());
  histogram_tester.ExpectTotalCount(kSearchResultClickedHistogram, 1);
}

// Tests that links are correctly opened in off the record profiles.
IN_PROC_BROWSER_TEST_F(SunfishBrowserTest, OpensLinksOffTheRecord) {
  base::HistogramTester histogram_tester;
  histogram_tester.ExpectTotalCount(kSearchResultClickedHistogram, 0);

  auto* controller = CaptureModeController::Get();
  controller->StartSunfishSession();
  VerifyActiveBehavior(BehaviorType::kSunfish);

  // Simulate showing the panel while the session is active.
  controller->ShowSearchResultsPanel();
  controller->NavigateSearchResultsPanel(GURL("kTestUrl1"));
  ASSERT_TRUE(controller->IsActive());
  auto* search_results_view =
      controller->GetSearchResultsPanel()->search_results_view();

  // Simulate clicking on a new URL in the web view.
  views::View* child_view =
      search_results_view->GetViewByID(kAshWebViewChildWebViewId);
  ASSERT_TRUE(child_view);
  auto* web_view = views::AsViewClass<views::WebView>(child_view);
  ASSERT_TRUE(web_view);
  content::WebContents* web_contents = web_view->web_contents();
  ASSERT_TRUE(web_contents);
  content::OpenURLParams params(
      GURL("https://assistant.google.com"), content::Referrer(),
      WindowOpenDisposition::OFF_THE_RECORD, ui::PAGE_TRANSITION_LINK,
      /*is_renderer_initiated=*/false);
  content::WebContents* new_contents =
      web_contents->OpenURL(params,
                            /*navigation_handle_callback=*/base::DoNothing());

  Profile* profile =
      Profile::FromBrowserContext(new_contents->GetBrowserContext());
  EXPECT_TRUE(profile->IsOffTheRecord());
  histogram_tester.ExpectTotalCount(kSearchResultClickedHistogram, 1);
}

IN_PROC_BROWSER_TEST_F(SunfishBrowserTest, SendSearchRequests) {
  // Send a region search, simulated inside a regular capture session.
  ChromeCaptureModeDelegate* delegate = ChromeCaptureModeDelegate::Get();
  delegate->SendLensWebRegionSearch(gfx::Image(),
                                    /*is_standalone_session=*/false,
                                    base::BindRepeating([](GURL url) {}),
                                    base::DoNothing(), base::DoNothing());

  // Send a region search, simulated inside a standalone Sunfish session.
  delegate->SendLensWebRegionSearch(gfx::Image(),
                                    /*is_standalone_session=*/true,
                                    base::BindRepeating([](GURL url) {}),
                                    base::DoNothing(), base::DoNothing());
}

// Tests that the policy can be read and set browser-side.
IN_PROC_BROWSER_TEST_F(SunfishBrowserTest, BrowserPolicy) {
  ChromeCaptureModeDelegate* delegate = ChromeCaptureModeDelegate::Get();
  EXPECT_TRUE(delegate->IsSearchAllowedByPolicy());
  EXPECT_TRUE(CanShowSunfishUi());

  browser()->profile()->GetPrefs()->SetInteger(
      lens::prefs::kLensOverlaySettings,
      static_cast<int>(lens::prefs::LensOverlaySettingsPolicyValue::kDisabled));
  EXPECT_FALSE(delegate->IsSearchAllowedByPolicy());
  EXPECT_FALSE(CanShowSunfishUi());
}

}  // namespace ash