Home | History | Annotate | Download | only in javascript
      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   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 =
     67       CJS_PublicMethods::MakeRegularDate(L"311285", L"ddmmyy", &bWrongFormat);
     68   date = RoundDownDate(date);
     69   EXPECT_DOUBLE_EQ(3660595200000.0, date);
     70   EXPECT_FALSE(bWrongFormat);
     71 
     72   // 1995
     73   bWrongFormat = false;
     74   date = CJS_PublicMethods::MakeRegularDate(L"01021995", L"ddmmyyyy",
     75                                             &bWrongFormat);
     76   date = RoundDownDate(date);
     77   EXPECT_DOUBLE_EQ(791596800000.0, date);
     78   EXPECT_FALSE(bWrongFormat);
     79 
     80   // 2095, the other '95.
     81   bWrongFormat = false;
     82   date =
     83       CJS_PublicMethods::MakeRegularDate(L"010295", L"ddmmyy", &bWrongFormat);
     84   date = RoundDownDate(date);
     85   EXPECT_DOUBLE_EQ(3947356800000.0, date);
     86   EXPECT_FALSE(bWrongFormat);
     87 
     88   // 2005
     89   bWrongFormat = false;
     90   date = CJS_PublicMethods::MakeRegularDate(L"01022005", L"ddmmyyyy",
     91                                             &bWrongFormat);
     92   date = RoundDownDate(date);
     93   EXPECT_DOUBLE_EQ(1107216000000.0, date);
     94   EXPECT_FALSE(bWrongFormat);
     95 
     96   // 2005
     97   bWrongFormat = false;
     98   date =
     99       CJS_PublicMethods::MakeRegularDate(L"010205", L"ddmmyy", &bWrongFormat);
    100   date = RoundDownDate(date);
    101   EXPECT_DOUBLE_EQ(1107216000000.0, date);
    102   EXPECT_FALSE(bWrongFormat);
    103 }
    104 
    105 TEST_F(PublicMethodsEmbedderTest, MakeFormatDate) {
    106   v8::Isolate::Scope isolate_scope(isolate());
    107 #ifdef PDF_ENABLE_XFA
    108   v8::Locker locker(isolate());
    109 #endif  // PDF_ENABLE_XFA
    110   v8::HandleScope handle_scope(isolate());
    111   v8::Context::Scope context_scope(GetV8Context());
    112   CFX_WideString formatted_date;
    113 
    114   // 1968-06-25
    115   formatted_date = CJS_PublicMethods::MakeFormatDate(-47952000000, L"ddmmyy");
    116   EXPECT_STREQ(L"250668", formatted_date);
    117   formatted_date = CJS_PublicMethods::MakeFormatDate(-47952000000, L"yy/mm/dd");
    118   EXPECT_STREQ(L"68/06/25", formatted_date);
    119 
    120   // 1969-12-31
    121   formatted_date = CJS_PublicMethods::MakeFormatDate(-0.0001, L"ddmmyy");
    122   EXPECT_STREQ(L"311269", formatted_date);
    123   formatted_date = CJS_PublicMethods::MakeFormatDate(-0.0001, L"yy!mmdd");
    124   EXPECT_STREQ(L"69!1231", formatted_date);
    125 
    126   // 1970-01-01
    127   formatted_date = CJS_PublicMethods::MakeFormatDate(0, L"ddmmyy");
    128   EXPECT_STREQ(L"010170", formatted_date);
    129   formatted_date = CJS_PublicMethods::MakeFormatDate(0, L"mm-yyyy-dd");
    130   EXPECT_STREQ(L"01-1970-01", formatted_date);
    131 
    132   // 1985-12-31
    133   formatted_date = CJS_PublicMethods::MakeFormatDate(504835200000.0, L"ddmmyy");
    134   EXPECT_STREQ(L"311285", formatted_date);
    135   formatted_date = CJS_PublicMethods::MakeFormatDate(504835200000.0, L"yymmdd");
    136   EXPECT_STREQ(L"851231", formatted_date);
    137 
    138   // 1995-02-01
    139   formatted_date = CJS_PublicMethods::MakeFormatDate(791596800000.0, L"ddmmyy");
    140   EXPECT_STREQ(L"010295", formatted_date);
    141   formatted_date =
    142       CJS_PublicMethods::MakeFormatDate(791596800000.0, L"yyyymmdd");
    143   EXPECT_STREQ(L"19950201", formatted_date);
    144 
    145   // 2005-02-01
    146   formatted_date =
    147       CJS_PublicMethods::MakeFormatDate(1107216000000.0, L"ddmmyy");
    148   EXPECT_STREQ(L"010205", formatted_date);
    149   formatted_date =
    150       CJS_PublicMethods::MakeFormatDate(1107216000000.0, L"yyyyddmm");
    151   EXPECT_STREQ(L"20050102", formatted_date);
    152 
    153   // 2085-12-31
    154   formatted_date =
    155       CJS_PublicMethods::MakeFormatDate(3660595200000.0, L"ddmmyy");
    156   EXPECT_STREQ(L"311285", formatted_date);
    157   formatted_date =
    158       CJS_PublicMethods::MakeFormatDate(3660595200000.0, L"yyyydd");
    159   EXPECT_STREQ(L"208531", formatted_date);
    160 
    161   // 2095-02-01
    162   formatted_date =
    163       CJS_PublicMethods::MakeFormatDate(3947356800000.0, L"ddmmyy");
    164   EXPECT_STREQ(L"010295", formatted_date);
    165   formatted_date =
    166       CJS_PublicMethods::MakeFormatDate(3947356800000.0, L"mmddyyyy");
    167   EXPECT_STREQ(L"02012095", formatted_date);
    168 }
    169