Chromium Code Reviews| Index: chrome/browser/net/dns_probe_runner_unittest.cc |
| diff --git a/chrome/browser/net/dns_probe_runner_unittest.cc b/chrome/browser/net/dns_probe_runner_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e1d5133136bb47cee88ba424fe8530138767c99e |
| --- /dev/null |
| +++ b/chrome/browser/net/dns_probe_runner_unittest.cc |
| @@ -0,0 +1,120 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/bind.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/message_loop.h" |
| +#include "base/run_loop.h" |
| +#include "chrome/browser/net/dns_probe_runner.h" |
| +#include "chrome/browser/net/dns_probe_test_util.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "net/dns/dns_client.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using base::MessageLoopForIO; |
| +using base::RunLoop; |
| +using content::TestBrowserThreadBundle; |
| +using net::MockDnsClientRule; |
| + |
| +namespace chrome_browser_net { |
| + |
| +namespace { |
| + |
| +class TestDnsProbeRunnerCallback { |
| + public: |
| + TestDnsProbeRunnerCallback(); |
| + const DnsProbeRunner::ProbeCallback& callback() const; |
|
szym
2013/06/13 20:50:48
Put the accessor code inline.
Deprecated (see juliatuttle)
2013/06/18 19:36:37
Done.
|
| + bool called() const; |
| + DnsProbeRunner::Result result() const; |
| + |
| + private: |
| + void OnCalled(DnsProbeRunner::Result result); |
| + |
| + DnsProbeRunner::ProbeCallback callback_; |
| + bool called_; |
| + DnsProbeRunner::Result result_; |
| +}; |
| + |
| +TestDnsProbeRunnerCallback::TestDnsProbeRunnerCallback() |
| + : callback_(base::Bind(&TestDnsProbeRunnerCallback::OnCalled, |
| + base::Unretained(this))), |
| + called_(false), |
| + result_(DnsProbeRunner::UNKNOWN) {} |
| + |
| +const DnsProbeRunner::ProbeCallback& |
| + TestDnsProbeRunnerCallback::callback() const { |
| + return callback_; |
| +} |
| + |
| +bool TestDnsProbeRunnerCallback::called() const { |
| + return called_; |
| +} |
| + |
| +DnsProbeRunner::Result TestDnsProbeRunnerCallback::result() const { |
| + DCHECK(called_); |
| + return result_; |
| +} |
| + |
| +void TestDnsProbeRunnerCallback::OnCalled(DnsProbeRunner::Result result) { |
| + DCHECK(!called_); |
| + called_ = true; |
| + result_ = result; |
| +} |
| + |
| +class DnsProbeRunnerTest : public testing::Test { |
| + protected: |
| + void RunTest(MockDnsClientRule::Result good_query_result, |
| + DnsProbeRunner::Result expected_probe_result); |
| + |
| + TestBrowserThreadBundle bundle_; |
| + DnsProbeRunner runner_; |
| +}; |
| + |
| +void DnsProbeRunnerTest::RunTest( |
| + MockDnsClientRule::Result good_query_result, |
| + DnsProbeRunner::Result expected_probe_result) { |
| + TestDnsProbeRunnerCallback callback; |
| + |
| + runner_.SetClient(CreateMockDnsClientForProbes(good_query_result)); |
| + runner_.RunProbe(callback.callback()); |
| + if (good_query_result != MockDnsClientRule::FAIL_SYNC) |
| + EXPECT_TRUE(runner_.IsRunning()); |
| + |
| + RunLoop().RunUntilIdle(); |
| + EXPECT_FALSE(runner_.IsRunning()); |
| + EXPECT_TRUE(callback.called()); |
| + EXPECT_EQ(expected_probe_result, callback.result()); |
| +} |
| + |
| +TEST_F(DnsProbeRunnerTest, Null) { |
|
szym
2013/06/13 20:50:48
All it does is test constructor+destructor. If tha
Deprecated (see juliatuttle)
2013/06/18 19:36:37
Done.
|
| +} |
| + |
| +TEST_F(DnsProbeRunnerTest, Probe_OK) { |
| + RunTest(MockDnsClientRule::OK, DnsProbeRunner::CORRECT); |
| +} |
| + |
| +TEST_F(DnsProbeRunnerTest, Probe_EMPTY) { |
| + RunTest(MockDnsClientRule::EMPTY, DnsProbeRunner::INCORRECT); |
| +} |
| + |
| +TEST_F(DnsProbeRunnerTest, Probe_TIMEOUT) { |
| + RunTest(MockDnsClientRule::TIMEOUT, DnsProbeRunner::UNREACHABLE); |
| +} |
| + |
| +TEST_F(DnsProbeRunnerTest, Probe_FAIL_ASYNC) { |
| + RunTest(MockDnsClientRule::FAIL_ASYNC, DnsProbeRunner::INCORRECT); |
|
szym
2013/06/13 20:50:48
Consider (perhaps in a future CL) adding new MockD
Deprecated (see juliatuttle)
2013/06/18 19:36:37
Yeah, it'd be useful to be able to specify an erro
|
| +} |
| + |
| +TEST_F(DnsProbeRunnerTest, Probe_FAIL_SYNC) { |
| + RunTest(MockDnsClientRule::FAIL_SYNC, DnsProbeRunner::INCORRECT); |
| +} |
| + |
| +TEST_F(DnsProbeRunnerTest, TwoProbes) { |
| + RunTest(MockDnsClientRule::OK, DnsProbeRunner::CORRECT); |
| + RunTest(MockDnsClientRule::EMPTY, DnsProbeRunner::INCORRECT); |
| +} |
| + |
| +} // namespace |
| + |
| +} // namespace chrome_browser_net |