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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/extensions/api/dns/mock_host_resolver_creator.h" 6 7 #include "chrome/test/base/in_process_browser_test.h" 8 #include "content/public/browser/browser_thread.h" 9 #include "net/dns/mock_host_resolver.h" 10 11 using content::BrowserThread; 12 13 namespace extensions { 14 15 const std::string MockHostResolverCreator::kHostname = "www.sowbug.com"; 16 const std::string MockHostResolverCreator::kAddress = "9.8.7.6"; 17 MockHostResolverCreator()18MockHostResolverCreator::MockHostResolverCreator() 19 : resolver_event_(true, false), 20 mock_host_resolver_(NULL) { 21 } 22 ~MockHostResolverCreator()23MockHostResolverCreator::~MockHostResolverCreator() { 24 } 25 CreateMockHostResolver()26net::MockHostResolver* MockHostResolverCreator::CreateMockHostResolver() { 27 DCHECK(!mock_host_resolver_); 28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 29 30 bool result = BrowserThread::PostTask( 31 BrowserThread::IO, FROM_HERE, 32 base::Bind(&MockHostResolverCreator::CreateMockHostResolverOnIOThread, 33 this)); 34 DCHECK(result); 35 36 base::TimeDelta max_time = base::TimeDelta::FromSeconds(5); 37 resolver_event_.TimedWait(max_time); 38 39 return mock_host_resolver_; 40 } 41 CreateMockHostResolverOnIOThread()42void MockHostResolverCreator::CreateMockHostResolverOnIOThread() { 43 mock_host_resolver_ = new net::MockHostResolver(); 44 mock_host_resolver_->rules()->AddRule(kHostname, kAddress); 45 mock_host_resolver_->rules()->AddSimulatedFailure("this.hostname.is.bogus"); 46 resolver_event_.Signal(); 47 } 48 DeleteMockHostResolver()49void MockHostResolverCreator::DeleteMockHostResolver() { 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 51 if (!mock_host_resolver_) 52 return; 53 resolver_event_.Reset(); 54 bool result = BrowserThread::PostTask( 55 BrowserThread::IO, FROM_HERE, 56 base::Bind(&MockHostResolverCreator::DeleteMockHostResolverOnIOThread, 57 this)); 58 DCHECK(result); 59 60 base::TimeDelta max_time = base::TimeDelta::FromSeconds(5); 61 ASSERT_TRUE(resolver_event_.TimedWait(max_time)); 62 } 63 DeleteMockHostResolverOnIOThread()64void MockHostResolverCreator::DeleteMockHostResolverOnIOThread() { 65 delete(mock_host_resolver_); 66 mock_host_resolver_ = NULL; 67 resolver_event_.Signal(); 68 } 69 70 } // namespace extensions 71