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/public/test/test_browser_context.h" | 16 #include "content/public/test/test_browser_context.h" |
16 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "url/origin.h" | 19 #include "url/origin.h" |
19 | 20 |
20 namespace content { | 21 namespace content { |
21 | 22 |
22 class BackgroundFetchRegistrationId; | 23 class BackgroundFetchRegistrationId; |
| 24 class MockDownloadManager; |
23 class ServiceWorkerRegistration; | 25 class ServiceWorkerRegistration; |
24 | 26 |
25 // Base class containing common functionality needed in unit tests written for | 27 // Base class containing common functionality needed in unit tests written for |
26 // the Background Fetch feature. | 28 // the Background Fetch feature. |
27 class BackgroundFetchTestBase : public ::testing::Test { | 29 class BackgroundFetchTestBase : public ::testing::Test { |
28 public: | 30 public: |
29 BackgroundFetchTestBase(); | 31 BackgroundFetchTestBase(); |
30 ~BackgroundFetchTestBase() override; | 32 ~BackgroundFetchTestBase() override; |
31 | 33 |
32 // ::testing::Test overrides. | 34 // ::testing::Test overrides. |
33 void SetUp() override; | 35 void SetUp() override; |
34 void TearDown() override; | 36 void TearDown() override; |
35 | 37 |
36 // Creates a Background Fetch registration backed by a Service Worker | 38 // Creates a Background Fetch registration backed by a Service Worker |
37 // registration for the testing origin. The resulting registration will be | 39 // registration for the testing origin. The resulting registration will be |
38 // stored in |*registration_id|. Returns whether creation was successful, | 40 // stored in |*registration_id|. Returns whether creation was successful, |
39 // which must be asserted by tests. The ServiceWorkerRegistration that | 41 // which must be asserted by tests. The ServiceWorkerRegistration that |
40 // backs the |*registration_id| will be kept alive for the test's lifetime. | 42 // backs the |*registration_id| will be kept alive for the test's lifetime. |
41 bool CreateRegistrationId(const std::string& tag, | 43 bool CreateRegistrationId(const std::string& tag, |
42 BackgroundFetchRegistrationId* registration_id) | 44 BackgroundFetchRegistrationId* registration_id) |
43 WARN_UNUSED_RESULT; | 45 WARN_UNUSED_RESULT; |
44 | 46 |
| 47 // Creates a ServiceWorkerFetchRequest instance for the given details and |
| 48 // provides a faked response with |status_code| and |response_text| to the |
| 49 // download manager, that will resolve the download with that information. |
| 50 ServiceWorkerFetchRequest CreateRequestWithProvidedResponse( |
| 51 const std::string& method, |
| 52 const std::string& url_string, |
| 53 int status_code, |
| 54 const std::string& response_text); |
| 55 |
45 // Returns the embedded worker test helper instance, which can be used to | 56 // Returns the embedded worker test helper instance, which can be used to |
46 // influence the behaviour of the Service Worker events. | 57 // influence the behaviour of the Service Worker events. |
47 BackgroundFetchEmbeddedWorkerTestHelper* embedded_worker_test_helper() { | 58 BackgroundFetchEmbeddedWorkerTestHelper* embedded_worker_test_helper() { |
48 return &embedded_worker_test_helper_; | 59 return &embedded_worker_test_helper_; |
49 } | 60 } |
50 | 61 |
51 // Returns the browser context that should be used for the tests. | 62 // Returns the browser context that should be used for the tests. |
52 BrowserContext* browser_context() { return &browser_context_; } | 63 BrowserContext* browser_context() { return &browser_context_; } |
53 | 64 |
| 65 // Returns the download manager used for the tests. |
| 66 MockDownloadManager* download_manager(); |
| 67 |
54 // Returns the origin that should be used for Background Fetch tests. | 68 // Returns the origin that should be used for Background Fetch tests. |
55 const url::Origin& origin() const { return origin_; } | 69 const url::Origin& origin() const { return origin_; } |
56 | 70 |
57 private: | 71 private: |
| 72 class RespondingDownloadManager; |
| 73 |
58 TestBrowserThreadBundle thread_bundle_; | 74 TestBrowserThreadBundle thread_bundle_; |
59 TestBrowserContext browser_context_; | 75 TestBrowserContext browser_context_; |
60 | 76 |
| 77 RespondingDownloadManager* download_manager_; // owned by |browser_context_| |
| 78 |
61 BackgroundFetchEmbeddedWorkerTestHelper embedded_worker_test_helper_; | 79 BackgroundFetchEmbeddedWorkerTestHelper embedded_worker_test_helper_; |
62 | 80 |
63 url::Origin origin_; | 81 url::Origin origin_; |
64 | 82 |
65 // Vector of ServiceWorkerRegistration instances that have to be kept alive | 83 // Vector of ServiceWorkerRegistration instances that have to be kept alive |
66 // for the lifetime of this test. | 84 // for the lifetime of this test. |
67 std::vector<scoped_refptr<ServiceWorkerRegistration>> | 85 std::vector<scoped_refptr<ServiceWorkerRegistration>> |
68 service_worker_registrations_; | 86 service_worker_registrations_; |
69 | 87 |
70 bool set_up_called_ = false; | 88 bool set_up_called_ = false; |
71 bool tear_down_called_ = false; | 89 bool tear_down_called_ = false; |
72 | 90 |
73 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchTestBase); | 91 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchTestBase); |
74 }; | 92 }; |
75 | 93 |
76 } // namespace content | 94 } // namespace content |
77 | 95 |
78 #endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_TEST_BASE_H_ | 96 #endif // CONTENT_BROWSER_BACKGROUND_FETCH_BACKGROUND_FETCH_TEST_BASE_H_ |
OLD | NEW |