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 /** @fileoverview Suite of tests for route-details. */
6 cr.define('route_details', function() {
7 function registerTests() {
8 suite('RouteDetails', function() {
10 * Route Details created before each test.
11 * @type {RouteDetails}
16 * First fake route created before each test.
17 * @type {media_router.Route}
22 * Second fake route created before each test.
23 * @type {media_router.Route}
28 * First fake sink created before each test.
29 * @type {media_router.Sink}
34 * Second fake sink created before each test.
35 * @type {media_router.Sink}
39 // Checks whether |expected| and the text in the span element in
40 // the |elementId| element are equal.
41 var checkSpanText = function(expected, elementId) {
42 assertEquals(expected,
43 details.$[elementId].querySelector('span').innerText);
46 // Checks the default route view is shown.
47 var checkDefaultViewIsShown = function() {
48 assertFalse(details.$['route-information'].hasAttribute('hidden'));
49 assertTrue(details.$['custom-controller'].hasAttribute('hidden'));
52 // Checks the custom controller is shown.
53 var checkCustomControllerIsShown = function() {
54 assertTrue(details.$['route-information'].hasAttribute('hidden'));
55 assertFalse(details.$['custom-controller'].hasAttribute('hidden'));
58 // Checks whether |expected| and the text in the |elementId| element
59 // are equal given an id.
60 var checkElementTextWithId = function(expected, elementId) {
61 assertEquals(expected, details.$[elementId].innerText);
64 // Import route_details.html before running suite.
65 suiteSetup(function() {
66 return PolymerTest.importHtml(
67 'chrome://media-router/elements/route_details/' +
68 'route_details.html');
71 // Initialize a route-details before each test.
72 setup(function(done) {
73 PolymerTest.clearBody();
74 details = document.createElement('route-details');
75 document.body.appendChild(details);
77 // Initialize routes and sinks.
78 fakeRouteOne = new media_router.Route('route id 1', 'sink id 1',
79 'Video 1', 1, true, 'chrome-extension://123/custom_view.html');
80 fakeRouteTwo = new media_router.Route('route id 2', 'sink id 2',
82 fakeSinkOne = new media_router.Sink('sink id 1', 'Living Room',
83 media_router.SinkIconType.CAST,
84 media_router.SinkStatus.ACTIVE, [0, 1, 2]);
85 fakeSinkTwo = new media_router.Sink('sink id 2', 'my device',
86 media_router.SinkIconType.CAST,
87 media_router.SinkStatus.ACTIVE, [0, 1, 2]);
89 // Allow for the route details to be created and attached.
93 // Tests for 'back-click' event firing when the 'back-to-devices'
95 test('back button click', function(done) {
96 details.addEventListener('back-click', function() {
99 MockInteractions.tap(details.$['back-button']);
102 // Tests for 'close-button-click' event firing when the close button
104 test('close button click', function(done) {
105 details.addEventListener('close-button-click', function() {
108 MockInteractions.tap(details.$['close-button']);
111 // Tests for 'close-route-click' event firing when the
112 // 'close-route-button' button is clicked.
113 test('close route button click', function(done) {
114 details.addEventListener('close-route-click', function() {
117 MockInteractions.tap(details.$['close-route-button']);
120 // Tests the initial expected text.
121 test('initial text setting', function() {
122 // <paper-button> text is styled as upper case.
123 checkSpanText(loadTimeData.getString('stopCastingButton')
124 .toUpperCase(), 'close-route-button');
125 checkSpanText('', 'route-information');
126 checkElementTextWithId('', 'sink-name');
129 // Tests when |route| exists but |sink| is null.
130 test('route is set', function() {
132 assertEquals(null, details.route);
133 checkDefaultViewIsShown();
135 // Set |route| to be non-null.
136 details.route = fakeRouteOne;
137 assertEquals(fakeRouteOne, details.route);
138 checkSpanText(loadTimeData.getStringF('castingActivityStatus',
139 fakeRouteOne.description), 'route-information');
140 assertEquals(null, details.sink);
141 checkDefaultViewIsShown();
143 // Set |route| to a different route.
144 details.route = fakeRouteTwo;
145 assertEquals(fakeRouteTwo, details.route);
146 checkSpanText(loadTimeData.getStringF('castingActivityStatus',
147 fakeRouteTwo.description), 'route-information');
148 checkDefaultViewIsShown();
151 // Tests when |sink| exists but |route| is null.
152 test('sink is set', function() {
154 assertEquals(null, details.sink);
155 checkSpanText('', 'route-information');
157 // Set |sink| to be non-null. 'route-information' should be updated.
158 details.sink = fakeSinkOne;
159 assertEquals(fakeSinkOne, details.sink);
160 assertEquals(null, details.route);
161 checkElementTextWithId(fakeSinkOne.name, 'sink-name');
162 checkSpanText('', 'route-information');
164 // Set |sink| to be a different sink. 'route-information' text should
166 details.sink = fakeSinkTwo;
167 assertEquals(fakeSinkTwo, details.sink);
168 checkElementTextWithId(fakeSinkTwo.name, 'sink-name');
169 checkSpanText('', 'route-information');
172 // Tests when |route| and |sink| both exist.
173 test('sink and route are set', function() {
174 details.route = fakeRouteOne;
175 details.sink = fakeSinkOne;
176 assertEquals(fakeSinkOne, details.sink);
177 assertEquals(fakeRouteOne, details.route);
178 checkElementTextWithId(fakeSinkOne.name, 'sink-name');
179 checkSpanText(loadTimeData.getStringF('castingActivityStatus',
180 fakeRouteOne.description), 'route-information');
183 // Tests when |route| and |sink| are both null.
184 test('sink and route are null', function() {
185 assertEquals(null, details.route);
186 assertEquals(null, details.sink);
187 checkSpanText('', 'route-information');
190 // Tests when |route| and |sink| both exist and |route| has custom
191 // controller and it loads.
192 test('route has custom controller and loading succeeds', function(done) {
193 var loadInvoked = false;
194 details.$['custom-controller'].load = function(url) {
196 assertEquals('chrome-extension://123/custom_view.html', url);
197 return Promise.resolve();
200 details.route = fakeRouteOne;
201 details.sink = fakeSinkOne;
202 setTimeout(function() {
203 assertTrue(loadInvoked);
204 checkCustomControllerIsShown();
209 // Tests when |route| and |sink| both exist and |route| has custom
210 // controller but it fails to load.
211 test('route has custom controller but loading fails', function(done) {
212 var loadInvoked = false;
213 details.$['custom-controller'].load = function(url) {
215 return Promise.reject();
218 details.route = fakeRouteOne;
219 details.sink = fakeSinkOne;
220 setTimeout(function() {
221 assertTrue(loadInvoked);
222 checkDefaultViewIsShown();
230 registerTests: registerTests,