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