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
|
// 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/google_apis/operation_registry.h"
#include "base/compiler_specific.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "chrome/browser/google_apis/operation_registry.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace google_apis {
namespace {
class MockOperation : public OperationRegistry::Operation,
public base::SupportsWeakPtr<MockOperation> {
public:
explicit MockOperation(OperationRegistry* registry)
: OperationRegistry::Operation(
registry,
base::FilePath(FILE_PATH_LITERAL("/dummy/download"))) {
}
MOCK_METHOD0(DoCancel, void());
// Make them public so that they can be called from test cases.
using OperationRegistry::Operation::NotifyStart;
using OperationRegistry::Operation::NotifyFinish;
using OperationRegistry::Operation::NotifySuspend;
using OperationRegistry::Operation::NotifyResume;
};
} // namespace
class OperationRegistryTest : public testing::Test {
protected:
OperationRegistryTest()
: ui_thread_(content::BrowserThread::UI, &message_loop_) {
}
MessageLoopForUI message_loop_;
content::TestBrowserThread ui_thread_;
};
TEST_F(OperationRegistryTest, OneSuccess) {
OperationRegistry registry;
base::WeakPtr<MockOperation> op1 =
(new MockOperation(®istry))->AsWeakPtr();
EXPECT_CALL(*op1, DoCancel()).Times(0);
op1->NotifyStart();
op1->NotifyFinish(OPERATION_COMPLETED);
EXPECT_EQ(NULL, op1.get()); // deleted
}
TEST_F(OperationRegistryTest, OneCancel) {
OperationRegistry registry;
base::WeakPtr<MockOperation> op1 =
(new MockOperation(®istry))->AsWeakPtr();
EXPECT_CALL(*op1, DoCancel());
op1->NotifyStart();
registry.CancelAll();
EXPECT_EQ(NULL, op1.get()); // deleted
}
TEST_F(OperationRegistryTest, TwoSuccess) {
OperationRegistry registry;
base::WeakPtr<MockOperation> op1 =
(new MockOperation(®istry))->AsWeakPtr();
base::WeakPtr<MockOperation> op2 =
(new MockOperation(®istry))->AsWeakPtr();
EXPECT_CALL(*op1, DoCancel()).Times(0);
EXPECT_CALL(*op2, DoCancel()).Times(0);
op1->NotifyStart();
op2->NotifyStart();
op1->NotifyFinish(OPERATION_COMPLETED);
op2->NotifyFinish(OPERATION_COMPLETED);
EXPECT_EQ(NULL, op1.get()); // deleted
EXPECT_EQ(NULL, op2.get()); // deleted
}
TEST_F(OperationRegistryTest, RestartOperation) {
OperationRegistry registry;
base::WeakPtr<MockOperation> op1 =
(new MockOperation(®istry))->AsWeakPtr();
EXPECT_CALL(*op1, DoCancel()).Times(0);
op1->NotifyStart();
op1->NotifyStart(); // restart
op1->NotifyFinish(OPERATION_COMPLETED);
EXPECT_EQ(NULL, op1.get()); // deleted
}
TEST_F(OperationRegistryTest, SuspendCancel) {
OperationRegistry registry;
// Suspend-then-resume is a hack in OperationRegistry to tie physically
// split but logically single operation (= chunked uploading split into
// multiple HTTP requests). When the "logically-single" operation is
// canceled between the two physical operations,
// |----op1----| CANCEL! |----op2----|
// the cancellation is notified to the callback function associated with
// op2, not op1. This is because, op1's callback is already invoked at this
// point to notify the completion of the physical operation. Completion
// callback must not be called more than once.
base::WeakPtr<MockOperation> op1 =
(new MockOperation(®istry))->AsWeakPtr();
EXPECT_CALL(*op1, DoCancel()).Times(0);
op1->NotifyStart();
op1->NotifySuspend();
registry.CancelAll();
EXPECT_EQ(NULL, op1.get()); // deleted
base::WeakPtr<MockOperation> op2 =
(new MockOperation(®istry))->AsWeakPtr();
EXPECT_CALL(*op2, DoCancel()).Times(1);
op2->NotifyResume();
EXPECT_EQ(NULL, op2.get()); // deleted
}
} // namespace google_apis
|