OLD | NEW |
1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 PDFium 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 <memory> | 5 #include <memory> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "core/fpdfapi/parser/cpdf_dictionary.h" | 8 #include "core/fpdfapi/parser/cpdf_dictionary.h" |
9 #include "core/fpdfapi/parser/cpdf_name.h" | 9 #include "core/fpdfapi/parser/cpdf_name.h" |
10 #include "core/fpdfapi/parser/cpdf_string.h" | 10 #include "core/fpdfapi/parser/cpdf_string.h" |
11 #include "core/fpdfdoc/cpdf_filespec.h" | 11 #include "core/fpdfdoc/cpdf_filespec.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "testing/test_support.h" | 13 #include "testing/test_support.h" |
14 | 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(cpdf_filespec, EncodeDecodeFileName) { | 15 TEST(cpdf_filespec, EncodeDecodeFileName) { |
23 std::vector<pdfium::NullTermWstrFuncTestData> test_data = { | 16 std::vector<pdfium::NullTermWstrFuncTestData> test_data = { |
24 // Empty src string. | 17 // Empty src string. |
25 {L"", L""}, | 18 {L"", L""}, |
26 // only file name. | 19 // only file name. |
27 {L"test.pdf", L"test.pdf"}, | 20 {L"test.pdf", L"test.pdf"}, |
28 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 21 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
29 // With drive identifier. | 22 // With drive identifier. |
30 {L"r:\\pdfdocs\\spec.pdf", L"/r/pdfdocs/spec.pdf"}, | 23 {L"r:\\pdfdocs\\spec.pdf", L"/r/pdfdocs/spec.pdf"}, |
31 // Relative path. | 24 // Relative path. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 L"/C/docs/test.pdf", | 60 L"/C/docs/test.pdf", |
68 L"C:\\docs\\test.pdf" | 61 L"C:\\docs\\test.pdf" |
69 #elif _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | 62 #elif _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
70 L"/Mac HD/docs/test.pdf", | 63 L"/Mac HD/docs/test.pdf", |
71 L"Mac HD:docs:test.pdf" | 64 L"Mac HD:docs:test.pdf" |
72 #else | 65 #else |
73 L"/docs/test.pdf", | 66 L"/docs/test.pdf", |
74 L"/docs/test.pdf" | 67 L"/docs/test.pdf" |
75 #endif | 68 #endif |
76 }; | 69 }; |
77 ScopedObj str_obj(new CPDF_String(test_data.input)); | 70 std::unique_ptr<CPDF_Object> str_obj(new CPDF_String(test_data.input)); |
78 CPDF_FileSpec file_spec(str_obj.get()); | 71 CPDF_FileSpec file_spec(str_obj.get()); |
79 CFX_WideString file_name; | 72 CFX_WideString file_name; |
80 EXPECT_TRUE(file_spec.GetFileName(&file_name)); | 73 EXPECT_TRUE(file_spec.GetFileName(&file_name)); |
81 EXPECT_TRUE(file_name == test_data.expected); | 74 EXPECT_TRUE(file_name == test_data.expected); |
82 } | 75 } |
83 { | 76 { |
84 // Dictionary object. | 77 // Dictionary object. |
85 pdfium::NullTermWstrFuncTestData test_data[5] = { | 78 pdfium::NullTermWstrFuncTestData test_data[5] = { |
86 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 79 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
87 {L"/C/docs/test.pdf", L"C:\\docs\\test.pdf"}, | 80 {L"/C/docs/test.pdf", L"C:\\docs\\test.pdf"}, |
(...skipping 10 matching lines...) Expand all Loading... |
98 #else | 91 #else |
99 {L"/docs/a/test.pdf", L"/docs/a/test.pdf"}, | 92 {L"/docs/a/test.pdf", L"/docs/a/test.pdf"}, |
100 {L"/docs/b/test.pdf", L"/docs/b/test.pdf"}, | 93 {L"/docs/b/test.pdf", L"/docs/b/test.pdf"}, |
101 {L"/docs/c/test.pdf", L"/docs/c/test.pdf"}, | 94 {L"/docs/c/test.pdf", L"/docs/c/test.pdf"}, |
102 {L"/docs/d/test.pdf", L"/docs/d/test.pdf"}, | 95 {L"/docs/d/test.pdf", L"/docs/d/test.pdf"}, |
103 {L"/docs/e/test.pdf", L"/docs/e/test.pdf"}, | 96 {L"/docs/e/test.pdf", L"/docs/e/test.pdf"}, |
104 #endif | 97 #endif |
105 }; | 98 }; |
106 // Keyword fields in reverse order of precedence to retrieve the file name. | 99 // Keyword fields in reverse order of precedence to retrieve the file name. |
107 const char* const keywords[5] = {"Unix", "Mac", "DOS", "F", "UF"}; | 100 const char* const keywords[5] = {"Unix", "Mac", "DOS", "F", "UF"}; |
108 ScopedDict dict_obj(new CPDF_Dictionary()); | 101 std::unique_ptr<CPDF_Dictionary> dict_obj(new CPDF_Dictionary()); |
109 CPDF_FileSpec file_spec(dict_obj.get()); | 102 CPDF_FileSpec file_spec(dict_obj.get()); |
110 CFX_WideString file_name; | 103 CFX_WideString file_name; |
111 for (int i = 0; i < 5; ++i) { | 104 for (int i = 0; i < 5; ++i) { |
112 dict_obj->SetFor(keywords[i], new CPDF_String(test_data[i].input)); | 105 dict_obj->SetFor(keywords[i], new CPDF_String(test_data[i].input)); |
113 EXPECT_TRUE(file_spec.GetFileName(&file_name)); | 106 EXPECT_TRUE(file_spec.GetFileName(&file_name)); |
114 EXPECT_TRUE(file_name == test_data[i].expected); | 107 EXPECT_TRUE(file_name == test_data[i].expected); |
115 } | 108 } |
116 | 109 |
117 // With all the former fields and 'FS' field suggests 'URL' type. | 110 // With all the former fields and 'FS' field suggests 'URL' type. |
118 dict_obj->SetStringFor("FS", "URL"); | 111 dict_obj->SetStringFor("FS", "URL"); |
119 EXPECT_TRUE(file_spec.GetFileName(&file_name)); | 112 EXPECT_TRUE(file_spec.GetFileName(&file_name)); |
120 // Url string is not decoded. | 113 // Url string is not decoded. |
121 EXPECT_TRUE(file_name == test_data[4].input); | 114 EXPECT_TRUE(file_name == test_data[4].input); |
122 } | 115 } |
123 { | 116 { |
124 // Invalid object. | 117 // Invalid object. |
125 ScopedObj name_obj(new CPDF_Name("test.pdf")); | 118 std::unique_ptr<CPDF_Object> name_obj(new CPDF_Name("test.pdf")); |
126 CPDF_FileSpec file_spec(name_obj.get()); | 119 CPDF_FileSpec file_spec(name_obj.get()); |
127 CFX_WideString file_name; | 120 CFX_WideString file_name; |
128 EXPECT_FALSE(file_spec.GetFileName(&file_name)); | 121 EXPECT_FALSE(file_spec.GetFileName(&file_name)); |
129 } | 122 } |
130 } | 123 } |
131 | 124 |
132 TEST(cpdf_filespec, SetFileName) { | 125 TEST(cpdf_filespec, SetFileName) { |
133 pdfium::NullTermWstrFuncTestData test_data = { | 126 pdfium::NullTermWstrFuncTestData test_data = { |
134 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 127 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
135 L"C:\\docs\\test.pdf", | 128 L"C:\\docs\\test.pdf", |
136 L"/C/docs/test.pdf" | 129 L"/C/docs/test.pdf" |
137 #elif _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ | 130 #elif _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
138 L"Mac HD:docs:test.pdf", | 131 L"Mac HD:docs:test.pdf", |
139 L"/Mac HD/docs/test.pdf" | 132 L"/Mac HD/docs/test.pdf" |
140 #else | 133 #else |
141 L"/docs/test.pdf", | 134 L"/docs/test.pdf", |
142 L"/docs/test.pdf" | 135 L"/docs/test.pdf" |
143 #endif | 136 #endif |
144 }; | 137 }; |
145 // String object. | 138 // String object. |
146 ScopedObj str_obj(new CPDF_String(L"babababa")); | 139 std::unique_ptr<CPDF_Object> str_obj(new CPDF_String(L"babababa")); |
147 CPDF_FileSpec file_spec1(str_obj.get()); | 140 CPDF_FileSpec file_spec1(str_obj.get()); |
148 file_spec1.SetFileName(test_data.input); | 141 file_spec1.SetFileName(test_data.input); |
149 // Check internal object value. | 142 // Check internal object value. |
150 CFX_ByteString str = CFX_ByteString::FromUnicode(test_data.expected); | 143 CFX_ByteString str = CFX_ByteString::FromUnicode(test_data.expected); |
151 EXPECT_TRUE(str == str_obj->GetString()); | 144 EXPECT_TRUE(str == str_obj->GetString()); |
152 // Check we can get the file name back. | 145 // Check we can get the file name back. |
153 CFX_WideString file_name; | 146 CFX_WideString file_name; |
154 EXPECT_TRUE(file_spec1.GetFileName(&file_name)); | 147 EXPECT_TRUE(file_spec1.GetFileName(&file_name)); |
155 EXPECT_TRUE(file_name == test_data.input); | 148 EXPECT_TRUE(file_name == test_data.input); |
156 | 149 |
157 // Dictionary object. | 150 // Dictionary object. |
158 ScopedDict dict_obj(new CPDF_Dictionary()); | 151 std::unique_ptr<CPDF_Dictionary> dict_obj(new CPDF_Dictionary()); |
159 CPDF_FileSpec file_spec2(dict_obj.get()); | 152 CPDF_FileSpec file_spec2(dict_obj.get()); |
160 file_spec2.SetFileName(test_data.input); | 153 file_spec2.SetFileName(test_data.input); |
161 // Check internal object value. | 154 // Check internal object value. |
162 file_name = dict_obj->GetUnicodeTextFor("F"); | 155 file_name = dict_obj->GetUnicodeTextFor("F"); |
163 EXPECT_TRUE(file_name == test_data.expected); | 156 EXPECT_TRUE(file_name == test_data.expected); |
164 file_name = dict_obj->GetUnicodeTextFor("UF"); | 157 file_name = dict_obj->GetUnicodeTextFor("UF"); |
165 EXPECT_TRUE(file_name == test_data.expected); | 158 EXPECT_TRUE(file_name == test_data.expected); |
166 // Check we can get the file name back. | 159 // Check we can get the file name back. |
167 EXPECT_TRUE(file_spec2.GetFileName(&file_name)); | 160 EXPECT_TRUE(file_spec2.GetFileName(&file_name)); |
168 EXPECT_TRUE(file_name == test_data.input); | 161 EXPECT_TRUE(file_name == test_data.input); |
169 } | 162 } |
OLD | NEW |