Fix for making popup size proper when we have 'Block third-party cookies and site...
[chromium-blink-merge.git] / sql / sqlite_features_unittest.cc
blob2b95bb8d3e72165b9fc47258a6f13ec5df4895ba
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/test_helpers.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/sqlite/sqlite3.h"
16 // Test that certain features are/are-not enabled in our SQLite.
18 namespace {
20 void CaptureErrorCallback(int* error_pointer, std::string* sql_text,
21 int error, sql::Statement* stmt) {
22 *error_pointer = error;
23 const char* text = stmt ? stmt->GetSQLStatement() : NULL;
24 *sql_text = text ? text : "no statement available";
27 class SQLiteFeaturesTest : public testing::Test {
28 public:
29 SQLiteFeaturesTest() : error_(SQLITE_OK) {}
31 void SetUp() override {
32 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
33 ASSERT_TRUE(db_.Open(temp_dir_.path().AppendASCII("SQLStatementTest.db")));
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(base::Bind(&CaptureErrorCallback,
38 &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_;
45 db_.Close();
48 sql::Connection& db() { return db_; }
49 int error() { return error_; }
51 private:
52 base::ScopedTempDir temp_dir_;
53 sql::Connection db_;
55 // The error code of the most recent error.
56 int error_;
57 // Original statement which has caused the error.
58 std::string sql_text_;
61 // Do not include fts1 support, it is not useful, and nobody is
62 // looking at it.
63 TEST_F(SQLiteFeaturesTest, NoFTS1) {
64 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(
65 "CREATE VIRTUAL TABLE foo USING fts1(x)"));
68 // Do not include fts2 support, it is not useful, and nobody is
69 // looking at it.
70 TEST_F(SQLiteFeaturesTest, NoFTS2) {
71 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(
72 "CREATE VIRTUAL TABLE foo USING fts2(x)"));
75 // fts3 used to be used for history files, and may also be used by WebDatabase
76 // clients.
77 TEST_F(SQLiteFeaturesTest, FTS3) {
78 ASSERT_TRUE(db().Execute("CREATE VIRTUAL TABLE foo USING fts3(x)"));
81 #if !defined(USE_SYSTEM_SQLITE)
82 // Originally history used fts2, which Chromium patched to treat "foo*" as a
83 // prefix search, though the icu tokenizer would return it as two tokens {"foo",
84 // "*"}. Test that fts3 works correctly.
85 TEST_F(SQLiteFeaturesTest, FTS3_Prefix) {
86 const char kCreateSql[] =
87 "CREATE VIRTUAL TABLE foo USING fts3(x, tokenize icu)";
88 ASSERT_TRUE(db().Execute(kCreateSql));
90 ASSERT_TRUE(db().Execute("INSERT INTO foo (x) VALUES ('test')"));
92 sql::Statement s(db().GetUniqueStatement(
93 "SELECT x FROM foo WHERE x MATCH 'te*'"));
94 ASSERT_TRUE(s.Step());
95 EXPECT_EQ("test", s.ColumnString(0));
97 #endif
99 #if !defined(USE_SYSTEM_SQLITE)
100 // Verify that Chromium's SQLite is compiled with HAVE_USLEEP defined. With
101 // HAVE_USLEEP, SQLite uses usleep() with millisecond granularity. Otherwise it
102 // uses sleep() with second granularity.
103 TEST_F(SQLiteFeaturesTest, UsesUsleep) {
104 base::TimeTicks before = base::TimeTicks::Now();
105 sqlite3_sleep(1);
106 base::TimeDelta delta = base::TimeTicks::Now() - before;
108 // It is not impossible for this to be over 1000 if things are compiled the
109 // right way. But it is very unlikely, most platforms seem to be around
110 // <TBD>.
111 LOG(ERROR) << "Milliseconds: " << delta.InMilliseconds();
112 EXPECT_LT(delta.InMilliseconds(), 1000);
114 #endif
116 // Ensure that our SQLite version has working foreign key support with cascade
117 // delete support.
118 TEST_F(SQLiteFeaturesTest, ForeignKeySupport) {
119 ASSERT_TRUE(db().Execute("PRAGMA foreign_keys=1"));
120 ASSERT_TRUE(db().Execute("CREATE TABLE parents (id INTEGER PRIMARY KEY)"));
121 ASSERT_TRUE(db().Execute(
122 "CREATE TABLE children ("
123 " id INTEGER PRIMARY KEY,"
124 " pid INTEGER NOT NULL REFERENCES parents(id) ON DELETE CASCADE)"));
126 // Inserting without a matching parent should fail with constraint violation.
127 // Mask off any extended error codes for USE_SYSTEM_SQLITE.
128 int insertErr = db().ExecuteAndReturnErrorCode(
129 "INSERT INTO children VALUES (10, 1)");
130 EXPECT_EQ(SQLITE_CONSTRAINT, (insertErr&0xff));
132 size_t rows;
133 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows));
134 EXPECT_EQ(0u, rows);
136 // Inserting with a matching parent should work.
137 ASSERT_TRUE(db().Execute("INSERT INTO parents VALUES (1)"));
138 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (11, 1)"));
139 EXPECT_TRUE(db().Execute("INSERT INTO children VALUES (12, 1)"));
140 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows));
141 EXPECT_EQ(2u, rows);
143 // Deleting the parent should cascade, i.e., delete the children as well.
144 ASSERT_TRUE(db().Execute("DELETE FROM parents"));
145 EXPECT_TRUE(sql::test::CountTableRows(&db(), "children", &rows));
146 EXPECT_EQ(0u, rows);
149 } // namespace