| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/previews/previews_infobar_delegate.h" | 5 #include "chrome/browser/previews/previews_infobar_delegate.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/optional.h" | 13 #include "base/optional.h" |
| 14 #include "base/test/histogram_tester.h" | 14 #include "base/test/histogram_tester.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "chrome/browser/android/android_theme_resources.h" | 16 #include "chrome/browser/android/android_theme_resources.h" |
| 17 #include "chrome/browser/infobars/infobar_service.h" | 17 #include "chrome/browser/infobars/infobar_service.h" |
| 18 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" | 18 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" |
| 19 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_fact
ory.h" | 19 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_fact
ory.h" |
| 20 #include "chrome/browser/previews/previews_infobar_tab_helper.h" | 20 #include "chrome/browser/previews/previews_infobar_tab_helper.h" |
| 21 #include "chrome/grit/generated_resources.h" | 21 #include "chrome/grit/generated_resources.h" |
| 22 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 22 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 23 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test
_utils.h" | 23 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test
_utils.h" |
| 24 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_
names.h" | 24 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_
names.h" |
| 25 #include "components/infobars/core/confirm_infobar_delegate.h" | 25 #include "components/infobars/core/confirm_infobar_delegate.h" |
| 26 #include "components/infobars/core/infobar.h" | 26 #include "components/infobars/core/infobar.h" |
| 27 #include "components/infobars/core/infobar_delegate.h" | 27 #include "components/infobars/core/infobar_delegate.h" |
| 28 #include "components/prefs/pref_registry_simple.h" | 28 #include "components/prefs/pref_registry_simple.h" |
| 29 #include "components/proxy_config/proxy_config_pref_names.h" | 29 #include "components/proxy_config/proxy_config_pref_names.h" |
| 30 #include "content/public/browser/navigation_entry.h" |
| 31 #include "content/public/browser/navigation_handle.h" |
| 30 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
| 33 #include "content/public/browser/web_contents_observer.h" |
| 34 #include "content/public/browser/web_contents_user_data.h" |
| 31 #include "content/public/common/referrer.h" | 35 #include "content/public/common/referrer.h" |
| 36 #include "content/public/test/test_renderer_host.h" |
| 32 #include "content/public/test/web_contents_tester.h" | 37 #include "content/public/test/web_contents_tester.h" |
| 33 #include "ui/base/l10n/l10n_util.h" | 38 #include "ui/base/l10n/l10n_util.h" |
| 34 #include "ui/base/page_transition_types.h" | 39 #include "ui/base/page_transition_types.h" |
| 35 #include "ui/base/window_open_disposition.h" | 40 #include "ui/base/window_open_disposition.h" |
| 36 | 41 |
| 37 namespace { | 42 namespace { |
| 38 | 43 |
| 39 const char kTestUrl[] = "http://www.test.com/"; | 44 const char kTestUrl[] = "http://www.test.com/"; |
| 40 | 45 |
| 41 // Key of the UMA Previews.InfoBarAction.LoFi histogram. | 46 // Key of the UMA Previews.InfoBarAction.LoFi histogram. |
| 42 const char kUMAPreviewsInfoBarActionLoFi[] = "Previews.InfoBarAction.LoFi"; | 47 const char kUMAPreviewsInfoBarActionLoFi[] = "Previews.InfoBarAction.LoFi"; |
| 43 | 48 |
| 44 // Key of the UMA Previews.InfoBarAction.Offline histogram. | 49 // Key of the UMA Previews.InfoBarAction.Offline histogram. |
| 45 const char kUMAPreviewsInfoBarActionOffline[] = | 50 const char kUMAPreviewsInfoBarActionOffline[] = |
| 46 "Previews.InfoBarAction.Offline"; | 51 "Previews.InfoBarAction.Offline"; |
| 47 | 52 |
| 48 // Key of the UMA Previews.InfoBarAction.LitePage histogram. | 53 // Key of the UMA Previews.InfoBarAction.LitePage histogram. |
| 49 const char kUMAPreviewsInfoBarActionLitePage[] = | 54 const char kUMAPreviewsInfoBarActionLitePage[] = |
| 50 "Previews.InfoBarAction.LitePage"; | 55 "Previews.InfoBarAction.LitePage"; |
| 51 | 56 |
| 57 class TestPreviewsWebContentsObserver |
| 58 : public content::WebContentsObserver, |
| 59 public content::WebContentsUserData<TestPreviewsWebContentsObserver> { |
| 60 public: |
| 61 explicit TestPreviewsWebContentsObserver(content::WebContents* web_contents) |
| 62 : content::WebContentsObserver(web_contents), |
| 63 last_navigation_reload_type_(content::ReloadType::NONE) {} |
| 64 ~TestPreviewsWebContentsObserver() override {} |
| 65 |
| 66 content::ReloadType last_navigation_reload_type() { |
| 67 return last_navigation_reload_type_; |
| 68 } |
| 69 |
| 70 void DidFinishNavigation( |
| 71 content::NavigationHandle* navigation_handle) override { |
| 72 last_navigation_reload_type_ = navigation_handle->GetReloadType(); |
| 73 } |
| 74 |
| 75 private: |
| 76 content::ReloadType last_navigation_reload_type_; |
| 77 }; |
| 78 |
| 52 } // namespace | 79 } // namespace |
| 53 | 80 |
| 81 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TestPreviewsWebContentsObserver); |
| 82 |
| 54 class PreviewsInfoBarDelegateUnitTest : public ChromeRenderViewHostTestHarness { | 83 class PreviewsInfoBarDelegateUnitTest : public ChromeRenderViewHostTestHarness { |
| 55 protected: | 84 protected: |
| 56 void SetUp() override { | 85 void SetUp() override { |
| 57 ChromeRenderViewHostTestHarness::SetUp(); | 86 ChromeRenderViewHostTestHarness::SetUp(); |
| 58 InfoBarService::CreateForWebContents(web_contents()); | 87 InfoBarService::CreateForWebContents(web_contents()); |
| 59 PreviewsInfoBarTabHelper::CreateForWebContents(web_contents()); | 88 PreviewsInfoBarTabHelper::CreateForWebContents(web_contents()); |
| 89 TestPreviewsWebContentsObserver::CreateForWebContents(web_contents()); |
| 60 | 90 |
| 61 drp_test_context_ = | 91 drp_test_context_ = |
| 62 data_reduction_proxy::DataReductionProxyTestContext::Builder() | 92 data_reduction_proxy::DataReductionProxyTestContext::Builder() |
| 63 .WithMockConfig() | 93 .WithMockConfig() |
| 64 .SkipSettingsInitialization() | 94 .SkipSettingsInitialization() |
| 65 .Build(); | 95 .Build(); |
| 66 | 96 |
| 67 auto* data_reduction_proxy_settings = | 97 auto* data_reduction_proxy_settings = |
| 68 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( | 98 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
| 69 web_contents()->GetBrowserContext()); | 99 web_contents()->GetBrowserContext()); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 | 155 |
| 126 // Try showing a second infobar. Another should not be shown since the page | 156 // Try showing a second infobar. Another should not be shown since the page |
| 127 // has not navigated. | 157 // has not navigated. |
| 128 PreviewsInfoBarDelegate::Create( | 158 PreviewsInfoBarDelegate::Create( |
| 129 web_contents(), PreviewsInfoBarDelegate::LOFI, | 159 web_contents(), PreviewsInfoBarDelegate::LOFI, |
| 130 true /* is_data_saver_user */, | 160 true /* is_data_saver_user */, |
| 131 PreviewsInfoBarDelegate::OnDismissPreviewsInfobarCallback()); | 161 PreviewsInfoBarDelegate::OnDismissPreviewsInfobarCallback()); |
| 132 EXPECT_EQ(1U, infobar_service()->infobar_count()); | 162 EXPECT_EQ(1U, infobar_service()->infobar_count()); |
| 133 | 163 |
| 134 // Navigate and make sure the infobar is dismissed. | 164 // Navigate and make sure the infobar is dismissed. |
| 135 content::WebContentsTester::For(web_contents()) | 165 NavigateAndCommit(GURL(kTestUrl)); |
| 136 ->NavigateAndCommit(GURL(kTestUrl)); | |
| 137 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 166 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 138 EXPECT_FALSE(user_opt_out_.value()); | 167 EXPECT_FALSE(user_opt_out_.value()); |
| 139 | 168 |
| 140 tester.ExpectBucketCount( | 169 tester.ExpectBucketCount( |
| 141 kUMAPreviewsInfoBarActionLoFi, | 170 kUMAPreviewsInfoBarActionLoFi, |
| 142 PreviewsInfoBarDelegate::INFOBAR_DISMISSED_BY_NAVIGATION, 1); | 171 PreviewsInfoBarDelegate::INFOBAR_DISMISSED_BY_NAVIGATION, 1); |
| 143 EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger( | 172 EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger( |
| 144 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); | 173 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); |
| 145 } | 174 } |
| 146 | 175 |
| 147 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestReloadDismissal) { | 176 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestReloadDismissal) { |
| 148 base::HistogramTester tester; | 177 base::HistogramTester tester; |
| 149 | 178 |
| 150 // Navigate to test URL, so we can reload later. | 179 // Navigate to test URL, so we can reload later. |
| 151 content::WebContentsTester::For(web_contents()) | 180 NavigateAndCommit(GURL(kTestUrl)); |
| 152 ->NavigateAndCommit(GURL(kTestUrl)); | |
| 153 | 181 |
| 154 CreateInfoBar(PreviewsInfoBarDelegate::LOFI, true /* is_data_saver_user */); | 182 CreateInfoBar(PreviewsInfoBarDelegate::LOFI, true /* is_data_saver_user */); |
| 155 | 183 |
| 156 // Try showing a second infobar. Another should not be shown since the page | 184 // Try showing a second infobar. Another should not be shown since the page |
| 157 // has not navigated. | 185 // has not navigated. |
| 158 PreviewsInfoBarDelegate::Create( | 186 PreviewsInfoBarDelegate::Create( |
| 159 web_contents(), PreviewsInfoBarDelegate::LOFI, | 187 web_contents(), PreviewsInfoBarDelegate::LOFI, |
| 160 true /* is_data_saver_user */, | 188 true /* is_data_saver_user */, |
| 161 PreviewsInfoBarDelegate::OnDismissPreviewsInfobarCallback()); | 189 PreviewsInfoBarDelegate::OnDismissPreviewsInfobarCallback()); |
| 162 EXPECT_EQ(1U, infobar_service()->infobar_count()); | 190 EXPECT_EQ(1U, infobar_service()->infobar_count()); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 188 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 216 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 189 | 217 |
| 190 tester.ExpectBucketCount(kUMAPreviewsInfoBarActionLoFi, | 218 tester.ExpectBucketCount(kUMAPreviewsInfoBarActionLoFi, |
| 191 PreviewsInfoBarDelegate::INFOBAR_DISMISSED_BY_USER, | 219 PreviewsInfoBarDelegate::INFOBAR_DISMISSED_BY_USER, |
| 192 1); | 220 1); |
| 193 EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger( | 221 EXPECT_EQ(0, drp_test_context_->pref_service()->GetInteger( |
| 194 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); | 222 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); |
| 195 EXPECT_FALSE(user_opt_out_.value()); | 223 EXPECT_FALSE(user_opt_out_.value()); |
| 196 } | 224 } |
| 197 | 225 |
| 198 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestClickLink) { | 226 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestClickLinkLoFi) { |
| 199 base::HistogramTester tester; | 227 base::HistogramTester tester; |
| 200 | 228 |
| 201 ConfirmInfoBarDelegate* infobar = CreateInfoBar( | 229 ConfirmInfoBarDelegate* infobar = CreateInfoBar( |
| 202 PreviewsInfoBarDelegate::LOFI, true /* is_data_saver_user */); | 230 PreviewsInfoBarDelegate::LOFI, true /* is_data_saver_user */); |
| 203 | 231 |
| 204 // Simulate clicking the infobar link. | 232 // Simulate clicking the infobar link. |
| 205 if (infobar->LinkClicked(WindowOpenDisposition::CURRENT_TAB)) | 233 if (infobar->LinkClicked(WindowOpenDisposition::CURRENT_TAB)) |
| 206 infobar_service()->infobar_at(0)->RemoveSelf(); | 234 infobar_service()->infobar_at(0)->RemoveSelf(); |
| 207 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 235 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 208 | 236 |
| 209 tester.ExpectBucketCount( | 237 tester.ExpectBucketCount( |
| 210 kUMAPreviewsInfoBarActionLoFi, | 238 kUMAPreviewsInfoBarActionLoFi, |
| 211 PreviewsInfoBarDelegate::INFOBAR_LOAD_ORIGINAL_CLICKED, 1); | 239 PreviewsInfoBarDelegate::INFOBAR_LOAD_ORIGINAL_CLICKED, 1); |
| 212 EXPECT_EQ(1, drp_test_context_->pref_service()->GetInteger( | 240 EXPECT_EQ(1, drp_test_context_->pref_service()->GetInteger( |
| 213 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); | 241 data_reduction_proxy::prefs::kLoFiLoadImagesPerSession)); |
| 214 EXPECT_TRUE(user_opt_out_.value()); | 242 EXPECT_TRUE(user_opt_out_.value()); |
| 215 } | 243 } |
| 216 | 244 |
| 245 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestClickLinkLitePage) { |
| 246 base::HistogramTester tester; |
| 247 |
| 248 NavigateAndCommit(GURL(kTestUrl)); |
| 249 |
| 250 ConfirmInfoBarDelegate* infobar = CreateInfoBar( |
| 251 PreviewsInfoBarDelegate::LITE_PAGE, true /* is_data_saver_user */); |
| 252 |
| 253 // Simulate clicking the infobar link. |
| 254 if (infobar->LinkClicked(WindowOpenDisposition::CURRENT_TAB)) |
| 255 infobar_service()->infobar_at(0)->RemoveSelf(); |
| 256 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 257 |
| 258 tester.ExpectBucketCount( |
| 259 kUMAPreviewsInfoBarActionLitePage, |
| 260 PreviewsInfoBarDelegate::INFOBAR_LOAD_ORIGINAL_CLICKED, 1); |
| 261 |
| 262 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 263 |
| 264 EXPECT_EQ(content::ReloadType::DISABLE_LOFI_MODE, |
| 265 TestPreviewsWebContentsObserver::FromWebContents(web_contents()) |
| 266 ->last_navigation_reload_type()); |
| 267 } |
| 268 |
| 217 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestShownOncePerNavigation) { | 269 TEST_F(PreviewsInfoBarDelegateUnitTest, InfobarTestShownOncePerNavigation) { |
| 218 ConfirmInfoBarDelegate* infobar = CreateInfoBar( | 270 ConfirmInfoBarDelegate* infobar = CreateInfoBar( |
| 219 PreviewsInfoBarDelegate::LOFI, true /* is_data_saver_user */); | 271 PreviewsInfoBarDelegate::LOFI, true /* is_data_saver_user */); |
| 220 | 272 |
| 221 // Simulate dismissing the infobar. | 273 // Simulate dismissing the infobar. |
| 222 infobar->InfoBarDismissed(); | 274 infobar->InfoBarDismissed(); |
| 223 infobar_service()->infobar_at(0)->RemoveSelf(); | 275 infobar_service()->infobar_at(0)->RemoveSelf(); |
| 224 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 276 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 225 | 277 |
| 226 PreviewsInfoBarDelegate::Create( | 278 PreviewsInfoBarDelegate::Create( |
| 227 web_contents(), PreviewsInfoBarDelegate::LOFI, | 279 web_contents(), PreviewsInfoBarDelegate::LOFI, |
| 228 true /* is_data_saver_user */, | 280 true /* is_data_saver_user */, |
| 229 PreviewsInfoBarDelegate::OnDismissPreviewsInfobarCallback()); | 281 PreviewsInfoBarDelegate::OnDismissPreviewsInfobarCallback()); |
| 230 | 282 |
| 231 // Infobar should not be shown again since a navigation hasn't happened. | 283 // Infobar should not be shown again since a navigation hasn't happened. |
| 232 EXPECT_EQ(0U, infobar_service()->infobar_count()); | 284 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 233 | 285 |
| 234 // Navigate and show infobar again. | 286 // Navigate and show infobar again. |
| 235 content::WebContentsTester::For(web_contents()) | 287 NavigateAndCommit(GURL(kTestUrl)); |
| 236 ->NavigateAndCommit(GURL(kTestUrl)); | |
| 237 CreateInfoBar(PreviewsInfoBarDelegate::LOFI, true /* is_data_saver_user */); | 288 CreateInfoBar(PreviewsInfoBarDelegate::LOFI, true /* is_data_saver_user */); |
| 238 } | 289 } |
| 239 | 290 |
| 240 TEST_F(PreviewsInfoBarDelegateUnitTest, LoFiInfobarTest) { | 291 TEST_F(PreviewsInfoBarDelegateUnitTest, LoFiInfobarTest) { |
| 241 base::HistogramTester tester; | 292 base::HistogramTester tester; |
| 242 | 293 |
| 243 ConfirmInfoBarDelegate* infobar = CreateInfoBar( | 294 ConfirmInfoBarDelegate* infobar = CreateInfoBar( |
| 244 PreviewsInfoBarDelegate::LOFI, true /* is_data_saver_user */); | 295 PreviewsInfoBarDelegate::LOFI, true /* is_data_saver_user */); |
| 245 | 296 |
| 246 tester.ExpectUniqueSample(kUMAPreviewsInfoBarActionLoFi, | 297 tester.ExpectUniqueSample(kUMAPreviewsInfoBarActionLoFi, |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE), | 375 ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_SAVED_DATA_TITLE), |
| 325 infobar->GetMessageText()); | 376 infobar->GetMessageText()); |
| 326 ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_LINK), | 377 ASSERT_EQ(l10n_util::GetStringUTF16(IDS_PREVIEWS_INFOBAR_LINK), |
| 327 infobar->GetLinkText()); | 378 infobar->GetLinkText()); |
| 328 #if defined(OS_ANDROID) | 379 #if defined(OS_ANDROID) |
| 329 ASSERT_EQ(IDR_ANDROID_INFOBAR_PREVIEWS, infobar->GetIconId()); | 380 ASSERT_EQ(IDR_ANDROID_INFOBAR_PREVIEWS, infobar->GetIconId()); |
| 330 #else | 381 #else |
| 331 ASSERT_EQ(PreviewsInfoBarDelegate::kNoIconID, infobar->GetIconId()); | 382 ASSERT_EQ(PreviewsInfoBarDelegate::kNoIconID, infobar->GetIconId()); |
| 332 #endif | 383 #endif |
| 333 } | 384 } |
| 385 |
| 386 TEST_F(PreviewsInfoBarDelegateUnitTest, OfflineInfobarDisablesLoFi) { |
| 387 base::HistogramTester tester; |
| 388 |
| 389 NavigateAndCommit(GURL(kTestUrl)); |
| 390 |
| 391 ConfirmInfoBarDelegate* infobar = CreateInfoBar( |
| 392 PreviewsInfoBarDelegate::OFFLINE, true /* is_data_saver_user */); |
| 393 |
| 394 tester.ExpectUniqueSample(kUMAPreviewsInfoBarActionOffline, |
| 395 PreviewsInfoBarDelegate::INFOBAR_SHOWN, 1); |
| 396 |
| 397 // Simulate clicking the infobar link. |
| 398 if (infobar->LinkClicked(WindowOpenDisposition::CURRENT_TAB)) |
| 399 infobar_service()->infobar_at(0)->RemoveSelf(); |
| 400 EXPECT_EQ(0U, infobar_service()->infobar_count()); |
| 401 |
| 402 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 403 |
| 404 EXPECT_EQ(content::ReloadType::DISABLE_LOFI_MODE, |
| 405 TestPreviewsWebContentsObserver::FromWebContents(web_contents()) |
| 406 ->last_navigation_reload_type()); |
| 407 } |
| OLD | NEW |