1 // Copyright (c) 2012 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_util.h"
8 #include "base/scoped_temp_dir.h"
9 #include "sql/connection.h"
10 #include "sql/statement.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/sqlite/sqlite3.h"
14 // Test that certain features are/are-not enabled in our SQLite.
19 class StatementErrorHandler
: public sql::ErrorDelegate
{
21 StatementErrorHandler() : error_(SQLITE_OK
) {}
23 virtual int OnError(int error
, sql::Connection
* connection
,
24 sql::Statement
* stmt
) OVERRIDE
{
26 const char* sql_txt
= stmt
? stmt
->GetSQLStatement() : NULL
;
27 sql_text_
= sql_txt
? sql_txt
: "no statement available";
31 int error() const { return error_
; }
38 const char* sql_statement() const { return sql_text_
.c_str(); }
41 virtual ~StatementErrorHandler() {}
45 std::string sql_text_
;
48 class SQLiteFeaturesTest
: public testing::Test
{
50 SQLiteFeaturesTest() : error_handler_(new StatementErrorHandler
) {}
53 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
54 ASSERT_TRUE(db_
.Open(temp_dir_
.path().AppendASCII("SQLStatementTest.db")));
56 // The |error_handler_| will be called if any sqlite statement operation
57 // returns an error code.
58 db_
.set_error_delegate(error_handler_
);
62 // If any error happened the original sql statement can be found in
63 // error_handler_->sql_statement().
64 EXPECT_EQ(SQLITE_OK
, error_handler_
->error());
68 sql::Connection
& db() { return db_
; }
70 int sqlite_error() const { return error_handler_
->error(); }
71 void reset_error() const { error_handler_
->reset_error(); }
74 ScopedTempDir temp_dir_
;
76 scoped_refptr
<StatementErrorHandler
> error_handler_
;
79 // Do not include fts1 support, it is not useful, and nobody is
81 TEST_F(SQLiteFeaturesTest
, NoFTS1
) {
82 ASSERT_EQ(SQLITE_ERROR
, db().ExecuteAndReturnErrorCode(
83 "CREATE VIRTUAL TABLE foo USING fts1(x)"));
87 // fts2 is used for older history files, so we're signed on for keeping our
88 // version up-to-date. iOS does not include fts2, so this test does not run on
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)"));
97 // fts3 is used for current history files, and also for WebDatabase.
98 TEST_F(SQLiteFeaturesTest
, FTS3
) {
99 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));