OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include <string> | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "base/run_loop.h" | |
10 #include "chrome/browser/devtools/devtools_network_controller.h" | |
11 #include "chrome/browser/devtools/devtools_network_transaction.h" | |
12 #include "net/http/http_transaction_unittest.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 #include "url/gurl.h" | |
15 | |
16 namespace { | |
mmenke
2014/04/18 15:41:42
nit: Blank line after namespace, and before end o
eustas
2014/04/21 11:34:59
Done.
| |
17 class TestCallback { | |
18 public: | |
19 TestCallback() : run_count_(0), value_(0) {} | |
20 void Run(int value) { | |
21 run_count_++; | |
22 value_ = value; | |
23 } | |
24 int run_count() { return run_count_; } | |
25 int value() { return value_; } | |
26 private: | |
mmenke
2014/04/18 15:41:42
nit: Blank line before private.
eustas
2014/04/21 11:34:59
Done.
| |
27 int run_count_; | |
28 int value_; | |
29 }; | |
30 | |
31 class TransactionHelper { | |
32 public: | |
33 TransactionHelper() : | |
34 completion_callback_( | |
35 base::Bind(&TestCallback::Run, base::Unretained(&callback_))), | |
36 mock_transaction_(kSimpleGET_Transaction), | |
37 buffer_(new net::IOBuffer(64)) { | |
38 mock_transaction_.test_mode = TEST_MODE_SYNC_NET_START; | |
39 mock_transaction_.url = "http://dot.com"; | |
40 AddMockTransaction(&mock_transaction_); | |
41 | |
42 scoped_ptr<net::HttpTransaction> network_transaction; | |
43 network_layer_.CreateTransaction( | |
44 net::DEFAULT_PRIORITY, &network_transaction); | |
45 transaction_.reset(new DevToolsNetworkTransaction( | |
46 &controller_, network_transaction.Pass())); | |
47 } | |
48 | |
49 int Start() { | |
50 MockHttpRequest request(mock_transaction_); | |
51 return transaction_->Start( | |
52 &request, completion_callback_, net::BoundNetLog()); | |
53 } | |
54 | |
55 int Read() { | |
56 return transaction_->Read(buffer_.get(), 64, completion_callback_); | |
57 } | |
58 | |
59 ~TransactionHelper() { | |
60 RemoveMockTransaction(&mock_transaction_); | |
61 } | |
62 | |
63 TestCallback* callback() { return &callback_; } | |
64 MockTransaction* mock_transaction() { return &mock_transaction_; } | |
65 DevToolsNetworkController* controller() { return &controller_; } | |
66 DevToolsNetworkTransaction* transaction() { return transaction_.get(); } | |
67 | |
68 private: | |
69 base::MessageLoop message_loop_; | |
70 MockNetworkLayer network_layer_; | |
71 TestCallback callback_; | |
72 net::CompletionCallback completion_callback_; | |
73 MockTransaction mock_transaction_; | |
74 DevToolsNetworkController controller_; | |
75 scoped_ptr<DevToolsNetworkTransaction> transaction_; | |
76 scoped_refptr<net::IOBuffer> buffer_; | |
mmenke
2014/04/18 15:41:42
Include ref_counted.h
eustas
2014/04/21 11:34:59
Done.
| |
77 }; | |
78 } // namespace | |
mmenke
2014/04/18 15:41:42
nit: 2 spaces between code and comment.
mmenke
2014/04/18 15:41:42
Couldn't this go at the end of the file?
eustas
2014/04/21 11:34:59
Done.
eustas
2014/04/21 11:34:59
I can split definition/declaration, but this won't
| |
79 | |
80 TEST(DevToolsNetworkControllerTest, SingleDisableEnable) { | |
81 TransactionHelper helper; | |
82 DevToolsNetworkController* controller = helper.controller(); | |
83 DevToolsNetworkTransaction* transaction = helper.transaction(); | |
84 std::string test_id = "42"; | |
85 helper.Start(); | |
86 | |
87 EXPECT_FALSE(controller->ShouldFail(transaction)); | |
88 controller->DisableNetwork(test_id, true); | |
89 EXPECT_TRUE(controller->ShouldFail(transaction)); | |
90 controller->DisableNetwork(test_id, false); | |
91 EXPECT_FALSE(controller->ShouldFail(transaction)); | |
92 } | |
93 | |
94 TEST(DevToolsNetworkControllerTest, DoubleDisableEnable) { | |
95 TransactionHelper helper; | |
96 DevToolsNetworkController* controller = helper.controller(); | |
97 DevToolsNetworkTransaction* transaction = helper.transaction(); | |
98 std::string test_id_1 = "42"; | |
99 std::string test_id_2 = "24"; | |
100 helper.Start(); | |
101 | |
102 EXPECT_FALSE(controller->ShouldFail(transaction)); | |
103 controller->DisableNetwork(test_id_1, true); | |
104 EXPECT_TRUE(controller->ShouldFail(transaction)); | |
105 controller->DisableNetwork(test_id_2, true); | |
106 EXPECT_TRUE(controller->ShouldFail(transaction)); | |
107 controller->DisableNetwork(test_id_1, false); | |
108 EXPECT_TRUE(controller->ShouldFail(transaction)); | |
109 controller->DisableNetwork(test_id_2, false); | |
110 EXPECT_FALSE(controller->ShouldFail(transaction)); | |
111 } | |
112 | |
113 TEST(DevToolsNetworkControllerTest, FailOnStart) { | |
114 TransactionHelper helper; | |
115 helper.controller()->DisableNetwork("42", true); | |
116 | |
117 int rv = helper.Start(); | |
118 EXPECT_EQ(rv, net::ERR_INTERNET_DISCONNECTED); | |
119 | |
120 base::RunLoop().RunUntilIdle(); | |
121 EXPECT_EQ(helper.callback()->run_count(), 0); | |
122 } | |
123 | |
124 TEST(DevToolsNetworkControllerTest, FailRunningTransaction) { | |
125 TransactionHelper helper; | |
126 DevToolsNetworkController* controller = helper.controller(); | |
127 TestCallback* callback = helper.callback(); | |
128 std::string test_id = "42"; | |
129 | |
130 int rv = helper.Start(); | |
131 EXPECT_EQ(rv, net::OK); | |
132 | |
133 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(64)); | |
134 rv = helper.Read(); | |
135 EXPECT_EQ(rv, net::ERR_IO_PENDING); | |
136 EXPECT_EQ(callback->run_count(), 0); | |
137 | |
138 controller->DisableNetwork(test_id, true); | |
139 EXPECT_EQ(callback->run_count(), 1); | |
140 EXPECT_EQ(callback->value(), net::ERR_INTERNET_DISCONNECTED); | |
141 | |
142 base::RunLoop().RunUntilIdle(); | |
143 EXPECT_EQ(callback->run_count(), 1); | |
144 | |
145 // Check that transaction in not failed second time. | |
146 controller->DisableNetwork(test_id, false); | |
147 controller->DisableNetwork(test_id, true); | |
148 EXPECT_EQ(callback->run_count(), 1); | |
149 } | |
150 | |
151 TEST(DevToolsNetworkControllerTest, BypassDevToolsRequests) { | |
152 TransactionHelper helper; | |
153 helper.mock_transaction()->request_headers = | |
154 "X-DevTools-Request-Initiator: frontend\r\n"; | |
155 DevToolsNetworkController* controller = helper.controller(); | |
156 DevToolsNetworkTransaction* transaction = helper.transaction(); | |
157 helper.Start(); | |
158 | |
159 EXPECT_FALSE(controller->ShouldFail(transaction)); | |
160 controller->DisableNetwork("42", true); | |
161 EXPECT_FALSE(controller->ShouldFail(transaction)); | |
162 } | |
OLD | NEW |