1 // Copyright (c) 2012 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.
5 #include "chrome/browser/chromeos/gdata/gdata_operation_registry.h"
7 #include "base/compiler_specific.h"
8 #include "base/memory/weak_ptr.h"
9 #include "base/message_loop.h"
10 #include "content/public/test/test_browser_thread.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using testing::ElementsAre
;
20 class MockOperation
: public GDataOperationRegistry::Operation
,
21 public base::SupportsWeakPtr
<MockOperation
> {
23 MockOperation(GDataOperationRegistry
* registry
,
24 GDataOperationRegistry::OperationType type
,
26 : GDataOperationRegistry::Operation(registry
, type
, path
) {}
28 MOCK_METHOD0(DoCancel
, void());
30 // Make them public so that they can be called from test cases.
31 using GDataOperationRegistry::Operation::NotifyStart
;
32 using GDataOperationRegistry::Operation::NotifyProgress
;
33 using GDataOperationRegistry::Operation::NotifyFinish
;
36 class MockUploadOperation
: public MockOperation
{
38 explicit MockUploadOperation(GDataOperationRegistry
* registry
)
39 : MockOperation(registry
,
40 GDataOperationRegistry::OPERATION_UPLOAD
,
41 FilePath("/dummy/upload")) {}
44 class MockDownloadOperation
: public MockOperation
{
46 explicit MockDownloadOperation(GDataOperationRegistry
* registry
)
47 : MockOperation(registry
,
48 GDataOperationRegistry::OPERATION_DOWNLOAD
,
49 FilePath("/dummy/download")) {}
52 class MockOtherOperation
: public MockOperation
{
54 explicit MockOtherOperation(GDataOperationRegistry
* registry
)
55 : MockOperation(registry
,
56 GDataOperationRegistry::OPERATION_OTHER
,
57 FilePath("/dummy/other")) {}
60 class TestObserver
: public GDataOperationRegistry::Observer
{
62 virtual void OnProgressUpdate(
63 const std::vector
<GDataOperationRegistry::ProgressStatus
>& list
)
68 const std::vector
<GDataOperationRegistry::ProgressStatus
>& status() const {
73 std::vector
<GDataOperationRegistry::ProgressStatus
> status_
;
77 : public ::testing::MatcherInterface
<
78 const GDataOperationRegistry::ProgressStatus
&> {
80 ProgressMatcher(int64 expected_current
, int64 expected_total
)
81 : expected_current_(expected_current
),
82 expected_total_(expected_total
) {}
84 virtual bool MatchAndExplain(
85 const GDataOperationRegistry::ProgressStatus
& status
,
86 testing::MatchResultListener
* /* listener */) const OVERRIDE
{
87 return status
.progress_current
== expected_current_
&&
88 status
.progress_total
== expected_total_
;
91 virtual void DescribeTo(::std::ostream
* os
) const OVERRIDE
{
92 *os
<< "current / total equals " << expected_current_
<< " / " <<
96 virtual void DescribeNegationTo(::std::ostream
* os
) const OVERRIDE
{
97 *os
<< "current / total does not equal " << expected_current_
<< " / " <<
102 const int64 expected_current_
;
103 const int64 expected_total_
;
106 testing::Matcher
<const GDataOperationRegistry::ProgressStatus
&> Progress(
107 int64 current
, int64 total
) {
108 return testing::MakeMatcher(new ProgressMatcher(current
, total
));
113 // Pretty-prints ProgressStatus for testing purpose.
114 std::ostream
& operator<<(std::ostream
& os
,
115 const GDataOperationRegistry::ProgressStatus
& status
) {
116 return os
<< status
.DebugString();
119 class GDataOperationRegistryTest
: public testing::Test
{
121 GDataOperationRegistryTest()
122 : ui_thread_(content::BrowserThread::UI
, &message_loop_
) {
124 MessageLoopForUI message_loop_
;
125 content::TestBrowserThread ui_thread_
;
128 TEST_F(GDataOperationRegistryTest
, OneSuccess
) {
129 TestObserver observer
;
130 GDataOperationRegistry registry
;
131 registry
.DisableNotificationFrequencyControlForTest();
132 registry
.AddObserver(&observer
);
134 base::WeakPtr
<MockOperation
> op1
=
135 (new MockUploadOperation(®istry
))->AsWeakPtr();
136 EXPECT_CALL(*op1
, DoCancel()).Times(0);
138 EXPECT_EQ(0U, observer
.status().size());
140 EXPECT_THAT(observer
.status(), ElementsAre(Progress(0, -1)));
141 op1
->NotifyProgress(0, 100);
142 EXPECT_THAT(observer
.status(), ElementsAre(Progress(0, 100)));
143 op1
->NotifyProgress(100, 100);
144 EXPECT_THAT(observer
.status(), ElementsAre(Progress(100, 100)));
145 op1
->NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED
);
146 // Contains one "COMPLETED" notification.
147 EXPECT_THAT(observer
.status(), ElementsAre(Progress(100, 100)));
148 // Then it is removed.
149 EXPECT_EQ(0U, registry
.GetProgressStatusList().size());
150 EXPECT_EQ(NULL
, op1
.get()); // deleted
153 TEST_F(GDataOperationRegistryTest
, OneCancel
) {
154 TestObserver observer
;
155 GDataOperationRegistry registry
;
156 registry
.DisableNotificationFrequencyControlForTest();
157 registry
.AddObserver(&observer
);
159 base::WeakPtr
<MockOperation
> op1
=
160 (new MockUploadOperation(®istry
))->AsWeakPtr();
161 EXPECT_CALL(*op1
, DoCancel());
163 EXPECT_EQ(0U, observer
.status().size());
165 EXPECT_THAT(observer
.status(), ElementsAre(Progress(0, -1)));
166 op1
->NotifyProgress(0, 100);
167 EXPECT_THAT(observer
.status(), ElementsAre(Progress(0, 100)));
168 registry
.CancelAll();
169 EXPECT_THAT(observer
.status(), ElementsAre(Progress(0, 100)));
170 EXPECT_EQ(0U, registry
.GetProgressStatusList().size());
171 EXPECT_EQ(NULL
, op1
.get()); // deleted
174 TEST_F(GDataOperationRegistryTest
, TwoSuccess
) {
175 TestObserver observer
;
176 GDataOperationRegistry registry
;
177 registry
.DisableNotificationFrequencyControlForTest();
178 registry
.AddObserver(&observer
);
180 base::WeakPtr
<MockOperation
> op1
=
181 (new MockUploadOperation(®istry
))->AsWeakPtr();
182 base::WeakPtr
<MockOperation
> op2
=
183 (new MockDownloadOperation(®istry
))->AsWeakPtr();
184 EXPECT_CALL(*op1
, DoCancel()).Times(0);
185 EXPECT_CALL(*op2
, DoCancel()).Times(0);
187 EXPECT_EQ(0U, observer
.status().size());
189 op1
->NotifyProgress(0, 100);
190 EXPECT_THAT(observer
.status(), ElementsAre(Progress(0, 100)));
192 op2
->NotifyProgress(0, 200);
193 op1
->NotifyProgress(50, 100);
194 EXPECT_THAT(observer
.status(), ElementsAre(Progress(50, 100),
196 op1
->NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED
);
197 EXPECT_THAT(observer
.status(), ElementsAre(Progress(50, 100),
199 EXPECT_EQ(1U, registry
.GetProgressStatusList().size());
200 op2
->NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED
);
201 EXPECT_THAT(observer
.status(), ElementsAre(Progress(0, 200)));
202 EXPECT_EQ(0U, registry
.GetProgressStatusList().size());
203 EXPECT_EQ(NULL
, op1
.get()); // deleted
204 EXPECT_EQ(NULL
, op2
.get()); // deleted
207 TEST_F(GDataOperationRegistryTest
, ThreeCancel
) {
208 TestObserver observer
;
209 GDataOperationRegistry registry
;
210 registry
.DisableNotificationFrequencyControlForTest();
211 registry
.AddObserver(&observer
);
213 base::WeakPtr
<MockOperation
> op1
=
214 (new MockUploadOperation(®istry
))->AsWeakPtr();
215 base::WeakPtr
<MockOperation
> op2
=
216 (new MockDownloadOperation(®istry
))->AsWeakPtr();
217 base::WeakPtr
<MockOperation
> op3
=
218 (new MockOtherOperation(®istry
))->AsWeakPtr();
219 EXPECT_CALL(*op1
, DoCancel());
220 EXPECT_CALL(*op2
, DoCancel());
221 EXPECT_CALL(*op3
, DoCancel());
223 EXPECT_EQ(0U, observer
.status().size());
225 EXPECT_EQ(1U, observer
.status().size());
227 EXPECT_EQ(2U, observer
.status().size());
229 EXPECT_EQ(2U, observer
.status().size()); // only upload/download is reported.
230 registry
.CancelAll();
231 EXPECT_EQ(1U, observer
.status().size()); // holds the last one "COMPLETED"
232 EXPECT_EQ(0U, registry
.GetProgressStatusList().size());
233 EXPECT_EQ(NULL
, op1
.get()); // deleted
234 EXPECT_EQ(NULL
, op2
.get()); // deleted
235 EXPECT_EQ(NULL
, op3
.get()); // deleted. CancelAll cares all operations.
238 TEST_F(GDataOperationRegistryTest
, RestartOperation
) {
239 TestObserver observer
;
240 GDataOperationRegistry registry
;
241 registry
.DisableNotificationFrequencyControlForTest();
242 registry
.AddObserver(&observer
);
244 base::WeakPtr
<MockOperation
> op1
=
245 (new MockUploadOperation(®istry
))->AsWeakPtr();
246 EXPECT_CALL(*op1
, DoCancel()).Times(0);
249 EXPECT_EQ(1U, registry
.GetProgressStatusList().size());
250 op1
->NotifyStart(); // restart
251 EXPECT_EQ(1U, registry
.GetProgressStatusList().size());
252 op1
->NotifyProgress(0, 200);
253 op1
->NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED
);
254 EXPECT_EQ(0U, registry
.GetProgressStatusList().size());
255 EXPECT_EQ(NULL
, op1
.get()); // deleted