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.
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
;
17 using content::TestBrowserThreadBundle
;
20 using net::MockDnsClientRule
;
22 namespace chrome_browser_net
{
26 class TestDnsProbeRunnerCallback
{
28 TestDnsProbeRunnerCallback()
29 : callback_(base::Bind(&TestDnsProbeRunnerCallback::OnCalled
,
30 base::Unretained(this))),
33 const base::Closure
& callback() const { return callback_
; }
34 bool called() const { return called_
; }
38 EXPECT_FALSE(called_
);
42 base::Closure callback_
;
46 class DnsProbeRunnerTest
: public testing::Test
{
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());
111 } // namespace chrome_browser_net