Infobar material design refresh: bg color
[chromium-blink-merge.git] / sql / mojo / sql_test_base.cc
blob107411342bf8fe51f2446e9ea95a3a79890901b0
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 #include "sql/mojo/sql_test_base.h"
7 #include "mojo/application/public/cpp/application_impl.h"
8 #include "mojo/util/capture_util.h"
9 #include "sql/mojo/mojo_vfs.h"
10 #include "sql/test/test_helpers.h"
12 using mojo::Capture;
14 namespace sql {
16 SQLTestBase::SQLTestBase()
17 : binding_(this) {
20 SQLTestBase::~SQLTestBase() {
23 base::FilePath SQLTestBase::db_path() {
24 return base::FilePath(FILE_PATH_LITERAL("SQLTest.db"));
27 sql::Connection& SQLTestBase::db() {
28 return db_;
31 bool SQLTestBase::Reopen() {
32 db_.Close();
33 return db_.Open(db_path());
36 bool SQLTestBase::GetPathExists(const base::FilePath& path) {
37 filesystem::FileError error = filesystem::FILE_ERROR_FAILED;
38 bool exists = false;
39 vfs_->GetDirectory()->Exists(path.AsUTF8Unsafe(), Capture(&error, &exists));
40 vfs_->GetDirectory().WaitForIncomingResponse();
41 if (error != filesystem::FILE_ERROR_OK)
42 return false;
43 return exists;
46 bool SQLTestBase::CorruptSizeInHeaderOfDB() {
47 // See http://www.sqlite.org/fileformat.html#database_header
48 const size_t kHeaderSize = 100;
50 mojo::Array<uint8_t> header;
52 filesystem::FileError error = filesystem::FILE_ERROR_FAILED;
53 filesystem::FilePtr file_ptr;
54 vfs_->GetDirectory()->OpenFile(
55 mojo::String(db_path().AsUTF8Unsafe()), GetProxy(&file_ptr),
56 filesystem::kFlagRead | filesystem::kFlagWrite |
57 filesystem::kFlagOpenAlways,
58 Capture(&error));
59 vfs_->GetDirectory().WaitForIncomingResponse();
60 if (error != filesystem::FILE_ERROR_OK)
61 return false;
63 file_ptr->Read(kHeaderSize, 0, filesystem::WHENCE_FROM_BEGIN,
64 Capture(&error, &header));
65 file_ptr.WaitForIncomingResponse();
66 if (error != filesystem::FILE_ERROR_OK)
67 return false;
69 filesystem::FileInformationPtr info;
70 file_ptr->Stat(Capture(&error, &info));
71 file_ptr.WaitForIncomingResponse();
72 if (error != filesystem::FILE_ERROR_OK)
73 return false;
74 int64_t db_size = info->size;
76 test::CorruptSizeInHeaderMemory(&header.front(), db_size);
78 uint32_t num_bytes_written = 0;
79 file_ptr->Write(header.Pass(), 0, filesystem::WHENCE_FROM_BEGIN,
80 Capture(&error, &num_bytes_written));
81 file_ptr.WaitForIncomingResponse();
82 if (error != filesystem::FILE_ERROR_OK)
83 return false;
84 if (num_bytes_written != kHeaderSize)
85 return false;
87 return true;
90 void SQLTestBase::WriteJunkToDatabase(WriteJunkType type) {
91 uint32_t flags = 0;
92 if (type == TYPE_OVERWRITE_AND_TRUNCATE)
93 flags = filesystem::kFlagWrite | filesystem::kFlagCreate;
94 else
95 flags = filesystem::kFlagWrite | filesystem::kFlagOpen;
97 filesystem::FileError error = filesystem::FILE_ERROR_FAILED;
98 filesystem::FilePtr file_ptr;
99 vfs_->GetDirectory()->OpenFile(
100 mojo::String(db_path().AsUTF8Unsafe()), GetProxy(&file_ptr),
101 flags,
102 Capture(&error));
103 vfs_->GetDirectory().WaitForIncomingResponse();
104 if (error != filesystem::FILE_ERROR_OK)
105 return;
107 const char* kJunk = "Now is the winter of our discontent.";
108 mojo::Array<uint8_t> data(strlen(kJunk));
109 memcpy(&data.front(), kJunk, strlen(kJunk));
111 uint32_t num_bytes_written = 0;
112 file_ptr->Write(data.Pass(), 0, filesystem::WHENCE_FROM_BEGIN,
113 Capture(&error, &num_bytes_written));
114 file_ptr.WaitForIncomingResponse();
117 void SQLTestBase::TruncateDatabase() {
118 filesystem::FileError error = filesystem::FILE_ERROR_FAILED;
119 filesystem::FilePtr file_ptr;
120 vfs_->GetDirectory()->OpenFile(
121 mojo::String(db_path().AsUTF8Unsafe()), GetProxy(&file_ptr),
122 filesystem::kFlagWrite | filesystem::kFlagOpen,
123 Capture(&error));
124 vfs_->GetDirectory().WaitForIncomingResponse();
125 if (error != filesystem::FILE_ERROR_OK)
126 return;
128 file_ptr->Truncate(0, Capture(&error));
129 file_ptr.WaitForIncomingResponse();
130 ASSERT_EQ(filesystem::FILE_ERROR_OK, error);
133 void SQLTestBase::SetUp() {
134 ApplicationTestBase::SetUp();
136 mojo::URLRequestPtr request(mojo::URLRequest::New());
137 request->url = mojo::String::From("mojo:filesystem");
138 application_impl()->ConnectToService(request.Pass(), &files_);
140 filesystem::FileSystemClientPtr client;
141 binding_.Bind(GetProxy(&client));
143 filesystem::FileError error = filesystem::FILE_ERROR_FAILED;
144 filesystem::DirectoryPtr directory;
145 files()->OpenFileSystem("temp", GetProxy(&directory), client.Pass(),
146 Capture(&error));
147 ASSERT_TRUE(files().WaitForIncomingResponse());
148 ASSERT_EQ(filesystem::FILE_ERROR_OK, error);
150 vfs_.reset(new ScopedMojoFilesystemVFS(directory.Pass()));
151 ASSERT_TRUE(db_.Open(db_path()));
154 void SQLTestBase::TearDown() {
155 db_.Close();
156 vfs_.reset();
158 ApplicationTestBase::TearDown();
161 void SQLTestBase::OnFileSystemShutdown() {
164 } // namespace sql