[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] | 9903468 | 2012-06-13 03:31:16 | [diff] [blame^] | 8 | #include "base/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 | |
| 18 | |
| 19 | class StatementErrorHandler : public sql::ErrorDelegate { |
| 20 | public: |
| 21 | StatementErrorHandler() : error_(SQLITE_OK) {} |
| 22 | |
| 23 | virtual int OnError(int error, sql::Connection* connection, |
[email protected] | 60e60dd | 2012-04-28 08:16:04 | [diff] [blame] | 24 | sql::Statement* stmt) OVERRIDE { |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 25 | 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] | 60e60dd | 2012-04-28 08:16:04 | [diff] [blame] | 40 | protected: |
| 41 | virtual ~StatementErrorHandler() {} |
| 42 | |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 43 | private: |
| 44 | int error_; |
| 45 | std::string sql_text_; |
| 46 | }; |
| 47 | |
| 48 | class SQLiteFeaturesTest : public testing::Test { |
| 49 | public: |
| 50 | SQLiteFeaturesTest() : error_handler_(new StatementErrorHandler) {} |
| 51 | |
| 52 | void SetUp() { |
[email protected] | 9903468 | 2012-06-13 03:31:16 | [diff] [blame^] | 53 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 54 | ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db"))); |
| 55 | |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 56 | // 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] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 66 | } |
| 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] | 9903468 | 2012-06-13 03:31:16 | [diff] [blame^] | 74 | ScopedTempDir temp_dir_; |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 75 | 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. |
| 81 | TEST_F(SQLiteFeaturesTest, NoFTS1) { |
[email protected] | eff1fa52 | 2011-12-12 23:50:59 | [diff] [blame] | 82 | ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode( |
| 83 | "CREATE VIRTUAL TABLE foo USING fts1(x)")); |
[email protected] | 67361b3 | 2011-04-12 20:13:06 | [diff] [blame] | 84 | } |
| 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. |
| 90 | TEST_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. |
| 95 | TEST_F(SQLiteFeaturesTest, FTS3) { |
| 96 | ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)")); |
| 97 | } |
| 98 | |
| 99 | } // namespace |