blob: 08e18194594b7078f8704a15ffc6e55541007a51 [file] [log] [blame]
[email protected]60e60dd2012-04-28 08:16:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]67361b32011-04-12 20:13:062// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <string>
6
[email protected]67361b32011-04-12 20:13:067#include "base/file_util.h"
[email protected]99034682012-06-13 03:31:168#include "base/scoped_temp_dir.h"
[email protected]f0a54b22011-07-19 18:40:219#include "sql/connection.h"
10#include "sql/statement.h"
[email protected]67361b32011-04-12 20:13:0611#include "testing/gtest/include/gtest/gtest.h"
12#include "third_party/sqlite/sqlite3.h"
13
14// Test that certain features are/are-not enabled in our SQLite.
15
16namespace {
17
18
19class StatementErrorHandler : public sql::ErrorDelegate {
20 public:
21 StatementErrorHandler() : error_(SQLITE_OK) {}
22
23 virtual int OnError(int error, sql::Connection* connection,
[email protected]60e60dd2012-04-28 08:16:0424 sql::Statement* stmt) OVERRIDE {
[email protected]67361b32011-04-12 20:13:0625 error_ = error;
26 const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL;
27 sql_text_ = sql_txt ? sql_txt : "no statement available";
28 return error;
29 }
30
31 int error() const { return error_; }
32
33 void reset_error() {
34 sql_text_.clear();
35 error_ = SQLITE_OK;
36 }
37
38 const char* sql_statement() const { return sql_text_.c_str(); }
39
[email protected]60e60dd2012-04-28 08:16:0440 protected:
41 virtual ~StatementErrorHandler() {}
42
[email protected]67361b32011-04-12 20:13:0643 private:
44 int error_;
45 std::string sql_text_;
46};
47
48class SQLiteFeaturesTest : public testing::Test {
49 public:
50 SQLiteFeaturesTest() : error_handler_(new StatementErrorHandler) {}
51
52 void SetUp() {
[email protected]99034682012-06-13 03:31:1653 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
54 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db")));
55
[email protected]67361b32011-04-12 20:13:0656 // The |error_handler_| will be called if any sqlite statement operation
57 // returns an error code.
58 db_.set_error_delegate(error_handler_);
59 }
60
61 void TearDown() {
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());
65 db_.Close();
[email protected]67361b32011-04-12 20:13:0666 }
67
68 sql::Connection& db() { return db_; }
69
70 int sqlite_error() const { return error_handler_->error(); }
71 void reset_error() const { error_handler_->reset_error(); }
72
73 private:
[email protected]99034682012-06-13 03:31:1674 ScopedTempDir temp_dir_;
[email protected]67361b32011-04-12 20:13:0675 sql::Connection db_;
76 scoped_refptr<StatementErrorHandler> error_handler_;
77};
78
79// Do not include fts1 support, it is not useful, and nobody is
80// looking at it.
81TEST_F(SQLiteFeaturesTest, NoFTS1) {
[email protected]eff1fa522011-12-12 23:50:5982 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(
83 "CREATE VIRTUAL TABLE foo USING fts1(x)"));
[email protected]67361b32011-04-12 20:13:0684}
85
86// fts2 is used for older history files, so we're signed on for
87// keeping our version up-to-date.
88// TODO(shess): Think up a crazy way to get out from having to support
89// this forever.
90TEST_F(SQLiteFeaturesTest, FTS2) {
91 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts2(x)"));
92}
93
94// fts3 is used for current history files, and also for WebDatabase.
95TEST_F(SQLiteFeaturesTest, FTS3) {
96 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));
97}
98
99} // namespace