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