Home | History | Annotate | Download | only in extensions
      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 "chrome/browser/extensions/test_extension_environment.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/json/json_writer.h"
      9 #include "base/run_loop.h"
     10 #include "base/values.h"
     11 #include "chrome/browser/extensions/extension_service.h"
     12 #include "chrome/browser/extensions/test_extension_system.h"
     13 #include "chrome/browser/sessions/session_tab_helper.h"
     14 #include "chrome/test/base/testing_profile.h"
     15 #include "content/public/test/test_utils.h"
     16 #include "content/public/test/web_contents_tester.h"
     17 #include "extensions/common/extension.h"
     18 #include "extensions/common/extension_builder.h"
     19 #include "extensions/common/value_builder.h"
     20 #include "testing/gtest/include/gtest/gtest.h"
     21 
     22 #if defined(USE_AURA)
     23 #include "ui/aura/env.h"
     24 #endif
     25 
     26 namespace extensions {
     27 
     28 using content::BrowserThread;
     29 
     30 TestExtensionEnvironment::TestExtensionEnvironment()
     31     : profile_(new TestingProfile),
     32       extension_service_(NULL),
     33       extension_prefs_(NULL) {
     34 #if defined(USE_AURA)
     35   aura::Env::CreateInstance(true);
     36 #endif
     37 }
     38 
     39 TestExtensionEnvironment::~TestExtensionEnvironment() {
     40 #if defined(USE_AURA)
     41   aura::Env::DeleteInstance();
     42 #endif
     43 }
     44 
     45 TestingProfile* TestExtensionEnvironment::profile() const {
     46   return profile_.get();
     47 }
     48 
     49 TestExtensionSystem* TestExtensionEnvironment::GetExtensionSystem() {
     50   return static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()));
     51 }
     52 
     53 ExtensionService* TestExtensionEnvironment::GetExtensionService() {
     54   if (extension_service_ == NULL) {
     55     extension_service_ = GetExtensionSystem()->CreateExtensionService(
     56         CommandLine::ForCurrentProcess(), base::FilePath(), false);
     57   }
     58   return extension_service_;
     59 }
     60 
     61 ExtensionPrefs* TestExtensionEnvironment::GetExtensionPrefs() {
     62   if (extension_prefs_ == NULL) {
     63     extension_prefs_ = GetExtensionSystem()->CreateExtensionPrefs(
     64         CommandLine::ForCurrentProcess(), base::FilePath());
     65   }
     66   return extension_prefs_;
     67 }
     68 
     69 const Extension* TestExtensionEnvironment::MakeExtension(
     70     const base::Value& manifest_extra) {
     71   scoped_ptr<base::DictionaryValue> manifest = DictionaryBuilder()
     72       .Set("name", "Extension")
     73       .Set("version", "1.0")
     74       .Set("manifest_version", 2)
     75       .Build();
     76   const base::DictionaryValue* manifest_extra_dict;
     77   if (manifest_extra.GetAsDictionary(&manifest_extra_dict)) {
     78     manifest->MergeDictionary(manifest_extra_dict);
     79   } else {
     80     std::string manifest_json;
     81     base::JSONWriter::Write(&manifest_extra, &manifest_json);
     82     ADD_FAILURE() << "Expected dictionary; got \"" << manifest_json << "\"";
     83   }
     84 
     85   scoped_refptr<Extension> result =
     86       ExtensionBuilder().SetManifest(manifest.Pass()).Build();
     87   GetExtensionService()->AddExtension(result.get());
     88   return result.get();
     89 }
     90 
     91 scoped_ptr<content::WebContents> TestExtensionEnvironment::MakeTab() const {
     92   scoped_ptr<content::WebContents> contents(
     93       content::WebContentsTester::CreateTestWebContents(profile(), NULL));
     94   // Create a tab id.
     95   SessionTabHelper::CreateForWebContents(contents.get());
     96   return contents.Pass();
     97 }
     98 
     99 }  // namespace extensions
    100