diff options
author | michaeln@google.com <michaeln@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-28 00:32:27 +0000 |
---|---|---|
committer | michaeln@google.com <michaeln@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-28 00:32:27 +0000 |
commit | d1545e00fb7e2f1dfdbbcf8fe25da04c54ff0d10 (patch) | |
tree | 051d837a59e0e6c7988f95f3a1a8749e28a9bdb7 /webkit/dom_storage | |
parent | 55b67c2a6a1ea4846656de82fe2acd1e3d59048a (diff) | |
download | chromium_src-d1545e00fb7e2f1dfdbbcf8fe25da04c54ff0d10.zip chromium_src-d1545e00fb7e2f1dfdbbcf8fe25da04c54ff0d10.tar.gz chromium_src-d1545e00fb7e2f1dfdbbcf8fe25da04c54ff0d10.tar.bz2 |
DomStorageContext unittests.
TBR=tony
BUG=106763
Review URL: https://chromiumcodereview.appspot.com/9860018
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@129317 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/dom_storage')
-rw-r--r-- | webkit/dom_storage/dom_storage_context.h | 3 | ||||
-rw-r--r-- | webkit/dom_storage/dom_storage_context_unittest.cc | 181 |
2 files changed, 184 insertions, 0 deletions
diff --git a/webkit/dom_storage/dom_storage_context.h b/webkit/dom_storage/dom_storage_context.h index ee89e16..69bcae8 100644 --- a/webkit/dom_storage/dom_storage_context.h +++ b/webkit/dom_storage/dom_storage_context.h @@ -12,6 +12,7 @@ #include "base/atomic_sequence_num.h" #include "base/basictypes.h" #include "base/file_path.h" +#include "base/gtest_prod_util.h" #include "base/memory/ref_counted.h" #include "base/observer_list.h" #include "base/time.h" @@ -146,6 +147,8 @@ class DomStorageContext void CloneSessionNamespace(int64 existing_id, int64 new_id); private: + friend class DomStorageContextTest; + FRIEND_TEST_ALL_PREFIXES(DomStorageContextTest, Basics); friend class base::RefCountedThreadSafe<DomStorageContext>; typedef std::map<int64, scoped_refptr<DomStorageNamespace> > StorageNamespaceMap; diff --git a/webkit/dom_storage/dom_storage_context_unittest.cc b/webkit/dom_storage/dom_storage_context_unittest.cc new file mode 100644 index 0000000..d83dbde --- /dev/null +++ b/webkit/dom_storage/dom_storage_context_unittest.cc @@ -0,0 +1,181 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/bind.h" +#include "base/file_util.h" +#include "base/message_loop.h" +#include "base/message_loop_proxy.h" +#include "base/scoped_temp_dir.h" +#include "base/threading/sequenced_worker_pool.h" +#include "base/time.h" +#include "base/utf_string_conversions.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "webkit/dom_storage/dom_storage_area.h" +#include "webkit/dom_storage/dom_storage_context.h" +#include "webkit/dom_storage/dom_storage_namespace.h" +#include "webkit/dom_storage/dom_storage_task_runner.h" +#include "webkit/dom_storage/dom_storage_types.h" +#include "webkit/quota/mock_special_storage_policy.h" + +namespace dom_storage { + +class DomStorageContextTest : public testing::Test { + public: + DomStorageContextTest() + : kOrigin(GURL("http://dom_storage/")), + kKey(ASCIIToUTF16("key")), + kValue(ASCIIToUTF16("value")), + kDontIncludeFileInfo(false), + kDoIncludeFileInfo(true) { + } + + const GURL kOrigin; + const string16 kKey; + const string16 kValue; + const bool kDontIncludeFileInfo; + const bool kDoIncludeFileInfo; + + virtual void SetUp() { + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); + storage_policy_ = new quota::MockSpecialStoragePolicy; + task_runner_ = new MockDomStorageTaskRunner( + base::MessageLoopProxy::current()); + context_ = new DomStorageContext(temp_dir_.path(), + storage_policy_, + task_runner_); + } + + virtual void TearDown() { + MessageLoop::current()->RunAllPending(); + } + + void VerifySingleOriginRemains(const GURL& origin) { + // Use a new instance to examine the contexts of temp_dir_. + scoped_refptr<DomStorageContext> context = + new DomStorageContext(temp_dir_.path(), NULL, NULL); + std::vector<DomStorageContext::UsageInfo> infos; + context->GetUsageInfo(&infos, kDontIncludeFileInfo); + EXPECT_EQ(1u, infos.size()); + EXPECT_EQ(origin, infos[0].origin); + } + + protected: + ScopedTempDir temp_dir_; + scoped_refptr<quota::MockSpecialStoragePolicy> storage_policy_; + scoped_refptr<MockDomStorageTaskRunner> task_runner_; + scoped_refptr<DomStorageContext> context_; + DISALLOW_COPY_AND_ASSIGN(DomStorageContextTest); +}; + +TEST_F(DomStorageContextTest, Basics) { + // This test doesn't do much, checks that the constructor + // initializes members properly and that invoking methods + // on a newly created object w/o any data on disk do no harm. + EXPECT_EQ(temp_dir_.path(), context_->directory()); + EXPECT_EQ(storage_policy_.get(), context_->special_storage_policy_.get()); + context_->PurgeMemory(); + context_->DeleteOrigin(GURL("http://chromium.org/")); + context_->DeleteDataModifiedSince(base::Time::Now()); + const int kFirstSessionStorageNamespaceId = 1; + EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)); + EXPECT_FALSE(context_->GetStorageNamespace(kFirstSessionStorageNamespaceId)); + EXPECT_EQ(kFirstSessionStorageNamespaceId, context_->AllocateSessionId()); + std::vector<DomStorageContext::UsageInfo> infos; + context_->GetUsageInfo(&infos, kDontIncludeFileInfo); + EXPECT_TRUE(infos.empty()); + context_->Shutdown(); +} + +TEST_F(DomStorageContextTest, UsageInfo) { + // Should be empty initially + std::vector<DomStorageContext::UsageInfo> infos; + context_->GetUsageInfo(&infos, kDontIncludeFileInfo); + EXPECT_TRUE(infos.empty()); + context_->GetUsageInfo(&infos, kDoIncludeFileInfo); + EXPECT_TRUE(infos.empty()); + + // Put some data into local storage and shutdown the context + // to ensure data is written to disk. + NullableString16 old_value; + EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> + OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value)); + context_->Shutdown(); + context_ = NULL; + MessageLoop::current()->RunAllPending(); + + // Create a new context that points to the same directory, see that + // it knows about the origin that we stored data for. + context_ = new DomStorageContext(temp_dir_.path(), NULL, NULL); + context_->GetUsageInfo(&infos, kDontIncludeFileInfo); + EXPECT_EQ(1u, infos.size()); + EXPECT_EQ(kOrigin, infos[0].origin); + EXPECT_EQ(0u, infos[0].data_size); + EXPECT_EQ(base::Time(), infos[0].last_modified); + infos.clear(); + context_->GetUsageInfo(&infos, kDoIncludeFileInfo); + EXPECT_EQ(1u, infos.size()); + EXPECT_EQ(kOrigin, infos[0].origin); + EXPECT_NE(0u, infos[0].data_size); + EXPECT_NE(base::Time(), infos[0].last_modified); +} + +TEST_F(DomStorageContextTest, SessionOnly) { + const GURL kSessionOnlyOrigin("http://www.sessiononly.com/"); + storage_policy_->AddSessionOnly(kSessionOnlyOrigin); + + // Store data for a normal and a session-only origin and then + // invoke Shutdown() which should delete data for session-only + // origins. + NullableString16 old_value; + EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> + OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value)); + EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> + OpenStorageArea(kSessionOnlyOrigin)->SetItem(kKey, kValue, &old_value)); + context_->Shutdown(); + context_ = NULL; + MessageLoop::current()->RunAllPending(); + + // Verify that the session-only origin data is gone. + VerifySingleOriginRemains(kOrigin); +} + +TEST_F(DomStorageContextTest, ClearLocalState) { + const GURL kProtectedOrigin("http://www.protected.com/"); + storage_policy_->AddProtected(kProtectedOrigin); + + // Store data for a normal and a protected origin, setup shutdown options + // to clear normal local state, then shutdown and let things flush. + NullableString16 old_value; + EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> + OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value)); + EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> + OpenStorageArea(kProtectedOrigin)->SetItem(kKey, kValue, &old_value)); + context_->SetClearLocalState(true); + context_->Shutdown(); + context_ = NULL; + MessageLoop::current()->RunAllPending(); + + VerifySingleOriginRemains(kProtectedOrigin); +} + +TEST_F(DomStorageContextTest, SaveSessionState) { + const GURL kSessionOnlyOrigin("http://www.sessiononly.com/"); + storage_policy_->AddSessionOnly(kSessionOnlyOrigin); + + // Store data for a session-only origin, setup to save session data, then + // shutdown. + NullableString16 old_value; + EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> + OpenStorageArea(kSessionOnlyOrigin)->SetItem(kKey, kValue, &old_value)); + context_->SetClearLocalState(true); + context_->SaveSessionState(); // Should override clear behavior. + context_->Shutdown(); + context_ = NULL; + MessageLoop::current()->RunAllPending(); + + VerifySingleOriginRemains(kSessionOnlyOrigin); +} + +} // namespace dom_storage + |