OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/previews/previews_infobar_delegate.h" |
| 6 |
| 7 #include "base/test/histogram_tester.h" |
| 8 #include "chrome/browser/android/android_theme_resources.h" |
| 9 #include "chrome/browser/infobars/infobar_service.h" |
| 10 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" |
| 11 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_fact
ory.h" |
| 12 #include "chrome/browser/previews/previews_infobar_tab_helper.h" |
| 13 #include "chrome/grit/generated_resources.h" |
| 14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 15 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test
_utils.h" |
| 16 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_
names.h" |
| 17 #include "components/infobars/core/confirm_infobar_delegate.h" |
| 18 #include "components/infobars/core/infobar.h" |
| 19 #include "components/infobars/core/infobar_delegate.h" |
| 20 #include "components/prefs/pref_registry_simple.h" |
| 21 #include "components/proxy_config/proxy_config_pref_names.h" |
| 22 #include "content/public/browser/web_contents.h" |
| 23 #include "content/public/test/web_contents_tester.h" |
| 24 #include "ui/base/l10n/l10n_util.h" |
| 25 #include "ui/base/window_open_disposition.h" |
| 26 |
| 27 namespace { |
| 28 |
| 29 const char kTestUrl[] = "http://www.test.com/"; |
| 30 |
| 31 // Key of the UMA Previews.InfoBarAction.LoFi histogram. |
| 32 const char kUMAPreviewsInfoBarActionLoFi[] = "Previews.InfoBarAction.LoFi"; |
| 33 |
| 34 // Key of the UMA Previews.InfoBarAction.Offline histogram. |
| 35 const char kUMAPreviewsInfoBarActionOffline[] = |
| 36 "Previews.InfoBarAction.Offline"; |
| 37 |
| 38 // Key of the UMA Previews.InfoBarAction.LitePage histogram. |
| 39 const char kUMAPreviewsInfoBarActionLitePage[] = |
| 40 "Previews.InfoBarAction.LitePage"; |
| 41 |
| 42 // Actions on the previews infobar. This enum must remain synchronized with the |
| 43 // enum of the same name in metrics/histograms/histograms.xml. |
| 44 enum PreviewsInfoBarAction { |
| 45 INFOBAR_SHOWN = 0, |
| 46 INFOBAR_LOAD_ORIGINAL_CLICKED = 1, |
| 47 INFOBAR_DISMISSED_BY_USER = 2, |
| 48 INFOBAR_DISMISSED_BY_NAVIGATION = 3, |
| 49 INFOBAR_INDEX_BOUNDARY |
| 50 }; |
| 51 |
| 52 } // namespace |
| 53 |
| 54 class PreviewsInfoBarDelegateUnitTest : public ChromeRenderViewHostTestHarness { |
| 55 protected: |
| 56 void SetUp() override { |
| 57 ChromeRenderViewHostTestHarness::SetUp(); |
| 58 InfoBarService::CreateForWebContents(web_contents()); |
| 59 PreviewsInfoBarTabHelper::CreateForWebContents(web_contents()); |
| 60 |
| 61 drp_test_context_ = |
| 62 data_reduction_proxy::DataReductionProxyTestContext::Builder() |
| 63 .WithMockConfig() |
| 64 .SkipSettingsInitialization() |
| 65 .Build(); |
| 66 |
| 67 auto* data_reduction_proxy_settings = |
| 68 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
| 69 web_contents()->GetBrowserContext()); |
| 70 |
| 71 PrefRegistrySimple* registry = |
| 72 drp_test_context_->pref_service()->registry(); |
| 73 registry->RegisterDictionaryPref(proxy_config::prefs::kProxy); |
| 74 data_reduction_proxy_settings |
| 75 ->set_data_reduction_proxy_enabled_pref_name_for_test( |
| 76 drp_test_context_->GetDataReductionProxyEnabledPrefName()); |
| 77 data_reduction_proxy_settings->InitDataReductionProxySettings( |
| 78 drp_test_context_->io_data(), drp_test_context_->pref_service(), |
| 79 drp_test_context_->request_context_getter(), |
| 80 base::WrapUnique(new data_reduction_proxy::DataStore()), |
| 81 base::ThreadTaskRunnerHandle::Get(), |
| 82 base::ThreadTaskRunnerHandle::Get()); |
| 83 } |
| 84 |
| 85 void TearDown() override { |
| 86 drp_test_context_->DestroySettings(); |
| 87 ChromeRenderViewHostTestHarness::TearDown(); |
| 88 } |
| 89 |
| 90 ConfirmInfoBarDelegate* CreateInfoBar( |
| 91 PreviewsInfoBarDelegate::PreviewsInfoBarType type) { |
| 92 PreviewsInfoBarDelegate::Create(web_contents(), type); |
| 93 |
| 94 InfoBarService* infobar_service = |
| 95 InfoBarService::FromWebContents(web_contents()); |
| 96 EXPECT_EQ(1U, infobar_service->infobar_count()); |
| 97 |
| 98 return infobar_service->infobar_at(0) |
| 99 ->delegate() |
| 100 ->AsConfirmInfoBarDelegate(); |
| 101 } |
| 102 |
| 103 InfoBarService* infobar_service() { |
| 104 return InfoBarService::FromWebContents(web_contents()); |
| 105 } |
| 106 |
| 107 std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext> |
| 108 drp_test_context_; |
| 109 }; |
| 110 |
| 111 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestNavigationDismissal) { |
| 112 base::HistogramTester tester; |
| 113 |
| 114 CreateInfoBar(PreviewsInfoBarDelegate::LOFI); |
| 115 |
| 116 // Try showing a second infobar. Another should not be shown since the page |
| 117 // has not navigated. |
| 118 PreviewsInfoBarDelegate::Create(web_contents(), |
| 119 PreviewsInfoBarDelegate::LOFI); |
| 120 EXPECT_EQ(1U, infobar_service()->infobar_count()); |
| 121 |
| 122 // Navigate and make sure the infobar is dismissed. |
| 123 content::WebContentsTester::For(web_contents())-> |
| 124 NavigateAndCommit(GURL(kTestUrl)); |
| 125 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 126 |
| 127 tester.ExpectBucketCount(kUMAPreviewsInfoBarActionLoFi, |
| 128 INFOBAR_DISMISSED_BY_NAVIGATION, 1); |
| 129 EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger( |
| 130 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); |
| 131 } |
| 132 |
| 133 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestUserDismissal) { |
| 134 base::HistogramTester tester; |
| 135 |
| 136 ConfirmInfoBarDelegate* infobar = |
| 137 CreateInfoBar(PreviewsInfoBarDelegate::LOFI); |
| 138 |
| 139 // Simulate dismissing the infobar. |
| 140 infobar->InfoBarDismissed(); |
| 141 infobar_service()->infobar_at(0)->RemoveSelf(); |
| 142 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 143 |
| 144 tester.ExpectBucketCount(kUMAPreviewsInfoBarActionLoFi, |
| 145 INFOBAR_DISMISSED_BY_USER, 1); |
| 146 EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger( |
| 147 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); |
| 148 } |
| 149 |
| 150 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestClickLink) { |
| 151 base::HistogramTester tester; |
| 152 |
| 153 ConfirmInfoBarDelegate* infobar = |
| 154 CreateInfoBar(PreviewsInfoBarDelegate::LOFI); |
| 155 |
| 156 // Simulate clicking the infobar link. |
| 157 if (infobar->LinkClicked(WindowOpenDisposition::CURRENT_TAB)) |
| 158 infobar_service()->infobar_at(0)->RemoveSelf(); |
| 159 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 160 |
| 161 tester.ExpectBucketCount(kUMAPreviewsInfoBarActionLoFi, |
| 162 INFOBAR_LOAD_ORIGINAL_CLICKED, 1); |
| 163 EXPECT_EQ(1, drp_test_context_->pref_service()->GetInteger( |
| 164 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); |
| 165 } |
| 166 |
| 167 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestShownOncePerNavigation) { |
| 168 ConfirmInfoBarDelegate* infobar = |
| 169 CreateInfoBar(PreviewsInfoBarDelegate::LOFI); |
| 170 |
| 171 // Simulate dismissing the infobar. |
| 172 infobar->InfoBarDismissed(); |
| 173 infobar_service()->infobar_at(0)->RemoveSelf(); |
| 174 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 175 |
| 176 PreviewsInfoBarDelegate::Create(web_contents(), |
| 177 PreviewsInfoBarDelegate::LOFI); |
| 178 |
| 179 // Infobar should not be shown again since a navigation hasn't happened. |
| 180 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 181 |
| 182 // Navigate and show infobar again. |
| 183 content::WebContentsTester::For(web_contents())-> |
| 184 NavigateAndCommit(GURL(kTestUrl)); |
| 185 CreateInfoBar(PreviewsInfoBarDelegate::LOFI); |
| 186 } |
| 187 |
| 188 TEST_F(PreviewsInfoBarDelegateUnitTest, LoFiInfobarTest) { |
| 189 base::HistogramTester tester; |
| 190 |
| 191 ConfirmInfoBarDelegate* infobar = |
| 192 CreateInfoBar(PreviewsInfoBarDelegate::LOFI); |
| 193 |
| 194 tester.ExpectUniqueSample(kUMAPreviewsInfoBarActionLoFi, INFOBAR_SHOWN, 1); |
| 195 EXPECT_EQ(1, drp_test_context_->pref_service()->GetInteger( |
| 196 data_reduction_proxy::prefs::kLoFiUIShownPerSession)); |
| 197 |
| 198 ASSERT_TRUE(infobar); |
| 199 ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE), |
| 200 infobar->GetMessageText()); |
| 201 ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_LINK), |
| 202 infobar->GetLinkText()); |
| 203 #if defined(OS_ANDROID) |
| 204 ASSERT_EQ(IDR_ANDROID_INFOBAR_PREVIEWS, infobar->GetIconId()); |
| 205 #else |
| 206 ASSERT_EQ(PreviewsInfoBarDelegate::kNoIconID, infobar->GetIconId()); |
| 207 #endif |
| 208 } |
| 209 |
| 210 TEST_F(PreviewsInfoBarDelegateUnitTest, PreviewInfobarTest) { |
| 211 base::HistogramTester tester; |
| 212 |
| 213 ConfirmInfoBarDelegate* infobar = |
| 214 CreateInfoBar(PreviewsInfoBarDelegate::LITE_PAGE); |
| 215 |
| 216 tester.ExpectUniqueSample(kUMAPreviewsInfoBarActionLitePage, INFOBAR_SHOWN, |
| 217 1); |
| 218 EXPECT_EQ(1, drp_test_context_->pref_service()->GetInteger( |
| 219 data_reduction_proxy::prefs::kLoFiUIShownPerSession)); |
| 220 |
| 221 // Check the strings. |
| 222 ASSERT_TRUE(infobar); |
| 223 ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE), |
| 224 infobar->GetMessageText()); |
| 225 ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_LINK), |
| 226 infobar->GetLinkText()); |
| 227 #if defined(OS_ANDROID) |
| 228 ASSERT_EQ(IDR_ANDROID_INFOBAR_PREVIEWS, infobar->GetIconId()); |
| 229 #else |
| 230 ASSERT_EQ(PreviewsInfoBarDelegate::kNoIconID, infobar->GetIconId()); |
| 231 #endif |
| 232 } |
| 233 |
| 234 TEST_F(PreviewsInfoBarDelegateUnitTest, OfflineInfobarTest) { |
| 235 base::HistogramTester tester; |
| 236 |
| 237 ConfirmInfoBarDelegate* infobar = |
| 238 CreateInfoBar(PreviewsInfoBarDelegate::OFFLINE); |
| 239 |
| 240 tester.ExpectUniqueSample(kUMAPreviewsInfoBarActionOffline, INFOBAR_SHOWN, 1); |
| 241 EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger( |
| 242 data_reduction_proxy::prefs::kLoFiUIShownPerSession)); |
| 243 |
| 244 // Check the strings. |
| 245 ASSERT_TRUE(infobar); |
| 246 ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_FASTER_PAGE_TITLE), |
| 247 infobar->GetMessageText()); |
| 248 ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_LINK), |
| 249 infobar->GetLinkText()); |
| 250 #if defined(OS_ANDROID) |
| 251 ASSERT_EQ(IDR_ANDROID_INFOBAR_PREVIEWS, infobar->GetIconId()); |
| 252 #else |
| 253 ASSERT_EQ(PreviewsInfoBarDelegate::kNoIconID, infobar->GetIconId()); |
| 254 #endif |
| 255 } |
OLD | NEW |