| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_TEST_BASE_H_ | 5 #ifndef CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_TEST_BASE_H_ |
| 6 #define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_TEST_BASE_H_ | 6 #define CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_TEST_BASE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "content/browser/background_fetch/background_fetch_embedded_worker_test
_helper.h" | 14 #include "content/browser/background_fetch/background_fetch_embedded_worker_test
_helper.h" |
| 15 #include "content/common/service_worker/service_worker_types.h" | 15 #include "content/common/service_worker/service_worker_types.h" |
| 16 #include "content/public/test/test_browser_context.h" | 16 #include "content/public/test/test_browser_context.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "url/origin.h" | 19 #include "url/origin.h" |
| 20 | 20 |
| 21 namespace net { |
| 22 class HttpResponseHeaders; |
| 23 } |
| 24 |
| 21 namespace content { | 25 namespace content { |
| 22 | 26 |
| 23 class BackgroundFetchRegistrationId; | 27 class BackgroundFetchRegistrationId; |
| 24 class MockDownloadManager; | 28 class MockDownloadManager; |
| 25 class ServiceWorkerRegistration; | 29 class ServiceWorkerRegistration; |
| 26 | 30 |
| 27 // Base class containing common functionality needed in unit tests written for | 31 // Base class containing common functionality needed in unit tests written for |
| 28 // the Background Fetch feature. | 32 // the Background Fetch feature. |
| 29 class BackgroundFetchTestBase : public ::testing::Test { | 33 class BackgroundFetchTestBase : public ::testing::Test { |
| 30 public: | 34 public: |
| 31 BackgroundFetchTestBase(); | 35 BackgroundFetchTestBase(); |
| 32 ~BackgroundFetchTestBase() override; | 36 ~BackgroundFetchTestBase() override; |
| 33 | 37 |
| 34 // ::testing::Test overrides. | 38 // ::testing::Test overrides. |
| 35 void SetUp() override; | 39 void SetUp() override; |
| 36 void TearDown() override; | 40 void TearDown() override; |
| 37 | 41 |
| 42 // Structure encapsulating the data for a injected response. Should only be |
| 43 // created by the builder, which also defines the ownership semantics. |
| 44 struct TestResponse { |
| 45 TestResponse(); |
| 46 ~TestResponse(); |
| 47 |
| 48 scoped_refptr<net::HttpResponseHeaders> headers; |
| 49 std::string data; |
| 50 }; |
| 51 |
| 52 // Builder for creating a TestResponse object with the given data. The faked |
| 53 // download manager will respond to the corresponding request based on this. |
| 54 class TestResponseBuilder { |
| 55 public: |
| 56 explicit TestResponseBuilder(int response_code); |
| 57 ~TestResponseBuilder(); |
| 58 |
| 59 TestResponseBuilder& AddResponseHeader(const std::string& name, |
| 60 const std::string& value); |
| 61 TestResponseBuilder& SetResponseData(std::string data); |
| 62 |
| 63 // Finalizes the builder and invalidates the underlying response. |
| 64 std::unique_ptr<TestResponse> Build(); |
| 65 |
| 66 private: |
| 67 std::unique_ptr<TestResponse> response_; |
| 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(TestResponseBuilder); |
| 70 }; |
| 71 |
| 38 // Creates a Background Fetch registration backed by a Service Worker | 72 // Creates a Background Fetch registration backed by a Service Worker |
| 39 // registration for the testing origin. The resulting registration will be | 73 // registration for the testing origin. The resulting registration will be |
| 40 // stored in |*registration_id|. Returns whether creation was successful, | 74 // stored in |*registration_id|. Returns whether creation was successful, |
| 41 // which must be asserted by tests. The ServiceWorkerRegistration that | 75 // which must be asserted by tests. The ServiceWorkerRegistration that |
| 42 // backs the |*registration_id| will be kept alive for the test's lifetime. | 76 // backs the |*registration_id| will be kept alive for the test's lifetime. |
| 43 bool CreateRegistrationId(const std::string& tag, | 77 bool CreateRegistrationId(const std::string& tag, |
| 44 BackgroundFetchRegistrationId* registration_id) | 78 BackgroundFetchRegistrationId* registration_id) |
| 45 WARN_UNUSED_RESULT; | 79 WARN_UNUSED_RESULT; |
| 46 | 80 |
| 47 // Creates a ServiceWorkerFetchRequest instance for the given details and | 81 // Creates a ServiceWorkerFetchRequest instance for the given details and |
| 48 // provides a faked response with |status_code| and |response_text| to the | 82 // provides a faked |response| with the faked download manager. |
| 49 // download manager, that will resolve the download with that information. | |
| 50 ServiceWorkerFetchRequest CreateRequestWithProvidedResponse( | 83 ServiceWorkerFetchRequest CreateRequestWithProvidedResponse( |
| 51 const std::string& method, | 84 const std::string& method, |
| 52 const std::string& url_string, | 85 const std::string& url, |
| 53 int status_code, | 86 std::unique_ptr<TestResponse> response); |
| 54 const std::string& response_text); | |
| 55 | 87 |
| 56 // Returns the embedded worker test helper instance, which can be used to | 88 // Returns the embedded worker test helper instance, which can be used to |
| 57 // influence the behaviour of the Service Worker events. | 89 // influence the behaviour of the Service Worker events. |
| 58 BackgroundFetchEmbeddedWorkerTestHelper* embedded_worker_test_helper() { | 90 BackgroundFetchEmbeddedWorkerTestHelper* embedded_worker_test_helper() { |
| 59 return &embedded_worker_test_helper_; | 91 return &embedded_worker_test_helper_; |
| 60 } | 92 } |
| 61 | 93 |
| 62 // Returns the browser context that should be used for the tests. | 94 // Returns the browser context that should be used for the tests. |
| 63 BrowserContext* browser_context() { return &browser_context_; } | 95 BrowserContext* browser_context() { return &browser_context_; } |
| 64 | 96 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 87 | 119 |
| 88 bool set_up_called_ = false; | 120 bool set_up_called_ = false; |
| 89 bool tear_down_called_ = false; | 121 bool tear_down_called_ = false; |
| 90 | 122 |
| 91 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchTestBase); | 123 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchTestBase); |
| 92 }; | 124 }; |
| 93 | 125 |
| 94 } // namespace content | 126 } // namespace content |
| 95 | 127 |
| 96 #endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_TEST_BASE_H_ | 128 #endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_TEST_BASE_H_ |
| OLD | NEW |