blob: 2f6a49eb48eb90621acaf3beeea2fe584bf5ed14 [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]ea1a3f62012-11-16 20:34:238#include "base/files/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
[email protected]67361b32011-04-12 20:13:0618class StatementErrorHandler : public sql::ErrorDelegate {
19 public:
[email protected]49dc4f22012-10-17 17:41:1620 StatementErrorHandler(int* error, std::string* sql_text)
21 : error_(error),
22 sql_text_(sql_text) {}
23
24 virtual ~StatementErrorHandler() {}
[email protected]67361b32011-04-12 20:13:0625
26 virtual int OnError(int error, sql::Connection* connection,
[email protected]60e60dd2012-04-28 08:16:0427 sql::Statement* stmt) OVERRIDE {
[email protected]49dc4f22012-10-17 17:41:1628 *error_ = error;
[email protected]67361b32011-04-12 20:13:0629 const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL;
[email protected]49dc4f22012-10-17 17:41:1630 *sql_text_ = sql_txt ? sql_txt : "no statement available";
[email protected]67361b32011-04-12 20:13:0631 return error;
32 }
33
[email protected]67361b32011-04-12 20:13:0634 private:
[email protected]49dc4f22012-10-17 17:41:1635 int* error_;
36 std::string* sql_text_;
37
38 DISALLOW_COPY_AND_ASSIGN(StatementErrorHandler);
[email protected]67361b32011-04-12 20:13:0639};
40
41class SQLiteFeaturesTest : public testing::Test {
42 public:
[email protected]49dc4f22012-10-17 17:41:1643 SQLiteFeaturesTest() : error_(SQLITE_OK) {}
[email protected]67361b32011-04-12 20:13:0644
[email protected]3dbbf7d2013-02-06 18:13:5345 virtual void SetUp() {
[email protected]99034682012-06-13 03:31:1646 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
47 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db")));
48
[email protected]49dc4f22012-10-17 17:41:1649 // 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_));
[email protected]67361b32011-04-12 20:13:0652 }
53
[email protected]3dbbf7d2013-02-06 18:13:5354 virtual void TearDown() {
[email protected]67361b32011-04-12 20:13:0655 // If any error happened the original sql statement can be found in
[email protected]49dc4f22012-10-17 17:41:1656 // |sql_text_|.
57 EXPECT_EQ(SQLITE_OK, error_);
[email protected]67361b32011-04-12 20:13:0658 db_.Close();
[email protected]67361b32011-04-12 20:13:0659 }
60
61 sql::Connection& db() { return db_; }
62
[email protected]49dc4f22012-10-17 17:41:1663 int sqlite_error() const {
64 return error_;
65 }
[email protected]67361b32011-04-12 20:13:0666
67 private:
[email protected]ea1a3f62012-11-16 20:34:2368 base::ScopedTempDir temp_dir_;
[email protected]67361b32011-04-12 20:13:0669 sql::Connection db_;
[email protected]49dc4f22012-10-17 17:41:1670
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_;
[email protected]67361b32011-04-12 20:13:0675};
76
77// Do not include fts1 support, it is not useful, and nobody is
78// looking at it.
79TEST_F(SQLiteFeaturesTest, NoFTS1) {
[email protected]eff1fa522011-12-12 23:50:5980 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(
81 "CREATE VIRTUAL TABLE foo USING fts1(x)"));
[email protected]67361b32011-04-12 20:13:0682}
83
[email protected]e73e89222012-07-13 18:55:2284#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.
[email protected]67361b32011-04-12 20:13:0688// 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}
[email protected]e73e89222012-07-13 18:55:2293#endif
[email protected]67361b32011-04-12 20:13:0694
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