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

Unified Diff: content/browser/background_fetch/background_fetch_test_base.cc

Issue 2796973002: Unify the two overridden DownloadManagers used by Background Fetch (Closed)
Patch Set: Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/browser/background_fetch/background_fetch_test_base.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/background_fetch/background_fetch_test_base.cc
diff --git a/content/browser/background_fetch/background_fetch_test_base.cc b/content/browser/background_fetch/background_fetch_test_base.cc
index 2ecb64dbcea82c980aaa53a78fb03b319d6221a6..161e67072e640ca32f389e882127afe8a2bfe932 100644
--- a/content/browser/background_fetch/background_fetch_test_base.cc
+++ b/content/browser/background_fetch/background_fetch_test_base.cc
@@ -5,15 +5,27 @@
#include "content/browser/background_fetch/background_fetch_test_base.h"
#include <stdint.h>
+#include <memory>
+#include <utility>
#include "base/bind.h"
#include "base/callback.h"
+#include "base/files/file_util.h"
+#include "base/files/scoped_temp_dir.h"
+#include "base/guid.h"
#include "base/logging.h"
+#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
+#include "base/time/time.h"
#include "content/browser/background_fetch/background_fetch_registration_id.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_registration.h"
#include "content/common/service_worker/service_worker_status_code.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/download_item.h"
+#include "content/public/browser/download_url_parameters.h"
+#include "content/public/test/fake_download_item.h"
+#include "content/public/test/mock_download_manager.h"
#include "url/gurl.h"
namespace content {
@@ -51,6 +63,98 @@ void DidFindServiceWorkerRegistration(
} // namespace
+// Faked download manager that will respond to known HTTP requests with a test-
+// defined response. See CreateRequestWithProvidedResponse().
+class BackgroundFetchTestBase::RespondingDownloadManager
+ : public MockDownloadManager {
+ public:
+ RespondingDownloadManager() : weak_ptr_factory_(this) {}
+ ~RespondingDownloadManager() override = default;
+
+ // Responds to requests to |url| with the |status_code| and |response_text|.
+ void RegisterResponse(const GURL& url,
+ int status_code,
+ const std::string& response_text) {
+ DCHECK_EQ(registered_responses_.count(url), 0u);
+ registered_responses_[url] = std::make_pair(status_code, response_text);
+ }
+
+ // Called when the Background Fetch system starts a download, all information
+ // for which is contained in the |params|.
+ void DownloadUrl(std::unique_ptr<DownloadUrlParameters> params) override {
+ auto iter = registered_responses_.find(params->url());
+ if (iter == registered_responses_.end())
+ return;
+
+ std::unique_ptr<FakeDownloadItem> download_item =
+ base::MakeUnique<FakeDownloadItem>();
+
+ download_item->SetURL(params->url());
+ download_item->SetUrlChain({params->url()});
+ download_item->SetState(DownloadItem::DownloadState::IN_PROGRESS);
+ download_item->SetGuid(base::GenerateGUID());
+ download_item->SetStartTime(base::Time::Now());
+
+ // Asynchronously invoke the callback set on the |params|, and then continue
+ // dealing with the response in this class.
+ BrowserThread::PostTaskAndReply(
+ BrowserThread::UI, FROM_HERE,
+ base::Bind(params->callback(), download_item.get(),
+ DOWNLOAD_INTERRUPT_REASON_NONE),
+ base::Bind(&RespondingDownloadManager::DidStartDownload,
+ weak_ptr_factory_.GetWeakPtr(), download_item.get()));
+
+ download_items_.push_back(std::move(download_item));
+ }
+
+ private:
+ // Called when the download has been "started" by the download manager. This
+ // is where we finish the download by sending a single update.
+ void DidStartDownload(FakeDownloadItem* download_item) {
+ auto iter = registered_responses_.find(download_item->GetURL());
+ DCHECK(iter != registered_responses_.end());
+
+ const ResponseInfo& response_info = iter->second;
+
+ download_item->SetState(DownloadItem::DownloadState::COMPLETE);
+ download_item->SetEndTime(base::Time::Now());
+
+ base::FilePath response_path;
+ if (!temp_directory_.IsValid())
+ ASSERT_TRUE(temp_directory_.CreateUniqueTempDir());
+
+ // Write the |response_info|'s response_text to a temporary file.
+ ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory_.GetPath(),
+ &response_path));
+
+ ASSERT_NE(-1 /* error */,
+ base::WriteFile(response_path, response_info.second.c_str(),
+ response_info.second.size()));
+
+ download_item->SetTargetFilePath(response_path);
+ download_item->SetReceivedBytes(response_info.second.size());
+
+ // Notify the Job Controller about the download having been updated.
+ download_item->NotifyDownloadUpdated();
+ }
+
+ using ResponseInfo =
+ std::pair<int /* status_code */, std::string /* response_text */>;
+
+ // Map of URL to the response information associated with that URL.
+ std::map<GURL, ResponseInfo> registered_responses_;
+
+ // Only used to guarantee the lifetime of the created FakeDownloadItems.
+ std::vector<std::unique_ptr<FakeDownloadItem>> download_items_;
+
+ // Temporary directory in which successfully downloaded files will be stored.
+ base::ScopedTempDir temp_directory_;
+
+ base::WeakPtrFactory<RespondingDownloadManager> weak_ptr_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(RespondingDownloadManager);
+};
+
BackgroundFetchTestBase::BackgroundFetchTestBase()
: thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
origin_(GURL(kTestOrigin)) {}
@@ -61,10 +165,19 @@ BackgroundFetchTestBase::~BackgroundFetchTestBase() {
}
void BackgroundFetchTestBase::SetUp() {
+ download_manager_ = new RespondingDownloadManager();
+
+ // The |download_manager_| ownership is given to the BrowserContext, and the
+ // BrowserContext will take care of deallocating it.
+ BrowserContext::SetDownloadManagerForTesting(browser_context(),
+ download_manager_);
+
set_up_called_ = true;
}
void BackgroundFetchTestBase::TearDown() {
+ EXPECT_CALL(*download_manager_, Shutdown()).Times(1);
+
service_worker_registrations_.clear();
tear_down_called_ = true;
}
@@ -122,4 +235,28 @@ bool BackgroundFetchTestBase::CreateRegistrationId(
return true;
}
+// Creates a ServiceWorkerFetchRequest instance for the given details and
+// provides a faked response with |status_code| and |response_text| to the
+// download manager, that will resolve the download with that information.
+ServiceWorkerFetchRequest
+BackgroundFetchTestBase::CreateRequestWithProvidedResponse(
+ const std::string& method,
+ const std::string& url_string,
+ int status_code,
+ const std::string& response_text) {
+ GURL url(url_string);
+
+ // Register the |status_code| and |response_text| with the download manager.
+ download_manager_->RegisterResponse(GURL(url_string), status_code,
+ response_text);
+
+ // Create a ServiceWorkerFetchRequest request with the same information.
+ return ServiceWorkerFetchRequest(url, method, ServiceWorkerHeaderMap(),
+ Referrer(), false /* is_reload */);
+}
+
+MockDownloadManager* BackgroundFetchTestBase::download_manager() {
+ return download_manager_;
+}
+
} // namespace content
« no previous file with comments | « content/browser/background_fetch/background_fetch_test_base.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698