| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 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 | 
|  | 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/memory/ref_counted.h" | 
|  | 11 #include "base/message_loop.h" | 
|  | 12 #include "chrome/browser/browsing_data_file_system_helper.h" | 
|  | 13 #include "chrome/browser/browsing_data_helper_browsertest.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 "content/browser/browser_thread.h" | 
|  | 18 #include "testing/gtest/include/gtest/gtest.h" | 
|  | 19 #include "webkit/fileapi/file_system_context.h" | 
|  | 20 #include "webkit/fileapi/file_system_path_manager.h" | 
|  | 21 #include "webkit/fileapi/file_system_types.h" | 
|  | 22 #include "webkit/fileapi/sandbox_mount_point_provider.h" | 
|  | 23 | 
|  | 24 namespace { | 
|  | 25 | 
|  | 26 typedef BrowsingDataHelperCallback<BrowsingDataFileSystemHelper::FileSystemInfo> | 
|  | 27     TestCompletionCallback; | 
|  | 28 | 
|  | 29 const char kTestOrigin1[] = "http://host1:1/"; | 
|  | 30 const char kTestOrigin2[] = "http://host2:1/"; | 
|  | 31 const char kTestOrigin3[] = "http://host3:1/"; | 
|  | 32 | 
|  | 33 const char kTestOriginExtension[] = | 
|  | 34   "chrome-extension://behllobkkfkfnphdnhnkndlbkcpglgmj:1/"; | 
|  | 35 | 
|  | 36 class BrowsingDataFileSystemHelperTest : public InProcessBrowserTest { | 
|  | 37  public: | 
|  | 38   virtual void PopulateTestData() { | 
|  | 39     const GURL origin1(kTestOrigin1); | 
|  | 40     const GURL origin2(kTestOrigin2); | 
|  | 41     const GURL origin3(kTestOrigin3); | 
|  | 42 | 
|  | 43     sandbox_ = testing_profile_.GetFileSystemContext()-> | 
|  | 44                path_manager()->sandbox_provider(); | 
|  | 45 | 
|  | 46     CreateDirectoryForOriginAndType(origin1, fileapi::kFileSystemTypeTemporary); | 
|  | 47     CreateDirectoryForOriginAndType(origin2, | 
|  | 48         fileapi::kFileSystemTypePersistent); | 
|  | 49     CreateDirectoryForOriginAndType(origin3, fileapi::kFileSystemTypeTemporary); | 
|  | 50     CreateDirectoryForOriginAndType(origin3, | 
|  | 51         fileapi::kFileSystemTypePersistent); | 
|  | 52   } | 
|  | 53 | 
|  | 54  protected: | 
|  | 55   void CreateDirectoryForOriginAndType(const GURL& origin, | 
|  | 56                                        fileapi::FileSystemType type) { | 
|  | 57     FilePath target = sandbox_->ValidateFileSystemRootAndGetPathOnFileThread( | 
|  | 58         origin, type, FilePath(), true); | 
|  | 59     ASSERT_TRUE(file_util::CreateDirectory(target)); | 
|  | 60   } | 
|  | 61 | 
|  | 62   TestingProfile testing_profile_; | 
|  | 63   fileapi::SandboxMountPointProvider* sandbox_; | 
|  | 64 }; | 
|  | 65 | 
|  | 66 // Called back by BrowsingDataFileSystemHelper on the UI thread once the | 
|  | 67 // database information has been retrieved. | 
|  | 68 class StopTestOnCallback { | 
|  | 69  public: | 
|  | 70   explicit StopTestOnCallback( | 
|  | 71       BrowsingDataFileSystemHelper* file_system_helper) | 
|  | 72       : file_system_helper_(file_system_helper) { | 
|  | 73     DCHECK(file_system_helper_); | 
|  | 74   } | 
|  | 75 | 
|  | 76   void CallbackFetchData( | 
|  | 77       const std::vector<BrowsingDataFileSystemHelper::FileSystemInfo>& | 
|  | 78           file_system_info_list) { | 
|  | 79     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
|  | 80     ASSERT_EQ(3UL, file_system_info_list.size()); | 
|  | 81 | 
|  | 82     // Order is arbitrary, verify all three origins. | 
|  | 83     bool test_hosts_found[3] = {false, false, false}; | 
|  | 84     for (size_t i = 0; i < file_system_info_list.size(); i++) { | 
|  | 85       BrowsingDataFileSystemHelper::FileSystemInfo info = | 
|  | 86           file_system_info_list.at(i); | 
|  | 87       if (info.origin == GURL(kTestOrigin1)) { | 
|  | 88         test_hosts_found[0] = true; | 
|  | 89         ASSERT_TRUE(info.has_temporary); | 
|  | 90         ASSERT_FALSE(info.has_persistent); | 
|  | 91       } else if (info.origin == GURL(kTestOrigin2)) { | 
|  | 92         test_hosts_found[1] = true; | 
|  | 93         ASSERT_FALSE(info.has_temporary); | 
|  | 94         ASSERT_TRUE(info.has_persistent); | 
|  | 95       } else if (info.origin == GURL(kTestOrigin3)) { | 
|  | 96         test_hosts_found[2] = true; | 
|  | 97         ASSERT_TRUE(info.has_temporary); | 
|  | 98         ASSERT_TRUE(info.has_persistent); | 
|  | 99       } else { | 
|  | 100         ADD_FAILURE() << info.origin.spec() << " isn't an origin we added."; | 
|  | 101       } | 
|  | 102     } | 
|  | 103     for (size_t i = 0; i < arraysize(test_hosts_found); i++) { | 
|  | 104       ASSERT_TRUE(test_hosts_found[i]); | 
|  | 105     } | 
|  | 106     MessageLoop::current()->Quit(); | 
|  | 107   } | 
|  | 108 | 
|  | 109   void CallbackDeleteData( | 
|  | 110       const std::vector<BrowsingDataFileSystemHelper::FileSystemInfo>& | 
|  | 111           file_system_info_list) { | 
|  | 112     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 
|  | 113     ASSERT_EQ(1UL, file_system_info_list.size()); | 
|  | 114     BrowsingDataFileSystemHelper::FileSystemInfo info = | 
|  | 115         file_system_info_list.at(0); | 
|  | 116     ASSERT_EQ(info.origin, GURL(kTestOrigin3)); | 
|  | 117     ASSERT_TRUE(info.has_temporary); | 
|  | 118     ASSERT_TRUE(info.has_persistent); | 
|  | 119     MessageLoop::current()->Quit(); | 
|  | 120   } | 
|  | 121 | 
|  | 122  private: | 
|  | 123   BrowsingDataFileSystemHelper* file_system_helper_; | 
|  | 124 }; | 
|  | 125 | 
|  | 126 | 
|  | 127 IN_PROC_BROWSER_TEST_F(BrowsingDataFileSystemHelperTest, FetchData) { | 
|  | 128   PopulateTestData(); | 
|  | 129   scoped_refptr<BrowsingDataFileSystemHelper> file_system_helper( | 
|  | 130       BrowsingDataFileSystemHelper::Create(&testing_profile_)); | 
|  | 131   StopTestOnCallback stop_test_on_callback(file_system_helper); | 
|  | 132   file_system_helper->StartFetching( | 
|  | 133       NewCallback(&stop_test_on_callback, | 
|  | 134       &StopTestOnCallback::CallbackFetchData)); | 
|  | 135   // Blocks until StopTestOnCallback::CallbackFetchData is notified. | 
|  | 136   ui_test_utils::RunMessageLoop(); | 
|  | 137 } | 
|  | 138 | 
|  | 139 IN_PROC_BROWSER_TEST_F(BrowsingDataFileSystemHelperTest, DeleteData) { | 
|  | 140   PopulateTestData(); | 
|  | 141   scoped_refptr<BrowsingDataFileSystemHelper> file_system_helper( | 
|  | 142       BrowsingDataFileSystemHelper::Create(&testing_profile_)); | 
|  | 143   StopTestOnCallback stop_test_on_callback(file_system_helper); | 
|  | 144   file_system_helper->DeleteFileSystemOrigin(GURL(kTestOrigin1)); | 
|  | 145   file_system_helper->DeleteFileSystemOrigin(GURL(kTestOrigin2)); | 
|  | 146   file_system_helper->StartFetching( | 
|  | 147       NewCallback(&stop_test_on_callback, | 
|  | 148       &StopTestOnCallback::CallbackDeleteData)); | 
|  | 149   // Blocks until StopTestOnCallback::CallbackDeleteData is notified. | 
|  | 150   ui_test_utils::RunMessageLoop(); | 
|  | 151 } | 
|  | 152 | 
|  | 153 IN_PROC_BROWSER_TEST_F(BrowsingDataFileSystemHelperTest, CannedAddFileSystem) { | 
|  | 154   const GURL origin1(kTestOrigin1); | 
|  | 155   const GURL origin2(kTestOrigin2); | 
|  | 156 | 
|  | 157   scoped_refptr<CannedBrowsingDataFileSystemHelper> helper( | 
|  | 158       new CannedBrowsingDataFileSystemHelper(&testing_profile_)); | 
|  | 159   helper->AddFileSystem(origin1, fileapi::kFileSystemTypeTemporary); | 
|  | 160   helper->AddFileSystem(origin2, fileapi::kFileSystemTypePersistent); | 
|  | 161 | 
|  | 162   TestCompletionCallback callback; | 
|  | 163   helper->StartFetching( | 
|  | 164       NewCallback(&callback, &TestCompletionCallback::callback)); | 
|  | 165 | 
|  | 166   std::vector<BrowsingDataFileSystemHelper::FileSystemInfo> result = | 
|  | 167       callback.result(); | 
|  | 168 | 
|  | 169   ASSERT_EQ(2U, result.size()); | 
|  | 170   ASSERT_EQ(origin1, result[0].origin); | 
|  | 171   ASSERT_TRUE(result[0].has_temporary); | 
|  | 172   ASSERT_FALSE(result[0].has_persistent); | 
|  | 173   ASSERT_EQ(origin2, result[1].origin); | 
|  | 174   ASSERT_FALSE(result[1].has_temporary); | 
|  | 175   ASSERT_TRUE(result[1].has_persistent); | 
|  | 176 } | 
|  | 177 | 
|  | 178 IN_PROC_BROWSER_TEST_F(BrowsingDataFileSystemHelperTest, CannedUnique) { | 
|  | 179   const GURL origin3(kTestOrigin3); | 
|  | 180 | 
|  | 181   scoped_refptr<CannedBrowsingDataFileSystemHelper> helper( | 
|  | 182       new CannedBrowsingDataFileSystemHelper(&testing_profile_)); | 
|  | 183   helper->AddFileSystem(origin3, fileapi::kFileSystemTypePersistent); | 
|  | 184   helper->AddFileSystem(origin3, fileapi::kFileSystemTypeTemporary); | 
|  | 185 | 
|  | 186   TestCompletionCallback callback; | 
|  | 187   helper->StartFetching( | 
|  | 188       NewCallback(&callback, &TestCompletionCallback::callback)); | 
|  | 189 | 
|  | 190   std::vector<BrowsingDataFileSystemHelper::FileSystemInfo> result = | 
|  | 191       callback.result(); | 
|  | 192 | 
|  | 193   ASSERT_EQ(1U, result.size()); | 
|  | 194   ASSERT_EQ(origin3, result[0].origin); | 
|  | 195   ASSERT_TRUE(result[0].has_temporary); | 
|  | 196   ASSERT_TRUE(result[0].has_persistent); | 
|  | 197 } | 
|  | 198 | 
|  | 199 }  // namespace | 
| OLD | NEW | 
|---|