| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/data_reduction_proxy/content/browser/content_lofi_ui_servic
e.h" | 5 #include "components/data_reduction_proxy/content/browser/content_lofi_ui_servic
e.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/render_frame_host.h" | 16 #include "content/public/browser/render_frame_host.h" |
| 17 #include "content/public/browser/render_process_host.h" | 17 #include "content/public/browser/render_process_host.h" |
| 18 #include "content/public/browser/resource_request_info.h" | 18 #include "content/public/browser/resource_request_info.h" |
| 19 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/test/test_renderer_host.h" | 20 #include "content/public/test/test_renderer_host.h" |
| 21 #include "net/socket/socket_test_util.h" | 21 #include "net/socket/socket_test_util.h" |
| 22 #include "net/url_request/url_request.h" | 22 #include "net/url_request/url_request.h" |
| 23 #include "net/url_request/url_request_test_util.h" | 23 #include "net/url_request/url_request_test_util.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 25 |
| 26 namespace data_reduction_proxy { | 26 namespace data_reduction_proxy { |
| 27 | 27 |
| 28 class ContentLoFiUIServiceTest : public content::RenderViewHostTestHarness { | 28 class ContentLoFiUIServiceTest : public content::RenderViewHostTestHarness { |
| 29 public: | 29 public: |
| 30 ContentLoFiUIServiceTest() : callback_called_(false), is_preview_(false) { | 30 ContentLoFiUIServiceTest() : callback_called_(false) { |
| 31 // Cannot use IO_MAIN_LOOP with RenderViewHostTestHarness. | 31 // Cannot use IO_MAIN_LOOP with RenderViewHostTestHarness. |
| 32 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); | 32 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); |
| 33 } | 33 } |
| 34 | 34 |
| 35 void RunTestOnIOThread(base::RunLoop* ui_run_loop, bool is_preview) { | 35 void RunTestOnIOThread(base::RunLoop* ui_run_loop) { |
| 36 ASSERT_TRUE(ui_run_loop); | 36 ASSERT_TRUE(ui_run_loop); |
| 37 EXPECT_TRUE( | 37 EXPECT_TRUE( |
| 38 content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 38 content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 39 | 39 |
| 40 net::TestURLRequestContext context(true); | 40 net::TestURLRequestContext context(true); |
| 41 net::MockClientSocketFactory mock_socket_factory; | 41 net::MockClientSocketFactory mock_socket_factory; |
| 42 net::TestDelegate delegate; | 42 net::TestDelegate delegate; |
| 43 context.set_client_socket_factory(&mock_socket_factory); | 43 context.set_client_socket_factory(&mock_socket_factory); |
| 44 context.Init(); | 44 context.Init(); |
| 45 | 45 |
| 46 content_lofi_ui_service_.reset(new ContentLoFiUIService( | 46 content_lofi_ui_service_.reset(new ContentLoFiUIService( |
| 47 content::BrowserThread::GetTaskRunnerForThread( | 47 content::BrowserThread::GetTaskRunnerForThread( |
| 48 content::BrowserThread::UI), | 48 content::BrowserThread::UI), |
| 49 base::Bind(&ContentLoFiUIServiceTest::OnLoFiResponseReceivedCallback, | 49 base::Bind(&ContentLoFiUIServiceTest::OnLoFiResponseReceivedCallback, |
| 50 base::Unretained(this)))); | 50 base::Unretained(this)))); |
| 51 | 51 |
| 52 std::unique_ptr<net::URLRequest> request = | 52 std::unique_ptr<net::URLRequest> request = |
| 53 CreateRequest(context, &delegate); | 53 CreateRequest(context, &delegate); |
| 54 | 54 |
| 55 content_lofi_ui_service_->OnLoFiReponseReceived(*request, is_preview); | 55 content_lofi_ui_service_->OnLoFiReponseReceived(*request); |
| 56 | 56 |
| 57 content::BrowserThread::PostTask( | 57 content::BrowserThread::PostTask( |
| 58 content::BrowserThread::UI, FROM_HERE, | 58 content::BrowserThread::UI, FROM_HERE, |
| 59 base::Bind(&base::RunLoop::Quit, base::Unretained(ui_run_loop))); | 59 base::Bind(&base::RunLoop::Quit, base::Unretained(ui_run_loop))); |
| 60 } | 60 } |
| 61 | 61 |
| 62 std::unique_ptr<net::URLRequest> CreateRequest( | 62 std::unique_ptr<net::URLRequest> CreateRequest( |
| 63 const net::TestURLRequestContext& context, | 63 const net::TestURLRequestContext& context, |
| 64 net::TestDelegate* delegate) { | 64 net::TestDelegate* delegate) { |
| 65 EXPECT_TRUE( | 65 EXPECT_TRUE( |
| 66 content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 66 content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 67 | 67 |
| 68 std::unique_ptr<net::URLRequest> request = context.CreateRequest( | 68 std::unique_ptr<net::URLRequest> request = context.CreateRequest( |
| 69 GURL("http://www.google.com/"), net::IDLE, delegate); | 69 GURL("http://www.google.com/"), net::IDLE, delegate); |
| 70 | 70 |
| 71 content::ResourceRequestInfo::AllocateForTesting( | 71 content::ResourceRequestInfo::AllocateForTesting( |
| 72 request.get(), content::RESOURCE_TYPE_SUB_FRAME, NULL, | 72 request.get(), content::RESOURCE_TYPE_SUB_FRAME, NULL, |
| 73 web_contents()->GetMainFrame()->GetProcess()->GetID(), -1, | 73 web_contents()->GetMainFrame()->GetProcess()->GetID(), -1, |
| 74 web_contents()->GetMainFrame()->GetRoutingID(), | 74 web_contents()->GetMainFrame()->GetRoutingID(), |
| 75 false, // is_main_frame | 75 false, // is_main_frame |
| 76 false, // parent_is_main_frame | 76 false, // parent_is_main_frame |
| 77 false, // allow_download | 77 false, // allow_download |
| 78 false, // is_async | 78 false, // is_async |
| 79 true); // is_using_lofi | 79 true); // is_using_lofi |
| 80 | 80 |
| 81 return request; | 81 return request; |
| 82 } | 82 } |
| 83 | 83 |
| 84 void OnLoFiResponseReceivedCallback(content::WebContents* web_contents, | 84 void OnLoFiResponseReceivedCallback(content::WebContents* web_contents) { |
| 85 bool is_preview) { | |
| 86 EXPECT_TRUE( | 85 EXPECT_TRUE( |
| 87 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 86 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 88 callback_called_ = true; | 87 callback_called_ = true; |
| 89 is_preview_ = is_preview; | |
| 90 } | 88 } |
| 91 | 89 |
| 92 void VerifyOnLoFiResponseReceivedCallback(bool is_preview) { | 90 void VerifyOnLoFiResponseReceivedCallback() { |
| 93 EXPECT_TRUE( | 91 EXPECT_TRUE( |
| 94 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 92 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 95 EXPECT_TRUE(callback_called_); | 93 EXPECT_TRUE(callback_called_); |
| 96 EXPECT_EQ(is_preview, is_preview_); | |
| 97 } | 94 } |
| 98 | 95 |
| 99 private: | 96 private: |
| 100 std::unique_ptr<ContentLoFiUIService> content_lofi_ui_service_; | 97 std::unique_ptr<ContentLoFiUIService> content_lofi_ui_service_; |
| 101 bool callback_called_; | 98 bool callback_called_; |
| 102 bool is_preview_; | |
| 103 }; | 99 }; |
| 104 | 100 |
| 105 TEST_F(ContentLoFiUIServiceTest, OnLoFiResponseReceived) { | 101 TEST_F(ContentLoFiUIServiceTest, OnLoFiResponseReceived) { |
| 106 base::RunLoop ui_run_loop; | 102 base::RunLoop ui_run_loop; |
| 107 content::BrowserThread::PostTask( | 103 content::BrowserThread::PostTask( |
| 108 content::BrowserThread::IO, FROM_HERE, | 104 content::BrowserThread::IO, FROM_HERE, |
| 109 base::Bind(&ContentLoFiUIServiceTest::RunTestOnIOThread, | 105 base::Bind(&ContentLoFiUIServiceTest::RunTestOnIOThread, |
| 110 base::Unretained(this), &ui_run_loop, false)); | 106 base::Unretained(this), &ui_run_loop)); |
| 111 ui_run_loop.Run(); | 107 ui_run_loop.Run(); |
| 112 base::RunLoop().RunUntilIdle(); | 108 base::RunLoop().RunUntilIdle(); |
| 113 VerifyOnLoFiResponseReceivedCallback(false); | 109 VerifyOnLoFiResponseReceivedCallback(); |
| 114 } | |
| 115 | |
| 116 TEST_F(ContentLoFiUIServiceTest, OnLoFiPreviewResponseReceived) { | |
| 117 base::RunLoop ui_run_loop; | |
| 118 content::BrowserThread::PostTask( | |
| 119 content::BrowserThread::IO, FROM_HERE, | |
| 120 base::Bind(&ContentLoFiUIServiceTest::RunTestOnIOThread, | |
| 121 base::Unretained(this), &ui_run_loop, true)); | |
| 122 ui_run_loop.Run(); | |
| 123 base::RunLoop().RunUntilIdle(); | |
| 124 VerifyOnLoFiResponseReceivedCallback(true); | |
| 125 } | 110 } |
| 126 | 111 |
| 127 } // namespace data_reduction_proxy | 112 } // namespace data_reduction_proxy |
| OLD | NEW |