Home | History | Annotate | Download | only in socket
      1 // Copyright (c) 2012 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/socket_api.h"
     11 #include "chrome/browser/extensions/extension_function_test_utils.h"
     12 #include "chrome/browser/extensions/test_extension_system.h"
     13 #include "chrome/browser/profiles/profile_manager.h"
     14 #include "chrome/test/base/browser_with_test_window_test.h"
     15 #include "chrome/test/base/testing_browser_process.h"
     16 #include "testing/gmock/include/gmock/gmock.h"
     17 #include "testing/gtest/include/gtest/gtest.h"
     18 
     19 namespace utils = extension_function_test_utils;
     20 
     21 namespace extensions {
     22 
     23 BrowserContextKeyedService* ApiResourceManagerTestFactory(
     24     content::BrowserContext* profile) {
     25   content::BrowserThread::ID id;
     26   CHECK(content::BrowserThread::GetCurrentThreadIdentifier(&id));
     27   return ApiResourceManager<Socket>::CreateApiResourceManagerForTest(
     28       static_cast<Profile*>(profile), id);
     29 }
     30 
     31 class SocketUnitTest : public BrowserWithTestWindowTest {
     32  public:
     33   virtual void SetUp() {
     34     BrowserWithTestWindowTest::SetUp();
     35 
     36     ApiResourceManager<Socket>::GetFactoryInstance()->SetTestingFactoryAndUse(
     37         browser()->profile(), ApiResourceManagerTestFactory);
     38 
     39     extension_ = utils::CreateEmptyExtensionWithLocation(
     40         extensions::Manifest::UNPACKED);
     41   }
     42 
     43   base::Value* RunFunctionWithExtension(
     44       UIThreadExtensionFunction* function, const std::string& args) {
     45     scoped_refptr<UIThreadExtensionFunction> delete_function(function);
     46     function->set_extension(extension_.get());
     47     return utils::RunFunctionAndReturnSingleResult(function, args, browser());
     48   }
     49 
     50   base::DictionaryValue* RunFunctionAndReturnDict(
     51       UIThreadExtensionFunction* function, const std::string& args) {
     52     base::Value* result = RunFunctionWithExtension(function, args);
     53     return result ? utils::ToDictionary(result) : NULL;
     54   }
     55 
     56  protected:
     57   scoped_refptr<extensions::Extension> extension_;
     58 };
     59 
     60 TEST_F(SocketUnitTest, Create) {
     61   // Get BrowserThread
     62   content::BrowserThread::ID id;
     63   CHECK(content::BrowserThread::GetCurrentThreadIdentifier(&id));
     64 
     65   // Create SocketCreateFunction and put it on BrowserThread
     66   SocketCreateFunction *function = new SocketCreateFunction();
     67   function->set_work_thread_id(id);
     68 
     69   // Run tests
     70   scoped_ptr<base::DictionaryValue> result(RunFunctionAndReturnDict(
     71       function, "[\"tcp\"]"));
     72   ASSERT_TRUE(result.get());
     73 }
     74 
     75 }  // namespace extensions
     76