summaryrefslogtreecommitdiffstats
path: root/chrome/browser/google_apis/request_registry_unittest.cc
blob: 3d891dc660e4ac05bed64ef6f33400950fdd1747 (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
// 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/request_registry.h"

#include "base/compiler_specific.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "chrome/browser/google_apis/request_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 MockRequest : public RequestRegistry::Request,
                    public base::SupportsWeakPtr<MockRequest> {
 public:
  explicit MockRequest(RequestRegistry* registry)
      : RequestRegistry::Request(
          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 RequestRegistry::Request::NotifyStart;
  using RequestRegistry::Request::NotifyFinish;
  using RequestRegistry::Request::NotifySuspend;
  using RequestRegistry::Request::NotifyResume;
};

}  // namespace

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

TEST_F(RequestRegistryTest, OneSuccess) {
  RequestRegistry registry;

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

  op1->NotifyStart();
  op1->NotifyFinish(REQUEST_COMPLETED);
  EXPECT_EQ(NULL, op1.get());  // deleted
}

TEST_F(RequestRegistryTest, OneCancel) {
  RequestRegistry registry;

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

  op1->NotifyStart();
  registry.CancelAll();
  EXPECT_EQ(NULL, op1.get());  // deleted
}

TEST_F(RequestRegistryTest, TwoSuccess) {
  RequestRegistry registry;

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

  op1->NotifyStart();
  op2->NotifyStart();
  op1->NotifyFinish(REQUEST_COMPLETED);
  op2->NotifyFinish(REQUEST_COMPLETED);
  EXPECT_EQ(NULL, op1.get());  // deleted
  EXPECT_EQ(NULL, op2.get());  // deleted
}

TEST_F(RequestRegistryTest, RestartRequest) {
  RequestRegistry registry;

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

  op1->NotifyStart();
  op1->NotifyStart();  // restart
  op1->NotifyFinish(REQUEST_COMPLETED);
  EXPECT_EQ(NULL, op1.get());  // deleted
}

TEST_F(RequestRegistryTest, SuspendCancel) {
  RequestRegistry registry;

  // Suspend-then-resume is a hack in RequestRegistry to tie physically
  // split but logically single request (= chunked uploading split into
  // multiple HTTP requests). When the "logically-single" request is
  // canceled between the two physical requests,
  //    |----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 request. Completion
  // callback must not be called more than once.

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

  op1->NotifyStart();
  op1->NotifySuspend();
  registry.CancelAll();
  EXPECT_EQ(NULL, op1.get());  // deleted

  base::WeakPtr<MockRequest> op2 =
      (new MockRequest(&registry))->AsWeakPtr();
  EXPECT_CALL(*op2, DoCancel()).Times(1);

  op2->NotifyResume();
  EXPECT_EQ(NULL, op2.get());  // deleted
}

}  // namespace google_apis