Introduce ProfilerMetricsProvider
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_network_controller_unittest.cc
blob6fe625644828d68f7d11c498724f81a84a97c846
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_controller.h"
12 #include "chrome/browser/devtools/devtools_network_transaction.h"
13 #include "net/http/http_transaction_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "url/gurl.h"
17 namespace test {
19 const char kClientId[] = "42";
20 const char kAnotherClientId[] = "24";
22 class TestCallback {
23 public:
24 TestCallback() : run_count_(0), value_(0) {}
25 void Run(int value) {
26 run_count_++;
27 value_ = value;
29 int run_count() { return run_count_; }
30 int value() { return value_; }
32 private:
33 int run_count_;
34 int value_;
37 class DevToolsNetworkControllerHelper {
38 public:
39 DevToolsNetworkControllerHelper() :
40 completion_callback_(
41 base::Bind(&TestCallback::Run, base::Unretained(&callback_))),
42 mock_transaction_(kSimpleGET_Transaction),
43 buffer_(new net::IOBuffer(64)) {
44 mock_transaction_.test_mode = TEST_MODE_SYNC_NET_START;
45 mock_transaction_.url = "http://dot.com";
46 AddMockTransaction(&mock_transaction_);
48 scoped_ptr<net::HttpTransaction> network_transaction;
49 network_layer_.CreateTransaction(
50 net::DEFAULT_PRIORITY, &network_transaction);
51 transaction_.reset(new DevToolsNetworkTransaction(
52 &controller_, network_transaction.Pass()));
55 net::HttpRequestInfo* GetRequest() {
56 if (!request_)
57 request_.reset(new MockHttpRequest(mock_transaction_));
58 return request_.get();
61 void SetNetworkState(const std::string& client_id, bool offline) {
62 controller_.SetNetworkStateOnIO(client_id, offline);
65 int Start() {
66 return transaction_->Start(
67 GetRequest(), completion_callback_, net::BoundNetLog());
70 int Read() {
71 return transaction_->Read(buffer_.get(), 64, completion_callback_);
74 ~DevToolsNetworkControllerHelper() {
75 RemoveMockTransaction(&mock_transaction_);
78 TestCallback* callback() { return &callback_; }
79 MockTransaction* mock_transaction() { return &mock_transaction_; }
80 DevToolsNetworkController* controller() { return &controller_; }
81 DevToolsNetworkTransaction* transaction() { return transaction_.get(); }
83 private:
84 base::MessageLoop message_loop_;
85 MockNetworkLayer network_layer_;
86 TestCallback callback_;
87 net::CompletionCallback completion_callback_;
88 MockTransaction mock_transaction_;
89 DevToolsNetworkController controller_;
90 scoped_ptr<DevToolsNetworkTransaction> transaction_;
91 scoped_refptr<net::IOBuffer> buffer_;
92 scoped_ptr<MockHttpRequest> request_;
95 TEST(DevToolsNetworkControllerTest, SingleDisableEnable) {
96 DevToolsNetworkControllerHelper helper;
97 DevToolsNetworkController* controller = helper.controller();
98 net::HttpRequestInfo* request = helper.GetRequest();
100 EXPECT_FALSE(controller->ShouldFail(request));
101 helper.SetNetworkState(kClientId, true);
102 EXPECT_TRUE(controller->ShouldFail(request));
103 helper.SetNetworkState(kClientId, false);
104 EXPECT_FALSE(controller->ShouldFail(request));
107 TEST(DevToolsNetworkControllerTest, DoubleDisableEnable) {
108 DevToolsNetworkControllerHelper helper;
109 DevToolsNetworkController* controller = helper.controller();
110 net::HttpRequestInfo* request = helper.GetRequest();
112 EXPECT_FALSE(controller->ShouldFail(request));
113 helper.SetNetworkState(kClientId, true);
114 EXPECT_TRUE(controller->ShouldFail(request));
115 helper.SetNetworkState(kAnotherClientId, true);
116 EXPECT_TRUE(controller->ShouldFail(request));
117 helper.SetNetworkState(kClientId, false);
118 EXPECT_TRUE(controller->ShouldFail(request));
119 helper.SetNetworkState(kAnotherClientId, false);
120 EXPECT_FALSE(controller->ShouldFail(request));
123 TEST(DevToolsNetworkControllerTest, FailOnStart) {
124 DevToolsNetworkControllerHelper helper;
125 helper.SetNetworkState(kClientId, true);
127 int rv = helper.Start();
128 EXPECT_EQ(rv, net::ERR_INTERNET_DISCONNECTED);
130 base::RunLoop().RunUntilIdle();
131 EXPECT_EQ(helper.callback()->run_count(), 0);
134 TEST(DevToolsNetworkControllerTest, FailRunningTransaction) {
135 DevToolsNetworkControllerHelper helper;
136 TestCallback* callback = helper.callback();
138 int rv = helper.Start();
139 EXPECT_EQ(rv, net::OK);
141 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(64));
142 rv = helper.Read();
143 EXPECT_EQ(rv, net::ERR_IO_PENDING);
144 EXPECT_EQ(callback->run_count(), 0);
146 helper.SetNetworkState(kClientId, true);
147 EXPECT_EQ(callback->run_count(), 1);
148 EXPECT_EQ(callback->value(), net::ERR_INTERNET_DISCONNECTED);
150 // Wait until HttpTrancation completes reading and invokes callback.
151 // DevToolsNetworkTransaction should ignore callback, because it has
152 // reported network error already.
153 base::RunLoop().RunUntilIdle();
154 EXPECT_EQ(callback->run_count(), 1);
156 // Check that transaction in not failed second time.
157 helper.SetNetworkState(kClientId, false);
158 helper.SetNetworkState(kClientId, true);
159 EXPECT_EQ(callback->run_count(), 1);
162 TEST(DevToolsNetworkControllerTest, ReadAfterFail) {
163 DevToolsNetworkControllerHelper helper;
165 int rv = helper.Start();
166 EXPECT_EQ(rv, net::OK);
167 EXPECT_TRUE(helper.transaction()->request());
169 helper.SetNetworkState(kClientId, true);
170 EXPECT_TRUE(helper.transaction()->failed());
172 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(64));
173 rv = helper.Read();
174 EXPECT_EQ(rv, net::ERR_INTERNET_DISCONNECTED);
176 // Check that callback is never invoked.
177 base::RunLoop().RunUntilIdle();
178 EXPECT_EQ(helper.callback()->run_count(), 0);
181 TEST(DevToolsNetworkControllerTest, AllowsDevToolsRequests) {
182 DevToolsNetworkControllerHelper helper;
183 helper.mock_transaction()->request_headers =
184 "X-DevTools-Request-Initiator: frontend\r\n";
185 DevToolsNetworkController* controller = helper.controller();
186 net::HttpRequestInfo* request = helper.GetRequest();
188 EXPECT_FALSE(controller->ShouldFail(request));
189 helper.SetNetworkState(kClientId, true);
190 EXPECT_FALSE(controller->ShouldFail(request));
193 } // namespace test