Home | History | Annotate | Download | only in importer
      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.
      4 
      5 #include "chrome/common/importer/firefox_importer_utils.h"
      6 
      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"
     13 
     14 namespace {
     15 
     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" },
     25   // Value has parens.
     26   { "user_pref(\"foo.bar\", \"Value (detail)\");",
     27     "foo.bar", "Value (detail)" },
     28   // Multi-line case.
     29   { "user_pref(\"foo.bar\", 1);\n"
     30     "user_pref(\"foo.baz\", 2);\n"
     31     "user_pref(\"foo.bag\", 3);",
     32     "foo.baz", "2" },
     33   // Malformed content.
     34   { "user_pref(\"foo.bar\", 1);\n"
     35     "user_pref(\"foo.baz\", 2;\n"
     36     "user_pref(\"foo.bag\", 3);",
     37     "foo.baz", "" },
     38   // Malformed content.
     39   { "uesr_pref(\"foo.bar\", 1);", "foo.bar", "" },
     40 };
     41 
     42 struct GetFirefoxImporterNameCase {
     43   std::string app_ini_content;
     44   int resource_id;
     45 } GetFirefoxImporterNameCases[] = {
     46   // Basic case
     47   { "[App]\n"
     48     "Vendor=Mozilla\n"
     49     "Name=iceweasel\n"
     50     "Version=10.0.6\n"
     51     "BuildID=20120717115048\n"
     52     "ID={ec8030f7-c20a-464f-9b0e-13a3a9e97384}",
     53     IDS_IMPORT_FROM_ICEWEASEL },
     54   // Whitespace
     55   { " \t[App] \n"
     56     "Vendor=Mozilla\n"
     57     "   Name=Firefox\t \r\n"
     58     "Version=10.0.6\n",
     59     IDS_IMPORT_FROM_FIREFOX },
     60   // No Name setting
     61   { "[App]\n"
     62     "Vendor=Mozilla\n"
     63     "Version=10.0.6\n"
     64     "BuildID=20120717115048\n"
     65     "ID={ec8030f7-c20a-464f-9b0e-13a3a9e97384}",
     66     IDS_IMPORT_FROM_FIREFOX },
     67   // No [App] section
     68   { "[Foo]\n"
     69     "Vendor=Mozilla\n"
     70     "Name=Foo\n",
     71     IDS_IMPORT_FROM_FIREFOX },
     72   // Multiple Name settings in different sections
     73   { "[Foo]\n"
     74     "Vendor=Mozilla\n"
     75     "Name=Firefox\n"
     76     "[App]\n"
     77     "Profile=mozilla/firefox\n"
     78     "Name=iceweasel\n"
     79     "[Bar]\n"
     80     "Name=Bar\n"
     81     "ID={ec8030f7-c20a-464f-9b0e-13a3a9e97384}",
     82     IDS_IMPORT_FROM_ICEWEASEL },
     83   // Case-insensitivity
     84   { "[App]\n"
     85     "Vendor=Mozilla\n"
     86     "Name=IceWeasel\n"
     87     "Version=10.0.6\n",
     88     IDS_IMPORT_FROM_ICEWEASEL },
     89   // Empty file
     90   { "", IDS_IMPORT_FROM_FIREFOX }
     91 };
     92 
     93 }  // anonymous namespace
     94 
     95 TEST(FirefoxImporterUtilsTest, GetPrefsJsValue) {
     96   for (size_t i = 0; i < arraysize(GetPrefsJsValueCases); ++i) {
     97     EXPECT_EQ(
     98       GetPrefsJsValueCases[i].pref_value,
     99       GetPrefsJsValue(GetPrefsJsValueCases[i].prefs_content,
    100                       GetPrefsJsValueCases[i].pref_name));
    101   }
    102 }
    103 
    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));
    115   }
    116   EXPECT_EQ(l10n_util::GetStringUTF16(
    117           IDS_IMPORT_FROM_FIREFOX),
    118       GetFirefoxImporterName(base::FilePath(
    119                                         FILE_PATH_LITERAL("/invalid/path"))));
    120 }
    121 
    122 TEST(FirefoxImporterUtilsTest, GetFirefoxProfilePath) {
    123   base::DictionaryValue no_profiles;
    124   EXPECT_EQ("",
    125             GetFirefoxProfilePathFromDictionary(no_profiles).MaybeAsASCII());
    126 
    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");
    131   EXPECT_EQ("first",
    132             GetFirefoxProfilePathFromDictionary(single_profile).MaybeAsASCII());
    133 
    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");
    139   EXPECT_EQ("first",
    140             GetFirefoxProfilePathFromDictionary(no_default).MaybeAsASCII());
    141 
    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");
    148   EXPECT_EQ("first",
    149             GetFirefoxProfilePathFromDictionary(default_first).MaybeAsASCII());
    150 
    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");
    157   EXPECT_EQ("second",
    158             GetFirefoxProfilePathFromDictionary(default_second).MaybeAsASCII());
    159 }
    160