- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / sockets_udp / sockets_udp_api_unittest.cc
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.
4
5 #include "base/values.h"
6 #include "chrome/browser/browser_process_impl.h"
7 #include "chrome/browser/extensions/api/api_function.h"
8 #include "chrome/browser/extensions/api/api_resource_manager.h"
9 #include "chrome/browser/extensions/api/socket/socket.h"
10 #include "chrome/browser/extensions/api/socket/udp_socket.h"
11 #include "chrome/browser/extensions/api/sockets_udp/sockets_udp_api.h"
12 #include "chrome/browser/extensions/extension_function_test_utils.h"
13 #include "chrome/browser/extensions/test_extension_system.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/test/base/browser_with_test_window_test.h"
16 #include "chrome/test/base/testing_browser_process.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace utils = extension_function_test_utils;
21
22 namespace extensions {
23 namespace api {
24
25 static
26 BrowserContextKeyedService* ApiResourceManagerTestFactory(
27     content::BrowserContext* profile) {
28   content::BrowserThread::ID id;
29   CHECK(content::BrowserThread::GetCurrentThreadIdentifier(&id));
30   return ApiResourceManager<ResumableUDPSocket>::
31       CreateApiResourceManagerForTest(static_cast<Profile*>(profile), id);
32 }
33
34 class SocketsUdpUnitTest : public BrowserWithTestWindowTest {
35  public:
36   virtual void SetUp() {
37     BrowserWithTestWindowTest::SetUp();
38
39     ApiResourceManager<ResumableUDPSocket>::GetFactoryInstance()->
40         SetTestingFactoryAndUse(browser()->profile(),
41                                 ApiResourceManagerTestFactory);
42
43     extension_ = utils::CreateEmptyExtensionWithLocation(
44         extensions::Manifest::UNPACKED);
45   }
46
47   base::Value* RunFunctionWithExtension(
48       UIThreadExtensionFunction* function, const std::string& args) {
49     scoped_refptr<UIThreadExtensionFunction> delete_function(function);
50     function->set_extension(extension_.get());
51     return utils::RunFunctionAndReturnSingleResult(function, args, browser());
52   }
53
54   base::DictionaryValue* RunFunctionAndReturnDict(
55       UIThreadExtensionFunction* function, const std::string& args) {
56     base::Value* result = RunFunctionWithExtension(function, args);
57     return result ? utils::ToDictionary(result) : NULL;
58   }
59
60  protected:
61   scoped_refptr<extensions::Extension> extension_;
62 };
63
64 TEST_F(SocketsUdpUnitTest, Create) {
65   // Get BrowserThread
66   content::BrowserThread::ID id;
67   CHECK(content::BrowserThread::GetCurrentThreadIdentifier(&id));
68
69   // Create SocketCreateFunction and put it on BrowserThread
70   SocketsUdpCreateFunction *function = new SocketsUdpCreateFunction();
71   function->set_work_thread_id(id);
72
73   // Run tests
74   scoped_ptr<base::DictionaryValue> result(RunFunctionAndReturnDict(
75       function, "[{\"persistent\": true, \"name\": \"foo\"}]"));
76   ASSERT_TRUE(result.get());
77 }
78
79 }  // namespace api
80 }  // namespace extensions