| 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_tab_helper.h" | 5 #include "chrome/browser/previews/previews_infobar_tab_helper.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "chrome/browser/infobars/infobar_service.h" | 11 #include "chrome/browser/infobars/infobar_service.h" |
| 12 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.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" | 13 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_fact
ory.h" |
| 14 #include "chrome/browser/previews/previews_infobar_tab_helper.h" | 14 #include "chrome/browser/previews/previews_infobar_tab_helper.h" |
| 15 #include "chrome/common/features.h" | |
| 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 17 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test
_utils.h" | 16 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test
_utils.h" |
| 18 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_heade
rs.h" | 17 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_heade
rs.h" |
| 19 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_
names.h" | 18 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_
names.h" |
| 20 #include "components/offline_pages/offline_page_item.h" | |
| 21 #include "components/offline_pages/request_header/offline_page_header.h" | |
| 22 #include "components/prefs/pref_registry_simple.h" | 19 #include "components/prefs/pref_registry_simple.h" |
| 23 #include "components/proxy_config/proxy_config_pref_names.h" | 20 #include "components/proxy_config/proxy_config_pref_names.h" |
| 24 #include "content/public/browser/navigation_handle.h" | 21 #include "content/public/browser/navigation_handle.h" |
| 25 #include "content/public/test/web_contents_tester.h" | 22 #include "content/public/test/web_contents_tester.h" |
| 26 #include "net/http/http_util.h" | 23 #include "net/http/http_util.h" |
| 27 | 24 |
| 28 #if BUILDFLAG(ANDROID_JAVA_UI) | |
| 29 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" | |
| 30 #endif // BUILDFLAG(ANDROID_JAVA_UI) | |
| 31 | |
| 32 namespace { | 25 namespace { |
| 33 const char kTestUrl[] = "http://www.test.com/"; | 26 const char kTestUrl[] = "http://www.test.com/"; |
| 34 } | 27 } |
| 35 | 28 |
| 36 class PreviewsInfoBarTabHelperUnitTest | 29 class PreviewsInfoBarTabHelperUnitTest |
| 37 : public ChromeRenderViewHostTestHarness { | 30 : public ChromeRenderViewHostTestHarness { |
| 38 protected: | 31 protected: |
| 39 void SetUp() override { | 32 void SetUp() override { |
| 40 ChromeRenderViewHostTestHarness::SetUp(); | 33 ChromeRenderViewHostTestHarness::SetUp(); |
| 41 // Insert an OfflinePageTabHelper before PreviewsInfoBarTabHelper. | |
| 42 #if BUILDFLAG(ANDROID_JAVA_UI) | |
| 43 offline_pages::OfflinePageTabHelper::CreateForWebContents(web_contents()); | |
| 44 #endif // BUILDFLAG(ANDROID_JAVA_UI) | |
| 45 InfoBarService::CreateForWebContents(web_contents()); | 34 InfoBarService::CreateForWebContents(web_contents()); |
| 46 PreviewsInfoBarTabHelper::CreateForWebContents(web_contents()); | 35 PreviewsInfoBarTabHelper::CreateForWebContents(web_contents()); |
| 47 test_handle_ = content::NavigationHandle::CreateNavigationHandleForTesting( | 36 test_handle_ = content::NavigationHandle::CreateNavigationHandleForTesting( |
| 48 GURL(kTestUrl), main_rfh()); | 37 GURL(kTestUrl), main_rfh()); |
| 49 | 38 |
| 50 drp_test_context_ = | 39 drp_test_context_ = |
| 51 data_reduction_proxy::DataReductionProxyTestContext::Builder() | 40 data_reduction_proxy::DataReductionProxyTestContext::Builder() |
| 52 .WithMockConfig() | 41 .WithMockConfig() |
| 53 .SkipSettingsInitialization() | 42 .SkipSettingsInitialization() |
| 54 .Build(); | 43 .Build(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 77 } | 66 } |
| 78 | 67 |
| 79 void SimulateWillProcessResponse() { | 68 void SimulateWillProcessResponse() { |
| 80 std::string headers = base::StringPrintf( | 69 std::string headers = base::StringPrintf( |
| 81 "HTTP/1.1 200 OK\n%s: %s\n\n", | 70 "HTTP/1.1 200 OK\n%s: %s\n\n", |
| 82 data_reduction_proxy::chrome_proxy_content_transform_header(), | 71 data_reduction_proxy::chrome_proxy_content_transform_header(), |
| 83 data_reduction_proxy::lite_page_directive()); | 72 data_reduction_proxy::lite_page_directive()); |
| 84 test_handle_->CallWillProcessResponseForTesting( | 73 test_handle_->CallWillProcessResponseForTesting( |
| 85 main_rfh(), | 74 main_rfh(), |
| 86 net::HttpUtil::AssembleRawHeaders(headers.c_str(), headers.size())); | 75 net::HttpUtil::AssembleRawHeaders(headers.c_str(), headers.size())); |
| 87 SimulateCommit(); | |
| 88 } | |
| 89 | |
| 90 void SimulateCommit() { | |
| 91 test_handle_->CallDidCommitNavigationForTesting(GURL(kTestUrl)); | 76 test_handle_->CallDidCommitNavigationForTesting(GURL(kTestUrl)); |
| 92 } | 77 } |
| 93 | 78 |
| 94 void CallDidFinishNavigation() { test_handle_.reset(); } | 79 void CallDidFinishNavigation() { test_handle_.reset(); } |
| 95 | 80 |
| 96 private: | 81 private: |
| 97 std::unique_ptr<content::NavigationHandle> test_handle_; | 82 std::unique_ptr<content::NavigationHandle> test_handle_; |
| 98 std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext> | 83 std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext> |
| 99 drp_test_context_; | 84 drp_test_context_; |
| 100 }; | 85 }; |
| 101 | 86 |
| 102 TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateLitePageInfoBar) { | 87 TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateLitePageInfoBar) { |
| 103 PreviewsInfoBarTabHelper* infobar_tab_helper = | 88 PreviewsInfoBarTabHelper* infobar_tab_helper = |
| 104 PreviewsInfoBarTabHelper::FromWebContents(web_contents()); | 89 PreviewsInfoBarTabHelper::FromWebContents(web_contents()); |
| 105 EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); | 90 EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); |
| 106 | 91 |
| 107 SimulateWillProcessResponse(); | 92 SimulateWillProcessResponse(); |
| 108 CallDidFinishNavigation(); | 93 CallDidFinishNavigation(); |
| 109 | 94 |
| 110 InfoBarService* infobar_service = | 95 InfoBarService* infobar_service = |
| 111 InfoBarService::FromWebContents(web_contents()); | 96 InfoBarService::FromWebContents(web_contents()); |
| 112 EXPECT_EQ(1U, infobar_service->infobar_count()); | 97 EXPECT_EQ(1U, infobar_service->infobar_count()); |
| 113 EXPECT_TRUE(infobar_tab_helper->displayed_preview_infobar()); | 98 EXPECT_TRUE(infobar_tab_helper->displayed_preview_infobar()); |
| 114 | 99 |
| 115 // Navigate to reset the displayed state. | 100 // Navigate to reset the displayed state. |
| 116 content::WebContentsTester::For(web_contents()) | 101 content::WebContentsTester::For(web_contents()) |
| 117 ->NavigateAndCommit(GURL(kTestUrl)); | 102 ->NavigateAndCommit(GURL(kTestUrl)); |
| 118 | |
| 119 EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); | 103 EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); |
| 120 } | 104 } |
| 121 | |
| 122 #if BUILDFLAG(ANDROID_JAVA_UI) | |
| 123 TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateOfflineInfoBar) { | |
| 124 PreviewsInfoBarTabHelper* infobar_tab_helper = | |
| 125 PreviewsInfoBarTabHelper::FromWebContents(web_contents()); | |
| 126 EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); | |
| 127 | |
| 128 SimulateCommit(); | |
| 129 offline_pages::OfflinePageItem item; | |
| 130 offline_pages::OfflinePageHeader header; | |
| 131 offline_pages::OfflinePageTabHelper::FromWebContents(web_contents()) | |
| 132 ->SetOfflinePage(item, header, true); | |
| 133 CallDidFinishNavigation(); | |
| 134 | |
| 135 InfoBarService* infobar_service = | |
| 136 InfoBarService::FromWebContents(web_contents()); | |
| 137 EXPECT_EQ(1U, infobar_service->infobar_count()); | |
| 138 EXPECT_TRUE(infobar_tab_helper->displayed_preview_infobar()); | |
| 139 | |
| 140 // Navigate to reset the displayed state. | |
| 141 content::WebContentsTester::For(web_contents()) | |
| 142 ->NavigateAndCommit(GURL(kTestUrl)); | |
| 143 | |
| 144 EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); | |
| 145 } | |
| 146 #endif // BUILDFLAG(ANDROID_JAVA_UI) | |
| OLD | NEW |