1 // Copyright (c) 2011 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.
7 #include "base/file_path.h"
8 #include "base/file_util.h"
9 #include "base/path_service.h"
10 #include "sql/connection.h"
11 #include "sql/statement.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/sqlite/sqlite3.h"
15 // Test that certain features are/are-not enabled in our SQLite.
20 class StatementErrorHandler
: public sql::ErrorDelegate
{
22 StatementErrorHandler() : error_(SQLITE_OK
) {}
24 virtual int OnError(int error
, sql::Connection
* connection
,
25 sql::Statement
* stmt
) {
27 const char* sql_txt
= stmt
? stmt
->GetSQLStatement() : NULL
;
28 sql_text_
= sql_txt
? sql_txt
: "no statement available";
32 int error() const { return error_
; }
39 const char* sql_statement() const { return sql_text_
.c_str(); }
43 std::string sql_text_
;
46 class SQLiteFeaturesTest
: public testing::Test
{
48 SQLiteFeaturesTest() : error_handler_(new StatementErrorHandler
) {}
51 ASSERT_TRUE(PathService::Get(base::DIR_TEMP
, &path_
));
52 path_
= path_
.AppendASCII("SQLStatementTest.db");
53 file_util::Delete(path_
, false);
54 ASSERT_TRUE(db_
.Open(path_
));
55 // The |error_handler_| will be called if any sqlite statement operation
56 // returns an error code.
57 db_
.set_error_delegate(error_handler_
);
61 // If any error happened the original sql statement can be found in
62 // error_handler_->sql_statement().
63 EXPECT_EQ(SQLITE_OK
, error_handler_
->error());
65 // If this fails something is going on with cleanup and later tests may
66 // fail, so we want to identify problems right away.
67 ASSERT_TRUE(file_util::Delete(path_
, false));
70 sql::Connection
& db() { return db_
; }
72 int sqlite_error() const { return error_handler_
->error(); }
73 void reset_error() const { error_handler_
->reset_error(); }
78 scoped_refptr
<StatementErrorHandler
> error_handler_
;
81 // Do not include fts1 support, it is not useful, and nobody is
83 TEST_F(SQLiteFeaturesTest
, NoFTS1
) {
84 ASSERT_EQ(SQLITE_ERROR
, db().ExecuteAndReturnErrorCode(
85 "CREATE VIRTUAL TABLE foo USING fts1(x)"));
88 // fts2 is used for older history files, so we're signed on for
89 // keeping our version up-to-date.
90 // TODO(shess): Think up a crazy way to get out from having to support
92 TEST_F(SQLiteFeaturesTest
, FTS2
) {
93 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts2(x)"));
96 // fts3 is used for current history files, and also for WebDatabase.
97 TEST_F(SQLiteFeaturesTest
, FTS3
) {
98 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));