Fix DropdownPopupWindow accessibility.
[chromium-blink-merge.git] / third_party / leveldatabase / env_chromium_unittest.cc
blob46be76822e50f0a9506eaffe6953799922374293
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.
5 #include "base/files/file.h"
6 #include "base/files/file_enumerator.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/test/test_suite.h"
11 #include "third_party/leveldatabase/env_chromium.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/leveldatabase/env_idb.h"
14 #include "third_party/leveldatabase/src/include/leveldb/db.h"
16 #define FPL FILE_PATH_LITERAL
18 using leveldb::DB;
19 using leveldb::Env;
20 using leveldb::IDBEnv;
21 using leveldb::Options;
22 using leveldb::ReadOptions;
23 using leveldb::Slice;
24 using leveldb::Status;
25 using leveldb::WritableFile;
26 using leveldb::WriteOptions;
27 using leveldb_env::ChromiumEnv;
28 using leveldb_env::MethodID;
30 TEST(ErrorEncoding, OnlyAMethod) {
31 const MethodID in_method = leveldb_env::kSequentialFileRead;
32 const Status s = MakeIOError("Somefile.txt", "message", in_method);
33 MethodID method;
34 int error = -75;
35 EXPECT_EQ(leveldb_env::METHOD_ONLY,
36 ParseMethodAndError(s.ToString().c_str(), &method, &error));
37 EXPECT_EQ(in_method, method);
38 EXPECT_EQ(-75, error);
41 TEST(ErrorEncoding, FileError) {
42 const MethodID in_method = leveldb_env::kWritableFileClose;
43 const base::File::Error fe = base::File::FILE_ERROR_INVALID_OPERATION;
44 const Status s = MakeIOError("Somefile.txt", "message", in_method, fe);
45 MethodID method;
46 int error;
47 EXPECT_EQ(leveldb_env::METHOD_AND_PFE,
48 ParseMethodAndError(s.ToString().c_str(), &method, &error));
49 EXPECT_EQ(in_method, method);
50 EXPECT_EQ(fe, error);
53 TEST(ErrorEncoding, NoEncodedMessage) {
54 Status s = Status::IOError("Some message", "from leveldb itself");
55 MethodID method = leveldb_env::kRandomAccessFileRead;
56 int error = 4;
57 EXPECT_EQ(leveldb_env::NONE,
58 ParseMethodAndError(s.ToString().c_str(), &method, &error));
59 EXPECT_EQ(leveldb_env::kRandomAccessFileRead, method);
60 EXPECT_EQ(4, error);
63 template <typename T>
64 class MyEnv : public T {
65 public:
66 MyEnv() : directory_syncs_(0) {}
67 int directory_syncs() { return directory_syncs_; }
69 protected:
70 virtual void DidSyncDir(const std::string& fname) {
71 ++directory_syncs_;
72 leveldb_env::ChromiumEnv::DidSyncDir(fname);
75 private:
76 int directory_syncs_;
79 template <typename T>
80 class ChromiumEnvMultiPlatformTests : public ::testing::Test {
81 public:
84 typedef ::testing::Types<ChromiumEnv> ChromiumEnvMultiPlatformTestsTypes;
85 TYPED_TEST_CASE(ChromiumEnvMultiPlatformTests,
86 ChromiumEnvMultiPlatformTestsTypes);
88 TYPED_TEST(ChromiumEnvMultiPlatformTests, DirectorySyncing) {
89 MyEnv<TypeParam> env;
91 base::ScopedTempDir dir;
92 ASSERT_TRUE(dir.CreateUniqueTempDir());
93 base::FilePath dir_path = dir.path();
94 std::string some_data = "some data";
95 Slice data = some_data;
97 std::string manifest_file_name =
98 dir_path.Append(FILE_PATH_LITERAL("MANIFEST-001")).AsUTF8Unsafe();
99 WritableFile* manifest_file_ptr;
100 Status s = env.NewWritableFile(manifest_file_name, &manifest_file_ptr);
101 EXPECT_TRUE(s.ok());
102 scoped_ptr<WritableFile> manifest_file(manifest_file_ptr);
103 manifest_file->Append(data);
104 EXPECT_EQ(0, env.directory_syncs());
105 manifest_file->Append(data);
106 EXPECT_EQ(0, env.directory_syncs());
108 std::string sst_file_name =
109 dir_path.Append(FILE_PATH_LITERAL("000003.sst")).AsUTF8Unsafe();
110 WritableFile* sst_file_ptr;
111 s = env.NewWritableFile(sst_file_name, &sst_file_ptr);
112 EXPECT_TRUE(s.ok());
113 scoped_ptr<WritableFile> sst_file(sst_file_ptr);
114 sst_file->Append(data);
115 EXPECT_EQ(0, env.directory_syncs());
117 manifest_file->Append(data);
118 EXPECT_EQ(1, env.directory_syncs());
119 manifest_file->Append(data);
120 EXPECT_EQ(1, env.directory_syncs());
123 int CountFilesWithExtension(const base::FilePath& dir,
124 const base::FilePath::StringType& extension) {
125 int matching_files = 0;
126 base::FileEnumerator dir_reader(
127 dir, false, base::FileEnumerator::FILES);
128 for (base::FilePath fname = dir_reader.Next(); !fname.empty();
129 fname = dir_reader.Next()) {
130 if (fname.MatchesExtension(extension))
131 matching_files++;
133 return matching_files;
136 bool GetFirstLDBFile(const base::FilePath& dir, base::FilePath* ldb_file) {
137 base::FileEnumerator dir_reader(
138 dir, false, base::FileEnumerator::FILES);
139 for (base::FilePath fname = dir_reader.Next(); !fname.empty();
140 fname = dir_reader.Next()) {
141 if (fname.MatchesExtension(FPL(".ldb"))) {
142 *ldb_file = fname;
143 return true;
146 return false;
149 TEST(ChromiumEnv, BackupTables) {
150 Options options;
151 options.create_if_missing = true;
152 options.env = IDBEnv();
154 base::ScopedTempDir scoped_temp_dir;
155 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
156 base::FilePath dir = scoped_temp_dir.path();
158 DB* db;
159 Status status = DB::Open(options, dir.AsUTF8Unsafe(), &db);
160 EXPECT_TRUE(status.ok()) << status.ToString();
161 status = db->Put(WriteOptions(), "key", "value");
162 EXPECT_TRUE(status.ok()) << status.ToString();
163 Slice a = "a";
164 Slice z = "z";
165 db->CompactRange(&a, &z);
166 int ldb_files = CountFilesWithExtension(dir, FPL(".ldb"));
167 int bak_files = CountFilesWithExtension(dir, FPL(".bak"));
168 EXPECT_GT(ldb_files, 0);
169 EXPECT_EQ(ldb_files, bak_files);
170 base::FilePath ldb_file;
171 EXPECT_TRUE(GetFirstLDBFile(dir, &ldb_file));
172 delete db;
173 EXPECT_TRUE(base::DeleteFile(ldb_file, false));
174 EXPECT_EQ(ldb_files - 1, CountFilesWithExtension(dir, FPL(".ldb")));
176 // The ldb file deleted above should be restored in Open.
177 status = leveldb::DB::Open(options, dir.AsUTF8Unsafe(), &db);
178 EXPECT_TRUE(status.ok()) << status.ToString();
179 std::string value;
180 status = db->Get(ReadOptions(), "key", &value);
181 EXPECT_TRUE(status.ok()) << status.ToString();
182 EXPECT_EQ("value", value);
183 delete db;
185 // Ensure that deleting an ldb file also deletes its backup.
186 int orig_ldb_files = CountFilesWithExtension(dir, FPL(".ldb"));
187 EXPECT_GT(ldb_files, 0);
188 EXPECT_EQ(ldb_files, bak_files);
189 EXPECT_TRUE(GetFirstLDBFile(dir, &ldb_file));
190 options.env->DeleteFile(ldb_file.AsUTF8Unsafe());
191 ldb_files = CountFilesWithExtension(dir, FPL(".ldb"));
192 bak_files = CountFilesWithExtension(dir, FPL(".bak"));
193 EXPECT_EQ(orig_ldb_files - 1, ldb_files);
194 EXPECT_EQ(bak_files, ldb_files);
197 TEST(ChromiumEnv, GetChildrenEmptyDir) {
198 base::ScopedTempDir scoped_temp_dir;
199 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
200 base::FilePath dir = scoped_temp_dir.path();
202 Env* env = IDBEnv();
203 std::vector<std::string> result;
204 leveldb::Status status = env->GetChildren(dir.AsUTF8Unsafe(), &result);
205 EXPECT_TRUE(status.ok());
206 EXPECT_EQ(0U, result.size());
209 TEST(ChromiumEnv, GetChildrenPriorResults) {
210 base::ScopedTempDir scoped_temp_dir;
211 ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
212 base::FilePath dir = scoped_temp_dir.path();
214 base::FilePath new_file_dir = dir.Append(FPL("tmp_file"));
215 FILE* f = fopen(new_file_dir.AsUTF8Unsafe().c_str(), "w");
216 if (f) {
217 fputs("Temp file contents", f);
218 fclose(f);
221 Env* env = IDBEnv();
222 std::vector<std::string> result;
223 leveldb::Status status = env->GetChildren(dir.AsUTF8Unsafe(), &result);
224 EXPECT_TRUE(status.ok());
225 EXPECT_EQ(1U, result.size());
227 // And a second time should also return one result
228 status = env->GetChildren(dir.AsUTF8Unsafe(), &result);
229 EXPECT_TRUE(status.ok());
230 EXPECT_EQ(1U, result.size());
233 int main(int argc, char** argv) { return base::TestSuite(argc, argv).Run(); }