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..3902e10a07cbc71e09cca2e0f7aa3969a181a92c |
--- /dev/null |
+++ b/chrome/browser/net/dns_probe_runner_unittest.cc |
@@ -0,0 +1,105 @@ |
+// 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/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 "net/dns/dns_client.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using base::MessageLoopForIO; |
+using base::RunLoop; |
+using net::MockDnsClientRule; |
+ |
+namespace chrome_browser_net { |
+ |
+namespace { |
+ |
+class TestDnsProbeRunnerCallback { |
+ public: |
+ TestDnsProbeRunnerCallback(); |
+ const DnsProbeRunner::ProbeCallback& callback() const; |
+ bool called() const; |
+ DnsProbeRunner::Result result() const; |
+ |
+ private: |
+ void OnCalled(DnsProbeRunner::Result result); |
+ |
+ base::WeakPtrFactory<TestDnsProbeRunnerCallback> weak_factory_; |
mmenke
2013/06/11 16:15:35
I don't think we need this.
Deprecated (see juliatuttle)
2013/06/13 14:37:04
Done.
|
+ DnsProbeRunner::ProbeCallback callback_; |
+ bool called_; |
+ DnsProbeRunner::Result result_; |
+}; |
+ |
+TestDnsProbeRunnerCallback::TestDnsProbeRunnerCallback() |
+ : weak_factory_(this), |
+ callback_(base::Bind(&TestDnsProbeRunnerCallback::OnCalled, |
+ weak_factory_.GetWeakPtr())), |
+ 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 SetRule(MockDnsClientRule::Result good_result); |
+ void RunUntilIdle(); |
+ |
+ MessageLoopForIO message_loop_; |
mmenke
2013/06/11 16:15:35
There was a recent post to Chromium-dev announcing
Deprecated (see juliatuttle)
2013/06/13 14:37:04
Done.
|
+ DnsProbeRunner runner_; |
+}; |
+ |
+void DnsProbeRunnerTest::SetRule(MockDnsClientRule::Result good_result) { |
+ runner_.set_client(CreateMockDnsClientForProbes(good_result)); |
+} |
+ |
+ |
+void DnsProbeRunnerTest::RunUntilIdle() { |
+ RunLoop run_loop; |
+ run_loop.RunUntilIdle(); |
mmenke
2013/06/11 16:15:35
Think it's more common to just use:
RunLoop().Run
Deprecated (see juliatuttle)
2013/06/13 14:37:04
Done.
|
+} |
+ |
+TEST_F(DnsProbeRunnerTest, Null) { |
+ SetRule(MockDnsClientRule::OK); |
+} |
+ |
+TEST_F(DnsProbeRunnerTest, Probe) { |
+ SetRule(MockDnsClientRule::OK); |
mmenke
2013/06/11 16:15:35
I suggest turning this into a function, along the
mmenke
2013/06/12 19:17:12
Also, seems like one test should runs two probes w
Deprecated (see juliatuttle)
2013/06/13 14:37:04
Done.
Deprecated (see juliatuttle)
2013/06/13 14:37:04
The new one runs them all with the same runner, in
|
+ |
+ EXPECT_FALSE(runner_.is_running()); |
+ |
+ TestDnsProbeRunnerCallback cb; |
mmenke
2013/06/11 16:15:35
nit: Google style discourages abbreviations, sugg
Deprecated (see juliatuttle)
2013/06/13 14:37:04
Done.
|
+ |
+ runner_.RunProbe(cb.callback()); |
+ EXPECT_TRUE(runner_.is_running()); |
+ |
+ RunUntilIdle(); |
+ EXPECT_FALSE(runner_.is_running()); |
+ EXPECT_TRUE(cb.called()); |
+ EXPECT_EQ(DnsProbeRunner::CORRECT, cb.result()); |
+} |
+ |
+} // namespace |
+ |
+} // namespace chrome_browser_net |