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 "base/memory/scoped_ptr.h"
6#include "base/memory/weak_ptr.h"
7#include "chrome/browser/download/download_status_updater.h"
8#include "chrome/browser/download/download_status_updater_delegate.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11namespace {
12
13class MockDelegate : public DownloadStatusUpdaterDelegate {
14 public:
15  explicit MockDelegate(DownloadStatusUpdater* updater)
16      : updater_(updater->AsWeakPtr()),
17        is_download_progress_known_(true),
18        in_progress_download_count_(0),
19        received_bytes_(0),
20        total_bytes_(0) {
21    EXPECT_TRUE(updater);
22    if (updater_)
23      updater_->AddDelegate(this);
24  }
25
26  ~MockDelegate() {
27    EXPECT_TRUE(updater_);
28    if (updater_)
29      updater_->RemoveDelegate(this);
30  }
31
32  // Overriden from DownloadStatusUpdaterDelegate:
33  virtual bool IsDownloadProgressKnown() {
34    return is_download_progress_known_;
35  }
36
37  virtual int64 GetInProgressDownloadCount() {
38    return in_progress_download_count_;
39  }
40
41  virtual int64 GetReceivedDownloadBytes() {
42    return received_bytes_;
43  }
44
45  virtual int64 GetTotalDownloadBytes() {
46    return total_bytes_;
47  }
48
49  void set_is_download_progress_known(bool progress_known) {
50    is_download_progress_known_ = progress_known;
51  }
52
53  void set_in_progress_download_count(int64 download_count) {
54    in_progress_download_count_ = download_count;
55  }
56
57  void set_received_bytes(int64 received_bytes) {
58    received_bytes_ = received_bytes;
59  }
60
61  void set_total_bytes(int64 total_bytes) {
62    total_bytes_ = total_bytes;
63  }
64
65 private:
66  base::WeakPtr<DownloadStatusUpdater> updater_;
67
68  bool is_download_progress_known_;
69  int64 in_progress_download_count_;
70  int64 received_bytes_;
71  int64 total_bytes_;
72
73  DISALLOW_COPY_AND_ASSIGN(MockDelegate);
74};
75
76}  // namespace
77
78class DownloadStatusUpdaterTest : public testing::Test {
79 protected:
80  DownloadStatusUpdater updater_;
81};
82
83TEST_F(DownloadStatusUpdaterTest, Basic) {
84  float progress = -1;
85  EXPECT_TRUE(updater_.GetProgress(&progress));
86  EXPECT_FLOAT_EQ(0, progress);
87  EXPECT_EQ(0, updater_.GetInProgressDownloadCount());
88}
89
90TEST_F(DownloadStatusUpdaterTest, OneDelegate) {
91  MockDelegate delegate(&updater_);
92
93  {
94    float progress = -1;
95    EXPECT_TRUE(updater_.GetProgress(&progress));
96    EXPECT_FLOAT_EQ(0, progress);
97    EXPECT_EQ(0, updater_.GetInProgressDownloadCount());
98  }
99
100  delegate.set_in_progress_download_count(1);
101  delegate.set_received_bytes(21);
102  delegate.set_total_bytes(42);
103
104  {
105    float progress = -1;
106    EXPECT_TRUE(updater_.GetProgress(&progress));
107    EXPECT_FLOAT_EQ(static_cast<float>(1) / 2, progress);
108    EXPECT_EQ(1, updater_.GetInProgressDownloadCount());
109  }
110
111  delegate.set_is_download_progress_known(false);
112
113  {
114    float progress = -1;
115    EXPECT_FALSE(updater_.GetProgress(&progress));
116    EXPECT_FLOAT_EQ(0, progress);
117    EXPECT_EQ(1, updater_.GetInProgressDownloadCount());
118  }
119}
120
121TEST_F(DownloadStatusUpdaterTest, MultipleDelegates) {
122  scoped_ptr<MockDelegate> delegate1(new MockDelegate(&updater_));
123  scoped_ptr<MockDelegate> delegate2(new MockDelegate(&updater_));
124
125  {
126    float progress = -1;
127    EXPECT_TRUE(updater_.GetProgress(&progress));
128    EXPECT_FLOAT_EQ(0, progress);
129    EXPECT_EQ(0, updater_.GetInProgressDownloadCount());
130  }
131
132  delegate1->set_in_progress_download_count(1);
133  delegate1->set_received_bytes(14);
134  delegate1->set_total_bytes(21);
135
136  delegate2->set_in_progress_download_count(2);
137  delegate2->set_received_bytes(7);
138  delegate2->set_total_bytes(21);
139
140  {
141    float progress = -1;
142    EXPECT_TRUE(updater_.GetProgress(&progress));
143    EXPECT_FLOAT_EQ(static_cast<float>(1) / 2, progress);
144    EXPECT_EQ(3, updater_.GetInProgressDownloadCount());
145  }
146
147  delegate1->set_is_download_progress_known(false);
148
149  {
150    float progress = -1;
151    EXPECT_FALSE(updater_.GetProgress(&progress));
152    EXPECT_FLOAT_EQ(0, progress);
153    EXPECT_EQ(3, updater_.GetInProgressDownloadCount());
154  }
155
156  delegate1.reset();
157
158  {
159    float progress = -1;
160    EXPECT_TRUE(updater_.GetProgress(&progress));
161    EXPECT_FLOAT_EQ(static_cast<float>(1) / 3, progress);
162    EXPECT_EQ(2, updater_.GetInProgressDownloadCount());
163  }
164}
165