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/path_service.h" | 11 #include "base/path_service.h" |
12 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chrome/test/chromedriver/chrome/status.h" | 14 #include "chrome/test/chromedriver/chrome/status.h" |
15 #include "chrome/test/chromedriver/chrome_launcher.h" | 15 #include "chrome/test/chromedriver/chrome_launcher.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 TEST(ProcessCommandLineArgs, NoArgs) { | |
19 CommandLine command(CommandLine::NO_PROGRAM); | |
20 base::ListValue switches; | |
21 ASSERT_TRUE(switches.empty()); | |
22 Status status = internal::ProcessCommandLineArgs(&switches, &command); | |
23 ASSERT_TRUE(status.IsOk()); | |
24 ASSERT_TRUE(command.GetSwitches().empty()); | |
25 } | |
26 | |
27 TEST(ProcessCommandLineArgs, SingleArgWithoutValue) { | |
28 CommandLine command(CommandLine::NO_PROGRAM); | |
29 base::ListValue switches; | |
30 switches.AppendString("enable-nacl"); | |
31 ASSERT_EQ(1u, switches.GetSize()); | |
32 Status status = internal::ProcessCommandLineArgs(&switches, &command); | |
33 ASSERT_TRUE(status.IsOk()); | |
34 ASSERT_EQ(1u, command.GetSwitches().size()); | |
35 ASSERT_TRUE(command.HasSwitch("enable-nacl")); | |
36 } | |
37 | |
38 TEST(ProcessCommandLineArgs, SingleArgWithValue) { | |
39 CommandLine command(CommandLine::NO_PROGRAM); | |
40 base::ListValue switches; | |
41 switches.AppendString("load-extension=/test/extension"); | |
42 ASSERT_EQ(1u, switches.GetSize()); | |
43 Status status = internal::ProcessCommandLineArgs(&switches, &command); | |
44 ASSERT_TRUE(status.IsOk()); | |
45 ASSERT_EQ(1u, command.GetSwitches().size()); | |
46 ASSERT_TRUE(command.HasSwitch("load-extension")); | |
47 ASSERT_EQ("/test/extension", command.GetSwitchValueASCII("load-extension")); | |
48 } | |
49 | |
50 TEST(ProcessCommandLineArgs, MultipleArgs) { | |
51 CommandLine command(CommandLine::NO_PROGRAM); | |
52 base::ListValue switches; | |
53 switches.AppendString("disable-sync"); | |
54 switches.AppendString("user-data-dir=/test/user/data"); | |
55 ASSERT_EQ(2u, switches.GetSize()); | |
56 Status status = internal::ProcessCommandLineArgs(&switches, &command); | |
57 ASSERT_TRUE(status.IsOk()); | |
58 ASSERT_EQ(2u, command.GetSwitches().size()); | |
59 ASSERT_TRUE(command.HasSwitch("disable-sync")); | |
60 ASSERT_TRUE(command.HasSwitch("user-data-dir")); | |
61 ASSERT_EQ("/test/user/data", command.GetSwitchValueASCII("user-data-dir")); | |
62 } | |
63 | |
64 TEST(ProcessExtensions, AutomationExtension) { | |
65 CommandLine command(CommandLine::NO_PROGRAM); | |
66 base::ListValue extensions; | |
67 base::FilePath extension_dir; | |
68 Status status = internal::ProcessExtensions(&extensions, extension_dir, | |
69 true, &command); | |
70 ASSERT_TRUE(status.IsOk()) << status.message(); | |
71 ASSERT_TRUE(command.HasSwitch("load-extension")); | |
72 base::FilePath temp_ext_path = command.GetSwitchValuePath("load-extension"); | |
73 ASSERT_TRUE(file_util::PathExists(temp_ext_path)); | |
74 } | |
75 | |
76 TEST(ProcessExtensions, NoExtension) { | 18 TEST(ProcessExtensions, NoExtension) { |
77 CommandLine command(CommandLine::NO_PROGRAM); | 19 CommandLine command(CommandLine::NO_PROGRAM); |
78 base::ListValue extensions; | 20 std::vector<std::string> extensions; |
79 base::FilePath extension_dir; | 21 base::FilePath extension_dir; |
80 Status status = internal::ProcessExtensions(&extensions, extension_dir, | 22 Status status = internal::ProcessExtensions(extensions, extension_dir, |
81 false, &command); | 23 false, &command); |
82 ASSERT_TRUE(status.IsOk()); | 24 ASSERT_TRUE(status.IsOk()); |
83 ASSERT_FALSE(command.HasSwitch("load-extension")); | 25 ASSERT_FALSE(command.HasSwitch("load-extension")); |
84 } | 26 } |
85 | 27 |
86 TEST(ProcessExtensions, SingleExtension) { | 28 TEST(ProcessExtensions, SingleExtension) { |
87 base::FilePath source_root; | 29 base::FilePath source_root; |
88 PathService::Get(base::DIR_SOURCE_ROOT, &source_root); | 30 PathService::Get(base::DIR_SOURCE_ROOT, &source_root); |
89 base::FilePath crx_file_path = source_root.AppendASCII( | 31 base::FilePath crx_file_path = source_root.AppendASCII( |
90 "chrome/test/data/chromedriver/ext_test_1.crx"); | 32 "chrome/test/data/chromedriver/ext_test_1.crx"); |
91 std::string crx_contents; | 33 std::string crx_contents; |
92 ASSERT_TRUE(file_util::ReadFileToString(crx_file_path, &crx_contents)); | 34 ASSERT_TRUE(file_util::ReadFileToString(crx_file_path, &crx_contents)); |
93 | 35 |
94 base::ListValue extensions; | 36 std::vector<std::string> extensions; |
95 std::string crx_encoded; | 37 std::string crx_encoded; |
96 ASSERT_TRUE(base::Base64Encode(crx_contents, &crx_encoded)); | 38 ASSERT_TRUE(base::Base64Encode(crx_contents, &crx_encoded)); |
97 extensions.AppendString(crx_encoded); | 39 extensions.push_back(crx_encoded); |
98 | 40 |
99 base::ScopedTempDir extension_dir; | 41 base::ScopedTempDir extension_dir; |
100 ASSERT_TRUE(extension_dir.CreateUniqueTempDir()); | 42 ASSERT_TRUE(extension_dir.CreateUniqueTempDir()); |
101 | 43 |
102 CommandLine command(CommandLine::NO_PROGRAM); | 44 CommandLine command(CommandLine::NO_PROGRAM); |
103 Status status = internal::ProcessExtensions(&extensions, extension_dir.path(), | 45 Status status = internal::ProcessExtensions(extensions, extension_dir.path(), |
104 false, &command); | 46 false, &command); |
105 ASSERT_TRUE(status.IsOk()); | 47 ASSERT_TRUE(status.IsOk()); |
106 ASSERT_TRUE(command.HasSwitch("load-extension")); | 48 ASSERT_TRUE(command.HasSwitch("load-extension")); |
107 base::FilePath temp_ext_path = command.GetSwitchValuePath("load-extension"); | 49 base::FilePath temp_ext_path = command.GetSwitchValuePath("load-extension"); |
108 ASSERT_TRUE(file_util::PathExists(temp_ext_path)); | 50 ASSERT_TRUE(file_util::PathExists(temp_ext_path)); |
109 } | 51 } |
110 | 52 |
111 TEST(ProcessExtensions, MultipleExtensions) { | 53 TEST(ProcessExtensions, MultipleExtensions) { |
112 base::FilePath source_root; | 54 base::FilePath source_root; |
113 PathService::Get(base::DIR_SOURCE_ROOT, &source_root); | 55 PathService::Get(base::DIR_SOURCE_ROOT, &source_root); |
114 base::FilePath test_ext_path = source_root.AppendASCII( | 56 base::FilePath test_ext_path = source_root.AppendASCII( |
115 "chrome/test/data/chromedriver"); | 57 "chrome/test/data/chromedriver"); |
116 base::FilePath test_crx_1 = test_ext_path.AppendASCII("ext_test_1.crx"); | 58 base::FilePath test_crx_1 = test_ext_path.AppendASCII("ext_test_1.crx"); |
117 base::FilePath test_crx_2 = test_ext_path.AppendASCII("ext_test_2.crx"); | 59 base::FilePath test_crx_2 = test_ext_path.AppendASCII("ext_test_2.crx"); |
118 | 60 |
119 std::string crx_1_contents, crx_2_contents; | 61 std::string crx_1_contents, crx_2_contents; |
120 ASSERT_TRUE(file_util::ReadFileToString(test_crx_1, &crx_1_contents)); | 62 ASSERT_TRUE(file_util::ReadFileToString(test_crx_1, &crx_1_contents)); |
121 ASSERT_TRUE(file_util::ReadFileToString(test_crx_2, &crx_2_contents)); | 63 ASSERT_TRUE(file_util::ReadFileToString(test_crx_2, &crx_2_contents)); |
122 | 64 |
123 base::ListValue extensions; | 65 std::vector<std::string> extensions; |
124 std::string crx_1_encoded, crx_2_encoded; | 66 std::string crx_1_encoded, crx_2_encoded; |
125 ASSERT_TRUE(base::Base64Encode(crx_1_contents, &crx_1_encoded)); | 67 ASSERT_TRUE(base::Base64Encode(crx_1_contents, &crx_1_encoded)); |
126 ASSERT_TRUE(base::Base64Encode(crx_2_contents, &crx_2_encoded)); | 68 ASSERT_TRUE(base::Base64Encode(crx_2_contents, &crx_2_encoded)); |
127 extensions.AppendString(crx_1_encoded); | 69 extensions.push_back(crx_1_encoded); |
128 extensions.AppendString(crx_2_encoded); | 70 extensions.push_back(crx_2_encoded); |
129 | 71 |
130 base::ScopedTempDir extension_dir; | 72 base::ScopedTempDir extension_dir; |
131 ASSERT_TRUE(extension_dir.CreateUniqueTempDir()); | 73 ASSERT_TRUE(extension_dir.CreateUniqueTempDir()); |
132 | 74 |
133 CommandLine command(CommandLine::NO_PROGRAM); | 75 CommandLine command(CommandLine::NO_PROGRAM); |
134 Status status = internal::ProcessExtensions(&extensions, extension_dir.path(), | 76 Status status = internal::ProcessExtensions(extensions, extension_dir.path(), |
135 false, &command); | 77 false, &command); |
136 ASSERT_TRUE(status.IsOk()); | 78 ASSERT_TRUE(status.IsOk()); |
137 ASSERT_TRUE(command.HasSwitch("load-extension")); | 79 ASSERT_TRUE(command.HasSwitch("load-extension")); |
138 CommandLine::StringType ext_paths = command.GetSwitchValueNative( | 80 CommandLine::StringType ext_paths = command.GetSwitchValueNative( |
139 "load-extension"); | 81 "load-extension"); |
140 std::vector<CommandLine::StringType> ext_path_list; | 82 std::vector<CommandLine::StringType> ext_path_list; |
141 base::SplitString(ext_paths, FILE_PATH_LITERAL(','), &ext_path_list); | 83 base::SplitString(ext_paths, FILE_PATH_LITERAL(','), &ext_path_list); |
142 ASSERT_EQ(2u, ext_path_list.size()); | 84 ASSERT_EQ(2u, ext_path_list.size()); |
143 ASSERT_TRUE(file_util::PathExists(base::FilePath(ext_path_list[0]))); | 85 ASSERT_TRUE(file_util::PathExists(base::FilePath(ext_path_list[0]))); |
144 ASSERT_TRUE(file_util::PathExists(base::FilePath(ext_path_list[1]))); | 86 ASSERT_TRUE(file_util::PathExists(base::FilePath(ext_path_list[1]))); |
(...skipping 16 matching lines...) Expand all Loading... |
161 temp_dir.path().AppendASCII("Default").AppendASCII("Preferences"); | 103 temp_dir.path().AppendASCII("Default").AppendASCII("Preferences"); |
162 std::string prefs_str; | 104 std::string prefs_str; |
163 ASSERT_TRUE(file_util::ReadFileToString(prefs_file, &prefs_str)); | 105 ASSERT_TRUE(file_util::ReadFileToString(prefs_file, &prefs_str)); |
164 ASSERT_TRUE(prefs_str.find("myPrefsKey") != std::string::npos); | 106 ASSERT_TRUE(prefs_str.find("myPrefsKey") != std::string::npos); |
165 | 107 |
166 base::FilePath local_state_file = temp_dir.path().AppendASCII("Local State"); | 108 base::FilePath local_state_file = temp_dir.path().AppendASCII("Local State"); |
167 std::string local_state_str; | 109 std::string local_state_str; |
168 ASSERT_TRUE(file_util::ReadFileToString(local_state_file, &local_state_str)); | 110 ASSERT_TRUE(file_util::ReadFileToString(local_state_file, &local_state_str)); |
169 ASSERT_TRUE(local_state_str.find("myLocalKey") != std::string::npos); | 111 ASSERT_TRUE(local_state_str.find("myLocalKey") != std::string::npos); |
170 } | 112 } |
OLD | NEW |