Chromium Code Reviews| Index: content/browser/cache_storage/cache_storage_operation_unittest.cc |
| diff --git a/content/browser/cache_storage/cache_storage_operation_unittest.cc b/content/browser/cache_storage/cache_storage_operation_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8462cc53701a2de5fb08a0043e5c23706fb44991 |
| --- /dev/null |
| +++ b/content/browser/cache_storage/cache_storage_operation_unittest.cc |
| @@ -0,0 +1,89 @@ |
| +// Copyright 2015 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 "content/browser/cache_storage/cache_storage_operation.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/callback.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/run_loop.h" |
| +#include "base/test/histogram_tester.h" |
| +#include "base/test/test_mock_time_task_runner.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
|
jsbell
2016/07/21 18:13:31
Needed?
jkarlin
2016/07/21 18:29:24
Done.
|
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +const char kSlowOperation[] = |
| + "ServiceWorkerCache.CacheStorage.Scheduler.OperationSlow"; |
| + |
| +class TestTask { |
| + public: |
| + TestTask() = default; |
| + ~TestTask() = default; |
| + virtual void Run() { callback_count_++; } |
|
jsbell
2016/07/21 18:13:31
It doesn't look like this needs to be virtual. cop
jkarlin
2016/07/21 18:29:24
Done.
|
| + |
| + int callback_count() const { return callback_count_; } |
| + |
| + protected: |
| + int callback_count_ = 0; |
| +}; |
| + |
| +} // namespace |
| + |
| +class CacheStorageOperationTest : public testing::Test { |
| + protected: |
| + CacheStorageOperationTest() |
| + : mock_task_runner_(new base::TestMockTimeTaskRunner()) { |
| + operation_ = base::MakeUnique<CacheStorageOperation>( |
| + base::Bind(&TestTask::Run, base::Unretained(&task_)), |
| + CacheStorageSchedulerClient::CLIENT_STORAGE, mock_task_runner_); |
| + } |
| + |
| + // TestBrowserThreadBundle browser_thread_bundle_; |
|
jsbell
2016/07/21 18:13:31
Remove
jkarlin
2016/07/21 18:29:24
Done.
|
| + base::HistogramTester histogram_tester_; |
| + TestTask task_; |
| + scoped_refptr<base::TestMockTimeTaskRunner> mock_task_runner_; |
| + std::unique_ptr<CacheStorageOperation> operation_; |
| +}; |
| + |
| +TEST_F(CacheStorageOperationTest, OperationFast) { |
| + operation_->Run(); |
| + mock_task_runner_->RunUntilIdle(); |
| + EXPECT_EQ(1, task_.callback_count()); |
| + histogram_tester_.ExpectTotalCount(kSlowOperation, 0); |
| + |
| + // Finish the operation. |
| + operation_.reset(); |
| + |
| + mock_task_runner_->RunUntilIdle(); |
| + EXPECT_EQ(1, task_.callback_count()); |
| + histogram_tester_.ExpectTotalCount(kSlowOperation, 1); |
| + histogram_tester_.ExpectBucketCount(kSlowOperation, false, 1); |
| +} |
| + |
| +TEST_F(CacheStorageOperationTest, OperationSlow) { |
| + operation_->Run(); |
| + mock_task_runner_->RunUntilIdle(); |
| + EXPECT_EQ(1, task_.callback_count()); |
| + histogram_tester_.ExpectTotalCount(kSlowOperation, 0); |
| + |
| + // The operation takes 10 seconds. |
| + mock_task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(10)); |
| + mock_task_runner_->RunUntilIdle(); |
| + histogram_tester_.ExpectTotalCount(kSlowOperation, 1); |
| + histogram_tester_.ExpectBucketCount(kSlowOperation, true, 1); |
| + |
| + // Finish the operation. |
| + operation_.reset(); |
| + mock_task_runner_->RunUntilIdle(); |
| + EXPECT_EQ(1, task_.callback_count()); |
| + histogram_tester_.ExpectTotalCount(kSlowOperation, 1); |
| + histogram_tester_.ExpectBucketCount(kSlowOperation, true, 1); |
| +} |
| + |
| +} // namespace content |