OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/browser/preload_check_test_util.h" | 5 #include "extensions/browser/preload_check_test_util.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
14 #include "extensions/common/extension.h" | 14 #include "extensions/common/extension.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 namespace extensions { | 17 namespace extensions { |
18 | 18 |
19 // PreloadCheckRunner: | 19 // PreloadCheckRunner: |
20 PreloadCheckRunner::PreloadCheckRunner() : called_(false) {} | 20 PreloadCheckRunner::PreloadCheckRunner() : called_(false) {} |
21 PreloadCheckRunner::~PreloadCheckRunner() {} | 21 PreloadCheckRunner::~PreloadCheckRunner() {} |
22 | 22 |
23 void PreloadCheckRunner::OnCheckComplete(PreloadCheck::Errors errors) { | |
24 ASSERT_FALSE(called_); | |
25 called_ = true; | |
26 errors_ = errors; | |
27 if (run_loop_) | |
28 run_loop_->Quit(); | |
29 } | |
30 | |
31 void PreloadCheckRunner::Run(PreloadCheck* check) { | 23 void PreloadCheckRunner::Run(PreloadCheck* check) { |
32 check->Start(GetCallback()); | 24 check->Start(GetCallback()); |
33 } | 25 } |
34 | 26 |
35 void PreloadCheckRunner::RunUntilComplete(PreloadCheck* check) { | 27 void PreloadCheckRunner::RunUntilComplete(PreloadCheck* check) { |
36 Run(check); | 28 Run(check); |
37 ASSERT_FALSE(called_); | 29 ASSERT_FALSE(called_); |
38 | 30 |
39 run_loop_ = base::MakeUnique<base::RunLoop>(); | 31 WaitForComplete(); |
40 run_loop_->Run(); | |
41 ASSERT_TRUE(called_); | 32 ASSERT_TRUE(called_); |
42 } | 33 } |
43 | 34 |
| 35 void PreloadCheckRunner::WaitForComplete() { |
| 36 run_loop_ = base::MakeUnique<base::RunLoop>(); |
| 37 run_loop_->Run(); |
| 38 } |
| 39 |
| 40 void PreloadCheckRunner::WaitForIdle() { |
| 41 run_loop_ = base::MakeUnique<base::RunLoop>(); |
| 42 run_loop_->RunUntilIdle(); |
| 43 } |
| 44 |
44 PreloadCheck::ResultCallback PreloadCheckRunner::GetCallback() { | 45 PreloadCheck::ResultCallback PreloadCheckRunner::GetCallback() { |
45 return base::Bind(&PreloadCheckRunner::OnCheckComplete, | 46 return base::Bind(&PreloadCheckRunner::OnCheckComplete, |
46 base::Unretained(this)); | 47 base::Unretained(this)); |
47 } | 48 } |
48 | 49 |
49 void PreloadCheckRunner::WaitForIdle() { | 50 void PreloadCheckRunner::OnCheckComplete(PreloadCheck::Errors errors) { |
50 run_loop_ = base::MakeUnique<base::RunLoop>(); | 51 ASSERT_FALSE(called_); |
51 run_loop_->RunUntilIdle(); | 52 called_ = true; |
| 53 errors_ = errors; |
| 54 |
| 55 if (run_loop_) |
| 56 run_loop_->Quit(); |
52 } | 57 } |
53 | 58 |
54 // PreloadCheckStub: | 59 // PreloadCheckStub: |
55 PreloadCheckStub::PreloadCheckStub() | 60 PreloadCheckStub::PreloadCheckStub(const Errors& errors) |
56 : PreloadCheck(nullptr), is_async_(false), weak_ptr_factory_(this) {} | 61 : PreloadCheck(nullptr), errors_(errors), weak_ptr_factory_(this) {} |
57 | 62 |
58 PreloadCheckStub::~PreloadCheckStub() {} | 63 PreloadCheckStub::~PreloadCheckStub() {} |
59 | 64 |
60 void PreloadCheckStub::AddError(Error error) { | |
61 errors_.insert(error); | |
62 } | |
63 | |
64 void PreloadCheckStub::Start(ResultCallback callback) { | 65 void PreloadCheckStub::Start(ResultCallback callback) { |
65 DCHECK(!callback.is_null()); | 66 DCHECK(!callback.is_null()); |
| 67 started_ = true; |
66 if (is_async_) { | 68 if (is_async_) { |
67 // TODO(michaelpg): Bind the callback directly and remove RunCallback | 69 // TODO(michaelpg): Bind the callback directly and remove RunCallback |
68 // once crbug.com/704027 is addressed. | 70 // once crbug.com/704027 is addressed. |
69 base::ThreadTaskRunnerHandle::Get()->PostTask( | 71 base::ThreadTaskRunnerHandle::Get()->PostTask( |
70 FROM_HERE, | 72 FROM_HERE, |
71 base::Bind(&PreloadCheckStub::RunCallback, | 73 base::Bind(&PreloadCheckStub::RunCallback, |
72 weak_ptr_factory_.GetWeakPtr(), base::Passed(&callback))); | 74 weak_ptr_factory_.GetWeakPtr(), base::Passed(&callback))); |
73 } else { | 75 } else { |
74 std::move(callback).Run(errors_); | 76 std::move(callback).Run(errors_); |
75 } | 77 } |
76 } | 78 } |
77 | 79 |
78 void PreloadCheckStub::RunCallback(ResultCallback callback) { | 80 void PreloadCheckStub::RunCallback(ResultCallback callback) { |
79 std::move(callback).Run(errors_); | 81 std::move(callback).Run(errors_); |
80 } | 82 } |
81 | 83 |
82 base::string16 PreloadCheckStub::GetErrorMessage() const { | |
83 return message_; | |
84 } | |
85 | |
86 } // namespace extensions | 84 } // namespace extensions |
OLD | NEW |