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