Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / sql / sqlite_features_unittest.cc
blob20e002d3916477a747b01a70e2451229f773d0f4
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
3 // found in the LICENSE file.
5 #include <string>
7 #include "base/bind.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "sql/connection.h"
11 #include "sql/statement.h"
12 #include "sql/test/sql_test_base.h"
13 #include "sql/test/test_helpers.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/sqlite/sqlite3.h"
17 // Test that certain features are/are-not enabled in our SQLite.
19 namespace {
21 void CaptureErrorCallback(int* error_pointer, std::string* sql_text,
22 int error, sql::Statement* stmt) {
23 *error_pointer = error;
24 const char* text = stmt ? stmt->GetSQLStatement() : NULL;
25 *sql_text = text ? text : "no statement available";
28 class SQLiteFeaturesTest : public sql::SQLTestBase {
29 public:
30 SQLiteFeaturesTest() : error_(SQLITE_OK) {}
32 void SetUp() override {
33 SQLTestBase::SetUp();
35 // The error delegate will set |error_| and |sql_text_| when any sqlite
36 // statement operation returns an error code.
37 db().set_error_callback(
38 base::Bind(&CaptureErrorCallback, &error_, &sql_text_));
41 void TearDown() override {
42 // If any error happened the original sql statement can be found in
43 // |sql_text_|.
44 EXPECT_EQ(SQLITE_OK, error_) << sql_text_;
46 SQLTestBase::TearDown();
49 int error() { return error_; }
51 private:
52 // The error code of the most recent error.
53 int error_;
54 // Original statement which has caused the error.
55 std::string sql_text_;
58 // Do not include fts1 support, it is not useful, and nobody is
59 // looking at it.
60 TEST_F(SQLiteFeaturesTest, NoFTS1) {
61 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(
62 "CREATE VIRTUAL TABLE foo USING fts1(x)"));
65 // Do not include fts2 support, it is not useful, and nobody is
66 // looking at it.
67 TEST_F(SQLiteFeaturesTest, NoFTS2) {
68 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(
69 "CREATE VIRTUAL TABLE foo USING fts2(x)"));
72 // fts3 used to be used for history files, and may also be used by WebDatabase
73 // clients.
74 TEST_F(SQLiteFeaturesTest, FTS3) {
75 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));
78 #if !defined(USE_SYSTEM_SQLITE)
79 // Originally history used fts2, which Chromium patched to treat "foo*" as a
80 // prefix search, though the icu tokenizer would return it as two tokens {"foo",
81 // "*"}. Test that fts3 works correctly.
82 TEST_F(SQLiteFeaturesTest, FTS3_Prefix) {
83 const char kCreateSql[] =
84 "CREATE VIRTUAL TABLE foo USING fts3(x, tokenize icu)";
85 ASSERT_TRUE(db().Execute(kCreateSql));
87 ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')"));
89 sql::Statement s(db().GetUniqueStatement(
90 "SELECT x FROM foo WHERE x MATCH 'te*'"));
91 ASSERT_TRUE(s.Step());
92 EXPECT_EQ("test", s.ColumnString(0));
94 #endif
96 #if !defined(USE_SYSTEM_SQLITE)
97 // Verify that Chromium's SQLite is compiled with HAVE_USLEEP defined. With
98 // HAVE_USLEEP, SQLite uses usleep() with millisecond granularity. Otherwise it
99 // uses sleep() with second granularity.
100 TEST_F(SQLiteFeaturesTest, UsesUsleep) {
101 base::TimeTicks before = base::TimeTicks::Now();
102 sqlite3_sleep(1);
103 base::TimeDelta delta = base::TimeTicks::Now() - before;
105 // It is not impossible for this to be over 1000 if things are compiled the
106 // right way. But it is very unlikely, most platforms seem to be around
107 // <TBD>.
108 LOG(ERROR) << "Milliseconds: " << delta.InMilliseconds();
109 EXPECT_LT(delta.InMilliseconds(), 1000);
111 #endif
113 // Ensure that our SQLite version has working foreign key support with cascade
114 // delete support.
115 TEST_F(SQLiteFeaturesTest, ForeignKeySupport) {
116 ASSERT_TRUE(db().Execute("PRAGMA foreign_keys=1"));
117 ASSERT_TRUE(db().Execute("CREATE TABLE parents (id INTEGER PRIMARY KEY)"));
118 ASSERT_TRUE(db().Execute(
119 "CREATE TABLE children ("
120 " id INTEGER PRIMARY KEY,"
121 " pid INTEGER NOT NULL REFERENCES parents(id) ON DELETE CASCADE)"));
123 // Inserting without a matching parent should fail with constraint violation.
124 // Mask off any extended error codes for USE_SYSTEM_SQLITE.
125 int insertErr = db().ExecuteAndReturnErrorCode(
126 "INSERT INTO children VALUES (10, 1)");
127 EXPECT_EQ(SQLITE_CONSTRAINT, (insertErr&0xff));
129 size_t rows;
130 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows));
131 EXPECT_EQ(0u, rows);
133 // Inserting with a matching parent should work.
134 ASSERT_TRUE(db().Execute("INSERT INTO parents VALUES (1)"));
135 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (11, 1)"));
136 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)"));
137 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows));
138 EXPECT_EQ(2u, rows);
140 // Deleting the parent should cascade, i.e., delete the children as well.
141 ASSERT_TRUE(db().Execute("DELETE FROM parents"));
142 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows));
143 EXPECT_EQ(0u, rows);
146 } // namespace