| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/file_util.h" |
| 7 #include "base/message_loop.h" |
| 8 #include "base/message_loop_proxy.h" |
| 9 #include "base/scoped_temp_dir.h" |
| 10 #include "base/threading/sequenced_worker_pool.h" |
| 11 #include "base/time.h" |
| 12 #include "base/utf_string_conversions.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "webkit/dom_storage/dom_storage_area.h" |
| 15 #include "webkit/dom_storage/dom_storage_context.h" |
| 16 #include "webkit/dom_storage/dom_storage_namespace.h" |
| 17 #include "webkit/dom_storage/dom_storage_task_runner.h" |
| 18 #include "webkit/dom_storage/dom_storage_types.h" |
| 19 #include "webkit/quota/mock_special_storage_policy.h" |
| 20 |
| 21 namespace dom_storage { |
| 22 |
| 23 class DomStorageContextTest : public testing::Test { |
| 24 public: |
| 25 DomStorageContextTest() |
| 26 : kOrigin(GURL("http://dom_storage/")), |
| 27 kKey(ASCIIToUTF16("key")), |
| 28 kValue(ASCIIToUTF16("value")) { |
| 29 } |
| 30 |
| 31 const GURL kOrigin; |
| 32 const string16 kKey; |
| 33 const string16 kValue; |
| 34 |
| 35 virtual void SetUp() { |
| 36 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 37 storage_policy_ = new quota::MockSpecialStoragePolicy; |
| 38 task_runner_ = new MockDomStorageTaskRunner( |
| 39 base::MessageLoopProxy::current()); |
| 40 context_ = new DomStorageContext(temp_dir_.path(), |
| 41 storage_policy_, |
| 42 task_runner_); |
| 43 } |
| 44 |
| 45 virtual void TearDown() { |
| 46 MessageLoop::current()->RunAllPending(); |
| 47 } |
| 48 |
| 49 void VerifySingleOriginRemains(const GURL& origin) { |
| 50 // Use a new instance to examine the contexts of temp_dir_. |
| 51 scoped_refptr<DomStorageContext> context = |
| 52 new DomStorageContext(temp_dir_.path(), NULL, NULL); |
| 53 std::vector<DomStorageContext::UsageInfo> infos; |
| 54 context->GetUsageInfo(&infos, false); |
| 55 EXPECT_EQ(1u, infos.size()); |
| 56 EXPECT_EQ(origin, infos[0].origin); |
| 57 } |
| 58 |
| 59 protected: |
| 60 ScopedTempDir temp_dir_; |
| 61 scoped_refptr<quota::MockSpecialStoragePolicy> storage_policy_; |
| 62 scoped_refptr<MockDomStorageTaskRunner> task_runner_; |
| 63 scoped_refptr<DomStorageContext> context_; |
| 64 DISALLOW_COPY_AND_ASSIGN(DomStorageContextTest); |
| 65 }; |
| 66 |
| 67 TEST_F(DomStorageContextTest, Basics) { |
| 68 // This test doesn't do much, checks that the constructor |
| 69 // initializes members properly and that invoking methods |
| 70 // on a newly created object w/o any data on disk do no harm. |
| 71 EXPECT_EQ(temp_dir_.path(), context_->directory()); |
| 72 EXPECT_EQ(storage_policy_.get(), context_->special_storage_policy_.get()); |
| 73 context_->PurgeMemory(); |
| 74 context_->DeleteOrigin(GURL("http://chromium.org/")); |
| 75 context_->DeleteDataModifiedSince(base::Time::Now()); |
| 76 const int kFirstSessionStorageNamespaceId = 1; |
| 77 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)); |
| 78 EXPECT_FALSE(context_->GetStorageNamespace(kFirstSessionStorageNamespaceId)); |
| 79 EXPECT_EQ(kFirstSessionStorageNamespaceId, context_->AllocateSessionId()); |
| 80 const bool kIncludeFileInfo = false; |
| 81 std::vector<DomStorageContext::UsageInfo> infos; |
| 82 context_->GetUsageInfo(&infos, kIncludeFileInfo); |
| 83 EXPECT_TRUE(infos.empty()); |
| 84 context_->Shutdown(); |
| 85 } |
| 86 |
| 87 TEST_F(DomStorageContextTest, UsageInfo) { |
| 88 // Should be empty initially |
| 89 std::vector<DomStorageContext::UsageInfo> infos; |
| 90 const bool kDontIncludeFileInfo = false; |
| 91 context_->GetUsageInfo(&infos, kDontIncludeFileInfo); |
| 92 EXPECT_TRUE(infos.empty()); |
| 93 const bool kIncludeFileInfo = true; |
| 94 context_->GetUsageInfo(&infos, kIncludeFileInfo); |
| 95 EXPECT_TRUE(infos.empty()); |
| 96 |
| 97 // Put some data into local storage and shutdown the context |
| 98 // to ensure data is written to disk. |
| 99 NullableString16 old_value; |
| 100 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 101 OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value)); |
| 102 context_->Shutdown(); |
| 103 context_ = NULL; |
| 104 MessageLoop::current()->RunAllPending(); |
| 105 |
| 106 // Create a new context that points to the same directory, see that |
| 107 // it knows about the origin that we stored data for. |
| 108 context_ = new DomStorageContext(temp_dir_.path(), NULL, NULL); |
| 109 context_->GetUsageInfo(&infos, kDontIncludeFileInfo); |
| 110 EXPECT_EQ(1u, infos.size()); |
| 111 EXPECT_EQ(kOrigin, infos[0].origin); |
| 112 EXPECT_EQ(0u, infos[0].data_size); |
| 113 EXPECT_EQ(base::Time(), infos[0].last_modified); |
| 114 infos.clear(); |
| 115 context_->GetUsageInfo(&infos, kIncludeFileInfo); |
| 116 EXPECT_EQ(1u, infos.size()); |
| 117 EXPECT_EQ(kOrigin, infos[0].origin); |
| 118 EXPECT_NE(0u, infos[0].data_size); |
| 119 EXPECT_NE(base::Time(), infos[0].last_modified); |
| 120 } |
| 121 |
| 122 TEST_F(DomStorageContextTest, SessionOnly) { |
| 123 const GURL kSessionOnlyOrigin("http://www.sessiononly.com/"); |
| 124 storage_policy_->AddSessionOnly(kSessionOnlyOrigin); |
| 125 |
| 126 // Store data for a normal and a session-only origin and then |
| 127 // invoke Shutdown() which should delete data for session-only |
| 128 // origins. |
| 129 NullableString16 old_value; |
| 130 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 131 OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value)); |
| 132 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 133 OpenStorageArea(kSessionOnlyOrigin)->SetItem(kKey, kValue, &old_value)); |
| 134 context_->Shutdown(); |
| 135 context_ = NULL; |
| 136 MessageLoop::current()->RunAllPending(); |
| 137 |
| 138 // Verify that the session-only origin data is gone. |
| 139 VerifySingleOriginRemains(kOrigin); |
| 140 } |
| 141 |
| 142 TEST_F(DomStorageContextTest, ClearLocalState) { |
| 143 const GURL kProtectedOrigin("http://www.protected.com/"); |
| 144 storage_policy_->AddProtected(kProtectedOrigin); |
| 145 |
| 146 // Store data for a normal and a protected origin, setup shutdown options |
| 147 // to clear normal local state, then shutdown and let things flush. |
| 148 NullableString16 old_value; |
| 149 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 150 OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value)); |
| 151 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 152 OpenStorageArea(kProtectedOrigin)->SetItem(kKey, kValue, &old_value)); |
| 153 context_->SetClearLocalState(true); |
| 154 context_->Shutdown(); |
| 155 context_ = NULL; |
| 156 MessageLoop::current()->RunAllPending(); |
| 157 |
| 158 VerifySingleOriginRemains(kProtectedOrigin); |
| 159 } |
| 160 |
| 161 TEST_F(DomStorageContextTest, SaveSessionState) { |
| 162 const GURL kSessionOnlyOrigin("http://www.sessiononly.com/"); |
| 163 storage_policy_->AddSessionOnly(kSessionOnlyOrigin); |
| 164 |
| 165 // Store data for a session-only origin, setup to save session data, then |
| 166 // shutdown. |
| 167 NullableString16 old_value; |
| 168 EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 169 OpenStorageArea(kSessionOnlyOrigin)->SetItem(kKey, kValue, &old_value)); |
| 170 context_->SetClearLocalState(true); |
| 171 context_->SaveSessionState(); // Should override clear behavior. |
| 172 context_->Shutdown(); |
| 173 context_ = NULL; |
| 174 MessageLoop::current()->RunAllPending(); |
| 175 |
| 176 VerifySingleOriginRemains(kSessionOnlyOrigin); |
| 177 } |
| 178 |
| 179 } // namespace dom_storage |
| 180 |
| OLD | NEW |