1 // Copyright 2018 The 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 "samples/pdfium_test_event_helper.h"
6
7 #include <stdio.h>
8
9 #include <string>
10 #include <vector>
11
12 #include "public/fpdf_fwlevent.h"
13 #include "public/fpdfview.h"
14 #include "testing/fx_string_testhelpers.h"
15
16 namespace {
SendCharCodeEvent(FPDF_FORMHANDLE form,FPDF_PAGE page,const std::vector<std::string> & tokens)17 void SendCharCodeEvent(FPDF_FORMHANDLE form,
18 FPDF_PAGE page,
19 const std::vector<std::string>& tokens) {
20 if (tokens.size() != 2) {
21 fprintf(stderr, "charcode: bad args\n");
22 return;
23 }
24
25 int keycode = atoi(tokens[1].c_str());
26 FORM_OnChar(form, page, keycode, 0);
27 }
28
SendKeyCodeEvent(FPDF_FORMHANDLE form,FPDF_PAGE page,const std::vector<std::string> & tokens)29 void SendKeyCodeEvent(FPDF_FORMHANDLE form,
30 FPDF_PAGE page,
31 const std::vector<std::string>& tokens) {
32 if (tokens.size() != 2) {
33 fprintf(stderr, "keycode: bad args\n");
34 return;
35 }
36
37 int keycode = atoi(tokens[1].c_str());
38 FORM_OnKeyDown(form, page, keycode, 0);
39 FORM_OnKeyUp(form, page, keycode, 0);
40 }
41
GetModifiers(std::string modifiers_string)42 uint32_t GetModifiers(std::string modifiers_string) {
43 int modifiers = 0;
44 if (modifiers_string.find("shift") != std::string::npos)
45 modifiers |= FWL_EVENTFLAG_ShiftKey;
46 if (modifiers_string.find("control") != std::string::npos)
47 modifiers |= FWL_EVENTFLAG_ControlKey;
48 if (modifiers_string.find("alt") != std::string::npos)
49 modifiers |= FWL_EVENTFLAG_AltKey;
50
51 return modifiers;
52 }
53
SendMouseDownEvent(FPDF_FORMHANDLE form,FPDF_PAGE page,const std::vector<std::string> & tokens)54 void SendMouseDownEvent(FPDF_FORMHANDLE form,
55 FPDF_PAGE page,
56 const std::vector<std::string>& tokens) {
57 if (tokens.size() != 4 && tokens.size() != 5) {
58 fprintf(stderr, "mousedown: bad args\n");
59 return;
60 }
61
62 int x = atoi(tokens[2].c_str());
63 int y = atoi(tokens[3].c_str());
64 uint32_t modifiers = tokens.size() >= 5 ? GetModifiers(tokens[4]) : 0;
65
66 if (tokens[1] == "left")
67 FORM_OnLButtonDown(form, page, modifiers, x, y);
68 else if (tokens[1] == "right")
69 FORM_OnRButtonDown(form, page, modifiers, x, y);
70 else
71 fprintf(stderr, "mousedown: bad button name\n");
72 }
73
SendMouseUpEvent(FPDF_FORMHANDLE form,FPDF_PAGE page,const std::vector<std::string> & tokens)74 void SendMouseUpEvent(FPDF_FORMHANDLE form,
75 FPDF_PAGE page,
76 const std::vector<std::string>& tokens) {
77 if (tokens.size() != 4 && tokens.size() != 5) {
78 fprintf(stderr, "mouseup: bad args\n");
79 return;
80 }
81
82 int x = atoi(tokens[2].c_str());
83 int y = atoi(tokens[3].c_str());
84 int modifiers = tokens.size() >= 5 ? GetModifiers(tokens[4]) : 0;
85 if (tokens[1] == "left")
86 FORM_OnLButtonUp(form, page, modifiers, x, y);
87 else if (tokens[1] == "right")
88 FORM_OnRButtonUp(form, page, modifiers, x, y);
89 else
90 fprintf(stderr, "mouseup: bad button name\n");
91 }
92
SendMouseDoubleClickEvent(FPDF_FORMHANDLE form,FPDF_PAGE page,const std::vector<std::string> & tokens)93 void SendMouseDoubleClickEvent(FPDF_FORMHANDLE form,
94 FPDF_PAGE page,
95 const std::vector<std::string>& tokens) {
96 if (tokens.size() != 4 && tokens.size() != 5) {
97 fprintf(stderr, "mousedoubleclick: bad args\n");
98 return;
99 }
100
101 int x = atoi(tokens[2].c_str());
102 int y = atoi(tokens[3].c_str());
103 int modifiers = tokens.size() >= 5 ? GetModifiers(tokens[4]) : 0;
104 if (tokens[1] != "left") {
105 fprintf(stderr, "mousedoubleclick: bad button name\n");
106 return;
107 }
108 FORM_OnLButtonDoubleClick(form, page, modifiers, x, y);
109 }
110
SendMouseMoveEvent(FPDF_FORMHANDLE form,FPDF_PAGE page,const std::vector<std::string> & tokens)111 void SendMouseMoveEvent(FPDF_FORMHANDLE form,
112 FPDF_PAGE page,
113 const std::vector<std::string>& tokens) {
114 if (tokens.size() != 3) {
115 fprintf(stderr, "mousemove: bad args\n");
116 return;
117 }
118
119 int x = atoi(tokens[1].c_str());
120 int y = atoi(tokens[2].c_str());
121 FORM_OnMouseMove(form, page, 0, x, y);
122 }
123
SendFocusEvent(FPDF_FORMHANDLE form,FPDF_PAGE page,const std::vector<std::string> & tokens)124 void SendFocusEvent(FPDF_FORMHANDLE form,
125 FPDF_PAGE page,
126 const std::vector<std::string>& tokens) {
127 if (tokens.size() != 3) {
128 fprintf(stderr, "focus: bad args\n");
129 return;
130 }
131
132 int x = atoi(tokens[1].c_str());
133 int y = atoi(tokens[2].c_str());
134 FORM_OnFocus(form, page, 0, x, y);
135 }
136 } // namespace
137
SendPageEvents(FPDF_FORMHANDLE form,FPDF_PAGE page,const std::string & events)138 void SendPageEvents(FPDF_FORMHANDLE form,
139 FPDF_PAGE page,
140 const std::string& events) {
141 auto lines = StringSplit(events, '\n');
142 for (auto line : lines) {
143 auto command = StringSplit(line, '#');
144 if (command[0].empty())
145 continue;
146 auto tokens = StringSplit(command[0], ',');
147 if (tokens[0] == "charcode") {
148 SendCharCodeEvent(form, page, tokens);
149 } else if (tokens[0] == "keycode") {
150 SendKeyCodeEvent(form, page, tokens);
151 } else if (tokens[0] == "mousedown") {
152 SendMouseDownEvent(form, page, tokens);
153 } else if (tokens[0] == "mouseup") {
154 SendMouseUpEvent(form, page, tokens);
155 } else if (tokens[0] == "mousedoubleclick") {
156 SendMouseDoubleClickEvent(form, page, tokens);
157 } else if (tokens[0] == "mousemove") {
158 SendMouseMoveEvent(form, page, tokens);
159 } else if (tokens[0] == "focus") {
160 SendFocusEvent(form, page, tokens);
161 } else {
162 fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str());
163 }
164 }
165 }
166