blob: 8121701838e97e585f5c2e75e00200f288fac999 [file] [log] [blame]
K. Moon832a6942022-10-31 20:11:31 +00001// Copyright 2018 The PDFium Authors
Tom Sepezeccfe0e2018-11-01 16:34:52 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "fxjs/cfx_globaldata.h"
6
Lei Zhang2810c832022-08-16 18:45:22 +00007#include <stdint.h>
8
Tom Sepezbf2ef782018-11-07 20:03:30 +00009#include <utility>
Tom Sepezeccfe0e2018-11-01 16:34:52 +000010#include <vector>
11
Lei Zhang2810c832022-08-16 18:45:22 +000012#include "core/fxcrt/data_vector.h"
Tom Sepezeccfe0e2018-11-01 16:34:52 +000013#include "testing/gtest/include/gtest/gtest.h"
Tom Sepezeccfe0e2018-11-01 16:34:52 +000014
15namespace {
16
17class TestDelegate : public CFX_GlobalData::Delegate {
18 public:
Tom Sepezbf2ef782018-11-07 20:03:30 +000019 TestDelegate() = default;
Lei Zhang511d49d2021-04-01 05:13:22 +000020 ~TestDelegate() override = default;
Tom Sepezeccfe0e2018-11-01 16:34:52 +000021
22 bool StoreBuffer(pdfium::span<const uint8_t> buffer) override {
Lei Zhang2810c832022-08-16 18:45:22 +000023 last_buffer_ = DataVector<uint8_t>(buffer.begin(), buffer.end());
Tom Sepezeccfe0e2018-11-01 16:34:52 +000024 return true;
25 }
Lei Zhang2c495302021-10-07 23:13:30 +000026 absl::optional<pdfium::span<uint8_t>> LoadBuffer() override {
Tom Sepezeccfe0e2018-11-01 16:34:52 +000027 return pdfium::span<uint8_t>(last_buffer_);
28 }
29 void BufferDone() override {
Tom Sepeza52ce902020-04-06 23:37:54 +000030 // Catch misuse after done.
Lei Zhang2810c832022-08-16 18:45:22 +000031 last_buffer_ = DataVector<uint8_t>();
Tom Sepezeccfe0e2018-11-01 16:34:52 +000032 }
33
Lei Zhang2810c832022-08-16 18:45:22 +000034 DataVector<uint8_t> last_buffer_;
Tom Sepezeccfe0e2018-11-01 16:34:52 +000035};
36
37} // namespace
38
Tom Sepezbf2ef782018-11-07 20:03:30 +000039TEST(CFXGlobalData, GetSafety) {
40 CFX_GlobalData* pInstance = CFX_GlobalData::GetRetainedInstance(nullptr);
Tom Sepez9e61a722022-06-08 16:54:30 +000041 EXPECT_FALSE(pInstance->GetGlobalVariable("nonesuch"));
42 EXPECT_FALSE(pInstance->GetAt(-1));
43 EXPECT_FALSE(pInstance->GetAt(0));
44 EXPECT_FALSE(pInstance->GetAt(1));
Tom Sepezbf2ef782018-11-07 20:03:30 +000045
46 pInstance->SetGlobalVariableNumber("double", 2.0);
47 pInstance->SetGlobalVariableString("string", "clams");
48
Tom Sepez9e61a722022-06-08 16:54:30 +000049 EXPECT_FALSE(pInstance->GetGlobalVariable("nonesuch"));
50 EXPECT_FALSE(pInstance->GetAt(-1));
Tom Sepezbf2ef782018-11-07 20:03:30 +000051 EXPECT_EQ(pInstance->GetGlobalVariable("double"), pInstance->GetAt(0));
52 EXPECT_EQ(pInstance->GetGlobalVariable("string"), pInstance->GetAt(1));
Tom Sepez9e61a722022-06-08 16:54:30 +000053 EXPECT_FALSE(pInstance->GetAt(2));
Tom Sepezbf2ef782018-11-07 20:03:30 +000054
55 ASSERT_TRUE(pInstance->Release());
56}
57
Tom Sepezeccfe0e2018-11-01 16:34:52 +000058TEST(CFXGlobalData, StoreReload) {
59 TestDelegate delegate;
Tom Sepez574ee372020-01-29 17:25:43 +000060 std::vector<std::unique_ptr<CFX_KeyValue>> array;
Tom Sepezeccfe0e2018-11-01 16:34:52 +000061 CFX_GlobalData* pInstance = CFX_GlobalData::GetRetainedInstance(&delegate);
62 pInstance->SetGlobalVariableNumber("double", 2.0);
63 pInstance->SetGlobalVariableString("string", "clams");
Tom Sepezbf2ef782018-11-07 20:03:30 +000064 pInstance->SetGlobalVariableBoolean("boolean", true);
65 pInstance->SetGlobalVariableNull("null");
66 pInstance->SetGlobalVariableObject("array", std::move(array));
Tom Sepezeccfe0e2018-11-01 16:34:52 +000067 pInstance->SetGlobalVariablePersistent("double", true);
68 pInstance->SetGlobalVariablePersistent("string", true);
Tom Sepezbf2ef782018-11-07 20:03:30 +000069 pInstance->SetGlobalVariablePersistent("boolean", true);
70 pInstance->SetGlobalVariablePersistent("null", true);
71 pInstance->SetGlobalVariablePersistent("array", true);
Tom Sepezeccfe0e2018-11-01 16:34:52 +000072 ASSERT_TRUE(pInstance->Release());
73
74 pInstance = CFX_GlobalData::GetRetainedInstance(&delegate);
75 auto* element = pInstance->GetAt(0);
76 ASSERT_TRUE(element);
77 EXPECT_EQ("double", element->data.sKey);
Lei Zhang4709fa22020-06-02 20:02:26 +000078 EXPECT_EQ(CFX_Value::DataType::kNumber, element->data.nType);
Tom Sepezeccfe0e2018-11-01 16:34:52 +000079 EXPECT_EQ(2.0, element->data.dData);
80
81 element = pInstance->GetAt(1);
82 ASSERT_TRUE(element);
83 EXPECT_EQ("string", element->data.sKey);
Lei Zhang4709fa22020-06-02 20:02:26 +000084 EXPECT_EQ(CFX_Value::DataType::kString, element->data.nType);
Tom Sepezeccfe0e2018-11-01 16:34:52 +000085 EXPECT_EQ("clams", element->data.sData);
Tom Sepezbf2ef782018-11-07 20:03:30 +000086
87 element = pInstance->GetAt(2);
88 ASSERT_TRUE(element);
89 EXPECT_EQ("boolean", element->data.sKey);
Lei Zhang4709fa22020-06-02 20:02:26 +000090 EXPECT_EQ(CFX_Value::DataType::kBoolean, element->data.nType);
Tom Sepezbf2ef782018-11-07 20:03:30 +000091 EXPECT_EQ(true, element->data.bData);
92
93 element = pInstance->GetAt(3);
94 ASSERT_TRUE(element);
95 EXPECT_EQ("null", element->data.sKey);
Lei Zhang4709fa22020-06-02 20:02:26 +000096 EXPECT_EQ(CFX_Value::DataType::kNull, element->data.nType);
Tom Sepezbf2ef782018-11-07 20:03:30 +000097
Tom Sepez3eaad182018-11-07 21:52:21 +000098 // Arrays don't get persisted.
Tom Sepezbf2ef782018-11-07 20:03:30 +000099 element = pInstance->GetAt(4);
100 ASSERT_FALSE(element);
101
102 ASSERT_TRUE(pInstance->Release());
103}
104
105TEST(CFXGlobalData, ResetValues) {
106 CFX_GlobalData* pInstance = CFX_GlobalData::GetRetainedInstance(nullptr);
107 pInstance->SetGlobalVariableString("double", "bogus!!!");
108 pInstance->SetGlobalVariableString("string", "bogus!!!");
109 pInstance->SetGlobalVariableString("boolean", "bogus!!!");
110 pInstance->SetGlobalVariableString("null", "bogus!!!");
111
112 pInstance->SetGlobalVariableNumber("double", 2.0);
113 pInstance->SetGlobalVariableString("string", "clams");
114 pInstance->SetGlobalVariableBoolean("boolean", true);
115 pInstance->SetGlobalVariableNull("null");
116
117 auto* element = pInstance->GetAt(0);
118 ASSERT_TRUE(element);
119 EXPECT_EQ("double", element->data.sKey);
Lei Zhang4709fa22020-06-02 20:02:26 +0000120 EXPECT_EQ(CFX_Value::DataType::kNumber, element->data.nType);
Tom Sepezbf2ef782018-11-07 20:03:30 +0000121 EXPECT_EQ(2.0, element->data.dData);
122
123 element = pInstance->GetAt(1);
124 ASSERT_TRUE(element);
125 EXPECT_EQ("string", element->data.sKey);
Lei Zhang4709fa22020-06-02 20:02:26 +0000126 EXPECT_EQ(CFX_Value::DataType::kString, element->data.nType);
Tom Sepezbf2ef782018-11-07 20:03:30 +0000127 EXPECT_EQ("clams", element->data.sData);
128
129 element = pInstance->GetAt(2);
130 ASSERT_TRUE(element);
131 EXPECT_EQ("boolean", element->data.sKey);
Lei Zhang4709fa22020-06-02 20:02:26 +0000132 EXPECT_EQ(CFX_Value::DataType::kBoolean, element->data.nType);
Tom Sepezbf2ef782018-11-07 20:03:30 +0000133 EXPECT_EQ(true, element->data.bData);
134
135 element = pInstance->GetAt(3);
136 ASSERT_TRUE(element);
137 EXPECT_EQ("null", element->data.sKey);
Lei Zhang4709fa22020-06-02 20:02:26 +0000138 EXPECT_EQ(CFX_Value::DataType::kNull, element->data.nType);
Tom Sepezbf2ef782018-11-07 20:03:30 +0000139
140 ASSERT_TRUE(pInstance->Release());
141}
142
143TEST(CFXGlobalData, DeleteValues) {
144 CFX_GlobalData* pInstance = CFX_GlobalData::GetRetainedInstance(nullptr);
145 pInstance->SetGlobalVariableNumber("double", 2.0);
146 pInstance->SetGlobalVariableString("string", "clams");
147 pInstance->SetGlobalVariableBoolean("boolean", true);
148 pInstance->SetGlobalVariableNull("null");
149 EXPECT_EQ(4, pInstance->GetSize());
150
151 pInstance->DeleteGlobalVariable("nonesuch");
152 EXPECT_EQ(4, pInstance->GetSize());
153
154 pInstance->DeleteGlobalVariable("boolean");
155 EXPECT_EQ(3, pInstance->GetSize());
156
157 pInstance->DeleteGlobalVariable("string");
158 EXPECT_EQ(2, pInstance->GetSize());
159
160 pInstance->DeleteGlobalVariable("double");
161 EXPECT_EQ(1, pInstance->GetSize());
162
163 pInstance->DeleteGlobalVariable("null");
164 EXPECT_EQ(0, pInstance->GetSize());
165
Tom Sepezeccfe0e2018-11-01 16:34:52 +0000166 ASSERT_TRUE(pInstance->Release());
167}