OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/search/iframe_source.h" | 5 #include "chrome/browser/search/iframe_source.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ref_counted_memory.h" | 8 #include "base/memory/ref_counted_memory.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "chrome/browser/search/instant_io_context.h" | 11 #include "chrome/browser/search/instant_io_context.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 #include "content/public/browser/resource_request_info.h" | 13 #include "content/public/browser/resource_request_info.h" |
14 #include "content/public/test/mock_resource_context.h" | 14 #include "content/public/test/mock_resource_context.h" |
15 #include "content/public/test/test_browser_thread.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
16 #include "grit/browser_resources.h" | 16 #include "grit/browser_resources.h" |
17 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
18 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
| 19 #include "net/url_request/url_request_test_util.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 #include "url/gurl.h" | 21 #include "url/gurl.h" |
21 | 22 |
22 const int kNonInstantRendererPID = 0; | 23 const int kNonInstantRendererPID = 0; |
23 const char kNonInstantOrigin[] = "http://evil"; | 24 const char kNonInstantOrigin[] = "http://evil"; |
24 const int kInstantRendererPID = 1; | 25 const int kInstantRendererPID = 1; |
25 const char kInstantOrigin[] = "chrome-search://instant"; | 26 const char kInstantOrigin[] = "chrome-search://instant"; |
26 const int kInvalidRendererPID = 42; | 27 const int kInvalidRendererPID = 42; |
27 | 28 |
28 class TestIframeSource : public IframeSource { | 29 class TestIframeSource : public IframeSource { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 } | 67 } |
67 }; | 68 }; |
68 | 69 |
69 class IframeSourceTest : public testing::Test { | 70 class IframeSourceTest : public testing::Test { |
70 public: | 71 public: |
71 // net::URLRequest wants to be executed with a message loop that has TYPE_IO. | 72 // net::URLRequest wants to be executed with a message loop that has TYPE_IO. |
72 // InstantIOContext needs to be created on the UI thread and have everything | 73 // InstantIOContext needs to be created on the UI thread and have everything |
73 // else happen on the IO thread. This setup is a hacky way to satisfy all | 74 // else happen on the IO thread. This setup is a hacky way to satisfy all |
74 // those constraints. | 75 // those constraints. |
75 IframeSourceTest() | 76 IframeSourceTest() |
76 : message_loop_(base::MessageLoop::TYPE_IO), | 77 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
77 ui_thread_(content::BrowserThread::UI, &message_loop_), | 78 resource_context_(&test_url_request_context_), |
78 io_thread_(content::BrowserThread::IO, &message_loop_), | 79 instant_io_context_(NULL), |
79 instant_io_context_(NULL), | 80 response_(NULL) { |
80 response_(NULL) { | |
81 } | 81 } |
82 | 82 |
83 TestIframeSource* source() { return source_.get(); } | 83 TestIframeSource* source() { return source_.get(); } |
84 | 84 |
85 std::string response_string() { | 85 std::string response_string() { |
86 if (response_.get()) { | 86 if (response_.get()) { |
87 return std::string(reinterpret_cast<const char*>(response_->front()), | 87 return std::string(reinterpret_cast<const char*>(response_->front()), |
88 response_->size()); | 88 response_->size()); |
89 } | 89 } |
90 return ""; | 90 return ""; |
(...skipping 23 matching lines...) Expand all Loading... |
114 | 114 |
115 void SendJSWithOrigin( | 115 void SendJSWithOrigin( |
116 int resource_id, | 116 int resource_id, |
117 int render_process_id, | 117 int render_process_id, |
118 int render_view_id) { | 118 int render_view_id) { |
119 source()->SendJSWithOrigin(resource_id, render_process_id, render_view_id, | 119 source()->SendJSWithOrigin(resource_id, render_process_id, render_view_id, |
120 callback_); | 120 callback_); |
121 } | 121 } |
122 | 122 |
123 private: | 123 private: |
124 virtual void SetUp() { | 124 virtual void SetUp() OVERRIDE { |
125 source_.reset(new TestIframeSource()); | 125 source_.reset(new TestIframeSource()); |
126 callback_ = base::Bind(&IframeSourceTest::SaveResponse, | 126 callback_ = base::Bind(&IframeSourceTest::SaveResponse, |
127 base::Unretained(this)); | 127 base::Unretained(this)); |
128 instant_io_context_ = new InstantIOContext; | 128 instant_io_context_ = new InstantIOContext; |
129 InstantIOContext::SetUserDataOnIO(&resource_context_, instant_io_context_); | 129 InstantIOContext::SetUserDataOnIO(&resource_context_, instant_io_context_); |
130 InstantIOContext::AddInstantProcessOnIO(instant_io_context_, | 130 InstantIOContext::AddInstantProcessOnIO(instant_io_context_, |
131 kInstantRendererPID); | 131 kInstantRendererPID); |
132 response_ = NULL; | 132 response_ = NULL; |
133 } | 133 } |
134 | 134 |
135 virtual void TearDown() { | 135 virtual void TearDown() { |
136 source_.reset(); | 136 source_.reset(); |
137 } | 137 } |
138 | 138 |
139 void SaveResponse(base::RefCountedMemory* data) { | 139 void SaveResponse(base::RefCountedMemory* data) { |
140 response_ = data; | 140 response_ = data; |
141 } | 141 } |
142 | 142 |
143 base::MessageLoop message_loop_; | 143 content::TestBrowserThreadBundle thread_bundle_; |
144 content::TestBrowserThread ui_thread_; | |
145 content::TestBrowserThread io_thread_; | |
146 | 144 |
| 145 net::TestURLRequestContext test_url_request_context_; |
147 content::MockResourceContext resource_context_; | 146 content::MockResourceContext resource_context_; |
148 scoped_ptr<TestIframeSource> source_; | 147 scoped_ptr<TestIframeSource> source_; |
149 content::URLDataSource::GotDataCallback callback_; | 148 content::URLDataSource::GotDataCallback callback_; |
150 scoped_refptr<InstantIOContext> instant_io_context_; | 149 scoped_refptr<InstantIOContext> instant_io_context_; |
151 scoped_refptr<base::RefCountedMemory> response_; | 150 scoped_refptr<base::RefCountedMemory> response_; |
152 }; | 151 }; |
153 | 152 |
154 TEST_F(IframeSourceTest, ShouldServiceRequest) { | 153 TEST_F(IframeSourceTest, ShouldServiceRequest) { |
155 scoped_ptr<net::URLRequest> request; | 154 scoped_ptr<net::URLRequest> request; |
156 request.reset(MockRequest("http://test/loader.js", true, | 155 request.reset(MockRequest("http://test/loader.js", true, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 } | 187 } |
189 | 188 |
190 TEST_F(IframeSourceTest, SendJSWithOrigin) { | 189 TEST_F(IframeSourceTest, SendJSWithOrigin) { |
191 SendJSWithOrigin(IDR_MOST_VISITED_TITLE_JS, kInstantRendererPID, 0); | 190 SendJSWithOrigin(IDR_MOST_VISITED_TITLE_JS, kInstantRendererPID, 0); |
192 EXPECT_FALSE(response_string().empty()); | 191 EXPECT_FALSE(response_string().empty()); |
193 SendJSWithOrigin(IDR_MOST_VISITED_TITLE_JS, kNonInstantRendererPID, 0); | 192 SendJSWithOrigin(IDR_MOST_VISITED_TITLE_JS, kNonInstantRendererPID, 0); |
194 EXPECT_FALSE(response_string().empty()); | 193 EXPECT_FALSE(response_string().empty()); |
195 SendJSWithOrigin(IDR_MOST_VISITED_TITLE_JS, kInvalidRendererPID, 0); | 194 SendJSWithOrigin(IDR_MOST_VISITED_TITLE_JS, kInvalidRendererPID, 0); |
196 EXPECT_TRUE(response_string().empty()); | 195 EXPECT_TRUE(response_string().empty()); |
197 } | 196 } |
OLD | NEW |