| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/compiler_specific.h" |
| 6 #include "content/public/test/render_view_test.h" |
| 7 #include "ppapi/proxy/connection.h" |
| 8 #include "ppapi/proxy/url_request_info_resource.h" |
| 9 #include "ppapi/shared_impl/test_globals.h" |
| 10 #include "ppapi/shared_impl/url_request_info_data.h" |
| 5 #include "ppapi/thunk/thunk.h" | 11 #include "ppapi/thunk/thunk.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 6 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 7 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrameClient.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrameClient.h" |
| 8 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" |
| 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 10 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 17 #include "webkit/plugins/ppapi/url_request_info_util.h" |
| 11 #include "webkit/plugins/ppapi/ppb_url_request_info_impl.h" | |
| 12 #include "webkit/plugins/ppapi/ppapi_unittest.h" | |
| 13 #include "webkit/user_agent/user_agent.h" | 18 #include "webkit/user_agent/user_agent.h" |
| 14 #include "webkit/user_agent/user_agent_util.h" | 19 #include "webkit/user_agent/user_agent_util.h" |
| 15 | 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 |
| 16 using WebKit::WebCString; | 26 using WebKit::WebCString; |
| 17 using WebKit::WebFrame; | 27 using WebKit::WebFrame; |
| 18 using WebKit::WebFrameClient; | 28 using WebKit::WebFrameClient; |
| 19 using WebKit::WebString; | 29 using WebKit::WebString; |
| 20 using WebKit::WebView; | 30 using WebKit::WebView; |
| 21 using WebKit::WebURL; | 31 using WebKit::WebURL; |
| 22 using WebKit::WebURLRequest; | 32 using WebKit::WebURLRequest; |
| 23 | 33 |
| 24 namespace { | 34 namespace { |
| 25 | 35 |
| 26 bool IsExpected(const WebCString& web_string, const char* expected) { | 36 bool IsExpected(const WebCString& web_string, const char* expected) { |
| 27 const char* result = web_string.data(); | 37 const char* result = web_string.data(); |
| 28 return strcmp(result, expected) == 0; | 38 return strcmp(result, expected) == 0; |
| 29 } | 39 } |
| 30 | 40 |
| 31 bool IsExpected(const WebString& web_string, const char* expected) { | 41 bool IsExpected(const WebString& web_string, const char* expected) { |
| 32 return IsExpected(web_string.utf8(), expected); | 42 return IsExpected(web_string.utf8(), expected); |
| 33 } | 43 } |
| 34 | 44 |
| 35 bool IsNullOrEmpty(const WebString& web_string) { | 45 bool IsNullOrEmpty(const WebString& web_string) { |
| 36 return web_string.isNull() || web_string.isEmpty(); | 46 return web_string.isNull() || web_string.isEmpty(); |
| 37 } | 47 } |
| 38 | 48 |
| 39 // The base class destructor is protected, so derive. | 49 // The base class destructor is protected, so derive. |
| 40 class TestWebFrameClient : public WebFrameClient { | 50 class TestWebFrameClient : public WebFrameClient { |
| 41 }; | 51 }; |
| 42 | 52 |
| 43 } // namespace | 53 } // namespace |
| 44 | 54 |
| 55 using ppapi::proxy::URLRequestInfoResource; |
| 56 using ppapi::URLRequestInfoData; |
| 57 |
| 58 // TODO(brettw) move to content namespace when url_request_info_util.h is moved |
| 59 // to this directory. This file used to be in webkit/plugins/ppapi and had to |
| 60 // be moved in advance of the rest of the files to make things compile. |
| 45 namespace webkit { | 61 namespace webkit { |
| 46 namespace ppapi { | 62 namespace ppapi { |
| 47 | 63 |
| 48 class URLRequestInfoTest : public PpapiUnittest { | 64 class URLRequestInfoTest : public content::RenderViewTest { |
| 49 public: | 65 public: |
| 50 URLRequestInfoTest() { | 66 URLRequestInfoTest() : pp_instance_(1234) { |
| 51 } | 67 } |
| 52 | 68 |
| 53 virtual void SetUp() { | 69 virtual void SetUp() OVERRIDE { |
| 54 PpapiUnittest::SetUp(); | 70 RenderViewTest::SetUp(); |
| 55 | 71 |
| 56 // Must be after our base class's SetUp for the instance to be valid. | 72 test_globals_.GetResourceTracker()->DidCreateInstance(pp_instance_); |
| 57 info_ = new PPB_URLRequestInfo_Impl(instance()->pp_instance(), | 73 |
| 58 ::ppapi::PPB_URLRequestInfo_Data()); | 74 // This resource doesn't do IPC, so a null connection is fine. |
| 75 info_ = new URLRequestInfoResource(::ppapi::proxy::Connection(), |
| 76 pp_instance_, |
| 77 URLRequestInfoData()); |
| 59 } | 78 } |
| 60 | 79 |
| 61 static void SetUpTestCase() { | 80 virtual void TearDown() OVERRIDE { |
| 62 webkit_glue::SetUserAgent(webkit_glue::BuildUserAgentFromProduct( | 81 test_globals_.GetResourceTracker()->DidDeleteInstance(pp_instance_); |
| 63 "TestShell/0.0.0.0"), false); | 82 RenderViewTest::TearDown(); |
| 64 web_view_ = WebView::create(NULL); | |
| 65 web_view_->initializeMainFrame(&web_frame_client_); | |
| 66 WebURL web_url(GURL("")); | |
| 67 WebURLRequest url_request; | |
| 68 url_request.initialize(); | |
| 69 url_request.setURL(web_url); | |
| 70 frame_ = web_view_->mainFrame(); | |
| 71 frame_->loadRequest(url_request); | |
| 72 } | |
| 73 | |
| 74 static void TearDownTestCase() { | |
| 75 web_view_->close(); | |
| 76 } | 83 } |
| 77 | 84 |
| 78 bool GetDownloadToFile() { | 85 bool GetDownloadToFile() { |
| 79 WebURLRequest web_request; | 86 WebURLRequest web_request; |
| 80 if (!info_->ToWebURLRequest(frame_, &web_request)) | 87 URLRequestInfoData data = info_->GetData(); |
| 88 if (!CreateWebURLRequest(&data, GetMainFrame(), &web_request)) |
| 81 return false; | 89 return false; |
| 82 return web_request.downloadToFile(); | 90 return web_request.downloadToFile(); |
| 83 } | 91 } |
| 84 | 92 |
| 85 WebCString GetURL() { | 93 WebCString GetURL() { |
| 86 WebURLRequest web_request; | 94 WebURLRequest web_request; |
| 87 if (!info_->ToWebURLRequest(frame_, &web_request)) | 95 URLRequestInfoData data = info_->GetData(); |
| 96 if (!CreateWebURLRequest(&data, GetMainFrame(), &web_request)) |
| 88 return WebCString(); | 97 return WebCString(); |
| 89 return web_request.url().spec(); | 98 return web_request.url().spec(); |
| 90 } | 99 } |
| 91 | 100 |
| 92 WebString GetMethod() { | 101 WebString GetMethod() { |
| 93 WebURLRequest web_request; | 102 WebURLRequest web_request; |
| 94 if (!info_->ToWebURLRequest(frame_, &web_request)) | 103 URLRequestInfoData data = info_->GetData(); |
| 104 if (!CreateWebURLRequest(&data, GetMainFrame(), &web_request)) |
| 95 return WebString(); | 105 return WebString(); |
| 96 return web_request.httpMethod(); | 106 return web_request.httpMethod(); |
| 97 } | 107 } |
| 98 | 108 |
| 99 WebString GetHeaderValue(const char* field) { | 109 WebString GetHeaderValue(const char* field) { |
| 100 WebURLRequest web_request; | 110 WebURLRequest web_request; |
| 101 if (!info_->ToWebURLRequest(frame_, &web_request)) | 111 URLRequestInfoData data = info_->GetData(); |
| 112 if (!CreateWebURLRequest(&data, GetMainFrame(), &web_request)) |
| 102 return WebString(); | 113 return WebString(); |
| 103 return web_request.httpHeaderField(WebString::fromUTF8(field)); | 114 return web_request.httpHeaderField(WebString::fromUTF8(field)); |
| 104 } | 115 } |
| 105 | 116 |
| 106 bool SetBooleanProperty(PP_URLRequestProperty prop, bool b) { | 117 bool SetBooleanProperty(PP_URLRequestProperty prop, bool b) { |
| 107 return info_->SetBooleanProperty(prop, b); | 118 return info_->SetBooleanProperty(prop, b); |
| 108 } | 119 } |
| 109 bool SetStringProperty(PP_URLRequestProperty prop, const std::string& s) { | 120 bool SetStringProperty(PP_URLRequestProperty prop, const std::string& s) { |
| 110 return info_->SetStringProperty(prop, s); | 121 return info_->SetStringProperty(prop, s); |
| 111 } | 122 } |
| 112 | 123 |
| 113 scoped_refptr<PPB_URLRequestInfo_Impl> info_; | 124 PP_Instance pp_instance_; |
| 114 | 125 |
| 115 static TestWebFrameClient web_frame_client_; | 126 // Needs to be alive for resource tracking to work. |
| 116 static WebView* web_view_; | 127 ::ppapi::TestGlobals test_globals_; |
| 117 static WebFrame* frame_; | 128 |
| 129 scoped_refptr<URLRequestInfoResource> info_; |
| 118 }; | 130 }; |
| 119 | 131 |
| 120 TestWebFrameClient URLRequestInfoTest::web_frame_client_; | |
| 121 WebView* URLRequestInfoTest::web_view_; | |
| 122 WebFrame* URLRequestInfoTest::frame_; | |
| 123 | |
| 124 TEST_F(URLRequestInfoTest, GetInterface) { | 132 TEST_F(URLRequestInfoTest, GetInterface) { |
| 125 const PPB_URLRequestInfo* request_info = | 133 const PPB_URLRequestInfo* request_info = |
| 126 ::ppapi::thunk::GetPPB_URLRequestInfo_1_0_Thunk(); | 134 ::ppapi::thunk::GetPPB_URLRequestInfo_1_0_Thunk(); |
| 127 EXPECT_TRUE(request_info); | 135 EXPECT_TRUE(request_info); |
| 128 EXPECT_TRUE(request_info->Create); | 136 EXPECT_TRUE(request_info->Create); |
| 129 EXPECT_TRUE(request_info->IsURLRequestInfo); | 137 EXPECT_TRUE(request_info->IsURLRequestInfo); |
| 130 EXPECT_TRUE(request_info->SetProperty); | 138 EXPECT_TRUE(request_info->SetProperty); |
| 131 EXPECT_TRUE(request_info->AppendDataToBody); | 139 EXPECT_TRUE(request_info->AppendDataToBody); |
| 132 EXPECT_TRUE(request_info->AppendFileToBody); | 140 EXPECT_TRUE(request_info->AppendFileToBody); |
| 133 } | 141 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 EXPECT_TRUE(SetBooleanProperty( | 212 EXPECT_TRUE(SetBooleanProperty( |
| 205 PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS, true)); | 213 PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS, true)); |
| 206 EXPECT_TRUE(info_->GetData().allow_credentials); | 214 EXPECT_TRUE(info_->GetData().allow_credentials); |
| 207 | 215 |
| 208 EXPECT_TRUE(SetBooleanProperty( | 216 EXPECT_TRUE(SetBooleanProperty( |
| 209 PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS, false)); | 217 PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS, false)); |
| 210 EXPECT_FALSE(info_->GetData().allow_credentials); | 218 EXPECT_FALSE(info_->GetData().allow_credentials); |
| 211 } | 219 } |
| 212 | 220 |
| 213 TEST_F(URLRequestInfoTest, SetURL) { | 221 TEST_F(URLRequestInfoTest, SetURL) { |
| 214 // Test default URL is "about:blank". | |
| 215 EXPECT_TRUE(IsExpected(GetURL(), "about:blank")); | |
| 216 | |
| 217 const char* url = "http://www.google.com/"; | 222 const char* url = "http://www.google.com/"; |
| 218 EXPECT_TRUE(SetStringProperty( | 223 EXPECT_TRUE(SetStringProperty( |
| 219 PP_URLREQUESTPROPERTY_URL, url)); | 224 PP_URLREQUESTPROPERTY_URL, url)); |
| 220 EXPECT_TRUE(IsExpected(GetURL(), url)); | 225 EXPECT_TRUE(IsExpected(GetURL(), url)); |
| 221 } | 226 } |
| 222 | 227 |
| 223 TEST_F(URLRequestInfoTest, JavascriptURL) { | 228 TEST_F(URLRequestInfoTest, JavascriptURL) { |
| 224 const char* url = "javascript:foo = bar"; | 229 const char* url = "javascript:foo = bar"; |
| 225 EXPECT_FALSE(info_->RequiresUniversalAccess()); | 230 EXPECT_FALSE(URLRequestRequiresUniversalAccess(info_->GetData())); |
| 226 SetStringProperty(PP_URLREQUESTPROPERTY_URL, url); | 231 SetStringProperty(PP_URLREQUESTPROPERTY_URL, url); |
| 227 EXPECT_TRUE(info_->RequiresUniversalAccess()); | 232 EXPECT_TRUE(URLRequestRequiresUniversalAccess(info_->GetData())); |
| 228 } | 233 } |
| 229 | 234 |
| 230 TEST_F(URLRequestInfoTest, SetMethod) { | 235 TEST_F(URLRequestInfoTest, SetMethod) { |
| 231 // Test default method is "GET". | 236 // Test default method is "GET". |
| 232 EXPECT_TRUE(IsExpected(GetMethod(), "GET")); | 237 EXPECT_TRUE(IsExpected(GetMethod(), "GET")); |
| 233 EXPECT_TRUE(SetStringProperty( | 238 EXPECT_TRUE(SetStringProperty( |
| 234 PP_URLREQUESTPROPERTY_METHOD, "POST")); | 239 PP_URLREQUESTPROPERTY_METHOD, "POST")); |
| 235 EXPECT_TRUE(IsExpected(GetMethod(), "POST")); | 240 EXPECT_TRUE(IsExpected(GetMethod(), "POST")); |
| 236 } | 241 } |
| 237 | 242 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 250 EXPECT_TRUE(IsExpected( | 255 EXPECT_TRUE(IsExpected( |
| 251 GetHeaderValue("foo"), "bar")); | 256 GetHeaderValue("foo"), "bar")); |
| 252 EXPECT_TRUE(IsExpected( | 257 EXPECT_TRUE(IsExpected( |
| 253 GetHeaderValue("bar"), "baz")); | 258 GetHeaderValue("bar"), "baz")); |
| 254 } | 259 } |
| 255 | 260 |
| 256 // TODO(bbudge) Unit tests for AppendDataToBody, AppendFileToBody. | 261 // TODO(bbudge) Unit tests for AppendDataToBody, AppendFileToBody. |
| 257 | 262 |
| 258 } // namespace ppapi | 263 } // namespace ppapi |
| 259 } // namespace webkit | 264 } // namespace webkit |
| OLD | NEW |