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_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
) OVERRIDE
{
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(); }
42 virtual ~StatementErrorHandler() {}
46 std::string sql_text_
;
49 class SQLiteFeaturesTest
: public testing::Test
{
51 SQLiteFeaturesTest() : error_handler_(new StatementErrorHandler
) {}
54 ASSERT_TRUE(PathService::Get(base::DIR_TEMP
, &path_
));
55 path_
= path_
.AppendASCII("SQLStatementTest.db");
56 file_util::Delete(path_
, false);
57 ASSERT_TRUE(db_
.Open(path_
));
58 // The |error_handler_| will be called if any sqlite statement operation
59 // returns an error code.
60 db_
.set_error_delegate(error_handler_
);
64 // If any error happened the original sql statement can be found in
65 // error_handler_->sql_statement().
66 EXPECT_EQ(SQLITE_OK
, error_handler_
->error());
68 // If this fails something is going on with cleanup and later tests may
69 // fail, so we want to identify problems right away.
70 ASSERT_TRUE(file_util::Delete(path_
, false));
73 sql::Connection
& db() { return db_
; }
75 int sqlite_error() const { return error_handler_
->error(); }
76 void reset_error() const { error_handler_
->reset_error(); }
81 scoped_refptr
<StatementErrorHandler
> error_handler_
;
84 // Do not include fts1 support, it is not useful, and nobody is
86 TEST_F(SQLiteFeaturesTest
, NoFTS1
) {
87 ASSERT_EQ(SQLITE_ERROR
, db().ExecuteAndReturnErrorCode(
88 "CREATE VIRTUAL TABLE foo USING fts1(x)"));
91 // fts2 is used for older history files, so we're signed on for
92 // keeping our version up-to-date.
93 // TODO(shess): Think up a crazy way to get out from having to support
95 TEST_F(SQLiteFeaturesTest
, FTS2
) {
96 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts2(x)"));
99 // fts3 is used for current history files, and also for WebDatabase.
100 TEST_F(SQLiteFeaturesTest
, FTS3
) {
101 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));