Commit e22bc30d authored by Becca Hughes's avatar Becca Hughes Committed by Commit Bot

Add browser test for KaleidoscopeTabHelper

Change-Id: I64be99576fe6557dd9adc7c987c3c113254be6fc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2544076Reviewed-by: default avatarTommy Steimel <steimel@chromium.org>
Commit-Queue: Becca Hughes <beccahughes@chromium.org>
Cr-Commit-Position: refs/heads/master@{#828354}
parent 6f2e5a25
......@@ -27,16 +27,6 @@ const url::Origin& KaleidoscopeUntrustedOrigin() {
return *origin;
}
const char kKaleidoscopeNavigationHistogramName[] =
"Media.Kaleidoscope.Navigation";
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class KaleidoscopeNavigation {
kNormal = 0,
kMaxValue = kNormal,
};
bool IsOpenedFromKaleidoscope(content::NavigationHandle* handle) {
return (handle->GetInitiatorOrigin() &&
handle->GetInitiatorOrigin()->IsSameOriginWith(
......@@ -60,6 +50,9 @@ bool ShouldAllowAutoplay(content::NavigationHandle* handle) {
} // namespace
const char KaleidoscopeTabHelper::kKaleidoscopeNavigationHistogramName[] =
"Media.Kaleidoscope.Navigation";
KaleidoscopeTabHelper::KaleidoscopeTabHelper(content::WebContents* web_contents)
: content::WebContentsObserver(web_contents) {}
......@@ -70,15 +63,12 @@ void KaleidoscopeTabHelper::ReadyToCommitNavigation(
if (handle->IsSameDocument() || handle->IsErrorPage())
return;
if (!ShouldAllowAutoplay(handle))
return;
mojo::AssociatedRemote<blink::mojom::AutoplayConfigurationClient> client;
handle->GetRenderFrameHost()->GetRemoteAssociatedInterfaces()->GetInterface(
&client);
client->AddAutoplayFlags(url::Origin::Create(handle->GetURL()),
blink::mojom::kAutoplayFlagUserException);
RecordMetricsOnNavigation(handle);
SetAutoplayOnNavigation(handle);
}
void KaleidoscopeTabHelper::RecordMetricsOnNavigation(
content::NavigationHandle* handle) {
// Only record metrics if this page was opened by Kaleidoscope.
if (IsOpenedFromKaleidoscope(handle)) {
base::UmaHistogramEnumeration(kKaleidoscopeNavigationHistogramName,
......@@ -95,4 +85,16 @@ void KaleidoscopeTabHelper::ReadyToCommitNavigation(
}
}
void KaleidoscopeTabHelper::SetAutoplayOnNavigation(
content::NavigationHandle* handle) {
if (!ShouldAllowAutoplay(handle))
return;
mojo::AssociatedRemote<blink::mojom::AutoplayConfigurationClient> client;
handle->GetRenderFrameHost()->GetRemoteAssociatedInterfaces()->GetInterface(
&client);
client->AddAutoplayFlags(url::Origin::Create(handle->GetURL()),
blink::mojom::kAutoplayFlagUserException);
}
WEB_CONTENTS_USER_DATA_KEY_IMPL(KaleidoscopeTabHelper)
......@@ -12,6 +12,15 @@ class KaleidoscopeTabHelper
: public content::WebContentsObserver,
public content::WebContentsUserData<KaleidoscopeTabHelper> {
public:
static const char kKaleidoscopeNavigationHistogramName[];
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class KaleidoscopeNavigation {
kNormal = 0,
kMaxValue = kNormal,
};
~KaleidoscopeTabHelper() override;
KaleidoscopeTabHelper(const KaleidoscopeTabHelper&) = delete;
KaleidoscopeTabHelper& operator=(const KaleidoscopeTabHelper&) = delete;
......@@ -24,6 +33,9 @@ class KaleidoscopeTabHelper
explicit KaleidoscopeTabHelper(content::WebContents* web_contents);
void RecordMetricsOnNavigation(content::NavigationHandle* handle);
void SetAutoplayOnNavigation(content::NavigationHandle* handle);
WEB_CONTENTS_USER_DATA_KEY_DECL();
};
......
// Copyright 2020 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 "base/test/metrics/histogram_tester.h"
#include "chrome/browser/media/kaleidoscope/constants.h"
#include "chrome/browser/media/kaleidoscope/kaleidoscope_tab_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "net/dns/mock_host_resolver.h"
#include "services/metrics/public/cpp/ukm_builders.h"
namespace {
static constexpr char const kTestPagePath[] = "/media/unified_autoplay.html";
} // anonymous namespace
class KaleidoscopeTabHelperBrowserTest : public InProcessBrowserTest {
public:
KaleidoscopeTabHelperBrowserTest() = default;
~KaleidoscopeTabHelperBrowserTest() override = default;
void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(embedded_test_server()->Start());
InProcessBrowserTest::SetUpOnMainThread();
}
bool AttemptPlay(content::WebContents* web_contents) {
bool played = false;
EXPECT_TRUE(content::ExecuteScriptWithoutUserGestureAndExtractBool(
web_contents, "attemptPlay();", &played));
return played;
}
bool NavigateInRenderer(content::WebContents* web_contents, const GURL& url) {
content::TestNavigationObserver observer(web_contents);
bool result = content::ExecuteScriptWithoutUserGesture(
web_contents, "window.location = '" + url.spec() + "';");
if (result)
observer.Wait();
return result;
}
content::WebContents* GetWebContents() {
return browser()->tab_strip_model()->GetActiveWebContents();
}
base::HistogramTester histogram_tester_;
};
IN_PROC_BROWSER_TEST_F(KaleidoscopeTabHelperBrowserTest,
NotOpenedFromKaleidoscope) {
const GURL kTestPageUrl = embedded_test_server()->GetURL(kTestPagePath);
ukm::TestAutoSetUkmRecorder test_ukm_recorder;
NavigateParams params(browser(), kTestPageUrl, ui::PAGE_TRANSITION_LINK);
ui_test_utils::NavigateToURL(&params);
// Autoplay should not be allowed since that is the default.
EXPECT_FALSE(AttemptPlay(GetWebContents()));
histogram_tester_.ExpectTotalCount(
KaleidoscopeTabHelper::kKaleidoscopeNavigationHistogramName, 0);
auto ukm_entries = test_ukm_recorder.GetEntriesByName(
ukm::builders::Media_Kaleidoscope_Navigation::kEntryName);
ASSERT_EQ(0u, ukm_entries.size());
}
IN_PROC_BROWSER_TEST_F(KaleidoscopeTabHelperBrowserTest,
OpenedFromKaleidoscope) {
const GURL kTestPageUrl = embedded_test_server()->GetURL(kTestPagePath);
ukm::TestAutoSetUkmRecorder test_ukm_recorder;
NavigateParams params(browser(), kTestPageUrl, ui::PAGE_TRANSITION_LINK);
params.initiator_origin =
url::Origin::Create(GURL(kKaleidoscopeUntrustedContentUIURL));
ui_test_utils::NavigateToURL(&params);
// Autoplay should be allowed because this page was opened from Kaleidoscope.
EXPECT_TRUE(AttemptPlay(GetWebContents()));
// Autoplay should not be allowed since this is a derived navigation.
NavigateInRenderer(GetWebContents(), kTestPageUrl);
EXPECT_FALSE(AttemptPlay(GetWebContents()));
histogram_tester_.ExpectBucketCount(
KaleidoscopeTabHelper::kKaleidoscopeNavigationHistogramName,
KaleidoscopeTabHelper::KaleidoscopeNavigation::kNormal, 1);
auto ukm_entries = test_ukm_recorder.GetEntriesByName(
ukm::builders::Media_Kaleidoscope_Navigation::kEntryName);
ASSERT_EQ(1u, ukm_entries.size());
auto* ukm_entry = ukm_entries.back();
test_ukm_recorder.ExpectEntrySourceHasUrl(ukm_entry, kTestPageUrl);
}
......@@ -1071,6 +1071,7 @@ if (!is_android) {
"../browser/media/encrypted_media_supported_types_browsertest.cc",
"../browser/media/feeds/media_feeds_browsertest.cc",
"../browser/media/history/media_history_browsertest.cc",
"../browser/media/kaleidoscope/kaleidoscope_tab_helper_browsertest.cc",
"../browser/media/media_browsertest.cc",
"../browser/media/media_browsertest.h",
"../browser/media/media_engagement_autoplay_browsertest.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment