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

#include <utility>

#include "base/memory/raw_ptr.h"
#include "base/strings/stringprintf.h"
#include "base/test/run_until.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "components/guest_view/browser/guest_view_manager.h"
#include "components/guest_view/browser/guest_view_manager_factory.h"
#include "components/guest_view/browser/test_guest_view_manager.h"
#include "content/public/browser/child_process_termination_info.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_process_host_observer.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/api/app_runtime/app_runtime_api.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "extensions/browser/extension_host.h"
#include "extensions/browser/guest_view/app_view/app_view_constants.h"
#include "extensions/browser/guest_view/app_view/app_view_guest.h"
#include "extensions/browser/guest_view/extensions_guest_view_manager_delegate.h"
#include "extensions/browser/guest_view/web_view/web_view_guest.h"
#include "extensions/browser/process_manager.h"
#include "extensions/common/switches.h"
#include "extensions/test/extension_test_message_listener.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "testing/gtest/include/gtest/gtest.h"

using extensions::ExtensionsAPIClient;
using guest_view::GuestViewManager;
using guest_view::TestGuestViewManagerFactory;

class AppViewTest : public extensions::PlatformAppBrowserTest,
                    public testing::WithParamInterface<bool> {
 public:
  static std::string DescribeParams(
      const testing::TestParamInfo<ParamType>& info) {
    return info.param ? "MPArch" : "InnerWebContents";
  }

  AppViewTest() {
    scoped_feature_list_.InitWithFeatureState(features::kGuestViewMPArch,
                                              GetParam());
  }

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

  enum TestServer { NEEDS_TEST_SERVER, NO_TEST_SERVER };

  void TestHelper(const std::string& test_name,
                  const std::string& app_location,
                  const std::string& app_to_embed,
                  TestServer test_server) {
    // For serving guest pages.
    if (test_server == NEEDS_TEST_SERVER) {
      if (!StartEmbeddedTestServer()) {
        LOG(ERROR) << "FAILED TO START TEST SERVER.";
        return;
      }
    }

    LoadAndLaunchPlatformApp(app_location.c_str(), "Launched");

    // Flush any pending events to make sure we start with a clean slate.
    content::RunAllPendingInMessageLoop();

    content::WebContents* embedder_web_contents =
        GetFirstAppWindowWebContents();
    if (!embedder_web_contents) {
      LOG(ERROR) << "UNABLE TO FIND EMBEDDER WEB CONTENTS.";
      return;
    }

    ExtensionTestMessageListener done_listener("TEST_PASSED");
    done_listener.set_failure_message("TEST_FAILED");
    if (!content::ExecJs(
            embedder_web_contents,
            base::StringPrintf("runTest('%s', '%s')", test_name.c_str(),
                               app_to_embed.c_str()))) {
      LOG(ERROR) << "UNABLE TO START TEST.";
      return;
    }
    ASSERT_TRUE(done_listener.WaitUntilSatisfied());
  }

  guest_view::TestGuestViewManager* test_guest_view_manager() const {
    return test_guest_view_manager_;
  }

  void CloseAppWindow() {
    content::WebContents* embedder_web_contents =
        GetFirstAppWindowWebContents();
    content::WebContentsDestroyedWatcher destroyed_watcher(
        embedder_web_contents);
    EXPECT_TRUE(content::ExecJs(embedder_web_contents, "window.close()"));
    destroyed_watcher.Wait();
  }

  // Completes an onEmbedRequested request in `app`. Assumes the app has a
  // `continueEmbedding` function which does this.
  void ContinueEmbedding(const extensions::Extension* app, bool allow_request) {
    content::WebContents* host_contents =
        extensions::ProcessManager::Get(browser()->profile())
            ->GetBackgroundHostForExtension(app->id())
            ->host_contents();
    ASSERT_TRUE(content::WaitForLoadStop(host_contents));
    ASSERT_TRUE(content::ExecJs(
        host_contents,
        content::JsReplace("continueEmbedding($1);", allow_request)));
  }

 private:
  void SetUpOnMainThread() override {
    extensions::PlatformAppBrowserTest::SetUpOnMainThread();
    test_guest_view_manager_ = factory_.GetOrCreateTestGuestViewManager(
        browser()->profile(),
        ExtensionsAPIClient::Get()->CreateGuestViewManagerDelegate());
  }

  void TearDownOnMainThread() override {
    test_guest_view_manager_ = nullptr;
    extensions::PlatformAppBrowserTest::TearDownOnMainThread();
  }

  TestGuestViewManagerFactory factory_;
  raw_ptr<guest_view::TestGuestViewManager> test_guest_view_manager_ = nullptr;
  base::test::ScopedFeatureList scoped_feature_list_;
};

INSTANTIATE_TEST_SUITE_P(/* no prefix */,
                         AppViewTest,
                         testing::Bool(),
                         AppViewTest::DescribeParams);

// Tests that <appview> is able to navigate to another installed app.
IN_PROC_BROWSER_TEST_P(AppViewTest, TestAppViewWithUndefinedDataShouldSucceed) {
  const extensions::Extension* skeleton_app =
      InstallPlatformApp("app_view/shim/skeleton");
  TestHelper("testAppViewWithUndefinedDataShouldSucceed", "app_view/shim",
             skeleton_app->id(), NO_TEST_SERVER);
  // Note that the callback of the appview connect method runs after guest
  // creation, but not necessarily after attachment. So we now ensure that the
  // guest successfully attaches and loads.
  EXPECT_TRUE(test_guest_view_manager()->WaitUntilAttachedAndLoaded(
      test_guest_view_manager()->WaitForSingleGuestViewCreated()));
}

// Tests that <appview> correctly processes parameters passed on connect.
IN_PROC_BROWSER_TEST_P(AppViewTest, TestAppViewRefusedDataShouldFail) {
  const extensions::Extension* skeleton_app =
      InstallPlatformApp("app_view/shim/skeleton");
  TestHelper("testAppViewRefusedDataShouldFail", "app_view/shim",
             skeleton_app->id(), NO_TEST_SERVER);
}

// Tests that <appview> correctly processes parameters passed on connect.
IN_PROC_BROWSER_TEST_P(AppViewTest, TestAppViewGoodDataShouldSucceed) {
  const extensions::Extension* skeleton_app =
      InstallPlatformApp("app_view/shim/skeleton");
  TestHelper("testAppViewGoodDataShouldSucceed", "app_view/shim",
             skeleton_app->id(), NO_TEST_SERVER);
  EXPECT_TRUE(test_guest_view_manager()->WaitUntilAttachedAndLoaded(
      test_guest_view_manager()->WaitForSingleGuestViewCreated()));
}

// Tests that <appview> correctly handles multiple successive connects.
IN_PROC_BROWSER_TEST_P(AppViewTest, TestAppViewMultipleConnects) {
  const extensions::Extension* skeleton_app =
      InstallPlatformApp("app_view/shim/skeleton");
  TestHelper("testAppViewMultipleConnects", "app_view/shim", skeleton_app->id(),
             NO_TEST_SERVER);
  EXPECT_TRUE(test_guest_view_manager()->WaitUntilAttachedAndLoaded(
      test_guest_view_manager()->WaitForSingleGuestViewCreated()));
}

// Tests that <appview> correctly handles connects that occur after the
// completion of a previous connect.
IN_PROC_BROWSER_TEST_P(AppViewTest,
                       TestAppViewConnectFollowingPreviousConnect) {
  const extensions::Extension* skeleton_app =
      InstallPlatformApp("app_view/shim/skeleton");
  TestHelper("testAppViewConnectFollowingPreviousConnect", "app_view/shim",
             skeleton_app->id(), NO_TEST_SERVER);
  EXPECT_TRUE(test_guest_view_manager()->WaitUntilAttachedAndLoaded(
      test_guest_view_manager()->WaitForSingleGuestViewCreated()));
}

// Tests that <appview> does not embed self (the app which owns appview).
IN_PROC_BROWSER_TEST_P(AppViewTest, TestAppViewEmbedSelfShouldFail) {
  const extensions::Extension* skeleton_app =
      InstallPlatformApp("app_view/shim/skeleton");
  TestHelper("testAppViewEmbedSelfShouldFail", "app_view/shim",
             skeleton_app->id(), NO_TEST_SERVER);
}

IN_PROC_BROWSER_TEST_P(AppViewTest, TestCloseWithPendingEmbedRequestDeny) {
  const extensions::Extension* skeleton_app =
      InstallPlatformApp("app_view/shim/skeleton");
  TestHelper("testCloseWithPendingEmbedRequest", "app_view/shim",
             skeleton_app->id(), NO_TEST_SERVER);
  CloseAppWindow();
  ContinueEmbedding(skeleton_app, false);
}

IN_PROC_BROWSER_TEST_P(AppViewTest, TestCloseWithPendingEmbedRequestAllow) {
  const extensions::Extension* skeleton_app =
      InstallPlatformApp("app_view/shim/skeleton");
  TestHelper("testCloseWithPendingEmbedRequest", "app_view/shim",
             skeleton_app->id(), NO_TEST_SERVER);
  CloseAppWindow();
  ContinueEmbedding(skeleton_app, true);
}

IN_PROC_BROWSER_TEST_P(AppViewTest, KillGuestWithInvalidInstanceID) {
  const extensions::Extension* bad_app =
      LoadAndLaunchPlatformApp("app_view/bad_app", "AppViewTest.LAUNCHED");

  content::RenderProcessHost* bad_app_render_process_host =
      extensions::AppWindowRegistry::Get(browser()->profile())
          ->GetCurrentAppWindowForApp(bad_app->id())
          ->web_contents()
          ->GetPrimaryMainFrame()
          ->GetProcess();

  // Monitor |bad_app|'s RenderProcessHost for its exiting.
  content::RenderProcessHostWatcher exit_observer(
      bad_app_render_process_host,
      content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);

  // Choosing a |guest_instance_id| which does not exist.
  int invalid_guest_instance_id =
      test_guest_view_manager()->GetNextInstanceID();
  // Call the desired function to verify that the |bad_app| gets killed if
  // the provided |guest_instance_id| is not mapped to any "GuestView"'s.
  extensions::AppViewGuest::CompletePendingRequest(
      browser()->profile(), GURL("about:blank"), invalid_guest_instance_id,
      bad_app->id(), bad_app_render_process_host);
  exit_observer.Wait();
  EXPECT_FALSE(exit_observer.did_exit_normally());
}

// TODO(crbug.com/40169473): this is flaky on wayland-ozone.
#if BUILDFLAG(IS_OZONE)
#define MAYBE_KillGuestCommunicatingWithWrongAppView \
  DISABLED_KillGuestCommunicatingWithWrongAppView
#else
#define MAYBE_KillGuestCommunicatingWithWrongAppView \
  KillGuestCommunicatingWithWrongAppView
#endif
IN_PROC_BROWSER_TEST_P(AppViewTest,
                       MAYBE_KillGuestCommunicatingWithWrongAppView) {
  const extensions::Extension* host_app =
      LoadAndLaunchPlatformApp("app_view/host_app", "AppViewTest.LAUNCHED");
  const extensions::Extension* guest_app =
      InstallPlatformApp("app_view/guest_app");
  const extensions::Extension* bad_app =
      LoadAndLaunchPlatformApp("app_view/bad_app", "AppViewTest.LAUNCHED");
  // The host app attemps to embed the guest
  EXPECT_TRUE(
      content::ExecJs(extensions::AppWindowRegistry::Get(browser()->profile())
                          ->GetCurrentAppWindowForApp(host_app->id())
                          ->web_contents(),
                      base::StringPrintf("onAppCommand('%s', '%s');", "EMBED",
                                         guest_app->id().c_str())));
  ExtensionTestMessageListener on_embed_requested_listener(
      "AppViewTest.EmbedRequested");
  EXPECT_TRUE(on_embed_requested_listener.WaitUntilSatisfied());
  // While the host is waiting for the guest to accept/deny embedding, the bad
  // app sends a request to the host.
  int guest_instance_id =
      extensions::AppViewGuest::GetAllRegisteredInstanceIdsForTesting()[0];
  content::RenderProcessHostWatcher bad_app_obs(
      extensions::ProcessManager::Get(browser()->profile())
          ->GetBackgroundHostForExtension(bad_app->id())
          ->render_process_host(),
      content::RenderProcessHostWatcher::WATCH_FOR_PROCESS_EXIT);
  base::Value::Dict fake_embed_request_param;
  fake_embed_request_param.Set(appview::kGuestInstanceID, guest_instance_id);
  fake_embed_request_param.Set(appview::kEmbedderID, host_app->id());
  extensions::AppRuntimeEventRouter::DispatchOnEmbedRequestedEvent(
      browser()->profile(), std::move(fake_embed_request_param), bad_app);
  bad_app_obs.Wait();
  EXPECT_FALSE(bad_app_obs.did_exit_normally());
  // Now ask the guest to continue embedding.
  ContinueEmbedding(guest_app, true);
}

// Load an AppView which loads a WebView with a text field. The embedding app
// calls `focus()` on the AppView. The AppView calls `focus()` on the WebView.
// This should be enough to focus the content of the WebView without further
// user input like needing to click on the WebView.
IN_PROC_BROWSER_TEST_P(AppViewTest, FocusWebViewInAppView) {
  // TODO(crbug.com/40202416): Fix focus under MPArch.
  if (base::FeatureList::IsEnabled(features::kGuestViewMPArch)) {
    GTEST_SKIP() << "MPArch implementation skipped. https://crbug.com/40202416";
  }

  const extensions::Extension* skeleton_app =
      InstallPlatformApp("app_view/shim/skeleton");
  TestHelper("testFocusWebViewInAppView", "app_view/shim", skeleton_app->id(),
             NO_TEST_SERVER);

  extensions::WebViewGuest* webview_guest = nullptr;
  content::WebContents* embedder_web_contents = GetFirstAppWindowWebContents();
  ASSERT_TRUE(base::test::RunUntil([&]() {
    content::RenderFrameHost* focused =
        embedder_web_contents->GetFocusedFrame();
    webview_guest = extensions::WebViewGuest::FromRenderFrameHost(focused);
    return !!webview_guest;
  })) << "Timeout waiting for webview focus";

  content::SimulateKeyPress(embedder_web_contents,
                            ui::DomKey::FromCharacter('F'), ui::DomCode::US_F,
                            ui::VKEY_F, false, false, false, false);
  EXPECT_TRUE(
      content::ExecJs(webview_guest->GetGuestMainFrame(), "waitForInput();"));
}

IN_PROC_BROWSER_TEST_P(AppViewTest, TestAppViewCannotOpenNewWindow) {
  // TODO(crbug.com/40202416): Fix new window blocking under MPArch.
  if (base::FeatureList::IsEnabled(features::kGuestViewMPArch)) {
    GTEST_SKIP() << "MPArch implementation skipped. https://crbug.com/40202416";
  }

  const extensions::Extension* skeleton_app =
      InstallPlatformApp("app_view/shim/skeleton");
  TestHelper("testBasicConnect", "app_view/shim", skeleton_app->id(),
             NO_TEST_SERVER);

  auto* guest = test_guest_view_manager()->WaitForSingleGuestViewCreated();
  test_guest_view_manager()->WaitUntilAttached(guest);
  content::WaitForLoadStop(guest->web_contents());
  auto* guest_rfh = guest->GetGuestMainFrame();

  // Try to open new windows. Since it's inside an appview, nothing should
  // happen.
  EXPECT_TRUE(content::ExecJs(guest_rfh, "window.open('', 'attempt1');"));
  // Also test with noopener, since the browser side code paths have substantial
  // differences between the opener and noopener cases.
  EXPECT_TRUE(
      content::ExecJs(guest_rfh, "window.open('', 'attempt2', 'noopener');"));

  // Verify that the window open attempts didn't create additional guests.
  EXPECT_EQ(1u, test_guest_view_manager()->num_guests_created());
}