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 joinedSession
= null;
13 var presentationUrl
= "http://www.google.com/#__testprovider__=true";
14 var startSessionRequest
= new PresentationRequest(presentationUrl
);
16 window
.navigator
.presentation
.defaultRequest
= startSessionRequest
;
19 * Waits until one device is available.
21 function waitUntilDeviceAvailable() {
22 startSessionRequest
.getAvailability(presentationUrl
).then(
23 function(availability
) {
24 console
.log('availability ' + availability
.value
+ '\n');
25 if (availability
.value
) {
28 sendResult(false, 'device unavailable');
31 sendResult(false, 'got error');
38 function startSession() {
39 startSessionPromise
= startSessionRequest
.start();
40 console
.log('start session');
45 * Checks if the session has been started successfully.
47 function checkSession() {
48 if (!startSessionPromise
) {
49 sendResult(false, 'Failed to start session');
51 startSessionPromise
.then(function(session
) {
53 sendResult(false, 'Failed to start session');
55 // set the new session
56 startedSession
= session
;
60 // close old session if exists
61 startedSession
&& startedSession
.close();
62 sendResult(false, 'Failed to start session');
69 * Stops current session.
71 function stopSession() {
73 startedSession
.close();
80 * Joins the started session and verify that it succeeds.
81 * @param {!string} sessionId ID of session to join.
83 function joinSession(sessionId
) {
84 var joinSessionRequest
= new PresentationRequest(presentationUrl
);
85 joinSessionRequest
.join(sessionId
).then(function(session
) {
87 sendResult(false, 'joinSession returned null session');
89 joinedSession
= session
;
92 }).catch(function(error
) {
93 sendResult(false, 'joinSession failed: ' + error
.message
);
98 * Sends the test result back to browser test.
99 * @param passed true if test passes, otherwise false.
100 * @param errorMessage empty string if test passes, error message if test
103 function sendResult(passed
, errorMessage
) {
104 window
.domAutomationController
.send(JSON
.stringify({
106 errorMessage
: errorMessage