OLD | NEW |
---|---|
(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 #ifndef CONTENT_BROWSER_DOWNLOAD_MOCK_DOWNLOAD_FILE_H_ | |
6 #define CONTENT_BROWSER_DOWNLOAD_MOCK_DOWNLOAD_FILE_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 #include <map> | |
11 | |
12 #include "base/file_path.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "content/browser/download/download_file.h" | |
15 #include "content/browser/download/download_id.h" | |
16 #include "content/browser/download/download_manager.h" | |
17 #include "content/browser/download/download_request_handle.h" | |
18 #include "net/base/net_errors.h" | |
19 | |
20 struct DownloadCreateInfo; | |
21 | |
22 class MockDownloadFile : virtual public DownloadFile { | |
Randy Smith (Not in Mondays)
2011/11/14 21:58:39
What are the advantages of creating this class as
ahendrickson
2011/11/16 19:39:11
It's a lot more flexible. You can monitor stuff,
| |
23 public: | |
24 // Class to extract statistics from the usage of |MockDownloadFile|. | |
25 class StatisticsRecorder { | |
26 public: | |
27 enum StatisticsIndex { | |
28 STAT_INITIALIZE, | |
29 STAT_APPEND, | |
30 STAT_RENAME, | |
31 STAT_DETACH, | |
32 STAT_CANCEL, | |
33 STAT_FINISH, | |
34 STAT_BYTES | |
35 }; | |
36 | |
37 StatisticsRecorder(); | |
38 ~StatisticsRecorder(); | |
39 | |
40 // Records the statistic. | |
41 // |index| indicates what statistic to use. | |
42 void Record(StatisticsIndex index); | |
43 | |
44 // Adds to the statistic. | |
45 // |index| indicates what statistic to use. | |
46 void Add(StatisticsIndex index, int count); | |
47 | |
48 // Returns the statistic value. | |
49 // |index| indicates what statistic to use. | |
50 int Count(StatisticsIndex index); | |
51 | |
52 private: | |
53 typedef std::map<StatisticsIndex, int> StatisticsMap; | |
54 | |
55 StatisticsMap map_; | |
56 }; | |
57 | |
58 MockDownloadFile(const DownloadCreateInfo* info, | |
59 const DownloadRequestHandle& request_handle, | |
60 DownloadManager* download_manager, | |
61 StatisticsRecorder* recorder); | |
62 virtual ~MockDownloadFile(); | |
63 | |
64 // DownloadFile functions. | |
65 virtual net::Error Initialize(bool calculate_hash) OVERRIDE; | |
66 virtual net::Error AppendDataToFile(const char* data, | |
67 size_t data_len) OVERRIDE; | |
68 virtual net::Error Rename(const FilePath& full_path) OVERRIDE; | |
69 virtual void Detach() OVERRIDE; | |
70 virtual void Cancel() OVERRIDE; | |
71 virtual void Finish() OVERRIDE; | |
72 virtual void AnnotateWithSourceInformation() OVERRIDE; | |
73 virtual FilePath FullPath() const OVERRIDE; | |
74 virtual bool InProgress() const OVERRIDE; | |
75 virtual int64 BytesSoFar() const OVERRIDE; | |
76 virtual bool GetSha256Hash(std::string* hash) OVERRIDE; | |
77 virtual void CancelDownloadRequest() OVERRIDE; | |
78 virtual int Id() const OVERRIDE; | |
79 virtual DownloadManager* GetDownloadManager() OVERRIDE; | |
80 virtual const DownloadId& GlobalId() const OVERRIDE; | |
81 virtual std::string DebugString() const OVERRIDE; | |
82 | |
83 // Functions relating to setting and checking expectations. | |
84 size_t rename_count() const { return rename_count_; } | |
85 void SetExpectedPath(size_t index, const FilePath& path); | |
86 | |
87 private: | |
88 // The unique identifier for this download, assigned at creation by | |
89 // the DownloadFileManager for its internal record keeping. | |
90 DownloadId id_; | |
91 | |
92 // The handle to the request information. Used for operations outside the | |
93 // download system, specifically canceling a download. | |
94 DownloadRequestHandle request_handle_; | |
95 | |
96 // DownloadManager this download belongs to. | |
97 scoped_refptr<DownloadManager> download_manager_; | |
98 | |
99 // Records usage statistics. Not owned by this class (survives destruction). | |
100 StatisticsRecorder* recorder_; | |
101 | |
102 // The number of times |Rename()| has been called. | |
103 // Used instead of checking |recorder_| because the latter can be NULL. | |
104 size_t rename_count_; | |
105 | |
106 // A vector of paths that we expect to call |Rename()| with. | |
107 std::vector<FilePath> expected_rename_path_list_; | |
108 | |
109 // A buffer to hold the data we write. | |
110 std::string data_; | |
111 | |
112 // Dummy in-progress flag. | |
113 bool in_progress_; | |
114 }; | |
115 | |
116 #endif // CONTENT_BROWSER_DOWNLOAD_MOCK_DOWNLOAD_FILE_H_ | |
OLD | NEW |