Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / search / iframe_source_unittest.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/search/iframe_source.h"
6
7 #include "base/bind.h"
8 #include "base/memory/ref_counted_memory.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "chrome/browser/search/instant_io_context.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/resource_request_info.h"
14 #include "content/public/test/mock_resource_context.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "grit/browser_resources.h"
17 #include "ipc/ipc_message.h"
18 #include "net/base/request_priority.h"
19 #include "net/url_request/url_request.h"
20 #include "net/url_request/url_request_context.h"
21 #include "net/url_request/url_request_test_util.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "url/gurl.h"
24
25 using content::ResourceType;
26
27 const int kNonInstantRendererPID = 0;
28 const char kNonInstantOrigin[] = "http://evil";
29 const int kInstantRendererPID = 1;
30 const char kInstantOrigin[] = "chrome-search://instant";
31 const int kInvalidRendererPID = 42;
32
33 class TestIframeSource : public IframeSource {
34  public:
35   using IframeSource::GetMimeType;
36   using IframeSource::ShouldServiceRequest;
37   using IframeSource::SendResource;
38   using IframeSource::SendJSWithOrigin;
39
40  protected:
41   std::string GetSource() const override { return "test"; }
42
43   bool ServesPath(const std::string& path) const override {
44     return path == "/valid.html" || path == "/valid.js";
45   }
46
47   void StartDataRequest(
48       const std::string& path,
49       int render_process_id,
50       int render_frame_id,
51       const content::URLDataSource::GotDataCallback& callback) override {}
52
53   // RenderFrameHost is hard to mock in concert with everything else, so stub
54   // this method out for testing.
55   bool GetOrigin(int process_id,
56                  int render_frame_id,
57                  std::string* origin) const override {
58     if (process_id == kInstantRendererPID) {
59       *origin = kInstantOrigin;
60       return true;
61     }
62     if (process_id == kNonInstantRendererPID) {
63       *origin = kNonInstantOrigin;
64       return true;
65     }
66     return false;
67   }
68 };
69
70 class IframeSourceTest : public testing::Test {
71  public:
72   // net::URLRequest wants to be executed with a message loop that has TYPE_IO.
73   // InstantIOContext needs to be created on the UI thread and have everything
74   // else happen on the IO thread. This setup is a hacky way to satisfy all
75   // those constraints.
76   IframeSourceTest()
77       : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
78         resource_context_(&test_url_request_context_),
79         instant_io_context_(NULL),
80         response_(NULL) {
81   }
82
83   TestIframeSource* source() { return source_.get(); }
84
85   std::string response_string() {
86     if (response_.get()) {
87       return std::string(response_->front_as<char>(), response_->size());
88     }
89     return "";
90   }
91
92   scoped_ptr<net::URLRequest> MockRequest(
93       const std::string& url,
94       bool allocate_info,
95       int render_process_id,
96       int render_frame_id) {
97     scoped_ptr<net::URLRequest> request(
98         resource_context_.GetRequestContext()->CreateRequest(
99             GURL(url),
100             net::DEFAULT_PRIORITY,
101             NULL,
102             NULL));
103     if (allocate_info) {
104       content::ResourceRequestInfo::AllocateForTesting(
105           request.get(),
106           content::RESOURCE_TYPE_SUB_FRAME,
107           &resource_context_,
108           render_process_id,
109           render_frame_id,
110           MSG_ROUTING_NONE,
111           false);
112     }
113     return request.Pass();
114   }
115
116   void SendResource(int resource_id) {
117     source()->SendResource(resource_id, callback_);
118   }
119
120   void SendJSWithOrigin(
121       int resource_id,
122       int render_process_id,
123       int render_frame_id) {
124     source()->SendJSWithOrigin(resource_id, render_process_id, render_frame_id,
125                                callback_);
126   }
127
128  private:
129   void SetUp() override {
130     source_.reset(new TestIframeSource());
131     callback_ = base::Bind(&IframeSourceTest::SaveResponse,
132                            base::Unretained(this));
133     instant_io_context_ = new InstantIOContext;
134     InstantIOContext::SetUserDataOnIO(&resource_context_, instant_io_context_);
135     InstantIOContext::AddInstantProcessOnIO(instant_io_context_,
136                                             kInstantRendererPID);
137     response_ = NULL;
138   }
139
140   void TearDown() override { source_.reset(); }
141
142   void SaveResponse(base::RefCountedMemory* data) {
143     response_ = data;
144   }
145
146   content::TestBrowserThreadBundle thread_bundle_;
147
148   net::TestURLRequestContext test_url_request_context_;
149   content::MockResourceContext resource_context_;
150   scoped_ptr<TestIframeSource> source_;
151   content::URLDataSource::GotDataCallback callback_;
152   scoped_refptr<InstantIOContext> instant_io_context_;
153   scoped_refptr<base::RefCountedMemory> response_;
154 };
155
156 TEST_F(IframeSourceTest, ShouldServiceRequest) {
157   scoped_ptr<net::URLRequest> request;
158   request = MockRequest("http://test/loader.js", true,
159                         kNonInstantRendererPID, 0);
160   EXPECT_FALSE(source()->ShouldServiceRequest(request.get()));
161   request = MockRequest("chrome-search://bogus/valid.js", true,
162                         kInstantRendererPID, 0);
163   EXPECT_FALSE(source()->ShouldServiceRequest(request.get()));
164   request = MockRequest("chrome-search://test/bogus.js", true,
165                         kInstantRendererPID, 0);
166   EXPECT_FALSE(source()->ShouldServiceRequest(request.get()));
167   request = MockRequest("chrome-search://test/valid.js", true,
168                         kInstantRendererPID, 0);
169   EXPECT_TRUE(source()->ShouldServiceRequest(request.get()));
170   request = MockRequest("chrome-search://test/valid.js", true,
171                         kNonInstantRendererPID, 0);
172   EXPECT_FALSE(source()->ShouldServiceRequest(request.get()));
173   request = MockRequest("chrome-search://test/valid.js", true,
174                         kInvalidRendererPID, 0);
175   EXPECT_FALSE(source()->ShouldServiceRequest(request.get()));
176 }
177
178 TEST_F(IframeSourceTest, GetMimeType) {
179   // URLDataManagerBackend does not include / in path_and_query.
180   EXPECT_EQ("text/html", source()->GetMimeType("foo.html"));
181   EXPECT_EQ("application/javascript", source()->GetMimeType("foo.js"));
182   EXPECT_EQ("text/css", source()->GetMimeType("foo.css"));
183   EXPECT_EQ("image/png", source()->GetMimeType("foo.png"));
184   EXPECT_EQ("", source()->GetMimeType("bogus"));
185 }
186
187 TEST_F(IframeSourceTest, SendResource) {
188   SendResource(IDR_MOST_VISITED_TITLE_HTML);
189   EXPECT_FALSE(response_string().empty());
190 }
191
192 TEST_F(IframeSourceTest, SendJSWithOrigin) {
193   SendJSWithOrigin(IDR_MOST_VISITED_TITLE_JS, kInstantRendererPID, 0);
194   EXPECT_FALSE(response_string().empty());
195   SendJSWithOrigin(IDR_MOST_VISITED_TITLE_JS, kNonInstantRendererPID, 0);
196   EXPECT_FALSE(response_string().empty());
197   SendJSWithOrigin(IDR_MOST_VISITED_TITLE_JS, kInvalidRendererPID, 0);
198   EXPECT_TRUE(response_string().empty());
199 }