[email protected] | 60e60dd | 2012-04-28 08:16:04 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 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 | |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 7 | #include "base/file_util.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 8 | #include "base/files/scoped_temp_dir.h" |
[email protected] | f0a54b2 | 2011-07-19 18:40:21 | [diff] [blame] | 9 | #include "sql/connection.h" |
| 10 | #include "sql/statement.h" |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 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 | |
| 16 | namespace { |
| 17 | |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 18 | class StatementErrorHandler : public sql::ErrorDelegate { |
| 19 | public: |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 20 | StatementErrorHandler(int* error, std::string* sql_text) |
| 21 | : error_(error), |
| 22 | sql_text_(sql_text) {} |
| 23 | |
| 24 | virtual ~StatementErrorHandler() {} |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 25 | |
| 26 | virtual int OnError(int error, sql::Connection* connection, |
[email protected] | 60e60dd | 2012-04-28 08:16:04 | [diff] [blame] | 27 | sql::Statement* stmt) OVERRIDE { |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 28 | *error_ = error; |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 29 | const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL; |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 30 | *sql_text_ = sql_txt ? sql_txt : "no statement available"; |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 31 | return error; |
| 32 | } |
| 33 | |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 34 | private: |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 35 | int* error_; |
| 36 | std::string* sql_text_; |
| 37 | |
| 38 | DISALLOW_COPY_AND_ASSIGN(StatementErrorHandler); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | class SQLiteFeaturesTest : public testing::Test { |
| 42 | public: |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 43 | SQLiteFeaturesTest() : error_(SQLITE_OK) {} |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 44 | |
[email protected] | 3dbbf7d | 2013-02-06 18:13:53 | [diff] [blame] | 45 | virtual void SetUp() { |
[email protected] | 9903468 | 2012-06-13 03:31:16 | [diff] [blame] | 46 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 47 | ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db"))); |
| 48 | |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 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_)); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 52 | } |
| 53 | |
[email protected] | 3dbbf7d | 2013-02-06 18:13:53 | [diff] [blame] | 54 | virtual void TearDown() { |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 55 | // If any error happened the original sql statement can be found in |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 56 | // |sql_text_|. |
| 57 | EXPECT_EQ(SQLITE_OK, error_); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 58 | db_.Close(); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | sql::Connection& db() { return db_; } |
| 62 | |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 63 | int sqlite_error() const { |
| 64 | return error_; |
| 65 | } |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 66 | |
| 67 | private: |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 68 | base::ScopedTempDir temp_dir_; |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 69 | sql::Connection db_; |
[email protected] | 49dc4f2 | 2012-10-17 17:41:16 | [diff] [blame] | 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_; |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | // Do not include fts1 support, it is not useful, and nobody is |
| 78 | // looking at it. |
| 79 | TEST_F(SQLiteFeaturesTest, NoFTS1) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 80 | ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode( |
| 81 | "CREATE VIRTUAL TABLE foo USING fts1(x)")); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 82 | } |
| 83 | |
[email protected] | e73e8922 | 2012-07-13 18:55:22 | [diff] [blame] | 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. |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 88 | // TODO(shess): Think up a crazy way to get out from having to support |
| 89 | // this forever. |
| 90 | TEST_F(SQLiteFeaturesTest, FTS2) { |
| 91 | ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts2(x)")); |
| 92 | } |
[email protected] | e73e8922 | 2012-07-13 18:55:22 | [diff] [blame] | 93 | #endif |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 94 | |
| 95 | // fts3 is used for current history files, and also for WebDatabase. |
| 96 | TEST_F(SQLiteFeaturesTest, FTS3) { |
| 97 | ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)")); |
| 98 | } |
| 99 | |
| 100 | } // namespace |