Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / pepper_url_request_unittest.cc
1 // Copyright (c) 2012 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 "base/compiler_specific.h"
6 #include "content/public/common/user_agent.h"
7 #include "content/public/test/render_view_test.h"
8 #include "content/renderer/pepper/url_request_info_util.h"
9 #include "ppapi/proxy/connection.h"
10 #include "ppapi/proxy/url_request_info_resource.h"
11 #include "ppapi/shared_impl/proxy_lock.h"
12 #include "ppapi/shared_impl/test_globals.h"
13 #include "ppapi/shared_impl/url_request_info_data.h"
14 #include "ppapi/thunk/thunk.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/WebKit/public/platform/WebURLRequest.h"
17 #include "third_party/WebKit/public/web/WebFrameClient.h"
18 #include "third_party/WebKit/public/web/WebLocalFrame.h"
19 #include "third_party/WebKit/public/web/WebView.h"
20
21 // This test is a end-to-end test from the resource to the WebKit request
22 // object. The actual resource implementation is so simple, it makes sense to
23 // test it by making sure the conversion routines actually work at the same
24 // time.
25
26 using blink::WebCString;
27 using blink::WebFrameClient;
28 using blink::WebString;
29 using blink::WebView;
30 using blink::WebURL;
31 using blink::WebURLRequest;
32
33 namespace {
34
35 bool IsExpected(const WebCString& web_string, const char* expected) {
36   const char* result = web_string.data();
37   return strcmp(result, expected) == 0;
38 }
39
40 bool IsExpected(const WebString& web_string, const char* expected) {
41   return IsExpected(web_string.utf8(), expected);
42 }
43
44 // The base class destructor is protected, so derive.
45 class TestWebFrameClient : public WebFrameClient {};
46
47 }  // namespace
48
49 using ppapi::proxy::URLRequestInfoResource;
50 using ppapi::URLRequestInfoData;
51
52 namespace content {
53
54 class URLRequestInfoTest : public RenderViewTest {
55  public:
56   // Note: using -1 as the instance value allows code in
57   // url_request_info_util.cc to detect that this is a test instance.
58   URLRequestInfoTest() : pp_instance_(-1) {}
59
60   virtual void SetUp() OVERRIDE {
61     RenderViewTest::SetUp();
62     ppapi::ProxyLock::DisableLockingOnThreadForTest();
63
64     test_globals_.GetResourceTracker()->DidCreateInstance(pp_instance_);
65
66     // This resource doesn't do IPC, so a null connection is fine.
67     info_ = new URLRequestInfoResource(
68         ppapi::proxy::Connection(), pp_instance_, URLRequestInfoData());
69   }
70
71   virtual void TearDown() OVERRIDE {
72     test_globals_.GetResourceTracker()->DidDeleteInstance(pp_instance_);
73     RenderViewTest::TearDown();
74   }
75
76   bool GetDownloadToFile() {
77     WebURLRequest web_request;
78     URLRequestInfoData data = info_->GetData();
79     if (!CreateWebURLRequest(pp_instance_, &data, GetMainFrame(), &web_request))
80       return false;
81     return web_request.downloadToFile();
82   }
83
84   WebCString GetURL() {
85     WebURLRequest web_request;
86     URLRequestInfoData data = info_->GetData();
87     if (!CreateWebURLRequest(pp_instance_, &data, GetMainFrame(), &web_request))
88       return WebCString();
89     return web_request.url().spec();
90   }
91
92   WebString GetMethod() {
93     WebURLRequest web_request;
94     URLRequestInfoData data = info_->GetData();
95     if (!CreateWebURLRequest(pp_instance_, &data, GetMainFrame(), &web_request))
96       return WebString();
97     return web_request.httpMethod();
98   }
99
100   WebString GetHeaderValue(const char* field) {
101     WebURLRequest web_request;
102     URLRequestInfoData data = info_->GetData();
103     if (!CreateWebURLRequest(pp_instance_, &data, GetMainFrame(), &web_request))
104       return WebString();
105     return web_request.httpHeaderField(WebString::fromUTF8(field));
106   }
107
108   bool SetBooleanProperty(PP_URLRequestProperty prop, bool b) {
109     return info_->SetBooleanProperty(prop, b);
110   }
111   bool SetStringProperty(PP_URLRequestProperty prop, const std::string& s) {
112     return info_->SetStringProperty(prop, s);
113   }
114
115   PP_Instance pp_instance_;
116
117   // Needs to be alive for resource tracking to work.
118   ppapi::TestGlobals test_globals_;
119
120   scoped_refptr<URLRequestInfoResource> info_;
121 };
122
123 TEST_F(URLRequestInfoTest, GetInterface) {
124   const PPB_URLRequestInfo* request_info =
125       ppapi::thunk::GetPPB_URLRequestInfo_1_0_Thunk();
126   EXPECT_TRUE(request_info);
127   EXPECT_TRUE(request_info->Create);
128   EXPECT_TRUE(request_info->IsURLRequestInfo);
129   EXPECT_TRUE(request_info->SetProperty);
130   EXPECT_TRUE(request_info->AppendDataToBody);
131   EXPECT_TRUE(request_info->AppendFileToBody);
132 }
133
134 TEST_F(URLRequestInfoTest, AsURLRequestInfo) {
135   EXPECT_EQ(info_.get(), info_->AsPPB_URLRequestInfo_API());
136 }
137
138 TEST_F(URLRequestInfoTest, StreamToFile) {
139   SetStringProperty(PP_URLREQUESTPROPERTY_URL, "http://www.google.com");
140
141   EXPECT_FALSE(GetDownloadToFile());
142
143   EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_STREAMTOFILE, true));
144   EXPECT_TRUE(GetDownloadToFile());
145
146   EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_STREAMTOFILE, false));
147   EXPECT_FALSE(GetDownloadToFile());
148 }
149
150 TEST_F(URLRequestInfoTest, FollowRedirects) {
151   EXPECT_TRUE(info_->GetData().follow_redirects);
152
153   EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS, false));
154   EXPECT_FALSE(info_->GetData().follow_redirects);
155
156   EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS, true));
157   EXPECT_TRUE(info_->GetData().follow_redirects);
158 }
159
160 TEST_F(URLRequestInfoTest, RecordDownloadProgress) {
161   EXPECT_FALSE(info_->GetData().record_download_progress);
162
163   EXPECT_TRUE(
164       SetBooleanProperty(PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS, true));
165   EXPECT_TRUE(info_->GetData().record_download_progress);
166
167   EXPECT_TRUE(
168       SetBooleanProperty(PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS, false));
169   EXPECT_FALSE(info_->GetData().record_download_progress);
170 }
171
172 TEST_F(URLRequestInfoTest, RecordUploadProgress) {
173   EXPECT_FALSE(info_->GetData().record_upload_progress);
174
175   EXPECT_TRUE(
176       SetBooleanProperty(PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS, true));
177   EXPECT_TRUE(info_->GetData().record_upload_progress);
178
179   EXPECT_TRUE(
180       SetBooleanProperty(PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS, false));
181   EXPECT_FALSE(info_->GetData().record_upload_progress);
182 }
183
184 TEST_F(URLRequestInfoTest, AllowCrossOriginRequests) {
185   EXPECT_FALSE(info_->GetData().allow_cross_origin_requests);
186
187   EXPECT_TRUE(
188       SetBooleanProperty(PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS, true));
189   EXPECT_TRUE(info_->GetData().allow_cross_origin_requests);
190
191   EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS,
192                                  false));
193   EXPECT_FALSE(info_->GetData().allow_cross_origin_requests);
194 }
195
196 TEST_F(URLRequestInfoTest, AllowCredentials) {
197   EXPECT_FALSE(info_->GetData().allow_credentials);
198
199   EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS, true));
200   EXPECT_TRUE(info_->GetData().allow_credentials);
201
202   EXPECT_TRUE(
203       SetBooleanProperty(PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS, false));
204   EXPECT_FALSE(info_->GetData().allow_credentials);
205 }
206
207 TEST_F(URLRequestInfoTest, SetURL) {
208   const char* url = "http://www.google.com/";
209   EXPECT_TRUE(SetStringProperty(PP_URLREQUESTPROPERTY_URL, url));
210   EXPECT_TRUE(IsExpected(GetURL(), url));
211 }
212
213 TEST_F(URLRequestInfoTest, JavascriptURL) {
214   const char* url = "javascript:foo = bar";
215   EXPECT_FALSE(URLRequestRequiresUniversalAccess(info_->GetData()));
216   SetStringProperty(PP_URLREQUESTPROPERTY_URL, url);
217   EXPECT_TRUE(URLRequestRequiresUniversalAccess(info_->GetData()));
218 }
219
220 TEST_F(URLRequestInfoTest, SetMethod) {
221   // Test default method is "GET".
222   EXPECT_TRUE(IsExpected(GetMethod(), "GET"));
223   EXPECT_TRUE(SetStringProperty(PP_URLREQUESTPROPERTY_METHOD, "POST"));
224   EXPECT_TRUE(IsExpected(GetMethod(), "POST"));
225 }
226
227 TEST_F(URLRequestInfoTest, SetHeaders) {
228   // Test default header field.
229   EXPECT_TRUE(IsExpected(GetHeaderValue("foo"), ""));
230   // Test that we can set a header field.
231   EXPECT_TRUE(SetStringProperty(PP_URLREQUESTPROPERTY_HEADERS, "foo: bar"));
232   EXPECT_TRUE(IsExpected(GetHeaderValue("foo"), "bar"));
233   // Test that we can set multiple header fields using \n delimiter.
234   EXPECT_TRUE(
235       SetStringProperty(PP_URLREQUESTPROPERTY_HEADERS, "foo: bar\nbar: baz"));
236   EXPECT_TRUE(IsExpected(GetHeaderValue("foo"), "bar"));
237   EXPECT_TRUE(IsExpected(GetHeaderValue("bar"), "baz"));
238 }
239
240 // TODO(bbudge) Unit tests for AppendDataToBody, AppendFileToBody.
241
242 }  // namespace content