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