summaryrefslogtreecommitdiffstats
path: root/chrome/common/importer/firefox_importer_utils_unittest.cc
blob: 987b0e955faffb5066ac290f318c3ae7d8db3495 (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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2013 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/common/importer/firefox_importer_utils.h"

#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/values.h"
#include "grit/generated_resources.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"

namespace {

struct GetPrefsJsValueCase {
  std::string prefs_content;
  std::string pref_name;
  std::string pref_value;
} GetPrefsJsValueCases[] = {
  // Basic case. Single pref, unquoted value.
  { "user_pref(\"foo.bar\", 1);", "foo.bar", "1" },
  // Value is quoted. Quotes should be stripped.
  { "user_pref(\"foo.bar\", \"1\");", "foo.bar", "1" },
  // Value has parens.
  { "user_pref(\"foo.bar\", \"Value (detail)\");",
    "foo.bar", "Value (detail)" },
  // Multi-line case.
  { "user_pref(\"foo.bar\", 1);\n"
    "user_pref(\"foo.baz\", 2);\n"
    "user_pref(\"foo.bag\", 3);",
    "foo.baz", "2" },
  // Malformed content.
  { "user_pref(\"foo.bar\", 1);\n"
    "user_pref(\"foo.baz\", 2;\n"
    "user_pref(\"foo.bag\", 3);",
    "foo.baz", "" },
  // Malformed content.
  { "uesr_pref(\"foo.bar\", 1);", "foo.bar", "" },
};

struct GetFirefoxImporterNameCase {
  std::string app_ini_content;
  int resource_id;
} GetFirefoxImporterNameCases[] = {
  // Basic case
  { "[App]\n"
    "Vendor=Mozilla\n"
    "Name=iceweasel\n"
    "Version=10.0.6\n"
    "BuildID=20120717115048\n"
    "ID={ec8030f7-c20a-464f-9b0e-13a3a9e97384}",
    IDS_IMPORT_FROM_ICEWEASEL },
  // Whitespace
  { " \t[App] \n"
    "Vendor=Mozilla\n"
    "   Name=Firefox\t \r\n"
    "Version=10.0.6\n",
    IDS_IMPORT_FROM_FIREFOX },
  // No Name setting
  { "[App]\n"
    "Vendor=Mozilla\n"
    "Version=10.0.6\n"
    "BuildID=20120717115048\n"
    "ID={ec8030f7-c20a-464f-9b0e-13a3a9e97384}",
    IDS_IMPORT_FROM_FIREFOX },
  // No [App] section
  { "[Foo]\n"
    "Vendor=Mozilla\n"
    "Name=Foo\n",
    IDS_IMPORT_FROM_FIREFOX },
  // Multiple Name settings in different sections
  { "[Foo]\n"
    "Vendor=Mozilla\n"
    "Name=Firefox\n"
    "[App]\n"
    "Profile=mozilla/firefox\n"
    "Name=iceweasel\n"
    "[Bar]\n"
    "Name=Bar\n"
    "ID={ec8030f7-c20a-464f-9b0e-13a3a9e97384}",
    IDS_IMPORT_FROM_ICEWEASEL },
  // Case-insensitivity
  { "[App]\n"
    "Vendor=Mozilla\n"
    "Name=IceWeasel\n"
    "Version=10.0.6\n",
    IDS_IMPORT_FROM_ICEWEASEL },
  // Empty file
  { "", IDS_IMPORT_FROM_FIREFOX }
};

}  // anonymous namespace

TEST(FirefoxImporterUtilsTest, GetPrefsJsValue) {
  for (size_t i = 0; i < arraysize(GetPrefsJsValueCases); ++i) {
    EXPECT_EQ(
      GetPrefsJsValueCases[i].pref_value,
      GetPrefsJsValue(GetPrefsJsValueCases[i].prefs_content,
                      GetPrefsJsValueCases[i].pref_name));
  }
}

TEST(FirefoxImporterUtilsTest, GetFirefoxImporterName) {
  base::ScopedTempDir temp_dir;
  ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
  const base::FilePath app_ini_file(
      temp_dir.path().AppendASCII("application.ini"));
  for (size_t i = 0; i < arraysize(GetFirefoxImporterNameCases); ++i) {
    base::WriteFile(app_ini_file,
                    GetFirefoxImporterNameCases[i].app_ini_content.c_str(),
                    GetFirefoxImporterNameCases[i].app_ini_content.size());
    EXPECT_EQ(GetFirefoxImporterName(temp_dir.path()),
        l10n_util::GetStringUTF16(GetFirefoxImporterNameCases[i].resource_id));
  }
  EXPECT_EQ(l10n_util::GetStringUTF16(
          IDS_IMPORT_FROM_FIREFOX),
      GetFirefoxImporterName(base::FilePath(
                                        FILE_PATH_LITERAL("/invalid/path"))));
}

TEST(FirefoxImporterUtilsTest, GetFirefoxProfilePath) {
  base::DictionaryValue no_profiles;
  EXPECT_EQ("",
            GetFirefoxProfilePathFromDictionary(no_profiles).MaybeAsASCII());

  base::DictionaryValue single_profile;
  single_profile.SetString("Profile0.Path", "first");
  single_profile.SetString("Profile0.IsRelative", "0");
  single_profile.SetString("Profile0.Default", "1");
  EXPECT_EQ("first",
            GetFirefoxProfilePathFromDictionary(single_profile).MaybeAsASCII());

  base::DictionaryValue no_default;
  no_default.SetString("Profile0.Path", "first");
  no_default.SetString("Profile0.IsRelative", "0");
  no_default.SetString("Profile1.Path", "second");
  no_default.SetString("Profile1.IsRelative", "0");
  EXPECT_EQ("first",
            GetFirefoxProfilePathFromDictionary(no_default).MaybeAsASCII());

  base::DictionaryValue default_first;
  default_first.SetString("Profile0.Path", "first");
  default_first.SetString("Profile0.IsRelative", "0");
  default_first.SetString("Profile0.Default", "1");
  default_first.SetString("Profile1.Path", "second");
  default_first.SetString("Profile1.IsRelative", "0");
  EXPECT_EQ("first",
            GetFirefoxProfilePathFromDictionary(default_first).MaybeAsASCII());

  base::DictionaryValue default_second;
  default_second.SetString("Profile0.Path", "first");
  default_second.SetString("Profile0.IsRelative", "0");
  default_second.SetString("Profile1.Path", "second");
  default_second.SetString("Profile1.IsRelative", "0");
  default_second.SetString("Profile1.Default", "1");
  EXPECT_EQ("second",
            GetFirefoxProfilePathFromDictionary(default_second).MaybeAsASCII());
}