Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(305)

Side by Side Diff: content/browser/loader/mock_resource_loader.cc

Issue 2626663002: Update MimeSniffingResourceHandler tests to use MockResourceLoader. (Closed)
Patch Set: Fix double-cancel tests Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "content/browser/loader/mock_resource_loader.h" 5 #include "content/browser/loader/mock_resource_loader.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
(...skipping 17 matching lines...) Expand all
28 EXPECT_EQ(Status::IDLE, status_); 28 EXPECT_EQ(Status::IDLE, status_);
29 status_ = Status::CALLING_HANDLER; 29 status_ = Status::CALLING_HANDLER;
30 30
31 bool defer = false; 31 bool defer = false;
32 bool result = resource_handler_->OnWillStart(url, &defer); 32 bool result = resource_handler_->OnWillStart(url, &defer);
33 // The second case isn't really allowed, but a number of classes do it 33 // The second case isn't really allowed, but a number of classes do it
34 // anyways. 34 // anyways.
35 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || 35 EXPECT_TRUE(status_ == Status::CALLING_HANDLER ||
36 (result == false && status_ == Status::CANCELED)); 36 (result == false && status_ == Status::CANCELED));
37 if (!result) { 37 if (!result) {
38 // In the case of double-cancels, keep the old error code.
39 if (status_ != Status::CANCELED)
40 error_code_ = net::ERR_ABORTED;
38 status_ = Status::CANCELED; 41 status_ = Status::CANCELED;
39 } else if (defer) { 42 } else if (defer) {
40 status_ = Status::CALLBACK_PENDING; 43 status_ = Status::CALLBACK_PENDING;
41 } else { 44 } else {
42 status_ = Status::IDLE; 45 status_ = Status::IDLE;
43 } 46 }
44 return status_; 47 return status_;
45 } 48 }
46 49
47 MockResourceLoader::Status MockResourceLoader::OnRequestRedirected( 50 MockResourceLoader::Status MockResourceLoader::OnRequestRedirected(
48 const net::RedirectInfo& redirect_info, 51 const net::RedirectInfo& redirect_info,
49 scoped_refptr<ResourceResponse> response) { 52 scoped_refptr<ResourceResponse> response) {
50 EXPECT_EQ(Status::IDLE, status_); 53 EXPECT_EQ(Status::IDLE, status_);
51 status_ = Status::CALLING_HANDLER; 54 status_ = Status::CALLING_HANDLER;
52 55
53 bool defer = false; 56 bool defer = false;
54 bool result = resource_handler_->OnRequestRedirected(redirect_info, 57 bool result = resource_handler_->OnRequestRedirected(redirect_info,
55 response.get(), &defer); 58 response.get(), &defer);
56 // The second case isn't really allowed, but a number of classes do it 59 // The second case isn't really allowed, but a number of classes do it
57 // anyways. 60 // anyways.
58 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || 61 EXPECT_TRUE(status_ == Status::CALLING_HANDLER ||
59 (result == false && status_ == Status::CANCELED)); 62 (result == false && status_ == Status::CANCELED));
60 if (!result) { 63 if (!result) {
64 // In the case of double-cancels, keep the old error code.
65 if (status_ != Status::CANCELED)
66 error_code_ = net::ERR_ABORTED;
61 status_ = Status::CANCELED; 67 status_ = Status::CANCELED;
62 } else if (defer) { 68 } else if (defer) {
63 status_ = Status::CALLBACK_PENDING; 69 status_ = Status::CALLBACK_PENDING;
64 } else { 70 } else {
65 status_ = Status::IDLE; 71 status_ = Status::IDLE;
66 } 72 }
67 return status_; 73 return status_;
68 } 74 }
69 75
70 MockResourceLoader::Status MockResourceLoader::OnResponseStarted( 76 MockResourceLoader::Status MockResourceLoader::OnResponseStarted(
71 scoped_refptr<ResourceResponse> response) { 77 scoped_refptr<ResourceResponse> response) {
72 EXPECT_EQ(Status::IDLE, status_); 78 EXPECT_EQ(Status::IDLE, status_);
73 status_ = Status::CALLING_HANDLER; 79 status_ = Status::CALLING_HANDLER;
74 80
75 bool defer = false; 81 bool defer = false;
76 bool result = resource_handler_->OnResponseStarted(response.get(), &defer); 82 bool result = resource_handler_->OnResponseStarted(response.get(), &defer);
77 // The second case isn't really allowed, but a number of classes do it 83 // The second case isn't really allowed, but a number of classes do it
78 // anyways. 84 // anyways.
79 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || 85 EXPECT_TRUE(status_ == Status::CALLING_HANDLER ||
80 (result == false && status_ == Status::CANCELED)); 86 (result == false && status_ == Status::CANCELED));
81 if (!result) { 87 if (!result) {
88 // In the case of double-cancels, keep the old error code.
89 if (status_ != Status::CANCELED)
90 error_code_ = net::ERR_ABORTED;
82 status_ = Status::CANCELED; 91 status_ = Status::CANCELED;
83 } else if (defer) { 92 } else if (defer) {
84 status_ = Status::CALLBACK_PENDING; 93 status_ = Status::CALLBACK_PENDING;
85 } else { 94 } else {
86 status_ = Status::IDLE; 95 status_ = Status::IDLE;
87 } 96 }
88 return status_; 97 return status_;
89 } 98 }
90 99
91 MockResourceLoader::Status MockResourceLoader::OnWillRead(int min_size) { 100 MockResourceLoader::Status MockResourceLoader::OnWillRead(int min_size) {
92 EXPECT_EQ(Status::IDLE, status_); 101 EXPECT_EQ(Status::IDLE, status_);
93 EXPECT_FALSE(io_buffer_); 102 EXPECT_FALSE(io_buffer_);
94 EXPECT_EQ(0, io_buffer_size_); 103 EXPECT_EQ(0, io_buffer_size_);
95 104
96 status_ = Status::CALLING_HANDLER; 105 status_ = Status::CALLING_HANDLER;
97 106
98 bool result = 107 bool result =
99 resource_handler_->OnWillRead(&io_buffer_, &io_buffer_size_, min_size); 108 resource_handler_->OnWillRead(&io_buffer_, &io_buffer_size_, min_size);
100 // The second case isn't really allowed, but a number of classes do it 109 // The second case isn't really allowed, but a number of classes do it
101 // anyways. 110 // anyways.
102 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || 111 EXPECT_TRUE(status_ == Status::CALLING_HANDLER ||
103 (result == false && status_ == Status::CANCELED)); 112 (result == false && status_ == Status::CANCELED));
104 if (!result) { 113 if (!result) {
105 EXPECT_EQ(0, io_buffer_size_); 114 EXPECT_EQ(0, io_buffer_size_);
106 EXPECT_FALSE(io_buffer_); 115 EXPECT_FALSE(io_buffer_);
107 status_ = Status::CANCELED; 116 status_ = Status::CANCELED;
117 error_code_ = net::ERR_ABORTED;
108 } else { 118 } else {
109 EXPECT_LE(min_size, io_buffer_size_); 119 EXPECT_LE(min_size, io_buffer_size_);
110 EXPECT_LT(0, io_buffer_size_); 120 EXPECT_LT(0, io_buffer_size_);
111 EXPECT_TRUE(io_buffer_); 121 EXPECT_TRUE(io_buffer_);
112 status_ = Status::IDLE; 122 status_ = Status::IDLE;
113 } 123 }
114 return status_; 124 return status_;
115 }; 125 };
116 126
117 MockResourceLoader::Status MockResourceLoader::OnReadCompleted( 127 MockResourceLoader::Status MockResourceLoader::OnReadCompleted(
118 base::StringPiece bytes) { 128 base::StringPiece bytes) {
119 EXPECT_EQ(Status::IDLE, status_); 129 EXPECT_EQ(Status::IDLE, status_);
120 EXPECT_LE(bytes.size(), static_cast<size_t>(io_buffer_size_)); 130 EXPECT_LE(bytes.size(), static_cast<size_t>(io_buffer_size_));
121 131
122 status_ = Status::CALLING_HANDLER; 132 status_ = Status::CALLING_HANDLER;
123 std::copy(bytes.begin(), bytes.end(), io_buffer_->data()); 133 std::copy(bytes.begin(), bytes.end(), io_buffer_->data());
124 io_buffer_ = nullptr; 134 io_buffer_ = nullptr;
125 io_buffer_size_ = 0; 135 io_buffer_size_ = 0;
126 status_ = Status::CALLING_HANDLER; 136 status_ = Status::CALLING_HANDLER;
127 137
128 bool defer = false; 138 bool defer = false;
129 bool result = resource_handler_->OnReadCompleted(bytes.size(), &defer); 139 bool result = resource_handler_->OnReadCompleted(bytes.size(), &defer);
130 // The second case isn't really allowed, but a number of classes do it 140 // The second case isn't really allowed, but a number of classes do it
131 // anyways. 141 // anyways.
132 EXPECT_TRUE(status_ == Status::CALLING_HANDLER || 142 EXPECT_TRUE(status_ == Status::CALLING_HANDLER ||
133 (result == false && status_ == Status::CANCELED)); 143 (result == false && status_ == Status::CANCELED));
134 if (!result) { 144 if (!result) {
145 // In the case of double-cancels, keep the old error code.
146 if (status_ != Status::CANCELED)
147 error_code_ = net::ERR_ABORTED;
135 status_ = Status::CANCELED; 148 status_ = Status::CANCELED;
136 } else if (defer) { 149 } else if (defer) {
137 status_ = Status::CALLBACK_PENDING; 150 status_ = Status::CALLBACK_PENDING;
138 } else { 151 } else {
139 status_ = Status::IDLE; 152 status_ = Status::IDLE;
140 } 153 }
141 return status_; 154 return status_;
142 } 155 }
143 156
144 MockResourceLoader::Status MockResourceLoader::OnResponseCompleted( 157 MockResourceLoader::Status MockResourceLoader::OnResponseCompleted(
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 } 236 }
224 237
225 void MockResourceLoader::Resume() { 238 void MockResourceLoader::Resume() {
226 EXPECT_EQ(Status::CALLBACK_PENDING, status_); 239 EXPECT_EQ(Status::CALLBACK_PENDING, status_);
227 status_ = Status::IDLE; 240 status_ = Status::IDLE;
228 if (canceled_or_idle_run_loop_) 241 if (canceled_or_idle_run_loop_)
229 canceled_or_idle_run_loop_->Quit(); 242 canceled_or_idle_run_loop_->Quit();
230 } 243 }
231 244
232 } // namespace content 245 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698