indexed_db_cleanup_on_io_error_unittest.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
1// Copyright (c) 2013 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/files/file_path.h"
6#include "base/files/scoped_temp_dir.h"
7#include "base/strings/string16.h"
8#include "base/strings/utf_string_conversions.h"
9#include "content/browser/indexed_db/indexed_db_backing_store.h"
10#include "content/browser/indexed_db/leveldb/leveldb_database.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13using base::StringPiece;
14using content::IndexedDBBackingStore;
15using content::LevelDBComparator;
16using content::LevelDBDatabase;
17using content::LevelDBFactory;
18using content::LevelDBSnapshot;
19
20namespace {
21
22class BustedLevelDBDatabase : public LevelDBDatabase {
23 public:
24  static scoped_ptr<LevelDBDatabase> Open(
25      const base::FilePath& file_name,
26      const LevelDBComparator* /*comparator*/) {
27    return scoped_ptr<LevelDBDatabase>(new BustedLevelDBDatabase);
28  }
29  virtual bool Get(const base::StringPiece& key,
30                   std::string* value,
31                   bool* found,
32                   const LevelDBSnapshot* = 0) OVERRIDE {
33    // false means IO error.
34    return false;
35  }
36};
37
38class MockLevelDBFactory : public LevelDBFactory {
39 public:
40  MockLevelDBFactory() : destroy_called_(false) {}
41  virtual scoped_ptr<LevelDBDatabase> OpenLevelDB(
42      const base::FilePath& file_name,
43      const LevelDBComparator* comparator,
44      bool* is_disk_full = 0) OVERRIDE {
45    return BustedLevelDBDatabase::Open(file_name, comparator);
46  }
47  virtual bool DestroyLevelDB(const base::FilePath& file_name) OVERRIDE {
48    EXPECT_FALSE(destroy_called_);
49    destroy_called_ = true;
50    return false;
51  }
52  virtual ~MockLevelDBFactory() { EXPECT_TRUE(destroy_called_); }
53
54 private:
55  bool destroy_called_;
56};
57
58TEST(IndexedDBIOErrorTest, CleanUpTest) {
59  std::string origin_identifier("http_localhost_81");
60  base::ScopedTempDir temp_directory;
61  ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
62  const base::FilePath path = temp_directory.path();
63  std::string dummy_file_identifier;
64  MockLevelDBFactory mock_leveldb_factory;
65  WebKit::WebIDBCallbacks::DataLoss data_loss =
66      WebKit::WebIDBCallbacks::DataLossNone;
67  scoped_refptr<IndexedDBBackingStore> backing_store =
68      IndexedDBBackingStore::Open(origin_identifier,
69                                  path,
70                                  dummy_file_identifier,
71                                  &data_loss,
72                                  &mock_leveldb_factory);
73}
74
75}  // namespace
76