1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <android-base/file.h>
18 #include <gtest/gtest.h>
19
20 #include <memory>
21 #include <string>
22 #include <vector>
23
24 // Goes first due to conflicts.
25 #include "document.h"
26 #include "page.h"
27 #include "rect.h"
28 // #include "file/base/path.h"
29 #include "cpp/fpdf_scopers.h"
30 #include "fpdfview.h"
31
32 using pdfClient::Document;
33 using pdfClient::Page;
34 using pdfClient::Rectangle_i;
35
36 namespace {
37
38 static const std::string kTestdata = "testdata";
39 static const std::string kLinksFile = "sample_links.pdf";
40
Area(const Rectangle_i & rect)41 int Area(const Rectangle_i& rect) {
42 return rect.Width() * rect.Height();
43 }
44
GetTestDataDir()45 std::string GetTestDataDir() {
46 return android::base::GetExecutableDirectory();
47 }
48
GetTestFile(std::string filename)49 std::string GetTestFile(std::string filename) {
50 return GetTestDataDir() + "/" + kTestdata + "/" + filename;
51 }
52
LoadTestDocument(const std::string filename)53 ScopedFPDFDocument LoadTestDocument(const std::string filename) {
54 return ScopedFPDFDocument(FPDF_LoadDocument(GetTestFile(filename).c_str(), nullptr));
55 }
56
TEST(Test,GetLinksUtf8)57 TEST(Test, GetLinksUtf8) {
58 Document doc(LoadTestDocument(kLinksFile), false);
59 std::shared_ptr<Page> page = doc.GetPage(0);
60
61 std::vector<Rectangle_i> rects;
62 std::vector<int> link_to_rect;
63 std::vector<std::string> urls;
64 page->GetLinksUtf8(&rects, &link_to_rect, &urls);
65
66 EXPECT_EQ(1, rects.size());
67 EXPECT_GT(Area(rects[0]), 0);
68
69 EXPECT_EQ(1, urls.size());
70 EXPECT_EQ("https://www.google.com/", urls[0]);
71
72 EXPECT_EQ(1, link_to_rect.size());
73 EXPECT_EQ(0, link_to_rect[0]);
74 }
75
76 } // namespace
77
78 // int main(int argc, char** argv) {
79 // ::testing::InitGoogleTest(&argc, argv);
80 // FPDF_InitLibrary();
81 // int status = RUN_ALL_TESTS();
82 // // Destroy the library to keep the memory leak checker happy.
83 // FPDF_DestroyLibrary();
84 // return status;
85 // }