Chromium Code Reviews| 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/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.
| |
| 11 #include "base/macros.h" | |
| 10 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/time/time.h" | |
| 11 #include "chrome/browser/infobars/infobar_service.h" | 14 #include "chrome/browser/infobars/infobar_service.h" |
| 15 #include "chrome/browser/loader/chrome_navigation_data.h" | |
| 12 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" | 16 #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" | 17 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_fact ory.h" |
| 14 #include "chrome/browser/previews/previews_infobar_tab_helper.h" | 18 #include "chrome/browser/previews/previews_infobar_tab_helper.h" |
| 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 19 #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" | 20 #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" | 21 #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" | 22 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_ names.h" |
| 23 #include "components/offline_pages/loaded_offline_page_info.h" | |
| 24 #include "components/offline_pages/offline_page_item.h" | |
| 25 #include "components/offline_pages/request_header/offline_page_header.h" | |
| 19 #include "components/prefs/pref_registry_simple.h" | 26 #include "components/prefs/pref_registry_simple.h" |
| 20 #include "components/proxy_config/proxy_config_pref_names.h" | 27 #include "components/proxy_config/proxy_config_pref_names.h" |
| 21 #include "content/public/browser/navigation_handle.h" | 28 #include "content/public/browser/navigation_handle.h" |
| 29 #include "content/public/browser/web_contents_observer.h" | |
| 30 #include "content/public/browser/web_contents_user_data.h" | |
| 22 #include "content/public/test/web_contents_tester.h" | 31 #include "content/public/test/web_contents_tester.h" |
| 32 #include "net/base/request_priority.h" | |
| 23 #include "net/http/http_util.h" | 33 #include "net/http/http_util.h" |
| 34 #include "net/url_request/url_request.h" | |
| 35 #include "net/url_request/url_request_context.h" | |
| 24 | 36 |
| 25 namespace { | 37 namespace { |
| 38 | |
| 26 const char kTestUrl[] = "http://www.test.com/"; | 39 const char kTestUrl[] = "http://www.test.com/"; |
| 27 } | 40 const char kTestUrl2[] = "http://www.test2.com/"; |
| 41 | |
| 42 class TestPreviewsTabHelper : public content::WebContentsObserver { | |
| 43 public: | |
| 44 explicit TestPreviewsTabHelper(content::WebContents* web_contents) | |
| 45 : WebContentsObserver(web_contents), is_offline_preview_(false) {} | |
| 46 ~TestPreviewsTabHelper() override {} | |
| 47 | |
| 48 void set_is_offline_preview(bool is_offline_preview) { | |
| 49 is_offline_preview_ = is_offline_preview; | |
| 50 } | |
| 51 | |
| 52 private: | |
| 53 // Overridden from content::WebContentsObserver: | |
| 54 void DidFinishNavigation( | |
| 55 content::NavigationHandle* navigation_handle) override { | |
| 56 // Modify the navigation handle to contain LoadedOfflinePageInfo that claims | |
| 57 // to be an offline preview iff |is_offline_preview_| is true.. | |
| 58 std::unique_ptr<net::URLRequest> request = | |
| 59 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
| |
| 60 offline_pages::LoadedOfflinePageInfo::CreateInfoForRequest( | |
| 61 request.get(), base::MakeUnique<offline_pages::OfflinePageItem>(), | |
| 62 base::MakeUnique<offline_pages::OfflinePageHeader>(), | |
| 63 is_offline_preview_); | |
| 64 std::unique_ptr<offline_pages::LoadedOfflinePageInfo> info = | |
| 65 offline_pages::LoadedOfflinePageInfo::GetInfo(*request)->DeepCopy(); | |
| 66 std::unique_ptr<ChromeNavigationData> chrome_navigation_data( | |
| 67 new ChromeNavigationData()); | |
| 68 chrome_navigation_data->SetLoadedOfflinePageInfo(std::move(info)); | |
| 69 content::WebContentsTester::For(web_contents()) | |
| 70 ->SetNavigationData(navigation_handle, | |
| 71 std::move(chrome_navigation_data)); | |
| 72 } | |
| 73 | |
| 74 bool is_offline_preview_; | |
| 75 net::TestURLRequestContext context_; | |
| 76 | |
| 77 DISALLOW_COPY_AND_ASSIGN(TestPreviewsTabHelper); | |
| 78 }; | |
| 79 | |
| 80 } // namespace | |
| 28 | 81 |
| 29 class PreviewsInfoBarTabHelperUnitTest | 82 class PreviewsInfoBarTabHelperUnitTest |
| 30 : public ChromeRenderViewHostTestHarness { | 83 : public ChromeRenderViewHostTestHarness { |
| 31 protected: | 84 protected: |
| 32 void SetUp() override { | 85 void SetUp() override { |
| 33 ChromeRenderViewHostTestHarness::SetUp(); | 86 ChromeRenderViewHostTestHarness::SetUp(); |
| 87 // Insert a tab helper that will get callbacks called before | |
| 88 // PreviewsInfoBarTabHelper. | |
| 89 test_tab_helper_.reset(new TestPreviewsTabHelper(web_contents())); | |
| 34 InfoBarService::CreateForWebContents(web_contents()); | 90 InfoBarService::CreateForWebContents(web_contents()); |
| 35 PreviewsInfoBarTabHelper::CreateForWebContents(web_contents()); | 91 PreviewsInfoBarTabHelper::CreateForWebContents(web_contents()); |
| 36 test_handle_ = content::NavigationHandle::CreateNavigationHandleForTesting( | 92 test_handle_ = content::NavigationHandle::CreateNavigationHandleForTesting( |
| 37 GURL(kTestUrl), main_rfh()); | 93 GURL(kTestUrl), main_rfh()); |
| 38 | 94 |
| 39 drp_test_context_ = | 95 drp_test_context_ = |
| 40 data_reduction_proxy::DataReductionProxyTestContext::Builder() | 96 data_reduction_proxy::DataReductionProxyTestContext::Builder() |
| 41 .WithMockConfig() | 97 .WithMockConfig() |
| 42 .SkipSettingsInitialization() | 98 .SkipSettingsInitialization() |
| 43 .Build(); | 99 .Build(); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 66 } | 122 } |
| 67 | 123 |
| 68 void SimulateWillProcessResponse() { | 124 void SimulateWillProcessResponse() { |
| 69 std::string headers = base::StringPrintf( | 125 std::string headers = base::StringPrintf( |
| 70 "HTTP/1.1 200 OK\n%s: %s\n\n", | 126 "HTTP/1.1 200 OK\n%s: %s\n\n", |
| 71 data_reduction_proxy::chrome_proxy_header(), | 127 data_reduction_proxy::chrome_proxy_header(), |
| 72 data_reduction_proxy::chrome_proxy_lite_page_directive()); | 128 data_reduction_proxy::chrome_proxy_lite_page_directive()); |
| 73 test_handle_->CallWillProcessResponseForTesting( | 129 test_handle_->CallWillProcessResponseForTesting( |
| 74 main_rfh(), | 130 main_rfh(), |
| 75 net::HttpUtil::AssembleRawHeaders(headers.c_str(), headers.size())); | 131 net::HttpUtil::AssembleRawHeaders(headers.c_str(), headers.size())); |
| 132 SimulateCommit(); | |
| 133 } | |
| 134 | |
| 135 void SimulateCommit() { | |
| 76 test_handle_->CallDidCommitNavigationForTesting(GURL(kTestUrl)); | 136 test_handle_->CallDidCommitNavigationForTesting(GURL(kTestUrl)); |
| 77 } | 137 } |
| 78 | 138 |
| 79 void CallDidFinishNavigation() { test_handle_.reset(); } | 139 void CallDidFinishNavigation() { test_handle_.reset(); } |
| 80 | 140 |
| 141 void set_is_offline_preview(bool is_offline_preview) { | |
| 142 test_tab_helper_->set_is_offline_preview(is_offline_preview); | |
| 143 } | |
| 144 | |
| 81 private: | 145 private: |
| 82 std::unique_ptr<content::NavigationHandle> test_handle_; | 146 std::unique_ptr<content::NavigationHandle> test_handle_; |
| 83 std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext> | 147 std::unique_ptr<data_reduction_proxy::DataReductionProxyTestContext> |
| 84 drp_test_context_; | 148 drp_test_context_; |
| 149 std::unique_ptr<TestPreviewsTabHelper> test_tab_helper_; | |
| 85 }; | 150 }; |
| 86 | 151 |
| 87 TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateLitePageInfoBar) { | 152 TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateLitePageInfoBar) { |
| 88 PreviewsInfoBarTabHelper* infobar_tab_helper = | 153 PreviewsInfoBarTabHelper* infobar_tab_helper = |
| 89 PreviewsInfoBarTabHelper::FromWebContents(web_contents()); | 154 PreviewsInfoBarTabHelper::FromWebContents(web_contents()); |
| 90 EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); | 155 EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); |
| 91 | 156 |
| 92 SimulateWillProcessResponse(); | 157 SimulateWillProcessResponse(); |
| 93 CallDidFinishNavigation(); | 158 CallDidFinishNavigation(); |
| 94 | 159 |
| 95 InfoBarService* infobar_service = | 160 InfoBarService* infobar_service = |
| 96 InfoBarService::FromWebContents(web_contents()); | 161 InfoBarService::FromWebContents(web_contents()); |
| 97 EXPECT_EQ(1U, infobar_service->infobar_count()); | 162 EXPECT_EQ(1U, infobar_service->infobar_count()); |
| 98 EXPECT_TRUE(infobar_tab_helper->displayed_preview_infobar()); | 163 EXPECT_TRUE(infobar_tab_helper->displayed_preview_infobar()); |
| 99 | 164 |
| 100 // Navigate to reset the displayed state. | 165 // Navigate to reset the displayed state. |
| 101 content::WebContentsTester::For(web_contents()) | 166 content::WebContentsTester::For(web_contents()) |
| 102 ->NavigateAndCommit(GURL(kTestUrl)); | 167 ->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.
| |
| 168 | |
| 103 EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); | 169 EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); |
| 104 } | 170 } |
| 171 | |
| 172 TEST_F(PreviewsInfoBarTabHelperUnitTest, CreateOfflineInfoBar) { | |
| 173 set_is_offline_preview(true); | |
| 174 | |
| 175 PreviewsInfoBarTabHelper* infobar_tab_helper = | |
| 176 PreviewsInfoBarTabHelper::FromWebContents(web_contents()); | |
| 177 EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); | |
| 178 | |
| 179 SimulateCommit(); | |
| 180 CallDidFinishNavigation(); | |
| 181 | |
| 182 InfoBarService* infobar_service = | |
| 183 InfoBarService::FromWebContents(web_contents()); | |
| 184 EXPECT_EQ(1U, infobar_service->infobar_count()); | |
| 185 EXPECT_TRUE(infobar_tab_helper->displayed_preview_infobar()); | |
| 186 set_is_offline_preview(false); | |
| 187 | |
| 188 // Navigate to reset the displayed state. | |
| 189 content::WebContentsTester::For(web_contents()) | |
| 190 ->NavigateAndCommit(GURL(kTestUrl2)); | |
| 191 | |
| 192 EXPECT_FALSE(infobar_tab_helper->displayed_preview_infobar()); | |
| 193 } | |
| OLD | NEW |