| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chrome/browser/browsing_data/media_licenses_counter.h" | 5 #include "chrome/browser/browsing_data/media_licenses_counter.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/common/pref_names.h" | 11 #include "chrome/common/pref_names.h" |
| 11 #include "chrome/test/base/in_process_browser_test.h" | 12 #include "chrome/test/base/in_process_browser_test.h" |
| 12 #include "components/prefs/pref_service.h" | 13 #include "components/prefs/pref_service.h" |
| 13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/storage_partition.h" | 15 #include "content/public/browser/storage_partition.h" |
| 15 #include "ppapi/shared_impl/ppapi_constants.h" | 16 #include "ppapi/shared_impl/ppapi_constants.h" |
| 16 #include "storage/browser/fileapi/async_file_util.h" | 17 #include "storage/browser/fileapi/async_file_util.h" |
| 17 #include "storage/browser/fileapi/file_system_context.h" | 18 #include "storage/browser/fileapi/file_system_context.h" |
| 18 #include "storage/browser/fileapi/file_system_operation_context.h" | 19 #include "storage/browser/fileapi/file_system_operation_context.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 48 } | 49 } |
| 49 | 50 |
| 50 // Start running and allow the asynchronous IO operations to complete. | 51 // Start running and allow the asynchronous IO operations to complete. |
| 51 void RunAndWaitForResult() { | 52 void RunAndWaitForResult() { |
| 52 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 53 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 53 run_loop_.reset(new base::RunLoop()); | 54 run_loop_.reset(new base::RunLoop()); |
| 54 run_loop_->Run(); | 55 run_loop_->Run(); |
| 55 } | 56 } |
| 56 | 57 |
| 57 // Callback from the counter. | 58 // Callback from the counter. |
| 58 void CountingCallback(std::unique_ptr<BrowsingDataCounter::Result> result) { | 59 void CountingCallback( |
| 60 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { |
| 59 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 61 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 60 | 62 |
| 61 callback_called_ = true; | 63 callback_called_ = true; |
| 62 finished_ = result->Finished(); | 64 finished_ = result->Finished(); |
| 63 if (finished_) { | 65 if (finished_) { |
| 64 MediaLicensesCounter::MediaLicenseResult* media_result = | 66 MediaLicensesCounter::MediaLicenseResult* media_result = |
| 65 static_cast<MediaLicensesCounter::MediaLicenseResult*>(result.get()); | 67 static_cast<MediaLicensesCounter::MediaLicenseResult*>(result.get()); |
| 66 count_ = media_result->Value(); | 68 count_ = media_result->Value(); |
| 67 origin_ = media_result->GetOneOrigin(); | 69 origin_ = media_result->GetOneOrigin(); |
| 68 } | 70 } |
| 69 | 71 |
| 70 if (run_loop_ && finished_) | 72 if (run_loop_ && finished_) |
| 71 run_loop_->Quit(); | 73 run_loop_->Quit(); |
| 72 } | 74 } |
| 73 | 75 |
| 74 bool CallbackCalled() { return callback_called_; } | 76 bool CallbackCalled() { return callback_called_; } |
| 75 | 77 |
| 76 BrowsingDataCounter::ResultInt GetCount() { | 78 browsing_data::BrowsingDataCounter::ResultInt GetCount() { |
| 77 DCHECK(finished_); | 79 DCHECK(finished_); |
| 78 return count_; | 80 return count_; |
| 79 } | 81 } |
| 80 | 82 |
| 81 const std::string& GetOrigin() { | 83 const std::string& GetOrigin() { |
| 82 DCHECK(finished_); | 84 DCHECK(finished_); |
| 83 return origin_; | 85 return origin_; |
| 84 } | 86 } |
| 85 | 87 |
| 86 private: | 88 private: |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 | 138 |
| 137 void OnFileCreated(base::File::Error result, bool created) { | 139 void OnFileCreated(base::File::Error result, bool created) { |
| 138 EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result); | 140 EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result); |
| 139 EXPECT_TRUE(created); | 141 EXPECT_TRUE(created); |
| 140 if (run_loop_) | 142 if (run_loop_) |
| 141 run_loop_->Quit(); | 143 run_loop_->Quit(); |
| 142 } | 144 } |
| 143 | 145 |
| 144 bool callback_called_ = false; | 146 bool callback_called_ = false; |
| 145 bool finished_ = false; | 147 bool finished_ = false; |
| 146 BrowsingDataCounter::ResultInt count_; | 148 browsing_data::BrowsingDataCounter::ResultInt count_; |
| 147 std::string origin_; | 149 std::string origin_; |
| 148 | 150 |
| 149 std::unique_ptr<base::RunLoop> run_loop_; | 151 std::unique_ptr<base::RunLoop> run_loop_; |
| 150 }; | 152 }; |
| 151 | 153 |
| 152 // Tests that for the empty file system, the result is zero. | 154 // Tests that for the empty file system, the result is zero. |
| 153 IN_PROC_BROWSER_TEST_F(MediaLicensesCounterTest, Empty) { | 155 IN_PROC_BROWSER_TEST_F(MediaLicensesCounterTest, Empty) { |
| 154 MediaLicensesCounter counter; | 156 Profile* profile = browser()->profile(); |
| 155 counter.Init(browser()->profile(), | 157 MediaLicensesCounter counter(profile); |
| 158 counter.Init(profile->GetPrefs(), |
| 156 base::Bind(&MediaLicensesCounterTest::CountingCallback, | 159 base::Bind(&MediaLicensesCounterTest::CountingCallback, |
| 157 base::Unretained(this))); | 160 base::Unretained(this))); |
| 158 counter.Restart(); | 161 counter.Restart(); |
| 159 | 162 |
| 160 RunAndWaitForResult(); | 163 RunAndWaitForResult(); |
| 161 | 164 |
| 162 EXPECT_TRUE(CallbackCalled()); | 165 EXPECT_TRUE(CallbackCalled()); |
| 163 EXPECT_EQ(0u, GetCount()); | 166 EXPECT_EQ(0u, GetCount()); |
| 164 EXPECT_TRUE(GetOrigin().empty()); | 167 EXPECT_TRUE(GetOrigin().empty()); |
| 165 } | 168 } |
| 166 | 169 |
| 167 // Tests that for a non-empty file system, the result is nonzero. | 170 // Tests that for a non-empty file system, the result is nonzero. |
| 168 IN_PROC_BROWSER_TEST_F(MediaLicensesCounterTest, NonEmpty) { | 171 IN_PROC_BROWSER_TEST_F(MediaLicensesCounterTest, NonEmpty) { |
| 169 CreateMediaLicenseTestData(); | 172 CreateMediaLicenseTestData(); |
| 170 | 173 |
| 171 MediaLicensesCounter counter; | 174 Profile* profile = browser()->profile(); |
| 172 counter.Init(browser()->profile(), | 175 MediaLicensesCounter counter(profile); |
| 176 counter.Init(profile->GetPrefs(), |
| 173 base::Bind(&MediaLicensesCounterTest::CountingCallback, | 177 base::Bind(&MediaLicensesCounterTest::CountingCallback, |
| 174 base::Unretained(this))); | 178 base::Unretained(this))); |
| 175 counter.Restart(); | 179 counter.Restart(); |
| 176 | 180 |
| 177 RunAndWaitForResult(); | 181 RunAndWaitForResult(); |
| 178 | 182 |
| 179 EXPECT_TRUE(CallbackCalled()); | 183 EXPECT_TRUE(CallbackCalled()); |
| 180 EXPECT_EQ(1u, GetCount()); | 184 EXPECT_EQ(1u, GetCount()); |
| 181 EXPECT_EQ(kOrigin.host(), GetOrigin()); | 185 EXPECT_EQ(kOrigin.host(), GetOrigin()); |
| 182 } | 186 } |
| 183 | 187 |
| 184 // Tests that the counter does not count if the deletion preference is false. | 188 // Tests that the counter does not count if the deletion preference is false. |
| 185 IN_PROC_BROWSER_TEST_F(MediaLicensesCounterTest, PrefIsFalse) { | 189 IN_PROC_BROWSER_TEST_F(MediaLicensesCounterTest, PrefIsFalse) { |
| 186 SetMediaLicenseDeletionPref(false); | 190 SetMediaLicenseDeletionPref(false); |
| 187 | 191 |
| 188 MediaLicensesCounter counter; | 192 Profile* profile = browser()->profile(); |
| 189 counter.Init(browser()->profile(), | 193 MediaLicensesCounter counter(profile); |
| 194 counter.Init(profile->GetPrefs(), |
| 190 base::Bind(&MediaLicensesCounterTest::CountingCallback, | 195 base::Bind(&MediaLicensesCounterTest::CountingCallback, |
| 191 base::Unretained(this))); | 196 base::Unretained(this))); |
| 192 counter.Restart(); | 197 counter.Restart(); |
| 193 | 198 |
| 194 EXPECT_FALSE(CallbackCalled()); | 199 EXPECT_FALSE(CallbackCalled()); |
| 195 } | 200 } |
| 196 | 201 |
| 197 // Tests that the counter starts counting automatically when the deletion | 202 // Tests that the counter starts counting automatically when the deletion |
| 198 // pref changes to true. | 203 // pref changes to true. |
| 199 IN_PROC_BROWSER_TEST_F(MediaLicensesCounterTest, PrefChanged) { | 204 IN_PROC_BROWSER_TEST_F(MediaLicensesCounterTest, PrefChanged) { |
| 200 SetMediaLicenseDeletionPref(false); | 205 SetMediaLicenseDeletionPref(false); |
| 201 | 206 |
| 202 MediaLicensesCounter counter; | 207 Profile* profile = browser()->profile(); |
| 203 counter.Init(browser()->profile(), | 208 MediaLicensesCounter counter(profile); |
| 209 counter.Init(profile->GetPrefs(), |
| 204 base::Bind(&MediaLicensesCounterTest::CountingCallback, | 210 base::Bind(&MediaLicensesCounterTest::CountingCallback, |
| 205 base::Unretained(this))); | 211 base::Unretained(this))); |
| 206 SetMediaLicenseDeletionPref(true); | 212 SetMediaLicenseDeletionPref(true); |
| 207 | 213 |
| 208 RunAndWaitForResult(); | 214 RunAndWaitForResult(); |
| 209 | 215 |
| 210 EXPECT_TRUE(CallbackCalled()); | 216 EXPECT_TRUE(CallbackCalled()); |
| 211 EXPECT_EQ(0u, GetCount()); | 217 EXPECT_EQ(0u, GetCount()); |
| 212 EXPECT_TRUE(GetOrigin().empty()); | 218 EXPECT_TRUE(GetOrigin().empty()); |
| 213 } | 219 } |
| 214 | 220 |
| 215 } // namespace | 221 } // namespace |
| OLD | NEW |