Chromium Code Reviews| Index: chrome/browser/browsing_data/media_license_counter_browsertest.cc |
| diff --git a/chrome/browser/browsing_data/media_license_counter_browsertest.cc b/chrome/browser/browsing_data/media_license_counter_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4e33fe206658e916ce6e4e4a6f73e5f9ea0ad7d3 |
| --- /dev/null |
| +++ b/chrome/browser/browsing_data/media_license_counter_browsertest.cc |
| @@ -0,0 +1,254 @@ |
| +// Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/browsing_data/media_license_counter.h" |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "base/run_loop.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chrome/test/base/in_process_browser_test.h" |
| +#include "components/prefs/pref_service.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/storage_partition.h" |
| +#include "ppapi/shared_impl/ppapi_constants.h" |
| +#include "storage/browser/fileapi/async_file_util.h" |
| +#include "storage/browser/fileapi/file_system_context.h" |
| +#include "storage/browser/fileapi/file_system_operation_context.h" |
| +#include "storage/browser/fileapi/isolated_context.h" |
| +#include "storage/browser/quota/quota_manager.h" |
| +#include "storage/common/fileapi/file_system_util.h" |
| +#include "url/gurl.h" |
| + |
| +namespace { |
| + |
| +const char kTestOrigin[] = "https://host1/"; |
| +const GURL kOrigin(kTestOrigin); |
| +const char kClearKeyCdmPluginId[] = "application_x-ppapi-clearkey-cdm"; |
| + |
| +// Helper class that waits for an event, but keeps the MessageLoop running. |
| +class AwaitCompletionHelper { |
|
xhwang
2016/06/17 06:24:30
Add a TODO to avoid duplicating this class?
jrummell
2016/06/21 00:13:44
Removed it, as the places it is used is all asynch
|
| + public: |
| + AwaitCompletionHelper() : start_(false), already_quit_(false) {} |
| + virtual ~AwaitCompletionHelper() {} |
| + |
| + void BlockUntilNotified() { |
| + if (!already_quit_) { |
| + DCHECK(!start_); |
| + start_ = true; |
| + base::MessageLoop::current()->Run(); |
| + } else { |
| + DCHECK(!start_); |
| + already_quit_ = false; |
| + } |
| + } |
| + |
| + void Notify() { |
| + if (start_) { |
| + DCHECK(!already_quit_); |
| + base::MessageLoop::current()->QuitWhenIdle(); |
| + start_ = false; |
| + } else { |
| + DCHECK(!already_quit_); |
| + already_quit_ = true; |
| + } |
| + } |
| + |
| + private: |
| + bool start_; |
| + bool already_quit_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(AwaitCompletionHelper); |
| +}; |
| + |
| +class MediaLicenseCounterTest : public InProcessBrowserTest { |
| + public: |
| + void SetUpOnMainThread() override { SetMediaLicenseDeletionPref(true); } |
| + |
| + void SetMediaLicenseDeletionPref(bool value) { |
| + browser()->profile()->GetPrefs()->SetBoolean(prefs::kDeleteMediaLicenses, |
| + value); |
| + } |
| + |
| + // Create some test data for origin |kOrigin|. |
| + void CreateMediaLicenseTestData() { |
| + storage::FileSystemContext* filesystem_context = |
| + content::BrowserContext::GetDefaultStoragePartition( |
| + browser()->profile()) |
| + ->GetFileSystemContext(); |
| + std::string clearkey_fsid = |
| + CreateFileSystem(filesystem_context, kClearKeyCdmPluginId, kOrigin); |
| + CreateFile(filesystem_context, kOrigin, clearkey_fsid, "foo"); |
| + } |
| + |
| + // Wait for IO thread operations, such as cache creation, counting, writing, |
| + // deletion etc. |
| + void WaitForIOThread() { |
|
xhwang
2016/06/17 06:24:30
This is copied from cache_counter_browsertest.cc b
jrummell
2016/06/21 00:13:44
Done (and simplified to use base::MessageLoop like
|
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + run_loop_.reset(new base::RunLoop()); |
| + run_loop_->Run(); |
| + } |
| + |
| + // Callback from the counter. |
| + void CountingCallback(std::unique_ptr<BrowsingDataCounter::Result> result) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + callback_called_ = true; |
| + finished_ = result->Finished(); |
| + if (finished_) { |
| + MediaLicenseCounter::MediaLicenseResult* media_result = |
| + static_cast<MediaLicenseCounter::MediaLicenseResult*>(result.get()); |
| + count_ = media_result->Value(); |
| + origin_ = media_result->GetOneOrigin(); |
| + } |
| + |
| + if (run_loop_ && finished_) |
| + run_loop_->Quit(); |
| + } |
| + |
| + bool CallbackCalled() { return callback_called_; } |
| + |
| + BrowsingDataCounter::ResultInt GetCount() { |
| + DCHECK(finished_); |
| + return count_; |
| + } |
| + |
| + const std::string& GetOrigin() { |
| + DCHECK(finished_); |
| + return origin_; |
| + } |
| + |
| + private: |
| + // Creates a PluginPrivateFileSystem for the |plugin_name| and |origin| |
| + // provided. Returns the file system ID for the created |
| + // PluginPrivateFileSystem. |
| + std::string CreateFileSystem(storage::FileSystemContext* filesystem_context, |
| + const std::string& plugin_name, |
| + const GURL& origin) { |
| + AwaitCompletionHelper await_completion; |
| + std::string fsid = storage::IsolatedContext::GetInstance() |
| + ->RegisterFileSystemForVirtualPath( |
| + storage::kFileSystemTypePluginPrivate, |
| + ppapi::kPluginPrivateRootName, base::FilePath()); |
| + EXPECT_TRUE(storage::ValidateIsolatedFileSystemId(fsid)); |
| + filesystem_context->OpenPluginPrivateFileSystem( |
| + origin, storage::kFileSystemTypePluginPrivate, fsid, plugin_name, |
| + storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
| + base::Bind(&MediaLicenseCounterTest::OnFileSystemOpened, |
| + base::Unretained(this), &await_completion)); |
| + await_completion.BlockUntilNotified(); |
| + return fsid; |
| + } |
| + |
| + // Creates a file named |file_name| in the PluginPrivateFileSystem identified |
| + // by |origin| and |fsid|. |
| + void CreateFile(storage::FileSystemContext* filesystem_context, |
| + const GURL& origin, |
| + const std::string& fsid, |
| + const std::string& file_name) { |
| + AwaitCompletionHelper await_completion; |
| + std::string root = storage::GetIsolatedFileSystemRootURIString( |
| + origin, fsid, ppapi::kPluginPrivateRootName); |
| + storage::FileSystemURL file_url = |
| + filesystem_context->CrackURL(GURL(root + file_name)); |
| + storage::AsyncFileUtil* file_util = filesystem_context->GetAsyncFileUtil( |
| + storage::kFileSystemTypePluginPrivate); |
| + std::unique_ptr<storage::FileSystemOperationContext> operation_context = |
| + base::WrapUnique( |
| + new storage::FileSystemOperationContext(filesystem_context)); |
| + operation_context->set_allowed_bytes_growth( |
| + storage::QuotaManager::kNoLimit); |
| + file_util->EnsureFileExists( |
| + std::move(operation_context), file_url, |
| + base::Bind(&MediaLicenseCounterTest::OnFileCreated, |
| + base::Unretained(this), &await_completion)); |
| + await_completion.BlockUntilNotified(); |
| + } |
| + |
| + void OnFileSystemOpened(AwaitCompletionHelper* await_completion, |
| + base::File::Error result) { |
| + EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result); |
| + await_completion->Notify(); |
| + } |
| + |
| + void OnFileCreated(AwaitCompletionHelper* await_completion, |
| + base::File::Error result, |
| + bool created) { |
| + EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result); |
| + EXPECT_TRUE(created); |
| + await_completion->Notify(); |
| + } |
| + |
| + bool callback_called_ = false; |
| + bool finished_ = false; |
| + BrowsingDataCounter::ResultInt count_; |
| + std::string origin_; |
| + |
| + std::unique_ptr<base::RunLoop> run_loop_; |
| +}; |
| + |
| +// Tests that for the empty file system, the result is zero. |
| +IN_PROC_BROWSER_TEST_F(MediaLicenseCounterTest, Empty) { |
| + MediaLicenseCounter counter; |
| + counter.Init(browser()->profile(), |
| + base::Bind(&MediaLicenseCounterTest::CountingCallback, |
| + base::Unretained(this))); |
| + counter.Restart(); |
| + |
| + WaitForIOThread(); |
| + |
| + EXPECT_TRUE(CallbackCalled()); |
| + EXPECT_EQ(0u, GetCount()); |
| + EXPECT_TRUE(GetOrigin().empty()); |
| +} |
| + |
| +// Tests that for a non-empty cache, the result is nonzero. |
| +IN_PROC_BROWSER_TEST_F(MediaLicenseCounterTest, NonEmpty) { |
| + CreateMediaLicenseTestData(); |
| + |
| + MediaLicenseCounter counter; |
| + counter.Init(browser()->profile(), |
| + base::Bind(&MediaLicenseCounterTest::CountingCallback, |
| + base::Unretained(this))); |
| + counter.Restart(); |
| + |
| + WaitForIOThread(); |
| + |
| + EXPECT_TRUE(CallbackCalled()); |
| + EXPECT_EQ(1u, GetCount()); |
| + EXPECT_EQ(kTestOrigin, GetOrigin()); |
| +} |
| + |
| +// Tests that the counter does not count if the deletion preference is false. |
| +IN_PROC_BROWSER_TEST_F(MediaLicenseCounterTest, PrefIsFalse) { |
| + SetMediaLicenseDeletionPref(false); |
| + |
| + MediaLicenseCounter counter; |
| + counter.Init(browser()->profile(), |
| + base::Bind(&MediaLicenseCounterTest::CountingCallback, |
| + base::Unretained(this))); |
| + counter.Restart(); |
| + |
| + EXPECT_FALSE(CallbackCalled()); |
| +} |
| + |
| +// Tests that the counter starts counting automatically when the deletion |
| +// pref changes to true. |
| +IN_PROC_BROWSER_TEST_F(MediaLicenseCounterTest, PrefChanged) { |
| + SetMediaLicenseDeletionPref(false); |
| + |
| + MediaLicenseCounter counter; |
| + counter.Init(browser()->profile(), |
| + base::Bind(&MediaLicenseCounterTest::CountingCallback, |
| + base::Unretained(this))); |
| + SetMediaLicenseDeletionPref(true); |
| + |
| + WaitForIOThread(); |
| + |
| + EXPECT_TRUE(CallbackCalled()); |
| + EXPECT_EQ(0u, GetCount()); |
| + EXPECT_TRUE(GetOrigin().empty()); |
| +} |
| + |
| +} // namespace |