Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(283)

Side by Side Diff: chrome/browser/net/dns_probe_service_unittest.cc

Issue 13270005: Display DNS probe results. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Refactor a bit again (DnsProbeRunner is now tied to a single DnsClient), other fixes Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/net/dns_probe_service.h" 5 #include "chrome/browser/net/dns_probe_service.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/memory/weak_ptr.h" 9 #include "base/memory/weak_ptr.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "chrome/browser/net/dns_probe_job.h" 12 #include "chrome/browser/net/dns_probe_runner.h"
13 #include "chrome/browser/net/dns_probe_test_util.h"
13 #include "chrome/common/net/net_error_info.h" 14 #include "chrome/common/net/net_error_info.h"
15 #include "net/dns/dns_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
15 17
16 using chrome_common_net::DnsProbeResult; 18 using base::MessageLoopForIO;
19 using base::RunLoop;
20 using chrome_common_net::DnsProbeStatus;
21 using net::MockDnsClientRule;
17 22
18 namespace chrome_browser_net { 23 namespace chrome_browser_net {
19 24
20 namespace { 25 namespace {
21 26
22 class MockDnsProbeJob : public DnsProbeJob {
23 public:
24 MockDnsProbeJob(const CallbackType& callback,
25 DnsProbeJob::Result result)
26 : weak_factory_(this) {
27 base::MessageLoop::current()->PostTask(
28 FROM_HERE,
29 base::Bind(&MockDnsProbeJob::CallCallback,
30 weak_factory_.GetWeakPtr(),
31 callback,
32 result));
33 }
34
35 virtual ~MockDnsProbeJob() { }
36
37 private:
38 void CallCallback(const CallbackType& callback, Result result) {
39 callback.Run(this, result);
40 }
41
42 base::WeakPtrFactory<MockDnsProbeJob> weak_factory_;
43 };
44
45 class TestDnsProbeService : public DnsProbeService {
46 public:
47 TestDnsProbeService()
48 : DnsProbeService(),
49 system_job_created_(false),
50 public_job_created_(false),
51 mock_system_result_(DnsProbeJob::SERVERS_UNKNOWN),
52 mock_public_result_(DnsProbeJob::SERVERS_UNKNOWN),
53 mock_system_fail_(false) {
54 }
55
56 virtual ~TestDnsProbeService() { }
57
58 void set_mock_results(
59 DnsProbeJob::Result mock_system_result,
60 DnsProbeJob::Result mock_public_result) {
61 mock_system_result_ = mock_system_result;
62 mock_public_result_ = mock_public_result;
63 }
64
65 void set_mock_system_fail(bool mock_system_fail) {
66 mock_system_fail_ = mock_system_fail;
67 }
68
69 bool jobs_created(void) {
70 return system_job_created_ && public_job_created_;
71 }
72
73 void ResetJobsCreated() {
74 system_job_created_ = false;
75 public_job_created_ = false;
76 }
77
78 void MockExpireResults() {
79 ExpireResults();
80 }
81
82 bool system_job_created_;
83 bool public_job_created_;
84
85 private:
86 // Override methods in DnsProbeService to return mock jobs:
87
88 virtual scoped_ptr<DnsProbeJob> CreateSystemProbeJob(
89 const DnsProbeJob::CallbackType& job_callback) OVERRIDE {
90 if (mock_system_fail_)
91 return scoped_ptr<DnsProbeJob>(NULL);
92
93 system_job_created_ = true;
94 return scoped_ptr<DnsProbeJob>(
95 new MockDnsProbeJob(job_callback,
96 mock_system_result_));
97 }
98
99 virtual scoped_ptr<DnsProbeJob> CreatePublicProbeJob(
100 const DnsProbeJob::CallbackType& job_callback) OVERRIDE {
101 public_job_created_ = true;
102 return scoped_ptr<DnsProbeJob>(
103 new MockDnsProbeJob(job_callback,
104 mock_public_result_));
105 }
106
107 DnsProbeJob::Result mock_system_result_;
108 DnsProbeJob::Result mock_public_result_;
109 bool mock_system_fail_;
110 };
111
112 class DnsProbeServiceTest : public testing::Test { 27 class DnsProbeServiceTest : public testing::Test {
113 public: 28 public:
114 DnsProbeServiceTest() 29 DnsProbeServiceTest()
115 : callback_called_(false), 30 : callback_called_(false),
116 callback_result_(chrome_common_net::DNS_PROBE_UNKNOWN) { 31 callback_result_(chrome_common_net::DNS_PROBE_MAX) {
117 } 32 }
118 33
119 void Probe() { 34 void Probe() {
120 service_.ProbeDns(base::Bind(&DnsProbeServiceTest::ProbeCallback, 35 service_.ProbeDns(base::Bind(&DnsProbeServiceTest::ProbeCallback,
121 base::Unretained(this))); 36 base::Unretained(this)));
122 } 37 }
123 38
124 void RunUntilIdle() { 39 void RunUntilIdle() {
125 base::RunLoop run_loop; 40 RunLoop run_loop;
126 run_loop.RunUntilIdle(); 41 run_loop.RunUntilIdle();
mmenke 2013/06/12 19:17:12 Per earlier comment, suggest inlining this everywh
Deprecated (see juliatuttle) 2013/06/13 14:37:04 Done.
127 } 42 }
128 43
129 void Reset() { 44 void Reset() {
130 service_.ResetJobsCreated();
131 callback_called_ = false; 45 callback_called_ = false;
132 } 46 }
133 47
134 base::MessageLoopForIO message_loop_; 48 protected:
135 TestDnsProbeService service_; 49 void SetRules(MockDnsClientRule::Result system_good_result,
136 bool callback_called_; 50 MockDnsClientRule::Result public_good_result) {
137 DnsProbeResult callback_result_; 51 service_.SetSystemClientForTesting(
52 CreateMockDnsClientForProbes(system_good_result));
53 service_.SetPublicClientForTesting(
54 CreateMockDnsClientForProbes(public_good_result));
55 }
56
57 void ExpireServiceResult() {
58 service_.ExpireResultForTesting();
59 }
60
61 bool callback_called() { return callback_called_; }
62 DnsProbeStatus callback_result() { return callback_result_; }
138 63
139 private: 64 private:
140 void ProbeCallback(DnsProbeResult result) { 65 void ProbeCallback(DnsProbeStatus result) {
141 callback_called_ = true; 66 callback_called_ = true;
142 callback_result_ = result; 67 callback_result_ = result;
143 } 68 }
69
70 DnsProbeService service_;
71 bool callback_called_;
72 DnsProbeStatus callback_result_;
73 MessageLoopForIO message_loop_;
mmenke 2013/06/12 19:17:12 Other comment about how to handle message loops ap
Deprecated (see juliatuttle) 2013/06/13 14:37:04 Done.
144 }; 74 };
145 75
146 TEST_F(DnsProbeServiceTest, Null) { 76 TEST_F(DnsProbeServiceTest, Null) {
147 } 77 }
148 78
149 TEST_F(DnsProbeServiceTest, Probe) { 79 TEST_F(DnsProbeServiceTest, Probe) {
150 service_.set_mock_results(DnsProbeJob::SERVERS_CORRECT, 80 SetRules(MockDnsClientRule::OK, MockDnsClientRule::OK);
151 DnsProbeJob::SERVERS_CORRECT);
152
153 Probe();
154 EXPECT_TRUE(service_.jobs_created());
155 EXPECT_FALSE(callback_called_);
156
157 RunUntilIdle();
158 EXPECT_TRUE(callback_called_);
159 EXPECT_EQ(chrome_common_net::DNS_PROBE_NXDOMAIN, callback_result_);
160 }
161
162 TEST_F(DnsProbeServiceTest, Cache) {
163 service_.set_mock_results(DnsProbeJob::SERVERS_CORRECT,
164 DnsProbeJob::SERVERS_CORRECT);
165 81
166 Probe(); 82 Probe();
167 RunUntilIdle(); 83 RunUntilIdle();
168 Reset(); 84 EXPECT_TRUE(callback_called());
85 EXPECT_EQ(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN, callback_result());
86 }
87
88 // TODO(ttuttle): More test cases?
mmenke 2013/06/12 19:17:12 Suggestions: Sync failure, all other different re
Deprecated (see juliatuttle) 2013/06/13 14:37:04 Done.
89
90 TEST_F(DnsProbeServiceTest, Cache) {
91 SetRules(MockDnsClientRule::OK, MockDnsClientRule::OK);
92
93 Probe();
94 RunUntilIdle();
169 95
170 // Cached NXDOMAIN result should persist. 96 // Cached NXDOMAIN result should persist.
mmenke 2013/06/12 19:17:12 Shouldn't you change the result, so if the result
Deprecated (see juliatuttle) 2013/06/13 14:37:04 Done.
171 97
98 Reset();
99
172 Probe(); 100 Probe();
173 EXPECT_FALSE(service_.jobs_created());
174
175 RunUntilIdle(); 101 RunUntilIdle();
mmenke 2013/06/12 19:17:12 Not needed, is it?
Deprecated (see juliatuttle) 2013/06/13 14:37:04 It is now.
176 EXPECT_TRUE(callback_called_); 102 EXPECT_TRUE(callback_called());
177 EXPECT_EQ(chrome_common_net::DNS_PROBE_NXDOMAIN, callback_result_); 103 EXPECT_EQ(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN, callback_result());
178 } 104 }
179 105
180 TEST_F(DnsProbeServiceTest, Expired) { 106 TEST_F(DnsProbeServiceTest, Expired) {
181 service_.set_mock_results(DnsProbeJob::SERVERS_CORRECT, 107 SetRules(MockDnsClientRule::OK, MockDnsClientRule::OK);
182 DnsProbeJob::SERVERS_CORRECT);
183 108
184 Probe(); 109 Probe();
185 EXPECT_TRUE(service_.jobs_created()); 110 RunUntilIdle();
111 EXPECT_TRUE(callback_called());
112 EXPECT_EQ(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN, callback_result());
186 113
187 RunUntilIdle(); 114 ExpireServiceResult();
188 EXPECT_TRUE(callback_called_);
189 EXPECT_EQ(chrome_common_net::DNS_PROBE_NXDOMAIN, callback_result_);
190
191 Reset(); 115 Reset();
192 116
193 service_.MockExpireResults();
194
195 Probe(); 117 Probe();
mmenke 2013/06/12 19:17:12 Should change the result for the second probe.
Deprecated (see juliatuttle) 2013/06/13 14:37:04 Done.
196 EXPECT_TRUE(service_.jobs_created());
197
198 RunUntilIdle(); 118 RunUntilIdle();
199 EXPECT_TRUE(callback_called_); 119 EXPECT_TRUE(callback_called());
200 EXPECT_EQ(chrome_common_net::DNS_PROBE_NXDOMAIN, callback_result_); 120 EXPECT_EQ(chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN, callback_result());
201 }
202
203 TEST_F(DnsProbeServiceTest, SystemFail) {
204 service_.set_mock_results(DnsProbeJob::SERVERS_CORRECT,
205 DnsProbeJob::SERVERS_CORRECT);
206 service_.set_mock_system_fail(true);
207
208 Probe();
209 EXPECT_TRUE(callback_called_);
210 EXPECT_EQ(chrome_common_net::DNS_PROBE_UNKNOWN, callback_result_);
211
212 Reset();
213
214 RunUntilIdle();
215 EXPECT_FALSE(callback_called_);
216 } 121 }
217 122
218 } // namespace 123 } // namespace
219 124
220 } // namespace chrome_browser_net 125 } // namespace chrome_browser_net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698