| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/base64.h" | 5 #include "base/base64.h" |
| 6 #include "base/base_paths.h" | 6 #include "base/base_paths.h" |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/test/chromedriver/chrome/status.h" | 16 #include "chrome/test/chromedriver/chrome/status.h" |
| 17 #include "chrome/test/chromedriver/chrome_launcher.h" | 17 #include "chrome/test/chromedriver/chrome_launcher.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 TEST(ProcessExtensions, NoExtension) { | 20 TEST(ProcessExtensions, NoExtension) { |
| 21 CommandLine command(CommandLine::NO_PROGRAM); | 21 Switches switches; |
| 22 std::vector<std::string> extensions; | 22 std::vector<std::string> extensions; |
| 23 base::FilePath extension_dir; | 23 base::FilePath extension_dir; |
| 24 std::vector<std::string> bg_pages; | 24 std::vector<std::string> bg_pages; |
| 25 Status status = internal::ProcessExtensions(extensions, extension_dir, | 25 Status status = internal::ProcessExtensions(extensions, extension_dir, |
| 26 false, &command, &bg_pages); | 26 false, &switches, &bg_pages); |
| 27 ASSERT_TRUE(status.IsOk()); | 27 ASSERT_TRUE(status.IsOk()); |
| 28 ASSERT_FALSE(command.HasSwitch("load-extension")); | 28 ASSERT_FALSE(switches.HasSwitch("load-extension")); |
| 29 ASSERT_EQ(0u, bg_pages.size()); | 29 ASSERT_EQ(0u, bg_pages.size()); |
| 30 } | 30 } |
| 31 | 31 |
| 32 bool AddExtensionForInstall(const std::string& relative_path, | 32 bool AddExtensionForInstall(const std::string& relative_path, |
| 33 std::vector<std::string>* extensions) { | 33 std::vector<std::string>* extensions) { |
| 34 base::FilePath source_root; | 34 base::FilePath source_root; |
| 35 PathService::Get(base::DIR_SOURCE_ROOT, &source_root); | 35 PathService::Get(base::DIR_SOURCE_ROOT, &source_root); |
| 36 base::FilePath crx_file_path = source_root.AppendASCII( | 36 base::FilePath crx_file_path = source_root.AppendASCII( |
| 37 "chrome/test/data/chromedriver/" + relative_path); | 37 "chrome/test/data/chromedriver/" + relative_path); |
| 38 std::string crx_contents; | 38 std::string crx_contents; |
| 39 if (!file_util::ReadFileToString(crx_file_path, &crx_contents)) | 39 if (!file_util::ReadFileToString(crx_file_path, &crx_contents)) |
| 40 return false; | 40 return false; |
| 41 | 41 |
| 42 std::string crx_encoded; | 42 std::string crx_encoded; |
| 43 if (!base::Base64Encode(crx_contents, &crx_encoded)) | 43 if (!base::Base64Encode(crx_contents, &crx_encoded)) |
| 44 return false; | 44 return false; |
| 45 extensions->push_back(crx_encoded); | 45 extensions->push_back(crx_encoded); |
| 46 return true; | 46 return true; |
| 47 } | 47 } |
| 48 | 48 |
| 49 TEST(ProcessExtensions, SingleExtensionWithBgPage) { | 49 TEST(ProcessExtensions, SingleExtensionWithBgPage) { |
| 50 std::vector<std::string> extensions; | 50 std::vector<std::string> extensions; |
| 51 ASSERT_TRUE(AddExtensionForInstall("ext_slow_loader.crx", &extensions)); | 51 ASSERT_TRUE(AddExtensionForInstall("ext_slow_loader.crx", &extensions)); |
| 52 | 52 |
| 53 base::ScopedTempDir extension_dir; | 53 base::ScopedTempDir extension_dir; |
| 54 ASSERT_TRUE(extension_dir.CreateUniqueTempDir()); | 54 ASSERT_TRUE(extension_dir.CreateUniqueTempDir()); |
| 55 | 55 |
| 56 CommandLine command(CommandLine::NO_PROGRAM); | 56 Switches switches; |
| 57 std::vector<std::string> bg_pages; | 57 std::vector<std::string> bg_pages; |
| 58 Status status = internal::ProcessExtensions(extensions, extension_dir.path(), | 58 Status status = internal::ProcessExtensions(extensions, extension_dir.path(), |
| 59 false, &command, &bg_pages); | 59 false, &switches, &bg_pages); |
| 60 ASSERT_TRUE(status.IsOk()); | 60 ASSERT_TRUE(status.IsOk()); |
| 61 ASSERT_TRUE(command.HasSwitch("load-extension")); | 61 ASSERT_TRUE(switches.HasSwitch("load-extension")); |
| 62 base::FilePath temp_ext_path = command.GetSwitchValuePath("load-extension"); | 62 base::FilePath temp_ext_path(switches.GetSwitchValueNative("load-extension")); |
| 63 ASSERT_TRUE(base::PathExists(temp_ext_path)); | 63 ASSERT_TRUE(base::PathExists(temp_ext_path)); |
| 64 std::string manifest_txt; | 64 std::string manifest_txt; |
| 65 ASSERT_TRUE(file_util::ReadFileToString( | 65 ASSERT_TRUE(file_util::ReadFileToString( |
| 66 temp_ext_path.AppendASCII("manifest.json"), &manifest_txt)); | 66 temp_ext_path.AppendASCII("manifest.json"), &manifest_txt)); |
| 67 scoped_ptr<base::Value> manifest(base::JSONReader::Read(manifest_txt)); | 67 scoped_ptr<base::Value> manifest(base::JSONReader::Read(manifest_txt)); |
| 68 ASSERT_TRUE(manifest); | 68 ASSERT_TRUE(manifest); |
| 69 base::DictionaryValue* manifest_dict = NULL; | 69 base::DictionaryValue* manifest_dict = NULL; |
| 70 ASSERT_TRUE(manifest->GetAsDictionary(&manifest_dict)); | 70 ASSERT_TRUE(manifest->GetAsDictionary(&manifest_dict)); |
| 71 std::string key; | 71 std::string key; |
| 72 ASSERT_TRUE(manifest_dict->GetString("key", &key)); | 72 ASSERT_TRUE(manifest_dict->GetString("key", &key)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 84 } | 84 } |
| 85 | 85 |
| 86 TEST(ProcessExtensions, MultipleExtensionsNoBgPages) { | 86 TEST(ProcessExtensions, MultipleExtensionsNoBgPages) { |
| 87 std::vector<std::string> extensions; | 87 std::vector<std::string> extensions; |
| 88 ASSERT_TRUE(AddExtensionForInstall("ext_test_1.crx", &extensions)); | 88 ASSERT_TRUE(AddExtensionForInstall("ext_test_1.crx", &extensions)); |
| 89 ASSERT_TRUE(AddExtensionForInstall("ext_test_2.crx", &extensions)); | 89 ASSERT_TRUE(AddExtensionForInstall("ext_test_2.crx", &extensions)); |
| 90 | 90 |
| 91 base::ScopedTempDir extension_dir; | 91 base::ScopedTempDir extension_dir; |
| 92 ASSERT_TRUE(extension_dir.CreateUniqueTempDir()); | 92 ASSERT_TRUE(extension_dir.CreateUniqueTempDir()); |
| 93 | 93 |
| 94 CommandLine command(CommandLine::NO_PROGRAM); | 94 Switches switches; |
| 95 std::vector<std::string> bg_pages; | 95 std::vector<std::string> bg_pages; |
| 96 Status status = internal::ProcessExtensions(extensions, extension_dir.path(), | 96 Status status = internal::ProcessExtensions(extensions, extension_dir.path(), |
| 97 false, &command, &bg_pages); | 97 false, &switches, &bg_pages); |
| 98 ASSERT_TRUE(status.IsOk()); | 98 ASSERT_TRUE(status.IsOk()); |
| 99 ASSERT_TRUE(command.HasSwitch("load-extension")); | 99 ASSERT_TRUE(switches.HasSwitch("load-extension")); |
| 100 CommandLine::StringType ext_paths = command.GetSwitchValueNative( | 100 CommandLine::StringType ext_paths = switches.GetSwitchValueNative( |
| 101 "load-extension"); | 101 "load-extension"); |
| 102 std::vector<CommandLine::StringType> ext_path_list; | 102 std::vector<CommandLine::StringType> ext_path_list; |
| 103 base::SplitString(ext_paths, FILE_PATH_LITERAL(','), &ext_path_list); | 103 base::SplitString(ext_paths, FILE_PATH_LITERAL(','), &ext_path_list); |
| 104 ASSERT_EQ(2u, ext_path_list.size()); | 104 ASSERT_EQ(2u, ext_path_list.size()); |
| 105 ASSERT_TRUE(base::PathExists(base::FilePath(ext_path_list[0]))); | 105 ASSERT_TRUE(base::PathExists(base::FilePath(ext_path_list[0]))); |
| 106 ASSERT_TRUE(base::PathExists(base::FilePath(ext_path_list[1]))); | 106 ASSERT_TRUE(base::PathExists(base::FilePath(ext_path_list[1]))); |
| 107 ASSERT_EQ(0u, bg_pages.size()); | 107 ASSERT_EQ(0u, bg_pages.size()); |
| 108 } | 108 } |
| 109 | 109 |
| 110 TEST(ProcessExtensions, CommandLineExtensions) { |
| 111 std::vector<std::string> extensions; |
| 112 ASSERT_TRUE(AddExtensionForInstall("ext_test_1.crx", &extensions)); |
| 113 base::ScopedTempDir extension_dir; |
| 114 ASSERT_TRUE(extension_dir.CreateUniqueTempDir()); |
| 115 |
| 116 Switches switches; |
| 117 switches.SetSwitch("load-extension", "/a"); |
| 118 std::vector<std::string> bg_pages; |
| 119 Status status = internal::ProcessExtensions(extensions, extension_dir.path(), |
| 120 false, &switches, &bg_pages); |
| 121 ASSERT_EQ(kOk, status.code()); |
| 122 base::FilePath::StringType load = switches.GetSwitchValueNative( |
| 123 "load-extension"); |
| 124 ASSERT_EQ(FILE_PATH_LITERAL("/a,"), load.substr(0, 3)); |
| 125 ASSERT_TRUE(base::PathExists(base::FilePath(load.substr(3)))); |
| 126 } |
| 127 |
| 110 namespace { | 128 namespace { |
| 111 | 129 |
| 112 void AssertEQ(const base::DictionaryValue& dict, const std::string& key, | 130 void AssertEQ(const base::DictionaryValue& dict, const std::string& key, |
| 113 const char* expected_value) { | 131 const char* expected_value) { |
| 114 std::string value; | 132 std::string value; |
| 115 ASSERT_TRUE(dict.GetString(key, &value)); | 133 ASSERT_TRUE(dict.GetString(key, &value)); |
| 116 ASSERT_STREQ(value.c_str(), expected_value); | 134 ASSERT_STREQ(value.c_str(), expected_value); |
| 117 } | 135 } |
| 118 | 136 |
| 119 } // namespace | 137 } // namespace |
| 120 | 138 |
| 121 TEST(PrepareUserDataDir, CustomPrefs) { | 139 TEST(PrepareUserDataDir, CustomPrefs) { |
| 122 base::ScopedTempDir temp_dir; | 140 base::ScopedTempDir temp_dir; |
| 123 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 141 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 124 | 142 |
| 125 CommandLine command(CommandLine::NO_PROGRAM); | 143 Switches switches; |
| 126 base::DictionaryValue prefs; | 144 base::DictionaryValue prefs; |
| 127 prefs.SetString("myPrefsKey", "ok"); | 145 prefs.SetString("myPrefsKey", "ok"); |
| 128 prefs.SetStringWithoutPathExpansion("pref.sub", "1"); | 146 prefs.SetStringWithoutPathExpansion("pref.sub", "1"); |
| 129 base::DictionaryValue local_state; | 147 base::DictionaryValue local_state; |
| 130 local_state.SetString("myLocalKey", "ok"); | 148 local_state.SetString("myLocalKey", "ok"); |
| 131 local_state.SetStringWithoutPathExpansion("local.state.sub", "2"); | 149 local_state.SetStringWithoutPathExpansion("local.state.sub", "2"); |
| 132 Status status = internal::PrepareUserDataDir( | 150 Status status = internal::PrepareUserDataDir( |
| 133 temp_dir.path(), &prefs, &local_state); | 151 temp_dir.path(), &prefs, &local_state); |
| 134 ASSERT_EQ(kOk, status.code()); | 152 ASSERT_EQ(kOk, status.code()); |
| 135 | 153 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 146 base::FilePath local_state_file = temp_dir.path().AppendASCII("Local State"); | 164 base::FilePath local_state_file = temp_dir.path().AppendASCII("Local State"); |
| 147 std::string local_state_str; | 165 std::string local_state_str; |
| 148 ASSERT_TRUE(file_util::ReadFileToString(local_state_file, &local_state_str)); | 166 ASSERT_TRUE(file_util::ReadFileToString(local_state_file, &local_state_str)); |
| 149 scoped_ptr<base::Value> local_state_value( | 167 scoped_ptr<base::Value> local_state_value( |
| 150 base::JSONReader::Read(local_state_str)); | 168 base::JSONReader::Read(local_state_str)); |
| 151 const base::DictionaryValue* local_state_dict = NULL; | 169 const base::DictionaryValue* local_state_dict = NULL; |
| 152 ASSERT_TRUE(local_state_value->GetAsDictionary(&local_state_dict)); | 170 ASSERT_TRUE(local_state_value->GetAsDictionary(&local_state_dict)); |
| 153 AssertEQ(*local_state_dict, "myLocalKey", "ok"); | 171 AssertEQ(*local_state_dict, "myLocalKey", "ok"); |
| 154 AssertEQ(*local_state_dict, "local.state.sub", "2"); | 172 AssertEQ(*local_state_dict, "local.state.sub", "2"); |
| 155 } | 173 } |
| OLD | NEW |