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/memory/weak_ptr.h" | |
6 #include "base/message_loop.h" | |
7 #include "base/run_loop.h" | |
8 #include "chrome/browser/net/dns_probe_runner.h" | |
9 #include "chrome/browser/net/dns_probe_test_util.h" | |
10 #include "net/dns/dns_client.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 | |
13 using base::MessageLoopForIO; | |
14 using base::RunLoop; | |
15 using net::MockDnsClientRule; | |
16 | |
17 namespace chrome_browser_net { | |
18 | |
19 namespace { | |
20 | |
21 class TestDnsProbeRunnerCallback { | |
22 public: | |
23 TestDnsProbeRunnerCallback(); | |
24 const DnsProbeRunner::ProbeCallback& callback() const; | |
25 bool called() const; | |
26 DnsProbeRunner::Result result() const; | |
27 | |
28 private: | |
29 void OnCalled(DnsProbeRunner::Result result); | |
30 | |
31 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.
| |
32 DnsProbeRunner::ProbeCallback callback_; | |
33 bool called_; | |
34 DnsProbeRunner::Result result_; | |
35 }; | |
36 | |
37 TestDnsProbeRunnerCallback::TestDnsProbeRunnerCallback() | |
38 : weak_factory_(this), | |
39 callback_(base::Bind(&TestDnsProbeRunnerCallback::OnCalled, | |
40 weak_factory_.GetWeakPtr())), | |
41 called_(false), | |
42 result_(DnsProbeRunner::UNKNOWN) {} | |
43 | |
44 const DnsProbeRunner::ProbeCallback& | |
45 TestDnsProbeRunnerCallback::callback() const { | |
46 return callback_; | |
47 } | |
48 | |
49 bool TestDnsProbeRunnerCallback::called() const { | |
50 return called_; | |
51 } | |
52 | |
53 DnsProbeRunner::Result TestDnsProbeRunnerCallback::result() const { | |
54 DCHECK(called_); | |
55 return result_; | |
56 } | |
57 | |
58 void TestDnsProbeRunnerCallback::OnCalled(DnsProbeRunner::Result result) { | |
59 DCHECK(!called_); | |
60 called_ = true; | |
61 result_ = result; | |
62 } | |
63 | |
64 class DnsProbeRunnerTest : public testing::Test { | |
65 protected: | |
66 void SetRule(MockDnsClientRule::Result good_result); | |
67 void RunUntilIdle(); | |
68 | |
69 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.
| |
70 DnsProbeRunner runner_; | |
71 }; | |
72 | |
73 void DnsProbeRunnerTest::SetRule(MockDnsClientRule::Result good_result) { | |
74 runner_.set_client(CreateMockDnsClientForProbes(good_result)); | |
75 } | |
76 | |
77 | |
78 void DnsProbeRunnerTest::RunUntilIdle() { | |
79 RunLoop run_loop; | |
80 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.
| |
81 } | |
82 | |
83 TEST_F(DnsProbeRunnerTest, Null) { | |
84 SetRule(MockDnsClientRule::OK); | |
85 } | |
86 | |
87 TEST_F(DnsProbeRunnerTest, Probe) { | |
88 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
| |
89 | |
90 EXPECT_FALSE(runner_.is_running()); | |
91 | |
92 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.
| |
93 | |
94 runner_.RunProbe(cb.callback()); | |
95 EXPECT_TRUE(runner_.is_running()); | |
96 | |
97 RunUntilIdle(); | |
98 EXPECT_FALSE(runner_.is_running()); | |
99 EXPECT_TRUE(cb.called()); | |
100 EXPECT_EQ(DnsProbeRunner::CORRECT, cb.result()); | |
101 } | |
102 | |
103 } // namespace | |
104 | |
105 } // namespace chrome_browser_net | |
OLD | NEW |