OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 <string> |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/file_path.h" |
| 9 #include "base/ref_counted.h" |
| 10 #include "chrome/browser/in_process_webkit/webkit_context.h" |
| 11 #include "chrome/browser/in_process_webkit/webkit_thread.h" |
| 12 #include "chrome/browser/browsing_data_local_storage_helper.h" |
| 13 #include "chrome/test/in_process_browser_test.h" |
| 14 #include "chrome/test/testing_profile.h" |
| 15 #include "chrome/test/ui_test_utils.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 static const FilePath::CharType kTestFile0[] = |
| 19 FILE_PATH_LITERAL("http_www.chromium.org_0.localstorage"); |
| 20 |
| 21 static const FilePath::CharType kTestFile1[] = |
| 22 FILE_PATH_LITERAL("http_www.google.com_0.localstorage"); |
| 23 |
| 24 static const FilePath::CharType kTestFileInvalid[] = |
| 25 FILE_PATH_LITERAL("http_www.google.com_localstorage_0.foo"); |
| 26 |
| 27 class BrowsingDataLocalStorageHelperTest : public InProcessBrowserTest { |
| 28 protected: |
| 29 void CreateLocalStorageFilesForTest() { |
| 30 FilePath storage_path = GetLocalStoragePathForTestingProfile(); |
| 31 file_util::CreateDirectory(storage_path); |
| 32 const FilePath::CharType* kFilesToCreate[] = { |
| 33 kTestFile0, kTestFile1, kTestFileInvalid, |
| 34 }; |
| 35 for (size_t i = 0; i < arraysize(kFilesToCreate); ++i) { |
| 36 FilePath file_path = storage_path.Append(kFilesToCreate[i]); |
| 37 file_util::WriteFile(file_path, NULL, 0); |
| 38 } |
| 39 } |
| 40 |
| 41 FilePath GetLocalStoragePathForTestingProfile() { |
| 42 FilePath storage_path(testing_profile_.GetPath()); |
| 43 storage_path = storage_path.Append( |
| 44 DOMStorageContext::kLocalStorageDirectory); |
| 45 return storage_path; |
| 46 } |
| 47 TestingProfile testing_profile_; |
| 48 }; |
| 49 |
| 50 // This class is notified by BrowsingDataLocalStorageHelper on the UI thread |
| 51 // once it finishes fetching the local storage data. |
| 52 class StopTestOnCallback { |
| 53 public: |
| 54 explicit StopTestOnCallback( |
| 55 BrowsingDataLocalStorageHelper* local_storage_helper) |
| 56 : local_storage_helper_(local_storage_helper) { |
| 57 DCHECK(local_storage_helper_); |
| 58 } |
| 59 |
| 60 void Callback( |
| 61 const std::vector<BrowsingDataLocalStorageHelper::LocalStorageInfo>& |
| 62 local_storage_info) { |
| 63 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 64 // There's no guarantee on the order, ensure these files are there. |
| 65 const char* const kTestHosts[] = {"www.chromium.org", "www.google.com"}; |
| 66 bool test_hosts_found[arraysize(kTestHosts)] = {false, false}; |
| 67 ASSERT_EQ(arraysize(kTestHosts), local_storage_info.size()); |
| 68 for (size_t i = 0; i < arraysize(kTestHosts); ++i) { |
| 69 for (size_t j = 0; j < local_storage_info.size(); ++j) { |
| 70 BrowsingDataLocalStorageHelper::LocalStorageInfo info = |
| 71 local_storage_info.at(j); |
| 72 ASSERT_EQ("http", info.protocol); |
| 73 if (info.host == kTestHosts[i]) { |
| 74 ASSERT_FALSE(test_hosts_found[i]); |
| 75 test_hosts_found[i] = true; |
| 76 } |
| 77 } |
| 78 } |
| 79 for (size_t i = 0; i < arraysize(kTestHosts); ++i) { |
| 80 ASSERT_TRUE(test_hosts_found[i]) << kTestHosts[i]; |
| 81 } |
| 82 MessageLoop::current()->Quit(); |
| 83 } |
| 84 |
| 85 private: |
| 86 BrowsingDataLocalStorageHelper* local_storage_helper_; |
| 87 }; |
| 88 |
| 89 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, CallbackCompletes) { |
| 90 scoped_refptr<BrowsingDataLocalStorageHelper> local_storage_helper( |
| 91 new BrowsingDataLocalStorageHelper(&testing_profile_)); |
| 92 CreateLocalStorageFilesForTest(); |
| 93 StopTestOnCallback stop_test_on_callback(local_storage_helper); |
| 94 local_storage_helper->StartFetching( |
| 95 NewCallback(&stop_test_on_callback, &StopTestOnCallback::Callback)); |
| 96 // Blocks until StopTestOnCallback::Callback is notified. |
| 97 ui_test_utils::RunMessageLoop(); |
| 98 } |
| 99 |
| 100 class WaitForWebKitThread |
| 101 : public base::RefCountedThreadSafe<WaitForWebKitThread> { |
| 102 public: |
| 103 void QuitUiMessageLoopAfterWebKitThreadNotified() { |
| 104 ChromeThread::PostTask(ChromeThread::WEBKIT, |
| 105 FROM_HERE, |
| 106 NewRunnableMethod( |
| 107 this, &WaitForWebKitThread::RunInWebKitThread)); |
| 108 } |
| 109 |
| 110 private: |
| 111 void RunInWebKitThread() { |
| 112 ChromeThread::PostTask(ChromeThread::UI, |
| 113 FROM_HERE, |
| 114 NewRunnableMethod( |
| 115 this, &WaitForWebKitThread::RunInUiThread)); |
| 116 } |
| 117 |
| 118 void RunInUiThread() { |
| 119 MessageLoop::current()->Quit(); |
| 120 } |
| 121 }; |
| 122 |
| 123 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, DeleteSingleFile) { |
| 124 scoped_refptr<BrowsingDataLocalStorageHelper> local_storage_helper( |
| 125 new BrowsingDataLocalStorageHelper(&testing_profile_)); |
| 126 CreateLocalStorageFilesForTest(); |
| 127 local_storage_helper->DeleteLocalStorageFile( |
| 128 GetLocalStoragePathForTestingProfile().Append(FilePath(kTestFile0))); |
| 129 scoped_refptr<WaitForWebKitThread> wait_for_webkit_thread( |
| 130 new WaitForWebKitThread); |
| 131 wait_for_webkit_thread->QuitUiMessageLoopAfterWebKitThreadNotified(); |
| 132 // Blocks until WaitForWebKitThread is notified. |
| 133 ui_test_utils::RunMessageLoop(); |
| 134 // Ensure the file has been deleted. |
| 135 file_util::FileEnumerator file_enumerator( |
| 136 GetLocalStoragePathForTestingProfile(), |
| 137 false, |
| 138 file_util::FileEnumerator::FILES); |
| 139 int num_files = 0; |
| 140 for (FilePath file_path = file_enumerator.Next(); |
| 141 !file_path.empty(); |
| 142 file_path = file_enumerator.Next()) { |
| 143 ASSERT_FALSE(FilePath(kTestFile0) == file_path.BaseName()); |
| 144 ++num_files; |
| 145 } |
| 146 ASSERT_EQ(2, num_files); |
| 147 } |
| 148 |
| 149 IN_PROC_BROWSER_TEST_F(BrowsingDataLocalStorageHelperTest, DeleteAllFiles) { |
| 150 scoped_refptr<BrowsingDataLocalStorageHelper> local_storage_helper( |
| 151 new BrowsingDataLocalStorageHelper(&testing_profile_)); |
| 152 CreateLocalStorageFilesForTest(); |
| 153 local_storage_helper->DeleteAllLocalStorageFiles(); |
| 154 scoped_refptr<WaitForWebKitThread> wait_for_webkit_thread( |
| 155 new WaitForWebKitThread); |
| 156 wait_for_webkit_thread->QuitUiMessageLoopAfterWebKitThreadNotified(); |
| 157 // Blocks until WaitForWebKitThread is notified. |
| 158 ui_test_utils::RunMessageLoop(); |
| 159 // Ensure the alls files but the one without local storage extension have been |
| 160 // deleted. |
| 161 file_util::FileEnumerator file_enumerator( |
| 162 GetLocalStoragePathForTestingProfile(), |
| 163 false, |
| 164 file_util::FileEnumerator::FILES); |
| 165 for (FilePath file_path = file_enumerator.Next(); |
| 166 !file_path.empty(); |
| 167 file_path = file_enumerator.Next()) { |
| 168 ASSERT_TRUE(FilePath(kTestFileInvalid) == file_path.BaseName()); |
| 169 } |
| 170 } |
OLD | NEW |