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/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_unittest.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 #include "url/gurl.h" | |
16 | |
17 namespace { | |
18 | |
19 class TestCallback { | |
mmenke
2014/04/21 17:16:51
Shouldn't indent code in a namespace.
eustas
2014/04/22 14:23:09
Done.
| |
20 public: | |
21 TestCallback() : run_count_(0), value_(0) {} | |
22 void Run(int value) { | |
23 run_count_++; | |
24 value_ = value; | |
25 } | |
26 int run_count() { return run_count_; } | |
27 int value() { return value_; } | |
28 | |
29 private: | |
30 int run_count_; | |
31 int value_; | |
32 }; | |
33 | |
34 class TransactionHelper { | |
35 public: | |
36 TransactionHelper() : | |
37 completion_callback_( | |
38 base::Bind(&TestCallback::Run, base::Unretained(&callback_))), | |
39 mock_transaction_(kSimpleGET_Transaction), | |
40 buffer_(new net::IOBuffer(64)) { | |
41 mock_transaction_.test_mode = TEST_MODE_SYNC_NET_START; | |
42 mock_transaction_.url = "http://dot.com"; | |
43 AddMockTransaction(&mock_transaction_); | |
44 | |
45 scoped_ptr<net::HttpTransaction> network_transaction; | |
46 network_layer_.CreateTransaction( | |
47 net::DEFAULT_PRIORITY, &network_transaction); | |
48 transaction_.reset(new DevToolsNetworkTransaction( | |
49 &controller_, network_transaction.Pass())); | |
50 } | |
51 | |
52 int Start() { | |
53 MockHttpRequest request(mock_transaction_); | |
54 return transaction_->Start( | |
mmenke
2014/04/21 17:16:51
nit: -2 indent
eustas
2014/04/22 14:23:09
Done.
| |
55 &request, completion_callback_, net::BoundNetLog()); | |
56 } | |
57 | |
58 int Read() { | |
59 return transaction_->Read(buffer_.get(), 64, completion_callback_); | |
60 } | |
61 | |
62 ~TransactionHelper() { | |
63 RemoveMockTransaction(&mock_transaction_); | |
64 } | |
65 | |
66 TestCallback* callback() { return &callback_; } | |
67 MockTransaction* mock_transaction() { return &mock_transaction_; } | |
68 DevToolsNetworkController* controller() { return &controller_; } | |
69 DevToolsNetworkTransaction* transaction() { return transaction_.get(); } | |
70 | |
71 private: | |
72 base::MessageLoop message_loop_; | |
73 MockNetworkLayer network_layer_; | |
74 TestCallback callback_; | |
75 net::CompletionCallback completion_callback_; | |
76 MockTransaction mock_transaction_; | |
77 DevToolsNetworkController controller_; | |
78 scoped_ptr<DevToolsNetworkTransaction> transaction_; | |
79 scoped_refptr<net::IOBuffer> buffer_; | |
80 }; | |
81 | |
82 } // namespace | |
mmenke
2014/04/21 17:16:51
Sorry, should have been clearer - can the rest of
eustas
2014/04/22 14:23:09
Done.
| |
83 | |
84 TEST(DevToolsNetworkControllerTest, SingleDisableEnable) { | |
85 TransactionHelper helper; | |
86 DevToolsNetworkController* controller = helper.controller(); | |
87 DevToolsNetworkTransaction* transaction = helper.transaction(); | |
88 std::string test_id = "42"; | |
mmenke
2014/04/21 17:16:51
These should probably be:
const char kTestId[] =
eustas
2014/04/22 14:23:09
Done.
| |
89 helper.Start(); | |
90 | |
91 EXPECT_FALSE(controller->ShouldFail(transaction)); | |
92 controller->DisableNetwork(test_id, true); | |
93 EXPECT_TRUE(controller->ShouldFail(transaction)); | |
94 controller->DisableNetwork(test_id, false); | |
95 EXPECT_FALSE(controller->ShouldFail(transaction)); | |
96 } | |
97 | |
98 TEST(DevToolsNetworkControllerTest, DoubleDisableEnable) { | |
99 TransactionHelper helper; | |
100 DevToolsNetworkController* controller = helper.controller(); | |
101 DevToolsNetworkTransaction* transaction = helper.transaction(); | |
102 std::string test_id_1 = "42"; | |
103 std::string test_id_2 = "24"; | |
104 helper.Start(); | |
105 | |
106 EXPECT_FALSE(controller->ShouldFail(transaction)); | |
107 controller->DisableNetwork(test_id_1, true); | |
108 EXPECT_TRUE(controller->ShouldFail(transaction)); | |
109 controller->DisableNetwork(test_id_2, true); | |
110 EXPECT_TRUE(controller->ShouldFail(transaction)); | |
111 controller->DisableNetwork(test_id_1, false); | |
112 EXPECT_TRUE(controller->ShouldFail(transaction)); | |
113 controller->DisableNetwork(test_id_2, false); | |
114 EXPECT_FALSE(controller->ShouldFail(transaction)); | |
115 } | |
116 | |
117 TEST(DevToolsNetworkControllerTest, FailOnStart) { | |
118 TransactionHelper helper; | |
119 helper.controller()->DisableNetwork("42", true); | |
120 | |
121 int rv = helper.Start(); | |
122 EXPECT_EQ(rv, net::ERR_INTERNET_DISCONNECTED); | |
123 | |
124 base::RunLoop().RunUntilIdle(); | |
125 EXPECT_EQ(helper.callback()->run_count(), 0); | |
126 } | |
127 | |
128 TEST(DevToolsNetworkControllerTest, FailRunningTransaction) { | |
129 TransactionHelper helper; | |
130 DevToolsNetworkController* controller = helper.controller(); | |
131 TestCallback* callback = helper.callback(); | |
132 std::string test_id = "42"; | |
133 | |
134 int rv = helper.Start(); | |
135 EXPECT_EQ(rv, net::OK); | |
136 | |
137 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(64)); | |
138 rv = helper.Read(); | |
139 EXPECT_EQ(rv, net::ERR_IO_PENDING); | |
140 EXPECT_EQ(callback->run_count(), 0); | |
141 | |
142 controller->DisableNetwork(test_id, true); | |
143 EXPECT_EQ(callback->run_count(), 1); | |
144 EXPECT_EQ(callback->value(), net::ERR_INTERNET_DISCONNECTED); | |
145 | |
146 base::RunLoop().RunUntilIdle(); | |
mmenke
2014/04/21 17:16:51
Think this is worth a comment (Just something alon
eustas
2014/04/22 14:23:09
Done.
| |
147 EXPECT_EQ(callback->run_count(), 1); | |
148 | |
149 // Check that transaction in not failed second time. | |
150 controller->DisableNetwork(test_id, false); | |
151 controller->DisableNetwork(test_id, true); | |
152 EXPECT_EQ(callback->run_count(), 1); | |
153 } | |
154 | |
155 TEST(DevToolsNetworkControllerTest, ReadAfterFail) { | |
mmenke
2014/04/21 17:16:51
This shouldn't happen - should test when a fail wi
eustas
2014/04/22 14:23:09
Done.
| |
156 TransactionHelper helper; | |
157 helper.controller()->DisableNetwork("42", true); | |
158 | |
159 int rv = helper.Start(); | |
160 EXPECT_EQ(rv, net::ERR_INTERNET_DISCONNECTED); | |
161 | |
162 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(64)); | |
163 rv = helper.Read(); | |
164 EXPECT_EQ(rv, net::ERR_INTERNET_DISCONNECTED); | |
165 } | |
166 | |
167 TEST(DevToolsNetworkControllerTest, BypassDevToolsRequests) { | |
mmenke
2014/04/21 17:16:51
nit: Think this name is confusing. Maybe AllowsD
eustas
2014/04/22 14:23:09
Done.
| |
168 TransactionHelper helper; | |
169 helper.mock_transaction()->request_headers = | |
170 "X-DevTools-Request-Initiator: frontend\r\n"; | |
171 DevToolsNetworkController* controller = helper.controller(); | |
172 DevToolsNetworkTransaction* transaction = helper.transaction(); | |
173 helper.Start(); | |
174 | |
175 EXPECT_FALSE(controller->ShouldFail(transaction)); | |
176 controller->DisableNetwork("42", true); | |
177 EXPECT_FALSE(controller->ShouldFail(transaction)); | |
178 } | |
OLD | NEW |