describe("Core Provider", function() {
  var freedom_src;

  var freedom;
  beforeEach(function() {
    freedom_src = getFreedomSource();
    
    var global = {
      console: {
        log: function() {}
      }
    };
  
    // Setup resource loading for the test environment, which uses file:// urls.
    fdom.resources = new Resource();
    fdom.resources.addResolver(function(manifest, url, deferred) {
      if (url.indexOf('relative://') === 0) {
        var dirname = manifest.substr(0, manifest.lastIndexOf('/'));
        deferred.resolve(dirname + '/' + url.substr(11));
        return true;
      }
      return false;
    });
    fdom.resources.addResolver(function(manifest, url, deferred) {
      if (manifest.indexOf('file://') === 0) {
        manifest = 'http' + manifest.substr(4);
        fdom.resources.resolve(manifest, url).done(function(addr) {
          addr = 'file' + addr.substr(4);
          deferred.resolve(addr);
        });
        return true;
      }
      return false;
    });
    fdom.resources.addRetriever('file', fdom.resources.xhrRetriever);

    var path = window.location.href,
        dir_idx = path.lastIndexOf('/'),
        dir = path.substr(0, dir_idx) + '/';
    freedom = setup(global, undefined, {
      manifest: "relative://spec/helper/channel.json",
      portType: 'Frame',
      inject: dir + "node_modules/es5-shim/es5-shim.js",
      src: freedom_src
    });
  });
  
  afterEach(function() {
    var frames = document.getElementsByTagName('iframe');
    for (var i = 0; i < frames.length; i++) {
      frames[i].parentNode.removeChild(frames[i]);
    }
  });

  it("Manages Channels Between Modules", function() {
    var cb = jasmine.createSpy('cb');
    var called = false;
    runs(function() {
      freedom.once('message', function(msg) {
        // created.
        expect(msg).toEqual('creating custom channel 0');
        freedom.on('message', cb);
        freedom.on('message', function() {
          called = true;
        });
        freedom.emit('message', 0);
      });
      freedom.emit('create');
    });

    waitsFor(function() {
      return called;
    }, "Freedom should return success", 4000);

    runs(function() {
      expect(cb).toHaveBeenCalledWith('sending message to 0');
    });
  });

  it("Manages Channels With providers", function() {
    var cb = jasmine.createSpy('cb');
    var called = false;
    runs(function() {
      freedom.once('message', function(msg) {
        // created.
        freedom.on('message', cb);
        freedom.on('message', function() {
          called = true;
        });
        freedom.emit('message', 0);
      });
      freedom.emit('peer');
    });

    waitsFor(function() {
      return called;
    }, "Freedom should return success", 4000);

    runs(function() {
      expect(cb).toHaveBeenCalledWith('sending message to peer 0');
    });
  });
});

@@ -16,16 +16,28 @@
 Provider
+ Integration
 %22, funct
@@ -2881,12 +2881,1184 @@
 ;%0A  %7D);%0A%7D);%0A
+%0Adescribe(%22Core Provider Channels%22, function() %7B%0A  var manager, hub, global;%0A  beforeEach(function() %7B%0A    global = %7Bfreedom: %7B%7D%7D;%0A    hub = new fdom.Hub();%0A    manager = new fdom.port.Manager(hub);%0A    hub.emit('config', %7B%0A      global: global%0A    %7D);%0A  %7D);%0A%0A  it('Links Custom Channels', function() %7B%0A    var source = createTestPort('test');%0A    manager.setup(source);%0A%0A    var chan = source.gotMessage('control').channel;%0A    hub.onMessage(chan, %7B%0A      type: 'Core Provider',%0A      request: 'core'%0A    %7D);%0A    %0A    var core = source.gotMessage('control', %7Btype: 'core'%7D).core;%0A    expect(core).toBeDefined();%0A%0A    var c = new core(), id, input;%0A    var call = c.createChannel(function(chan) %7B%0A      id = chan.identifier;%0A      input = chan.channel;%0A    %7D);%0A    expect(input).toBeDefined();%0A    %0A    var inHandle = jasmine.createSpy('input');%0A    input.on(inHandle);%0A    expect(inHandle).not.toHaveBeenCalled();%0A%0A    var output;%0A    c.bindChannel(id, function(chan) %7B%0A      output = chan;%0A    %7D);%0A    expect(output).toBeDefined();%0A    %0A    expect(inHandle).not.toHaveBeenCalled();%0A    output.emit('message', 'whoo!');%0A    expect(inHandle).toHaveBeenCalled();%0A  %7D);%0A%7D);%0A

add core custom channel basic spec