summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/gdata/gdata_operation_registry_unittest.cc
blob: 9d3fe6dc7f18ecd65fceb56e8017147d568f7727 (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright (c) 2012 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 "chrome/browser/chromeos/gdata/gdata_operation_registry.h"

#include "base/compiler_specific.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::ElementsAre;

namespace gdata {

namespace {

class MockOperation : public GDataOperationRegistry::Operation,
                      public base::SupportsWeakPtr<MockOperation> {
 public:
  MockOperation(GDataOperationRegistry* registry,
                GDataOperationRegistry::OperationType type,
                const FilePath& path)
      : GDataOperationRegistry::Operation(registry, type, path) {}

  MOCK_METHOD0(DoCancel, void());

  // Make them public so that they can be called from test cases.
  using GDataOperationRegistry::Operation::NotifyStart;
  using GDataOperationRegistry::Operation::NotifyProgress;
  using GDataOperationRegistry::Operation::NotifyFinish;
};

class MockUploadOperation : public MockOperation {
 public:
  explicit MockUploadOperation(GDataOperationRegistry* registry)
      : MockOperation(registry,
                      GDataOperationRegistry::OPERATION_UPLOAD,
                      FilePath("/dummy/upload")) {}
};

class MockDownloadOperation : public MockOperation {
 public:
  explicit MockDownloadOperation(GDataOperationRegistry* registry)
      : MockOperation(registry,
                      GDataOperationRegistry::OPERATION_DOWNLOAD,
                      FilePath("/dummy/download")) {}
};

class MockOtherOperation : public MockOperation {
 public:
  explicit MockOtherOperation(GDataOperationRegistry* registry)
      : MockOperation(registry,
                      GDataOperationRegistry::OPERATION_OTHER,
                      FilePath("/dummy/other")) {}
};

class TestObserver : public GDataOperationRegistry::Observer {
 public:
  virtual void OnProgressUpdate(
      const std::vector<GDataOperationRegistry::ProgressStatus>& list)
      OVERRIDE {
    status_ = list;
  }

  const std::vector<GDataOperationRegistry::ProgressStatus>& status() const {
    return status_;
  }

 private:
  std::vector<GDataOperationRegistry::ProgressStatus> status_;
};

class ProgressMatcher
    : public ::testing::MatcherInterface<
        const GDataOperationRegistry::ProgressStatus&> {
 public:
  ProgressMatcher(int64 expected_current, int64 expected_total)
      : expected_current_(expected_current),
        expected_total_(expected_total) {}

  virtual bool MatchAndExplain(
      const GDataOperationRegistry::ProgressStatus& status,
      testing::MatchResultListener* /* listener */) const OVERRIDE {
    return status.progress_current == expected_current_ &&
        status.progress_total == expected_total_;
  }

  virtual void DescribeTo(::std::ostream* os) const OVERRIDE {
    *os << "current / total equals " << expected_current_ << " / " <<
        expected_total_;
  }

  virtual void DescribeNegationTo(::std::ostream* os) const OVERRIDE {
    *os << "current / total does not equal " << expected_current_ << " / " <<
        expected_total_;
  }

 private:
  const int64 expected_current_;
  const int64 expected_total_;
};

testing::Matcher<const GDataOperationRegistry::ProgressStatus&> Progress(
    int64 current, int64 total) {
  return testing::MakeMatcher(new ProgressMatcher(current, total));
}

}  // namespace

// Pretty-prints ProgressStatus for testing purpose.
std::ostream& operator<<(std::ostream& os,
                         const GDataOperationRegistry::ProgressStatus& status) {
  return os << status.DebugString();
}

class GDataOperationRegistryTest : public testing::Test {
 protected:
  GDataOperationRegistryTest()
      : ui_thread_(content::BrowserThread::UI, &message_loop_) {
  }
  MessageLoopForUI message_loop_;
  content::TestBrowserThread ui_thread_;
};

TEST_F(GDataOperationRegistryTest, OneSuccess) {
  TestObserver observer;
  GDataOperationRegistry registry;
  registry.DisableNotificationFrequencyControlForTest();
  registry.AddObserver(&observer);

  base::WeakPtr<MockOperation> op1 =
      (new MockUploadOperation(&registry))->AsWeakPtr();
  EXPECT_CALL(*op1, DoCancel()).Times(0);

  EXPECT_EQ(0U, observer.status().size());
  op1->NotifyStart();
  EXPECT_THAT(observer.status(), ElementsAre(Progress(0, -1)));
  op1->NotifyProgress(0, 100);
  EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 100)));
  op1->NotifyProgress(100, 100);
  EXPECT_THAT(observer.status(), ElementsAre(Progress(100, 100)));
  op1->NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED);
  // Contains one "COMPLETED" notification.
  EXPECT_THAT(observer.status(), ElementsAre(Progress(100, 100)));
  // Then it is removed.
  EXPECT_EQ(0U, registry.GetProgressStatusList().size());
  EXPECT_EQ(NULL, op1.get()); // deleted
}

TEST_F(GDataOperationRegistryTest, OneCancel) {
  TestObserver observer;
  GDataOperationRegistry registry;
  registry.DisableNotificationFrequencyControlForTest();
  registry.AddObserver(&observer);

  base::WeakPtr<MockOperation> op1 =
      (new MockUploadOperation(&registry))->AsWeakPtr();
  EXPECT_CALL(*op1, DoCancel());

  EXPECT_EQ(0U, observer.status().size());
  op1->NotifyStart();
  EXPECT_THAT(observer.status(), ElementsAre(Progress(0, -1)));
  op1->NotifyProgress(0, 100);
  EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 100)));
  registry.CancelAll();
  EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 100)));
  EXPECT_EQ(0U, registry.GetProgressStatusList().size());
  EXPECT_EQ(NULL, op1.get()); // deleted
}

TEST_F(GDataOperationRegistryTest, TwoSuccess) {
  TestObserver observer;
  GDataOperationRegistry registry;
  registry.DisableNotificationFrequencyControlForTest();
  registry.AddObserver(&observer);

  base::WeakPtr<MockOperation> op1 =
      (new MockUploadOperation(&registry))->AsWeakPtr();
  base::WeakPtr<MockOperation> op2 =
      (new MockDownloadOperation(&registry))->AsWeakPtr();
  EXPECT_CALL(*op1, DoCancel()).Times(0);
  EXPECT_CALL(*op2, DoCancel()).Times(0);

  EXPECT_EQ(0U, observer.status().size());
  op1->NotifyStart();
  op1->NotifyProgress(0, 100);
  EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 100)));
  op2->NotifyStart();
  op2->NotifyProgress(0, 200);
  op1->NotifyProgress(50, 100);
  EXPECT_THAT(observer.status(), ElementsAre(Progress(50, 100),
                                             Progress(0, 200)));
  op1->NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED);
  EXPECT_THAT(observer.status(), ElementsAre(Progress(50, 100),
                                             Progress(0, 200)));
  EXPECT_EQ(1U, registry.GetProgressStatusList().size());
  op2->NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED);
  EXPECT_THAT(observer.status(), ElementsAre(Progress(0, 200)));
  EXPECT_EQ(0U, registry.GetProgressStatusList().size());
  EXPECT_EQ(NULL, op1.get()); // deleted
  EXPECT_EQ(NULL, op2.get()); // deleted
}

TEST_F(GDataOperationRegistryTest, ThreeCancel) {
  TestObserver observer;
  GDataOperationRegistry registry;
  registry.DisableNotificationFrequencyControlForTest();
  registry.AddObserver(&observer);

  base::WeakPtr<MockOperation> op1 =
      (new MockUploadOperation(&registry))->AsWeakPtr();
  base::WeakPtr<MockOperation> op2 =
      (new MockDownloadOperation(&registry))->AsWeakPtr();
  base::WeakPtr<MockOperation> op3 =
      (new MockOtherOperation(&registry))->AsWeakPtr();
  EXPECT_CALL(*op1, DoCancel());
  EXPECT_CALL(*op2, DoCancel());
  EXPECT_CALL(*op3, DoCancel());

  EXPECT_EQ(0U, observer.status().size());
  op1->NotifyStart();
  EXPECT_EQ(1U, observer.status().size());
  op2->NotifyStart();
  EXPECT_EQ(2U, observer.status().size());
  op3->NotifyStart();
  EXPECT_EQ(2U, observer.status().size()); // only upload/download is reported.
  registry.CancelAll();
  EXPECT_EQ(1U, observer.status().size()); // holds the last one "COMPLETED"
  EXPECT_EQ(0U, registry.GetProgressStatusList().size());
  EXPECT_EQ(NULL, op1.get()); // deleted
  EXPECT_EQ(NULL, op2.get()); // deleted
  EXPECT_EQ(NULL, op3.get()); // deleted. CancelAll cares all operations.
}

TEST_F(GDataOperationRegistryTest, RestartOperation) {
  TestObserver observer;
  GDataOperationRegistry registry;
  registry.DisableNotificationFrequencyControlForTest();
  registry.AddObserver(&observer);

  base::WeakPtr<MockOperation> op1 =
      (new MockUploadOperation(&registry))->AsWeakPtr();
  EXPECT_CALL(*op1, DoCancel()).Times(0);

  op1->NotifyStart();
  EXPECT_EQ(1U, registry.GetProgressStatusList().size());
  op1->NotifyStart(); // restart
  EXPECT_EQ(1U, registry.GetProgressStatusList().size());
  op1->NotifyProgress(0, 200);
  op1->NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED);
  EXPECT_EQ(0U, registry.GetProgressStatusList().size());
  EXPECT_EQ(NULL, op1.get()); // deleted
}

}  // namespace gdata