Roll src/third_party/WebKit 605a979:06cb9e9 (svn 202556:202558)
[chromium-blink-merge.git] / sql / mojo / sql_test_base.h
blobd39198da96834b68a67e98df3d9ea8ec098385fc
1 // Copyright 2015 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 #ifndef SQL_MOJO_SQL_TEST_BASE_H_
6 #define SQL_MOJO_SQL_TEST_BASE_H_
8 #include "base/files/file_path.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "components/filesystem/public/interfaces/file_system.mojom.h"
11 #include "mojo/application/public/cpp/application_test_base.h"
12 #include "mojo/public/cpp/bindings/binding.h"
13 #include "sql/connection.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace sql {
18 class Connection;
19 class ScopedMojoFilesystemVFS;
21 // Base class for SQL tests.
23 // WARNING: We want to run the same gtest based unit test code both against
24 // chromium (which uses this implementation here), and the mojo code (which
25 // uses a different class named SQLTestBase). These two classes need to have
26 // the same interface because we compile time switch them based on a
27 // #define. We need to have two different implementations because the mojo
28 // version derives from mojo::test::ApplicationTestBase instead of
29 // testing::Test.
30 class SQLTestBase : public mojo::test::ApplicationTestBase,
31 public filesystem::FileSystemClient {
32 public:
33 SQLTestBase();
34 ~SQLTestBase() override;
36 enum WriteJunkType {
37 TYPE_OVERWRITE_AND_TRUNCATE,
38 TYPE_OVERWRITE
41 // Returns the path to the database.
42 base::FilePath db_path();
44 // Returns a connection to the database at db_path().
45 sql::Connection& db();
47 // Closes the current connection to the database and reopens it.
48 bool Reopen();
50 // Proxying method around base::PathExists.
51 bool GetPathExists(const base::FilePath& path);
53 // SQLite stores the database size in the header, and if the actual
54 // OS-derived size is smaller, the database is considered corrupt.
55 // [This case is actually a common form of corruption in the wild.]
56 // This helper sets the in-header size to one page larger than the
57 // actual file size. The resulting file will return SQLITE_CORRUPT
58 // for most operations unless PRAGMA writable_schema is turned ON.
60 // Returns false if any error occurs accessing the file.
61 bool CorruptSizeInHeaderOfDB();
63 // Writes junk to the start of the file.
64 void WriteJunkToDatabase(WriteJunkType type);
66 // Sets the database file size to 0.
67 void TruncateDatabase();
69 // Overridden from testing::Test:
70 void SetUp() override;
71 void TearDown() override;
73 // Overridden from FileSystemClient:
74 void OnFileSystemShutdown() override;
76 protected:
77 filesystem::FileSystemPtr& files() { return files_; }
79 private:
80 filesystem::FileSystemPtr files_;
82 scoped_ptr<ScopedMojoFilesystemVFS> vfs_;
83 mojo::Binding<filesystem::FileSystemClient> binding_;
84 sql::Connection db_;
86 DISALLOW_COPY_AND_ASSIGN(SQLTestBase);
89 } // namespace sql
91 #endif // SQL_MOJO_SQL_TEST_BASE_H_