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 |
| index 105a35939758c5750f8e2a98f6df93dfee362af3..f94b6a89da0b822829980055782194b569320af3 100644 |
| --- a/chrome/browser/previews/previews_infobar_tab_helper_unittest.cc |
| +++ b/chrome/browser/previews/previews_infobar_tab_helper_unittest.cc |
| @@ -1,43 +1,99 @@ |
| // 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 <memory> |
| #include <string> |
| +#include "base/files/file_path.h" |
|
megjablon
2016/10/10 22:25:13
Is this include needed? I may be missing where it'
RyanSturm
2016/10/11 02:09:56
Done.
|
| +#include "base/macros.h" |
| #include "base/strings/stringprintf.h" |
| +#include "base/time/time.h" |
| #include "chrome/browser/infobars/infobar_service.h" |
| +#include "chrome/browser/loader/chrome_navigation_data.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/offline_pages/loaded_offline_page_info.h" |
| +#include "components/offline_pages/offline_page_item.h" |
| +#include "components/offline_pages/request_header/offline_page_header.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/browser/web_contents_observer.h" |
| +#include "content/public/browser/web_contents_user_data.h" |
| #include "content/public/test/web_contents_tester.h" |
| +#include "net/base/request_priority.h" |
| #include "net/http/http_util.h" |
| +#include "net/url_request/url_request.h" |
| +#include "net/url_request/url_request_context.h" |
| namespace { |
| + |
| const char kTestUrl[] = "http://www.test.com/"; |
| -} |
| +const char kTestUrl2[] = "http://www.test2.com/"; |
| + |
| +class TestPreviewsTabHelper : public content::WebContentsObserver { |
| + public: |
| + explicit TestPreviewsTabHelper(content::WebContents* web_contents) |
| + : WebContentsObserver(web_contents), is_offline_preview_(false) {} |
| + ~TestPreviewsTabHelper() override {} |
| + |
| + void set_is_offline_preview(bool is_offline_preview) { |
| + is_offline_preview_ = is_offline_preview; |
| + } |
| + |
| + private: |
| + // Overridden from content::WebContentsObserver: |
| + void DidFinishNavigation( |
| + content::NavigationHandle* navigation_handle) override { |
| + // Modify the navigation handle to contain LoadedOfflinePageInfo that claims |
| + // to be an offline preview iff |is_offline_preview_| is true.. |
| + std::unique_ptr<net::URLRequest> request = |
| + context_.CreateRequest(GURL("a.com"), net::IDLE, nullptr); |
|
megjablon
2016/10/10 22:25:13
Can this use kTestUrl instead of "a.com"?
RyanSturm
2016/10/11 02:09:56
Done. This request is short-lived though, as it is
|
| + offline_pages::LoadedOfflinePageInfo::CreateInfoForRequest( |
| + request.get(), base::MakeUnique<offline_pages::OfflinePageItem>(), |
| + base::MakeUnique<offline_pages::OfflinePageHeader>(), |
| + is_offline_preview_); |
| + std::unique_ptr<offline_pages::LoadedOfflinePageInfo> info = |
| + offline_pages::LoadedOfflinePageInfo::GetInfo(*request)->DeepCopy(); |
| + std::unique_ptr<ChromeNavigationData> chrome_navigation_data( |
| + new ChromeNavigationData()); |
| + chrome_navigation_data->SetLoadedOfflinePageInfo(std::move(info)); |
| + content::WebContentsTester::For(web_contents()) |
| + ->SetNavigationData(navigation_handle, |
| + std::move(chrome_navigation_data)); |
| + } |
| + |
| + bool is_offline_preview_; |
| + net::TestURLRequestContext context_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestPreviewsTabHelper); |
| +}; |
| + |
| +} // namespace |
| class PreviewsInfoBarTabHelperUnitTest |
| : public ChromeRenderViewHostTestHarness { |
| protected: |
| void SetUp() override { |
| ChromeRenderViewHostTestHarness::SetUp(); |
| + // Insert a tab helper that will get callbacks called before |
| + // PreviewsInfoBarTabHelper. |
| + test_tab_helper_.reset(new TestPreviewsTabHelper(web_contents())); |
| 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(); |
| @@ -66,39 +122,72 @@ class PreviewsInfoBarTabHelperUnitTest |
| } |
| void SimulateWillProcessResponse() { |
| std::string headers = base::StringPrintf( |
| "HTTP/1.1 200 OK\n%s: %s\n\n", |
| data_reduction_proxy::chrome_proxy_header(), |
| data_reduction_proxy::chrome_proxy_lite_page_directive()); |
| test_handle_->CallWillProcessResponseForTesting( |
| main_rfh(), |
| net::HttpUtil::AssembleRawHeaders(headers.c_str(), headers.size())); |
| + SimulateCommit(); |
| + } |
| + |
| + void SimulateCommit() { |
| test_handle_->CallDidCommitNavigationForTesting(GURL(kTestUrl)); |
| } |
| void CallDidFinishNavigation() { test_handle_.reset(); } |
| + void set_is_offline_preview(bool is_offline_preview) { |
| + test_tab_helper_->set_is_offline_preview(is_offline_preview); |
| + } |
| + |
| private: |
| std::unique_ptr<content::NavigationHandle> test_handle_; |
| std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext> |
| drp_test_context_; |
| + std::unique_ptr<TestPreviewsTabHelper> test_tab_helper_; |
| }; |
| 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)); |
| + ->NavigateAndCommit(GURL(kTestUrl2)); |
|
megjablon
2016/10/10 22:25:13
Can you add a test that checks that when the url r
RyanSturm
2016/10/11 02:09:56
Done.
|
| + |
| + EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); |
| +} |
| + |
| +TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateOfflineInfoBar) { |
| + set_is_offline_preview(true); |
| + |
| + PreviewsInfoBarTabHelper* infobar_tab_helper = |
| + PreviewsInfoBarTabHelper::FromWebContents(web_contents()); |
| + EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); |
| + |
| + SimulateCommit(); |
| + CallDidFinishNavigation(); |
| + |
| + InfoBarService* infobar_service = |
| + InfoBarService::FromWebContents(web_contents()); |
| + EXPECT_EQ(1U, infobar_service->infobar_count()); |
| + EXPECT_TRUE(infobar_tab_helper->displayed_preview_infobar()); |
| + set_is_offline_preview(false); |
| + |
| + // Navigate to reset the displayed state. |
| + content::WebContentsTester::For(web_contents()) |
| + ->NavigateAndCommit(GURL(kTestUrl2)); |
| + |
| EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); |
| } |