1 // Copyright 2013 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.
5 #include "chrome/common/importer/firefox_importer_utils.h"
7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/values.h"
10 #include "grit/generated_resources.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/base/l10n/l10n_util.h"
16 struct GetPrefsJsValueCase
{
17 std::string prefs_content
;
18 std::string pref_name
;
19 std::string pref_value
;
20 } GetPrefsJsValueCases
[] = {
21 // Basic case. Single pref, unquoted value.
22 { "user_pref(\"foo.bar\", 1);", "foo.bar", "1" },
23 // Value is quoted. Quotes should be stripped.
24 { "user_pref(\"foo.bar\", \"1\");", "foo.bar", "1" },
26 { "user_pref(\"foo.bar\", \"Value (detail)\");",
27 "foo.bar", "Value (detail)" },
29 { "user_pref(\"foo.bar\", 1);\n"
30 "user_pref(\"foo.baz\", 2);\n"
31 "user_pref(\"foo.bag\", 3);",
34 { "user_pref(\"foo.bar\", 1);\n"
35 "user_pref(\"foo.baz\", 2;\n"
36 "user_pref(\"foo.bag\", 3);",
39 { "uesr_pref(\"foo.bar\", 1);", "foo.bar", "" },
42 struct GetFirefoxImporterNameCase
{
43 std::string app_ini_content
;
45 } GetFirefoxImporterNameCases
[] = {
51 "BuildID=20120717115048\n"
52 "ID={ec8030f7-c20a-464f-9b0e-13a3a9e97384}",
53 IDS_IMPORT_FROM_ICEWEASEL
},
57 " Name=Firefox\t \r\n"
59 IDS_IMPORT_FROM_FIREFOX
},
64 "BuildID=20120717115048\n"
65 "ID={ec8030f7-c20a-464f-9b0e-13a3a9e97384}",
66 IDS_IMPORT_FROM_FIREFOX
},
71 IDS_IMPORT_FROM_FIREFOX
},
72 // Multiple Name settings in different sections
77 "Profile=mozilla/firefox\n"
81 "ID={ec8030f7-c20a-464f-9b0e-13a3a9e97384}",
82 IDS_IMPORT_FROM_ICEWEASEL
},
88 IDS_IMPORT_FROM_ICEWEASEL
},
90 { "", IDS_IMPORT_FROM_FIREFOX
}
93 } // anonymous namespace
95 TEST(FirefoxImporterUtilsTest
, GetPrefsJsValue
) {
96 for (size_t i
= 0; i
< arraysize(GetPrefsJsValueCases
); ++i
) {
98 GetPrefsJsValueCases
[i
].pref_value
,
99 GetPrefsJsValue(GetPrefsJsValueCases
[i
].prefs_content
,
100 GetPrefsJsValueCases
[i
].pref_name
));
104 TEST(FirefoxImporterUtilsTest
, GetFirefoxImporterName
) {
105 base::ScopedTempDir temp_dir
;
106 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
107 const base::FilePath
app_ini_file(
108 temp_dir
.path().AppendASCII("application.ini"));
109 for (size_t i
= 0; i
< arraysize(GetFirefoxImporterNameCases
); ++i
) {
110 base::WriteFile(app_ini_file
,
111 GetFirefoxImporterNameCases
[i
].app_ini_content
.c_str(),
112 GetFirefoxImporterNameCases
[i
].app_ini_content
.size());
113 EXPECT_EQ(GetFirefoxImporterName(temp_dir
.path()),
114 l10n_util::GetStringUTF16(GetFirefoxImporterNameCases
[i
].resource_id
));
116 EXPECT_EQ(l10n_util::GetStringUTF16(
117 IDS_IMPORT_FROM_FIREFOX
),
118 GetFirefoxImporterName(base::FilePath(
119 FILE_PATH_LITERAL("/invalid/path"))));
122 TEST(FirefoxImporterUtilsTest
, GetFirefoxProfilePath
) {
123 base::DictionaryValue no_profiles
;
125 GetFirefoxProfilePathFromDictionary(no_profiles
).MaybeAsASCII());
127 base::DictionaryValue single_profile
;
128 single_profile
.SetString("Profile0.Path", "first");
129 single_profile
.SetString("Profile0.IsRelative", "0");
130 single_profile
.SetString("Profile0.Default", "1");
132 GetFirefoxProfilePathFromDictionary(single_profile
).MaybeAsASCII());
134 base::DictionaryValue no_default
;
135 no_default
.SetString("Profile0.Path", "first");
136 no_default
.SetString("Profile0.IsRelative", "0");
137 no_default
.SetString("Profile1.Path", "second");
138 no_default
.SetString("Profile1.IsRelative", "0");
140 GetFirefoxProfilePathFromDictionary(no_default
).MaybeAsASCII());
142 base::DictionaryValue default_first
;
143 default_first
.SetString("Profile0.Path", "first");
144 default_first
.SetString("Profile0.IsRelative", "0");
145 default_first
.SetString("Profile0.Default", "1");
146 default_first
.SetString("Profile1.Path", "second");
147 default_first
.SetString("Profile1.IsRelative", "0");
149 GetFirefoxProfilePathFromDictionary(default_first
).MaybeAsASCII());
151 base::DictionaryValue default_second
;
152 default_second
.SetString("Profile0.Path", "first");
153 default_second
.SetString("Profile0.IsRelative", "0");
154 default_second
.SetString("Profile1.Path", "second");
155 default_second
.SetString("Profile1.IsRelative", "0");
156 default_second
.SetString("Profile1.Default", "1");
158 GetFirefoxProfilePathFromDictionary(default_second
).MaybeAsASCII());