Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(311)

Side by Side Diff: sql/sqlite_features_unittest.cc

Issue 11111021: Remove ref counting on sql::ErrorDelegate (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 18
19 class StatementErrorHandler : public sql::ErrorDelegate { 19 class StatementErrorHandler : public sql::ErrorDelegate {
20 public: 20 public:
21 StatementErrorHandler() : error_(SQLITE_OK) {} 21 StatementErrorHandler() : error_(SQLITE_OK) {}
22 22
23 virtual ~StatementErrorHandler() {}
24
23 virtual int OnError(int error, sql::Connection* connection, 25 virtual int OnError(int error, sql::Connection* connection,
24 sql::Statement* stmt) OVERRIDE { 26 sql::Statement* stmt) OVERRIDE {
25 error_ = error; 27 error_ = error;
26 const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL; 28 const char* sql_txt = stmt ? stmt->GetSQLStatement() : NULL;
27 sql_text_ = sql_txt ? sql_txt : "no statement available"; 29 sql_text_ = sql_txt ? sql_txt : "no statement available";
28 return error; 30 return error;
29 } 31 }
30 32
31 int error() const { return error_; } 33 int error() const { return error_; }
32 34
33 void reset_error() { 35 void reset_error() {
34 sql_text_.clear(); 36 sql_text_.clear();
35 error_ = SQLITE_OK; 37 error_ = SQLITE_OK;
36 } 38 }
37 39
38 const char* sql_statement() const { return sql_text_.c_str(); } 40 const char* sql_statement() const { return sql_text_.c_str(); }
39 41
40 protected:
41 virtual ~StatementErrorHandler() {}
42
43 private: 42 private:
44 int error_; 43 int error_;
45 std::string sql_text_; 44 std::string sql_text_;
45
46 DISALLOW_COPY_AND_ASSIGN(StatementErrorHandler);
46 }; 47 };
47 48
48 class SQLiteFeaturesTest : public testing::Test { 49 class SQLiteFeaturesTest : public testing::Test {
49 public: 50 public:
50 SQLiteFeaturesTest() : error_handler_(new StatementErrorHandler) {} 51 SQLiteFeaturesTest() : error_handler_(new StatementErrorHandler) {}
Scott Hess - ex-Googler 2012/10/12 20:04:51 I'm nervous about this, because it's an owning poi
51 52
52 void SetUp() { 53 void SetUp() {
53 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 54 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
54 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db"))); 55 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db")));
55 56
56 // The |error_handler_| will be called if any sqlite statement operation 57 // The |error_handler_| will be called if any sqlite statement operation
57 // returns an error code. 58 // returns an error code.
58 db_.set_error_delegate(error_handler_); 59 db_.set_error_delegate(error_handler_);
59 } 60 }
60 61
61 void TearDown() { 62 void TearDown() {
62 // If any error happened the original sql statement can be found in 63 // If any error happened the original sql statement can be found in
63 // error_handler_->sql_statement(). 64 // error_handler_->sql_statement().
64 EXPECT_EQ(SQLITE_OK, error_handler_->error()); 65 EXPECT_EQ(SQLITE_OK, error_handler_->error());
65 db_.Close(); 66 db_.Close();
66 } 67 }
67 68
68 sql::Connection& db() { return db_; } 69 sql::Connection& db() { return db_; }
69 70
70 int sqlite_error() const { return error_handler_->error(); } 71 int sqlite_error() const { return error_handler_->error(); }
71 void reset_error() const { error_handler_->reset_error(); } 72 void reset_error() const { error_handler_->reset_error(); }
72 73
73 private: 74 private:
74 ScopedTempDir temp_dir_; 75 ScopedTempDir temp_dir_;
75 sql::Connection db_; 76 sql::Connection db_;
76 scoped_refptr<StatementErrorHandler> error_handler_; 77 StatementErrorHandler* error_handler_;
77 }; 78 };
78 79
79 // Do not include fts1 support, it is not useful, and nobody is 80 // Do not include fts1 support, it is not useful, and nobody is
80 // looking at it. 81 // looking at it.
81 TEST_F(SQLiteFeaturesTest, NoFTS1) { 82 TEST_F(SQLiteFeaturesTest, NoFTS1) {
82 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode( 83 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(
83 "CREATE VIRTUAL TABLE foo USING fts1(x)")); 84 "CREATE VIRTUAL TABLE foo USING fts1(x)"));
84 } 85 }
85 86
86 #if !defined(OS_IOS) 87 #if !defined(OS_IOS)
87 // fts2 is used for older history files, so we're signed on for keeping our 88 // 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 89 // version up-to-date. iOS does not include fts2, so this test does not run on
89 // iOS. 90 // iOS.
90 // TODO(shess): Think up a crazy way to get out from having to support 91 // TODO(shess): Think up a crazy way to get out from having to support
91 // this forever. 92 // this forever.
92 TEST_F(SQLiteFeaturesTest, FTS2) { 93 TEST_F(SQLiteFeaturesTest, FTS2) {
93 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts2(x)")); 94 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts2(x)"));
94 } 95 }
95 #endif 96 #endif
96 97
97 // fts3 is used for current history files, and also for WebDatabase. 98 // fts3 is used for current history files, and also for WebDatabase.
98 TEST_F(SQLiteFeaturesTest, FTS3) { 99 TEST_F(SQLiteFeaturesTest, FTS3) {
99 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)")); 100 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));
100 } 101 }
101 102
102 } // namespace 103 } // namespace
OLDNEW
« no previous file with comments | « sql/connection.h ('k') | sql/statement_unittest.cc » ('j') | sql/statement_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698