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