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_tab_helper.h" |
| 6 |
| 7 #include "base/memory/ref_counted.h" |
| 8 #include "chrome/browser/infobars/infobar_service.h" |
| 9 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" |
| 10 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_fact
ory.h" |
| 11 #include "chrome/browser/previews/previews_infobar_tab_helper.h" |
| 12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 13 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test
_utils.h" |
| 14 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_heade
rs.h" |
| 15 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_
names.h" |
| 16 #include "components/prefs/pref_registry_simple.h" |
| 17 #include "components/proxy_config/proxy_config_pref_names.h" |
| 18 #include "content/public/browser/navigation_handle.h" |
| 19 #include "content/public/test/web_contents_tester.h" |
| 20 #include "net/http/http_response_headers.h" |
| 21 |
| 22 namespace { |
| 23 |
| 24 const char kTestUrl[] = "http://www.test.com/"; |
| 25 |
| 26 } |
| 27 |
| 28 class PreviewsInfoBarTabHelperUnitTest |
| 29 : public ChromeRenderViewHostTestHarness { |
| 30 protected: |
| 31 void SetUp() override { |
| 32 ChromeRenderViewHostTestHarness::SetUp(); |
| 33 InfoBarService::CreateForWebContents(web_contents()); |
| 34 PreviewsInfoBarTabHelper::CreateForWebContents(web_contents()); |
| 35 test_handle_ = content::NavigationHandle::CreateNavigationHandleForTesting( |
| 36 GURL(kTestUrl), main_rfh()); |
| 37 |
| 38 drp_test_context_ = |
| 39 data_reduction_proxy::DataReductionProxyTestContext::Builder() |
| 40 .WithMockConfig() |
| 41 .SkipSettingsInitialization() |
| 42 .Build(); |
| 43 |
| 44 auto* data_reduction_proxy_settings = |
| 45 DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
| 46 web_contents()->GetBrowserContext()); |
| 47 |
| 48 PrefRegistrySimple* registry = |
| 49 drp_test_context_->pref_service()->registry(); |
| 50 registry->RegisterDictionaryPref(proxy_config::prefs::kProxy); |
| 51 data_reduction_proxy_settings |
| 52 ->set_data_reduction_proxy_enabled_pref_name_for_test( |
| 53 drp_test_context_->GetDataReductionProxyEnabledPrefName()); |
| 54 data_reduction_proxy_settings->InitDataReductionProxySettings( |
| 55 drp_test_context_->io_data(), drp_test_context_->pref_service(), |
| 56 drp_test_context_->request_context_getter(), |
| 57 base::WrapUnique(new data_reduction_proxy::DataStore()), |
| 58 base::ThreadTaskRunnerHandle::Get(), |
| 59 base::ThreadTaskRunnerHandle::Get()); |
| 60 } |
| 61 |
| 62 void TearDown() override { |
| 63 drp_test_context_->DestroySettings(); |
| 64 ChromeRenderViewHostTestHarness::TearDown(); |
| 65 } |
| 66 |
| 67 void SimulateWillProcessResponse() { |
| 68 scoped_refptr<net::HttpResponseHeaders> headers = |
| 69 new net::HttpResponseHeaders(""); |
| 70 headers->AddHeader( |
| 71 std::string(data_reduction_proxy::chrome_proxy_header()) + ": " + |
| 72 data_reduction_proxy::chrome_proxy_lo_fi_preview_directive()); |
| 73 test_handle_->CallWillProcessResponseForTesting(main_rfh(), headers); |
| 74 test_handle_->CallDidCommitNavigationForTesting( |
| 75 GURL(kTestUrl), ui::PAGE_TRANSITION_TYPED, false, main_rfh()); |
| 76 } |
| 77 |
| 78 void CallDidFinishNavigation() { |
| 79 test_handle_.reset(); |
| 80 } |
| 81 |
| 82 private: |
| 83 std::unique_ptr<content::NavigationHandle> test_handle_; |
| 84 std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext> |
| 85 drp_test_context_; |
| 86 }; |
| 87 |
| 88 TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateLitePageInfoBar) { |
| 89 PreviewsInfoBarTabHelper* infobar_tab_helper = |
| 90 PreviewsInfoBarTabHelper::FromWebContents(web_contents()); |
| 91 EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); |
| 92 |
| 93 SimulateWillProcessResponse(); |
| 94 CallDidFinishNavigation(); |
| 95 |
| 96 InfoBarService* infobar_service = |
| 97 InfoBarService::FromWebContents(web_contents()); |
| 98 EXPECT_EQ(1U, infobar_service->infobar_count()); |
| 99 EXPECT_TRUE(infobar_tab_helper->displayed_preview_infobar()); |
| 100 |
| 101 // Navigate to reset the displayed state. |
| 102 content::WebContentsTester::For(web_contents())-> |
| 103 NavigateAndCommit(GURL(kTestUrl)); |
| 104 EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); |
| 105 } |
OLD | NEW |