OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/scoped_temp_dir.h" | 8 #include "base/scoped_temp_dir.h" |
9 #include "sql/connection.h" | 9 #include "sql/connection.h" |
10 #include "sql/statement.h" | 10 #include "sql/statement.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "third_party/sqlite/sqlite3.h" | 12 #include "third_party/sqlite/sqlite3.h" |
13 | 13 |
14 // Test that certain features are/are-not enabled in our SQLite. | 14 // Test that certain features are/are-not enabled in our SQLite. |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 | |
19 class StatementErrorHandler : public sql::ErrorDelegate { | 18 class StatementErrorHandler : public sql::ErrorDelegate { |
20 public: | 19 public: |
21 StatementErrorHandler() : error_(SQLITE_OK) {} | 20 StatementErrorHandler(int* error, std::string* sql_text) |
| 21 : error_(error), |
| 22 sql_text_(sql_text) {} |
| 23 |
| 24 virtual ~StatementErrorHandler() {} |
22 | 25 |
23 virtual int OnError(int error, sql::Connection* connection, | 26 virtual int OnError(int error, sql::Connection* connection, |
24 sql::Statement* stmt) OVERRIDE { | 27 sql::Statement* stmt) OVERRIDE { |
25 error_ = error; | 28 *error_ = error; |
26 const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL; | 29 const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL; |
27 sql_text_ = sql_txt ? sql_txt : "no statement available"; | 30 *sql_text_ = sql_txt ? sql_txt : "no statement available"; |
28 return error; | 31 return error; |
29 } | 32 } |
30 | 33 |
31 int error() const { return error_; } | 34 private: |
| 35 int* error_; |
| 36 std::string* sql_text_; |
32 | 37 |
33 void reset_error() { | 38 DISALLOW_COPY_AND_ASSIGN(StatementErrorHandler); |
34 sql_text_.clear(); | |
35 error_ = SQLITE_OK; | |
36 } | |
37 | |
38 const char* sql_statement() const { return sql_text_.c_str(); } | |
39 | |
40 protected: | |
41 virtual ~StatementErrorHandler() {} | |
42 | |
43 private: | |
44 int error_; | |
45 std::string sql_text_; | |
46 }; | 39 }; |
47 | 40 |
48 class SQLiteFeaturesTest : public testing::Test { | 41 class SQLiteFeaturesTest : public testing::Test { |
49 public: | 42 public: |
50 SQLiteFeaturesTest() : error_handler_(new StatementErrorHandler) {} | 43 SQLiteFeaturesTest() : error_(SQLITE_OK) {} |
51 | 44 |
52 void SetUp() { | 45 void SetUp() { |
53 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 46 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
54 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db"))); | 47 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db"))); |
55 | 48 |
56 // The |error_handler_| will be called if any sqlite statement operation | 49 // The error delegate will set |error_| and |sql_text_| when any sqlite |
57 // returns an error code. | 50 // statement operation returns an error code. |
58 db_.set_error_delegate(error_handler_); | 51 db_.set_error_delegate(new StatementErrorHandler(&error_, &sql_text_)); |
59 } | 52 } |
60 | 53 |
61 void TearDown() { | 54 void TearDown() { |
62 // If any error happened the original sql statement can be found in | 55 // If any error happened the original sql statement can be found in |
63 // error_handler_->sql_statement(). | 56 // |sql_text_|. |
64 EXPECT_EQ(SQLITE_OK, error_handler_->error()); | 57 EXPECT_EQ(SQLITE_OK, error_); |
65 db_.Close(); | 58 db_.Close(); |
66 } | 59 } |
67 | 60 |
68 sql::Connection& db() { return db_; } | 61 sql::Connection& db() { return db_; } |
69 | 62 |
70 int sqlite_error() const { return error_handler_->error(); } | 63 int sqlite_error() const { |
71 void reset_error() const { error_handler_->reset_error(); } | 64 return error_; |
| 65 } |
72 | 66 |
73 private: | 67 private: |
74 ScopedTempDir temp_dir_; | 68 ScopedTempDir temp_dir_; |
75 sql::Connection db_; | 69 sql::Connection db_; |
76 scoped_refptr<StatementErrorHandler> error_handler_; | 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_; |
77 }; | 75 }; |
78 | 76 |
79 // Do not include fts1 support, it is not useful, and nobody is | 77 // Do not include fts1 support, it is not useful, and nobody is |
80 // looking at it. | 78 // looking at it. |
81 TEST_F(SQLiteFeaturesTest, NoFTS1) { | 79 TEST_F(SQLiteFeaturesTest, NoFTS1) { |
82 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode( | 80 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode( |
83 "CREATE VIRTUAL TABLE foo USING fts1(x)")); | 81 "CREATE VIRTUAL TABLE foo USING fts1(x)")); |
84 } | 82 } |
85 | 83 |
86 #if !defined(OS_IOS) | 84 #if !defined(OS_IOS) |
87 // fts2 is used for older history files, so we're signed on for keeping our | 85 // fts2 is used for older history files, so we're signed on for keeping our |
88 // version up-to-date. iOS does not include fts2, so this test does not run on | 86 // version up-to-date. iOS does not include fts2, so this test does not run on |
89 // iOS. | 87 // iOS. |
90 // TODO(shess): Think up a crazy way to get out from having to support | 88 // TODO(shess): Think up a crazy way to get out from having to support |
91 // this forever. | 89 // this forever. |
92 TEST_F(SQLiteFeaturesTest, FTS2) { | 90 TEST_F(SQLiteFeaturesTest, FTS2) { |
93 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts2(x)")); | 91 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts2(x)")); |
94 } | 92 } |
95 #endif | 93 #endif |
96 | 94 |
97 // fts3 is used for current history files, and also for WebDatabase. | 95 // fts3 is used for current history files, and also for WebDatabase. |
98 TEST_F(SQLiteFeaturesTest, FTS3) { | 96 TEST_F(SQLiteFeaturesTest, FTS3) { |
99 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)")); | 97 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)")); |
100 } | 98 } |
101 | 99 |
102 } // namespace | 100 } // namespace |
OLD | NEW |