From d20e0b986707dd49ae7a2cad347f4e50140ec86e Mon Sep 17 00:00:00 2001 From: "hans@chromium.org" Date: Thu, 17 Feb 2011 12:56:09 +0000 Subject: IndexedDB: Allow injection of keys into values via key path. BUG=70118 TEST=browser_tests Review URL: http://codereview.chromium.org/6529021 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@75258 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/idbbindingutilities_browsertest.cc | 116 +++++++++++++++++----- 1 file changed, 89 insertions(+), 27 deletions(-) (limited to 'chrome/browser/idbbindingutilities_browsertest.cc') diff --git a/chrome/browser/idbbindingutilities_browsertest.cc b/chrome/browser/idbbindingutilities_browsertest.cc index 581e736..9d25d17 100644 --- a/chrome/browser/idbbindingutilities_browsertest.cc +++ b/chrome/browser/idbbindingutilities_browsertest.cc @@ -98,14 +98,18 @@ class IDBKeyPathHelper : public UtilityProcessHost::Client { new MessageLoop::QuitTask()); } - void SetExpected(int expected_id, - const std::vector& expected_values, - bool failed) { + void SetExpectedKeys(int expected_id, + const std::vector& expected_keys, + bool failed) { expected_id_ = expected_id; - expected_values_ = expected_values; + expected_keys_ = expected_keys; value_for_key_path_failed_ = failed; } + void SetExpectedValue(const SerializedScriptValue& expected_value) { + expected_value_ = expected_value; + } + void CheckValuesForKeyPath( int id, const std::vector& serialized_values, const string16& key_path) { @@ -123,20 +127,35 @@ class IDBKeyPathHelper : public UtilityProcessHost::Client { ASSERT_TRUE(ret); } + void CheckInjectValue(const IndexedDBKey& key, + const SerializedScriptValue& value, + const string16& key_path) { + if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { + BrowserThread::PostTask( + BrowserThread::IO, FROM_HERE, + NewRunnableMethod(this, &IDBKeyPathHelper::CheckInjectValue, + key, value, key_path)); + return; + } + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); + bool ret = utility_process_host_->StartInjectIDBKey(key, value, key_path); + ASSERT_TRUE(ret); + } + // UtilityProcessHost::Client virtual void OnIDBKeysFromValuesAndKeyPathSucceeded( int id, const std::vector& values) { EXPECT_EQ(expected_id_, id); EXPECT_FALSE(value_for_key_path_failed_); - ASSERT_EQ(expected_values_.size(), values.size()); + ASSERT_EQ(expected_keys_.size(), values.size()); size_t pos = 0; for (std::vector::const_iterator i(values.begin()); i != values.end(); ++i, ++pos) { - ASSERT_EQ(expected_values_[pos].type(), i->type()); + ASSERT_EQ(expected_keys_[pos].type(), i->type()); if (i->type() == WebKit::WebIDBKey::StringType) { - ASSERT_EQ(expected_values_[pos].string(), i->string()); + ASSERT_EQ(expected_keys_[pos].string(), i->string()); } else if (i->type() == WebKit::WebIDBKey::NumberType) { - ASSERT_EQ(expected_values_[pos].number(), i->number()); + ASSERT_EQ(expected_keys_[pos].number(), i->number()); } } BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, @@ -149,11 +168,20 @@ class IDBKeyPathHelper : public UtilityProcessHost::Client { new MessageLoop::QuitTask()); } + virtual void OnInjectIDBKeyFinished( + const SerializedScriptValue& new_value) { + EXPECT_EQ(expected_value_.data(), new_value.data()); + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, + new MessageLoop::QuitTask()); + } + + private: int expected_id_; - std::vector expected_values_; + std::vector expected_keys_; UtilityProcessHost* utility_process_host_; bool value_for_key_path_failed_; + SerializedScriptValue expected_value_; }; // This test fixture runs in the UI thread. However, most of the work done by @@ -175,9 +203,13 @@ class ScopedIDBKeyPathHelper { ui_test_utils::RunMessageLoop(); } - void SetExpected(int id, const std::vector& expected_values, + void SetExpectedKeys(int id, const std::vector& expected_keys, bool failed) { - key_path_helper_->SetExpected(id, expected_values, failed); + key_path_helper_->SetExpectedKeys(id, expected_keys, failed); + } + + void SetExpectedValue(const SerializedScriptValue& expected_value) { + key_path_helper_->SetExpectedValue(expected_value); } void CheckValuesForKeyPath( @@ -189,6 +221,13 @@ class ScopedIDBKeyPathHelper { ui_test_utils::RunMessageLoop(); } + void CheckInjectValue(const IndexedDBKey& key, + const SerializedScriptValue& value, + const string16& key_path) { + key_path_helper_->CheckInjectValue(key, value, key_path); + ui_test_utils::RunMessageLoop(); + } + private: scoped_refptr key_path_helper_; }; @@ -196,16 +235,16 @@ class ScopedIDBKeyPathHelper { IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, IDBKeyPathExtract) { ScopedIDBKeyPathHelper scoped_helper; const int kId = 7; - std::vector expected_values; + std::vector expected_keys; IndexedDBKey value; value.SetString(UTF8ToUTF16("zoo")); - expected_values.push_back(value); + expected_keys.push_back(value); IndexedDBKey invalid_value; invalid_value.SetInvalid(); - expected_values.push_back(invalid_value); + expected_keys.push_back(invalid_value); - scoped_helper.SetExpected(kId, expected_values, false); + scoped_helper.SetExpectedKeys(kId, expected_keys, false); char16 data[] = {0x0353,0x6f66,0x536f,0x7a03,0x6f6f,0x017b}; std::vector serialized_values; @@ -220,13 +259,13 @@ IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, IDBKeyPathExtract) { IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, IDBKeyPathPropertyNotAvailable) { ScopedIDBKeyPathHelper scoped_helper; const int kId = 7; - std::vector expected_values; + std::vector expected_keys; IndexedDBKey invalid_value; invalid_value.SetInvalid(); - expected_values.push_back(invalid_value); - expected_values.push_back(invalid_value); + expected_keys.push_back(invalid_value); + expected_keys.push_back(invalid_value); - scoped_helper.SetExpected(kId, expected_values, false); + scoped_helper.SetExpectedKeys(kId, expected_keys, false); char16 data[] = {0x0353,0x6f66,0x536f,0x7a03,0x6f6f,0x017b}; std::vector serialized_values; @@ -241,13 +280,13 @@ IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, IDBKeyPathPropertyNotAvailable) { IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, IDBKeyPathMultipleCalls) { ScopedIDBKeyPathHelper scoped_helper; const int kId = 7; - std::vector expected_values; + std::vector expected_keys; IndexedDBKey invalid_value; invalid_value.SetInvalid(); - expected_values.push_back(invalid_value); - expected_values.push_back(invalid_value); + expected_keys.push_back(invalid_value); + expected_keys.push_back(invalid_value); - scoped_helper.SetExpected(kId, expected_values, true); + scoped_helper.SetExpectedKeys(kId, expected_keys, true); char16 data[] = {0x0353,0x6f66,0x536f,0x7a03,0x6f6f,0x017b}; std::vector serialized_values; @@ -259,12 +298,35 @@ IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, IDBKeyPathMultipleCalls) { UTF8ToUTF16("!+Invalid[KeyPath[[[")); // Call again with the Utility process in batch mode and with valid keys. - expected_values.clear(); + expected_keys.clear(); IndexedDBKey value; value.SetString(UTF8ToUTF16("zoo")); - expected_values.push_back(value); - expected_values.push_back(invalid_value); - scoped_helper.SetExpected(kId + 1, expected_values, false); + expected_keys.push_back(value); + expected_keys.push_back(invalid_value); + scoped_helper.SetExpectedKeys(kId + 1, expected_keys, false); scoped_helper.CheckValuesForKeyPath(kId + 1, serialized_values, UTF8ToUTF16("foo")); } + +IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, InjectIDBKey) { + // {foo: 'zoo'} + const char16 data[] = {0x0353,0x6f66,0x536f,0x7a03,0x6f6f,0x017b}; + SerializedScriptValue value(false, false, string16(data, arraysize(data))); + IndexedDBKey key; + key.SetString(UTF8ToUTF16("myNewKey")); + + // {foo: 'zoo', bar: 'myNewKey'} + const char16 expected_data[] = {0x353, 0x6f66, 0x536f, 0x7a03, 0x6f6f, 0x353, + 0x6162, 0x5372, 0x6d08, 0x4e79, 0x7765, + 0x654b, 0x7b79, 0x2}; + SerializedScriptValue expected_value(false, false, + string16(expected_data, + arraysize(expected_data))); + + ScopedIDBKeyPathHelper scoped_helper; + scoped_helper.SetExpectedValue(expected_value); + scoped_helper.CheckInjectValue(key, value, UTF8ToUTF16("bar")); + + scoped_helper.SetExpectedValue(SerializedScriptValue()); // Expect null. + scoped_helper.CheckInjectValue(key, value, UTF8ToUTF16("bad.key.path")); +} -- cgit v1.1