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 "headless/public/util/deterministic_dispatcher.h" | 5 #include "headless/public/util/deterministic_dispatcher.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
| 11 #include "base/memory/ptr_util.h" |
11 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
12 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 14 #include "headless/public/util/navigation_request.h" |
13 #include "headless/public/util/testing/fake_managed_dispatch_url_request_job.h" | 15 #include "headless/public/util/testing/fake_managed_dispatch_url_request_job.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 | 18 |
17 using testing::ElementsAre; | 19 using testing::ElementsAre; |
18 | 20 |
19 namespace headless { | 21 namespace headless { |
20 | 22 |
21 class DeterministicDispatcherTest : public ::testing::Test { | 23 class DeterministicDispatcherTest : public ::testing::Test { |
22 protected: | 24 protected: |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 121 |
120 job->Kill(); | 122 job->Kill(); |
121 } | 123 } |
122 | 124 |
123 EXPECT_TRUE(notifications.empty()); | 125 EXPECT_TRUE(notifications.empty()); |
124 | 126 |
125 base::RunLoop().RunUntilIdle(); | 127 base::RunLoop().RunUntilIdle(); |
126 EXPECT_TRUE(notifications.empty()); | 128 EXPECT_TRUE(notifications.empty()); |
127 } | 129 } |
128 | 130 |
| 131 namespace { |
| 132 class NavigationRequestForTest : public NavigationRequest { |
| 133 public: |
| 134 explicit NavigationRequestForTest(base::Closure* done_closure) |
| 135 : done_closure_(done_closure) {} |
| 136 |
| 137 ~NavigationRequestForTest() override {} |
| 138 |
| 139 // NavigationRequest implementation: |
| 140 void StartProcessing(base::Closure done_callback) override { |
| 141 *done_closure_ = std::move(done_callback); |
| 142 } |
| 143 |
| 144 private: |
| 145 base::Closure* done_closure_; // NOT OWNED |
| 146 }; |
| 147 } // namespace |
| 148 |
| 149 TEST_F(DeterministicDispatcherTest, NavigationBlocksUrlRequests) { |
| 150 std::vector<std::string> notifications; |
| 151 std::unique_ptr<FakeManagedDispatchURLRequestJob> job1( |
| 152 new FakeManagedDispatchURLRequestJob(deterministic_dispatcher_.get(), 1, |
| 153 ¬ifications)); |
| 154 base::Closure navigation_done_closure; |
| 155 deterministic_dispatcher_->NavigationRequested( |
| 156 base::MakeUnique<NavigationRequestForTest>(&navigation_done_closure)); |
| 157 std::unique_ptr<FakeManagedDispatchURLRequestJob> job2( |
| 158 new FakeManagedDispatchURLRequestJob(deterministic_dispatcher_.get(), 2, |
| 159 ¬ifications)); |
| 160 std::unique_ptr<FakeManagedDispatchURLRequestJob> job3( |
| 161 new FakeManagedDispatchURLRequestJob(deterministic_dispatcher_.get(), 3, |
| 162 ¬ifications)); |
| 163 std::unique_ptr<FakeManagedDispatchURLRequestJob> job4( |
| 164 new FakeManagedDispatchURLRequestJob(deterministic_dispatcher_.get(), 4, |
| 165 ¬ifications)); |
| 166 job1->DispatchHeadersComplete(); |
| 167 job2->DispatchHeadersComplete(); |
| 168 job3->DispatchHeadersComplete(); |
| 169 job4->DispatchHeadersComplete(); |
| 170 |
| 171 EXPECT_TRUE(notifications.empty()); |
| 172 |
| 173 base::RunLoop().RunUntilIdle(); |
| 174 EXPECT_THAT(notifications, ElementsAre("id: 1 OK")); |
| 175 |
| 176 // This triggers a call to NavigationRequestForTest::StartProcessing. |
| 177 job1.reset(); |
| 178 base::RunLoop().RunUntilIdle(); |
| 179 EXPECT_THAT(notifications, ElementsAre("id: 1 OK")); |
| 180 |
| 181 // Navigations should be blocked until we call the done closure. |
| 182 navigation_done_closure.Run(); |
| 183 |
| 184 base::RunLoop().RunUntilIdle(); |
| 185 EXPECT_THAT(notifications, ElementsAre("id: 1 OK", "id: 2 OK")); |
| 186 |
| 187 job2.reset(); |
| 188 base::RunLoop().RunUntilIdle(); |
| 189 EXPECT_THAT(notifications, ElementsAre("id: 1 OK", "id: 2 OK", "id: 3 OK")); |
| 190 |
| 191 job3.reset(); |
| 192 base::RunLoop().RunUntilIdle(); |
| 193 EXPECT_THAT(notifications, |
| 194 ElementsAre("id: 1 OK", "id: 2 OK", "id: 3 OK", "id: 4 OK")); |
| 195 } |
| 196 |
129 } // namespace headless | 197 } // namespace headless |
OLD | NEW |