File: permissions_api_interactive_uitest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (83 lines) | stat: -rw-r--r-- 3,655 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
// 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 "base/test/bind.h"
#include "chrome/browser/extensions/api/permissions/permissions_api.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/extensions/extension_action_test_helper.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "content/public/test/browser_test.h"
#include "extensions/test/extension_test_message_listener.h"
#include "ui/gfx/native_widget_types.h"

namespace extensions {
using PermissionsApiInteractiveTest = ExtensionApiTest;

// Tests that the dialog is parented to the correct window when there are
// multiple browser windows open. Regression test for crbug.com/41482206.
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
// BringBrowserWindowToFront hangs on Linux: http://crbug.com/356183782
#define MAYBE_DialogWithMultipleWindows DISABLED_DialogWithMultipleWindows
#else
#define MAYBE_DialogWithMultipleWindows DialogWithMultipleWindows
#endif
IN_PROC_BROWSER_TEST_F(PermissionsApiInteractiveTest,
                       MAYBE_DialogWithMultipleWindows) {
  PermissionsRequestFunction::SetIgnoreUserGestureForTests(true);
  auto dialog_action_reset =
      PermissionsRequestFunction::SetDialogActionForTests(
          PermissionsRequestFunction::DialogAction::kProgrammatic);

  const Extension* extension = LoadExtension(
      test_data_dir_.AppendASCII("permissions/optional_request_from_popup"));

  Browser* first_browser = browser();
  ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(first_browser));

  // Create a second browser window and wait for activation.
  Browser* second_browser = CreateBrowser(browser()->profile());
  ASSERT_NE(first_browser, second_browser);
  ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(second_browser));

  // Activate the first browser (again).
  ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(first_browser));

  // Simulate a click on the extension action. The extension expects back a
  // reply from the browser. On receiving the reply, it will request additional
  // permissions.
  std::unique_ptr<ExtensionActionTestHelper> action_helper =
      ExtensionActionTestHelper::Create(first_browser);
  ExtensionTestMessageListener popup_listener("popup_loaded",
                                              ReplyBehavior::kWillReply);
  action_helper->Press(extension->id());
  ASSERT_TRUE(popup_listener.WaitUntilSatisfied());

  // We must wait for the popup to be activated, before asking it to request
  // additional permissions.
  action_helper->WaitForPopup();

  // Configure a custom show dialog callback to capture the parent_window. This
  // callback is our actual test, which validates the dialog's parent window.
  base::RunLoop run_loop;
  auto show_dialog_callback = [&](gfx::NativeWindow parent_window) {
    EXPECT_EQ(parent_window, first_browser->window()->GetNativeWindow());
    run_loop.Quit();
  };

  auto bound_callback = base::BindLambdaForTesting(show_dialog_callback);
  auto show_dialog_callback_reset =
      PermissionsRequestFunction::SetShowDialogCallbackForTests(
          &bound_callback);

  // At this point, the popup is active and our test hooks are configured. Ask
  // the popup to request additional permissions and wait for our test callback.
  popup_listener.Reply("request_permissions");
  run_loop.Run();

  PermissionsRequestFunction::ResolvePendingDialogForTests(
      /*accept_dialog=*/true);
}
}  // namespace extensions