Home | History | Annotate | Download | only in chromedriver
      1 // Copyright (c) 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 "base/base64.h"
      6 #include "base/base_paths.h"
      7 #include "base/command_line.h"
      8 #include "base/file_util.h"
      9 #include "base/files/file_path.h"
     10 #include "base/files/scoped_temp_dir.h"
     11 #include "base/json/json_reader.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/path_service.h"
     14 #include "base/strings/string_split.h"
     15 #include "base/values.h"
     16 #include "chrome/test/chromedriver/chrome/status.h"
     17 #include "chrome/test/chromedriver/chrome_launcher.h"
     18 #include "testing/gtest/include/gtest/gtest.h"
     19 
     20 TEST(ProcessExtensions, NoExtension) {
     21   CommandLine command(CommandLine::NO_PROGRAM);
     22   std::vector<std::string> extensions;
     23   base::FilePath extension_dir;
     24   Status status = internal::ProcessExtensions(extensions, extension_dir,
     25                                               false, &command);
     26   ASSERT_TRUE(status.IsOk());
     27   ASSERT_FALSE(command.HasSwitch("load-extension"));
     28 }
     29 
     30 TEST(ProcessExtensions, SingleExtension) {
     31   base::FilePath source_root;
     32   PathService::Get(base::DIR_SOURCE_ROOT, &source_root);
     33   base::FilePath crx_file_path = source_root.AppendASCII(
     34       "chrome/test/data/chromedriver/ext_test_1.crx");
     35   std::string crx_contents;
     36   ASSERT_TRUE(file_util::ReadFileToString(crx_file_path, &crx_contents));
     37 
     38   std::vector<std::string> extensions;
     39   std::string crx_encoded;
     40   ASSERT_TRUE(base::Base64Encode(crx_contents, &crx_encoded));
     41   extensions.push_back(crx_encoded);
     42 
     43   base::ScopedTempDir extension_dir;
     44   ASSERT_TRUE(extension_dir.CreateUniqueTempDir());
     45 
     46   CommandLine command(CommandLine::NO_PROGRAM);
     47   Status status = internal::ProcessExtensions(extensions, extension_dir.path(),
     48                                               false, &command);
     49   ASSERT_TRUE(status.IsOk());
     50   ASSERT_TRUE(command.HasSwitch("load-extension"));
     51   base::FilePath temp_ext_path = command.GetSwitchValuePath("load-extension");
     52   ASSERT_TRUE(base::PathExists(temp_ext_path));
     53 }
     54 
     55 TEST(ProcessExtensions, MultipleExtensions) {
     56   base::FilePath source_root;
     57   PathService::Get(base::DIR_SOURCE_ROOT, &source_root);
     58   base::FilePath test_ext_path = source_root.AppendASCII(
     59       "chrome/test/data/chromedriver");
     60   base::FilePath test_crx_1 = test_ext_path.AppendASCII("ext_test_1.crx");
     61   base::FilePath test_crx_2 = test_ext_path.AppendASCII("ext_test_2.crx");
     62 
     63   std::string crx_1_contents, crx_2_contents;
     64   ASSERT_TRUE(file_util::ReadFileToString(test_crx_1, &crx_1_contents));
     65   ASSERT_TRUE(file_util::ReadFileToString(test_crx_2, &crx_2_contents));
     66 
     67   std::vector<std::string> extensions;
     68   std::string crx_1_encoded, crx_2_encoded;
     69   ASSERT_TRUE(base::Base64Encode(crx_1_contents, &crx_1_encoded));
     70   ASSERT_TRUE(base::Base64Encode(crx_2_contents, &crx_2_encoded));
     71   extensions.push_back(crx_1_encoded);
     72   extensions.push_back(crx_2_encoded);
     73 
     74   base::ScopedTempDir extension_dir;
     75   ASSERT_TRUE(extension_dir.CreateUniqueTempDir());
     76 
     77   CommandLine command(CommandLine::NO_PROGRAM);
     78   Status status = internal::ProcessExtensions(extensions, extension_dir.path(),
     79                                               false, &command);
     80   ASSERT_TRUE(status.IsOk());
     81   ASSERT_TRUE(command.HasSwitch("load-extension"));
     82   CommandLine::StringType ext_paths = command.GetSwitchValueNative(
     83       "load-extension");
     84   std::vector<CommandLine::StringType> ext_path_list;
     85   base::SplitString(ext_paths, FILE_PATH_LITERAL(','), &ext_path_list);
     86   ASSERT_EQ(2u, ext_path_list.size());
     87   ASSERT_TRUE(base::PathExists(base::FilePath(ext_path_list[0])));
     88   ASSERT_TRUE(base::PathExists(base::FilePath(ext_path_list[1])));
     89 }
     90 
     91 namespace {
     92 
     93 void AssertEQ(const base::DictionaryValue& dict, const std::string& key,
     94               const char* expected_value) {
     95   std::string value;
     96   ASSERT_TRUE(dict.GetString(key, &value));
     97   ASSERT_STREQ(value.c_str(), expected_value);
     98 }
     99 
    100 }  // namespace
    101 
    102 TEST(PrepareUserDataDir, CustomPrefs) {
    103   base::ScopedTempDir temp_dir;
    104   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
    105 
    106   CommandLine command(CommandLine::NO_PROGRAM);
    107   base::DictionaryValue prefs;
    108   prefs.SetString("myPrefsKey", "ok");
    109   prefs.SetStringWithoutPathExpansion("pref.sub", "1");
    110   base::DictionaryValue local_state;
    111   local_state.SetString("myLocalKey", "ok");
    112   local_state.SetStringWithoutPathExpansion("local.state.sub", "2");
    113   Status status = internal::PrepareUserDataDir(
    114       temp_dir.path(), &prefs, &local_state);
    115   ASSERT_EQ(kOk, status.code());
    116 
    117   base::FilePath prefs_file =
    118       temp_dir.path().AppendASCII("Default").AppendASCII("Preferences");
    119   std::string prefs_str;
    120   ASSERT_TRUE(file_util::ReadFileToString(prefs_file, &prefs_str));
    121   scoped_ptr<base::Value> prefs_value(base::JSONReader::Read(prefs_str));
    122   const base::DictionaryValue* prefs_dict = NULL;
    123   ASSERT_TRUE(prefs_value->GetAsDictionary(&prefs_dict));
    124   AssertEQ(*prefs_dict, "myPrefsKey", "ok");
    125   AssertEQ(*prefs_dict, "pref.sub", "1");
    126 
    127   base::FilePath local_state_file = temp_dir.path().AppendASCII("Local State");
    128   std::string local_state_str;
    129   ASSERT_TRUE(file_util::ReadFileToString(local_state_file, &local_state_str));
    130   scoped_ptr<base::Value> local_state_value(
    131       base::JSONReader::Read(local_state_str));
    132   const base::DictionaryValue* local_state_dict = NULL;
    133   ASSERT_TRUE(local_state_value->GetAsDictionary(&local_state_dict));
    134   AssertEQ(*local_state_dict, "myLocalKey", "ok");
    135   AssertEQ(*local_state_dict, "local.state.sub", "2");
    136 }
    137