| Index: content/renderer/presentation/presentation_dispatcher_unittest.cc
|
| diff --git a/content/renderer/presentation/presentation_dispatcher_unittest.cc b/content/renderer/presentation/presentation_dispatcher_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..45cc10ef142210de55d95bc922e35e8bb96f6d34
|
| --- /dev/null
|
| +++ b/content/renderer/presentation/presentation_dispatcher_unittest.cc
|
| @@ -0,0 +1,361 @@
|
| +// Copyright 2014 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 "base/macros.h"
|
| +#include "base/run_loop.h"
|
| +#include "content/renderer/presentation/presentation_dispatcher.h"
|
| +#include "mojo/public/cpp/bindings/interface_ptr.h"
|
| +#include "testing/gmock/include/gmock/gmock.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +#include "third_party/WebKit/public/platform/modules/presentation/WebPresentationAvailabilityObserver.h"
|
| +#include "third_party/WebKit/public/platform/modules/presentation/WebPresentationClient.h"
|
| +#include "third_party/WebKit/public/platform/modules/presentation/WebPresentationError.h"
|
| +#include "third_party/WebKit/public/platform/modules/presentation/presentation.mojom.h"
|
| +
|
| +using ::testing::_;
|
| +
|
| +namespace content {
|
| +
|
| +class MockPresentationService : public blink::mojom::PresentationService {
|
| + public:
|
| + void SetClient(blink::mojom::PresentationServiceClientPtr client) override {
|
| + SetClient(*client);
|
| + }
|
| + MOCK_METHOD1(SetClient, void(const blink::mojom::PresentationServiceClient&));
|
| + MOCK_METHOD1(SetDefaultPresentationUrls, void(const std::vector<GURL>&));
|
| + MOCK_METHOD1(ListenForScreenAvailability, void(const GURL&));
|
| + MOCK_METHOD1(StopListeningForScreenAvailability, void(const GURL&));
|
| + MOCK_METHOD2(StartSession,
|
| + void(const std::vector<GURL>&, const StartSessionCallback&));
|
| + MOCK_METHOD3(JoinSession,
|
| + void(const std::vector<GURL>&,
|
| + const base::Optional<std::string>&,
|
| + const JoinSessionCallback&));
|
| + void SendConnectionMessage(
|
| + blink::mojom::PresentationSessionInfoPtr sessionInfo,
|
| + blink::mojom::ConnectionMessagePtr message_request,
|
| + const SendConnectionMessageCallback& callback) override {
|
| + SendConnectionMessage(*sessionInfo, *message_request, callback);
|
| + }
|
| + MOCK_METHOD3(SendConnectionMessage,
|
| + void(blink::mojom::PresentationSessionInfo,
|
| + blink::mojom::ConnectionMessage,
|
| + const SendConnectionMessageCallback&));
|
| + MOCK_METHOD2(CloseConnection, void(const GURL&, const std::string&));
|
| + MOCK_METHOD2(Terminate, void(const GURL&, const std::string&));
|
| + void ListenForConnectionMessages(
|
| + blink::mojom::PresentationSessionInfoPtr sessionInfo) override {
|
| + ListenForConnectionMessages(*sessionInfo);
|
| + }
|
| + MOCK_METHOD1(ListenForConnectionMessages,
|
| + void(const blink::mojom::PresentationSessionInfo&));
|
| +};
|
| +
|
| +class MockPresentationAvailabilityCallbacks final
|
| + : public blink::WebCallbacks<bool, const blink::WebPresentationError&> {
|
| + public:
|
| + MOCK_METHOD1(onSuccess, void(bool value));
|
| + MOCK_METHOD1(onError, void(const blink::WebPresentationError&));
|
| +};
|
| +
|
| +class MockPresentationAvailabilityObserver
|
| + : public blink::WebPresentationAvailabilityObserver {
|
| + public:
|
| + MockPresentationAvailabilityObserver(
|
| + const blink::WebVector<blink::WebURL>& urls)
|
| + : urls_(urls) {}
|
| +
|
| + MOCK_METHOD1(availabilityChanged, void(bool));
|
| +
|
| + const blink::WebVector<blink::WebURL>& urls() const override { return urls_; }
|
| +
|
| + blink::WebVector<blink::WebURL> urls_;
|
| +};
|
| +
|
| +class PresentationDispatcherTest : public testing::Test {
|
| + public:
|
| + enum class URLState { Available, Unavailable, Unsupported };
|
| +
|
| + PresentationDispatcherTest() {
|
| + urls_ = {GURL("http://example0.com/"), GURL("http://example1.com/"),
|
| + GURL("http://example2.com/"), GURL("http://example3.com/")};
|
| +
|
| + blink::WebVector<blink::WebURL> urls_1(static_cast<size_t>(3));
|
| + urls_1[0] = urls_[0];
|
| + urls_1[1] = urls_[1];
|
| + urls_1[2] = urls_[2];
|
| +
|
| + blink::WebVector<blink::WebURL> urls_2(static_cast<size_t>(3));
|
| + urls_2[0] = urls_[1];
|
| + urls_2[1] = urls_[2];
|
| + urls_2[2] = urls_[3];
|
| +
|
| + blink::WebVector<blink::WebURL> urls_3(static_cast<size_t>(2));
|
| + urls_3[0] = urls_[1];
|
| + urls_3[1] = urls_[3];
|
| +
|
| + urls_set_ = {urls_1, urls_2, urls_3};
|
| + }
|
| +
|
| + ~PresentationDispatcherTest() override {}
|
| +
|
| + void SetUp() override {
|
| + client_ = base::MakeUnique<PresentationDispatcher>(nullptr);
|
| +
|
| + blink::mojom::PresentationServicePtr service_ptr;
|
| + service_binding_ = new mojo::Binding<blink::mojom::PresentationService>(
|
| + &mock_presentation_service_, mojo::GetProxy(&service_ptr));
|
| + client_->SetPresentationServiceForTest(std::move(service_ptr));
|
| +
|
| + for (const auto& urls : urls_set_) {
|
| + mock_observers_.push_back(
|
| + base::MakeUnique<MockPresentationAvailabilityObserver>(urls));
|
| + }
|
| + }
|
| +
|
| + void ChangeURLState(const GURL& url, URLState state) {
|
| + switch (state) {
|
| + case URLState::Available:
|
| + client_->OnScreenAvailabilityUpdated(url, true);
|
| + break;
|
| + case URLState::Unavailable:
|
| + client_->OnScreenAvailabilityUpdated(url, false);
|
| + break;
|
| + case URLState::Unsupported:
|
| + client_->OnScreenAvailabilityNotSupported(url);
|
| + break;
|
| + }
|
| + }
|
| +
|
| + void StartListening(MockPresentationAvailabilityObserver* observer) {
|
| + client_->startListening(observer);
|
| + }
|
| +
|
| + void StopListening(MockPresentationAvailabilityObserver* observer) {
|
| + client_->stopListening(observer);
|
| + }
|
| +
|
| + void TestGetAvailabilityKUrls(
|
| + size_t k,
|
| + std::vector<URLState> states,
|
| + MockPresentationAvailabilityCallbacks* mock_callback) {
|
| + blink::WebVector<blink::WebURL> urls(k);
|
| +
|
| + for (size_t i = 0; i < k; i++) {
|
| + GURL url("http://example" + std::to_string(i) + ".com/");
|
| + urls[i] = url;
|
| + EXPECT_CALL(mock_presentation_service_, ListenForScreenAvailability(url))
|
| + .Times(1);
|
| + if (states.size() == k) {
|
| + EXPECT_CALL(mock_presentation_service_,
|
| + StopListeningForScreenAvailability(url))
|
| + .Times(1);
|
| + }
|
| + }
|
| +
|
| + base::RunLoop run_loop;
|
| +
|
| + client()->getAvailability(urls, base::WrapUnique(mock_callback));
|
| + for (size_t i = 0; i < std::min(k, states.size()); i++)
|
| + ChangeURLState(urls[i], states[i]);
|
| +
|
| + run_loop.RunUntilIdle();
|
| + }
|
| +
|
| + void StartListeningToMultipleRequests() {
|
| + for (const auto& url : urls_) {
|
| + EXPECT_CALL(mock_presentation_service_, ListenForScreenAvailability(url))
|
| + .Times(1);
|
| + EXPECT_CALL(mock_presentation_service_,
|
| + StopListeningForScreenAvailability(url))
|
| + .Times(1);
|
| + }
|
| +
|
| + base::RunLoop run_loop;
|
| +
|
| + for (const auto& urls : urls_set_) {
|
| + auto* mock_callback = new MockPresentationAvailabilityCallbacks();
|
| + EXPECT_CALL(*mock_callback, onSuccess(false)).Times(1);
|
| + client()->getAvailability(urls, base::WrapUnique(mock_callback));
|
| + }
|
| +
|
| + // Clean up callbacks.
|
| + for (const auto& url : urls_)
|
| + ChangeURLState(url, URLState::Unavailable);
|
| +
|
| + run_loop.RunUntilIdle();
|
| +
|
| + for (const auto& url : urls_) {
|
| + EXPECT_CALL(mock_presentation_service_, ListenForScreenAvailability(url))
|
| + .Times(1);
|
| + }
|
| +
|
| + base::RunLoop run_loop_2;
|
| + for (const auto& mock_observer : mock_observers_)
|
| + StartListening(mock_observer.get());
|
| + run_loop_2.RunUntilIdle();
|
| + }
|
| +
|
| + blink::WebPresentationClient* client() { return client_.get(); }
|
| +
|
| + std::unique_ptr<PresentationDispatcher> client_;
|
| + MockPresentationService mock_presentation_service_;
|
| + mojo::Binding<blink::mojom::PresentationService>* service_binding_;
|
| + base::MessageLoop message_loop_;
|
| +
|
| + std::vector<GURL> urls_;
|
| + std::vector<blink::WebVector<blink::WebURL>> urls_set_;
|
| + std::vector<std::unique_ptr<MockPresentationAvailabilityObserver>>
|
| + mock_observers_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(PresentationDispatcherTest);
|
| +};
|
| +
|
| +TEST_F(PresentationDispatcherTest, GetAvailabilityOneUrlNoAvailabilityChange) {
|
| + auto* mock_callback = new MockPresentationAvailabilityCallbacks();
|
| + std::vector<URLState> states;
|
| + TestGetAvailabilityKUrls(1, states, mock_callback);
|
| +}
|
| +
|
| +TEST_F(PresentationDispatcherTest, GetAvailabilityOneUrlBecomesAvailable) {
|
| + auto* mock_callback = new MockPresentationAvailabilityCallbacks();
|
| + EXPECT_CALL(*mock_callback, onSuccess(true));
|
| + std::vector<URLState> states = {URLState::Available};
|
| + TestGetAvailabilityKUrls(1, states, mock_callback);
|
| +}
|
| +
|
| +TEST_F(PresentationDispatcherTest, GetAvailabilityOneUrlBecomesUnavailable) {
|
| + auto* mock_callback = new MockPresentationAvailabilityCallbacks();
|
| + EXPECT_CALL(*mock_callback, onSuccess(false));
|
| +
|
| + std::vector<URLState> states = {URLState::Unavailable};
|
| + TestGetAvailabilityKUrls(1, states, mock_callback);
|
| +}
|
| +
|
| +TEST_F(PresentationDispatcherTest, GetAvailabilityOneUrlBecomesNotSupported) {
|
| + auto* mock_callback = new MockPresentationAvailabilityCallbacks();
|
| + EXPECT_CALL(*mock_callback, onError(_));
|
| +
|
| + std::vector<URLState> states = {URLState::Unsupported};
|
| + TestGetAvailabilityKUrls(1, states, mock_callback);
|
| +}
|
| +
|
| +TEST_F(PresentationDispatcherTest,
|
| + GetAvailabilityMultipleUrlsAllBecomesAvailable) {
|
| + auto* mock_callback = new MockPresentationAvailabilityCallbacks();
|
| + EXPECT_CALL(*mock_callback, onSuccess(true)).Times(1);
|
| +
|
| + std::vector<URLState> state_seq = {URLState::Available, URLState::Available};
|
| + TestGetAvailabilityKUrls(2, state_seq, mock_callback);
|
| +}
|
| +
|
| +TEST_F(PresentationDispatcherTest,
|
| + GetAvailabilityMultipleUrlsAllBecomesUnavailable) {
|
| + auto* mock_callback = new MockPresentationAvailabilityCallbacks();
|
| + EXPECT_CALL(*mock_callback, onSuccess(false)).Times(1);
|
| +
|
| + std::vector<URLState> state_seq = {URLState::Unavailable,
|
| + URLState::Unavailable};
|
| + TestGetAvailabilityKUrls(2, state_seq, mock_callback);
|
| +}
|
| +
|
| +TEST_F(PresentationDispatcherTest,
|
| + GetAvailabilityMultipleUrlsAllBecomesUnsupported) {
|
| + auto* mock_callback = new MockPresentationAvailabilityCallbacks();
|
| + EXPECT_CALL(*mock_callback, onError(_)).Times(1);
|
| +
|
| + std::vector<URLState> state_seq = {URLState::Available,
|
| + URLState::Unsupported};
|
| + TestGetAvailabilityKUrls(2, state_seq, mock_callback);
|
| +}
|
| +
|
| +TEST_F(PresentationDispatcherTest,
|
| + GetAvailabilityMultipleUrlsOneBecomesAvailable) {
|
| + auto* mock_callback = new MockPresentationAvailabilityCallbacks();
|
| + EXPECT_CALL(*mock_callback, onSuccess(true)).Times(1);
|
| +
|
| + std::vector<URLState> state_seq = {URLState::Unavailable,
|
| + URLState::Available};
|
| + TestGetAvailabilityKUrls(2, state_seq, mock_callback);
|
| +}
|
| +
|
| +TEST_F(PresentationDispatcherTest,
|
| + GetAvailabilityReturnsDirectlyForAlreadyListeningUrls) {
|
| + auto* mock_callback_1 = new MockPresentationAvailabilityCallbacks();
|
| + EXPECT_CALL(*mock_callback_1, onSuccess(true)).Times(1);
|
| +
|
| + std::vector<URLState> state_seq = {URLState::Available, URLState::Unavailable,
|
| + URLState::Unavailable};
|
| + TestGetAvailabilityKUrls(3, state_seq, mock_callback_1);
|
| +
|
| + base::RunLoop run_loop;
|
| +
|
| + blink::WebVector<blink::WebURL> urls(static_cast<size_t>(2));
|
| + urls[0] = GURL("http://example0.com");
|
| + urls[1] = GURL("http://example1.com");
|
| +
|
| + auto* mock_callback_2 = new MockPresentationAvailabilityCallbacks();
|
| + EXPECT_CALL(*mock_callback_2, onSuccess(true)).Times(1);
|
| +
|
| + client()->getAvailability(urls, base::WrapUnique(mock_callback_2));
|
| +
|
| + run_loop.RunUntilIdle();
|
| +}
|
| +
|
| +TEST_F(PresentationDispatcherTest, StartListeningListenToEachURLOnce) {
|
| + StartListeningToMultipleRequests();
|
| +}
|
| +
|
| +TEST_F(PresentationDispatcherTest, StopListeningListenToEachURLOnce) {
|
| + StartListeningToMultipleRequests();
|
| +
|
| + for (const auto& url : urls_) {
|
| + EXPECT_CALL(mock_presentation_service_,
|
| + StopListeningForScreenAvailability(url))
|
| + .Times(1);
|
| + }
|
| +
|
| + base::RunLoop run_loop;
|
| + for (const auto& mock_observer : mock_observers_)
|
| + StopListening(mock_observer.get());
|
| + run_loop.RunUntilIdle();
|
| +}
|
| +
|
| +TEST_F(PresentationDispatcherTest,
|
| + StopListeningDoesNotStopIfURLListenedByOthers) {
|
| + StartListeningToMultipleRequests();
|
| +
|
| + EXPECT_CALL(mock_presentation_service_,
|
| + StopListeningForScreenAvailability(urls_[0]))
|
| + .Times(1);
|
| +
|
| + base::RunLoop run_loop;
|
| + StopListening(mock_observers_[0].get());
|
| + run_loop.RunUntilIdle();
|
| +}
|
| +
|
| +TEST_F(PresentationDispatcherTest,
|
| + OnScreenAvailabilityUpdatedInvokesAvailabilityChanged) {
|
| + StartListeningToMultipleRequests();
|
| +
|
| + EXPECT_CALL(*(mock_observers_[0]), availabilityChanged(true));
|
| +
|
| + base::RunLoop run_loop;
|
| + ChangeURLState(urls_[0], URLState::Available);
|
| + run_loop.RunUntilIdle();
|
| +}
|
| +
|
| +TEST_F(PresentationDispatcherTest,
|
| + OnScreenAvailabilityUpdatedInvokesMultipleAvailabilityChanged) {
|
| + StartListeningToMultipleRequests();
|
| +
|
| + for (const auto& mock_observer : mock_observers_)
|
| + EXPECT_CALL(*mock_observer, availabilityChanged(true));
|
| +
|
| + base::RunLoop run_loop;
|
| + ChangeURLState(urls_[1], URLState::Available);
|
| + run_loop.RunUntilIdle();
|
| +}
|
| +
|
| +} // namespace content
|
|
|