1 // Copyright 2014 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.
5 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h"
6 #include "chrome/browser/extensions/api/image_writer_private/test_utils.h"
7 #include "chrome/browser/extensions/api/image_writer_private/write_from_url_operation.h"
8 #include "chrome/test/base/testing_profile.h"
9 #include "content/test/net/url_request_prepackaged_interceptor.h"
10 #include "net/url_request/url_fetcher.h"
12 namespace extensions {
13 namespace image_writer {
18 using testing::AnyNumber;
19 using testing::AtLeast;
23 const char kTestImageUrl[] = "http://localhost/test/image.zip";
25 typedef content::URLLocalHostRequestPrepackagedInterceptor GetInterceptor;
27 // This class gives us a generic Operation with the ability to set or inspect
28 // the current path to the image file.
29 class OperationForTest : public WriteFromUrlOperation {
31 OperationForTest(base::WeakPtr<OperationManager> manager_,
32 const ExtensionId& extension_id,
33 net::URLRequestContextGetter* request_context,
35 const std::string& hash,
36 const std::string& storage_unit_id)
37 : WriteFromUrlOperation(manager_,
44 virtual void StartImpl() OVERRIDE {}
46 // Expose stages for testing.
47 void GetDownloadTarget(const base::Closure& continuation) {
48 WriteFromUrlOperation::GetDownloadTarget(continuation);
51 void Download(const base::Closure& continuation) {
52 WriteFromUrlOperation::Download(continuation);
55 void VerifyDownload(const base::Closure& continuation) {
56 WriteFromUrlOperation::VerifyDownload(continuation);
59 // Helpers to set-up state for intermediate stages.
60 void SetImagePath(const base::FilePath image_path) {
61 image_path_ = image_path;
64 base::FilePath GetImagePath() { return image_path_; }
67 virtual ~OperationForTest() {};
70 class ImageWriterWriteFromUrlOperationTest : public ImageWriterUnitTestBase {
72 ImageWriterWriteFromUrlOperationTest() : manager_(&test_profile_) {}
74 virtual void SetUp() OVERRIDE {
75 ImageWriterUnitTestBase::SetUp();
77 // Turn on interception and set up our dummy file.
78 net::URLFetcher::SetEnableInterceptionForTests(true);
79 get_interceptor_.reset(new GetInterceptor());
80 get_interceptor_->SetResponse(GURL(kTestImageUrl), test_image_path_);
83 virtual void TearDown() OVERRIDE {
84 ImageWriterUnitTestBase::TearDown();
86 // Remember to turn off global interception.
87 net::URLFetcher::SetEnableInterceptionForTests(false);
90 scoped_refptr<OperationForTest> CreateOperation(const GURL& url,
91 const std::string& hash) {
92 scoped_refptr<OperationForTest> operation(
93 new OperationForTest(manager_.AsWeakPtr(),
95 test_profile_.GetRequestContext(),
98 test_device_path_.AsUTF8Unsafe()));
103 TestingProfile test_profile_;
104 scoped_ptr<GetInterceptor> get_interceptor_;
106 MockOperationManager manager_;
109 TEST_F(ImageWriterWriteFromUrlOperationTest, SelectTargetWithoutExtension) {
110 scoped_refptr<OperationForTest> operation =
111 CreateOperation(GURL("http://localhost/foo/bar"), "");
113 operation->GetDownloadTarget(base::Bind(&base::DoNothing));
115 EXPECT_EQ(FILE_PATH_LITERAL("bar"),
116 operation->GetImagePath().BaseName().value());
121 TEST_F(ImageWriterWriteFromUrlOperationTest, SelectTargetWithExtension) {
122 scoped_refptr<OperationForTest> operation =
123 CreateOperation(GURL("http://localhost/foo/bar.zip"), "");
125 operation->GetDownloadTarget(base::Bind(&base::DoNothing));
127 EXPECT_EQ(FILE_PATH_LITERAL("bar.zip"),
128 operation->GetImagePath().BaseName().value());
133 TEST_F(ImageWriterWriteFromUrlOperationTest, DownloadFile) {
134 // This test actually triggers the URL fetch code, which will drain the
135 // message queues while waiting for IO, thus we have to run until the
136 // operation completes.
137 base::RunLoop runloop;
138 base::Closure quit_closure = runloop.QuitClosure();
139 base::FilePath download_target_path;
140 scoped_refptr<OperationForTest> operation =
141 CreateOperation(GURL(kTestImageUrl), "");
144 base::CreateTemporaryFileInDir(temp_dir_.path(), &download_target_path));
145 operation->SetImagePath(download_target_path);
149 OnProgress(kDummyExtensionId, image_writer_api::STAGE_DOWNLOAD, _))
153 OnProgress(kDummyExtensionId, image_writer_api::STAGE_DOWNLOAD, 0))
157 OnProgress(kDummyExtensionId, image_writer_api::STAGE_DOWNLOAD, 100))
160 content::BrowserThread::PostTask(
161 content::BrowserThread::FILE,
163 base::Bind(&OperationForTest::Download, operation, quit_closure));
167 EXPECT_TRUE(base::ContentsEqual(test_image_path_, operation->GetImagePath()));
169 EXPECT_EQ(1, get_interceptor_->GetHitCount());
174 TEST_F(ImageWriterWriteFromUrlOperationTest, VerifyFile) {
175 scoped_ptr<char[]> data_buffer(new char[kTestFileSize]);
176 base::ReadFile(test_image_path_, data_buffer.get(), kTestFileSize);
177 base::MD5Digest expected_digest;
178 base::MD5Sum(data_buffer.get(), kTestFileSize, &expected_digest);
179 std::string expected_hash = base::MD5DigestToBase16(expected_digest);
181 scoped_refptr<OperationForTest> operation =
182 CreateOperation(GURL(""), expected_hash);
186 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYDOWNLOAD, _))
190 OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYDOWNLOAD, 0))
192 EXPECT_CALL(manager_,
193 OnProgress(kDummyExtensionId,
194 image_writer_api::STAGE_VERIFYDOWNLOAD,
195 100)).Times(AtLeast(1));
197 operation->SetImagePath(test_image_path_);
198 content::BrowserThread::PostTask(content::BrowserThread::FILE,
200 base::Bind(&OperationForTest::VerifyDownload,
202 base::Bind(&base::DoNothing)));
204 base::RunLoop().RunUntilIdle();
211 } // namespace image_writer
212 } // namespace extensions