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