| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 #include "webkit/dom_storage/dom_storage_map.h" | 7 #include "webkit/dom_storage/dom_storage_map.h" |
| 8 | 8 |
| 9 namespace dom_storage { | 9 namespace dom_storage { |
| 10 | 10 |
| 11 TEST(DomStorageMapTest, DomStorageMapBasics) { | 11 TEST(DomStorageMapTest, DomStorageMapBasics) { |
| 12 const string16 kKey(ASCIIToUTF16("key")); | 12 const base::string16 kKey(ASCIIToUTF16("key")); |
| 13 const string16 kValue(ASCIIToUTF16("value")); | 13 const base::string16 kValue(ASCIIToUTF16("value")); |
| 14 const size_t kValueBytes = kValue.size() * sizeof(char16); | 14 const size_t kValueBytes = kValue.size() * sizeof(char16); |
| 15 const size_t kItemBytes = | 15 const size_t kItemBytes = |
| 16 (kKey.size() + kValue.size()) * sizeof(char16); | 16 (kKey.size() + kValue.size()) * sizeof(char16); |
| 17 const string16 kKey2(ASCIIToUTF16("key2")); | 17 const base::string16 kKey2(ASCIIToUTF16("key2")); |
| 18 const size_t kKey2Bytes = kKey2.size() * sizeof(char16); | 18 const size_t kKey2Bytes = kKey2.size() * sizeof(char16); |
| 19 const string16 kValue2(ASCIIToUTF16("value2")); | 19 const base::string16 kValue2(ASCIIToUTF16("value2")); |
| 20 const size_t kItem2Bytes = | 20 const size_t kItem2Bytes = |
| 21 (kKey2.size() + kValue2.size()) * sizeof(char16); | 21 (kKey2.size() + kValue2.size()) * sizeof(char16); |
| 22 const size_t kQuota = 1024; // 1K quota for this test. | 22 const size_t kQuota = 1024; // 1K quota for this test. |
| 23 | 23 |
| 24 scoped_refptr<DomStorageMap> map(new DomStorageMap(kQuota)); | 24 scoped_refptr<DomStorageMap> map(new DomStorageMap(kQuota)); |
| 25 string16 old_value; | 25 base::string16 old_value; |
| 26 NullableString16 old_nullable_value; | 26 NullableString16 old_nullable_value; |
| 27 ValuesMap swap; | 27 ValuesMap swap; |
| 28 scoped_refptr<DomStorageMap> copy; | 28 scoped_refptr<DomStorageMap> copy; |
| 29 | 29 |
| 30 // Check the behavior of an empty map. | 30 // Check the behavior of an empty map. |
| 31 EXPECT_EQ(0u, map->Length()); | 31 EXPECT_EQ(0u, map->Length()); |
| 32 EXPECT_TRUE(map->Key(0).is_null()); | 32 EXPECT_TRUE(map->Key(0).is_null()); |
| 33 EXPECT_TRUE(map->Key(100).is_null()); | 33 EXPECT_TRUE(map->Key(100).is_null()); |
| 34 EXPECT_TRUE(map->GetItem(kKey).is_null()); | 34 EXPECT_TRUE(map->GetItem(kKey).is_null()); |
| 35 EXPECT_FALSE(map->RemoveItem(kKey, &old_value)); | 35 EXPECT_FALSE(map->RemoveItem(kKey, &old_value)); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 EXPECT_TRUE(copy->Key(2).is_null()); | 76 EXPECT_TRUE(copy->Key(2).is_null()); |
| 77 EXPECT_EQ(kItemBytes + kItem2Bytes, copy->bytes_used()); | 77 EXPECT_EQ(kItemBytes + kItem2Bytes, copy->bytes_used()); |
| 78 | 78 |
| 79 map->SwapValues(&swap); | 79 map->SwapValues(&swap); |
| 80 EXPECT_EQ(2ul, swap.size()); | 80 EXPECT_EQ(2ul, swap.size()); |
| 81 EXPECT_EQ(0u, map->Length()); | 81 EXPECT_EQ(0u, map->Length()); |
| 82 EXPECT_EQ(0u, map->bytes_used()); | 82 EXPECT_EQ(0u, map->bytes_used()); |
| 83 } | 83 } |
| 84 | 84 |
| 85 TEST(DomStorageMapTest, EnforcesQuota) { | 85 TEST(DomStorageMapTest, EnforcesQuota) { |
| 86 const string16 kKey = ASCIIToUTF16("test_key"); | 86 const base::string16 kKey = ASCIIToUTF16("test_key"); |
| 87 const string16 kValue = ASCIIToUTF16("test_value"); | 87 const base::string16 kValue = ASCIIToUTF16("test_value"); |
| 88 const string16 kKey2 = ASCIIToUTF16("test_key_2"); | 88 const base::string16 kKey2 = ASCIIToUTF16("test_key_2"); |
| 89 | 89 |
| 90 // A 50 byte quota is too small to hold both keys, so we | 90 // A 50 byte quota is too small to hold both keys, so we |
| 91 // should see the DomStorageMap enforcing it. | 91 // should see the DomStorageMap enforcing it. |
| 92 const size_t kQuota = 50; | 92 const size_t kQuota = 50; |
| 93 | 93 |
| 94 string16 old_value; | 94 base::string16 old_value; |
| 95 NullableString16 old_nullable_value; | 95 NullableString16 old_nullable_value; |
| 96 | 96 |
| 97 scoped_refptr<DomStorageMap> map(new DomStorageMap(kQuota)); | 97 scoped_refptr<DomStorageMap> map(new DomStorageMap(kQuota)); |
| 98 EXPECT_TRUE(map->SetItem(kKey, kValue, &old_nullable_value)); | 98 EXPECT_TRUE(map->SetItem(kKey, kValue, &old_nullable_value)); |
| 99 EXPECT_FALSE(map->SetItem(kKey2, kValue, &old_nullable_value)); | 99 EXPECT_FALSE(map->SetItem(kKey2, kValue, &old_nullable_value)); |
| 100 EXPECT_EQ(1u, map->Length()); | 100 EXPECT_EQ(1u, map->Length()); |
| 101 | 101 |
| 102 EXPECT_TRUE(map->RemoveItem(kKey, &old_value)); | 102 EXPECT_TRUE(map->RemoveItem(kKey, &old_value)); |
| 103 EXPECT_EQ(kValue, old_value); | 103 EXPECT_EQ(kValue, old_value); |
| 104 EXPECT_EQ(0u, map->Length()); | 104 EXPECT_EQ(0u, map->Length()); |
| 105 EXPECT_TRUE(map->SetItem(kKey2, kValue, &old_nullable_value)); | 105 EXPECT_TRUE(map->SetItem(kKey2, kValue, &old_nullable_value)); |
| 106 EXPECT_EQ(1u, map->Length()); | 106 EXPECT_EQ(1u, map->Length()); |
| 107 | 107 |
| 108 // Verify that the SwapValues method does not do quota checking. | 108 // Verify that the SwapValues method does not do quota checking. |
| 109 ValuesMap swap; | 109 ValuesMap swap; |
| 110 swap[kKey] = NullableString16(kValue, false); | 110 swap[kKey] = NullableString16(kValue, false); |
| 111 swap[kKey2] = NullableString16(kValue, false); | 111 swap[kKey2] = NullableString16(kValue, false); |
| 112 map->SwapValues(&swap); | 112 map->SwapValues(&swap); |
| 113 EXPECT_GT(map->bytes_used(), kQuota); | 113 EXPECT_GT(map->bytes_used(), kQuota); |
| 114 | 114 |
| 115 // When overbudget, a new value of greater size than the existing value can | 115 // When overbudget, a new value of greater size than the existing value can |
| 116 // not be set, but a new value of lesser or equal size can be set. | 116 // not be set, but a new value of lesser or equal size can be set. |
| 117 EXPECT_TRUE(map->SetItem(kKey, kValue, &old_nullable_value)); | 117 EXPECT_TRUE(map->SetItem(kKey, kValue, &old_nullable_value)); |
| 118 EXPECT_FALSE(map->SetItem(kKey, string16(kValue + kValue), | 118 EXPECT_FALSE(map->SetItem(kKey, base::string16(kValue + kValue), |
| 119 &old_nullable_value)); | 119 &old_nullable_value)); |
| 120 EXPECT_TRUE(map->SetItem(kKey, string16(), &old_nullable_value)); | 120 EXPECT_TRUE(map->SetItem(kKey, base::string16(), &old_nullable_value)); |
| 121 EXPECT_EQ(kValue, old_nullable_value.string()); | 121 EXPECT_EQ(kValue, old_nullable_value.string()); |
| 122 } | 122 } |
| 123 | 123 |
| 124 } // namespace dom_storage | 124 } // namespace dom_storage |
| OLD | NEW |