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

Side by Side Diff: content/browser/download/mock_download_file.cc

Issue 8372034: Created an interface for DownloadFile, for use in unit tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed typo Created 9 years, 1 month 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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/download/mock_download_file.h"
6
7 #include "content/browser/download/download_create_info.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 MockDownloadFile::StatisticsRecorder::StatisticsRecorder() {
11 }
12
13 MockDownloadFile::StatisticsRecorder::~StatisticsRecorder() {
14 }
15
16 void MockDownloadFile::StatisticsRecorder::Record(StatisticsIndex index) {
17 Add(index, 1);
18 }
19
20 void MockDownloadFile::StatisticsRecorder::Add(StatisticsIndex index,
21 int count) {
22 map_[index] = map_[index] + count;
23 }
24
25 int MockDownloadFile::StatisticsRecorder::Count(StatisticsIndex index) {
26 if (map_.find(index) == map_.end())
27 return 0;
28 return map_[index];
29 }
30
31 MockDownloadFile::MockDownloadFile(
32 const DownloadCreateInfo* info,
33 const DownloadRequestHandle& request_handle,
34 DownloadManager* download_manager,
35 StatisticsRecorder* recorder)
36 : id_(info->download_id),
37 request_handle_(request_handle),
38 download_manager_(download_manager),
39 recorder_(recorder),
40 rename_count_(0),
41 in_progress_(true) {
42 }
43
44 MockDownloadFile::~MockDownloadFile() {
45 }
46
47 net::Error MockDownloadFile::Initialize(bool calculate_hash) {
48 in_progress_ = true;
49 if (recorder_)
50 recorder_->Record(StatisticsRecorder::STAT_INITIALIZE);
51 return net::OK;
52 }
53
54 net::Error MockDownloadFile::AppendDataToFile(
55 const char* data, size_t data_len) {
56 data_.append(data, data_len);
57 if (recorder_) {
58 recorder_->Record(StatisticsRecorder::STAT_APPEND);
59 recorder_->Add(StatisticsRecorder::STAT_BYTES, data_len);
60 }
61 return net::OK;
62 }
63
64 net::Error MockDownloadFile::Rename(const FilePath& full_path) {
65 EXPECT_LT(rename_count_, expected_rename_path_list_.size());
66 EXPECT_STREQ(expected_rename_path_list_[rename_count_].value().c_str(),
67 full_path.value().c_str());
68 ++rename_count_;
69 if (recorder_)
70 recorder_->Record(StatisticsRecorder::STAT_RENAME);
71 return net::OK;
72 }
73
74 void MockDownloadFile::Detach() {
75 if (recorder_)
76 recorder_->Record(StatisticsRecorder::STAT_DETACH);
77 }
78
79 void MockDownloadFile::Cancel() {
80 in_progress_ = false;
81 if (recorder_)
82 recorder_->Record(StatisticsRecorder::STAT_CANCEL);
83 }
84
85 void MockDownloadFile::Finish() {
86 in_progress_ = false;
87 if (recorder_)
88 recorder_->Record(StatisticsRecorder::STAT_FINISH);
89 }
90
91 void MockDownloadFile::AnnotateWithSourceInformation() {
92 }
93
94 FilePath MockDownloadFile::FullPath() const {
95 return FilePath();
96 }
97
98 bool MockDownloadFile::InProgress() const {
99 return in_progress_;
100 }
101
102 int64 MockDownloadFile::BytesSoFar() const {
103 return data_.length();
104 }
105
106 bool MockDownloadFile::GetSha256Hash(std::string* hash) {
107 return false;
108 }
109
110 // DownloadFileInterface implementation.
111 void MockDownloadFile::CancelDownloadRequest() {
112 }
113
114 int MockDownloadFile::Id() const {
115 return id_.local();
116 }
117
118 DownloadManager* MockDownloadFile::GetDownloadManager() {
119 return download_manager_;
120 }
121
122 const DownloadId& MockDownloadFile::GlobalId() const {
123 return id_;
124 }
125
126 std::string MockDownloadFile::DebugString() const {
127 return "";
128 }
129
130 void MockDownloadFile::SetExpectedPath(size_t index, const FilePath& path) {
131 if (expected_rename_path_list_.size() < index + 1)
132 expected_rename_path_list_.resize(index + 1);
133 expected_rename_path_list_[index] = path;
134 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698