/*
 * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
 * http://paperjs.org/
 *
 * Copyright (c) 2011 - 2016, Juerg Lehni & Jonathan Puckey
 * http://scratchdisk.com/ & http://jonathanpuckey.com/
 *
 * Distributed under the MIT license. See LICENSE file for details.
 *
 * All rights reserved.
 */

QUnit.module('Emitter');

test('on()', function() {
    var emitter = new Item(),
        installed;
    // fake event type registration
    emitter._eventTypes = {mousemove: {install: function(){ installed = true;} } };
    equals(function() {
        return !emitter.responds('mousemove');
    }, true);
    emitter.on('mousemove', function() {});
    equals(function() {
        return emitter.responds('mousemove')
    }, true);
    equals(function() { return installed; }, true);
    // one time installation only
    installed = false;
    emitter.on('mousemove', function() {});
    equals(function() { return !installed; }, true);

    emitter.on('customUnregistered', function() {});
    equals(function() {
        return emitter.responds('customUnregistered')
    }, true);
});

test('off()', function() {
    var emitter = new Item(),
        uninstalled, called = 0,
        handler = function () {called++},
        handler2 = function () {};
    emitter._eventTypes = {mousemove: {uninstall: function(){ uninstalled = true;} } };

    emitter.on('mousemove', handler);
    emitter.on('mousemove', handler2);
    emitter.on('custom', handler);
    emitter.emit('mousemove');
    equals(function() { return called == 1; }, true);

    emitter.off('mousemove', handler2);
    emitter.emit('mousemove');
    equals(function() { return called == 2; }, true);
    equals(function() { return !uninstalled }, true);

    emitter.off('mousemove', handler);
    emitter.emit('mousemove');
    equals(function() { return called == 2; }, true);
    equals(function() { return uninstalled }, true);

    called = 0;
    emitter.emit('custom');
    equals(function() { return called == 1; }, true);
    emitter.off('custom', handler);
    emitter.emit('custom');
    equals(function() { return called == 1; }, true);
});

test('emit()', function() {
    var emitter = new Item(),
        called,
        handler = function (e) {called = e};
    // fake event type registration
    emitter._eventTypes = {mousemove: {} };
    emitter.on('mousemove', handler);
    emitter.on('custom', handler);

    emitter.emit('mousemove', 'mousemove');
    equals(function() {
        return called == 'mousemove';
    }, true);

    emitter.emit('custom', 'custom');
    equals(function() {
        return called == 'custom';
    }, true);
});

@@ -462,32 +462,41 @@
 ._eventTypes = %7B
+%0A        
 mousemove: %7Binst
@@ -491,16 +491,29 @@
 emove: %7B
+%0A            
 install:
@@ -527,30 +527,72 @@
 on()
-%7B installed = true;%7D %7D
+ %7B%0A                installed = true;%0A            %7D%0A        %7D%0A   
  %7D;%0A
@@ -668,32 +668,33 @@
 ;%0A    %7D, true);%0A
+%0A
     emitter.on('
@@ -740,32 +740,24 @@
 function() %7B
-%0A       
  return emit
@@ -781,20 +781,17 @@
 semove')
-%0A   
+;
  %7D, true
@@ -841,24 +841,25 @@
 ; %7D, true);%0A
+%0A
     // one t
@@ -1131,16 +1131,17 @@
 stered')
+;
 %0A    %7D, 
@@ -1229,16 +1229,24 @@
 stalled,
+%0A       
  called 
@@ -1285,16 +1285,39 @@
 () %7B
-called++
+%0A            called++;%0A        
 %7D,%0A 
@@ -1347,19 +1347,29 @@
 ion () %7B
+%0A        
 %7D;%0A
+%0A
     emit
@@ -1379,32 +1379,41 @@
 ._eventTypes = %7B
+%0A        
 mousemove: %7Bunin
@@ -1408,16 +1408,29 @@
 emove: %7B
+%0A            
 uninstal
@@ -1442,17 +1442,34 @@
 nction()
-%7B
+ %7B%0A               
  uninsta
@@ -1480,19 +1480,44 @@
  = true;
-%7D %7D
+%0A            %7D%0A        %7D%0A   
  %7D;%0A%0A   
@@ -1871,32 +1871,33 @@
 urn !uninstalled
+;
  %7D, true);%0A%0A    
@@ -2058,16 +2058,17 @@
 nstalled
+;
  %7D, true
@@ -2402,18 +2402,41 @@
 e) %7B
-called = e
+%0A            called = e;%0A        
 %7D;%0A 
@@ -2497,16 +2497,25 @@
 ypes = %7B
+%0A        
 mousemov
@@ -2519,16 +2519,20 @@
 move: %7B%7D
+%0A   
  %7D;%0A    

Clean up Emitter tests.