Commit c38aa26f authored by megjablon's avatar megjablon Committed by Commit bot

Previews infobar tests

Unit tests for both the PreviewsInfoBarDelegate and
PreviewsInfoBarTabHelper. Verfies that the infobar shows
with the correct strings and only shows once for a page.

BUG=615566
CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.linux:linux_site_isolation

Review-Url: https://codereview.chromium.org/2266653002
Cr-Commit-Position: refs/heads/master@{#418617}
parent 8e715596
......@@ -29,28 +29,18 @@ const char kUMAPreviewsInfoBarActionOffline[] =
const char kUMAPreviewsInfoBarActionLitePage[] =
"Previews.InfoBarAction.LitePage";
// Actions on the previews infobar. This enum must remain synchronized with the
// enum of the same name in metrics/histograms/histograms.xml.
enum PreviewsInfoBarAction {
INFOBAR_SHOWN = 0,
INFOBAR_LOAD_ORIGINAL_CLICKED = 1,
INFOBAR_DISMISSED_BY_USER = 2,
INFOBAR_DISMISSED_BY_NAVIGATION = 3,
INFOBAR_INDEX_BOUNDARY
};
void RecordPreviewsInfoBarAction(
PreviewsInfoBarDelegate::PreviewsInfoBarType infobar_type,
PreviewsInfoBarAction action) {
PreviewsInfoBarDelegate::PreviewsInfoBarAction action) {
if (infobar_type == PreviewsInfoBarDelegate::LOFI) {
UMA_HISTOGRAM_ENUMERATION(kUMAPreviewsInfoBarActionLoFi, action,
INFOBAR_INDEX_BOUNDARY);
PreviewsInfoBarDelegate::INFOBAR_INDEX_BOUNDARY);
} else if (infobar_type == PreviewsInfoBarDelegate::LITE_PAGE) {
UMA_HISTOGRAM_ENUMERATION(kUMAPreviewsInfoBarActionLitePage, action,
INFOBAR_INDEX_BOUNDARY);
PreviewsInfoBarDelegate::INFOBAR_INDEX_BOUNDARY);
} else if (infobar_type == PreviewsInfoBarDelegate::OFFLINE) {
UMA_HISTOGRAM_ENUMERATION(kUMAPreviewsInfoBarActionOffline, action,
INFOBAR_INDEX_BOUNDARY);
PreviewsInfoBarDelegate::INFOBAR_INDEX_BOUNDARY);
}
}
......
......@@ -25,6 +25,16 @@ class PreviewsInfoBarDelegate : public ConfirmInfoBarDelegate {
OFFLINE, // Offline copy of the page.
};
// Actions on the previews infobar. This enum must remain synchronized with
// the enum of the same name in metrics/histograms/histograms.xml.
enum PreviewsInfoBarAction {
INFOBAR_SHOWN = 0,
INFOBAR_LOAD_ORIGINAL_CLICKED = 1,
INFOBAR_DISMISSED_BY_USER = 2,
INFOBAR_DISMISSED_BY_NAVIGATION = 3,
INFOBAR_INDEX_BOUNDARY
};
~PreviewsInfoBarDelegate() override;
// Creates a preview infobar and corresponding delegate and adds the infobar
......
// Copyright 2016 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 "chrome/browser/previews/previews_infobar_delegate.h"
#include <memory>
#include "base/test/histogram_tester.h"
#include "chrome/browser/android/android_theme_resources.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h"
#include "chrome/browser/previews/previews_infobar_tab_helper.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"
#include "components/infobars/core/confirm_infobar_delegate.h"
#include "components/infobars/core/infobar.h"
#include "components/infobars/core/infobar_delegate.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/proxy_config/proxy_config_pref_names.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/web_contents_tester.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/window_open_disposition.h"
namespace {
const char kTestUrl[] = "http://www.test.com/";
// Key of the UMA Previews.InfoBarAction.LoFi histogram.
const char kUMAPreviewsInfoBarActionLoFi[] = "Previews.InfoBarAction.LoFi";
// Key of the UMA Previews.InfoBarAction.Offline histogram.
const char kUMAPreviewsInfoBarActionOffline[] =
"Previews.InfoBarAction.Offline";
// Key of the UMA Previews.InfoBarAction.LitePage histogram.
const char kUMAPreviewsInfoBarActionLitePage[] =
"Previews.InfoBarAction.LitePage";
} // namespace
class PreviewsInfoBarDelegateUnitTest : public ChromeRenderViewHostTestHarness {
protected:
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
InfoBarService::CreateForWebContents(web_contents());
PreviewsInfoBarTabHelper::CreateForWebContents(web_contents());
drp_test_context_ =
data_reduction_proxy::DataReductionProxyTestContext::Builder()
.WithMockConfig()
.SkipSettingsInitialization()
.Build();
auto* data_reduction_proxy_settings =
DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
web_contents()->GetBrowserContext());
PrefRegistrySimple* registry =
drp_test_context_->pref_service()->registry();
registry->RegisterDictionaryPref(proxy_config::prefs::kProxy);
data_reduction_proxy_settings
->set_data_reduction_proxy_enabled_pref_name_for_test(
drp_test_context_->GetDataReductionProxyEnabledPrefName());
data_reduction_proxy_settings->InitDataReductionProxySettings(
drp_test_context_->io_data(), drp_test_context_->pref_service(),
drp_test_context_->request_context_getter(),
base::WrapUnique(new data_reduction_proxy::DataStore()),
base::ThreadTaskRunnerHandle::Get(),
base::ThreadTaskRunnerHandle::Get());
}
void TearDown() override {
drp_test_context_->DestroySettings();
ChromeRenderViewHostTestHarness::TearDown();
}
ConfirmInfoBarDelegate* CreateInfoBar(
PreviewsInfoBarDelegate::PreviewsInfoBarType type) {
PreviewsInfoBarDelegate::Create(web_contents(), type);
InfoBarService* infobar_service =
InfoBarService::FromWebContents(web_contents());
EXPECT_EQ(1U, infobar_service->infobar_count());
return infobar_service->infobar_at(0)
->delegate()
->AsConfirmInfoBarDelegate();
}
InfoBarService* infobar_service() {
return InfoBarService::FromWebContents(web_contents());
}
std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext>
drp_test_context_;
};
TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestNavigationDismissal) {
base::HistogramTester tester;
CreateInfoBar(PreviewsInfoBarDelegate::LOFI);
// Try showing a second infobar. Another should not be shown since the page
// has not navigated.
PreviewsInfoBarDelegate::Create(web_contents(),
PreviewsInfoBarDelegate::LOFI);
EXPECT_EQ(1U, infobar_service()->infobar_count());
// Navigate and make sure the infobar is dismissed.
content::WebContentsTester::For(web_contents())
->NavigateAndCommit(GURL(kTestUrl));
EXPECT_EQ(0U, infobar_service()->infobar_count());
tester.ExpectBucketCount(
kUMAPreviewsInfoBarActionLoFi,
PreviewsInfoBarDelegate::INFOBAR_DISMISSED_BY_NAVIGATION, 1);
EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger(
data_reduction_proxy::prefs::kLoFiLoadImagesPerSession));
}
TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestUserDismissal) {
base::HistogramTester tester;
ConfirmInfoBarDelegate* infobar =
CreateInfoBar(PreviewsInfoBarDelegate::LOFI);
// Simulate dismissing the infobar.
infobar->InfoBarDismissed();
infobar_service()->infobar_at(0)->RemoveSelf();
EXPECT_EQ(0U, infobar_service()->infobar_count());
tester.ExpectBucketCount(kUMAPreviewsInfoBarActionLoFi,
PreviewsInfoBarDelegate::INFOBAR_DISMISSED_BY_USER,
1);
EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger(
data_reduction_proxy::prefs::kLoFiLoadImagesPerSession));
}
TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestClickLink) {
base::HistogramTester tester;
ConfirmInfoBarDelegate* infobar =
CreateInfoBar(PreviewsInfoBarDelegate::LOFI);
// Simulate clicking the infobar link.
if (infobar->LinkClicked(WindowOpenDisposition::CURRENT_TAB))
infobar_service()->infobar_at(0)->RemoveSelf();
EXPECT_EQ(0U, infobar_service()->infobar_count());
tester.ExpectBucketCount(
kUMAPreviewsInfoBarActionLoFi,
PreviewsInfoBarDelegate::INFOBAR_LOAD_ORIGINAL_CLICKED, 1);
EXPECT_EQ(1, drp_test_context_->pref_service()->GetInteger(
data_reduction_proxy::prefs::kLoFiLoadImagesPerSession));
}
TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestShownOncePerNavigation) {
ConfirmInfoBarDelegate* infobar =
CreateInfoBar(PreviewsInfoBarDelegate::LOFI);
// Simulate dismissing the infobar.
infobar->InfoBarDismissed();
infobar_service()->infobar_at(0)->RemoveSelf();
EXPECT_EQ(0U, infobar_service()->infobar_count());
PreviewsInfoBarDelegate::Create(web_contents(),
PreviewsInfoBarDelegate::LOFI);
// Infobar should not be shown again since a navigation hasn't happened.
EXPECT_EQ(0U, infobar_service()->infobar_count());
// Navigate and show infobar again.
content::WebContentsTester::For(web_contents())
->NavigateAndCommit(GURL(kTestUrl));
CreateInfoBar(PreviewsInfoBarDelegate::LOFI);
}
TEST_F(PreviewsInfoBarDelegateUnitTest, LoFiInfobarTest) {
base::HistogramTester tester;
ConfirmInfoBarDelegate* infobar =
CreateInfoBar(PreviewsInfoBarDelegate::LOFI);
tester.ExpectUniqueSample(kUMAPreviewsInfoBarActionLoFi,
PreviewsInfoBarDelegate::INFOBAR_SHOWN, 1);
EXPECT_EQ(1, drp_test_context_->pref_service()->GetInteger(
data_reduction_proxy::prefs::kLoFiUIShownPerSession));
ASSERT_TRUE(infobar);
ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE),
infobar->GetMessageText());
ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_LINK),
infobar->GetLinkText());
#if defined(OS_ANDROID)
ASSERT_EQ(IDR_ANDROID_INFOBAR_PREVIEWS, infobar->GetIconId());
#else
ASSERT_EQ(PreviewsInfoBarDelegate::kNoIconID, infobar->GetIconId());
#endif
}
TEST_F(PreviewsInfoBarDelegateUnitTest, PreviewInfobarTest) {
base::HistogramTester tester;
ConfirmInfoBarDelegate* infobar =
CreateInfoBar(PreviewsInfoBarDelegate::LITE_PAGE);
tester.ExpectUniqueSample(kUMAPreviewsInfoBarActionLitePage,
PreviewsInfoBarDelegate::INFOBAR_SHOWN, 1);
EXPECT_EQ(1, drp_test_context_->pref_service()->GetInteger(
data_reduction_proxy::prefs::kLoFiUIShownPerSession));
// Check the strings.
ASSERT_TRUE(infobar);
ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE),
infobar->GetMessageText());
ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_LINK),
infobar->GetLinkText());
#if defined(OS_ANDROID)
ASSERT_EQ(IDR_ANDROID_INFOBAR_PREVIEWS, infobar->GetIconId());
#else
ASSERT_EQ(PreviewsInfoBarDelegate::kNoIconID, infobar->GetIconId());
#endif
}
TEST_F(PreviewsInfoBarDelegateUnitTest, OfflineInfobarTest) {
base::HistogramTester tester;
ConfirmInfoBarDelegate* infobar =
CreateInfoBar(PreviewsInfoBarDelegate::OFFLINE);
tester.ExpectUniqueSample(kUMAPreviewsInfoBarActionOffline,
PreviewsInfoBarDelegate::INFOBAR_SHOWN, 1);
EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger(
data_reduction_proxy::prefs::kLoFiUIShownPerSession));
// Check the strings.
ASSERT_TRUE(infobar);
ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_FASTER_PAGE_TITLE),
infobar->GetMessageText());
ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_LINK),
infobar->GetLinkText());
#if defined(OS_ANDROID)
ASSERT_EQ(IDR_ANDROID_INFOBAR_PREVIEWS, infobar->GetIconId());
#else
ASSERT_EQ(PreviewsInfoBarDelegate::kNoIconID, infobar->GetIconId());
#endif
}
......@@ -37,6 +37,7 @@ class PreviewsInfoBarTabHelper
private:
friend class content::WebContentsUserData<PreviewsInfoBarTabHelper>;
friend class PreviewsInfoBarTabHelperUnitTest;
explicit PreviewsInfoBarTabHelper(content::WebContents* web_contents);
......
// Copyright 2016 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 "chrome/browser/previews/previews_infobar_tab_helper.h"
#include <memory>
#include <string>
#include "base/strings/stringprintf.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h"
#include "chrome/browser/previews/previews_infobar_tab_helper.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/proxy_config/proxy_config_pref_names.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/test/web_contents_tester.h"
#include "net/http/http_util.h"
namespace {
const char kTestUrl[] = "http://www.test.com/";
}
class PreviewsInfoBarTabHelperUnitTest
: public ChromeRenderViewHostTestHarness {
protected:
void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp();
InfoBarService::CreateForWebContents(web_contents());
PreviewsInfoBarTabHelper::CreateForWebContents(web_contents());
test_handle_ = content::NavigationHandle::CreateNavigationHandleForTesting(
GURL(kTestUrl), main_rfh());
drp_test_context_ =
data_reduction_proxy::DataReductionProxyTestContext::Builder()
.WithMockConfig()
.SkipSettingsInitialization()
.Build();
auto* data_reduction_proxy_settings =
DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
web_contents()->GetBrowserContext());
PrefRegistrySimple* registry =
drp_test_context_->pref_service()->registry();
registry->RegisterDictionaryPref(proxy_config::prefs::kProxy);
data_reduction_proxy_settings
->set_data_reduction_proxy_enabled_pref_name_for_test(
drp_test_context_->GetDataReductionProxyEnabledPrefName());
data_reduction_proxy_settings->InitDataReductionProxySettings(
drp_test_context_->io_data(), drp_test_context_->pref_service(),
drp_test_context_->request_context_getter(),
base::WrapUnique(new data_reduction_proxy::DataStore()),
base::ThreadTaskRunnerHandle::Get(),
base::ThreadTaskRunnerHandle::Get());
}
void TearDown() override {
drp_test_context_->DestroySettings();
ChromeRenderViewHostTestHarness::TearDown();
}
void SimulateWillProcessResponse() {
std::string headers = base::StringPrintf(
"HTTP/1.1 200 OK\n%s: %s\n\n",
data_reduction_proxy::chrome_proxy_header(),
data_reduction_proxy::chrome_proxy_lo_fi_preview_directive());
test_handle_->CallWillProcessResponseForTesting(
main_rfh(),
net::HttpUtil::AssembleRawHeaders(headers.c_str(), headers.size()));
test_handle_->CallDidCommitNavigationForTesting(GURL(kTestUrl));
}
void CallDidFinishNavigation() { test_handle_.reset(); }
private:
std::unique_ptr<content::NavigationHandle> test_handle_;
std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext>
drp_test_context_;
};
TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateLitePageInfoBar) {
PreviewsInfoBarTabHelper* infobar_tab_helper =
PreviewsInfoBarTabHelper::FromWebContents(web_contents());
EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar());
SimulateWillProcessResponse();
CallDidFinishNavigation();
InfoBarService* infobar_service =
InfoBarService::FromWebContents(web_contents());
EXPECT_EQ(1U, infobar_service->infobar_count());
EXPECT_TRUE(infobar_tab_helper->displayed_preview_infobar());
// Navigate to reset the displayed state.
content::WebContentsTester::For(web_contents())
->NavigateAndCommit(GURL(kTestUrl));
EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar());
}
......@@ -3174,6 +3174,8 @@ test("unit_tests") {
"../browser/prerender/prerender_resource_throttle_unittest.cc",
"../browser/prerender/prerender_unittest.cc",
"../browser/prerender/prerender_util_unittest.cc",
"../browser/previews/previews_infobar_delegate_unittest.cc",
"../browser/previews/previews_infobar_tab_helper_unittest.cc",
"../browser/process_singleton_win_unittest.cc",
"../browser/profiles/gaia_info_update_service_unittest.cc",
"../browser/profiles/incognito_mode_policy_handler_unittest.cc",
......
......@@ -5,6 +5,7 @@
#include "components/subresource_filter/content/browser/subresource_filter_navigation_throttle.h"
#include <memory>
#include <string>
#include "base/macros.h"
#include "base/memory/ptr_util.h"
......@@ -113,7 +114,7 @@ class SubresourceFilterNavigationThrottleTest
}
void SimulateWillProcessResponse() {
handle()->CallWillProcessResponseForTesting(main_rfh());
handle()->CallWillProcessResponseForTesting(main_rfh(), std::string());
}
private:
......
......@@ -305,10 +305,13 @@ NavigationHandleImpl::CallWillRedirectRequestForTesting(
NavigationThrottle::ThrottleCheckResult
NavigationHandleImpl::CallWillProcessResponseForTesting(
content::RenderFrameHost* render_frame_host) {
content::RenderFrameHost* render_frame_host,
const std::string& raw_response_headers) {
scoped_refptr<net::HttpResponseHeaders> headers =
new net::HttpResponseHeaders(raw_response_headers);
NavigationThrottle::ThrottleCheckResult result = NavigationThrottle::DEFER;
WillProcessResponse(static_cast<RenderFrameHostImpl*>(render_frame_host),
scoped_refptr<net::HttpResponseHeaders>(), SSLStatus(),
headers, SSLStatus(),
base::Bind(&UpdateThrottleCheckResult, &result));
// Reset the callback to ensure it will not be called later.
......@@ -316,6 +319,28 @@ NavigationHandleImpl::CallWillProcessResponseForTesting(
return result;
}
void NavigationHandleImpl::CallDidCommitNavigationForTesting(const GURL& url) {
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.page_id = 1;
params.nav_entry_id = 1;
params.url = url;
params.referrer = content::Referrer();
params.transition = ui::PAGE_TRANSITION_TYPED;
params.redirects = std::vector<GURL>();
params.should_update_history = false;
params.searchable_form_url = GURL();
params.searchable_form_encoding = std::string();
params.did_create_new_entry = false;
params.gesture = NavigationGestureUser;
params.was_within_same_page = false;
params.method = "GET";
params.page_state = PageState::CreateFromURL(url);
params.contents_mime_type = std::string("text/html");
DidCommitNavigation(params, false, render_frame_host_);
}
NavigationData* NavigationHandleImpl::GetNavigationData() {
return navigation_data_.get();
}
......
......@@ -8,6 +8,7 @@
#include "content/public/browser/navigation_handle.h"
#include <stddef.h>
#include <string>
#include "base/callback.h"
#include "base/macros.h"
......@@ -117,7 +118,9 @@ class CONTENT_EXPORT NavigationHandleImpl : public NavigationHandle {
const GURL& new_referrer_url,
bool new_is_external_protocol) override;
NavigationThrottle::ThrottleCheckResult CallWillProcessResponseForTesting(
RenderFrameHost* render_frame_host) override;
RenderFrameHost* render_frame_host,
const std::string& raw_response_header) override;
void CallDidCommitNavigationForTesting(const GURL& url) override;
NavigationData* GetNavigationData() override;
......
......@@ -204,7 +204,12 @@ class CONTENT_EXPORT NavigationHandle {
// Simulates the reception of the network response.
virtual NavigationThrottle::ThrottleCheckResult
CallWillProcessResponseForTesting(RenderFrameHost* render_frame_host) = 0;
CallWillProcessResponseForTesting(
RenderFrameHost* render_frame_host,
const std::string& raw_response_headers) = 0;
// Simulates the navigation being committed.
virtual void CallDidCommitNavigationForTesting(const GURL& url) = 0;
// The NavigationData that the embedder returned from
// ResourceDispatcherHostDelegate::GetNavigationData during commit. This will
......
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