summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/test_extension_environment.cc
blob: cf5cc15f084c60bb8c898a531598b28d7841d333 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/test_extension_environment.h"

#include "base/command_line.h"
#include "base/json/json_writer.h"
#include "base/run_loop.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/sessions/session_tab_helper.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_builder.h"
#include "chrome/common/extensions/value_builder.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace extensions {

using content::BrowserThread;

TestExtensionEnvironment::TestExtensionEnvironment()
    : ui_thread_(BrowserThread::UI, &loop_),
      file_thread_(BrowserThread::FILE),
      file_blocking_thread_(BrowserThread::FILE_USER_BLOCKING),
      io_thread_(BrowserThread::IO),
      profile_(new TestingProfile),
      extension_service_(NULL) {
  file_thread_.Start();
  file_blocking_thread_.Start();
  io_thread_.StartIOThread();
}

TestExtensionEnvironment::~TestExtensionEnvironment() {
  profile_.reset();
  // Delete the profile, and then cycle the message loop to clear
  // out delayed deletions.
  base::RunLoop run_loop;
  loop_.PostTask(FROM_HERE, run_loop.QuitClosure());
  run_loop.Run();
}

ExtensionService* TestExtensionEnvironment::GetExtensionService() {
  if (extension_service_ == NULL) {
    TestExtensionSystem* extension_system =
        static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()));
    extension_service_ = extension_system->CreateExtensionService(
        CommandLine::ForCurrentProcess(), base::FilePath(), false);
  }
  return extension_service_;
}

const Extension* TestExtensionEnvironment::MakeExtension(
    const Value& manifest_extra) {
  scoped_ptr<base::DictionaryValue> manifest = DictionaryBuilder()
      .Set("name", "Extension")
      .Set("version", "1.0")
      .Set("manifest_version", 2)
      .Build();
  const DictionaryValue* manifest_extra_dict;
  if (manifest_extra.GetAsDictionary(&manifest_extra_dict)) {
    manifest->MergeDictionary(manifest_extra_dict);
  } else {
    std::string manifest_json;
    base::JSONWriter::Write(&manifest_extra, &manifest_json);
    ADD_FAILURE() << "Expected dictionary; got \"" << manifest_json << "\"";
  }

  scoped_refptr<Extension> result =
      ExtensionBuilder().SetManifest(manifest.Pass()).Build();
  GetExtensionService()->AddExtension(result);
  return result.get();
}

scoped_ptr<content::WebContents> TestExtensionEnvironment::MakeTab() const {
  scoped_ptr<content::WebContents> contents(
      content::WebContentsTester::CreateTestWebContents(profile(), NULL));
  // Create a tab id.
  SessionTabHelper::CreateForWebContents(contents.get());
  return contents.Pass();
}

}  // namespace extensions