Index: components/data_reduction_proxy/content/browser/content_lofi_ui_service_unittest.cc |
diff --git a/components/data_reduction_proxy/content/browser/content_lofi_ui_service_unittest.cc b/components/data_reduction_proxy/content/browser/content_lofi_ui_service_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..cdf14834390e58e1adedc52938a27af52d69b6b5 |
--- /dev/null |
+++ b/components/data_reduction_proxy/content/browser/content_lofi_ui_service_unittest.cc |
@@ -0,0 +1,108 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/data_reduction_proxy/content/browser/content_lofi_ui_service.h" |
+ |
+#include <stddef.h> |
+ |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "base/macros.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/run_loop.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/render_frame_host.h" |
+#include "content/public/browser/render_process_host.h" |
+#include "content/public/browser/resource_request_info.h" |
+#include "content/public/browser/web_contents.h" |
+#include "content/public/test/test_renderer_host.h" |
+#include "net/socket/socket_test_util.h" |
+#include "net/url_request/url_request.h" |
+#include "net/url_request/url_request_test_util.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace data_reduction_proxy { |
+ |
+class ContentLoFiUIServiceTest : public content::RenderViewHostTestHarness { |
+ public: |
+ ContentLoFiUIServiceTest() : callback_called_(false) { |
+ // Cannot use IO_MAIN_LOOP with RenderViewHostTestHarness. |
+ SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); |
+ } |
+ |
+ void RunTestOnIOThread(base::RunLoop* ui_run_loop) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ 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
|
+ |
+ net::TestURLRequestContext context(true); |
+ net::MockClientSocketFactory mock_socket_factory; |
+ net::TestDelegate delegate; |
+ context.set_client_socket_factory(&mock_socket_factory); |
+ context.Init(); |
+ |
+ content_lofi_ui_service_.reset(new ContentLoFiUIService( |
+ content::BrowserThread::GetMessageLoopProxyForThread( |
+ content::BrowserThread::UI), |
+ base::Bind( |
+ &ContentLoFiUIServiceTest::NotifyLoFiResponseReceivedCallback, |
+ base::Unretained(this)))); |
+ |
+ scoped_ptr<net::URLRequest> request = CreateRequest(context, &delegate); |
+ |
+ content_lofi_ui_service_->NotifyLoFiReponseReceived(*request); |
+ |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::UI, FROM_HERE, |
+ base::Bind(&base::RunLoop::Quit, base::Unretained(ui_run_loop))); |
+ } |
+ |
+ scoped_ptr<net::URLRequest> CreateRequest( |
+ const net::TestURLRequestContext& context, |
+ net::TestDelegate* delegate) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
+ |
+ scoped_ptr<net::URLRequest> request = context.CreateRequest( |
+ GURL("http://www.google.com/"), net::IDLE, delegate); |
+ |
+ content::ResourceRequestInfo::AllocateForTesting( |
+ request.get(), content::RESOURCE_TYPE_SUB_FRAME, NULL, |
+ web_contents()->GetMainFrame()->GetProcess()->GetID(), -1, |
+ web_contents()->GetMainFrame()->GetRoutingID(), |
+ false, // is_main_frame |
+ false, // parent_is_main_frame |
+ false, // allow_download |
+ false, // is_async |
+ true); // is_using_lofi |
+ |
+ return request; |
+ } |
+ |
+ void NotifyLoFiResponseReceivedCallback(content::WebContents* web_contents) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ callback_called_ = true; |
+ } |
+ |
+ void VerifyNotifyLoFiResponseReceivedCallback() { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ EXPECT_TRUE(callback_called_); |
+ } |
+ |
+ private: |
+ scoped_ptr<ContentLoFiUIService> content_lofi_ui_service_; |
+ bool callback_called_; |
+}; |
+ |
+TEST_F(ContentLoFiUIServiceTest, NotifyLoFiResponseReceived) { |
+ base::RunLoop ui_run_loop; |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::IO, FROM_HERE, |
+ base::Bind(&ContentLoFiUIServiceTest::RunTestOnIOThread, |
+ base::Unretained(this), &ui_run_loop)); |
+ ui_run_loop.Run(); |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ VerifyNotifyLoFiResponseReceivedCallback(); |
+} |
+ |
+} // namespace data_reduction_proxy |