[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 1 | // 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] | f0397fa | 2008-12-11 17:59:58 | [diff] [blame] | 5 | #include <algorithm> |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 6 | #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] | f0397fa | 2008-12-11 17:59:58 | [diff] [blame] | 13 | #include "chrome/browser/extensions/extension.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 14 | #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] | f66c110c | 2008-12-05 20:26:29 | [diff] [blame] | 18 | #include "testing/platform_test.h" |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 19 | |
[email protected] | f0397fa | 2008-12-11 17:59:58 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | struct ExtensionsOrder { |
| 23 | bool operator()(const Extension* a, const Extension* b) { |
| 24 | return a->name() < b->name(); |
| 25 | } |
| 26 | }; |
| 27 | |
| 28 | } // namespace |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 29 | |
| 30 | // A mock implementation of ExtensionsServiceFrontendInterface for testing the |
| 31 | // backend. |
| 32 | class ExtensionsServiceTestFrontend |
| 33 | : public ExtensionsServiceFrontendInterface { |
| 34 | public: |
[email protected] | 3acbd42 | 2008-12-08 18:25:00 | [diff] [blame] | 35 | ~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] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 43 | return &errors_; |
| 44 | } |
| 45 | |
| 46 | ExtensionList* extensions() { |
[email protected] | 3acbd42 | 2008-12-08 18:25:00 | [diff] [blame] | 47 | return &extensions_; |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // ExtensionsServiceFrontendInterface |
| 51 | virtual MessageLoop* GetMessageLoop() { |
| 52 | return &message_loop_; |
| 53 | } |
| 54 | |
[email protected] | 3acbd42 | 2008-12-08 18:25:00 | [diff] [blame] | 55 | virtual void OnExtensionLoadError(const std::string& message) { |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 56 | errors_.push_back(message); |
| 57 | } |
| 58 | |
[email protected] | 08816d0d | 2008-12-08 18:43:53 | [diff] [blame] | 59 | virtual void OnExtensionsLoadedFromDirectory(ExtensionList* new_extensions) { |
| 60 | extensions_.insert(extensions_.end(), new_extensions->begin(), |
| 61 | new_extensions->end()); |
| 62 | delete new_extensions; |
[email protected] | f0397fa | 2008-12-11 17:59:58 | [diff] [blame] | 63 | // 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] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | private: |
| 69 | MessageLoop message_loop_; |
[email protected] | 3acbd42 | 2008-12-08 18:25:00 | [diff] [blame] | 70 | ExtensionList extensions_; |
| 71 | std::vector<std::string> errors_; |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 72 | }; |
| 73 | |
[email protected] | f66c110c | 2008-12-05 20:26:29 | [diff] [blame] | 74 | // make the test a PlatformTest to setup autorelease pools properly on mac |
| 75 | typedef PlatformTest ExtensionsServiceTest; |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 76 | |
| 77 | // Test loading extensions from the profile directory. |
| 78 | TEST_F(ExtensionsServiceTest, LoadAllExtensionsFromDirectory) { |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 79 | std::wstring extensions_dir; |
| 80 | ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_dir)); |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame^] | 81 | FilePath extensions_path = FilePath::FromWStringHack(extensions_dir).Append( |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 82 | 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] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame^] | 89 | EXPECT_TRUE(backend->LoadExtensionsFromDirectory(extensions_path, |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 90 | 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] | f0397fa | 2008-12-11 17:59:58 | [diff] [blame] | 96 | ASSERT_EQ(2u, frontend->extensions()->size()); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 97 | |
[email protected] | e1cec06c | 2008-12-18 01:22:23 | [diff] [blame] | 98 | EXPECT_EQ(std::string("com.google.myextension1"), |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 99 | frontend->extensions()->at(0)->id()); |
[email protected] | e1cec06c | 2008-12-18 01:22:23 | [diff] [blame] | 100 | EXPECT_EQ(std::string("My extension 1"), |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 101 | frontend->extensions()->at(0)->name()); |
[email protected] | e1cec06c | 2008-12-18 01:22:23 | [diff] [blame] | 102 | EXPECT_EQ(std::string("The first extension that I made."), |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 103 | frontend->extensions()->at(0)->description()); |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame^] | 104 | |
| 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] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 117 | |
[email protected] | e1cec06c | 2008-12-18 01:22:23 | [diff] [blame] | 118 | EXPECT_EQ(std::string("com.google.myextension2"), |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 119 | frontend->extensions()->at(1)->id()); |
[email protected] | e1cec06c | 2008-12-18 01:22:23 | [diff] [blame] | 120 | EXPECT_EQ(std::string("My extension 2"), |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 121 | frontend->extensions()->at(1)->name()); |
[email protected] | e1cec06c | 2008-12-18 01:22:23 | [diff] [blame] | 122 | EXPECT_EQ(std::string(""), |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 123 | frontend->extensions()->at(1)->description()); |
[email protected] | eab9b45 | 2009-01-23 20:48:59 | [diff] [blame^] | 124 | ASSERT_EQ(0u, frontend->extensions()->at(1)->user_scripts().size()); |
[email protected] | 6014d67 | 2008-12-05 00:38:25 | [diff] [blame] | 125 | }; |