Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_network_controller_unittest.cc
blob6893b3ec173951ecd20abd5b173e161eacbab469
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <string>
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "chrome/browser/devtools/devtools_network_conditions.h"
12 #include "chrome/browser/devtools/devtools_network_controller.h"
13 #include "chrome/browser/devtools/devtools_network_interceptor.h"
14 #include "chrome/browser/devtools/devtools_network_transaction.h"
15 #include "net/http/http_transaction_test_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "url/gurl.h"
19 namespace test {
21 using net::kSimpleGET_Transaction;
22 using net::MockHttpRequest;
23 using net::MockNetworkLayer;
24 using net::MockTransaction;
25 using net::TEST_MODE_SYNC_NET_START;
27 const char kClientId[] = "42";
28 const char kAnotherClientId[] = "24";
30 class TestCallback {
31 public:
32 TestCallback() : run_count_(0), value_(0) {}
33 void Run(int value) {
34 run_count_++;
35 value_ = value;
37 int run_count() { return run_count_; }
38 int value() { return value_; }
40 private:
41 int run_count_;
42 int value_;
45 class DevToolsNetworkControllerHelper {
46 public:
47 DevToolsNetworkControllerHelper() :
48 completion_callback_(
49 base::Bind(&TestCallback::Run, base::Unretained(&callback_))),
50 mock_transaction_(kSimpleGET_Transaction),
51 buffer_(new net::IOBuffer(64)) {
52 mock_transaction_.test_mode = TEST_MODE_SYNC_NET_START;
53 mock_transaction_.url = "http://dot.com";
54 mock_transaction_.request_headers =
55 "X-DevTools-Emulate-Network-Conditions-Client-Id: 42\r\n";
56 AddMockTransaction(&mock_transaction_);
58 scoped_ptr<net::HttpTransaction> network_transaction;
59 network_layer_.CreateTransaction(
60 net::DEFAULT_PRIORITY, &network_transaction);
61 transaction_.reset(new DevToolsNetworkTransaction(
62 &controller_, network_transaction.Pass()));
65 net::HttpRequestInfo* GetRequest() {
66 if (!request_)
67 request_.reset(new MockHttpRequest(mock_transaction_));
68 return request_.get();
71 void SetNetworkState(const std::string id, bool offline) {
72 scoped_ptr<DevToolsNetworkConditions> conditions(
73 new DevToolsNetworkConditions(offline));
74 controller_.SetNetworkState(id, conditions.Pass());
77 int Start() {
78 return transaction_->Start(
79 GetRequest(), completion_callback_, net::BoundNetLog());
82 int Read() {
83 return transaction_->Read(buffer_.get(), 64, completion_callback_);
86 bool ShouldFail() {
87 return transaction_->interceptor_->ShouldFail(transaction_.get());
90 ~DevToolsNetworkControllerHelper() {
91 RemoveMockTransaction(&mock_transaction_);
94 TestCallback* callback() { return &callback_; }
95 MockTransaction* mock_transaction() { return &mock_transaction_; }
96 DevToolsNetworkController* controller() { return &controller_; }
97 DevToolsNetworkTransaction* transaction() { return transaction_.get(); }
99 private:
100 base::MessageLoop message_loop_;
101 MockNetworkLayer network_layer_;
102 TestCallback callback_;
103 net::CompletionCallback completion_callback_;
104 MockTransaction mock_transaction_;
105 DevToolsNetworkController controller_;
106 scoped_ptr<DevToolsNetworkTransaction> transaction_;
107 scoped_refptr<net::IOBuffer> buffer_;
108 scoped_ptr<MockHttpRequest> request_;
111 TEST(DevToolsNetworkControllerTest, SingleDisableEnable) {
112 DevToolsNetworkControllerHelper helper;
113 helper.SetNetworkState(kClientId, false);
114 helper.Start();
116 EXPECT_FALSE(helper.ShouldFail());
117 helper.SetNetworkState(kClientId, true);
118 EXPECT_TRUE(helper.ShouldFail());
119 helper.SetNetworkState(kClientId, false);
120 EXPECT_FALSE(helper.ShouldFail());
122 base::RunLoop().RunUntilIdle();
125 TEST(DevToolsNetworkControllerTest, InterceptorIsolation) {
126 DevToolsNetworkControllerHelper helper;
127 helper.SetNetworkState(kClientId, false);
128 helper.Start();
130 EXPECT_FALSE(helper.ShouldFail());
131 helper.SetNetworkState(kAnotherClientId, true);
132 EXPECT_FALSE(helper.ShouldFail());
133 helper.SetNetworkState(kClientId, true);
134 EXPECT_TRUE(helper.ShouldFail());
136 helper.SetNetworkState(kAnotherClientId, false);
137 helper.SetNetworkState(kClientId, false);
138 base::RunLoop().RunUntilIdle();
141 TEST(DevToolsNetworkControllerTest, FailOnStart) {
142 DevToolsNetworkControllerHelper helper;
143 helper.SetNetworkState(kClientId, true);
145 int rv = helper.Start();
146 EXPECT_EQ(rv, net::ERR_INTERNET_DISCONNECTED);
148 base::RunLoop().RunUntilIdle();
149 EXPECT_EQ(helper.callback()->run_count(), 0);
152 TEST(DevToolsNetworkControllerTest, FailRunningTransaction) {
153 DevToolsNetworkControllerHelper helper;
154 helper.SetNetworkState(kClientId, false);
155 TestCallback* callback = helper.callback();
157 int rv = helper.Start();
158 EXPECT_EQ(rv, net::OK);
160 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(64));
161 rv = helper.Read();
162 EXPECT_EQ(rv, net::ERR_IO_PENDING);
163 EXPECT_EQ(callback->run_count(), 0);
165 helper.SetNetworkState(kClientId, true);
166 EXPECT_EQ(callback->run_count(), 1);
167 EXPECT_EQ(callback->value(), net::ERR_INTERNET_DISCONNECTED);
169 // Wait until HttpTrancation completes reading and invokes callback.
170 // DevToolsNetworkTransaction should ignore callback, because it has
171 // reported network error already.
172 base::RunLoop().RunUntilIdle();
173 EXPECT_EQ(callback->run_count(), 1);
175 // Check that transaction in not failed second time.
176 helper.SetNetworkState(kClientId, false);
177 helper.SetNetworkState(kClientId, true);
178 EXPECT_EQ(callback->run_count(), 1);
181 TEST(DevToolsNetworkControllerTest, ReadAfterFail) {
182 DevToolsNetworkControllerHelper helper;
183 helper.SetNetworkState(kClientId, false);
185 int rv = helper.Start();
186 EXPECT_EQ(rv, net::OK);
187 EXPECT_TRUE(helper.transaction()->request());
189 helper.SetNetworkState(kClientId, true);
190 EXPECT_TRUE(helper.transaction()->failed());
192 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(64));
193 rv = helper.Read();
194 EXPECT_EQ(rv, net::ERR_INTERNET_DISCONNECTED);
196 // Check that callback is never invoked.
197 base::RunLoop().RunUntilIdle();
198 EXPECT_EQ(helper.callback()->run_count(), 0);
201 TEST(DevToolsNetworkControllerTest, AllowsDevToolsRequests) {
202 DevToolsNetworkControllerHelper helper;
203 helper.SetNetworkState(kClientId, false);
204 helper.mock_transaction()->request_headers =
205 "X-DevTools-Emulate-Network-Conditions-Client-Id: 42\r\n"
206 "X-DevTools-Request-Initiator: frontend\r\n";
207 helper.Start();
209 EXPECT_FALSE(helper.ShouldFail());
210 helper.SetNetworkState(kClientId, true);
211 EXPECT_FALSE(helper.ShouldFail());
214 } // namespace test