forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweblayer_browser_test_utils.cc
157 lines (129 loc) · 5.32 KB
/
weblayer_browser_test_utils.cc
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "weblayer/test/weblayer_browser_test_utils.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "build/build_config.h"
#include "components/subresource_filter/content/browser/content_subresource_filter_web_contents_helper.h"
#include "components/subresource_filter/content/browser/fake_safe_browsing_database_manager.h"
#include "url/gurl.h"
#include "weblayer/browser/browser_process.h"
#include "weblayer/browser/tab_impl.h"
#include "weblayer/public/navigation_controller.h"
#include "weblayer/public/tab.h"
#include "weblayer/shell/browser/shell.h"
#include "weblayer/test/test_navigation_observer.h"
#if BUILDFLAG(IS_ANDROID)
#include "weblayer/test/stub_autofill_provider.h"
#endif // BUILDFLAG(IS_ANDROID)
namespace weblayer {
namespace {
// Navigates to |url| in |tab| and waits for |event| to occur.
void NavigateAndWaitForEvent(const GURL& url,
Tab* tab,
TestNavigationObserver::NavigationEvent event) {
TestNavigationObserver test_observer(url, event, tab);
tab->GetNavigationController()->Navigate(url);
test_observer.Wait();
}
} // namespace
void NavigateAndWaitForCompletion(const GURL& url, Shell* shell) {
NavigateAndWaitForEvent(url, shell->tab(),
TestNavigationObserver::NavigationEvent::kCompletion);
}
void NavigateAndWaitForCompletion(const GURL& url, Tab* tab) {
NavigateAndWaitForEvent(url, tab,
TestNavigationObserver::NavigationEvent::kCompletion);
}
void NavigateAndWaitForFailure(const GURL& url, Shell* shell) {
NavigateAndWaitForEvent(url, shell->tab(),
TestNavigationObserver::NavigationEvent::kFailure);
}
void NavigateAndWaitForStart(const GURL& url, Tab* tab) {
NavigateAndWaitForEvent(url, tab,
TestNavigationObserver::NavigationEvent::kStart);
}
base::Value ExecuteScript(Shell* shell,
const std::string& script,
bool use_separate_isolate) {
return ExecuteScript(shell->tab(), script, use_separate_isolate);
}
base::Value ExecuteScript(Tab* tab,
const std::string& script,
bool use_separate_isolate) {
base::Value final_result;
base::RunLoop run_loop;
tab->ExecuteScript(base::ASCIIToUTF16(script), use_separate_isolate,
base::BindLambdaForTesting(
[&run_loop, &final_result](base::Value result) {
final_result = std::move(result);
run_loop.Quit();
}));
run_loop.Run();
return final_result;
}
void ExecuteScriptWithUserGesture(Shell* shell, const std::string& script) {
ExecuteScriptWithUserGesture(shell->tab(), script);
}
void ExecuteScriptWithUserGesture(Tab* tab, const std::string& script) {
TabImpl* tab_impl = static_cast<TabImpl*>(tab);
tab_impl->ExecuteScriptWithUserGestureForTests(base::ASCIIToUTF16(script));
}
const std::u16string& GetTitle(Shell* shell) {
TabImpl* tab_impl = static_cast<TabImpl*>(shell->tab());
return tab_impl->web_contents()->GetTitle();
}
#if BUILDFLAG(IS_ANDROID)
void InitializeAutofillWithEventForwarding(
Shell* shell,
const base::RepeatingCallback<void(const autofill::FormData&)>&
on_received_form_data) {
TabImpl* tab_impl = static_cast<TabImpl*>(shell->tab());
new StubAutofillProvider(tab_impl->web_contents(), on_received_form_data);
tab_impl->InitializeAutofillForTests();
}
#endif // BUILDFLAG(IS_ANDROID)
void ActivateSubresourceFilterInWebContentsForURL(
content::WebContents* web_contents,
const GURL& url) {
scoped_refptr<FakeSafeBrowsingDatabaseManager> database_manager =
base::MakeRefCounted<FakeSafeBrowsingDatabaseManager>();
database_manager->AddBlocklistedUrl(
url, safe_browsing::SB_THREAT_TYPE_URL_PHISHING);
subresource_filter::ContentSubresourceFilterWebContentsHelper::
FromWebContents(web_contents)
->SetDatabaseManagerForTesting(std::move(database_manager));
}
OneShotNavigationObserver::OneShotNavigationObserver(Shell* shell)
: tab_(shell->tab()) {
tab_->GetNavigationController()->AddObserver(this);
}
OneShotNavigationObserver::~OneShotNavigationObserver() {
tab_->GetNavigationController()->RemoveObserver(this);
}
void OneShotNavigationObserver::WaitForNavigation() {
run_loop_.Run();
}
void OneShotNavigationObserver::NavigationStarted(Navigation* navigation) {
is_page_initiated_ = navigation->IsPageInitiated();
}
void OneShotNavigationObserver::NavigationCompleted(Navigation* navigation) {
completed_ = true;
Finish(navigation);
}
void OneShotNavigationObserver::NavigationFailed(Navigation* navigation) {
Finish(navigation);
}
void OneShotNavigationObserver::Finish(Navigation* navigation) {
is_error_page_ = navigation->IsErrorPage();
is_download_ = navigation->IsDownload();
is_reload_ = navigation->IsReload();
was_stop_called_ = navigation->WasStopCalled();
load_error_ = navigation->GetLoadError();
http_status_code_ = navigation->GetHttpStatusCode();
navigation_state_ = navigation->GetState();
run_loop_.Quit();
}
} // namespace weblayer