Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: content/browser/background_fetch/background_fetch_data_manager_unittest.cc

Issue 2708943002: Create the BackgroundFetchBatchManager and initiate a download. (Closed)
Patch Set: Integrated code review comments and added BackgroundFetchDataManager::BatchData which feeds FetchRe… Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/batch_request.h"
9 #include "content/browser/background_fetch/fetch_request.h" 10 #include "content/browser/background_fetch/fetch_request.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 BatchRequest and a set of FetchRequests.
51 std::vector<FetchRequest> fetch_requests;
52 std::vector<std::string> fetch_guids;
53 for (int i = 0; i < 10; i++) {
54 FetchRequest fetch_request(GURL(kResource), kTag);
55 fetch_guids.push_back(fetch_request.guid());
56 fetch_requests.push_back(std::move(fetch_request));
57 }
58 BatchRequest batch_request(kTag, url::Origin(GURL(kOrigin)),
59 kServiceWorkerRegistrationId);
60
61 // Initialize a BatchData with the constructed requests.
48 BackgroundFetchDataManager* data_manager = GetDataManager(); 62 BackgroundFetchDataManager* data_manager = GetDataManager();
49 FetchRequest request(url::Origin(GURL(kOrigin)), GURL(kResource), 63 BackgroundFetchDataManager::BatchData* batch_data =
50 kServiceWorkerRegistrationId, kTag); 64 data_manager->CreateRequest(batch_request, fetch_requests);
51 65
52 data_manager->CreateRequest(request); 66 // Get all of the fetch requests from the BatchData.
67 for (int i = 0; i < 10; i++) {
68 EXPECT_FALSE(batch_data->IsComplete());
69 ASSERT_TRUE(batch_data->HasRequestsRemaining());
70 const FetchRequest& fetch_request = batch_data->GetNextFetchRequest();
71 EXPECT_EQ(fetch_request.tag(), kTag);
72 }
53 73
54 // TODO(harkness) There's no output to test yet. Once there is, check that the 74 // At this point, all the fetches have been started, but none finished.
55 // request was actually added. 75 EXPECT_FALSE(batch_data->HasRequestsRemaining());
76 EXPECT_FALSE(batch_data->IsComplete());
77
78 // Complete all buy one of the fetch requests.
79 for (int i = 0; i < 9; i++) {
80 EXPECT_FALSE(batch_data->FetchRequestComplete(fetch_guids[i]));
81 EXPECT_FALSE(batch_data->IsComplete());
82 }
83
84 // Complete the final fetch request.
85 EXPECT_FALSE(batch_data->FetchRequestComplete(fetch_guids[9]));
86 EXPECT_TRUE(batch_data->IsComplete());
56 } 87 }
57 88
58 } // namespace content 89 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698