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
|
// 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.
#include <memory>
#include "base/files/file_path.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/scoped_logging_settings.h"
#include "chrome/browser/android/customtabs/tab_interaction_recorder_android.h"
#include "chrome/test/base/android/android_browser_test.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/content/browser/test_autofill_manager_injector.h"
#include "components/autofill/core/browser/foundations/browser_autofill_manager.h"
#include "components/autofill/core/browser/foundations/test_autofill_manager_waiter.h"
#include "components/back_forward_cache/back_forward_cache_disable.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_user_data.h"
#include "content/public/common/content_features.h"
#include "content/public/test/back_forward_cache_util.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_utils.h"
#include "net/dns/mock_host_resolver.h"
#include "url/gurl.h"
const char kHistogramBackForwardCachePageWithFormStorable[] =
"BackForwardCache.PageWithForm.Storable";
const char kHistogramBackForwardCachePageWithFormRestoreResult[] =
"BackForwardCache.PageWithForm.RestoreResult";
// Browser test that aim to test whether bits regarding tab interaction and form
// seen is correctly recorded during navigation. This test is largely
// referencing the ChromeBackForwardCacheBrowserTest.
class TabInteractionRecorderAndroidBrowserTest : public AndroidBrowserTest {
public:
void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
customtabs::TabInteractionRecorderAndroid::CreateForWebContents(
web_contents());
}
void SetUpCommandLine(base::CommandLine* command_line) override {
// For using an HTTPS server.
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kIgnoreCertificateErrors);
scoped_feature_list_.InitWithFeaturesAndParameters(
content::GetDefaultEnabledBackForwardCacheFeaturesForTesting(),
content::GetDefaultDisabledBackForwardCacheFeaturesForTesting());
// TODO(crbug.com/40285326): This fails with the field trial testing config.
command_line->AppendSwitch("disable-field-trial-config");
}
// Helper functions to verify Histogram
// BackForwardCachePageWithFormEventCounts recorded with the right samples.
// These helper are used as enum BackForwardCachePageWithFormsEvent lives in
// //content/browser and is not visible to this test.
void ExpectPageWithFormSeenRecorded(int count) {
histogram_tester_.ExpectBucketCount(
kHistogramBackForwardCachePageWithFormStorable, /*kPageSeen*/ 0, count);
}
void ExpectPageWithFormStoredRecorded(int count) {
histogram_tester_.ExpectBucketCount(
kHistogramBackForwardCachePageWithFormStorable, /*kPageStored*/ 1,
count);
}
void ExpectPageWithFormRestoredRecorded(int count) {
histogram_tester_.ExpectBucketCount(
kHistogramBackForwardCachePageWithFormRestoreResult, /*kRestored*/ 0,
count);
}
void ExpectPageWithFormNotRestoredRecorded(int count) {
histogram_tester_.ExpectBucketCount(
kHistogramBackForwardCachePageWithFormRestoreResult,
/*kNotRestored*/ 1, count);
}
protected:
class TestAutofillManager : public autofill::BrowserAutofillManager {
public:
explicit TestAutofillManager(autofill::ContentAutofillDriver* driver)
: autofill::BrowserAutofillManager(driver) {}
[[nodiscard]] testing::AssertionResult WaitForFormsSeen(
int min_num_awaited_calls) {
return forms_seen_waiter_.Wait(min_num_awaited_calls);
}
private:
autofill::TestAutofillManagerWaiter forms_seen_waiter_{
*this,
{autofill::AutofillManagerEvent::kFormsSeen}};
};
content::WebContents* web_contents() {
return chrome_test_utils::GetActiveWebContents(this);
}
content::RenderFrameHost* current_frame_host() {
return web_contents()->GetPrimaryMainFrame();
}
TestAutofillManager& GetAutofillManagerInMainFrame() {
autofill::ContentAutofillDriver* driver =
autofill::ContentAutofillDriver::GetForRenderFrameHost(
web_contents()->GetPrimaryMainFrame());
return static_cast<TestAutofillManager&>(driver->GetAutofillManager());
}
// Returns a URL with host `host` and path "/title1.html".
GURL GetURLWithHost(const std::string& host) {
return embedded_test_server()->GetURL(host, "/title1.html");
}
GURL GetTestFormUrl() {
return embedded_test_server()->GetURL("/autofill/autofill_test_form.html");
}
GURL GetTestFormUrlWithHandle(const std::string& handle) {
return embedded_test_server()->GetURL("/autofill/autofill_test_form.html#" +
handle);
}
GURL GetTestFormInSubFrameUrl() {
return embedded_test_server()->GetURL(
"/autofill/iframe_autocomplete_simple_form.html");
}
base::HistogramTester histogram_tester_;
private:
base::test::ScopedFeatureList scoped_feature_list_;
std::map<base::test::FeatureRef, std::map<std::string, std::string>>
features_with_params_;
logging::ScopedVmoduleSwitches vmodule_switches_;
autofill::TestAutofillManagerInjector<TestAutofillManager>
autofill_manager_injector_;
};
IN_PROC_BROWSER_TEST_F(TabInteractionRecorderAndroidBrowserTest,
PageSeenAndStore) {
ASSERT_TRUE(embedded_test_server()->Start());
// 1) Navigate to page with test form.
EXPECT_TRUE(content::NavigateToURL(web_contents(), GetTestFormUrl()));
EXPECT_TRUE(GetAutofillManagerInMainFrame().WaitForFormsSeen(1));
content::RenderFrameHostWrapper rfh_a(current_frame_host());
// 2) Navigate to B.
EXPECT_TRUE(content::NavigateToURL(web_contents(), GetURLWithHost("b.com")));
content::RenderFrameHostWrapper rfh_b(current_frame_host());
// A is frozen in back/forward cache, page seen and store is recorded.
EXPECT_EQ(rfh_a->GetLifecycleState(),
content::RenderFrameHost::LifecycleState::kInBackForwardCache);
content::FetchHistogramsFromChildProcesses();
ExpectPageWithFormSeenRecorded(1);
ExpectPageWithFormStoredRecorded(1);
ExpectPageWithFormRestoredRecorded(0);
ExpectPageWithFormNotRestoredRecorded(0);
// 3) Go back
web_contents()->GetController().GoBack();
EXPECT_TRUE(content::WaitForLoadStop(web_contents()));
// Page A should be restored and the histogram value is recorded.
content::FetchHistogramsFromChildProcesses();
ExpectPageWithFormRestoredRecorded(1);
ExpectPageWithFormNotRestoredRecorded(0);
}
IN_PROC_BROWSER_TEST_F(TabInteractionRecorderAndroidBrowserTest,
StoreAterSameDocumentNavigation) {
ASSERT_TRUE(embedded_test_server()->Start());
// 1) Navigate to page with test form.
EXPECT_TRUE(content::NavigateToURL(web_contents(), GetTestFormUrl()));
EXPECT_TRUE(GetAutofillManagerInMainFrame().WaitForFormsSeen(1));
content::RenderFrameHostWrapper rfh_a(current_frame_host());
// 2) Perform a JS that does a same document navigation.
EXPECT_TRUE(
content::NavigateToURL(web_contents(), GetTestFormUrlWithHandle("foo")));
EXPECT_EQ(rfh_a.get(), current_frame_host());
// Page seen is not recorded for same-document navigation.
ExpectPageWithFormSeenRecorded(0);
// 3) Navigate to B.
EXPECT_TRUE(content::NavigateToURL(web_contents(), GetURLWithHost("b.com")));
content::RenderFrameHostWrapper rfh_b(current_frame_host());
// A is frozen in back/forward cache. Page seen and page stored is recorded.
EXPECT_EQ(rfh_a->GetLifecycleState(),
content::RenderFrameHost::LifecycleState::kInBackForwardCache);
content::FetchHistogramsFromChildProcesses();
ExpectPageWithFormSeenRecorded(1);
ExpectPageWithFormStoredRecorded(1);
ExpectPageWithFormRestoredRecorded(0);
ExpectPageWithFormNotRestoredRecorded(0);
}
IN_PROC_BROWSER_TEST_F(TabInteractionRecorderAndroidBrowserTest,
DoNotRecordForPageWithoutForm) {
ASSERT_TRUE(embedded_test_server()->Start());
// 1) Navigate to A (no form attached).
EXPECT_TRUE(content::NavigateToURL(web_contents(), GetURLWithHost("a.com")));
content::RenderFrameHostWrapper rfh_a(current_frame_host());
// 3) Navigate to B.
EXPECT_TRUE(content::NavigateToURL(web_contents(), GetURLWithHost("b.com")));
content::RenderFrameHostWrapper rfh_b(current_frame_host());
// A is frozen in BackforwardCache, but nothing should be recorded since
// no page with forms attached is involved.
EXPECT_EQ(rfh_a->GetLifecycleState(),
content::RenderFrameHost::LifecycleState::kInBackForwardCache);
content::FetchHistogramsFromChildProcesses();
ExpectPageWithFormSeenRecorded(0);
ExpectPageWithFormStoredRecorded(0);
ExpectPageWithFormRestoredRecorded(0);
ExpectPageWithFormNotRestoredRecorded(0);
}
IN_PROC_BROWSER_TEST_F(TabInteractionRecorderAndroidBrowserTest,
FormInSubFrameStoresInMainFrameNavigation) {
ASSERT_TRUE(embedded_test_server()->Start());
// 1) Navigate to page with form in sub frame.
EXPECT_TRUE(
content::NavigateToURL(web_contents(), GetTestFormInSubFrameUrl()));
content::RenderFrameHostWrapper rfh_a(current_frame_host());
// 3) Navigate to B.
EXPECT_TRUE(content::NavigateToURL(web_contents(), GetURLWithHost("b.com")));
content::RenderFrameHostWrapper rfh_b(current_frame_host());
// A is frozen in back/forward cache. Page seen and page stored is recorded.
EXPECT_EQ(rfh_a->GetLifecycleState(),
content::RenderFrameHost::LifecycleState::kInBackForwardCache);
content::FetchHistogramsFromChildProcesses();
ExpectPageWithFormSeenRecorded(1);
ExpectPageWithFormStoredRecorded(1);
ExpectPageWithFormRestoredRecorded(0);
ExpectPageWithFormNotRestoredRecorded(0);
}
IN_PROC_BROWSER_TEST_F(TabInteractionRecorderAndroidBrowserTest,
PageDoNotStoreWhenCacheDisabled) {
ASSERT_TRUE(embedded_test_server()->Start());
// 1) Navigate to page with test form.
EXPECT_TRUE(content::NavigateToURL(web_contents(), GetTestFormUrl()));
EXPECT_TRUE(GetAutofillManagerInMainFrame().WaitForFormsSeen(1));
content::RenderFrameHostWrapper rfh_a(current_frame_host());
// Disable the backforward cache for rfh_a will result in failed to store.
content::BackForwardCache::DisableForRenderFrameHost(
rfh_a.get(), back_forward_cache::DisabledReason(
back_forward_cache::DisabledReasonId::kUnknown));
// 2) Navigate to B.
EXPECT_TRUE(content::NavigateToURL(web_contents(), GetURLWithHost("b.com")));
content::RenderFrameHostWrapper rfh_b(current_frame_host());
EXPECT_TRUE(rfh_a.WaitUntilRenderFrameDeleted());
// Page A should be seen as forms attached, not be stored and the histogram
// value is recorded.
content::FetchHistogramsFromChildProcesses();
ExpectPageWithFormSeenRecorded(1);
ExpectPageWithFormStoredRecorded(0);
ExpectPageWithFormRestoredRecorded(0);
ExpectPageWithFormNotRestoredRecorded(0);
}
IN_PROC_BROWSER_TEST_F(TabInteractionRecorderAndroidBrowserTest,
PageDoNotRestoreWhenCacheFlushed) {
ASSERT_TRUE(embedded_test_server()->Start());
// 1) Navigate to page with test form.
EXPECT_TRUE(content::NavigateToURL(web_contents(), GetTestFormUrl()));
EXPECT_TRUE(GetAutofillManagerInMainFrame().WaitForFormsSeen(1));
content::RenderFrameHostWrapper rfh_a(current_frame_host());
// 2) Navigate to B.
EXPECT_TRUE(content::NavigateToURL(web_contents(), GetURLWithHost("b.com")));
content::RenderFrameHostWrapper rfh_b(current_frame_host());
// Page A should be seen as forms attached, stored in cache and the histogram
// value is recorded.
EXPECT_EQ(rfh_a->GetLifecycleState(),
content::RenderFrameHost::LifecycleState::kInBackForwardCache);
content::FetchHistogramsFromChildProcesses();
ExpectPageWithFormSeenRecorded(1);
ExpectPageWithFormStoredRecorded(1);
ExpectPageWithFormRestoredRecorded(0);
ExpectPageWithFormNotRestoredRecorded(0);
web_contents()->GetController().GetBackForwardCache().Flush();
EXPECT_TRUE(rfh_a.WaitUntilRenderFrameDeleted());
// 3) Go back. Page is not restored and histogram is recorded.
web_contents()->GetController().GoBack();
EXPECT_TRUE(content::WaitForLoadStop(web_contents()));
content::FetchHistogramsFromChildProcesses();
ExpectPageWithFormSeenRecorded(1);
ExpectPageWithFormStoredRecorded(1);
ExpectPageWithFormRestoredRecorded(0);
ExpectPageWithFormNotRestoredRecorded(1);
}
|