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