Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(172)

Side by Side Diff: chrome/browser/sync/glue/http_bridge_unittest.cc

Issue 258008: Move initialization of ChromeURLRequestContexts to the IO thread. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: sync again, just in case Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/sync/glue/http_bridge.cc ('k') | chrome/browser/sync/glue/sync_backend_host.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 #if defined(BROWSER_SYNC) 5 #if defined(BROWSER_SYNC)
6 6
7 #include "base/thread.h" 7 #include "base/thread.h"
8 #include "chrome/browser/sync/glue/http_bridge.h" 8 #include "chrome/browser/sync/glue/http_bridge.h"
9 #include "net/url_request/url_request_unittest.h" 9 #include "net/url_request/url_request_unittest.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 11
12 using browser_sync::HttpBridge; 12 using browser_sync::HttpBridge;
13 13
14 namespace { 14 namespace {
15 // TODO(timsteele): Should use PathService here. See Chromium Issue 3113. 15 // TODO(timsteele): Should use PathService here. See Chromium Issue 3113.
16 const wchar_t kDocRoot[] = L"chrome/test/data"; 16 const wchar_t kDocRoot[] = L"chrome/test/data";
17 } 17 }
18 18
19 // Lazy getter for TestURLRequestContext instances.
20 class TestURLRequestContextGetter : public URLRequestContextGetter {
21 public:
22 virtual URLRequestContext* GetURLRequestContext() {
23 if (!context_)
24 context_ = new TestURLRequestContext;
25 return context_;
26 }
27 private:
28 scoped_refptr<URLRequestContext> context_;
29 };
30
19 class HttpBridgeTest : public testing::Test { 31 class HttpBridgeTest : public testing::Test {
20 public: 32 public:
21 HttpBridgeTest() 33 HttpBridgeTest()
22 : fake_default_request_context_(NULL), 34 : fake_default_request_context_getter_(NULL),
23 io_thread_("HttpBridgeTest IO thread") { 35 io_thread_("HttpBridgeTest IO thread") {
24 } 36 }
25 37
26 virtual void SetUp() { 38 virtual void SetUp() {
27 base::Thread::Options options; 39 base::Thread::Options options;
28 options.message_loop_type = MessageLoop::TYPE_IO; 40 options.message_loop_type = MessageLoop::TYPE_IO;
29 io_thread_.StartWithOptions(options); 41 io_thread_.StartWithOptions(options);
30 } 42 }
31 43
32 virtual void TearDown() { 44 virtual void TearDown() {
33 io_thread_loop()->ReleaseSoon(FROM_HERE, fake_default_request_context_); 45 io_thread_loop()->ReleaseSoon(FROM_HERE,
46 fake_default_request_context_getter_);
34 io_thread_.Stop(); 47 io_thread_.Stop();
35 fake_default_request_context_ = NULL; 48 fake_default_request_context_getter_ = NULL;
36 } 49 }
37 50
38 HttpBridge* BuildBridge() { 51 HttpBridge* BuildBridge() {
39 if (!fake_default_request_context_) { 52 if (!fake_default_request_context_getter_) {
40 fake_default_request_context_ = new TestURLRequestContext(); 53 fake_default_request_context_getter_ = new TestURLRequestContextGetter();
41 fake_default_request_context_->AddRef(); 54 fake_default_request_context_getter_->AddRef();
42 } 55 }
43 HttpBridge* bridge = new HttpBridge( 56 HttpBridge* bridge = new HttpBridge(
44 new HttpBridge::RequestContext(fake_default_request_context_), 57 new HttpBridge::RequestContextGetter(
45 io_thread_.message_loop()); 58 fake_default_request_context_getter_),
59 io_thread_loop());
46 bridge->use_io_loop_for_testing_ = true; 60 bridge->use_io_loop_for_testing_ = true;
47 return bridge; 61 return bridge;
48 } 62 }
49 63
50 MessageLoop* io_thread_loop() { return io_thread_.message_loop(); } 64 MessageLoop* io_thread_loop() { return io_thread_.message_loop(); }
51 65
52 // Note this is lazy created, so don't call this before your bridge. 66 // Note this is lazy created, so don't call this before your bridge.
53 TestURLRequestContext* GetTestRequestContext() { 67 TestURLRequestContextGetter* GetTestRequestContextGetter() {
54 return fake_default_request_context_; 68 return fake_default_request_context_getter_;
55 } 69 }
56 70
57 private: 71 private:
58 // A make-believe "default" request context, as would be returned by 72 // A make-believe "default" request context, as would be returned by
59 // Profile::GetDefaultRequestContext(). Created lazily by BuildBridge. 73 // Profile::GetDefaultRequestContext(). Created lazily by BuildBridge.
60 TestURLRequestContext* fake_default_request_context_; 74 TestURLRequestContextGetter* fake_default_request_context_getter_;
61 75
62 // Separate thread for IO used by the HttpBridge. 76 // Separate thread for IO used by the HttpBridge.
63 base::Thread io_thread_; 77 base::Thread io_thread_;
64 }; 78 };
65 79
66 // An HttpBridge that doesn't actually make network requests and just calls 80 // An HttpBridge that doesn't actually make network requests and just calls
67 // back with dummy response info. 81 // back with dummy response info.
68 class ShuntedHttpBridge : public HttpBridge { 82 class ShuntedHttpBridge : public HttpBridge {
69 public: 83 public:
70 ShuntedHttpBridge(URLRequestContext* baseline_context, 84 ShuntedHttpBridge(URLRequestContextGetter* baseline_context_getter,
71 MessageLoop* io_loop, HttpBridgeTest* test) 85 MessageLoop* io_loop, HttpBridgeTest* test)
72 : HttpBridge(new HttpBridge::RequestContext(baseline_context), 86 : HttpBridge(new HttpBridge::RequestContextGetter(
87 baseline_context_getter),
73 io_loop), test_(test) { } 88 io_loop), test_(test) { }
74 protected: 89 protected:
75 virtual void MakeAsynchronousPost() { 90 virtual void MakeAsynchronousPost() {
76 ASSERT_TRUE(MessageLoop::current() == test_->io_thread_loop()); 91 ASSERT_TRUE(MessageLoop::current() == test_->io_thread_loop());
77 // We don't actually want to make a request for this test, so just callback 92 // We don't actually want to make a request for this test, so just callback
78 // as if it completed. 93 // as if it completed.
79 test_->io_thread_loop()->PostTask(FROM_HERE, 94 test_->io_thread_loop()->PostTask(FROM_HERE,
80 NewRunnableMethod(this, &ShuntedHttpBridge::CallOnURLFetchComplete)); 95 NewRunnableMethod(this, &ShuntedHttpBridge::CallOnURLFetchComplete));
81 } 96 }
82 private: 97 private:
83 void CallOnURLFetchComplete() { 98 void CallOnURLFetchComplete() {
84 ASSERT_TRUE(MessageLoop::current() == test_->io_thread_loop()); 99 ASSERT_TRUE(MessageLoop::current() == test_->io_thread_loop());
85 // We return no cookies and a dummy content response. 100 // We return no cookies and a dummy content response.
86 ResponseCookies cookies; 101 ResponseCookies cookies;
87 102
88 std::string response_content = "success!"; 103 std::string response_content = "success!";
89 OnURLFetchComplete(NULL, GURL("www.google.com"), URLRequestStatus(), 104 OnURLFetchComplete(NULL, GURL("www.google.com"), URLRequestStatus(),
90 200, cookies, response_content); 105 200, cookies, response_content);
91 } 106 }
92 HttpBridgeTest* test_; 107 HttpBridgeTest* test_;
93 }; 108 };
94 109
95 TEST_F(HttpBridgeTest, TestUsesSameHttpNetworkSession) { 110 TEST_F(HttpBridgeTest, TestUsesSameHttpNetworkSession) {
96 scoped_refptr<HttpBridge> http_bridge(this->BuildBridge()); 111 scoped_refptr<HttpBridge> http_bridge(this->BuildBridge());
97 EXPECT_TRUE(GetTestRequestContext()); 112 EXPECT_TRUE(GetTestRequestContextGetter());
98 net::HttpNetworkSession* test_session = 113 net::HttpNetworkSession* test_session =
99 GetTestRequestContext()->http_transaction_factory()->GetSession(); 114 GetTestRequestContextGetter()->GetURLRequestContext()->
115 http_transaction_factory()->GetSession();
100 EXPECT_EQ(test_session, 116 EXPECT_EQ(test_session,
101 http_bridge->GetRequestContext()-> 117 http_bridge->GetRequestContextGetter()->
118 GetURLRequestContext()->
102 http_transaction_factory()->GetSession()); 119 http_transaction_factory()->GetSession());
103 } 120 }
104 121
105 // Test the HttpBridge without actually making any network requests. 122 // Test the HttpBridge without actually making any network requests.
106 TEST_F(HttpBridgeTest, TestMakeSynchronousPostShunted) { 123 TEST_F(HttpBridgeTest, TestMakeSynchronousPostShunted) {
107 scoped_refptr<TestURLRequestContext> ctx(new TestURLRequestContext()); 124 scoped_refptr<URLRequestContextGetter> ctx_getter(
125 new TestURLRequestContextGetter());
108 scoped_refptr<HttpBridge> http_bridge(new ShuntedHttpBridge( 126 scoped_refptr<HttpBridge> http_bridge(new ShuntedHttpBridge(
109 ctx, io_thread_loop(), this)); 127 ctx_getter, io_thread_loop(), this));
110 http_bridge->SetUserAgent("bob"); 128 http_bridge->SetUserAgent("bob");
111 http_bridge->SetURL("http://www.google.com", 9999); 129 http_bridge->SetURL("http://www.google.com", 9999);
112 http_bridge->SetPostPayload("text/plain", 2, " "); 130 http_bridge->SetPostPayload("text/plain", 2, " ");
113 131
114 int os_error = 0; 132 int os_error = 0;
115 int response_code = 0; 133 int response_code = 0;
116 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); 134 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code);
117 EXPECT_TRUE(success); 135 EXPECT_TRUE(success);
118 EXPECT_EQ(200, response_code); 136 EXPECT_EQ(200, response_code);
119 EXPECT_EQ(0, os_error); 137 EXPECT_EQ(0, os_error);
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 EXPECT_EQ(0, os_error); 219 EXPECT_EQ(0, os_error);
202 220
203 std::string response(http_bridge->GetResponseContent(), 221 std::string response(http_bridge->GetResponseContent(),
204 http_bridge->GetResponseContentLength()); 222 http_bridge->GetResponseContentLength());
205 223
206 EXPECT_NE(std::string::npos, response.find("fnord")); 224 EXPECT_NE(std::string::npos, response.find("fnord"));
207 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); 225 EXPECT_NE(std::string::npos, response.find(test_payload.c_str()));
208 } 226 }
209 227
210 #endif // defined(BROWSER_SYNC) 228 #endif // defined(BROWSER_SYNC)
OLDNEW
« no previous file with comments | « chrome/browser/sync/glue/http_bridge.cc ('k') | chrome/browser/sync/glue/sync_backend_host.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698