| 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/google_apis/operation_registry.h" | 5 #include "chrome/browser/google_apis/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 OperationRegistry::Operation, | 20 class MockOperation : public OperationRegistry::Operation, |
| 21 public base::SupportsWeakPtr<MockOperation> { | 21 public base::SupportsWeakPtr<MockOperation> { |
| 22 public: | 22 public: |
| 23 MockOperation(OperationRegistry* registry, | 23 MockOperation(OperationRegistry* registry, |
| 24 OperationRegistry::OperationType type, | 24 OperationType type, |
| 25 const FilePath& path) | 25 const FilePath& path) |
| 26 : OperationRegistry::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 OperationRegistry::Operation::NotifyStart; | 31 using OperationRegistry::Operation::NotifyStart; |
| 32 using OperationRegistry::Operation::NotifyProgress; | 32 using OperationRegistry::Operation::NotifyProgress; |
| 33 using OperationRegistry::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(OperationRegistry* registry) | 38 explicit MockUploadOperation(OperationRegistry* registry) |
| 39 : MockOperation(registry, | 39 : MockOperation(registry, |
| 40 OperationRegistry::OPERATION_UPLOAD, | 40 OPERATION_UPLOAD, |
| 41 FilePath(FILE_PATH_LITERAL("/dummy/upload"))) {} | 41 FilePath(FILE_PATH_LITERAL("/dummy/upload"))) {} |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 class MockDownloadOperation : public MockOperation { | 44 class MockDownloadOperation : public MockOperation { |
| 45 public: | 45 public: |
| 46 explicit MockDownloadOperation(OperationRegistry* registry) | 46 explicit MockDownloadOperation(OperationRegistry* registry) |
| 47 : MockOperation(registry, | 47 : MockOperation(registry, |
| 48 OperationRegistry::OPERATION_DOWNLOAD, | 48 OPERATION_DOWNLOAD, |
| 49 FilePath(FILE_PATH_LITERAL("/dummy/download"))) {} | 49 FilePath(FILE_PATH_LITERAL("/dummy/download"))) {} |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 class MockOtherOperation : public MockOperation { | 52 class MockOtherOperation : public MockOperation { |
| 53 public: | 53 public: |
| 54 explicit MockOtherOperation(OperationRegistry* registry) | 54 explicit MockOtherOperation(OperationRegistry* registry) |
| 55 : MockOperation(registry, | 55 : MockOperation(registry, |
| 56 OperationRegistry::OPERATION_OTHER, | 56 OPERATION_OTHER, |
| 57 FilePath(FILE_PATH_LITERAL("/dummy/other"))) {} | 57 FilePath(FILE_PATH_LITERAL("/dummy/other"))) {} |
| 58 }; | 58 }; |
| 59 | 59 |
| 60 class TestObserver : public OperationRegistry::Observer { | 60 class TestObserver : public OperationRegistry::Observer { |
| 61 public: | 61 public: |
| 62 virtual void OnProgressUpdate( | 62 virtual void OnProgressUpdate( |
| 63 const std::vector<OperationRegistry::ProgressStatus>& list) | 63 const OperationProgressStatusList& list) OVERRIDE { |
| 64 OVERRIDE { | |
| 65 status_ = list; | 64 status_ = list; |
| 66 } | 65 } |
| 67 | 66 |
| 68 const std::vector<OperationRegistry::ProgressStatus>& status() const { | 67 const OperationProgressStatusList& status() const { |
| 69 return status_; | 68 return status_; |
| 70 } | 69 } |
| 71 | 70 |
| 72 private: | 71 private: |
| 73 std::vector<OperationRegistry::ProgressStatus> status_; | 72 OperationProgressStatusList status_; |
| 74 }; | 73 }; |
| 75 | 74 |
| 76 class ProgressMatcher | 75 class ProgressMatcher |
| 77 : public ::testing::MatcherInterface< | 76 : public ::testing::MatcherInterface<const OperationProgressStatus&> { |
| 78 const OperationRegistry::ProgressStatus&> { | |
| 79 public: | 77 public: |
| 80 ProgressMatcher(int64 expected_current, int64 expected_total) | 78 ProgressMatcher(int64 expected_current, int64 expected_total) |
| 81 : expected_current_(expected_current), | 79 : expected_current_(expected_current), |
| 82 expected_total_(expected_total) {} | 80 expected_total_(expected_total) {} |
| 83 | 81 |
| 84 virtual bool MatchAndExplain( | 82 virtual bool MatchAndExplain( |
| 85 const OperationRegistry::ProgressStatus& status, | 83 const OperationProgressStatus& status, |
| 86 testing::MatchResultListener* /* listener */) const OVERRIDE { | 84 testing::MatchResultListener* /* listener */) const OVERRIDE { |
| 87 return status.progress_current == expected_current_ && | 85 return status.progress_current == expected_current_ && |
| 88 status.progress_total == expected_total_; | 86 status.progress_total == expected_total_; |
| 89 } | 87 } |
| 90 | 88 |
| 91 virtual void DescribeTo(::std::ostream* os) const OVERRIDE { | 89 virtual void DescribeTo(::std::ostream* os) const OVERRIDE { |
| 92 *os << "current / total equals " << expected_current_ << " / " << | 90 *os << "current / total equals " << expected_current_ << " / " << |
| 93 expected_total_; | 91 expected_total_; |
| 94 } | 92 } |
| 95 | 93 |
| 96 virtual void DescribeNegationTo(::std::ostream* os) const OVERRIDE { | 94 virtual void DescribeNegationTo(::std::ostream* os) const OVERRIDE { |
| 97 *os << "current / total does not equal " << expected_current_ << " / " << | 95 *os << "current / total does not equal " << expected_current_ << " / " << |
| 98 expected_total_; | 96 expected_total_; |
| 99 } | 97 } |
| 100 | 98 |
| 101 private: | 99 private: |
| 102 const int64 expected_current_; | 100 const int64 expected_current_; |
| 103 const int64 expected_total_; | 101 const int64 expected_total_; |
| 104 }; | 102 }; |
| 105 | 103 |
| 106 testing::Matcher<const OperationRegistry::ProgressStatus&> Progress( | 104 testing::Matcher<const OperationProgressStatus&> Progress( |
| 107 int64 current, int64 total) { | 105 int64 current, int64 total) { |
| 108 return testing::MakeMatcher(new ProgressMatcher(current, total)); | 106 return testing::MakeMatcher(new ProgressMatcher(current, total)); |
| 109 } | 107 } |
| 110 | 108 |
| 111 } // namespace | 109 } // namespace |
| 112 | 110 |
| 113 // Pretty-prints ProgressStatus for testing purpose. | 111 // Pretty-prints OperationProgressStatus for testing purpose. |
| 114 std::ostream& operator<<(std::ostream& os, | 112 std::ostream& operator<<(std::ostream& os, |
| 115 const OperationRegistry::ProgressStatus& status) { | 113 const OperationProgressStatus& status) { |
| 116 return os << status.DebugString(); | 114 return os << status.DebugString(); |
| 117 } | 115 } |
| 118 | 116 |
| 119 class OperationRegistryTest : public testing::Test { | 117 class OperationRegistryTest : public testing::Test { |
| 120 protected: | 118 protected: |
| 121 OperationRegistryTest() | 119 OperationRegistryTest() |
| 122 : ui_thread_(content::BrowserThread::UI, &message_loop_) { | 120 : ui_thread_(content::BrowserThread::UI, &message_loop_) { |
| 123 } | 121 } |
| 124 MessageLoopForUI message_loop_; | 122 MessageLoopForUI message_loop_; |
| 125 content::TestBrowserThread ui_thread_; | 123 content::TestBrowserThread ui_thread_; |
| 126 }; | 124 }; |
| 127 | 125 |
| 128 TEST_F(OperationRegistryTest, OneSuccess) { | 126 TEST_F(OperationRegistryTest, OneSuccess) { |
| 129 TestObserver observer; | 127 TestObserver observer; |
| 130 OperationRegistry registry; | 128 OperationRegistry registry; |
| 131 registry.DisableNotificationFrequencyControlForTest(); | 129 registry.DisableNotificationFrequencyControlForTest(); |
| 132 registry.AddObserver(&observer); | 130 registry.AddObserver(&observer); |
| 133 | 131 |
| 134 base::WeakPtr<MockOperation> op1 = | 132 base::WeakPtr<MockOperation> op1 = |
| 135 (new MockUploadOperation(®istry))->AsWeakPtr(); | 133 (new MockUploadOperation(®istry))->AsWeakPtr(); |
| 136 EXPECT_CALL(*op1, DoCancel()).Times(0); | 134 EXPECT_CALL(*op1, DoCancel()).Times(0); |
| 137 | 135 |
| 138 EXPECT_EQ(0U, observer.status().size()); | 136 EXPECT_EQ(0U, observer.status().size()); |
| 139 op1->NotifyStart(); | 137 op1->NotifyStart(); |
| 140 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, -1))); | 138 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, -1))); |
| 141 op1->NotifyProgress(0, 100); | 139 op1->NotifyProgress(0, 100); |
| 142 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 100))); | 140 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 100))); |
| 143 op1->NotifyProgress(100, 100); | 141 op1->NotifyProgress(100, 100); |
| 144 EXPECT_THAT(observer.status(), ElementsAre(Progress(100, 100))); | 142 EXPECT_THAT(observer.status(), ElementsAre(Progress(100, 100))); |
| 145 op1->NotifyFinish(OperationRegistry::OPERATION_COMPLETED); | 143 op1->NotifyFinish(OPERATION_COMPLETED); |
| 146 // Contains one "COMPLETED" notification. | 144 // Contains one "COMPLETED" notification. |
| 147 EXPECT_THAT(observer.status(), ElementsAre(Progress(100, 100))); | 145 EXPECT_THAT(observer.status(), ElementsAre(Progress(100, 100))); |
| 148 // Then it is removed. | 146 // Then it is removed. |
| 149 EXPECT_EQ(0U, registry.GetProgressStatusList().size()); | 147 EXPECT_EQ(0U, registry.GetProgressStatusList().size()); |
| 150 EXPECT_EQ(NULL, op1.get()); // deleted | 148 EXPECT_EQ(NULL, op1.get()); // deleted |
| 151 } | 149 } |
| 152 | 150 |
| 153 TEST_F(OperationRegistryTest, OneCancel) { | 151 TEST_F(OperationRegistryTest, OneCancel) { |
| 154 TestObserver observer; | 152 TestObserver observer; |
| 155 OperationRegistry registry; | 153 OperationRegistry registry; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 186 | 184 |
| 187 EXPECT_EQ(0U, observer.status().size()); | 185 EXPECT_EQ(0U, observer.status().size()); |
| 188 op1->NotifyStart(); | 186 op1->NotifyStart(); |
| 189 op1->NotifyProgress(0, 100); | 187 op1->NotifyProgress(0, 100); |
| 190 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 100))); | 188 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 100))); |
| 191 op2->NotifyStart(); | 189 op2->NotifyStart(); |
| 192 op2->NotifyProgress(0, 200); | 190 op2->NotifyProgress(0, 200); |
| 193 op1->NotifyProgress(50, 100); | 191 op1->NotifyProgress(50, 100); |
| 194 EXPECT_THAT(observer.status(), ElementsAre(Progress(50, 100), | 192 EXPECT_THAT(observer.status(), ElementsAre(Progress(50, 100), |
| 195 Progress(0, 200))); | 193 Progress(0, 200))); |
| 196 op1->NotifyFinish(OperationRegistry::OPERATION_COMPLETED); | 194 op1->NotifyFinish(OPERATION_COMPLETED); |
| 197 EXPECT_THAT(observer.status(), ElementsAre(Progress(50, 100), | 195 EXPECT_THAT(observer.status(), ElementsAre(Progress(50, 100), |
| 198 Progress(0, 200))); | 196 Progress(0, 200))); |
| 199 EXPECT_EQ(1U, registry.GetProgressStatusList().size()); | 197 EXPECT_EQ(1U, registry.GetProgressStatusList().size()); |
| 200 op2->NotifyFinish(OperationRegistry::OPERATION_COMPLETED); | 198 op2->NotifyFinish(OPERATION_COMPLETED); |
| 201 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 200))); | 199 EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 200))); |
| 202 EXPECT_EQ(0U, registry.GetProgressStatusList().size()); | 200 EXPECT_EQ(0U, registry.GetProgressStatusList().size()); |
| 203 EXPECT_EQ(NULL, op1.get()); // deleted | 201 EXPECT_EQ(NULL, op1.get()); // deleted |
| 204 EXPECT_EQ(NULL, op2.get()); // deleted | 202 EXPECT_EQ(NULL, op2.get()); // deleted |
| 205 } | 203 } |
| 206 | 204 |
| 207 TEST_F(OperationRegistryTest, ThreeCancel) { | 205 TEST_F(OperationRegistryTest, ThreeCancel) { |
| 208 TestObserver observer; | 206 TestObserver observer; |
| 209 OperationRegistry registry; | 207 OperationRegistry registry; |
| 210 registry.DisableNotificationFrequencyControlForTest(); | 208 registry.DisableNotificationFrequencyControlForTest(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 | 241 |
| 244 base::WeakPtr<MockOperation> op1 = | 242 base::WeakPtr<MockOperation> op1 = |
| 245 (new MockUploadOperation(®istry))->AsWeakPtr(); | 243 (new MockUploadOperation(®istry))->AsWeakPtr(); |
| 246 EXPECT_CALL(*op1, DoCancel()).Times(0); | 244 EXPECT_CALL(*op1, DoCancel()).Times(0); |
| 247 | 245 |
| 248 op1->NotifyStart(); | 246 op1->NotifyStart(); |
| 249 EXPECT_EQ(1U, registry.GetProgressStatusList().size()); | 247 EXPECT_EQ(1U, registry.GetProgressStatusList().size()); |
| 250 op1->NotifyStart(); // restart | 248 op1->NotifyStart(); // restart |
| 251 EXPECT_EQ(1U, registry.GetProgressStatusList().size()); | 249 EXPECT_EQ(1U, registry.GetProgressStatusList().size()); |
| 252 op1->NotifyProgress(0, 200); | 250 op1->NotifyProgress(0, 200); |
| 253 op1->NotifyFinish(OperationRegistry::OPERATION_COMPLETED); | 251 op1->NotifyFinish(OPERATION_COMPLETED); |
| 254 EXPECT_EQ(0U, registry.GetProgressStatusList().size()); | 252 EXPECT_EQ(0U, registry.GetProgressStatusList().size()); |
| 255 EXPECT_EQ(NULL, op1.get()); // deleted | 253 EXPECT_EQ(NULL, op1.get()); // deleted |
| 256 } | 254 } |
| 257 | 255 |
| 258 } // namespace gdata | 256 } // namespace gdata |
| OLD | NEW |