Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 5 | #include "content/browser/presentation/presentation_service_impl.h" |
| 6 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 9 | |
mfoltz | 0dc2a6b | 2017-03-07 18:24:19 | [diff] [blame] | 10 | #include <iterator> |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 11 | #include <memory> |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 12 | #include <optional> |
kmarshall | f5367c64 | 2015-08-20 19:23:13 | [diff] [blame] | 13 | #include <string> |
Avi Drissman | 5d5d48d6 | 2022-01-07 20:23:58 | [diff] [blame] | 14 | #include <tuple> |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 15 | #include <utility> |
kmarshall | f5367c64 | 2015-08-20 19:23:13 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 18 | #include "base/functional/bind.h" |
| 19 | #include "base/functional/callback_helpers.h" |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 20 | #include "base/run_loop.h" |
Lei Zhang | e02299a | 2021-04-26 23:12:24 | [diff] [blame] | 21 | #include "base/strings/stringprintf.h" |
Rakina Zata Amni | c7bc8263 | 2019-12-09 05:21:22 | [diff] [blame] | 22 | #include "content/browser/presentation/presentation_test_utils.h" |
Derek Cheng | 1071a23 | 2017-07-25 22:23:56 | [diff] [blame] | 23 | #include "content/public/browser/presentation_request.h" |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 24 | #include "content/public/browser/presentation_service_delegate.h" |
Camille Lamy | fa5767c | 2019-01-07 14:53:59 | [diff] [blame] | 25 | #include "content/public/test/mock_navigation_handle.h" |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 26 | #include "content/test/test_render_frame_host.h" |
| 27 | #include "content/test/test_render_view_host.h" |
| 28 | #include "content/test/test_web_contents.h" |
Mario Sanchez Prada | 2311a9e | 2019-08-26 14:29:49 | [diff] [blame] | 29 | #include "mojo/public/cpp/bindings/pending_receiver.h" |
| 30 | #include "mojo/public/cpp/bindings/pending_remote.h" |
| 31 | #include "mojo/public/cpp/bindings/receiver.h" |
| 32 | #include "mojo/public/cpp/bindings/remote.h" |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 33 | #include "testing/gmock/include/gmock/gmock.h" |
| 34 | |
Wei4 Wang | e1c8c00 | 2023-04-24 23:30:25 | [diff] [blame] | 35 | using blink::mojom::PresentationConnectionResult; |
| 36 | using blink::mojom::PresentationInfo; |
| 37 | |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 38 | namespace content { |
| 39 | |
imcheng | 2ed5f820 | 2015-04-24 19:41:17 | [diff] [blame] | 40 | namespace { |
| 41 | |
mfoltz | fcad5262 | 2016-09-03 03:08:33 | [diff] [blame] | 42 | const char kPresentationId[] = "presentationId"; |
| 43 | const char kPresentationUrl1[] = "http://foo.com/index.html"; |
| 44 | const char kPresentationUrl2[] = "http://example.com/index.html"; |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 45 | const char kPresentationUrl3[] = "http://example.net/index.html"; |
haibinlu | 4ba4019 | 2015-06-23 21:43:09 | [diff] [blame] | 46 | |
imcheng | 2ed5f820 | 2015-04-24 19:41:17 | [diff] [blame] | 47 | } // namespace |
| 48 | |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 49 | class PresentationServiceImplTest : public RenderViewHostImplTestHarness { |
| 50 | public: |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 51 | PresentationServiceImplTest() |
| 52 | : presentation_url1_(GURL(kPresentationUrl1)), |
| 53 | presentation_url2_(GURL(kPresentationUrl2)), |
| 54 | presentation_url3_(GURL(kPresentationUrl3)) {} |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 55 | |
| 56 | void SetUp() override { |
| 57 | RenderViewHostImplTestHarness::SetUp(); |
Avi Drissman | 650a39d1 | 2020-07-14 17:18:29 | [diff] [blame] | 58 | // This needed to keep the WebContentsObserverConsistencyChecker checks |
| 59 | // happy for when AppendChild is called. |
jam | 5fdbb58d | 2017-02-06 22:09:59 | [diff] [blame] | 60 | NavigateAndCommit(GURL("about:blank")); |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 61 | |
imcheng | 2ed5f820 | 2015-04-24 19:41:17 | [diff] [blame] | 62 | EXPECT_CALL(mock_delegate_, AddObserver(_, _, _)).Times(1); |
Dave Tapuska | 327c06c9 | 2022-06-13 20:31:51 | [diff] [blame] | 63 | TestRenderFrameHost* render_frame_host = contents()->GetPrimaryMainFrame(); |
imcheng | ad59d85 | 2016-04-21 20:05:06 | [diff] [blame] | 64 | render_frame_host->InitializeRenderFrameIfNeeded(); |
imcheng | 16819b9 | 2015-04-03 08:52:45 | [diff] [blame] | 65 | service_impl_.reset(new PresentationServiceImpl( |
zhaobin | f3704f8 | 2017-01-12 07:19:36 | [diff] [blame] | 66 | render_frame_host, contents(), &mock_delegate_, nullptr)); |
imcheng | 9ce5394b | 2015-05-12 19:27:01 | [diff] [blame] | 67 | |
Mario Sanchez Prada | 2ae29e5a | 2019-08-26 14:42:23 | [diff] [blame] | 68 | mojo::PendingRemote<PresentationController> presentation_controller_remote; |
| 69 | controller_receiver_.emplace( |
| 70 | &mock_controller_, |
| 71 | presentation_controller_remote.InitWithNewPipeAndPassReceiver()); |
| 72 | service_impl_->SetController(std::move(presentation_controller_remote)); |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 73 | |
| 74 | presentation_urls_.push_back(presentation_url1_); |
| 75 | presentation_urls_.push_back(presentation_url2_); |
mark a. foltz | d9d2a451 | 2018-04-16 20:42:45 | [diff] [blame] | 76 | |
| 77 | expect_presentation_success_cb_ = |
| 78 | base::BindOnce(&PresentationServiceImplTest::ExpectPresentationSuccess, |
| 79 | base::Unretained(this)); |
| 80 | expect_presentation_error_cb_ = |
| 81 | base::BindOnce(&PresentationServiceImplTest::ExpectPresentationError, |
| 82 | base::Unretained(this)); |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void TearDown() override { |
imcheng | 16819b9 | 2015-04-03 08:52:45 | [diff] [blame] | 86 | if (service_impl_.get()) { |
Derek Cheng | 4de47c5 | 2017-10-10 01:05:54 | [diff] [blame] | 87 | ExpectDelegateReset(); |
imcheng | 2ed5f820 | 2015-04-24 19:41:17 | [diff] [blame] | 88 | EXPECT_CALL(mock_delegate_, RemoveObserver(_, _)).Times(1); |
imcheng | 16819b9 | 2015-04-03 08:52:45 | [diff] [blame] | 89 | service_impl_.reset(); |
| 90 | } |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 91 | RenderViewHostImplTestHarness::TearDown(); |
| 92 | } |
| 93 | |
jam | 5fdbb58d | 2017-02-06 22:09:59 | [diff] [blame] | 94 | void Navigate(bool main_frame) { |
mfoltz | 0dc2a6b | 2017-03-07 18:24:19 | [diff] [blame] | 95 | RenderFrameHost* rfh = main_rfh(); |
| 96 | RenderFrameHostTester* rfh_tester = RenderFrameHostTester::For(rfh); |
jam | 5fdbb58d | 2017-02-06 22:09:59 | [diff] [blame] | 97 | if (!main_frame) |
| 98 | rfh = rfh_tester->AppendChild("subframe"); |
Camille Lamy | fa5767c | 2019-01-07 14:53:59 | [diff] [blame] | 99 | MockNavigationHandle handle(GURL(), rfh); |
| 100 | handle.set_has_committed(true); |
| 101 | service_impl_->DidFinishNavigation(&handle); |
jam | 5fdbb58d | 2017-02-06 22:09:59 | [diff] [blame] | 102 | } |
| 103 | |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 104 | void ListenForScreenAvailabilityAndWait(const GURL& url, |
tapted | 8f125bdd | 2016-08-01 04:51:09 | [diff] [blame] | 105 | bool delegate_success) { |
mlamouri | 0c8ce52 | 2015-10-02 21:47:38 | [diff] [blame] | 106 | EXPECT_CALL(mock_delegate_, AddScreenAvailabilityListener()) |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 107 | .WillOnce(Return(delegate_success)); |
| 108 | service_impl_->ListenForScreenAvailability(url); |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 109 | |
| 110 | EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); |
| 111 | } |
| 112 | |
Anton Vayvod | e3c3986 | 2017-06-20 20:53:32 | [diff] [blame] | 113 | void SimulateScreenAvailabilityChangeAndWait( |
| 114 | const GURL& url, |
| 115 | ScreenAvailability availability) { |
mfoltz | 3f30502f | 2015-08-11 16:13:38 | [diff] [blame] | 116 | auto listener_it = service_impl_->screen_availability_listeners_.find(url); |
| 117 | ASSERT_TRUE(listener_it->second); |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 118 | |
Derek Cheng | f8ffbec5 | 2017-12-06 19:43:04 | [diff] [blame] | 119 | EXPECT_CALL(mock_controller_, |
| 120 | OnScreenAvailabilityUpdated(url, availability)); |
Anton Vayvod | e3c3986 | 2017-06-20 20:53:32 | [diff] [blame] | 121 | listener_it->second->OnScreenAvailabilityChanged(availability); |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 122 | base::RunLoop().RunUntilIdle(); |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 123 | } |
| 124 | |
Derek Cheng | 4de47c5 | 2017-10-10 01:05:54 | [diff] [blame] | 125 | void ExpectDelegateReset() { |
imcheng | 2ed5f820 | 2015-04-24 19:41:17 | [diff] [blame] | 126 | EXPECT_CALL(mock_delegate_, Reset(_, _)).Times(1); |
imcheng | 271b9ef | 2015-03-18 19:43:19 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void ExpectCleanState() { |
mfoltz | fcad5262 | 2016-09-03 03:08:33 | [diff] [blame] | 130 | EXPECT_TRUE(service_impl_->default_presentation_urls_.empty()); |
mfoltz | 3f30502f | 2015-08-11 16:13:38 | [diff] [blame] | 131 | EXPECT_EQ( |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 132 | service_impl_->screen_availability_listeners_.find(presentation_url1_), |
mfoltz | 3f30502f | 2015-08-11 16:13:38 | [diff] [blame] | 133 | service_impl_->screen_availability_listeners_.end()); |
imcheng | 271b9ef | 2015-03-18 19:43:19 | [diff] [blame] | 134 | } |
| 135 | |
btolsch | 8ca10fb2 | 2018-08-29 01:35:09 | [diff] [blame] | 136 | void ExpectPresentationSuccess(PresentationConnectionResultPtr result, |
mark a. foltz | 756d07e | 2018-07-20 20:59:18 | [diff] [blame] | 137 | PresentationErrorPtr error) { |
btolsch | 8ca10fb2 | 2018-08-29 01:35:09 | [diff] [blame] | 138 | EXPECT_TRUE(result); |
| 139 | EXPECT_FALSE(error); |
mark a. foltz | d9d2a451 | 2018-04-16 20:42:45 | [diff] [blame] | 140 | presentation_cb_was_run_ = true; |
| 141 | } |
| 142 | |
btolsch | 8ca10fb2 | 2018-08-29 01:35:09 | [diff] [blame] | 143 | void ExpectPresentationError(PresentationConnectionResultPtr result, |
mark a. foltz | 756d07e | 2018-07-20 20:59:18 | [diff] [blame] | 144 | PresentationErrorPtr error) { |
btolsch | 8ca10fb2 | 2018-08-29 01:35:09 | [diff] [blame] | 145 | EXPECT_FALSE(result); |
| 146 | EXPECT_TRUE(error); |
mark a. foltz | d9d2a451 | 2018-04-16 20:42:45 | [diff] [blame] | 147 | presentation_cb_was_run_ = true; |
| 148 | } |
| 149 | |
| 150 | void ExpectPresentationCallbackWasRun() const { |
| 151 | EXPECT_TRUE(presentation_cb_was_run_) |
| 152 | << "ExpectPresentationSuccess or ExpectPresentationError was called"; |
| 153 | } |
| 154 | |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 155 | MockPresentationServiceDelegate mock_delegate_; |
zhaobin | f3704f8 | 2017-01-12 07:19:36 | [diff] [blame] | 156 | MockReceiverPresentationServiceDelegate mock_receiver_delegate_; |
imcheng | 9ce5394b | 2015-05-12 19:27:01 | [diff] [blame] | 157 | |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 158 | std::unique_ptr<PresentationServiceImpl> service_impl_; |
imcheng | 9ce5394b | 2015-05-12 19:27:01 | [diff] [blame] | 159 | |
Derek Cheng | f8ffbec5 | 2017-12-06 19:43:04 | [diff] [blame] | 160 | MockPresentationController mock_controller_; |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 161 | std::optional<mojo::Receiver<PresentationController>> controller_receiver_; |
imcheng | 9ce5394b | 2015-05-12 19:27:01 | [diff] [blame] | 162 | |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 163 | GURL presentation_url1_; |
| 164 | GURL presentation_url2_; |
| 165 | GURL presentation_url3_; |
| 166 | std::vector<GURL> presentation_urls_; |
mark a. foltz | d9d2a451 | 2018-04-16 20:42:45 | [diff] [blame] | 167 | |
| 168 | NewPresentationCallback expect_presentation_success_cb_; |
| 169 | NewPresentationCallback expect_presentation_error_cb_; |
| 170 | bool presentation_cb_was_run_ = false; |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 171 | }; |
| 172 | |
avayvod | 9b5dbea4 | 2015-03-25 18:52:38 | [diff] [blame] | 173 | TEST_F(PresentationServiceImplTest, ListenForScreenAvailability) { |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 174 | ListenForScreenAvailabilityAndWait(presentation_url1_, true); |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 175 | |
Anton Vayvod | e3c3986 | 2017-06-20 20:53:32 | [diff] [blame] | 176 | SimulateScreenAvailabilityChangeAndWait(presentation_url1_, |
| 177 | ScreenAvailability::AVAILABLE); |
| 178 | SimulateScreenAvailabilityChangeAndWait(presentation_url1_, |
| 179 | ScreenAvailability::UNAVAILABLE); |
| 180 | SimulateScreenAvailabilityChangeAndWait(presentation_url1_, |
| 181 | ScreenAvailability::AVAILABLE); |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 182 | } |
| 183 | |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 184 | TEST_F(PresentationServiceImplTest, ScreenAvailabilityNotSupported) { |
| 185 | mock_delegate_.set_screen_availability_listening_supported(false); |
Derek Cheng | f8ffbec5 | 2017-12-06 19:43:04 | [diff] [blame] | 186 | EXPECT_CALL(mock_controller_, |
Anton Vayvod | ddd1c408d | 2017-06-20 20:54:59 | [diff] [blame] | 187 | OnScreenAvailabilityUpdated(presentation_url1_, |
| 188 | ScreenAvailability::DISABLED)); |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 189 | ListenForScreenAvailabilityAndWait(presentation_url1_, false); |
| 190 | base::RunLoop().RunUntilIdle(); |
| 191 | } |
| 192 | |
Derek Cheng | 4de47c5 | 2017-10-10 01:05:54 | [diff] [blame] | 193 | TEST_F(PresentationServiceImplTest, OnDelegateDestroyed) { |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 194 | ListenForScreenAvailabilityAndWait(presentation_url1_, true); |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 195 | |
Derek Cheng | 4de47c5 | 2017-10-10 01:05:54 | [diff] [blame] | 196 | service_impl_->OnDelegateDestroyed(); |
| 197 | |
| 198 | // TearDown() expects |mock_delegate_| to have been notified when |
| 199 | // |service_impl_| is destroyed; this does not apply here since the delegate |
| 200 | // is destroyed first. |
| 201 | service_impl_.reset(); |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | TEST_F(PresentationServiceImplTest, DidNavigateThisFrame) { |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 205 | ListenForScreenAvailabilityAndWait(presentation_url1_, true); |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 206 | |
Derek Cheng | 4de47c5 | 2017-10-10 01:05:54 | [diff] [blame] | 207 | ExpectDelegateReset(); |
jam | 5fdbb58d | 2017-02-06 22:09:59 | [diff] [blame] | 208 | Navigate(true); |
imcheng | 271b9ef | 2015-03-18 19:43:19 | [diff] [blame] | 209 | ExpectCleanState(); |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 210 | } |
| 211 | |
imcheng | 9ce5394b | 2015-05-12 19:27:01 | [diff] [blame] | 212 | TEST_F(PresentationServiceImplTest, DidNavigateOtherFrame) { |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 213 | ListenForScreenAvailabilityAndWait(presentation_url1_, true); |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 214 | |
jam | 5fdbb58d | 2017-02-06 22:09:59 | [diff] [blame] | 215 | Navigate(false); |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 216 | |
| 217 | // Availability is reported and callback is invoked since it was not |
| 218 | // removed. |
Anton Vayvod | e3c3986 | 2017-06-20 20:53:32 | [diff] [blame] | 219 | SimulateScreenAvailabilityChangeAndWait(presentation_url1_, |
| 220 | ScreenAvailability::AVAILABLE); |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 221 | } |
| 222 | |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 223 | TEST_F(PresentationServiceImplTest, DelegateFails) { |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 224 | ListenForScreenAvailabilityAndWait(presentation_url1_, false); |
mfoltz | 3f30502f | 2015-08-11 16:13:38 | [diff] [blame] | 225 | ASSERT_EQ( |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 226 | service_impl_->screen_availability_listeners_.end(), |
| 227 | service_impl_->screen_availability_listeners_.find(presentation_url1_)); |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 228 | } |
| 229 | |
mfoltz | fcad5262 | 2016-09-03 03:08:33 | [diff] [blame] | 230 | TEST_F(PresentationServiceImplTest, SetDefaultPresentationUrls) { |
Derek Cheng | 1071a23 | 2017-07-25 22:23:56 | [diff] [blame] | 231 | EXPECT_CALL(mock_delegate_, SetDefaultPresentationUrls( |
| 232 | PresentationUrlsAre(presentation_urls_), _)) |
imcheng | 271b9ef | 2015-03-18 19:43:19 | [diff] [blame] | 233 | .Times(1); |
imcheng | 271b9ef | 2015-03-18 19:43:19 | [diff] [blame] | 234 | |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 235 | service_impl_->SetDefaultPresentationUrls(presentation_urls_); |
mfoltz | fcad5262 | 2016-09-03 03:08:33 | [diff] [blame] | 236 | |
| 237 | // Sets different DPUs. |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 238 | std::vector<GURL> more_urls = presentation_urls_; |
| 239 | more_urls.push_back(presentation_url3_); |
mfoltz | fcad5262 | 2016-09-03 03:08:33 | [diff] [blame] | 240 | |
mfoltz | 11fc4df8 | 2017-03-20 23:49:18 | [diff] [blame] | 241 | PresentationConnectionCallback callback; |
Derek Cheng | 1071a23 | 2017-07-25 22:23:56 | [diff] [blame] | 242 | EXPECT_CALL(mock_delegate_, |
| 243 | SetDefaultPresentationUrls(PresentationUrlsAre(more_urls), _)) |
| 244 | .WillOnce(SaveArgByMove<1>(&callback)); |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 245 | service_impl_->SetDefaultPresentationUrls(more_urls); |
imcheng | fa43aae0 | 2015-11-11 17:28:44 | [diff] [blame] | 246 | |
mfoltz | 11fc4df8 | 2017-03-20 23:49:18 | [diff] [blame] | 247 | PresentationInfo presentation_info(presentation_url2_, kPresentationId); |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 248 | |
btolsch | 8ca10fb2 | 2018-08-29 01:35:09 | [diff] [blame] | 249 | EXPECT_CALL(mock_controller_, OnDefaultPresentationStarted(_)) |
| 250 | .WillOnce([&presentation_info](PresentationConnectionResultPtr result) { |
| 251 | EXPECT_THAT(*result->presentation_info, InfoEquals(presentation_info)); |
| 252 | }); |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 253 | EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _)); |
btolsch | 8ca10fb2 | 2018-08-29 01:35:09 | [diff] [blame] | 254 | |
Mario Sanchez Prada | 23a91b2 | 2019-08-26 14:45:33 | [diff] [blame] | 255 | mojo::PendingRemote<PresentationConnection> presentation_connection_remote; |
Mario Sanchez Prada | 2311a9e | 2019-08-26 14:29:49 | [diff] [blame] | 256 | mojo::Remote<PresentationConnection> controller_remote; |
Avi Drissman | 5d5d48d6 | 2022-01-07 20:23:58 | [diff] [blame] | 257 | std::ignore = presentation_connection_remote.InitWithNewPipeAndPassReceiver(); |
btolsch | 8ca10fb2 | 2018-08-29 01:35:09 | [diff] [blame] | 258 | std::move(callback).Run(PresentationConnectionResult::New( |
Wei4 Wang | e1c8c00 | 2023-04-24 23:30:25 | [diff] [blame] | 259 | PresentationInfo::New(presentation_url2_, kPresentationId), |
Mario Sanchez Prada | 23a91b2 | 2019-08-26 14:45:33 | [diff] [blame] | 260 | std::move(presentation_connection_remote), |
Mario Sanchez Prada | 2311a9e | 2019-08-26 14:29:49 | [diff] [blame] | 261 | controller_remote.BindNewPipeAndPassReceiver())); |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 262 | base::RunLoop().RunUntilIdle(); |
imcheng | 271b9ef | 2015-03-18 19:43:19 | [diff] [blame] | 263 | } |
| 264 | |
imcheng | 22578da | 2017-07-11 23:09:11 | [diff] [blame] | 265 | TEST_F(PresentationServiceImplTest, |
| 266 | SetDefaultPresentationUrlsNoopsOnNonMainFrame) { |
| 267 | RenderFrameHost* rfh = main_rfh(); |
| 268 | RenderFrameHostTester* rfh_tester = RenderFrameHostTester::For(rfh); |
| 269 | rfh = rfh_tester->AppendChild("subframe"); |
| 270 | |
| 271 | EXPECT_CALL(mock_delegate_, RemoveObserver(_, _)).Times(1); |
| 272 | EXPECT_CALL(mock_delegate_, AddObserver(_, _, _)).Times(1); |
| 273 | service_impl_.reset( |
| 274 | new PresentationServiceImpl(rfh, contents(), &mock_delegate_, nullptr)); |
| 275 | |
Derek Cheng | 1071a23 | 2017-07-25 22:23:56 | [diff] [blame] | 276 | EXPECT_CALL(mock_delegate_, SetDefaultPresentationUrls(_, _)).Times(0); |
imcheng | 22578da | 2017-07-11 23:09:11 | [diff] [blame] | 277 | service_impl_->SetDefaultPresentationUrls(presentation_urls_); |
| 278 | } |
| 279 | |
zhaobin | 42407c7 | 2017-02-07 02:04:16 | [diff] [blame] | 280 | TEST_F(PresentationServiceImplTest, ListenForConnectionStateChange) { |
mfoltz | 11fc4df8 | 2017-03-20 23:49:18 | [diff] [blame] | 281 | PresentationInfo connection(presentation_url1_, kPresentationId); |
mfoltz | 0dc2a6b | 2017-03-07 18:24:19 | [diff] [blame] | 282 | PresentationConnectionStateChangedCallback state_changed_cb; |
Derek Cheng | f8ffbec5 | 2017-12-06 19:43:04 | [diff] [blame] | 283 | // Trigger state change. It should be propagated back up to |
| 284 | // |mock_controller_|. |
mfoltz | 11fc4df8 | 2017-03-20 23:49:18 | [diff] [blame] | 285 | PresentationInfo presentation_connection(presentation_url1_, kPresentationId); |
zhaobin | badd595 | 2016-11-09 18:25:50 | [diff] [blame] | 286 | |
| 287 | EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _)) |
| 288 | .WillOnce(SaveArg<3>(&state_changed_cb)); |
imcheng | 1ebdd16 | 2017-01-26 22:01:52 | [diff] [blame] | 289 | service_impl_->ListenForConnectionStateChange(connection); |
zhaobin | badd595 | 2016-11-09 18:25:50 | [diff] [blame] | 290 | |
mark a. foltz | 756d07e | 2018-07-20 20:59:18 | [diff] [blame] | 291 | EXPECT_CALL(mock_controller_, OnConnectionStateChanged( |
| 292 | InfoPtrEquals(presentation_connection), |
mark a. foltz | e8ad320 | 2018-04-23 22:50:23 | [diff] [blame] | 293 | PresentationConnectionState::TERMINATED)); |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 294 | state_changed_cb.Run(PresentationConnectionStateChangeInfo( |
mark a. foltz | e8ad320 | 2018-04-23 22:50:23 | [diff] [blame] | 295 | PresentationConnectionState::TERMINATED)); |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 296 | base::RunLoop().RunUntilIdle(); |
imcheng | ed10e96 | 2016-02-23 06:34:44 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | TEST_F(PresentationServiceImplTest, ListenForConnectionClose) { |
mfoltz | 11fc4df8 | 2017-03-20 23:49:18 | [diff] [blame] | 300 | PresentationInfo connection(presentation_url1_, kPresentationId); |
mfoltz | 0dc2a6b | 2017-03-07 18:24:19 | [diff] [blame] | 301 | PresentationConnectionStateChangedCallback state_changed_cb; |
imcheng | ed10e96 | 2016-02-23 06:34:44 | [diff] [blame] | 302 | EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _)) |
| 303 | .WillOnce(SaveArg<3>(&state_changed_cb)); |
imcheng | 1ebdd16 | 2017-01-26 22:01:52 | [diff] [blame] | 304 | service_impl_->ListenForConnectionStateChange(connection); |
imcheng | ed10e96 | 2016-02-23 06:34:44 | [diff] [blame] | 305 | |
| 306 | // Trigger connection close. It should be propagated back up to |
Derek Cheng | f8ffbec5 | 2017-12-06 19:43:04 | [diff] [blame] | 307 | // |mock_controller_|. |
mfoltz | 11fc4df8 | 2017-03-20 23:49:18 | [diff] [blame] | 308 | PresentationInfo presentation_connection(presentation_url1_, kPresentationId); |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 309 | PresentationConnectionStateChangeInfo closed_info( |
mark a. foltz | e8ad320 | 2018-04-23 22:50:23 | [diff] [blame] | 310 | PresentationConnectionState::CLOSED); |
mark a. foltz | d9d2a451 | 2018-04-16 20:42:45 | [diff] [blame] | 311 | closed_info.close_reason = PresentationConnectionCloseReason::WENT_AWAY; |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 312 | closed_info.message = "Foo"; |
imcheng | ed10e96 | 2016-02-23 06:34:44 | [diff] [blame] | 313 | |
mark a. foltz | 756d07e | 2018-07-20 20:59:18 | [diff] [blame] | 314 | EXPECT_CALL( |
| 315 | mock_controller_, |
| 316 | OnConnectionClosed(InfoPtrEquals(presentation_connection), |
| 317 | PresentationConnectionCloseReason::WENT_AWAY, "Foo")); |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 318 | state_changed_cb.Run(closed_info); |
| 319 | base::RunLoop().RunUntilIdle(); |
imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 320 | } |
| 321 | |
mfoltz | fcad5262 | 2016-09-03 03:08:33 | [diff] [blame] | 322 | TEST_F(PresentationServiceImplTest, SetSameDefaultPresentationUrls) { |
Derek Cheng | 1071a23 | 2017-07-25 22:23:56 | [diff] [blame] | 323 | EXPECT_CALL(mock_delegate_, SetDefaultPresentationUrls(_, _)).Times(1); |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 324 | service_impl_->SetDefaultPresentationUrls(presentation_urls_); |
imcheng | 271b9ef | 2015-03-18 19:43:19 | [diff] [blame] | 325 | EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); |
mfoltz | fcad5262 | 2016-09-03 03:08:33 | [diff] [blame] | 326 | |
| 327 | // Same URLs as before; no-ops. |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 328 | service_impl_->SetDefaultPresentationUrls(presentation_urls_); |
mfoltz | fcad5262 | 2016-09-03 03:08:33 | [diff] [blame] | 329 | EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_delegate_)); |
imcheng | 271b9ef | 2015-03-18 19:43:19 | [diff] [blame] | 330 | } |
| 331 | |
mfoltz | 11fc4df8 | 2017-03-20 23:49:18 | [diff] [blame] | 332 | TEST_F(PresentationServiceImplTest, StartPresentationSuccess) { |
btolsch | 8ca10fb2 | 2018-08-29 01:35:09 | [diff] [blame] | 333 | PresentationConnectionCallback saved_success_cb; |
mark a. foltz | 756d07e | 2018-07-20 20:59:18 | [diff] [blame] | 334 | EXPECT_CALL(mock_delegate_, StartPresentation(_, _, _)) |
btolsch | 8ca10fb2 | 2018-08-29 01:35:09 | [diff] [blame] | 335 | .WillOnce([&saved_success_cb](const auto& request, auto success_cb, |
| 336 | auto error_cb) { |
mark a. foltz | 756d07e | 2018-07-20 20:59:18 | [diff] [blame] | 337 | saved_success_cb = std::move(success_cb); |
| 338 | }); |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 339 | service_impl_->StartPresentation(presentation_urls_, |
mark a. foltz | d9d2a451 | 2018-04-16 20:42:45 | [diff] [blame] | 340 | std::move(expect_presentation_success_cb_)); |
mark a. foltz | 756d07e | 2018-07-20 20:59:18 | [diff] [blame] | 341 | EXPECT_FALSE(saved_success_cb.is_null()); |
imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 342 | EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _)) |
| 343 | .Times(1); |
mark a. foltz | 756d07e | 2018-07-20 20:59:18 | [diff] [blame] | 344 | std::move(saved_success_cb) |
btolsch | 8ca10fb2 | 2018-08-29 01:35:09 | [diff] [blame] | 345 | .Run(PresentationConnectionResult::New( |
Wei4 Wang | e1c8c00 | 2023-04-24 23:30:25 | [diff] [blame] | 346 | PresentationInfo::New(presentation_url1_, kPresentationId), |
Mario Sanchez Prada | 2311a9e | 2019-08-26 14:29:49 | [diff] [blame] | 347 | mojo::NullRemote(), mojo::NullReceiver())); |
mark a. foltz | d9d2a451 | 2018-04-16 20:42:45 | [diff] [blame] | 348 | ExpectPresentationCallbackWasRun(); |
imcheng | 271b9ef | 2015-03-18 19:43:19 | [diff] [blame] | 349 | } |
| 350 | |
mfoltz | 11fc4df8 | 2017-03-20 23:49:18 | [diff] [blame] | 351 | TEST_F(PresentationServiceImplTest, StartPresentationError) { |
mark a. foltz | 756d07e | 2018-07-20 20:59:18 | [diff] [blame] | 352 | base::OnceCallback<void(const PresentationError&)> saved_error_cb; |
| 353 | EXPECT_CALL(mock_delegate_, StartPresentation(_, _, _)) |
| 354 | .WillOnce([&](const auto& request, auto success_cb, auto error_cb) { |
| 355 | saved_error_cb = std::move(error_cb); |
| 356 | }); |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 357 | service_impl_->StartPresentation(presentation_urls_, |
mark a. foltz | d9d2a451 | 2018-04-16 20:42:45 | [diff] [blame] | 358 | std::move(expect_presentation_error_cb_)); |
mark a. foltz | 756d07e | 2018-07-20 20:59:18 | [diff] [blame] | 359 | EXPECT_FALSE(saved_error_cb.is_null()); |
| 360 | std::move(saved_error_cb) |
| 361 | .Run(PresentationError(PresentationErrorType::UNKNOWN, "Error message")); |
mark a. foltz | d9d2a451 | 2018-04-16 20:42:45 | [diff] [blame] | 362 | ExpectPresentationCallbackWasRun(); |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | TEST_F(PresentationServiceImplTest, StartPresentationInProgress) { |
mark a. foltz | 756d07e | 2018-07-20 20:59:18 | [diff] [blame] | 366 | EXPECT_CALL(mock_delegate_, StartPresentation(_, _, _)).Times(1); |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 367 | // Uninvoked callbacks must outlive |service_impl_| since they get invoked |
| 368 | // at |service_impl_|'s destruction. |
Peter Kasting | 341e1fb | 2018-02-24 00:03:01 | [diff] [blame] | 369 | service_impl_->StartPresentation(presentation_urls_, base::DoNothing()); |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 370 | |
| 371 | // This request should fail immediately, since there is already a |
| 372 | // StartPresentation in progress. |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 373 | service_impl_->StartPresentation(presentation_urls_, |
mark a. foltz | d9d2a451 | 2018-04-16 20:42:45 | [diff] [blame] | 374 | std::move(expect_presentation_error_cb_)); |
| 375 | ExpectPresentationCallbackWasRun(); |
imcheng | 271b9ef | 2015-03-18 19:43:19 | [diff] [blame] | 376 | } |
| 377 | |
mfoltz | 11fc4df8 | 2017-03-20 23:49:18 | [diff] [blame] | 378 | TEST_F(PresentationServiceImplTest, ReconnectPresentationSuccess) { |
btolsch | 8ca10fb2 | 2018-08-29 01:35:09 | [diff] [blame] | 379 | PresentationConnectionCallback saved_success_cb; |
mark a. foltz | 756d07e | 2018-07-20 20:59:18 | [diff] [blame] | 380 | EXPECT_CALL(mock_delegate_, ReconnectPresentation(_, kPresentationId, _, _)) |
btolsch | 8ca10fb2 | 2018-08-29 01:35:09 | [diff] [blame] | 381 | .WillOnce([&saved_success_cb](const auto& request, const auto& id, |
| 382 | auto success_cb, auto error_cb) { |
| 383 | saved_success_cb = std::move(success_cb); |
| 384 | }); |
mark a. foltz | d9d2a451 | 2018-04-16 20:42:45 | [diff] [blame] | 385 | service_impl_->ReconnectPresentation( |
| 386 | presentation_urls_, kPresentationId, |
| 387 | std::move(expect_presentation_success_cb_)); |
mark a. foltz | 756d07e | 2018-07-20 20:59:18 | [diff] [blame] | 388 | EXPECT_FALSE(saved_success_cb.is_null()); |
imcheng | f3e5a01 | 2015-11-20 04:08:37 | [diff] [blame] | 389 | EXPECT_CALL(mock_delegate_, ListenForConnectionStateChange(_, _, _, _)) |
| 390 | .Times(1); |
mark a. foltz | 756d07e | 2018-07-20 20:59:18 | [diff] [blame] | 391 | std::move(saved_success_cb) |
btolsch | 8ca10fb2 | 2018-08-29 01:35:09 | [diff] [blame] | 392 | .Run(PresentationConnectionResult::New( |
Wei4 Wang | e1c8c00 | 2023-04-24 23:30:25 | [diff] [blame] | 393 | PresentationInfo::New(presentation_url1_, kPresentationId), |
Mario Sanchez Prada | 2311a9e | 2019-08-26 14:29:49 | [diff] [blame] | 394 | mojo::NullRemote(), mojo::NullReceiver())); |
mark a. foltz | d9d2a451 | 2018-04-16 20:42:45 | [diff] [blame] | 395 | ExpectPresentationCallbackWasRun(); |
imcheng | 271b9ef | 2015-03-18 19:43:19 | [diff] [blame] | 396 | } |
| 397 | |
mfoltz | 11fc4df8 | 2017-03-20 23:49:18 | [diff] [blame] | 398 | TEST_F(PresentationServiceImplTest, ReconnectPresentationError) { |
mark a. foltz | 756d07e | 2018-07-20 20:59:18 | [diff] [blame] | 399 | base::OnceCallback<void(const PresentationError&)> saved_error_cb; |
| 400 | EXPECT_CALL(mock_delegate_, ReconnectPresentation(_, kPresentationId, _, _)) |
| 401 | .WillOnce([&](const auto& request, const std::string& id, auto success_cb, |
| 402 | auto error_cb) { saved_error_cb = std::move(error_cb); }); |
mark a. foltz | d9d2a451 | 2018-04-16 20:42:45 | [diff] [blame] | 403 | service_impl_->ReconnectPresentation( |
| 404 | presentation_urls_, kPresentationId, |
| 405 | std::move(expect_presentation_error_cb_)); |
mark a. foltz | 756d07e | 2018-07-20 20:59:18 | [diff] [blame] | 406 | EXPECT_FALSE(saved_error_cb.is_null()); |
| 407 | std::move(saved_error_cb) |
| 408 | .Run(PresentationError(PresentationErrorType::UNKNOWN, "Error message")); |
mark a. foltz | d9d2a451 | 2018-04-16 20:42:45 | [diff] [blame] | 409 | ExpectPresentationCallbackWasRun(); |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | TEST_F(PresentationServiceImplTest, MaxPendingReconnectPresentationRequests) { |
Peter Kasting | 4524c839 | 2023-10-17 15:55:08 | [diff] [blame] | 413 | static constexpr char kPresentationUrlTemplate[] = "http://fooUrl%d"; |
| 414 | static constexpr char kPresentationIdTemplate[] = "presentationId%d"; |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 415 | int num_requests = PresentationServiceImpl::kMaxQueuedRequests; |
| 416 | int i = 0; |
mark a. foltz | 756d07e | 2018-07-20 20:59:18 | [diff] [blame] | 417 | EXPECT_CALL(mock_delegate_, ReconnectPresentation(_, _, _, _)) |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 418 | .Times(num_requests); |
| 419 | for (; i < num_requests; ++i) { |
Peter Kasting | 4524c839 | 2023-10-17 15:55:08 | [diff] [blame] | 420 | std::vector<GURL> urls = { |
| 421 | GURL(base::StringPrintf(kPresentationUrlTemplate, i))}; |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 422 | // Uninvoked callbacks must outlive |service_impl_| since they get invoked |
| 423 | // at |service_impl_|'s destruction. |
Peter Kasting | 341e1fb | 2018-02-24 00:03:01 | [diff] [blame] | 424 | service_impl_->ReconnectPresentation( |
Peter Kasting | 4524c839 | 2023-10-17 15:55:08 | [diff] [blame] | 425 | urls, base::StringPrintf(kPresentationIdTemplate, i), |
| 426 | base::DoNothing()); |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 427 | } |
| 428 | |
Peter Kasting | 4524c839 | 2023-10-17 15:55:08 | [diff] [blame] | 429 | std::vector<GURL> urls = { |
| 430 | GURL(base::StringPrintf(kPresentationUrlTemplate, i))}; |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 431 | // Exceeded maximum queue size, should invoke mojo callback with error. |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 432 | service_impl_->ReconnectPresentation( |
Peter Kasting | 4524c839 | 2023-10-17 15:55:08 | [diff] [blame] | 433 | urls, base::StringPrintf(kPresentationIdTemplate, i), |
mark a. foltz | d9d2a451 | 2018-04-16 20:42:45 | [diff] [blame] | 434 | std::move(expect_presentation_error_cb_)); |
| 435 | ExpectPresentationCallbackWasRun(); |
imcheng | 271b9ef | 2015-03-18 19:43:19 | [diff] [blame] | 436 | } |
| 437 | |
mfoltz | d0c3de1 | 2015-12-12 04:43:08 | [diff] [blame] | 438 | TEST_F(PresentationServiceImplTest, CloseConnection) { |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 439 | EXPECT_CALL(mock_delegate_, CloseConnection(_, _, Eq(kPresentationId))); |
| 440 | service_impl_->CloseConnection(presentation_url1_, kPresentationId); |
haibinlu | 4ba4019 | 2015-06-23 21:43:09 | [diff] [blame] | 441 | } |
| 442 | |
mfoltz | d0c3de1 | 2015-12-12 04:43:08 | [diff] [blame] | 443 | TEST_F(PresentationServiceImplTest, Terminate) { |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 444 | EXPECT_CALL(mock_delegate_, Terminate(_, _, Eq(kPresentationId))); |
| 445 | service_impl_->Terminate(presentation_url1_, kPresentationId); |
mlamouri | c65a70d7 | 2015-11-27 16:28:59 | [diff] [blame] | 446 | } |
| 447 | |
zhaobin | f3704f8 | 2017-01-12 07:19:36 | [diff] [blame] | 448 | TEST_F(PresentationServiceImplTest, ReceiverPresentationServiceDelegate) { |
zhaobin | dca98f63 | 2017-06-29 18:42:28 | [diff] [blame] | 449 | EXPECT_CALL(mock_receiver_delegate_, AddObserver(_, _, _)).Times(1); |
zhaobin | f3704f8 | 2017-01-12 07:19:36 | [diff] [blame] | 450 | |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 451 | PresentationServiceImpl service_impl(main_rfh(), contents(), nullptr, |
zhaobin | dca98f63 | 2017-06-29 18:42:28 | [diff] [blame] | 452 | &mock_receiver_delegate_); |
zhaobin | f3704f8 | 2017-01-12 07:19:36 | [diff] [blame] | 453 | |
| 454 | ReceiverConnectionAvailableCallback callback; |
zhaobin | dca98f63 | 2017-06-29 18:42:28 | [diff] [blame] | 455 | EXPECT_CALL(mock_receiver_delegate_, |
zhaobin | f3704f8 | 2017-01-12 07:19:36 | [diff] [blame] | 456 | RegisterReceiverConnectionAvailableCallback(_)) |
| 457 | .WillOnce(SaveArg<0>(&callback)); |
| 458 | |
Derek Cheng | 4de47c5 | 2017-10-10 01:05:54 | [diff] [blame] | 459 | MockPresentationReceiver mock_receiver; |
Mario Sanchez Prada | 23a91b2 | 2019-08-26 14:45:33 | [diff] [blame] | 460 | mojo::Receiver<blink::mojom::PresentationReceiver> |
| 461 | presentation_receiver_receiver(&mock_receiver); |
| 462 | service_impl.SetReceiver( |
| 463 | presentation_receiver_receiver.BindNewPipeAndPassRemote()); |
zhaobin | f3704f8 | 2017-01-12 07:19:36 | [diff] [blame] | 464 | EXPECT_FALSE(callback.is_null()); |
| 465 | |
mark a. foltz | e2084383c | 2018-05-11 19:21:50 | [diff] [blame] | 466 | PresentationInfo expected(presentation_url1_, kPresentationId); |
zhaobin | f3704f8 | 2017-01-12 07:19:36 | [diff] [blame] | 467 | |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 468 | // Client gets notified of receiver connections. |
Mario Sanchez Prada | 2311a9e | 2019-08-26 14:29:49 | [diff] [blame] | 469 | mojo::PendingRemote<PresentationConnection> controller_connection; |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 470 | MockPresentationConnection mock_presentation_connection; |
Mario Sanchez Prada | 2311a9e | 2019-08-26 14:29:49 | [diff] [blame] | 471 | mojo::Receiver<PresentationConnection> connection_binding( |
| 472 | &mock_presentation_connection, |
| 473 | controller_connection.InitWithNewPipeAndPassReceiver()); |
| 474 | mojo::Remote<PresentationConnection> receiver_connection; |
Derek Cheng | 4de47c5 | 2017-10-10 01:05:54 | [diff] [blame] | 475 | |
Wei4 Wang | e1c8c00 | 2023-04-24 23:30:25 | [diff] [blame] | 476 | EXPECT_CALL(mock_receiver, OnReceiverConnectionAvailable(_)).Times(1); |
| 477 | callback.Run(PresentationConnectionResult::New( |
| 478 | PresentationInfo::New(expected), std::move(controller_connection), |
| 479 | receiver_connection.BindNewPipeAndPassReceiver())); |
imcheng | fafb67d0 | 2017-06-15 17:30:14 | [diff] [blame] | 480 | base::RunLoop().RunUntilIdle(); |
| 481 | |
zhaobin | dca98f63 | 2017-06-29 18:42:28 | [diff] [blame] | 482 | EXPECT_CALL(mock_receiver_delegate_, RemoveObserver(_, _)).Times(1); |
| 483 | } |
| 484 | |
| 485 | TEST_F(PresentationServiceImplTest, ReceiverDelegateOnSubFrame) { |
| 486 | EXPECT_CALL(mock_receiver_delegate_, AddObserver(_, _, _)).Times(1); |
| 487 | |
| 488 | PresentationServiceImpl service_impl(main_rfh(), contents(), nullptr, |
| 489 | &mock_receiver_delegate_); |
Claudio DeSouza | ee45cae | 2021-11-24 20:30:27 | [diff] [blame] | 490 | service_impl.is_outermost_document_ = false; |
zhaobin | dca98f63 | 2017-06-29 18:42:28 | [diff] [blame] | 491 | |
| 492 | ReceiverConnectionAvailableCallback callback; |
| 493 | EXPECT_CALL(mock_receiver_delegate_, |
| 494 | RegisterReceiverConnectionAvailableCallback(_)) |
| 495 | .Times(0); |
| 496 | |
Mario Sanchez Prada | 2ae29e5a | 2019-08-26 14:42:23 | [diff] [blame] | 497 | mojo::PendingRemote<PresentationController> presentation_controller_remote; |
| 498 | controller_receiver_.emplace( |
| 499 | &mock_controller_, |
| 500 | presentation_controller_remote.InitWithNewPipeAndPassReceiver()); |
zhaobin | dca98f63 | 2017-06-29 18:42:28 | [diff] [blame] | 501 | service_impl.controller_delegate_ = nullptr; |
Mario Sanchez Prada | 2ae29e5a | 2019-08-26 14:42:23 | [diff] [blame] | 502 | service_impl.SetController(std::move(presentation_controller_remote)); |
zhaobin | dca98f63 | 2017-06-29 18:42:28 | [diff] [blame] | 503 | |
| 504 | EXPECT_CALL(mock_receiver_delegate_, Reset(_, _)).Times(0); |
| 505 | service_impl.Reset(); |
| 506 | |
| 507 | EXPECT_CALL(mock_receiver_delegate_, RemoveObserver(_, _)).Times(1); |
mlamouri | 0c8ce52 | 2015-10-02 21:47:38 | [diff] [blame] | 508 | } |
| 509 | |
imcheng | 9f515d0 | 2015-02-10 20:09:24 | [diff] [blame] | 510 | } // namespace content |