OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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. |
| 4 |
| 5 #include "base/utf_string_conversions.h" |
| 6 #include "chrome/browser/browser_process.h" |
| 7 #include "chrome/browser/browser.h" |
| 8 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
| 9 #include "chrome/browser/utility_process_host.h" |
| 10 #include "chrome/common/indexed_db_key.h" |
| 11 #include "chrome/common/serialized_script_value.h" |
| 12 #include "chrome/test/in_process_browser_test.h" |
| 13 #include "chrome/test/ui_test_utils.h" |
| 14 #include "googleurl/src/gurl.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "webkit/glue/idb_bindings.h" |
| 17 |
| 18 using WebKit::WebSerializedScriptValue; |
| 19 |
| 20 // Sanity test, check the function call directly outside the sandbox. |
| 21 TEST(IDBKeyPathWithoutSandbox, Value) { |
| 22 char16 data[] = {0x0353,0x6f66,0x536f,0x7a03,0x6f6f,0x017b}; |
| 23 std::vector<WebSerializedScriptValue> serialized_values; |
| 24 serialized_values.push_back( |
| 25 WebSerializedScriptValue::fromString(string16(data, arraysize(data)))); |
| 26 serialized_values.push_back( |
| 27 WebSerializedScriptValue::fromString(string16())); |
| 28 |
| 29 std::vector<WebKit::WebIDBKey> values; |
| 30 string16 key_path(UTF8ToUTF16("foo")); |
| 31 bool error = webkit_glue::IDBKeysFromValuesAndKeyPath( |
| 32 serialized_values, key_path, &values); |
| 33 |
| 34 ASSERT_EQ(size_t(2), values.size()); |
| 35 ASSERT_EQ(WebKit::WebIDBKey::StringType, values[0].type()); |
| 36 ASSERT_EQ(UTF8ToUTF16("zoo"), values[0].string()); |
| 37 ASSERT_EQ(WebKit::WebIDBKey::InvalidType, values[1].type()); |
| 38 ASSERT_FALSE(error); |
| 39 |
| 40 values.clear(); |
| 41 key_path = UTF8ToUTF16("PropertyNotAvailable"); |
| 42 error = webkit_glue::IDBKeysFromValuesAndKeyPath( |
| 43 serialized_values, key_path, &values); |
| 44 |
| 45 ASSERT_EQ(size_t(2), values.size()); |
| 46 ASSERT_EQ(WebKit::WebIDBKey::InvalidType, values[0].type()); |
| 47 ASSERT_EQ(WebKit::WebIDBKey::InvalidType, values[1].type()); |
| 48 ASSERT_FALSE(error); |
| 49 |
| 50 values.clear(); |
| 51 key_path = UTF8ToUTF16("!+Invalid[KeyPath[[["); |
| 52 error = webkit_glue::IDBKeysFromValuesAndKeyPath( |
| 53 serialized_values, key_path, &values); |
| 54 |
| 55 ASSERT_TRUE(error); |
| 56 ASSERT_EQ(size_t(2), values.size()); |
| 57 ASSERT_EQ(WebKit::WebIDBKey::InvalidType, values[0].type()); |
| 58 ASSERT_EQ(WebKit::WebIDBKey::InvalidType, values[1].type()); |
| 59 } |
| 60 |
| 61 class IDBKeyPathHelper : public UtilityProcessHost::Client { |
| 62 public: |
| 63 IDBKeyPathHelper() |
| 64 : expected_id_(0), |
| 65 utility_process_host_(NULL), |
| 66 value_for_key_path_failed_(false) { |
| 67 } |
| 68 |
| 69 void CreateUtilityProcess(ResourceDispatcherHost* resource_dispatcher_host) { |
| 70 if (!ChromeThread::CurrentlyOn(ChromeThread::IO)) { |
| 71 ChromeThread::PostTask( |
| 72 ChromeThread::IO, FROM_HERE, |
| 73 NewRunnableMethod(this, &IDBKeyPathHelper::CreateUtilityProcess, |
| 74 resource_dispatcher_host)); |
| 75 return; |
| 76 } |
| 77 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 78 utility_process_host_ = |
| 79 new UtilityProcessHost(resource_dispatcher_host, this, |
| 80 ChromeThread::IO); |
| 81 utility_process_host_->StartBatchMode(); |
| 82 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, |
| 83 new MessageLoop::QuitTask()); |
| 84 } |
| 85 |
| 86 void DestroyUtilityProcess() { |
| 87 if (!ChromeThread::CurrentlyOn(ChromeThread::IO)) { |
| 88 ChromeThread::PostTask( |
| 89 ChromeThread::IO, FROM_HERE, |
| 90 NewRunnableMethod(this, &IDBKeyPathHelper::DestroyUtilityProcess)); |
| 91 return; |
| 92 } |
| 93 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 94 utility_process_host_->EndBatchMode(); |
| 95 utility_process_host_ = NULL; |
| 96 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, |
| 97 new MessageLoop::QuitTask()); |
| 98 } |
| 99 |
| 100 void SetExpected(int expected_id, |
| 101 const std::vector<IndexedDBKey>& expected_values, |
| 102 bool failed) { |
| 103 expected_id_ = expected_id; |
| 104 expected_values_ = expected_values; |
| 105 value_for_key_path_failed_ = failed; |
| 106 } |
| 107 |
| 108 void CheckValuesForKeyPath( |
| 109 int id, const std::vector<SerializedScriptValue>& serialized_values, |
| 110 const string16& key_path) { |
| 111 if (!ChromeThread::CurrentlyOn(ChromeThread::IO)) { |
| 112 ChromeThread::PostTask( |
| 113 ChromeThread::IO, FROM_HERE, |
| 114 NewRunnableMethod(this, &IDBKeyPathHelper::CheckValuesForKeyPath, |
| 115 id, serialized_values, key_path)); |
| 116 return; |
| 117 } |
| 118 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 119 bool ret = |
| 120 utility_process_host_->StartIDBKeysFromValuesAndKeyPath( |
| 121 id, serialized_values, key_path); |
| 122 ASSERT_TRUE(ret); |
| 123 } |
| 124 |
| 125 // UtilityProcessHost::Client |
| 126 virtual void OnIDBKeysFromValuesAndKeyPathSucceeded( |
| 127 int id, const std::vector<IndexedDBKey>& values) { |
| 128 EXPECT_EQ(expected_id_, id); |
| 129 EXPECT_FALSE(value_for_key_path_failed_); |
| 130 ASSERT_EQ(expected_values_.size(), values.size()); |
| 131 size_t pos = 0; |
| 132 for (std::vector<IndexedDBKey>::const_iterator i(values.begin()); |
| 133 i != values.end(); ++i, ++pos) { |
| 134 ASSERT_EQ(expected_values_[pos].type(), i->type()); |
| 135 if (i->type() == WebKit::WebIDBKey::StringType) { |
| 136 ASSERT_EQ(expected_values_[pos].string(), i->string()); |
| 137 } else if (i->type() == WebKit::WebIDBKey::NumberType) { |
| 138 ASSERT_EQ(expected_values_[pos].number(), i->number()); |
| 139 } |
| 140 } |
| 141 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, |
| 142 new MessageLoop::QuitTask()); |
| 143 } |
| 144 |
| 145 virtual void OnIDBKeysFromValuesAndKeyPathFailed(int id) { |
| 146 EXPECT_TRUE(value_for_key_path_failed_); |
| 147 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, |
| 148 new MessageLoop::QuitTask()); |
| 149 } |
| 150 |
| 151 private: |
| 152 int expected_id_; |
| 153 std::vector<IndexedDBKey> expected_values_; |
| 154 UtilityProcessHost* utility_process_host_; |
| 155 bool value_for_key_path_failed_; |
| 156 }; |
| 157 |
| 158 // This test fixture runs in the UI thread. However, most of the work done by |
| 159 // UtilityProcessHost (and wrapped by IDBKeyPathHelper above) happens on the IO |
| 160 // thread. This fixture delegates to IDBKeyPathHelper and blocks via |
| 161 // "ui_test_utils::RunMessageLoop()", until IDBKeyPathHelper posts a quit |
| 162 // message the MessageLoop. |
| 163 class ScopedIDBKeyPathHelper { |
| 164 public: |
| 165 ScopedIDBKeyPathHelper() { |
| 166 key_path_helper_ = new IDBKeyPathHelper(); |
| 167 key_path_helper_->CreateUtilityProcess( |
| 168 g_browser_process->resource_dispatcher_host()); |
| 169 ui_test_utils::RunMessageLoop(); |
| 170 } |
| 171 |
| 172 ~ScopedIDBKeyPathHelper() { |
| 173 key_path_helper_->DestroyUtilityProcess(); |
| 174 ui_test_utils::RunMessageLoop(); |
| 175 } |
| 176 |
| 177 void SetExpected(int id, const std::vector<IndexedDBKey>& expected_values, |
| 178 bool failed) { |
| 179 key_path_helper_->SetExpected(id, expected_values, failed); |
| 180 } |
| 181 |
| 182 void CheckValuesForKeyPath( |
| 183 int id, |
| 184 const std::vector<SerializedScriptValue>& serialized_script_values, |
| 185 const string16& key_path) { |
| 186 key_path_helper_->CheckValuesForKeyPath(id, serialized_script_values, |
| 187 key_path); |
| 188 ui_test_utils::RunMessageLoop(); |
| 189 } |
| 190 |
| 191 private: |
| 192 scoped_refptr<IDBKeyPathHelper> key_path_helper_; |
| 193 }; |
| 194 |
| 195 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, IDBKeyPathExtract) { |
| 196 ScopedIDBKeyPathHelper scoped_helper; |
| 197 const int kId = 7; |
| 198 std::vector<IndexedDBKey> expected_values; |
| 199 IndexedDBKey value; |
| 200 value.Set(UTF8ToUTF16("zoo")); |
| 201 expected_values.push_back(value); |
| 202 |
| 203 IndexedDBKey invalid_value; |
| 204 invalid_value.SetInvalid(); |
| 205 expected_values.push_back(invalid_value); |
| 206 |
| 207 scoped_helper.SetExpected(kId, expected_values, false); |
| 208 |
| 209 char16 data[] = {0x0353,0x6f66,0x536f,0x7a03,0x6f6f,0x017b}; |
| 210 std::vector<SerializedScriptValue> serialized_values; |
| 211 serialized_values.push_back( |
| 212 SerializedScriptValue(false, false, string16(data, arraysize(data)))); |
| 213 serialized_values.push_back( |
| 214 SerializedScriptValue(true, false, string16())); |
| 215 scoped_helper.CheckValuesForKeyPath( |
| 216 kId, serialized_values, UTF8ToUTF16("foo")); |
| 217 } |
| 218 |
| 219 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, IDBKeyPathPropertyNotAvailable) { |
| 220 ScopedIDBKeyPathHelper scoped_helper; |
| 221 const int kId = 7; |
| 222 std::vector<IndexedDBKey> expected_values; |
| 223 IndexedDBKey invalid_value; |
| 224 invalid_value.SetInvalid(); |
| 225 expected_values.push_back(invalid_value); |
| 226 expected_values.push_back(invalid_value); |
| 227 |
| 228 scoped_helper.SetExpected(kId, expected_values, false); |
| 229 |
| 230 char16 data[] = {0x0353,0x6f66,0x536f,0x7a03,0x6f6f,0x017b}; |
| 231 std::vector<SerializedScriptValue> serialized_values; |
| 232 serialized_values.push_back( |
| 233 SerializedScriptValue(false, false, string16(data, arraysize(data)))); |
| 234 serialized_values.push_back( |
| 235 SerializedScriptValue(true, false, string16())); |
| 236 scoped_helper.CheckValuesForKeyPath(kId, serialized_values, |
| 237 UTF8ToUTF16("PropertyNotAvailable")); |
| 238 } |
| 239 |
| 240 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, IDBKeyPathMultipleCalls) { |
| 241 ScopedIDBKeyPathHelper scoped_helper; |
| 242 const int kId = 7; |
| 243 std::vector<IndexedDBKey> expected_values; |
| 244 IndexedDBKey invalid_value; |
| 245 invalid_value.SetInvalid(); |
| 246 expected_values.push_back(invalid_value); |
| 247 expected_values.push_back(invalid_value); |
| 248 |
| 249 scoped_helper.SetExpected(kId, expected_values, true); |
| 250 |
| 251 char16 data[] = {0x0353,0x6f66,0x536f,0x7a03,0x6f6f,0x017b}; |
| 252 std::vector<SerializedScriptValue> serialized_values; |
| 253 serialized_values.push_back( |
| 254 SerializedScriptValue(false, false, string16(data, arraysize(data)))); |
| 255 serialized_values.push_back( |
| 256 SerializedScriptValue(true, false, string16())); |
| 257 scoped_helper.CheckValuesForKeyPath(kId, serialized_values, |
| 258 UTF8ToUTF16("!+Invalid[KeyPath[[[")); |
| 259 |
| 260 // Call again with the Utility process in batch mode and with valid keys. |
| 261 expected_values.clear(); |
| 262 IndexedDBKey value; |
| 263 value.Set(UTF8ToUTF16("zoo")); |
| 264 expected_values.push_back(value); |
| 265 expected_values.push_back(invalid_value); |
| 266 scoped_helper.SetExpected(kId + 1, expected_values, false); |
| 267 scoped_helper.CheckValuesForKeyPath(kId + 1, serialized_values, |
| 268 UTF8ToUTF16("foo")); |
| 269 } |
OLD | NEW |