blob: 7e750e97c6bc4d14827a71fc105d578973e506a3 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
4
5#include <string>
6
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"
13
14// Test that certain features are/are-not enabled in our SQLite.
15
16namespace {
17
18class StatementErrorHandler : public sql::ErrorDelegate {
19 public:
20 StatementErrorHandler(int* error, std::string* sql_text)
21 : error_(error),
22 sql_text_(sql_text) {}
23
24 virtual ~StatementErrorHandler() {}
25
26 virtual int OnError(int error, sql::Connection* connection,
27 sql::Statement* stmt) OVERRIDE {
28 *error_ = error;
29 const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL;
30 *sql_text_ = sql_txt ? sql_txt : "no statement available";
31 return error;
32 }
33
34 private:
35 int* error_;
36 std::string* sql_text_;
37
38 DISALLOW_COPY_AND_ASSIGN(StatementErrorHandler);
39};
40
41class SQLiteFeaturesTest : public testing::Test {
42 public:
43 SQLiteFeaturesTest() : error_(SQLITE_OK) {}
44
45 void SetUp() {
46 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
47 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db")));
48
49 // The error delegate will set |error_| and |sql_text_| when any sqlite
50 // statement operation returns an error code.
51 db_.set_error_delegate(new StatementErrorHandler(&error_, &sql_text_));
52 }
53
54 void TearDown() {
55 // If any error happened the original sql statement can be found in
56 // |sql_text_|.
57 EXPECT_EQ(SQLITE_OK, error_);
58 db_.Close();
59 }
60
61 sql::Connection& db() { return db_; }
62
63 int sqlite_error() const {
64 return error_;
65 }
66
67 private:
68 ScopedTempDir temp_dir_;
69 sql::Connection db_;
70
71 // The error code of the most recent error.
72 int error_;
73 // Original statement which has caused the error.
74 std::string sql_text_;
75};
76
77// Do not include fts1 support, it is not useful, and nobody is
78// looking at it.
79TEST_F(SQLiteFeaturesTest, NoFTS1) {
80 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(
81 "CREATE VIRTUAL TABLE foo USING fts1(x)"));
82}
83
84#if !defined(OS_IOS)
85// fts2 is used for older history files, so we're signed on for keeping our
86// version up-to-date. iOS does not include fts2, so this test does not run on
87// iOS.
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#endif
94
95// fts3 is used for current history files, and also for WebDatabase.
96TEST_F(SQLiteFeaturesTest, FTS3) {
97 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));
98}
99
100} // namespace