2 // browser_tests.exe --gtest_filter=ExtensionApiTest.TabConnect
4 // We have a bunch of places where we need to remember some state from one
5 // test (or setup code) to subsequent tests.
8 var pass = chrome.test.callbackPass;
9 var assertEq = chrome.test.assertEq;
10 var assertTrue = chrome.test.assertTrue;
11 var callbackFail = chrome.test.callbackFail;
12 var fail = chrome.test.fail;
14 chrome.test.getConfig(function(config) {
16 chrome.test.runTests([
17 function setupConnect() {
18 // The web page that our content script will be injected into.
19 var relativePath = '/extensions/api_test/tabs/basics/relative.html';
21 ['http://localhost:PORT'.replace(/PORT/, config.testServer.port),
22 relativePath].join('');
24 setupWindow([testUrl], pass(function(winId, tabIds) {
25 testTabId = tabIds[0];
26 waitForAllTabs(pass());
30 function connectMultipleConnects() {
32 function connect10() {
33 var port = chrome.tabs.connect(testTabId);
34 chrome.test.listenOnce(port.onMessage, function(msg) {
35 assertEq(++connectCount, msg.connections);
36 if (connectCount < 10)
39 port.postMessage("GET");
44 function connectName() {
45 var name = "akln3901n12la";
46 var port = chrome.tabs.connect(testTabId, {"name": name});
47 chrome.test.listenOnce(port.onMessage, function(msg) {
48 assertEq(name, msg.name);
50 var port = chrome.tabs.connect(testTabId);
51 chrome.test.listenOnce(port.onMessage, function(msg) {
52 assertEq('', msg.name);
54 port.postMessage("GET");
56 port.postMessage("GET");
59 function connectPostMessageTypes() {
60 var port = chrome.tabs.connect(testTabId);
61 // Test the content script echoes the message back.
62 var echoMsg = {"num": 10, "string": "hi", "array": [1,2,3,4,5],
64 chrome.test.listenOnce(port.onMessage, function(msg) {
65 assertEq(echoMsg.num, msg.num);
66 assertEq(echoMsg.string, msg.string);
67 assertEq(echoMsg.array[4], msg.array[4]);
68 assertEq(echoMsg.obj.dec, msg.obj.dec);
70 port.postMessage(echoMsg);
73 function connectPostManyMessages() {
74 var port = chrome.tabs.connect(testTabId);
76 var done = chrome.test.listenForever(port.onMessage, function(msg) {
77 assertEq(count++, msg);
82 for (var i = 0; i < 1000; i++) {
87 function connectNoTab() {
88 // Expect a disconnect event when you connect to a non-existant tab, and
89 // once disconnected, expect an error while trying to post messages.
91 "Could not establish connection. Receiving end does not exist.";
92 chrome.tabs.create({}, pass(function(tab) {
93 chrome.tabs.remove(tab.id, pass(function() {
94 var p = chrome.tabs.connect(tab.id);
95 p.onDisconnect.addListener(callbackFail(errorMsg, function() {
98 fail("Error should have been thrown.");
100 // Do nothing- an exception should be thrown.
107 function sendRequest() {
108 var request = "test";
109 chrome.tabs.sendRequest(testTabId, request, pass(function(response) {
110 assertEq(request, response);
114 function sendRequestNoTab() {
116 "Could not establish connection. Receiving end does not exist.";
117 chrome.tabs.create({}, pass(function(tab) {
118 chrome.tabs.remove(tab.id, pass(function() {
119 chrome.tabs.sendRequest(tab.id, "test", callbackFail(errorMsg));