Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/data_reduction_proxy/content/browser/content_lofi_ui_servic e.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 #include "base/run_loop.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/public/browser/render_frame_host.h" | |
| 17 #include "content/public/browser/render_process_host.h" | |
| 18 #include "content/public/browser/resource_request_info.h" | |
| 19 #include "content/public/browser/web_contents.h" | |
| 20 #include "content/public/test/test_renderer_host.h" | |
| 21 #include "net/socket/socket_test_util.h" | |
| 22 #include "net/url_request/url_request.h" | |
| 23 #include "net/url_request/url_request_test_util.h" | |
| 24 #include "testing/gtest/include/gtest/gtest.h" | |
| 25 | |
| 26 namespace data_reduction_proxy { | |
| 27 | |
| 28 class ContentLoFiUIServiceTest : public content::RenderViewHostTestHarness { | |
| 29 public: | |
| 30 ContentLoFiUIServiceTest() : callback_called_(false) { | |
| 31 // Cannot use IO_MAIN_LOOP with RenderViewHostTestHarness. | |
| 32 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); | |
| 33 } | |
| 34 | |
| 35 void RunTestOnIOThread(base::RunLoop* ui_run_loop) { | |
| 36 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 37 DCHECK(ui_run_loop); | |
|
tbansal1
2016/01/05 03:10:59
DCHECK not allowed in tests.
See https://www.chro
megjablon
2016/01/05 20:13:03
Done and good to know. We do this in other parts o
| |
| 38 | |
| 39 net::TestURLRequestContext context(true); | |
| 40 net::MockClientSocketFactory mock_socket_factory; | |
| 41 net::TestDelegate delegate; | |
| 42 context.set_client_socket_factory(&mock_socket_factory); | |
| 43 context.Init(); | |
| 44 | |
| 45 content_lofi_ui_service_.reset(new ContentLoFiUIService( | |
| 46 content::BrowserThread::GetMessageLoopProxyForThread( | |
| 47 content::BrowserThread::UI), | |
| 48 base::Bind( | |
| 49 &ContentLoFiUIServiceTest::NotifyLoFiResponseReceivedCallback, | |
| 50 base::Unretained(this)))); | |
| 51 | |
| 52 scoped_ptr<net::URLRequest> request = CreateRequest(context, &delegate); | |
| 53 | |
| 54 content_lofi_ui_service_->NotifyLoFiReponseReceived(*request); | |
| 55 | |
| 56 content::BrowserThread::PostTask( | |
| 57 content::BrowserThread::UI, FROM_HERE, | |
| 58 base::Bind(&base::RunLoop::Quit, base::Unretained(ui_run_loop))); | |
| 59 } | |
| 60 | |
| 61 scoped_ptr<net::URLRequest> CreateRequest( | |
| 62 const net::TestURLRequestContext& context, | |
| 63 net::TestDelegate* delegate) { | |
| 64 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 65 | |
| 66 scoped_ptr<net::URLRequest> request = context.CreateRequest( | |
| 67 GURL("http://www.google.com/"), net::IDLE, delegate); | |
| 68 | |
| 69 content::ResourceRequestInfo::AllocateForTesting( | |
| 70 request.get(), content::RESOURCE_TYPE_SUB_FRAME, NULL, | |
| 71 web_contents()->GetMainFrame()->GetProcess()->GetID(), -1, | |
| 72 web_contents()->GetMainFrame()->GetRoutingID(), | |
| 73 false, // is_main_frame | |
| 74 false, // parent_is_main_frame | |
| 75 false, // allow_download | |
| 76 false, // is_async | |
| 77 true); // is_using_lofi | |
| 78 | |
| 79 return request; | |
| 80 } | |
| 81 | |
| 82 void NotifyLoFiResponseReceivedCallback(content::WebContents* web_contents) { | |
| 83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 84 callback_called_ = true; | |
| 85 } | |
| 86 | |
| 87 void VerifyNotifyLoFiResponseReceivedCallback() { | |
| 88 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 89 EXPECT_TRUE(callback_called_); | |
| 90 } | |
| 91 | |
| 92 private: | |
| 93 scoped_ptr<ContentLoFiUIService> content_lofi_ui_service_; | |
| 94 bool callback_called_; | |
| 95 }; | |
| 96 | |
| 97 TEST_F(ContentLoFiUIServiceTest, NotifyLoFiResponseReceived) { | |
| 98 base::RunLoop ui_run_loop; | |
| 99 content::BrowserThread::PostTask( | |
| 100 content::BrowserThread::IO, FROM_HERE, | |
| 101 base::Bind(&ContentLoFiUIServiceTest::RunTestOnIOThread, | |
| 102 base::Unretained(this), &ui_run_loop)); | |
| 103 ui_run_loop.Run(); | |
| 104 base::MessageLoop::current()->RunUntilIdle(); | |
| 105 VerifyNotifyLoFiResponseReceivedCallback(); | |
| 106 } | |
| 107 | |
| 108 } // namespace data_reduction_proxy | |
| OLD | NEW |