// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

exports.defineManualTests = function(rootEl, addButton) {
  var addr = '127.0.0.1';
  var port = 12345;

  var arr = new Uint8Array(256);
  for (var i = 0; i < arr.length; i++) {
    arr[i] = i;
  }

  function acceptErrorListener(info) {
    logger('AcceptError on socket: ' + info.socketId);
    logger(info);
    chrome.sockets.tcpServer.disconnect(info.socketId);
    chrome.sockets.tcpServer.close(info.socketId);
  }

  function acceptListener(info) {
    logger('Accepted on socket: ' + info.socketId);
    logger(info);
    chrome.sockets.tcp.setPaused(info.clientSocketId, false, function() {
      chrome.sockets.tcp.send(info.clientSocketId, arr.buffer, function(result) {
        if (result.resultCode === 0) {
          logger('TCP send: success');
        }
      });
    });
  }

  function receiveErrorListener(info) {
    logger('Server RecvError on socket: ' + info.socketId);
    logger(info);
    chrome.sockets.tcp.disconnect(info.socketId);
    chrome.sockets.tcp.close(info.socketId);
  }

  function receiveListener(info) {
    logger('Server Recv: success');
    logger(info);
    chrome.sockets.tcp.disconnect(info.socketId);
    chrome.sockets.tcp.close(info.socketId);
  }

  function addReceiveListeners() {
    chrome.sockets.tcp.onReceiveError.addListener(receiveErrorListener);
    chrome.sockets.tcp.onReceive.addListener(receiveListener);
  }

  function removeReceiveListeners() {
    chrome.sockets.tcp.onReceiveError.removeListener(receiveErrorListener);
    chrome.sockets.tcp.onReceive.removeListener(receiveListener);
  }

  function addAcceptListeners() {
    chrome.sockets.tcpServer.onAcceptError.addListener(acceptErrorListener);
    chrome.sockets.tcpServer.onAccept.addListener(acceptListener);
  }

  function removeAcceptListeners() {
    chrome.sockets.tcpServer.onAcceptError.removeListener(acceptErrorListener);
    chrome.sockets.tcpServer.onAccept.removeListener(acceptListener);
  }

  function listen() {
    chrome.sockets.tcpServer.create(function(createInfo) {
      chrome.sockets.tcpServer.listen(createInfo.socketId, addr, port, /** backlog */ function(result){
        if (result === 0) {
          logger('socket is listenning');
        }
      });
    });
  }

  function updateSocket() {
    chrome.sockets.tcpServer.create({}, function(createInfo) {
      updatedProperties = {
        persistent: true,
        name: 'testUpdate',
      };

      chrome.sockets.tcpServer.update(createInfo.socketId, updatedProperties);

      chrome.sockets.tcpServer.getInfo(createInfo.socketId, function(socketInfo) {
        logger(socketInfo);
      });
    });
  }

  function getSockets() {
    chrome.sockets.tcpServer.getSockets(function(socketsInfo) {
      logger('Get Tcp Server Sockets');
      if (!socketsInfo) return;
      for (var i = 0; i < socketsInfo.length; i++) {
        logger(socketsInfo[i]);
      }
    });

    chrome.sockets.tcp.getSockets(function(socketsInfo) {
      logger('Get Tcp Sockets');
      if (!socketsInfo) return;
      for (var i = 0; i < socketsInfo.length; i++) {
        logger(socketsInfo[i]);
      }
    });
  }

  function closeSockets() {
    chrome.sockets.tcpServer.getSockets(function(socketsInfo) {
      if (!socketsInfo) return;
      for (var i = 0; i < socketsInfo.length; i++) {
        logger('closing server socket: ' + socketsInfo[i].socketId);
        chrome.sockets.tcpServer.close(socketsInfo[i].socketId);
      }
    });
  }

  function initPage() {

    addButton('add TCP client recieve listeners', function() {
      addReceiveListeners();
    });

    addButton('remove TCP recieve listeners', function() {
      removeReceiveListeners();
    });

    addButton('add accept listeners', function() {
      addAcceptListeners();
    });

    addButton('remove accept listeners', function() {
      removeAcceptListeners();
    });

    addButton('TCP - SERVER: Listen', function() {
      listen();
    });

    addButton('TCP - SERVER: Update socket', function() {
      updateSocket();
    });

    addButton('TCP - SERVER: Get sockets', function() {
      getSockets();
    });

    addButton('TCP - SERVER: Close sockets', function() {
      closeSockets();
    });
  }

  initPage();
};

@@ -398,38 +398,43 @@
 ner(info) %7B%0A    
+console.
 log
-ger
 ('AcceptError on
@@ -458,38 +458,43 @@
 .socketId);%0A    
+console.
 log
-ger
 (info);%0A    chro
@@ -635,22 +635,27 @@
 ) %7B%0A    
+console.
 log
-ger
 ('Accept
@@ -684,38 +684,43 @@
 .socketId);%0A    
+console.
 log
-ger
 (info);%0A    chro
@@ -916,22 +916,27 @@
         
+console.
 log
-ger
 ('TCP se
@@ -1019,38 +1019,43 @@
 ner(info) %7B%0A    
+console.
 log
-ger
 ('Server RecvErr
@@ -1088,30 +1088,35 @@
 ketId);%0A    
+console.
 log
-ger
 (info);%0A    
@@ -1250,22 +1250,27 @@
 ) %7B%0A    
+console.
 log
-ger
 ('Server
@@ -1291,22 +1291,27 @@
 ');%0A    
+console.
 log
-ger
 (info);%0A
@@ -2369,22 +2369,27 @@
         
+console.
 log
-ger
 ('socket
@@ -2789,30 +2789,35 @@
 ) %7B%0A        
+console.
 log
-ger
 (socketInfo)
@@ -2929,38 +2929,43 @@
 tsInfo) %7B%0A      
+console.
 log
-ger
 ('Get Tcp Server
@@ -3061,38 +3061,43 @@
  i++) %7B%0A        
+console.
 log
-ger
 (socketsInfo%5Bi%5D)
@@ -3179,22 +3179,27 @@
 %7B%0A      
+console.
 log
-ger
 ('Get Tc
@@ -3304,22 +3304,27 @@
         
+console.
 log
-ger
 (sockets
@@ -3543,14 +3543,19 @@
     
+console.
 log
-ger
 ('cl

Fix references specific to ChromeSpec - Use console.log instead of `logger` from ChromeSpec app