Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / net / dns_probe_runner_unittest.cc
blob5020afc0bea87f7f61edb1244af7051669f6cff8
1 // Copyright 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.
5 #include "base/bind.h"
6 #include "base/memory/weak_ptr.h"
7 #include "base/message_loop/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"
15 using base::MessageLoopForIO;
16 using base::RunLoop;
17 using content::TestBrowserThreadBundle;
18 using net::DnsClient;
19 using net::DnsConfig;
20 using net::MockDnsClientRule;
22 namespace chrome_browser_net {
24 namespace {
26 class TestDnsProbeRunnerCallback {
27 public:
28 TestDnsProbeRunnerCallback()
29 : callback_(base::Bind(&TestDnsProbeRunnerCallback::OnCalled,
30 base::Unretained(this))),
31 called_(false) {}
33 const base::Closure& callback() const { return callback_; }
34 bool called() const { return called_; }
36 private:
37 void OnCalled() {
38 EXPECT_FALSE(called_);
39 called_ = true;
42 base::Closure callback_;
43 bool called_;
46 class DnsProbeRunnerTest : public testing::Test {
47 protected:
48 void RunTest(MockDnsClientRule::Result query_result,
49 DnsProbeRunner::Result expected_probe_result);
51 TestBrowserThreadBundle bundle_;
52 DnsProbeRunner runner_;
55 void DnsProbeRunnerTest::RunTest(
56 MockDnsClientRule::Result query_result,
57 DnsProbeRunner::Result expected_probe_result) {
58 TestDnsProbeRunnerCallback callback;
60 runner_.SetClient(CreateMockDnsClientForProbes(query_result));
61 runner_.RunProbe(callback.callback());
62 EXPECT_TRUE(runner_.IsRunning());
64 RunLoop().RunUntilIdle();
65 EXPECT_FALSE(runner_.IsRunning());
66 EXPECT_TRUE(callback.called());
67 EXPECT_EQ(expected_probe_result, runner_.result());
70 TEST_F(DnsProbeRunnerTest, Probe_OK) {
71 RunTest(MockDnsClientRule::OK, DnsProbeRunner::CORRECT);
74 TEST_F(DnsProbeRunnerTest, Probe_EMPTY) {
75 RunTest(MockDnsClientRule::EMPTY, DnsProbeRunner::INCORRECT);
78 TEST_F(DnsProbeRunnerTest, Probe_TIMEOUT) {
79 RunTest(MockDnsClientRule::TIMEOUT, DnsProbeRunner::UNREACHABLE);
82 TEST_F(DnsProbeRunnerTest, Probe_FAIL) {
83 RunTest(MockDnsClientRule::FAIL, DnsProbeRunner::INCORRECT);
86 TEST_F(DnsProbeRunnerTest, TwoProbes) {
87 RunTest(MockDnsClientRule::OK, DnsProbeRunner::CORRECT);
88 RunTest(MockDnsClientRule::EMPTY, DnsProbeRunner::INCORRECT);
91 TEST_F(DnsProbeRunnerTest, InvalidDnsConfig) {
92 scoped_ptr<DnsClient> dns_client(DnsClient::CreateClient(NULL));
93 DnsConfig empty_config;
94 dns_client->SetConfig(empty_config);
95 ASSERT_EQ(NULL, dns_client->GetTransactionFactory());
96 runner_.SetClient(dns_client.Pass());
98 TestDnsProbeRunnerCallback callback;
100 runner_.RunProbe(callback.callback());
101 EXPECT_TRUE(runner_.IsRunning());
103 RunLoop().RunUntilIdle();
104 EXPECT_FALSE(runner_.IsRunning());
105 EXPECT_TRUE(callback.called());
106 EXPECT_EQ(DnsProbeRunner::UNKNOWN, runner_.result());
109 } // namespace
111 } // namespace chrome_browser_net