OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "xfa/fde/cfde_txtedtbuf.h" |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "testing/test_support.h" |
| 9 #include "third_party/base/ptr_util.h" |
| 10 |
| 11 class CFDE_TxtEdtBufTest : public testing::Test { |
| 12 public: |
| 13 void SetUp() override { |
| 14 buf_ = pdfium::MakeUnique<CFDE_TxtEdtBuf>(); |
| 15 buf_->SetChunkSizeForTesting(5); |
| 16 } |
| 17 size_t ChunkCount() const { return buf_->m_Chunks.GetSize(); } |
| 18 |
| 19 std::unique_ptr<CFDE_TxtEdtBuf> buf_; |
| 20 }; |
| 21 |
| 22 TEST_F(CFDE_TxtEdtBufTest, SetTextLessThenChunkSize) { |
| 23 buf_->SetText(L"Hi"); |
| 24 EXPECT_EQ(1UL, ChunkCount()); |
| 25 EXPECT_EQ(2, buf_->GetTextLength()); |
| 26 |
| 27 CFX_WideString res = buf_->GetText(); |
| 28 EXPECT_EQ(2, res.GetLength()); |
| 29 EXPECT_EQ(L"Hi", res); |
| 30 } |
| 31 |
| 32 TEST_F(CFDE_TxtEdtBufTest, InsertAppendChunk) { |
| 33 buf_->SetText(L"Hi"); |
| 34 |
| 35 CFX_WideString end = L" World"; |
| 36 buf_->Insert(2, end.c_str(), end.GetLength()); |
| 37 EXPECT_EQ(3UL, ChunkCount()); |
| 38 EXPECT_EQ(8, buf_->GetTextLength()); |
| 39 |
| 40 CFX_WideString res = buf_->GetText(); |
| 41 EXPECT_EQ(8, res.GetLength()); |
| 42 EXPECT_EQ(L"Hi World", res); |
| 43 } |
| 44 |
| 45 TEST_F(CFDE_TxtEdtBufTest, InsertPrependChunk) { |
| 46 buf_->SetText(L"Hi"); |
| 47 |
| 48 CFX_WideString end = L"World "; |
| 49 buf_->Insert(0, end.c_str(), end.GetLength()); |
| 50 EXPECT_EQ(3UL, ChunkCount()); |
| 51 EXPECT_EQ(8, buf_->GetTextLength()); |
| 52 |
| 53 CFX_WideString res = buf_->GetText(); |
| 54 EXPECT_EQ(L"World Hi", res); |
| 55 EXPECT_EQ(8, res.GetLength()); |
| 56 } |
| 57 |
| 58 TEST_F(CFDE_TxtEdtBufTest, InsertBetweenChunks) { |
| 59 buf_->SetText(L"Hello World"); |
| 60 EXPECT_EQ(3UL, ChunkCount()); |
| 61 |
| 62 CFX_WideString inst = L"there "; |
| 63 buf_->Insert(6, inst.c_str(), inst.GetLength()); |
| 64 EXPECT_EQ(5UL, ChunkCount()); |
| 65 EXPECT_EQ(17, buf_->GetTextLength()); |
| 66 |
| 67 CFX_WideString res = buf_->GetText(); |
| 68 EXPECT_EQ(L"Hello there World", res); |
| 69 EXPECT_EQ(17, res.GetLength()); |
| 70 } |
| 71 |
| 72 TEST_F(CFDE_TxtEdtBufTest, SetText) { |
| 73 buf_->SetText(L"Hello World"); |
| 74 EXPECT_EQ(11, buf_->GetTextLength()); |
| 75 |
| 76 buf_->SetText(L"Hi"); |
| 77 // Don't remove chunks on setting shorter text. |
| 78 EXPECT_EQ(3UL, ChunkCount()); |
| 79 EXPECT_EQ(2, buf_->GetTextLength()); |
| 80 |
| 81 CFX_WideString res = buf_->GetText(); |
| 82 EXPECT_EQ(L"Hi", res); |
| 83 EXPECT_EQ(2, res.GetLength()); |
| 84 } |
| 85 |
| 86 TEST_F(CFDE_TxtEdtBufTest, DeleteMiddleText) { |
| 87 buf_->SetText(L"Hello there World"); |
| 88 buf_->Delete(6, 6); |
| 89 EXPECT_EQ(4UL, ChunkCount()); |
| 90 EXPECT_EQ(11, buf_->GetTextLength()); |
| 91 |
| 92 CFX_WideString res = buf_->GetText(); |
| 93 EXPECT_EQ(L"Hello World", res); |
| 94 EXPECT_EQ(11, res.GetLength()); |
| 95 } |
| 96 |
| 97 TEST_F(CFDE_TxtEdtBufTest, DeleteEndText) { |
| 98 buf_->SetText(L"Hello World"); |
| 99 buf_->Delete(5, 6); |
| 100 EXPECT_EQ(1UL, ChunkCount()); |
| 101 EXPECT_EQ(5, buf_->GetTextLength()); |
| 102 |
| 103 CFX_WideString res = buf_->GetText(); |
| 104 EXPECT_EQ(L"Hello", res); |
| 105 EXPECT_EQ(5, res.GetLength()); |
| 106 } |
| 107 |
| 108 TEST_F(CFDE_TxtEdtBufTest, DeleteStartText) { |
| 109 buf_->SetText(L"Hello World"); |
| 110 buf_->Delete(0, 6); |
| 111 EXPECT_EQ(2UL, ChunkCount()); |
| 112 EXPECT_EQ(5, buf_->GetTextLength()); |
| 113 |
| 114 CFX_WideString res = buf_->GetText(); |
| 115 EXPECT_EQ(L"World", res); |
| 116 EXPECT_EQ(5, res.GetLength()); |
| 117 } |
| 118 |
| 119 TEST_F(CFDE_TxtEdtBufTest, DeleteAllText) { |
| 120 buf_->SetText(L"Hello World"); |
| 121 buf_->Delete(0, 11); |
| 122 EXPECT_EQ(0UL, ChunkCount()); |
| 123 EXPECT_EQ(0, buf_->GetTextLength()); |
| 124 |
| 125 CFX_WideString res = buf_->GetText(); |
| 126 EXPECT_EQ(L"", res); |
| 127 EXPECT_EQ(0, res.GetLength()); |
| 128 } |
| 129 |
| 130 TEST_F(CFDE_TxtEdtBufTest, ClearWithRelease) { |
| 131 buf_->SetText(L"Hello World"); |
| 132 buf_->Clear(true); |
| 133 EXPECT_EQ(0UL, ChunkCount()); |
| 134 EXPECT_EQ(0, buf_->GetTextLength()); |
| 135 |
| 136 CFX_WideString res = buf_->GetText(); |
| 137 EXPECT_EQ(L"", res); |
| 138 EXPECT_EQ(0, res.GetLength()); |
| 139 } |
| 140 |
| 141 TEST_F(CFDE_TxtEdtBufTest, ClearWithoutRelease) { |
| 142 buf_->SetText(L"Hello World"); |
| 143 buf_->Clear(false); |
| 144 EXPECT_EQ(3UL, ChunkCount()); |
| 145 EXPECT_EQ(0, buf_->GetTextLength()); |
| 146 |
| 147 CFX_WideString res = buf_->GetText(); |
| 148 EXPECT_EQ(L"", res); |
| 149 EXPECT_EQ(0, res.GetLength()); |
| 150 } |
| 151 |
| 152 TEST_F(CFDE_TxtEdtBufTest, GetCharByIndex) { |
| 153 buf_->SetText(L"Hello world"); |
| 154 EXPECT_EQ(L"e", CFX_WideString(buf_->GetCharByIndex(1))); |
| 155 EXPECT_EQ(L"o", CFX_WideString(buf_->GetCharByIndex(7))); |
| 156 } |
| 157 |
| 158 TEST_F(CFDE_TxtEdtBufTest, GetRange) { |
| 159 buf_->SetText(L"Hello World"); |
| 160 EXPECT_EQ(L"", buf_->GetRange(1, 0)); |
| 161 EXPECT_EQ(L"ello", buf_->GetRange(1, 4)); |
| 162 EXPECT_EQ(L"lo Wo", buf_->GetRange(3, 5)); |
| 163 } |
| 164 |
| 165 #ifndef NDEBUG |
| 166 using CFDE_TxtEdtBufTestDeathTest = CFDE_TxtEdtBufTest; |
| 167 |
| 168 TEST_F(CFDE_TxtEdtBufTestDeathTest, InsertBadIndexes) { |
| 169 CFX_WideString inst = L"there "; |
| 170 |
| 171 buf_->SetText(L"Hi"); |
| 172 EXPECT_DEATH(buf_->Insert(-4, inst.c_str(), inst.GetLength()), "Assertion"); |
| 173 EXPECT_DEATH(buf_->Insert(9999, inst.c_str(), inst.GetLength()), "Assertion"); |
| 174 EXPECT_DEATH(buf_->Insert(1, inst.c_str(), -6), "Assertion"); |
| 175 } |
| 176 |
| 177 TEST_F(CFDE_TxtEdtBufTestDeathTest, DeleteWithBadIdx) { |
| 178 buf_->SetText(L"Hi"); |
| 179 EXPECT_DEATH(buf_->Delete(-10, 4), "Assertion"); |
| 180 EXPECT_DEATH(buf_->Delete(1, -5), "Assertion"); |
| 181 EXPECT_DEATH(buf_->Delete(5, 1), "Assertion"); |
| 182 EXPECT_DEATH(buf_->Delete(0, 10000), "Assertion"); |
| 183 } |
| 184 |
| 185 TEST_F(CFDE_TxtEdtBufTestDeathTest, GetCharByIndex) { |
| 186 buf_->SetText(L"Hi"); |
| 187 EXPECT_DEATH(buf_->GetCharByIndex(-1), "Assertion"); |
| 188 EXPECT_DEATH(buf_->GetCharByIndex(100), "Assertion"); |
| 189 } |
| 190 |
| 191 TEST_F(CFDE_TxtEdtBufTestDeathTest, GetRange) { |
| 192 buf_->SetText(L"Hi"); |
| 193 EXPECT_DEATH(buf_->GetRange(1, -1), "Assertion"); |
| 194 EXPECT_DEATH(buf_->GetRange(-1, 1), "Assertion"); |
| 195 EXPECT_DEATH(buf_->GetRange(10, 1), "Assertion"); |
| 196 EXPECT_DEATH(buf_->GetRange(1, 100), "Assertion"); |
| 197 } |
| 198 |
| 199 #endif // NDEBUG |
OLD | NEW |