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