File: browser_non_client_frame_view_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 (446 lines) | stat: -rw-r--r-- 18,211 bytes parent folder | download | duplicates (6)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Copyright 2016 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/ui/views/frame/browser_non_client_frame_view.h"

#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/page_action/page_action_icon_type.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/location_bar/custom_tab_bar_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/web_applications/test/os_integration_test_override_impl.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_install_utils.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/content/browser/test_autofill_manager_injector.h"
#include "components/autofill/core/browser/form_import/form_data_importer.h"
#include "components/autofill/core/browser/form_import/form_data_importer_test_api.h"
#include "components/autofill/core/browser/foundations/browser_autofill_manager.h"
#include "components/autofill/core/browser/foundations/test_autofill_manager_waiter.h"
#include "components/autofill/core/browser/payments/credit_card_save_manager.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/theme_change_waiter.h"
#include "third_party/blink/public/mojom/frame/fullscreen.mojom.h"
#include "third_party/blink/public/mojom/manifest/manifest.mojom.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/color_utils.h"

namespace {

class TestAutofillManager : public autofill::BrowserAutofillManager {
 public:
  explicit TestAutofillManager(autofill::ContentAutofillDriver* driver)
      : BrowserAutofillManager(driver) {}

  [[nodiscard]] testing::AssertionResult WaitForFormsSeen(
      int min_num_awaited_calls) {
    return forms_seen_waiter_.Wait(min_num_awaited_calls);
  }

 private:
  autofill::TestAutofillManagerWaiter forms_seen_waiter_{
      *this,
      {autofill::AutofillManagerEvent::kFormsSeen}};
};

}  // namespace

class BrowserNonClientFrameViewBrowserTest
    : public extensions::ExtensionBrowserTest {
 public:
  BrowserNonClientFrameViewBrowserTest() = default;

  BrowserNonClientFrameViewBrowserTest(
      const BrowserNonClientFrameViewBrowserTest&) = delete;
  BrowserNonClientFrameViewBrowserTest& operator=(
      const BrowserNonClientFrameViewBrowserTest&) = delete;

  ~BrowserNonClientFrameViewBrowserTest() override = default;

  void SetUp() override {
    embedded_test_server()->ServeFilesFromSourceDirectory(
        "components/test/data");
    ASSERT_TRUE(embedded_test_server()->Start());

    extensions::ExtensionBrowserTest::SetUp();
  }

  // Note: A "bookmark app" is a type of hosted app. All of these tests apply
  // equally to hosted and bookmark apps, but it's easier to install a bookmark
  // app in a test.
  // TODO: Add tests for non-bookmark hosted apps, as bookmark apps will no
  // longer be hosted apps when BMO ships.
  void InstallAndLaunchBookmarkApp(std::optional<GURL> app_url = std::nullopt) {
    blink::mojom::Manifest manifest;
    manifest.manifest_url = embedded_test_server()->GetURL("/manifest");
    manifest.start_url = app_url.value_or(GetAppURL());
    manifest.scope = manifest.start_url.GetWithoutFilename();
    manifest.has_theme_color = true;
    manifest.theme_color = app_theme_color_;

    auto web_app_info =
        web_app::WebAppInstallInfo::CreateWithStartUrlForTesting(
            manifest.start_url);
    web_app::UpdateWebAppInfoFromManifest(manifest, web_app_info.get());

    webapps::AppId app_id =
        web_app::test::InstallWebApp(profile(), std::move(web_app_info));
    app_browser_ = web_app::LaunchWebAppBrowser(profile(), app_id);
    web_contents_ = app_browser_->tab_strip_model()->GetActiveWebContents();
    // Ensure the main page has loaded and is ready for ExecJs DOM
    // manipulation.
    ASSERT_TRUE(content::NavigateToURL(web_contents_, manifest.start_url));

    app_browser_view_ = BrowserView::GetBrowserViewForBrowser(app_browser_);
  }

  // Frame view may get reset after theme change, so always access from the
  // browser view and don't retain the pointer.
  // TODO(crbug.com/40656280): Make it not do this and only refresh the Widget.
  BrowserNonClientFrameView* GetAppFrameView() {
    return app_browser_view_->frame()->GetFrameView();
  }

 protected:
  SkColor app_theme_color_ = SK_ColorBLUE;
  raw_ptr<Browser, AcrossTasksDanglingUntriaged> app_browser_ = nullptr;
  raw_ptr<BrowserView, AcrossTasksDanglingUntriaged> app_browser_view_ =
      nullptr;
  raw_ptr<content::WebContents, AcrossTasksDanglingUntriaged> web_contents_ =
      nullptr;
  autofill::TestAutofillManagerInjector<TestAutofillManager>
      autofill_manager_injector_;

 private:
  GURL GetAppURL() { return embedded_test_server()->GetURL("/empty.html"); }

  web_app::OsIntegrationTestOverrideBlockingRegistration faked_os_integration_;
};

// Tests the frame color for a normal browser window.
IN_PROC_BROWSER_TEST_F(BrowserNonClientFrameViewBrowserTest,
                       BrowserFrameColorThemed) {
  InstallExtension(test_data_dir_.AppendASCII("theme"), 1);

  BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser());
  const BrowserNonClientFrameView* frame_view =
      browser_view->frame()->GetFrameView();
  const ui::ColorProvider* color_provider = frame_view->GetColorProvider();
  const SkColor expected_active_color =
      color_provider->GetColor(ui::kColorFrameActive);
  const SkColor expected_inactive_color =
      color_provider->GetColor(ui::kColorFrameInactive);

  EXPECT_EQ(expected_active_color,
            frame_view->GetFrameColor(BrowserFrameActiveState::kActive));
  EXPECT_EQ(expected_inactive_color,
            frame_view->GetFrameColor(BrowserFrameActiveState::kInactive));
}

// Tests the frame color for a bookmark app when a theme is applied.
IN_PROC_BROWSER_TEST_F(BrowserNonClientFrameViewBrowserTest,
                       BookmarkAppFrameColorCustomTheme) {
  // The theme color should not affect the window, but the theme must not be the
  // default GTK theme for Linux so we install one anyway.
  InstallExtension(test_data_dir_.AppendASCII("theme"), 1);
  InstallAndLaunchBookmarkApp();
  // Note: This is checking for the bookmark app's theme color, not the user's
  // theme color.
  EXPECT_EQ(app_theme_color_,
            GetAppFrameView()->GetFrameColor(BrowserFrameActiveState::kActive));
}

// Tests the frame color for a bookmark app when a theme is applied, with the
// app itself having no theme color.
IN_PROC_BROWSER_TEST_F(BrowserNonClientFrameViewBrowserTest,
                       BookmarkAppFrameColorCustomThemeNoThemeColor) {
  InstallAndLaunchBookmarkApp();
  const SkColor color_without_theme =
      GetAppFrameView()->GetFrameColor(BrowserFrameActiveState::kActive);

  InstallExtension(test_data_dir_.AppendASCII("theme"), 1);
  // Bookmark apps are not affected by browser themes.
  EXPECT_EQ(color_without_theme,
            GetAppFrameView()->GetFrameColor(BrowserFrameActiveState::kActive));
}

// Tests that an opaque frame color is used for a web app with a transparent
// theme color.
IN_PROC_BROWSER_TEST_F(BrowserNonClientFrameViewBrowserTest,
                       OpaqueFrameColorForTransparentWebAppThemeColor) {
  // Ensure we're not using the system theme on Linux.
  ThemeService* theme_service =
      ThemeServiceFactory::GetForProfile(browser()->profile());
  theme_service->UseDefaultTheme();

  app_theme_color_ = SkColorSetA(SK_ColorBLUE, 0x88);
  InstallAndLaunchBookmarkApp();
  EXPECT_EQ(GetAppFrameView()->GetFrameColor(BrowserFrameActiveState::kActive),
            SK_ColorBLUE);
}

// Tests the frame color for a bookmark app when the system theme is applied.
IN_PROC_BROWSER_TEST_F(BrowserNonClientFrameViewBrowserTest,
                       BookmarkAppFrameColorSystemTheme) {
  ThemeService* theme_service =
      ThemeServiceFactory::GetForProfile(browser()->profile());
  // Should be using the system theme by default, but this assert was not true
  // on the bots. Explicitly set.
  theme_service->UseSystemTheme();
  ASSERT_TRUE(theme_service->UsingSystemTheme());

  InstallAndLaunchBookmarkApp();
#if BUILDFLAG(IS_LINUX)
  // On Linux, the system theme is the GTK theme and should change the frame
  // color to the system color (not the app theme color); otherwise the title
  // and border would clash horribly with the GTK title bar.
  // (https://crbug.com/878636)
  const ui::ColorProvider* color_provider =
      GetAppFrameView()->GetColorProvider();
  const SkColor frame_color = color_provider->GetColor(ui::kColorFrameActive);
  EXPECT_EQ(frame_color,
            GetAppFrameView()->GetFrameColor(BrowserFrameActiveState::kActive));
#else
  EXPECT_EQ(app_theme_color_,
            GetAppFrameView()->GetFrameColor(BrowserFrameActiveState::kActive));
#endif
}

// Verifies that the incognito window frame is always the right color.
IN_PROC_BROWSER_TEST_F(BrowserNonClientFrameViewBrowserTest,
                       IncognitoIsCorrectColor) {
  // Set the color that's expected to be ignored.
  auto* theme = ui::NativeTheme::GetInstanceForNativeUi();
  theme->set_user_color(gfx::kGoogleBlue400);
  theme->NotifyOnNativeThemeUpdated();

  Browser* incognito_browser = CreateIncognitoBrowser(browser()->profile());

  BrowserView* view = BrowserView::GetBrowserViewForBrowser(incognito_browser);
  BrowserFrame* frame = view->frame();
  BrowserNonClientFrameView* frame_view = frame->GetFrameView();

  color_utils::HSL frame_color_hsl;
  SkColorToHSL(frame_view->GetFrameColor(BrowserFrameActiveState::kActive),
               &frame_color_hsl);
  // Ensure that the frame color is very dark in Incognito.
  EXPECT_LT(frame_color_hsl.l, 0.2);

  incognito_browser->window()->Close();
}

// Checks that the title bar for hosted app windows is hidden when in fullscreen
// for tab mode.
IN_PROC_BROWSER_TEST_F(BrowserNonClientFrameViewBrowserTest,
                       FullscreenForTabTitlebarHeight) {
  InstallAndLaunchBookmarkApp();
  static_cast<content::WebContentsDelegate*>(app_browser_)
      ->EnterFullscreenModeForTab(web_contents_->GetPrimaryMainFrame(), {});

  EXPECT_EQ(GetAppFrameView()->GetTopInset(false), 0);
}

// Tests that the custom tab bar is visible in fullscreen mode.
// TODO(crbug.com/40855995): Flaky on linux-wayland-rel.
#if BUILDFLAG(IS_LINUX)
#define MAYBE_CustomTabBarIsVisibleInFullscreen \
  DISABLED_CustomTabBarIsVisibleInFullscreen
#else
#define MAYBE_CustomTabBarIsVisibleInFullscreen \
  CustomTabBarIsVisibleInFullscreen
#endif
IN_PROC_BROWSER_TEST_F(BrowserNonClientFrameViewBrowserTest,
                       MAYBE_CustomTabBarIsVisibleInFullscreen) {
  InstallAndLaunchBookmarkApp();

  ASSERT_TRUE(
      ui_test_utils::NavigateToURL(app_browser_, GURL("http://example.com")));

  static_cast<content::WebContentsDelegate*>(app_browser_)
      ->EnterFullscreenModeForTab(web_contents_->GetPrimaryMainFrame(), {});

  EXPECT_TRUE(app_browser_view_->toolbar()->custom_tab_bar()->IsDrawn());
}

// Tests that hosted app frames reflect the theme color set by HTML meta tags.
IN_PROC_BROWSER_TEST_F(BrowserNonClientFrameViewBrowserTest,
                       HTMLMetaThemeColorOverridesManifest) {
  // Ensure we're not using the system theme on Linux.
  ThemeService* theme_service =
      ThemeServiceFactory::GetForProfile(browser()->profile());
  theme_service->UseDefaultTheme();

  InstallAndLaunchBookmarkApp();
  ASSERT_EQ(app_theme_color_, SK_ColorBLUE);
  EXPECT_EQ(
      GetAppFrameView()->GetFrameColor(BrowserFrameActiveState::kUseCurrent),
      app_theme_color_);

  views::View* const window_title_view =
      GetAppFrameView()->GetViewByID(VIEW_ID_WINDOW_TITLE);
  views::Label* const window_title =
      window_title_view ? static_cast<views::Label*>(window_title_view)
                        : nullptr;
  if (window_title) {
    EXPECT_EQ(window_title->GetBackgroundColor(), app_theme_color_);
  }

  {
    // Add two meta theme color elements. The first element's color should be
    // picked.
    content::ThemeChangeWaiter waiter(web_contents_);
    EXPECT_TRUE(content::ExecJs(
        web_contents_.get(),
        "document.documentElement.innerHTML = '"
        "<meta id=\"first\"  name=\"theme-color\" content=\"red\">"
        "<meta id=\"second\" name=\"theme-color\" content=\"#00ff00\">'"));
    waiter.Wait();

    EXPECT_EQ(
        GetAppFrameView()->GetFrameColor(BrowserFrameActiveState::kUseCurrent),
        SK_ColorRED);
    if (window_title) {
      EXPECT_EQ(window_title->GetBackgroundColor(), SK_ColorRED);
    }
  }
  {
    // Change the color of the first element. The new color should be picked.
    content::ThemeChangeWaiter waiter(web_contents_);
    EXPECT_TRUE(content::ExecJs(
        web_contents_.get(),
        "document.getElementById('first').setAttribute('content', 'yellow')"));
    waiter.Wait();

    EXPECT_EQ(
        GetAppFrameView()->GetFrameColor(BrowserFrameActiveState::kUseCurrent),
        SK_ColorYELLOW);
    if (window_title) {
      EXPECT_EQ(window_title->GetBackgroundColor(), SK_ColorYELLOW);
    }
  }
  {
    // Set a non matching media query to the first element. The second element's
    // color should be picked.
    content::ThemeChangeWaiter waiter(web_contents_);
    EXPECT_TRUE(content::ExecJs(web_contents_.get(),
                                "document.getElementById('first')."
                                "setAttribute('media', '(max-width: 0px)')"));
    waiter.Wait();

    EXPECT_EQ(
        GetAppFrameView()->GetFrameColor(BrowserFrameActiveState::kUseCurrent),
        SK_ColorGREEN);
  }
  {
    // Remove the second element. The manifest color should be picked because
    // the first element still does not match.
    content::ThemeChangeWaiter waiter(web_contents_);
    EXPECT_TRUE(content::ExecJs(web_contents_.get(),
                                "document.getElementById('second').remove()"));
    waiter.Wait();

    EXPECT_EQ(
        GetAppFrameView()->GetFrameColor(BrowserFrameActiveState::kUseCurrent),
        SK_ColorBLUE);
  }
  {
    // Set a matching media query to the first element. The first element's
    // color should be picked.
    content::ThemeChangeWaiter waiter(web_contents_);
    std::string width =
        content::EvalJs(web_contents_.get(), "innerWidth.toString()")
            .ExtractString();
    EXPECT_TRUE(content::ExecJs(web_contents_.get(),
                                "document.getElementById('first')."
                                "setAttribute('media', '(max-width: " +
                                    width + "px')"));
    waiter.Wait();

    EXPECT_EQ(
        GetAppFrameView()->GetFrameColor(BrowserFrameActiveState::kUseCurrent),
        SK_ColorYELLOW);
  }
  {
    // Resize the window so that the media query on the first element does not
    // match anymore. The manifest color should be picked.
    content::ThemeChangeWaiter waiter(web_contents_);
    EXPECT_TRUE(content::ExecJs(web_contents_.get(), "window.resizeBy(24, 0)"));
    waiter.Wait();

    EXPECT_EQ(
        GetAppFrameView()->GetFrameColor(BrowserFrameActiveState::kUseCurrent),
        SK_ColorBLUE);
  }
}

class SaveCardOfferObserver
    : public autofill::CreditCardSaveManager::ObserverForTest {
 public:
  explicit SaveCardOfferObserver(content::WebContents* web_contents) {
    manager_ = autofill::ContentAutofillDriver::GetForRenderFrameHost(
                   web_contents->GetPrimaryMainFrame())
                   ->GetAutofillManager()
                   .client()
                   .GetFormDataImporter()
                   ->GetCreditCardSaveManager();
    manager_->SetEventObserverForTesting(this);
  }

  ~SaveCardOfferObserver() override {
    manager_->SetEventObserverForTesting(nullptr);
  }

  // CreditCardSaveManager::ObserverForTest:
  void OnOfferLocalSave() override { run_loop_.Quit(); }

  void Wait() { run_loop_.Run(); }

 private:
  raw_ptr<autofill::CreditCardSaveManager> manager_ = nullptr;
  base::RunLoop run_loop_;
};

// TODO(crbug.com/40866991): Test is flaky.
// Tests that hosted app frames reflect the theme color set by HTML meta tags.
IN_PROC_BROWSER_TEST_F(BrowserNonClientFrameViewBrowserTest,
                       DISABLED_SaveCardIcon) {
  InstallAndLaunchBookmarkApp(embedded_test_server()->GetURL(
      "/autofill/credit_card_upload_form_address_and_cc.html"));
  ASSERT_TRUE(autofill_manager_injector_[web_contents_]->WaitForFormsSeen(1));
  ASSERT_TRUE(content::ExecJs(web_contents_.get(), "fill_form.click();"));

  content::TestNavigationObserver nav_observer(web_contents_);
  SaveCardOfferObserver offer_observer(web_contents_);
  ASSERT_TRUE(content::ExecJs(web_contents_.get(), "submit.click();"));
  nav_observer.Wait();
  offer_observer.Wait();

  PageActionIconView* icon =
      app_browser_view_->toolbar_button_provider()->GetPageActionIconView(
          PageActionIconType::kSaveCard);
  EXPECT_TRUE(GetAppFrameView()->Contains(icon));
  EXPECT_TRUE(icon->GetVisible());
}

#if BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(BrowserNonClientFrameViewBrowserTest,
                       BrowserFrameWindowMask) {
  BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser());
  BrowserNonClientFrameView* frame_view = browser_view->frame()->GetFrameView();
  SkPath path;
  frame_view->GetWindowMask(frame_view->bounds().size(), &path);
  EXPECT_TRUE(path.isEmpty());
}
#endif  // BUILDFLAG(IS_CHROMEOS)