'use strict'

const tabs = require('sdk/tabs')

const fs = require('../lib/protocols.js').fs.createInstance()
const gw = require('../lib/gateways.js')
const self = require('sdk/self')
const testpage = self.data.url('linkify-demo.html')
const sripage = 'fs:/ipfs/QmSrCRJmzE4zE1nAfWPbzVfanKQNBhp7ZWmMnEdbiLvYNh/mdown#sample.md'
const parent = require('sdk/remote/parent')

parent.remoteRequire('../lib/child-main.js', module)

const {Cc, Ci} = require('chrome')
const ioservice = Cc['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOService)

ioservice.newURI('fs:/ipns/foo', null, null)

exports['test newURI'] = function (assert) {
  require('sdk/simple-prefs').prefs.fsUris = true

  assert.equal(fs.newURI('fs:/ipns/foo', null, null).spec, 'fs:/ipns/foo', 'keeps fs:/ uris as-is')
}

exports['test newChannel'] = function (assert) {
  require('sdk/simple-prefs').prefs.fsUris = true
  gw.redirectEnabled = false

  let uri = fs.newURI('fs:///ipns/foo', null, null)
  let chan = fs.newChannel(uri)

  assert.equal(chan.originalURI.spec, 'fs:/ipns/foo', "keeps fs: URI as channel's originalURI")

  // double and triple slashes lead to gateway redirects, which cause CORS troubles -> check normalization
  assert.equal(chan.URI.spec, gw.publicUri.spec + 'ipns/foo', 'redirect off, channel has normalized http urls')

  gw.redirectEnabled = true

  chan = fs.newChannel(uri)

  assert.equal(chan.URI.spec, gw.customUri.spec + 'ipns/foo', 'redirect on, channel has normalized http urls')
}

// https://github.com/lidel/ipfs-firefox-addon/issues/3
exports['test subresource loading'] = function (assert, done) {
  require('sdk/simple-prefs').prefs.fsUris = true
  gw.redirectEnabled = false

  tabs.open({
    url: testpage,
    onReady: (tab) => {
      // first load somehow doesn't have protocol handlers registered. so load resource:// first, then redirect to fs:/ page
      if (tab.url !== sripage) {
        tab.url = sripage
        tab.reload()
        return
      }

      let worker = tab.attach({
        contentScript: `
        let obs = new MutationObserver(function(mutations) {
          let result = (document.querySelector("#ipfs-markdown-reader") instanceof HTMLHeadingElement)
          self.port.emit("test result", {result: result})
        })
        obs.observe(document.body,{childList: true})
        `
      })
      worker.port.on('test result', (msg) => {
        assert.equal(msg.result, true, 'subresource loaded successfully')
        tab.close(done)
      })
    }
  })
}

require('./prefs-util.js').isolateTestCases(exports)
require('sdk/test').run(exports)

@@ -39,16 +39,62 @@
 k/tabs')
+%0Aconst %7B prefs %7D = require('sdk/simple-prefs')
 %0A%0Aconst 
@@ -285,23 +285,21 @@
 nst 
-sripage = 'fs:/
+mdownPath = '
 ipfs
@@ -363,16 +363,51 @@
 ple.md'%0A
+const sripage = 'fs:/' + mdownPath%0A
 const pa
@@ -665,16 +665,206 @@
  null)%0A%0A
+exports%5B'test mdownPath load via http handler'%5D = function (assert, done) %7B%0A  tabs.open(%7B%0A    url: gw.publicUri.spec + mdownPath,%0A    onReady: (tab) =%3E %7B%0A      tab.close(done)%0A    %7D%0A  %7D)%0A%7D%0A%0A
 exports%5B
@@ -906,36 +906,8 @@
 %7B%0A  
-require('sdk/simple-prefs').
 pref
@@ -1081,36 +1081,8 @@
 %7B%0A  
-require('sdk/simple-prefs').
 pref
@@ -1827,36 +1827,8 @@
 %7B%0A  
-require('sdk/simple-prefs').
 pref
@@ -2118,29 +2118,8 @@
 age%0A
-        tab.reload()%0A
     

test - removed dupplicate reload, added preload