Convert browser_tests to Swarming.
[chromium-blink-merge.git] / chrome / browser / media / router / presentation_service_delegate_impl_unittest.cc
blob4011f41cabbc8abb7dfcb240e6204af5caa6efc2
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.
5 #include <vector>
7 #include "base/strings/stringprintf.h"
8 #include "chrome/browser/media/router/media_source.h"
9 #include "chrome/browser/media/router/media_source_helper.h"
10 #include "chrome/browser/media/router/mock_media_router.h"
11 #include "chrome/browser/media/router/mock_screen_availability_listener.h"
12 #include "chrome/browser/media/router/presentation_service_delegate_impl.h"
13 #include "chrome/browser/media/router/test_helper.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
16 #include "content/public/browser/presentation_screen_availability_listener.h"
17 #include "content/public/browser/presentation_session.h"
18 #include "content/public/browser/render_process_host.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/test/web_contents_tester.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "ui/base/l10n/l10n_util.h"
24 using ::testing::_;
25 using ::testing::Mock;
26 using ::testing::Return;
27 using ::testing::StrictMock;
29 namespace media_router {
31 class MockDelegateObserver
32 : public content::PresentationServiceDelegate::Observer {
33 public:
34 MOCK_METHOD0(OnDelegateDestroyed, void());
35 MOCK_METHOD1(OnDefaultPresentationStarted,
36 void(const content::PresentationSessionInfo&));
39 class MockDefaultMediaSourceObserver
40 : public PresentationServiceDelegateImpl::DefaultMediaSourceObserver {
41 public:
42 MOCK_METHOD2(OnDefaultMediaSourceChanged,
43 void(const MediaSource&, const GURL&));
46 class PresentationServiceDelegateImplTest
47 : public ChromeRenderViewHostTestHarness {
48 public:
49 void SetUp() override {
50 ChromeRenderViewHostTestHarness::SetUp();
51 content::WebContents* wc = web_contents();
52 ASSERT_TRUE(wc);
53 PresentationServiceDelegateImpl::CreateForWebContents(wc);
54 delegate_impl_ = PresentationServiceDelegateImpl::FromWebContents(wc);
55 delegate_impl_->SetMediaRouterForTest(&router_);
58 PresentationServiceDelegateImpl* delegate_impl_;
59 MockMediaRouter router_;
62 TEST_F(PresentationServiceDelegateImplTest, AddScreenAvailabilityListener) {
63 std::string presentation_url1("http://url1");
64 std::string presentation_url2("http://url2");
65 MediaSource source1 = MediaSourceForPresentationUrl(presentation_url1);
66 MediaSource source2 = MediaSourceForPresentationUrl(presentation_url2);
67 MockScreenAvailabilityListener listener1(presentation_url1);
68 MockScreenAvailabilityListener listener2(presentation_url2);
69 int render_process_id = 10;
70 int render_frame_id1 = 1;
71 int render_frame_id2 = 2;
73 EXPECT_CALL(router_, RegisterMediaSinksObserver(_)).Times(2);
74 EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener(
75 render_process_id, render_frame_id1, &listener1));
76 EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener(
77 render_process_id, render_frame_id2, &listener2));
78 EXPECT_TRUE(delegate_impl_->HasScreenAvailabilityListenerForTest(
79 render_process_id, render_frame_id1, source1.id()))
80 << "Mapping not found for " << source1.ToString();
81 EXPECT_TRUE(delegate_impl_->HasScreenAvailabilityListenerForTest(
82 render_process_id, render_frame_id2, source2.id()))
83 << "Mapping not found for " << source2.ToString();
85 EXPECT_CALL(router_, UnregisterMediaSinksObserver(_)).Times(2);
86 delegate_impl_->RemoveScreenAvailabilityListener(
87 render_process_id, render_frame_id1, &listener1);
88 delegate_impl_->RemoveScreenAvailabilityListener(
89 render_process_id, render_frame_id2, &listener2);
90 EXPECT_FALSE(delegate_impl_->HasScreenAvailabilityListenerForTest(
91 render_process_id, render_frame_id1, source1.id()));
92 EXPECT_FALSE(delegate_impl_->HasScreenAvailabilityListenerForTest(
93 render_process_id, render_frame_id2, source2.id()));
96 TEST_F(PresentationServiceDelegateImplTest, AddSameListenerTwice) {
97 std::string presentation_url1("http://url1");
98 MediaSource source1(MediaSourceForPresentationUrl(presentation_url1));
99 MockScreenAvailabilityListener listener1(presentation_url1);
100 int render_process_id = 1;
101 int render_frame_id = 0;
103 EXPECT_CALL(router_, RegisterMediaSinksObserver(_)).Times(1);
104 EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener(
105 render_process_id, render_frame_id, &listener1));
106 EXPECT_FALSE(delegate_impl_->AddScreenAvailabilityListener(
107 render_process_id, render_frame_id, &listener1));
108 EXPECT_TRUE(delegate_impl_->HasScreenAvailabilityListenerForTest(
109 render_process_id, render_frame_id, source1.id()));
111 EXPECT_CALL(router_, UnregisterMediaSinksObserver(_)).Times(1);
112 delegate_impl_->RemoveScreenAvailabilityListener(render_process_id,
113 render_frame_id, &listener1);
114 EXPECT_FALSE(delegate_impl_->HasScreenAvailabilityListenerForTest(
115 render_process_id, render_frame_id, source1.id()));
118 TEST_F(PresentationServiceDelegateImplTest, SetDefaultPresentationUrl) {
119 content::WebContentsTester::For(web_contents())
120 ->NavigateAndCommit(GURL("http://www.google.com"));
121 content::RenderFrameHost* main_frame = web_contents()->GetMainFrame();
122 ASSERT_TRUE(main_frame);
123 int render_process_id = main_frame->GetProcess()->GetID();
124 int routing_id = main_frame->GetRoutingID();
126 std::string presentation_url1("http://foo");
127 delegate_impl_->SetDefaultPresentationUrl(render_process_id, routing_id,
128 presentation_url1,
129 "defaultPresentationId");
130 EXPECT_TRUE(delegate_impl_->default_source().Equals(
131 MediaSourceForPresentationUrl(presentation_url1)));
133 // Remove default presentation URL.
134 delegate_impl_->SetDefaultPresentationUrl(render_process_id, routing_id, "",
135 "");
136 EXPECT_TRUE(delegate_impl_->default_source().Empty());
138 // Set to a new default presentation URL
139 std::string presentation_url2("https://youtube.com");
140 delegate_impl_->SetDefaultPresentationUrl(render_process_id, routing_id,
141 presentation_url2,
142 "defaultPresentationId");
143 EXPECT_TRUE(delegate_impl_->default_source().Equals(
144 MediaSourceForPresentationUrl(presentation_url2)));
147 TEST_F(PresentationServiceDelegateImplTest, DefaultMediaSourceObserver) {
148 content::WebContentsTester::For(web_contents())
149 ->NavigateAndCommit(GURL("http://www.google.com"));
150 content::RenderFrameHost* main_frame = web_contents()->GetMainFrame();
151 ASSERT_TRUE(main_frame);
152 int render_process_id = main_frame->GetProcess()->GetID();
153 int routing_id = main_frame->GetRoutingID();
154 StrictMock<MockDefaultMediaSourceObserver> observer1;
155 StrictMock<MockDefaultMediaSourceObserver> observer2;
156 delegate_impl_->AddDefaultMediaSourceObserver(&observer1);
157 delegate_impl_->AddDefaultMediaSourceObserver(&observer2);
158 std::string url1("http://foo");
159 EXPECT_CALL(observer1, OnDefaultMediaSourceChanged(
160 Equals(MediaSourceForPresentationUrl(url1)),
161 GURL("http://www.google.com"))).Times(1);
162 EXPECT_CALL(observer2, OnDefaultMediaSourceChanged(
163 Equals(MediaSourceForPresentationUrl(url1)),
164 GURL("http://www.google.com"))).Times(1);
165 delegate_impl_->SetDefaultPresentationUrl(render_process_id, routing_id, url1,
166 "defaultPresentationId");
168 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer1));
169 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer2));
171 delegate_impl_->RemoveDefaultMediaSourceObserver(&observer2);
172 std::string url2("http://youtube.com");
173 EXPECT_CALL(observer1, OnDefaultMediaSourceChanged(
174 Equals(MediaSourceForPresentationUrl(url2)),
175 GURL("http://www.google.com"))).Times(1);
176 delegate_impl_->SetDefaultPresentationUrl(render_process_id, routing_id, url2,
177 "defaultPresentationId");
179 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer1));
180 // Remove default presentation URL.
181 EXPECT_CALL(observer1, OnDefaultMediaSourceChanged(
182 Equals(MediaSource()),
183 GURL("http://www.google.com"))).Times(1);
184 delegate_impl_->SetDefaultPresentationUrl(render_process_id, routing_id, "",
185 "");
188 TEST_F(PresentationServiceDelegateImplTest, Reset) {
189 std::string presentation_url1("http://url1");
190 MediaSource source = MediaSourceForPresentationUrl(presentation_url1);
191 MockScreenAvailabilityListener listener1(presentation_url1);
192 int render_process_id = 1;
193 int render_frame_id = 0;
195 EXPECT_TRUE(delegate_impl_->AddScreenAvailabilityListener(
196 render_process_id, render_frame_id, &listener1));
197 EXPECT_TRUE(delegate_impl_->HasScreenAvailabilityListenerForTest(
198 render_process_id, render_frame_id, source.id()));
199 EXPECT_CALL(router_, UnregisterMediaSinksObserver(_)).Times(1);
200 delegate_impl_->Reset(render_process_id, render_frame_id);
201 EXPECT_FALSE(delegate_impl_->HasScreenAvailabilityListenerForTest(
202 render_process_id, render_frame_id, source.id()));
205 TEST_F(PresentationServiceDelegateImplTest, DelegateObservers) {
206 scoped_ptr<PresentationServiceDelegateImpl> manager(
207 new PresentationServiceDelegateImpl(web_contents()));
208 manager->SetMediaRouterForTest(&router_);
210 StrictMock<MockDelegateObserver> delegate_observer1;
211 StrictMock<MockDelegateObserver> delegate_observer2;
213 manager->AddObserver(123, 234, &delegate_observer1);
214 manager->AddObserver(345, 456, &delegate_observer2);
216 // Removes |delegate_observer2|.
217 manager->RemoveObserver(345, 456);
219 EXPECT_CALL(delegate_observer1, OnDelegateDestroyed()).Times(1);
220 manager.reset();
223 } // namespace media_router