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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
// Copyright (c) 2010 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 "base/scoped_ptr.h"
#include "base/weak_ptr.h"
#include "chrome/browser/download/download_status_updater.h"
#include "chrome/browser/download/download_status_updater_delegate.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class MockDelegate : public DownloadStatusUpdaterDelegate {
public:
explicit MockDelegate(DownloadStatusUpdater* updater)
: updater_(updater->AsWeakPtr()),
is_download_progress_known_(true),
in_progress_download_count_(0),
received_bytes_(0),
total_bytes_(0) {
EXPECT_TRUE(updater);
if (updater_)
updater_->AddDelegate(this);
}
~MockDelegate() {
EXPECT_TRUE(updater_);
if (updater_)
updater_->RemoveDelegate(this);
}
// Overriden from DownloadStatusUpdaterDelegate:
virtual bool IsDownloadProgressKnown() {
return is_download_progress_known_;
}
virtual int64 GetInProgressDownloadCount() {
return in_progress_download_count_;
}
virtual int64 GetReceivedDownloadBytes() {
return received_bytes_;
}
virtual int64 GetTotalDownloadBytes() {
return total_bytes_;
}
void set_is_download_progress_known(bool progress_known) {
is_download_progress_known_ = progress_known;
}
void set_in_progress_download_count(int64 download_count) {
in_progress_download_count_ = download_count;
}
void set_received_bytes(int64 received_bytes) {
received_bytes_ = received_bytes;
}
void set_total_bytes(int64 total_bytes) {
total_bytes_ = total_bytes;
}
private:
base::WeakPtr<DownloadStatusUpdater> updater_;
bool is_download_progress_known_;
int64 in_progress_download_count_;
int64 received_bytes_;
int64 total_bytes_;
DISALLOW_COPY_AND_ASSIGN(MockDelegate);
};
} // namespace
class DownloadStatusUpdaterTest : public testing::Test {
protected:
DownloadStatusUpdater updater_;
};
TEST_F(DownloadStatusUpdaterTest, Basic) {
float progress = -1;
EXPECT_TRUE(updater_.GetProgress(&progress));
EXPECT_FLOAT_EQ(0, progress);
EXPECT_EQ(0, updater_.GetInProgressDownloadCount());
}
TEST_F(DownloadStatusUpdaterTest, OneDelegate) {
MockDelegate delegate(&updater_);
{
float progress = -1;
EXPECT_TRUE(updater_.GetProgress(&progress));
EXPECT_FLOAT_EQ(0, progress);
EXPECT_EQ(0, updater_.GetInProgressDownloadCount());
}
delegate.set_in_progress_download_count(1);
delegate.set_received_bytes(21);
delegate.set_total_bytes(42);
{
float progress = -1;
EXPECT_TRUE(updater_.GetProgress(&progress));
EXPECT_FLOAT_EQ(static_cast<float>(1) / 2, progress);
EXPECT_EQ(1, updater_.GetInProgressDownloadCount());
}
delegate.set_is_download_progress_known(false);
{
float progress = -1;
EXPECT_FALSE(updater_.GetProgress(&progress));
EXPECT_FLOAT_EQ(0, progress);
EXPECT_EQ(1, updater_.GetInProgressDownloadCount());
}
}
TEST_F(DownloadStatusUpdaterTest, MultipleDelegates) {
scoped_ptr<MockDelegate> delegate1(new MockDelegate(&updater_));
scoped_ptr<MockDelegate> delegate2(new MockDelegate(&updater_));
{
float progress = -1;
EXPECT_TRUE(updater_.GetProgress(&progress));
EXPECT_FLOAT_EQ(0, progress);
EXPECT_EQ(0, updater_.GetInProgressDownloadCount());
}
delegate1->set_in_progress_download_count(1);
delegate1->set_received_bytes(14);
delegate1->set_total_bytes(21);
delegate2->set_in_progress_download_count(2);
delegate2->set_received_bytes(7);
delegate2->set_total_bytes(21);
{
float progress = -1;
EXPECT_TRUE(updater_.GetProgress(&progress));
EXPECT_FLOAT_EQ(static_cast<float>(1) / 2, progress);
EXPECT_EQ(3, updater_.GetInProgressDownloadCount());
}
delegate1->set_is_download_progress_known(false);
{
float progress = -1;
EXPECT_FALSE(updater_.GetProgress(&progress));
EXPECT_FLOAT_EQ(0, progress);
EXPECT_EQ(3, updater_.GetInProgressDownloadCount());
}
delegate1.reset();
{
float progress = -1;
EXPECT_TRUE(updater_.GetProgress(&progress));
EXPECT_FLOAT_EQ(static_cast<float>(1) / 3, progress);
EXPECT_EQ(2, updater_.GetInProgressDownloadCount());
}
}
|