preload_check_test_util.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2017 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. #include "extensions/browser/preload_check_test_util.h"
  5. #include "base/bind.h"
  6. #include "base/callback_helpers.h"
  7. #include "base/check.h"
  8. #include "base/run_loop.h"
  9. #include "base/task/single_thread_task_runner.h"
  10. #include "base/threading/thread_task_runner_handle.h"
  11. #include "extensions/common/extension.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. namespace extensions {
  14. // PreloadCheckRunner:
  15. PreloadCheckRunner::PreloadCheckRunner() : called_(false) {}
  16. PreloadCheckRunner::~PreloadCheckRunner() {}
  17. void PreloadCheckRunner::Run(PreloadCheck* check) {
  18. check->Start(GetCallback());
  19. }
  20. void PreloadCheckRunner::RunUntilComplete(PreloadCheck* check) {
  21. Run(check);
  22. ASSERT_FALSE(called_);
  23. WaitForComplete();
  24. ASSERT_TRUE(called_);
  25. }
  26. void PreloadCheckRunner::WaitForComplete() {
  27. run_loop_ = std::make_unique<base::RunLoop>();
  28. run_loop_->Run();
  29. }
  30. void PreloadCheckRunner::WaitForIdle() {
  31. run_loop_ = std::make_unique<base::RunLoop>();
  32. run_loop_->RunUntilIdle();
  33. }
  34. PreloadCheck::ResultCallback PreloadCheckRunner::GetCallback() {
  35. return base::BindOnce(&PreloadCheckRunner::OnCheckComplete,
  36. base::Unretained(this));
  37. }
  38. void PreloadCheckRunner::OnCheckComplete(const PreloadCheck::Errors& errors) {
  39. ASSERT_FALSE(called_);
  40. called_ = true;
  41. errors_ = errors;
  42. if (run_loop_)
  43. run_loop_->Quit();
  44. }
  45. // PreloadCheckStub:
  46. PreloadCheckStub::PreloadCheckStub(const Errors& errors)
  47. : PreloadCheck(nullptr), errors_(errors) {}
  48. PreloadCheckStub::~PreloadCheckStub() {}
  49. void PreloadCheckStub::Start(ResultCallback callback) {
  50. DCHECK(!callback.is_null());
  51. started_ = true;
  52. if (is_async_) {
  53. // TODO(michaelpg): Bind the callback directly and remove RunCallback
  54. // once crbug.com/704027 is addressed.
  55. base::ThreadTaskRunnerHandle::Get()->PostTask(
  56. FROM_HERE,
  57. base::BindOnce(&PreloadCheckStub::RunCallback,
  58. weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
  59. } else {
  60. std::move(callback).Run(errors_);
  61. }
  62. }
  63. void PreloadCheckStub::RunCallback(ResultCallback callback) {
  64. std::move(callback).Run(errors_);
  65. }
  66. } // namespace extensions