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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
// Copyright (c) 2009 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 "chrome/browser/extensions/extension_l10n_util.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/scoped_ptr.h"
#include "base/scoped_temp_dir.h"
#include "base/values.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace keys = extension_manifest_keys;
namespace {
Extension* CreateMinimalExtension(const std::string& default_locale) {
#if defined(OS_WIN)
FilePath path(FILE_PATH_LITERAL("C:\\foo"));
#elif defined(OS_POSIX)
FilePath path(FILE_PATH_LITERAL("/foo"));
#endif
Extension* extension = new Extension(path);
std::string error;
DictionaryValue input_value;
// Test minimal extension
input_value.SetString(keys::kVersion, "1.0.0.0");
input_value.SetString(keys::kName, "my extension");
if (!default_locale.empty()) {
input_value.SetString(keys::kDefaultLocale, default_locale);
}
EXPECT_TRUE(extension->InitFromValue(input_value, false, &error));
return extension;
}
TEST(ExtensionL10nUtil, AddValidLocalesEmptyLocaleFolder) {
ScopedTempDir temp;
ASSERT_TRUE(temp.CreateUniqueTempDir());
FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder);
ASSERT_TRUE(file_util::CreateDirectory(src_path));
scoped_ptr<Extension> extension(CreateMinimalExtension(""));
std::string error;
EXPECT_FALSE(extension_l10n_util::AddValidLocales(src_path,
extension.get(),
&error));
EXPECT_TRUE(extension->supported_locales().empty());
}
TEST(ExtensionL10nUtil, AddValidLocalesWithValidLocaleNoMessagesFile) {
ScopedTempDir temp;
ASSERT_TRUE(temp.CreateUniqueTempDir());
FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder);
ASSERT_TRUE(file_util::CreateDirectory(src_path));
ASSERT_TRUE(file_util::CreateDirectory(src_path.AppendASCII("sr")));
scoped_ptr<Extension> extension(CreateMinimalExtension(""));
std::string error;
EXPECT_FALSE(extension_l10n_util::AddValidLocales(src_path,
extension.get(),
&error));
EXPECT_TRUE(extension->supported_locales().empty());
}
TEST(ExtensionL10nUtil, AddValidLocalesWithValidLocalesAndMessagesFile) {
ScopedTempDir temp;
ASSERT_TRUE(temp.CreateUniqueTempDir());
FilePath src_path = temp.path().AppendASCII(Extension::kLocaleFolder);
ASSERT_TRUE(file_util::CreateDirectory(src_path));
FilePath locale_1 = src_path.AppendASCII("sr");
ASSERT_TRUE(file_util::CreateDirectory(locale_1));
std::string data = "foobar";
ASSERT_TRUE(
file_util::WriteFile(locale_1.AppendASCII(Extension::kMessagesFilename),
data.c_str(), data.length()));
FilePath locale_2 = src_path.AppendASCII("en_US");
ASSERT_TRUE(file_util::CreateDirectory(locale_2));
ASSERT_TRUE(
file_util::WriteFile(locale_2.AppendASCII(Extension::kMessagesFilename),
data.c_str(), data.length()));
scoped_ptr<Extension> extension(CreateMinimalExtension(""));
std::string error;
EXPECT_TRUE(extension_l10n_util::AddValidLocales(src_path,
extension.get(),
&error));
EXPECT_EQ(static_cast<unsigned int>(2),
extension->supported_locales().size());
}
TEST(ExtensionL10nUtil, SetDefaultLocaleGoodDefaultLocaleInManifest) {
scoped_ptr<Extension> extension(CreateMinimalExtension("sr"));
extension->AddSupportedLocale("sr");
extension->AddSupportedLocale("en-US");
EXPECT_TRUE(extension_l10n_util::ValidateDefaultLocale(extension.get()));
EXPECT_EQ("sr", extension->default_locale());
}
TEST(ExtensionL10nUtil, SetDefaultLocaleNoDefaultLocaleInManifest) {
scoped_ptr<Extension> extension(CreateMinimalExtension(""));
extension->AddSupportedLocale("sr");
extension->AddSupportedLocale("en-US");
EXPECT_FALSE(extension_l10n_util::ValidateDefaultLocale(extension.get()));
}
TEST(ExtensionL10nUtil, SetDefaultLocaleWrongDefaultLocaleInManifest) {
scoped_ptr<Extension> extension(CreateMinimalExtension("ko"));
extension->AddSupportedLocale("sr");
extension->AddSupportedLocale("en-US");
EXPECT_FALSE(extension_l10n_util::ValidateDefaultLocale(extension.get()));
}
} // namespace
|