1 // Copyright 2015 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.
6 #include "chrome/browser/media/router/create_session_request.h"
7 #include "chrome/browser/media/router/media_source_helper.h"
8 #include "content/public/browser/presentation_service_delegate.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace media_router
{
15 const char kPresentationUrl
[] = "http://fooUrl";
16 const char kPresentationId
[] = "presentationId";
20 class CreateSessionRequestTest
: public ::testing::Test
{
22 CreateSessionRequestTest() : cb_invoked_(false) {}
23 ~CreateSessionRequestTest() override
{}
25 void OnSuccess(const content::PresentationSessionInfo
& expected_info
,
26 const content::PresentationSessionInfo
& actual_info
,
27 const MediaRoute::Id
& route_id
) {
29 EXPECT_EQ(expected_info
.presentation_url
, actual_info
.presentation_url
);
30 EXPECT_EQ(expected_info
.presentation_id
, actual_info
.presentation_id
);
33 void OnError(const content::PresentationError
& expected_error
,
34 const content::PresentationError
& actual_error
) {
36 EXPECT_EQ(expected_error
.error_type
, actual_error
.error_type
);
37 EXPECT_EQ(expected_error
.message
, actual_error
.message
);
40 void FailOnSuccess(const content::PresentationSessionInfo
& info
,
41 const MediaRoute::Id
& route_id
) {
42 FAIL() << "Success callback should not have been called.";
45 void FailOnError(const content::PresentationError
& error
) {
46 FAIL() << "Error should not have been called.";
52 // Test that the object's getters match the constructor parameters.
53 TEST_F(CreateSessionRequestTest
, Getters
) {
54 GURL
frame_url("http://frameUrl");
56 content::PresentationSessionInfo
session_info(kPresentationUrl
,
59 CreateSessionRequest
context(
60 kPresentationUrl
, kPresentationId
, frame_url
,
61 CreateSessionRequest::PresentationSessionSuccessCallback(),
62 CreateSessionRequest::PresentationSessionErrorCallback());
63 EXPECT_TRUE(MediaSourceForPresentationUrl(kPresentationUrl
)
64 .Equals(context
.GetMediaSource()));
65 EXPECT_EQ(frame_url
, context
.frame_url());
66 content::PresentationSessionInfo
actual_session_info(
67 context
.presentation_info());
68 EXPECT_EQ(kPresentationUrl
, actual_session_info
.presentation_url
);
69 EXPECT_EQ(kPresentationId
, actual_session_info
.presentation_id
);
72 TEST_F(CreateSessionRequestTest
, SuccessCallback
) {
73 GURL
frame_url("http://frameUrl");
74 content::PresentationSessionInfo
session_info(kPresentationUrl
,
76 CreateSessionRequest
context(
77 kPresentationUrl
, kPresentationId
, frame_url
,
78 base::Bind(&CreateSessionRequestTest::OnSuccess
, base::Unretained(this),
80 base::Bind(&CreateSessionRequestTest::FailOnError
,
81 base::Unretained(this)));
82 context
.MaybeInvokeSuccessCallback("routeid");
83 // No-op since success callback is already invoked.
84 context
.MaybeInvokeErrorCallback(content::PresentationError(
85 content::PRESENTATION_ERROR_NO_AVAILABLE_SCREENS
, "Error message"));
86 EXPECT_TRUE(cb_invoked_
);
89 TEST_F(CreateSessionRequestTest
, ErrorCallback
) {
90 GURL
frame_url("http://frameUrl");
91 content::PresentationSessionInfo
session_info(kPresentationUrl
,
93 content::PresentationError
error(
94 content::PRESENTATION_ERROR_SESSION_REQUEST_CANCELLED
,
95 "This is an error message");
96 CreateSessionRequest
context(
97 kPresentationUrl
, kPresentationId
, frame_url
,
98 base::Bind(&CreateSessionRequestTest::FailOnSuccess
,
99 base::Unretained(this)),
100 base::Bind(&CreateSessionRequestTest::OnError
, base::Unretained(this),
102 context
.MaybeInvokeErrorCallback(error
);
103 // No-op since error callback is already invoked.
104 context
.MaybeInvokeSuccessCallback("routeid");
105 EXPECT_TRUE(cb_invoked_
);
108 } // namespace media_router