blob: bfc5664b9e16638e13f38703ab587c890a93e4a0 [file] [log] [blame]
[email protected]6014d672008-12-05 00:38:251// Copyright (c) 2006-2008 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
[email protected]f0397fa2008-12-11 17:59:585#include <algorithm>
[email protected]6014d672008-12-05 00:38:256#include <vector>
7
8#include "base/file_path.h"
9#include "base/file_util.h"
10#include "base/message_loop.h"
11#include "base/path_service.h"
12#include "base/string_util.h"
[email protected]f0397fa2008-12-11 17:59:5813#include "chrome/browser/extensions/extension.h"
[email protected]6014d672008-12-05 00:38:2514#include "chrome/browser/extensions/extensions_service.h"
15#include "chrome/common/chrome_paths.h"
16#include "chrome/common/json_value_serializer.h"
17#include "testing/gtest/include/gtest/gtest.h"
[email protected]f66c110c2008-12-05 20:26:2918#include "testing/platform_test.h"
[email protected]6014d672008-12-05 00:38:2519
[email protected]f0397fa2008-12-11 17:59:5820namespace {
21
22struct ExtensionsOrder {
23 bool operator()(const Extension* a, const Extension* b) {
24 return a->name() < b->name();
25 }
26};
27
28} // namespace
[email protected]6014d672008-12-05 00:38:2529
30// A mock implementation of ExtensionsServiceFrontendInterface for testing the
31// backend.
32class ExtensionsServiceTestFrontend
33 : public ExtensionsServiceFrontendInterface {
34 public:
[email protected]3acbd422008-12-08 18:25:0035 ~ExtensionsServiceTestFrontend() {
36 for (ExtensionList::iterator iter = extensions_.begin();
37 iter != extensions_.end(); ++iter) {
38 delete *iter;
39 }
40 }
41
42 std::vector<std::string>* errors() {
[email protected]6014d672008-12-05 00:38:2543 return &errors_;
44 }
45
46 ExtensionList* extensions() {
[email protected]3acbd422008-12-08 18:25:0047 return &extensions_;
[email protected]6014d672008-12-05 00:38:2548 }
49
50 // ExtensionsServiceFrontendInterface
51 virtual MessageLoop* GetMessageLoop() {
52 return &message_loop_;
53 }
54
[email protected]3acbd422008-12-08 18:25:0055 virtual void OnExtensionLoadError(const std::string& message) {
[email protected]6014d672008-12-05 00:38:2556 errors_.push_back(message);
57 }
58
[email protected]08816d0d2008-12-08 18:43:5359 virtual void OnExtensionsLoadedFromDirectory(ExtensionList* new_extensions) {
60 extensions_.insert(extensions_.end(), new_extensions->begin(),
61 new_extensions->end());
62 delete new_extensions;
[email protected]f0397fa2008-12-11 17:59:5863 // In the tests we rely on extensions being in particular order,
64 // which is not always the case (and is not guaranteed by used APIs).
65 std::stable_sort(extensions_.begin(), extensions_.end(), ExtensionsOrder());
[email protected]6014d672008-12-05 00:38:2566 }
67
68 private:
69 MessageLoop message_loop_;
[email protected]3acbd422008-12-08 18:25:0070 ExtensionList extensions_;
71 std::vector<std::string> errors_;
[email protected]6014d672008-12-05 00:38:2572};
73
[email protected]f66c110c2008-12-05 20:26:2974// make the test a PlatformTest to setup autorelease pools properly on mac
75typedef PlatformTest ExtensionsServiceTest;
[email protected]6014d672008-12-05 00:38:2576
77// Test loading extensions from the profile directory.
78TEST_F(ExtensionsServiceTest, LoadAllExtensionsFromDirectory) {
[email protected]6014d672008-12-05 00:38:2579 std::wstring extensions_dir;
80 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_dir));
[email protected]eab9b452009-01-23 20:48:5981 FilePath extensions_path = FilePath::FromWStringHack(extensions_dir).Append(
[email protected]6014d672008-12-05 00:38:2582 FILE_PATH_LITERAL("extensions"));
83
84 scoped_refptr<ExtensionsServiceBackend> backend(new ExtensionsServiceBackend);
85 scoped_refptr<ExtensionsServiceTestFrontend> frontend(
86 new ExtensionsServiceTestFrontend);
87
88 std::vector<Extension*> extensions;
[email protected]eab9b452009-01-23 20:48:5989 EXPECT_TRUE(backend->LoadExtensionsFromDirectory(extensions_path,
[email protected]6014d672008-12-05 00:38:2590 scoped_refptr<ExtensionsServiceFrontendInterface>(frontend.get())));
91 frontend->GetMessageLoop()->RunAllPending();
92
93 // Note: There can be more errors if there are extra directories, like .svn
94 // directories.
95 EXPECT_TRUE(frontend->errors()->size() >= 2u);
[email protected]f0397fa2008-12-11 17:59:5896 ASSERT_EQ(2u, frontend->extensions()->size());
[email protected]6014d672008-12-05 00:38:2597
[email protected]e1cec06c2008-12-18 01:22:2398 EXPECT_EQ(std::string("com.google.myextension1"),
[email protected]6014d672008-12-05 00:38:2599 frontend->extensions()->at(0)->id());
[email protected]e1cec06c2008-12-18 01:22:23100 EXPECT_EQ(std::string("My extension 1"),
[email protected]6014d672008-12-05 00:38:25101 frontend->extensions()->at(0)->name());
[email protected]e1cec06c2008-12-18 01:22:23102 EXPECT_EQ(std::string("The first extension that I made."),
[email protected]6014d672008-12-05 00:38:25103 frontend->extensions()->at(0)->description());
[email protected]eab9b452009-01-23 20:48:59104
105 Extension* extension = frontend->extensions()->at(0);
106 const UserScriptList& scripts = extension->user_scripts();
107 ASSERT_EQ(2u, scripts.size());
108 EXPECT_EQ(2u, scripts[0].matches.size());
109 EXPECT_EQ("http://*.google.com/*", scripts[0].matches[0]);
110 EXPECT_EQ("https://*.google.com/*", scripts[0].matches[1]);
111 EXPECT_EQ(extension->path().Append(FILE_PATH_LITERAL("script1.js")).value(),
112 scripts[0].path.value());
113 EXPECT_EQ(1u, scripts[1].matches.size());
114 EXPECT_EQ("http://*.yahoo.com/*", scripts[1].matches[0]);
115 EXPECT_EQ(extension->path().Append(FILE_PATH_LITERAL("script2.js")).value(),
116 scripts[1].path.value());
[email protected]6014d672008-12-05 00:38:25117
[email protected]e1cec06c2008-12-18 01:22:23118 EXPECT_EQ(std::string("com.google.myextension2"),
[email protected]6014d672008-12-05 00:38:25119 frontend->extensions()->at(1)->id());
[email protected]e1cec06c2008-12-18 01:22:23120 EXPECT_EQ(std::string("My extension 2"),
[email protected]6014d672008-12-05 00:38:25121 frontend->extensions()->at(1)->name());
[email protected]e1cec06c2008-12-18 01:22:23122 EXPECT_EQ(std::string(""),
[email protected]6014d672008-12-05 00:38:25123 frontend->extensions()->at(1)->description());
[email protected]eab9b452009-01-23 20:48:59124 ASSERT_EQ(0u, frontend->extensions()->at(1)->user_scripts().size());
[email protected]6014d672008-12-05 00:38:25125};