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/common/extensions/extension_file_util.h"
6
7#include <set>
8#include <string>
9
10#include "base/path_service.h"
11#include "chrome/common/chrome_paths.h"
12#include "extensions/common/extension.h"
13#include "extensions/common/file_util.h"
14#include "extensions/common/manifest.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17namespace extensions {
18
19typedef testing::Test ExtensionFileUtilTest;
20
21// Test that a browser action extension returns a path to an icon.
22TEST_F(ExtensionFileUtilTest, GetBrowserImagePaths) {
23  base::FilePath install_dir;
24  ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir));
25  install_dir = install_dir.AppendASCII("extensions")
26                    .AppendASCII("api_test")
27                    .AppendASCII("browser_action")
28                    .AppendASCII("basics");
29
30  std::string error;
31  scoped_refptr<Extension> extension(file_util::LoadExtension(
32      install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error));
33  ASSERT_TRUE(extension.get());
34
35  // The extension contains one icon.
36  std::set<base::FilePath> paths =
37      extension_file_util::GetBrowserImagePaths(extension.get());
38  ASSERT_EQ(1u, paths.size());
39  EXPECT_EQ("icon.png", paths.begin()->BaseName().AsUTF8Unsafe());
40}
41
42// Test that extensions with zero-length action icons will not load.
43TEST_F(ExtensionFileUtilTest, CheckZeroLengthActionIconFiles) {
44  base::FilePath install_dir;
45  ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir));
46
47  // Try to install an extension with a zero-length browser action icon file.
48  base::FilePath ext_dir = install_dir.AppendASCII("extensions")
49                               .AppendASCII("bad")
50                               .AppendASCII("Extensions")
51                               .AppendASCII("gggggggggggggggggggggggggggggggg");
52
53  std::string error;
54  scoped_refptr<Extension> extension2(file_util::LoadExtension(
55      ext_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error));
56  EXPECT_FALSE(extension2.get());
57  EXPECT_STREQ("Could not load icon 'icon.png' for browser action.",
58               error.c_str());
59
60  // Try to install an extension with a zero-length page action icon file.
61  ext_dir = install_dir.AppendASCII("extensions")
62                .AppendASCII("bad")
63                .AppendASCII("Extensions")
64                .AppendASCII("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
65
66  scoped_refptr<Extension> extension3(file_util::LoadExtension(
67      ext_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error));
68  EXPECT_FALSE(extension3.get());
69  EXPECT_STREQ("Could not load icon 'icon.png' for page action.",
70               error.c_str());
71}
72
73}  // namespace extensions
74