blob: 4fc730c2b8120cb093ed2cb7f84aa4583e127070 [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
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +01007#include "base/bind.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +00008#include "base/file_util.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00009#include "base/files/scoped_temp_dir.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000010#include "sql/connection.h"
11#include "sql/statement.h"
12#include "testing/gtest/include/gtest/gtest.h"
13#include "third_party/sqlite/sqlite3.h"
14
15// Test that certain features are/are-not enabled in our SQLite.
16
17namespace {
18
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010019void CaptureErrorCallback(int* error_pointer, std::string* sql_text,
20 int error, sql::Statement* stmt) {
21 *error_pointer = error;
22 const char* text = stmt ? stmt->GetSQLStatement() : NULL;
23 *sql_text = text ? text : "no statement available";
24}
Torne (Richard Coles)58218062012-11-14 11:43:16 +000025
26class SQLiteFeaturesTest : public testing::Test {
27 public:
28 SQLiteFeaturesTest() : error_(SQLITE_OK) {}
29
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000030 virtual void SetUp() {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000031 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
32 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db")));
33
34 // The error delegate will set |error_| and |sql_text_| when any sqlite
35 // statement operation returns an error code.
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010036 db_.set_error_callback(base::Bind(&CaptureErrorCallback,
37 &error_, &sql_text_));
Torne (Richard Coles)58218062012-11-14 11:43:16 +000038 }
39
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000040 virtual void TearDown() {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000041 // If any error happened the original sql statement can be found in
42 // |sql_text_|.
43 EXPECT_EQ(SQLITE_OK, error_);
44 db_.Close();
45 }
46
47 sql::Connection& db() { return db_; }
48
49 int sqlite_error() const {
50 return error_;
51 }
52
53 private:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000054 base::ScopedTempDir temp_dir_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000055 sql::Connection db_;
56
57 // The error code of the most recent error.
58 int error_;
59 // Original statement which has caused the error.
60 std::string sql_text_;
61};
62
63// Do not include fts1 support, it is not useful, and nobody is
64// looking at it.
65TEST_F(SQLiteFeaturesTest, NoFTS1) {
66 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(
67 "CREATE VIRTUAL TABLE foo USING fts1(x)"));
68}
69
70#if !defined(OS_IOS)
71// fts2 is used for older history files, so we're signed on for keeping our
72// version up-to-date. iOS does not include fts2, so this test does not run on
73// iOS.
74// TODO(shess): Think up a crazy way to get out from having to support
75// this forever.
76TEST_F(SQLiteFeaturesTest, FTS2) {
77 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts2(x)"));
78}
79#endif
80
81// fts3 is used for current history files, and also for WebDatabase.
82TEST_F(SQLiteFeaturesTest, FTS3) {
83 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));
84}
85
86} // namespace