summaryrefslogtreecommitdiff
path: root/fxjs/cjs_publicmethods_embeddertest.cpp
blob: 9413aa6a461114474b27e5bac3711a3e5153e75b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright 2015 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <cmath>
#include <vector>

#include "core/fxcrt/fx_string.h"
#include "fxjs/cjs_event_context.h"
#include "fxjs/cjs_publicmethods.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/js_embedder_test.h"

namespace {

double RoundDownDate(double date) {
  return date - fmod(date, 86400000);
}

}  // namespace

class CJS_PublicMethodsEmbedderTest : public JSEmbedderTest {};

TEST_F(CJS_PublicMethodsEmbedderTest, MakeRegularDate) {
  v8::Isolate::Scope isolate_scope(isolate());
  v8::HandleScope handle_scope(isolate());
  v8::Context::Scope context_scope(GetV8Context());
  bool bWrongFormat;
  double date;

  // 1968
  bWrongFormat = false;
  date = CJS_PublicMethods::MakeRegularDate(L"06/25/1968", L"mm/dd/yyyy",
                                            &bWrongFormat);
  date = RoundDownDate(date);
  EXPECT_DOUBLE_EQ(-47865600000, date);
  EXPECT_FALSE(bWrongFormat);

  // 1968
  bWrongFormat = false;
  date = CJS_PublicMethods::MakeRegularDate(L"25061968", L"ddmmyyyy",
                                            &bWrongFormat);
  date = RoundDownDate(date);
  EXPECT_DOUBLE_EQ(-47865600000, date);
  EXPECT_FALSE(bWrongFormat);

  // 1968
  bWrongFormat = false;
  date = CJS_PublicMethods::MakeRegularDate(L"19680625", L"yyyymmdd",
                                            &bWrongFormat);
  date = RoundDownDate(date);
  EXPECT_DOUBLE_EQ(-47865600000, date);
  EXPECT_FALSE(bWrongFormat);

  // 1985
  bWrongFormat = false;
  date = CJS_PublicMethods::MakeRegularDate(L"31121985", L"ddmmyyyy",
                                            &bWrongFormat);
  date = RoundDownDate(date);
  EXPECT_DOUBLE_EQ(504835200000.0, date);
  EXPECT_FALSE(bWrongFormat);

  // 2085, the other '85.
  bWrongFormat = false;
  date =
      CJS_PublicMethods::MakeRegularDate(L"311285", L"ddmmyy", &bWrongFormat);
  date = RoundDownDate(date);
  EXPECT_DOUBLE_EQ(3660595200000.0, date);
  EXPECT_FALSE(bWrongFormat);

  // 1995
  bWrongFormat = false;
  date = CJS_PublicMethods::MakeRegularDate(L"01021995", L"ddmmyyyy",
                                            &bWrongFormat);
  date = RoundDownDate(date);
  EXPECT_DOUBLE_EQ(791596800000.0, date);
  EXPECT_FALSE(bWrongFormat);

  // 2095, the other '95.
  bWrongFormat = false;
  date =
      CJS_PublicMethods::MakeRegularDate(L"010295", L"ddmmyy", &bWrongFormat);
  date = RoundDownDate(date);
  EXPECT_DOUBLE_EQ(3947356800000.0, date);
  EXPECT_FALSE(bWrongFormat);

  // 2005
  bWrongFormat = false;
  date = CJS_PublicMethods::MakeRegularDate(L"01022005", L"ddmmyyyy",
                                            &bWrongFormat);
  date = RoundDownDate(date);
  EXPECT_DOUBLE_EQ(1107216000000.0, date);
  EXPECT_FALSE(bWrongFormat);

  // 2005
  bWrongFormat = false;
  date =
      CJS_PublicMethods::MakeRegularDate(L"010205", L"ddmmyy", &bWrongFormat);
  date = RoundDownDate(date);
  EXPECT_DOUBLE_EQ(1107216000000.0, date);
  EXPECT_FALSE(bWrongFormat);

  // 2005 in a different format. https://crbug.com/436572
  bWrongFormat = false;
  date =
      CJS_PublicMethods::MakeRegularDate(L"050201", L"yymmdd", &bWrongFormat);
  date = RoundDownDate(date);
  EXPECT_DOUBLE_EQ(1107216000000.0, date);
  EXPECT_FALSE(bWrongFormat);
}

TEST_F(CJS_PublicMethodsEmbedderTest, MakeFormatDate) {
  v8::Isolate::Scope isolate_scope(isolate());
  v8::HandleScope handle_scope(isolate());
  v8::Context::Scope context_scope(GetV8Context());
  WideString formatted_date;

  // 1968-06-25
  formatted_date = CJS_PublicMethods::MakeFormatDate(-47952000000, L"ddmmyy");
  EXPECT_STREQ(L"250668", formatted_date.c_str());
  formatted_date = CJS_PublicMethods::MakeFormatDate(-47952000000, L"yy/mm/dd");
  EXPECT_STREQ(L"68/06/25", formatted_date.c_str());

  // 1969-12-31
  formatted_date = CJS_PublicMethods::MakeFormatDate(-0.0001, L"ddmmyy");
  EXPECT_STREQ(L"311269", formatted_date.c_str());
  formatted_date = CJS_PublicMethods::MakeFormatDate(-0.0001, L"yy!mmdd");
  EXPECT_STREQ(L"69!1231", formatted_date.c_str());

  // 1970-01-01
  formatted_date = CJS_PublicMethods::MakeFormatDate(0, L"ddmmyy");
  EXPECT_STREQ(L"010170", formatted_date.c_str());
  formatted_date = CJS_PublicMethods::MakeFormatDate(0, L"mm-yyyy-dd");
  EXPECT_STREQ(L"01-1970-01", formatted_date.c_str());

  // 1985-12-31
  formatted_date = CJS_PublicMethods::MakeFormatDate(504835200000.0, L"ddmmyy");
  EXPECT_STREQ(L"311285", formatted_date.c_str());
  formatted_date = CJS_PublicMethods::MakeFormatDate(504835200000.0, L"yymmdd");
  EXPECT_STREQ(L"851231", formatted_date.c_str());

  // 1995-02-01
  formatted_date = CJS_PublicMethods::MakeFormatDate(791596800000.0, L"ddmmyy");
  EXPECT_STREQ(L"010295", formatted_date.c_str());
  formatted_date =
      CJS_PublicMethods::MakeFormatDate(791596800000.0, L"yyyymmdd");
  EXPECT_STREQ(L"19950201", formatted_date.c_str());

  // 2005-02-01
  formatted_date =
      CJS_PublicMethods::MakeFormatDate(1107216000000.0, L"ddmmyy");
  EXPECT_STREQ(L"010205", formatted_date.c_str());
  formatted_date =
      CJS_PublicMethods::MakeFormatDate(1107216000000.0, L"yyyyddmm");
  EXPECT_STREQ(L"20050102", formatted_date.c_str());

  // 2085-12-31
  formatted_date =
      CJS_PublicMethods::MakeFormatDate(3660595200000.0, L"ddmmyy");
  EXPECT_STREQ(L"311285", formatted_date.c_str());
  formatted_date =
      CJS_PublicMethods::MakeFormatDate(3660595200000.0, L"yyyydd");
  EXPECT_STREQ(L"208531", formatted_date.c_str());

  // 2095-02-01
  formatted_date =
      CJS_PublicMethods::MakeFormatDate(3947356800000.0, L"ddmmyy");
  EXPECT_STREQ(L"010295", formatted_date.c_str());
  formatted_date =
      CJS_PublicMethods::MakeFormatDate(3947356800000.0, L"mmddyyyy");
  EXPECT_STREQ(L"02012095", formatted_date.c_str());
}

TEST_F(CJS_PublicMethodsEmbedderTest, AFSimple_CalculateSum) {
  v8::Isolate::Scope isolate_scope(isolate());
  v8::HandleScope handle_scope(isolate());
  v8::Context::Scope context_scope(GetV8Context());

  EXPECT_TRUE(OpenDocument("calculate.pdf"));
  auto* page = LoadPage(0);
  ASSERT_TRUE(page);

  CJS_Runtime runtime(static_cast<CPDFSDK_FormFillEnvironment*>(form_handle()));
  runtime.NewEventContext();

  WideString result;
  runtime.GetCurrentEventContext()->GetEventHandler()->m_pValue = &result;

  auto ary = runtime.NewArray();

  runtime.PutArrayElement(ary, 0, runtime.NewString(L"Calc1_A"));
  runtime.PutArrayElement(ary, 1, runtime.NewString(L"Calc1_B"));

  std::vector<v8::Local<v8::Value>> params;
  params.push_back(runtime.NewString("SUM"));
  params.push_back(ary);

  CJS_Return ret = CJS_PublicMethods::AFSimple_Calculate(&runtime, params);
  UnloadPage(page);

  runtime.GetCurrentEventContext()->GetEventHandler()->m_pValue = nullptr;

  ASSERT_TRUE(!ret.HasError());
  ASSERT_TRUE(!ret.HasReturn());
  ASSERT_EQ(L"7", result);
}

TEST_F(CJS_PublicMethodsEmbedderTest, AFNumber_Keystroke) {
  v8::Isolate::Scope isolate_scope(isolate());
  v8::HandleScope handle_scope(isolate());
  v8::Context::Scope context_scope(GetV8Context());

  EXPECT_TRUE(OpenDocument("calculate.pdf"));
  auto* page = LoadPage(0);
  ASSERT_TRUE(page);

  CJS_Runtime runtime(static_cast<CPDFSDK_FormFillEnvironment*>(form_handle()));
  runtime.NewEventContext();

  auto* handler = runtime.GetCurrentEventContext()->GetEventHandler();

  bool valid = true;
  WideString result = L"-10";
  WideString change = L"";

  handler->m_pValue = &result;
  handler->m_pbRc = &valid;
  handler->m_pWideStrChange = &change;

  handler->m_bWillCommit = 0;
  handler->SetSelStart(0);
  handler->SetSelEnd(0);

  std::vector<v8::Local<v8::Value>> params;
  params.push_back(runtime.NewString(L"-10"));
  params.push_back(runtime.NewString(L""));

  CJS_Return ret = CJS_PublicMethods::AFNumber_Keystroke(&runtime, params);
  EXPECT_TRUE(valid);
  EXPECT_TRUE(!ret.HasError());
  EXPECT_TRUE(!ret.HasReturn());

  UnloadPage(page);

  // Keep the *SAN bots happy. One of these is an UnownedPtr, another seems to
  // used during destruction. Clear them all to be safe and consistent.
  handler->m_pValue = nullptr;
  handler->m_pbRc = nullptr;
  handler->m_pWideStrChange = nullptr;
}