Home | History | Annotate | Download | only in indexeddb
      1 /*
      2  * Copyright (C) 2013 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "modules/indexeddb/IDBDatabase.h"
     33 
     34 #include "core/dom/DOMError.h"
     35 #include "core/dom/Document.h"
     36 
     37 #include <gtest/gtest.h>
     38 
     39 using namespace WebCore;
     40 
     41 namespace {
     42 
     43 class IDBDatabaseTest : public testing::Test {
     44 public:
     45     IDBDatabaseTest()
     46         : m_handleScope(v8::Isolate::GetCurrent())
     47         , m_scope(v8::Context::New(v8::Isolate::GetCurrent()))
     48         , m_document(Document::create())
     49     {
     50     }
     51 
     52     ScriptExecutionContext* scriptExecutionContext()
     53     {
     54         return m_document.get();
     55     }
     56 
     57 private:
     58     v8::HandleScope m_handleScope;
     59     v8::Context::Scope m_scope;
     60     RefPtr<Document> m_document;
     61 };
     62 
     63 class FakeIDBDatabaseBackendProxy : public IDBDatabaseBackendInterface {
     64 public:
     65     static PassRefPtr<FakeIDBDatabaseBackendProxy> create() { return adoptRef(new FakeIDBDatabaseBackendProxy()); }
     66 
     67     virtual void createObjectStore(int64_t transactionId, int64_t objectStoreId, const String& name, const IDBKeyPath&, bool autoIncrement) OVERRIDE { }
     68     virtual void deleteObjectStore(int64_t transactionId, int64_t objectStoreId) OVERRIDE { }
     69     virtual void createTransaction(int64_t transactionId, PassRefPtr<IDBDatabaseCallbacks>, const Vector<int64_t>& objectStoreIds, unsigned short mode) OVERRIDE { }
     70     virtual void close(PassRefPtr<IDBDatabaseCallbacks>) OVERRIDE { }
     71 
     72     virtual void commit(int64_t transactionId) OVERRIDE { }
     73     virtual void abort(int64_t transactionId) OVERRIDE { }
     74 
     75     virtual void createIndex(int64_t transactionId, int64_t objectStoreId, int64_t indexId, const String& name, const IDBKeyPath&, bool unique, bool multiEntry) OVERRIDE { }
     76     virtual void deleteIndex(int64_t transactionId, int64_t objectStoreId, int64_t indexId) OVERRIDE { }
     77 
     78     virtual void get(int64_t transactionId, int64_t objectStoreId, int64_t indexId, PassRefPtr<IDBKeyRange>, bool keyOnly, PassRefPtr<IDBCallbacks>) OVERRIDE { }
     79     virtual void put(int64_t transactionId, int64_t objectStoreId, PassRefPtr<SharedBuffer> value, PassRefPtr<IDBKey>, PutMode, PassRefPtr<IDBCallbacks>, const Vector<int64_t>& indexIds, const Vector<IndexKeys>&) OVERRIDE { }
     80     virtual void setIndexKeys(int64_t transactionId, int64_t objectStoreId, PassRefPtr<IDBKey>, const Vector<int64_t>& indexIds, const Vector<IndexKeys>&) OVERRIDE { }
     81     virtual void setIndexesReady(int64_t transactionId, int64_t objectStoreId, const Vector<int64_t>& indexIds) OVERRIDE { }
     82     virtual void openCursor(int64_t transactionId, int64_t objectStoreId, int64_t indexId, PassRefPtr<IDBKeyRange>, IndexedDB::CursorDirection, bool keyOnly, TaskType, PassRefPtr<IDBCallbacks>) OVERRIDE { }
     83     virtual void count(int64_t transactionId, int64_t objectStoreId, int64_t indexId, PassRefPtr<IDBKeyRange>, PassRefPtr<IDBCallbacks>) OVERRIDE { }
     84     virtual void deleteRange(int64_t transactionId, int64_t objectStoreId, PassRefPtr<IDBKeyRange>, PassRefPtr<IDBCallbacks>) OVERRIDE { }
     85     virtual void clear(int64_t transactionId, int64_t objectStoreId, PassRefPtr<IDBCallbacks>) OVERRIDE { }
     86 
     87 private:
     88     FakeIDBDatabaseBackendProxy() { }
     89 };
     90 
     91 class FakeIDBDatabaseCallbacks : public IDBDatabaseCallbacks {
     92 public:
     93     static PassRefPtr<FakeIDBDatabaseCallbacks> create() { return adoptRef(new FakeIDBDatabaseCallbacks()); }
     94     virtual void onVersionChange(int64_t oldVersion, int64_t newVersion) OVERRIDE { }
     95     virtual void onForcedClose() OVERRIDE { }
     96     virtual void onAbort(int64_t transactionId, PassRefPtr<DOMError> error) OVERRIDE { }
     97     virtual void onComplete(int64_t transactionId) OVERRIDE { }
     98 private:
     99     FakeIDBDatabaseCallbacks() { }
    100 };
    101 
    102 TEST_F(IDBDatabaseTest, BackendRetention)
    103 {
    104     RefPtr<FakeIDBDatabaseBackendProxy> proxy = FakeIDBDatabaseBackendProxy::create();
    105     RefPtr<FakeIDBDatabaseCallbacks> connection = FakeIDBDatabaseCallbacks::create();
    106 
    107     EXPECT_EQ(1, proxy->refCount()); // local
    108 
    109     RefPtr<IDBDatabase> db = IDBDatabase::create(scriptExecutionContext(), proxy, connection);
    110 
    111     EXPECT_EQ(2, proxy->refCount()); // local and db
    112 
    113     db->close();
    114 
    115     EXPECT_EQ(1, proxy->refCount()); // local
    116 }
    117 
    118 } // namespace
    119