Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / extensions / browser / api / sockets_tcp_server / sockets_tcp_server_apitest.cc
blob3414f36768d3e0fae85f868af68cfc9702db8959
1 // Copyright 2013 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_tcp_server/sockets_tcp_server_api.h"
10 #include "extensions/browser/api_test_utils.h"
11 #include "extensions/browser/extension_registry.h"
12 #include "extensions/common/test_util.h"
13 #include "extensions/shell/test/shell_apitest.h"
14 #include "extensions/test/extension_test_message_listener.h"
15 #include "extensions/test/result_catcher.h"
16 #include "net/dns/mock_host_resolver.h"
17 #include "net/test/spawned_test_server/spawned_test_server.h"
19 namespace extensions {
21 const std::string kHostname = "127.0.0.1";
22 const int kPort = 8888;
24 class SocketsTcpServerApiTest : public ShellApiTest {
25 public:
26 SocketsTcpServerApiTest()
27 : resolver_event_(true, false),
28 resolver_creator_(new MockHostResolverCreator()) {}
30 void SetUpOnMainThread() override {
31 ShellApiTest::SetUpOnMainThread();
33 HostResolverWrapper::GetInstance()->SetHostResolverForTesting(
34 resolver_creator_->CreateMockHostResolver());
37 void TearDownOnMainThread() override {
38 HostResolverWrapper::GetInstance()->SetHostResolverForTesting(NULL);
39 resolver_creator_->DeleteMockHostResolver();
41 ShellApiTest::TearDownOnMainThread();
44 private:
45 base::WaitableEvent resolver_event_;
47 // The MockHostResolver asserts that it's used on the same thread on which
48 // it's created, which is actually a stronger rule than its real counterpart.
49 // But that's fine; it's good practice.
50 scoped_refptr<MockHostResolverCreator> resolver_creator_;
53 IN_PROC_BROWSER_TEST_F(SocketsTcpServerApiTest, SocketTCPCreateGood) {
54 scoped_refptr<api::SocketsTcpServerCreateFunction> socket_create_function(
55 new api::SocketsTcpServerCreateFunction());
56 scoped_refptr<Extension> empty_extension(test_util::CreateEmptyExtension());
58 socket_create_function->set_extension(empty_extension.get());
59 socket_create_function->set_has_callback(true);
61 scoped_ptr<base::Value> result(
62 api_test_utils::RunFunctionAndReturnSingleResult(
63 socket_create_function.get(), "[]", browser_context()));
64 ASSERT_EQ(base::Value::TYPE_DICTIONARY, result->GetType());
65 base::DictionaryValue* value =
66 static_cast<base::DictionaryValue*>(result.get());
67 int socketId = -1;
68 EXPECT_TRUE(value->GetInteger("socketId", &socketId));
69 ASSERT_TRUE(socketId > 0);
72 IN_PROC_BROWSER_TEST_F(SocketsTcpServerApiTest, SocketTCPServerExtension) {
73 ResultCatcher catcher;
74 catcher.RestrictToBrowserContext(browser_context());
75 ExtensionTestMessageListener listener("info_please", true);
76 ASSERT_TRUE(LoadApp("sockets_tcp_server/api"));
77 EXPECT_TRUE(listener.WaitUntilSatisfied());
78 listener.Reply(
79 base::StringPrintf("tcp_server:%s:%d", kHostname.c_str(), kPort));
81 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
84 IN_PROC_BROWSER_TEST_F(SocketsTcpServerApiTest, SocketTCPServerUnbindOnUnload) {
85 std::string path("sockets_tcp_server/unload");
86 ResultCatcher catcher;
87 const Extension* extension = LoadApp(path);
88 ASSERT_TRUE(extension);
89 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
91 UnloadApp(extension);
93 ASSERT_TRUE(LoadApp(path)) << message_;
94 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
97 } // namespace extensions