File: link_capturing_feature_test_support.h

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

#ifndef CHROME_BROWSER_APPS_LINK_CAPTURING_LINK_CAPTURING_FEATURE_TEST_SUPPORT_H_
#define CHROME_BROWSER_APPS_LINK_CAPTURING_LINK_CAPTURING_FEATURE_TEST_SUPPORT_H_

#include <optional>
#include <string>
#include <vector>

#include "base/test/scoped_feature_list.h"
#include "base/types/expected.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/webapps/common/web_app_id.h"

class Profile;
namespace content {
class DOMMessageQueue;
class NavigationHandle;
class WebContents;
}  // namespace content

namespace testing {
class AssertionResult;
}

namespace apps::test {

// The valid link capturing configurations that can be enabled. ChromeOS does
// not support default-on.
enum class LinkCapturingFeatureVersion {
  // TODO(https://crbug.com/377522792): Remove v1 values on non-ChromeOS
  kV1DefaultOff,
  kV2DefaultOff,
  kV2DefaultOffCaptureExistingFrames,
#if !BUILDFLAG(IS_CHROMEOS)
  kV2DefaultOn,
#endif
};

// Returns if links that target existing frames (e.g. "_self", "_top",
// "namedFrame" where the frame exists, etc) should capture into an app.
bool ShouldLinksWithExistingFrameTargetsCapture(
    LinkCapturingFeatureVersion version);
bool IsV1(LinkCapturingFeatureVersion version);
bool IsV2(LinkCapturingFeatureVersion version);

std::string ToString(LinkCapturingFeatureVersion version);

std::string LinkCapturingVersionToString(
    const testing::TestParamInfo<LinkCapturingFeatureVersion>& version);

// Used from tests to enable link capturing on all platforms, taking into
// account per platform behavior.
std::vector<base::test::FeatureRefAndParams> GetFeaturesToEnableLinkCapturingUX(
    LinkCapturingFeatureVersion version);

// Enables link capturing as if the user did it from the app settings page.
// Returns the error description if there was an error.
base::expected<void, std::string> EnableLinkCapturingByUser(
    Profile* profile,
    const webapps::AppId& app_id);

// Disables link capturing as if the user did it from the app settings page.
// Returns the error description if there was an error.
base::expected<void, std::string> DisableLinkCapturingByUser(
    Profile* profile,
    const webapps::AppId& app_id);

// Observer which waits for navigation events and blocks until a specific URL
// navigation is complete.
class NavigationCommittedForUrlObserver
    : public ui_test_utils::AllTabsObserver {
 public:
  // `url` is the URL to look for.
  explicit NavigationCommittedForUrlObserver(const GURL& url);
  ~NavigationCommittedForUrlObserver() override;

  // Returns the WebContents which navigated to `url`.
  content::WebContents* web_contents() const { return web_contents_; }

 protected:
  // AllTabsObserver
  std::unique_ptr<base::CheckedObserver> ProcessOneContents(
      content::WebContents* web_contents) override;

  void DidFinishNavigation(content::NavigationHandle* handle);

 private:
  friend class LoadStopObserver;

  GURL url_;
  raw_ptr<content::WebContents> web_contents_ = nullptr;
};

// Flush the `WebAppLaunchQueue` instance for every browser tabs.
void FlushLaunchQueuesForAllBrowserTabs();

// Intended to be used with test sites in
// chrome/test/data/banners/link_capturing.
//
// Calls `resolveLaunchParamsFlush()` on any web contents where that function is
// globally defined. This is used in `WaitForNavigationFinishedMessage` below
// (which is recommended for most tests), but defined publicly for Kombucha
// tests which have custom waiting for dom messages.
base::expected<void, std::vector<std::string>>
ResolveWebContentsWaitingForLaunchQueueFlush();

// Intended to be used with test sites in
// chrome/test/data/banners/link_capturing.
//
// Waits for "PleaseFlushLaunchQueue" and "FinishedNavigating" messages, exiting
// after receiving the latter. When a "PleaseFlushLaunchQueue" message is
// received, this will call `FlushLaunchQueuesForAllBrowserTabs()` above and
// then `ResolveWebContentsWaitingForLaunchQueueFlush()` to allow the
// participating tab to then proceed to emit the "FinishedNavigating" message,
// allowing this function to exit.
testing::AssertionResult WaitForNavigationFinishedMessage(
    content::DOMMessageQueue& message_queue);

// Looks for the existence of `params_variable_name` in `contents` assuming that
// urls are stored in it, and returns them.
// One of the use-cases this is used more consistently is by returning the urls
// enqueued in the launch queue of the site currently loaded in `contents`
// inside `params_variable_name`. This will CHECK-fail if `params_variable_name`
// exists but it doesn't store a list or the list doesn't contain any strings.
std::vector<GURL> GetLaunchParamUrlsInContents(
    content::WebContents* contents,
    const std::string& params_variable_name);

}  // namespace apps::test

#endif  // CHROME_BROWSER_APPS_LINK_CAPTURING_LINK_CAPTURING_FEATURE_TEST_SUPPORT_H_