Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / image_writer_private / write_from_url_operation_unittest.cc
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.
4
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"
11
12 namespace extensions {
13 namespace image_writer {
14
15 namespace {
16
17 using testing::_;
18 using testing::AnyNumber;
19 using testing::AtLeast;
20 using testing::Gt;
21 using testing::Lt;
22
23 const char kTestImageUrl[] = "http://localhost/test/image.zip";
24
25 typedef content::URLLocalHostRequestPrepackagedInterceptor GetInterceptor;
26
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 {
30  public:
31   OperationForTest(base::WeakPtr<OperationManager> manager_,
32                    const ExtensionId& extension_id,
33                    net::URLRequestContextGetter* request_context,
34                    GURL url,
35                    const std::string& hash,
36                    const std::string& storage_unit_id)
37       : WriteFromUrlOperation(manager_,
38                               extension_id,
39                               request_context,
40                               url,
41                               hash,
42                               storage_unit_id) {}
43
44   virtual void StartImpl() OVERRIDE {}
45
46   // Expose stages for testing.
47   void GetDownloadTarget(const base::Closure& continuation) {
48     WriteFromUrlOperation::GetDownloadTarget(continuation);
49   }
50
51   void Download(const base::Closure& continuation) {
52     WriteFromUrlOperation::Download(continuation);
53   }
54
55   void VerifyDownload(const base::Closure& continuation) {
56     WriteFromUrlOperation::VerifyDownload(continuation);
57   }
58
59   // Helpers to set-up state for intermediate stages.
60   void SetImagePath(const base::FilePath image_path) {
61     image_path_ = image_path;
62   }
63
64   base::FilePath GetImagePath() { return image_path_; }
65
66  private:
67   virtual ~OperationForTest() {};
68 };
69
70 class ImageWriterWriteFromUrlOperationTest : public ImageWriterUnitTestBase {
71  protected:
72   ImageWriterWriteFromUrlOperationTest() : manager_(&test_profile_) {}
73
74   virtual void SetUp() OVERRIDE {
75     ImageWriterUnitTestBase::SetUp();
76
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_);
81   }
82
83   virtual void TearDown() OVERRIDE {
84     ImageWriterUnitTestBase::TearDown();
85
86     // Remember to turn off global interception.
87     net::URLFetcher::SetEnableInterceptionForTests(false);
88   }
89
90   scoped_refptr<OperationForTest> CreateOperation(const GURL& url,
91                                                   const std::string& hash) {
92     scoped_refptr<OperationForTest> operation(
93         new OperationForTest(manager_.AsWeakPtr(),
94                              kDummyExtensionId,
95                              test_profile_.GetRequestContext(),
96                              url,
97                              hash,
98                              test_device_path_.AsUTF8Unsafe()));
99     operation->Start();
100     return operation;
101   }
102
103   TestingProfile test_profile_;
104   scoped_ptr<GetInterceptor> get_interceptor_;
105
106   MockOperationManager manager_;
107 };
108
109 TEST_F(ImageWriterWriteFromUrlOperationTest, SelectTargetWithoutExtension) {
110   scoped_refptr<OperationForTest> operation =
111       CreateOperation(GURL("http://localhost/foo/bar"), "");
112
113   operation->GetDownloadTarget(base::Bind(&base::DoNothing));
114
115   EXPECT_EQ(FILE_PATH_LITERAL("bar"),
116             operation->GetImagePath().BaseName().value());
117
118   operation->Cancel();
119 }
120
121 TEST_F(ImageWriterWriteFromUrlOperationTest, SelectTargetWithExtension) {
122   scoped_refptr<OperationForTest> operation =
123       CreateOperation(GURL("http://localhost/foo/bar.zip"), "");
124
125   operation->GetDownloadTarget(base::Bind(&base::DoNothing));
126
127   EXPECT_EQ(FILE_PATH_LITERAL("bar.zip"),
128             operation->GetImagePath().BaseName().value());
129
130   operation->Cancel();
131 }
132
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), "");
142
143   EXPECT_TRUE(
144       base::CreateTemporaryFileInDir(temp_dir_.path(), &download_target_path));
145   operation->SetImagePath(download_target_path);
146
147   EXPECT_CALL(
148       manager_,
149       OnProgress(kDummyExtensionId, image_writer_api::STAGE_DOWNLOAD, _))
150       .Times(AtLeast(1));
151   EXPECT_CALL(
152       manager_,
153       OnProgress(kDummyExtensionId, image_writer_api::STAGE_DOWNLOAD, 0))
154       .Times(AnyNumber());
155   EXPECT_CALL(
156       manager_,
157       OnProgress(kDummyExtensionId, image_writer_api::STAGE_DOWNLOAD, 100))
158       .Times(AnyNumber());
159
160   content::BrowserThread::PostTask(
161       content::BrowserThread::FILE,
162       FROM_HERE,
163       base::Bind(&OperationForTest::Download, operation, quit_closure));
164
165   runloop.Run();
166
167   EXPECT_TRUE(base::ContentsEqual(test_image_path_, operation->GetImagePath()));
168
169   EXPECT_EQ(1, get_interceptor_->GetHitCount());
170
171   operation->Cancel();
172 }
173
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);
180
181   scoped_refptr<OperationForTest> operation =
182       CreateOperation(GURL(""), expected_hash);
183
184   EXPECT_CALL(
185       manager_,
186       OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYDOWNLOAD, _))
187       .Times(AtLeast(1));
188   EXPECT_CALL(
189       manager_,
190       OnProgress(kDummyExtensionId, image_writer_api::STAGE_VERIFYDOWNLOAD, 0))
191       .Times(AtLeast(1));
192   EXPECT_CALL(manager_,
193               OnProgress(kDummyExtensionId,
194                          image_writer_api::STAGE_VERIFYDOWNLOAD,
195                          100)).Times(AtLeast(1));
196
197   operation->SetImagePath(test_image_path_);
198   content::BrowserThread::PostTask(content::BrowserThread::FILE,
199                                    FROM_HERE,
200                                    base::Bind(&OperationForTest::VerifyDownload,
201                                               operation,
202                                               base::Bind(&base::DoNothing)));
203
204   base::RunLoop().RunUntilIdle();
205
206   operation->Cancel();
207 }
208
209 }  // namespace
210
211 }  // namespace image_writer
212 }  // namespace extensions