Chromium Code Reviews| 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 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 | 27 |
| 28 namespace data_reduction_proxy { | 28 namespace data_reduction_proxy { |
| 29 | 29 |
| 30 class ContentLoFiUIServiceTest : public content::RenderViewHostTestHarness { | 30 class ContentLoFiUIServiceTest : public content::RenderViewHostTestHarness { |
| 31 public: | 31 public: |
| 32 ContentLoFiUIServiceTest() : callback_called_(false) { | 32 ContentLoFiUIServiceTest() : callback_called_(false) { |
| 33 // Cannot use IO_MAIN_LOOP with RenderViewHostTestHarness. | 33 // Cannot use IO_MAIN_LOOP with RenderViewHostTestHarness. |
| 34 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); | 34 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); |
| 35 } | 35 } |
| 36 | 36 |
| 37 void RunTestOnIOThread(base::RunLoop* ui_run_loop) { | 37 void RunTestOnIOThread(base::RunLoop* ui_run_loop, bool is_server_lofi) { |
| 38 ASSERT_TRUE(ui_run_loop); | 38 ASSERT_TRUE(ui_run_loop); |
| 39 EXPECT_TRUE( | 39 EXPECT_TRUE( |
| 40 content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 40 content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 41 | 41 |
| 42 net::TestURLRequestContext context(true); | 42 net::TestURLRequestContext context(true); |
| 43 net::MockClientSocketFactory mock_socket_factory; | 43 net::MockClientSocketFactory mock_socket_factory; |
| 44 net::TestDelegate delegate; | 44 net::TestDelegate delegate; |
| 45 context.set_client_socket_factory(&mock_socket_factory); | 45 context.set_client_socket_factory(&mock_socket_factory); |
| 46 context.Init(); | 46 context.Init(); |
| 47 | 47 |
| 48 content_lofi_ui_service_.reset(new ContentLoFiUIService( | 48 content_lofi_ui_service_.reset(new ContentLoFiUIService( |
| 49 content::BrowserThread::GetTaskRunnerForThread( | 49 content::BrowserThread::GetTaskRunnerForThread( |
| 50 content::BrowserThread::UI), | 50 content::BrowserThread::UI), |
| 51 base::Bind(&ContentLoFiUIServiceTest::OnLoFiResponseReceivedCallback, | 51 base::Bind(&ContentLoFiUIServiceTest::OnLoFiResponseReceivedCallback, |
| 52 base::Unretained(this)))); | 52 base::Unretained(this)))); |
| 53 | 53 |
| 54 std::unique_ptr<net::URLRequest> request = | 54 std::unique_ptr<net::URLRequest> request = |
| 55 CreateRequest(context, &delegate); | 55 CreateRequest(context, &delegate); |
| 56 | 56 |
| 57 content_lofi_ui_service_->OnLoFiReponseReceived(*request); | 57 content_lofi_ui_service_->OnLoFiReponseReceived(*request, is_server_lofi); |
| 58 | 58 |
| 59 content::BrowserThread::PostTask( | 59 content::BrowserThread::PostTask( |
| 60 content::BrowserThread::UI, FROM_HERE, | 60 content::BrowserThread::UI, FROM_HERE, |
| 61 base::Bind(&base::RunLoop::Quit, base::Unretained(ui_run_loop))); | 61 base::Bind(&base::RunLoop::Quit, base::Unretained(ui_run_loop))); |
| 62 } | 62 } |
| 63 | 63 |
| 64 std::unique_ptr<net::URLRequest> CreateRequest( | 64 std::unique_ptr<net::URLRequest> CreateRequest( |
| 65 const net::TestURLRequestContext& context, | 65 const net::TestURLRequestContext& context, |
| 66 net::TestDelegate* delegate) { | 66 net::TestDelegate* delegate) { |
| 67 EXPECT_TRUE( | 67 EXPECT_TRUE( |
| 68 content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 68 content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 69 | 69 |
| 70 std::unique_ptr<net::URLRequest> request = | 70 std::unique_ptr<net::URLRequest> request = |
| 71 context.CreateRequest(GURL("http://www.google.com/"), net::IDLE, | 71 context.CreateRequest(GURL("http://www.google.com/"), net::IDLE, |
| 72 delegate, TRAFFIC_ANNOTATION_FOR_TESTS); | 72 delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
| 73 | 73 |
| 74 content::ResourceRequestInfo::AllocateForTesting( | 74 content::ResourceRequestInfo::AllocateForTesting( |
| 75 request.get(), content::RESOURCE_TYPE_SUB_FRAME, NULL, | 75 request.get(), content::RESOURCE_TYPE_SUB_FRAME, NULL, |
| 76 web_contents()->GetMainFrame()->GetProcess()->GetID(), -1, | 76 web_contents()->GetMainFrame()->GetProcess()->GetID(), -1, |
| 77 web_contents()->GetMainFrame()->GetRoutingID(), | 77 web_contents()->GetMainFrame()->GetRoutingID(), |
| 78 /*is_main_frame=*/false, | 78 /*is_main_frame=*/false, |
| 79 /*parent_is_main_frame=*/false, | 79 /*parent_is_main_frame=*/false, |
| 80 /*allow_download=*/false, | 80 /*allow_download=*/false, |
| 81 /*is_async=*/false, content::SERVER_LOFI_ON); | 81 /*is_async=*/false, content::SERVER_LOFI_ON); |
| 82 | 82 |
| 83 return request; | 83 return request; |
| 84 } | 84 } |
| 85 | 85 |
| 86 void OnLoFiResponseReceivedCallback(content::WebContents* web_contents) { | 86 void OnLoFiResponseReceivedCallback(content::WebContents* web_contents, |
| 87 bool is_server_lofi) { | |
| 87 EXPECT_TRUE( | 88 EXPECT_TRUE( |
| 88 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 89 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 89 callback_called_ = true; | 90 callback_called_ = true; |
| 91 is_server_lofi_ = is_server_lofi; | |
| 90 } | 92 } |
| 91 | 93 |
| 92 void VerifyOnLoFiResponseReceivedCallback() { | 94 void VerifyOnLoFiResponseReceivedCallback(bool is_server_lofi) { |
|
sclittle
2017/05/03 22:51:29
nit: could this |is_server_lofi| argument here be
RyanSturm
2017/05/03 23:20:30
Done.
| |
| 93 EXPECT_TRUE( | 95 EXPECT_TRUE( |
| 94 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 96 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 95 EXPECT_TRUE(callback_called_); | 97 EXPECT_TRUE(callback_called_); |
| 98 EXPECT_EQ(is_server_lofi, is_server_lofi_); | |
| 96 } | 99 } |
| 97 | 100 |
| 98 private: | 101 private: |
| 99 std::unique_ptr<ContentLoFiUIService> content_lofi_ui_service_; | 102 std::unique_ptr<ContentLoFiUIService> content_lofi_ui_service_; |
| 100 bool callback_called_; | 103 bool callback_called_; |
| 104 bool is_server_lofi_; | |
| 101 }; | 105 }; |
| 102 | 106 |
| 103 TEST_F(ContentLoFiUIServiceTest, OnLoFiResponseReceived) { | 107 TEST_F(ContentLoFiUIServiceTest, OnLoFiResponseReceivedServer) { |
| 104 base::RunLoop ui_run_loop; | 108 base::RunLoop ui_run_loop; |
| 109 bool is_server_lofi = true; | |
| 105 content::BrowserThread::PostTask( | 110 content::BrowserThread::PostTask( |
| 106 content::BrowserThread::IO, FROM_HERE, | 111 content::BrowserThread::IO, FROM_HERE, |
| 107 base::Bind(&ContentLoFiUIServiceTest::RunTestOnIOThread, | 112 base::Bind(&ContentLoFiUIServiceTest::RunTestOnIOThread, |
| 108 base::Unretained(this), &ui_run_loop)); | 113 base::Unretained(this), &ui_run_loop, is_server_lofi)); |
| 109 ui_run_loop.Run(); | 114 ui_run_loop.Run(); |
| 110 base::RunLoop().RunUntilIdle(); | 115 base::RunLoop().RunUntilIdle(); |
| 111 VerifyOnLoFiResponseReceivedCallback(); | 116 VerifyOnLoFiResponseReceivedCallback(is_server_lofi); |
| 117 } | |
| 118 | |
| 119 TEST_F(ContentLoFiUIServiceTest, OnLoFiResponseReceivedClient) { | |
| 120 base::RunLoop ui_run_loop; | |
| 121 bool is_server_lofi = false; | |
| 122 content::BrowserThread::PostTask( | |
| 123 content::BrowserThread::IO, FROM_HERE, | |
| 124 base::Bind(&ContentLoFiUIServiceTest::RunTestOnIOThread, | |
| 125 base::Unretained(this), &ui_run_loop, is_server_lofi)); | |
| 126 ui_run_loop.Run(); | |
| 127 base::RunLoop().RunUntilIdle(); | |
| 128 VerifyOnLoFiResponseReceivedCallback(is_server_lofi); | |
| 112 } | 129 } |
| 113 | 130 |
| 114 } // namespace data_reduction_proxy | 131 } // namespace data_reduction_proxy |
| OLD | NEW |