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 #include "content/browser/background_fetch/background_fetch_data_manager.h" | 5 #include "content/browser/background_fetch/background_fetch_data_manager.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "content/browser/background_fetch/background_fetch_context.h" | 8 #include "content/browser/background_fetch/background_fetch_context.h" |
9 #include "content/browser/background_fetch/fetch_request.h" | 9 #include "content/browser/background_fetch/background_fetch_job_info.h" |
| 10 #include "content/browser/background_fetch/background_fetch_request_info.h" |
10 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 11 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
11 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
12 #include "content/public/test/test_browser_context.h" | 13 #include "content/public/test/test_browser_context.h" |
13 #include "content/public/test/test_browser_thread_bundle.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 const char kOrigin[] = "https://example.com/"; | 19 const char kOrigin[] = "https://example.com/"; |
19 const char kResource[] = "https://example.com/resource.html"; | 20 const char kResource[] = "https://example.com/resource.html"; |
20 const char kTag[] = "TestRequestTag"; | 21 const char kTag[] = "TestRequestTag"; |
21 constexpr int64_t kServiceWorkerRegistrationId = 9001; | 22 constexpr int64_t kServiceWorkerRegistrationId = 9001; |
22 | 23 |
23 } // namespace | 24 } // namespace |
24 | 25 |
25 namespace content { | 26 namespace content { |
26 | 27 |
27 class BackgroundFetchDataManagerTest : public testing::Test { | 28 class BackgroundFetchDataManagerTest : public testing::Test { |
28 protected: | 29 protected: |
29 BackgroundFetchDataManagerTest() | 30 BackgroundFetchDataManagerTest() |
30 : helper_(base::FilePath()), | 31 : helper_(base::FilePath()), |
31 background_fetch_context_( | 32 background_fetch_context_(new BackgroundFetchContext( |
32 new BackgroundFetchContext(&browser_context_, | 33 &browser_context_, |
33 helper_.context_wrapper())) {} | 34 BrowserContext::GetDefaultStoragePartition(&browser_context_), |
| 35 helper_.context_wrapper())) {} |
34 | 36 |
35 BackgroundFetchDataManager* GetDataManager() const { | 37 BackgroundFetchDataManager* GetDataManager() const { |
36 return background_fetch_context_->GetDataManagerForTesting(); | 38 return background_fetch_context_->GetDataManagerForTesting(); |
37 } | 39 } |
38 | 40 |
39 private: | 41 private: |
40 TestBrowserThreadBundle browser_thread_bundle_; | 42 TestBrowserThreadBundle browser_thread_bundle_; |
41 TestBrowserContext browser_context_; | 43 TestBrowserContext browser_context_; |
42 EmbeddedWorkerTestHelper helper_; | 44 EmbeddedWorkerTestHelper helper_; |
43 | 45 |
44 scoped_refptr<BackgroundFetchContext> background_fetch_context_; | 46 scoped_refptr<BackgroundFetchContext> background_fetch_context_; |
45 }; | 47 }; |
46 | 48 |
47 TEST_F(BackgroundFetchDataManagerTest, AddRequest) { | 49 TEST_F(BackgroundFetchDataManagerTest, AddRequest) { |
| 50 // Create a BackgroundFetchJobInfo and a set of BackgroundFetchRequestInfos. |
| 51 std::vector<BackgroundFetchRequestInfo> request_infos; |
| 52 std::vector<std::string> request_guids; |
| 53 for (int i = 0; i < 10; i++) { |
| 54 BackgroundFetchRequestInfo request_info(GURL(kResource), kTag); |
| 55 request_guids.push_back(request_info.guid()); |
| 56 request_infos.push_back(std::move(request_info)); |
| 57 } |
| 58 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)), |
| 59 kServiceWorkerRegistrationId); |
| 60 |
| 61 // Initialize a BackgroundFetchJobData with the constructed requests. |
48 BackgroundFetchDataManager* data_manager = GetDataManager(); | 62 BackgroundFetchDataManager* data_manager = GetDataManager(); |
49 FetchRequest request(url::Origin(GURL(kOrigin)), GURL(kResource), | 63 BackgroundFetchJobData* job_data = |
50 kServiceWorkerRegistrationId, kTag); | 64 data_manager->CreateRequest(job_info, request_infos); |
51 | 65 |
52 data_manager->CreateRequest(request); | 66 // Get all of the fetch requests from the BackgroundFetchJobData. |
| 67 for (int i = 0; i < 10; i++) { |
| 68 EXPECT_FALSE(job_data->IsComplete()); |
| 69 ASSERT_TRUE(job_data->HasRequestsRemaining()); |
| 70 const BackgroundFetchRequestInfo& request_info = |
| 71 job_data->GetNextBackgroundFetchRequestInfo(); |
| 72 EXPECT_EQ(request_info.tag(), kTag); |
| 73 } |
53 | 74 |
54 // TODO(harkness) There's no output to test yet. Once there is, check that the | 75 // At this point, all the fetches have been started, but none finished. |
55 // request was actually added. | 76 EXPECT_FALSE(job_data->HasRequestsRemaining()); |
| 77 EXPECT_FALSE(job_data->IsComplete()); |
| 78 |
| 79 // Complete all buy one of the fetch requests. |
| 80 for (int i = 0; i < 9; i++) { |
| 81 EXPECT_FALSE( |
| 82 job_data->BackgroundFetchRequestInfoComplete(request_guids[i])); |
| 83 EXPECT_FALSE(job_data->IsComplete()); |
| 84 } |
| 85 |
| 86 // Complete the final fetch request. |
| 87 EXPECT_FALSE(job_data->BackgroundFetchRequestInfoComplete(request_guids[9])); |
| 88 EXPECT_TRUE(job_data->IsComplete()); |
56 } | 89 } |
57 | 90 |
58 } // namespace content | 91 } // namespace content |
OLD | NEW |