Chromium Code Reviews| Index: content/browser/background_fetch/background_fetch_batch_manager_unittest.cc |
| diff --git a/content/browser/background_fetch/background_fetch_batch_manager_unittest.cc b/content/browser/background_fetch/background_fetch_batch_manager_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5617e8d41689312ad2ca89f2de727172aef1ec2a |
| --- /dev/null |
| +++ b/content/browser/background_fetch/background_fetch_batch_manager_unittest.cc |
| @@ -0,0 +1,91 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/background_fetch/background_fetch_batch_manager.h" |
| + |
| +#include "base/bind_helpers.h" |
| +#include "base/callback_helpers.h" |
| +#include "base/run_loop.h" |
| +#include "content/browser/background_fetch/background_fetch_data_manager.h" |
| +#include "content/browser/background_fetch/batch_request.h" |
| +#include "content/browser/background_fetch/fetch_request.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/test/mock_download_manager.h" |
| +#include "content/public/test/test_browser_context.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +const char kOrigin[] = "https://example.com/"; |
| +const char kBatchGuid[] = "TestRequestGuid"; |
| +constexpr int64_t kServiceWorkerRegistrationId = 9001; |
| +const char kTestUrl[] = "http://www.example.com/example.html"; |
| +const char kTag[] = "testTag"; |
| + |
| +} // namespace |
| + |
| +namespace content { |
| + |
| +class BackgroundFetchBatchManagerTest : public ::testing::Test { |
| + public: |
| + BackgroundFetchBatchManagerTest() |
| + : batch_manager_( |
| + &browser_context_, |
| + BrowserContext::GetDefaultStoragePartition(&browser_context_)), |
| + download_manager_(new MockDownloadManager()) { |
| + BrowserContext::SetDownloadManagerForTesting(&browser_context_, |
| + download_manager_); |
|
Peter Beverloo
2017/02/24 02:05:15
Something to consider --
We usually set overrides
harkness
2017/02/24 11:47:10
Sounds like a good practice to keep to. I've moved
|
| + } |
| + ~BackgroundFetchBatchManagerTest() override {} |
|
Peter Beverloo
2017/02/24 02:05:15
nit: "= default" all the things
harkness
2017/02/24 11:47:10
Done.
|
| + |
| + void ProcessBatch(const std::string& batch_guid, |
| + BackgroundFetchDataManager::BatchData* batch_data) { |
| + base::RunLoop run_loop; |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind(&BackgroundFetchBatchManagerTest::ProcessBatchOnIO, |
| + base::Unretained(this), batch_guid, batch_data, |
| + run_loop.QuitClosure())); |
| + run_loop.Run(); |
| + } |
| + |
| + void ProcessBatchOnIO(const std::string& batch_guid, |
| + BackgroundFetchDataManager::BatchData* batch_data, |
| + const base::Closure& closure) { |
| + batch_manager_.ProcessBatch(batch_guid, batch_data); |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, closure); |
| + } |
| + |
| + BackgroundFetchBatchManager* batch_manager() { return &batch_manager_; } |
| + MockDownloadManager* download_manager() { return download_manager_; } |
| + |
| + private: |
| + TestBrowserThreadBundle thread_bundle_; |
| + TestBrowserContext browser_context_; |
| + BackgroundFetchBatchManager batch_manager_; |
| + MockDownloadManager* download_manager_; |
|
Peter Beverloo
2017/02/24 02:05:15
This needs to be an std::unique_ptr<>. We leak the
harkness
2017/02/24 11:47:10
Actually, I believe that we want to pass the raw p
|
| +}; |
| + |
| +TEST_F(BackgroundFetchBatchManagerTest, StartDownload) { |
| + BatchRequest batch_request(kTag, url::Origin(GURL(kOrigin)), |
| + kServiceWorkerRegistrationId); |
| + FetchRequest fetch_request(GURL(kTestUrl), kBatchGuid); |
| + std::vector<FetchRequest> fetch_requests; |
| + fetch_requests.push_back(fetch_request); |
|
Peter Beverloo
2017/02/24 02:05:15
nit:
std::vector<FetchRequest> fetch_requests { f
harkness
2017/02/24 11:47:10
Done.
|
| + |
| + // Get a BatchData to give to the BatchManager. The BatchManager then gets the |
| + // FetchRequests from the BatchData. |
| + BackgroundFetchDataManager::BatchData batch_data(batch_request, |
| + fetch_requests); |
| + |
| + EXPECT_CALL(*(download_manager()), |
| + DownloadUrlMock(::testing::Pointee(::testing::Property( |
| + &DownloadUrlParameters::url, GURL(kTestUrl))))) |
| + .Times(1); |
| + |
| + ProcessBatch(kBatchGuid, &batch_data); |
| +} |
| + |
| +} // namespace content |