2 * Copyright 2015 The Chromium Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
6 * @fileoverview Common APIs for presentation integration tests.
10 var startSessionPromise = null;
11 var startedSession = null;
12 var reconnectedSession = null;
13 var presentationUrl = "http://www.google.com/#__testprovider__=true";
14 var startSessionRequest = new PresentationRequest(presentationUrl);
15 var defaultRequestSessionId = null;
17 window.navigator.presentation.defaultRequest = startSessionRequest;
18 window.navigator.presentation.defaultRequest.onsessionconnect = function(e) {
19 defaultRequestSessionId = e.session.id;
23 * Waits until one device is available.
25 function waitUntilDeviceAvailable() {
26 startSessionRequest.getAvailability(presentationUrl).then(
27 function(availability) {
28 console.log('availability ' + availability.value + '\n');
29 if (availability.value) {
32 sendResult(false, 'device unavailable');
35 sendResult(false, 'got error');
42 function startSession() {
43 startSessionPromise = startSessionRequest.start();
44 console.log('start session');
49 * Checks if the session has been started successfully.
51 function checkSession() {
52 if (!startSessionPromise) {
53 sendResult(false, 'Failed to start session');
55 startSessionPromise.then(function(session) {
57 sendResult(false, 'Failed to start session');
59 // set the new session
60 startedSession = session;
64 // close old session if exists
65 startedSession && startedSession.close();
66 sendResult(false, 'Failed to start session');
73 * Stops current session.
75 function stopSession() {
77 startedSession.close();
84 * Reconnects to |sessionId| and verifies that it succeeds.
85 * @param {!string} sessionId ID of session to reconnect.
87 function reconnectSession(sessionId) {
88 var reconnectSessionRequest = new PresentationRequest(presentationUrl);
89 reconnectSessionRequest.reconnect(sessionId).then(function(session) {
91 sendResult(false, 'reconnectSession returned null session');
93 reconnectedSession = session;
96 }).catch(function(error) {
97 sendResult(false, 'reconnectSession failed: ' + error.message);
102 * Calls reconnect(sessionId) and verifies that it fails.
103 * @param {!string} sessionId ID of session to reconnect.
104 * @param {!string} expectedErrorMessage
106 function reconnectSessionAndExpectFailure(sessionId, expectedErrorMessage) {
107 var reconnectSessionRequest = new PresentationRequest(presentationUrl);
108 reconnectSessionRequest.reconnect(sessionId).then(function(session) {
109 sendResult(false, 'reconnect() unexpectedly succeeded.');
110 }).catch(function(error) {
111 if (error.message.indexOf(expectedErrorMessage) > -1) {
112 sendResult(true, '');
115 'Error message mismatch. Expected: ' + expectedErrorMessage +
116 ', actual: ' + error.message);
122 * Sends the test result back to browser test.
123 * @param passed true if test passes, otherwise false.
124 * @param errorMessage empty string if test passes, error message if test
127 function sendResult(passed, errorMessage) {
128 window.domAutomationController.send(JSON.stringify({
130 errorMessage: errorMessage