1// Copyright (c) 2011 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 <algorithm>
6
7#include "base/file_util.h"
8#include "base/memory/scoped_temp_dir.h"
9#include "base/path_service.h"
10#include "chrome/common/chrome_paths.h"
11#include "chrome/common/extensions/extension.h"
12#include "chrome/common/extensions/extension_l10n_util.h"
13#include "chrome/common/extensions/extension_resource.h"
14#include "testing/gtest/include/gtest/gtest.h"
15#include "ui/base/l10n/l10n_util.h"
16
17TEST(ExtensionResourceTest, CreateEmptyResource) {
18  ExtensionResource resource;
19
20  EXPECT_TRUE(resource.extension_root().empty());
21  EXPECT_TRUE(resource.relative_path().empty());
22  EXPECT_TRUE(resource.GetFilePath().empty());
23}
24
25const FilePath::StringType ToLower(const FilePath::StringType& in_str) {
26  FilePath::StringType str(in_str);
27  std::transform(str.begin(), str.end(), str.begin(), tolower);
28  return str;
29}
30
31TEST(ExtensionResourceTest, CreateWithMissingResourceOnDisk) {
32  FilePath root_path;
33  ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &root_path));
34  FilePath relative_path;
35  relative_path = relative_path.AppendASCII("cira.js");
36  std::string extension_id;
37  Extension::GenerateId("test", &extension_id);
38  ExtensionResource resource(extension_id, root_path, relative_path);
39
40  // The path doesn't exist on disk, we will be returned an empty path.
41  EXPECT_EQ(root_path.value(), resource.extension_root().value());
42  EXPECT_EQ(relative_path.value(), resource.relative_path().value());
43  EXPECT_TRUE(resource.GetFilePath().empty());
44}
45
46TEST(ExtensionResourceTest, CreateWithAllResourcesOnDisk) {
47  ScopedTempDir temp;
48  ASSERT_TRUE(temp.CreateUniqueTempDir());
49
50  // Create resource in the extension root.
51  const char* filename = "res.ico";
52  FilePath root_resource = temp.path().AppendASCII(filename);
53  std::string data = "some foo";
54  ASSERT_TRUE(file_util::WriteFile(root_resource, data.c_str(), data.length()));
55
56  // Create l10n resources (for current locale and its parents).
57  FilePath l10n_path = temp.path().Append(Extension::kLocaleFolder);
58  ASSERT_TRUE(file_util::CreateDirectory(l10n_path));
59
60  std::vector<std::string> locales;
61  l10n_util::GetParentLocales(l10n_util::GetApplicationLocale(""), &locales);
62  ASSERT_FALSE(locales.empty());
63  for (size_t i = 0; i < locales.size(); i++) {
64    FilePath make_path;
65    make_path = l10n_path.AppendASCII(locales[i]);
66    ASSERT_TRUE(file_util::CreateDirectory(make_path));
67    ASSERT_TRUE(file_util::WriteFile(make_path.AppendASCII(filename),
68        data.c_str(), data.length()));
69  }
70
71  FilePath path;
72  std::string extension_id;
73  Extension::GenerateId("test", &extension_id);
74  ExtensionResource resource(extension_id, temp.path(),
75                             FilePath().AppendASCII(filename));
76  FilePath resolved_path = resource.GetFilePath();
77
78  FilePath expected_path;
79  // Expect default path only, since fallback logic is disabled.
80  // See http://crbug.com/27359.
81  expected_path = root_resource;
82  ASSERT_TRUE(file_util::AbsolutePath(&expected_path));
83
84  EXPECT_EQ(ToLower(expected_path.value()), ToLower(resolved_path.value()));
85  EXPECT_EQ(ToLower(temp.path().value()),
86            ToLower(resource.extension_root().value()));
87  EXPECT_EQ(ToLower(FilePath().AppendASCII(filename).value()),
88            ToLower(resource.relative_path().value()));
89}
90