OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "modules/background_fetch/BackgroundFetchManager.h" |
| 6 |
| 7 #include "bindings/core/v8/Dictionary.h" |
| 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "bindings/core/v8/ScriptState.h" |
| 10 #include "bindings/core/v8/V8Binding.h" |
| 11 #include "bindings/core/v8/V8BindingForTesting.h" |
| 12 #include "bindings/modules/v8/RequestOrUSVString.h" |
| 13 #include "bindings/modules/v8/RequestOrUSVStringOrRequestOrUSVStringSequence.h" |
| 14 #include "core/dom/ExceptionCode.h" |
| 15 #include "modules/fetch/Request.h" |
| 16 #include "public/platform/modules/serviceworker/WebServiceWorkerRequest.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace blink { |
| 20 |
| 21 class BackgroundFetchManagerTest : public ::testing::Test { |
| 22 protected: |
| 23 // Creates a vector of WebServiceWorkerRequest entries for the given |
| 24 // |requests| based on the |scope|. Proxied in the fixture to reduce the |
| 25 // number of friend declarations necessary in the BackgroundFetchManager. |
| 26 Vector<WebServiceWorkerRequest> createWebRequestVector( |
| 27 V8TestingScope& scope, |
| 28 const RequestOrUSVStringOrRequestOrUSVStringSequence& requests) { |
| 29 return BackgroundFetchManager::createWebRequestVector( |
| 30 scope.getScriptState(), requests, scope.getExceptionState()); |
| 31 } |
| 32 |
| 33 // Returns a Dictionary object that represents a JavaScript dictionary with |
| 34 // a single key-value pair, where the key always is "method" with the value |
| 35 // set to |method|. |
| 36 Dictionary getDictionaryForMethod(V8TestingScope& scope, const char* method) { |
| 37 v8::Isolate* isolate = scope.isolate(); |
| 38 v8::Local<v8::Object> data = v8::Object::New(isolate); |
| 39 |
| 40 data->Set(isolate->GetCurrentContext(), v8String(isolate, "method"), |
| 41 v8String(isolate, method)) |
| 42 .ToChecked(); |
| 43 |
| 44 return Dictionary(scope.isolate(), data, scope.getExceptionState()); |
| 45 } |
| 46 }; |
| 47 |
| 48 TEST_F(BackgroundFetchManagerTest, NullValue) { |
| 49 V8TestingScope scope; |
| 50 |
| 51 RequestOrUSVStringOrRequestOrUSVStringSequence requests; |
| 52 |
| 53 Vector<WebServiceWorkerRequest> webRequests = |
| 54 createWebRequestVector(scope, requests); |
| 55 ASSERT_TRUE(scope.getExceptionState().hadException()); |
| 56 EXPECT_EQ(scope.getExceptionState().code(), V8TypeError); |
| 57 } |
| 58 |
| 59 TEST_F(BackgroundFetchManagerTest, SingleUSVString) { |
| 60 V8TestingScope scope; |
| 61 |
| 62 KURL imageUrl(ParsedURLString, "https://www.example.com/my_image.png"); |
| 63 |
| 64 RequestOrUSVStringOrRequestOrUSVStringSequence requests = |
| 65 RequestOrUSVStringOrRequestOrUSVStringSequence::fromUSVString( |
| 66 imageUrl.getString()); |
| 67 |
| 68 Vector<WebServiceWorkerRequest> webRequests = |
| 69 createWebRequestVector(scope, requests); |
| 70 ASSERT_FALSE(scope.getExceptionState().hadException()); |
| 71 |
| 72 ASSERT_EQ(webRequests.size(), 1u); |
| 73 |
| 74 WebServiceWorkerRequest& webRequest = webRequests[0]; |
| 75 EXPECT_EQ(webRequest.url(), WebURL(imageUrl)); |
| 76 EXPECT_EQ(webRequest.method(), "GET"); |
| 77 } |
| 78 |
| 79 TEST_F(BackgroundFetchManagerTest, SingleRequest) { |
| 80 V8TestingScope scope; |
| 81 |
| 82 KURL imageUrl(ParsedURLString, "https://www.example.com/my_image.png"); |
| 83 |
| 84 Request* request = Request::create( |
| 85 scope.getScriptState(), imageUrl.getString(), |
| 86 getDictionaryForMethod(scope, "POST"), scope.getExceptionState()); |
| 87 ASSERT_FALSE(scope.getExceptionState().hadException()); |
| 88 ASSERT_TRUE(request); |
| 89 |
| 90 RequestOrUSVStringOrRequestOrUSVStringSequence requests = |
| 91 RequestOrUSVStringOrRequestOrUSVStringSequence::fromRequest(request); |
| 92 |
| 93 Vector<WebServiceWorkerRequest> webRequests = |
| 94 createWebRequestVector(scope, requests); |
| 95 ASSERT_FALSE(scope.getExceptionState().hadException()); |
| 96 |
| 97 ASSERT_EQ(webRequests.size(), 1u); |
| 98 |
| 99 WebServiceWorkerRequest& webRequest = webRequests[0]; |
| 100 EXPECT_EQ(webRequest.url(), WebURL(imageUrl)); |
| 101 EXPECT_EQ(webRequest.method(), "POST"); |
| 102 } |
| 103 |
| 104 TEST_F(BackgroundFetchManagerTest, Sequence) { |
| 105 V8TestingScope scope; |
| 106 |
| 107 KURL imageUrl(ParsedURLString, "https://www.example.com/my_image.png"); |
| 108 KURL iconUrl(ParsedURLString, "https://www.example.com/my_icon.jpg"); |
| 109 KURL catVideoUrl(ParsedURLString, "https://www.example.com/my_cat_video.avi"); |
| 110 |
| 111 RequestOrUSVString imageRequest = |
| 112 RequestOrUSVString::fromUSVString(imageUrl.getString()); |
| 113 RequestOrUSVString iconRequest = |
| 114 RequestOrUSVString::fromUSVString(iconUrl.getString()); |
| 115 |
| 116 Request* request = Request::create( |
| 117 scope.getScriptState(), catVideoUrl.getString(), |
| 118 getDictionaryForMethod(scope, "DELETE"), scope.getExceptionState()); |
| 119 ASSERT_FALSE(scope.getExceptionState().hadException()); |
| 120 ASSERT_TRUE(request); |
| 121 |
| 122 RequestOrUSVString catVideoRequest = RequestOrUSVString::fromRequest(request); |
| 123 |
| 124 HeapVector<RequestOrUSVString> requestSequence; |
| 125 requestSequence.push_back(imageRequest); |
| 126 requestSequence.push_back(iconRequest); |
| 127 requestSequence.push_back(catVideoRequest); |
| 128 |
| 129 RequestOrUSVStringOrRequestOrUSVStringSequence requests = |
| 130 RequestOrUSVStringOrRequestOrUSVStringSequence:: |
| 131 fromRequestOrUSVStringSequence(requestSequence); |
| 132 |
| 133 Vector<WebServiceWorkerRequest> webRequests = |
| 134 createWebRequestVector(scope, requests); |
| 135 ASSERT_FALSE(scope.getExceptionState().hadException()); |
| 136 |
| 137 ASSERT_EQ(webRequests.size(), 3u); |
| 138 EXPECT_EQ(webRequests[0].url(), WebURL(imageUrl)); |
| 139 EXPECT_EQ(webRequests[0].method(), "GET"); |
| 140 |
| 141 EXPECT_EQ(webRequests[1].url(), WebURL(iconUrl)); |
| 142 EXPECT_EQ(webRequests[1].method(), "GET"); |
| 143 |
| 144 EXPECT_EQ(webRequests[2].url(), WebURL(catVideoUrl)); |
| 145 EXPECT_EQ(webRequests[2].method(), "DELETE"); |
| 146 } |
| 147 |
| 148 TEST_F(BackgroundFetchManagerTest, SequenceEmpty) { |
| 149 V8TestingScope scope; |
| 150 |
| 151 HeapVector<RequestOrUSVString> requestSequence; |
| 152 RequestOrUSVStringOrRequestOrUSVStringSequence requests = |
| 153 RequestOrUSVStringOrRequestOrUSVStringSequence:: |
| 154 fromRequestOrUSVStringSequence(requestSequence); |
| 155 |
| 156 Vector<WebServiceWorkerRequest> webRequests = |
| 157 createWebRequestVector(scope, requests); |
| 158 ASSERT_TRUE(scope.getExceptionState().hadException()); |
| 159 EXPECT_EQ(scope.getExceptionState().code(), V8TypeError); |
| 160 } |
| 161 |
| 162 TEST_F(BackgroundFetchManagerTest, SequenceWithNullValue) { |
| 163 V8TestingScope scope; |
| 164 |
| 165 KURL imageUrl(ParsedURLString, "https://www.example.com/my_image.png"); |
| 166 |
| 167 RequestOrUSVString nullRequest; |
| 168 RequestOrUSVString imageRequest = |
| 169 RequestOrUSVString::fromUSVString(imageUrl.getString()); |
| 170 |
| 171 HeapVector<RequestOrUSVString> requestSequence; |
| 172 requestSequence.push_back(imageRequest); |
| 173 requestSequence.push_back(nullRequest); |
| 174 |
| 175 RequestOrUSVStringOrRequestOrUSVStringSequence requests = |
| 176 RequestOrUSVStringOrRequestOrUSVStringSequence:: |
| 177 fromRequestOrUSVStringSequence(requestSequence); |
| 178 |
| 179 Vector<WebServiceWorkerRequest> webRequests = |
| 180 createWebRequestVector(scope, requests); |
| 181 ASSERT_TRUE(scope.getExceptionState().hadException()); |
| 182 EXPECT_EQ(scope.getExceptionState().code(), V8TypeError); |
| 183 } |
| 184 |
| 185 } // namespace blink |
OLD | NEW |