File: app_browser_controller_browsertest_chromeos.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 (395 lines) | stat: -rw-r--r-- 14,995 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
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "chrome/browser/ash/browser_delegate/browser_delegate.h"
#include "chrome/browser/ash/system_web_apps/test_support/test_system_web_app_installation.h"
#include "chrome/browser/devtools/protocol/browser_handler.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/themes/custom_theme_supplier.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/ui/ash/system_web_apps/system_web_app_ui_utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/extensions/application_launch.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/ui/window_sizer/window_sizer.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_install_manager.h"
#include "chrome/browser/web_applications/web_app_utils.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/theme_change_waiter.h"
#include "extensions/browser/extension_registry.h"
#include "third_party/blink/public/mojom/frame/fullscreen.mojom.h"
#include "ui/display/types/display_constants.h"

namespace {
SkColor GetFrameColor(Browser* browser) {
  CustomThemeSupplier* theme = browser->app_controller()->GetThemeSupplier();
  SkColor result;
  EXPECT_TRUE(theme->GetColor(ThemeProperties::COLOR_FRAME_ACTIVE, &result));
  return result;
}
}  // namespace

namespace web_app {

class LoadFinishedWaiter : public TabStripModelObserver,
                           public content::WebContentsObserver {
 public:
  explicit LoadFinishedWaiter(Browser* browser) : browser_(browser) {
    browser_->tab_strip_model()->AddObserver(this);
  }

  ~LoadFinishedWaiter() override {
    browser_->tab_strip_model()->RemoveObserver(this);
  }

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

  SkColor GetColorAtNavigation() const { return color_at_navigation_; }

  // TabStripModelObserver:
  void OnTabStripModelChanged(
      TabStripModel* tab_strip_model,
      const TabStripModelChange& change,
      const TabStripSelectionChange& selection) override {
    if (selection.active_tab_changed()) {
      content::WebContentsObserver::Observe(selection.new_contents);
    }
  }

  // content::WebContentsObserver:
  void DidFinishNavigation(content::NavigationHandle* handle) override {
    color_at_navigation_ = GetFrameColor(browser_);
  }
  void DidFinishLoad(content::RenderFrameHost* render_frame_host,
                     const GURL& validated_url) override {
    run_loop_.Quit();
  }

 private:
  raw_ptr<Browser> browser_ = nullptr;
  SkColor color_at_navigation_;
  base::RunLoop run_loop_;
};

class AppBrowserControllerBrowserTest : public InProcessBrowserTest {
 public:
  AppBrowserControllerBrowserTest()
      : test_system_web_app_installation_(
            ash::TestSystemWebAppInstallation::SetUpTabbedMultiWindowApp()) {}
  AppBrowserControllerBrowserTest(const AppBrowserControllerBrowserTest&) =
      delete;
  AppBrowserControllerBrowserTest& operator=(
      const AppBrowserControllerBrowserTest&) = delete;

 protected:
  Profile* profile() {
    if (!profile_) {
      profile_ = browser()->profile();
    }
    return profile_;
  }

  void InstallMockSystemWebApp() {
    test_system_web_app_installation_->WaitForAppInstall();
  }

  Browser* LaunchMockApp() {
    app_browser_ = web_app::LaunchWebAppBrowser(
        profile(), test_system_web_app_installation_->GetAppId());
    tabbed_app_url_ = test_system_web_app_installation_->GetAppUrl();
    EXPECT_TRUE(content::NavigateToURL(
        app_browser_->tab_strip_model()->GetActiveWebContents(),
        tabbed_app_url_));
    return app_browser_;
  }

  void LaunchMockPopup() {
    auto params = ash::CreateSystemWebAppLaunchParams(
        profile(), test_system_web_app_installation_->GetType(),
        display::kInvalidDisplayId);
    EXPECT_TRUE(params.has_value());
    params->disposition = WindowOpenDisposition::NEW_POPUP;

    ash::BrowserDelegate* delegate = ash::LaunchSystemWebAppImpl(
        profile(), test_system_web_app_installation_->GetType(),
        test_system_web_app_installation_->GetAppUrl(), *params);
    app_browser_ = delegate ? &delegate->GetBrowser() : nullptr;
  }

  Browser* LaunchMockSWA() {
    auto params = ash::CreateSystemWebAppLaunchParams(
        profile(), test_system_web_app_installation_->GetType(),
        display::kInvalidDisplayId);
    EXPECT_TRUE(params.has_value());
    params->disposition = WindowOpenDisposition::NEW_WINDOW;

    ash::BrowserDelegate* delegate = ash::LaunchSystemWebAppImpl(
        profile(), test_system_web_app_installation_->GetType(),
        test_system_web_app_installation_->GetAppUrl(), *params);
    return delegate ? &delegate->GetBrowser() : nullptr;
  }

  Browser* InstallAndLaunchMockApp() {
    InstallMockSystemWebApp();
    return LaunchMockApp();
  }

  void InstallAndLaunchMockPopup() {
    InstallMockSystemWebApp();
    LaunchMockPopup();
  }

  Browser* InstallAndLaunchMockSWA() {
    InstallMockSystemWebApp();
    return LaunchMockSWA();
  }

  GURL GetActiveTabURL() {
    return app_browser_->tab_strip_model()
        ->GetActiveWebContents()
        ->GetVisibleURL();
  }

  raw_ptr<Profile, AcrossTasksDanglingUntriaged> profile_ = nullptr;
  raw_ptr<Browser, AcrossTasksDanglingUntriaged> app_browser_ = nullptr;
  GURL tabbed_app_url_;

 private:
  std::unique_ptr<ash::TestSystemWebAppInstallation>
      test_system_web_app_installation_;
};

IN_PROC_BROWSER_TEST_F(AppBrowserControllerBrowserTest, TabsTest) {
  InstallAndLaunchMockApp();

  EXPECT_TRUE(app_browser_->SupportsWindowFeature(Browser::FEATURE_TABSTRIP));

  // No favicons shown for web apps.
  EXPECT_FALSE(
      app_browser_->tab_strip_model()->delegate()->ShouldDisplayFavicon(
          app_browser_->tab_strip_model()->GetActiveWebContents()));

  // Tabbed PWAs only open URLs within the scope of the app. The manifest is
  // another URL besides |tabbed_app_url_| in scope.
  GURL manifest("chrome://test-system-app/manifest.json");
  // Check URL of tab1.
  EXPECT_EQ(GetActiveTabURL(), tabbed_app_url_);
  // Create tab2 with another URL from app, check URL, number of tabs.
  chrome::AddTabAt(app_browser_, manifest, -1, true);
  EXPECT_EQ(app_browser_->tab_strip_model()->count(), 2);
  EXPECT_EQ(GetActiveTabURL(), manifest);
  // Create tab3 with default URL, check URL, number of tabs.
  chrome::NewTab(app_browser_);
  EXPECT_EQ(app_browser_->tab_strip_model()->count(), 3);
  EXPECT_EQ(GetActiveTabURL(), tabbed_app_url_);
  // Switch to tab1, check URL.
  chrome::SelectNextTab(app_browser_);
  EXPECT_EQ(app_browser_->tab_strip_model()->count(), 3);
  EXPECT_EQ(GetActiveTabURL(), tabbed_app_url_);
  // Switch to tab2, check URL.
  chrome::SelectNextTab(app_browser_);
  EXPECT_EQ(app_browser_->tab_strip_model()->count(), 3);
  EXPECT_EQ(GetActiveTabURL(), manifest);
  // Switch to tab3, check URL.
  chrome::SelectNextTab(app_browser_);
  EXPECT_EQ(app_browser_->tab_strip_model()->count(), 3);
  EXPECT_EQ(GetActiveTabURL(), tabbed_app_url_);
  // Close tab3, check number of tabs.
  chrome::CloseTab(app_browser_);
  EXPECT_EQ(app_browser_->tab_strip_model()->count(), 2);
  EXPECT_EQ(GetActiveTabURL(), manifest);
  // Close tab2, check number of tabs.
  chrome::CloseTab(app_browser_);
  EXPECT_EQ(app_browser_->tab_strip_model()->count(), 1);
  EXPECT_EQ(GetActiveTabURL(), tabbed_app_url_);

  // Enter tab fullscreen, check tab strip not supported.
  static_cast<content::WebContentsDelegate*>(app_browser_)
      ->EnterFullscreenModeForTab(app_browser_->tab_strip_model()
                                      ->GetActiveWebContents()
                                      ->GetPrimaryMainFrame(),
                                  {});
  EXPECT_FALSE(app_browser_->SupportsWindowFeature(Browser::FEATURE_TABSTRIP));
}

IN_PROC_BROWSER_TEST_F(AppBrowserControllerBrowserTest, NonAppUrl) {
  InstallAndLaunchMockApp();

  // Check we have 2 browsers: |browser()| and |app_browser_|.
  EXPECT_EQ(BrowserList::GetInstance()->size(), 2u);
  EXPECT_NE(browser(), app_browser_);
  EXPECT_TRUE(browser()->is_type_normal());
  EXPECT_EQ(browser()->tab_strip_model()->count(), 1);
  EXPECT_EQ(
      browser()->tab_strip_model()->GetActiveWebContents()->GetVisibleURL(),
      "about:blank");
  EXPECT_TRUE(app_browser_->is_type_app());
  EXPECT_EQ(app_browser_->tab_strip_model()->count(), 1);
  EXPECT_EQ(GetActiveTabURL(), tabbed_app_url_);

  // Create tab2 with URL not from app, it will open in the NORMAL browser.
  chrome::AddTabAt(app_browser_, GURL(chrome::kChromeUINewTabURL), -1, true);
  EXPECT_EQ(BrowserList::GetInstance()->size(), 2u);
  EXPECT_NE(browser(), app_browser_);
  EXPECT_TRUE(browser()->is_type_normal());
  EXPECT_EQ(browser()->tab_strip_model()->count(), 2);
  EXPECT_EQ(
      browser()->tab_strip_model()->GetActiveWebContents()->GetVisibleURL(),
      "chrome://newtab/");
  EXPECT_TRUE(app_browser_->is_type_app());
  EXPECT_EQ(app_browser_->tab_strip_model()->count(), 1);
  EXPECT_EQ(GetActiveTabURL(), tabbed_app_url_);
}

IN_PROC_BROWSER_TEST_F(AppBrowserControllerBrowserTest, TabLoadNoThemeChange) {
  InstallAndLaunchMockApp();
  EXPECT_EQ(app_browser_->tab_strip_model()->count(), 1);
  // Frame gets manifest theme immediately.
  EXPECT_EQ(GetFrameColor(app_browser_), SK_ColorGREEN);

  // Dynamically change color.
  content::WebContents* web_contents =
      app_browser_->tab_strip_model()->GetActiveWebContents();
  content::ThemeChangeWaiter theme_waiter(web_contents);
  EXPECT_TRUE(content::ExecJs(web_contents, R"(
      const el = document.createElement("meta");
      el.setAttribute("name", "theme-color");
      el.setAttribute("content", "yellow");
      document.documentElement.appendChild(el);
  )",
                              content::EXECUTE_SCRIPT_DEFAULT_OPTIONS,
                              /*world_id=*/1));
  theme_waiter.Wait();
  EXPECT_EQ(GetFrameColor(app_browser_), SK_ColorYELLOW);

  // Second tab keeps dynamic theme until loaded.
  LoadFinishedWaiter load_waiter(app_browser_);
  chrome::NewTab(app_browser_);
  load_waiter.Wait();
  EXPECT_EQ(app_browser_->tab_strip_model()->count(), 2);
  EXPECT_EQ(load_waiter.GetColorAtNavigation(), SK_ColorYELLOW);
  EXPECT_EQ(GetFrameColor(app_browser_), SK_ColorGREEN);

  // Switching tabs updates themes immediately.
  chrome::SelectNextTab(app_browser_);
  EXPECT_EQ(GetFrameColor(app_browser_), SK_ColorYELLOW);
  chrome::SelectNextTab(app_browser_);
  EXPECT_EQ(GetFrameColor(app_browser_), SK_ColorGREEN);
}

IN_PROC_BROWSER_TEST_F(AppBrowserControllerBrowserTest,
                       WhiteThemeForSystemAppPopup) {
  InstallAndLaunchMockPopup();
  EXPECT_FALSE(app_browser_->app_controller()->GetThemeColor().has_value());
}

IN_PROC_BROWSER_TEST_F(AppBrowserControllerBrowserTest, Shutdown) {
  // Cache profile before browser() closes.
  profile();
  InstallMockSystemWebApp();

  BrowserHandler handler(nullptr, std::string());
  handler.Close();
  ui_test_utils::WaitForBrowserToClose();

  LaunchMockPopup();
  EXPECT_EQ(app_browser_, nullptr);
}

IN_PROC_BROWSER_TEST_F(AppBrowserControllerBrowserTest,
                       ReuseBrowserForSystemAppPopup) {
  InstallAndLaunchMockPopup();
  // We should have the original browser for this BrowserTest, plus new popup.
  EXPECT_EQ(BrowserList::GetInstance()->size(), 2u);
  InstallAndLaunchMockPopup();
  EXPECT_EQ(BrowserList::GetInstance()->size(), 2u);
}

IN_PROC_BROWSER_TEST_F(AppBrowserControllerBrowserTest,
                       OpenMultipleBrowsersForMultiWindowSWA) {
  Browser* first_browser = InstallAndLaunchMockSWA();
  // We should have the original browser for this BrowserTest, plus a new one,
  // offset by a tasteful amount.
  EXPECT_NE(nullptr, first_browser);
  EXPECT_EQ(BrowserList::GetInstance()->size(), 2u);
  Browser* second_browser = LaunchMockSWA();
  EXPECT_NE(nullptr, second_browser);
  EXPECT_EQ(BrowserList::GetInstance()->size(), 3u);

  auto bounds1 = first_browser->window()->GetRestoredBounds();
  auto bounds2 = second_browser->window()->GetRestoredBounds();
  // We've already hit the bottom bounds, so the y axis didn't move.
  EXPECT_EQ(bounds1.x() + WindowSizer::kWindowTilePixels, bounds2.x());

  // Open a ton of windows until they start stacking.
  bool hit_the_bottom_right = false;
  gfx::Rect previous_bounds = bounds2;
  for (int i = 0; i < 10; i++) {
    Browser* next_browser = LaunchMockSWA();
    if (previous_bounds == next_browser->window()->GetRestoredBounds()) {
      hit_the_bottom_right = true;
      break;
    }
    previous_bounds = next_browser->window()->GetRestoredBounds();
  }

  EXPECT_TRUE(hit_the_bottom_right);
}

IN_PROC_BROWSER_TEST_F(AppBrowserControllerBrowserTest,
                       NoExtensionsContainerExists) {
  InstallAndLaunchMockPopup();
  EXPECT_EQ(app_browser_->window()->GetExtensionsContainer(), nullptr);
}

class AppBrowserControllerChromeUntrustedBrowserTest
    : public InProcessBrowserTest {
 public:
  AppBrowserControllerChromeUntrustedBrowserTest()
      : test_system_web_app_installation_(
            ash::TestSystemWebAppInstallation::SetUpChromeUntrustedApp()) {}

 protected:
  Browser* InstallAndLaunchMockApp() {
    test_system_web_app_installation_->WaitForAppInstall();
    Browser* app_browser = web_app::LaunchWebAppBrowser(
        browser()->profile(), test_system_web_app_installation_->GetAppId());
    CHECK(content::NavigateToURL(
        app_browser->tab_strip_model()->GetActiveWebContents(),
        test_system_web_app_installation_->GetAppUrl()));
    return app_browser;
  }

 private:
  std::unique_ptr<ash::TestSystemWebAppInstallation>
      test_system_web_app_installation_;
};

IN_PROC_BROWSER_TEST_F(AppBrowserControllerChromeUntrustedBrowserTest,
                       DoesNotShowToolbar) {
  Browser* app_browser = InstallAndLaunchMockApp();
  EXPECT_FALSE(app_browser->app_controller()->ShouldShowCustomTabBar());
}

}  // namespace web_app