summaryrefslogtreecommitdiffstats
path: root/content/browser/download/mock_download_file.cc
blob: 885def180d9cc17b8f940a0d10b807e6ddd8b414 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) 2011 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/download/mock_download_file.h"

#include "content/browser/download/download_create_info.h"
#include "testing/gtest/include/gtest/gtest.h"

using content::DownloadManager;

MockDownloadFile::StatisticsRecorder::StatisticsRecorder() {
}

MockDownloadFile::StatisticsRecorder::~StatisticsRecorder() {
}

void MockDownloadFile::StatisticsRecorder::Record(StatisticsIndex index) {
  Add(index, 1);
}

void MockDownloadFile::StatisticsRecorder::Add(StatisticsIndex index,
                                               int count) {
  map_[index] = map_[index] + count;
}

int MockDownloadFile::StatisticsRecorder::Count(StatisticsIndex index) {
  if (map_.find(index) == map_.end())
    return 0;
  return map_[index];
}

MockDownloadFile::MockDownloadFile(
    const DownloadCreateInfo* info,
    const DownloadRequestHandle& request_handle,
    DownloadManager* download_manager,
    StatisticsRecorder* recorder)
        : id_(info->download_id),
          request_handle_(request_handle),
          download_manager_(download_manager),
          recorder_(recorder),
          rename_count_(0),
          in_progress_(true) {
}

MockDownloadFile::~MockDownloadFile() {
}

net::Error MockDownloadFile::Initialize() {
  in_progress_ = true;
  if (recorder_)
    recorder_->Record(StatisticsRecorder::STAT_INITIALIZE);
  return net::OK;
}

net::Error MockDownloadFile::AppendDataToFile(
    const char* data, size_t data_len) {
  data_.append(data, data_len);
  if (recorder_) {
    recorder_->Record(StatisticsRecorder::STAT_APPEND);
    recorder_->Add(StatisticsRecorder::STAT_BYTES, data_len);
  }
  return net::OK;
}

net::Error MockDownloadFile::Rename(const FilePath& full_path) {
    EXPECT_LT(rename_count_, expected_rename_path_list_.size());
    EXPECT_STREQ(expected_rename_path_list_[rename_count_].value().c_str(),
                 full_path.value().c_str());
    ++rename_count_;
    if (recorder_)
      recorder_->Record(StatisticsRecorder::STAT_RENAME);
    return net::OK;
}

void MockDownloadFile::Detach() {
  if (recorder_)
    recorder_->Record(StatisticsRecorder::STAT_DETACH);
}

void MockDownloadFile::Cancel() {
  in_progress_ = false;
  if (recorder_)
    recorder_->Record(StatisticsRecorder::STAT_CANCEL);
}

void MockDownloadFile::Finish() {
  in_progress_ = false;
  if (recorder_)
    recorder_->Record(StatisticsRecorder::STAT_FINISH);
}

void MockDownloadFile::AnnotateWithSourceInformation() {
}

FilePath MockDownloadFile::FullPath() const {
  return FilePath();
}

bool MockDownloadFile::InProgress() const {
  return in_progress_;
}

int64 MockDownloadFile::BytesSoFar() const {
  return data_.length();
}

int64 MockDownloadFile::CurrentSpeed() const {
  return 0;
}

bool MockDownloadFile::GetHash(std::string* hash) {
  return false;
}

std::string MockDownloadFile::GetHashState() {
  return "";
}

// DownloadFileInterface implementation.
void MockDownloadFile::CancelDownloadRequest() {
}

int MockDownloadFile::Id() const {
  return id_.local();
}

DownloadManager* MockDownloadFile::GetDownloadManager() {
  return download_manager_;
}

const DownloadId& MockDownloadFile::GlobalId() const {
  return id_;
}

std::string MockDownloadFile::DebugString() const {
  return "";
}

void MockDownloadFile::SetExpectedPath(size_t index, const FilePath& path) {
  if (expected_rename_path_list_.size() < index + 1)
    expected_rename_path_list_.resize(index + 1);
  expected_rename_path_list_[index] = path;
}