OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 "base/bind.h" |
| 6 #include "base/memory/weak_ptr.h" |
| 7 #include "base/message_loop.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "chrome/browser/net/dns_probe_runner.h" |
| 10 #include "chrome/browser/net/dns_probe_test_util.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "net/dns/dns_client.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 using base::MessageLoopForIO; |
| 16 using base::RunLoop; |
| 17 using content::TestBrowserThreadBundle; |
| 18 using net::MockDnsClientRule; |
| 19 |
| 20 namespace chrome_browser_net { |
| 21 |
| 22 namespace { |
| 23 |
| 24 class TestDnsProbeRunnerCallback { |
| 25 public: |
| 26 TestDnsProbeRunnerCallback() |
| 27 : callback_(base::Bind(&TestDnsProbeRunnerCallback::OnCalled, |
| 28 base::Unretained(this))), |
| 29 called_(false) {} |
| 30 |
| 31 const base::Closure& callback() const { return callback_; } |
| 32 bool called() const { return called_; } |
| 33 |
| 34 private: |
| 35 void OnCalled() { |
| 36 EXPECT_FALSE(called_); |
| 37 called_ = true; |
| 38 } |
| 39 |
| 40 base::Closure callback_; |
| 41 bool called_; |
| 42 }; |
| 43 |
| 44 class DnsProbeRunnerTest : public testing::Test { |
| 45 protected: |
| 46 void RunTest(MockDnsClientRule::Result query_result, |
| 47 DnsProbeRunner::Result expected_probe_result); |
| 48 |
| 49 TestBrowserThreadBundle bundle_; |
| 50 DnsProbeRunner runner_; |
| 51 }; |
| 52 |
| 53 void DnsProbeRunnerTest::RunTest( |
| 54 MockDnsClientRule::Result query_result, |
| 55 DnsProbeRunner::Result expected_probe_result) { |
| 56 TestDnsProbeRunnerCallback callback; |
| 57 |
| 58 runner_.SetClient(CreateMockDnsClientForProbes(query_result)); |
| 59 runner_.RunProbe(callback.callback()); |
| 60 EXPECT_TRUE(runner_.IsRunning()); |
| 61 |
| 62 RunLoop().RunUntilIdle(); |
| 63 EXPECT_FALSE(runner_.IsRunning()); |
| 64 EXPECT_TRUE(callback.called()); |
| 65 EXPECT_EQ(expected_probe_result, runner_.result()); |
| 66 } |
| 67 |
| 68 TEST_F(DnsProbeRunnerTest, Probe_OK) { |
| 69 RunTest(MockDnsClientRule::OK, DnsProbeRunner::CORRECT); |
| 70 } |
| 71 |
| 72 TEST_F(DnsProbeRunnerTest, Probe_EMPTY) { |
| 73 RunTest(MockDnsClientRule::EMPTY, DnsProbeRunner::INCORRECT); |
| 74 } |
| 75 |
| 76 TEST_F(DnsProbeRunnerTest, Probe_TIMEOUT) { |
| 77 RunTest(MockDnsClientRule::TIMEOUT, DnsProbeRunner::UNREACHABLE); |
| 78 } |
| 79 |
| 80 TEST_F(DnsProbeRunnerTest, Probe_FAIL_ASYNC) { |
| 81 RunTest(MockDnsClientRule::FAIL_ASYNC, DnsProbeRunner::INCORRECT); |
| 82 } |
| 83 |
| 84 TEST_F(DnsProbeRunnerTest, Probe_FAIL_SYNC) { |
| 85 RunTest(MockDnsClientRule::FAIL_SYNC, DnsProbeRunner::INCORRECT); |
| 86 } |
| 87 |
| 88 TEST_F(DnsProbeRunnerTest, TwoProbes) { |
| 89 RunTest(MockDnsClientRule::OK, DnsProbeRunner::CORRECT); |
| 90 RunTest(MockDnsClientRule::EMPTY, DnsProbeRunner::INCORRECT); |
| 91 } |
| 92 |
| 93 } // namespace |
| 94 |
| 95 } // namespace chrome_browser_net |
OLD | NEW |