Chromium Code Reviews| Index: chrome/browser/previews/previews_infobar_tab_helper_unittest.cc |
| diff --git a/chrome/browser/previews/previews_infobar_tab_helper_unittest.cc b/chrome/browser/previews/previews_infobar_tab_helper_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f62ee7a27ec627a8425a6971bc7713ce211fc071 |
| --- /dev/null |
| +++ b/chrome/browser/previews/previews_infobar_tab_helper_unittest.cc |
| @@ -0,0 +1,103 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/previews/previews_infobar_tab_helper.h" |
| + |
| +#include "base/strings/stringprintf.h" |
| +#include "chrome/browser/infobars/infobar_service.h" |
| +#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" |
| +#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h" |
| +#include "chrome/browser/previews/previews_infobar_tab_helper.h" |
| +#include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| +#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h" |
| +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_headers.h" |
| +#include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h" |
| +#include "components/prefs/pref_registry_simple.h" |
| +#include "components/proxy_config/proxy_config_pref_names.h" |
| +#include "content/public/browser/navigation_handle.h" |
| +#include "content/public/test/web_contents_tester.h" |
| +#include "net/http/http_util.h" |
| + |
| +namespace { |
| +const char kTestUrl[] = "http://www.test.com/"; |
| +} |
| + |
| +class PreviewsInfoBarTabHelperUnitTest |
| + : public ChromeRenderViewHostTestHarness { |
| + protected: |
| + void SetUp() override { |
| + ChromeRenderViewHostTestHarness::SetUp(); |
| + InfoBarService::CreateForWebContents(web_contents()); |
| + PreviewsInfoBarTabHelper::CreateForWebContents(web_contents()); |
| + test_handle_ = content::NavigationHandle::CreateNavigationHandleForTesting( |
| + GURL(kTestUrl), main_rfh()); |
| + |
| + drp_test_context_ = |
| + data_reduction_proxy::DataReductionProxyTestContext::Builder() |
| + .WithMockConfig() |
| + .SkipSettingsInitialization() |
| + .Build(); |
| + |
| + auto* data_reduction_proxy_settings = |
| + DataReductionProxyChromeSettingsFactory::GetForBrowserContext( |
| + web_contents()->GetBrowserContext()); |
| + |
| + PrefRegistrySimple* registry = |
| + drp_test_context_->pref_service()->registry(); |
| + registry->RegisterDictionaryPref(proxy_config::prefs::kProxy); |
| + data_reduction_proxy_settings |
| + ->set_data_reduction_proxy_enabled_pref_name_for_test( |
| + drp_test_context_->GetDataReductionProxyEnabledPrefName()); |
| + data_reduction_proxy_settings->InitDataReductionProxySettings( |
| + drp_test_context_->io_data(), drp_test_context_->pref_service(), |
| + drp_test_context_->request_context_getter(), |
| + base::WrapUnique(new data_reduction_proxy::DataStore()), |
| + base::ThreadTaskRunnerHandle::Get(), |
| + base::ThreadTaskRunnerHandle::Get()); |
| + } |
| + |
| + void TearDown() override { |
| + drp_test_context_->DestroySettings(); |
| + ChromeRenderViewHostTestHarness::TearDown(); |
| + } |
| + |
| + void SimulateWillProcessResponse() { |
| + std::string headers = base::StringPrintf( |
|
bengr
2016/09/13 23:18:18
#include <string>
megjablon
2016/09/13 23:27:12
Done.
|
| + "HTTP/1.1 200 OK\n%s: %s\n\n", |
| + data_reduction_proxy::chrome_proxy_header(), |
| + data_reduction_proxy::chrome_proxy_lo_fi_preview_directive()); |
| + test_handle_->CallWillProcessResponseForTesting( |
| + main_rfh(), |
| + net::HttpUtil::AssembleRawHeaders(headers.c_str(), headers.size())); |
| + test_handle_->CallDidCommitNavigationForTesting(GURL(kTestUrl)); |
| + } |
| + |
| + void CallDidFinishNavigation() { |
| + test_handle_.reset(); |
| + } |
| + |
| + private: |
| + std::unique_ptr<content::NavigationHandle> test_handle_; |
|
bengr
2016/09/13 23:18:18
#include <memory>
megjablon
2016/09/13 23:27:12
Done.
|
| + std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext> |
| + drp_test_context_; |
| +}; |
| + |
| +TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateLitePageInfoBar) { |
| + PreviewsInfoBarTabHelper* infobar_tab_helper = |
| + PreviewsInfoBarTabHelper::FromWebContents(web_contents()); |
| + EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); |
| + |
| + SimulateWillProcessResponse(); |
| + CallDidFinishNavigation(); |
| + |
| + InfoBarService* infobar_service = |
| + InfoBarService::FromWebContents(web_contents()); |
| + EXPECT_EQ(1U, infobar_service->infobar_count()); |
| + EXPECT_TRUE(infobar_tab_helper->displayed_preview_infobar()); |
| + |
| + // Navigate to reset the displayed state. |
| + content::WebContentsTester::For(web_contents())-> |
| + NavigateAndCommit(GURL(kTestUrl)); |
| + EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); |
| +} |