Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfxjse_formcalc_context_embeddertest.cpp
Go to the documentation of this file.
1// Copyright 2017 The PDFium Authors
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 <math.h>
6
7#include <algorithm>
8
9#include "core/fxcrt/compiler_specific.h"
10#include "core/fxcrt/fx_extension.h"
11#include "fxjs/fxv8.h"
12#include "fxjs/xfa/cfxjse_engine.h"
13#include "fxjs/xfa/cfxjse_isolatetracker.h"
14#include "fxjs/xfa/cfxjse_value.h"
15#include "testing/gtest/include/gtest/gtest.h"
16#include "testing/scoped_set_tz.h"
17#include "testing/xfa_js_embedder_test.h"
18#include "xfa/fxfa/cxfa_eventparam.h"
19
21 public:
24
25 protected:
27 return GetScriptContext()->GetJseContextForTest();
28 }
29
31 EXPECT_FALSE(Execute(input)) << "Program: " << input;
32 }
33
35 EXPECT_TRUE(Execute(input)) << "Program: " << input;
36
38 EXPECT_TRUE(fxv8::IsNull(GetValue())) << "Program: " << input;
39 }
40
41 void ExecuteExpectBool(ByteStringView input, bool expected) {
42 EXPECT_TRUE(Execute(input)) << "Program: " << input;
43
45 v8::Local<v8::Value> value = GetValue();
46
47 // Yes, bools might be integers, somehow.
48 EXPECT_TRUE(fxv8::IsBoolean(value) || fxv8::IsInteger(value))
49 << "Program: " << input;
50 EXPECT_EQ(expected, fxv8::ReentrantToBooleanHelper(isolate(), value))
51 << "Program: " << input;
52 }
53
54 void ExecuteExpectInt32(ByteStringView input, int32_t expected) {
55 EXPECT_TRUE(Execute(input)) << "Program: " << input;
56
58 v8::Local<v8::Value> value = GetValue();
59 EXPECT_TRUE(fxv8::IsInteger(value)) << "Program: " << input;
60 EXPECT_EQ(expected, fxv8::ReentrantToInt32Helper(isolate(), value))
61 << "Program: " << input;
62 }
63
64 void ExecuteExpectFloat(ByteStringView input, float expected) {
65 EXPECT_TRUE(Execute(input)) << "Program: " << input;
66
68 v8::Local<v8::Value> value = GetValue();
69 EXPECT_TRUE(fxv8::IsNumber(value));
70 EXPECT_FLOAT_EQ(expected, fxv8::ReentrantToFloatHelper(isolate(), value))
71 << "Program: " << input;
72 }
73
74 void ExecuteExpectFloatNear(ByteStringView input, float expected) {
75 constexpr float kPrecision = 0.000001f;
76
77 EXPECT_TRUE(Execute(input)) << "Program: " << input;
78
80 v8::Local<v8::Value> value = GetValue();
81 EXPECT_TRUE(fxv8::IsNumber(value));
82 EXPECT_NEAR(expected, fxv8::ReentrantToFloatHelper(isolate(), value),
83 kPrecision)
84 << "Program: " << input;
85 }
86
88 EXPECT_TRUE(Execute(input)) << "Program: " << input;
89
91 v8::Local<v8::Value> value = GetValue();
92 EXPECT_TRUE(fxv8::IsNumber(value));
93 EXPECT_TRUE(isnan(fxv8::ReentrantToDoubleHelper(isolate(), value)));
94 }
95
96 void ExecuteExpectString(ByteStringView input, const char* expected) {
97 EXPECT_TRUE(Execute(input)) << "Program: " << input;
98
100 v8::Local<v8::Value> value = GetValue();
101 EXPECT_TRUE(fxv8::IsString(value));
102 EXPECT_EQ(expected, fxv8::ReentrantToByteStringHelper(isolate(), value))
103 << "Program: " << input;
104 }
105};
106
107// TODO(dsinclair): Comment out tests are broken and need to be fixed.
108
110 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
111
112 const char input[] = "";
113 EXPECT_TRUE(Execute(input));
114 // TODO(dsinclair): This should probably throw as a blank formcalc script
115 // is invalid.
116}
117
119 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
120 ExecuteExpectInt32("123", 123);
121}
122
124 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
125
126 ExecuteExpectInt32("123 + 456", 579);
127 ExecuteExpectInt32("2 - 3 * 10 / 2 + 7", -6);
128 ExecuteExpectInt32("10 * 3 + 5 * 4", 50);
129 ExecuteExpectInt32("(5 - \"abc\") * 3", 15);
130 ExecuteExpectInt32("\"100\" / 10e1", 1);
131 ExecuteExpectInt32("5 + null + 3", 8);
132#if 0
133 // TODO(thestig): Investigate these cases.
134 ExecuteExpectInt32(
135 "if (\"abc\") then\n"
136 " 10\n"
137 "else\n"
138 " 20\n"
139 "endif",
140 20);
141 ExecuteExpectInt32("3 / 0 + 1", 0);
142#endif
143 ExecuteExpectInt32("-(17)", -17);
144 ExecuteExpectInt32("-(-17)", 17);
145 ExecuteExpectInt32("+(17)", 17);
146 ExecuteExpectInt32("+(-17)", -17);
147 ExecuteExpectInt32("if (1 < 2) then\n1\nendif", 1);
148 ExecuteExpectInt32(
149 "if (\"abc\" > \"def\") then\n"
150 " 1 and 0\n"
151 "else\n"
152 " 0\n"
153 "endif",
154 0);
155}
156
158 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
159
160 ExecuteExpectString("\"abc\"", "abc");
161 ExecuteExpectString(
162 "concat(\"The total is \", 2, \" dollars and \", 57, \" cents.\")",
163 "The total is 2 dollars and 57 cents.");
164}
165
167 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
168
169 ExecuteExpectBool("0 and 1 or 2 > 1", true);
170 ExecuteExpectBool("2 < 3 not 1 == 1", false);
171 ExecuteExpectBool("\"abc\" | 2", true);
172 ExecuteExpectBool("1 or 0", true);
173 ExecuteExpectBool("0 | 0", false);
174 ExecuteExpectBool("0 or 1 | 0 or 0", true);
175 ExecuteExpectBool("1 and 0", false);
176 ExecuteExpectBool("0 and 1 & 0 and 0", false);
177 ExecuteExpectBool("not(\"true\")", true);
178 ExecuteExpectBool("not(1)", false);
179 ExecuteExpectBool("3 == 3", true);
180 ExecuteExpectBool("3 <> 4", true);
181 ExecuteExpectBool("\"abc\" eq \"def\"", false);
182 ExecuteExpectBool("\"def\" ne \"abc\"", true);
183 ExecuteExpectBool("5 + 5 == 10", true);
184 ExecuteExpectBool("5 + 5 <> \"10\"", false);
185 ExecuteExpectBool("3 < 3", false);
186 ExecuteExpectBool("3 > 4", false);
187 ExecuteExpectBool("\"abc\" <= \"def\"", true);
188 ExecuteExpectBool("\"def\" > \"abc\"", true);
189 ExecuteExpectBool("12 >= 12", true);
190 ExecuteExpectBool("\"true\" < \"false\"", false);
191#if 0
192 // TODO(thestig): Investigate this case.
193 // Confirm with Reader.
194 ExecuteExpectBool("0 & 0", true);
195#endif
196}
197
199 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
200
201 ExecuteExpectFloat("Abs(1.03)", 1.03f);
202 ExecuteExpectFloat("Abs(-1.03)", 1.03f);
203 ExecuteExpectFloat("Abs(0)", 0.0f);
204}
205
207 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
208
209 ExecuteExpectFloat("Avg(0, 32, 16)", 16.0f);
210 ExecuteExpectFloat("Avg(2.5, 17, null)", 9.75f);
211}
212
214 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
215
216 ExecuteExpectInt32("Ceil(2.5875)", 3);
217 ExecuteExpectInt32("Ceil(-5.9)", -5);
218 ExecuteExpectInt32("Ceil(\"abc\")", 0);
219}
220
222 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
223
224 ExecuteExpectInt32("Count(\"Tony\", \"Blue\", 41)", 3);
225}
226
228 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
229
230 ExecuteExpectInt32("Floor(21.3409873)", 21);
231 ExecuteExpectInt32("Floor(5.999965342)", 5);
232 ExecuteExpectInt32("Floor(3.2 * 15)", 48);
233}
234
236 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
237
238 ExecuteExpectInt32("Max(234, 15, 107)", 234);
239 ExecuteExpectInt32("Max(\"abc\", 15, \"Tony Blue\")", 15);
240 ExecuteExpectInt32("Max(\"abc\")", 0);
241}
242
244 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
245
246 ExecuteExpectInt32("Min(234, 15, 107)", 15);
247#if 0
248 // TODO(thestig): Investigate these cases.
249 // Verify with Reader; This should have a return value of 0.
250 ExecuteExpectInt32("Min(\"abc\", 15, \"Tony Blue\")", 15);
251#endif
252 ExecuteExpectInt32("Min(\"abc\")", 0);
253}
254
256 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
257
258 ExecuteExpectInt32("Mod(64, -3)", 1);
259 ExecuteExpectInt32("Mod(-13, 3)", -1);
260 ExecuteExpectInt32("Mod(\"abc\", 2)", 0);
261
262 ExecuteExpectNaN("Mod(10, NaN)");
263 ExecuteExpectNaN("Mod(10, Infinity)");
264}
265
267 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
268
269 ExecuteExpectFloat("Round(12.389764537, 4)", 12.3898f);
270 ExecuteExpectFloat("Round(20/3, 2)", 6.67f);
271 ExecuteExpectFloat("Round(8.9897, \"abc\")", 9.0f);
272 ExecuteExpectFloat("Round(FV(400, 0.10/12, 30*12), 2)", 904195.17f);
273}
274
276 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
277
278 ExecuteExpectInt32("Sum(2, 4, 6, 8)", 20);
279 ExecuteExpectInt32("Sum(-2, 4, -6, 8)", 4);
280 ExecuteExpectInt32("Sum(4, 16, \"abc\", 19)", 39);
281}
282
283// TEST_F(CFXJSEFormCalcContextEmbedderTest, DISABLED_Date) {
284// ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
285//
286// TODO(dsinclair): Make compatible with windows.
287// time_t seconds = time(nullptr);
288// int days = seconds / (60 * 60 * 24);
289
290// EXPECT_TRUE(Execute("Date()"));
291
292// v8::Local<v8::Value> value = GetValue();
293// EXPECT_TRUE(value->IsNumber());
294// EXPECT_EQ(days, value->ToInteger());
295// }
296
298 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
299
300 ExecuteExpectInt32("Date2Num(\"1/1/1900\", \"D/M/YYYY\")", 1);
301 ExecuteExpectInt32("Date2Num(\"03/15/96\", \"MM/DD/YY\")", 35138);
302 ExecuteExpectInt32("Date2Num(\"96-08-20\", \"YY-MM-DD\", \"fr_FR\")", 35296);
303 ExecuteExpectInt32(
304 "Date2Num(\"1/3/00\", \"D/M/YY\") - Date2Num(\"1/2/00\", \"D/M/YY\")",
305 29);
306#if 0
307 // TODO(thestig): Investigate these cases.
308 ExecuteExpectInt32("Date2Num(\"Mar 15, 1996\")", 35138);
309 ExecuteExpectInt32("Date2Num(\"Aug 1, 1996\", \"MMM D, YYYY\")", 35277);
310#endif
311}
312
314 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
315
316 ExecuteExpectString("DateFmt(3, \"de_DE\")", "D. MMMM YYYY");
317#if 0
318 // TODO(thestig): Investigate these cases.
319 ExecuteExpectString("DateFmt(1)", "M/D/YY");
320 ExecuteExpectString("DateFmt(2, \"fr_CA\")", "YY-MM-DD");
321 ExecuteExpectString("DateFmt(4, \"fr_FR\")", "EEE D' MMMM YYYY");
322#endif
323}
324
326 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
327
328 ExecuteExpectInt32("IsoDate2Num(\"1900\")", 1);
329 ExecuteExpectInt32("IsoDate2Num(\"1900-01\")", 1);
330 ExecuteExpectInt32("IsoDate2Num(\"1900-01-01\")", 1);
331 ExecuteExpectInt32("IsoDate2Num(\"19960315T20:20:20\")", 35138);
332 ExecuteExpectInt32("IsoDate2Num(\"2000-03-01\") - IsoDate2Num(\"20000201\")",
333 29);
334}
335
337 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
338
339 ExecuteExpectInt32("IsoTime2Num(\"00:00:00Z\")", 1);
340}
341
343 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
344
345 ExecuteExpectString("LocalDateFmt(3, \"de_CH\")", "t. MMMM jjjj");
346 ExecuteExpectString("LocalDateFmt(4, \"fr_FR\")", "EEEE j MMMM aaaa");
347#if 0
348 // TODO(thestig): Investigate these cases.
349 ExecuteExpectString("LocalDateFmt(1, \"de_DE\")", "tt.MM.uu");
350 ExecuteExpectString("LocalDateFmt(2, \"fr_CA\")", "aa-MM-jj");
351#endif
352}
353
355 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
356
357 ExecuteExpectString("LocalTimeFmt(1, \"de_DE\")", "HH:mm");
358 ExecuteExpectString("LocalTimeFmt(2, \"fr_CA\")", "HH:mm::ss");
359 ExecuteExpectString("LocalTimeFmt(3, \"de_CH\")", "HH:mm:ss z");
360 ExecuteExpectString("LocalTimeFmt(4, \"fr_FR\")", "HH' h 'mm z");
361}
362
364 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
365
366 ExecuteExpectString("Num2Date(1, \"DD/MM/YYYY\")", "01/01/1900");
367 ExecuteExpectString("Num2Date(35139, \"DD-MMM-YYYY\", \"de_DE\")",
368 "16-Mrz-1996");
369#if 0
370 // TODO(thestig): Investigate this case.
371 ExecuteExpectString(
372 "Num2Date(Date2Num(\"Mar 15, 2000\") - Date2Num(\"98-03-15\", "
373 "\"YY-MM-DD\", \"fr_CA\"))",
374 "Jan 1, 1902");
375#endif
376}
377
379 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
380
381 // Broken on Windows only.
382 ExecuteExpectString("Num2GMTime(1, \"HH:MM:SS\")", "00:00:00");
383 // Below broken on other platforms.
384 ExecuteExpectString("Num2GMTime(65593001, \"HH:MM:SS Z\")", "18:13:13 GMT");
385 ExecuteExpectString("Num2GMTime(43993001, TimeFmt(4, \"de_DE\"), \"de_DE\")",
386 "12.13 Uhr GMT");
387}
388
389// TODO(dsinclair): Broken on Mac ...
391 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
392
393 ExecuteExpectString("Num2Time(1, \"HH:MM:SS\")", "00:00:00");
394}
395
396// TEST_F(CFXJSEFormCalcContextEmbedderTest, DISABLED_Time) {
397// ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
398// TODO(dsinclair): Make compatible with windows.
399// struct timeval tp;
400// gettimeofday(&tp, nullptr);
401
402// EXPECT_TRUE(Execute("Time()"));
403
404// v8::Local<v8::Value> value = GetValue();
405// EXPECT_TRUE(value->IsInteger());
406// EXPECT_EQ(tp.tv_sec * 1000L + tp.tv_usec / 1000, value->ToInteger())
407// << "Program: Time()";
408// }
409
411 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
412
413 ExecuteExpectInt32("Time2Num(\"00:00:00 GMT\", \"HH:MM:SS Z\")", 1);
414 ExecuteExpectInt32("Time2Num(\"00:00:01 GMT\", \"HH:MM:SS Z\")", 1001);
415 ExecuteExpectInt32("Time2Num(\"00:01:00 GMT\", \"HH:MM:SS Z\")", 60001);
416 ExecuteExpectInt32("Time2Num(\"01:00:00 GMT\", \"HH:MM:SS Z\")", 3600001);
417 ExecuteExpectInt32("Time2Num(\"23:59:59 GMT\", \"HH:MM:SS Z\")", 86399001);
418 // https://crbug.com/pdfium/1257
419 ExecuteExpectInt32("Time2Num(\"\", \"\", 1)", 0);
420 ExecuteExpectInt32("Time2Num(\"13:13:13 GMT\", \"HH:MM:SS Z\", \"fr_FR\")",
421 47593001);
422}
423
425 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
426
427 static constexpr const char* kTimeZones[] = {
428 "UTC+14", "UTC-14", "UTC+9:30", "UTC-0:30",
429 "UTC+0:30", "UTC-0:01", "UTC+0:01"};
430 for (const char* tz : kTimeZones) {
431 ScopedSetTZ scoped_set_tz(tz);
432 ExecuteExpectInt32("Time2Num(\"00:00:00 GMT\", \"HH:MM:SS Z\")", 1);
433 ExecuteExpectInt32("Time2Num(\"11:59:59 GMT\", \"HH:MM:SS Z\")", 43199001);
434 ExecuteExpectInt32("Time2Num(\"12:00:00 GMT\", \"HH:MM:SS Z\")", 43200001);
435 ExecuteExpectInt32("Time2Num(\"23:59:59 GMT\", \"HH:MM:SS Z\")", 86399001);
436 }
437 {
438 ScopedSetTZ scoped_set_tz("UTC-3:00");
439 ExecuteExpectInt32("Time2Num(\"1:13:13 PM\")", 36793001);
440 ExecuteExpectInt32(
441 "Time2Num(\"13:13:13 GMT\", \"HH:MM:SS Z\") - "
442 "Time2Num(\"13:13:13\", \"HH:MM:SS\")",
443 10800000);
444 }
445}
446
448 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
449
450 ExecuteExpectString("TimeFmt(2, \"fr_CA\")", "HH:MM:SS");
451 ExecuteExpectString("TimeFmt(3, \"fr_FR\")", "HH:MM:SS Z");
452#if 0
453 // TODO(thestig): Investigate these cases.
454 ExecuteExpectString("TimeFmt(1)", "h::MM A");
455 ExecuteExpectString("TimeFmt(4, \"de_DE\")", "H.MM' Uhr 'Z");
456#endif
457}
458
460 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
461
462 ExecuteExpectFloatNear("Apr(35000, 269.50, 360)", 0.08515404566f);
463 ExecuteExpectFloatNear("Apr(210000 * 0.75, 850 + 110, 25 * 26)",
464 0.07161332404f);
465
466 ExecuteExpectError("Apr(2, 2, 2147483648)");
467}
468
470 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
471
472 ExecuteExpectFloat("CTerm(0.10, 500000, 12000)", 39.13224648502f);
473#if 0
474 // TODO(thestig): Investigate these cases.
475 ExecuteExpectFloat("CTerm(0.02, 1000, 100)", 116.2767474515f);
476 ExecuteExpectFloat("CTerm(0.0275 + 0.0025, 1000000, 55000 * 0.10)",
477 176.02226044975f);
478#endif
479}
480
482 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
483
484 ExecuteExpectFloat("FV(400, 0.10 / 12, 30 * 12)", 904195.16991842445f);
485 ExecuteExpectFloat("FV(1000, 0.075 / 4, 10 * 4)", 58791.96145535981f);
486
487 ExecuteExpectError("FV(2, 2, 2147483648)");
488}
489
491 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
492
493 ExecuteExpectFloat("IPmt(30000, 0.085, 295.50, 7, 3)", 624.8839283142f);
494 ExecuteExpectFloat("IPmt(160000, 0.0475, 980, 24, 12)", 7103.80833569485f);
495 ExecuteExpectFloat("IPmt(15000, 0.065, 65.50, 15, 1)", 0.0f);
496}
497
499 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
500
501 ExecuteExpectFloat("NPV(0.065, 5000)", 4694.83568075117f);
502 ExecuteExpectFloat("NPV(0.10, 500, 1500, 4000, 10000)", 11529.60863329007f);
503 ExecuteExpectFloat("NPV(0.0275 / 12, 50, 60, 40, 100, 25)", 273.14193838457f);
504}
505
507 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
508
509 ExecuteExpectFloat("Pmt(25000, 0.085, 12)", 3403.82145169876f);
510 ExecuteExpectFloat("Pmt(5000, 0.01, 1)", 5050);
511 ExecuteExpectFloat("Pmt(5000, 0.01, 1.5)", 5050);
512 ExecuteExpectFloat("Pmt(30000.00, .085 / 12, 12 * 12)", 333.01666929435f);
513 ExecuteExpectFloat("Pmt(10000, .08 / 12, 10)", 1037.03208935916f);
514 ExecuteExpectFloat("Pmt(150000, 0.0475 / 12, 25 * 12)", 855.17604207164f);
515
516 // https://crbug.com/1293179
517 ExecuteExpectError("Pmt(2, 2, 99999997952)");
518}
519
521 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
522
523 ExecuteExpectFloat("PPmt(30000, 0.085, 295.50, 7, 3)", 261.6160716858f);
524 ExecuteExpectFloat("PPmt(160000, 0.0475, 980, 24, 12)", 4656.19166430515f);
525#if 0
526 // TODO(thestig): Investigate this case.
527 ExecuteExpectFloat("PPmt(15000, 0.065, 65.50, 15, 1)", 0.0f);
528#endif
529}
530
532 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
533
534 ExecuteExpectFloat("PV(400, 0.10 / 12, 30 * 12)", 45580.32799074439f);
535 ExecuteExpectFloat("PV(1000, 0.075 / 4, 10 * 4)", 27964.88770467326f);
536
537 // https://crbug.com/1296840
538 ExecuteExpectError("PV(2, 2, 2147483648)");
539}
540
542 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
543
544 ExecuteExpectFloatNear("Rate(12000, 8000, 5)", 0.0844717712f);
545 ExecuteExpectFloatNear("Rate(10000, 0.25 * 5000, 4 * 12)", 0.04427378243f);
546
547 ExecuteExpectError("Rate(2, 2, 2147483648)");
548}
549
551 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
552
553 ExecuteExpectFloat("Term(2500, 0.0275 + 0.0025, 5000)", 1.97128786369f);
554#if 0
555 // TODO(thestig): Investigate this case.
556 ExecuteExpectFloat("Term(475, .05, 1500)", 3.00477517728f);
557#endif
558}
559
561 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
562
563 ExecuteExpectString("Choose(3, \"Taxes\", \"Price\", \"Person\", \"Teller\")",
564 "Person");
565 ExecuteExpectString("Choose(2, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)", "9");
566 ExecuteExpectString(
567 "Choose(20/3, \"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\")",
568 "F");
569}
570
572 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
573 ExecuteExpectBool("Exists(\"hello world\")", false);
574}
575
577 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
578
579 ExecuteExpectBool("HasValue(2)", true);
580 ExecuteExpectBool("HasValue(\" \")", false);
581}
582
584 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
585
586 ExecuteExpectBool("Oneof(3, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)", true);
587 ExecuteExpectBool(
588 "Oneof(\"John\", \"Bill\", \"Gary\", \"Joan\", \"John\", \"Lisa\")",
589 true);
590 ExecuteExpectBool("Oneof(3, 1, 25)", false);
591 ExecuteExpectBool("Oneof(3, 3, null)", true);
592 ExecuteExpectBool("Oneof(3, null, null)", false);
593}
594
596 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
597
598 ExecuteExpectBool("Within(\"C\", \"A\", \"D\")", true);
599 ExecuteExpectBool("Within(1.5, 0, 2)", true);
600 ExecuteExpectBool("Within(-1, 0, 2)", false);
601}
602
604 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
605
606 ExecuteExpectInt32("eval(\"10*3+5*4\")", 50);
607}
608
610 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
611
612 ExecuteExpectString("Null()", "null");
613 ExecuteExpectString("Concat(\"ABC\", Null(), \"DEF\")", "ABCDEF");
614 ExecuteExpectInt32("Null() + 5", 5);
615}
616
618 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
619
620 ExecuteExpectString("Ref(\"10*3+5*4\")", "10*3+5*4");
621 ExecuteExpectString("Ref(\"hello\")", "hello");
622}
623
625 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
626
627 ExecuteExpectString("UnitType(\"36 in\")", "in");
628 ExecuteExpectString("UnitType(\"2.54centimeters\")", "cm");
629 ExecuteExpectString("UnitType(\"picas\")", "pt");
630 ExecuteExpectString("UnitType(\"2.cm\")", "cm");
631 ExecuteExpectString("UnitType(\"2.zero cm\")", "in");
632 ExecuteExpectString("UnitType(\"kilometers\")", "in");
633}
634
636 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
637
638 ExecuteExpectFloat("UnitValue(\"2in\")", 2.0f);
639 ExecuteExpectFloat("UnitValue(\"2in\", \"cm\")", 5.08f);
640#if 0
641 // TODO(thestig): Investigate these cases.
642 // Should the UnitType cases move into the UnitType test case?
643 ExecuteExpectFloat("UnitValue(\"6\", \"pt\")", 432f);
644 ExecuteExpectFloat("UnitType(\"A\", \"cm\")", 0.0f);
645 ExecuteExpectFloat("UnitType(\"5.08cm\", \"kilograms\")", 2.0f);
646#endif
647}
648
650 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
651
652 ExecuteExpectInt32("At(\"ABCDEFGH\", \"AB\")", 1);
653 ExecuteExpectInt32("At(\"ABCDEFGH\", \"F\")", 6);
654 ExecuteExpectInt32("At(23412931298471, 29)", 5);
655}
656
658 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
659
660 ExecuteExpectString("Concat(\"ABC\", \"DEF\")", "ABCDEF");
661 ExecuteExpectString("Concat(\"Tony\", Space(1), \"Blue\")", "Tony Blue");
662 ExecuteExpectString("Concat(\"You owe \", WordNum(1154.67, 2), \".\")",
663 "You owe One Thousand One Hundred Fifty-four Dollars And "
664 "Sixty-seven Cents.");
665}
666
668 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
669
670 // HTML
671 ExecuteExpectString(R"(Decode("", "html"))", "");
672 ExecuteExpectString(R"(Decode("abc&Acirc;xyz", "html"))", "abc\xC3\x82xyz");
673 ExecuteExpectString(R"(Decode("abc&NoneSuchButVeryLongIndeed;", "html"))",
674 "abc");
675 ExecuteExpectString(R"(Decode("&#x0041;&AElig;&Aacute;", "html"))",
676 "A\xC3\x86\xC3\x81");
677 ExecuteExpectString(R"(Decode("xyz&#", "html"))", "xyz");
678 ExecuteExpectString(R"(Decode("|&zzzzzz;|", "html"))", "||");
679
680 // XML
681 ExecuteExpectString(R"(Decode("", "xml"))", "");
682 ExecuteExpectString(R"(Decode("~!@#$%%^&amp;*()_+|`", "xml"))",
683 "~!@#$%%^&*()_+|`");
684 ExecuteExpectString(R"(Decode("abc&nonesuchbutverylongindeed;", "xml"))",
685 "abc");
686 ExecuteExpectString(R"(Decode("&quot;&#x45;&lt;&gt;[].&apos;", "xml"))",
687 "\"E<>[].'");
688 ExecuteExpectString(R"(Decode("xyz&#", "xml"))", "xyz");
689 ExecuteExpectString(R"(Decode("|&zzzzzz;|", "xml"))", "||");
690
691 // URL
692 ExecuteExpectString(R"(Decode("", "url"))", "");
693 ExecuteExpectString(R"(Decode("~%26^&*()_+|`{", "url"))", "~&^&*()_+|`{");
694 ExecuteExpectString(R"(Decode("~%26^&*()_+|`{", "mbogo"))", "~&^&*()_+|`{");
695 ExecuteExpectString(R"(Decode("~%26^&*()_+|`{"))", "~&^&*()_+|`{");
696 ExecuteExpectString(R"(Decode("~%~~"))", "");
697 ExecuteExpectString(R"(Decode("?%f~"))", "");
698 ExecuteExpectString(R"(Decode("?%~"))", "");
699 ExecuteExpectString(R"(Decode("?%"))", "");
700}
701
703 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
704
705 ExecuteExpectString("Encode(\"X/~&^*<=>?|\")",
706 "X%2f%7e%26%5e*%3c%3d%3e%3f%7c");
707 ExecuteExpectString("Encode(\"X/~&^*<=>?|\", \"mbogo\")",
708 "X%2f%7e%26%5e*%3c%3d%3e%3f%7c");
709 ExecuteExpectString("Encode(\"X/~&^*<=>?|\", \"url\")",
710 "X%2f%7e%26%5e*%3c%3d%3e%3f%7c");
711 ExecuteExpectString("Encode(\"X/~&^*<=>?|\", \"xml\")",
712 "X/~&amp;^*&lt;=&gt;?|");
713 ExecuteExpectString("Encode(\"X/~&^*<=>?|\", \"html\")",
714 "X/~&amp;^*&lt;=&gt;?|");
715
716 ExecuteExpectString("Encode(\"\\u0022\\u00f5\\ufed0\", \"url\")",
717 "%22%f5%fe%d0");
718 ExecuteExpectString("Encode(\"\\u0022\\u00f4\\ufed0\", \"xml\")",
719 "&quot;&#xf4;&#xfed0;");
720 ExecuteExpectString("Encode(\"\\u0022\\u00f5\\ufed0\", \"html\")",
721 "&quot;&otilde;&#xfed0;");
722
723 ExecuteExpectString("Encode(\"\\uD83D\\uDCA9\", \"url\")", "%01%f4%a9");
724 ExecuteExpectString("Encode(\"\\uD83D\\uDCA9\", \"xml\")", "");
725 ExecuteExpectString("Encode(\"\\uD83D\\uDCA9\", \"html\")", "");
726}
727
729 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
730
731 ExecuteExpectString("Format(\"MMM D, YYYY\", \"20020901\")", "Sep 1, 2002");
732 ExecuteExpectString("Format(\"$9,999,999.99\", 1234567.89)", "$1,234,567.89");
733}
734
736 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
737
738 ExecuteExpectString("Left(\"ABCDEFGH\", 3)", "ABC");
739 ExecuteExpectString("Left(\"Tony Blue\", 5)", "Tony ");
740}
741
743 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
744
745 ExecuteExpectInt32("Len(\"ABCDEFGH\")", 8);
746 ExecuteExpectInt32("Len(4)", 1);
747 ExecuteExpectInt32("Len(Str(4.532, 6, 4))", 6);
748}
749
751 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
752
753 ExecuteExpectString("Lower(\"ABC\")", "abc");
754 ExecuteExpectString("Lower(\"21 Main St.\")", "21 main st.");
755 ExecuteExpectString("Lower(15)", "15");
756}
757
758// This is testing for an OOB read, so will likely only fail under ASAN.
760 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
761
762 const uint8_t test_string[] = {
763 0x4c, 0x6f, 0x77, 0x65, 0x72, 0x28, 0x22, 0xc3,
764 0x85, 0xc3, 0x85, 0xc3, 0x85, 0x22, 0x29}; // Lower("Ã…Ã…Ã…")
765 Execute(ByteStringView(pdfium::make_span(test_string)));
766}
767
769 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
770
771 ExecuteExpectString("Ltrim(\" ABCD\")", "ABCD");
772 ExecuteExpectString("Ltrim(Rtrim(\" Tony Blue \"))", "Tony Blue");
773}
774
776 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
777
778 ExecuteExpectString("Parse(\"MMM D, YYYY\", \"Sep 1, 2002\")", "2002-09-01");
779 ExecuteExpectFloat("Parse(\"$9,999,999.99\", \"$1,234,567.89\")",
780 1234567.89f);
781}
782
784 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
785
786 ExecuteExpectString("Replace(\"Tony Blue\", \"Tony\", \"Chris\")",
787 "Chris Blue");
788 ExecuteExpectString("Replace(\"ABCDEFGH\", \"D\")", "ABCEFGH");
789 ExecuteExpectString("Replace(\"ABCDEFGH\", \"d\")", "ABCDEFGH");
790}
791
793 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
794
795 ExecuteExpectString("Right(\"ABCDEFGH\", 3)", "FGH");
796 ExecuteExpectString("Right(\"Tony Blue\", 5)", " Blue");
797}
798
800 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
801
802 ExecuteExpectString("Rtrim(\"ABCD \")", "ABCD");
803 ExecuteExpectString("Rtrim(\"Tony Blue \t\")", "Tony Blue");
804}
805
807 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
808
809 ExecuteExpectString("Space(5)", " ");
810 ExecuteExpectString("Concat(\"Tony\", Space(1), \"Blue\")", "Tony Blue");
811
812 // Error cases.
813 ExecuteExpectError("Space(15654909)");
814 ExecuteExpectError("Space(99999999)");
815 ExecuteExpectError("Space()");
816 ExecuteExpectError("Space(1, 2)");
817 ExecuteExpectNull("Space( $)");
818}
819
821 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
822
823 ExecuteExpectString("Str(2.456)", " 2");
824 ExecuteExpectString("Str(4.532, 6, 4)", "4.5320");
825 ExecuteExpectString("Str(234.458, 4)", " 234");
826 ExecuteExpectString("Str(31.2345, 4, 2)", "****");
827
828 // Test maximum "n3" precision value.
829 ExecuteExpectString("Str(-765, 19, 14)", "-765.00000000000000");
830 ExecuteExpectString("Str(-765, 20, 15)", "-765.000000000000000");
831 ExecuteExpectString("Str(-765, 21, 16)", " -765.000000000000000");
832
833 // Error cases.
834 ExecuteExpectError("Str()");
835 ExecuteExpectError("Str(1, 2, 3, 4)");
836 ExecuteExpectError("Str(42, 15654909)");
837 ExecuteExpectNull("Str( $)");
838}
839
841 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
842
843 // Test wrong number of parameters.
844 ExecuteExpectError("Stuff(1, 2)");
845 ExecuteExpectError("Stuff(1, 2, 3, 4, 5)");
846
847 // Test null arguments.
848 ExecuteExpectNull("Stuff(null, 0, 4)");
849 ExecuteExpectNull("Stuff(\"ABCDEFG\", null, 4)");
850 ExecuteExpectNull("Stuff(\"ABCDEFG\", 0, null)");
851
852 // Insertions.
853 ExecuteExpectString("Stuff(\"\", 0, 0, \"clams\")", "clams");
854 ExecuteExpectString("Stuff(\"TonyBlue\", 5, 0, \" \")", "Tony Blue");
855
856 // Deletions.
857 ExecuteExpectString("Stuff(\"A\", 1, 0)", "A");
858 ExecuteExpectString("Stuff(\"A\", 1, 1)", "");
859 ExecuteExpectString("Stuff(\"ABCDEFGH\", 4, 2)", "ABCFGH");
860 ExecuteExpectString("Stuff(\"ABCDEFGH\", 7, 2)", "ABCDEF");
861
862 // Test index clamping.
863 ExecuteExpectString("Stuff(\"ABCDEFGH\", -400, 400)", "");
864
865 // Need significant amount of text to test start + count overflow due to
866 // intermediate float representation of count not being able to hold
867 // INT_MAX.
868 ExecuteExpectString(
869 "Stuff(\""
870 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678900"
871 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678900"
872 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678900"
873 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678900"
874 "\", 133, 2147483520)",
875 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678900"
876 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678900"
877 "abcd");
878}
879
881 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
882
883 // Test wrong number of parameters.
884 ExecuteExpectError("Substr()");
885 ExecuteExpectError("Substr(1)");
886 ExecuteExpectError("Substr(1, 2)");
887 ExecuteExpectError("Substr(1, 2, 3, 4)");
888
889 // Test null input.
890 ExecuteExpectNull("Substr(null, 0, 4)");
891 ExecuteExpectNull("Substr(\"ABCDEFG\", null, 4)");
892 ExecuteExpectNull("Substr(\"ABCDEFG\", 0, null)");
893 ExecuteExpectNull("Substr(null, null, 4)");
894 ExecuteExpectNull("Substr(null, 0, null)");
895 ExecuteExpectNull("Substr(\"ABCDEFG\", null, null)");
896 ExecuteExpectNull("Substr(null, null, null)");
897
898 ExecuteExpectString("Substr(\"ABCDEFG\", -1, 4)", "ABCD");
899 ExecuteExpectString("Substr(\"ABCDEFG\", 0, 4)", "ABCD");
900 ExecuteExpectString("Substr(\"ABCDEFG\", 3, 4)", "CDEF");
901 ExecuteExpectString("Substr(\"ABCDEFG\", 4, 4)", "DEFG");
902 ExecuteExpectString("Substr(\"ABCDEFG\", 5, 4)", "EFG");
903 ExecuteExpectString("Substr(\"ABCDEFG\", 6, 4)", "FG");
904 ExecuteExpectString("Substr(\"ABCDEFG\", 7, 4)", "G");
905 ExecuteExpectString("Substr(\"ABCDEFG\", 8, 4)", "");
906 ExecuteExpectString("Substr(\"ABCDEFG\", 5, -1)", "");
907 ExecuteExpectString("Substr(\"ABCDEFG\", 5, 0)", "");
908 ExecuteExpectString("Substr(\"ABCDEFG\", 5, 1)", "E");
909 ExecuteExpectString("Substr(\"abcdefghi\", 5, 3)", "efg");
910 ExecuteExpectString("Substr(3214, 2, 1)", "2");
911 ExecuteExpectString("Substr(\"21 Waterloo St.\", 4, 5)", "Water");
912}
913
915 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
916 EXPECT_TRUE(Execute("Uuid()"));
917
918 CFXJSE_ScopeUtil_IsolateHandleContext scope(GetJseContext());
919 v8::Local<v8::Value> value = GetValue();
920 ASSERT_TRUE(fxv8::IsString(value));
921 ByteString bstr = fxv8::ToByteString(isolate(), value.As<v8::String>());
922 EXPECT_EQ(bstr.GetLength(), 32u);
923 EXPECT_TRUE(std::all_of(bstr.begin(), bstr.end(), FXSYS_IsHexDigit));
924 EXPECT_TRUE(
925 std::any_of(bstr.begin(), bstr.end(), [](char c) { return c != '0'; }));
926}
927
929 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
930
931 ExecuteExpectString("Upper(\"abc\")", "ABC");
932 ExecuteExpectString("Upper(\"21 Main St.\")", "21 MAIN ST.");
933 ExecuteExpectString("Upper(15)", "15");
934}
935
937 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
938
939 // Wrong number of parameters.
940 ExecuteExpectError("WordNum()");
941 ExecuteExpectError("WordNum(1, 2, 3, 4)");
942
943 // Normal format codes.
944 ExecuteExpectString("WordNum(123.45)", "One Hundred Twenty-three");
945 ExecuteExpectString("WordNum(123.45, 0)", "One Hundred Twenty-three");
946 ExecuteExpectString("WordNum(123.45, 1)", "One Hundred Twenty-three Dollars");
947 ExecuteExpectString("WordNum(123.45, 2)",
948 "One Hundred Twenty-three Dollars And Forty-five Cents");
949
950 // Invalid format code.
951 ExecuteExpectString("WordNum(123.45, -1)", "");
952 ExecuteExpectString("WordNum(123.45, 3)", "");
953
954 // Locale string is ignored.
955 ExecuteExpectString("WordNum(123.45, 0, \"zh_CN\")",
956 "One Hundred Twenty-three");
957
958 // Zero (and near zero) values.
959 ExecuteExpectString("WordNum(0, 0)", "Zero");
960 ExecuteExpectString("WordNum(0, 1)", "Zero Dollars");
961 ExecuteExpectString("WordNum(0, 2)", "Zero Dollars And Zero Cents");
962 ExecuteExpectString("WordNum(0.12, 0)", "Zero");
963 ExecuteExpectString("WordNum(0.12, 1)", "Zero Dollars");
964 ExecuteExpectString("WordNum(0.12, 2)", "Zero Dollars And Twelve Cents");
965
966 // Negative values.
967 ExecuteExpectString("WordNum(-1, 0)", "*");
968 ExecuteExpectString("WordNum(-1, 1)", "*");
969 ExecuteExpectString("WordNum(-1, 2)", "*");
970
971 // Test larger values
972 // TODO(tsepez): check on "Thousand Zero"
973 ExecuteExpectString("WordNum(1.234e+6)",
974 "One Million Two Hundred Thirty-four Thousand Zero");
975
976 // TODO(tsepez): check on "Zero Thousand Zero"
977 ExecuteExpectString(
978 "WordNum(1.234e+9)",
979 "One Billion Two Hundred Thirty-four Million Zero Thousand Zero");
980
981 // TODO(tsepez): check on "Zero Million"
982 ExecuteExpectString(
983 "WordNum(1.234e+12)",
984 "One Trillion Two Hundred Thirty-four Billion Zero Million Nineteen "
985 "Thousand Four Hundred Fifty-six");
986
987 ExecuteExpectString(
988 "WordNum(1.234e+15)",
989 "One Thousand Two Hundred Thirty-three Trillion Nine Hundred Ninety-nine "
990 "Billion Nine Hundred Thirty-eight Million Seven Hundred Fifteen "
991 "Thousand "
992 "Six Hundred Forty-eight");
993
994 // Out-of-range.
995 ExecuteExpectString("WordNum(1.234e+18)", "*");
996 ExecuteExpectString("WordNum(1.234e+21)", "*");
997 ExecuteExpectString("WordNum(1.234e+24)", "*");
998 ExecuteExpectString("WordNum(1.234e+30)", "*");
999}
1000
1002 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1003 ExecuteExpectString("Get(\"https://example.com\")", "<body>secrets</body>");
1004}
1005
1007 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1008 ExecuteExpectString(
1009 "Post(\"http://example.com\", \"secret stuff\", \"text/plain\")",
1010 "posted");
1011}
1012
1014 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1015 ExecuteExpectString("Put(\"http://example.com\", \"secret stuff\")", "");
1016}
1017
1019 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1020
1021 EXPECT_FALSE(ExecuteSilenceFailure("F()"));
1022 EXPECT_FALSE(ExecuteSilenceFailure("()"));
1023 EXPECT_FALSE(ExecuteSilenceFailure("()()()"));
1024 EXPECT_FALSE(ExecuteSilenceFailure("Round(2.0)()"));
1025}
1026
1028 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1029
1030 const char test[] = {"$form.form1.TextField11.getAttribute(\"h\")"};
1031 ExecuteExpectString(test, "12.7mm");
1032}
1033
1035 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1036
1038 params.m_wsChange = L"changed";
1039
1040 CFXJSE_Engine* context = GetScriptContext();
1041 CFXJSE_Engine::EventParamScope event_scope(context, nullptr, &params);
1042
1043 const char test[] = {"xfa.event.change"};
1044 ExecuteExpectString(test, "changed");
1045}
1046
1048 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1049
1051 CFXJSE_Engine* context = GetScriptContext();
1052 CFXJSE_Engine::EventParamScope event_scope(context, nullptr, &params);
1053
1054 const char test[] = {"xfa.event.change = \"changed\""};
1055 EXPECT_TRUE(Execute(test));
1056 EXPECT_EQ(L"changed", params.m_wsChange);
1057}
1058
1060 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1061
1063 params.m_wsFullText = L"Original Full Text";
1064
1065 CFXJSE_Engine* context = GetScriptContext();
1066 CFXJSE_Engine::EventParamScope event_scope(context, nullptr, &params);
1067
1068 const char test[] = {"xfa.event.fullText = \"Changed Full Text\""};
1069 EXPECT_TRUE(Execute(test));
1070 EXPECT_EQ(L"Original Full Text", params.m_wsFullText);
1071}
1072
1074 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1075
1077 params.m_wsPrevText = L"1234";
1078 params.m_iSelStart = 1;
1079 params.m_iSelEnd = 3;
1080
1081 CFXJSE_Engine* context = GetScriptContext();
1082 CFXJSE_Engine::EventParamScope event_scope(context, nullptr, &params);
1083
1084 // Moving end to start works fine.
1085 EXPECT_TRUE(Execute("xfa.event.selEnd = \"1\""));
1086 EXPECT_EQ(1, params.m_iSelStart);
1087 EXPECT_EQ(1, params.m_iSelEnd);
1088
1089 // Moving end before end, forces start to move in response.
1090 EXPECT_TRUE(Execute("xfa.event.selEnd = \"0\""));
1091 EXPECT_EQ(0, params.m_iSelStart);
1092 EXPECT_EQ(0, params.m_iSelEnd);
1093
1094 // Negatives not allowed
1095 EXPECT_TRUE(Execute("xfa.event.selEnd = \"-1\""));
1096 EXPECT_EQ(0, params.m_iSelStart);
1097 EXPECT_EQ(0, params.m_iSelEnd);
1098
1099 // Negatives not allowed
1100 EXPECT_TRUE(Execute("xfa.event.selStart = \"-1\""));
1101 EXPECT_EQ(0, params.m_iSelStart);
1102 EXPECT_EQ(0, params.m_iSelEnd);
1103
1104 params.m_iSelEnd = 1;
1105
1106 // Moving start to end works fine.
1107 EXPECT_TRUE(Execute("xfa.event.selStart = \"1\""));
1108 EXPECT_EQ(1, params.m_iSelStart);
1109 EXPECT_EQ(1, params.m_iSelEnd);
1110
1111 // Moving start after end moves end.
1112 EXPECT_TRUE(Execute("xfa.event.selStart = \"2\""));
1113 EXPECT_EQ(2, params.m_iSelStart);
1114 EXPECT_EQ(2, params.m_iSelEnd);
1115
1116 // Setting End past end of string clamps to string length;
1117 EXPECT_TRUE(Execute("xfa.event.selEnd = \"20\""));
1118 EXPECT_EQ(2, params.m_iSelStart);
1119 EXPECT_EQ(4, params.m_iSelEnd);
1120
1121 // Setting Start past end of string clamps to string length;
1122 EXPECT_TRUE(Execute("xfa.event.selStart = \"20\""));
1123 EXPECT_EQ(4, params.m_iSelStart);
1124 EXPECT_EQ(4, params.m_iSelEnd);
1125}
1126
1128 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1129
1131 params.m_bCancelAction = false;
1132
1133 CFXJSE_Engine* context = GetScriptContext();
1134 CFXJSE_Engine::EventParamScope event_scope(context, nullptr, &params);
1135 ExecuteExpectBool("xfa.event.cancelAction", false);
1136 EXPECT_TRUE(Execute("xfa.event.cancelAction = \"true\""));
1137 EXPECT_TRUE(params.m_bCancelAction);
1138}
1139
1141 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1142
1144 params.m_wsChange = L"g";
1145 params.m_wsPrevText = L"abcd";
1146 params.m_iSelStart = 1;
1147 params.m_iSelEnd = 3;
1148
1149 CFXJSE_Engine* context = GetScriptContext();
1150 CFXJSE_Engine::EventParamScope event_scope(context, nullptr, &params);
1151
1152 EXPECT_EQ(L"abcd", params.m_wsPrevText);
1153 EXPECT_EQ(L"agd", params.GetNewText());
1154 EXPECT_EQ(L"g", params.m_wsChange);
1155 EXPECT_EQ(1, params.m_iSelStart);
1156 EXPECT_EQ(3, params.m_iSelEnd);
1157
1158 const char change_event[] = {"xfa.event.change = \"xyz\""};
1159 EXPECT_TRUE(Execute(change_event));
1160
1161 EXPECT_EQ(L"abcd", params.m_wsPrevText);
1162 EXPECT_EQ(L"xyz", params.m_wsChange);
1163 EXPECT_EQ(L"axyzd", params.GetNewText());
1164 EXPECT_EQ(1, params.m_iSelStart);
1165 EXPECT_EQ(3, params.m_iSelEnd);
1166
1167 const char sel_event[] = {"xfa.event.selEnd = \"1\""};
1168 EXPECT_TRUE(Execute(sel_event));
1169
1170 EXPECT_EQ(L"abcd", params.m_wsPrevText);
1171 EXPECT_EQ(L"xyz", params.m_wsChange);
1172 EXPECT_EQ(L"axyzbcd", params.GetNewText());
1173 EXPECT_EQ(1, params.m_iSelStart);
1174 EXPECT_EQ(1, params.m_iSelEnd);
1175}
1176
1177// Should not crash.
1179 ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
1180 EXPECT_TRUE(Execute("!.somExpression=0"));
1181}
fxcrt::ByteString ByteString
Definition bytestring.h:180
TEST_F(CFXJSEFormCalcContextEmbedderTest, TranslateEmpty)
void ExecuteExpectInt32(ByteStringView input, int32_t expected)
void ExecuteExpectFloat(ByteStringView input, float expected)
~CFXJSEFormCalcContextEmbedderTest() override=default
void ExecuteExpectBool(ByteStringView input, bool expected)
void ExecuteExpectString(ByteStringView input, const char *expected)
void ExecuteExpectFloatNear(ByteStringView input, float expected)
EventParamScope(CFXJSE_Engine *pEngine, CXFA_Node *pTarget, CXFA_EventParam *pEventParam)
friend class EventParamScope
CFXJSE_ScopeUtil_IsolateHandleContext(CFXJSE_Context *pContext)
WideString GetNewText() const
CXFA_EventParam(XFA_EVENTTYPE type)
bool Execute(ByteStringView input)
CFXJSE_Engine * GetScriptContext() const
@ XFA_EVENT_Unknown
bool FXSYS_IsHexDigit(char c)
fxcrt::ByteStringView ByteStringView