| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/test/thread_test_helper.h" | 12 #include "base/test/thread_test_helper.h" |
| 13 #include "chrome/browser/browsing_data_helper_browsertest.h" | 13 #include "chrome/browser/browsing_data_helper_browsertest.h" |
| 14 #include "chrome/browser/browsing_data_local_storage_helper.h" | 14 #include "chrome/browser/browsing_data_local_storage_helper.h" |
| 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/test/base/in_process_browser_test.h" | 17 #include "chrome/test/base/in_process_browser_test.h" |
| 16 #include "chrome/test/base/testing_profile.h" | |
| 17 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
| 18 #include "content/browser/in_process_webkit/webkit_context.h" | 19 #include "content/browser/in_process_webkit/webkit_context.h" |
| 19 #include "content/browser/in_process_webkit/webkit_thread.h" | 20 #include "content/browser/in_process_webkit/webkit_thread.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 typedef | 24 typedef |
| 24 BrowsingDataHelperCallback<BrowsingDataLocalStorageHelper::LocalStorageInfo> | 25 BrowsingDataHelperCallback<BrowsingDataLocalStorageHelper::LocalStorageInfo> |
| 25 TestCompletionCallback; | 26 TestCompletionCallback; |
| 26 | 27 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 45 const FilePath::CharType* kFilesToCreate[] = { | 46 const FilePath::CharType* kFilesToCreate[] = { |
| 46 kTestFile0, kTestFile1, kTestFileInvalid, kTestFileExtension | 47 kTestFile0, kTestFile1, kTestFileInvalid, kTestFileExtension |
| 47 }; | 48 }; |
| 48 for (size_t i = 0; i < arraysize(kFilesToCreate); ++i) { | 49 for (size_t i = 0; i < arraysize(kFilesToCreate); ++i) { |
| 49 FilePath file_path = storage_path.Append(kFilesToCreate[i]); | 50 FilePath file_path = storage_path.Append(kFilesToCreate[i]); |
| 50 file_util::WriteFile(file_path, NULL, 0); | 51 file_util::WriteFile(file_path, NULL, 0); |
| 51 } | 52 } |
| 52 } | 53 } |
| 53 | 54 |
| 54 FilePath GetLocalStoragePathForTestingProfile() { | 55 FilePath GetLocalStoragePathForTestingProfile() { |
| 55 FilePath storage_path(testing_profile_.GetPath()); | 56 FilePath storage_path(browser()->profile()->GetPath()); |
| 56 storage_path = storage_path.Append( | 57 storage_path = storage_path.Append( |
| 57 DOMStorageContext::kLocalStorageDirectory); | 58 DOMStorageContext::kLocalStorageDirectory); |
| 58 return storage_path; | 59 return storage_path; |
| 59 } | 60 } |
| 60 TestingProfile testing_profile_; | |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 // This class is notified by BrowsingDataLocalStorageHelper on the UI thread | 63 // This class is notified by BrowsingDataLocalStorageHelper on the UI thread |
| 64 // once it finishes fetching the local storage data. | 64 // once it finishes fetching the local storage data. |
| 65 class StopTestOnCallback { | 65 class StopTestOnCallback { |
| 66 public: | 66 public: |
| 67 explicit StopTestOnCallback( | 67 explicit StopTestOnCallback( |
| 68 BrowsingDataLocalStorageHelper* local_storage_helper) | 68 BrowsingDataLocalStorageHelper* local_storage_helper) |
| 69 : local_storage_helper_(local_storage_helper) { | 69 : local_storage_helper_(local_storage_helper) { |
| 70 DCHECK(local_storage_helper_); | 70 DCHECK(local_storage_helper_); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 94 } | 94 } |
| 95 MessageLoop::current()->Quit(); | 95 MessageLoop::current()->Quit(); |
| 96 } | 96 } |
| 97 | 97 |
| 98 private: | 98 private: |
| 99 BrowsingDataLocalStorageHelper* local_storage_helper_; | 99 BrowsingDataLocalStorageHelper* local_storage_helper_; |
| 100 }; | 100 }; |
| 101 | 101 |
| 102 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, CallbackCompletes) { | 102 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, CallbackCompletes) { |
| 103 scoped_refptr<BrowsingDataLocalStorageHelper> local_storage_helper( | 103 scoped_refptr<BrowsingDataLocalStorageHelper> local_storage_helper( |
| 104 new BrowsingDataLocalStorageHelper(&testing_profile_)); | 104 new BrowsingDataLocalStorageHelper(browser()->profile())); |
| 105 CreateLocalStorageFilesForTest(); | 105 CreateLocalStorageFilesForTest(); |
| 106 StopTestOnCallback stop_test_on_callback(local_storage_helper); | 106 StopTestOnCallback stop_test_on_callback(local_storage_helper); |
| 107 local_storage_helper->StartFetching( | 107 local_storage_helper->StartFetching( |
| 108 NewCallback(&stop_test_on_callback, &StopTestOnCallback::Callback)); | 108 NewCallback(&stop_test_on_callback, &StopTestOnCallback::Callback)); |
| 109 // Blocks until StopTestOnCallback::Callback is notified. | 109 // Blocks until StopTestOnCallback::Callback is notified. |
| 110 ui_test_utils::RunMessageLoop(); | 110 ui_test_utils::RunMessageLoop(); |
| 111 } | 111 } |
| 112 | 112 |
| 113 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, DeleteSingleFile) { | 113 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, DeleteSingleFile) { |
| 114 scoped_refptr<BrowsingDataLocalStorageHelper> local_storage_helper( | 114 scoped_refptr<BrowsingDataLocalStorageHelper> local_storage_helper( |
| 115 new BrowsingDataLocalStorageHelper(&testing_profile_)); | 115 new BrowsingDataLocalStorageHelper(browser()->profile())); |
| 116 CreateLocalStorageFilesForTest(); | 116 CreateLocalStorageFilesForTest(); |
| 117 local_storage_helper->DeleteLocalStorageFile( | 117 local_storage_helper->DeleteLocalStorageFile( |
| 118 GetLocalStoragePathForTestingProfile().Append(FilePath(kTestFile0))); | 118 GetLocalStoragePathForTestingProfile().Append(FilePath(kTestFile0))); |
| 119 scoped_refptr<base::ThreadTestHelper> wait_for_webkit_thread( | 119 scoped_refptr<base::ThreadTestHelper> wait_for_webkit_thread( |
| 120 new base::ThreadTestHelper( | 120 new base::ThreadTestHelper( |
| 121 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::WEBKIT))); | 121 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::WEBKIT))); |
| 122 ASSERT_TRUE(wait_for_webkit_thread->Run()); | 122 ASSERT_TRUE(wait_for_webkit_thread->Run()); |
| 123 // Ensure the file has been deleted. | 123 // Ensure the file has been deleted. |
| 124 file_util::FileEnumerator file_enumerator( | 124 file_util::FileEnumerator file_enumerator( |
| 125 GetLocalStoragePathForTestingProfile(), | 125 GetLocalStoragePathForTestingProfile(), |
| (...skipping 12 matching lines...) Expand all Loading... |
| 138 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, | 138 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, |
| 139 CannedAddLocalStorage) { | 139 CannedAddLocalStorage) { |
| 140 const GURL origin1("http://host1:1/"); | 140 const GURL origin1("http://host1:1/"); |
| 141 const GURL origin2("http://host2:1/"); | 141 const GURL origin2("http://host2:1/"); |
| 142 const FilePath::CharType file1[] = | 142 const FilePath::CharType file1[] = |
| 143 FILE_PATH_LITERAL("http_host1_1.localstorage"); | 143 FILE_PATH_LITERAL("http_host1_1.localstorage"); |
| 144 const FilePath::CharType file2[] = | 144 const FilePath::CharType file2[] = |
| 145 FILE_PATH_LITERAL("http_host2_1.localstorage"); | 145 FILE_PATH_LITERAL("http_host2_1.localstorage"); |
| 146 | 146 |
| 147 scoped_refptr<CannedBrowsingDataLocalStorageHelper> helper( | 147 scoped_refptr<CannedBrowsingDataLocalStorageHelper> helper( |
| 148 new CannedBrowsingDataLocalStorageHelper(&testing_profile_)); | 148 new CannedBrowsingDataLocalStorageHelper(browser()->profile())); |
| 149 helper->AddLocalStorage(origin1); | 149 helper->AddLocalStorage(origin1); |
| 150 helper->AddLocalStorage(origin2); | 150 helper->AddLocalStorage(origin2); |
| 151 | 151 |
| 152 TestCompletionCallback callback; | 152 TestCompletionCallback callback; |
| 153 helper->StartFetching( | 153 helper->StartFetching( |
| 154 NewCallback(&callback, &TestCompletionCallback::callback)); | 154 NewCallback(&callback, &TestCompletionCallback::callback)); |
| 155 | 155 |
| 156 std::vector<BrowsingDataLocalStorageHelper::LocalStorageInfo> result = | 156 std::vector<BrowsingDataLocalStorageHelper::LocalStorageInfo> result = |
| 157 callback.result(); | 157 callback.result(); |
| 158 | 158 |
| 159 ASSERT_EQ(2u, result.size()); | 159 ASSERT_EQ(2u, result.size()); |
| 160 EXPECT_EQ(FilePath(file1).value(), result[0].file_path.BaseName().value()); | 160 EXPECT_EQ(FilePath(file1).value(), result[0].file_path.BaseName().value()); |
| 161 EXPECT_EQ(FilePath(file2).value(), result[1].file_path.BaseName().value()); | 161 EXPECT_EQ(FilePath(file2).value(), result[1].file_path.BaseName().value()); |
| 162 } | 162 } |
| 163 | 163 |
| 164 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, CannedUnique) { | 164 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, CannedUnique) { |
| 165 const GURL origin("http://host1:1/"); | 165 const GURL origin("http://host1:1/"); |
| 166 const FilePath::CharType file[] = | 166 const FilePath::CharType file[] = |
| 167 FILE_PATH_LITERAL("http_host1_1.localstorage"); | 167 FILE_PATH_LITERAL("http_host1_1.localstorage"); |
| 168 | 168 |
| 169 scoped_refptr<CannedBrowsingDataLocalStorageHelper> helper( | 169 scoped_refptr<CannedBrowsingDataLocalStorageHelper> helper( |
| 170 new CannedBrowsingDataLocalStorageHelper(&testing_profile_)); | 170 new CannedBrowsingDataLocalStorageHelper(browser()->profile())); |
| 171 helper->AddLocalStorage(origin); | 171 helper->AddLocalStorage(origin); |
| 172 helper->AddLocalStorage(origin); | 172 helper->AddLocalStorage(origin); |
| 173 | 173 |
| 174 TestCompletionCallback callback; | 174 TestCompletionCallback callback; |
| 175 helper->StartFetching( | 175 helper->StartFetching( |
| 176 NewCallback(&callback, &TestCompletionCallback::callback)); | 176 NewCallback(&callback, &TestCompletionCallback::callback)); |
| 177 | 177 |
| 178 std::vector<BrowsingDataLocalStorageHelper::LocalStorageInfo> result = | 178 std::vector<BrowsingDataLocalStorageHelper::LocalStorageInfo> result = |
| 179 callback.result(); | 179 callback.result(); |
| 180 | 180 |
| 181 ASSERT_EQ(1u, result.size()); | 181 ASSERT_EQ(1u, result.size()); |
| 182 EXPECT_EQ(FilePath(file).value(), result[0].file_path.BaseName().value()); | 182 EXPECT_EQ(FilePath(file).value(), result[0].file_path.BaseName().value()); |
| 183 } | 183 } |
| 184 } // namespace | 184 } // namespace |
| OLD | NEW |