OLD | NEW |
| (Empty) |
1 // Copyright 2016 PDFium 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 <memory> | |
6 #include <vector> | |
7 | |
8 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" | |
9 #include "core/fpdfapi/fpdf_parser/include/cpdf_name.h" | |
10 #include "core/fpdfapi/fpdf_parser/include/cpdf_string.h" | |
11 #include "core/fpdfdoc/include/cpdf_filespec.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 #include "testing/test_support.h" | |
14 | |
15 namespace { | |
16 | |
17 using ScopedObj = std::unique_ptr<CPDF_Object, ReleaseDeleter<CPDF_Object>>; | |
18 using ScopedDict = | |
19 std::unique_ptr<CPDF_Dictionary, ReleaseDeleter<CPDF_Dictionary>>; | |
20 } | |
21 | |
22 TEST(doc_basic_filespec, EncodeDecodeFileName) { | |
23 std::vector<pdfium::NullTermWstrFuncTestData> test_data = { | |
24 // Empty src string. | |
25 {L"", L""}, | |
26 // only file name. | |
27 {L"test.pdf", L"test.pdf"}, | |
28 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | |
29 // With drive identifier. | |
30 {L"r:\\pdfdocs\\spec.pdf", L"/r/pdfdocs/spec.pdf"}, | |
31 // Relative path. | |
32 {L"My Document\\test.pdf", L"My Document/test.pdf"}, | |
33 // Absolute path without drive identifier. | |
34 {L"\\pdfdocs\\spec.pdf", L"//pdfdocs/spec.pdf"}, | |
35 // Absolute path with double backslashes. | |
36 {L"\\\\pdfdocs\\spec.pdf", L"/pdfdocs/spec.pdf"}, | |
37 // Network resource name. It is not supported yet. | |
38 // {L"pclib/eng:\\pdfdocs\\spec.pdf", L"/pclib/eng/pdfdocs/spec.pdf"}, | |
39 #elif _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
40 // Absolute path with colon separator. | |
41 {L"Mac HD:PDFDocs:spec.pdf", L"/Mac HD/PDFDocs/spec.pdf"}, | |
42 // Relative path with colon separator. | |
43 {L"PDFDocs:spec.pdf", L"PDFDocs/spec.pdf"}, | |
44 #else | |
45 // Relative path. | |
46 {L"./docs/test.pdf", L"./docs/test.pdf"}, | |
47 // Relative path with parent dir. | |
48 {L"../test_docs/test.pdf", L"../test_docs/test.pdf"}, | |
49 // Absolute path. | |
50 {L"/usr/local/home/test.pdf", L"/usr/local/home/test.pdf"}, | |
51 #endif | |
52 }; | |
53 for (const auto& data : test_data) { | |
54 CFX_WideString encoded_str = CPDF_FileSpec::EncodeFileName(data.input); | |
55 EXPECT_TRUE(encoded_str == data.expected); | |
56 // DecodeFileName is the reverse procedure of EncodeFileName. | |
57 CFX_WideString decoded_str = CPDF_FileSpec::DecodeFileName(data.expected); | |
58 EXPECT_TRUE(decoded_str == data.input); | |
59 } | |
60 } | |
61 | |
62 TEST(doc_basic_filespec, GetFileName) { | |
63 { | |
64 // String object. | |
65 pdfium::NullTermWstrFuncTestData test_data = { | |
66 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | |
67 L"/C/docs/test.pdf", | |
68 L"C:\\docs\\test.pdf" | |
69 #elif _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
70 L"/Mac HD/docs/test.pdf", | |
71 L"Mac HD:docs:test.pdf" | |
72 #else | |
73 L"/docs/test.pdf", | |
74 L"/docs/test.pdf" | |
75 #endif | |
76 }; | |
77 ScopedObj str_obj(new CPDF_String(test_data.input)); | |
78 CPDF_FileSpec file_spec(str_obj.get()); | |
79 CFX_WideString file_name; | |
80 EXPECT_TRUE(file_spec.GetFileName(&file_name)); | |
81 EXPECT_TRUE(file_name == test_data.expected); | |
82 } | |
83 { | |
84 // Dictionary object. | |
85 pdfium::NullTermWstrFuncTestData test_data[5] = { | |
86 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | |
87 {L"/C/docs/test.pdf", L"C:\\docs\\test.pdf"}, | |
88 {L"/D/docs/test.pdf", L"D:\\docs\\test.pdf"}, | |
89 {L"/E/docs/test.pdf", L"E:\\docs\\test.pdf"}, | |
90 {L"/F/docs/test.pdf", L"F:\\docs\\test.pdf"}, | |
91 {L"/G/docs/test.pdf", L"G:\\docs\\test.pdf"}, | |
92 #elif _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
93 {L"/Mac HD/docs1/test.pdf", L"Mac HD:docs1:test.pdf"}, | |
94 {L"/Mac HD/docs2/test.pdf", L"Mac HD:docs2:test.pdf"}, | |
95 {L"/Mac HD/docs3/test.pdf", L"Mac HD:docs3:test.pdf"}, | |
96 {L"/Mac HD/docs4/test.pdf", L"Mac HD:docs4:test.pdf"}, | |
97 {L"/Mac HD/docs5/test.pdf", L"Mac HD:docs5:test.pdf"}, | |
98 #else | |
99 {L"/docs/a/test.pdf", L"/docs/a/test.pdf"}, | |
100 {L"/docs/b/test.pdf", L"/docs/b/test.pdf"}, | |
101 {L"/docs/c/test.pdf", L"/docs/c/test.pdf"}, | |
102 {L"/docs/d/test.pdf", L"/docs/d/test.pdf"}, | |
103 {L"/docs/e/test.pdf", L"/docs/e/test.pdf"}, | |
104 #endif | |
105 }; | |
106 // Keyword fields in reverse order of precedence to retrieve the file name. | |
107 const char* const keywords[5] = {"Unix", "Mac", "DOS", "F", "UF"}; | |
108 ScopedDict dict_obj(new CPDF_Dictionary); | |
109 CPDF_FileSpec file_spec(dict_obj.get()); | |
110 CFX_WideString file_name; | |
111 for (int i = 0; i < 5; ++i) { | |
112 dict_obj->SetAt(keywords[i], new CPDF_String(test_data[i].input)); | |
113 EXPECT_TRUE(file_spec.GetFileName(&file_name)); | |
114 EXPECT_TRUE(file_name == test_data[i].expected); | |
115 } | |
116 | |
117 // With all the former fields and 'FS' field suggests 'URL' type. | |
118 dict_obj->SetAtString("FS", "URL"); | |
119 EXPECT_TRUE(file_spec.GetFileName(&file_name)); | |
120 // Url string is not decoded. | |
121 EXPECT_TRUE(file_name == test_data[4].input); | |
122 } | |
123 { | |
124 // Invalid object. | |
125 ScopedObj name_obj(new CPDF_Name("test.pdf")); | |
126 CPDF_FileSpec file_spec(name_obj.get()); | |
127 CFX_WideString file_name; | |
128 EXPECT_FALSE(file_spec.GetFileName(&file_name)); | |
129 } | |
130 } | |
131 | |
132 TEST(doc_basic_filespec, SetFileName) { | |
133 pdfium::NullTermWstrFuncTestData test_data = { | |
134 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | |
135 L"C:\\docs\\test.pdf", | |
136 L"/C/docs/test.pdf" | |
137 #elif _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | |
138 L"Mac HD:docs:test.pdf", | |
139 L"/Mac HD/docs/test.pdf" | |
140 #else | |
141 L"/docs/test.pdf", | |
142 L"/docs/test.pdf" | |
143 #endif | |
144 }; | |
145 // String object. | |
146 ScopedObj str_obj(new CPDF_String(L"babababa")); | |
147 CPDF_FileSpec file_spec1(str_obj.get()); | |
148 file_spec1.SetFileName(test_data.input); | |
149 // Check internal object value. | |
150 CFX_ByteString str = CFX_ByteString::FromUnicode(test_data.expected); | |
151 EXPECT_TRUE(str == str_obj->GetString()); | |
152 // Check we can get the file name back. | |
153 CFX_WideString file_name; | |
154 EXPECT_TRUE(file_spec1.GetFileName(&file_name)); | |
155 EXPECT_TRUE(file_name == test_data.input); | |
156 | |
157 // Dictionary object. | |
158 ScopedDict dict_obj(new CPDF_Dictionary); | |
159 CPDF_FileSpec file_spec2(dict_obj.get()); | |
160 file_spec2.SetFileName(test_data.input); | |
161 // Check internal object value. | |
162 file_name = dict_obj->GetUnicodeTextBy("F"); | |
163 EXPECT_TRUE(file_name == test_data.expected); | |
164 file_name = dict_obj->GetUnicodeTextBy("UF"); | |
165 EXPECT_TRUE(file_name == test_data.expected); | |
166 // Check we can get the file name back. | |
167 EXPECT_TRUE(file_spec2.GetFileName(&file_name)); | |
168 EXPECT_TRUE(file_name == test_data.input); | |
169 } | |
OLD | NEW |