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

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

Issue 2749043003: Moved tests from DataManager to JobData. (Closed)
Patch Set: Remove unnecessary include. 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
(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 "content/browser/background_fetch/background_fetch_data_manager.h"
6
7 #include "base/files/file_path.h"
8 #include "content/browser/background_fetch/background_fetch_context.h"
9 #include "content/browser/background_fetch/background_fetch_job_info.h"
10 #include "content/browser/background_fetch/background_fetch_request_info.h"
11 #include "content/browser/service_worker/embedded_worker_test_helper.h"
12 #include "content/browser/service_worker/service_worker_context_wrapper.h"
13 #include "content/public/browser/download_interrupt_reasons.h"
14 #include "content/public/browser/download_item.h"
15 #include "content/public/test/test_browser_context.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace {
20
21 const char kOrigin[] = "https://example.com/";
22 const char kResource[] = "https://example.com/resource.html";
23 const char kTag[] = "TestRequestTag";
24 constexpr int64_t kServiceWorkerRegistrationId = 9001;
25
26 } // namespace
27
28 namespace content {
29
30 class BackgroundFetchDataManagerTest : public testing::Test {
31 protected:
32 BackgroundFetchDataManagerTest()
33 : helper_(base::FilePath()),
34 background_fetch_context_(new BackgroundFetchContext(
35 &browser_context_,
36 BrowserContext::GetDefaultStoragePartition(&browser_context_),
37 helper_.context_wrapper())) {}
38
39 BackgroundFetchDataManager* GetDataManager() const {
40 return background_fetch_context_->GetDataManagerForTesting();
41 }
42
43 private:
44 TestBrowserThreadBundle browser_thread_bundle_;
45 TestBrowserContext browser_context_;
46 EmbeddedWorkerTestHelper helper_;
47
48 scoped_refptr<BackgroundFetchContext> background_fetch_context_;
49 };
50
51 TEST_F(BackgroundFetchDataManagerTest, AddRequest) {
52 // Create a BackgroundFetchJobInfo and a set of BackgroundFetchRequestInfos.
53 std::vector<BackgroundFetchRequestInfo> request_infos;
54 std::vector<std::string> request_guids;
55 for (int i = 0; i < 10; i++) {
56 BackgroundFetchRequestInfo request_info(GURL(kResource), kTag);
57 request_guids.push_back(request_info.guid());
58 request_infos.push_back(std::move(request_info));
59 }
60 BackgroundFetchJobInfo job_info(kTag, url::Origin(GURL(kOrigin)),
61 kServiceWorkerRegistrationId);
62
63 // Initialize a BackgroundFetchJobData with the constructed requests.
64 BackgroundFetchDataManager* data_manager = GetDataManager();
65 std::unique_ptr<BackgroundFetchJobData> job_data =
66 data_manager->CreateRequest(job_info, request_infos);
67
68 // Get all of the fetch requests from the BackgroundFetchJobData.
69 for (int i = 0; i < 10; i++) {
70 EXPECT_FALSE(job_data->IsComplete());
71 ASSERT_TRUE(job_data->HasRequestsRemaining());
72 const BackgroundFetchRequestInfo& request_info =
73 job_data->GetNextBackgroundFetchRequestInfo();
74 EXPECT_EQ(request_info.tag(), kTag);
75 EXPECT_EQ(request_info.state(),
76 DownloadItem::DownloadState::MAX_DOWNLOAD_STATE);
77 EXPECT_EQ(request_info.interrupt_reason(),
78 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE);
79 }
80
81 // At this point, all the fetches have been started, but none finished.
82 EXPECT_FALSE(job_data->HasRequestsRemaining());
83 EXPECT_FALSE(job_data->IsComplete());
84
85 // Complete all but one of the fetch requests.
86 for (int i = 0; i < 9; i++) {
87 EXPECT_FALSE(job_data->UpdateBackgroundFetchRequestState(
88 request_guids[i], DownloadItem::DownloadState::COMPLETE,
89 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE));
90 EXPECT_FALSE(job_data->IsComplete());
91 }
92
93 // Set the state of the last task to be interrupted. This should not complete
94 // the job.
95 EXPECT_FALSE(job_data->UpdateBackgroundFetchRequestState(
96 request_guids[9], DownloadItem::DownloadState::INTERRUPTED,
97 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN));
98 EXPECT_FALSE(job_data->IsComplete());
99
100 // Complete the final fetch request.
101 EXPECT_FALSE(job_data->UpdateBackgroundFetchRequestState(
102 request_guids[9], DownloadItem::DownloadState::COMPLETE,
103 DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE));
104 EXPECT_TRUE(job_data->IsComplete());
105 }
106
107 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698