OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/gdata/gdata_operation_registry.h" | 5 #include "chrome/browser/chromeos/gdata/operation_registry.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "content/public/test/test_browser_thread.h" | 10 #include "content/public/test/test_browser_thread.h" |
11 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 using testing::ElementsAre; | 14 using testing::ElementsAre; |
15 | 15 |
16 namespace gdata { | 16 namespace gdata { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 class MockOperation : public GDataOperationRegistry::Operation, | 20 class MockOperation : public OperationRegistry::Operation, |
21 public base::SupportsWeakPtr<MockOperation> { | 21 public base::SupportsWeakPtr<MockOperation> { |
22 public: | 22 public: |
23 MockOperation(GDataOperationRegistry* registry, | 23 MockOperation(OperationRegistry* registry, |
24 GDataOperationRegistry::OperationType type, | 24 OperationRegistry::OperationType type, |
25 const FilePath& path) | 25 const FilePath& path) |
26 : GDataOperationRegistry::Operation(registry, type, path) {} | 26 : OperationRegistry::Operation(registry, type, path) {} |
27 | 27 |
28 MOCK_METHOD0(DoCancel, void()); | 28 MOCK_METHOD0(DoCancel, void()); |
29 | 29 |
30 // Make them public so that they can be called from test cases. | 30 // Make them public so that they can be called from test cases. |
31 using GDataOperationRegistry::Operation::NotifyStart; | 31 using OperationRegistry::Operation::NotifyStart; |
32 using GDataOperationRegistry::Operation::NotifyProgress; | 32 using OperationRegistry::Operation::NotifyProgress; |
33 using GDataOperationRegistry::Operation::NotifyFinish; | 33 using OperationRegistry::Operation::NotifyFinish; |
34 }; | 34 }; |
35 | 35 |
36 class MockUploadOperation : public MockOperation { | 36 class MockUploadOperation : public MockOperation { |
37 public: | 37 public: |
38 explicit MockUploadOperation(GDataOperationRegistry* registry) | 38 explicit MockUploadOperation(OperationRegistry* registry) |
39 : MockOperation(registry, | 39 : MockOperation(registry, |
40 GDataOperationRegistry::OPERATION_UPLOAD, | 40 OperationRegistry::OPERATION_UPLOAD, |
41 FilePath("/dummy/upload")) {} | 41 FilePath("/dummy/upload")) {} |
42 }; | 42 }; |
43 | 43 |
44 class MockDownloadOperation : public MockOperation { | 44 class MockDownloadOperation : public MockOperation { |
45 public: | 45 public: |
46 explicit MockDownloadOperation(GDataOperationRegistry* registry) | 46 explicit MockDownloadOperation(OperationRegistry* registry) |
47 : MockOperation(registry, | 47 : MockOperation(registry, |
48 GDataOperationRegistry::OPERATION_DOWNLOAD, | 48 OperationRegistry::OPERATION_DOWNLOAD, |
49 FilePath("/dummy/download")) {} | 49 FilePath("/dummy/download")) {} |
50 }; | 50 }; |
51 | 51 |
52 class MockOtherOperation : public MockOperation { | 52 class MockOtherOperation : public MockOperation { |
53 public: | 53 public: |
54 explicit MockOtherOperation(GDataOperationRegistry* registry) | 54 explicit MockOtherOperation(OperationRegistry* registry) |
55 : MockOperation(registry, | 55 : MockOperation(registry, |
56 GDataOperationRegistry::OPERATION_OTHER, | 56 OperationRegistry::OPERATION_OTHER, |
57 FilePath("/dummy/other")) {} | 57 FilePath("/dummy/other")) {} |
58 }; | 58 }; |
59 | 59 |
60 class TestObserver : public GDataOperationRegistry::Observer { | 60 class TestObserver : public OperationRegistry::Observer { |
61 public: | 61 public: |
62 virtual void OnProgressUpdate( | 62 virtual void OnProgressUpdate( |
63 const std::vector<GDataOperationRegistry::ProgressStatus>& list) | 63 const std::vector<OperationRegistry::ProgressStatus>& list) |
64 OVERRIDE { | 64 OVERRIDE { |
65 status_ = list; | 65 status_ = list; |
66 } | 66 } |
67 | 67 |
68 const std::vector<GDataOperationRegistry::ProgressStatus>& status() const { | 68 const std::vector<OperationRegistry::ProgressStatus>& status() const { |
69 return status_; | 69 return status_; |
70 } | 70 } |
71 | 71 |
72 private: | 72 private: |
73 std::vector<GDataOperationRegistry::ProgressStatus> status_; | 73 std::vector<OperationRegistry::ProgressStatus> status_; |
74 }; | 74 }; |
75 | 75 |
76 class ProgressMatcher | 76 class ProgressMatcher |
77 : public ::testing::MatcherInterface< | 77 : public ::testing::MatcherInterface< |
78 const GDataOperationRegistry::ProgressStatus&> { | 78 const OperationRegistry::ProgressStatus&> { |
79 public: | 79 public: |
80 ProgressMatcher(int64 expected_current, int64 expected_total) | 80 ProgressMatcher(int64 expected_current, int64 expected_total) |
81 : expected_current_(expected_current), | 81 : expected_current_(expected_current), |
82 expected_total_(expected_total) {} | 82 expected_total_(expected_total) {} |
83 | 83 |
84 virtual bool MatchAndExplain( | 84 virtual bool MatchAndExplain( |
85 const GDataOperationRegistry::ProgressStatus& status, | 85 const OperationRegistry::ProgressStatus& status, |
86 testing::MatchResultListener* /* listener */) const OVERRIDE { | 86 testing::MatchResultListener* /* listener */) const OVERRIDE { |
87 return status.progress_current == expected_current_ && | 87 return status.progress_current == expected_current_ && |
88 status.progress_total == expected_total_; | 88 status.progress_total == expected_total_; |
89 } | 89 } |
90 | 90 |
91 virtual void DescribeTo(::std::ostream* os) const OVERRIDE { | 91 virtual void DescribeTo(::std::ostream* os) const OVERRIDE { |
92 *os << "current / total equals " << expected_current_ << " / " << | 92 *os << "current / total equals " << expected_current_ << " / " << |
93 expected_total_; | 93 expected_total_; |
94 } | 94 } |
95 | 95 |
96 virtual void DescribeNegationTo(::std::ostream* os) const OVERRIDE { | 96 virtual void DescribeNegationTo(::std::ostream* os) const OVERRIDE { |
97 *os << "current / total does not equal " << expected_current_ << " / " << | 97 *os << "current / total does not equal " << expected_current_ << " / " << |
98 expected_total_; | 98 expected_total_; |
99 } | 99 } |
100 | 100 |
101 private: | 101 private: |
102 const int64 expected_current_; | 102 const int64 expected_current_; |
103 const int64 expected_total_; | 103 const int64 expected_total_; |
104 }; | 104 }; |
105 | 105 |
106 testing::Matcher<const GDataOperationRegistry::ProgressStatus&> Progress( | 106 testing::Matcher<const OperationRegistry::ProgressStatus&> Progress( |
107 int64 current, int64 total) { | 107 int64 current, int64 total) { |
108 return testing::MakeMatcher(new ProgressMatcher(current, total)); | 108 return testing::MakeMatcher(new ProgressMatcher(current, total)); |
109 } | 109 } |
110 | 110 |
111 } // namespace | 111 } // namespace |
112 | 112 |
113 // Pretty-prints ProgressStatus for testing purpose. | 113 // Pretty-prints ProgressStatus for testing purpose. |
114 std::ostream& operator<<(std::ostream& os, | 114 std::ostream& operator<<(std::ostream& os, |
115 const GDataOperationRegistry::ProgressStatus& status) { | 115 const OperationRegistry::ProgressStatus& status) { |
116 return os << status.DebugString(); | 116 return os << status.DebugString(); |
117 } | 117 } |
118 | 118 |
119 class GDataOperationRegistryTest : public testing::Test { | 119 class OperationRegistryTest : public testing::Test { |
120 protected: | 120 protected: |
121 GDataOperationRegistryTest() | 121 OperationRegistryTest() |
122 : ui_thread_(content::BrowserThread::UI, &message_loop_) { | 122 : ui_thread_(content::BrowserThread::UI, &message_loop_) { |
123 } | 123 } |
124 MessageLoopForUI message_loop_; | 124 MessageLoopForUI message_loop_; |
125 content::TestBrowserThread ui_thread_; | 125 content::TestBrowserThread ui_thread_; |
126 }; | 126 }; |
127 | 127 |
128 TEST_F(GDataOperationRegistryTest, OneSuccess) { | 128 TEST_F(OperationRegistryTest, OneSuccess) { |
129 TestObserver observer; | 129 TestObserver observer; |
130 GDataOperationRegistry registry; | 130 OperationRegistry registry; |
131 registry.DisableNotificationFrequencyControlForTest(); | 131 registry.DisableNotificationFrequencyControlForTest(); |
132 registry.AddObserver(&observer); | 132 registry.AddObserver(&observer); |
133 | 133 |
134 base::WeakPtr<MockOperation> op1 = | 134 base::WeakPtr<MockOperation> op1 = |
135 (new MockUploadOperation(®istry))->AsWeakPtr(); | 135 (new MockUploadOperation(®istry))->AsWeakPtr(); |
136 EXPECT_CALL(*op1, DoCancel()).Times(0); | 136 EXPECT_CALL(*op1, DoCancel()).Times(0); |
137 | 137 |
138 EXPECT_EQ(0U, observer.status().size()); | 138 EXPECT_EQ(0U, observer.status().size()); |
139 op1->NotifyStart(); | 139 op1->NotifyStart(); |
140 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, -1))); | 140 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, -1))); |
141 op1->NotifyProgress(0, 100); | 141 op1->NotifyProgress(0, 100); |
142 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 100))); | 142 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 100))); |
143 op1->NotifyProgress(100, 100); | 143 op1->NotifyProgress(100, 100); |
144 EXPECT_THAT(observer.status(), ElementsAre(Progress(100, 100))); | 144 EXPECT_THAT(observer.status(), ElementsAre(Progress(100, 100))); |
145 op1->NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED); | 145 op1->NotifyFinish(OperationRegistry::OPERATION_COMPLETED); |
146 // Contains one "COMPLETED" notification. | 146 // Contains one "COMPLETED" notification. |
147 EXPECT_THAT(observer.status(), ElementsAre(Progress(100, 100))); | 147 EXPECT_THAT(observer.status(), ElementsAre(Progress(100, 100))); |
148 // Then it is removed. | 148 // Then it is removed. |
149 EXPECT_EQ(0U, registry.GetProgressStatusList().size()); | 149 EXPECT_EQ(0U, registry.GetProgressStatusList().size()); |
150 EXPECT_EQ(NULL, op1.get()); // deleted | 150 EXPECT_EQ(NULL, op1.get()); // deleted |
151 } | 151 } |
152 | 152 |
153 TEST_F(GDataOperationRegistryTest, OneCancel) { | 153 TEST_F(OperationRegistryTest, OneCancel) { |
154 TestObserver observer; | 154 TestObserver observer; |
155 GDataOperationRegistry registry; | 155 OperationRegistry registry; |
156 registry.DisableNotificationFrequencyControlForTest(); | 156 registry.DisableNotificationFrequencyControlForTest(); |
157 registry.AddObserver(&observer); | 157 registry.AddObserver(&observer); |
158 | 158 |
159 base::WeakPtr<MockOperation> op1 = | 159 base::WeakPtr<MockOperation> op1 = |
160 (new MockUploadOperation(®istry))->AsWeakPtr(); | 160 (new MockUploadOperation(®istry))->AsWeakPtr(); |
161 EXPECT_CALL(*op1, DoCancel()); | 161 EXPECT_CALL(*op1, DoCancel()); |
162 | 162 |
163 EXPECT_EQ(0U, observer.status().size()); | 163 EXPECT_EQ(0U, observer.status().size()); |
164 op1->NotifyStart(); | 164 op1->NotifyStart(); |
165 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, -1))); | 165 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, -1))); |
166 op1->NotifyProgress(0, 100); | 166 op1->NotifyProgress(0, 100); |
167 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 100))); | 167 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 100))); |
168 registry.CancelAll(); | 168 registry.CancelAll(); |
169 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 100))); | 169 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 100))); |
170 EXPECT_EQ(0U, registry.GetProgressStatusList().size()); | 170 EXPECT_EQ(0U, registry.GetProgressStatusList().size()); |
171 EXPECT_EQ(NULL, op1.get()); // deleted | 171 EXPECT_EQ(NULL, op1.get()); // deleted |
172 } | 172 } |
173 | 173 |
174 TEST_F(GDataOperationRegistryTest, TwoSuccess) { | 174 TEST_F(OperationRegistryTest, TwoSuccess) { |
175 TestObserver observer; | 175 TestObserver observer; |
176 GDataOperationRegistry registry; | 176 OperationRegistry registry; |
177 registry.DisableNotificationFrequencyControlForTest(); | 177 registry.DisableNotificationFrequencyControlForTest(); |
178 registry.AddObserver(&observer); | 178 registry.AddObserver(&observer); |
179 | 179 |
180 base::WeakPtr<MockOperation> op1 = | 180 base::WeakPtr<MockOperation> op1 = |
181 (new MockUploadOperation(®istry))->AsWeakPtr(); | 181 (new MockUploadOperation(®istry))->AsWeakPtr(); |
182 base::WeakPtr<MockOperation> op2 = | 182 base::WeakPtr<MockOperation> op2 = |
183 (new MockDownloadOperation(®istry))->AsWeakPtr(); | 183 (new MockDownloadOperation(®istry))->AsWeakPtr(); |
184 EXPECT_CALL(*op1, DoCancel()).Times(0); | 184 EXPECT_CALL(*op1, DoCancel()).Times(0); |
185 EXPECT_CALL(*op2, DoCancel()).Times(0); | 185 EXPECT_CALL(*op2, DoCancel()).Times(0); |
186 | 186 |
187 EXPECT_EQ(0U, observer.status().size()); | 187 EXPECT_EQ(0U, observer.status().size()); |
188 op1->NotifyStart(); | 188 op1->NotifyStart(); |
189 op1->NotifyProgress(0, 100); | 189 op1->NotifyProgress(0, 100); |
190 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 100))); | 190 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 100))); |
191 op2->NotifyStart(); | 191 op2->NotifyStart(); |
192 op2->NotifyProgress(0, 200); | 192 op2->NotifyProgress(0, 200); |
193 op1->NotifyProgress(50, 100); | 193 op1->NotifyProgress(50, 100); |
194 EXPECT_THAT(observer.status(), ElementsAre(Progress(50, 100), | 194 EXPECT_THAT(observer.status(), ElementsAre(Progress(50, 100), |
195 Progress(0, 200))); | 195 Progress(0, 200))); |
196 op1->NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED); | 196 op1->NotifyFinish(OperationRegistry::OPERATION_COMPLETED); |
197 EXPECT_THAT(observer.status(), ElementsAre(Progress(50, 100), | 197 EXPECT_THAT(observer.status(), ElementsAre(Progress(50, 100), |
198 Progress(0, 200))); | 198 Progress(0, 200))); |
199 EXPECT_EQ(1U, registry.GetProgressStatusList().size()); | 199 EXPECT_EQ(1U, registry.GetProgressStatusList().size()); |
200 op2->NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED); | 200 op2->NotifyFinish(OperationRegistry::OPERATION_COMPLETED); |
201 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 200))); | 201 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 200))); |
202 EXPECT_EQ(0U, registry.GetProgressStatusList().size()); | 202 EXPECT_EQ(0U, registry.GetProgressStatusList().size()); |
203 EXPECT_EQ(NULL, op1.get()); // deleted | 203 EXPECT_EQ(NULL, op1.get()); // deleted |
204 EXPECT_EQ(NULL, op2.get()); // deleted | 204 EXPECT_EQ(NULL, op2.get()); // deleted |
205 } | 205 } |
206 | 206 |
207 TEST_F(GDataOperationRegistryTest, ThreeCancel) { | 207 TEST_F(OperationRegistryTest, ThreeCancel) { |
208 TestObserver observer; | 208 TestObserver observer; |
209 GDataOperationRegistry registry; | 209 OperationRegistry registry; |
210 registry.DisableNotificationFrequencyControlForTest(); | 210 registry.DisableNotificationFrequencyControlForTest(); |
211 registry.AddObserver(&observer); | 211 registry.AddObserver(&observer); |
212 | 212 |
213 base::WeakPtr<MockOperation> op1 = | 213 base::WeakPtr<MockOperation> op1 = |
214 (new MockUploadOperation(®istry))->AsWeakPtr(); | 214 (new MockUploadOperation(®istry))->AsWeakPtr(); |
215 base::WeakPtr<MockOperation> op2 = | 215 base::WeakPtr<MockOperation> op2 = |
216 (new MockDownloadOperation(®istry))->AsWeakPtr(); | 216 (new MockDownloadOperation(®istry))->AsWeakPtr(); |
217 base::WeakPtr<MockOperation> op3 = | 217 base::WeakPtr<MockOperation> op3 = |
218 (new MockOtherOperation(®istry))->AsWeakPtr(); | 218 (new MockOtherOperation(®istry))->AsWeakPtr(); |
219 EXPECT_CALL(*op1, DoCancel()); | 219 EXPECT_CALL(*op1, DoCancel()); |
220 EXPECT_CALL(*op2, DoCancel()); | 220 EXPECT_CALL(*op2, DoCancel()); |
221 EXPECT_CALL(*op3, DoCancel()); | 221 EXPECT_CALL(*op3, DoCancel()); |
222 | 222 |
223 EXPECT_EQ(0U, observer.status().size()); | 223 EXPECT_EQ(0U, observer.status().size()); |
224 op1->NotifyStart(); | 224 op1->NotifyStart(); |
225 EXPECT_EQ(1U, observer.status().size()); | 225 EXPECT_EQ(1U, observer.status().size()); |
226 op2->NotifyStart(); | 226 op2->NotifyStart(); |
227 EXPECT_EQ(2U, observer.status().size()); | 227 EXPECT_EQ(2U, observer.status().size()); |
228 op3->NotifyStart(); | 228 op3->NotifyStart(); |
229 EXPECT_EQ(2U, observer.status().size()); // only upload/download is reported. | 229 EXPECT_EQ(2U, observer.status().size()); // only upload/download is reported. |
230 registry.CancelAll(); | 230 registry.CancelAll(); |
231 EXPECT_EQ(1U, observer.status().size()); // holds the last one "COMPLETED" | 231 EXPECT_EQ(1U, observer.status().size()); // holds the last one "COMPLETED" |
232 EXPECT_EQ(0U, registry.GetProgressStatusList().size()); | 232 EXPECT_EQ(0U, registry.GetProgressStatusList().size()); |
233 EXPECT_EQ(NULL, op1.get()); // deleted | 233 EXPECT_EQ(NULL, op1.get()); // deleted |
234 EXPECT_EQ(NULL, op2.get()); // deleted | 234 EXPECT_EQ(NULL, op2.get()); // deleted |
235 EXPECT_EQ(NULL, op3.get()); // deleted. CancelAll cares all operations. | 235 EXPECT_EQ(NULL, op3.get()); // deleted. CancelAll cares all operations. |
236 } | 236 } |
237 | 237 |
238 TEST_F(GDataOperationRegistryTest, RestartOperation) { | 238 TEST_F(OperationRegistryTest, RestartOperation) { |
239 TestObserver observer; | 239 TestObserver observer; |
240 GDataOperationRegistry registry; | 240 OperationRegistry registry; |
241 registry.DisableNotificationFrequencyControlForTest(); | 241 registry.DisableNotificationFrequencyControlForTest(); |
242 registry.AddObserver(&observer); | 242 registry.AddObserver(&observer); |
243 | 243 |
244 base::WeakPtr<MockOperation> op1 = | 244 base::WeakPtr<MockOperation> op1 = |
245 (new MockUploadOperation(®istry))->AsWeakPtr(); | 245 (new MockUploadOperation(®istry))->AsWeakPtr(); |
246 EXPECT_CALL(*op1, DoCancel()).Times(0); | 246 EXPECT_CALL(*op1, DoCancel()).Times(0); |
247 | 247 |
248 op1->NotifyStart(); | 248 op1->NotifyStart(); |
249 EXPECT_EQ(1U, registry.GetProgressStatusList().size()); | 249 EXPECT_EQ(1U, registry.GetProgressStatusList().size()); |
250 op1->NotifyStart(); // restart | 250 op1->NotifyStart(); // restart |
251 EXPECT_EQ(1U, registry.GetProgressStatusList().size()); | 251 EXPECT_EQ(1U, registry.GetProgressStatusList().size()); |
252 op1->NotifyProgress(0, 200); | 252 op1->NotifyProgress(0, 200); |
253 op1->NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED); | 253 op1->NotifyFinish(OperationRegistry::OPERATION_COMPLETED); |
254 EXPECT_EQ(0U, registry.GetProgressStatusList().size()); | 254 EXPECT_EQ(0U, registry.GetProgressStatusList().size()); |
255 EXPECT_EQ(NULL, op1.get()); // deleted | 255 EXPECT_EQ(NULL, op1.get()); // deleted |
256 } | 256 } |
257 | 257 |
258 } // namespace gdata | 258 } // namespace gdata |
OLD | NEW |