summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extensions_service_unittest.cc
blob: da86f275ac49a2824e70bd90888c3c971723e64a (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
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) 2006-2008 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 <vector>

#include "base/file_path.h"
#include "base/file_util.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/string_util.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/json_value_serializer.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"


// A mock implementation of ExtensionsServiceFrontendInterface for testing the
// backend.
class ExtensionsServiceTestFrontend
    : public ExtensionsServiceFrontendInterface {
 public:
  std::vector<std::wstring>* errors() {
    return &errors_;
  }

  ExtensionList* extensions() {
    return extensions_.get();
  }

  // ExtensionsServiceFrontendInterface
  virtual MessageLoop* GetMessageLoop() {
    return &message_loop_;
  }

  virtual void OnExtensionLoadError(const std::wstring& message) {
    errors_.push_back(message);
  }

  virtual void OnExtensionsLoadedFromDirectory(ExtensionList* extensions) {
    extensions_.reset(extensions);
  }

 private:
  MessageLoop message_loop_;
  scoped_ptr<ExtensionList> extensions_;
  std::vector<std::wstring> errors_;
};

// make the test a PlatformTest to setup autorelease pools properly on mac
typedef PlatformTest ExtensionsServiceTest;

// Test loading extensions from the profile directory.
TEST_F(ExtensionsServiceTest, LoadAllExtensionsFromDirectory) {
  std::wstring extensions_dir;
  ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_dir));
  FilePath manifest_path = FilePath::FromWStringHack(extensions_dir).Append(
      FILE_PATH_LITERAL("extensions"));

  scoped_refptr<ExtensionsServiceBackend> backend(new ExtensionsServiceBackend);
  scoped_refptr<ExtensionsServiceTestFrontend> frontend(
      new ExtensionsServiceTestFrontend);

  std::vector<Extension*> extensions;
  EXPECT_TRUE(backend->LoadExtensionsFromDirectory(manifest_path,
      scoped_refptr<ExtensionsServiceFrontendInterface>(frontend.get())));
  frontend->GetMessageLoop()->RunAllPending();

  // Note: There can be more errors if there are extra directories, like .svn
  // directories.
  EXPECT_TRUE(frontend->errors()->size() >= 2u);
  EXPECT_EQ(Extension::kInvalidManifestError, frontend->errors()->at(0));
  EXPECT_EQ(Extension::kInvalidManifestError, frontend->errors()->at(1));
  EXPECT_EQ(2u, frontend->extensions()->size());

  EXPECT_EQ(std::wstring(L"com.google.myextension1"),
            frontend->extensions()->at(0)->id());
  EXPECT_EQ(std::wstring(L"My extension 1"),
            frontend->extensions()->at(0)->name());
  EXPECT_EQ(std::wstring(L"The first extension that I made."),
            frontend->extensions()->at(0)->description());
  EXPECT_EQ(2u, frontend->extensions()->at(0)->content_scripts().size());
  EXPECT_EQ(std::wstring(L"script1.user.js"),
            frontend->extensions()->at(0)->content_scripts().at(0));
  EXPECT_EQ(std::wstring(L"script2.user.js"),
            frontend->extensions()->at(0)->content_scripts().at(1));

  EXPECT_EQ(std::wstring(L"com.google.myextension2"),
            frontend->extensions()->at(1)->id());
  EXPECT_EQ(std::wstring(L"My extension 2"),
            frontend->extensions()->at(1)->name());
  EXPECT_EQ(std::wstring(L""),
            frontend->extensions()->at(1)->description());
  EXPECT_EQ(0u, frontend->extensions()->at(1)->content_scripts().size());
};