Finish refactoring of DomCodeToUsLayoutKeyboardCode().
[chromium-blink-merge.git] / extensions / browser / api / sockets_udp / sockets_udp_apitest.cc
blobe442114652465cf3b5e69601d220e5c65fb4ff8e
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.
5 #include "base/memory/ref_counted.h"
6 #include "base/strings/stringprintf.h"
7 #include "extensions/browser/api/dns/host_resolver_wrapper.h"
8 #include "extensions/browser/api/dns/mock_host_resolver_creator.h"
9 #include "extensions/browser/api/sockets_udp/sockets_udp_api.h"
10 #include "extensions/browser/api_test_utils.h"
11 #include "extensions/common/extension.h"
12 #include "extensions/common/test_util.h"
13 #include "extensions/shell/test/shell_apitest.h"
14 #include "extensions/shell/test/shell_test.h"
15 #include "extensions/test/extension_test_message_listener.h"
16 #include "extensions/test/result_catcher.h"
17 #include "net/dns/mock_host_resolver.h"
18 #include "net/test/spawned_test_server/spawned_test_server.h"
20 namespace extensions {
22 const std::string kHostname = "127.0.0.1";
23 const int kPort = 8888;
25 class SocketsUdpApiTest : public ShellApiTest {
26 public:
27 SocketsUdpApiTest()
28 : resolver_event_(true, false),
29 resolver_creator_(new MockHostResolverCreator()) {}
31 void SetUpOnMainThread() override {
32 ShellApiTest::SetUpOnMainThread();
34 HostResolverWrapper::GetInstance()->SetHostResolverForTesting(
35 resolver_creator_->CreateMockHostResolver());
38 void TearDownOnMainThread() override {
39 HostResolverWrapper::GetInstance()->SetHostResolverForTesting(NULL);
40 resolver_creator_->DeleteMockHostResolver();
42 ShellApiTest::TearDownOnMainThread();
45 private:
46 base::WaitableEvent resolver_event_;
48 // The MockHostResolver asserts that it's used on the same thread on which
49 // it's created, which is actually a stronger rule than its real counterpart.
50 // But that's fine; it's good practice.
51 scoped_refptr<MockHostResolverCreator> resolver_creator_;
54 IN_PROC_BROWSER_TEST_F(SocketsUdpApiTest, SocketsUdpCreateGood) {
55 scoped_refptr<core_api::SocketsUdpCreateFunction> socket_create_function(
56 new core_api::SocketsUdpCreateFunction());
57 scoped_refptr<Extension> empty_extension = test_util::CreateEmptyExtension();
59 socket_create_function->set_extension(empty_extension.get());
60 socket_create_function->set_has_callback(true);
62 scoped_ptr<base::Value> result(
63 api_test_utils::RunFunctionAndReturnSingleResult(
64 socket_create_function.get(), "[]", browser_context()));
66 base::DictionaryValue* value = NULL;
67 ASSERT_TRUE(result->GetAsDictionary(&value));
68 int socketId = -1;
69 EXPECT_TRUE(value->GetInteger("socketId", &socketId));
70 ASSERT_TRUE(socketId > 0);
73 IN_PROC_BROWSER_TEST_F(SocketsUdpApiTest, SocketsUdpExtension) {
74 scoped_ptr<net::SpawnedTestServer> test_server(new net::SpawnedTestServer(
75 net::SpawnedTestServer::TYPE_UDP_ECHO, net::SpawnedTestServer::kLocalhost,
76 base::FilePath(FILE_PATH_LITERAL("net/data"))));
77 EXPECT_TRUE(test_server->Start());
79 net::HostPortPair host_port_pair = test_server->host_port_pair();
80 int port = host_port_pair.port();
81 ASSERT_TRUE(port > 0);
83 // Test that sendTo() is properly resolving hostnames.
84 host_port_pair.set_host("LOCALhost");
86 ResultCatcher catcher;
87 catcher.RestrictToBrowserContext(browser_context());
89 ExtensionTestMessageListener listener("info_please", true);
91 ASSERT_TRUE(LoadApp("sockets_udp/api"));
92 EXPECT_TRUE(listener.WaitUntilSatisfied());
93 listener.Reply(
94 base::StringPrintf("udp:%s:%d", host_port_pair.host().c_str(), port));
96 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
99 // See crbug.com/321451.
100 IN_PROC_BROWSER_TEST_F(SocketsUdpApiTest, DISABLED_SocketsUdpMulticast) {
101 ResultCatcher catcher;
102 catcher.RestrictToBrowserContext(browser_context());
103 ExtensionTestMessageListener listener("info_please", true);
104 ASSERT_TRUE(LoadApp("sockets_udp/api"));
105 EXPECT_TRUE(listener.WaitUntilSatisfied());
106 listener.Reply(
107 base::StringPrintf("multicast:%s:%d", kHostname.c_str(), kPort));
109 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
112 } // namespace extensions