Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / tab_capture / api_tests.js
1 // Copyright 2014 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.
4
5 var tabCapture = chrome.tabCapture;
6
7 chrome.test.runTests([
8   function captureTabAndVerifyStateTransitions() {
9     // Tab capture events in the order they happen.
10     var tabCaptureEvents = [];
11
12     var tabCaptureListener = function(info) {
13       console.log(info.status);
14       if (info.status == 'stopped') {
15         chrome.test.assertEq('active', tabCaptureEvents.pop());
16         chrome.test.assertEq('pending', tabCaptureEvents.pop());
17         tabCapture.onStatusChanged.removeListener(tabCaptureListener);
18         chrome.test.succeed();
19         return;
20       }
21       tabCaptureEvents.push(info.status);
22     };
23     tabCapture.onStatusChanged.addListener(tabCaptureListener);
24
25     tabCapture.capture({audio: true, video: true}, function(stream) {
26       chrome.test.assertTrue(!!stream);
27       stream.stop();
28     });
29   },
30
31   function getCapturedTabs() {
32     chrome.tabs.create({active:true}, function(secondTab) {
33       // chrome.tabCapture.capture() will only capture the active tab.
34       chrome.test.assertTrue(secondTab.active);
35
36       function checkInfoForSecondTabHasStatus(infos, status) {
37         for (var i = 0; i < infos.length; ++i) {
38           if (infos[i].tabId == secondTab) {
39             chrome.test.assertNe(null, status);
40             chrome.test.assertEq(status, infos[i].status);
41             chrome.test.assertEq(false, infos[i].fullscreen);
42             return;
43           }
44         }
45       }
46
47       // Step 4: After the second tab is closed, check that getCapturedTabs()
48       // returns no info at all about the second tab.  http://crbug.com/338445
49       chrome.tabs.onRemoved.addListener(function() {
50         tabCapture.getCapturedTabs(function checkNoInfos(infos) {
51           checkInfoForSecondTabHasStatus(infos, null);
52           chrome.test.succeed();
53         });
54       });
55
56       var activeStream = null;
57
58       // Step 3: After the stream is stopped, check that getCapturedTabs()
59       // returns 'stopped' capturing status for the second tab.
60       var capturedTabsAfterStopCapture = function(infos) {
61         checkInfoForSecondTabHasStatus(infos, 'stopped');
62         chrome.tabs.remove(secondTab.id);
63       };
64
65       // Step 2: After the stream is started, check that getCapturedTabs()
66       // returns 'active' capturing status for the second tab.
67       var capturedTabsAfterStartCapture = function(infos) {
68         checkInfoForSecondTabHasStatus(infos, 'active');
69         activeStream.stop();
70         tabCapture.getCapturedTabs(capturedTabsAfterStopCapture);
71       };
72
73       // Step 1: Start capturing the second tab (the currently active tab).
74       tabCapture.capture({audio: true, video: true}, function(stream) {
75         chrome.test.assertTrue(!!stream);
76         activeStream = stream;
77         tabCapture.getCapturedTabs(capturedTabsAfterStartCapture);
78       });
79     });
80   },
81
82   function captureSameTab() {
83     var stream1 = null;
84
85     var tabMediaRequestCallback2 = function(stream) {
86       chrome.test.assertLastError(
87           'Cannot capture a tab with an active stream.');
88       chrome.test.assertTrue(!stream);
89       stream1.stop();
90       chrome.test.succeed();
91     };
92
93     tabCapture.capture({audio: true, video: true}, function(stream) {
94       chrome.test.assertTrue(!!stream);
95       stream1 = stream;
96       tabCapture.capture({audio: true, video: true}, tabMediaRequestCallback2);
97     });
98   },
99
100   function onlyVideo() {
101     tabCapture.capture({video: true}, function(stream) {
102       chrome.test.assertTrue(!!stream);
103       stream.stop();
104       chrome.test.succeed();
105     });
106   },
107
108   function noAudioOrVideoRequested() {
109     // If not specified, video is not requested.
110     tabCapture.capture({audio: false}, function(stream) {
111       chrome.test.assertTrue(!stream);
112       chrome.test.succeed();
113     });
114   }
115
116 ]);