OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "apps/shell/test/shell_test.h" | |
6 #include "base/macros.h" | |
7 #include "base/memory/ref_counted.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/values.h" | |
10 #include "extensions/browser/api/dns/dns_api.h" | |
11 #include "extensions/browser/api/dns/host_resolver_wrapper.h" | |
12 #include "extensions/browser/api/dns/mock_host_resolver_creator.h" | |
13 #include "extensions/browser/api_test_utils.h" | |
14 #include "extensions/browser/extension_function_dispatcher.h" | |
15 #include "extensions/common/extension.h" | |
16 #include "extensions/common/extension_builder.h" | |
17 #include "net/base/net_errors.h" | |
18 | |
19 using extensions::ExtensionFunctionDispatcher; | |
20 using extensions::api_test_utils::RunFunctionAndReturnSingleResult; | |
21 | |
22 namespace { | |
23 | |
24 class TestFunctionDispatcherDelegate | |
25 : public ExtensionFunctionDispatcher::Delegate { | |
26 public: | |
27 TestFunctionDispatcherDelegate() {} | |
28 virtual ~TestFunctionDispatcherDelegate() {} | |
29 | |
30 // NULL implementation. | |
31 private: | |
32 DISALLOW_COPY_AND_ASSIGN(TestFunctionDispatcherDelegate); | |
33 }; | |
34 | |
35 } // namespace | |
36 | |
37 class DnsApiTest : public apps::AppShellTest { | |
38 public: | |
39 DnsApiTest() : resolver_creator_(new extensions::MockHostResolverCreator()) {} | |
40 | |
41 private: | |
42 virtual void SetUpOnMainThread() OVERRIDE { | |
43 apps::AppShellTest::SetUpOnMainThread(); | |
44 extensions::HostResolverWrapper::GetInstance()->SetHostResolverForTesting( | |
45 resolver_creator_->CreateMockHostResolver()); | |
46 } | |
47 | |
48 virtual void TearDownOnMainThread() OVERRIDE { | |
49 extensions::HostResolverWrapper::GetInstance()->SetHostResolverForTesting( | |
50 NULL); | |
51 resolver_creator_->DeleteMockHostResolver(); | |
52 apps::AppShellTest::TearDownOnMainThread(); | |
53 } | |
54 | |
55 // The MockHostResolver asserts that it's used on the same thread on which | |
56 // it's created, which is actually a stronger rule than its real counterpart. | |
57 // But that's fine; it's good practice. | |
58 scoped_refptr<extensions::MockHostResolverCreator> resolver_creator_; | |
59 }; | |
60 | |
61 IN_PROC_BROWSER_TEST_F(DnsApiTest, DnsResolveIPLiteral) { | |
62 scoped_refptr<extensions::DnsResolveFunction> resolve_function( | |
63 new extensions::DnsResolveFunction()); | |
64 scoped_refptr<extensions::Extension> empty_extension( | |
65 extensions::ExtensionBuilder() | |
66 .SetManifest(extensions::DictionaryBuilder().Set("name", "Test").Set( | |
67 "version", "1.0")) | |
68 .Build()); | |
69 | |
70 resolve_function->set_extension(empty_extension.get()); | |
71 resolve_function->set_has_callback(true); | |
72 | |
73 TestFunctionDispatcherDelegate delegate; | |
74 scoped_ptr<ExtensionFunctionDispatcher> dispatcher( | |
75 new ExtensionFunctionDispatcher(browser_context(), &delegate)); | |
76 | |
77 scoped_ptr<base::Value> result( | |
78 RunFunctionAndReturnSingleResult(resolve_function.get(), | |
79 "[\"127.0.0.1\"]", | |
80 browser_context(), | |
81 dispatcher.Pass())); | |
82 base::DictionaryValue* dict = NULL; | |
83 ASSERT_TRUE(result->GetAsDictionary(&dict)); | |
84 | |
85 int result_code = 0; | |
86 EXPECT_TRUE(dict->GetInteger("resultCode", &result_code)); | |
87 EXPECT_EQ(net::OK, result_code); | |
88 | |
89 std::string address; | |
90 EXPECT_TRUE(dict->GetString("address", &address)); | |
91 EXPECT_EQ("127.0.0.1", address); | |
92 } | |
93 | |
94 IN_PROC_BROWSER_TEST_F(DnsApiTest, DnsResolveHostname) { | |
95 scoped_refptr<extensions::DnsResolveFunction> resolve_function( | |
96 new extensions::DnsResolveFunction()); | |
97 scoped_refptr<extensions::Extension> empty_extension( | |
98 extensions::ExtensionBuilder() | |
99 .SetManifest(extensions::DictionaryBuilder().Set("name", "Test").Set( | |
100 "version", "1.0")) | |
101 .Build()); | |
102 | |
103 resolve_function->set_extension(empty_extension.get()); | |
104 resolve_function->set_has_callback(true); | |
105 | |
106 TestFunctionDispatcherDelegate delegate; | |
107 scoped_ptr<ExtensionFunctionDispatcher> dispatcher( | |
108 new ExtensionFunctionDispatcher(browser_context(), &delegate)); | |
109 | |
110 std::string function_arguments("[\""); | |
111 function_arguments += extensions::MockHostResolverCreator::kHostname; | |
112 function_arguments += "\"]"; | |
113 scoped_ptr<base::Value> result( | |
114 RunFunctionAndReturnSingleResult(resolve_function.get(), | |
115 function_arguments, | |
116 browser_context(), | |
117 dispatcher.Pass())); | |
118 base::DictionaryValue* dict = NULL; | |
119 ASSERT_TRUE(result->GetAsDictionary(&dict)); | |
120 | |
121 int result_code = 0; | |
122 EXPECT_TRUE(dict->GetInteger("resultCode", &result_code)); | |
123 EXPECT_EQ(net::OK, result_code); | |
124 | |
125 std::string address; | |
126 EXPECT_TRUE(dict->GetString("address", &address)); | |
127 EXPECT_EQ(extensions::MockHostResolverCreator::kAddress, address); | |
128 } | |
OLD | NEW |