146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// found in the LICENSE file.
446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include <string>
646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/memory/ref_counted.h"
846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/message_loop/message_loop.h"
1046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/run_loop.h"
1146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/browser/devtools/devtools_network_conditions.h"
1246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/browser/devtools/devtools_network_controller.h"
13f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/devtools/devtools_network_interceptor.h"
1446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/browser/devtools/devtools_network_transaction.h"
1546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "net/http/http_transaction_test_util.h"
1646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
1746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "url/gurl.h"
1846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
1946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)namespace test {
2046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
2146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)const char kClientId[] = "42";
2246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)const char kAnotherClientId[] = "24";
2346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
2446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class TestCallback {
2546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) public:
2646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestCallback() : run_count_(0), value_(0) {}
2746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void Run(int value) {
2846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    run_count_++;
2946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    value_ = value;
3046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
3146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  int run_count() { return run_count_; }
3246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  int value() { return value_; }
3346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
3446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) private:
3546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  int run_count_;
3646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  int value_;
3746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)};
3846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
3946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class DevToolsNetworkControllerHelper {
4046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) public:
4146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DevToolsNetworkControllerHelper() :
4246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      completion_callback_(
4346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)          base::Bind(&TestCallback::Run, base::Unretained(&callback_))),
4446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      mock_transaction_(kSimpleGET_Transaction),
4546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      buffer_(new net::IOBuffer(64)) {
4646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    mock_transaction_.test_mode = TEST_MODE_SYNC_NET_START;
47f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    mock_transaction_.url = "http://dot.com";
48f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    mock_transaction_.request_headers =
49f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        "X-DevTools-Emulate-Network-Conditions-Client-Id: 42\r\n";
5046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    AddMockTransaction(&mock_transaction_);
5146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
5246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    scoped_ptr<net::HttpTransaction> network_transaction;
5346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    network_layer_.CreateTransaction(
5446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        net::DEFAULT_PRIORITY, &network_transaction);
5546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    transaction_.reset(new DevToolsNetworkTransaction(
5646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        &controller_, network_transaction.Pass()));
5746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
5846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
5946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  net::HttpRequestInfo* GetRequest() {
6046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    if (!request_)
6146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      request_.reset(new MockHttpRequest(mock_transaction_));
6246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return request_.get();
6346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
6446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
65f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void SetNetworkState(const std::string id, bool offline) {
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    scoped_ptr<DevToolsNetworkConditions> conditions(
67f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        new DevToolsNetworkConditions(offline));
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    controller_.SetNetworkStateOnIO(id, conditions.Pass());
6946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
7046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
7146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  int Start() {
7246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return transaction_->Start(
7346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        GetRequest(), completion_callback_, net::BoundNetLog());
7446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
7546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
7646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  int Read() {
7746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return transaction_->Read(buffer_.get(), 64, completion_callback_);
7846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
7946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
80f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  bool ShouldFail() {
81f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return transaction_->interceptor_->ShouldFail(transaction_.get());
82f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
83f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
8446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ~DevToolsNetworkControllerHelper() {
8546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    RemoveMockTransaction(&mock_transaction_);
8646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
8746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
8846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestCallback* callback() { return &callback_; }
8946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  MockTransaction* mock_transaction() { return &mock_transaction_; }
9046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DevToolsNetworkController* controller() { return &controller_; }
9146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DevToolsNetworkTransaction* transaction() { return transaction_.get(); }
9246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
9346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) private:
9446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::MessageLoop message_loop_;
9546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  MockNetworkLayer network_layer_;
9646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestCallback callback_;
9746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  net::CompletionCallback completion_callback_;
9846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  MockTransaction mock_transaction_;
9946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DevToolsNetworkController controller_;
10046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  scoped_ptr<DevToolsNetworkTransaction> transaction_;
10146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  scoped_refptr<net::IOBuffer> buffer_;
10246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  scoped_ptr<MockHttpRequest> request_;
10346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)};
10446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
10546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST(DevToolsNetworkControllerTest, SingleDisableEnable) {
10646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DevToolsNetworkControllerHelper helper;
107f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  helper.SetNetworkState(kClientId, false);
108f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  helper.Start();
10946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
110f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_FALSE(helper.ShouldFail());
11146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  helper.SetNetworkState(kClientId, true);
112f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(helper.ShouldFail());
11346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  helper.SetNetworkState(kClientId, false);
114f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_FALSE(helper.ShouldFail());
115f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
116f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
11746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
11846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
119f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)TEST(DevToolsNetworkControllerTest, InterceptorIsolation) {
12046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DevToolsNetworkControllerHelper helper;
121f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  helper.SetNetworkState(kClientId, false);
122f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  helper.Start();
12346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
124f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_FALSE(helper.ShouldFail());
12546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  helper.SetNetworkState(kAnotherClientId, true);
126f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_FALSE(helper.ShouldFail());
127f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  helper.SetNetworkState(kClientId, true);
128f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(helper.ShouldFail());
129f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
13046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  helper.SetNetworkState(kAnotherClientId, false);
131f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  helper.SetNetworkState(kClientId, false);
132f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
13346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
13446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
13546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST(DevToolsNetworkControllerTest, FailOnStart) {
13646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DevToolsNetworkControllerHelper helper;
13746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  helper.SetNetworkState(kClientId, true);
13846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
13946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  int rv = helper.Start();
14046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_EQ(rv, net::ERR_INTERNET_DISCONNECTED);
14146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
14246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
14346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_EQ(helper.callback()->run_count(), 0);
14446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
14546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
14646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST(DevToolsNetworkControllerTest, FailRunningTransaction) {
14746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DevToolsNetworkControllerHelper helper;
148f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  helper.SetNetworkState(kClientId, false);
14946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  TestCallback* callback = helper.callback();
15046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
15146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  int rv = helper.Start();
15246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_EQ(rv, net::OK);
15346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
15446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(64));
15546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  rv = helper.Read();
15646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_EQ(rv, net::ERR_IO_PENDING);
15746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_EQ(callback->run_count(), 0);
15846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
15946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  helper.SetNetworkState(kClientId, true);
16046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_EQ(callback->run_count(), 1);
16146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_EQ(callback->value(), net::ERR_INTERNET_DISCONNECTED);
16246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
16346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Wait until HttpTrancation completes reading and invokes callback.
16446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // DevToolsNetworkTransaction should ignore callback, because it has
16546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // reported network error already.
16646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
16746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_EQ(callback->run_count(), 1);
16846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
16946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Check that transaction in not failed second time.
17046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  helper.SetNetworkState(kClientId, false);
17146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  helper.SetNetworkState(kClientId, true);
17246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_EQ(callback->run_count(), 1);
17346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
17446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
17546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST(DevToolsNetworkControllerTest, ReadAfterFail) {
17646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DevToolsNetworkControllerHelper helper;
177f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  helper.SetNetworkState(kClientId, false);
17846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
17946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  int rv = helper.Start();
18046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_EQ(rv, net::OK);
18146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(helper.transaction()->request());
18246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
18346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  helper.SetNetworkState(kClientId, true);
18446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_TRUE(helper.transaction()->failed());
18546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
18646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(64));
18746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  rv = helper.Read();
18846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_EQ(rv, net::ERR_INTERNET_DISCONNECTED);
18946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
19046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Check that callback is never invoked.
19146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::RunLoop().RunUntilIdle();
19246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  EXPECT_EQ(helper.callback()->run_count(), 0);
19346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
19446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
19546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)TEST(DevToolsNetworkControllerTest, AllowsDevToolsRequests) {
19646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DevToolsNetworkControllerHelper helper;
197f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  helper.SetNetworkState(kClientId, false);
19846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  helper.mock_transaction()->request_headers =
199f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      "X-DevTools-Emulate-Network-Conditions-Client-Id: 42\r\n"
20046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      "X-DevTools-Request-Initiator: frontend\r\n";
201f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  helper.Start();
20246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
203f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_FALSE(helper.ShouldFail());
20446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  helper.SetNetworkState(kClientId, true);
205f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_FALSE(helper.ShouldFail());
20646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
20746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
20846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}  // namespace test
209