1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/web_applications/components/web_app_helpers.h"
6
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/browser_list.h"
9 #include "chrome/browser/ui/browser_navigator.h"
10 #include "chrome/browser/ui/browser_navigator_params.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/browser/ui/web_applications/app_browser_controller.h"
13 #include "chrome/browser/ui/web_applications/web_app_controller_browsertest.h"
14 #include "chrome/browser/web_applications/components/web_app_id.h"
15 #include "content/public/test/browser_test.h"
16 #include "ui/base/page_transition_types.h"
17 #include "ui/base/window_open_disposition.h"
18 #include "url/gurl.h"
19
20 namespace web_app {
21
22 class WebAppNavigateBrowserTest : public WebAppControllerBrowserTestBase {
23 public:
GetGoogleURL()24 static GURL GetGoogleURL() { return GURL("http://www.google.com/"); }
25
MakeNavigateParams() const26 NavigateParams MakeNavigateParams() const {
27 NavigateParams params(browser(), GetGoogleURL(), ui::PAGE_TRANSITION_LINK);
28 params.window_action = NavigateParams::SHOW_WINDOW;
29 return params;
30 }
31 };
32
33 // This test verifies that navigating with "open_pwa_window_if_possible = true"
34 // opens a new app window if there is an installed Web App for the URL.
IN_PROC_BROWSER_TEST_F(WebAppNavigateBrowserTest,AppInstalled_OpenAppWindowIfPossible_True)35 IN_PROC_BROWSER_TEST_F(WebAppNavigateBrowserTest,
36 AppInstalled_OpenAppWindowIfPossible_True) {
37 InstallPWA(GetGoogleURL());
38
39 NavigateParams params(MakeNavigateParams());
40 params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
41 params.open_pwa_window_if_possible = true;
42 Navigate(¶ms);
43
44 EXPECT_NE(browser(), params.browser);
45 EXPECT_FALSE(params.browser->is_type_normal());
46 EXPECT_TRUE(params.browser->is_type_app());
47 EXPECT_TRUE(params.browser->is_trusted_source());
48 }
49
50 // This test verifies that navigating with "open_pwa_window_if_possible = false"
51 // opens a new foreground tab even if there is an installed Web App for the
52 // URL.
IN_PROC_BROWSER_TEST_F(WebAppNavigateBrowserTest,AppInstalled_OpenAppWindowIfPossible_False)53 IN_PROC_BROWSER_TEST_F(WebAppNavigateBrowserTest,
54 AppInstalled_OpenAppWindowIfPossible_False) {
55 InstallPWA(GetGoogleURL());
56
57 int num_tabs = browser()->tab_strip_model()->count();
58
59 NavigateParams params(MakeNavigateParams());
60 params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
61 params.open_pwa_window_if_possible = false;
62 Navigate(¶ms);
63
64 EXPECT_EQ(browser(), params.browser);
65 EXPECT_EQ(++num_tabs, browser()->tab_strip_model()->count());
66 }
67
68 // This test verifies that navigating with "open_pwa_window_if_possible = true"
69 // opens a new foreground tab when there is no app installed for the URL.
IN_PROC_BROWSER_TEST_F(WebAppNavigateBrowserTest,NoAppInstalled_OpenAppWindowIfPossible)70 IN_PROC_BROWSER_TEST_F(WebAppNavigateBrowserTest,
71 NoAppInstalled_OpenAppWindowIfPossible) {
72 int num_tabs = browser()->tab_strip_model()->count();
73
74 NavigateParams params(MakeNavigateParams());
75 params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
76 params.open_pwa_window_if_possible = true;
77 Navigate(¶ms);
78
79 EXPECT_EQ(browser(), params.browser);
80 EXPECT_EQ(++num_tabs, browser()->tab_strip_model()->count());
81 }
82
IN_PROC_BROWSER_TEST_F(WebAppNavigateBrowserTest,NewPopup)83 IN_PROC_BROWSER_TEST_F(WebAppNavigateBrowserTest, NewPopup) {
84 BrowserList* const browser_list = BrowserList::GetInstance();
85 InstallPWA(GetGoogleURL());
86
87 {
88 NavigateParams params(MakeNavigateParams());
89 params.disposition = WindowOpenDisposition::NEW_WINDOW;
90 params.open_pwa_window_if_possible = true;
91 Navigate(¶ms);
92 }
93 Browser* const app_browser = browser_list->GetLastActive();
94 EXPECT_TRUE(app_browser->app_controller()->HasAppId());
95
96 {
97 NavigateParams params(MakeNavigateParams());
98 params.disposition = WindowOpenDisposition::NEW_WINDOW;
99 params.extension_app_id = app_browser->app_controller()->GetAppId();
100 Navigate(¶ms);
101 }
102 content::WebContents* const web_contents =
103 browser_list->GetLastActive()->tab_strip_model()->GetActiveWebContents();
104
105 {
106 // From a browser tab, a popup window opens.
107 NavigateParams params(MakeNavigateParams());
108 params.disposition = WindowOpenDisposition::NEW_POPUP;
109 params.source_contents = web_contents;
110 Navigate(¶ms);
111 EXPECT_FALSE(browser_list->GetLastActive()->app_controller());
112 }
113
114 {
115 // From a browser tab, an app window opens if app_id is specified.
116 NavigateParams params(MakeNavigateParams());
117 params.extension_app_id = app_browser->app_controller()->GetAppId();
118 params.disposition = WindowOpenDisposition::NEW_POPUP;
119 Navigate(¶ms);
120 EXPECT_TRUE(browser_list->GetLastActive()->app_controller()->HasAppId());
121 }
122
123 {
124 // From an app window, another app window opens.
125 NavigateParams params(MakeNavigateParams());
126 params.browser = app_browser;
127 params.disposition = WindowOpenDisposition::NEW_POPUP;
128 Navigate(¶ms);
129 EXPECT_TRUE(browser_list->GetLastActive()->app_controller()->HasAppId());
130 }
131 }
132
133 } // namespace web_app
134