OLD | NEW |
(Empty) | |
| 1 /** |
| 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. |
| 5 * |
| 6 * @fileoverview Common APIs for presentation integration tests. |
| 7 * |
| 8 */ |
| 9 |
| 10 var startSessionPromise = null; |
| 11 var currentSession = null; |
| 12 var presentation = window.navigator.presentation; |
| 13 |
| 14 |
| 15 /** |
| 16 * Waits until one device is available then starts session. |
| 17 */ |
| 18 function startSession() { |
| 19 presentation.onavailablechange = function (e) { |
| 20 console.log('onavailablechange ' + e.available + '\n'); |
| 21 if (!e.available) { |
| 22 sendResult(false, 'device unavailable'); |
| 23 } else { |
| 24 var presId = Math.random().toFixed(6).substr(2); |
| 25 // Start new session |
| 26 startSessionPromise = presentation.startSession( |
| 27 "http://www.google.com/#__testprovider__=true", presId) |
| 28 sendResult(true, ''); |
| 29 } |
| 30 }; |
| 31 } |
| 32 |
| 33 /** |
| 34 * Checks if the session has been started successfully. |
| 35 */ |
| 36 function checkSession() { |
| 37 if (!startSessionPromise) { |
| 38 sendResult(false, 'Failed to start session'); |
| 39 } else { |
| 40 startSessionPromise.then(function (currentSession) { |
| 41 if(!currentSession) { |
| 42 sendResult(false, 'Failed to start session'); |
| 43 } else { |
| 44 // set the new session |
| 45 currentSession = currentSession; |
| 46 sendResult(true, ''); |
| 47 } |
| 48 }).catch(function() { |
| 49 // close old session if exists |
| 50 currentSession && currentSession.close(); |
| 51 sendResult(false, 'Failed to start session'); |
| 52 }) |
| 53 } |
| 54 } |
| 55 |
| 56 |
| 57 /** |
| 58 * Stops current session. |
| 59 */ |
| 60 function stopSession() { |
| 61 if (currentSession) { |
| 62 currentSession.close(); |
| 63 } |
| 64 sendResult(true, ''); |
| 65 } |
| 66 |
| 67 |
| 68 /** |
| 69 * Sends the test result back to browser test. |
| 70 * @param passed true if test passes, otherwise false. |
| 71 * @param errorMessage empty string if test passes, error message if test |
| 72 * fails. |
| 73 */ |
| 74 function sendResult(passed, errorMessage) { |
| 75 window.domAutomationController.send(JSON.stringify({ |
| 76 passed: passed, |
| 77 errorMessage: errorMessage |
| 78 })); |
| 79 } |
OLD | NEW |