OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 <cmath> |
| 6 |
| 7 #include "core/include/fxcrt/fx_string.h" |
| 8 #include "fpdfsdk/src/javascript/PublicMethods.h" |
| 9 #include "testing/js_embedder_test.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace { |
| 13 |
| 14 double RoundDownDate(double date) { |
| 15 return date - fmod(date, 86400000); |
| 16 } |
| 17 |
| 18 } // namespace |
| 19 |
| 20 class PublicMethodsEmbedderTest : public JSEmbedderTest {}; |
| 21 |
| 22 TEST_F(PublicMethodsEmbedderTest, MakeRegularDate) { |
| 23 v8::Isolate::Scope isolate_scope(isolate()); |
| 24 #ifdef PDF_ENABLE_XFA |
| 25 v8::Locker locker(isolate()); |
| 26 #endif // PDF_ENABLE_XFA |
| 27 v8::HandleScope handle_scope(isolate()); |
| 28 v8::Context::Scope context_scope(GetV8Context()); |
| 29 FX_BOOL bWrongFormat; |
| 30 double date; |
| 31 |
| 32 // 1968 |
| 33 bWrongFormat = false; |
| 34 date = CJS_PublicMethods::MakeRegularDate(L"06/25/1968", L"mm/dd/yyyy", |
| 35 bWrongFormat); |
| 36 date = RoundDownDate(date); |
| 37 EXPECT_DOUBLE_EQ(-47865600000, date); |
| 38 EXPECT_FALSE(bWrongFormat); |
| 39 |
| 40 // 1968 |
| 41 bWrongFormat = false; |
| 42 date = CJS_PublicMethods::MakeRegularDate(L"25061968", L"ddmmyyyy", |
| 43 bWrongFormat); |
| 44 date = RoundDownDate(date); |
| 45 EXPECT_DOUBLE_EQ(-47865600000, date); |
| 46 EXPECT_FALSE(bWrongFormat); |
| 47 |
| 48 // 1968 |
| 49 bWrongFormat = false; |
| 50 date = CJS_PublicMethods::MakeRegularDate(L"19680625", L"yyyymmdd", |
| 51 bWrongFormat); |
| 52 date = RoundDownDate(date); |
| 53 EXPECT_DOUBLE_EQ(-47865600000, date); |
| 54 EXPECT_FALSE(bWrongFormat); |
| 55 |
| 56 // 1985 |
| 57 bWrongFormat = false; |
| 58 date = CJS_PublicMethods::MakeRegularDate(L"31121985", L"ddmmyyyy", |
| 59 bWrongFormat); |
| 60 date = RoundDownDate(date); |
| 61 EXPECT_DOUBLE_EQ(504835200000.0, date); |
| 62 EXPECT_FALSE(bWrongFormat); |
| 63 |
| 64 // 2085, the other '85. |
| 65 bWrongFormat = false; |
| 66 date = CJS_PublicMethods::MakeRegularDate(L"311285", L"ddmmyy", bWrongFormat); |
| 67 date = RoundDownDate(date); |
| 68 EXPECT_DOUBLE_EQ(3660595200000.0, date); |
| 69 EXPECT_FALSE(bWrongFormat); |
| 70 |
| 71 // 1995 |
| 72 bWrongFormat = false; |
| 73 date = CJS_PublicMethods::MakeRegularDate(L"01021995", L"ddmmyyyy", |
| 74 bWrongFormat); |
| 75 date = RoundDownDate(date); |
| 76 EXPECT_DOUBLE_EQ(791596800000.0, date); |
| 77 EXPECT_FALSE(bWrongFormat); |
| 78 |
| 79 // 2095, the other '95. |
| 80 bWrongFormat = false; |
| 81 date = CJS_PublicMethods::MakeRegularDate(L"010295", L"ddmmyy", bWrongFormat); |
| 82 date = RoundDownDate(date); |
| 83 EXPECT_DOUBLE_EQ(3947356800000.0, date); |
| 84 EXPECT_FALSE(bWrongFormat); |
| 85 |
| 86 // 2005 |
| 87 bWrongFormat = false; |
| 88 date = CJS_PublicMethods::MakeRegularDate(L"01022005", L"ddmmyyyy", |
| 89 bWrongFormat); |
| 90 date = RoundDownDate(date); |
| 91 EXPECT_DOUBLE_EQ(1107216000000.0, date); |
| 92 EXPECT_FALSE(bWrongFormat); |
| 93 |
| 94 // 2005 |
| 95 bWrongFormat = false; |
| 96 date = CJS_PublicMethods::MakeRegularDate(L"010205", L"ddmmyy", bWrongFormat); |
| 97 date = RoundDownDate(date); |
| 98 EXPECT_DOUBLE_EQ(1107216000000.0, date); |
| 99 EXPECT_FALSE(bWrongFormat); |
| 100 } |
| 101 |
| 102 TEST_F(PublicMethodsEmbedderTest, MakeFormatDate) { |
| 103 v8::Isolate::Scope isolate_scope(isolate()); |
| 104 #ifdef PDF_ENABLE_XFA |
| 105 v8::Locker locker(isolate()); |
| 106 #endif // PDF_ENABLE_XFA |
| 107 v8::HandleScope handle_scope(isolate()); |
| 108 v8::Context::Scope context_scope(GetV8Context()); |
| 109 CFX_WideString formatted_date; |
| 110 |
| 111 // 1968-06-25 |
| 112 formatted_date = CJS_PublicMethods::MakeFormatDate(-47952000000, L"ddmmyy"); |
| 113 EXPECT_STREQ(L"250668", formatted_date); |
| 114 formatted_date = CJS_PublicMethods::MakeFormatDate(-47952000000, L"yy/mm/dd"); |
| 115 EXPECT_STREQ(L"68/06/25", formatted_date); |
| 116 |
| 117 // 1969-12-31 |
| 118 formatted_date = CJS_PublicMethods::MakeFormatDate(-0.0001, L"ddmmyy"); |
| 119 EXPECT_STREQ(L"311269", formatted_date); |
| 120 formatted_date = CJS_PublicMethods::MakeFormatDate(-0.0001, L"yy!mmdd"); |
| 121 EXPECT_STREQ(L"69!1231", formatted_date); |
| 122 |
| 123 // 1970-01-01 |
| 124 formatted_date = CJS_PublicMethods::MakeFormatDate(0, L"ddmmyy"); |
| 125 EXPECT_STREQ(L"010170", formatted_date); |
| 126 formatted_date = CJS_PublicMethods::MakeFormatDate(0, L"mm-yyyy-dd"); |
| 127 EXPECT_STREQ(L"01-1970-01", formatted_date); |
| 128 |
| 129 // 1985-12-31 |
| 130 formatted_date = CJS_PublicMethods::MakeFormatDate(504835200000.0, L"ddmmyy"); |
| 131 EXPECT_STREQ(L"311285", formatted_date); |
| 132 formatted_date = CJS_PublicMethods::MakeFormatDate(504835200000.0, L"yymmdd"); |
| 133 EXPECT_STREQ(L"851231", formatted_date); |
| 134 |
| 135 // 1995-02-01 |
| 136 formatted_date = CJS_PublicMethods::MakeFormatDate(791596800000.0, L"ddmmyy"); |
| 137 EXPECT_STREQ(L"010295", formatted_date); |
| 138 formatted_date = |
| 139 CJS_PublicMethods::MakeFormatDate(791596800000.0, L"yyyymmdd"); |
| 140 EXPECT_STREQ(L"19950201", formatted_date); |
| 141 |
| 142 // 2005-02-01 |
| 143 formatted_date = |
| 144 CJS_PublicMethods::MakeFormatDate(1107216000000.0, L"ddmmyy"); |
| 145 EXPECT_STREQ(L"010205", formatted_date); |
| 146 formatted_date = |
| 147 CJS_PublicMethods::MakeFormatDate(1107216000000.0, L"yyyyddmm"); |
| 148 EXPECT_STREQ(L"20050102", formatted_date); |
| 149 |
| 150 // 2085-12-31 |
| 151 formatted_date = |
| 152 CJS_PublicMethods::MakeFormatDate(3660595200000.0, L"ddmmyy"); |
| 153 EXPECT_STREQ(L"311285", formatted_date); |
| 154 formatted_date = |
| 155 CJS_PublicMethods::MakeFormatDate(3660595200000.0, L"yyyydd"); |
| 156 EXPECT_STREQ(L"208531", formatted_date); |
| 157 |
| 158 // 2095-02-01 |
| 159 formatted_date = |
| 160 CJS_PublicMethods::MakeFormatDate(3947356800000.0, L"ddmmyy"); |
| 161 EXPECT_STREQ(L"010295", formatted_date); |
| 162 formatted_date = |
| 163 CJS_PublicMethods::MakeFormatDate(3947356800000.0, L"mmddyyyy"); |
| 164 EXPECT_STREQ(L"02012095", formatted_date); |
| 165 } |
OLD | NEW |