function createTestForWebviewsOnSamePartition() {
  var agent = new Messaging.Agent('second_test');
  var messageHandler = Messaging.GetHandler();

  // Requests |webviews[0]| and |webviews[1]| to delete all their cookies.
  agent.addTask(START_TEST, function(message, portFrom) {
    for (var i = 0; i < 2; ++i) {
      messageHandler.sendMessage(
          new Messaging.Message(
              agent.getName(), 'agent_cookie', {type: CLEAR_COOKIES}),
          webviews[i].contentWindow);
    }
  });

  // Handles the reponse from |webviews[0]| and |webviews[1]| regarding their
  // cookies being deleted.
  var numWebviewsClearedCookies = 0;
  agent.addTask(CLEAR_COOKIES_COMPLETE, function(message, portFrom) {
    // A webview cleared its cookies. Keeping track of their number.
    numWebviewsClearedCookies++;
    // Create a unique cookie for this webview.
    var cookieName = 'guest' + numWebviewsClearedCookies;
    // Request the webview to write their cookie.
    messageHandler.sendMessage(
        new Messaging.Message( agent.getName(), 'agent_cookie', {
          type: SET_COOKIES,
          cookieData: new Cookie.CookieData(cookieName, 'true', '/',
              tomorrow())
        }),
        portFrom);
  });

  // Handles the response from |webviews[0]| and |webviews[1]| acknowledging
  // the cookie writing task.
  var numCookiesWritten = 0;
  agent.addTask(SET_COOKIES_COMPLETE, function(message, portFrom) {
    numCookiesWritten++; // Keeping track of how many webviews finished writing.
    if (numCookiesWritten == 2) {
      // Both finished writing. Ask for their cookie value.
      for (var i = 0; i < 2; ++i) {
        console.log('Both webviews finished writing their cookies.' +
            ' Asking them to read us their cookie now.');
        messageHandler.sendMessage(
            new Messaging.Message(
                agent.getName(), 'agent_cookie', {type: GET_COOKIES}),
            webviews[i].contentWindow);
      }
    }
  });

  // Handles the response from webviews reporting their cookie value.
  var numCookiesRead = 0;
  var wrongCookieCount = false;
  agent.addTask(GET_COOKIES_COMPLETE, function(message, portFrom) {
    numCookiesRead++; // Keeping track of how many webviews have reported.
    if (wrongCookieCount) {
      // Already failed the test.
      return;
    }
    console.log('Some webview sent us its cookie(s).');
    console.log('Cookie: ' + JSON.stringify(message.content.cookies) + '.');
    var count = Object.keys(message.content.cookies).length;
    // Expecting two cookies. If less or more, test fails.
    if (count !== 2) {
      wrongCookieCount = true;
      announceTestEnd(agent.getName(), TEST_FAILED);
      return;
    }
    console.log("Read exactly two cookies.");
    if (numCookiesRead == 2) {
      // Read from both webviews with no errors.
      // Test is over.
      announceTestEnd(agent.getName(), TEST_SUCCEEDED);
    }

  });
  // End of the second Test.

  messageHandler.addAgent(agent);
}
// Tests that the two webviews on the same in-memory partition share cookies.
Tests that the two webviews on the same in-memory partition share cookies.
Tests that the two webviews on the same in - memory partition share cookies .
function createTestForWebviewsOnSamePartition() {
  var agent = new Messaging.Agent('second_test');
  var messageHandler = Messaging.GetHandler();
  agent.addTask(START_TEST, function(message, portFrom) {
    for (var i = 0; i < 2; ++i) {
      messageHandler.sendMessage(
          new Messaging.Message(
              agent.getName(), 'agent_cookie', {type: CLEAR_COOKIES}),
          webviews[i].contentWindow);
    }
  });
  var numWebviewsClearedCookies = 0;
  agent.addTask(CLEAR_COOKIES_COMPLETE, function(message, portFrom) {
    numWebviewsClearedCookies++;
    var cookieName = 'guest' + numWebviewsClearedCookies;
    messageHandler.sendMessage(
        new Messaging.Message( agent.getName(), 'agent_cookie', {
          type: SET_COOKIES,
          cookieData: new Cookie.CookieData(cookieName, 'true', '/',
              tomorrow())
        }),
        portFrom);
  });
  var numCookiesWritten = 0;
  agent.addTask(SET_COOKIES_COMPLETE, function(message, portFrom) {
    numCookiesWritten++; 
    if (numCookiesWritten == 2) {
      for (var i = 0; i < 2; ++i) {
        console.log('Both webviews finished writing their cookies.' +
            ' Asking them to read us their cookie now.');
        messageHandler.sendMessage(
            new Messaging.Message(
                agent.getName(), 'agent_cookie', {type: GET_COOKIES}),
            webviews[i].contentWindow);
      }
    }
  });
  var numCookiesRead = 0;
  var wrongCookieCount = false;
  agent.addTask(GET_COOKIES_COMPLETE, function(message, portFrom) {
    numCookiesRead++; 
    if (wrongCookieCount) {
      return;
    }
    console.log('Some webview sent us its cookie(s).');
    console.log('Cookie: ' + JSON.stringify(message.content.cookies) + '.');
    var count = Object.keys(message.content.cookies).length;
    if (count !== 2) {
      wrongCookieCount = true;
      announceTestEnd(agent.getName(), TEST_FAILED);
      return;
    }
    console.log("Read exactly two cookies.");
    if (numCookiesRead == 2) {
      announceTestEnd(agent.getName(), TEST_SUCCEEDED);
    }
  });
  messageHandler.addAgent(agent);
}
function createTestForWebviewsOnSamePartition ( ) { var agent = new Messaging . Agent ( 'second_test' ) ; var messageHandler = Messaging . GetHandler ( ) ; agent . addTask ( START_TEST , function ( message , portFrom ) { for ( var i = 0 ; i < 2 ; ++ i ) { messageHandler . sendMessage ( new Messaging . Message ( agent . getName ( ) , 'agent_cookie' , { type : CLEAR_COOKIES } ) , webviews [ i ] . contentWindow ) ; } } ) ; var numWebviewsClearedCookies = 0 ; agent . addTask ( CLEAR_COOKIES_COMPLETE , function ( message , portFrom ) { numWebviewsClearedCookies ++ ; var cookieName = 'guest' + numWebviewsClearedCookies ; messageHandler . sendMessage ( new Messaging . Message ( agent . getName ( ) , 'agent_cookie' , { type : SET_COOKIES , cookieData : new Cookie . CookieData ( cookieName , 'true' , '/' , tomorrow ( ) ) } ) , portFrom ) ; } ) ; var numCookiesWritten = 0 ; agent . addTask ( SET_COOKIES_COMPLETE , function ( message , portFrom ) { numCookiesWritten ++ ; if ( numCookiesWritten == 2 ) { for ( var i = 0 ; i < 2 ; ++ i ) { console . log ( 'Both webviews finished writing their cookies.' + ' Asking them to read us their cookie now.' ) ; messageHandler . sendMessage ( new Messaging . Message ( agent . getName ( ) , 'agent_cookie' , { type : GET_COOKIES } ) , webviews [ i ] . contentWindow ) ; } } } ) ; var numCookiesRead = 0 ; var wrongCookieCount = false ; agent . addTask ( GET_COOKIES_COMPLETE , function ( message , portFrom ) { numCookiesRead ++ ; if ( wrongCookieCount ) { return ; } console . log ( 'Some webview sent us its cookie(s).' ) ; console . log ( 'Cookie: ' + JSON . stringify ( message . content . cookies ) + '.' ) ; var count = Object . keys ( message . content . cookies ) . length ; if ( count !== 2 ) { wrongCookieCount = true ; announceTestEnd ( agent . getName ( ) , TEST_FAILED ) ; return ; } console . log ( "Read exactly two cookies." ) ; if ( numCookiesRead == 2 ) { announceTestEnd ( agent . getName ( ) , TEST_SUCCEEDED ) ; } } ) ; messageHandler . addAgent ( agent ) ; }
Tests that the two webviews on the same in-memory partition share cookies.
Tests that the two webviews on the same in - memory partition share cookies .
// Requests |webviews[0]| and |webviews[1]| to delete all their cookies. // Handles the reponse from |webviews[0]| and |webviews[1]| regarding their // cookies being deleted. // A webview cleared its cookies. Keeping track of their number. // Create a unique cookie for this webview. // Request the webview to write their cookie. // Handles the response from |webviews[0]| and |webviews[1]| acknowledging // the cookie writing task. // Keeping track of how many webviews finished writing. // Both finished writing. Ask for their cookie value. // Handles the response from webviews reporting their cookie value. // Keeping track of how many webviews have reported. // Already failed the test. // Expecting two cookies. If less or more, test fails. // Read from both webviews with no errors. // Test is over. // End of the second Test.