summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /addon-sdk/source/test
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'addon-sdk/source/test')
-rw-r--r--addon-sdk/source/test/addons/addon-manager/lib/main.js8
-rw-r--r--addon-sdk/source/test/addons/addon-manager/lib/test-main.js12
-rw-r--r--addon-sdk/source/test/addons/addon-manager/package.json7
-rw-r--r--addon-sdk/source/test/addons/author-email/main.js14
-rw-r--r--addon-sdk/source/test/addons/author-email/package.json6
-rw-r--r--addon-sdk/source/test/addons/child_process/index.js39
-rw-r--r--addon-sdk/source/test/addons/child_process/package.json5
-rw-r--r--addon-sdk/source/test/addons/chrome/chrome.manifest5
-rw-r--r--addon-sdk/source/test/addons/chrome/chrome/content/new-window.xul4
-rw-r--r--addon-sdk/source/test/addons/chrome/chrome/content/panel.html10
-rw-r--r--addon-sdk/source/test/addons/chrome/chrome/locale/en-US/description.properties1
-rw-r--r--addon-sdk/source/test/addons/chrome/chrome/locale/ja-JP/description.properties1
-rw-r--r--addon-sdk/source/test/addons/chrome/chrome/skin/style.css4
-rw-r--r--addon-sdk/source/test/addons/chrome/data/panel.js10
-rw-r--r--addon-sdk/source/test/addons/chrome/main.js97
-rw-r--r--addon-sdk/source/test/addons/chrome/package.json5
-rw-r--r--addon-sdk/source/test/addons/content-permissions/httpd.js5211
-rw-r--r--addon-sdk/source/test/addons/content-permissions/main.js89
-rw-r--r--addon-sdk/source/test/addons/content-permissions/package.json8
-rw-r--r--addon-sdk/source/test/addons/content-script-messages-latency/httpd.js5211
-rw-r--r--addon-sdk/source/test/addons/content-script-messages-latency/main.js90
-rw-r--r--addon-sdk/source/test/addons/content-script-messages-latency/package.json6
-rw-r--r--addon-sdk/source/test/addons/contributors/main.js19
-rw-r--r--addon-sdk/source/test/addons/contributors/package.json6
-rw-r--r--addon-sdk/source/test/addons/curly-id/lib/main.js29
-rw-r--r--addon-sdk/source/test/addons/curly-id/package.json13
-rw-r--r--addon-sdk/source/test/addons/developers/main.js19
-rw-r--r--addon-sdk/source/test/addons/developers/package.json8
-rw-r--r--addon-sdk/source/test/addons/e10s-content/data/test-contentScriptFile.js5
-rw-r--r--addon-sdk/source/test/addons/e10s-content/data/test-page-worker.html13
-rw-r--r--addon-sdk/source/test/addons/e10s-content/data/test-page-worker.js29
-rw-r--r--addon-sdk/source/test/addons/e10s-content/data/test.html13
-rw-r--r--addon-sdk/source/test/addons/e10s-content/lib/fixtures.js8
-rw-r--r--addon-sdk/source/test/addons/e10s-content/lib/httpd.js5212
-rw-r--r--addon-sdk/source/test/addons/e10s-content/lib/main.js22
-rw-r--r--addon-sdk/source/test/addons/e10s-content/lib/test-content-script.js845
-rw-r--r--addon-sdk/source/test/addons/e10s-content/lib/test-content-worker.js1127
-rw-r--r--addon-sdk/source/test/addons/e10s-content/lib/test-page-worker.js524
-rw-r--r--addon-sdk/source/test/addons/e10s-content/package.json11
-rw-r--r--addon-sdk/source/test/addons/e10s-l10n/data/test-localization.html29
-rw-r--r--addon-sdk/source/test/addons/e10s-l10n/locale/en.properties38
-rw-r--r--addon-sdk/source/test/addons/e10s-l10n/locale/eo.properties5
-rw-r--r--addon-sdk/source/test/addons/e10s-l10n/locale/fr-FR.properties14
-rw-r--r--addon-sdk/source/test/addons/e10s-l10n/main.js289
-rw-r--r--addon-sdk/source/test/addons/e10s-l10n/package.json5
-rw-r--r--addon-sdk/source/test/addons/e10s-remote/main.js578
-rw-r--r--addon-sdk/source/test/addons/e10s-remote/package.json9
-rw-r--r--addon-sdk/source/test/addons/e10s-remote/remote-module.js129
-rw-r--r--addon-sdk/source/test/addons/e10s-remote/utils.js110
-rw-r--r--addon-sdk/source/test/addons/e10s-tabs/lib/main.js23
-rw-r--r--addon-sdk/source/test/addons/e10s-tabs/lib/private-browsing/helper.js91
-rw-r--r--addon-sdk/source/test/addons/e10s-tabs/lib/test-tab-events.js238
-rw-r--r--addon-sdk/source/test/addons/e10s-tabs/lib/test-tab-observer.js46
-rw-r--r--addon-sdk/source/test/addons/e10s-tabs/lib/test-tab-utils.js67
-rw-r--r--addon-sdk/source/test/addons/e10s-tabs/lib/test-tab.js87
-rw-r--r--addon-sdk/source/test/addons/e10s-tabs/package.json11
-rw-r--r--addon-sdk/source/test/addons/e10s/lib/main.js65
-rw-r--r--addon-sdk/source/test/addons/e10s/package.json10
-rw-r--r--addon-sdk/source/test/addons/embedded-webextension/main.js159
-rw-r--r--addon-sdk/source/test/addons/embedded-webextension/package.json6
-rw-r--r--addon-sdk/source/test/addons/embedded-webextension/webextension/background-page.js10
-rw-r--r--addon-sdk/source/test/addons/embedded-webextension/webextension/content-script.js10
-rw-r--r--addon-sdk/source/test/addons/embedded-webextension/webextension/manifest.json18
-rw-r--r--addon-sdk/source/test/addons/jetpack-addon.ini48
-rw-r--r--addon-sdk/source/test/addons/l10n-properties/app-extension/application.ini11
-rw-r--r--addon-sdk/source/test/addons/l10n-properties/app-extension/bootstrap.js339
-rw-r--r--addon-sdk/source/test/addons/l10n-properties/app-extension/install.rdf33
-rw-r--r--addon-sdk/source/test/addons/l10n-properties/app-extension/locale/en-GB.properties28
-rw-r--r--addon-sdk/source/test/addons/l10n-properties/app-extension/locale/en-US.properties22
-rw-r--r--addon-sdk/source/test/addons/l10n-properties/app-extension/locale/eo.properties5
-rw-r--r--addon-sdk/source/test/addons/l10n-properties/app-extension/locale/fr-FR.properties14
-rw-r--r--addon-sdk/source/test/addons/l10n-properties/data/test-localization.html24
-rw-r--r--addon-sdk/source/test/addons/l10n-properties/main.js202
-rw-r--r--addon-sdk/source/test/addons/l10n-properties/package.json6
-rw-r--r--addon-sdk/source/test/addons/l10n/data/test-localization.html29
-rw-r--r--addon-sdk/source/test/addons/l10n/locale/en.properties38
-rw-r--r--addon-sdk/source/test/addons/l10n/locale/eo.properties5
-rw-r--r--addon-sdk/source/test/addons/l10n/locale/fr-FR.properties14
-rw-r--r--addon-sdk/source/test/addons/l10n/main.js289
-rw-r--r--addon-sdk/source/test/addons/l10n/package.json5
-rw-r--r--addon-sdk/source/test/addons/layout-change/lib/main.js15
-rw-r--r--addon-sdk/source/test/addons/layout-change/lib/test-cuddlefish-loader.js164
-rw-r--r--addon-sdk/source/test/addons/layout-change/lib/test-toolkit-loader.js10
-rw-r--r--addon-sdk/source/test/addons/layout-change/package.json7
-rw-r--r--addon-sdk/source/test/addons/main/main.js37
-rw-r--r--addon-sdk/source/test/addons/main/package.json5
-rw-r--r--addon-sdk/source/test/addons/name-in-numbers-plus/index.js12
-rw-r--r--addon-sdk/source/test/addons/name-in-numbers-plus/package.json6
-rw-r--r--addon-sdk/source/test/addons/name-in-numbers/index.js12
-rw-r--r--addon-sdk/source/test/addons/name-in-numbers/package.json6
-rw-r--r--addon-sdk/source/test/addons/packaging/main.js57
-rw-r--r--addon-sdk/source/test/addons/packaging/package.json6
-rw-r--r--addon-sdk/source/test/addons/packed/main.js20
-rw-r--r--addon-sdk/source/test/addons/packed/package.json6
-rw-r--r--addon-sdk/source/test/addons/page-mod-debugger-post/data/index.html11
-rw-r--r--addon-sdk/source/test/addons/page-mod-debugger-post/data/script.js16
-rw-r--r--addon-sdk/source/test/addons/page-mod-debugger-post/main.js136
-rw-r--r--addon-sdk/source/test/addons/page-mod-debugger-post/package.json6
-rw-r--r--addon-sdk/source/test/addons/page-mod-debugger-pre/data/index.html11
-rw-r--r--addon-sdk/source/test/addons/page-mod-debugger-pre/data/script.js16
-rw-r--r--addon-sdk/source/test/addons/page-mod-debugger-pre/main.js134
-rw-r--r--addon-sdk/source/test/addons/page-mod-debugger-pre/package.json6
-rw-r--r--addon-sdk/source/test/addons/page-worker/data/page.html9
-rw-r--r--addon-sdk/source/test/addons/page-worker/data/page.js13
-rw-r--r--addon-sdk/source/test/addons/page-worker/main.js53
-rw-r--r--addon-sdk/source/test/addons/page-worker/package.json3
-rw-r--r--addon-sdk/source/test/addons/places/lib/favicon-helpers.js54
-rw-r--r--addon-sdk/source/test/addons/places/lib/httpd.js5211
-rw-r--r--addon-sdk/source/test/addons/places/lib/main.js27
-rw-r--r--addon-sdk/source/test/addons/places/lib/places-helper.js239
-rw-r--r--addon-sdk/source/test/addons/places/lib/test-places-bookmarks.js948
-rw-r--r--addon-sdk/source/test/addons/places/lib/test-places-events.js328
-rw-r--r--addon-sdk/source/test/addons/places/lib/test-places-favicon.js242
-rw-r--r--addon-sdk/source/test/addons/places/lib/test-places-history.js244
-rw-r--r--addon-sdk/source/test/addons/places/lib/test-places-host.js301
-rw-r--r--addon-sdk/source/test/addons/places/lib/test-places-utils.js78
-rw-r--r--addon-sdk/source/test/addons/places/package.json5
-rw-r--r--addon-sdk/source/test/addons/predefined-id-with-at/lib/main.js32
-rw-r--r--addon-sdk/source/test/addons/predefined-id-with-at/package.json13
-rw-r--r--addon-sdk/source/test/addons/preferences-branch/lib/main.js28
-rw-r--r--addon-sdk/source/test/addons/preferences-branch/package.json14
-rw-r--r--addon-sdk/source/test/addons/private-browsing-supported/main.js28
-rw-r--r--addon-sdk/source/test/addons/private-browsing-supported/package.json8
-rw-r--r--addon-sdk/source/test/addons/private-browsing-supported/sidebar/utils.js67
-rw-r--r--addon-sdk/source/test/addons/private-browsing-supported/test-page-mod.js119
-rw-r--r--addon-sdk/source/test/addons/private-browsing-supported/test-panel.js99
-rw-r--r--addon-sdk/source/test/addons/private-browsing-supported/test-private-browsing.js111
-rw-r--r--addon-sdk/source/test/addons/private-browsing-supported/test-selection.js447
-rw-r--r--addon-sdk/source/test/addons/private-browsing-supported/test-sidebar.js212
-rw-r--r--addon-sdk/source/test/addons/private-browsing-supported/test-tabs.js34
-rw-r--r--addon-sdk/source/test/addons/private-browsing-supported/test-window-tabs.js75
-rw-r--r--addon-sdk/source/test/addons/private-browsing-supported/test-windows.js240
-rw-r--r--addon-sdk/source/test/addons/remote/main.js578
-rw-r--r--addon-sdk/source/test/addons/remote/package.json8
-rw-r--r--addon-sdk/source/test/addons/remote/remote-module.js129
-rw-r--r--addon-sdk/source/test/addons/remote/utils.js110
-rw-r--r--addon-sdk/source/test/addons/require/list.js6
-rw-r--r--addon-sdk/source/test/addons/require/main.js87
-rw-r--r--addon-sdk/source/test/addons/require/multiple/a.js5
-rw-r--r--addon-sdk/source/test/addons/require/multiple/b.js5
-rw-r--r--addon-sdk/source/test/addons/require/package.json8
-rw-r--r--addon-sdk/source/test/addons/require/packages/tabs/main.js5
-rw-r--r--addon-sdk/source/test/addons/require/packages/tabs/package.json3
-rw-r--r--addon-sdk/source/test/addons/require/packages/tabs/page-mod.js5
-rw-r--r--addon-sdk/source/test/addons/require/same-folder.js5
-rw-r--r--addon-sdk/source/test/addons/require/sub-folder/module.js5
-rw-r--r--addon-sdk/source/test/addons/require/tabs.js5
-rw-r--r--addon-sdk/source/test/addons/self/data/data.md1
-rw-r--r--addon-sdk/source/test/addons/self/main.js23
-rw-r--r--addon-sdk/source/test/addons/self/package.json5
-rw-r--r--addon-sdk/source/test/addons/simple-prefs-l10n/locale/en.properties5
-rw-r--r--addon-sdk/source/test/addons/simple-prefs-l10n/main.js65
-rw-r--r--addon-sdk/source/test/addons/simple-prefs-l10n/package.json10
-rw-r--r--addon-sdk/source/test/addons/simple-prefs-regression/app-extension/application.ini11
-rw-r--r--addon-sdk/source/test/addons/simple-prefs-regression/app-extension/bootstrap.js339
-rw-r--r--addon-sdk/source/test/addons/simple-prefs-regression/app-extension/defaults/preferences/prefs.js7
-rw-r--r--addon-sdk/source/test/addons/simple-prefs-regression/app-extension/install.rdf34
-rw-r--r--addon-sdk/source/test/addons/simple-prefs-regression/app-extension/options.xul5
-rw-r--r--addon-sdk/source/test/addons/simple-prefs-regression/lib/main.js94
-rw-r--r--addon-sdk/source/test/addons/simple-prefs-regression/package.json24
-rw-r--r--addon-sdk/source/test/addons/simple-prefs/lib/main.js109
-rw-r--r--addon-sdk/source/test/addons/simple-prefs/package.json32
-rw-r--r--addon-sdk/source/test/addons/standard-id/lib/main.js30
-rw-r--r--addon-sdk/source/test/addons/standard-id/package.json13
-rw-r--r--addon-sdk/source/test/addons/tab-close-on-startup/main.js31
-rw-r--r--addon-sdk/source/test/addons/tab-close-on-startup/package.json5
-rw-r--r--addon-sdk/source/test/addons/toolkit-require-reload/main.js77
-rw-r--r--addon-sdk/source/test/addons/toolkit-require-reload/package.json5
-rw-r--r--addon-sdk/source/test/addons/translators/main.js20
-rw-r--r--addon-sdk/source/test/addons/translators/package.json8
-rw-r--r--addon-sdk/source/test/addons/unsafe-content-script/main.js68
-rw-r--r--addon-sdk/source/test/addons/unsafe-content-script/package.json8
-rw-r--r--addon-sdk/source/test/buffers/test-read-types.js368
-rw-r--r--addon-sdk/source/test/buffers/test-write-types.js602
-rw-r--r--addon-sdk/source/test/commonjs-test-adapter/asserts.js54
-rw-r--r--addon-sdk/source/test/context-menu/framescript.js44
-rw-r--r--addon-sdk/source/test/context-menu/test-helper.js539
-rw-r--r--addon-sdk/source/test/context-menu/util.js141
-rw-r--r--addon-sdk/source/test/event/helpers.js112
-rw-r--r--addon-sdk/source/test/fixtures.js41
-rw-r--r--addon-sdk/source/test/fixtures/addon-install-unit-test@mozilla.com.xpibin0 -> 5670 bytes
-rw-r--r--addon-sdk/source/test/fixtures/addon-sdk/data/border-style.css4
-rw-r--r--addon-sdk/source/test/fixtures/addon-sdk/data/test-contentScriptFile.js5
-rw-r--r--addon-sdk/source/test/fixtures/addon-sdk/data/test-page-worker.html13
-rw-r--r--addon-sdk/source/test/fixtures/addon-sdk/data/test-page-worker.js29
-rw-r--r--addon-sdk/source/test/fixtures/addon-sdk/data/test.html13
-rw-r--r--addon-sdk/source/test/fixtures/addon/bootstrap.js9
-rw-r--r--addon-sdk/source/test/fixtures/addon/index.js4
-rw-r--r--addon-sdk/source/test/fixtures/addon/package.json5
-rw-r--r--addon-sdk/source/test/fixtures/bootstrap-addon/META-INF/manifest.mf17
-rw-r--r--addon-sdk/source/test/fixtures/bootstrap-addon/META-INF/mozilla.rsabin0 -> 4191 bytes
-rw-r--r--addon-sdk/source/test/fixtures/bootstrap-addon/META-INF/mozilla.sf4
-rw-r--r--addon-sdk/source/test/fixtures/bootstrap-addon/bootstrap.js10
-rw-r--r--addon-sdk/source/test/fixtures/bootstrap-addon/install.rdf27
-rw-r--r--addon-sdk/source/test/fixtures/bootstrap-addon/options.xul3
-rw-r--r--addon-sdk/source/test/fixtures/bootstrap/utils.js52
-rw-r--r--addon-sdk/source/test/fixtures/border-style.css4
-rw-r--r--addon-sdk/source/test/fixtures/child-process-scripts.js81
-rw-r--r--addon-sdk/source/test/fixtures/chrome-worker/addEventListener.js6
-rw-r--r--addon-sdk/source/test/fixtures/chrome-worker/jsctypes.js6
-rw-r--r--addon-sdk/source/test/fixtures/chrome-worker/onerror.js6
-rw-r--r--addon-sdk/source/test/fixtures/chrome-worker/onmessage.js8
-rw-r--r--addon-sdk/source/test/fixtures/chrome-worker/setTimeout.js8
-rw-r--r--addon-sdk/source/test/fixtures/chrome-worker/xhr.js11
-rw-r--r--addon-sdk/source/test/fixtures/create_xpi.py15
-rw-r--r--addon-sdk/source/test/fixtures/es5.js8
-rw-r--r--addon-sdk/source/test/fixtures/include-file.css4
-rw-r--r--addon-sdk/source/test/fixtures/index.html18
-rw-r--r--addon-sdk/source/test/fixtures/jsm-package/Test.jsm11
-rw-r--r--addon-sdk/source/test/fixtures/jsm-package/index.js46
-rw-r--r--addon-sdk/source/test/fixtures/jsm-package/package.json3
-rw-r--r--addon-sdk/source/test/fixtures/loader/cycles/a.js7
-rw-r--r--addon-sdk/source/test/fixtures/loader/cycles/b.js7
-rw-r--r--addon-sdk/source/test/fixtures/loader/cycles/c.js7
-rw-r--r--addon-sdk/source/test/fixtures/loader/cycles/main.js14
-rw-r--r--addon-sdk/source/test/fixtures/loader/errors/boomer.js7
-rw-r--r--addon-sdk/source/test/fixtures/loader/errors/main.js9
-rw-r--r--addon-sdk/source/test/fixtures/loader/exceptions/boomer.js9
-rw-r--r--addon-sdk/source/test/fixtures/loader/exceptions/main.js11
-rw-r--r--addon-sdk/source/test/fixtures/loader/globals/main.js7
-rw-r--r--addon-sdk/source/test/fixtures/loader/json/invalid.json3
-rw-r--r--addon-sdk/source/test/fixtures/loader/json/manifest.json14
-rw-r--r--addon-sdk/source/test/fixtures/loader/json/mutation.json1
-rw-r--r--addon-sdk/source/test/fixtures/loader/json/nodotjson.json.js8
-rw-r--r--addon-sdk/source/test/fixtures/loader/json/test.json3
-rw-r--r--addon-sdk/source/test/fixtures/loader/json/test.json.js7
-rw-r--r--addon-sdk/source/test/fixtures/loader/lazy/main.js9
-rw-r--r--addon-sdk/source/test/fixtures/loader/missing-twice/file.json1
-rw-r--r--addon-sdk/source/test/fixtures/loader/missing-twice/main.js32
-rw-r--r--addon-sdk/source/test/fixtures/loader/missing/main.js10
-rw-r--r--addon-sdk/source/test/fixtures/loader/self/main.js8
-rw-r--r--addon-sdk/source/test/fixtures/loader/syntax-error/error.js11
-rw-r--r--addon-sdk/source/test/fixtures/loader/syntax-error/main.js10
-rw-r--r--addon-sdk/source/test/fixtures/loader/unsupported/fennec.js10
-rw-r--r--addon-sdk/source/test/fixtures/loader/unsupported/firefox.js10
-rw-r--r--addon-sdk/source/test/fixtures/mofo_logo.SVG45
-rw-r--r--addon-sdk/source/test/fixtures/moz.build22
-rw-r--r--addon-sdk/source/test/fixtures/moz_favicon.icobin0 -> 1406 bytes
-rw-r--r--addon-sdk/source/test/fixtures/native-addon-test/dir/a.js5
-rw-r--r--addon-sdk/source/test/fixtures/native-addon-test/dir/a/index.js5
-rw-r--r--addon-sdk/source/test/fixtures/native-addon-test/dir/b.js6
-rw-r--r--addon-sdk/source/test/fixtures/native-addon-test/dir/c.js6
-rw-r--r--addon-sdk/source/test/fixtures/native-addon-test/dir/dummy.js6
-rw-r--r--addon-sdk/source/test/fixtures/native-addon-test/dir/test.jsm6
-rw-r--r--addon-sdk/source/test/fixtures/native-addon-test/expectedmap.json25
-rw-r--r--addon-sdk/source/test/fixtures/native-addon-test/index.js37
-rw-r--r--addon-sdk/source/test/fixtures/native-addon-test/newmodule/index.js5
-rw-r--r--addon-sdk/source/test/fixtures/native-addon-test/newmodule/lib/file.js5
-rw-r--r--addon-sdk/source/test/fixtures/native-addon-test/newmodule/package.json3
-rw-r--r--addon-sdk/source/test/fixtures/native-addon-test/package.json10
-rw-r--r--addon-sdk/source/test/fixtures/native-addon-test/utils/index.js7
-rw-r--r--addon-sdk/source/test/fixtures/native-overrides-test/ignore.js6
-rw-r--r--addon-sdk/source/test/fixtures/native-overrides-test/index.js19
-rw-r--r--addon-sdk/source/test/fixtures/native-overrides-test/lib/ignore.js6
-rw-r--r--addon-sdk/source/test/fixtures/native-overrides-test/lib/internal.js6
-rw-r--r--addon-sdk/source/test/fixtures/native-overrides-test/lib/tabs.js6
-rw-r--r--addon-sdk/source/test/fixtures/native-overrides-test/package.json18
-rw-r--r--addon-sdk/source/test/fixtures/preferences/curly-id/package.json14
-rw-r--r--addon-sdk/source/test/fixtures/preferences/no-prefs/package.json6
-rw-r--r--addon-sdk/source/test/fixtures/preferences/preferences-branch/package.json14
-rw-r--r--addon-sdk/source/test/fixtures/preferences/simple-prefs/package.json75
-rw-r--r--addon-sdk/source/test/fixtures/sandbox-complex-character.js5
-rw-r--r--addon-sdk/source/test/fixtures/sandbox-normal.js7
-rw-r--r--addon-sdk/source/test/fixtures/test-addon-extras-window.html21
-rw-r--r--addon-sdk/source/test/fixtures/test-addon-extras.html31
-rw-r--r--addon-sdk/source/test/fixtures/test-contentScriptFile.js5
-rw-r--r--addon-sdk/source/test/fixtures/test-context-menu.js5
-rw-r--r--addon-sdk/source/test/fixtures/test-iframe-postmessage.html23
-rw-r--r--addon-sdk/source/test/fixtures/test-iframe.html12
-rw-r--r--addon-sdk/source/test/fixtures/test-iframe.js16
-rw-r--r--addon-sdk/source/test/fixtures/test-message-manager.js6
-rw-r--r--addon-sdk/source/test/fixtures/test-net-url.txt1
-rw-r--r--addon-sdk/source/test/fixtures/test-page-mod.html12
-rw-r--r--addon-sdk/source/test/fixtures/test-sidebar-addon-global.html15
-rw-r--r--addon-sdk/source/test/fixtures/test-trusted-document.html20
-rw-r--r--addon-sdk/source/test/fixtures/test.html25
-rw-r--r--addon-sdk/source/test/fixtures/testLocalXhr.json1
-rw-r--r--addon-sdk/source/test/framescript-manager/frame-script.js13
-rw-r--r--addon-sdk/source/test/framescript-manager/pong.js7
-rw-r--r--addon-sdk/source/test/framescript-util/frame-script.js21
-rw-r--r--addon-sdk/source/test/jetpack-package.ini179
-rw-r--r--addon-sdk/source/test/leak/jetpack-package.ini7
-rw-r--r--addon-sdk/source/test/leak/leak-utils.js80
-rw-r--r--addon-sdk/source/test/leak/test-leak-event-dom-closed-window.js29
-rw-r--r--addon-sdk/source/test/leak/test-leak-tab-events.js46
-rw-r--r--addon-sdk/source/test/leak/test-leak-window-events.js65
-rw-r--r--addon-sdk/source/test/lib/httpd.js5212
-rw-r--r--addon-sdk/source/test/loader/b2g.js41
-rw-r--r--addon-sdk/source/test/loader/fixture.js7
-rw-r--r--addon-sdk/source/test/loader/user-global.js11
-rw-r--r--addon-sdk/source/test/modules/add.js9
-rw-r--r--addon-sdk/source/test/modules/async1.js14
-rw-r--r--addon-sdk/source/test/modules/async2.js8
-rw-r--r--addon-sdk/source/test/modules/badExportAndReturn.js10
-rw-r--r--addon-sdk/source/test/modules/badFirst.js9
-rw-r--r--addon-sdk/source/test/modules/badSecond.js8
-rw-r--r--addon-sdk/source/test/modules/blue.js9
-rw-r--r--addon-sdk/source/test/modules/castor.js10
-rw-r--r--addon-sdk/source/test/modules/cheetah.js9
-rw-r--r--addon-sdk/source/test/modules/color.js7
-rw-r--r--addon-sdk/source/test/modules/dupe.js15
-rw-r--r--addon-sdk/source/test/modules/dupeNested.js15
-rw-r--r--addon-sdk/source/test/modules/dupeSetExports.js8
-rw-r--r--addon-sdk/source/test/modules/exportsEquals.js5
-rw-r--r--addon-sdk/source/test/modules/green.js10
-rw-r--r--addon-sdk/source/test/modules/lion.js7
-rw-r--r--addon-sdk/source/test/modules/orange.js10
-rw-r--r--addon-sdk/source/test/modules/pollux.js10
-rw-r--r--addon-sdk/source/test/modules/red.js16
-rw-r--r--addon-sdk/source/test/modules/setExports.js5
-rw-r--r--addon-sdk/source/test/modules/subtract.js9
-rw-r--r--addon-sdk/source/test/modules/tiger.js8
-rw-r--r--addon-sdk/source/test/modules/traditional1.js12
-rw-r--r--addon-sdk/source/test/modules/traditional2.js6
-rw-r--r--addon-sdk/source/test/modules/types/cat.js5
-rw-r--r--addon-sdk/source/test/page-mod/helpers.js117
-rw-r--r--addon-sdk/source/test/path/test-path.js430
-rw-r--r--addon-sdk/source/test/preferences/common.json16
-rw-r--r--addon-sdk/source/test/preferences/e10s-off.json5
-rw-r--r--addon-sdk/source/test/preferences/e10s-on.json3
-rw-r--r--addon-sdk/source/test/preferences/firefox.json11
-rw-r--r--addon-sdk/source/test/preferences/no-connections.json41
-rw-r--r--addon-sdk/source/test/preferences/test-e10s-preferences.js15
-rw-r--r--addon-sdk/source/test/preferences/test-preferences.js15
-rw-r--r--addon-sdk/source/test/preferences/test.json46
-rw-r--r--addon-sdk/source/test/private-browsing/helper.js58
-rw-r--r--addon-sdk/source/test/private-browsing/tabs.js25
-rw-r--r--addon-sdk/source/test/private-browsing/windows.js115
-rw-r--r--addon-sdk/source/test/querystring/test-querystring.js205
-rw-r--r--addon-sdk/source/test/sidebar/utils.js74
-rw-r--r--addon-sdk/source/test/tabs/test-fennec-tabs.js595
-rw-r--r--addon-sdk/source/test/tabs/test-firefox-tabs.js1305
-rw-r--r--addon-sdk/source/test/tabs/utils.js24
-rw-r--r--addon-sdk/source/test/test-addon-bootstrap.js97
-rw-r--r--addon-sdk/source/test/test-addon-extras.js70
-rw-r--r--addon-sdk/source/test/test-addon-installer.js230
-rw-r--r--addon-sdk/source/test/test-addon-window.js22
-rw-r--r--addon-sdk/source/test/test-api-utils.js316
-rw-r--r--addon-sdk/source/test/test-array.js103
-rw-r--r--addon-sdk/source/test/test-base64.js100
-rw-r--r--addon-sdk/source/test/test-bootstrap.js19
-rw-r--r--addon-sdk/source/test/test-browser-events.js102
-rw-r--r--addon-sdk/source/test/test-buffer.js563
-rw-r--r--addon-sdk/source/test/test-byte-streams.js169
-rw-r--r--addon-sdk/source/test/test-child_process.js545
-rw-r--r--addon-sdk/source/test/test-chrome.js84
-rw-r--r--addon-sdk/source/test/test-clipboard.js170
-rw-r--r--addon-sdk/source/test/test-collection.js128
-rw-r--r--addon-sdk/source/test/test-commonjs-test-adapter.js11
-rw-r--r--addon-sdk/source/test/test-content-events.js92
-rw-r--r--addon-sdk/source/test/test-content-script.js845
-rw-r--r--addon-sdk/source/test/test-content-sync-worker.js965
-rw-r--r--addon-sdk/source/test/test-content-worker.js1129
-rw-r--r--addon-sdk/source/test/test-context-menu.html98
-rw-r--r--addon-sdk/source/test/test-context-menu.js3763
-rw-r--r--addon-sdk/source/test/test-context-menu@2.js1350
-rw-r--r--addon-sdk/source/test/test-cuddlefish.js78
-rw-r--r--addon-sdk/source/test/test-deprecate.js160
-rw-r--r--addon-sdk/source/test/test-dev-panel.js426
-rw-r--r--addon-sdk/source/test/test-diffpatcher.js8
-rw-r--r--addon-sdk/source/test/test-dispatcher.js76
-rw-r--r--addon-sdk/source/test/test-disposable.js393
-rw-r--r--addon-sdk/source/test/test-dom.js88
-rw-r--r--addon-sdk/source/test/test-environment.js49
-rw-r--r--addon-sdk/source/test/test-event-core.js347
-rw-r--r--addon-sdk/source/test/test-event-dom.js92
-rw-r--r--addon-sdk/source/test/test-event-target.js222
-rw-r--r--addon-sdk/source/test/test-event-utils.js285
-rw-r--r--addon-sdk/source/test/test-file.js271
-rw-r--r--addon-sdk/source/test/test-frame-utils.js59
-rw-r--r--addon-sdk/source/test/test-framescript-manager.js32
-rw-r--r--addon-sdk/source/test/test-framescript-util.js45
-rw-r--r--addon-sdk/source/test/test-fs.js621
-rw-r--r--addon-sdk/source/test/test-functional.js463
-rw-r--r--addon-sdk/source/test/test-globals.js30
-rw-r--r--addon-sdk/source/test/test-heritage.js301
-rw-r--r--addon-sdk/source/test/test-hidden-frame.js71
-rw-r--r--addon-sdk/source/test/test-host-events.js99
-rw-r--r--addon-sdk/source/test/test-hotkeys.js183
-rw-r--r--addon-sdk/source/test/test-httpd.js73
-rw-r--r--addon-sdk/source/test/test-indexed-db.js182
-rw-r--r--addon-sdk/source/test/test-jetpack-id.js64
-rw-r--r--addon-sdk/source/test/test-keyboard-observer.js36
-rw-r--r--addon-sdk/source/test/test-keyboard-utils.js61
-rw-r--r--addon-sdk/source/test/test-l10n-locale.js169
-rw-r--r--addon-sdk/source/test/test-l10n-plural-rules.js85
-rw-r--r--addon-sdk/source/test/test-lang-type.js166
-rw-r--r--addon-sdk/source/test/test-libxul.js18
-rw-r--r--addon-sdk/source/test/test-list.js58
-rw-r--r--addon-sdk/source/test/test-loader.js657
-rw-r--r--addon-sdk/source/test/test-match-pattern.js137
-rw-r--r--addon-sdk/source/test/test-method.js7
-rw-r--r--addon-sdk/source/test/test-module.js36
-rw-r--r--addon-sdk/source/test/test-modules.js150
-rw-r--r--addon-sdk/source/test/test-mozilla-toolkit-versioning.js59
-rw-r--r--addon-sdk/source/test/test-mpl2-license-header.js105
-rw-r--r--addon-sdk/source/test/test-namespace.js120
-rw-r--r--addon-sdk/source/test/test-native-loader.js423
-rw-r--r--addon-sdk/source/test/test-native-options.js183
-rw-r--r--addon-sdk/source/test/test-net-url.js137
-rw-r--r--addon-sdk/source/test/test-node-os.js33
-rw-r--r--addon-sdk/source/test/test-notifications.js94
-rw-r--r--addon-sdk/source/test/test-object.js36
-rw-r--r--addon-sdk/source/test/test-observers.js183
-rw-r--r--addon-sdk/source/test/test-page-mod-debug.js66
-rw-r--r--addon-sdk/source/test/test-page-mod.js2214
-rw-r--r--addon-sdk/source/test/test-page-worker.js558
-rw-r--r--addon-sdk/source/test/test-panel.js1426
-rw-r--r--addon-sdk/source/test/test-passwords-utils.js141
-rw-r--r--addon-sdk/source/test/test-passwords.js280
-rw-r--r--addon-sdk/source/test/test-path.js6
-rw-r--r--addon-sdk/source/test/test-plain-text-console.js278
-rw-r--r--addon-sdk/source/test/test-preferences-service.js155
-rw-r--r--addon-sdk/source/test/test-preferences-target.js42
-rw-r--r--addon-sdk/source/test/test-private-browsing.js88
-rw-r--r--addon-sdk/source/test/test-promise.js461
-rw-r--r--addon-sdk/source/test/test-querystring.js6
-rw-r--r--addon-sdk/source/test/test-reference.js99
-rw-r--r--addon-sdk/source/test/test-request.js548
-rw-r--r--addon-sdk/source/test/test-require.js67
-rw-r--r--addon-sdk/source/test/test-rules.js79
-rw-r--r--addon-sdk/source/test/test-sandbox.js161
-rw-r--r--addon-sdk/source/test/test-selection.js985
-rw-r--r--addon-sdk/source/test/test-self.js79
-rw-r--r--addon-sdk/source/test/test-sequence.js1245
-rw-r--r--addon-sdk/source/test/test-set-exports.js37
-rw-r--r--addon-sdk/source/test/test-shared-require.js36
-rw-r--r--addon-sdk/source/test/test-simple-prefs.js331
-rw-r--r--addon-sdk/source/test/test-simple-storage.js322
-rw-r--r--addon-sdk/source/test/test-system-events.js278
-rw-r--r--addon-sdk/source/test/test-system-input-output.js319
-rw-r--r--addon-sdk/source/test/test-system-runtime.js25
-rw-r--r--addon-sdk/source/test/test-system-startup.js19
-rw-r--r--addon-sdk/source/test/test-system.js37
-rw-r--r--addon-sdk/source/test/test-tab-events.js238
-rw-r--r--addon-sdk/source/test/test-tab-observer.js45
-rw-r--r--addon-sdk/source/test/test-tab-utils.js69
-rw-r--r--addon-sdk/source/test/test-tab.js228
-rw-r--r--addon-sdk/source/test/test-tabs-common.js654
-rw-r--r--addon-sdk/source/test/test-tabs.js20
-rw-r--r--addon-sdk/source/test/test-test-addon-file.js16
-rw-r--r--addon-sdk/source/test/test-test-assert.js218
-rw-r--r--addon-sdk/source/test/test-test-loader.js59
-rw-r--r--addon-sdk/source/test/test-test-memory.js25
-rw-r--r--addon-sdk/source/test/test-test-utils-async.js86
-rw-r--r--addon-sdk/source/test/test-test-utils-generator.js76
-rw-r--r--addon-sdk/source/test/test-test-utils-sync.js84
-rw-r--r--addon-sdk/source/test/test-test-utils.js81
-rw-r--r--addon-sdk/source/test/test-text-streams.js154
-rw-r--r--addon-sdk/source/test/test-timer.js229
-rw-r--r--addon-sdk/source/test/test-traceback.js139
-rw-r--r--addon-sdk/source/test/test-ui-action-button.js1182
-rw-r--r--addon-sdk/source/test/test-ui-frame.js252
-rw-r--r--addon-sdk/source/test/test-ui-id.js43
-rw-r--r--addon-sdk/source/test/test-ui-sidebar-private-browsing.js203
-rw-r--r--addon-sdk/source/test/test-ui-sidebar.js1579
-rw-r--r--addon-sdk/source/test/test-ui-toggle-button.js1386
-rw-r--r--addon-sdk/source/test/test-ui-toolbar.js511
-rw-r--r--addon-sdk/source/test/test-unit-test-finder.js57
-rw-r--r--addon-sdk/source/test/test-unit-test.js270
-rw-r--r--addon-sdk/source/test/test-unload.js71
-rw-r--r--addon-sdk/source/test/test-unsupported-skip.js24
-rw-r--r--addon-sdk/source/test/test-uri-resource.js43
-rw-r--r--addon-sdk/source/test/test-url.js502
-rw-r--r--addon-sdk/source/test/test-uuid.js26
-rw-r--r--addon-sdk/source/test/test-weak-set.js146
-rw-r--r--addon-sdk/source/test/test-window-events.js63
-rw-r--r--addon-sdk/source/test/test-window-observer.js61
-rw-r--r--addon-sdk/source/test/test-window-utils-private-browsing.js210
-rw-r--r--addon-sdk/source/test/test-window-utils.js266
-rw-r--r--addon-sdk/source/test/test-window-utils2.js112
-rw-r--r--addon-sdk/source/test/test-windows-common.js104
-rw-r--r--addon-sdk/source/test/test-windows.js24
-rw-r--r--addon-sdk/source/test/test-xhr.js89
-rw-r--r--addon-sdk/source/test/test-xpcom.js232
-rw-r--r--addon-sdk/source/test/test-xul-app.js145
-rw-r--r--addon-sdk/source/test/traits/assert.js101
-rw-r--r--addon-sdk/source/test/traits/utils.js56
-rw-r--r--addon-sdk/source/test/util.js90
-rw-r--r--addon-sdk/source/test/windows/test-fennec-windows.js48
-rw-r--r--addon-sdk/source/test/windows/test-firefox-windows.js621
-rw-r--r--addon-sdk/source/test/zip/utils.js126
482 files changed, 86139 insertions, 0 deletions
diff --git a/addon-sdk/source/test/addons/addon-manager/lib/main.js b/addon-sdk/source/test/addons/addon-manager/lib/main.js
new file mode 100644
index 000000000..043424f59
--- /dev/null
+++ b/addon-sdk/source/test/addons/addon-manager/lib/main.js
@@ -0,0 +1,8 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+module.exports = require("./test-main.js");
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/addon-manager/lib/test-main.js b/addon-sdk/source/test/addons/addon-manager/lib/test-main.js
new file mode 100644
index 000000000..7204c4a40
--- /dev/null
+++ b/addon-sdk/source/test/addons/addon-manager/lib/test-main.js
@@ -0,0 +1,12 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { id } = require("sdk/self");
+const { getAddonByID } = require("sdk/addon/manager");
+
+exports["test getAddonByID"] = function*(assert) {
+ let addon = yield getAddonByID(id);
+ assert.equal(addon.id, id, "getAddonByID works");
+}
diff --git a/addon-sdk/source/test/addons/addon-manager/package.json b/addon-sdk/source/test/addons/addon-manager/package.json
new file mode 100644
index 000000000..2ed748498
--- /dev/null
+++ b/addon-sdk/source/test/addons/addon-manager/package.json
@@ -0,0 +1,7 @@
+{
+ "id": "test-addon-manager@jetpack",
+ "main": "./lib/main.js",
+ "name": "test-addon-manager",
+ "version": "0.0.1",
+ "author": "Erik Vold"
+}
diff --git a/addon-sdk/source/test/addons/author-email/main.js b/addon-sdk/source/test/addons/author-email/main.js
new file mode 100644
index 000000000..34786475a
--- /dev/null
+++ b/addon-sdk/source/test/addons/author-email/main.js
@@ -0,0 +1,14 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { id } = require('sdk/self');
+const { getAddonByID } = require('sdk/addon/manager');
+
+exports.testContributors = function*(assert) {
+ let addon = yield getAddonByID(id);
+ assert.equal(addon.creator.name, 'test <test@mozilla.com>', '< and > characters work');
+}
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/author-email/package.json b/addon-sdk/source/test/addons/author-email/package.json
new file mode 100644
index 000000000..2654ec431
--- /dev/null
+++ b/addon-sdk/source/test/addons/author-email/package.json
@@ -0,0 +1,6 @@
+{
+ "id": "test-addon-author-email@jetpack",
+ "author": "test <test@mozilla.com>",
+ "version": "0.0.1",
+ "main": "./main.js"
+}
diff --git a/addon-sdk/source/test/addons/child_process/index.js b/addon-sdk/source/test/addons/child_process/index.js
new file mode 100644
index 000000000..bf69e0380
--- /dev/null
+++ b/addon-sdk/source/test/addons/child_process/index.js
@@ -0,0 +1,39 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+/**
+ * Ensures using child_process and underlying subprocess.jsm
+ * works within an addon
+ */
+
+const { exec } = require("sdk/system/child_process");
+const { platform, pathFor } = require("sdk/system");
+const PROFILE_DIR = pathFor("ProfD");
+const isWindows = platform.toLowerCase().indexOf("win") === 0;
+const app = require("sdk/system/xul-app");
+
+// Once Bug 903018 is resolved, just move the application testing to
+// module.metadata.engines
+if (app.is("Firefox")) {
+ exports["test child_process in an addon"] = (assert, done) => {
+ exec(isWindows ? "DIR /A-D" : "ls -al", {
+ cwd: PROFILE_DIR
+ }, (err, stdout, stderr) => {
+ assert.equal(err, null, "no errors");
+ assert.equal(stderr, "", "stderr is empty");
+ assert.ok(/extensions\.ini/.test(stdout), "stdout output of `ls -al` finds files");
+
+ if (isWindows)
+ assert.ok(!/<DIR>/.test(stdout), "passing args works");
+ else
+ assert.ok(/d(r[-|w][-|x]){3}/.test(stdout), "passing args works");
+ done();
+ });
+ };
+} else {
+ exports["test unsupported"] = (assert) => assert.pass("This application is unsupported.");
+}
+require("sdk/test/runner").runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/child_process/package.json b/addon-sdk/source/test/addons/child_process/package.json
new file mode 100644
index 000000000..3b882d0c4
--- /dev/null
+++ b/addon-sdk/source/test/addons/child_process/package.json
@@ -0,0 +1,5 @@
+{
+ "id": "test-child-process@jetpack",
+ "main": "./index.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/chrome/chrome.manifest b/addon-sdk/source/test/addons/chrome/chrome.manifest
new file mode 100644
index 000000000..35e59a107
--- /dev/null
+++ b/addon-sdk/source/test/addons/chrome/chrome.manifest
@@ -0,0 +1,5 @@
+content test chrome/content/
+skin test classic/1.0 chrome/skin/
+
+locale test en-US chrome/locale/en-US/
+locale test ja-JP chrome/locale/ja-JP/
diff --git a/addon-sdk/source/test/addons/chrome/chrome/content/new-window.xul b/addon-sdk/source/test/addons/chrome/chrome/content/new-window.xul
new file mode 100644
index 000000000..25e219b80
--- /dev/null
+++ b/addon-sdk/source/test/addons/chrome/chrome/content/new-window.xul
@@ -0,0 +1,4 @@
+<?xml version="1.0"?>
+<dialog xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
+ windowtype="test:window">
+</dialog>
diff --git a/addon-sdk/source/test/addons/chrome/chrome/content/panel.html b/addon-sdk/source/test/addons/chrome/chrome/content/panel.html
new file mode 100644
index 000000000..595cc7455
--- /dev/null
+++ b/addon-sdk/source/test/addons/chrome/chrome/content/panel.html
@@ -0,0 +1,10 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="UTF-8">
+ </head>
+ <body></body>
+</html>
diff --git a/addon-sdk/source/test/addons/chrome/chrome/locale/en-US/description.properties b/addon-sdk/source/test/addons/chrome/chrome/locale/en-US/description.properties
new file mode 100644
index 000000000..148e2a127
--- /dev/null
+++ b/addon-sdk/source/test/addons/chrome/chrome/locale/en-US/description.properties
@@ -0,0 +1 @@
+test=Test
diff --git a/addon-sdk/source/test/addons/chrome/chrome/locale/ja-JP/description.properties b/addon-sdk/source/test/addons/chrome/chrome/locale/ja-JP/description.properties
new file mode 100644
index 000000000..cf01ac85b
--- /dev/null
+++ b/addon-sdk/source/test/addons/chrome/chrome/locale/ja-JP/description.properties
@@ -0,0 +1 @@
+test=テスト
diff --git a/addon-sdk/source/test/addons/chrome/chrome/skin/style.css b/addon-sdk/source/test/addons/chrome/chrome/skin/style.css
new file mode 100644
index 000000000..22abf3596
--- /dev/null
+++ b/addon-sdk/source/test/addons/chrome/chrome/skin/style.css
@@ -0,0 +1,4 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+test{}
diff --git a/addon-sdk/source/test/addons/chrome/data/panel.js b/addon-sdk/source/test/addons/chrome/data/panel.js
new file mode 100644
index 000000000..c38eca852
--- /dev/null
+++ b/addon-sdk/source/test/addons/chrome/data/panel.js
@@ -0,0 +1,10 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+self.port.on('echo', _ => {
+ self.port.emit('echo', '');
+});
+
+self.port.emit('start', '');
diff --git a/addon-sdk/source/test/addons/chrome/main.js b/addon-sdk/source/test/addons/chrome/main.js
new file mode 100644
index 000000000..84b822458
--- /dev/null
+++ b/addon-sdk/source/test/addons/chrome/main.js
@@ -0,0 +1,97 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict'
+
+const { Cu, Cc, Ci } = require('chrome');
+const Request = require('sdk/request').Request;
+const { WindowTracker } = require('sdk/deprecated/window-utils');
+const { close, open } = require('sdk/window/helpers');
+const { data } = require('sdk/self');
+const { Panel } = require('sdk/panel');
+
+const XUL_URL = 'chrome://test/content/new-window.xul'
+
+const { Services } = Cu.import('resource://gre/modules/Services.jsm', {});
+const { NetUtil } = Cu.import('resource://gre/modules/NetUtil.jsm', {});
+
+exports.testChromeSkin = function(assert, done) {
+ let skinURL = 'chrome://test/skin/style.css';
+
+ Request({
+ url: skinURL,
+ overrideMimeType: 'text/plain',
+ onComplete: function (response) {
+ assert.ok(/test\{\}\s*$/.test(response.text), 'chrome.manifest skin folder was registered!');
+ done();
+ }
+ }).get();
+
+ assert.pass('requesting ' + skinURL);
+}
+
+exports.testChromeContent = function(assert, done) {
+ let wt = WindowTracker({
+ onTrack: function(window) {
+ if (window.document.documentElement.getAttribute('windowtype') === 'test:window') {
+ assert.pass('test xul window was opened');
+ wt.unload();
+
+ close(window).then(done, assert.fail);
+ }
+ }
+ });
+
+ open(XUL_URL).then(
+ assert.pass.bind(assert, 'opened ' + XUL_URL),
+ assert.fail);
+
+ assert.pass('opening ' + XUL_URL);
+}
+
+exports.testChromeLocale = function(assert) {
+ let jpLocalePath = Cc['@mozilla.org/chrome/chrome-registry;1'].
+ getService(Ci.nsIChromeRegistry).
+ convertChromeURL(NetUtil.newURI('chrome://test/locale/description.properties')).
+ spec.replace(/(en\-US|ja\-JP)/, 'ja-JP');
+ let enLocalePath = jpLocalePath.replace(/ja\-JP/, 'en-US');
+
+ let jpStringBundle = Services.strings.createBundle(jpLocalePath);
+ assert.equal(jpStringBundle.GetStringFromName('test'),
+ 'テスト',
+ 'locales ja-JP folder was copied correctly');
+
+ let enStringBundle = Services.strings.createBundle(enLocalePath);
+ assert.equal(enStringBundle.GetStringFromName('test'),
+ 'Test',
+ 'locales en-US folder was copied correctly');
+}
+
+exports.testChromeInPanel = function*(assert) {
+ let panel = Panel({
+ contentURL: 'chrome://test/content/panel.html',
+ contentScriptWhen: 'end',
+ contentScriptFile: data.url('panel.js')
+ });
+
+ yield new Promise(resolve => panel.port.once('start', resolve));
+ assert.pass('start was emitted');
+
+ yield new Promise(resolve => {
+ panel.once('show', resolve);
+ panel.show();
+ });
+ assert.pass('panel shown');
+
+ yield new Promise(resolve => {
+ panel.port.once('echo', resolve);
+ panel.port.emit('echo');
+ });
+
+ assert.pass('got echo');
+
+ panel.destroy();
+ assert.pass('panel is destroyed');
+}
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/chrome/package.json b/addon-sdk/source/test/addons/chrome/package.json
new file mode 100644
index 000000000..82c6db899
--- /dev/null
+++ b/addon-sdk/source/test/addons/chrome/package.json
@@ -0,0 +1,5 @@
+{
+ "id": "test-chrome@jetpack",
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/content-permissions/httpd.js b/addon-sdk/source/test/addons/content-permissions/httpd.js
new file mode 100644
index 000000000..964dc9bbd
--- /dev/null
+++ b/addon-sdk/source/test/addons/content-permissions/httpd.js
@@ -0,0 +1,5211 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/*
+* NOTE: do not edit this file, this is copied from:
+* https://github.com/mozilla/addon-sdk/blob/master/test/lib/httpd.js
+*/
+
+module.metadata = {
+ "stability": "experimental"
+};
+
+const { components, CC, Cc, Ci, Cr, Cu } = require("chrome");
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+
+const PR_UINT32_MAX = Math.pow(2, 32) - 1;
+
+/** True if debugging output is enabled, false otherwise. */
+var DEBUG = false; // non-const *only* so tweakable in server tests
+
+/** True if debugging output should be timestamped. */
+var DEBUG_TIMESTAMP = false; // non-const so tweakable in server tests
+
+var gGlobalObject = Cc["@mozilla.org/systemprincipal;1"].createInstance();
+
+/**
+* Asserts that the given condition holds. If it doesn't, the given message is
+* dumped, a stack trace is printed, and an exception is thrown to attempt to
+* stop execution (which unfortunately must rely upon the exception not being
+* accidentally swallowed by the code that uses it).
+*/
+function NS_ASSERT(cond, msg)
+{
+ if (DEBUG && !cond)
+ {
+ dumpn("###!!!");
+ dumpn("###!!! ASSERTION" + (msg ? ": " + msg : "!"));
+ dumpn("###!!! Stack follows:");
+
+ var stack = new Error().stack.split(/\n/);
+ dumpn(stack.map(function(val) { return "###!!! " + val; }).join("\n"));
+
+ throw Cr.NS_ERROR_ABORT;
+ }
+}
+
+/** Constructs an HTTP error object. */
+function HttpError(code, description)
+{
+ this.code = code;
+ this.description = description;
+}
+HttpError.prototype =
+{
+ toString: function()
+ {
+ return this.code + " " + this.description;
+ }
+};
+
+/**
+* Errors thrown to trigger specific HTTP server responses.
+*/
+const HTTP_400 = new HttpError(400, "Bad Request");
+const HTTP_401 = new HttpError(401, "Unauthorized");
+const HTTP_402 = new HttpError(402, "Payment Required");
+const HTTP_403 = new HttpError(403, "Forbidden");
+const HTTP_404 = new HttpError(404, "Not Found");
+const HTTP_405 = new HttpError(405, "Method Not Allowed");
+const HTTP_406 = new HttpError(406, "Not Acceptable");
+const HTTP_407 = new HttpError(407, "Proxy Authentication Required");
+const HTTP_408 = new HttpError(408, "Request Timeout");
+const HTTP_409 = new HttpError(409, "Conflict");
+const HTTP_410 = new HttpError(410, "Gone");
+const HTTP_411 = new HttpError(411, "Length Required");
+const HTTP_412 = new HttpError(412, "Precondition Failed");
+const HTTP_413 = new HttpError(413, "Request Entity Too Large");
+const HTTP_414 = new HttpError(414, "Request-URI Too Long");
+const HTTP_415 = new HttpError(415, "Unsupported Media Type");
+const HTTP_417 = new HttpError(417, "Expectation Failed");
+
+const HTTP_500 = new HttpError(500, "Internal Server Error");
+const HTTP_501 = new HttpError(501, "Not Implemented");
+const HTTP_502 = new HttpError(502, "Bad Gateway");
+const HTTP_503 = new HttpError(503, "Service Unavailable");
+const HTTP_504 = new HttpError(504, "Gateway Timeout");
+const HTTP_505 = new HttpError(505, "HTTP Version Not Supported");
+
+/** Creates a hash with fields corresponding to the values in arr. */
+function array2obj(arr)
+{
+ var obj = {};
+ for (var i = 0; i < arr.length; i++)
+ obj[arr[i]] = arr[i];
+ return obj;
+}
+
+/** Returns an array of the integers x through y, inclusive. */
+function range(x, y)
+{
+ var arr = [];
+ for (var i = x; i <= y; i++)
+ arr.push(i);
+ return arr;
+}
+
+/** An object (hash) whose fields are the numbers of all HTTP error codes. */
+const HTTP_ERROR_CODES = array2obj(range(400, 417).concat(range(500, 505)));
+
+
+/**
+* The character used to distinguish hidden files from non-hidden files, a la
+* the leading dot in Apache. Since that mechanism also hides files from
+* easy display in LXR, ls output, etc. however, we choose instead to use a
+* suffix character. If a requested file ends with it, we append another
+* when getting the file on the server. If it doesn't, we just look up that
+* file. Therefore, any file whose name ends with exactly one of the character
+* is "hidden" and available for use by the server.
+*/
+const HIDDEN_CHAR = "^";
+
+/**
+* The file name suffix indicating the file containing overridden headers for
+* a requested file.
+*/
+const HEADERS_SUFFIX = HIDDEN_CHAR + "headers" + HIDDEN_CHAR;
+
+/** Type used to denote SJS scripts for CGI-like functionality. */
+const SJS_TYPE = "sjs";
+
+/** Base for relative timestamps produced by dumpn(). */
+var firstStamp = 0;
+
+/** dump(str) with a trailing "\n" -- only outputs if DEBUG. */
+function dumpn(str)
+{
+ if (DEBUG)
+ {
+ var prefix = "HTTPD-INFO | ";
+ if (DEBUG_TIMESTAMP)
+ {
+ if (firstStamp === 0)
+ firstStamp = Date.now();
+
+ var elapsed = Date.now() - firstStamp; // milliseconds
+ var min = Math.floor(elapsed / 60000);
+ var sec = (elapsed % 60000) / 1000;
+
+ if (sec < 10)
+ prefix += min + ":0" + sec.toFixed(3) + " | ";
+ else
+ prefix += min + ":" + sec.toFixed(3) + " | ";
+ }
+
+ dump(prefix + str + "\n");
+ }
+}
+
+/** Dumps the current JS stack if DEBUG. */
+function dumpStack()
+{
+ // peel off the frames for dumpStack() and Error()
+ var stack = new Error().stack.split(/\n/).slice(2);
+ stack.forEach(dumpn);
+}
+
+
+/** The XPCOM thread manager. */
+var gThreadManager = null;
+
+/** The XPCOM prefs service. */
+var gRootPrefBranch = null;
+function getRootPrefBranch()
+{
+ if (!gRootPrefBranch)
+ {
+ gRootPrefBranch = Cc["@mozilla.org/preferences-service;1"]
+ .getService(Ci.nsIPrefBranch);
+ }
+ return gRootPrefBranch;
+}
+
+/**
+* JavaScript constructors for commonly-used classes; precreating these is a
+* speedup over doing the same from base principles. See the docs at
+* http://developer.mozilla.org/en/docs/components.Constructor for details.
+*/
+const ServerSocket = CC("@mozilla.org/network/server-socket;1",
+ "nsIServerSocket",
+ "init");
+const ScriptableInputStream = CC("@mozilla.org/scriptableinputstream;1",
+ "nsIScriptableInputStream",
+ "init");
+const Pipe = CC("@mozilla.org/pipe;1",
+ "nsIPipe",
+ "init");
+const FileInputStream = CC("@mozilla.org/network/file-input-stream;1",
+ "nsIFileInputStream",
+ "init");
+const ConverterInputStream = CC("@mozilla.org/intl/converter-input-stream;1",
+ "nsIConverterInputStream",
+ "init");
+const WritablePropertyBag = CC("@mozilla.org/hash-property-bag;1",
+ "nsIWritablePropertyBag2");
+const SupportsString = CC("@mozilla.org/supports-string;1",
+ "nsISupportsString");
+
+/* These two are non-const only so a test can overwrite them. */
+var BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
+ "nsIBinaryInputStream",
+ "setInputStream");
+var BinaryOutputStream = CC("@mozilla.org/binaryoutputstream;1",
+ "nsIBinaryOutputStream",
+ "setOutputStream");
+
+/**
+* Returns the RFC 822/1123 representation of a date.
+*
+* @param date : Number
+* the date, in milliseconds from midnight (00:00:00), January 1, 1970 GMT
+* @returns string
+* the representation of the given date
+*/
+function toDateString(date)
+{
+ //
+ // rfc1123-date = wkday "," SP date1 SP time SP "GMT"
+ // date1 = 2DIGIT SP month SP 4DIGIT
+ // ; day month year (e.g., 02 Jun 1982)
+ // time = 2DIGIT ":" 2DIGIT ":" 2DIGIT
+ // ; 00:00:00 - 23:59:59
+ // wkday = "Mon" | "Tue" | "Wed"
+ // | "Thu" | "Fri" | "Sat" | "Sun"
+ // month = "Jan" | "Feb" | "Mar" | "Apr"
+ // | "May" | "Jun" | "Jul" | "Aug"
+ // | "Sep" | "Oct" | "Nov" | "Dec"
+ //
+
+ const wkdayStrings = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
+ const monthStrings = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
+
+ /**
+* Processes a date and returns the encoded UTC time as a string according to
+* the format specified in RFC 2616.
+*
+* @param date : Date
+* the date to process
+* @returns string
+* a string of the form "HH:MM:SS", ranging from "00:00:00" to "23:59:59"
+*/
+ function toTime(date)
+ {
+ var hrs = date.getUTCHours();
+ var rv = (hrs < 10) ? "0" + hrs : hrs;
+
+ var mins = date.getUTCMinutes();
+ rv += ":";
+ rv += (mins < 10) ? "0" + mins : mins;
+
+ var secs = date.getUTCSeconds();
+ rv += ":";
+ rv += (secs < 10) ? "0" + secs : secs;
+
+ return rv;
+ }
+
+ /**
+* Processes a date and returns the encoded UTC date as a string according to
+* the date1 format specified in RFC 2616.
+*
+* @param date : Date
+* the date to process
+* @returns string
+* a string of the form "HH:MM:SS", ranging from "00:00:00" to "23:59:59"
+*/
+ function toDate1(date)
+ {
+ var day = date.getUTCDate();
+ var month = date.getUTCMonth();
+ var year = date.getUTCFullYear();
+
+ var rv = (day < 10) ? "0" + day : day;
+ rv += " " + monthStrings[month];
+ rv += " " + year;
+
+ return rv;
+ }
+
+ date = new Date(date);
+
+ const fmtString = "%wkday%, %date1% %time% GMT";
+ var rv = fmtString.replace("%wkday%", wkdayStrings[date.getUTCDay()]);
+ rv = rv.replace("%time%", toTime(date));
+ return rv.replace("%date1%", toDate1(date));
+}
+
+/**
+* Prints out a human-readable representation of the object o and its fields,
+* omitting those whose names begin with "_" if showMembers != true (to ignore
+* "private" properties exposed via getters/setters).
+*/
+function printObj(o, showMembers)
+{
+ var s = "******************************\n";
+ s += "o = {\n";
+ for (var i in o)
+ {
+ if (typeof(i) != "string" ||
+ (showMembers || (i.length > 0 && i[0] != "_")))
+ s+= " " + i + ": " + o[i] + ",\n";
+ }
+ s += " };\n";
+ s += "******************************";
+ dumpn(s);
+}
+
+/**
+* Instantiates a new HTTP server.
+*/
+function nsHttpServer()
+{
+ if (!gThreadManager)
+ gThreadManager = Cc["@mozilla.org/thread-manager;1"].getService();
+
+ /** The port on which this server listens. */
+ this._port = undefined;
+
+ /** The socket associated with this. */
+ this._socket = null;
+
+ /** The handler used to process requests to this server. */
+ this._handler = new ServerHandler(this);
+
+ /** Naming information for this server. */
+ this._identity = new ServerIdentity();
+
+ /**
+* Indicates when the server is to be shut down at the end of the request.
+*/
+ this._doQuit = false;
+
+ /**
+* True if the socket in this is closed (and closure notifications have been
+* sent and processed if the socket was ever opened), false otherwise.
+*/
+ this._socketClosed = true;
+
+ /**
+* Used for tracking existing connections and ensuring that all connections
+* are properly cleaned up before server shutdown; increases by 1 for every
+* new incoming connection.
+*/
+ this._connectionGen = 0;
+
+ /**
+* Hash of all open connections, indexed by connection number at time of
+* creation.
+*/
+ this._connections = {};
+}
+nsHttpServer.prototype =
+{
+ classID: components.ID("{54ef6f81-30af-4b1d-ac55-8ba811293e41}"),
+
+ // NSISERVERSOCKETLISTENER
+
+ /**
+* Processes an incoming request coming in on the given socket and contained
+* in the given transport.
+*
+* @param socket : nsIServerSocket
+* the socket through which the request was served
+* @param trans : nsISocketTransport
+* the transport for the request/response
+* @see nsIServerSocketListener.onSocketAccepted
+*/
+ onSocketAccepted: function(socket, trans)
+ {
+ dumpn("*** onSocketAccepted(socket=" + socket + ", trans=" + trans + ")");
+
+ dumpn(">>> new connection on " + trans.host + ":" + trans.port);
+
+ const SEGMENT_SIZE = 8192;
+ const SEGMENT_COUNT = 1024;
+ try
+ {
+ var input = trans.openInputStream(0, SEGMENT_SIZE, SEGMENT_COUNT)
+ .QueryInterface(Ci.nsIAsyncInputStream);
+ var output = trans.openOutputStream(0, 0, 0);
+ }
+ catch (e)
+ {
+ dumpn("*** error opening transport streams: " + e);
+ trans.close(Cr.NS_BINDING_ABORTED);
+ return;
+ }
+
+ var connectionNumber = ++this._connectionGen;
+
+ try
+ {
+ var conn = new Connection(input, output, this, socket.port, trans.port,
+ connectionNumber);
+ var reader = new RequestReader(conn);
+
+ // XXX add request timeout functionality here!
+
+ // Note: must use main thread here, or we might get a GC that will cause
+ // threadsafety assertions. We really need to fix XPConnect so that
+ // you can actually do things in multi-threaded JS. :-(
+ input.asyncWait(reader, 0, 0, gThreadManager.mainThread);
+ }
+ catch (e)
+ {
+ // Assume this connection can't be salvaged and bail on it completely;
+ // don't attempt to close it so that we can assert that any connection
+ // being closed is in this._connections.
+ dumpn("*** error in initial request-processing stages: " + e);
+ trans.close(Cr.NS_BINDING_ABORTED);
+ return;
+ }
+
+ this._connections[connectionNumber] = conn;
+ dumpn("*** starting connection " + connectionNumber);
+ },
+
+ /**
+* Called when the socket associated with this is closed.
+*
+* @param socket : nsIServerSocket
+* the socket being closed
+* @param status : nsresult
+* the reason the socket stopped listening (NS_BINDING_ABORTED if the server
+* was stopped using nsIHttpServer.stop)
+* @see nsIServerSocketListener.onStopListening
+*/
+ onStopListening: function(socket, status)
+ {
+ dumpn(">>> shutting down server on port " + socket.port);
+ this._socketClosed = true;
+ if (!this._hasOpenConnections())
+ {
+ dumpn("*** no open connections, notifying async from onStopListening");
+
+ // Notify asynchronously so that any pending teardown in stop() has a
+ // chance to run first.
+ var self = this;
+ var stopEvent =
+ {
+ run: function()
+ {
+ dumpn("*** _notifyStopped async callback");
+ self._notifyStopped();
+ }
+ };
+ gThreadManager.currentThread
+ .dispatch(stopEvent, Ci.nsIThread.DISPATCH_NORMAL);
+ }
+ },
+
+ // NSIHTTPSERVER
+
+ //
+ // see nsIHttpServer.start
+ //
+ start: function(port)
+ {
+ this._start(port, "localhost")
+ },
+
+ _start: function(port, host)
+ {
+ if (this._socket)
+ throw Cr.NS_ERROR_ALREADY_INITIALIZED;
+
+ this._port = port;
+ this._doQuit = this._socketClosed = false;
+
+ this._host = host;
+
+ // The listen queue needs to be long enough to handle
+ // network.http.max-persistent-connections-per-server concurrent connections,
+ // plus a safety margin in case some other process is talking to
+ // the server as well.
+ var prefs = getRootPrefBranch();
+ var maxConnections;
+ try {
+ // Bug 776860: The original pref was removed in favor of this new one:
+ maxConnections = prefs.getIntPref("network.http.max-persistent-connections-per-server") + 5;
+ }
+ catch(e) {
+ maxConnections = prefs.getIntPref("network.http.max-connections-per-server") + 5;
+ }
+
+ try
+ {
+ var loopback = true;
+ if (this._host != "127.0.0.1" && this._host != "localhost") {
+ var loopback = false;
+ }
+
+ var socket = new ServerSocket(this._port,
+ loopback, // true = localhost, false = everybody
+ maxConnections);
+ dumpn(">>> listening on port " + socket.port + ", " + maxConnections +
+ " pending connections");
+ socket.asyncListen(this);
+ this._identity._initialize(socket.port, host, true);
+ this._socket = socket;
+ }
+ catch (e)
+ {
+ dumpn("!!! could not start server on port " + port + ": " + e);
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ }
+ },
+
+ //
+ // see nsIHttpServer.stop
+ //
+ stop: function(callback)
+ {
+ if (!callback)
+ throw Cr.NS_ERROR_NULL_POINTER;
+ if (!this._socket)
+ throw Cr.NS_ERROR_UNEXPECTED;
+
+ this._stopCallback = typeof callback === "function"
+ ? callback
+ : function() { callback.onStopped(); };
+
+ dumpn(">>> stopping listening on port " + this._socket.port);
+ this._socket.close();
+ this._socket = null;
+
+ // We can't have this identity any more, and the port on which we're running
+ // this server now could be meaningless the next time around.
+ this._identity._teardown();
+
+ this._doQuit = false;
+
+ // socket-close notification and pending request completion happen async
+ },
+
+ //
+ // see nsIHttpServer.registerFile
+ //
+ registerFile: function(path, file)
+ {
+ if (file && (!file.exists() || file.isDirectory()))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ this._handler.registerFile(path, file);
+ },
+
+ //
+ // see nsIHttpServer.registerDirectory
+ //
+ registerDirectory: function(path, directory)
+ {
+ // XXX true path validation!
+ if (path.charAt(0) != "/" ||
+ path.charAt(path.length - 1) != "/" ||
+ (directory &&
+ (!directory.exists() || !directory.isDirectory())))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ // XXX determine behavior of nonexistent /foo/bar when a /foo/bar/ mapping
+ // exists!
+
+ this._handler.registerDirectory(path, directory);
+ },
+
+ //
+ // see nsIHttpServer.registerPathHandler
+ //
+ registerPathHandler: function(path, handler)
+ {
+ this._handler.registerPathHandler(path, handler);
+ },
+
+ //
+ // see nsIHttpServer.registerPrefixHandler
+ //
+ registerPrefixHandler: function(prefix, handler)
+ {
+ this._handler.registerPrefixHandler(prefix, handler);
+ },
+
+ //
+ // see nsIHttpServer.registerErrorHandler
+ //
+ registerErrorHandler: function(code, handler)
+ {
+ this._handler.registerErrorHandler(code, handler);
+ },
+
+ //
+ // see nsIHttpServer.setIndexHandler
+ //
+ setIndexHandler: function(handler)
+ {
+ this._handler.setIndexHandler(handler);
+ },
+
+ //
+ // see nsIHttpServer.registerContentType
+ //
+ registerContentType: function(ext, type)
+ {
+ this._handler.registerContentType(ext, type);
+ },
+
+ //
+ // see nsIHttpServer.serverIdentity
+ //
+ get identity()
+ {
+ return this._identity;
+ },
+
+ //
+ // see nsIHttpServer.getState
+ //
+ getState: function(path, k)
+ {
+ return this._handler._getState(path, k);
+ },
+
+ //
+ // see nsIHttpServer.setState
+ //
+ setState: function(path, k, v)
+ {
+ return this._handler._setState(path, k, v);
+ },
+
+ //
+ // see nsIHttpServer.getSharedState
+ //
+ getSharedState: function(k)
+ {
+ return this._handler._getSharedState(k);
+ },
+
+ //
+ // see nsIHttpServer.setSharedState
+ //
+ setSharedState: function(k, v)
+ {
+ return this._handler._setSharedState(k, v);
+ },
+
+ //
+ // see nsIHttpServer.getObjectState
+ //
+ getObjectState: function(k)
+ {
+ return this._handler._getObjectState(k);
+ },
+
+ //
+ // see nsIHttpServer.setObjectState
+ //
+ setObjectState: function(k, v)
+ {
+ return this._handler._setObjectState(k, v);
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIServerSocketListener) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // NON-XPCOM PUBLIC API
+
+ /**
+* Returns true iff this server is not running (and is not in the process of
+* serving any requests still to be processed when the server was last
+* stopped after being run).
+*/
+ isStopped: function()
+ {
+ return this._socketClosed && !this._hasOpenConnections();
+ },
+
+ // PRIVATE IMPLEMENTATION
+
+ /** True if this server has any open connections to it, false otherwise. */
+ _hasOpenConnections: function()
+ {
+ //
+ // If we have any open connections, they're tracked as numeric properties on
+ // |this._connections|. The non-standard __count__ property could be used
+ // to check whether there are any properties, but standard-wise, even
+ // looking forward to ES5, there's no less ugly yet still O(1) way to do
+ // this.
+ //
+ for (var n in this._connections)
+ return true;
+ return false;
+ },
+
+ /** Calls the server-stopped callback provided when stop() was called. */
+ _notifyStopped: function()
+ {
+ NS_ASSERT(this._stopCallback !== null, "double-notifying?");
+ NS_ASSERT(!this._hasOpenConnections(), "should be done serving by now");
+
+ //
+ // NB: We have to grab this now, null out the member, *then* call the
+ // callback here, or otherwise the callback could (indirectly) futz with
+ // this._stopCallback by starting and immediately stopping this, at
+ // which point we'd be nulling out a field we no longer have a right to
+ // modify.
+ //
+ var callback = this._stopCallback;
+ this._stopCallback = null;
+ try
+ {
+ callback();
+ }
+ catch (e)
+ {
+ // not throwing because this is specified as being usually (but not
+ // always) asynchronous
+ dump("!!! error running onStopped callback: " + e + "\n");
+ }
+ },
+
+ /**
+* Notifies this server that the given connection has been closed.
+*
+* @param connection : Connection
+* the connection that was closed
+*/
+ _connectionClosed: function(connection)
+ {
+ NS_ASSERT(connection.number in this._connections,
+ "closing a connection " + this + " that we never added to the " +
+ "set of open connections?");
+ NS_ASSERT(this._connections[connection.number] === connection,
+ "connection number mismatch? " +
+ this._connections[connection.number]);
+ delete this._connections[connection.number];
+
+ // Fire a pending server-stopped notification if it's our responsibility.
+ if (!this._hasOpenConnections() && this._socketClosed)
+ this._notifyStopped();
+ },
+
+ /**
+* Requests that the server be shut down when possible.
+*/
+ _requestQuit: function()
+ {
+ dumpn(">>> requesting a quit");
+ dumpStack();
+ this._doQuit = true;
+ }
+};
+
+
+//
+// RFC 2396 section 3.2.2:
+//
+// host = hostname | IPv4address
+// hostname = *( domainlabel "." ) toplabel [ "." ]
+// domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
+// toplabel = alpha | alpha *( alphanum | "-" ) alphanum
+// IPv4address = 1*digit "." 1*digit "." 1*digit "." 1*digit
+//
+
+const HOST_REGEX =
+ new RegExp("^(?:" +
+ // *( domainlabel "." )
+ "(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)*" +
+ // toplabel
+ "[a-z](?:[a-z0-9-]*[a-z0-9])?" +
+ "|" +
+ // IPv4 address
+ "\\d+\\.\\d+\\.\\d+\\.\\d+" +
+ ")$",
+ "i");
+
+
+/**
+* Represents the identity of a server. An identity consists of a set of
+* (scheme, host, port) tuples denoted as locations (allowing a single server to
+* serve multiple sites or to be used behind both HTTP and HTTPS proxies for any
+* host/port). Any incoming request must be to one of these locations, or it
+* will be rejected with an HTTP 400 error. One location, denoted as the
+* primary location, is the location assigned in contexts where a location
+* cannot otherwise be endogenously derived, such as for HTTP/1.0 requests.
+*
+* A single identity may contain at most one location per unique host/port pair;
+* other than that, no restrictions are placed upon what locations may
+* constitute an identity.
+*/
+function ServerIdentity()
+{
+ /** The scheme of the primary location. */
+ this._primaryScheme = "http";
+
+ /** The hostname of the primary location. */
+ this._primaryHost = "127.0.0.1"
+
+ /** The port number of the primary location. */
+ this._primaryPort = -1;
+
+ /**
+* The current port number for the corresponding server, stored so that a new
+* primary location can always be set if the current one is removed.
+*/
+ this._defaultPort = -1;
+
+ /**
+* Maps hosts to maps of ports to schemes, e.g. the following would represent
+* https://example.com:789/ and http://example.org/:
+*
+* {
+* "xexample.com": { 789: "https" },
+* "xexample.org": { 80: "http" }
+* }
+*
+* Note the "x" prefix on hostnames, which prevents collisions with special
+* JS names like "prototype".
+*/
+ this._locations = { "xlocalhost": {} };
+}
+ServerIdentity.prototype =
+{
+ // NSIHTTPSERVERIDENTITY
+
+ //
+ // see nsIHttpServerIdentity.primaryScheme
+ //
+ get primaryScheme()
+ {
+ if (this._primaryPort === -1)
+ throw Cr.NS_ERROR_NOT_INITIALIZED;
+ return this._primaryScheme;
+ },
+
+ //
+ // see nsIHttpServerIdentity.primaryHost
+ //
+ get primaryHost()
+ {
+ if (this._primaryPort === -1)
+ throw Cr.NS_ERROR_NOT_INITIALIZED;
+ return this._primaryHost;
+ },
+
+ //
+ // see nsIHttpServerIdentity.primaryPort
+ //
+ get primaryPort()
+ {
+ if (this._primaryPort === -1)
+ throw Cr.NS_ERROR_NOT_INITIALIZED;
+ return this._primaryPort;
+ },
+
+ //
+ // see nsIHttpServerIdentity.add
+ //
+ add: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ var entry = this._locations["x" + host];
+ if (!entry)
+ this._locations["x" + host] = entry = {};
+
+ entry[port] = scheme;
+ },
+
+ //
+ // see nsIHttpServerIdentity.remove
+ //
+ remove: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ var entry = this._locations["x" + host];
+ if (!entry)
+ return false;
+
+ var present = port in entry;
+ delete entry[port];
+
+ if (this._primaryScheme == scheme &&
+ this._primaryHost == host &&
+ this._primaryPort == port &&
+ this._defaultPort !== -1)
+ {
+ // Always keep at least one identity in existence at any time, unless
+ // we're in the process of shutting down (the last condition above).
+ this._primaryPort = -1;
+ this._initialize(this._defaultPort, host, false);
+ }
+
+ return present;
+ },
+
+ //
+ // see nsIHttpServerIdentity.has
+ //
+ has: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ return "x" + host in this._locations &&
+ scheme === this._locations["x" + host][port];
+ },
+
+ //
+ // see nsIHttpServerIdentity.has
+ //
+ getScheme: function(host, port)
+ {
+ this._validate("http", host, port);
+
+ var entry = this._locations["x" + host];
+ if (!entry)
+ return "";
+
+ return entry[port] || "";
+ },
+
+ //
+ // see nsIHttpServerIdentity.setPrimary
+ //
+ setPrimary: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ this.add(scheme, host, port);
+
+ this._primaryScheme = scheme;
+ this._primaryHost = host;
+ this._primaryPort = port;
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIHttpServerIdentity) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // PRIVATE IMPLEMENTATION
+
+ /**
+* Initializes the primary name for the corresponding server, based on the
+* provided port number.
+*/
+ _initialize: function(port, host, addSecondaryDefault)
+ {
+ this._host = host;
+ if (this._primaryPort !== -1)
+ this.add("http", host, port);
+ else
+ this.setPrimary("http", "localhost", port);
+ this._defaultPort = port;
+
+ // Only add this if we're being called at server startup
+ if (addSecondaryDefault && host != "127.0.0.1")
+ this.add("http", "127.0.0.1", port);
+ },
+
+ /**
+* Called at server shutdown time, unsets the primary location only if it was
+* the default-assigned location and removes the default location from the
+* set of locations used.
+*/
+ _teardown: function()
+ {
+ if (this._host != "127.0.0.1") {
+ // Not the default primary location, nothing special to do here
+ this.remove("http", "127.0.0.1", this._defaultPort);
+ }
+
+ // This is a *very* tricky bit of reasoning here; make absolutely sure the
+ // tests for this code pass before you commit changes to it.
+ if (this._primaryScheme == "http" &&
+ this._primaryHost == this._host &&
+ this._primaryPort == this._defaultPort)
+ {
+ // Make sure we don't trigger the readding logic in .remove(), then remove
+ // the default location.
+ var port = this._defaultPort;
+ this._defaultPort = -1;
+ this.remove("http", this._host, port);
+
+ // Ensure a server start triggers the setPrimary() path in ._initialize()
+ this._primaryPort = -1;
+ }
+ else
+ {
+ // No reason not to remove directly as it's not our primary location
+ this.remove("http", this._host, this._defaultPort);
+ }
+ },
+
+ /**
+* Ensures scheme, host, and port are all valid with respect to RFC 2396.
+*
+* @throws NS_ERROR_ILLEGAL_VALUE
+* if any argument doesn't match the corresponding production
+*/
+ _validate: function(scheme, host, port)
+ {
+ if (scheme !== "http" && scheme !== "https")
+ {
+ dumpn("*** server only supports http/https schemes: '" + scheme + "'");
+ dumpStack();
+ throw Cr.NS_ERROR_ILLEGAL_VALUE;
+ }
+ if (!HOST_REGEX.test(host))
+ {
+ dumpn("*** unexpected host: '" + host + "'");
+ throw Cr.NS_ERROR_ILLEGAL_VALUE;
+ }
+ if (port < 0 || port > 65535)
+ {
+ dumpn("*** unexpected port: '" + port + "'");
+ throw Cr.NS_ERROR_ILLEGAL_VALUE;
+ }
+ }
+};
+
+
+/**
+* Represents a connection to the server (and possibly in the future the thread
+* on which the connection is processed).
+*
+* @param input : nsIInputStream
+* stream from which incoming data on the connection is read
+* @param output : nsIOutputStream
+* stream to write data out the connection
+* @param server : nsHttpServer
+* the server handling the connection
+* @param port : int
+* the port on which the server is running
+* @param outgoingPort : int
+* the outgoing port used by this connection
+* @param number : uint
+* a serial number used to uniquely identify this connection
+*/
+function Connection(input, output, server, port, outgoingPort, number)
+{
+ dumpn("*** opening new connection " + number + " on port " + outgoingPort);
+
+ /** Stream of incoming data. */
+ this.input = input;
+
+ /** Stream for outgoing data. */
+ this.output = output;
+
+ /** The server associated with this request. */
+ this.server = server;
+
+ /** The port on which the server is running. */
+ this.port = port;
+
+ /** The outgoing poort used by this connection. */
+ this._outgoingPort = outgoingPort;
+
+ /** The serial number of this connection. */
+ this.number = number;
+
+ /**
+* The request for which a response is being generated, null if the
+* incoming request has not been fully received or if it had errors.
+*/
+ this.request = null;
+
+ /** State variables for debugging. */
+ this._closed = this._processed = false;
+}
+Connection.prototype =
+{
+ /** Closes this connection's input/output streams. */
+ close: function()
+ {
+ dumpn("*** closing connection " + this.number +
+ " on port " + this._outgoingPort);
+
+ this.input.close();
+ this.output.close();
+ this._closed = true;
+
+ var server = this.server;
+ server._connectionClosed(this);
+
+ // If an error triggered a server shutdown, act on it now
+ if (server._doQuit)
+ server.stop(function() { /* not like we can do anything better */ });
+ },
+
+ /**
+* Initiates processing of this connection, using the data in the given
+* request.
+*
+* @param request : Request
+* the request which should be processed
+*/
+ process: function(request)
+ {
+ NS_ASSERT(!this._closed && !this._processed);
+
+ this._processed = true;
+
+ this.request = request;
+ this.server._handler.handleResponse(this);
+ },
+
+ /**
+* Initiates processing of this connection, generating a response with the
+* given HTTP error code.
+*
+* @param code : uint
+* an HTTP code, so in the range [0, 1000)
+* @param request : Request
+* incomplete data about the incoming request (since there were errors
+* during its processing
+*/
+ processError: function(code, request)
+ {
+ NS_ASSERT(!this._closed && !this._processed);
+
+ this._processed = true;
+ this.request = request;
+ this.server._handler.handleError(code, this);
+ },
+
+ /** Converts this to a string for debugging purposes. */
+ toString: function()
+ {
+ return "<Connection(" + this.number +
+ (this.request ? ", " + this.request.path : "") +"): " +
+ (this._closed ? "closed" : "open") + ">";
+ }
+};
+
+
+
+/** Returns an array of count bytes from the given input stream. */
+function readBytes(inputStream, count)
+{
+ return new BinaryInputStream(inputStream).readByteArray(count);
+}
+
+
+
+/** Request reader processing states; see RequestReader for details. */
+const READER_IN_REQUEST_LINE = 0;
+const READER_IN_HEADERS = 1;
+const READER_IN_BODY = 2;
+const READER_FINISHED = 3;
+
+
+/**
+* Reads incoming request data asynchronously, does any necessary preprocessing,
+* and forwards it to the request handler. Processing occurs in three states:
+*
+* READER_IN_REQUEST_LINE Reading the request's status line
+* READER_IN_HEADERS Reading headers in the request
+* READER_IN_BODY Reading the body of the request
+* READER_FINISHED Entire request has been read and processed
+*
+* During the first two stages, initial metadata about the request is gathered
+* into a Request object. Once the status line and headers have been processed,
+* we start processing the body of the request into the Request. Finally, when
+* the entire body has been read, we create a Response and hand it off to the
+* ServerHandler to be given to the appropriate request handler.
+*
+* @param connection : Connection
+* the connection for the request being read
+*/
+function RequestReader(connection)
+{
+ /** Connection metadata for this request. */
+ this._connection = connection;
+
+ /**
+* A container providing line-by-line access to the raw bytes that make up the
+* data which has been read from the connection but has not yet been acted
+* upon (by passing it to the request handler or by extracting request
+* metadata from it).
+*/
+ this._data = new LineData();
+
+ /**
+* The amount of data remaining to be read from the body of this request.
+* After all headers in the request have been read this is the value in the
+* Content-Length header, but as the body is read its value decreases to zero.
+*/
+ this._contentLength = 0;
+
+ /** The current state of parsing the incoming request. */
+ this._state = READER_IN_REQUEST_LINE;
+
+ /** Metadata constructed from the incoming request for the request handler. */
+ this._metadata = new Request(connection.port);
+
+ /**
+* Used to preserve state if we run out of line data midway through a
+* multi-line header. _lastHeaderName stores the name of the header, while
+* _lastHeaderValue stores the value we've seen so far for the header.
+*
+* These fields are always either both undefined or both strings.
+*/
+ this._lastHeaderName = this._lastHeaderValue = undefined;
+}
+RequestReader.prototype =
+{
+ // NSIINPUTSTREAMCALLBACK
+
+ /**
+* Called when more data from the incoming request is available. This method
+* then reads the available data from input and deals with that data as
+* necessary, depending upon the syntax of already-downloaded data.
+*
+* @param input : nsIAsyncInputStream
+* the stream of incoming data from the connection
+*/
+ onInputStreamReady: function(input)
+ {
+ dumpn("*** onInputStreamReady(input=" + input + ") on thread " +
+ gThreadManager.currentThread + " (main is " +
+ gThreadManager.mainThread + ")");
+ dumpn("*** this._state == " + this._state);
+
+ // Handle cases where we get more data after a request error has been
+ // discovered but *before* we can close the connection.
+ var data = this._data;
+ if (!data)
+ return;
+
+ try
+ {
+ data.appendBytes(readBytes(input, input.available()));
+ }
+ catch (e)
+ {
+ if (streamClosed(e))
+ {
+ dumpn("*** WARNING: unexpected error when reading from socket; will " +
+ "be treated as if the input stream had been closed");
+ dumpn("*** WARNING: actual error was: " + e);
+ }
+
+ // We've lost a race -- input has been closed, but we're still expecting
+ // to read more data. available() will throw in this case, and since
+ // we're dead in the water now, destroy the connection.
+ dumpn("*** onInputStreamReady called on a closed input, destroying " +
+ "connection");
+ this._connection.close();
+ return;
+ }
+
+ switch (this._state)
+ {
+ default:
+ NS_ASSERT(false, "invalid state: " + this._state);
+ break;
+
+ case READER_IN_REQUEST_LINE:
+ if (!this._processRequestLine())
+ break;
+ /* fall through */
+
+ case READER_IN_HEADERS:
+ if (!this._processHeaders())
+ break;
+ /* fall through */
+
+ case READER_IN_BODY:
+ this._processBody();
+ }
+
+ if (this._state != READER_FINISHED)
+ input.asyncWait(this, 0, 0, gThreadManager.currentThread);
+ },
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(aIID)
+ {
+ if (aIID.equals(Ci.nsIInputStreamCallback) ||
+ aIID.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // PRIVATE API
+
+ /**
+* Processes unprocessed, downloaded data as a request line.
+*
+* @returns boolean
+* true iff the request line has been fully processed
+*/
+ _processRequestLine: function()
+ {
+ NS_ASSERT(this._state == READER_IN_REQUEST_LINE);
+
+ // Servers SHOULD ignore any empty line(s) received where a Request-Line
+ // is expected (section 4.1).
+ var data = this._data;
+ var line = {};
+ var readSuccess;
+ while ((readSuccess = data.readLine(line)) && line.value == "")
+ dumpn("*** ignoring beginning blank line...");
+
+ // if we don't have a full line, wait until we do
+ if (!readSuccess)
+ return false;
+
+ // we have the first non-blank line
+ try
+ {
+ this._parseRequestLine(line.value);
+ this._state = READER_IN_HEADERS;
+ return true;
+ }
+ catch (e)
+ {
+ this._handleError(e);
+ return false;
+ }
+ },
+
+ /**
+* Processes stored data, assuming it is either at the beginning or in
+* the middle of processing request headers.
+*
+* @returns boolean
+* true iff header data in the request has been fully processed
+*/
+ _processHeaders: function()
+ {
+ NS_ASSERT(this._state == READER_IN_HEADERS);
+
+ // XXX things to fix here:
+ //
+ // - need to support RFC 2047-encoded non-US-ASCII characters
+
+ try
+ {
+ var done = this._parseHeaders();
+ if (done)
+ {
+ var request = this._metadata;
+
+ // XXX this is wrong for requests with transfer-encodings applied to
+ // them, particularly chunked (which by its nature can have no
+ // meaningful Content-Length header)!
+ this._contentLength = request.hasHeader("Content-Length")
+ ? parseInt(request.getHeader("Content-Length"), 10)
+ : 0;
+ dumpn("_processHeaders, Content-length=" + this._contentLength);
+
+ this._state = READER_IN_BODY;
+ }
+ return done;
+ }
+ catch (e)
+ {
+ this._handleError(e);
+ return false;
+ }
+ },
+
+ /**
+* Processes stored data, assuming it is either at the beginning or in
+* the middle of processing the request body.
+*
+* @returns boolean
+* true iff the request body has been fully processed
+*/
+ _processBody: function()
+ {
+ NS_ASSERT(this._state == READER_IN_BODY);
+
+ // XXX handle chunked transfer-coding request bodies!
+
+ try
+ {
+ if (this._contentLength > 0)
+ {
+ var data = this._data.purge();
+ var count = Math.min(data.length, this._contentLength);
+ dumpn("*** loading data=" + data + " len=" + data.length +
+ " excess=" + (data.length - count));
+
+ var bos = new BinaryOutputStream(this._metadata._bodyOutputStream);
+ bos.writeByteArray(data, count);
+ this._contentLength -= count;
+ }
+
+ dumpn("*** remaining body data len=" + this._contentLength);
+ if (this._contentLength == 0)
+ {
+ this._validateRequest();
+ this._state = READER_FINISHED;
+ this._handleResponse();
+ return true;
+ }
+
+ return false;
+ }
+ catch (e)
+ {
+ this._handleError(e);
+ return false;
+ }
+ },
+
+ /**
+* Does various post-header checks on the data in this request.
+*
+* @throws : HttpError
+* if the request was malformed in some way
+*/
+ _validateRequest: function()
+ {
+ NS_ASSERT(this._state == READER_IN_BODY);
+
+ dumpn("*** _validateRequest");
+
+ var metadata = this._metadata;
+ var headers = metadata._headers;
+
+ // 19.6.1.1 -- servers MUST report 400 to HTTP/1.1 requests w/o Host header
+ var identity = this._connection.server.identity;
+ if (metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1))
+ {
+ if (!headers.hasHeader("Host"))
+ {
+ dumpn("*** malformed HTTP/1.1 or greater request with no Host header!");
+ throw HTTP_400;
+ }
+
+ // If the Request-URI wasn't absolute, then we need to determine our host.
+ // We have to determine what scheme was used to access us based on the
+ // server identity data at this point, because the request just doesn't
+ // contain enough data on its own to do this, sadly.
+ if (!metadata._host)
+ {
+ var host, port;
+ var hostPort = headers.getHeader("Host");
+ var colon = hostPort.indexOf(":");
+ if (colon < 0)
+ {
+ host = hostPort;
+ port = "";
+ }
+ else
+ {
+ host = hostPort.substring(0, colon);
+ port = hostPort.substring(colon + 1);
+ }
+
+ // NB: We allow an empty port here because, oddly, a colon may be
+ // present even without a port number, e.g. "example.com:"; in this
+ // case the default port applies.
+ if (!HOST_REGEX.test(host) || !/^\d*$/.test(port))
+ {
+ dumpn("*** malformed hostname (" + hostPort + ") in Host " +
+ "header, 400 time");
+ throw HTTP_400;
+ }
+
+ // If we're not given a port, we're stuck, because we don't know what
+ // scheme to use to look up the correct port here, in general. Since
+ // the HTTPS case requires a tunnel/proxy and thus requires that the
+ // requested URI be absolute (and thus contain the necessary
+ // information), let's assume HTTP will prevail and use that.
+ port = +port || 80;
+
+ var scheme = identity.getScheme(host, port);
+ if (!scheme)
+ {
+ dumpn("*** unrecognized hostname (" + hostPort + ") in Host " +
+ "header, 400 time");
+ throw HTTP_400;
+ }
+
+ metadata._scheme = scheme;
+ metadata._host = host;
+ metadata._port = port;
+ }
+ }
+ else
+ {
+ NS_ASSERT(metadata._host === undefined,
+ "HTTP/1.0 doesn't allow absolute paths in the request line!");
+
+ metadata._scheme = identity.primaryScheme;
+ metadata._host = identity.primaryHost;
+ metadata._port = identity.primaryPort;
+ }
+
+ NS_ASSERT(identity.has(metadata._scheme, metadata._host, metadata._port),
+ "must have a location we recognize by now!");
+ },
+
+ /**
+* Handles responses in case of error, either in the server or in the request.
+*
+* @param e
+* the specific error encountered, which is an HttpError in the case where
+* the request is in some way invalid or cannot be fulfilled; if this isn't
+* an HttpError we're going to be paranoid and shut down, because that
+* shouldn't happen, ever
+*/
+ _handleError: function(e)
+ {
+ // Don't fall back into normal processing!
+ this._state = READER_FINISHED;
+
+ var server = this._connection.server;
+ if (e instanceof HttpError)
+ {
+ var code = e.code;
+ }
+ else
+ {
+ dumpn("!!! UNEXPECTED ERROR: " + e +
+ (e.lineNumber ? ", line " + e.lineNumber : ""));
+
+ // no idea what happened -- be paranoid and shut down
+ code = 500;
+ server._requestQuit();
+ }
+
+ // make attempted reuse of data an error
+ this._data = null;
+
+ this._connection.processError(code, this._metadata);
+ },
+
+ /**
+* Now that we've read the request line and headers, we can actually hand off
+* the request to be handled.
+*
+* This method is called once per request, after the request line and all
+* headers and the body, if any, have been received.
+*/
+ _handleResponse: function()
+ {
+ NS_ASSERT(this._state == READER_FINISHED);
+
+ // We don't need the line-based data any more, so make attempted reuse an
+ // error.
+ this._data = null;
+
+ this._connection.process(this._metadata);
+ },
+
+
+ // PARSING
+
+ /**
+* Parses the request line for the HTTP request associated with this.
+*
+* @param line : string
+* the request line
+*/
+ _parseRequestLine: function(line)
+ {
+ NS_ASSERT(this._state == READER_IN_REQUEST_LINE);
+
+ dumpn("*** _parseRequestLine('" + line + "')");
+
+ var metadata = this._metadata;
+
+ // clients and servers SHOULD accept any amount of SP or HT characters
+ // between fields, even though only a single SP is required (section 19.3)
+ var request = line.split(/[ \t]+/);
+ if (!request || request.length != 3)
+ throw HTTP_400;
+
+ metadata._method = request[0];
+
+ // get the HTTP version
+ var ver = request[2];
+ var match = ver.match(/^HTTP\/(\d+\.\d+)$/);
+ if (!match)
+ throw HTTP_400;
+
+ // determine HTTP version
+ try
+ {
+ metadata._httpVersion = new nsHttpVersion(match[1]);
+ if (!metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_0))
+ throw "unsupported HTTP version";
+ }
+ catch (e)
+ {
+ // we support HTTP/1.0 and HTTP/1.1 only
+ throw HTTP_501;
+ }
+
+
+ var fullPath = request[1];
+ var serverIdentity = this._connection.server.identity;
+
+ var scheme, host, port;
+
+ if (fullPath.charAt(0) != "/")
+ {
+ // No absolute paths in the request line in HTTP prior to 1.1
+ if (!metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1))
+ throw HTTP_400;
+
+ try
+ {
+ var uri = Cc["@mozilla.org/network/io-service;1"]
+ .getService(Ci.nsIIOService)
+ .newURI(fullPath, null, null);
+ fullPath = uri.path;
+ scheme = uri.scheme;
+ host = metadata._host = uri.asciiHost;
+ port = uri.port;
+ if (port === -1)
+ {
+ if (scheme === "http")
+ port = 80;
+ else if (scheme === "https")
+ port = 443;
+ else
+ throw HTTP_400;
+ }
+ }
+ catch (e)
+ {
+ // If the host is not a valid host on the server, the response MUST be a
+ // 400 (Bad Request) error message (section 5.2). Alternately, the URI
+ // is malformed.
+ throw HTTP_400;
+ }
+
+ if (!serverIdentity.has(scheme, host, port) || fullPath.charAt(0) != "/")
+ throw HTTP_400;
+ }
+
+ var splitter = fullPath.indexOf("?");
+ if (splitter < 0)
+ {
+ // _queryString already set in ctor
+ metadata._path = fullPath;
+ }
+ else
+ {
+ metadata._path = fullPath.substring(0, splitter);
+ metadata._queryString = fullPath.substring(splitter + 1);
+ }
+
+ metadata._scheme = scheme;
+ metadata._host = host;
+ metadata._port = port;
+ },
+
+ /**
+* Parses all available HTTP headers in this until the header-ending CRLFCRLF,
+* adding them to the store of headers in the request.
+*
+* @throws
+* HTTP_400 if the headers are malformed
+* @returns boolean
+* true if all headers have now been processed, false otherwise
+*/
+ _parseHeaders: function()
+ {
+ NS_ASSERT(this._state == READER_IN_HEADERS);
+
+ dumpn("*** _parseHeaders");
+
+ var data = this._data;
+
+ var headers = this._metadata._headers;
+ var lastName = this._lastHeaderName;
+ var lastVal = this._lastHeaderValue;
+
+ var line = {};
+ while (true)
+ {
+ NS_ASSERT(!((lastVal === undefined) ^ (lastName === undefined)),
+ lastName === undefined ?
+ "lastVal without lastName? lastVal: '" + lastVal + "'" :
+ "lastName without lastVal? lastName: '" + lastName + "'");
+
+ if (!data.readLine(line))
+ {
+ // save any data we have from the header we might still be processing
+ this._lastHeaderName = lastName;
+ this._lastHeaderValue = lastVal;
+ return false;
+ }
+
+ var lineText = line.value;
+ var firstChar = lineText.charAt(0);
+
+ // blank line means end of headers
+ if (lineText == "")
+ {
+ // we're finished with the previous header
+ if (lastName)
+ {
+ try
+ {
+ headers.setHeader(lastName, lastVal, true);
+ }
+ catch (e)
+ {
+ dumpn("*** e == " + e);
+ throw HTTP_400;
+ }
+ }
+ else
+ {
+ // no headers in request -- valid for HTTP/1.0 requests
+ }
+
+ // either way, we're done processing headers
+ this._state = READER_IN_BODY;
+ return true;
+ }
+ else if (firstChar == " " || firstChar == "\t")
+ {
+ // multi-line header if we've already seen a header line
+ if (!lastName)
+ {
+ // we don't have a header to continue!
+ throw HTTP_400;
+ }
+
+ // append this line's text to the value; starts with SP/HT, so no need
+ // for separating whitespace
+ lastVal += lineText;
+ }
+ else
+ {
+ // we have a new header, so set the old one (if one existed)
+ if (lastName)
+ {
+ try
+ {
+ headers.setHeader(lastName, lastVal, true);
+ }
+ catch (e)
+ {
+ dumpn("*** e == " + e);
+ throw HTTP_400;
+ }
+ }
+
+ var colon = lineText.indexOf(":"); // first colon must be splitter
+ if (colon < 1)
+ {
+ // no colon or missing header field-name
+ throw HTTP_400;
+ }
+
+ // set header name, value (to be set in the next loop, usually)
+ lastName = lineText.substring(0, colon);
+ lastVal = lineText.substring(colon + 1);
+ } // empty, continuation, start of header
+ } // while (true)
+ }
+};
+
+
+/** The character codes for CR and LF. */
+const CR = 0x0D, LF = 0x0A;
+
+/**
+* Calculates the number of characters before the first CRLF pair in array, or
+* -1 if the array contains no CRLF pair.
+*
+* @param array : Array
+* an array of numbers in the range [0, 256), each representing a single
+* character; the first CRLF is the lowest index i where
+* |array[i] == "\r".charCodeAt(0)| and |array[i+1] == "\n".charCodeAt(0)|,
+* if such an |i| exists, and -1 otherwise
+* @returns int
+* the index of the first CRLF if any were present, -1 otherwise
+*/
+function findCRLF(array)
+{
+ for (var i = array.indexOf(CR); i >= 0; i = array.indexOf(CR, i + 1))
+ {
+ if (array[i + 1] == LF)
+ return i;
+ }
+ return -1;
+}
+
+
+/**
+* A container which provides line-by-line access to the arrays of bytes with
+* which it is seeded.
+*/
+function LineData()
+{
+ /** An array of queued bytes from which to get line-based characters. */
+ this._data = [];
+}
+LineData.prototype =
+{
+ /**
+* Appends the bytes in the given array to the internal data cache maintained
+* by this.
+*/
+ appendBytes: function(bytes)
+ {
+ Array.prototype.push.apply(this._data, bytes);
+ },
+
+ /**
+* Removes and returns a line of data, delimited by CRLF, from this.
+*
+* @param out
+* an object whose "value" property will be set to the first line of text
+* present in this, sans CRLF, if this contains a full CRLF-delimited line
+* of text; if this doesn't contain enough data, the value of the property
+* is undefined
+* @returns boolean
+* true if a full line of data could be read from the data in this, false
+* otherwise
+*/
+ readLine: function(out)
+ {
+ var data = this._data;
+ var length = findCRLF(data);
+ if (length < 0)
+ return false;
+
+ //
+ // We have the index of the CR, so remove all the characters, including
+ // CRLF, from the array with splice, and convert the removed array into the
+ // corresponding string, from which we then strip the trailing CRLF.
+ //
+ // Getting the line in this matter acknowledges that substring is an O(1)
+ // operation in SpiderMonkey because strings are immutable, whereas two
+ // splices, both from the beginning of the data, are less likely to be as
+ // cheap as a single splice plus two extra character conversions.
+ //
+ var line = String.fromCharCode.apply(null, data.splice(0, length + 2));
+ out.value = line.substring(0, length);
+
+ return true;
+ },
+
+ /**
+* Removes the bytes currently within this and returns them in an array.
+*
+* @returns Array
+* the bytes within this when this method is called
+*/
+ purge: function()
+ {
+ var data = this._data;
+ this._data = [];
+ return data;
+ }
+};
+
+
+
+/**
+* Creates a request-handling function for an nsIHttpRequestHandler object.
+*/
+function createHandlerFunc(handler)
+{
+ return function(metadata, response) { handler.handle(metadata, response); };
+}
+
+
+/**
+* The default handler for directories; writes an HTML response containing a
+* slightly-formatted directory listing.
+*/
+function defaultIndexHandler(metadata, response)
+{
+ response.setHeader("Content-Type", "text/html", false);
+
+ var path = htmlEscape(decodeURI(metadata.path));
+
+ //
+ // Just do a very basic bit of directory listings -- no need for too much
+ // fanciness, especially since we don't have a style sheet in which we can
+ // stick rules (don't want to pollute the default path-space).
+ //
+
+ var body = '<html>\
+<head>\
+<title>' + path + '</title>\
+</head>\
+<body>\
+<h1>' + path + '</h1>\
+<ol style="list-style-type: none">';
+
+ var directory = metadata.getProperty("directory").QueryInterface(Ci.nsILocalFile);
+ NS_ASSERT(directory && directory.isDirectory());
+
+ var fileList = [];
+ var files = directory.directoryEntries;
+ while (files.hasMoreElements())
+ {
+ var f = files.getNext().QueryInterface(Ci.nsIFile);
+ var name = f.leafName;
+ if (!f.isHidden() &&
+ (name.charAt(name.length - 1) != HIDDEN_CHAR ||
+ name.charAt(name.length - 2) == HIDDEN_CHAR))
+ fileList.push(f);
+ }
+
+ fileList.sort(fileSort);
+
+ for (var i = 0; i < fileList.length; i++)
+ {
+ var file = fileList[i];
+ try
+ {
+ var name = file.leafName;
+ if (name.charAt(name.length - 1) == HIDDEN_CHAR)
+ name = name.substring(0, name.length - 1);
+ var sep = file.isDirectory() ? "/" : "";
+
+ // Note: using " to delimit the attribute here because encodeURIComponent
+ // passes through '.
+ var item = '<li><a href="' + encodeURIComponent(name) + sep + '">' +
+ htmlEscape(name) + sep +
+ '</a></li>';
+
+ body += item;
+ }
+ catch (e) { /* some file system error, ignore the file */ }
+ }
+
+ body += ' </ol>\
+</body>\
+</html>';
+
+ response.bodyOutputStream.write(body, body.length);
+}
+
+/**
+* Sorts a and b (nsIFile objects) into an aesthetically pleasing order.
+*/
+function fileSort(a, b)
+{
+ var dira = a.isDirectory(), dirb = b.isDirectory();
+
+ if (dira && !dirb)
+ return -1;
+ if (dirb && !dira)
+ return 1;
+
+ var namea = a.leafName.toLowerCase(), nameb = b.leafName.toLowerCase();
+ return nameb > namea ? -1 : 1;
+}
+
+
+/**
+* Converts an externally-provided path into an internal path for use in
+* determining file mappings.
+*
+* @param path
+* the path to convert
+* @param encoded
+* true if the given path should be passed through decodeURI prior to
+* conversion
+* @throws URIError
+* if path is incorrectly encoded
+*/
+function toInternalPath(path, encoded)
+{
+ if (encoded)
+ path = decodeURI(path);
+
+ var comps = path.split("/");
+ for (var i = 0, sz = comps.length; i < sz; i++)
+ {
+ var comp = comps[i];
+ if (comp.charAt(comp.length - 1) == HIDDEN_CHAR)
+ comps[i] = comp + HIDDEN_CHAR;
+ }
+ return comps.join("/");
+}
+
+
+/**
+* Adds custom-specified headers for the given file to the given response, if
+* any such headers are specified.
+*
+* @param file
+* the file on the disk which is to be written
+* @param metadata
+* metadata about the incoming request
+* @param response
+* the Response to which any specified headers/data should be written
+* @throws HTTP_500
+* if an error occurred while processing custom-specified headers
+*/
+function maybeAddHeaders(file, metadata, response)
+{
+ var name = file.leafName;
+ if (name.charAt(name.length - 1) == HIDDEN_CHAR)
+ name = name.substring(0, name.length - 1);
+
+ var headerFile = file.parent;
+ headerFile.append(name + HEADERS_SUFFIX);
+
+ if (!headerFile.exists())
+ return;
+
+ const PR_RDONLY = 0x01;
+ var fis = new FileInputStream(headerFile, PR_RDONLY, 0o444,
+ Ci.nsIFileInputStream.CLOSE_ON_EOF);
+
+ try
+ {
+ var lis = new ConverterInputStream(fis, "UTF-8", 1024, 0x0);
+ lis.QueryInterface(Ci.nsIUnicharLineInputStream);
+
+ var line = {value: ""};
+ var more = lis.readLine(line);
+
+ if (!more && line.value == "")
+ return;
+
+
+ // request line
+
+ var status = line.value;
+ if (status.indexOf("HTTP ") == 0)
+ {
+ status = status.substring(5);
+ var space = status.indexOf(" ");
+ var code, description;
+ if (space < 0)
+ {
+ code = status;
+ description = "";
+ }
+ else
+ {
+ code = status.substring(0, space);
+ description = status.substring(space + 1, status.length);
+ }
+
+ response.setStatusLine(metadata.httpVersion, parseInt(code, 10), description);
+
+ line.value = "";
+ more = lis.readLine(line);
+ }
+
+ // headers
+ while (more || line.value != "")
+ {
+ var header = line.value;
+ var colon = header.indexOf(":");
+
+ response.setHeader(header.substring(0, colon),
+ header.substring(colon + 1, header.length),
+ false); // allow overriding server-set headers
+
+ line.value = "";
+ more = lis.readLine(line);
+ }
+ }
+ catch (e)
+ {
+ dumpn("WARNING: error in headers for " + metadata.path + ": " + e);
+ throw HTTP_500;
+ }
+ finally
+ {
+ fis.close();
+ }
+}
+
+
+/**
+* An object which handles requests for a server, executing default and
+* overridden behaviors as instructed by the code which uses and manipulates it.
+* Default behavior includes the paths / and /trace (diagnostics), with some
+* support for HTTP error pages for various codes and fallback to HTTP 500 if
+* those codes fail for any reason.
+*
+* @param server : nsHttpServer
+* the server in which this handler is being used
+*/
+function ServerHandler(server)
+{
+ // FIELDS
+
+ /**
+* The nsHttpServer instance associated with this handler.
+*/
+ this._server = server;
+
+ /**
+* A FileMap object containing the set of path->nsILocalFile mappings for
+* all directory mappings set in the server (e.g., "/" for /var/www/html/,
+* "/foo/bar/" for /local/path/, and "/foo/bar/baz/" for /local/path2).
+*
+* Note carefully: the leading and trailing "/" in each path (not file) are
+* removed before insertion to simplify the code which uses this. You have
+* been warned!
+*/
+ this._pathDirectoryMap = new FileMap();
+
+ /**
+* Custom request handlers for the server in which this resides. Path-handler
+* pairs are stored as property-value pairs in this property.
+*
+* @see ServerHandler.prototype._defaultPaths
+*/
+ this._overridePaths = {};
+
+ /**
+* Custom request handlers for the server in which this resides. Prefix-handler
+* pairs are stored as property-value pairs in this property.
+*/
+ this._overridePrefixes = {};
+
+ /**
+* Custom request handlers for the error handlers in the server in which this
+* resides. Path-handler pairs are stored as property-value pairs in this
+* property.
+*
+* @see ServerHandler.prototype._defaultErrors
+*/
+ this._overrideErrors = {};
+
+ /**
+* Maps file extensions to their MIME types in the server, overriding any
+* mapping that might or might not exist in the MIME service.
+*/
+ this._mimeMappings = {};
+
+ /**
+* The default handler for requests for directories, used to serve directories
+* when no index file is present.
+*/
+ this._indexHandler = defaultIndexHandler;
+
+ /** Per-path state storage for the server. */
+ this._state = {};
+
+ /** Entire-server state storage. */
+ this._sharedState = {};
+
+ /** Entire-server state storage for nsISupports values. */
+ this._objectState = {};
+}
+ServerHandler.prototype =
+{
+ // PUBLIC API
+
+ /**
+* Handles a request to this server, responding to the request appropriately
+* and initiating server shutdown if necessary.
+*
+* This method never throws an exception.
+*
+* @param connection : Connection
+* the connection for this request
+*/
+ handleResponse: function(connection)
+ {
+ var request = connection.request;
+ var response = new Response(connection);
+
+ var path = request.path;
+ dumpn("*** path == " + path);
+
+ try
+ {
+ try
+ {
+ if (path in this._overridePaths)
+ {
+ // explicit paths first, then files based on existing directory mappings,
+ // then (if the file doesn't exist) built-in server default paths
+ dumpn("calling override for " + path);
+ this._overridePaths[path](request, response);
+ }
+ else
+ {
+ let longestPrefix = "";
+ for (let prefix in this._overridePrefixes)
+ {
+ if (prefix.length > longestPrefix.length && path.startsWith(prefix))
+ {
+ longestPrefix = prefix;
+ }
+ }
+ if (longestPrefix.length > 0)
+ {
+ dumpn("calling prefix override for " + longestPrefix);
+ this._overridePrefixes[longestPrefix](request, response);
+ }
+ else
+ {
+ this._handleDefault(request, response);
+ }
+ }
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort(e);
+ return;
+ }
+
+ if (!(e instanceof HttpError))
+ {
+ dumpn("*** unexpected error: e == " + e);
+ throw HTTP_500;
+ }
+ if (e.code !== 404)
+ throw e;
+
+ dumpn("*** default: " + (path in this._defaultPaths));
+
+ response = new Response(connection);
+ if (path in this._defaultPaths)
+ this._defaultPaths[path](request, response);
+ else
+ throw HTTP_404;
+ }
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort(e);
+ return;
+ }
+
+ var errorCode = "internal";
+
+ try
+ {
+ if (!(e instanceof HttpError))
+ throw e;
+
+ errorCode = e.code;
+ dumpn("*** errorCode == " + errorCode);
+
+ response = new Response(connection);
+ if (e.customErrorHandling)
+ e.customErrorHandling(response);
+ this._handleError(errorCode, request, response);
+ return;
+ }
+ catch (e2)
+ {
+ dumpn("*** error handling " + errorCode + " error: " +
+ "e2 == " + e2 + ", shutting down server");
+
+ connection.server._requestQuit();
+ response.abort(e2);
+ return;
+ }
+ }
+
+ response.complete();
+ },
+
+ //
+ // see nsIHttpServer.registerFile
+ //
+ registerFile: function(path, file)
+ {
+ if (!file)
+ {
+ dumpn("*** unregistering '" + path + "' mapping");
+ delete this._overridePaths[path];
+ return;
+ }
+
+ dumpn("*** registering '" + path + "' as mapping to " + file.path);
+ file = file.clone();
+
+ var self = this;
+ this._overridePaths[path] =
+ function(request, response)
+ {
+ if (!file.exists())
+ throw HTTP_404;
+
+ response.setStatusLine(request.httpVersion, 200, "OK");
+ self._writeFileResponse(request, file, response, 0, file.fileSize);
+ };
+ },
+
+ //
+ // see nsIHttpServer.registerPathHandler
+ //
+ registerPathHandler: function(path, handler)
+ {
+ // XXX true path validation!
+ if (path.charAt(0) != "/")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ this._handlerToField(handler, this._overridePaths, path);
+ },
+
+ //
+ // see nsIHttpServer.registerPrefixHandler
+ //
+ registerPrefixHandler: function(prefix, handler)
+ {
+ // XXX true prefix validation!
+ if (!(prefix.startsWith("/") && prefix.endsWith("/")))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ this._handlerToField(handler, this._overridePrefixes, prefix);
+ },
+
+ //
+ // see nsIHttpServer.registerDirectory
+ //
+ registerDirectory: function(path, directory)
+ {
+ // strip off leading and trailing '/' so that we can use lastIndexOf when
+ // determining exactly how a path maps onto a mapped directory --
+ // conditional is required here to deal with "/".substring(1, 0) being
+ // converted to "/".substring(0, 1) per the JS specification
+ var key = path.length == 1 ? "" : path.substring(1, path.length - 1);
+
+ // the path-to-directory mapping code requires that the first character not
+ // be "/", or it will go into an infinite loop
+ if (key.charAt(0) == "/")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ key = toInternalPath(key, false);
+
+ if (directory)
+ {
+ dumpn("*** mapping '" + path + "' to the location " + directory.path);
+ this._pathDirectoryMap.put(key, directory);
+ }
+ else
+ {
+ dumpn("*** removing mapping for '" + path + "'");
+ this._pathDirectoryMap.put(key, null);
+ }
+ },
+
+ //
+ // see nsIHttpServer.registerErrorHandler
+ //
+ registerErrorHandler: function(err, handler)
+ {
+ if (!(err in HTTP_ERROR_CODES))
+ dumpn("*** WARNING: registering non-HTTP/1.1 error code " +
+ "(" + err + ") handler -- was this intentional?");
+
+ this._handlerToField(handler, this._overrideErrors, err);
+ },
+
+ //
+ // see nsIHttpServer.setIndexHandler
+ //
+ setIndexHandler: function(handler)
+ {
+ if (!handler)
+ handler = defaultIndexHandler;
+ else if (typeof(handler) != "function")
+ handler = createHandlerFunc(handler);
+
+ this._indexHandler = handler;
+ },
+
+ //
+ // see nsIHttpServer.registerContentType
+ //
+ registerContentType: function(ext, type)
+ {
+ if (!type)
+ delete this._mimeMappings[ext];
+ else
+ this._mimeMappings[ext] = headerUtils.normalizeFieldValue(type);
+ },
+
+ // PRIVATE API
+
+ /**
+* Sets or remove (if handler is null) a handler in an object with a key.
+*
+* @param handler
+* a handler, either function or an nsIHttpRequestHandler
+* @param dict
+* The object to attach the handler to.
+* @param key
+* The field name of the handler.
+*/
+ _handlerToField: function(handler, dict, key)
+ {
+ // for convenience, handler can be a function if this is run from xpcshell
+ if (typeof(handler) == "function")
+ dict[key] = handler;
+ else if (handler)
+ dict[key] = createHandlerFunc(handler);
+ else
+ delete dict[key];
+ },
+
+ /**
+* Handles a request which maps to a file in the local filesystem (if a base
+* path has already been set; otherwise the 404 error is thrown).
+*
+* @param metadata : Request
+* metadata for the incoming request
+* @param response : Response
+* an uninitialized Response to the given request, to be initialized by a
+* request handler
+* @throws HTTP_###
+* if an HTTP error occurred (usually HTTP_404); note that in this case the
+* calling code must handle post-processing of the response
+*/
+ _handleDefault: function(metadata, response)
+ {
+ dumpn("*** _handleDefault()");
+
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+
+ var path = metadata.path;
+ NS_ASSERT(path.charAt(0) == "/", "invalid path: <" + path + ">");
+
+ // determine the actual on-disk file; this requires finding the deepest
+ // path-to-directory mapping in the requested URL
+ var file = this._getFileForPath(path);
+
+ // the "file" might be a directory, in which case we either serve the
+ // contained index.html or make the index handler write the response
+ if (file.exists() && file.isDirectory())
+ {
+ file.append("index.html"); // make configurable?
+ if (!file.exists() || file.isDirectory())
+ {
+ metadata._ensurePropertyBag();
+ metadata._bag.setPropertyAsInterface("directory", file.parent);
+ this._indexHandler(metadata, response);
+ return;
+ }
+ }
+
+ // alternately, the file might not exist
+ if (!file.exists())
+ throw HTTP_404;
+
+ var start, end;
+ if (metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1) &&
+ metadata.hasHeader("Range") &&
+ this._getTypeFromFile(file) !== SJS_TYPE)
+ {
+ var rangeMatch = metadata.getHeader("Range").match(/^bytes=(\d+)?-(\d+)?$/);
+ if (!rangeMatch)
+ throw HTTP_400;
+
+ if (rangeMatch[1] !== undefined)
+ start = parseInt(rangeMatch[1], 10);
+
+ if (rangeMatch[2] !== undefined)
+ end = parseInt(rangeMatch[2], 10);
+
+ if (start === undefined && end === undefined)
+ throw HTTP_400;
+
+ // No start given, so the end is really the count of bytes from the
+ // end of the file.
+ if (start === undefined)
+ {
+ start = Math.max(0, file.fileSize - end);
+ end = file.fileSize - 1;
+ }
+
+ // start and end are inclusive
+ if (end === undefined || end >= file.fileSize)
+ end = file.fileSize - 1;
+
+ if (start !== undefined && start >= file.fileSize) {
+ var HTTP_416 = new HttpError(416, "Requested Range Not Satisfiable");
+ HTTP_416.customErrorHandling = function(errorResponse)
+ {
+ maybeAddHeaders(file, metadata, errorResponse);
+ };
+ throw HTTP_416;
+ }
+
+ if (end < start)
+ {
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ start = 0;
+ end = file.fileSize - 1;
+ }
+ else
+ {
+ response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
+ var contentRange = "bytes " + start + "-" + end + "/" + file.fileSize;
+ response.setHeader("Content-Range", contentRange);
+ }
+ }
+ else
+ {
+ start = 0;
+ end = file.fileSize - 1;
+ }
+
+ // finally...
+ dumpn("*** handling '" + path + "' as mapping to " + file.path + " from " +
+ start + " to " + end + " inclusive");
+ this._writeFileResponse(metadata, file, response, start, end - start + 1);
+ },
+
+ /**
+* Writes an HTTP response for the given file, including setting headers for
+* file metadata.
+*
+* @param metadata : Request
+* the Request for which a response is being generated
+* @param file : nsILocalFile
+* the file which is to be sent in the response
+* @param response : Response
+* the response to which the file should be written
+* @param offset: uint
+* the byte offset to skip to when writing
+* @param count: uint
+* the number of bytes to write
+*/
+ _writeFileResponse: function(metadata, file, response, offset, count)
+ {
+ const PR_RDONLY = 0x01;
+
+ var type = this._getTypeFromFile(file);
+ if (type === SJS_TYPE)
+ {
+ var fis = new FileInputStream(file, PR_RDONLY, 0o444,
+ Ci.nsIFileInputStream.CLOSE_ON_EOF);
+
+ try
+ {
+ var sis = new ScriptableInputStream(fis);
+ var s = Cu.Sandbox(gGlobalObject);
+ s.importFunction(dump, "dump");
+
+ // Define a basic key-value state-preservation API across requests, with
+ // keys initially corresponding to the empty string.
+ var self = this;
+ var path = metadata.path;
+ s.importFunction(function getState(k)
+ {
+ return self._getState(path, k);
+ });
+ s.importFunction(function setState(k, v)
+ {
+ self._setState(path, k, v);
+ });
+ s.importFunction(function getSharedState(k)
+ {
+ return self._getSharedState(k);
+ });
+ s.importFunction(function setSharedState(k, v)
+ {
+ self._setSharedState(k, v);
+ });
+ s.importFunction(function getObjectState(k, callback)
+ {
+ callback(self._getObjectState(k));
+ });
+ s.importFunction(function setObjectState(k, v)
+ {
+ self._setObjectState(k, v);
+ });
+ s.importFunction(function registerPathHandler(p, h)
+ {
+ self.registerPathHandler(p, h);
+ });
+
+ // Make it possible for sjs files to access their location
+ this._setState(path, "__LOCATION__", file.path);
+
+ try
+ {
+ // Alas, the line number in errors dumped to console when calling the
+ // request handler is simply an offset from where we load the SJS file.
+ // Work around this in a reasonably non-fragile way by dynamically
+ // getting the line number where we evaluate the SJS file. Don't
+ // separate these two lines!
+ var line = new Error().lineNumber;
+ Cu.evalInSandbox(sis.read(file.fileSize), s);
+ }
+ catch (e)
+ {
+ dumpn("*** syntax error in SJS at " + file.path + ": " + e);
+ throw HTTP_500;
+ }
+
+ try
+ {
+ s.handleRequest(metadata, response);
+ }
+ catch (e)
+ {
+ dump("*** error running SJS at " + file.path + ": " +
+ e + " on line " +
+ (e instanceof Error
+ ? e.lineNumber + " in httpd.js"
+ : (e.lineNumber - line)) + "\n");
+ throw HTTP_500;
+ }
+ }
+ finally
+ {
+ fis.close();
+ }
+ }
+ else
+ {
+ try
+ {
+ response.setHeader("Last-Modified",
+ toDateString(file.lastModifiedTime),
+ false);
+ }
+ catch (e) { /* lastModifiedTime threw, ignore */ }
+
+ response.setHeader("Content-Type", type, false);
+ maybeAddHeaders(file, metadata, response);
+ response.setHeader("Content-Length", "" + count, false);
+
+ var fis = new FileInputStream(file, PR_RDONLY, 0o444,
+ Ci.nsIFileInputStream.CLOSE_ON_EOF);
+
+ offset = offset || 0;
+ count = count || file.fileSize;
+ NS_ASSERT(offset === 0 || offset < file.fileSize, "bad offset");
+ NS_ASSERT(count >= 0, "bad count");
+ NS_ASSERT(offset + count <= file.fileSize, "bad total data size");
+
+ try
+ {
+ if (offset !== 0)
+ {
+ // Seek (or read, if seeking isn't supported) to the correct offset so
+ // the data sent to the client matches the requested range.
+ if (fis instanceof Ci.nsISeekableStream)
+ fis.seek(Ci.nsISeekableStream.NS_SEEK_SET, offset);
+ else
+ new ScriptableInputStream(fis).read(offset);
+ }
+ }
+ catch (e)
+ {
+ fis.close();
+ throw e;
+ }
+
+ let writeMore = function writeMore()
+ {
+ gThreadManager.currentThread
+ .dispatch(writeData, Ci.nsIThread.DISPATCH_NORMAL);
+ }
+
+ var input = new BinaryInputStream(fis);
+ var output = new BinaryOutputStream(response.bodyOutputStream);
+ var writeData =
+ {
+ run: function()
+ {
+ var chunkSize = Math.min(65536, count);
+ count -= chunkSize;
+ NS_ASSERT(count >= 0, "underflow");
+
+ try
+ {
+ var data = input.readByteArray(chunkSize);
+ NS_ASSERT(data.length === chunkSize,
+ "incorrect data returned? got " + data.length +
+ ", expected " + chunkSize);
+ output.writeByteArray(data, data.length);
+ if (count === 0)
+ {
+ fis.close();
+ response.finish();
+ }
+ else
+ {
+ writeMore();
+ }
+ }
+ catch (e)
+ {
+ try
+ {
+ fis.close();
+ }
+ finally
+ {
+ response.finish();
+ }
+ throw e;
+ }
+ }
+ };
+
+ writeMore();
+
+ // Now that we know copying will start, flag the response as async.
+ response.processAsync();
+ }
+ },
+
+ /**
+* Get the value corresponding to a given key for the given path for SJS state
+* preservation across requests.
+*
+* @param path : string
+* the path from which the given state is to be retrieved
+* @param k : string
+* the key whose corresponding value is to be returned
+* @returns string
+* the corresponding value, which is initially the empty string
+*/
+ _getState: function(path, k)
+ {
+ var state = this._state;
+ if (path in state && k in state[path])
+ return state[path][k];
+ return "";
+ },
+
+ /**
+* Set the value corresponding to a given key for the given path for SJS state
+* preservation across requests.
+*
+* @param path : string
+* the path from which the given state is to be retrieved
+* @param k : string
+* the key whose corresponding value is to be set
+* @param v : string
+* the value to be set
+*/
+ _setState: function(path, k, v)
+ {
+ if (typeof v !== "string")
+ throw new Error("non-string value passed");
+ var state = this._state;
+ if (!(path in state))
+ state[path] = {};
+ state[path][k] = v;
+ },
+
+ /**
+* Get the value corresponding to a given key for SJS state preservation
+* across requests.
+*
+* @param k : string
+* the key whose corresponding value is to be returned
+* @returns string
+* the corresponding value, which is initially the empty string
+*/
+ _getSharedState: function(k)
+ {
+ var state = this._sharedState;
+ if (k in state)
+ return state[k];
+ return "";
+ },
+
+ /**
+* Set the value corresponding to a given key for SJS state preservation
+* across requests.
+*
+* @param k : string
+* the key whose corresponding value is to be set
+* @param v : string
+* the value to be set
+*/
+ _setSharedState: function(k, v)
+ {
+ if (typeof v !== "string")
+ throw new Error("non-string value passed");
+ this._sharedState[k] = v;
+ },
+
+ /**
+* Returns the object associated with the given key in the server for SJS
+* state preservation across requests.
+*
+* @param k : string
+* the key whose corresponding object is to be returned
+* @returns nsISupports
+* the corresponding object, or null if none was present
+*/
+ _getObjectState: function(k)
+ {
+ if (typeof k !== "string")
+ throw new Error("non-string key passed");
+ return this._objectState[k] || null;
+ },
+
+ /**
+* Sets the object associated with the given key in the server for SJS
+* state preservation across requests.
+*
+* @param k : string
+* the key whose corresponding object is to be set
+* @param v : nsISupports
+* the object to be associated with the given key; may be null
+*/
+ _setObjectState: function(k, v)
+ {
+ if (typeof k !== "string")
+ throw new Error("non-string key passed");
+ if (typeof v !== "object")
+ throw new Error("non-object value passed");
+ if (v && !("QueryInterface" in v))
+ {
+ throw new Error("must pass an nsISupports; use wrappedJSObject to ease " +
+ "pain when using the server from JS");
+ }
+
+ this._objectState[k] = v;
+ },
+
+ /**
+* Gets a content-type for the given file, first by checking for any custom
+* MIME-types registered with this handler for the file's extension, second by
+* asking the global MIME service for a content-type, and finally by failing
+* over to application/octet-stream.
+*
+* @param file : nsIFile
+* the nsIFile for which to get a file type
+* @returns string
+* the best content-type which can be determined for the file
+*/
+ _getTypeFromFile: function(file)
+ {
+ try
+ {
+ var name = file.leafName;
+ var dot = name.lastIndexOf(".");
+ if (dot > 0)
+ {
+ var ext = name.slice(dot + 1);
+ if (ext in this._mimeMappings)
+ return this._mimeMappings[ext];
+ }
+ return Cc["@mozilla.org/uriloader/external-helper-app-service;1"]
+ .getService(Ci.nsIMIMEService)
+ .getTypeFromFile(file);
+ }
+ catch (e)
+ {
+ return "application/octet-stream";
+ }
+ },
+
+ /**
+* Returns the nsILocalFile which corresponds to the path, as determined using
+* all registered path->directory mappings and any paths which are explicitly
+* overridden.
+*
+* @param path : string
+* the server path for which a file should be retrieved, e.g. "/foo/bar"
+* @throws HttpError
+* when the correct action is the corresponding HTTP error (i.e., because no
+* mapping was found for a directory in path, the referenced file doesn't
+* exist, etc.)
+* @returns nsILocalFile
+* the file to be sent as the response to a request for the path
+*/
+ _getFileForPath: function(path)
+ {
+ // decode and add underscores as necessary
+ try
+ {
+ path = toInternalPath(path, true);
+ }
+ catch (e)
+ {
+ throw HTTP_400; // malformed path
+ }
+
+ // next, get the directory which contains this path
+ var pathMap = this._pathDirectoryMap;
+
+ // An example progression of tmp for a path "/foo/bar/baz/" might be:
+ // "foo/bar/baz/", "foo/bar/baz", "foo/bar", "foo", ""
+ var tmp = path.substring(1);
+ while (true)
+ {
+ // do we have a match for current head of the path?
+ var file = pathMap.get(tmp);
+ if (file)
+ {
+ // XXX hack; basically disable showing mapping for /foo/bar/ when the
+ // requested path was /foo/bar, because relative links on the page
+ // will all be incorrect -- we really need the ability to easily
+ // redirect here instead
+ if (tmp == path.substring(1) &&
+ tmp.length != 0 &&
+ tmp.charAt(tmp.length - 1) != "/")
+ file = null;
+ else
+ break;
+ }
+
+ // if we've finished trying all prefixes, exit
+ if (tmp == "")
+ break;
+
+ tmp = tmp.substring(0, tmp.lastIndexOf("/"));
+ }
+
+ // no mapping applies, so 404
+ if (!file)
+ throw HTTP_404;
+
+
+ // last, get the file for the path within the determined directory
+ var parentFolder = file.parent;
+ var dirIsRoot = (parentFolder == null);
+
+ // Strategy here is to append components individually, making sure we
+ // never move above the given directory; this allows paths such as
+ // "<file>/foo/../bar" but prevents paths such as "<file>/../base-sibling";
+ // this component-wise approach also means the code works even on platforms
+ // which don't use "/" as the directory separator, such as Windows
+ var leafPath = path.substring(tmp.length + 1);
+ var comps = leafPath.split("/");
+ for (var i = 0, sz = comps.length; i < sz; i++)
+ {
+ var comp = comps[i];
+
+ if (comp == "..")
+ file = file.parent;
+ else if (comp == "." || comp == "")
+ continue;
+ else
+ file.append(comp);
+
+ if (!dirIsRoot && file.equals(parentFolder))
+ throw HTTP_403;
+ }
+
+ return file;
+ },
+
+ /**
+* Writes the error page for the given HTTP error code over the given
+* connection.
+*
+* @param errorCode : uint
+* the HTTP error code to be used
+* @param connection : Connection
+* the connection on which the error occurred
+*/
+ handleError: function(errorCode, connection)
+ {
+ var response = new Response(connection);
+
+ dumpn("*** error in request: " + errorCode);
+
+ this._handleError(errorCode, new Request(connection.port), response);
+ },
+
+ /**
+* Handles a request which generates the given error code, using the
+* user-defined error handler if one has been set, gracefully falling back to
+* the x00 status code if the code has no handler, and failing to status code
+* 500 if all else fails.
+*
+* @param errorCode : uint
+* the HTTP error which is to be returned
+* @param metadata : Request
+* metadata for the request, which will often be incomplete since this is an
+* error
+* @param response : Response
+* an uninitialized Response should be initialized when this method
+* completes with information which represents the desired error code in the
+* ideal case or a fallback code in abnormal circumstances (i.e., 500 is a
+* fallback for 505, per HTTP specs)
+*/
+ _handleError: function(errorCode, metadata, response)
+ {
+ if (!metadata)
+ throw Cr.NS_ERROR_NULL_POINTER;
+
+ var errorX00 = errorCode - (errorCode % 100);
+
+ try
+ {
+ if (!(errorCode in HTTP_ERROR_CODES))
+ dumpn("*** WARNING: requested invalid error: " + errorCode);
+
+ // RFC 2616 says that we should try to handle an error by its class if we
+ // can't otherwise handle it -- if that fails, we revert to handling it as
+ // a 500 internal server error, and if that fails we throw and shut down
+ // the server
+
+ // actually handle the error
+ try
+ {
+ if (errorCode in this._overrideErrors)
+ this._overrideErrors[errorCode](metadata, response);
+ else
+ this._defaultErrors[errorCode](metadata, response);
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort(e);
+ return;
+ }
+
+ // don't retry the handler that threw
+ if (errorX00 == errorCode)
+ throw HTTP_500;
+
+ dumpn("*** error in handling for error code " + errorCode + ", " +
+ "falling back to " + errorX00 + "...");
+ response = new Response(response._connection);
+ if (errorX00 in this._overrideErrors)
+ this._overrideErrors[errorX00](metadata, response);
+ else if (errorX00 in this._defaultErrors)
+ this._defaultErrors[errorX00](metadata, response);
+ else
+ throw HTTP_500;
+ }
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort();
+ return;
+ }
+
+ // we've tried everything possible for a meaningful error -- now try 500
+ dumpn("*** error in handling for error code " + errorX00 + ", falling " +
+ "back to 500...");
+
+ try
+ {
+ response = new Response(response._connection);
+ if (500 in this._overrideErrors)
+ this._overrideErrors[500](metadata, response);
+ else
+ this._defaultErrors[500](metadata, response);
+ }
+ catch (e2)
+ {
+ dumpn("*** multiple errors in default error handlers!");
+ dumpn("*** e == " + e + ", e2 == " + e2);
+ response.abort(e2);
+ return;
+ }
+ }
+
+ response.complete();
+ },
+
+ // FIELDS
+
+ /**
+* This object contains the default handlers for the various HTTP error codes.
+*/
+ _defaultErrors:
+ {
+ 400: function(metadata, response)
+ {
+ // none of the data in metadata is reliable, so hard-code everything here
+ response.setStatusLine("1.1", 400, "Bad Request");
+ response.setHeader("Content-Type", "text/plain", false);
+
+ var body = "Bad request\n";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 403: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 403, "Forbidden");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>403 Forbidden</title></head>\
+<body>\
+<h1>403 Forbidden</h1>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 404: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 404, "Not Found");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>404 Not Found</title></head>\
+<body>\
+<h1>404 Not Found</h1>\
+<p>\
+<span style='font-family: monospace;'>" +
+ htmlEscape(metadata.path) +
+ "</span> was not found.\
+</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 416: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion,
+ 416,
+ "Requested Range Not Satisfiable");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head>\
+<title>416 Requested Range Not Satisfiable</title></head>\
+<body>\
+<h1>416 Requested Range Not Satisfiable</h1>\
+<p>The byte range was not valid for the\
+requested resource.\
+</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 500: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion,
+ 500,
+ "Internal Server Error");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>500 Internal Server Error</title></head>\
+<body>\
+<h1>500 Internal Server Error</h1>\
+<p>Something's broken in this server and\
+needs to be fixed.</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 501: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 501, "Not Implemented");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>501 Not Implemented</title></head>\
+<body>\
+<h1>501 Not Implemented</h1>\
+<p>This server is not (yet) Apache.</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 505: function(metadata, response)
+ {
+ response.setStatusLine("1.1", 505, "HTTP Version Not Supported");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>505 HTTP Version Not Supported</title></head>\
+<body>\
+<h1>505 HTTP Version Not Supported</h1>\
+<p>This server only supports HTTP/1.0 and HTTP/1.1\
+connections.</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ }
+ },
+
+ /**
+* Contains handlers for the default set of URIs contained in this server.
+*/
+ _defaultPaths:
+ {
+ "/": function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>httpd.js</title></head>\
+<body>\
+<h1>httpd.js</h1>\
+<p>If you're seeing this page, httpd.js is up and\
+serving requests! Now set a base path and serve some\
+files!</p>\
+</body>\
+</html>";
+
+ response.bodyOutputStream.write(body, body.length);
+ },
+
+ "/trace": function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ response.setHeader("Content-Type", "text/plain", false);
+
+ var body = "Request-URI: " +
+ metadata.scheme + "://" + metadata.host + ":" + metadata.port +
+ metadata.path + "\n\n";
+ body += "Request (semantically equivalent, slightly reformatted):\n\n";
+ body += metadata.method + " " + metadata.path;
+
+ if (metadata.queryString)
+ body += "?" + metadata.queryString;
+
+ body += " HTTP/" + metadata.httpVersion + "\r\n";
+
+ var headEnum = metadata.headers;
+ while (headEnum.hasMoreElements())
+ {
+ var fieldName = headEnum.getNext()
+ .QueryInterface(Ci.nsISupportsString)
+ .data;
+ body += fieldName + ": " + metadata.getHeader(fieldName) + "\r\n";
+ }
+
+ response.bodyOutputStream.write(body, body.length);
+ }
+ }
+};
+
+
+/**
+* Maps absolute paths to files on the local file system (as nsILocalFiles).
+*/
+function FileMap()
+{
+ /** Hash which will map paths to nsILocalFiles. */
+ this._map = {};
+}
+FileMap.prototype =
+{
+ // PUBLIC API
+
+ /**
+* Maps key to a clone of the nsILocalFile value if value is non-null;
+* otherwise, removes any extant mapping for key.
+*
+* @param key : string
+* string to which a clone of value is mapped
+* @param value : nsILocalFile
+* the file to map to key, or null to remove a mapping
+*/
+ put: function(key, value)
+ {
+ if (value)
+ this._map[key] = value.clone();
+ else
+ delete this._map[key];
+ },
+
+ /**
+* Returns a clone of the nsILocalFile mapped to key, or null if no such
+* mapping exists.
+*
+* @param key : string
+* key to which the returned file maps
+* @returns nsILocalFile
+* a clone of the mapped file, or null if no mapping exists
+*/
+ get: function(key)
+ {
+ var val = this._map[key];
+ return val ? val.clone() : null;
+ }
+};
+
+
+// Response CONSTANTS
+
+// token = *<any CHAR except CTLs or separators>
+// CHAR = <any US-ASCII character (0-127)>
+// CTL = <any US-ASCII control character (0-31) and DEL (127)>
+// separators = "(" | ")" | "<" | ">" | "@"
+// | "," | ";" | ":" | "\" | <">
+// | "/" | "[" | "]" | "?" | "="
+// | "{" | "}" | SP | HT
+const IS_TOKEN_ARRAY =
+ [0, 0, 0, 0, 0, 0, 0, 0, // 0
+ 0, 0, 0, 0, 0, 0, 0, 0, // 8
+ 0, 0, 0, 0, 0, 0, 0, 0, // 16
+ 0, 0, 0, 0, 0, 0, 0, 0, // 24
+
+ 0, 1, 0, 1, 1, 1, 1, 1, // 32
+ 0, 0, 1, 1, 0, 1, 1, 0, // 40
+ 1, 1, 1, 1, 1, 1, 1, 1, // 48
+ 1, 1, 0, 0, 0, 0, 0, 0, // 56
+
+ 0, 1, 1, 1, 1, 1, 1, 1, // 64
+ 1, 1, 1, 1, 1, 1, 1, 1, // 72
+ 1, 1, 1, 1, 1, 1, 1, 1, // 80
+ 1, 1, 1, 0, 0, 0, 1, 1, // 88
+
+ 1, 1, 1, 1, 1, 1, 1, 1, // 96
+ 1, 1, 1, 1, 1, 1, 1, 1, // 104
+ 1, 1, 1, 1, 1, 1, 1, 1, // 112
+ 1, 1, 1, 0, 1, 0, 1]; // 120
+
+
+/**
+* Determines whether the given character code is a CTL.
+*
+* @param code : uint
+* the character code
+* @returns boolean
+* true if code is a CTL, false otherwise
+*/
+function isCTL(code)
+{
+ return (code >= 0 && code <= 31) || (code == 127);
+}
+
+/**
+* Represents a response to an HTTP request, encapsulating all details of that
+* response. This includes all headers, the HTTP version, status code and
+* explanation, and the entity itself.
+*
+* @param connection : Connection
+* the connection over which this response is to be written
+*/
+function Response(connection)
+{
+ /** The connection over which this response will be written. */
+ this._connection = connection;
+
+ /**
+* The HTTP version of this response; defaults to 1.1 if not set by the
+* handler.
+*/
+ this._httpVersion = nsHttpVersion.HTTP_1_1;
+
+ /**
+* The HTTP code of this response; defaults to 200.
+*/
+ this._httpCode = 200;
+
+ /**
+* The description of the HTTP code in this response; defaults to "OK".
+*/
+ this._httpDescription = "OK";
+
+ /**
+* An nsIHttpHeaders object in which the headers in this response should be
+* stored. This property is null after the status line and headers have been
+* written to the network, and it may be modified up until it is cleared,
+* except if this._finished is set first (in which case headers are written
+* asynchronously in response to a finish() call not preceded by
+* flushHeaders()).
+*/
+ this._headers = new nsHttpHeaders();
+
+ /**
+* Set to true when this response is ended (completely constructed if possible
+* and the connection closed); further actions on this will then fail.
+*/
+ this._ended = false;
+
+ /**
+* A stream used to hold data written to the body of this response.
+*/
+ this._bodyOutputStream = null;
+
+ /**
+* A stream containing all data that has been written to the body of this
+* response so far. (Async handlers make the data contained in this
+* unreliable as a way of determining content length in general, but auxiliary
+* saved information can sometimes be used to guarantee reliability.)
+*/
+ this._bodyInputStream = null;
+
+ /**
+* A stream copier which copies data to the network. It is initially null
+* until replaced with a copier for response headers; when headers have been
+* fully sent it is replaced with a copier for the response body, remaining
+* so for the duration of response processing.
+*/
+ this._asyncCopier = null;
+
+ /**
+* True if this response has been designated as being processed
+* asynchronously rather than for the duration of a single call to
+* nsIHttpRequestHandler.handle.
+*/
+ this._processAsync = false;
+
+ /**
+* True iff finish() has been called on this, signaling that no more changes
+* to this may be made.
+*/
+ this._finished = false;
+
+ /**
+* True iff powerSeized() has been called on this, signaling that this
+* response is to be handled manually by the response handler (which may then
+* send arbitrary data in response, even non-HTTP responses).
+*/
+ this._powerSeized = false;
+}
+Response.prototype =
+{
+ // PUBLIC CONSTRUCTION API
+
+ //
+ // see nsIHttpResponse.bodyOutputStream
+ //
+ get bodyOutputStream()
+ {
+ if (this._finished)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+
+ if (!this._bodyOutputStream)
+ {
+ var pipe = new Pipe(true, false, Response.SEGMENT_SIZE, PR_UINT32_MAX,
+ null);
+ this._bodyOutputStream = pipe.outputStream;
+ this._bodyInputStream = pipe.inputStream;
+ if (this._processAsync || this._powerSeized)
+ this._startAsyncProcessor();
+ }
+
+ return this._bodyOutputStream;
+ },
+
+ //
+ // see nsIHttpResponse.write
+ //
+ write: function(data)
+ {
+ if (this._finished)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+
+ var dataAsString = String(data);
+ this.bodyOutputStream.write(dataAsString, dataAsString.length);
+ },
+
+ //
+ // see nsIHttpResponse.setStatusLine
+ //
+ setStatusLine: function(httpVersion, code, description)
+ {
+ if (!this._headers || this._finished || this._powerSeized)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ this._ensureAlive();
+
+ if (!(code >= 0 && code < 1000))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ try
+ {
+ var httpVer;
+ // avoid version construction for the most common cases
+ if (!httpVersion || httpVersion == "1.1")
+ httpVer = nsHttpVersion.HTTP_1_1;
+ else if (httpVersion == "1.0")
+ httpVer = nsHttpVersion.HTTP_1_0;
+ else
+ httpVer = new nsHttpVersion(httpVersion);
+ }
+ catch (e)
+ {
+ throw Cr.NS_ERROR_INVALID_ARG;
+ }
+
+ // Reason-Phrase = *<TEXT, excluding CR, LF>
+ // TEXT = <any OCTET except CTLs, but including LWS>
+ //
+ // XXX this ends up disallowing octets which aren't Unicode, I think -- not
+ // much to do if description is IDL'd as string
+ if (!description)
+ description = "";
+ for (var i = 0; i < description.length; i++)
+ if (isCTL(description.charCodeAt(i)) && description.charAt(i) != "\t")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ // set the values only after validation to preserve atomicity
+ this._httpDescription = description;
+ this._httpCode = code;
+ this._httpVersion = httpVer;
+ },
+
+ //
+ // see nsIHttpResponse.setHeader
+ //
+ setHeader: function(name, value, merge)
+ {
+ if (!this._headers || this._finished || this._powerSeized)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ this._ensureAlive();
+
+ this._headers.setHeader(name, value, merge);
+ },
+
+ //
+ // see nsIHttpResponse.processAsync
+ //
+ processAsync: function()
+ {
+ if (this._finished)
+ throw Cr.NS_ERROR_UNEXPECTED;
+ if (this._powerSeized)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ if (this._processAsync)
+ return;
+ this._ensureAlive();
+
+ dumpn("*** processing connection " + this._connection.number + " async");
+ this._processAsync = true;
+
+ /*
+* Either the bodyOutputStream getter or this method is responsible for
+* starting the asynchronous processor and catching writes of data to the
+* response body of async responses as they happen, for the purpose of
+* forwarding those writes to the actual connection's output stream.
+* If bodyOutputStream is accessed first, calling this method will create
+* the processor (when it first is clear that body data is to be written
+* immediately, not buffered). If this method is called first, accessing
+* bodyOutputStream will create the processor. If only this method is
+* called, we'll write nothing, neither headers nor the nonexistent body,
+* until finish() is called. Since that delay is easily avoided by simply
+* getting bodyOutputStream or calling write(""), we don't worry about it.
+*/
+ if (this._bodyOutputStream && !this._asyncCopier)
+ this._startAsyncProcessor();
+ },
+
+ //
+ // see nsIHttpResponse.seizePower
+ //
+ seizePower: function()
+ {
+ if (this._processAsync)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ if (this._finished)
+ throw Cr.NS_ERROR_UNEXPECTED;
+ if (this._powerSeized)
+ return;
+ this._ensureAlive();
+
+ dumpn("*** forcefully seizing power over connection " +
+ this._connection.number + "...");
+
+ // Purge any already-written data without sending it. We could as easily
+ // swap out the streams entirely, but that makes it possible to acquire and
+ // unknowingly use a stale reference, so we require there only be one of
+ // each stream ever for any response to avoid this complication.
+ if (this._asyncCopier)
+ this._asyncCopier.cancel(Cr.NS_BINDING_ABORTED);
+ this._asyncCopier = null;
+ if (this._bodyOutputStream)
+ {
+ var input = new BinaryInputStream(this._bodyInputStream);
+ var avail;
+ while ((avail = input.available()) > 0)
+ input.readByteArray(avail);
+ }
+
+ this._powerSeized = true;
+ if (this._bodyOutputStream)
+ this._startAsyncProcessor();
+ },
+
+ //
+ // see nsIHttpResponse.finish
+ //
+ finish: function()
+ {
+ if (!this._processAsync && !this._powerSeized)
+ throw Cr.NS_ERROR_UNEXPECTED;
+ if (this._finished)
+ return;
+
+ dumpn("*** finishing connection " + this._connection.number);
+ this._startAsyncProcessor(); // in case bodyOutputStream was never accessed
+ if (this._bodyOutputStream)
+ this._bodyOutputStream.close();
+ this._finished = true;
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIHttpResponse) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // POST-CONSTRUCTION API (not exposed externally)
+
+ /**
+* The HTTP version number of this, as a string (e.g. "1.1").
+*/
+ get httpVersion()
+ {
+ this._ensureAlive();
+ return this._httpVersion.toString();
+ },
+
+ /**
+* The HTTP status code of this response, as a string of three characters per
+* RFC 2616.
+*/
+ get httpCode()
+ {
+ this._ensureAlive();
+
+ var codeString = (this._httpCode < 10 ? "0" : "") +
+ (this._httpCode < 100 ? "0" : "") +
+ this._httpCode;
+ return codeString;
+ },
+
+ /**
+* The description of the HTTP status code of this response, or "" if none is
+* set.
+*/
+ get httpDescription()
+ {
+ this._ensureAlive();
+
+ return this._httpDescription;
+ },
+
+ /**
+* The headers in this response, as an nsHttpHeaders object.
+*/
+ get headers()
+ {
+ this._ensureAlive();
+
+ return this._headers;
+ },
+
+ //
+ // see nsHttpHeaders.getHeader
+ //
+ getHeader: function(name)
+ {
+ this._ensureAlive();
+
+ return this._headers.getHeader(name);
+ },
+
+ /**
+* Determines whether this response may be abandoned in favor of a newly
+* constructed response. A response may be abandoned only if it is not being
+* sent asynchronously and if raw control over it has not been taken from the
+* server.
+*
+* @returns boolean
+* true iff no data has been written to the network
+*/
+ partiallySent: function()
+ {
+ dumpn("*** partiallySent()");
+ return this._processAsync || this._powerSeized;
+ },
+
+ /**
+* If necessary, kicks off the remaining request processing needed to be done
+* after a request handler performs its initial work upon this response.
+*/
+ complete: function()
+ {
+ dumpn("*** complete()");
+ if (this._processAsync || this._powerSeized)
+ {
+ NS_ASSERT(this._processAsync ^ this._powerSeized,
+ "can't both send async and relinquish power");
+ return;
+ }
+
+ NS_ASSERT(!this.partiallySent(), "completing a partially-sent response?");
+
+ this._startAsyncProcessor();
+
+ // Now make sure we finish processing this request!
+ if (this._bodyOutputStream)
+ this._bodyOutputStream.close();
+ },
+
+ /**
+* Abruptly ends processing of this response, usually due to an error in an
+* incoming request but potentially due to a bad error handler. Since we
+* cannot handle the error in the usual way (giving an HTTP error page in
+* response) because data may already have been sent (or because the response
+* might be expected to have been generated asynchronously or completely from
+* scratch by the handler), we stop processing this response and abruptly
+* close the connection.
+*
+* @param e : Error
+* the exception which precipitated this abort, or null if no such exception
+* was generated
+*/
+ abort: function(e)
+ {
+ dumpn("*** abort(<" + e + ">)");
+
+ // This response will be ended by the processor if one was created.
+ var copier = this._asyncCopier;
+ if (copier)
+ {
+ // We dispatch asynchronously here so that any pending writes of data to
+ // the connection will be deterministically written. This makes it easier
+ // to specify exact behavior, and it makes observable behavior more
+ // predictable for clients. Note that the correctness of this depends on
+ // callbacks in response to _waitToReadData in WriteThroughCopier
+ // happening asynchronously with respect to the actual writing of data to
+ // bodyOutputStream, as they currently do; if they happened synchronously,
+ // an event which ran before this one could write more data to the
+ // response body before we get around to canceling the copier. We have
+ // tests for this in test_seizepower.js, however, and I can't think of a
+ // way to handle both cases without removing bodyOutputStream access and
+ // moving its effective write(data, length) method onto Response, which
+ // would be slower and require more code than this anyway.
+ gThreadManager.currentThread.dispatch({
+ run: function()
+ {
+ dumpn("*** canceling copy asynchronously...");
+ copier.cancel(Cr.NS_ERROR_UNEXPECTED);
+ }
+ }, Ci.nsIThread.DISPATCH_NORMAL);
+ }
+ else
+ {
+ this.end();
+ }
+ },
+
+ /**
+* Closes this response's network connection, marks the response as finished,
+* and notifies the server handler that the request is done being processed.
+*/
+ end: function()
+ {
+ NS_ASSERT(!this._ended, "ending this response twice?!?!");
+
+ this._connection.close();
+ if (this._bodyOutputStream)
+ this._bodyOutputStream.close();
+
+ this._finished = true;
+ this._ended = true;
+ },
+
+ // PRIVATE IMPLEMENTATION
+
+ /**
+* Sends the status line and headers of this response if they haven't been
+* sent and initiates the process of copying data written to this response's
+* body to the network.
+*/
+ _startAsyncProcessor: function()
+ {
+ dumpn("*** _startAsyncProcessor()");
+
+ // Handle cases where we're being called a second time. The former case
+ // happens when this is triggered both by complete() and by processAsync(),
+ // while the latter happens when processAsync() in conjunction with sent
+ // data causes abort() to be called.
+ if (this._asyncCopier || this._ended)
+ {
+ dumpn("*** ignoring second call to _startAsyncProcessor");
+ return;
+ }
+
+ // Send headers if they haven't been sent already and should be sent, then
+ // asynchronously continue to send the body.
+ if (this._headers && !this._powerSeized)
+ {
+ this._sendHeaders();
+ return;
+ }
+
+ this._headers = null;
+ this._sendBody();
+ },
+
+ /**
+* Signals that all modifications to the response status line and headers are
+* complete and then sends that data over the network to the client. Once
+* this method completes, a different response to the request that resulted
+* in this response cannot be sent -- the only possible action in case of
+* error is to abort the response and close the connection.
+*/
+ _sendHeaders: function()
+ {
+ dumpn("*** _sendHeaders()");
+
+ NS_ASSERT(this._headers);
+ NS_ASSERT(!this._powerSeized);
+
+ // request-line
+ var statusLine = "HTTP/" + this.httpVersion + " " +
+ this.httpCode + " " +
+ this.httpDescription + "\r\n";
+
+ // header post-processing
+
+ var headers = this._headers;
+ headers.setHeader("Connection", "close", false);
+ headers.setHeader("Server", "httpd.js", false);
+ if (!headers.hasHeader("Date"))
+ headers.setHeader("Date", toDateString(Date.now()), false);
+
+ // Any response not being processed asynchronously must have an associated
+ // Content-Length header for reasons of backwards compatibility with the
+ // initial server, which fully buffered every response before sending it.
+ // Beyond that, however, it's good to do this anyway because otherwise it's
+ // impossible to test behaviors that depend on the presence or absence of a
+ // Content-Length header.
+ if (!this._processAsync)
+ {
+ dumpn("*** non-async response, set Content-Length");
+
+ var bodyStream = this._bodyInputStream;
+ var avail = bodyStream ? bodyStream.available() : 0;
+
+ // XXX assumes stream will always report the full amount of data available
+ headers.setHeader("Content-Length", "" + avail, false);
+ }
+
+
+ // construct and send response
+ dumpn("*** header post-processing completed, sending response head...");
+
+ // request-line
+ var preambleData = [statusLine];
+
+ // headers
+ var headEnum = headers.enumerator;
+ while (headEnum.hasMoreElements())
+ {
+ var fieldName = headEnum.getNext()
+ .QueryInterface(Ci.nsISupportsString)
+ .data;
+ var values = headers.getHeaderValues(fieldName);
+ for (var i = 0, sz = values.length; i < sz; i++)
+ preambleData.push(fieldName + ": " + values[i] + "\r\n");
+ }
+
+ // end request-line/headers
+ preambleData.push("\r\n");
+
+ var preamble = preambleData.join("");
+
+ var responseHeadPipe = new Pipe(true, false, 0, PR_UINT32_MAX, null);
+ responseHeadPipe.outputStream.write(preamble, preamble.length);
+
+ var response = this;
+ var copyObserver =
+ {
+ onStartRequest: function(request, cx)
+ {
+ dumpn("*** preamble copying started");
+ },
+
+ onStopRequest: function(request, cx, statusCode)
+ {
+ dumpn("*** preamble copying complete " +
+ "[status=0x" + statusCode.toString(16) + "]");
+
+ if (!components.isSuccessCode(statusCode))
+ {
+ dumpn("!!! header copying problems: non-success statusCode, " +
+ "ending response");
+
+ response.end();
+ }
+ else
+ {
+ response._sendBody();
+ }
+ },
+
+ QueryInterface: function(aIID)
+ {
+ if (aIID.equals(Ci.nsIRequestObserver) || aIID.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ }
+ };
+
+ var headerCopier = this._asyncCopier =
+ new WriteThroughCopier(responseHeadPipe.inputStream,
+ this._connection.output,
+ copyObserver, null);
+
+ responseHeadPipe.outputStream.close();
+
+ // Forbid setting any more headers or modifying the request line.
+ this._headers = null;
+ },
+
+ /**
+* Asynchronously writes the body of the response (or the entire response, if
+* seizePower() has been called) to the network.
+*/
+ _sendBody: function()
+ {
+ dumpn("*** _sendBody");
+
+ NS_ASSERT(!this._headers, "still have headers around but sending body?");
+
+ // If no body data was written, we're done
+ if (!this._bodyInputStream)
+ {
+ dumpn("*** empty body, response finished");
+ this.end();
+ return;
+ }
+
+ var response = this;
+ var copyObserver =
+ {
+ onStartRequest: function(request, context)
+ {
+ dumpn("*** onStartRequest");
+ },
+
+ onStopRequest: function(request, cx, statusCode)
+ {
+ dumpn("*** onStopRequest [status=0x" + statusCode.toString(16) + "]");
+
+ if (statusCode === Cr.NS_BINDING_ABORTED)
+ {
+ dumpn("*** terminating copy observer without ending the response");
+ }
+ else
+ {
+ if (!components.isSuccessCode(statusCode))
+ dumpn("*** WARNING: non-success statusCode in onStopRequest");
+
+ response.end();
+ }
+ },
+
+ QueryInterface: function(aIID)
+ {
+ if (aIID.equals(Ci.nsIRequestObserver) || aIID.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ }
+ };
+
+ dumpn("*** starting async copier of body data...");
+ this._asyncCopier =
+ new WriteThroughCopier(this._bodyInputStream, this._connection.output,
+ copyObserver, null);
+ },
+
+ /** Ensures that this hasn't been ended. */
+ _ensureAlive: function()
+ {
+ NS_ASSERT(!this._ended, "not handling response lifetime correctly");
+ }
+};
+
+/**
+* Size of the segments in the buffer used in storing response data and writing
+* it to the socket.
+*/
+Response.SEGMENT_SIZE = 8192;
+
+/** Serves double duty in WriteThroughCopier implementation. */
+function notImplemented()
+{
+ throw Cr.NS_ERROR_NOT_IMPLEMENTED;
+}
+
+/** Returns true iff the given exception represents stream closure. */
+function streamClosed(e)
+{
+ return e === Cr.NS_BASE_STREAM_CLOSED ||
+ (typeof e === "object" && e.result === Cr.NS_BASE_STREAM_CLOSED);
+}
+
+/** Returns true iff the given exception represents a blocked stream. */
+function wouldBlock(e)
+{
+ return e === Cr.NS_BASE_STREAM_WOULD_BLOCK ||
+ (typeof e === "object" && e.result === Cr.NS_BASE_STREAM_WOULD_BLOCK);
+}
+
+/**
+* Copies data from source to sink as it becomes available, when that data can
+* be written to sink without blocking.
+*
+* @param source : nsIAsyncInputStream
+* the stream from which data is to be read
+* @param sink : nsIAsyncOutputStream
+* the stream to which data is to be copied
+* @param observer : nsIRequestObserver
+* an observer which will be notified when the copy starts and finishes
+* @param context : nsISupports
+* context passed to observer when notified of start/stop
+* @throws NS_ERROR_NULL_POINTER
+* if source, sink, or observer are null
+*/
+function WriteThroughCopier(source, sink, observer, context)
+{
+ if (!source || !sink || !observer)
+ throw Cr.NS_ERROR_NULL_POINTER;
+
+ /** Stream from which data is being read. */
+ this._source = source;
+
+ /** Stream to which data is being written. */
+ this._sink = sink;
+
+ /** Observer watching this copy. */
+ this._observer = observer;
+
+ /** Context for the observer watching this. */
+ this._context = context;
+
+ /**
+* True iff this is currently being canceled (cancel has been called, the
+* callback may not yet have been made).
+*/
+ this._canceled = false;
+
+ /**
+* False until all data has been read from input and written to output, at
+* which point this copy is completed and cancel() is asynchronously called.
+*/
+ this._completed = false;
+
+ /** Required by nsIRequest, meaningless. */
+ this.loadFlags = 0;
+ /** Required by nsIRequest, meaningless. */
+ this.loadGroup = null;
+ /** Required by nsIRequest, meaningless. */
+ this.name = "response-body-copy";
+
+ /** Status of this request. */
+ this.status = Cr.NS_OK;
+
+ /** Arrays of byte strings waiting to be written to output. */
+ this._pendingData = [];
+
+ // start copying
+ try
+ {
+ observer.onStartRequest(this, context);
+ this._waitToReadData();
+ this._waitForSinkClosure();
+ }
+ catch (e)
+ {
+ dumpn("!!! error starting copy: " + e +
+ ("lineNumber" in e ? ", line " + e.lineNumber : ""));
+ dumpn(e.stack);
+ this.cancel(Cr.NS_ERROR_UNEXPECTED);
+ }
+}
+WriteThroughCopier.prototype =
+{
+ /* nsISupports implementation */
+
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIInputStreamCallback) ||
+ iid.equals(Ci.nsIOutputStreamCallback) ||
+ iid.equals(Ci.nsIRequest) ||
+ iid.equals(Ci.nsISupports))
+ {
+ return this;
+ }
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // NSIINPUTSTREAMCALLBACK
+
+ /**
+* Receives a more-data-in-input notification and writes the corresponding
+* data to the output.
+*
+* @param input : nsIAsyncInputStream
+* the input stream on whose data we have been waiting
+*/
+ onInputStreamReady: function(input)
+ {
+ if (this._source === null)
+ return;
+
+ dumpn("*** onInputStreamReady");
+
+ //
+ // Ordinarily we'll read a non-zero amount of data from input, queue it up
+ // to be written and then wait for further callbacks. The complications in
+ // this method are the cases where we deviate from that behavior when errors
+ // occur or when copying is drawing to a finish.
+ //
+ // The edge cases when reading data are:
+ //
+ // Zero data is read
+ // If zero data was read, we're at the end of available data, so we can
+ // should stop reading and move on to writing out what we have (or, if
+ // we've already done that, onto notifying of completion).
+ // A stream-closed exception is thrown
+ // This is effectively a less kind version of zero data being read; the
+ // only difference is that we notify of completion with that result
+ // rather than with NS_OK.
+ // Some other exception is thrown
+ // This is the least kind result. We don't know what happened, so we
+ // act as though the stream closed except that we notify of completion
+ // with the result NS_ERROR_UNEXPECTED.
+ //
+
+ var bytesWanted = 0, bytesConsumed = -1;
+ try
+ {
+ input = new BinaryInputStream(input);
+
+ bytesWanted = Math.min(input.available(), Response.SEGMENT_SIZE);
+ dumpn("*** input wanted: " + bytesWanted);
+
+ if (bytesWanted > 0)
+ {
+ var data = input.readByteArray(bytesWanted);
+ bytesConsumed = data.length;
+ this._pendingData.push(String.fromCharCode.apply(String, data));
+ }
+
+ dumpn("*** " + bytesConsumed + " bytes read");
+
+ // Handle the zero-data edge case in the same place as all other edge
+ // cases are handled.
+ if (bytesWanted === 0)
+ throw Cr.NS_BASE_STREAM_CLOSED;
+ }
+ catch (e)
+ {
+ if (streamClosed(e))
+ {
+ dumpn("*** input stream closed");
+ e = bytesWanted === 0 ? Cr.NS_OK : Cr.NS_ERROR_UNEXPECTED;
+ }
+ else
+ {
+ dumpn("!!! unexpected error reading from input, canceling: " + e);
+ e = Cr.NS_ERROR_UNEXPECTED;
+ }
+
+ this._doneReadingSource(e);
+ return;
+ }
+
+ var pendingData = this._pendingData;
+
+ NS_ASSERT(bytesConsumed > 0);
+ NS_ASSERT(pendingData.length > 0, "no pending data somehow?");
+ NS_ASSERT(pendingData[pendingData.length - 1].length > 0,
+ "buffered zero bytes of data?");
+
+ NS_ASSERT(this._source !== null);
+
+ // Reading has gone great, and we've gotten data to write now. What if we
+ // don't have a place to write that data, because output went away just
+ // before this read? Drop everything on the floor, including new data, and
+ // cancel at this point.
+ if (this._sink === null)
+ {
+ pendingData.length = 0;
+ this._doneReadingSource(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // Okay, we've read the data, and we know we have a place to write it. We
+ // need to queue up the data to be written, but *only* if none is queued
+ // already -- if data's already queued, the code that actually writes the
+ // data will make sure to wait on unconsumed pending data.
+ try
+ {
+ if (pendingData.length === 1)
+ this._waitToWriteData();
+ }
+ catch (e)
+ {
+ dumpn("!!! error waiting to write data just read, swallowing and " +
+ "writing only what we already have: " + e);
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // Whee! We successfully read some data, and it's successfully queued up to
+ // be written. All that remains now is to wait for more data to read.
+ try
+ {
+ this._waitToReadData();
+ }
+ catch (e)
+ {
+ dumpn("!!! error waiting to read more data: " + e);
+ this._doneReadingSource(Cr.NS_ERROR_UNEXPECTED);
+ }
+ },
+
+
+ // NSIOUTPUTSTREAMCALLBACK
+
+ /**
+* Callback when data may be written to the output stream without blocking, or
+* when the output stream has been closed.
+*
+* @param output : nsIAsyncOutputStream
+* the output stream on whose writability we've been waiting, also known as
+* this._sink
+*/
+ onOutputStreamReady: function(output)
+ {
+ if (this._sink === null)
+ return;
+
+ dumpn("*** onOutputStreamReady");
+
+ var pendingData = this._pendingData;
+ if (pendingData.length === 0)
+ {
+ // There's no pending data to write. The only way this can happen is if
+ // we're waiting on the output stream's closure, so we can respond to a
+ // copying failure as quickly as possible (rather than waiting for data to
+ // be available to read and then fail to be copied). Therefore, we must
+ // be done now -- don't bother to attempt to write anything and wrap
+ // things up.
+ dumpn("!!! output stream closed prematurely, ending copy");
+
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+
+ NS_ASSERT(pendingData[0].length > 0, "queued up an empty quantum?");
+
+ //
+ // Write out the first pending quantum of data. The possible errors here
+ // are:
+ //
+ // The write might fail because we can't write that much data
+ // Okay, we've written what we can now, so re-queue what's left and
+ // finish writing it out later.
+ // The write failed because the stream was closed
+ // Discard pending data that we can no longer write, stop reading, and
+ // signal that copying finished.
+ // Some other error occurred.
+ // Same as if the stream were closed, but notify with the status
+ // NS_ERROR_UNEXPECTED so the observer knows something was wonky.
+ //
+
+ try
+ {
+ var quantum = pendingData[0];
+
+ // XXX |quantum| isn't guaranteed to be ASCII, so we're relying on
+ // undefined behavior! We're only using this because writeByteArray
+ // is unusably broken for asynchronous output streams; see bug 532834
+ // for details.
+ var bytesWritten = output.write(quantum, quantum.length);
+ if (bytesWritten === quantum.length)
+ pendingData.shift();
+ else
+ pendingData[0] = quantum.substring(bytesWritten);
+
+ dumpn("*** wrote " + bytesWritten + " bytes of data");
+ }
+ catch (e)
+ {
+ if (wouldBlock(e))
+ {
+ NS_ASSERT(pendingData.length > 0,
+ "stream-blocking exception with no data to write?");
+ NS_ASSERT(pendingData[0].length > 0,
+ "stream-blocking exception with empty quantum?");
+ this._waitToWriteData();
+ return;
+ }
+
+ if (streamClosed(e))
+ dumpn("!!! output stream prematurely closed, signaling error...");
+ else
+ dumpn("!!! unknown error: " + e + ", quantum=" + quantum);
+
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // The day is ours! Quantum written, now let's see if we have more data
+ // still to write.
+ try
+ {
+ if (pendingData.length > 0)
+ {
+ this._waitToWriteData();
+ return;
+ }
+ }
+ catch (e)
+ {
+ dumpn("!!! unexpected error waiting to write pending data: " + e);
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // Okay, we have no more pending data to write -- but might we get more in
+ // the future?
+ if (this._source !== null)
+ {
+ /*
+* If we might, then wait for the output stream to be closed. (We wait
+* only for closure because we have no data to write -- and if we waited
+* for a specific amount of data, we would get repeatedly notified for no
+* reason if over time the output stream permitted more and more data to
+* be written to it without blocking.)
+*/
+ this._waitForSinkClosure();
+ }
+ else
+ {
+ /*
+* On the other hand, if we can't have more data because the input
+* stream's gone away, then it's time to notify of copy completion.
+* Victory!
+*/
+ this._sink = null;
+ this._cancelOrDispatchCancelCallback(Cr.NS_OK);
+ }
+ },
+
+
+ // NSIREQUEST
+
+ /** Returns true if the cancel observer hasn't been notified yet. */
+ isPending: function()
+ {
+ return !this._completed;
+ },
+
+ /** Not implemented, don't use! */
+ suspend: notImplemented,
+ /** Not implemented, don't use! */
+ resume: notImplemented,
+
+ /**
+* Cancels data reading from input, asynchronously writes out any pending
+* data, and causes the observer to be notified with the given error code when
+* all writing has finished.
+*
+* @param status : nsresult
+* the status to pass to the observer when data copying has been canceled
+*/
+ cancel: function(status)
+ {
+ dumpn("*** cancel(" + status.toString(16) + ")");
+
+ if (this._canceled)
+ {
+ dumpn("*** suppressing a late cancel");
+ return;
+ }
+
+ this._canceled = true;
+ this.status = status;
+
+ // We could be in the middle of absolutely anything at this point. Both
+ // input and output might still be around, we might have pending data to
+ // write, and in general we know nothing about the state of the world. We
+ // therefore must assume everything's in progress and take everything to its
+ // final steady state (or so far as it can go before we need to finish
+ // writing out remaining data).
+
+ this._doneReadingSource(status);
+ },
+
+
+ // PRIVATE IMPLEMENTATION
+
+ /**
+* Stop reading input if we haven't already done so, passing e as the status
+* when closing the stream, and kick off a copy-completion notice if no more
+* data remains to be written.
+*
+* @param e : nsresult
+* the status to be used when closing the input stream
+*/
+ _doneReadingSource: function(e)
+ {
+ dumpn("*** _doneReadingSource(0x" + e.toString(16) + ")");
+
+ this._finishSource(e);
+ if (this._pendingData.length === 0)
+ this._sink = null;
+ else
+ NS_ASSERT(this._sink !== null, "null output?");
+
+ // If we've written out all data read up to this point, then it's time to
+ // signal completion.
+ if (this._sink === null)
+ {
+ NS_ASSERT(this._pendingData.length === 0, "pending data still?");
+ this._cancelOrDispatchCancelCallback(e);
+ }
+ },
+
+ /**
+* Stop writing output if we haven't already done so, discard any data that
+* remained to be sent, close off input if it wasn't already closed, and kick
+* off a copy-completion notice.
+*
+* @param e : nsresult
+* the status to be used when closing input if it wasn't already closed
+*/
+ _doneWritingToSink: function(e)
+ {
+ dumpn("*** _doneWritingToSink(0x" + e.toString(16) + ")");
+
+ this._pendingData.length = 0;
+ this._sink = null;
+ this._doneReadingSource(e);
+ },
+
+ /**
+* Completes processing of this copy: either by canceling the copy if it
+* hasn't already been canceled using the provided status, or by dispatching
+* the cancel callback event (with the originally provided status, of course)
+* if it already has been canceled.
+*
+* @param status : nsresult
+* the status code to use to cancel this, if this hasn't already been
+* canceled
+*/
+ _cancelOrDispatchCancelCallback: function(status)
+ {
+ dumpn("*** _cancelOrDispatchCancelCallback(" + status + ")");
+
+ NS_ASSERT(this._source === null, "should have finished input");
+ NS_ASSERT(this._sink === null, "should have finished output");
+ NS_ASSERT(this._pendingData.length === 0, "should have no pending data");
+
+ if (!this._canceled)
+ {
+ this.cancel(status);
+ return;
+ }
+
+ var self = this;
+ var event =
+ {
+ run: function()
+ {
+ dumpn("*** onStopRequest async callback");
+
+ self._completed = true;
+ try
+ {
+ self._observer.onStopRequest(self, self._context, self.status);
+ }
+ catch (e)
+ {
+ NS_ASSERT(false,
+ "how are we throwing an exception here? we control " +
+ "all the callers! " + e);
+ }
+ }
+ };
+
+ gThreadManager.currentThread.dispatch(event, Ci.nsIThread.DISPATCH_NORMAL);
+ },
+
+ /**
+* Kicks off another wait for more data to be available from the input stream.
+*/
+ _waitToReadData: function()
+ {
+ dumpn("*** _waitToReadData");
+ this._source.asyncWait(this, 0, Response.SEGMENT_SIZE,
+ gThreadManager.mainThread);
+ },
+
+ /**
+* Kicks off another wait until data can be written to the output stream.
+*/
+ _waitToWriteData: function()
+ {
+ dumpn("*** _waitToWriteData");
+
+ var pendingData = this._pendingData;
+ NS_ASSERT(pendingData.length > 0, "no pending data to write?");
+ NS_ASSERT(pendingData[0].length > 0, "buffered an empty write?");
+
+ this._sink.asyncWait(this, 0, pendingData[0].length,
+ gThreadManager.mainThread);
+ },
+
+ /**
+* Kicks off a wait for the sink to which data is being copied to be closed.
+* We wait for stream closure when we don't have any data to be copied, rather
+* than waiting to write a specific amount of data. We can't wait to write
+* data because the sink might be infinitely writable, and if no data appears
+* in the source for a long time we might have to spin quite a bit waiting to
+* write, waiting to write again, &c. Waiting on stream closure instead means
+* we'll get just one notification if the sink dies. Note that when data
+* starts arriving from the sink we'll resume waiting for data to be written,
+* dropping this closure-only callback entirely.
+*/
+ _waitForSinkClosure: function()
+ {
+ dumpn("*** _waitForSinkClosure");
+
+ this._sink.asyncWait(this, Ci.nsIAsyncOutputStream.WAIT_CLOSURE_ONLY, 0,
+ gThreadManager.mainThread);
+ },
+
+ /**
+* Closes input with the given status, if it hasn't already been closed;
+* otherwise a no-op.
+*
+* @param status : nsresult
+* status code use to close the source stream if necessary
+*/
+ _finishSource: function(status)
+ {
+ dumpn("*** _finishSource(" + status.toString(16) + ")");
+
+ if (this._source !== null)
+ {
+ this._source.closeWithStatus(status);
+ this._source = null;
+ }
+ }
+};
+
+
+/**
+* A container for utility functions used with HTTP headers.
+*/
+const headerUtils =
+{
+ /**
+* Normalizes fieldName (by converting it to lowercase) and ensures it is a
+* valid header field name (although not necessarily one specified in RFC
+* 2616).
+*
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not match the field-name production in RFC 2616
+* @returns string
+* fieldName converted to lowercase if it is a valid header, for characters
+* where case conversion is possible
+*/
+ normalizeFieldName: function(fieldName)
+ {
+ if (fieldName == "")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ for (var i = 0, sz = fieldName.length; i < sz; i++)
+ {
+ if (!IS_TOKEN_ARRAY[fieldName.charCodeAt(i)])
+ {
+ dumpn(fieldName + " is not a valid header field name!");
+ throw Cr.NS_ERROR_INVALID_ARG;
+ }
+ }
+
+ return fieldName.toLowerCase();
+ },
+
+ /**
+* Ensures that fieldValue is a valid header field value (although not
+* necessarily as specified in RFC 2616 if the corresponding field name is
+* part of the HTTP protocol), normalizes the value if it is, and
+* returns the normalized value.
+*
+* @param fieldValue : string
+* a value to be normalized as an HTTP header field value
+* @throws NS_ERROR_INVALID_ARG
+* if fieldValue does not match the field-value production in RFC 2616
+* @returns string
+* fieldValue as a normalized HTTP header field value
+*/
+ normalizeFieldValue: function(fieldValue)
+ {
+ // field-value = *( field-content | LWS )
+ // field-content = <the OCTETs making up the field-value
+ // and consisting of either *TEXT or combinations
+ // of token, separators, and quoted-string>
+ // TEXT = <any OCTET except CTLs,
+ // but including LWS>
+ // LWS = [CRLF] 1*( SP | HT )
+ //
+ // quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
+ // qdtext = <any TEXT except <">>
+ // quoted-pair = "\" CHAR
+ // CHAR = <any US-ASCII character (octets 0 - 127)>
+
+ // Any LWS that occurs between field-content MAY be replaced with a single
+ // SP before interpreting the field value or forwarding the message
+ // downstream (section 4.2); we replace 1*LWS with a single SP
+ var val = fieldValue.replace(/(?:(?:\r\n)?[ \t]+)+/g, " ");
+
+ // remove leading/trailing LWS (which has been converted to SP)
+ val = val.replace(/^ +/, "").replace(/ +$/, "");
+
+ // that should have taken care of all CTLs, so val should contain no CTLs
+ for (var i = 0, len = val.length; i < len; i++)
+ if (isCTL(val.charCodeAt(i)))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ // XXX disallows quoted-pair where CHAR is a CTL -- will not invalidly
+ // normalize, however, so this can be construed as a tightening of the
+ // spec and not entirely as a bug
+ return val;
+ }
+};
+
+
+
+/**
+* Converts the given string into a string which is safe for use in an HTML
+* context.
+*
+* @param str : string
+* the string to make HTML-safe
+* @returns string
+* an HTML-safe version of str
+*/
+function htmlEscape(str)
+{
+ // this is naive, but it'll work
+ var s = "";
+ for (var i = 0; i < str.length; i++)
+ s += "&#" + str.charCodeAt(i) + ";";
+ return s;
+}
+
+
+/**
+* Constructs an object representing an HTTP version (see section 3.1).
+*
+* @param versionString
+* a string of the form "#.#", where # is an non-negative decimal integer with
+* or without leading zeros
+* @throws
+* if versionString does not specify a valid HTTP version number
+*/
+function nsHttpVersion(versionString)
+{
+ var matches = /^(\d+)\.(\d+)$/.exec(versionString);
+ if (!matches)
+ throw "Not a valid HTTP version!";
+
+ /** The major version number of this, as a number. */
+ this.major = parseInt(matches[1], 10);
+
+ /** The minor version number of this, as a number. */
+ this.minor = parseInt(matches[2], 10);
+
+ if (isNaN(this.major) || isNaN(this.minor) ||
+ this.major < 0 || this.minor < 0)
+ throw "Not a valid HTTP version!";
+}
+nsHttpVersion.prototype =
+{
+ /**
+* Returns the standard string representation of the HTTP version represented
+* by this (e.g., "1.1").
+*/
+ toString: function ()
+ {
+ return this.major + "." + this.minor;
+ },
+
+ /**
+* Returns true if this represents the same HTTP version as otherVersion,
+* false otherwise.
+*
+* @param otherVersion : nsHttpVersion
+* the version to compare against this
+*/
+ equals: function (otherVersion)
+ {
+ return this.major == otherVersion.major &&
+ this.minor == otherVersion.minor;
+ },
+
+ /** True if this >= otherVersion, false otherwise. */
+ atLeast: function(otherVersion)
+ {
+ return this.major > otherVersion.major ||
+ (this.major == otherVersion.major &&
+ this.minor >= otherVersion.minor);
+ }
+};
+
+nsHttpVersion.HTTP_1_0 = new nsHttpVersion("1.0");
+nsHttpVersion.HTTP_1_1 = new nsHttpVersion("1.1");
+
+
+/**
+* An object which stores HTTP headers for a request or response.
+*
+* Note that since headers are case-insensitive, this object converts headers to
+* lowercase before storing them. This allows the getHeader and hasHeader
+* methods to work correctly for any case of a header, but it means that the
+* values returned by .enumerator may not be equal case-sensitively to the
+* values passed to setHeader when adding headers to this.
+*/
+function nsHttpHeaders()
+{
+ /**
+* A hash of headers, with header field names as the keys and header field
+* values as the values. Header field names are case-insensitive, but upon
+* insertion here they are converted to lowercase. Header field values are
+* normalized upon insertion to contain no leading or trailing whitespace.
+*
+* Note also that per RFC 2616, section 4.2, two headers with the same name in
+* a message may be treated as one header with the same field name and a field
+* value consisting of the separate field values joined together with a "," in
+* their original order. This hash stores multiple headers with the same name
+* in this manner.
+*/
+ this._headers = {};
+}
+nsHttpHeaders.prototype =
+{
+ /**
+* Sets the header represented by name and value in this.
+*
+* @param name : string
+* the header name
+* @param value : string
+* the header value
+* @throws NS_ERROR_INVALID_ARG
+* if name or value is not a valid header component
+*/
+ setHeader: function(fieldName, fieldValue, merge)
+ {
+ var name = headerUtils.normalizeFieldName(fieldName);
+ var value = headerUtils.normalizeFieldValue(fieldValue);
+
+ // The following three headers are stored as arrays because their real-world
+ // syntax prevents joining individual headers into a single header using
+ // ",". See also <http://hg.mozilla.org/mozilla-central/diff/9b2a99adc05e/netwerk/protocol/http/src/nsHttpHeaderArray.cpp#l77>
+ if (merge && name in this._headers)
+ {
+ if (name === "www-authenticate" ||
+ name === "proxy-authenticate" ||
+ name === "set-cookie")
+ {
+ this._headers[name].push(value);
+ }
+ else
+ {
+ this._headers[name][0] += "," + value;
+ NS_ASSERT(this._headers[name].length === 1,
+ "how'd a non-special header have multiple values?")
+ }
+ }
+ else
+ {
+ this._headers[name] = [value];
+ }
+ },
+
+ /**
+* Returns the value for the header specified by this.
+*
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not constitute a valid header field name
+* @throws NS_ERROR_NOT_AVAILABLE
+* if the given header does not exist in this
+* @returns string
+* the field value for the given header, possibly with non-semantic changes
+* (i.e., leading/trailing whitespace stripped, whitespace runs replaced
+* with spaces, etc.) at the option of the implementation; multiple
+* instances of the header will be combined with a comma, except for
+* the three headers noted in the description of getHeaderValues
+*/
+ getHeader: function(fieldName)
+ {
+ return this.getHeaderValues(fieldName).join("\n");
+ },
+
+ /**
+* Returns the value for the header specified by fieldName as an array.
+*
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not constitute a valid header field name
+* @throws NS_ERROR_NOT_AVAILABLE
+* if the given header does not exist in this
+* @returns [string]
+* an array of all the header values in this for the given
+* header name. Header values will generally be collapsed
+* into a single header by joining all header values together
+* with commas, but certain headers (Proxy-Authenticate,
+* WWW-Authenticate, and Set-Cookie) violate the HTTP spec
+* and cannot be collapsed in this manner. For these headers
+* only, the returned array may contain multiple elements if
+* that header has been added more than once.
+*/
+ getHeaderValues: function(fieldName)
+ {
+ var name = headerUtils.normalizeFieldName(fieldName);
+
+ if (name in this._headers)
+ return this._headers[name];
+ else
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ },
+
+ /**
+* Returns true if a header with the given field name exists in this, false
+* otherwise.
+*
+* @param fieldName : string
+* the field name whose existence is to be determined in this
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not constitute a valid header field name
+* @returns boolean
+* true if the header's present, false otherwise
+*/
+ hasHeader: function(fieldName)
+ {
+ var name = headerUtils.normalizeFieldName(fieldName);
+ return (name in this._headers);
+ },
+
+ /**
+* Returns a new enumerator over the field names of the headers in this, as
+* nsISupportsStrings. The names returned will be in lowercase, regardless of
+* how they were input using setHeader (header names are case-insensitive per
+* RFC 2616).
+*/
+ get enumerator()
+ {
+ var headers = [];
+ for (var i in this._headers)
+ {
+ var supports = new SupportsString();
+ supports.data = i;
+ headers.push(supports);
+ }
+
+ return new nsSimpleEnumerator(headers);
+ }
+};
+
+
+/**
+* Constructs an nsISimpleEnumerator for the given array of items.
+*
+* @param items : Array
+* the items, which must all implement nsISupports
+*/
+function nsSimpleEnumerator(items)
+{
+ this._items = items;
+ this._nextIndex = 0;
+}
+nsSimpleEnumerator.prototype =
+{
+ hasMoreElements: function()
+ {
+ return this._nextIndex < this._items.length;
+ },
+ getNext: function()
+ {
+ if (!this.hasMoreElements())
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+
+ return this._items[this._nextIndex++];
+ },
+ QueryInterface: function(aIID)
+ {
+ if (Ci.nsISimpleEnumerator.equals(aIID) ||
+ Ci.nsISupports.equals(aIID))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ }
+};
+
+
+/**
+* A representation of the data in an HTTP request.
+*
+* @param port : uint
+* the port on which the server receiving this request runs
+*/
+function Request(port)
+{
+ /** Method of this request, e.g. GET or POST. */
+ this._method = "";
+
+ /** Path of the requested resource; empty paths are converted to '/'. */
+ this._path = "";
+
+ /** Query string, if any, associated with this request (not including '?'). */
+ this._queryString = "";
+
+ /** Scheme of requested resource, usually http, always lowercase. */
+ this._scheme = "http";
+
+ /** Hostname on which the requested resource resides. */
+ this._host = undefined;
+
+ /** Port number over which the request was received. */
+ this._port = port;
+
+ var bodyPipe = new Pipe(false, false, 0, PR_UINT32_MAX, null);
+
+ /** Stream from which data in this request's body may be read. */
+ this._bodyInputStream = bodyPipe.inputStream;
+
+ /** Stream to which data in this request's body is written. */
+ this._bodyOutputStream = bodyPipe.outputStream;
+
+ /**
+* The headers in this request.
+*/
+ this._headers = new nsHttpHeaders();
+
+ /**
+* For the addition of ad-hoc properties and new functionality without having
+* to change nsIHttpRequest every time; currently lazily created, as its only
+* use is in directory listings.
+*/
+ this._bag = null;
+}
+Request.prototype =
+{
+ // SERVER METADATA
+
+ //
+ // see nsIHttpRequest.scheme
+ //
+ get scheme()
+ {
+ return this._scheme;
+ },
+
+ //
+ // see nsIHttpRequest.host
+ //
+ get host()
+ {
+ return this._host;
+ },
+
+ //
+ // see nsIHttpRequest.port
+ //
+ get port()
+ {
+ return this._port;
+ },
+
+ // REQUEST LINE
+
+ //
+ // see nsIHttpRequest.method
+ //
+ get method()
+ {
+ return this._method;
+ },
+
+ //
+ // see nsIHttpRequest.httpVersion
+ //
+ get httpVersion()
+ {
+ return this._httpVersion.toString();
+ },
+
+ //
+ // see nsIHttpRequest.path
+ //
+ get path()
+ {
+ return this._path;
+ },
+
+ //
+ // see nsIHttpRequest.queryString
+ //
+ get queryString()
+ {
+ return this._queryString;
+ },
+
+ // HEADERS
+
+ //
+ // see nsIHttpRequest.getHeader
+ //
+ getHeader: function(name)
+ {
+ return this._headers.getHeader(name);
+ },
+
+ //
+ // see nsIHttpRequest.hasHeader
+ //
+ hasHeader: function(name)
+ {
+ return this._headers.hasHeader(name);
+ },
+
+ //
+ // see nsIHttpRequest.headers
+ //
+ get headers()
+ {
+ return this._headers.enumerator;
+ },
+
+ //
+ // see nsIPropertyBag.enumerator
+ //
+ get enumerator()
+ {
+ this._ensurePropertyBag();
+ return this._bag.enumerator;
+ },
+
+ //
+ // see nsIHttpRequest.headers
+ //
+ get bodyInputStream()
+ {
+ return this._bodyInputStream;
+ },
+
+ //
+ // see nsIPropertyBag.getProperty
+ //
+ getProperty: function(name)
+ {
+ this._ensurePropertyBag();
+ return this._bag.getProperty(name);
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIHttpRequest) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // PRIVATE IMPLEMENTATION
+
+ /** Ensures a property bag has been created for ad-hoc behaviors. */
+ _ensurePropertyBag: function()
+ {
+ if (!this._bag)
+ this._bag = new WritablePropertyBag();
+ }
+};
+
+
+// XPCOM trappings
+if ("XPCOMUtils" in this && // Firefox 3.6 doesn't load XPCOMUtils in this scope for some reason...
+ "generateNSGetFactory" in XPCOMUtils) {
+ var NSGetFactory = XPCOMUtils.generateNSGetFactory([nsHttpServer]);
+}
+
+
+
+/**
+* Creates a new HTTP server listening for loopback traffic on the given port,
+* starts it, and runs the server until the server processes a shutdown request,
+* spinning an event loop so that events posted by the server's socket are
+* processed.
+*
+* This method is primarily intended for use in running this script from within
+* xpcshell and running a functional HTTP server without having to deal with
+* non-essential details.
+*
+* Note that running multiple servers using variants of this method probably
+* doesn't work, simply due to how the internal event loop is spun and stopped.
+*
+* @note
+* This method only works with Mozilla 1.9 (i.e., Firefox 3 or trunk code);
+* you should use this server as a component in Mozilla 1.8.
+* @param port
+* the port on which the server will run, or -1 if there exists no preference
+* for a specific port; note that attempting to use some values for this
+* parameter (particularly those below 1024) may cause this method to throw or
+* may result in the server being prematurely shut down
+* @param basePath
+* a local directory from which requests will be served (i.e., if this is
+* "/home/jwalden/" then a request to /index.html will load
+* /home/jwalden/index.html); if this is omitted, only the default URLs in
+* this server implementation will be functional
+*/
+function server(port, basePath)
+{
+ if (basePath)
+ {
+ var lp = Cc["@mozilla.org/file/local;1"]
+ .createInstance(Ci.nsILocalFile);
+ lp.initWithPath(basePath);
+ }
+
+ // if you're running this, you probably want to see debugging info
+ DEBUG = true;
+
+ var srv = new nsHttpServer();
+ if (lp)
+ srv.registerDirectory("/", lp);
+ srv.registerContentType("sjs", SJS_TYPE);
+ srv.start(port);
+
+ var thread = gThreadManager.currentThread;
+ while (!srv.isStopped())
+ thread.processNextEvent(true);
+
+ // get rid of any pending requests
+ while (thread.hasPendingEvents())
+ thread.processNextEvent(true);
+
+ DEBUG = false;
+}
+
+function startServerAsync(port, basePath)
+{
+ if (basePath)
+ {
+ var lp = Cc["@mozilla.org/file/local;1"]
+ .createInstance(Ci.nsILocalFile);
+ lp.initWithPath(basePath);
+ }
+
+ var srv = new nsHttpServer();
+ if (lp)
+ srv.registerDirectory("/", lp);
+ srv.registerContentType("sjs", "sjs");
+ srv.start(port);
+ return srv;
+}
+
+exports.nsHttpServer = nsHttpServer;
+exports.ScriptableInputStream = ScriptableInputStream;
+exports.server = server;
+exports.startServerAsync = startServerAsync;
diff --git a/addon-sdk/source/test/addons/content-permissions/main.js b/addon-sdk/source/test/addons/content-permissions/main.js
new file mode 100644
index 000000000..b476ccb74
--- /dev/null
+++ b/addon-sdk/source/test/addons/content-permissions/main.js
@@ -0,0 +1,89 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { PageMod } = require("sdk/page-mod");
+const tabs = require("sdk/tabs");
+const { startServerAsync } = require("./httpd");
+
+const serverPort = 8099;
+const TEST_TAB_URL = "about:mozilla";
+
+exports.testCrossDomainIframe = function(assert, done) {
+ let server = startServerAsync(serverPort);
+ server.registerPathHandler("/iframe", function handle(request, response) {
+ response.write("<html><body>foo</body></html>");
+ });
+
+ let pageMod = PageMod({
+ include: TEST_TAB_URL,
+ contentScript: "new " + function ContentScriptScope() {
+ self.on("message", function (url) {
+ let iframe = document.createElement("iframe");
+ iframe.addEventListener("load", function onload() {
+ iframe.removeEventListener("load", onload, false);
+ self.postMessage(iframe.contentWindow.document.body.innerHTML);
+ }, false);
+ iframe.setAttribute("src", url);
+ document.documentElement.appendChild(iframe);
+ });
+ },
+ onAttach: function(w) {
+ w.on("message", function (body) {
+ assert.equal(body, "foo", "received iframe html content");
+ pageMod.destroy();
+ w.tab.close(function() {
+ server.stop(done);
+ });
+ });
+
+ w.postMessage("http://localhost:" + serverPort + "/iframe");
+ }
+ });
+
+ tabs.open({
+ url: TEST_TAB_URL,
+ inBackground: true
+ });
+};
+
+exports.testCrossDomainXHR = function(assert, done) {
+ let server = startServerAsync(serverPort);
+ server.registerPathHandler("/xhr", function handle(request, response) {
+ response.write("foo");
+ });
+
+ let pageMod = PageMod({
+ include: TEST_TAB_URL,
+ contentScript: "new " + function ContentScriptScope() {
+ self.on("message", function (url) {
+ let request = new XMLHttpRequest();
+ request.overrideMimeType("text/plain");
+ request.open("GET", url, true);
+ request.onload = function () {
+ self.postMessage(request.responseText);
+ };
+ request.send(null);
+ });
+ },
+ onAttach: function(w) {
+ w.on("message", function (body) {
+ assert.equal(body, "foo", "received XHR content");
+ pageMod.destroy();
+ w.tab.close(function() {
+ server.stop(done);
+ });
+ });
+
+ w.postMessage("http://localhost:" + serverPort + "/xhr");
+ }
+ });
+
+ tabs.open({
+ url: TEST_TAB_URL,
+ inBackground: true
+ });
+};
+
+require("sdk/test/runner").runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/content-permissions/package.json b/addon-sdk/source/test/addons/content-permissions/package.json
new file mode 100644
index 000000000..6e75d2044
--- /dev/null
+++ b/addon-sdk/source/test/addons/content-permissions/package.json
@@ -0,0 +1,8 @@
+{
+ "id": "content-permissions@jetpack",
+ "permissions": {
+ "cross-domain-content": ["http://localhost:8099"]
+ },
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/content-script-messages-latency/httpd.js b/addon-sdk/source/test/addons/content-script-messages-latency/httpd.js
new file mode 100644
index 000000000..964dc9bbd
--- /dev/null
+++ b/addon-sdk/source/test/addons/content-script-messages-latency/httpd.js
@@ -0,0 +1,5211 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/*
+* NOTE: do not edit this file, this is copied from:
+* https://github.com/mozilla/addon-sdk/blob/master/test/lib/httpd.js
+*/
+
+module.metadata = {
+ "stability": "experimental"
+};
+
+const { components, CC, Cc, Ci, Cr, Cu } = require("chrome");
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+
+const PR_UINT32_MAX = Math.pow(2, 32) - 1;
+
+/** True if debugging output is enabled, false otherwise. */
+var DEBUG = false; // non-const *only* so tweakable in server tests
+
+/** True if debugging output should be timestamped. */
+var DEBUG_TIMESTAMP = false; // non-const so tweakable in server tests
+
+var gGlobalObject = Cc["@mozilla.org/systemprincipal;1"].createInstance();
+
+/**
+* Asserts that the given condition holds. If it doesn't, the given message is
+* dumped, a stack trace is printed, and an exception is thrown to attempt to
+* stop execution (which unfortunately must rely upon the exception not being
+* accidentally swallowed by the code that uses it).
+*/
+function NS_ASSERT(cond, msg)
+{
+ if (DEBUG && !cond)
+ {
+ dumpn("###!!!");
+ dumpn("###!!! ASSERTION" + (msg ? ": " + msg : "!"));
+ dumpn("###!!! Stack follows:");
+
+ var stack = new Error().stack.split(/\n/);
+ dumpn(stack.map(function(val) { return "###!!! " + val; }).join("\n"));
+
+ throw Cr.NS_ERROR_ABORT;
+ }
+}
+
+/** Constructs an HTTP error object. */
+function HttpError(code, description)
+{
+ this.code = code;
+ this.description = description;
+}
+HttpError.prototype =
+{
+ toString: function()
+ {
+ return this.code + " " + this.description;
+ }
+};
+
+/**
+* Errors thrown to trigger specific HTTP server responses.
+*/
+const HTTP_400 = new HttpError(400, "Bad Request");
+const HTTP_401 = new HttpError(401, "Unauthorized");
+const HTTP_402 = new HttpError(402, "Payment Required");
+const HTTP_403 = new HttpError(403, "Forbidden");
+const HTTP_404 = new HttpError(404, "Not Found");
+const HTTP_405 = new HttpError(405, "Method Not Allowed");
+const HTTP_406 = new HttpError(406, "Not Acceptable");
+const HTTP_407 = new HttpError(407, "Proxy Authentication Required");
+const HTTP_408 = new HttpError(408, "Request Timeout");
+const HTTP_409 = new HttpError(409, "Conflict");
+const HTTP_410 = new HttpError(410, "Gone");
+const HTTP_411 = new HttpError(411, "Length Required");
+const HTTP_412 = new HttpError(412, "Precondition Failed");
+const HTTP_413 = new HttpError(413, "Request Entity Too Large");
+const HTTP_414 = new HttpError(414, "Request-URI Too Long");
+const HTTP_415 = new HttpError(415, "Unsupported Media Type");
+const HTTP_417 = new HttpError(417, "Expectation Failed");
+
+const HTTP_500 = new HttpError(500, "Internal Server Error");
+const HTTP_501 = new HttpError(501, "Not Implemented");
+const HTTP_502 = new HttpError(502, "Bad Gateway");
+const HTTP_503 = new HttpError(503, "Service Unavailable");
+const HTTP_504 = new HttpError(504, "Gateway Timeout");
+const HTTP_505 = new HttpError(505, "HTTP Version Not Supported");
+
+/** Creates a hash with fields corresponding to the values in arr. */
+function array2obj(arr)
+{
+ var obj = {};
+ for (var i = 0; i < arr.length; i++)
+ obj[arr[i]] = arr[i];
+ return obj;
+}
+
+/** Returns an array of the integers x through y, inclusive. */
+function range(x, y)
+{
+ var arr = [];
+ for (var i = x; i <= y; i++)
+ arr.push(i);
+ return arr;
+}
+
+/** An object (hash) whose fields are the numbers of all HTTP error codes. */
+const HTTP_ERROR_CODES = array2obj(range(400, 417).concat(range(500, 505)));
+
+
+/**
+* The character used to distinguish hidden files from non-hidden files, a la
+* the leading dot in Apache. Since that mechanism also hides files from
+* easy display in LXR, ls output, etc. however, we choose instead to use a
+* suffix character. If a requested file ends with it, we append another
+* when getting the file on the server. If it doesn't, we just look up that
+* file. Therefore, any file whose name ends with exactly one of the character
+* is "hidden" and available for use by the server.
+*/
+const HIDDEN_CHAR = "^";
+
+/**
+* The file name suffix indicating the file containing overridden headers for
+* a requested file.
+*/
+const HEADERS_SUFFIX = HIDDEN_CHAR + "headers" + HIDDEN_CHAR;
+
+/** Type used to denote SJS scripts for CGI-like functionality. */
+const SJS_TYPE = "sjs";
+
+/** Base for relative timestamps produced by dumpn(). */
+var firstStamp = 0;
+
+/** dump(str) with a trailing "\n" -- only outputs if DEBUG. */
+function dumpn(str)
+{
+ if (DEBUG)
+ {
+ var prefix = "HTTPD-INFO | ";
+ if (DEBUG_TIMESTAMP)
+ {
+ if (firstStamp === 0)
+ firstStamp = Date.now();
+
+ var elapsed = Date.now() - firstStamp; // milliseconds
+ var min = Math.floor(elapsed / 60000);
+ var sec = (elapsed % 60000) / 1000;
+
+ if (sec < 10)
+ prefix += min + ":0" + sec.toFixed(3) + " | ";
+ else
+ prefix += min + ":" + sec.toFixed(3) + " | ";
+ }
+
+ dump(prefix + str + "\n");
+ }
+}
+
+/** Dumps the current JS stack if DEBUG. */
+function dumpStack()
+{
+ // peel off the frames for dumpStack() and Error()
+ var stack = new Error().stack.split(/\n/).slice(2);
+ stack.forEach(dumpn);
+}
+
+
+/** The XPCOM thread manager. */
+var gThreadManager = null;
+
+/** The XPCOM prefs service. */
+var gRootPrefBranch = null;
+function getRootPrefBranch()
+{
+ if (!gRootPrefBranch)
+ {
+ gRootPrefBranch = Cc["@mozilla.org/preferences-service;1"]
+ .getService(Ci.nsIPrefBranch);
+ }
+ return gRootPrefBranch;
+}
+
+/**
+* JavaScript constructors for commonly-used classes; precreating these is a
+* speedup over doing the same from base principles. See the docs at
+* http://developer.mozilla.org/en/docs/components.Constructor for details.
+*/
+const ServerSocket = CC("@mozilla.org/network/server-socket;1",
+ "nsIServerSocket",
+ "init");
+const ScriptableInputStream = CC("@mozilla.org/scriptableinputstream;1",
+ "nsIScriptableInputStream",
+ "init");
+const Pipe = CC("@mozilla.org/pipe;1",
+ "nsIPipe",
+ "init");
+const FileInputStream = CC("@mozilla.org/network/file-input-stream;1",
+ "nsIFileInputStream",
+ "init");
+const ConverterInputStream = CC("@mozilla.org/intl/converter-input-stream;1",
+ "nsIConverterInputStream",
+ "init");
+const WritablePropertyBag = CC("@mozilla.org/hash-property-bag;1",
+ "nsIWritablePropertyBag2");
+const SupportsString = CC("@mozilla.org/supports-string;1",
+ "nsISupportsString");
+
+/* These two are non-const only so a test can overwrite them. */
+var BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
+ "nsIBinaryInputStream",
+ "setInputStream");
+var BinaryOutputStream = CC("@mozilla.org/binaryoutputstream;1",
+ "nsIBinaryOutputStream",
+ "setOutputStream");
+
+/**
+* Returns the RFC 822/1123 representation of a date.
+*
+* @param date : Number
+* the date, in milliseconds from midnight (00:00:00), January 1, 1970 GMT
+* @returns string
+* the representation of the given date
+*/
+function toDateString(date)
+{
+ //
+ // rfc1123-date = wkday "," SP date1 SP time SP "GMT"
+ // date1 = 2DIGIT SP month SP 4DIGIT
+ // ; day month year (e.g., 02 Jun 1982)
+ // time = 2DIGIT ":" 2DIGIT ":" 2DIGIT
+ // ; 00:00:00 - 23:59:59
+ // wkday = "Mon" | "Tue" | "Wed"
+ // | "Thu" | "Fri" | "Sat" | "Sun"
+ // month = "Jan" | "Feb" | "Mar" | "Apr"
+ // | "May" | "Jun" | "Jul" | "Aug"
+ // | "Sep" | "Oct" | "Nov" | "Dec"
+ //
+
+ const wkdayStrings = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
+ const monthStrings = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
+
+ /**
+* Processes a date and returns the encoded UTC time as a string according to
+* the format specified in RFC 2616.
+*
+* @param date : Date
+* the date to process
+* @returns string
+* a string of the form "HH:MM:SS", ranging from "00:00:00" to "23:59:59"
+*/
+ function toTime(date)
+ {
+ var hrs = date.getUTCHours();
+ var rv = (hrs < 10) ? "0" + hrs : hrs;
+
+ var mins = date.getUTCMinutes();
+ rv += ":";
+ rv += (mins < 10) ? "0" + mins : mins;
+
+ var secs = date.getUTCSeconds();
+ rv += ":";
+ rv += (secs < 10) ? "0" + secs : secs;
+
+ return rv;
+ }
+
+ /**
+* Processes a date and returns the encoded UTC date as a string according to
+* the date1 format specified in RFC 2616.
+*
+* @param date : Date
+* the date to process
+* @returns string
+* a string of the form "HH:MM:SS", ranging from "00:00:00" to "23:59:59"
+*/
+ function toDate1(date)
+ {
+ var day = date.getUTCDate();
+ var month = date.getUTCMonth();
+ var year = date.getUTCFullYear();
+
+ var rv = (day < 10) ? "0" + day : day;
+ rv += " " + monthStrings[month];
+ rv += " " + year;
+
+ return rv;
+ }
+
+ date = new Date(date);
+
+ const fmtString = "%wkday%, %date1% %time% GMT";
+ var rv = fmtString.replace("%wkday%", wkdayStrings[date.getUTCDay()]);
+ rv = rv.replace("%time%", toTime(date));
+ return rv.replace("%date1%", toDate1(date));
+}
+
+/**
+* Prints out a human-readable representation of the object o and its fields,
+* omitting those whose names begin with "_" if showMembers != true (to ignore
+* "private" properties exposed via getters/setters).
+*/
+function printObj(o, showMembers)
+{
+ var s = "******************************\n";
+ s += "o = {\n";
+ for (var i in o)
+ {
+ if (typeof(i) != "string" ||
+ (showMembers || (i.length > 0 && i[0] != "_")))
+ s+= " " + i + ": " + o[i] + ",\n";
+ }
+ s += " };\n";
+ s += "******************************";
+ dumpn(s);
+}
+
+/**
+* Instantiates a new HTTP server.
+*/
+function nsHttpServer()
+{
+ if (!gThreadManager)
+ gThreadManager = Cc["@mozilla.org/thread-manager;1"].getService();
+
+ /** The port on which this server listens. */
+ this._port = undefined;
+
+ /** The socket associated with this. */
+ this._socket = null;
+
+ /** The handler used to process requests to this server. */
+ this._handler = new ServerHandler(this);
+
+ /** Naming information for this server. */
+ this._identity = new ServerIdentity();
+
+ /**
+* Indicates when the server is to be shut down at the end of the request.
+*/
+ this._doQuit = false;
+
+ /**
+* True if the socket in this is closed (and closure notifications have been
+* sent and processed if the socket was ever opened), false otherwise.
+*/
+ this._socketClosed = true;
+
+ /**
+* Used for tracking existing connections and ensuring that all connections
+* are properly cleaned up before server shutdown; increases by 1 for every
+* new incoming connection.
+*/
+ this._connectionGen = 0;
+
+ /**
+* Hash of all open connections, indexed by connection number at time of
+* creation.
+*/
+ this._connections = {};
+}
+nsHttpServer.prototype =
+{
+ classID: components.ID("{54ef6f81-30af-4b1d-ac55-8ba811293e41}"),
+
+ // NSISERVERSOCKETLISTENER
+
+ /**
+* Processes an incoming request coming in on the given socket and contained
+* in the given transport.
+*
+* @param socket : nsIServerSocket
+* the socket through which the request was served
+* @param trans : nsISocketTransport
+* the transport for the request/response
+* @see nsIServerSocketListener.onSocketAccepted
+*/
+ onSocketAccepted: function(socket, trans)
+ {
+ dumpn("*** onSocketAccepted(socket=" + socket + ", trans=" + trans + ")");
+
+ dumpn(">>> new connection on " + trans.host + ":" + trans.port);
+
+ const SEGMENT_SIZE = 8192;
+ const SEGMENT_COUNT = 1024;
+ try
+ {
+ var input = trans.openInputStream(0, SEGMENT_SIZE, SEGMENT_COUNT)
+ .QueryInterface(Ci.nsIAsyncInputStream);
+ var output = trans.openOutputStream(0, 0, 0);
+ }
+ catch (e)
+ {
+ dumpn("*** error opening transport streams: " + e);
+ trans.close(Cr.NS_BINDING_ABORTED);
+ return;
+ }
+
+ var connectionNumber = ++this._connectionGen;
+
+ try
+ {
+ var conn = new Connection(input, output, this, socket.port, trans.port,
+ connectionNumber);
+ var reader = new RequestReader(conn);
+
+ // XXX add request timeout functionality here!
+
+ // Note: must use main thread here, or we might get a GC that will cause
+ // threadsafety assertions. We really need to fix XPConnect so that
+ // you can actually do things in multi-threaded JS. :-(
+ input.asyncWait(reader, 0, 0, gThreadManager.mainThread);
+ }
+ catch (e)
+ {
+ // Assume this connection can't be salvaged and bail on it completely;
+ // don't attempt to close it so that we can assert that any connection
+ // being closed is in this._connections.
+ dumpn("*** error in initial request-processing stages: " + e);
+ trans.close(Cr.NS_BINDING_ABORTED);
+ return;
+ }
+
+ this._connections[connectionNumber] = conn;
+ dumpn("*** starting connection " + connectionNumber);
+ },
+
+ /**
+* Called when the socket associated with this is closed.
+*
+* @param socket : nsIServerSocket
+* the socket being closed
+* @param status : nsresult
+* the reason the socket stopped listening (NS_BINDING_ABORTED if the server
+* was stopped using nsIHttpServer.stop)
+* @see nsIServerSocketListener.onStopListening
+*/
+ onStopListening: function(socket, status)
+ {
+ dumpn(">>> shutting down server on port " + socket.port);
+ this._socketClosed = true;
+ if (!this._hasOpenConnections())
+ {
+ dumpn("*** no open connections, notifying async from onStopListening");
+
+ // Notify asynchronously so that any pending teardown in stop() has a
+ // chance to run first.
+ var self = this;
+ var stopEvent =
+ {
+ run: function()
+ {
+ dumpn("*** _notifyStopped async callback");
+ self._notifyStopped();
+ }
+ };
+ gThreadManager.currentThread
+ .dispatch(stopEvent, Ci.nsIThread.DISPATCH_NORMAL);
+ }
+ },
+
+ // NSIHTTPSERVER
+
+ //
+ // see nsIHttpServer.start
+ //
+ start: function(port)
+ {
+ this._start(port, "localhost")
+ },
+
+ _start: function(port, host)
+ {
+ if (this._socket)
+ throw Cr.NS_ERROR_ALREADY_INITIALIZED;
+
+ this._port = port;
+ this._doQuit = this._socketClosed = false;
+
+ this._host = host;
+
+ // The listen queue needs to be long enough to handle
+ // network.http.max-persistent-connections-per-server concurrent connections,
+ // plus a safety margin in case some other process is talking to
+ // the server as well.
+ var prefs = getRootPrefBranch();
+ var maxConnections;
+ try {
+ // Bug 776860: The original pref was removed in favor of this new one:
+ maxConnections = prefs.getIntPref("network.http.max-persistent-connections-per-server") + 5;
+ }
+ catch(e) {
+ maxConnections = prefs.getIntPref("network.http.max-connections-per-server") + 5;
+ }
+
+ try
+ {
+ var loopback = true;
+ if (this._host != "127.0.0.1" && this._host != "localhost") {
+ var loopback = false;
+ }
+
+ var socket = new ServerSocket(this._port,
+ loopback, // true = localhost, false = everybody
+ maxConnections);
+ dumpn(">>> listening on port " + socket.port + ", " + maxConnections +
+ " pending connections");
+ socket.asyncListen(this);
+ this._identity._initialize(socket.port, host, true);
+ this._socket = socket;
+ }
+ catch (e)
+ {
+ dumpn("!!! could not start server on port " + port + ": " + e);
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ }
+ },
+
+ //
+ // see nsIHttpServer.stop
+ //
+ stop: function(callback)
+ {
+ if (!callback)
+ throw Cr.NS_ERROR_NULL_POINTER;
+ if (!this._socket)
+ throw Cr.NS_ERROR_UNEXPECTED;
+
+ this._stopCallback = typeof callback === "function"
+ ? callback
+ : function() { callback.onStopped(); };
+
+ dumpn(">>> stopping listening on port " + this._socket.port);
+ this._socket.close();
+ this._socket = null;
+
+ // We can't have this identity any more, and the port on which we're running
+ // this server now could be meaningless the next time around.
+ this._identity._teardown();
+
+ this._doQuit = false;
+
+ // socket-close notification and pending request completion happen async
+ },
+
+ //
+ // see nsIHttpServer.registerFile
+ //
+ registerFile: function(path, file)
+ {
+ if (file && (!file.exists() || file.isDirectory()))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ this._handler.registerFile(path, file);
+ },
+
+ //
+ // see nsIHttpServer.registerDirectory
+ //
+ registerDirectory: function(path, directory)
+ {
+ // XXX true path validation!
+ if (path.charAt(0) != "/" ||
+ path.charAt(path.length - 1) != "/" ||
+ (directory &&
+ (!directory.exists() || !directory.isDirectory())))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ // XXX determine behavior of nonexistent /foo/bar when a /foo/bar/ mapping
+ // exists!
+
+ this._handler.registerDirectory(path, directory);
+ },
+
+ //
+ // see nsIHttpServer.registerPathHandler
+ //
+ registerPathHandler: function(path, handler)
+ {
+ this._handler.registerPathHandler(path, handler);
+ },
+
+ //
+ // see nsIHttpServer.registerPrefixHandler
+ //
+ registerPrefixHandler: function(prefix, handler)
+ {
+ this._handler.registerPrefixHandler(prefix, handler);
+ },
+
+ //
+ // see nsIHttpServer.registerErrorHandler
+ //
+ registerErrorHandler: function(code, handler)
+ {
+ this._handler.registerErrorHandler(code, handler);
+ },
+
+ //
+ // see nsIHttpServer.setIndexHandler
+ //
+ setIndexHandler: function(handler)
+ {
+ this._handler.setIndexHandler(handler);
+ },
+
+ //
+ // see nsIHttpServer.registerContentType
+ //
+ registerContentType: function(ext, type)
+ {
+ this._handler.registerContentType(ext, type);
+ },
+
+ //
+ // see nsIHttpServer.serverIdentity
+ //
+ get identity()
+ {
+ return this._identity;
+ },
+
+ //
+ // see nsIHttpServer.getState
+ //
+ getState: function(path, k)
+ {
+ return this._handler._getState(path, k);
+ },
+
+ //
+ // see nsIHttpServer.setState
+ //
+ setState: function(path, k, v)
+ {
+ return this._handler._setState(path, k, v);
+ },
+
+ //
+ // see nsIHttpServer.getSharedState
+ //
+ getSharedState: function(k)
+ {
+ return this._handler._getSharedState(k);
+ },
+
+ //
+ // see nsIHttpServer.setSharedState
+ //
+ setSharedState: function(k, v)
+ {
+ return this._handler._setSharedState(k, v);
+ },
+
+ //
+ // see nsIHttpServer.getObjectState
+ //
+ getObjectState: function(k)
+ {
+ return this._handler._getObjectState(k);
+ },
+
+ //
+ // see nsIHttpServer.setObjectState
+ //
+ setObjectState: function(k, v)
+ {
+ return this._handler._setObjectState(k, v);
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIServerSocketListener) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // NON-XPCOM PUBLIC API
+
+ /**
+* Returns true iff this server is not running (and is not in the process of
+* serving any requests still to be processed when the server was last
+* stopped after being run).
+*/
+ isStopped: function()
+ {
+ return this._socketClosed && !this._hasOpenConnections();
+ },
+
+ // PRIVATE IMPLEMENTATION
+
+ /** True if this server has any open connections to it, false otherwise. */
+ _hasOpenConnections: function()
+ {
+ //
+ // If we have any open connections, they're tracked as numeric properties on
+ // |this._connections|. The non-standard __count__ property could be used
+ // to check whether there are any properties, but standard-wise, even
+ // looking forward to ES5, there's no less ugly yet still O(1) way to do
+ // this.
+ //
+ for (var n in this._connections)
+ return true;
+ return false;
+ },
+
+ /** Calls the server-stopped callback provided when stop() was called. */
+ _notifyStopped: function()
+ {
+ NS_ASSERT(this._stopCallback !== null, "double-notifying?");
+ NS_ASSERT(!this._hasOpenConnections(), "should be done serving by now");
+
+ //
+ // NB: We have to grab this now, null out the member, *then* call the
+ // callback here, or otherwise the callback could (indirectly) futz with
+ // this._stopCallback by starting and immediately stopping this, at
+ // which point we'd be nulling out a field we no longer have a right to
+ // modify.
+ //
+ var callback = this._stopCallback;
+ this._stopCallback = null;
+ try
+ {
+ callback();
+ }
+ catch (e)
+ {
+ // not throwing because this is specified as being usually (but not
+ // always) asynchronous
+ dump("!!! error running onStopped callback: " + e + "\n");
+ }
+ },
+
+ /**
+* Notifies this server that the given connection has been closed.
+*
+* @param connection : Connection
+* the connection that was closed
+*/
+ _connectionClosed: function(connection)
+ {
+ NS_ASSERT(connection.number in this._connections,
+ "closing a connection " + this + " that we never added to the " +
+ "set of open connections?");
+ NS_ASSERT(this._connections[connection.number] === connection,
+ "connection number mismatch? " +
+ this._connections[connection.number]);
+ delete this._connections[connection.number];
+
+ // Fire a pending server-stopped notification if it's our responsibility.
+ if (!this._hasOpenConnections() && this._socketClosed)
+ this._notifyStopped();
+ },
+
+ /**
+* Requests that the server be shut down when possible.
+*/
+ _requestQuit: function()
+ {
+ dumpn(">>> requesting a quit");
+ dumpStack();
+ this._doQuit = true;
+ }
+};
+
+
+//
+// RFC 2396 section 3.2.2:
+//
+// host = hostname | IPv4address
+// hostname = *( domainlabel "." ) toplabel [ "." ]
+// domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
+// toplabel = alpha | alpha *( alphanum | "-" ) alphanum
+// IPv4address = 1*digit "." 1*digit "." 1*digit "." 1*digit
+//
+
+const HOST_REGEX =
+ new RegExp("^(?:" +
+ // *( domainlabel "." )
+ "(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)*" +
+ // toplabel
+ "[a-z](?:[a-z0-9-]*[a-z0-9])?" +
+ "|" +
+ // IPv4 address
+ "\\d+\\.\\d+\\.\\d+\\.\\d+" +
+ ")$",
+ "i");
+
+
+/**
+* Represents the identity of a server. An identity consists of a set of
+* (scheme, host, port) tuples denoted as locations (allowing a single server to
+* serve multiple sites or to be used behind both HTTP and HTTPS proxies for any
+* host/port). Any incoming request must be to one of these locations, or it
+* will be rejected with an HTTP 400 error. One location, denoted as the
+* primary location, is the location assigned in contexts where a location
+* cannot otherwise be endogenously derived, such as for HTTP/1.0 requests.
+*
+* A single identity may contain at most one location per unique host/port pair;
+* other than that, no restrictions are placed upon what locations may
+* constitute an identity.
+*/
+function ServerIdentity()
+{
+ /** The scheme of the primary location. */
+ this._primaryScheme = "http";
+
+ /** The hostname of the primary location. */
+ this._primaryHost = "127.0.0.1"
+
+ /** The port number of the primary location. */
+ this._primaryPort = -1;
+
+ /**
+* The current port number for the corresponding server, stored so that a new
+* primary location can always be set if the current one is removed.
+*/
+ this._defaultPort = -1;
+
+ /**
+* Maps hosts to maps of ports to schemes, e.g. the following would represent
+* https://example.com:789/ and http://example.org/:
+*
+* {
+* "xexample.com": { 789: "https" },
+* "xexample.org": { 80: "http" }
+* }
+*
+* Note the "x" prefix on hostnames, which prevents collisions with special
+* JS names like "prototype".
+*/
+ this._locations = { "xlocalhost": {} };
+}
+ServerIdentity.prototype =
+{
+ // NSIHTTPSERVERIDENTITY
+
+ //
+ // see nsIHttpServerIdentity.primaryScheme
+ //
+ get primaryScheme()
+ {
+ if (this._primaryPort === -1)
+ throw Cr.NS_ERROR_NOT_INITIALIZED;
+ return this._primaryScheme;
+ },
+
+ //
+ // see nsIHttpServerIdentity.primaryHost
+ //
+ get primaryHost()
+ {
+ if (this._primaryPort === -1)
+ throw Cr.NS_ERROR_NOT_INITIALIZED;
+ return this._primaryHost;
+ },
+
+ //
+ // see nsIHttpServerIdentity.primaryPort
+ //
+ get primaryPort()
+ {
+ if (this._primaryPort === -1)
+ throw Cr.NS_ERROR_NOT_INITIALIZED;
+ return this._primaryPort;
+ },
+
+ //
+ // see nsIHttpServerIdentity.add
+ //
+ add: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ var entry = this._locations["x" + host];
+ if (!entry)
+ this._locations["x" + host] = entry = {};
+
+ entry[port] = scheme;
+ },
+
+ //
+ // see nsIHttpServerIdentity.remove
+ //
+ remove: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ var entry = this._locations["x" + host];
+ if (!entry)
+ return false;
+
+ var present = port in entry;
+ delete entry[port];
+
+ if (this._primaryScheme == scheme &&
+ this._primaryHost == host &&
+ this._primaryPort == port &&
+ this._defaultPort !== -1)
+ {
+ // Always keep at least one identity in existence at any time, unless
+ // we're in the process of shutting down (the last condition above).
+ this._primaryPort = -1;
+ this._initialize(this._defaultPort, host, false);
+ }
+
+ return present;
+ },
+
+ //
+ // see nsIHttpServerIdentity.has
+ //
+ has: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ return "x" + host in this._locations &&
+ scheme === this._locations["x" + host][port];
+ },
+
+ //
+ // see nsIHttpServerIdentity.has
+ //
+ getScheme: function(host, port)
+ {
+ this._validate("http", host, port);
+
+ var entry = this._locations["x" + host];
+ if (!entry)
+ return "";
+
+ return entry[port] || "";
+ },
+
+ //
+ // see nsIHttpServerIdentity.setPrimary
+ //
+ setPrimary: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ this.add(scheme, host, port);
+
+ this._primaryScheme = scheme;
+ this._primaryHost = host;
+ this._primaryPort = port;
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIHttpServerIdentity) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // PRIVATE IMPLEMENTATION
+
+ /**
+* Initializes the primary name for the corresponding server, based on the
+* provided port number.
+*/
+ _initialize: function(port, host, addSecondaryDefault)
+ {
+ this._host = host;
+ if (this._primaryPort !== -1)
+ this.add("http", host, port);
+ else
+ this.setPrimary("http", "localhost", port);
+ this._defaultPort = port;
+
+ // Only add this if we're being called at server startup
+ if (addSecondaryDefault && host != "127.0.0.1")
+ this.add("http", "127.0.0.1", port);
+ },
+
+ /**
+* Called at server shutdown time, unsets the primary location only if it was
+* the default-assigned location and removes the default location from the
+* set of locations used.
+*/
+ _teardown: function()
+ {
+ if (this._host != "127.0.0.1") {
+ // Not the default primary location, nothing special to do here
+ this.remove("http", "127.0.0.1", this._defaultPort);
+ }
+
+ // This is a *very* tricky bit of reasoning here; make absolutely sure the
+ // tests for this code pass before you commit changes to it.
+ if (this._primaryScheme == "http" &&
+ this._primaryHost == this._host &&
+ this._primaryPort == this._defaultPort)
+ {
+ // Make sure we don't trigger the readding logic in .remove(), then remove
+ // the default location.
+ var port = this._defaultPort;
+ this._defaultPort = -1;
+ this.remove("http", this._host, port);
+
+ // Ensure a server start triggers the setPrimary() path in ._initialize()
+ this._primaryPort = -1;
+ }
+ else
+ {
+ // No reason not to remove directly as it's not our primary location
+ this.remove("http", this._host, this._defaultPort);
+ }
+ },
+
+ /**
+* Ensures scheme, host, and port are all valid with respect to RFC 2396.
+*
+* @throws NS_ERROR_ILLEGAL_VALUE
+* if any argument doesn't match the corresponding production
+*/
+ _validate: function(scheme, host, port)
+ {
+ if (scheme !== "http" && scheme !== "https")
+ {
+ dumpn("*** server only supports http/https schemes: '" + scheme + "'");
+ dumpStack();
+ throw Cr.NS_ERROR_ILLEGAL_VALUE;
+ }
+ if (!HOST_REGEX.test(host))
+ {
+ dumpn("*** unexpected host: '" + host + "'");
+ throw Cr.NS_ERROR_ILLEGAL_VALUE;
+ }
+ if (port < 0 || port > 65535)
+ {
+ dumpn("*** unexpected port: '" + port + "'");
+ throw Cr.NS_ERROR_ILLEGAL_VALUE;
+ }
+ }
+};
+
+
+/**
+* Represents a connection to the server (and possibly in the future the thread
+* on which the connection is processed).
+*
+* @param input : nsIInputStream
+* stream from which incoming data on the connection is read
+* @param output : nsIOutputStream
+* stream to write data out the connection
+* @param server : nsHttpServer
+* the server handling the connection
+* @param port : int
+* the port on which the server is running
+* @param outgoingPort : int
+* the outgoing port used by this connection
+* @param number : uint
+* a serial number used to uniquely identify this connection
+*/
+function Connection(input, output, server, port, outgoingPort, number)
+{
+ dumpn("*** opening new connection " + number + " on port " + outgoingPort);
+
+ /** Stream of incoming data. */
+ this.input = input;
+
+ /** Stream for outgoing data. */
+ this.output = output;
+
+ /** The server associated with this request. */
+ this.server = server;
+
+ /** The port on which the server is running. */
+ this.port = port;
+
+ /** The outgoing poort used by this connection. */
+ this._outgoingPort = outgoingPort;
+
+ /** The serial number of this connection. */
+ this.number = number;
+
+ /**
+* The request for which a response is being generated, null if the
+* incoming request has not been fully received or if it had errors.
+*/
+ this.request = null;
+
+ /** State variables for debugging. */
+ this._closed = this._processed = false;
+}
+Connection.prototype =
+{
+ /** Closes this connection's input/output streams. */
+ close: function()
+ {
+ dumpn("*** closing connection " + this.number +
+ " on port " + this._outgoingPort);
+
+ this.input.close();
+ this.output.close();
+ this._closed = true;
+
+ var server = this.server;
+ server._connectionClosed(this);
+
+ // If an error triggered a server shutdown, act on it now
+ if (server._doQuit)
+ server.stop(function() { /* not like we can do anything better */ });
+ },
+
+ /**
+* Initiates processing of this connection, using the data in the given
+* request.
+*
+* @param request : Request
+* the request which should be processed
+*/
+ process: function(request)
+ {
+ NS_ASSERT(!this._closed && !this._processed);
+
+ this._processed = true;
+
+ this.request = request;
+ this.server._handler.handleResponse(this);
+ },
+
+ /**
+* Initiates processing of this connection, generating a response with the
+* given HTTP error code.
+*
+* @param code : uint
+* an HTTP code, so in the range [0, 1000)
+* @param request : Request
+* incomplete data about the incoming request (since there were errors
+* during its processing
+*/
+ processError: function(code, request)
+ {
+ NS_ASSERT(!this._closed && !this._processed);
+
+ this._processed = true;
+ this.request = request;
+ this.server._handler.handleError(code, this);
+ },
+
+ /** Converts this to a string for debugging purposes. */
+ toString: function()
+ {
+ return "<Connection(" + this.number +
+ (this.request ? ", " + this.request.path : "") +"): " +
+ (this._closed ? "closed" : "open") + ">";
+ }
+};
+
+
+
+/** Returns an array of count bytes from the given input stream. */
+function readBytes(inputStream, count)
+{
+ return new BinaryInputStream(inputStream).readByteArray(count);
+}
+
+
+
+/** Request reader processing states; see RequestReader for details. */
+const READER_IN_REQUEST_LINE = 0;
+const READER_IN_HEADERS = 1;
+const READER_IN_BODY = 2;
+const READER_FINISHED = 3;
+
+
+/**
+* Reads incoming request data asynchronously, does any necessary preprocessing,
+* and forwards it to the request handler. Processing occurs in three states:
+*
+* READER_IN_REQUEST_LINE Reading the request's status line
+* READER_IN_HEADERS Reading headers in the request
+* READER_IN_BODY Reading the body of the request
+* READER_FINISHED Entire request has been read and processed
+*
+* During the first two stages, initial metadata about the request is gathered
+* into a Request object. Once the status line and headers have been processed,
+* we start processing the body of the request into the Request. Finally, when
+* the entire body has been read, we create a Response and hand it off to the
+* ServerHandler to be given to the appropriate request handler.
+*
+* @param connection : Connection
+* the connection for the request being read
+*/
+function RequestReader(connection)
+{
+ /** Connection metadata for this request. */
+ this._connection = connection;
+
+ /**
+* A container providing line-by-line access to the raw bytes that make up the
+* data which has been read from the connection but has not yet been acted
+* upon (by passing it to the request handler or by extracting request
+* metadata from it).
+*/
+ this._data = new LineData();
+
+ /**
+* The amount of data remaining to be read from the body of this request.
+* After all headers in the request have been read this is the value in the
+* Content-Length header, but as the body is read its value decreases to zero.
+*/
+ this._contentLength = 0;
+
+ /** The current state of parsing the incoming request. */
+ this._state = READER_IN_REQUEST_LINE;
+
+ /** Metadata constructed from the incoming request for the request handler. */
+ this._metadata = new Request(connection.port);
+
+ /**
+* Used to preserve state if we run out of line data midway through a
+* multi-line header. _lastHeaderName stores the name of the header, while
+* _lastHeaderValue stores the value we've seen so far for the header.
+*
+* These fields are always either both undefined or both strings.
+*/
+ this._lastHeaderName = this._lastHeaderValue = undefined;
+}
+RequestReader.prototype =
+{
+ // NSIINPUTSTREAMCALLBACK
+
+ /**
+* Called when more data from the incoming request is available. This method
+* then reads the available data from input and deals with that data as
+* necessary, depending upon the syntax of already-downloaded data.
+*
+* @param input : nsIAsyncInputStream
+* the stream of incoming data from the connection
+*/
+ onInputStreamReady: function(input)
+ {
+ dumpn("*** onInputStreamReady(input=" + input + ") on thread " +
+ gThreadManager.currentThread + " (main is " +
+ gThreadManager.mainThread + ")");
+ dumpn("*** this._state == " + this._state);
+
+ // Handle cases where we get more data after a request error has been
+ // discovered but *before* we can close the connection.
+ var data = this._data;
+ if (!data)
+ return;
+
+ try
+ {
+ data.appendBytes(readBytes(input, input.available()));
+ }
+ catch (e)
+ {
+ if (streamClosed(e))
+ {
+ dumpn("*** WARNING: unexpected error when reading from socket; will " +
+ "be treated as if the input stream had been closed");
+ dumpn("*** WARNING: actual error was: " + e);
+ }
+
+ // We've lost a race -- input has been closed, but we're still expecting
+ // to read more data. available() will throw in this case, and since
+ // we're dead in the water now, destroy the connection.
+ dumpn("*** onInputStreamReady called on a closed input, destroying " +
+ "connection");
+ this._connection.close();
+ return;
+ }
+
+ switch (this._state)
+ {
+ default:
+ NS_ASSERT(false, "invalid state: " + this._state);
+ break;
+
+ case READER_IN_REQUEST_LINE:
+ if (!this._processRequestLine())
+ break;
+ /* fall through */
+
+ case READER_IN_HEADERS:
+ if (!this._processHeaders())
+ break;
+ /* fall through */
+
+ case READER_IN_BODY:
+ this._processBody();
+ }
+
+ if (this._state != READER_FINISHED)
+ input.asyncWait(this, 0, 0, gThreadManager.currentThread);
+ },
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(aIID)
+ {
+ if (aIID.equals(Ci.nsIInputStreamCallback) ||
+ aIID.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // PRIVATE API
+
+ /**
+* Processes unprocessed, downloaded data as a request line.
+*
+* @returns boolean
+* true iff the request line has been fully processed
+*/
+ _processRequestLine: function()
+ {
+ NS_ASSERT(this._state == READER_IN_REQUEST_LINE);
+
+ // Servers SHOULD ignore any empty line(s) received where a Request-Line
+ // is expected (section 4.1).
+ var data = this._data;
+ var line = {};
+ var readSuccess;
+ while ((readSuccess = data.readLine(line)) && line.value == "")
+ dumpn("*** ignoring beginning blank line...");
+
+ // if we don't have a full line, wait until we do
+ if (!readSuccess)
+ return false;
+
+ // we have the first non-blank line
+ try
+ {
+ this._parseRequestLine(line.value);
+ this._state = READER_IN_HEADERS;
+ return true;
+ }
+ catch (e)
+ {
+ this._handleError(e);
+ return false;
+ }
+ },
+
+ /**
+* Processes stored data, assuming it is either at the beginning or in
+* the middle of processing request headers.
+*
+* @returns boolean
+* true iff header data in the request has been fully processed
+*/
+ _processHeaders: function()
+ {
+ NS_ASSERT(this._state == READER_IN_HEADERS);
+
+ // XXX things to fix here:
+ //
+ // - need to support RFC 2047-encoded non-US-ASCII characters
+
+ try
+ {
+ var done = this._parseHeaders();
+ if (done)
+ {
+ var request = this._metadata;
+
+ // XXX this is wrong for requests with transfer-encodings applied to
+ // them, particularly chunked (which by its nature can have no
+ // meaningful Content-Length header)!
+ this._contentLength = request.hasHeader("Content-Length")
+ ? parseInt(request.getHeader("Content-Length"), 10)
+ : 0;
+ dumpn("_processHeaders, Content-length=" + this._contentLength);
+
+ this._state = READER_IN_BODY;
+ }
+ return done;
+ }
+ catch (e)
+ {
+ this._handleError(e);
+ return false;
+ }
+ },
+
+ /**
+* Processes stored data, assuming it is either at the beginning or in
+* the middle of processing the request body.
+*
+* @returns boolean
+* true iff the request body has been fully processed
+*/
+ _processBody: function()
+ {
+ NS_ASSERT(this._state == READER_IN_BODY);
+
+ // XXX handle chunked transfer-coding request bodies!
+
+ try
+ {
+ if (this._contentLength > 0)
+ {
+ var data = this._data.purge();
+ var count = Math.min(data.length, this._contentLength);
+ dumpn("*** loading data=" + data + " len=" + data.length +
+ " excess=" + (data.length - count));
+
+ var bos = new BinaryOutputStream(this._metadata._bodyOutputStream);
+ bos.writeByteArray(data, count);
+ this._contentLength -= count;
+ }
+
+ dumpn("*** remaining body data len=" + this._contentLength);
+ if (this._contentLength == 0)
+ {
+ this._validateRequest();
+ this._state = READER_FINISHED;
+ this._handleResponse();
+ return true;
+ }
+
+ return false;
+ }
+ catch (e)
+ {
+ this._handleError(e);
+ return false;
+ }
+ },
+
+ /**
+* Does various post-header checks on the data in this request.
+*
+* @throws : HttpError
+* if the request was malformed in some way
+*/
+ _validateRequest: function()
+ {
+ NS_ASSERT(this._state == READER_IN_BODY);
+
+ dumpn("*** _validateRequest");
+
+ var metadata = this._metadata;
+ var headers = metadata._headers;
+
+ // 19.6.1.1 -- servers MUST report 400 to HTTP/1.1 requests w/o Host header
+ var identity = this._connection.server.identity;
+ if (metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1))
+ {
+ if (!headers.hasHeader("Host"))
+ {
+ dumpn("*** malformed HTTP/1.1 or greater request with no Host header!");
+ throw HTTP_400;
+ }
+
+ // If the Request-URI wasn't absolute, then we need to determine our host.
+ // We have to determine what scheme was used to access us based on the
+ // server identity data at this point, because the request just doesn't
+ // contain enough data on its own to do this, sadly.
+ if (!metadata._host)
+ {
+ var host, port;
+ var hostPort = headers.getHeader("Host");
+ var colon = hostPort.indexOf(":");
+ if (colon < 0)
+ {
+ host = hostPort;
+ port = "";
+ }
+ else
+ {
+ host = hostPort.substring(0, colon);
+ port = hostPort.substring(colon + 1);
+ }
+
+ // NB: We allow an empty port here because, oddly, a colon may be
+ // present even without a port number, e.g. "example.com:"; in this
+ // case the default port applies.
+ if (!HOST_REGEX.test(host) || !/^\d*$/.test(port))
+ {
+ dumpn("*** malformed hostname (" + hostPort + ") in Host " +
+ "header, 400 time");
+ throw HTTP_400;
+ }
+
+ // If we're not given a port, we're stuck, because we don't know what
+ // scheme to use to look up the correct port here, in general. Since
+ // the HTTPS case requires a tunnel/proxy and thus requires that the
+ // requested URI be absolute (and thus contain the necessary
+ // information), let's assume HTTP will prevail and use that.
+ port = +port || 80;
+
+ var scheme = identity.getScheme(host, port);
+ if (!scheme)
+ {
+ dumpn("*** unrecognized hostname (" + hostPort + ") in Host " +
+ "header, 400 time");
+ throw HTTP_400;
+ }
+
+ metadata._scheme = scheme;
+ metadata._host = host;
+ metadata._port = port;
+ }
+ }
+ else
+ {
+ NS_ASSERT(metadata._host === undefined,
+ "HTTP/1.0 doesn't allow absolute paths in the request line!");
+
+ metadata._scheme = identity.primaryScheme;
+ metadata._host = identity.primaryHost;
+ metadata._port = identity.primaryPort;
+ }
+
+ NS_ASSERT(identity.has(metadata._scheme, metadata._host, metadata._port),
+ "must have a location we recognize by now!");
+ },
+
+ /**
+* Handles responses in case of error, either in the server or in the request.
+*
+* @param e
+* the specific error encountered, which is an HttpError in the case where
+* the request is in some way invalid or cannot be fulfilled; if this isn't
+* an HttpError we're going to be paranoid and shut down, because that
+* shouldn't happen, ever
+*/
+ _handleError: function(e)
+ {
+ // Don't fall back into normal processing!
+ this._state = READER_FINISHED;
+
+ var server = this._connection.server;
+ if (e instanceof HttpError)
+ {
+ var code = e.code;
+ }
+ else
+ {
+ dumpn("!!! UNEXPECTED ERROR: " + e +
+ (e.lineNumber ? ", line " + e.lineNumber : ""));
+
+ // no idea what happened -- be paranoid and shut down
+ code = 500;
+ server._requestQuit();
+ }
+
+ // make attempted reuse of data an error
+ this._data = null;
+
+ this._connection.processError(code, this._metadata);
+ },
+
+ /**
+* Now that we've read the request line and headers, we can actually hand off
+* the request to be handled.
+*
+* This method is called once per request, after the request line and all
+* headers and the body, if any, have been received.
+*/
+ _handleResponse: function()
+ {
+ NS_ASSERT(this._state == READER_FINISHED);
+
+ // We don't need the line-based data any more, so make attempted reuse an
+ // error.
+ this._data = null;
+
+ this._connection.process(this._metadata);
+ },
+
+
+ // PARSING
+
+ /**
+* Parses the request line for the HTTP request associated with this.
+*
+* @param line : string
+* the request line
+*/
+ _parseRequestLine: function(line)
+ {
+ NS_ASSERT(this._state == READER_IN_REQUEST_LINE);
+
+ dumpn("*** _parseRequestLine('" + line + "')");
+
+ var metadata = this._metadata;
+
+ // clients and servers SHOULD accept any amount of SP or HT characters
+ // between fields, even though only a single SP is required (section 19.3)
+ var request = line.split(/[ \t]+/);
+ if (!request || request.length != 3)
+ throw HTTP_400;
+
+ metadata._method = request[0];
+
+ // get the HTTP version
+ var ver = request[2];
+ var match = ver.match(/^HTTP\/(\d+\.\d+)$/);
+ if (!match)
+ throw HTTP_400;
+
+ // determine HTTP version
+ try
+ {
+ metadata._httpVersion = new nsHttpVersion(match[1]);
+ if (!metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_0))
+ throw "unsupported HTTP version";
+ }
+ catch (e)
+ {
+ // we support HTTP/1.0 and HTTP/1.1 only
+ throw HTTP_501;
+ }
+
+
+ var fullPath = request[1];
+ var serverIdentity = this._connection.server.identity;
+
+ var scheme, host, port;
+
+ if (fullPath.charAt(0) != "/")
+ {
+ // No absolute paths in the request line in HTTP prior to 1.1
+ if (!metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1))
+ throw HTTP_400;
+
+ try
+ {
+ var uri = Cc["@mozilla.org/network/io-service;1"]
+ .getService(Ci.nsIIOService)
+ .newURI(fullPath, null, null);
+ fullPath = uri.path;
+ scheme = uri.scheme;
+ host = metadata._host = uri.asciiHost;
+ port = uri.port;
+ if (port === -1)
+ {
+ if (scheme === "http")
+ port = 80;
+ else if (scheme === "https")
+ port = 443;
+ else
+ throw HTTP_400;
+ }
+ }
+ catch (e)
+ {
+ // If the host is not a valid host on the server, the response MUST be a
+ // 400 (Bad Request) error message (section 5.2). Alternately, the URI
+ // is malformed.
+ throw HTTP_400;
+ }
+
+ if (!serverIdentity.has(scheme, host, port) || fullPath.charAt(0) != "/")
+ throw HTTP_400;
+ }
+
+ var splitter = fullPath.indexOf("?");
+ if (splitter < 0)
+ {
+ // _queryString already set in ctor
+ metadata._path = fullPath;
+ }
+ else
+ {
+ metadata._path = fullPath.substring(0, splitter);
+ metadata._queryString = fullPath.substring(splitter + 1);
+ }
+
+ metadata._scheme = scheme;
+ metadata._host = host;
+ metadata._port = port;
+ },
+
+ /**
+* Parses all available HTTP headers in this until the header-ending CRLFCRLF,
+* adding them to the store of headers in the request.
+*
+* @throws
+* HTTP_400 if the headers are malformed
+* @returns boolean
+* true if all headers have now been processed, false otherwise
+*/
+ _parseHeaders: function()
+ {
+ NS_ASSERT(this._state == READER_IN_HEADERS);
+
+ dumpn("*** _parseHeaders");
+
+ var data = this._data;
+
+ var headers = this._metadata._headers;
+ var lastName = this._lastHeaderName;
+ var lastVal = this._lastHeaderValue;
+
+ var line = {};
+ while (true)
+ {
+ NS_ASSERT(!((lastVal === undefined) ^ (lastName === undefined)),
+ lastName === undefined ?
+ "lastVal without lastName? lastVal: '" + lastVal + "'" :
+ "lastName without lastVal? lastName: '" + lastName + "'");
+
+ if (!data.readLine(line))
+ {
+ // save any data we have from the header we might still be processing
+ this._lastHeaderName = lastName;
+ this._lastHeaderValue = lastVal;
+ return false;
+ }
+
+ var lineText = line.value;
+ var firstChar = lineText.charAt(0);
+
+ // blank line means end of headers
+ if (lineText == "")
+ {
+ // we're finished with the previous header
+ if (lastName)
+ {
+ try
+ {
+ headers.setHeader(lastName, lastVal, true);
+ }
+ catch (e)
+ {
+ dumpn("*** e == " + e);
+ throw HTTP_400;
+ }
+ }
+ else
+ {
+ // no headers in request -- valid for HTTP/1.0 requests
+ }
+
+ // either way, we're done processing headers
+ this._state = READER_IN_BODY;
+ return true;
+ }
+ else if (firstChar == " " || firstChar == "\t")
+ {
+ // multi-line header if we've already seen a header line
+ if (!lastName)
+ {
+ // we don't have a header to continue!
+ throw HTTP_400;
+ }
+
+ // append this line's text to the value; starts with SP/HT, so no need
+ // for separating whitespace
+ lastVal += lineText;
+ }
+ else
+ {
+ // we have a new header, so set the old one (if one existed)
+ if (lastName)
+ {
+ try
+ {
+ headers.setHeader(lastName, lastVal, true);
+ }
+ catch (e)
+ {
+ dumpn("*** e == " + e);
+ throw HTTP_400;
+ }
+ }
+
+ var colon = lineText.indexOf(":"); // first colon must be splitter
+ if (colon < 1)
+ {
+ // no colon or missing header field-name
+ throw HTTP_400;
+ }
+
+ // set header name, value (to be set in the next loop, usually)
+ lastName = lineText.substring(0, colon);
+ lastVal = lineText.substring(colon + 1);
+ } // empty, continuation, start of header
+ } // while (true)
+ }
+};
+
+
+/** The character codes for CR and LF. */
+const CR = 0x0D, LF = 0x0A;
+
+/**
+* Calculates the number of characters before the first CRLF pair in array, or
+* -1 if the array contains no CRLF pair.
+*
+* @param array : Array
+* an array of numbers in the range [0, 256), each representing a single
+* character; the first CRLF is the lowest index i where
+* |array[i] == "\r".charCodeAt(0)| and |array[i+1] == "\n".charCodeAt(0)|,
+* if such an |i| exists, and -1 otherwise
+* @returns int
+* the index of the first CRLF if any were present, -1 otherwise
+*/
+function findCRLF(array)
+{
+ for (var i = array.indexOf(CR); i >= 0; i = array.indexOf(CR, i + 1))
+ {
+ if (array[i + 1] == LF)
+ return i;
+ }
+ return -1;
+}
+
+
+/**
+* A container which provides line-by-line access to the arrays of bytes with
+* which it is seeded.
+*/
+function LineData()
+{
+ /** An array of queued bytes from which to get line-based characters. */
+ this._data = [];
+}
+LineData.prototype =
+{
+ /**
+* Appends the bytes in the given array to the internal data cache maintained
+* by this.
+*/
+ appendBytes: function(bytes)
+ {
+ Array.prototype.push.apply(this._data, bytes);
+ },
+
+ /**
+* Removes and returns a line of data, delimited by CRLF, from this.
+*
+* @param out
+* an object whose "value" property will be set to the first line of text
+* present in this, sans CRLF, if this contains a full CRLF-delimited line
+* of text; if this doesn't contain enough data, the value of the property
+* is undefined
+* @returns boolean
+* true if a full line of data could be read from the data in this, false
+* otherwise
+*/
+ readLine: function(out)
+ {
+ var data = this._data;
+ var length = findCRLF(data);
+ if (length < 0)
+ return false;
+
+ //
+ // We have the index of the CR, so remove all the characters, including
+ // CRLF, from the array with splice, and convert the removed array into the
+ // corresponding string, from which we then strip the trailing CRLF.
+ //
+ // Getting the line in this matter acknowledges that substring is an O(1)
+ // operation in SpiderMonkey because strings are immutable, whereas two
+ // splices, both from the beginning of the data, are less likely to be as
+ // cheap as a single splice plus two extra character conversions.
+ //
+ var line = String.fromCharCode.apply(null, data.splice(0, length + 2));
+ out.value = line.substring(0, length);
+
+ return true;
+ },
+
+ /**
+* Removes the bytes currently within this and returns them in an array.
+*
+* @returns Array
+* the bytes within this when this method is called
+*/
+ purge: function()
+ {
+ var data = this._data;
+ this._data = [];
+ return data;
+ }
+};
+
+
+
+/**
+* Creates a request-handling function for an nsIHttpRequestHandler object.
+*/
+function createHandlerFunc(handler)
+{
+ return function(metadata, response) { handler.handle(metadata, response); };
+}
+
+
+/**
+* The default handler for directories; writes an HTML response containing a
+* slightly-formatted directory listing.
+*/
+function defaultIndexHandler(metadata, response)
+{
+ response.setHeader("Content-Type", "text/html", false);
+
+ var path = htmlEscape(decodeURI(metadata.path));
+
+ //
+ // Just do a very basic bit of directory listings -- no need for too much
+ // fanciness, especially since we don't have a style sheet in which we can
+ // stick rules (don't want to pollute the default path-space).
+ //
+
+ var body = '<html>\
+<head>\
+<title>' + path + '</title>\
+</head>\
+<body>\
+<h1>' + path + '</h1>\
+<ol style="list-style-type: none">';
+
+ var directory = metadata.getProperty("directory").QueryInterface(Ci.nsILocalFile);
+ NS_ASSERT(directory && directory.isDirectory());
+
+ var fileList = [];
+ var files = directory.directoryEntries;
+ while (files.hasMoreElements())
+ {
+ var f = files.getNext().QueryInterface(Ci.nsIFile);
+ var name = f.leafName;
+ if (!f.isHidden() &&
+ (name.charAt(name.length - 1) != HIDDEN_CHAR ||
+ name.charAt(name.length - 2) == HIDDEN_CHAR))
+ fileList.push(f);
+ }
+
+ fileList.sort(fileSort);
+
+ for (var i = 0; i < fileList.length; i++)
+ {
+ var file = fileList[i];
+ try
+ {
+ var name = file.leafName;
+ if (name.charAt(name.length - 1) == HIDDEN_CHAR)
+ name = name.substring(0, name.length - 1);
+ var sep = file.isDirectory() ? "/" : "";
+
+ // Note: using " to delimit the attribute here because encodeURIComponent
+ // passes through '.
+ var item = '<li><a href="' + encodeURIComponent(name) + sep + '">' +
+ htmlEscape(name) + sep +
+ '</a></li>';
+
+ body += item;
+ }
+ catch (e) { /* some file system error, ignore the file */ }
+ }
+
+ body += ' </ol>\
+</body>\
+</html>';
+
+ response.bodyOutputStream.write(body, body.length);
+}
+
+/**
+* Sorts a and b (nsIFile objects) into an aesthetically pleasing order.
+*/
+function fileSort(a, b)
+{
+ var dira = a.isDirectory(), dirb = b.isDirectory();
+
+ if (dira && !dirb)
+ return -1;
+ if (dirb && !dira)
+ return 1;
+
+ var namea = a.leafName.toLowerCase(), nameb = b.leafName.toLowerCase();
+ return nameb > namea ? -1 : 1;
+}
+
+
+/**
+* Converts an externally-provided path into an internal path for use in
+* determining file mappings.
+*
+* @param path
+* the path to convert
+* @param encoded
+* true if the given path should be passed through decodeURI prior to
+* conversion
+* @throws URIError
+* if path is incorrectly encoded
+*/
+function toInternalPath(path, encoded)
+{
+ if (encoded)
+ path = decodeURI(path);
+
+ var comps = path.split("/");
+ for (var i = 0, sz = comps.length; i < sz; i++)
+ {
+ var comp = comps[i];
+ if (comp.charAt(comp.length - 1) == HIDDEN_CHAR)
+ comps[i] = comp + HIDDEN_CHAR;
+ }
+ return comps.join("/");
+}
+
+
+/**
+* Adds custom-specified headers for the given file to the given response, if
+* any such headers are specified.
+*
+* @param file
+* the file on the disk which is to be written
+* @param metadata
+* metadata about the incoming request
+* @param response
+* the Response to which any specified headers/data should be written
+* @throws HTTP_500
+* if an error occurred while processing custom-specified headers
+*/
+function maybeAddHeaders(file, metadata, response)
+{
+ var name = file.leafName;
+ if (name.charAt(name.length - 1) == HIDDEN_CHAR)
+ name = name.substring(0, name.length - 1);
+
+ var headerFile = file.parent;
+ headerFile.append(name + HEADERS_SUFFIX);
+
+ if (!headerFile.exists())
+ return;
+
+ const PR_RDONLY = 0x01;
+ var fis = new FileInputStream(headerFile, PR_RDONLY, 0o444,
+ Ci.nsIFileInputStream.CLOSE_ON_EOF);
+
+ try
+ {
+ var lis = new ConverterInputStream(fis, "UTF-8", 1024, 0x0);
+ lis.QueryInterface(Ci.nsIUnicharLineInputStream);
+
+ var line = {value: ""};
+ var more = lis.readLine(line);
+
+ if (!more && line.value == "")
+ return;
+
+
+ // request line
+
+ var status = line.value;
+ if (status.indexOf("HTTP ") == 0)
+ {
+ status = status.substring(5);
+ var space = status.indexOf(" ");
+ var code, description;
+ if (space < 0)
+ {
+ code = status;
+ description = "";
+ }
+ else
+ {
+ code = status.substring(0, space);
+ description = status.substring(space + 1, status.length);
+ }
+
+ response.setStatusLine(metadata.httpVersion, parseInt(code, 10), description);
+
+ line.value = "";
+ more = lis.readLine(line);
+ }
+
+ // headers
+ while (more || line.value != "")
+ {
+ var header = line.value;
+ var colon = header.indexOf(":");
+
+ response.setHeader(header.substring(0, colon),
+ header.substring(colon + 1, header.length),
+ false); // allow overriding server-set headers
+
+ line.value = "";
+ more = lis.readLine(line);
+ }
+ }
+ catch (e)
+ {
+ dumpn("WARNING: error in headers for " + metadata.path + ": " + e);
+ throw HTTP_500;
+ }
+ finally
+ {
+ fis.close();
+ }
+}
+
+
+/**
+* An object which handles requests for a server, executing default and
+* overridden behaviors as instructed by the code which uses and manipulates it.
+* Default behavior includes the paths / and /trace (diagnostics), with some
+* support for HTTP error pages for various codes and fallback to HTTP 500 if
+* those codes fail for any reason.
+*
+* @param server : nsHttpServer
+* the server in which this handler is being used
+*/
+function ServerHandler(server)
+{
+ // FIELDS
+
+ /**
+* The nsHttpServer instance associated with this handler.
+*/
+ this._server = server;
+
+ /**
+* A FileMap object containing the set of path->nsILocalFile mappings for
+* all directory mappings set in the server (e.g., "/" for /var/www/html/,
+* "/foo/bar/" for /local/path/, and "/foo/bar/baz/" for /local/path2).
+*
+* Note carefully: the leading and trailing "/" in each path (not file) are
+* removed before insertion to simplify the code which uses this. You have
+* been warned!
+*/
+ this._pathDirectoryMap = new FileMap();
+
+ /**
+* Custom request handlers for the server in which this resides. Path-handler
+* pairs are stored as property-value pairs in this property.
+*
+* @see ServerHandler.prototype._defaultPaths
+*/
+ this._overridePaths = {};
+
+ /**
+* Custom request handlers for the server in which this resides. Prefix-handler
+* pairs are stored as property-value pairs in this property.
+*/
+ this._overridePrefixes = {};
+
+ /**
+* Custom request handlers for the error handlers in the server in which this
+* resides. Path-handler pairs are stored as property-value pairs in this
+* property.
+*
+* @see ServerHandler.prototype._defaultErrors
+*/
+ this._overrideErrors = {};
+
+ /**
+* Maps file extensions to their MIME types in the server, overriding any
+* mapping that might or might not exist in the MIME service.
+*/
+ this._mimeMappings = {};
+
+ /**
+* The default handler for requests for directories, used to serve directories
+* when no index file is present.
+*/
+ this._indexHandler = defaultIndexHandler;
+
+ /** Per-path state storage for the server. */
+ this._state = {};
+
+ /** Entire-server state storage. */
+ this._sharedState = {};
+
+ /** Entire-server state storage for nsISupports values. */
+ this._objectState = {};
+}
+ServerHandler.prototype =
+{
+ // PUBLIC API
+
+ /**
+* Handles a request to this server, responding to the request appropriately
+* and initiating server shutdown if necessary.
+*
+* This method never throws an exception.
+*
+* @param connection : Connection
+* the connection for this request
+*/
+ handleResponse: function(connection)
+ {
+ var request = connection.request;
+ var response = new Response(connection);
+
+ var path = request.path;
+ dumpn("*** path == " + path);
+
+ try
+ {
+ try
+ {
+ if (path in this._overridePaths)
+ {
+ // explicit paths first, then files based on existing directory mappings,
+ // then (if the file doesn't exist) built-in server default paths
+ dumpn("calling override for " + path);
+ this._overridePaths[path](request, response);
+ }
+ else
+ {
+ let longestPrefix = "";
+ for (let prefix in this._overridePrefixes)
+ {
+ if (prefix.length > longestPrefix.length && path.startsWith(prefix))
+ {
+ longestPrefix = prefix;
+ }
+ }
+ if (longestPrefix.length > 0)
+ {
+ dumpn("calling prefix override for " + longestPrefix);
+ this._overridePrefixes[longestPrefix](request, response);
+ }
+ else
+ {
+ this._handleDefault(request, response);
+ }
+ }
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort(e);
+ return;
+ }
+
+ if (!(e instanceof HttpError))
+ {
+ dumpn("*** unexpected error: e == " + e);
+ throw HTTP_500;
+ }
+ if (e.code !== 404)
+ throw e;
+
+ dumpn("*** default: " + (path in this._defaultPaths));
+
+ response = new Response(connection);
+ if (path in this._defaultPaths)
+ this._defaultPaths[path](request, response);
+ else
+ throw HTTP_404;
+ }
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort(e);
+ return;
+ }
+
+ var errorCode = "internal";
+
+ try
+ {
+ if (!(e instanceof HttpError))
+ throw e;
+
+ errorCode = e.code;
+ dumpn("*** errorCode == " + errorCode);
+
+ response = new Response(connection);
+ if (e.customErrorHandling)
+ e.customErrorHandling(response);
+ this._handleError(errorCode, request, response);
+ return;
+ }
+ catch (e2)
+ {
+ dumpn("*** error handling " + errorCode + " error: " +
+ "e2 == " + e2 + ", shutting down server");
+
+ connection.server._requestQuit();
+ response.abort(e2);
+ return;
+ }
+ }
+
+ response.complete();
+ },
+
+ //
+ // see nsIHttpServer.registerFile
+ //
+ registerFile: function(path, file)
+ {
+ if (!file)
+ {
+ dumpn("*** unregistering '" + path + "' mapping");
+ delete this._overridePaths[path];
+ return;
+ }
+
+ dumpn("*** registering '" + path + "' as mapping to " + file.path);
+ file = file.clone();
+
+ var self = this;
+ this._overridePaths[path] =
+ function(request, response)
+ {
+ if (!file.exists())
+ throw HTTP_404;
+
+ response.setStatusLine(request.httpVersion, 200, "OK");
+ self._writeFileResponse(request, file, response, 0, file.fileSize);
+ };
+ },
+
+ //
+ // see nsIHttpServer.registerPathHandler
+ //
+ registerPathHandler: function(path, handler)
+ {
+ // XXX true path validation!
+ if (path.charAt(0) != "/")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ this._handlerToField(handler, this._overridePaths, path);
+ },
+
+ //
+ // see nsIHttpServer.registerPrefixHandler
+ //
+ registerPrefixHandler: function(prefix, handler)
+ {
+ // XXX true prefix validation!
+ if (!(prefix.startsWith("/") && prefix.endsWith("/")))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ this._handlerToField(handler, this._overridePrefixes, prefix);
+ },
+
+ //
+ // see nsIHttpServer.registerDirectory
+ //
+ registerDirectory: function(path, directory)
+ {
+ // strip off leading and trailing '/' so that we can use lastIndexOf when
+ // determining exactly how a path maps onto a mapped directory --
+ // conditional is required here to deal with "/".substring(1, 0) being
+ // converted to "/".substring(0, 1) per the JS specification
+ var key = path.length == 1 ? "" : path.substring(1, path.length - 1);
+
+ // the path-to-directory mapping code requires that the first character not
+ // be "/", or it will go into an infinite loop
+ if (key.charAt(0) == "/")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ key = toInternalPath(key, false);
+
+ if (directory)
+ {
+ dumpn("*** mapping '" + path + "' to the location " + directory.path);
+ this._pathDirectoryMap.put(key, directory);
+ }
+ else
+ {
+ dumpn("*** removing mapping for '" + path + "'");
+ this._pathDirectoryMap.put(key, null);
+ }
+ },
+
+ //
+ // see nsIHttpServer.registerErrorHandler
+ //
+ registerErrorHandler: function(err, handler)
+ {
+ if (!(err in HTTP_ERROR_CODES))
+ dumpn("*** WARNING: registering non-HTTP/1.1 error code " +
+ "(" + err + ") handler -- was this intentional?");
+
+ this._handlerToField(handler, this._overrideErrors, err);
+ },
+
+ //
+ // see nsIHttpServer.setIndexHandler
+ //
+ setIndexHandler: function(handler)
+ {
+ if (!handler)
+ handler = defaultIndexHandler;
+ else if (typeof(handler) != "function")
+ handler = createHandlerFunc(handler);
+
+ this._indexHandler = handler;
+ },
+
+ //
+ // see nsIHttpServer.registerContentType
+ //
+ registerContentType: function(ext, type)
+ {
+ if (!type)
+ delete this._mimeMappings[ext];
+ else
+ this._mimeMappings[ext] = headerUtils.normalizeFieldValue(type);
+ },
+
+ // PRIVATE API
+
+ /**
+* Sets or remove (if handler is null) a handler in an object with a key.
+*
+* @param handler
+* a handler, either function or an nsIHttpRequestHandler
+* @param dict
+* The object to attach the handler to.
+* @param key
+* The field name of the handler.
+*/
+ _handlerToField: function(handler, dict, key)
+ {
+ // for convenience, handler can be a function if this is run from xpcshell
+ if (typeof(handler) == "function")
+ dict[key] = handler;
+ else if (handler)
+ dict[key] = createHandlerFunc(handler);
+ else
+ delete dict[key];
+ },
+
+ /**
+* Handles a request which maps to a file in the local filesystem (if a base
+* path has already been set; otherwise the 404 error is thrown).
+*
+* @param metadata : Request
+* metadata for the incoming request
+* @param response : Response
+* an uninitialized Response to the given request, to be initialized by a
+* request handler
+* @throws HTTP_###
+* if an HTTP error occurred (usually HTTP_404); note that in this case the
+* calling code must handle post-processing of the response
+*/
+ _handleDefault: function(metadata, response)
+ {
+ dumpn("*** _handleDefault()");
+
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+
+ var path = metadata.path;
+ NS_ASSERT(path.charAt(0) == "/", "invalid path: <" + path + ">");
+
+ // determine the actual on-disk file; this requires finding the deepest
+ // path-to-directory mapping in the requested URL
+ var file = this._getFileForPath(path);
+
+ // the "file" might be a directory, in which case we either serve the
+ // contained index.html or make the index handler write the response
+ if (file.exists() && file.isDirectory())
+ {
+ file.append("index.html"); // make configurable?
+ if (!file.exists() || file.isDirectory())
+ {
+ metadata._ensurePropertyBag();
+ metadata._bag.setPropertyAsInterface("directory", file.parent);
+ this._indexHandler(metadata, response);
+ return;
+ }
+ }
+
+ // alternately, the file might not exist
+ if (!file.exists())
+ throw HTTP_404;
+
+ var start, end;
+ if (metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1) &&
+ metadata.hasHeader("Range") &&
+ this._getTypeFromFile(file) !== SJS_TYPE)
+ {
+ var rangeMatch = metadata.getHeader("Range").match(/^bytes=(\d+)?-(\d+)?$/);
+ if (!rangeMatch)
+ throw HTTP_400;
+
+ if (rangeMatch[1] !== undefined)
+ start = parseInt(rangeMatch[1], 10);
+
+ if (rangeMatch[2] !== undefined)
+ end = parseInt(rangeMatch[2], 10);
+
+ if (start === undefined && end === undefined)
+ throw HTTP_400;
+
+ // No start given, so the end is really the count of bytes from the
+ // end of the file.
+ if (start === undefined)
+ {
+ start = Math.max(0, file.fileSize - end);
+ end = file.fileSize - 1;
+ }
+
+ // start and end are inclusive
+ if (end === undefined || end >= file.fileSize)
+ end = file.fileSize - 1;
+
+ if (start !== undefined && start >= file.fileSize) {
+ var HTTP_416 = new HttpError(416, "Requested Range Not Satisfiable");
+ HTTP_416.customErrorHandling = function(errorResponse)
+ {
+ maybeAddHeaders(file, metadata, errorResponse);
+ };
+ throw HTTP_416;
+ }
+
+ if (end < start)
+ {
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ start = 0;
+ end = file.fileSize - 1;
+ }
+ else
+ {
+ response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
+ var contentRange = "bytes " + start + "-" + end + "/" + file.fileSize;
+ response.setHeader("Content-Range", contentRange);
+ }
+ }
+ else
+ {
+ start = 0;
+ end = file.fileSize - 1;
+ }
+
+ // finally...
+ dumpn("*** handling '" + path + "' as mapping to " + file.path + " from " +
+ start + " to " + end + " inclusive");
+ this._writeFileResponse(metadata, file, response, start, end - start + 1);
+ },
+
+ /**
+* Writes an HTTP response for the given file, including setting headers for
+* file metadata.
+*
+* @param metadata : Request
+* the Request for which a response is being generated
+* @param file : nsILocalFile
+* the file which is to be sent in the response
+* @param response : Response
+* the response to which the file should be written
+* @param offset: uint
+* the byte offset to skip to when writing
+* @param count: uint
+* the number of bytes to write
+*/
+ _writeFileResponse: function(metadata, file, response, offset, count)
+ {
+ const PR_RDONLY = 0x01;
+
+ var type = this._getTypeFromFile(file);
+ if (type === SJS_TYPE)
+ {
+ var fis = new FileInputStream(file, PR_RDONLY, 0o444,
+ Ci.nsIFileInputStream.CLOSE_ON_EOF);
+
+ try
+ {
+ var sis = new ScriptableInputStream(fis);
+ var s = Cu.Sandbox(gGlobalObject);
+ s.importFunction(dump, "dump");
+
+ // Define a basic key-value state-preservation API across requests, with
+ // keys initially corresponding to the empty string.
+ var self = this;
+ var path = metadata.path;
+ s.importFunction(function getState(k)
+ {
+ return self._getState(path, k);
+ });
+ s.importFunction(function setState(k, v)
+ {
+ self._setState(path, k, v);
+ });
+ s.importFunction(function getSharedState(k)
+ {
+ return self._getSharedState(k);
+ });
+ s.importFunction(function setSharedState(k, v)
+ {
+ self._setSharedState(k, v);
+ });
+ s.importFunction(function getObjectState(k, callback)
+ {
+ callback(self._getObjectState(k));
+ });
+ s.importFunction(function setObjectState(k, v)
+ {
+ self._setObjectState(k, v);
+ });
+ s.importFunction(function registerPathHandler(p, h)
+ {
+ self.registerPathHandler(p, h);
+ });
+
+ // Make it possible for sjs files to access their location
+ this._setState(path, "__LOCATION__", file.path);
+
+ try
+ {
+ // Alas, the line number in errors dumped to console when calling the
+ // request handler is simply an offset from where we load the SJS file.
+ // Work around this in a reasonably non-fragile way by dynamically
+ // getting the line number where we evaluate the SJS file. Don't
+ // separate these two lines!
+ var line = new Error().lineNumber;
+ Cu.evalInSandbox(sis.read(file.fileSize), s);
+ }
+ catch (e)
+ {
+ dumpn("*** syntax error in SJS at " + file.path + ": " + e);
+ throw HTTP_500;
+ }
+
+ try
+ {
+ s.handleRequest(metadata, response);
+ }
+ catch (e)
+ {
+ dump("*** error running SJS at " + file.path + ": " +
+ e + " on line " +
+ (e instanceof Error
+ ? e.lineNumber + " in httpd.js"
+ : (e.lineNumber - line)) + "\n");
+ throw HTTP_500;
+ }
+ }
+ finally
+ {
+ fis.close();
+ }
+ }
+ else
+ {
+ try
+ {
+ response.setHeader("Last-Modified",
+ toDateString(file.lastModifiedTime),
+ false);
+ }
+ catch (e) { /* lastModifiedTime threw, ignore */ }
+
+ response.setHeader("Content-Type", type, false);
+ maybeAddHeaders(file, metadata, response);
+ response.setHeader("Content-Length", "" + count, false);
+
+ var fis = new FileInputStream(file, PR_RDONLY, 0o444,
+ Ci.nsIFileInputStream.CLOSE_ON_EOF);
+
+ offset = offset || 0;
+ count = count || file.fileSize;
+ NS_ASSERT(offset === 0 || offset < file.fileSize, "bad offset");
+ NS_ASSERT(count >= 0, "bad count");
+ NS_ASSERT(offset + count <= file.fileSize, "bad total data size");
+
+ try
+ {
+ if (offset !== 0)
+ {
+ // Seek (or read, if seeking isn't supported) to the correct offset so
+ // the data sent to the client matches the requested range.
+ if (fis instanceof Ci.nsISeekableStream)
+ fis.seek(Ci.nsISeekableStream.NS_SEEK_SET, offset);
+ else
+ new ScriptableInputStream(fis).read(offset);
+ }
+ }
+ catch (e)
+ {
+ fis.close();
+ throw e;
+ }
+
+ let writeMore = function writeMore()
+ {
+ gThreadManager.currentThread
+ .dispatch(writeData, Ci.nsIThread.DISPATCH_NORMAL);
+ }
+
+ var input = new BinaryInputStream(fis);
+ var output = new BinaryOutputStream(response.bodyOutputStream);
+ var writeData =
+ {
+ run: function()
+ {
+ var chunkSize = Math.min(65536, count);
+ count -= chunkSize;
+ NS_ASSERT(count >= 0, "underflow");
+
+ try
+ {
+ var data = input.readByteArray(chunkSize);
+ NS_ASSERT(data.length === chunkSize,
+ "incorrect data returned? got " + data.length +
+ ", expected " + chunkSize);
+ output.writeByteArray(data, data.length);
+ if (count === 0)
+ {
+ fis.close();
+ response.finish();
+ }
+ else
+ {
+ writeMore();
+ }
+ }
+ catch (e)
+ {
+ try
+ {
+ fis.close();
+ }
+ finally
+ {
+ response.finish();
+ }
+ throw e;
+ }
+ }
+ };
+
+ writeMore();
+
+ // Now that we know copying will start, flag the response as async.
+ response.processAsync();
+ }
+ },
+
+ /**
+* Get the value corresponding to a given key for the given path for SJS state
+* preservation across requests.
+*
+* @param path : string
+* the path from which the given state is to be retrieved
+* @param k : string
+* the key whose corresponding value is to be returned
+* @returns string
+* the corresponding value, which is initially the empty string
+*/
+ _getState: function(path, k)
+ {
+ var state = this._state;
+ if (path in state && k in state[path])
+ return state[path][k];
+ return "";
+ },
+
+ /**
+* Set the value corresponding to a given key for the given path for SJS state
+* preservation across requests.
+*
+* @param path : string
+* the path from which the given state is to be retrieved
+* @param k : string
+* the key whose corresponding value is to be set
+* @param v : string
+* the value to be set
+*/
+ _setState: function(path, k, v)
+ {
+ if (typeof v !== "string")
+ throw new Error("non-string value passed");
+ var state = this._state;
+ if (!(path in state))
+ state[path] = {};
+ state[path][k] = v;
+ },
+
+ /**
+* Get the value corresponding to a given key for SJS state preservation
+* across requests.
+*
+* @param k : string
+* the key whose corresponding value is to be returned
+* @returns string
+* the corresponding value, which is initially the empty string
+*/
+ _getSharedState: function(k)
+ {
+ var state = this._sharedState;
+ if (k in state)
+ return state[k];
+ return "";
+ },
+
+ /**
+* Set the value corresponding to a given key for SJS state preservation
+* across requests.
+*
+* @param k : string
+* the key whose corresponding value is to be set
+* @param v : string
+* the value to be set
+*/
+ _setSharedState: function(k, v)
+ {
+ if (typeof v !== "string")
+ throw new Error("non-string value passed");
+ this._sharedState[k] = v;
+ },
+
+ /**
+* Returns the object associated with the given key in the server for SJS
+* state preservation across requests.
+*
+* @param k : string
+* the key whose corresponding object is to be returned
+* @returns nsISupports
+* the corresponding object, or null if none was present
+*/
+ _getObjectState: function(k)
+ {
+ if (typeof k !== "string")
+ throw new Error("non-string key passed");
+ return this._objectState[k] || null;
+ },
+
+ /**
+* Sets the object associated with the given key in the server for SJS
+* state preservation across requests.
+*
+* @param k : string
+* the key whose corresponding object is to be set
+* @param v : nsISupports
+* the object to be associated with the given key; may be null
+*/
+ _setObjectState: function(k, v)
+ {
+ if (typeof k !== "string")
+ throw new Error("non-string key passed");
+ if (typeof v !== "object")
+ throw new Error("non-object value passed");
+ if (v && !("QueryInterface" in v))
+ {
+ throw new Error("must pass an nsISupports; use wrappedJSObject to ease " +
+ "pain when using the server from JS");
+ }
+
+ this._objectState[k] = v;
+ },
+
+ /**
+* Gets a content-type for the given file, first by checking for any custom
+* MIME-types registered with this handler for the file's extension, second by
+* asking the global MIME service for a content-type, and finally by failing
+* over to application/octet-stream.
+*
+* @param file : nsIFile
+* the nsIFile for which to get a file type
+* @returns string
+* the best content-type which can be determined for the file
+*/
+ _getTypeFromFile: function(file)
+ {
+ try
+ {
+ var name = file.leafName;
+ var dot = name.lastIndexOf(".");
+ if (dot > 0)
+ {
+ var ext = name.slice(dot + 1);
+ if (ext in this._mimeMappings)
+ return this._mimeMappings[ext];
+ }
+ return Cc["@mozilla.org/uriloader/external-helper-app-service;1"]
+ .getService(Ci.nsIMIMEService)
+ .getTypeFromFile(file);
+ }
+ catch (e)
+ {
+ return "application/octet-stream";
+ }
+ },
+
+ /**
+* Returns the nsILocalFile which corresponds to the path, as determined using
+* all registered path->directory mappings and any paths which are explicitly
+* overridden.
+*
+* @param path : string
+* the server path for which a file should be retrieved, e.g. "/foo/bar"
+* @throws HttpError
+* when the correct action is the corresponding HTTP error (i.e., because no
+* mapping was found for a directory in path, the referenced file doesn't
+* exist, etc.)
+* @returns nsILocalFile
+* the file to be sent as the response to a request for the path
+*/
+ _getFileForPath: function(path)
+ {
+ // decode and add underscores as necessary
+ try
+ {
+ path = toInternalPath(path, true);
+ }
+ catch (e)
+ {
+ throw HTTP_400; // malformed path
+ }
+
+ // next, get the directory which contains this path
+ var pathMap = this._pathDirectoryMap;
+
+ // An example progression of tmp for a path "/foo/bar/baz/" might be:
+ // "foo/bar/baz/", "foo/bar/baz", "foo/bar", "foo", ""
+ var tmp = path.substring(1);
+ while (true)
+ {
+ // do we have a match for current head of the path?
+ var file = pathMap.get(tmp);
+ if (file)
+ {
+ // XXX hack; basically disable showing mapping for /foo/bar/ when the
+ // requested path was /foo/bar, because relative links on the page
+ // will all be incorrect -- we really need the ability to easily
+ // redirect here instead
+ if (tmp == path.substring(1) &&
+ tmp.length != 0 &&
+ tmp.charAt(tmp.length - 1) != "/")
+ file = null;
+ else
+ break;
+ }
+
+ // if we've finished trying all prefixes, exit
+ if (tmp == "")
+ break;
+
+ tmp = tmp.substring(0, tmp.lastIndexOf("/"));
+ }
+
+ // no mapping applies, so 404
+ if (!file)
+ throw HTTP_404;
+
+
+ // last, get the file for the path within the determined directory
+ var parentFolder = file.parent;
+ var dirIsRoot = (parentFolder == null);
+
+ // Strategy here is to append components individually, making sure we
+ // never move above the given directory; this allows paths such as
+ // "<file>/foo/../bar" but prevents paths such as "<file>/../base-sibling";
+ // this component-wise approach also means the code works even on platforms
+ // which don't use "/" as the directory separator, such as Windows
+ var leafPath = path.substring(tmp.length + 1);
+ var comps = leafPath.split("/");
+ for (var i = 0, sz = comps.length; i < sz; i++)
+ {
+ var comp = comps[i];
+
+ if (comp == "..")
+ file = file.parent;
+ else if (comp == "." || comp == "")
+ continue;
+ else
+ file.append(comp);
+
+ if (!dirIsRoot && file.equals(parentFolder))
+ throw HTTP_403;
+ }
+
+ return file;
+ },
+
+ /**
+* Writes the error page for the given HTTP error code over the given
+* connection.
+*
+* @param errorCode : uint
+* the HTTP error code to be used
+* @param connection : Connection
+* the connection on which the error occurred
+*/
+ handleError: function(errorCode, connection)
+ {
+ var response = new Response(connection);
+
+ dumpn("*** error in request: " + errorCode);
+
+ this._handleError(errorCode, new Request(connection.port), response);
+ },
+
+ /**
+* Handles a request which generates the given error code, using the
+* user-defined error handler if one has been set, gracefully falling back to
+* the x00 status code if the code has no handler, and failing to status code
+* 500 if all else fails.
+*
+* @param errorCode : uint
+* the HTTP error which is to be returned
+* @param metadata : Request
+* metadata for the request, which will often be incomplete since this is an
+* error
+* @param response : Response
+* an uninitialized Response should be initialized when this method
+* completes with information which represents the desired error code in the
+* ideal case or a fallback code in abnormal circumstances (i.e., 500 is a
+* fallback for 505, per HTTP specs)
+*/
+ _handleError: function(errorCode, metadata, response)
+ {
+ if (!metadata)
+ throw Cr.NS_ERROR_NULL_POINTER;
+
+ var errorX00 = errorCode - (errorCode % 100);
+
+ try
+ {
+ if (!(errorCode in HTTP_ERROR_CODES))
+ dumpn("*** WARNING: requested invalid error: " + errorCode);
+
+ // RFC 2616 says that we should try to handle an error by its class if we
+ // can't otherwise handle it -- if that fails, we revert to handling it as
+ // a 500 internal server error, and if that fails we throw and shut down
+ // the server
+
+ // actually handle the error
+ try
+ {
+ if (errorCode in this._overrideErrors)
+ this._overrideErrors[errorCode](metadata, response);
+ else
+ this._defaultErrors[errorCode](metadata, response);
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort(e);
+ return;
+ }
+
+ // don't retry the handler that threw
+ if (errorX00 == errorCode)
+ throw HTTP_500;
+
+ dumpn("*** error in handling for error code " + errorCode + ", " +
+ "falling back to " + errorX00 + "...");
+ response = new Response(response._connection);
+ if (errorX00 in this._overrideErrors)
+ this._overrideErrors[errorX00](metadata, response);
+ else if (errorX00 in this._defaultErrors)
+ this._defaultErrors[errorX00](metadata, response);
+ else
+ throw HTTP_500;
+ }
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort();
+ return;
+ }
+
+ // we've tried everything possible for a meaningful error -- now try 500
+ dumpn("*** error in handling for error code " + errorX00 + ", falling " +
+ "back to 500...");
+
+ try
+ {
+ response = new Response(response._connection);
+ if (500 in this._overrideErrors)
+ this._overrideErrors[500](metadata, response);
+ else
+ this._defaultErrors[500](metadata, response);
+ }
+ catch (e2)
+ {
+ dumpn("*** multiple errors in default error handlers!");
+ dumpn("*** e == " + e + ", e2 == " + e2);
+ response.abort(e2);
+ return;
+ }
+ }
+
+ response.complete();
+ },
+
+ // FIELDS
+
+ /**
+* This object contains the default handlers for the various HTTP error codes.
+*/
+ _defaultErrors:
+ {
+ 400: function(metadata, response)
+ {
+ // none of the data in metadata is reliable, so hard-code everything here
+ response.setStatusLine("1.1", 400, "Bad Request");
+ response.setHeader("Content-Type", "text/plain", false);
+
+ var body = "Bad request\n";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 403: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 403, "Forbidden");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>403 Forbidden</title></head>\
+<body>\
+<h1>403 Forbidden</h1>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 404: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 404, "Not Found");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>404 Not Found</title></head>\
+<body>\
+<h1>404 Not Found</h1>\
+<p>\
+<span style='font-family: monospace;'>" +
+ htmlEscape(metadata.path) +
+ "</span> was not found.\
+</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 416: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion,
+ 416,
+ "Requested Range Not Satisfiable");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head>\
+<title>416 Requested Range Not Satisfiable</title></head>\
+<body>\
+<h1>416 Requested Range Not Satisfiable</h1>\
+<p>The byte range was not valid for the\
+requested resource.\
+</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 500: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion,
+ 500,
+ "Internal Server Error");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>500 Internal Server Error</title></head>\
+<body>\
+<h1>500 Internal Server Error</h1>\
+<p>Something's broken in this server and\
+needs to be fixed.</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 501: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 501, "Not Implemented");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>501 Not Implemented</title></head>\
+<body>\
+<h1>501 Not Implemented</h1>\
+<p>This server is not (yet) Apache.</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 505: function(metadata, response)
+ {
+ response.setStatusLine("1.1", 505, "HTTP Version Not Supported");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>505 HTTP Version Not Supported</title></head>\
+<body>\
+<h1>505 HTTP Version Not Supported</h1>\
+<p>This server only supports HTTP/1.0 and HTTP/1.1\
+connections.</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ }
+ },
+
+ /**
+* Contains handlers for the default set of URIs contained in this server.
+*/
+ _defaultPaths:
+ {
+ "/": function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>httpd.js</title></head>\
+<body>\
+<h1>httpd.js</h1>\
+<p>If you're seeing this page, httpd.js is up and\
+serving requests! Now set a base path and serve some\
+files!</p>\
+</body>\
+</html>";
+
+ response.bodyOutputStream.write(body, body.length);
+ },
+
+ "/trace": function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ response.setHeader("Content-Type", "text/plain", false);
+
+ var body = "Request-URI: " +
+ metadata.scheme + "://" + metadata.host + ":" + metadata.port +
+ metadata.path + "\n\n";
+ body += "Request (semantically equivalent, slightly reformatted):\n\n";
+ body += metadata.method + " " + metadata.path;
+
+ if (metadata.queryString)
+ body += "?" + metadata.queryString;
+
+ body += " HTTP/" + metadata.httpVersion + "\r\n";
+
+ var headEnum = metadata.headers;
+ while (headEnum.hasMoreElements())
+ {
+ var fieldName = headEnum.getNext()
+ .QueryInterface(Ci.nsISupportsString)
+ .data;
+ body += fieldName + ": " + metadata.getHeader(fieldName) + "\r\n";
+ }
+
+ response.bodyOutputStream.write(body, body.length);
+ }
+ }
+};
+
+
+/**
+* Maps absolute paths to files on the local file system (as nsILocalFiles).
+*/
+function FileMap()
+{
+ /** Hash which will map paths to nsILocalFiles. */
+ this._map = {};
+}
+FileMap.prototype =
+{
+ // PUBLIC API
+
+ /**
+* Maps key to a clone of the nsILocalFile value if value is non-null;
+* otherwise, removes any extant mapping for key.
+*
+* @param key : string
+* string to which a clone of value is mapped
+* @param value : nsILocalFile
+* the file to map to key, or null to remove a mapping
+*/
+ put: function(key, value)
+ {
+ if (value)
+ this._map[key] = value.clone();
+ else
+ delete this._map[key];
+ },
+
+ /**
+* Returns a clone of the nsILocalFile mapped to key, or null if no such
+* mapping exists.
+*
+* @param key : string
+* key to which the returned file maps
+* @returns nsILocalFile
+* a clone of the mapped file, or null if no mapping exists
+*/
+ get: function(key)
+ {
+ var val = this._map[key];
+ return val ? val.clone() : null;
+ }
+};
+
+
+// Response CONSTANTS
+
+// token = *<any CHAR except CTLs or separators>
+// CHAR = <any US-ASCII character (0-127)>
+// CTL = <any US-ASCII control character (0-31) and DEL (127)>
+// separators = "(" | ")" | "<" | ">" | "@"
+// | "," | ";" | ":" | "\" | <">
+// | "/" | "[" | "]" | "?" | "="
+// | "{" | "}" | SP | HT
+const IS_TOKEN_ARRAY =
+ [0, 0, 0, 0, 0, 0, 0, 0, // 0
+ 0, 0, 0, 0, 0, 0, 0, 0, // 8
+ 0, 0, 0, 0, 0, 0, 0, 0, // 16
+ 0, 0, 0, 0, 0, 0, 0, 0, // 24
+
+ 0, 1, 0, 1, 1, 1, 1, 1, // 32
+ 0, 0, 1, 1, 0, 1, 1, 0, // 40
+ 1, 1, 1, 1, 1, 1, 1, 1, // 48
+ 1, 1, 0, 0, 0, 0, 0, 0, // 56
+
+ 0, 1, 1, 1, 1, 1, 1, 1, // 64
+ 1, 1, 1, 1, 1, 1, 1, 1, // 72
+ 1, 1, 1, 1, 1, 1, 1, 1, // 80
+ 1, 1, 1, 0, 0, 0, 1, 1, // 88
+
+ 1, 1, 1, 1, 1, 1, 1, 1, // 96
+ 1, 1, 1, 1, 1, 1, 1, 1, // 104
+ 1, 1, 1, 1, 1, 1, 1, 1, // 112
+ 1, 1, 1, 0, 1, 0, 1]; // 120
+
+
+/**
+* Determines whether the given character code is a CTL.
+*
+* @param code : uint
+* the character code
+* @returns boolean
+* true if code is a CTL, false otherwise
+*/
+function isCTL(code)
+{
+ return (code >= 0 && code <= 31) || (code == 127);
+}
+
+/**
+* Represents a response to an HTTP request, encapsulating all details of that
+* response. This includes all headers, the HTTP version, status code and
+* explanation, and the entity itself.
+*
+* @param connection : Connection
+* the connection over which this response is to be written
+*/
+function Response(connection)
+{
+ /** The connection over which this response will be written. */
+ this._connection = connection;
+
+ /**
+* The HTTP version of this response; defaults to 1.1 if not set by the
+* handler.
+*/
+ this._httpVersion = nsHttpVersion.HTTP_1_1;
+
+ /**
+* The HTTP code of this response; defaults to 200.
+*/
+ this._httpCode = 200;
+
+ /**
+* The description of the HTTP code in this response; defaults to "OK".
+*/
+ this._httpDescription = "OK";
+
+ /**
+* An nsIHttpHeaders object in which the headers in this response should be
+* stored. This property is null after the status line and headers have been
+* written to the network, and it may be modified up until it is cleared,
+* except if this._finished is set first (in which case headers are written
+* asynchronously in response to a finish() call not preceded by
+* flushHeaders()).
+*/
+ this._headers = new nsHttpHeaders();
+
+ /**
+* Set to true when this response is ended (completely constructed if possible
+* and the connection closed); further actions on this will then fail.
+*/
+ this._ended = false;
+
+ /**
+* A stream used to hold data written to the body of this response.
+*/
+ this._bodyOutputStream = null;
+
+ /**
+* A stream containing all data that has been written to the body of this
+* response so far. (Async handlers make the data contained in this
+* unreliable as a way of determining content length in general, but auxiliary
+* saved information can sometimes be used to guarantee reliability.)
+*/
+ this._bodyInputStream = null;
+
+ /**
+* A stream copier which copies data to the network. It is initially null
+* until replaced with a copier for response headers; when headers have been
+* fully sent it is replaced with a copier for the response body, remaining
+* so for the duration of response processing.
+*/
+ this._asyncCopier = null;
+
+ /**
+* True if this response has been designated as being processed
+* asynchronously rather than for the duration of a single call to
+* nsIHttpRequestHandler.handle.
+*/
+ this._processAsync = false;
+
+ /**
+* True iff finish() has been called on this, signaling that no more changes
+* to this may be made.
+*/
+ this._finished = false;
+
+ /**
+* True iff powerSeized() has been called on this, signaling that this
+* response is to be handled manually by the response handler (which may then
+* send arbitrary data in response, even non-HTTP responses).
+*/
+ this._powerSeized = false;
+}
+Response.prototype =
+{
+ // PUBLIC CONSTRUCTION API
+
+ //
+ // see nsIHttpResponse.bodyOutputStream
+ //
+ get bodyOutputStream()
+ {
+ if (this._finished)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+
+ if (!this._bodyOutputStream)
+ {
+ var pipe = new Pipe(true, false, Response.SEGMENT_SIZE, PR_UINT32_MAX,
+ null);
+ this._bodyOutputStream = pipe.outputStream;
+ this._bodyInputStream = pipe.inputStream;
+ if (this._processAsync || this._powerSeized)
+ this._startAsyncProcessor();
+ }
+
+ return this._bodyOutputStream;
+ },
+
+ //
+ // see nsIHttpResponse.write
+ //
+ write: function(data)
+ {
+ if (this._finished)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+
+ var dataAsString = String(data);
+ this.bodyOutputStream.write(dataAsString, dataAsString.length);
+ },
+
+ //
+ // see nsIHttpResponse.setStatusLine
+ //
+ setStatusLine: function(httpVersion, code, description)
+ {
+ if (!this._headers || this._finished || this._powerSeized)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ this._ensureAlive();
+
+ if (!(code >= 0 && code < 1000))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ try
+ {
+ var httpVer;
+ // avoid version construction for the most common cases
+ if (!httpVersion || httpVersion == "1.1")
+ httpVer = nsHttpVersion.HTTP_1_1;
+ else if (httpVersion == "1.0")
+ httpVer = nsHttpVersion.HTTP_1_0;
+ else
+ httpVer = new nsHttpVersion(httpVersion);
+ }
+ catch (e)
+ {
+ throw Cr.NS_ERROR_INVALID_ARG;
+ }
+
+ // Reason-Phrase = *<TEXT, excluding CR, LF>
+ // TEXT = <any OCTET except CTLs, but including LWS>
+ //
+ // XXX this ends up disallowing octets which aren't Unicode, I think -- not
+ // much to do if description is IDL'd as string
+ if (!description)
+ description = "";
+ for (var i = 0; i < description.length; i++)
+ if (isCTL(description.charCodeAt(i)) && description.charAt(i) != "\t")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ // set the values only after validation to preserve atomicity
+ this._httpDescription = description;
+ this._httpCode = code;
+ this._httpVersion = httpVer;
+ },
+
+ //
+ // see nsIHttpResponse.setHeader
+ //
+ setHeader: function(name, value, merge)
+ {
+ if (!this._headers || this._finished || this._powerSeized)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ this._ensureAlive();
+
+ this._headers.setHeader(name, value, merge);
+ },
+
+ //
+ // see nsIHttpResponse.processAsync
+ //
+ processAsync: function()
+ {
+ if (this._finished)
+ throw Cr.NS_ERROR_UNEXPECTED;
+ if (this._powerSeized)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ if (this._processAsync)
+ return;
+ this._ensureAlive();
+
+ dumpn("*** processing connection " + this._connection.number + " async");
+ this._processAsync = true;
+
+ /*
+* Either the bodyOutputStream getter or this method is responsible for
+* starting the asynchronous processor and catching writes of data to the
+* response body of async responses as they happen, for the purpose of
+* forwarding those writes to the actual connection's output stream.
+* If bodyOutputStream is accessed first, calling this method will create
+* the processor (when it first is clear that body data is to be written
+* immediately, not buffered). If this method is called first, accessing
+* bodyOutputStream will create the processor. If only this method is
+* called, we'll write nothing, neither headers nor the nonexistent body,
+* until finish() is called. Since that delay is easily avoided by simply
+* getting bodyOutputStream or calling write(""), we don't worry about it.
+*/
+ if (this._bodyOutputStream && !this._asyncCopier)
+ this._startAsyncProcessor();
+ },
+
+ //
+ // see nsIHttpResponse.seizePower
+ //
+ seizePower: function()
+ {
+ if (this._processAsync)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ if (this._finished)
+ throw Cr.NS_ERROR_UNEXPECTED;
+ if (this._powerSeized)
+ return;
+ this._ensureAlive();
+
+ dumpn("*** forcefully seizing power over connection " +
+ this._connection.number + "...");
+
+ // Purge any already-written data without sending it. We could as easily
+ // swap out the streams entirely, but that makes it possible to acquire and
+ // unknowingly use a stale reference, so we require there only be one of
+ // each stream ever for any response to avoid this complication.
+ if (this._asyncCopier)
+ this._asyncCopier.cancel(Cr.NS_BINDING_ABORTED);
+ this._asyncCopier = null;
+ if (this._bodyOutputStream)
+ {
+ var input = new BinaryInputStream(this._bodyInputStream);
+ var avail;
+ while ((avail = input.available()) > 0)
+ input.readByteArray(avail);
+ }
+
+ this._powerSeized = true;
+ if (this._bodyOutputStream)
+ this._startAsyncProcessor();
+ },
+
+ //
+ // see nsIHttpResponse.finish
+ //
+ finish: function()
+ {
+ if (!this._processAsync && !this._powerSeized)
+ throw Cr.NS_ERROR_UNEXPECTED;
+ if (this._finished)
+ return;
+
+ dumpn("*** finishing connection " + this._connection.number);
+ this._startAsyncProcessor(); // in case bodyOutputStream was never accessed
+ if (this._bodyOutputStream)
+ this._bodyOutputStream.close();
+ this._finished = true;
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIHttpResponse) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // POST-CONSTRUCTION API (not exposed externally)
+
+ /**
+* The HTTP version number of this, as a string (e.g. "1.1").
+*/
+ get httpVersion()
+ {
+ this._ensureAlive();
+ return this._httpVersion.toString();
+ },
+
+ /**
+* The HTTP status code of this response, as a string of three characters per
+* RFC 2616.
+*/
+ get httpCode()
+ {
+ this._ensureAlive();
+
+ var codeString = (this._httpCode < 10 ? "0" : "") +
+ (this._httpCode < 100 ? "0" : "") +
+ this._httpCode;
+ return codeString;
+ },
+
+ /**
+* The description of the HTTP status code of this response, or "" if none is
+* set.
+*/
+ get httpDescription()
+ {
+ this._ensureAlive();
+
+ return this._httpDescription;
+ },
+
+ /**
+* The headers in this response, as an nsHttpHeaders object.
+*/
+ get headers()
+ {
+ this._ensureAlive();
+
+ return this._headers;
+ },
+
+ //
+ // see nsHttpHeaders.getHeader
+ //
+ getHeader: function(name)
+ {
+ this._ensureAlive();
+
+ return this._headers.getHeader(name);
+ },
+
+ /**
+* Determines whether this response may be abandoned in favor of a newly
+* constructed response. A response may be abandoned only if it is not being
+* sent asynchronously and if raw control over it has not been taken from the
+* server.
+*
+* @returns boolean
+* true iff no data has been written to the network
+*/
+ partiallySent: function()
+ {
+ dumpn("*** partiallySent()");
+ return this._processAsync || this._powerSeized;
+ },
+
+ /**
+* If necessary, kicks off the remaining request processing needed to be done
+* after a request handler performs its initial work upon this response.
+*/
+ complete: function()
+ {
+ dumpn("*** complete()");
+ if (this._processAsync || this._powerSeized)
+ {
+ NS_ASSERT(this._processAsync ^ this._powerSeized,
+ "can't both send async and relinquish power");
+ return;
+ }
+
+ NS_ASSERT(!this.partiallySent(), "completing a partially-sent response?");
+
+ this._startAsyncProcessor();
+
+ // Now make sure we finish processing this request!
+ if (this._bodyOutputStream)
+ this._bodyOutputStream.close();
+ },
+
+ /**
+* Abruptly ends processing of this response, usually due to an error in an
+* incoming request but potentially due to a bad error handler. Since we
+* cannot handle the error in the usual way (giving an HTTP error page in
+* response) because data may already have been sent (or because the response
+* might be expected to have been generated asynchronously or completely from
+* scratch by the handler), we stop processing this response and abruptly
+* close the connection.
+*
+* @param e : Error
+* the exception which precipitated this abort, or null if no such exception
+* was generated
+*/
+ abort: function(e)
+ {
+ dumpn("*** abort(<" + e + ">)");
+
+ // This response will be ended by the processor if one was created.
+ var copier = this._asyncCopier;
+ if (copier)
+ {
+ // We dispatch asynchronously here so that any pending writes of data to
+ // the connection will be deterministically written. This makes it easier
+ // to specify exact behavior, and it makes observable behavior more
+ // predictable for clients. Note that the correctness of this depends on
+ // callbacks in response to _waitToReadData in WriteThroughCopier
+ // happening asynchronously with respect to the actual writing of data to
+ // bodyOutputStream, as they currently do; if they happened synchronously,
+ // an event which ran before this one could write more data to the
+ // response body before we get around to canceling the copier. We have
+ // tests for this in test_seizepower.js, however, and I can't think of a
+ // way to handle both cases without removing bodyOutputStream access and
+ // moving its effective write(data, length) method onto Response, which
+ // would be slower and require more code than this anyway.
+ gThreadManager.currentThread.dispatch({
+ run: function()
+ {
+ dumpn("*** canceling copy asynchronously...");
+ copier.cancel(Cr.NS_ERROR_UNEXPECTED);
+ }
+ }, Ci.nsIThread.DISPATCH_NORMAL);
+ }
+ else
+ {
+ this.end();
+ }
+ },
+
+ /**
+* Closes this response's network connection, marks the response as finished,
+* and notifies the server handler that the request is done being processed.
+*/
+ end: function()
+ {
+ NS_ASSERT(!this._ended, "ending this response twice?!?!");
+
+ this._connection.close();
+ if (this._bodyOutputStream)
+ this._bodyOutputStream.close();
+
+ this._finished = true;
+ this._ended = true;
+ },
+
+ // PRIVATE IMPLEMENTATION
+
+ /**
+* Sends the status line and headers of this response if they haven't been
+* sent and initiates the process of copying data written to this response's
+* body to the network.
+*/
+ _startAsyncProcessor: function()
+ {
+ dumpn("*** _startAsyncProcessor()");
+
+ // Handle cases where we're being called a second time. The former case
+ // happens when this is triggered both by complete() and by processAsync(),
+ // while the latter happens when processAsync() in conjunction with sent
+ // data causes abort() to be called.
+ if (this._asyncCopier || this._ended)
+ {
+ dumpn("*** ignoring second call to _startAsyncProcessor");
+ return;
+ }
+
+ // Send headers if they haven't been sent already and should be sent, then
+ // asynchronously continue to send the body.
+ if (this._headers && !this._powerSeized)
+ {
+ this._sendHeaders();
+ return;
+ }
+
+ this._headers = null;
+ this._sendBody();
+ },
+
+ /**
+* Signals that all modifications to the response status line and headers are
+* complete and then sends that data over the network to the client. Once
+* this method completes, a different response to the request that resulted
+* in this response cannot be sent -- the only possible action in case of
+* error is to abort the response and close the connection.
+*/
+ _sendHeaders: function()
+ {
+ dumpn("*** _sendHeaders()");
+
+ NS_ASSERT(this._headers);
+ NS_ASSERT(!this._powerSeized);
+
+ // request-line
+ var statusLine = "HTTP/" + this.httpVersion + " " +
+ this.httpCode + " " +
+ this.httpDescription + "\r\n";
+
+ // header post-processing
+
+ var headers = this._headers;
+ headers.setHeader("Connection", "close", false);
+ headers.setHeader("Server", "httpd.js", false);
+ if (!headers.hasHeader("Date"))
+ headers.setHeader("Date", toDateString(Date.now()), false);
+
+ // Any response not being processed asynchronously must have an associated
+ // Content-Length header for reasons of backwards compatibility with the
+ // initial server, which fully buffered every response before sending it.
+ // Beyond that, however, it's good to do this anyway because otherwise it's
+ // impossible to test behaviors that depend on the presence or absence of a
+ // Content-Length header.
+ if (!this._processAsync)
+ {
+ dumpn("*** non-async response, set Content-Length");
+
+ var bodyStream = this._bodyInputStream;
+ var avail = bodyStream ? bodyStream.available() : 0;
+
+ // XXX assumes stream will always report the full amount of data available
+ headers.setHeader("Content-Length", "" + avail, false);
+ }
+
+
+ // construct and send response
+ dumpn("*** header post-processing completed, sending response head...");
+
+ // request-line
+ var preambleData = [statusLine];
+
+ // headers
+ var headEnum = headers.enumerator;
+ while (headEnum.hasMoreElements())
+ {
+ var fieldName = headEnum.getNext()
+ .QueryInterface(Ci.nsISupportsString)
+ .data;
+ var values = headers.getHeaderValues(fieldName);
+ for (var i = 0, sz = values.length; i < sz; i++)
+ preambleData.push(fieldName + ": " + values[i] + "\r\n");
+ }
+
+ // end request-line/headers
+ preambleData.push("\r\n");
+
+ var preamble = preambleData.join("");
+
+ var responseHeadPipe = new Pipe(true, false, 0, PR_UINT32_MAX, null);
+ responseHeadPipe.outputStream.write(preamble, preamble.length);
+
+ var response = this;
+ var copyObserver =
+ {
+ onStartRequest: function(request, cx)
+ {
+ dumpn("*** preamble copying started");
+ },
+
+ onStopRequest: function(request, cx, statusCode)
+ {
+ dumpn("*** preamble copying complete " +
+ "[status=0x" + statusCode.toString(16) + "]");
+
+ if (!components.isSuccessCode(statusCode))
+ {
+ dumpn("!!! header copying problems: non-success statusCode, " +
+ "ending response");
+
+ response.end();
+ }
+ else
+ {
+ response._sendBody();
+ }
+ },
+
+ QueryInterface: function(aIID)
+ {
+ if (aIID.equals(Ci.nsIRequestObserver) || aIID.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ }
+ };
+
+ var headerCopier = this._asyncCopier =
+ new WriteThroughCopier(responseHeadPipe.inputStream,
+ this._connection.output,
+ copyObserver, null);
+
+ responseHeadPipe.outputStream.close();
+
+ // Forbid setting any more headers or modifying the request line.
+ this._headers = null;
+ },
+
+ /**
+* Asynchronously writes the body of the response (or the entire response, if
+* seizePower() has been called) to the network.
+*/
+ _sendBody: function()
+ {
+ dumpn("*** _sendBody");
+
+ NS_ASSERT(!this._headers, "still have headers around but sending body?");
+
+ // If no body data was written, we're done
+ if (!this._bodyInputStream)
+ {
+ dumpn("*** empty body, response finished");
+ this.end();
+ return;
+ }
+
+ var response = this;
+ var copyObserver =
+ {
+ onStartRequest: function(request, context)
+ {
+ dumpn("*** onStartRequest");
+ },
+
+ onStopRequest: function(request, cx, statusCode)
+ {
+ dumpn("*** onStopRequest [status=0x" + statusCode.toString(16) + "]");
+
+ if (statusCode === Cr.NS_BINDING_ABORTED)
+ {
+ dumpn("*** terminating copy observer without ending the response");
+ }
+ else
+ {
+ if (!components.isSuccessCode(statusCode))
+ dumpn("*** WARNING: non-success statusCode in onStopRequest");
+
+ response.end();
+ }
+ },
+
+ QueryInterface: function(aIID)
+ {
+ if (aIID.equals(Ci.nsIRequestObserver) || aIID.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ }
+ };
+
+ dumpn("*** starting async copier of body data...");
+ this._asyncCopier =
+ new WriteThroughCopier(this._bodyInputStream, this._connection.output,
+ copyObserver, null);
+ },
+
+ /** Ensures that this hasn't been ended. */
+ _ensureAlive: function()
+ {
+ NS_ASSERT(!this._ended, "not handling response lifetime correctly");
+ }
+};
+
+/**
+* Size of the segments in the buffer used in storing response data and writing
+* it to the socket.
+*/
+Response.SEGMENT_SIZE = 8192;
+
+/** Serves double duty in WriteThroughCopier implementation. */
+function notImplemented()
+{
+ throw Cr.NS_ERROR_NOT_IMPLEMENTED;
+}
+
+/** Returns true iff the given exception represents stream closure. */
+function streamClosed(e)
+{
+ return e === Cr.NS_BASE_STREAM_CLOSED ||
+ (typeof e === "object" && e.result === Cr.NS_BASE_STREAM_CLOSED);
+}
+
+/** Returns true iff the given exception represents a blocked stream. */
+function wouldBlock(e)
+{
+ return e === Cr.NS_BASE_STREAM_WOULD_BLOCK ||
+ (typeof e === "object" && e.result === Cr.NS_BASE_STREAM_WOULD_BLOCK);
+}
+
+/**
+* Copies data from source to sink as it becomes available, when that data can
+* be written to sink without blocking.
+*
+* @param source : nsIAsyncInputStream
+* the stream from which data is to be read
+* @param sink : nsIAsyncOutputStream
+* the stream to which data is to be copied
+* @param observer : nsIRequestObserver
+* an observer which will be notified when the copy starts and finishes
+* @param context : nsISupports
+* context passed to observer when notified of start/stop
+* @throws NS_ERROR_NULL_POINTER
+* if source, sink, or observer are null
+*/
+function WriteThroughCopier(source, sink, observer, context)
+{
+ if (!source || !sink || !observer)
+ throw Cr.NS_ERROR_NULL_POINTER;
+
+ /** Stream from which data is being read. */
+ this._source = source;
+
+ /** Stream to which data is being written. */
+ this._sink = sink;
+
+ /** Observer watching this copy. */
+ this._observer = observer;
+
+ /** Context for the observer watching this. */
+ this._context = context;
+
+ /**
+* True iff this is currently being canceled (cancel has been called, the
+* callback may not yet have been made).
+*/
+ this._canceled = false;
+
+ /**
+* False until all data has been read from input and written to output, at
+* which point this copy is completed and cancel() is asynchronously called.
+*/
+ this._completed = false;
+
+ /** Required by nsIRequest, meaningless. */
+ this.loadFlags = 0;
+ /** Required by nsIRequest, meaningless. */
+ this.loadGroup = null;
+ /** Required by nsIRequest, meaningless. */
+ this.name = "response-body-copy";
+
+ /** Status of this request. */
+ this.status = Cr.NS_OK;
+
+ /** Arrays of byte strings waiting to be written to output. */
+ this._pendingData = [];
+
+ // start copying
+ try
+ {
+ observer.onStartRequest(this, context);
+ this._waitToReadData();
+ this._waitForSinkClosure();
+ }
+ catch (e)
+ {
+ dumpn("!!! error starting copy: " + e +
+ ("lineNumber" in e ? ", line " + e.lineNumber : ""));
+ dumpn(e.stack);
+ this.cancel(Cr.NS_ERROR_UNEXPECTED);
+ }
+}
+WriteThroughCopier.prototype =
+{
+ /* nsISupports implementation */
+
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIInputStreamCallback) ||
+ iid.equals(Ci.nsIOutputStreamCallback) ||
+ iid.equals(Ci.nsIRequest) ||
+ iid.equals(Ci.nsISupports))
+ {
+ return this;
+ }
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // NSIINPUTSTREAMCALLBACK
+
+ /**
+* Receives a more-data-in-input notification and writes the corresponding
+* data to the output.
+*
+* @param input : nsIAsyncInputStream
+* the input stream on whose data we have been waiting
+*/
+ onInputStreamReady: function(input)
+ {
+ if (this._source === null)
+ return;
+
+ dumpn("*** onInputStreamReady");
+
+ //
+ // Ordinarily we'll read a non-zero amount of data from input, queue it up
+ // to be written and then wait for further callbacks. The complications in
+ // this method are the cases where we deviate from that behavior when errors
+ // occur or when copying is drawing to a finish.
+ //
+ // The edge cases when reading data are:
+ //
+ // Zero data is read
+ // If zero data was read, we're at the end of available data, so we can
+ // should stop reading and move on to writing out what we have (or, if
+ // we've already done that, onto notifying of completion).
+ // A stream-closed exception is thrown
+ // This is effectively a less kind version of zero data being read; the
+ // only difference is that we notify of completion with that result
+ // rather than with NS_OK.
+ // Some other exception is thrown
+ // This is the least kind result. We don't know what happened, so we
+ // act as though the stream closed except that we notify of completion
+ // with the result NS_ERROR_UNEXPECTED.
+ //
+
+ var bytesWanted = 0, bytesConsumed = -1;
+ try
+ {
+ input = new BinaryInputStream(input);
+
+ bytesWanted = Math.min(input.available(), Response.SEGMENT_SIZE);
+ dumpn("*** input wanted: " + bytesWanted);
+
+ if (bytesWanted > 0)
+ {
+ var data = input.readByteArray(bytesWanted);
+ bytesConsumed = data.length;
+ this._pendingData.push(String.fromCharCode.apply(String, data));
+ }
+
+ dumpn("*** " + bytesConsumed + " bytes read");
+
+ // Handle the zero-data edge case in the same place as all other edge
+ // cases are handled.
+ if (bytesWanted === 0)
+ throw Cr.NS_BASE_STREAM_CLOSED;
+ }
+ catch (e)
+ {
+ if (streamClosed(e))
+ {
+ dumpn("*** input stream closed");
+ e = bytesWanted === 0 ? Cr.NS_OK : Cr.NS_ERROR_UNEXPECTED;
+ }
+ else
+ {
+ dumpn("!!! unexpected error reading from input, canceling: " + e);
+ e = Cr.NS_ERROR_UNEXPECTED;
+ }
+
+ this._doneReadingSource(e);
+ return;
+ }
+
+ var pendingData = this._pendingData;
+
+ NS_ASSERT(bytesConsumed > 0);
+ NS_ASSERT(pendingData.length > 0, "no pending data somehow?");
+ NS_ASSERT(pendingData[pendingData.length - 1].length > 0,
+ "buffered zero bytes of data?");
+
+ NS_ASSERT(this._source !== null);
+
+ // Reading has gone great, and we've gotten data to write now. What if we
+ // don't have a place to write that data, because output went away just
+ // before this read? Drop everything on the floor, including new data, and
+ // cancel at this point.
+ if (this._sink === null)
+ {
+ pendingData.length = 0;
+ this._doneReadingSource(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // Okay, we've read the data, and we know we have a place to write it. We
+ // need to queue up the data to be written, but *only* if none is queued
+ // already -- if data's already queued, the code that actually writes the
+ // data will make sure to wait on unconsumed pending data.
+ try
+ {
+ if (pendingData.length === 1)
+ this._waitToWriteData();
+ }
+ catch (e)
+ {
+ dumpn("!!! error waiting to write data just read, swallowing and " +
+ "writing only what we already have: " + e);
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // Whee! We successfully read some data, and it's successfully queued up to
+ // be written. All that remains now is to wait for more data to read.
+ try
+ {
+ this._waitToReadData();
+ }
+ catch (e)
+ {
+ dumpn("!!! error waiting to read more data: " + e);
+ this._doneReadingSource(Cr.NS_ERROR_UNEXPECTED);
+ }
+ },
+
+
+ // NSIOUTPUTSTREAMCALLBACK
+
+ /**
+* Callback when data may be written to the output stream without blocking, or
+* when the output stream has been closed.
+*
+* @param output : nsIAsyncOutputStream
+* the output stream on whose writability we've been waiting, also known as
+* this._sink
+*/
+ onOutputStreamReady: function(output)
+ {
+ if (this._sink === null)
+ return;
+
+ dumpn("*** onOutputStreamReady");
+
+ var pendingData = this._pendingData;
+ if (pendingData.length === 0)
+ {
+ // There's no pending data to write. The only way this can happen is if
+ // we're waiting on the output stream's closure, so we can respond to a
+ // copying failure as quickly as possible (rather than waiting for data to
+ // be available to read and then fail to be copied). Therefore, we must
+ // be done now -- don't bother to attempt to write anything and wrap
+ // things up.
+ dumpn("!!! output stream closed prematurely, ending copy");
+
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+
+ NS_ASSERT(pendingData[0].length > 0, "queued up an empty quantum?");
+
+ //
+ // Write out the first pending quantum of data. The possible errors here
+ // are:
+ //
+ // The write might fail because we can't write that much data
+ // Okay, we've written what we can now, so re-queue what's left and
+ // finish writing it out later.
+ // The write failed because the stream was closed
+ // Discard pending data that we can no longer write, stop reading, and
+ // signal that copying finished.
+ // Some other error occurred.
+ // Same as if the stream were closed, but notify with the status
+ // NS_ERROR_UNEXPECTED so the observer knows something was wonky.
+ //
+
+ try
+ {
+ var quantum = pendingData[0];
+
+ // XXX |quantum| isn't guaranteed to be ASCII, so we're relying on
+ // undefined behavior! We're only using this because writeByteArray
+ // is unusably broken for asynchronous output streams; see bug 532834
+ // for details.
+ var bytesWritten = output.write(quantum, quantum.length);
+ if (bytesWritten === quantum.length)
+ pendingData.shift();
+ else
+ pendingData[0] = quantum.substring(bytesWritten);
+
+ dumpn("*** wrote " + bytesWritten + " bytes of data");
+ }
+ catch (e)
+ {
+ if (wouldBlock(e))
+ {
+ NS_ASSERT(pendingData.length > 0,
+ "stream-blocking exception with no data to write?");
+ NS_ASSERT(pendingData[0].length > 0,
+ "stream-blocking exception with empty quantum?");
+ this._waitToWriteData();
+ return;
+ }
+
+ if (streamClosed(e))
+ dumpn("!!! output stream prematurely closed, signaling error...");
+ else
+ dumpn("!!! unknown error: " + e + ", quantum=" + quantum);
+
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // The day is ours! Quantum written, now let's see if we have more data
+ // still to write.
+ try
+ {
+ if (pendingData.length > 0)
+ {
+ this._waitToWriteData();
+ return;
+ }
+ }
+ catch (e)
+ {
+ dumpn("!!! unexpected error waiting to write pending data: " + e);
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // Okay, we have no more pending data to write -- but might we get more in
+ // the future?
+ if (this._source !== null)
+ {
+ /*
+* If we might, then wait for the output stream to be closed. (We wait
+* only for closure because we have no data to write -- and if we waited
+* for a specific amount of data, we would get repeatedly notified for no
+* reason if over time the output stream permitted more and more data to
+* be written to it without blocking.)
+*/
+ this._waitForSinkClosure();
+ }
+ else
+ {
+ /*
+* On the other hand, if we can't have more data because the input
+* stream's gone away, then it's time to notify of copy completion.
+* Victory!
+*/
+ this._sink = null;
+ this._cancelOrDispatchCancelCallback(Cr.NS_OK);
+ }
+ },
+
+
+ // NSIREQUEST
+
+ /** Returns true if the cancel observer hasn't been notified yet. */
+ isPending: function()
+ {
+ return !this._completed;
+ },
+
+ /** Not implemented, don't use! */
+ suspend: notImplemented,
+ /** Not implemented, don't use! */
+ resume: notImplemented,
+
+ /**
+* Cancels data reading from input, asynchronously writes out any pending
+* data, and causes the observer to be notified with the given error code when
+* all writing has finished.
+*
+* @param status : nsresult
+* the status to pass to the observer when data copying has been canceled
+*/
+ cancel: function(status)
+ {
+ dumpn("*** cancel(" + status.toString(16) + ")");
+
+ if (this._canceled)
+ {
+ dumpn("*** suppressing a late cancel");
+ return;
+ }
+
+ this._canceled = true;
+ this.status = status;
+
+ // We could be in the middle of absolutely anything at this point. Both
+ // input and output might still be around, we might have pending data to
+ // write, and in general we know nothing about the state of the world. We
+ // therefore must assume everything's in progress and take everything to its
+ // final steady state (or so far as it can go before we need to finish
+ // writing out remaining data).
+
+ this._doneReadingSource(status);
+ },
+
+
+ // PRIVATE IMPLEMENTATION
+
+ /**
+* Stop reading input if we haven't already done so, passing e as the status
+* when closing the stream, and kick off a copy-completion notice if no more
+* data remains to be written.
+*
+* @param e : nsresult
+* the status to be used when closing the input stream
+*/
+ _doneReadingSource: function(e)
+ {
+ dumpn("*** _doneReadingSource(0x" + e.toString(16) + ")");
+
+ this._finishSource(e);
+ if (this._pendingData.length === 0)
+ this._sink = null;
+ else
+ NS_ASSERT(this._sink !== null, "null output?");
+
+ // If we've written out all data read up to this point, then it's time to
+ // signal completion.
+ if (this._sink === null)
+ {
+ NS_ASSERT(this._pendingData.length === 0, "pending data still?");
+ this._cancelOrDispatchCancelCallback(e);
+ }
+ },
+
+ /**
+* Stop writing output if we haven't already done so, discard any data that
+* remained to be sent, close off input if it wasn't already closed, and kick
+* off a copy-completion notice.
+*
+* @param e : nsresult
+* the status to be used when closing input if it wasn't already closed
+*/
+ _doneWritingToSink: function(e)
+ {
+ dumpn("*** _doneWritingToSink(0x" + e.toString(16) + ")");
+
+ this._pendingData.length = 0;
+ this._sink = null;
+ this._doneReadingSource(e);
+ },
+
+ /**
+* Completes processing of this copy: either by canceling the copy if it
+* hasn't already been canceled using the provided status, or by dispatching
+* the cancel callback event (with the originally provided status, of course)
+* if it already has been canceled.
+*
+* @param status : nsresult
+* the status code to use to cancel this, if this hasn't already been
+* canceled
+*/
+ _cancelOrDispatchCancelCallback: function(status)
+ {
+ dumpn("*** _cancelOrDispatchCancelCallback(" + status + ")");
+
+ NS_ASSERT(this._source === null, "should have finished input");
+ NS_ASSERT(this._sink === null, "should have finished output");
+ NS_ASSERT(this._pendingData.length === 0, "should have no pending data");
+
+ if (!this._canceled)
+ {
+ this.cancel(status);
+ return;
+ }
+
+ var self = this;
+ var event =
+ {
+ run: function()
+ {
+ dumpn("*** onStopRequest async callback");
+
+ self._completed = true;
+ try
+ {
+ self._observer.onStopRequest(self, self._context, self.status);
+ }
+ catch (e)
+ {
+ NS_ASSERT(false,
+ "how are we throwing an exception here? we control " +
+ "all the callers! " + e);
+ }
+ }
+ };
+
+ gThreadManager.currentThread.dispatch(event, Ci.nsIThread.DISPATCH_NORMAL);
+ },
+
+ /**
+* Kicks off another wait for more data to be available from the input stream.
+*/
+ _waitToReadData: function()
+ {
+ dumpn("*** _waitToReadData");
+ this._source.asyncWait(this, 0, Response.SEGMENT_SIZE,
+ gThreadManager.mainThread);
+ },
+
+ /**
+* Kicks off another wait until data can be written to the output stream.
+*/
+ _waitToWriteData: function()
+ {
+ dumpn("*** _waitToWriteData");
+
+ var pendingData = this._pendingData;
+ NS_ASSERT(pendingData.length > 0, "no pending data to write?");
+ NS_ASSERT(pendingData[0].length > 0, "buffered an empty write?");
+
+ this._sink.asyncWait(this, 0, pendingData[0].length,
+ gThreadManager.mainThread);
+ },
+
+ /**
+* Kicks off a wait for the sink to which data is being copied to be closed.
+* We wait for stream closure when we don't have any data to be copied, rather
+* than waiting to write a specific amount of data. We can't wait to write
+* data because the sink might be infinitely writable, and if no data appears
+* in the source for a long time we might have to spin quite a bit waiting to
+* write, waiting to write again, &c. Waiting on stream closure instead means
+* we'll get just one notification if the sink dies. Note that when data
+* starts arriving from the sink we'll resume waiting for data to be written,
+* dropping this closure-only callback entirely.
+*/
+ _waitForSinkClosure: function()
+ {
+ dumpn("*** _waitForSinkClosure");
+
+ this._sink.asyncWait(this, Ci.nsIAsyncOutputStream.WAIT_CLOSURE_ONLY, 0,
+ gThreadManager.mainThread);
+ },
+
+ /**
+* Closes input with the given status, if it hasn't already been closed;
+* otherwise a no-op.
+*
+* @param status : nsresult
+* status code use to close the source stream if necessary
+*/
+ _finishSource: function(status)
+ {
+ dumpn("*** _finishSource(" + status.toString(16) + ")");
+
+ if (this._source !== null)
+ {
+ this._source.closeWithStatus(status);
+ this._source = null;
+ }
+ }
+};
+
+
+/**
+* A container for utility functions used with HTTP headers.
+*/
+const headerUtils =
+{
+ /**
+* Normalizes fieldName (by converting it to lowercase) and ensures it is a
+* valid header field name (although not necessarily one specified in RFC
+* 2616).
+*
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not match the field-name production in RFC 2616
+* @returns string
+* fieldName converted to lowercase if it is a valid header, for characters
+* where case conversion is possible
+*/
+ normalizeFieldName: function(fieldName)
+ {
+ if (fieldName == "")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ for (var i = 0, sz = fieldName.length; i < sz; i++)
+ {
+ if (!IS_TOKEN_ARRAY[fieldName.charCodeAt(i)])
+ {
+ dumpn(fieldName + " is not a valid header field name!");
+ throw Cr.NS_ERROR_INVALID_ARG;
+ }
+ }
+
+ return fieldName.toLowerCase();
+ },
+
+ /**
+* Ensures that fieldValue is a valid header field value (although not
+* necessarily as specified in RFC 2616 if the corresponding field name is
+* part of the HTTP protocol), normalizes the value if it is, and
+* returns the normalized value.
+*
+* @param fieldValue : string
+* a value to be normalized as an HTTP header field value
+* @throws NS_ERROR_INVALID_ARG
+* if fieldValue does not match the field-value production in RFC 2616
+* @returns string
+* fieldValue as a normalized HTTP header field value
+*/
+ normalizeFieldValue: function(fieldValue)
+ {
+ // field-value = *( field-content | LWS )
+ // field-content = <the OCTETs making up the field-value
+ // and consisting of either *TEXT or combinations
+ // of token, separators, and quoted-string>
+ // TEXT = <any OCTET except CTLs,
+ // but including LWS>
+ // LWS = [CRLF] 1*( SP | HT )
+ //
+ // quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
+ // qdtext = <any TEXT except <">>
+ // quoted-pair = "\" CHAR
+ // CHAR = <any US-ASCII character (octets 0 - 127)>
+
+ // Any LWS that occurs between field-content MAY be replaced with a single
+ // SP before interpreting the field value or forwarding the message
+ // downstream (section 4.2); we replace 1*LWS with a single SP
+ var val = fieldValue.replace(/(?:(?:\r\n)?[ \t]+)+/g, " ");
+
+ // remove leading/trailing LWS (which has been converted to SP)
+ val = val.replace(/^ +/, "").replace(/ +$/, "");
+
+ // that should have taken care of all CTLs, so val should contain no CTLs
+ for (var i = 0, len = val.length; i < len; i++)
+ if (isCTL(val.charCodeAt(i)))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ // XXX disallows quoted-pair where CHAR is a CTL -- will not invalidly
+ // normalize, however, so this can be construed as a tightening of the
+ // spec and not entirely as a bug
+ return val;
+ }
+};
+
+
+
+/**
+* Converts the given string into a string which is safe for use in an HTML
+* context.
+*
+* @param str : string
+* the string to make HTML-safe
+* @returns string
+* an HTML-safe version of str
+*/
+function htmlEscape(str)
+{
+ // this is naive, but it'll work
+ var s = "";
+ for (var i = 0; i < str.length; i++)
+ s += "&#" + str.charCodeAt(i) + ";";
+ return s;
+}
+
+
+/**
+* Constructs an object representing an HTTP version (see section 3.1).
+*
+* @param versionString
+* a string of the form "#.#", where # is an non-negative decimal integer with
+* or without leading zeros
+* @throws
+* if versionString does not specify a valid HTTP version number
+*/
+function nsHttpVersion(versionString)
+{
+ var matches = /^(\d+)\.(\d+)$/.exec(versionString);
+ if (!matches)
+ throw "Not a valid HTTP version!";
+
+ /** The major version number of this, as a number. */
+ this.major = parseInt(matches[1], 10);
+
+ /** The minor version number of this, as a number. */
+ this.minor = parseInt(matches[2], 10);
+
+ if (isNaN(this.major) || isNaN(this.minor) ||
+ this.major < 0 || this.minor < 0)
+ throw "Not a valid HTTP version!";
+}
+nsHttpVersion.prototype =
+{
+ /**
+* Returns the standard string representation of the HTTP version represented
+* by this (e.g., "1.1").
+*/
+ toString: function ()
+ {
+ return this.major + "." + this.minor;
+ },
+
+ /**
+* Returns true if this represents the same HTTP version as otherVersion,
+* false otherwise.
+*
+* @param otherVersion : nsHttpVersion
+* the version to compare against this
+*/
+ equals: function (otherVersion)
+ {
+ return this.major == otherVersion.major &&
+ this.minor == otherVersion.minor;
+ },
+
+ /** True if this >= otherVersion, false otherwise. */
+ atLeast: function(otherVersion)
+ {
+ return this.major > otherVersion.major ||
+ (this.major == otherVersion.major &&
+ this.minor >= otherVersion.minor);
+ }
+};
+
+nsHttpVersion.HTTP_1_0 = new nsHttpVersion("1.0");
+nsHttpVersion.HTTP_1_1 = new nsHttpVersion("1.1");
+
+
+/**
+* An object which stores HTTP headers for a request or response.
+*
+* Note that since headers are case-insensitive, this object converts headers to
+* lowercase before storing them. This allows the getHeader and hasHeader
+* methods to work correctly for any case of a header, but it means that the
+* values returned by .enumerator may not be equal case-sensitively to the
+* values passed to setHeader when adding headers to this.
+*/
+function nsHttpHeaders()
+{
+ /**
+* A hash of headers, with header field names as the keys and header field
+* values as the values. Header field names are case-insensitive, but upon
+* insertion here they are converted to lowercase. Header field values are
+* normalized upon insertion to contain no leading or trailing whitespace.
+*
+* Note also that per RFC 2616, section 4.2, two headers with the same name in
+* a message may be treated as one header with the same field name and a field
+* value consisting of the separate field values joined together with a "," in
+* their original order. This hash stores multiple headers with the same name
+* in this manner.
+*/
+ this._headers = {};
+}
+nsHttpHeaders.prototype =
+{
+ /**
+* Sets the header represented by name and value in this.
+*
+* @param name : string
+* the header name
+* @param value : string
+* the header value
+* @throws NS_ERROR_INVALID_ARG
+* if name or value is not a valid header component
+*/
+ setHeader: function(fieldName, fieldValue, merge)
+ {
+ var name = headerUtils.normalizeFieldName(fieldName);
+ var value = headerUtils.normalizeFieldValue(fieldValue);
+
+ // The following three headers are stored as arrays because their real-world
+ // syntax prevents joining individual headers into a single header using
+ // ",". See also <http://hg.mozilla.org/mozilla-central/diff/9b2a99adc05e/netwerk/protocol/http/src/nsHttpHeaderArray.cpp#l77>
+ if (merge && name in this._headers)
+ {
+ if (name === "www-authenticate" ||
+ name === "proxy-authenticate" ||
+ name === "set-cookie")
+ {
+ this._headers[name].push(value);
+ }
+ else
+ {
+ this._headers[name][0] += "," + value;
+ NS_ASSERT(this._headers[name].length === 1,
+ "how'd a non-special header have multiple values?")
+ }
+ }
+ else
+ {
+ this._headers[name] = [value];
+ }
+ },
+
+ /**
+* Returns the value for the header specified by this.
+*
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not constitute a valid header field name
+* @throws NS_ERROR_NOT_AVAILABLE
+* if the given header does not exist in this
+* @returns string
+* the field value for the given header, possibly with non-semantic changes
+* (i.e., leading/trailing whitespace stripped, whitespace runs replaced
+* with spaces, etc.) at the option of the implementation; multiple
+* instances of the header will be combined with a comma, except for
+* the three headers noted in the description of getHeaderValues
+*/
+ getHeader: function(fieldName)
+ {
+ return this.getHeaderValues(fieldName).join("\n");
+ },
+
+ /**
+* Returns the value for the header specified by fieldName as an array.
+*
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not constitute a valid header field name
+* @throws NS_ERROR_NOT_AVAILABLE
+* if the given header does not exist in this
+* @returns [string]
+* an array of all the header values in this for the given
+* header name. Header values will generally be collapsed
+* into a single header by joining all header values together
+* with commas, but certain headers (Proxy-Authenticate,
+* WWW-Authenticate, and Set-Cookie) violate the HTTP spec
+* and cannot be collapsed in this manner. For these headers
+* only, the returned array may contain multiple elements if
+* that header has been added more than once.
+*/
+ getHeaderValues: function(fieldName)
+ {
+ var name = headerUtils.normalizeFieldName(fieldName);
+
+ if (name in this._headers)
+ return this._headers[name];
+ else
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ },
+
+ /**
+* Returns true if a header with the given field name exists in this, false
+* otherwise.
+*
+* @param fieldName : string
+* the field name whose existence is to be determined in this
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not constitute a valid header field name
+* @returns boolean
+* true if the header's present, false otherwise
+*/
+ hasHeader: function(fieldName)
+ {
+ var name = headerUtils.normalizeFieldName(fieldName);
+ return (name in this._headers);
+ },
+
+ /**
+* Returns a new enumerator over the field names of the headers in this, as
+* nsISupportsStrings. The names returned will be in lowercase, regardless of
+* how they were input using setHeader (header names are case-insensitive per
+* RFC 2616).
+*/
+ get enumerator()
+ {
+ var headers = [];
+ for (var i in this._headers)
+ {
+ var supports = new SupportsString();
+ supports.data = i;
+ headers.push(supports);
+ }
+
+ return new nsSimpleEnumerator(headers);
+ }
+};
+
+
+/**
+* Constructs an nsISimpleEnumerator for the given array of items.
+*
+* @param items : Array
+* the items, which must all implement nsISupports
+*/
+function nsSimpleEnumerator(items)
+{
+ this._items = items;
+ this._nextIndex = 0;
+}
+nsSimpleEnumerator.prototype =
+{
+ hasMoreElements: function()
+ {
+ return this._nextIndex < this._items.length;
+ },
+ getNext: function()
+ {
+ if (!this.hasMoreElements())
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+
+ return this._items[this._nextIndex++];
+ },
+ QueryInterface: function(aIID)
+ {
+ if (Ci.nsISimpleEnumerator.equals(aIID) ||
+ Ci.nsISupports.equals(aIID))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ }
+};
+
+
+/**
+* A representation of the data in an HTTP request.
+*
+* @param port : uint
+* the port on which the server receiving this request runs
+*/
+function Request(port)
+{
+ /** Method of this request, e.g. GET or POST. */
+ this._method = "";
+
+ /** Path of the requested resource; empty paths are converted to '/'. */
+ this._path = "";
+
+ /** Query string, if any, associated with this request (not including '?'). */
+ this._queryString = "";
+
+ /** Scheme of requested resource, usually http, always lowercase. */
+ this._scheme = "http";
+
+ /** Hostname on which the requested resource resides. */
+ this._host = undefined;
+
+ /** Port number over which the request was received. */
+ this._port = port;
+
+ var bodyPipe = new Pipe(false, false, 0, PR_UINT32_MAX, null);
+
+ /** Stream from which data in this request's body may be read. */
+ this._bodyInputStream = bodyPipe.inputStream;
+
+ /** Stream to which data in this request's body is written. */
+ this._bodyOutputStream = bodyPipe.outputStream;
+
+ /**
+* The headers in this request.
+*/
+ this._headers = new nsHttpHeaders();
+
+ /**
+* For the addition of ad-hoc properties and new functionality without having
+* to change nsIHttpRequest every time; currently lazily created, as its only
+* use is in directory listings.
+*/
+ this._bag = null;
+}
+Request.prototype =
+{
+ // SERVER METADATA
+
+ //
+ // see nsIHttpRequest.scheme
+ //
+ get scheme()
+ {
+ return this._scheme;
+ },
+
+ //
+ // see nsIHttpRequest.host
+ //
+ get host()
+ {
+ return this._host;
+ },
+
+ //
+ // see nsIHttpRequest.port
+ //
+ get port()
+ {
+ return this._port;
+ },
+
+ // REQUEST LINE
+
+ //
+ // see nsIHttpRequest.method
+ //
+ get method()
+ {
+ return this._method;
+ },
+
+ //
+ // see nsIHttpRequest.httpVersion
+ //
+ get httpVersion()
+ {
+ return this._httpVersion.toString();
+ },
+
+ //
+ // see nsIHttpRequest.path
+ //
+ get path()
+ {
+ return this._path;
+ },
+
+ //
+ // see nsIHttpRequest.queryString
+ //
+ get queryString()
+ {
+ return this._queryString;
+ },
+
+ // HEADERS
+
+ //
+ // see nsIHttpRequest.getHeader
+ //
+ getHeader: function(name)
+ {
+ return this._headers.getHeader(name);
+ },
+
+ //
+ // see nsIHttpRequest.hasHeader
+ //
+ hasHeader: function(name)
+ {
+ return this._headers.hasHeader(name);
+ },
+
+ //
+ // see nsIHttpRequest.headers
+ //
+ get headers()
+ {
+ return this._headers.enumerator;
+ },
+
+ //
+ // see nsIPropertyBag.enumerator
+ //
+ get enumerator()
+ {
+ this._ensurePropertyBag();
+ return this._bag.enumerator;
+ },
+
+ //
+ // see nsIHttpRequest.headers
+ //
+ get bodyInputStream()
+ {
+ return this._bodyInputStream;
+ },
+
+ //
+ // see nsIPropertyBag.getProperty
+ //
+ getProperty: function(name)
+ {
+ this._ensurePropertyBag();
+ return this._bag.getProperty(name);
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIHttpRequest) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // PRIVATE IMPLEMENTATION
+
+ /** Ensures a property bag has been created for ad-hoc behaviors. */
+ _ensurePropertyBag: function()
+ {
+ if (!this._bag)
+ this._bag = new WritablePropertyBag();
+ }
+};
+
+
+// XPCOM trappings
+if ("XPCOMUtils" in this && // Firefox 3.6 doesn't load XPCOMUtils in this scope for some reason...
+ "generateNSGetFactory" in XPCOMUtils) {
+ var NSGetFactory = XPCOMUtils.generateNSGetFactory([nsHttpServer]);
+}
+
+
+
+/**
+* Creates a new HTTP server listening for loopback traffic on the given port,
+* starts it, and runs the server until the server processes a shutdown request,
+* spinning an event loop so that events posted by the server's socket are
+* processed.
+*
+* This method is primarily intended for use in running this script from within
+* xpcshell and running a functional HTTP server without having to deal with
+* non-essential details.
+*
+* Note that running multiple servers using variants of this method probably
+* doesn't work, simply due to how the internal event loop is spun and stopped.
+*
+* @note
+* This method only works with Mozilla 1.9 (i.e., Firefox 3 or trunk code);
+* you should use this server as a component in Mozilla 1.8.
+* @param port
+* the port on which the server will run, or -1 if there exists no preference
+* for a specific port; note that attempting to use some values for this
+* parameter (particularly those below 1024) may cause this method to throw or
+* may result in the server being prematurely shut down
+* @param basePath
+* a local directory from which requests will be served (i.e., if this is
+* "/home/jwalden/" then a request to /index.html will load
+* /home/jwalden/index.html); if this is omitted, only the default URLs in
+* this server implementation will be functional
+*/
+function server(port, basePath)
+{
+ if (basePath)
+ {
+ var lp = Cc["@mozilla.org/file/local;1"]
+ .createInstance(Ci.nsILocalFile);
+ lp.initWithPath(basePath);
+ }
+
+ // if you're running this, you probably want to see debugging info
+ DEBUG = true;
+
+ var srv = new nsHttpServer();
+ if (lp)
+ srv.registerDirectory("/", lp);
+ srv.registerContentType("sjs", SJS_TYPE);
+ srv.start(port);
+
+ var thread = gThreadManager.currentThread;
+ while (!srv.isStopped())
+ thread.processNextEvent(true);
+
+ // get rid of any pending requests
+ while (thread.hasPendingEvents())
+ thread.processNextEvent(true);
+
+ DEBUG = false;
+}
+
+function startServerAsync(port, basePath)
+{
+ if (basePath)
+ {
+ var lp = Cc["@mozilla.org/file/local;1"]
+ .createInstance(Ci.nsILocalFile);
+ lp.initWithPath(basePath);
+ }
+
+ var srv = new nsHttpServer();
+ if (lp)
+ srv.registerDirectory("/", lp);
+ srv.registerContentType("sjs", "sjs");
+ srv.start(port);
+ return srv;
+}
+
+exports.nsHttpServer = nsHttpServer;
+exports.ScriptableInputStream = ScriptableInputStream;
+exports.server = server;
+exports.startServerAsync = startServerAsync;
diff --git a/addon-sdk/source/test/addons/content-script-messages-latency/main.js b/addon-sdk/source/test/addons/content-script-messages-latency/main.js
new file mode 100644
index 000000000..39bd7b64b
--- /dev/null
+++ b/addon-sdk/source/test/addons/content-script-messages-latency/main.js
@@ -0,0 +1,90 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { PageMod } = require("sdk/page-mod");
+const tabs = require("sdk/tabs");
+const { startServerAsync } = require("./httpd");
+const { setTimeout } = require("sdk/timers");
+
+const serverPort = 8099;
+
+exports.testContentScriptLatencyRegression = function*(assert) {
+ let server = startServerAsync(serverPort);
+ server.registerPathHandler("/", function handle(request, response) {
+ response.write(`<html>
+ <head>
+ <link rel="stylesheet" href="/slow.css">
+ </head>
+ <body>
+ slow loading page...
+ </body>
+ </html>`);
+ });
+
+ server.registerPathHandler("/slow.css", function handle(request, response) {
+ response.processAsync();
+ response.setHeader('Content-Type', 'text/css', false);
+ setTimeout(_ => {
+ response.write("body { background: red; }");
+ response.finish();
+ }, 2000);
+ });
+
+
+ let pageMod;
+
+ let worker = yield new Promise((resolve) => {
+ pageMod = PageMod({
+ include: "http://localhost:8099/",
+ attachTo: "top",
+ contentScriptWhen: "start",
+ contentScript: "new " + function ContentScriptScope() {
+ self.port.on("a-port-message", function () {
+ self.port.emit("document-ready-state", document.readyState);
+ });
+ },
+ onAttach: function(w) {
+ resolve(w);
+ }
+ });
+
+ tabs.open({
+ url: "http://localhost:8099/",
+ inBackground: true
+ });
+ });
+
+ worker.port.emit("a-port-message");
+
+ let waitForPortMessage = new Promise((resolve) => {
+ worker.port.once("document-ready-state", (msg) => {
+ resolve(msg);
+ });
+ });
+
+ let documentReadyState = yield waitForPortMessage;
+
+ assert.notEqual(
+ "complete", documentReadyState,
+ "content script received the port message when the page was still loading"
+ );
+
+ assert.notEqual(
+ "uninitialized", documentReadyState,
+ "content script should be frozen if document.readyState is still uninitialized"
+ );
+
+ assert.ok(
+ ["loading", "interactive"].includes(documentReadyState),
+ "content script message received with document.readyState was interactive or loading"
+ );
+
+ // Cleanup.
+ pageMod.destroy();
+ yield new Promise((resolve) => worker.tab.close(resolve));
+ yield new Promise((resolve) => server.stop(resolve));
+};
+
+require("sdk/test/runner").runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/content-script-messages-latency/package.json b/addon-sdk/source/test/addons/content-script-messages-latency/package.json
new file mode 100644
index 000000000..8280fe18b
--- /dev/null
+++ b/addon-sdk/source/test/addons/content-script-messages-latency/package.json
@@ -0,0 +1,6 @@
+
+{
+ "id": "content-script-messages-latency@jetpack",
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/contributors/main.js b/addon-sdk/source/test/addons/contributors/main.js
new file mode 100644
index 000000000..3827f277b
--- /dev/null
+++ b/addon-sdk/source/test/addons/contributors/main.js
@@ -0,0 +1,19 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { id } = require('sdk/self');
+const { getAddonByID } = require('sdk/addon/manager');
+
+exports.testContributors = function*(assert) {
+ let addon = yield getAddonByID(id);
+ let count = 0;
+ addon.contributors.forEach(({ name }) => {
+ assert.equal(name, ++count == 1 ? 'A' : 'B', 'The contributors keys are correct');
+ });
+ assert.equal(count, 2, 'The key count is correct');
+ assert.equal(addon.contributors.length, 2, 'The key length is correct');
+}
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/contributors/package.json b/addon-sdk/source/test/addons/contributors/package.json
new file mode 100644
index 000000000..b6f1798d3
--- /dev/null
+++ b/addon-sdk/source/test/addons/contributors/package.json
@@ -0,0 +1,6 @@
+{
+ "id": "test-contributors@jetpack",
+ "contributors": [ "A", "B" ],
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/curly-id/lib/main.js b/addon-sdk/source/test/addons/curly-id/lib/main.js
new file mode 100644
index 000000000..8b3f25645
--- /dev/null
+++ b/addon-sdk/source/test/addons/curly-id/lib/main.js
@@ -0,0 +1,29 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const simple = require('sdk/simple-prefs');
+const service = require('sdk/preferences/service');
+const { id, preferencesBranch } = require('sdk/self');
+const { getAddonByID } = require('sdk/addon/manager');
+
+exports.testCurlyID = function(assert) {
+ assert.equal(id, '{34a1eae1-c20a-464f-9b0e-000000000000}', 'curly ID is curly');
+ assert.equal(simple.prefs.test13, 26, 'test13 is 26');
+
+ simple.prefs.test14 = '15';
+ assert.equal(service.get('extensions.{34a1eae1-c20a-464f-9b0e-000000000000}.test14'), '15', 'test14 is 15');
+ assert.equal(service.get('extensions.{34a1eae1-c20a-464f-9b0e-000000000000}.test14'), simple.prefs.test14, 'simple test14 also 15');
+}
+
+// from `/test/test-self.js`, adapted to `sdk/test/assert` API
+exports.testSelfID = function*(assert) {
+ assert.equal(typeof(id), 'string', 'self.id is a string');
+ assert.ok(id.length > 0, 'self.id not empty');
+
+ let addon = yield getAddonByID(id);
+ assert.ok(addon, 'found addon with self.id');
+}
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/curly-id/package.json b/addon-sdk/source/test/addons/curly-id/package.json
new file mode 100644
index 000000000..213844662
--- /dev/null
+++ b/addon-sdk/source/test/addons/curly-id/package.json
@@ -0,0 +1,13 @@
+{
+ "id": "{34a1eae1-c20a-464f-9b0e-000000000000}",
+ "fullName": "curly ID test",
+ "author": "Tomislav Jovanovic",
+ "preferences": [{
+ "name": "test13",
+ "type": "integer",
+ "title": "test13",
+ "value": 26
+ }],
+ "main": "./lib/main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/developers/main.js b/addon-sdk/source/test/addons/developers/main.js
new file mode 100644
index 000000000..d42faf643
--- /dev/null
+++ b/addon-sdk/source/test/addons/developers/main.js
@@ -0,0 +1,19 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { id } = require('sdk/self');
+const { getAddonByID } = require('sdk/addon/manager');
+
+exports.testDevelopers = function*(assert) {
+ let addon = yield getAddonByID(id);
+ let count = 0;
+ addon.developers.forEach(({ name }) => {
+ assert.equal(name, ++count == 1 ? 'A' : 'B', 'The developers keys are correct');
+ });
+ assert.equal(count, 2, 'The key count is correct');
+ assert.equal(addon.developers.length, 2, 'The key length is correct');
+}
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/developers/package.json b/addon-sdk/source/test/addons/developers/package.json
new file mode 100644
index 000000000..1d2a189ec
--- /dev/null
+++ b/addon-sdk/source/test/addons/developers/package.json
@@ -0,0 +1,8 @@
+{
+ "id": "test-developers@jetpack",
+ "title": "Test developers package key",
+ "author": "Erik Vold",
+ "developers": [ "A", "B" ],
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/e10s-content/data/test-contentScriptFile.js b/addon-sdk/source/test/addons/e10s-content/data/test-contentScriptFile.js
new file mode 100644
index 000000000..7dc0e3f24
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-content/data/test-contentScriptFile.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+self.postMessage("msg from contentScriptFile");
diff --git a/addon-sdk/source/test/addons/e10s-content/data/test-page-worker.html b/addon-sdk/source/test/addons/e10s-content/data/test-page-worker.html
new file mode 100644
index 000000000..85264034a
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-content/data/test-page-worker.html
@@ -0,0 +1,13 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<html>
+<head>
+ <meta charset="UTF-8">
+ <title>Page Worker test</title>
+</head>
+<body>
+ <p id="paragraph">Lorem ipsum dolor sit amet.</p>
+</body>
+</html>
diff --git a/addon-sdk/source/test/addons/e10s-content/data/test-page-worker.js b/addon-sdk/source/test/addons/e10s-content/data/test-page-worker.js
new file mode 100644
index 000000000..5114fe4e0
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-content/data/test-page-worker.js
@@ -0,0 +1,29 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+
+// get title directly
+self.postMessage(["equal", document.title, "Page Worker test",
+ "Correct page title accessed directly"]);
+
+// get <p> directly
+var p = document.getElementById("paragraph");
+self.postMessage(["ok", !!p, "<p> can be accessed directly"]);
+self.postMessage(["equal", p.firstChild.nodeValue,
+ "Lorem ipsum dolor sit amet.",
+ "Correct text node expected"]);
+
+// Modify page
+var div = document.createElement("div");
+div.setAttribute("id", "block");
+div.appendChild(document.createTextNode("Test text created"));
+document.body.appendChild(div);
+
+// Check back the modification
+div = document.getElementById("block");
+self.postMessage(["ok", !!div, "<div> can be accessed directly"]);
+self.postMessage(["equal", div.firstChild.nodeValue,
+ "Test text created", "Correct text node expected"]);
+self.postMessage(["done"]);
+
diff --git a/addon-sdk/source/test/addons/e10s-content/data/test.html b/addon-sdk/source/test/addons/e10s-content/data/test.html
new file mode 100644
index 000000000..181e85f9b
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-content/data/test.html
@@ -0,0 +1,13 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<html>
+ <head>
+ <meta charset="UTF-8">
+ <title>foo</title>
+ </head>
+ <body>
+ <p>bar</p>
+ </body>
+</html>
diff --git a/addon-sdk/source/test/addons/e10s-content/lib/fixtures.js b/addon-sdk/source/test/addons/e10s-content/lib/fixtures.js
new file mode 100644
index 000000000..d3bd49300
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-content/lib/fixtures.js
@@ -0,0 +1,8 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { data } = require('sdk/self');
+
+exports.url = data.url;
diff --git a/addon-sdk/source/test/addons/e10s-content/lib/httpd.js b/addon-sdk/source/test/addons/e10s-content/lib/httpd.js
new file mode 100644
index 000000000..e46ca96a0
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-content/lib/httpd.js
@@ -0,0 +1,5212 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/*
+* An implementation of an HTTP server both as a loadable script and as an XPCOM
+* component. See the accompanying README file for user documentation on
+* httpd.js.
+*/
+
+module.metadata = {
+ "stability": "experimental"
+};
+
+const { components, CC, Cc, Ci, Cr, Cu } = require("chrome");
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+
+const PR_UINT32_MAX = Math.pow(2, 32) - 1;
+
+/** True if debugging output is enabled, false otherwise. */
+var DEBUG = false; // non-const *only* so tweakable in server tests
+
+/** True if debugging output should be timestamped. */
+var DEBUG_TIMESTAMP = false; // non-const so tweakable in server tests
+
+var gGlobalObject = Cc["@mozilla.org/systemprincipal;1"].createInstance();
+
+/**
+* Asserts that the given condition holds. If it doesn't, the given message is
+* dumped, a stack trace is printed, and an exception is thrown to attempt to
+* stop execution (which unfortunately must rely upon the exception not being
+* accidentally swallowed by the code that uses it).
+*/
+function NS_ASSERT(cond, msg)
+{
+ if (DEBUG && !cond)
+ {
+ dumpn("###!!!");
+ dumpn("###!!! ASSERTION" + (msg ? ": " + msg : "!"));
+ dumpn("###!!! Stack follows:");
+
+ var stack = new Error().stack.split(/\n/);
+ dumpn(stack.map(function(val) { return "###!!! " + val; }).join("\n"));
+
+ throw Cr.NS_ERROR_ABORT;
+ }
+}
+
+/** Constructs an HTTP error object. */
+function HttpError(code, description)
+{
+ this.code = code;
+ this.description = description;
+}
+HttpError.prototype =
+{
+ toString: function()
+ {
+ return this.code + " " + this.description;
+ }
+};
+
+/**
+* Errors thrown to trigger specific HTTP server responses.
+*/
+const HTTP_400 = new HttpError(400, "Bad Request");
+const HTTP_401 = new HttpError(401, "Unauthorized");
+const HTTP_402 = new HttpError(402, "Payment Required");
+const HTTP_403 = new HttpError(403, "Forbidden");
+const HTTP_404 = new HttpError(404, "Not Found");
+const HTTP_405 = new HttpError(405, "Method Not Allowed");
+const HTTP_406 = new HttpError(406, "Not Acceptable");
+const HTTP_407 = new HttpError(407, "Proxy Authentication Required");
+const HTTP_408 = new HttpError(408, "Request Timeout");
+const HTTP_409 = new HttpError(409, "Conflict");
+const HTTP_410 = new HttpError(410, "Gone");
+const HTTP_411 = new HttpError(411, "Length Required");
+const HTTP_412 = new HttpError(412, "Precondition Failed");
+const HTTP_413 = new HttpError(413, "Request Entity Too Large");
+const HTTP_414 = new HttpError(414, "Request-URI Too Long");
+const HTTP_415 = new HttpError(415, "Unsupported Media Type");
+const HTTP_417 = new HttpError(417, "Expectation Failed");
+
+const HTTP_500 = new HttpError(500, "Internal Server Error");
+const HTTP_501 = new HttpError(501, "Not Implemented");
+const HTTP_502 = new HttpError(502, "Bad Gateway");
+const HTTP_503 = new HttpError(503, "Service Unavailable");
+const HTTP_504 = new HttpError(504, "Gateway Timeout");
+const HTTP_505 = new HttpError(505, "HTTP Version Not Supported");
+
+/** Creates a hash with fields corresponding to the values in arr. */
+function array2obj(arr)
+{
+ var obj = {};
+ for (var i = 0; i < arr.length; i++)
+ obj[arr[i]] = arr[i];
+ return obj;
+}
+
+/** Returns an array of the integers x through y, inclusive. */
+function range(x, y)
+{
+ var arr = [];
+ for (var i = x; i <= y; i++)
+ arr.push(i);
+ return arr;
+}
+
+/** An object (hash) whose fields are the numbers of all HTTP error codes. */
+const HTTP_ERROR_CODES = array2obj(range(400, 417).concat(range(500, 505)));
+
+
+/**
+* The character used to distinguish hidden files from non-hidden files, a la
+* the leading dot in Apache. Since that mechanism also hides files from
+* easy display in LXR, ls output, etc. however, we choose instead to use a
+* suffix character. If a requested file ends with it, we append another
+* when getting the file on the server. If it doesn't, we just look up that
+* file. Therefore, any file whose name ends with exactly one of the character
+* is "hidden" and available for use by the server.
+*/
+const HIDDEN_CHAR = "^";
+
+/**
+* The file name suffix indicating the file containing overridden headers for
+* a requested file.
+*/
+const HEADERS_SUFFIX = HIDDEN_CHAR + "headers" + HIDDEN_CHAR;
+
+/** Type used to denote SJS scripts for CGI-like functionality. */
+const SJS_TYPE = "sjs";
+
+/** Base for relative timestamps produced by dumpn(). */
+var firstStamp = 0;
+
+/** dump(str) with a trailing "\n" -- only outputs if DEBUG. */
+function dumpn(str)
+{
+ if (DEBUG)
+ {
+ var prefix = "HTTPD-INFO | ";
+ if (DEBUG_TIMESTAMP)
+ {
+ if (firstStamp === 0)
+ firstStamp = Date.now();
+
+ var elapsed = Date.now() - firstStamp; // milliseconds
+ var min = Math.floor(elapsed / 60000);
+ var sec = (elapsed % 60000) / 1000;
+
+ if (sec < 10)
+ prefix += min + ":0" + sec.toFixed(3) + " | ";
+ else
+ prefix += min + ":" + sec.toFixed(3) + " | ";
+ }
+
+ dump(prefix + str + "\n");
+ }
+}
+
+/** Dumps the current JS stack if DEBUG. */
+function dumpStack()
+{
+ // peel off the frames for dumpStack() and Error()
+ var stack = new Error().stack.split(/\n/).slice(2);
+ stack.forEach(dumpn);
+}
+
+
+/** The XPCOM thread manager. */
+var gThreadManager = null;
+
+/** The XPCOM prefs service. */
+var gRootPrefBranch = null;
+function getRootPrefBranch()
+{
+ if (!gRootPrefBranch)
+ {
+ gRootPrefBranch = Cc["@mozilla.org/preferences-service;1"]
+ .getService(Ci.nsIPrefBranch);
+ }
+ return gRootPrefBranch;
+}
+
+/**
+* JavaScript constructors for commonly-used classes; precreating these is a
+* speedup over doing the same from base principles. See the docs at
+* http://developer.mozilla.org/en/docs/components.Constructor for details.
+*/
+const ServerSocket = CC("@mozilla.org/network/server-socket;1",
+ "nsIServerSocket",
+ "init");
+const ScriptableInputStream = CC("@mozilla.org/scriptableinputstream;1",
+ "nsIScriptableInputStream",
+ "init");
+const Pipe = CC("@mozilla.org/pipe;1",
+ "nsIPipe",
+ "init");
+const FileInputStream = CC("@mozilla.org/network/file-input-stream;1",
+ "nsIFileInputStream",
+ "init");
+const ConverterInputStream = CC("@mozilla.org/intl/converter-input-stream;1",
+ "nsIConverterInputStream",
+ "init");
+const WritablePropertyBag = CC("@mozilla.org/hash-property-bag;1",
+ "nsIWritablePropertyBag2");
+const SupportsString = CC("@mozilla.org/supports-string;1",
+ "nsISupportsString");
+
+/* These two are non-const only so a test can overwrite them. */
+var BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
+ "nsIBinaryInputStream",
+ "setInputStream");
+var BinaryOutputStream = CC("@mozilla.org/binaryoutputstream;1",
+ "nsIBinaryOutputStream",
+ "setOutputStream");
+
+/**
+* Returns the RFC 822/1123 representation of a date.
+*
+* @param date : Number
+* the date, in milliseconds from midnight (00:00:00), January 1, 1970 GMT
+* @returns string
+* the representation of the given date
+*/
+function toDateString(date)
+{
+ //
+ // rfc1123-date = wkday "," SP date1 SP time SP "GMT"
+ // date1 = 2DIGIT SP month SP 4DIGIT
+ // ; day month year (e.g., 02 Jun 1982)
+ // time = 2DIGIT ":" 2DIGIT ":" 2DIGIT
+ // ; 00:00:00 - 23:59:59
+ // wkday = "Mon" | "Tue" | "Wed"
+ // | "Thu" | "Fri" | "Sat" | "Sun"
+ // month = "Jan" | "Feb" | "Mar" | "Apr"
+ // | "May" | "Jun" | "Jul" | "Aug"
+ // | "Sep" | "Oct" | "Nov" | "Dec"
+ //
+
+ const wkdayStrings = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
+ const monthStrings = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
+
+ /**
+* Processes a date and returns the encoded UTC time as a string according to
+* the format specified in RFC 2616.
+*
+* @param date : Date
+* the date to process
+* @returns string
+* a string of the form "HH:MM:SS", ranging from "00:00:00" to "23:59:59"
+*/
+ function toTime(date)
+ {
+ var hrs = date.getUTCHours();
+ var rv = (hrs < 10) ? "0" + hrs : hrs;
+
+ var mins = date.getUTCMinutes();
+ rv += ":";
+ rv += (mins < 10) ? "0" + mins : mins;
+
+ var secs = date.getUTCSeconds();
+ rv += ":";
+ rv += (secs < 10) ? "0" + secs : secs;
+
+ return rv;
+ }
+
+ /**
+* Processes a date and returns the encoded UTC date as a string according to
+* the date1 format specified in RFC 2616.
+*
+* @param date : Date
+* the date to process
+* @returns string
+* a string of the form "HH:MM:SS", ranging from "00:00:00" to "23:59:59"
+*/
+ function toDate1(date)
+ {
+ var day = date.getUTCDate();
+ var month = date.getUTCMonth();
+ var year = date.getUTCFullYear();
+
+ var rv = (day < 10) ? "0" + day : day;
+ rv += " " + monthStrings[month];
+ rv += " " + year;
+
+ return rv;
+ }
+
+ date = new Date(date);
+
+ const fmtString = "%wkday%, %date1% %time% GMT";
+ var rv = fmtString.replace("%wkday%", wkdayStrings[date.getUTCDay()]);
+ rv = rv.replace("%time%", toTime(date));
+ return rv.replace("%date1%", toDate1(date));
+}
+
+/**
+* Prints out a human-readable representation of the object o and its fields,
+* omitting those whose names begin with "_" if showMembers != true (to ignore
+* "private" properties exposed via getters/setters).
+*/
+function printObj(o, showMembers)
+{
+ var s = "******************************\n";
+ s += "o = {\n";
+ for (var i in o)
+ {
+ if (typeof(i) != "string" ||
+ (showMembers || (i.length > 0 && i[0] != "_")))
+ s+= " " + i + ": " + o[i] + ",\n";
+ }
+ s += " };\n";
+ s += "******************************";
+ dumpn(s);
+}
+
+/**
+* Instantiates a new HTTP server.
+*/
+function nsHttpServer()
+{
+ if (!gThreadManager)
+ gThreadManager = Cc["@mozilla.org/thread-manager;1"].getService();
+
+ /** The port on which this server listens. */
+ this._port = undefined;
+
+ /** The socket associated with this. */
+ this._socket = null;
+
+ /** The handler used to process requests to this server. */
+ this._handler = new ServerHandler(this);
+
+ /** Naming information for this server. */
+ this._identity = new ServerIdentity();
+
+ /**
+* Indicates when the server is to be shut down at the end of the request.
+*/
+ this._doQuit = false;
+
+ /**
+* True if the socket in this is closed (and closure notifications have been
+* sent and processed if the socket was ever opened), false otherwise.
+*/
+ this._socketClosed = true;
+
+ /**
+* Used for tracking existing connections and ensuring that all connections
+* are properly cleaned up before server shutdown; increases by 1 for every
+* new incoming connection.
+*/
+ this._connectionGen = 0;
+
+ /**
+* Hash of all open connections, indexed by connection number at time of
+* creation.
+*/
+ this._connections = {};
+}
+nsHttpServer.prototype =
+{
+ classID: components.ID("{54ef6f81-30af-4b1d-ac55-8ba811293e41}"),
+
+ // NSISERVERSOCKETLISTENER
+
+ /**
+* Processes an incoming request coming in on the given socket and contained
+* in the given transport.
+*
+* @param socket : nsIServerSocket
+* the socket through which the request was served
+* @param trans : nsISocketTransport
+* the transport for the request/response
+* @see nsIServerSocketListener.onSocketAccepted
+*/
+ onSocketAccepted: function(socket, trans)
+ {
+ dumpn("*** onSocketAccepted(socket=" + socket + ", trans=" + trans + ")");
+
+ dumpn(">>> new connection on " + trans.host + ":" + trans.port);
+
+ const SEGMENT_SIZE = 8192;
+ const SEGMENT_COUNT = 1024;
+ try
+ {
+ var input = trans.openInputStream(0, SEGMENT_SIZE, SEGMENT_COUNT)
+ .QueryInterface(Ci.nsIAsyncInputStream);
+ var output = trans.openOutputStream(0, 0, 0);
+ }
+ catch (e)
+ {
+ dumpn("*** error opening transport streams: " + e);
+ trans.close(Cr.NS_BINDING_ABORTED);
+ return;
+ }
+
+ var connectionNumber = ++this._connectionGen;
+
+ try
+ {
+ var conn = new Connection(input, output, this, socket.port, trans.port,
+ connectionNumber);
+ var reader = new RequestReader(conn);
+
+ // XXX add request timeout functionality here!
+
+ // Note: must use main thread here, or we might get a GC that will cause
+ // threadsafety assertions. We really need to fix XPConnect so that
+ // you can actually do things in multi-threaded JS. :-(
+ input.asyncWait(reader, 0, 0, gThreadManager.mainThread);
+ }
+ catch (e)
+ {
+ // Assume this connection can't be salvaged and bail on it completely;
+ // don't attempt to close it so that we can assert that any connection
+ // being closed is in this._connections.
+ dumpn("*** error in initial request-processing stages: " + e);
+ trans.close(Cr.NS_BINDING_ABORTED);
+ return;
+ }
+
+ this._connections[connectionNumber] = conn;
+ dumpn("*** starting connection " + connectionNumber);
+ },
+
+ /**
+* Called when the socket associated with this is closed.
+*
+* @param socket : nsIServerSocket
+* the socket being closed
+* @param status : nsresult
+* the reason the socket stopped listening (NS_BINDING_ABORTED if the server
+* was stopped using nsIHttpServer.stop)
+* @see nsIServerSocketListener.onStopListening
+*/
+ onStopListening: function(socket, status)
+ {
+ dumpn(">>> shutting down server on port " + socket.port);
+ this._socketClosed = true;
+ if (!this._hasOpenConnections())
+ {
+ dumpn("*** no open connections, notifying async from onStopListening");
+
+ // Notify asynchronously so that any pending teardown in stop() has a
+ // chance to run first.
+ var self = this;
+ var stopEvent =
+ {
+ run: function()
+ {
+ dumpn("*** _notifyStopped async callback");
+ self._notifyStopped();
+ }
+ };
+ gThreadManager.currentThread
+ .dispatch(stopEvent, Ci.nsIThread.DISPATCH_NORMAL);
+ }
+ },
+
+ // NSIHTTPSERVER
+
+ //
+ // see nsIHttpServer.start
+ //
+ start: function(port)
+ {
+ this._start(port, "localhost")
+ },
+
+ _start: function(port, host)
+ {
+ if (this._socket)
+ throw Cr.NS_ERROR_ALREADY_INITIALIZED;
+
+ this._port = port;
+ this._doQuit = this._socketClosed = false;
+
+ this._host = host;
+
+ // The listen queue needs to be long enough to handle
+ // network.http.max-persistent-connections-per-server concurrent connections,
+ // plus a safety margin in case some other process is talking to
+ // the server as well.
+ var prefs = getRootPrefBranch();
+ var maxConnections;
+ try {
+ // Bug 776860: The original pref was removed in favor of this new one:
+ maxConnections = prefs.getIntPref("network.http.max-persistent-connections-per-server") + 5;
+ }
+ catch(e) {
+ maxConnections = prefs.getIntPref("network.http.max-connections-per-server") + 5;
+ }
+
+ try
+ {
+ var loopback = true;
+ if (this._host != "127.0.0.1" && this._host != "localhost") {
+ var loopback = false;
+ }
+
+ var socket = new ServerSocket(this._port,
+ loopback, // true = localhost, false = everybody
+ maxConnections);
+ dumpn(">>> listening on port " + socket.port + ", " + maxConnections +
+ " pending connections");
+ socket.asyncListen(this);
+ this._identity._initialize(socket.port, host, true);
+ this._socket = socket;
+ }
+ catch (e)
+ {
+ dumpn("!!! could not start server on port " + port + ": " + e);
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ }
+ },
+
+ //
+ // see nsIHttpServer.stop
+ //
+ stop: function(callback)
+ {
+ if (!callback)
+ throw Cr.NS_ERROR_NULL_POINTER;
+ if (!this._socket)
+ throw Cr.NS_ERROR_UNEXPECTED;
+
+ this._stopCallback = typeof callback === "function"
+ ? callback
+ : function() { callback.onStopped(); };
+
+ dumpn(">>> stopping listening on port " + this._socket.port);
+ this._socket.close();
+ this._socket = null;
+
+ // We can't have this identity any more, and the port on which we're running
+ // this server now could be meaningless the next time around.
+ this._identity._teardown();
+
+ this._doQuit = false;
+
+ // socket-close notification and pending request completion happen async
+ },
+
+ //
+ // see nsIHttpServer.registerFile
+ //
+ registerFile: function(path, file)
+ {
+ if (file && (!file.exists() || file.isDirectory()))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ this._handler.registerFile(path, file);
+ },
+
+ //
+ // see nsIHttpServer.registerDirectory
+ //
+ registerDirectory: function(path, directory)
+ {
+ // XXX true path validation!
+ if (path.charAt(0) != "/" ||
+ path.charAt(path.length - 1) != "/" ||
+ (directory &&
+ (!directory.exists() || !directory.isDirectory())))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ // XXX determine behavior of nonexistent /foo/bar when a /foo/bar/ mapping
+ // exists!
+
+ this._handler.registerDirectory(path, directory);
+ },
+
+ //
+ // see nsIHttpServer.registerPathHandler
+ //
+ registerPathHandler: function(path, handler)
+ {
+ this._handler.registerPathHandler(path, handler);
+ },
+
+ //
+ // see nsIHttpServer.registerPrefixHandler
+ //
+ registerPrefixHandler: function(prefix, handler)
+ {
+ this._handler.registerPrefixHandler(prefix, handler);
+ },
+
+ //
+ // see nsIHttpServer.registerErrorHandler
+ //
+ registerErrorHandler: function(code, handler)
+ {
+ this._handler.registerErrorHandler(code, handler);
+ },
+
+ //
+ // see nsIHttpServer.setIndexHandler
+ //
+ setIndexHandler: function(handler)
+ {
+ this._handler.setIndexHandler(handler);
+ },
+
+ //
+ // see nsIHttpServer.registerContentType
+ //
+ registerContentType: function(ext, type)
+ {
+ this._handler.registerContentType(ext, type);
+ },
+
+ //
+ // see nsIHttpServer.serverIdentity
+ //
+ get identity()
+ {
+ return this._identity;
+ },
+
+ //
+ // see nsIHttpServer.getState
+ //
+ getState: function(path, k)
+ {
+ return this._handler._getState(path, k);
+ },
+
+ //
+ // see nsIHttpServer.setState
+ //
+ setState: function(path, k, v)
+ {
+ return this._handler._setState(path, k, v);
+ },
+
+ //
+ // see nsIHttpServer.getSharedState
+ //
+ getSharedState: function(k)
+ {
+ return this._handler._getSharedState(k);
+ },
+
+ //
+ // see nsIHttpServer.setSharedState
+ //
+ setSharedState: function(k, v)
+ {
+ return this._handler._setSharedState(k, v);
+ },
+
+ //
+ // see nsIHttpServer.getObjectState
+ //
+ getObjectState: function(k)
+ {
+ return this._handler._getObjectState(k);
+ },
+
+ //
+ // see nsIHttpServer.setObjectState
+ //
+ setObjectState: function(k, v)
+ {
+ return this._handler._setObjectState(k, v);
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIServerSocketListener) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // NON-XPCOM PUBLIC API
+
+ /**
+* Returns true iff this server is not running (and is not in the process of
+* serving any requests still to be processed when the server was last
+* stopped after being run).
+*/
+ isStopped: function()
+ {
+ return this._socketClosed && !this._hasOpenConnections();
+ },
+
+ // PRIVATE IMPLEMENTATION
+
+ /** True if this server has any open connections to it, false otherwise. */
+ _hasOpenConnections: function()
+ {
+ //
+ // If we have any open connections, they're tracked as numeric properties on
+ // |this._connections|. The non-standard __count__ property could be used
+ // to check whether there are any properties, but standard-wise, even
+ // looking forward to ES5, there's no less ugly yet still O(1) way to do
+ // this.
+ //
+ for (var n in this._connections)
+ return true;
+ return false;
+ },
+
+ /** Calls the server-stopped callback provided when stop() was called. */
+ _notifyStopped: function()
+ {
+ NS_ASSERT(this._stopCallback !== null, "double-notifying?");
+ NS_ASSERT(!this._hasOpenConnections(), "should be done serving by now");
+
+ //
+ // NB: We have to grab this now, null out the member, *then* call the
+ // callback here, or otherwise the callback could (indirectly) futz with
+ // this._stopCallback by starting and immediately stopping this, at
+ // which point we'd be nulling out a field we no longer have a right to
+ // modify.
+ //
+ var callback = this._stopCallback;
+ this._stopCallback = null;
+ try
+ {
+ callback();
+ }
+ catch (e)
+ {
+ // not throwing because this is specified as being usually (but not
+ // always) asynchronous
+ dump("!!! error running onStopped callback: " + e + "\n");
+ }
+ },
+
+ /**
+* Notifies this server that the given connection has been closed.
+*
+* @param connection : Connection
+* the connection that was closed
+*/
+ _connectionClosed: function(connection)
+ {
+ NS_ASSERT(connection.number in this._connections,
+ "closing a connection " + this + " that we never added to the " +
+ "set of open connections?");
+ NS_ASSERT(this._connections[connection.number] === connection,
+ "connection number mismatch? " +
+ this._connections[connection.number]);
+ delete this._connections[connection.number];
+
+ // Fire a pending server-stopped notification if it's our responsibility.
+ if (!this._hasOpenConnections() && this._socketClosed)
+ this._notifyStopped();
+ },
+
+ /**
+* Requests that the server be shut down when possible.
+*/
+ _requestQuit: function()
+ {
+ dumpn(">>> requesting a quit");
+ dumpStack();
+ this._doQuit = true;
+ }
+};
+
+
+//
+// RFC 2396 section 3.2.2:
+//
+// host = hostname | IPv4address
+// hostname = *( domainlabel "." ) toplabel [ "." ]
+// domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
+// toplabel = alpha | alpha *( alphanum | "-" ) alphanum
+// IPv4address = 1*digit "." 1*digit "." 1*digit "." 1*digit
+//
+
+const HOST_REGEX =
+ new RegExp("^(?:" +
+ // *( domainlabel "." )
+ "(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)*" +
+ // toplabel
+ "[a-z](?:[a-z0-9-]*[a-z0-9])?" +
+ "|" +
+ // IPv4 address
+ "\\d+\\.\\d+\\.\\d+\\.\\d+" +
+ ")$",
+ "i");
+
+
+/**
+* Represents the identity of a server. An identity consists of a set of
+* (scheme, host, port) tuples denoted as locations (allowing a single server to
+* serve multiple sites or to be used behind both HTTP and HTTPS proxies for any
+* host/port). Any incoming request must be to one of these locations, or it
+* will be rejected with an HTTP 400 error. One location, denoted as the
+* primary location, is the location assigned in contexts where a location
+* cannot otherwise be endogenously derived, such as for HTTP/1.0 requests.
+*
+* A single identity may contain at most one location per unique host/port pair;
+* other than that, no restrictions are placed upon what locations may
+* constitute an identity.
+*/
+function ServerIdentity()
+{
+ /** The scheme of the primary location. */
+ this._primaryScheme = "http";
+
+ /** The hostname of the primary location. */
+ this._primaryHost = "127.0.0.1"
+
+ /** The port number of the primary location. */
+ this._primaryPort = -1;
+
+ /**
+* The current port number for the corresponding server, stored so that a new
+* primary location can always be set if the current one is removed.
+*/
+ this._defaultPort = -1;
+
+ /**
+* Maps hosts to maps of ports to schemes, e.g. the following would represent
+* https://example.com:789/ and http://example.org/:
+*
+* {
+* "xexample.com": { 789: "https" },
+* "xexample.org": { 80: "http" }
+* }
+*
+* Note the "x" prefix on hostnames, which prevents collisions with special
+* JS names like "prototype".
+*/
+ this._locations = { "xlocalhost": {} };
+}
+ServerIdentity.prototype =
+{
+ // NSIHTTPSERVERIDENTITY
+
+ //
+ // see nsIHttpServerIdentity.primaryScheme
+ //
+ get primaryScheme()
+ {
+ if (this._primaryPort === -1)
+ throw Cr.NS_ERROR_NOT_INITIALIZED;
+ return this._primaryScheme;
+ },
+
+ //
+ // see nsIHttpServerIdentity.primaryHost
+ //
+ get primaryHost()
+ {
+ if (this._primaryPort === -1)
+ throw Cr.NS_ERROR_NOT_INITIALIZED;
+ return this._primaryHost;
+ },
+
+ //
+ // see nsIHttpServerIdentity.primaryPort
+ //
+ get primaryPort()
+ {
+ if (this._primaryPort === -1)
+ throw Cr.NS_ERROR_NOT_INITIALIZED;
+ return this._primaryPort;
+ },
+
+ //
+ // see nsIHttpServerIdentity.add
+ //
+ add: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ var entry = this._locations["x" + host];
+ if (!entry)
+ this._locations["x" + host] = entry = {};
+
+ entry[port] = scheme;
+ },
+
+ //
+ // see nsIHttpServerIdentity.remove
+ //
+ remove: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ var entry = this._locations["x" + host];
+ if (!entry)
+ return false;
+
+ var present = port in entry;
+ delete entry[port];
+
+ if (this._primaryScheme == scheme &&
+ this._primaryHost == host &&
+ this._primaryPort == port &&
+ this._defaultPort !== -1)
+ {
+ // Always keep at least one identity in existence at any time, unless
+ // we're in the process of shutting down (the last condition above).
+ this._primaryPort = -1;
+ this._initialize(this._defaultPort, host, false);
+ }
+
+ return present;
+ },
+
+ //
+ // see nsIHttpServerIdentity.has
+ //
+ has: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ return "x" + host in this._locations &&
+ scheme === this._locations["x" + host][port];
+ },
+
+ //
+ // see nsIHttpServerIdentity.has
+ //
+ getScheme: function(host, port)
+ {
+ this._validate("http", host, port);
+
+ var entry = this._locations["x" + host];
+ if (!entry)
+ return "";
+
+ return entry[port] || "";
+ },
+
+ //
+ // see nsIHttpServerIdentity.setPrimary
+ //
+ setPrimary: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ this.add(scheme, host, port);
+
+ this._primaryScheme = scheme;
+ this._primaryHost = host;
+ this._primaryPort = port;
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIHttpServerIdentity) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // PRIVATE IMPLEMENTATION
+
+ /**
+* Initializes the primary name for the corresponding server, based on the
+* provided port number.
+*/
+ _initialize: function(port, host, addSecondaryDefault)
+ {
+ this._host = host;
+ if (this._primaryPort !== -1)
+ this.add("http", host, port);
+ else
+ this.setPrimary("http", "localhost", port);
+ this._defaultPort = port;
+
+ // Only add this if we're being called at server startup
+ if (addSecondaryDefault && host != "127.0.0.1")
+ this.add("http", "127.0.0.1", port);
+ },
+
+ /**
+* Called at server shutdown time, unsets the primary location only if it was
+* the default-assigned location and removes the default location from the
+* set of locations used.
+*/
+ _teardown: function()
+ {
+ if (this._host != "127.0.0.1") {
+ // Not the default primary location, nothing special to do here
+ this.remove("http", "127.0.0.1", this._defaultPort);
+ }
+
+ // This is a *very* tricky bit of reasoning here; make absolutely sure the
+ // tests for this code pass before you commit changes to it.
+ if (this._primaryScheme == "http" &&
+ this._primaryHost == this._host &&
+ this._primaryPort == this._defaultPort)
+ {
+ // Make sure we don't trigger the readding logic in .remove(), then remove
+ // the default location.
+ var port = this._defaultPort;
+ this._defaultPort = -1;
+ this.remove("http", this._host, port);
+
+ // Ensure a server start triggers the setPrimary() path in ._initialize()
+ this._primaryPort = -1;
+ }
+ else
+ {
+ // No reason not to remove directly as it's not our primary location
+ this.remove("http", this._host, this._defaultPort);
+ }
+ },
+
+ /**
+* Ensures scheme, host, and port are all valid with respect to RFC 2396.
+*
+* @throws NS_ERROR_ILLEGAL_VALUE
+* if any argument doesn't match the corresponding production
+*/
+ _validate: function(scheme, host, port)
+ {
+ if (scheme !== "http" && scheme !== "https")
+ {
+ dumpn("*** server only supports http/https schemes: '" + scheme + "'");
+ dumpStack();
+ throw Cr.NS_ERROR_ILLEGAL_VALUE;
+ }
+ if (!HOST_REGEX.test(host))
+ {
+ dumpn("*** unexpected host: '" + host + "'");
+ throw Cr.NS_ERROR_ILLEGAL_VALUE;
+ }
+ if (port < 0 || port > 65535)
+ {
+ dumpn("*** unexpected port: '" + port + "'");
+ throw Cr.NS_ERROR_ILLEGAL_VALUE;
+ }
+ }
+};
+
+
+/**
+* Represents a connection to the server (and possibly in the future the thread
+* on which the connection is processed).
+*
+* @param input : nsIInputStream
+* stream from which incoming data on the connection is read
+* @param output : nsIOutputStream
+* stream to write data out the connection
+* @param server : nsHttpServer
+* the server handling the connection
+* @param port : int
+* the port on which the server is running
+* @param outgoingPort : int
+* the outgoing port used by this connection
+* @param number : uint
+* a serial number used to uniquely identify this connection
+*/
+function Connection(input, output, server, port, outgoingPort, number)
+{
+ dumpn("*** opening new connection " + number + " on port " + outgoingPort);
+
+ /** Stream of incoming data. */
+ this.input = input;
+
+ /** Stream for outgoing data. */
+ this.output = output;
+
+ /** The server associated with this request. */
+ this.server = server;
+
+ /** The port on which the server is running. */
+ this.port = port;
+
+ /** The outgoing poort used by this connection. */
+ this._outgoingPort = outgoingPort;
+
+ /** The serial number of this connection. */
+ this.number = number;
+
+ /**
+* The request for which a response is being generated, null if the
+* incoming request has not been fully received or if it had errors.
+*/
+ this.request = null;
+
+ /** State variables for debugging. */
+ this._closed = this._processed = false;
+}
+Connection.prototype =
+{
+ /** Closes this connection's input/output streams. */
+ close: function()
+ {
+ dumpn("*** closing connection " + this.number +
+ " on port " + this._outgoingPort);
+
+ this.input.close();
+ this.output.close();
+ this._closed = true;
+
+ var server = this.server;
+ server._connectionClosed(this);
+
+ // If an error triggered a server shutdown, act on it now
+ if (server._doQuit)
+ server.stop(function() { /* not like we can do anything better */ });
+ },
+
+ /**
+* Initiates processing of this connection, using the data in the given
+* request.
+*
+* @param request : Request
+* the request which should be processed
+*/
+ process: function(request)
+ {
+ NS_ASSERT(!this._closed && !this._processed);
+
+ this._processed = true;
+
+ this.request = request;
+ this.server._handler.handleResponse(this);
+ },
+
+ /**
+* Initiates processing of this connection, generating a response with the
+* given HTTP error code.
+*
+* @param code : uint
+* an HTTP code, so in the range [0, 1000)
+* @param request : Request
+* incomplete data about the incoming request (since there were errors
+* during its processing
+*/
+ processError: function(code, request)
+ {
+ NS_ASSERT(!this._closed && !this._processed);
+
+ this._processed = true;
+ this.request = request;
+ this.server._handler.handleError(code, this);
+ },
+
+ /** Converts this to a string for debugging purposes. */
+ toString: function()
+ {
+ return "<Connection(" + this.number +
+ (this.request ? ", " + this.request.path : "") +"): " +
+ (this._closed ? "closed" : "open") + ">";
+ }
+};
+
+
+
+/** Returns an array of count bytes from the given input stream. */
+function readBytes(inputStream, count)
+{
+ return new BinaryInputStream(inputStream).readByteArray(count);
+}
+
+
+
+/** Request reader processing states; see RequestReader for details. */
+const READER_IN_REQUEST_LINE = 0;
+const READER_IN_HEADERS = 1;
+const READER_IN_BODY = 2;
+const READER_FINISHED = 3;
+
+
+/**
+* Reads incoming request data asynchronously, does any necessary preprocessing,
+* and forwards it to the request handler. Processing occurs in three states:
+*
+* READER_IN_REQUEST_LINE Reading the request's status line
+* READER_IN_HEADERS Reading headers in the request
+* READER_IN_BODY Reading the body of the request
+* READER_FINISHED Entire request has been read and processed
+*
+* During the first two stages, initial metadata about the request is gathered
+* into a Request object. Once the status line and headers have been processed,
+* we start processing the body of the request into the Request. Finally, when
+* the entire body has been read, we create a Response and hand it off to the
+* ServerHandler to be given to the appropriate request handler.
+*
+* @param connection : Connection
+* the connection for the request being read
+*/
+function RequestReader(connection)
+{
+ /** Connection metadata for this request. */
+ this._connection = connection;
+
+ /**
+* A container providing line-by-line access to the raw bytes that make up the
+* data which has been read from the connection but has not yet been acted
+* upon (by passing it to the request handler or by extracting request
+* metadata from it).
+*/
+ this._data = new LineData();
+
+ /**
+* The amount of data remaining to be read from the body of this request.
+* After all headers in the request have been read this is the value in the
+* Content-Length header, but as the body is read its value decreases to zero.
+*/
+ this._contentLength = 0;
+
+ /** The current state of parsing the incoming request. */
+ this._state = READER_IN_REQUEST_LINE;
+
+ /** Metadata constructed from the incoming request for the request handler. */
+ this._metadata = new Request(connection.port);
+
+ /**
+* Used to preserve state if we run out of line data midway through a
+* multi-line header. _lastHeaderName stores the name of the header, while
+* _lastHeaderValue stores the value we've seen so far for the header.
+*
+* These fields are always either both undefined or both strings.
+*/
+ this._lastHeaderName = this._lastHeaderValue = undefined;
+}
+RequestReader.prototype =
+{
+ // NSIINPUTSTREAMCALLBACK
+
+ /**
+* Called when more data from the incoming request is available. This method
+* then reads the available data from input and deals with that data as
+* necessary, depending upon the syntax of already-downloaded data.
+*
+* @param input : nsIAsyncInputStream
+* the stream of incoming data from the connection
+*/
+ onInputStreamReady: function(input)
+ {
+ dumpn("*** onInputStreamReady(input=" + input + ") on thread " +
+ gThreadManager.currentThread + " (main is " +
+ gThreadManager.mainThread + ")");
+ dumpn("*** this._state == " + this._state);
+
+ // Handle cases where we get more data after a request error has been
+ // discovered but *before* we can close the connection.
+ var data = this._data;
+ if (!data)
+ return;
+
+ try
+ {
+ data.appendBytes(readBytes(input, input.available()));
+ }
+ catch (e)
+ {
+ if (streamClosed(e))
+ {
+ dumpn("*** WARNING: unexpected error when reading from socket; will " +
+ "be treated as if the input stream had been closed");
+ dumpn("*** WARNING: actual error was: " + e);
+ }
+
+ // We've lost a race -- input has been closed, but we're still expecting
+ // to read more data. available() will throw in this case, and since
+ // we're dead in the water now, destroy the connection.
+ dumpn("*** onInputStreamReady called on a closed input, destroying " +
+ "connection");
+ this._connection.close();
+ return;
+ }
+
+ switch (this._state)
+ {
+ default:
+ NS_ASSERT(false, "invalid state: " + this._state);
+ break;
+
+ case READER_IN_REQUEST_LINE:
+ if (!this._processRequestLine())
+ break;
+ /* fall through */
+
+ case READER_IN_HEADERS:
+ if (!this._processHeaders())
+ break;
+ /* fall through */
+
+ case READER_IN_BODY:
+ this._processBody();
+ }
+
+ if (this._state != READER_FINISHED)
+ input.asyncWait(this, 0, 0, gThreadManager.currentThread);
+ },
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(aIID)
+ {
+ if (aIID.equals(Ci.nsIInputStreamCallback) ||
+ aIID.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // PRIVATE API
+
+ /**
+* Processes unprocessed, downloaded data as a request line.
+*
+* @returns boolean
+* true iff the request line has been fully processed
+*/
+ _processRequestLine: function()
+ {
+ NS_ASSERT(this._state == READER_IN_REQUEST_LINE);
+
+ // Servers SHOULD ignore any empty line(s) received where a Request-Line
+ // is expected (section 4.1).
+ var data = this._data;
+ var line = {};
+ var readSuccess;
+ while ((readSuccess = data.readLine(line)) && line.value == "")
+ dumpn("*** ignoring beginning blank line...");
+
+ // if we don't have a full line, wait until we do
+ if (!readSuccess)
+ return false;
+
+ // we have the first non-blank line
+ try
+ {
+ this._parseRequestLine(line.value);
+ this._state = READER_IN_HEADERS;
+ return true;
+ }
+ catch (e)
+ {
+ this._handleError(e);
+ return false;
+ }
+ },
+
+ /**
+* Processes stored data, assuming it is either at the beginning or in
+* the middle of processing request headers.
+*
+* @returns boolean
+* true iff header data in the request has been fully processed
+*/
+ _processHeaders: function()
+ {
+ NS_ASSERT(this._state == READER_IN_HEADERS);
+
+ // XXX things to fix here:
+ //
+ // - need to support RFC 2047-encoded non-US-ASCII characters
+
+ try
+ {
+ var done = this._parseHeaders();
+ if (done)
+ {
+ var request = this._metadata;
+
+ // XXX this is wrong for requests with transfer-encodings applied to
+ // them, particularly chunked (which by its nature can have no
+ // meaningful Content-Length header)!
+ this._contentLength = request.hasHeader("Content-Length")
+ ? parseInt(request.getHeader("Content-Length"), 10)
+ : 0;
+ dumpn("_processHeaders, Content-length=" + this._contentLength);
+
+ this._state = READER_IN_BODY;
+ }
+ return done;
+ }
+ catch (e)
+ {
+ this._handleError(e);
+ return false;
+ }
+ },
+
+ /**
+* Processes stored data, assuming it is either at the beginning or in
+* the middle of processing the request body.
+*
+* @returns boolean
+* true iff the request body has been fully processed
+*/
+ _processBody: function()
+ {
+ NS_ASSERT(this._state == READER_IN_BODY);
+
+ // XXX handle chunked transfer-coding request bodies!
+
+ try
+ {
+ if (this._contentLength > 0)
+ {
+ var data = this._data.purge();
+ var count = Math.min(data.length, this._contentLength);
+ dumpn("*** loading data=" + data + " len=" + data.length +
+ " excess=" + (data.length - count));
+
+ var bos = new BinaryOutputStream(this._metadata._bodyOutputStream);
+ bos.writeByteArray(data, count);
+ this._contentLength -= count;
+ }
+
+ dumpn("*** remaining body data len=" + this._contentLength);
+ if (this._contentLength == 0)
+ {
+ this._validateRequest();
+ this._state = READER_FINISHED;
+ this._handleResponse();
+ return true;
+ }
+
+ return false;
+ }
+ catch (e)
+ {
+ this._handleError(e);
+ return false;
+ }
+ },
+
+ /**
+* Does various post-header checks on the data in this request.
+*
+* @throws : HttpError
+* if the request was malformed in some way
+*/
+ _validateRequest: function()
+ {
+ NS_ASSERT(this._state == READER_IN_BODY);
+
+ dumpn("*** _validateRequest");
+
+ var metadata = this._metadata;
+ var headers = metadata._headers;
+
+ // 19.6.1.1 -- servers MUST report 400 to HTTP/1.1 requests w/o Host header
+ var identity = this._connection.server.identity;
+ if (metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1))
+ {
+ if (!headers.hasHeader("Host"))
+ {
+ dumpn("*** malformed HTTP/1.1 or greater request with no Host header!");
+ throw HTTP_400;
+ }
+
+ // If the Request-URI wasn't absolute, then we need to determine our host.
+ // We have to determine what scheme was used to access us based on the
+ // server identity data at this point, because the request just doesn't
+ // contain enough data on its own to do this, sadly.
+ if (!metadata._host)
+ {
+ var host, port;
+ var hostPort = headers.getHeader("Host");
+ var colon = hostPort.indexOf(":");
+ if (colon < 0)
+ {
+ host = hostPort;
+ port = "";
+ }
+ else
+ {
+ host = hostPort.substring(0, colon);
+ port = hostPort.substring(colon + 1);
+ }
+
+ // NB: We allow an empty port here because, oddly, a colon may be
+ // present even without a port number, e.g. "example.com:"; in this
+ // case the default port applies.
+ if (!HOST_REGEX.test(host) || !/^\d*$/.test(port))
+ {
+ dumpn("*** malformed hostname (" + hostPort + ") in Host " +
+ "header, 400 time");
+ throw HTTP_400;
+ }
+
+ // If we're not given a port, we're stuck, because we don't know what
+ // scheme to use to look up the correct port here, in general. Since
+ // the HTTPS case requires a tunnel/proxy and thus requires that the
+ // requested URI be absolute (and thus contain the necessary
+ // information), let's assume HTTP will prevail and use that.
+ port = +port || 80;
+
+ var scheme = identity.getScheme(host, port);
+ if (!scheme)
+ {
+ dumpn("*** unrecognized hostname (" + hostPort + ") in Host " +
+ "header, 400 time");
+ throw HTTP_400;
+ }
+
+ metadata._scheme = scheme;
+ metadata._host = host;
+ metadata._port = port;
+ }
+ }
+ else
+ {
+ NS_ASSERT(metadata._host === undefined,
+ "HTTP/1.0 doesn't allow absolute paths in the request line!");
+
+ metadata._scheme = identity.primaryScheme;
+ metadata._host = identity.primaryHost;
+ metadata._port = identity.primaryPort;
+ }
+
+ NS_ASSERT(identity.has(metadata._scheme, metadata._host, metadata._port),
+ "must have a location we recognize by now!");
+ },
+
+ /**
+* Handles responses in case of error, either in the server or in the request.
+*
+* @param e
+* the specific error encountered, which is an HttpError in the case where
+* the request is in some way invalid or cannot be fulfilled; if this isn't
+* an HttpError we're going to be paranoid and shut down, because that
+* shouldn't happen, ever
+*/
+ _handleError: function(e)
+ {
+ // Don't fall back into normal processing!
+ this._state = READER_FINISHED;
+
+ var server = this._connection.server;
+ if (e instanceof HttpError)
+ {
+ var code = e.code;
+ }
+ else
+ {
+ dumpn("!!! UNEXPECTED ERROR: " + e +
+ (e.lineNumber ? ", line " + e.lineNumber : ""));
+
+ // no idea what happened -- be paranoid and shut down
+ code = 500;
+ server._requestQuit();
+ }
+
+ // make attempted reuse of data an error
+ this._data = null;
+
+ this._connection.processError(code, this._metadata);
+ },
+
+ /**
+* Now that we've read the request line and headers, we can actually hand off
+* the request to be handled.
+*
+* This method is called once per request, after the request line and all
+* headers and the body, if any, have been received.
+*/
+ _handleResponse: function()
+ {
+ NS_ASSERT(this._state == READER_FINISHED);
+
+ // We don't need the line-based data any more, so make attempted reuse an
+ // error.
+ this._data = null;
+
+ this._connection.process(this._metadata);
+ },
+
+
+ // PARSING
+
+ /**
+* Parses the request line for the HTTP request associated with this.
+*
+* @param line : string
+* the request line
+*/
+ _parseRequestLine: function(line)
+ {
+ NS_ASSERT(this._state == READER_IN_REQUEST_LINE);
+
+ dumpn("*** _parseRequestLine('" + line + "')");
+
+ var metadata = this._metadata;
+
+ // clients and servers SHOULD accept any amount of SP or HT characters
+ // between fields, even though only a single SP is required (section 19.3)
+ var request = line.split(/[ \t]+/);
+ if (!request || request.length != 3)
+ throw HTTP_400;
+
+ metadata._method = request[0];
+
+ // get the HTTP version
+ var ver = request[2];
+ var match = ver.match(/^HTTP\/(\d+\.\d+)$/);
+ if (!match)
+ throw HTTP_400;
+
+ // determine HTTP version
+ try
+ {
+ metadata._httpVersion = new nsHttpVersion(match[1]);
+ if (!metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_0))
+ throw "unsupported HTTP version";
+ }
+ catch (e)
+ {
+ // we support HTTP/1.0 and HTTP/1.1 only
+ throw HTTP_501;
+ }
+
+
+ var fullPath = request[1];
+ var serverIdentity = this._connection.server.identity;
+
+ var scheme, host, port;
+
+ if (fullPath.charAt(0) != "/")
+ {
+ // No absolute paths in the request line in HTTP prior to 1.1
+ if (!metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1))
+ throw HTTP_400;
+
+ try
+ {
+ var uri = Cc["@mozilla.org/network/io-service;1"]
+ .getService(Ci.nsIIOService)
+ .newURI(fullPath, null, null);
+ fullPath = uri.path;
+ scheme = uri.scheme;
+ host = metadata._host = uri.asciiHost;
+ port = uri.port;
+ if (port === -1)
+ {
+ if (scheme === "http")
+ port = 80;
+ else if (scheme === "https")
+ port = 443;
+ else
+ throw HTTP_400;
+ }
+ }
+ catch (e)
+ {
+ // If the host is not a valid host on the server, the response MUST be a
+ // 400 (Bad Request) error message (section 5.2). Alternately, the URI
+ // is malformed.
+ throw HTTP_400;
+ }
+
+ if (!serverIdentity.has(scheme, host, port) || fullPath.charAt(0) != "/")
+ throw HTTP_400;
+ }
+
+ var splitter = fullPath.indexOf("?");
+ if (splitter < 0)
+ {
+ // _queryString already set in ctor
+ metadata._path = fullPath;
+ }
+ else
+ {
+ metadata._path = fullPath.substring(0, splitter);
+ metadata._queryString = fullPath.substring(splitter + 1);
+ }
+
+ metadata._scheme = scheme;
+ metadata._host = host;
+ metadata._port = port;
+ },
+
+ /**
+* Parses all available HTTP headers in this until the header-ending CRLFCRLF,
+* adding them to the store of headers in the request.
+*
+* @throws
+* HTTP_400 if the headers are malformed
+* @returns boolean
+* true if all headers have now been processed, false otherwise
+*/
+ _parseHeaders: function()
+ {
+ NS_ASSERT(this._state == READER_IN_HEADERS);
+
+ dumpn("*** _parseHeaders");
+
+ var data = this._data;
+
+ var headers = this._metadata._headers;
+ var lastName = this._lastHeaderName;
+ var lastVal = this._lastHeaderValue;
+
+ var line = {};
+ while (true)
+ {
+ NS_ASSERT(!((lastVal === undefined) ^ (lastName === undefined)),
+ lastName === undefined ?
+ "lastVal without lastName? lastVal: '" + lastVal + "'" :
+ "lastName without lastVal? lastName: '" + lastName + "'");
+
+ if (!data.readLine(line))
+ {
+ // save any data we have from the header we might still be processing
+ this._lastHeaderName = lastName;
+ this._lastHeaderValue = lastVal;
+ return false;
+ }
+
+ var lineText = line.value;
+ var firstChar = lineText.charAt(0);
+
+ // blank line means end of headers
+ if (lineText == "")
+ {
+ // we're finished with the previous header
+ if (lastName)
+ {
+ try
+ {
+ headers.setHeader(lastName, lastVal, true);
+ }
+ catch (e)
+ {
+ dumpn("*** e == " + e);
+ throw HTTP_400;
+ }
+ }
+ else
+ {
+ // no headers in request -- valid for HTTP/1.0 requests
+ }
+
+ // either way, we're done processing headers
+ this._state = READER_IN_BODY;
+ return true;
+ }
+ else if (firstChar == " " || firstChar == "\t")
+ {
+ // multi-line header if we've already seen a header line
+ if (!lastName)
+ {
+ // we don't have a header to continue!
+ throw HTTP_400;
+ }
+
+ // append this line's text to the value; starts with SP/HT, so no need
+ // for separating whitespace
+ lastVal += lineText;
+ }
+ else
+ {
+ // we have a new header, so set the old one (if one existed)
+ if (lastName)
+ {
+ try
+ {
+ headers.setHeader(lastName, lastVal, true);
+ }
+ catch (e)
+ {
+ dumpn("*** e == " + e);
+ throw HTTP_400;
+ }
+ }
+
+ var colon = lineText.indexOf(":"); // first colon must be splitter
+ if (colon < 1)
+ {
+ // no colon or missing header field-name
+ throw HTTP_400;
+ }
+
+ // set header name, value (to be set in the next loop, usually)
+ lastName = lineText.substring(0, colon);
+ lastVal = lineText.substring(colon + 1);
+ } // empty, continuation, start of header
+ } // while (true)
+ }
+};
+
+
+/** The character codes for CR and LF. */
+const CR = 0x0D, LF = 0x0A;
+
+/**
+* Calculates the number of characters before the first CRLF pair in array, or
+* -1 if the array contains no CRLF pair.
+*
+* @param array : Array
+* an array of numbers in the range [0, 256), each representing a single
+* character; the first CRLF is the lowest index i where
+* |array[i] == "\r".charCodeAt(0)| and |array[i+1] == "\n".charCodeAt(0)|,
+* if such an |i| exists, and -1 otherwise
+* @returns int
+* the index of the first CRLF if any were present, -1 otherwise
+*/
+function findCRLF(array)
+{
+ for (var i = array.indexOf(CR); i >= 0; i = array.indexOf(CR, i + 1))
+ {
+ if (array[i + 1] == LF)
+ return i;
+ }
+ return -1;
+}
+
+
+/**
+* A container which provides line-by-line access to the arrays of bytes with
+* which it is seeded.
+*/
+function LineData()
+{
+ /** An array of queued bytes from which to get line-based characters. */
+ this._data = [];
+}
+LineData.prototype =
+{
+ /**
+* Appends the bytes in the given array to the internal data cache maintained
+* by this.
+*/
+ appendBytes: function(bytes)
+ {
+ Array.prototype.push.apply(this._data, bytes);
+ },
+
+ /**
+* Removes and returns a line of data, delimited by CRLF, from this.
+*
+* @param out
+* an object whose "value" property will be set to the first line of text
+* present in this, sans CRLF, if this contains a full CRLF-delimited line
+* of text; if this doesn't contain enough data, the value of the property
+* is undefined
+* @returns boolean
+* true if a full line of data could be read from the data in this, false
+* otherwise
+*/
+ readLine: function(out)
+ {
+ var data = this._data;
+ var length = findCRLF(data);
+ if (length < 0)
+ return false;
+
+ //
+ // We have the index of the CR, so remove all the characters, including
+ // CRLF, from the array with splice, and convert the removed array into the
+ // corresponding string, from which we then strip the trailing CRLF.
+ //
+ // Getting the line in this matter acknowledges that substring is an O(1)
+ // operation in SpiderMonkey because strings are immutable, whereas two
+ // splices, both from the beginning of the data, are less likely to be as
+ // cheap as a single splice plus two extra character conversions.
+ //
+ var line = String.fromCharCode.apply(null, data.splice(0, length + 2));
+ out.value = line.substring(0, length);
+
+ return true;
+ },
+
+ /**
+* Removes the bytes currently within this and returns them in an array.
+*
+* @returns Array
+* the bytes within this when this method is called
+*/
+ purge: function()
+ {
+ var data = this._data;
+ this._data = [];
+ return data;
+ }
+};
+
+
+
+/**
+* Creates a request-handling function for an nsIHttpRequestHandler object.
+*/
+function createHandlerFunc(handler)
+{
+ return function(metadata, response) { handler.handle(metadata, response); };
+}
+
+
+/**
+* The default handler for directories; writes an HTML response containing a
+* slightly-formatted directory listing.
+*/
+function defaultIndexHandler(metadata, response)
+{
+ response.setHeader("Content-Type", "text/html", false);
+
+ var path = htmlEscape(decodeURI(metadata.path));
+
+ //
+ // Just do a very basic bit of directory listings -- no need for too much
+ // fanciness, especially since we don't have a style sheet in which we can
+ // stick rules (don't want to pollute the default path-space).
+ //
+
+ var body = '<html>\
+<head>\
+<title>' + path + '</title>\
+</head>\
+<body>\
+<h1>' + path + '</h1>\
+<ol style="list-style-type: none">';
+
+ var directory = metadata.getProperty("directory").QueryInterface(Ci.nsILocalFile);
+ NS_ASSERT(directory && directory.isDirectory());
+
+ var fileList = [];
+ var files = directory.directoryEntries;
+ while (files.hasMoreElements())
+ {
+ var f = files.getNext().QueryInterface(Ci.nsIFile);
+ var name = f.leafName;
+ if (!f.isHidden() &&
+ (name.charAt(name.length - 1) != HIDDEN_CHAR ||
+ name.charAt(name.length - 2) == HIDDEN_CHAR))
+ fileList.push(f);
+ }
+
+ fileList.sort(fileSort);
+
+ for (var i = 0; i < fileList.length; i++)
+ {
+ var file = fileList[i];
+ try
+ {
+ var name = file.leafName;
+ if (name.charAt(name.length - 1) == HIDDEN_CHAR)
+ name = name.substring(0, name.length - 1);
+ var sep = file.isDirectory() ? "/" : "";
+
+ // Note: using " to delimit the attribute here because encodeURIComponent
+ // passes through '.
+ var item = '<li><a href="' + encodeURIComponent(name) + sep + '">' +
+ htmlEscape(name) + sep +
+ '</a></li>';
+
+ body += item;
+ }
+ catch (e) { /* some file system error, ignore the file */ }
+ }
+
+ body += ' </ol>\
+</body>\
+</html>';
+
+ response.bodyOutputStream.write(body, body.length);
+}
+
+/**
+* Sorts a and b (nsIFile objects) into an aesthetically pleasing order.
+*/
+function fileSort(a, b)
+{
+ var dira = a.isDirectory(), dirb = b.isDirectory();
+
+ if (dira && !dirb)
+ return -1;
+ if (dirb && !dira)
+ return 1;
+
+ var namea = a.leafName.toLowerCase(), nameb = b.leafName.toLowerCase();
+ return nameb > namea ? -1 : 1;
+}
+
+
+/**
+* Converts an externally-provided path into an internal path for use in
+* determining file mappings.
+*
+* @param path
+* the path to convert
+* @param encoded
+* true if the given path should be passed through decodeURI prior to
+* conversion
+* @throws URIError
+* if path is incorrectly encoded
+*/
+function toInternalPath(path, encoded)
+{
+ if (encoded)
+ path = decodeURI(path);
+
+ var comps = path.split("/");
+ for (var i = 0, sz = comps.length; i < sz; i++)
+ {
+ var comp = comps[i];
+ if (comp.charAt(comp.length - 1) == HIDDEN_CHAR)
+ comps[i] = comp + HIDDEN_CHAR;
+ }
+ return comps.join("/");
+}
+
+
+/**
+* Adds custom-specified headers for the given file to the given response, if
+* any such headers are specified.
+*
+* @param file
+* the file on the disk which is to be written
+* @param metadata
+* metadata about the incoming request
+* @param response
+* the Response to which any specified headers/data should be written
+* @throws HTTP_500
+* if an error occurred while processing custom-specified headers
+*/
+function maybeAddHeaders(file, metadata, response)
+{
+ var name = file.leafName;
+ if (name.charAt(name.length - 1) == HIDDEN_CHAR)
+ name = name.substring(0, name.length - 1);
+
+ var headerFile = file.parent;
+ headerFile.append(name + HEADERS_SUFFIX);
+
+ if (!headerFile.exists())
+ return;
+
+ const PR_RDONLY = 0x01;
+ var fis = new FileInputStream(headerFile, PR_RDONLY, 0o444,
+ Ci.nsIFileInputStream.CLOSE_ON_EOF);
+
+ try
+ {
+ var lis = new ConverterInputStream(fis, "UTF-8", 1024, 0x0);
+ lis.QueryInterface(Ci.nsIUnicharLineInputStream);
+
+ var line = {value: ""};
+ var more = lis.readLine(line);
+
+ if (!more && line.value == "")
+ return;
+
+
+ // request line
+
+ var status = line.value;
+ if (status.indexOf("HTTP ") == 0)
+ {
+ status = status.substring(5);
+ var space = status.indexOf(" ");
+ var code, description;
+ if (space < 0)
+ {
+ code = status;
+ description = "";
+ }
+ else
+ {
+ code = status.substring(0, space);
+ description = status.substring(space + 1, status.length);
+ }
+
+ response.setStatusLine(metadata.httpVersion, parseInt(code, 10), description);
+
+ line.value = "";
+ more = lis.readLine(line);
+ }
+
+ // headers
+ while (more || line.value != "")
+ {
+ var header = line.value;
+ var colon = header.indexOf(":");
+
+ response.setHeader(header.substring(0, colon),
+ header.substring(colon + 1, header.length),
+ false); // allow overriding server-set headers
+
+ line.value = "";
+ more = lis.readLine(line);
+ }
+ }
+ catch (e)
+ {
+ dumpn("WARNING: error in headers for " + metadata.path + ": " + e);
+ throw HTTP_500;
+ }
+ finally
+ {
+ fis.close();
+ }
+}
+
+
+/**
+* An object which handles requests for a server, executing default and
+* overridden behaviors as instructed by the code which uses and manipulates it.
+* Default behavior includes the paths / and /trace (diagnostics), with some
+* support for HTTP error pages for various codes and fallback to HTTP 500 if
+* those codes fail for any reason.
+*
+* @param server : nsHttpServer
+* the server in which this handler is being used
+*/
+function ServerHandler(server)
+{
+ // FIELDS
+
+ /**
+* The nsHttpServer instance associated with this handler.
+*/
+ this._server = server;
+
+ /**
+* A FileMap object containing the set of path->nsILocalFile mappings for
+* all directory mappings set in the server (e.g., "/" for /var/www/html/,
+* "/foo/bar/" for /local/path/, and "/foo/bar/baz/" for /local/path2).
+*
+* Note carefully: the leading and trailing "/" in each path (not file) are
+* removed before insertion to simplify the code which uses this. You have
+* been warned!
+*/
+ this._pathDirectoryMap = new FileMap();
+
+ /**
+* Custom request handlers for the server in which this resides. Path-handler
+* pairs are stored as property-value pairs in this property.
+*
+* @see ServerHandler.prototype._defaultPaths
+*/
+ this._overridePaths = {};
+
+ /**
+* Custom request handlers for the server in which this resides. Prefix-handler
+* pairs are stored as property-value pairs in this property.
+*/
+ this._overridePrefixes = {};
+
+ /**
+* Custom request handlers for the error handlers in the server in which this
+* resides. Path-handler pairs are stored as property-value pairs in this
+* property.
+*
+* @see ServerHandler.prototype._defaultErrors
+*/
+ this._overrideErrors = {};
+
+ /**
+* Maps file extensions to their MIME types in the server, overriding any
+* mapping that might or might not exist in the MIME service.
+*/
+ this._mimeMappings = {};
+
+ /**
+* The default handler for requests for directories, used to serve directories
+* when no index file is present.
+*/
+ this._indexHandler = defaultIndexHandler;
+
+ /** Per-path state storage for the server. */
+ this._state = {};
+
+ /** Entire-server state storage. */
+ this._sharedState = {};
+
+ /** Entire-server state storage for nsISupports values. */
+ this._objectState = {};
+}
+ServerHandler.prototype =
+{
+ // PUBLIC API
+
+ /**
+* Handles a request to this server, responding to the request appropriately
+* and initiating server shutdown if necessary.
+*
+* This method never throws an exception.
+*
+* @param connection : Connection
+* the connection for this request
+*/
+ handleResponse: function(connection)
+ {
+ var request = connection.request;
+ var response = new Response(connection);
+
+ var path = request.path;
+ dumpn("*** path == " + path);
+
+ try
+ {
+ try
+ {
+ if (path in this._overridePaths)
+ {
+ // explicit paths first, then files based on existing directory mappings,
+ // then (if the file doesn't exist) built-in server default paths
+ dumpn("calling override for " + path);
+ this._overridePaths[path](request, response);
+ }
+ else
+ {
+ let longestPrefix = "";
+ for (let prefix in this._overridePrefixes)
+ {
+ if (prefix.length > longestPrefix.length && path.startsWith(prefix))
+ {
+ longestPrefix = prefix;
+ }
+ }
+ if (longestPrefix.length > 0)
+ {
+ dumpn("calling prefix override for " + longestPrefix);
+ this._overridePrefixes[longestPrefix](request, response);
+ }
+ else
+ {
+ this._handleDefault(request, response);
+ }
+ }
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort(e);
+ return;
+ }
+
+ if (!(e instanceof HttpError))
+ {
+ dumpn("*** unexpected error: e == " + e);
+ throw HTTP_500;
+ }
+ if (e.code !== 404)
+ throw e;
+
+ dumpn("*** default: " + (path in this._defaultPaths));
+
+ response = new Response(connection);
+ if (path in this._defaultPaths)
+ this._defaultPaths[path](request, response);
+ else
+ throw HTTP_404;
+ }
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort(e);
+ return;
+ }
+
+ var errorCode = "internal";
+
+ try
+ {
+ if (!(e instanceof HttpError))
+ throw e;
+
+ errorCode = e.code;
+ dumpn("*** errorCode == " + errorCode);
+
+ response = new Response(connection);
+ if (e.customErrorHandling)
+ e.customErrorHandling(response);
+ this._handleError(errorCode, request, response);
+ return;
+ }
+ catch (e2)
+ {
+ dumpn("*** error handling " + errorCode + " error: " +
+ "e2 == " + e2 + ", shutting down server");
+
+ connection.server._requestQuit();
+ response.abort(e2);
+ return;
+ }
+ }
+
+ response.complete();
+ },
+
+ //
+ // see nsIHttpServer.registerFile
+ //
+ registerFile: function(path, file)
+ {
+ if (!file)
+ {
+ dumpn("*** unregistering '" + path + "' mapping");
+ delete this._overridePaths[path];
+ return;
+ }
+
+ dumpn("*** registering '" + path + "' as mapping to " + file.path);
+ file = file.clone();
+
+ var self = this;
+ this._overridePaths[path] =
+ function(request, response)
+ {
+ if (!file.exists())
+ throw HTTP_404;
+
+ response.setStatusLine(request.httpVersion, 200, "OK");
+ self._writeFileResponse(request, file, response, 0, file.fileSize);
+ };
+ },
+
+ //
+ // see nsIHttpServer.registerPathHandler
+ //
+ registerPathHandler: function(path, handler)
+ {
+ // XXX true path validation!
+ if (path.charAt(0) != "/")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ this._handlerToField(handler, this._overridePaths, path);
+ },
+
+ //
+ // see nsIHttpServer.registerPrefixHandler
+ //
+ registerPrefixHandler: function(prefix, handler)
+ {
+ // XXX true prefix validation!
+ if (!(prefix.startsWith("/") && prefix.endsWith("/")))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ this._handlerToField(handler, this._overridePrefixes, prefix);
+ },
+
+ //
+ // see nsIHttpServer.registerDirectory
+ //
+ registerDirectory: function(path, directory)
+ {
+ // strip off leading and trailing '/' so that we can use lastIndexOf when
+ // determining exactly how a path maps onto a mapped directory --
+ // conditional is required here to deal with "/".substring(1, 0) being
+ // converted to "/".substring(0, 1) per the JS specification
+ var key = path.length == 1 ? "" : path.substring(1, path.length - 1);
+
+ // the path-to-directory mapping code requires that the first character not
+ // be "/", or it will go into an infinite loop
+ if (key.charAt(0) == "/")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ key = toInternalPath(key, false);
+
+ if (directory)
+ {
+ dumpn("*** mapping '" + path + "' to the location " + directory.path);
+ this._pathDirectoryMap.put(key, directory);
+ }
+ else
+ {
+ dumpn("*** removing mapping for '" + path + "'");
+ this._pathDirectoryMap.put(key, null);
+ }
+ },
+
+ //
+ // see nsIHttpServer.registerErrorHandler
+ //
+ registerErrorHandler: function(err, handler)
+ {
+ if (!(err in HTTP_ERROR_CODES))
+ dumpn("*** WARNING: registering non-HTTP/1.1 error code " +
+ "(" + err + ") handler -- was this intentional?");
+
+ this._handlerToField(handler, this._overrideErrors, err);
+ },
+
+ //
+ // see nsIHttpServer.setIndexHandler
+ //
+ setIndexHandler: function(handler)
+ {
+ if (!handler)
+ handler = defaultIndexHandler;
+ else if (typeof(handler) != "function")
+ handler = createHandlerFunc(handler);
+
+ this._indexHandler = handler;
+ },
+
+ //
+ // see nsIHttpServer.registerContentType
+ //
+ registerContentType: function(ext, type)
+ {
+ if (!type)
+ delete this._mimeMappings[ext];
+ else
+ this._mimeMappings[ext] = headerUtils.normalizeFieldValue(type);
+ },
+
+ // PRIVATE API
+
+ /**
+* Sets or remove (if handler is null) a handler in an object with a key.
+*
+* @param handler
+* a handler, either function or an nsIHttpRequestHandler
+* @param dict
+* The object to attach the handler to.
+* @param key
+* The field name of the handler.
+*/
+ _handlerToField: function(handler, dict, key)
+ {
+ // for convenience, handler can be a function if this is run from xpcshell
+ if (typeof(handler) == "function")
+ dict[key] = handler;
+ else if (handler)
+ dict[key] = createHandlerFunc(handler);
+ else
+ delete dict[key];
+ },
+
+ /**
+* Handles a request which maps to a file in the local filesystem (if a base
+* path has already been set; otherwise the 404 error is thrown).
+*
+* @param metadata : Request
+* metadata for the incoming request
+* @param response : Response
+* an uninitialized Response to the given request, to be initialized by a
+* request handler
+* @throws HTTP_###
+* if an HTTP error occurred (usually HTTP_404); note that in this case the
+* calling code must handle post-processing of the response
+*/
+ _handleDefault: function(metadata, response)
+ {
+ dumpn("*** _handleDefault()");
+
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+
+ var path = metadata.path;
+ NS_ASSERT(path.charAt(0) == "/", "invalid path: <" + path + ">");
+
+ // determine the actual on-disk file; this requires finding the deepest
+ // path-to-directory mapping in the requested URL
+ var file = this._getFileForPath(path);
+
+ // the "file" might be a directory, in which case we either serve the
+ // contained index.html or make the index handler write the response
+ if (file.exists() && file.isDirectory())
+ {
+ file.append("index.html"); // make configurable?
+ if (!file.exists() || file.isDirectory())
+ {
+ metadata._ensurePropertyBag();
+ metadata._bag.setPropertyAsInterface("directory", file.parent);
+ this._indexHandler(metadata, response);
+ return;
+ }
+ }
+
+ // alternately, the file might not exist
+ if (!file.exists())
+ throw HTTP_404;
+
+ var start, end;
+ if (metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1) &&
+ metadata.hasHeader("Range") &&
+ this._getTypeFromFile(file) !== SJS_TYPE)
+ {
+ var rangeMatch = metadata.getHeader("Range").match(/^bytes=(\d+)?-(\d+)?$/);
+ if (!rangeMatch)
+ throw HTTP_400;
+
+ if (rangeMatch[1] !== undefined)
+ start = parseInt(rangeMatch[1], 10);
+
+ if (rangeMatch[2] !== undefined)
+ end = parseInt(rangeMatch[2], 10);
+
+ if (start === undefined && end === undefined)
+ throw HTTP_400;
+
+ // No start given, so the end is really the count of bytes from the
+ // end of the file.
+ if (start === undefined)
+ {
+ start = Math.max(0, file.fileSize - end);
+ end = file.fileSize - 1;
+ }
+
+ // start and end are inclusive
+ if (end === undefined || end >= file.fileSize)
+ end = file.fileSize - 1;
+
+ if (start !== undefined && start >= file.fileSize) {
+ var HTTP_416 = new HttpError(416, "Requested Range Not Satisfiable");
+ HTTP_416.customErrorHandling = function(errorResponse)
+ {
+ maybeAddHeaders(file, metadata, errorResponse);
+ };
+ throw HTTP_416;
+ }
+
+ if (end < start)
+ {
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ start = 0;
+ end = file.fileSize - 1;
+ }
+ else
+ {
+ response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
+ var contentRange = "bytes " + start + "-" + end + "/" + file.fileSize;
+ response.setHeader("Content-Range", contentRange);
+ }
+ }
+ else
+ {
+ start = 0;
+ end = file.fileSize - 1;
+ }
+
+ // finally...
+ dumpn("*** handling '" + path + "' as mapping to " + file.path + " from " +
+ start + " to " + end + " inclusive");
+ this._writeFileResponse(metadata, file, response, start, end - start + 1);
+ },
+
+ /**
+* Writes an HTTP response for the given file, including setting headers for
+* file metadata.
+*
+* @param metadata : Request
+* the Request for which a response is being generated
+* @param file : nsILocalFile
+* the file which is to be sent in the response
+* @param response : Response
+* the response to which the file should be written
+* @param offset: uint
+* the byte offset to skip to when writing
+* @param count: uint
+* the number of bytes to write
+*/
+ _writeFileResponse: function(metadata, file, response, offset, count)
+ {
+ const PR_RDONLY = 0x01;
+
+ var type = this._getTypeFromFile(file);
+ if (type === SJS_TYPE)
+ {
+ var fis = new FileInputStream(file, PR_RDONLY, 0o444,
+ Ci.nsIFileInputStream.CLOSE_ON_EOF);
+
+ try
+ {
+ var sis = new ScriptableInputStream(fis);
+ var s = Cu.Sandbox(gGlobalObject);
+ s.importFunction(dump, "dump");
+
+ // Define a basic key-value state-preservation API across requests, with
+ // keys initially corresponding to the empty string.
+ var self = this;
+ var path = metadata.path;
+ s.importFunction(function getState(k)
+ {
+ return self._getState(path, k);
+ });
+ s.importFunction(function setState(k, v)
+ {
+ self._setState(path, k, v);
+ });
+ s.importFunction(function getSharedState(k)
+ {
+ return self._getSharedState(k);
+ });
+ s.importFunction(function setSharedState(k, v)
+ {
+ self._setSharedState(k, v);
+ });
+ s.importFunction(function getObjectState(k, callback)
+ {
+ callback(self._getObjectState(k));
+ });
+ s.importFunction(function setObjectState(k, v)
+ {
+ self._setObjectState(k, v);
+ });
+ s.importFunction(function registerPathHandler(p, h)
+ {
+ self.registerPathHandler(p, h);
+ });
+
+ // Make it possible for sjs files to access their location
+ this._setState(path, "__LOCATION__", file.path);
+
+ try
+ {
+ // Alas, the line number in errors dumped to console when calling the
+ // request handler is simply an offset from where we load the SJS file.
+ // Work around this in a reasonably non-fragile way by dynamically
+ // getting the line number where we evaluate the SJS file. Don't
+ // separate these two lines!
+ var line = new Error().lineNumber;
+ Cu.evalInSandbox(sis.read(file.fileSize), s);
+ }
+ catch (e)
+ {
+ dumpn("*** syntax error in SJS at " + file.path + ": " + e);
+ throw HTTP_500;
+ }
+
+ try
+ {
+ s.handleRequest(metadata, response);
+ }
+ catch (e)
+ {
+ dump("*** error running SJS at " + file.path + ": " +
+ e + " on line " +
+ (e instanceof Error
+ ? e.lineNumber + " in httpd.js"
+ : (e.lineNumber - line)) + "\n");
+ throw HTTP_500;
+ }
+ }
+ finally
+ {
+ fis.close();
+ }
+ }
+ else
+ {
+ try
+ {
+ response.setHeader("Last-Modified",
+ toDateString(file.lastModifiedTime),
+ false);
+ }
+ catch (e) { /* lastModifiedTime threw, ignore */ }
+
+ response.setHeader("Content-Type", type, false);
+ maybeAddHeaders(file, metadata, response);
+ response.setHeader("Content-Length", "" + count, false);
+
+ var fis = new FileInputStream(file, PR_RDONLY, 0o444,
+ Ci.nsIFileInputStream.CLOSE_ON_EOF);
+
+ offset = offset || 0;
+ count = count || file.fileSize;
+ NS_ASSERT(offset === 0 || offset < file.fileSize, "bad offset");
+ NS_ASSERT(count >= 0, "bad count");
+ NS_ASSERT(offset + count <= file.fileSize, "bad total data size");
+
+ try
+ {
+ if (offset !== 0)
+ {
+ // Seek (or read, if seeking isn't supported) to the correct offset so
+ // the data sent to the client matches the requested range.
+ if (fis instanceof Ci.nsISeekableStream)
+ fis.seek(Ci.nsISeekableStream.NS_SEEK_SET, offset);
+ else
+ new ScriptableInputStream(fis).read(offset);
+ }
+ }
+ catch (e)
+ {
+ fis.close();
+ throw e;
+ }
+
+ let writeMore = function writeMore()
+ {
+ gThreadManager.currentThread
+ .dispatch(writeData, Ci.nsIThread.DISPATCH_NORMAL);
+ }
+
+ var input = new BinaryInputStream(fis);
+ var output = new BinaryOutputStream(response.bodyOutputStream);
+ var writeData =
+ {
+ run: function()
+ {
+ var chunkSize = Math.min(65536, count);
+ count -= chunkSize;
+ NS_ASSERT(count >= 0, "underflow");
+
+ try
+ {
+ var data = input.readByteArray(chunkSize);
+ NS_ASSERT(data.length === chunkSize,
+ "incorrect data returned? got " + data.length +
+ ", expected " + chunkSize);
+ output.writeByteArray(data, data.length);
+ if (count === 0)
+ {
+ fis.close();
+ response.finish();
+ }
+ else
+ {
+ writeMore();
+ }
+ }
+ catch (e)
+ {
+ try
+ {
+ fis.close();
+ }
+ finally
+ {
+ response.finish();
+ }
+ throw e;
+ }
+ }
+ };
+
+ writeMore();
+
+ // Now that we know copying will start, flag the response as async.
+ response.processAsync();
+ }
+ },
+
+ /**
+* Get the value corresponding to a given key for the given path for SJS state
+* preservation across requests.
+*
+* @param path : string
+* the path from which the given state is to be retrieved
+* @param k : string
+* the key whose corresponding value is to be returned
+* @returns string
+* the corresponding value, which is initially the empty string
+*/
+ _getState: function(path, k)
+ {
+ var state = this._state;
+ if (path in state && k in state[path])
+ return state[path][k];
+ return "";
+ },
+
+ /**
+* Set the value corresponding to a given key for the given path for SJS state
+* preservation across requests.
+*
+* @param path : string
+* the path from which the given state is to be retrieved
+* @param k : string
+* the key whose corresponding value is to be set
+* @param v : string
+* the value to be set
+*/
+ _setState: function(path, k, v)
+ {
+ if (typeof v !== "string")
+ throw new Error("non-string value passed");
+ var state = this._state;
+ if (!(path in state))
+ state[path] = {};
+ state[path][k] = v;
+ },
+
+ /**
+* Get the value corresponding to a given key for SJS state preservation
+* across requests.
+*
+* @param k : string
+* the key whose corresponding value is to be returned
+* @returns string
+* the corresponding value, which is initially the empty string
+*/
+ _getSharedState: function(k)
+ {
+ var state = this._sharedState;
+ if (k in state)
+ return state[k];
+ return "";
+ },
+
+ /**
+* Set the value corresponding to a given key for SJS state preservation
+* across requests.
+*
+* @param k : string
+* the key whose corresponding value is to be set
+* @param v : string
+* the value to be set
+*/
+ _setSharedState: function(k, v)
+ {
+ if (typeof v !== "string")
+ throw new Error("non-string value passed");
+ this._sharedState[k] = v;
+ },
+
+ /**
+* Returns the object associated with the given key in the server for SJS
+* state preservation across requests.
+*
+* @param k : string
+* the key whose corresponding object is to be returned
+* @returns nsISupports
+* the corresponding object, or null if none was present
+*/
+ _getObjectState: function(k)
+ {
+ if (typeof k !== "string")
+ throw new Error("non-string key passed");
+ return this._objectState[k] || null;
+ },
+
+ /**
+* Sets the object associated with the given key in the server for SJS
+* state preservation across requests.
+*
+* @param k : string
+* the key whose corresponding object is to be set
+* @param v : nsISupports
+* the object to be associated with the given key; may be null
+*/
+ _setObjectState: function(k, v)
+ {
+ if (typeof k !== "string")
+ throw new Error("non-string key passed");
+ if (typeof v !== "object")
+ throw new Error("non-object value passed");
+ if (v && !("QueryInterface" in v))
+ {
+ throw new Error("must pass an nsISupports; use wrappedJSObject to ease " +
+ "pain when using the server from JS");
+ }
+
+ this._objectState[k] = v;
+ },
+
+ /**
+* Gets a content-type for the given file, first by checking for any custom
+* MIME-types registered with this handler for the file's extension, second by
+* asking the global MIME service for a content-type, and finally by failing
+* over to application/octet-stream.
+*
+* @param file : nsIFile
+* the nsIFile for which to get a file type
+* @returns string
+* the best content-type which can be determined for the file
+*/
+ _getTypeFromFile: function(file)
+ {
+ try
+ {
+ var name = file.leafName;
+ var dot = name.lastIndexOf(".");
+ if (dot > 0)
+ {
+ var ext = name.slice(dot + 1);
+ if (ext in this._mimeMappings)
+ return this._mimeMappings[ext];
+ }
+ return Cc["@mozilla.org/uriloader/external-helper-app-service;1"]
+ .getService(Ci.nsIMIMEService)
+ .getTypeFromFile(file);
+ }
+ catch (e)
+ {
+ return "application/octet-stream";
+ }
+ },
+
+ /**
+* Returns the nsILocalFile which corresponds to the path, as determined using
+* all registered path->directory mappings and any paths which are explicitly
+* overridden.
+*
+* @param path : string
+* the server path for which a file should be retrieved, e.g. "/foo/bar"
+* @throws HttpError
+* when the correct action is the corresponding HTTP error (i.e., because no
+* mapping was found for a directory in path, the referenced file doesn't
+* exist, etc.)
+* @returns nsILocalFile
+* the file to be sent as the response to a request for the path
+*/
+ _getFileForPath: function(path)
+ {
+ // decode and add underscores as necessary
+ try
+ {
+ path = toInternalPath(path, true);
+ }
+ catch (e)
+ {
+ throw HTTP_400; // malformed path
+ }
+
+ // next, get the directory which contains this path
+ var pathMap = this._pathDirectoryMap;
+
+ // An example progression of tmp for a path "/foo/bar/baz/" might be:
+ // "foo/bar/baz/", "foo/bar/baz", "foo/bar", "foo", ""
+ var tmp = path.substring(1);
+ while (true)
+ {
+ // do we have a match for current head of the path?
+ var file = pathMap.get(tmp);
+ if (file)
+ {
+ // XXX hack; basically disable showing mapping for /foo/bar/ when the
+ // requested path was /foo/bar, because relative links on the page
+ // will all be incorrect -- we really need the ability to easily
+ // redirect here instead
+ if (tmp == path.substring(1) &&
+ tmp.length != 0 &&
+ tmp.charAt(tmp.length - 1) != "/")
+ file = null;
+ else
+ break;
+ }
+
+ // if we've finished trying all prefixes, exit
+ if (tmp == "")
+ break;
+
+ tmp = tmp.substring(0, tmp.lastIndexOf("/"));
+ }
+
+ // no mapping applies, so 404
+ if (!file)
+ throw HTTP_404;
+
+
+ // last, get the file for the path within the determined directory
+ var parentFolder = file.parent;
+ var dirIsRoot = (parentFolder == null);
+
+ // Strategy here is to append components individually, making sure we
+ // never move above the given directory; this allows paths such as
+ // "<file>/foo/../bar" but prevents paths such as "<file>/../base-sibling";
+ // this component-wise approach also means the code works even on platforms
+ // which don't use "/" as the directory separator, such as Windows
+ var leafPath = path.substring(tmp.length + 1);
+ var comps = leafPath.split("/");
+ for (var i = 0, sz = comps.length; i < sz; i++)
+ {
+ var comp = comps[i];
+
+ if (comp == "..")
+ file = file.parent;
+ else if (comp == "." || comp == "")
+ continue;
+ else
+ file.append(comp);
+
+ if (!dirIsRoot && file.equals(parentFolder))
+ throw HTTP_403;
+ }
+
+ return file;
+ },
+
+ /**
+* Writes the error page for the given HTTP error code over the given
+* connection.
+*
+* @param errorCode : uint
+* the HTTP error code to be used
+* @param connection : Connection
+* the connection on which the error occurred
+*/
+ handleError: function(errorCode, connection)
+ {
+ var response = new Response(connection);
+
+ dumpn("*** error in request: " + errorCode);
+
+ this._handleError(errorCode, new Request(connection.port), response);
+ },
+
+ /**
+* Handles a request which generates the given error code, using the
+* user-defined error handler if one has been set, gracefully falling back to
+* the x00 status code if the code has no handler, and failing to status code
+* 500 if all else fails.
+*
+* @param errorCode : uint
+* the HTTP error which is to be returned
+* @param metadata : Request
+* metadata for the request, which will often be incomplete since this is an
+* error
+* @param response : Response
+* an uninitialized Response should be initialized when this method
+* completes with information which represents the desired error code in the
+* ideal case or a fallback code in abnormal circumstances (i.e., 500 is a
+* fallback for 505, per HTTP specs)
+*/
+ _handleError: function(errorCode, metadata, response)
+ {
+ if (!metadata)
+ throw Cr.NS_ERROR_NULL_POINTER;
+
+ var errorX00 = errorCode - (errorCode % 100);
+
+ try
+ {
+ if (!(errorCode in HTTP_ERROR_CODES))
+ dumpn("*** WARNING: requested invalid error: " + errorCode);
+
+ // RFC 2616 says that we should try to handle an error by its class if we
+ // can't otherwise handle it -- if that fails, we revert to handling it as
+ // a 500 internal server error, and if that fails we throw and shut down
+ // the server
+
+ // actually handle the error
+ try
+ {
+ if (errorCode in this._overrideErrors)
+ this._overrideErrors[errorCode](metadata, response);
+ else
+ this._defaultErrors[errorCode](metadata, response);
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort(e);
+ return;
+ }
+
+ // don't retry the handler that threw
+ if (errorX00 == errorCode)
+ throw HTTP_500;
+
+ dumpn("*** error in handling for error code " + errorCode + ", " +
+ "falling back to " + errorX00 + "...");
+ response = new Response(response._connection);
+ if (errorX00 in this._overrideErrors)
+ this._overrideErrors[errorX00](metadata, response);
+ else if (errorX00 in this._defaultErrors)
+ this._defaultErrors[errorX00](metadata, response);
+ else
+ throw HTTP_500;
+ }
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort();
+ return;
+ }
+
+ // we've tried everything possible for a meaningful error -- now try 500
+ dumpn("*** error in handling for error code " + errorX00 + ", falling " +
+ "back to 500...");
+
+ try
+ {
+ response = new Response(response._connection);
+ if (500 in this._overrideErrors)
+ this._overrideErrors[500](metadata, response);
+ else
+ this._defaultErrors[500](metadata, response);
+ }
+ catch (e2)
+ {
+ dumpn("*** multiple errors in default error handlers!");
+ dumpn("*** e == " + e + ", e2 == " + e2);
+ response.abort(e2);
+ return;
+ }
+ }
+
+ response.complete();
+ },
+
+ // FIELDS
+
+ /**
+* This object contains the default handlers for the various HTTP error codes.
+*/
+ _defaultErrors:
+ {
+ 400: function(metadata, response)
+ {
+ // none of the data in metadata is reliable, so hard-code everything here
+ response.setStatusLine("1.1", 400, "Bad Request");
+ response.setHeader("Content-Type", "text/plain", false);
+
+ var body = "Bad request\n";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 403: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 403, "Forbidden");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>403 Forbidden</title></head>\
+<body>\
+<h1>403 Forbidden</h1>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 404: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 404, "Not Found");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>404 Not Found</title></head>\
+<body>\
+<h1>404 Not Found</h1>\
+<p>\
+<span style='font-family: monospace;'>" +
+ htmlEscape(metadata.path) +
+ "</span> was not found.\
+</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 416: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion,
+ 416,
+ "Requested Range Not Satisfiable");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head>\
+<title>416 Requested Range Not Satisfiable</title></head>\
+<body>\
+<h1>416 Requested Range Not Satisfiable</h1>\
+<p>The byte range was not valid for the\
+requested resource.\
+</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 500: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion,
+ 500,
+ "Internal Server Error");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>500 Internal Server Error</title></head>\
+<body>\
+<h1>500 Internal Server Error</h1>\
+<p>Something's broken in this server and\
+needs to be fixed.</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 501: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 501, "Not Implemented");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>501 Not Implemented</title></head>\
+<body>\
+<h1>501 Not Implemented</h1>\
+<p>This server is not (yet) Apache.</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 505: function(metadata, response)
+ {
+ response.setStatusLine("1.1", 505, "HTTP Version Not Supported");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>505 HTTP Version Not Supported</title></head>\
+<body>\
+<h1>505 HTTP Version Not Supported</h1>\
+<p>This server only supports HTTP/1.0 and HTTP/1.1\
+connections.</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ }
+ },
+
+ /**
+* Contains handlers for the default set of URIs contained in this server.
+*/
+ _defaultPaths:
+ {
+ "/": function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>httpd.js</title></head>\
+<body>\
+<h1>httpd.js</h1>\
+<p>If you're seeing this page, httpd.js is up and\
+serving requests! Now set a base path and serve some\
+files!</p>\
+</body>\
+</html>";
+
+ response.bodyOutputStream.write(body, body.length);
+ },
+
+ "/trace": function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ response.setHeader("Content-Type", "text/plain", false);
+
+ var body = "Request-URI: " +
+ metadata.scheme + "://" + metadata.host + ":" + metadata.port +
+ metadata.path + "\n\n";
+ body += "Request (semantically equivalent, slightly reformatted):\n\n";
+ body += metadata.method + " " + metadata.path;
+
+ if (metadata.queryString)
+ body += "?" + metadata.queryString;
+
+ body += " HTTP/" + metadata.httpVersion + "\r\n";
+
+ var headEnum = metadata.headers;
+ while (headEnum.hasMoreElements())
+ {
+ var fieldName = headEnum.getNext()
+ .QueryInterface(Ci.nsISupportsString)
+ .data;
+ body += fieldName + ": " + metadata.getHeader(fieldName) + "\r\n";
+ }
+
+ response.bodyOutputStream.write(body, body.length);
+ }
+ }
+};
+
+
+/**
+* Maps absolute paths to files on the local file system (as nsILocalFiles).
+*/
+function FileMap()
+{
+ /** Hash which will map paths to nsILocalFiles. */
+ this._map = {};
+}
+FileMap.prototype =
+{
+ // PUBLIC API
+
+ /**
+* Maps key to a clone of the nsILocalFile value if value is non-null;
+* otherwise, removes any extant mapping for key.
+*
+* @param key : string
+* string to which a clone of value is mapped
+* @param value : nsILocalFile
+* the file to map to key, or null to remove a mapping
+*/
+ put: function(key, value)
+ {
+ if (value)
+ this._map[key] = value.clone();
+ else
+ delete this._map[key];
+ },
+
+ /**
+* Returns a clone of the nsILocalFile mapped to key, or null if no such
+* mapping exists.
+*
+* @param key : string
+* key to which the returned file maps
+* @returns nsILocalFile
+* a clone of the mapped file, or null if no mapping exists
+*/
+ get: function(key)
+ {
+ var val = this._map[key];
+ return val ? val.clone() : null;
+ }
+};
+
+
+// Response CONSTANTS
+
+// token = *<any CHAR except CTLs or separators>
+// CHAR = <any US-ASCII character (0-127)>
+// CTL = <any US-ASCII control character (0-31) and DEL (127)>
+// separators = "(" | ")" | "<" | ">" | "@"
+// | "," | ";" | ":" | "\" | <">
+// | "/" | "[" | "]" | "?" | "="
+// | "{" | "}" | SP | HT
+const IS_TOKEN_ARRAY =
+ [0, 0, 0, 0, 0, 0, 0, 0, // 0
+ 0, 0, 0, 0, 0, 0, 0, 0, // 8
+ 0, 0, 0, 0, 0, 0, 0, 0, // 16
+ 0, 0, 0, 0, 0, 0, 0, 0, // 24
+
+ 0, 1, 0, 1, 1, 1, 1, 1, // 32
+ 0, 0, 1, 1, 0, 1, 1, 0, // 40
+ 1, 1, 1, 1, 1, 1, 1, 1, // 48
+ 1, 1, 0, 0, 0, 0, 0, 0, // 56
+
+ 0, 1, 1, 1, 1, 1, 1, 1, // 64
+ 1, 1, 1, 1, 1, 1, 1, 1, // 72
+ 1, 1, 1, 1, 1, 1, 1, 1, // 80
+ 1, 1, 1, 0, 0, 0, 1, 1, // 88
+
+ 1, 1, 1, 1, 1, 1, 1, 1, // 96
+ 1, 1, 1, 1, 1, 1, 1, 1, // 104
+ 1, 1, 1, 1, 1, 1, 1, 1, // 112
+ 1, 1, 1, 0, 1, 0, 1]; // 120
+
+
+/**
+* Determines whether the given character code is a CTL.
+*
+* @param code : uint
+* the character code
+* @returns boolean
+* true if code is a CTL, false otherwise
+*/
+function isCTL(code)
+{
+ return (code >= 0 && code <= 31) || (code == 127);
+}
+
+/**
+* Represents a response to an HTTP request, encapsulating all details of that
+* response. This includes all headers, the HTTP version, status code and
+* explanation, and the entity itself.
+*
+* @param connection : Connection
+* the connection over which this response is to be written
+*/
+function Response(connection)
+{
+ /** The connection over which this response will be written. */
+ this._connection = connection;
+
+ /**
+* The HTTP version of this response; defaults to 1.1 if not set by the
+* handler.
+*/
+ this._httpVersion = nsHttpVersion.HTTP_1_1;
+
+ /**
+* The HTTP code of this response; defaults to 200.
+*/
+ this._httpCode = 200;
+
+ /**
+* The description of the HTTP code in this response; defaults to "OK".
+*/
+ this._httpDescription = "OK";
+
+ /**
+* An nsIHttpHeaders object in which the headers in this response should be
+* stored. This property is null after the status line and headers have been
+* written to the network, and it may be modified up until it is cleared,
+* except if this._finished is set first (in which case headers are written
+* asynchronously in response to a finish() call not preceded by
+* flushHeaders()).
+*/
+ this._headers = new nsHttpHeaders();
+
+ /**
+* Set to true when this response is ended (completely constructed if possible
+* and the connection closed); further actions on this will then fail.
+*/
+ this._ended = false;
+
+ /**
+* A stream used to hold data written to the body of this response.
+*/
+ this._bodyOutputStream = null;
+
+ /**
+* A stream containing all data that has been written to the body of this
+* response so far. (Async handlers make the data contained in this
+* unreliable as a way of determining content length in general, but auxiliary
+* saved information can sometimes be used to guarantee reliability.)
+*/
+ this._bodyInputStream = null;
+
+ /**
+* A stream copier which copies data to the network. It is initially null
+* until replaced with a copier for response headers; when headers have been
+* fully sent it is replaced with a copier for the response body, remaining
+* so for the duration of response processing.
+*/
+ this._asyncCopier = null;
+
+ /**
+* True if this response has been designated as being processed
+* asynchronously rather than for the duration of a single call to
+* nsIHttpRequestHandler.handle.
+*/
+ this._processAsync = false;
+
+ /**
+* True iff finish() has been called on this, signaling that no more changes
+* to this may be made.
+*/
+ this._finished = false;
+
+ /**
+* True iff powerSeized() has been called on this, signaling that this
+* response is to be handled manually by the response handler (which may then
+* send arbitrary data in response, even non-HTTP responses).
+*/
+ this._powerSeized = false;
+}
+Response.prototype =
+{
+ // PUBLIC CONSTRUCTION API
+
+ //
+ // see nsIHttpResponse.bodyOutputStream
+ //
+ get bodyOutputStream()
+ {
+ if (this._finished)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+
+ if (!this._bodyOutputStream)
+ {
+ var pipe = new Pipe(true, false, Response.SEGMENT_SIZE, PR_UINT32_MAX,
+ null);
+ this._bodyOutputStream = pipe.outputStream;
+ this._bodyInputStream = pipe.inputStream;
+ if (this._processAsync || this._powerSeized)
+ this._startAsyncProcessor();
+ }
+
+ return this._bodyOutputStream;
+ },
+
+ //
+ // see nsIHttpResponse.write
+ //
+ write: function(data)
+ {
+ if (this._finished)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+
+ var dataAsString = String(data);
+ this.bodyOutputStream.write(dataAsString, dataAsString.length);
+ },
+
+ //
+ // see nsIHttpResponse.setStatusLine
+ //
+ setStatusLine: function(httpVersion, code, description)
+ {
+ if (!this._headers || this._finished || this._powerSeized)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ this._ensureAlive();
+
+ if (!(code >= 0 && code < 1000))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ try
+ {
+ var httpVer;
+ // avoid version construction for the most common cases
+ if (!httpVersion || httpVersion == "1.1")
+ httpVer = nsHttpVersion.HTTP_1_1;
+ else if (httpVersion == "1.0")
+ httpVer = nsHttpVersion.HTTP_1_0;
+ else
+ httpVer = new nsHttpVersion(httpVersion);
+ }
+ catch (e)
+ {
+ throw Cr.NS_ERROR_INVALID_ARG;
+ }
+
+ // Reason-Phrase = *<TEXT, excluding CR, LF>
+ // TEXT = <any OCTET except CTLs, but including LWS>
+ //
+ // XXX this ends up disallowing octets which aren't Unicode, I think -- not
+ // much to do if description is IDL'd as string
+ if (!description)
+ description = "";
+ for (var i = 0; i < description.length; i++)
+ if (isCTL(description.charCodeAt(i)) && description.charAt(i) != "\t")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ // set the values only after validation to preserve atomicity
+ this._httpDescription = description;
+ this._httpCode = code;
+ this._httpVersion = httpVer;
+ },
+
+ //
+ // see nsIHttpResponse.setHeader
+ //
+ setHeader: function(name, value, merge)
+ {
+ if (!this._headers || this._finished || this._powerSeized)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ this._ensureAlive();
+
+ this._headers.setHeader(name, value, merge);
+ },
+
+ //
+ // see nsIHttpResponse.processAsync
+ //
+ processAsync: function()
+ {
+ if (this._finished)
+ throw Cr.NS_ERROR_UNEXPECTED;
+ if (this._powerSeized)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ if (this._processAsync)
+ return;
+ this._ensureAlive();
+
+ dumpn("*** processing connection " + this._connection.number + " async");
+ this._processAsync = true;
+
+ /*
+* Either the bodyOutputStream getter or this method is responsible for
+* starting the asynchronous processor and catching writes of data to the
+* response body of async responses as they happen, for the purpose of
+* forwarding those writes to the actual connection's output stream.
+* If bodyOutputStream is accessed first, calling this method will create
+* the processor (when it first is clear that body data is to be written
+* immediately, not buffered). If this method is called first, accessing
+* bodyOutputStream will create the processor. If only this method is
+* called, we'll write nothing, neither headers nor the nonexistent body,
+* until finish() is called. Since that delay is easily avoided by simply
+* getting bodyOutputStream or calling write(""), we don't worry about it.
+*/
+ if (this._bodyOutputStream && !this._asyncCopier)
+ this._startAsyncProcessor();
+ },
+
+ //
+ // see nsIHttpResponse.seizePower
+ //
+ seizePower: function()
+ {
+ if (this._processAsync)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ if (this._finished)
+ throw Cr.NS_ERROR_UNEXPECTED;
+ if (this._powerSeized)
+ return;
+ this._ensureAlive();
+
+ dumpn("*** forcefully seizing power over connection " +
+ this._connection.number + "...");
+
+ // Purge any already-written data without sending it. We could as easily
+ // swap out the streams entirely, but that makes it possible to acquire and
+ // unknowingly use a stale reference, so we require there only be one of
+ // each stream ever for any response to avoid this complication.
+ if (this._asyncCopier)
+ this._asyncCopier.cancel(Cr.NS_BINDING_ABORTED);
+ this._asyncCopier = null;
+ if (this._bodyOutputStream)
+ {
+ var input = new BinaryInputStream(this._bodyInputStream);
+ var avail;
+ while ((avail = input.available()) > 0)
+ input.readByteArray(avail);
+ }
+
+ this._powerSeized = true;
+ if (this._bodyOutputStream)
+ this._startAsyncProcessor();
+ },
+
+ //
+ // see nsIHttpResponse.finish
+ //
+ finish: function()
+ {
+ if (!this._processAsync && !this._powerSeized)
+ throw Cr.NS_ERROR_UNEXPECTED;
+ if (this._finished)
+ return;
+
+ dumpn("*** finishing connection " + this._connection.number);
+ this._startAsyncProcessor(); // in case bodyOutputStream was never accessed
+ if (this._bodyOutputStream)
+ this._bodyOutputStream.close();
+ this._finished = true;
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIHttpResponse) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // POST-CONSTRUCTION API (not exposed externally)
+
+ /**
+* The HTTP version number of this, as a string (e.g. "1.1").
+*/
+ get httpVersion()
+ {
+ this._ensureAlive();
+ return this._httpVersion.toString();
+ },
+
+ /**
+* The HTTP status code of this response, as a string of three characters per
+* RFC 2616.
+*/
+ get httpCode()
+ {
+ this._ensureAlive();
+
+ var codeString = (this._httpCode < 10 ? "0" : "") +
+ (this._httpCode < 100 ? "0" : "") +
+ this._httpCode;
+ return codeString;
+ },
+
+ /**
+* The description of the HTTP status code of this response, or "" if none is
+* set.
+*/
+ get httpDescription()
+ {
+ this._ensureAlive();
+
+ return this._httpDescription;
+ },
+
+ /**
+* The headers in this response, as an nsHttpHeaders object.
+*/
+ get headers()
+ {
+ this._ensureAlive();
+
+ return this._headers;
+ },
+
+ //
+ // see nsHttpHeaders.getHeader
+ //
+ getHeader: function(name)
+ {
+ this._ensureAlive();
+
+ return this._headers.getHeader(name);
+ },
+
+ /**
+* Determines whether this response may be abandoned in favor of a newly
+* constructed response. A response may be abandoned only if it is not being
+* sent asynchronously and if raw control over it has not been taken from the
+* server.
+*
+* @returns boolean
+* true iff no data has been written to the network
+*/
+ partiallySent: function()
+ {
+ dumpn("*** partiallySent()");
+ return this._processAsync || this._powerSeized;
+ },
+
+ /**
+* If necessary, kicks off the remaining request processing needed to be done
+* after a request handler performs its initial work upon this response.
+*/
+ complete: function()
+ {
+ dumpn("*** complete()");
+ if (this._processAsync || this._powerSeized)
+ {
+ NS_ASSERT(this._processAsync ^ this._powerSeized,
+ "can't both send async and relinquish power");
+ return;
+ }
+
+ NS_ASSERT(!this.partiallySent(), "completing a partially-sent response?");
+
+ this._startAsyncProcessor();
+
+ // Now make sure we finish processing this request!
+ if (this._bodyOutputStream)
+ this._bodyOutputStream.close();
+ },
+
+ /**
+* Abruptly ends processing of this response, usually due to an error in an
+* incoming request but potentially due to a bad error handler. Since we
+* cannot handle the error in the usual way (giving an HTTP error page in
+* response) because data may already have been sent (or because the response
+* might be expected to have been generated asynchronously or completely from
+* scratch by the handler), we stop processing this response and abruptly
+* close the connection.
+*
+* @param e : Error
+* the exception which precipitated this abort, or null if no such exception
+* was generated
+*/
+ abort: function(e)
+ {
+ dumpn("*** abort(<" + e + ">)");
+
+ // This response will be ended by the processor if one was created.
+ var copier = this._asyncCopier;
+ if (copier)
+ {
+ // We dispatch asynchronously here so that any pending writes of data to
+ // the connection will be deterministically written. This makes it easier
+ // to specify exact behavior, and it makes observable behavior more
+ // predictable for clients. Note that the correctness of this depends on
+ // callbacks in response to _waitToReadData in WriteThroughCopier
+ // happening asynchronously with respect to the actual writing of data to
+ // bodyOutputStream, as they currently do; if they happened synchronously,
+ // an event which ran before this one could write more data to the
+ // response body before we get around to canceling the copier. We have
+ // tests for this in test_seizepower.js, however, and I can't think of a
+ // way to handle both cases without removing bodyOutputStream access and
+ // moving its effective write(data, length) method onto Response, which
+ // would be slower and require more code than this anyway.
+ gThreadManager.currentThread.dispatch({
+ run: function()
+ {
+ dumpn("*** canceling copy asynchronously...");
+ copier.cancel(Cr.NS_ERROR_UNEXPECTED);
+ }
+ }, Ci.nsIThread.DISPATCH_NORMAL);
+ }
+ else
+ {
+ this.end();
+ }
+ },
+
+ /**
+* Closes this response's network connection, marks the response as finished,
+* and notifies the server handler that the request is done being processed.
+*/
+ end: function()
+ {
+ NS_ASSERT(!this._ended, "ending this response twice?!?!");
+
+ this._connection.close();
+ if (this._bodyOutputStream)
+ this._bodyOutputStream.close();
+
+ this._finished = true;
+ this._ended = true;
+ },
+
+ // PRIVATE IMPLEMENTATION
+
+ /**
+* Sends the status line and headers of this response if they haven't been
+* sent and initiates the process of copying data written to this response's
+* body to the network.
+*/
+ _startAsyncProcessor: function()
+ {
+ dumpn("*** _startAsyncProcessor()");
+
+ // Handle cases where we're being called a second time. The former case
+ // happens when this is triggered both by complete() and by processAsync(),
+ // while the latter happens when processAsync() in conjunction with sent
+ // data causes abort() to be called.
+ if (this._asyncCopier || this._ended)
+ {
+ dumpn("*** ignoring second call to _startAsyncProcessor");
+ return;
+ }
+
+ // Send headers if they haven't been sent already and should be sent, then
+ // asynchronously continue to send the body.
+ if (this._headers && !this._powerSeized)
+ {
+ this._sendHeaders();
+ return;
+ }
+
+ this._headers = null;
+ this._sendBody();
+ },
+
+ /**
+* Signals that all modifications to the response status line and headers are
+* complete and then sends that data over the network to the client. Once
+* this method completes, a different response to the request that resulted
+* in this response cannot be sent -- the only possible action in case of
+* error is to abort the response and close the connection.
+*/
+ _sendHeaders: function()
+ {
+ dumpn("*** _sendHeaders()");
+
+ NS_ASSERT(this._headers);
+ NS_ASSERT(!this._powerSeized);
+
+ // request-line
+ var statusLine = "HTTP/" + this.httpVersion + " " +
+ this.httpCode + " " +
+ this.httpDescription + "\r\n";
+
+ // header post-processing
+
+ var headers = this._headers;
+ headers.setHeader("Connection", "close", false);
+ headers.setHeader("Server", "httpd.js", false);
+ if (!headers.hasHeader("Date"))
+ headers.setHeader("Date", toDateString(Date.now()), false);
+
+ // Any response not being processed asynchronously must have an associated
+ // Content-Length header for reasons of backwards compatibility with the
+ // initial server, which fully buffered every response before sending it.
+ // Beyond that, however, it's good to do this anyway because otherwise it's
+ // impossible to test behaviors that depend on the presence or absence of a
+ // Content-Length header.
+ if (!this._processAsync)
+ {
+ dumpn("*** non-async response, set Content-Length");
+
+ var bodyStream = this._bodyInputStream;
+ var avail = bodyStream ? bodyStream.available() : 0;
+
+ // XXX assumes stream will always report the full amount of data available
+ headers.setHeader("Content-Length", "" + avail, false);
+ }
+
+
+ // construct and send response
+ dumpn("*** header post-processing completed, sending response head...");
+
+ // request-line
+ var preambleData = [statusLine];
+
+ // headers
+ var headEnum = headers.enumerator;
+ while (headEnum.hasMoreElements())
+ {
+ var fieldName = headEnum.getNext()
+ .QueryInterface(Ci.nsISupportsString)
+ .data;
+ var values = headers.getHeaderValues(fieldName);
+ for (var i = 0, sz = values.length; i < sz; i++)
+ preambleData.push(fieldName + ": " + values[i] + "\r\n");
+ }
+
+ // end request-line/headers
+ preambleData.push("\r\n");
+
+ var preamble = preambleData.join("");
+
+ var responseHeadPipe = new Pipe(true, false, 0, PR_UINT32_MAX, null);
+ responseHeadPipe.outputStream.write(preamble, preamble.length);
+
+ var response = this;
+ var copyObserver =
+ {
+ onStartRequest: function(request, cx)
+ {
+ dumpn("*** preamble copying started");
+ },
+
+ onStopRequest: function(request, cx, statusCode)
+ {
+ dumpn("*** preamble copying complete " +
+ "[status=0x" + statusCode.toString(16) + "]");
+
+ if (!components.isSuccessCode(statusCode))
+ {
+ dumpn("!!! header copying problems: non-success statusCode, " +
+ "ending response");
+
+ response.end();
+ }
+ else
+ {
+ response._sendBody();
+ }
+ },
+
+ QueryInterface: function(aIID)
+ {
+ if (aIID.equals(Ci.nsIRequestObserver) || aIID.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ }
+ };
+
+ var headerCopier = this._asyncCopier =
+ new WriteThroughCopier(responseHeadPipe.inputStream,
+ this._connection.output,
+ copyObserver, null);
+
+ responseHeadPipe.outputStream.close();
+
+ // Forbid setting any more headers or modifying the request line.
+ this._headers = null;
+ },
+
+ /**
+* Asynchronously writes the body of the response (or the entire response, if
+* seizePower() has been called) to the network.
+*/
+ _sendBody: function()
+ {
+ dumpn("*** _sendBody");
+
+ NS_ASSERT(!this._headers, "still have headers around but sending body?");
+
+ // If no body data was written, we're done
+ if (!this._bodyInputStream)
+ {
+ dumpn("*** empty body, response finished");
+ this.end();
+ return;
+ }
+
+ var response = this;
+ var copyObserver =
+ {
+ onStartRequest: function(request, context)
+ {
+ dumpn("*** onStartRequest");
+ },
+
+ onStopRequest: function(request, cx, statusCode)
+ {
+ dumpn("*** onStopRequest [status=0x" + statusCode.toString(16) + "]");
+
+ if (statusCode === Cr.NS_BINDING_ABORTED)
+ {
+ dumpn("*** terminating copy observer without ending the response");
+ }
+ else
+ {
+ if (!components.isSuccessCode(statusCode))
+ dumpn("*** WARNING: non-success statusCode in onStopRequest");
+
+ response.end();
+ }
+ },
+
+ QueryInterface: function(aIID)
+ {
+ if (aIID.equals(Ci.nsIRequestObserver) || aIID.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ }
+ };
+
+ dumpn("*** starting async copier of body data...");
+ this._asyncCopier =
+ new WriteThroughCopier(this._bodyInputStream, this._connection.output,
+ copyObserver, null);
+ },
+
+ /** Ensures that this hasn't been ended. */
+ _ensureAlive: function()
+ {
+ NS_ASSERT(!this._ended, "not handling response lifetime correctly");
+ }
+};
+
+/**
+* Size of the segments in the buffer used in storing response data and writing
+* it to the socket.
+*/
+Response.SEGMENT_SIZE = 8192;
+
+/** Serves double duty in WriteThroughCopier implementation. */
+function notImplemented()
+{
+ throw Cr.NS_ERROR_NOT_IMPLEMENTED;
+}
+
+/** Returns true iff the given exception represents stream closure. */
+function streamClosed(e)
+{
+ return e === Cr.NS_BASE_STREAM_CLOSED ||
+ (typeof e === "object" && e.result === Cr.NS_BASE_STREAM_CLOSED);
+}
+
+/** Returns true iff the given exception represents a blocked stream. */
+function wouldBlock(e)
+{
+ return e === Cr.NS_BASE_STREAM_WOULD_BLOCK ||
+ (typeof e === "object" && e.result === Cr.NS_BASE_STREAM_WOULD_BLOCK);
+}
+
+/**
+* Copies data from source to sink as it becomes available, when that data can
+* be written to sink without blocking.
+*
+* @param source : nsIAsyncInputStream
+* the stream from which data is to be read
+* @param sink : nsIAsyncOutputStream
+* the stream to which data is to be copied
+* @param observer : nsIRequestObserver
+* an observer which will be notified when the copy starts and finishes
+* @param context : nsISupports
+* context passed to observer when notified of start/stop
+* @throws NS_ERROR_NULL_POINTER
+* if source, sink, or observer are null
+*/
+function WriteThroughCopier(source, sink, observer, context)
+{
+ if (!source || !sink || !observer)
+ throw Cr.NS_ERROR_NULL_POINTER;
+
+ /** Stream from which data is being read. */
+ this._source = source;
+
+ /** Stream to which data is being written. */
+ this._sink = sink;
+
+ /** Observer watching this copy. */
+ this._observer = observer;
+
+ /** Context for the observer watching this. */
+ this._context = context;
+
+ /**
+* True iff this is currently being canceled (cancel has been called, the
+* callback may not yet have been made).
+*/
+ this._canceled = false;
+
+ /**
+* False until all data has been read from input and written to output, at
+* which point this copy is completed and cancel() is asynchronously called.
+*/
+ this._completed = false;
+
+ /** Required by nsIRequest, meaningless. */
+ this.loadFlags = 0;
+ /** Required by nsIRequest, meaningless. */
+ this.loadGroup = null;
+ /** Required by nsIRequest, meaningless. */
+ this.name = "response-body-copy";
+
+ /** Status of this request. */
+ this.status = Cr.NS_OK;
+
+ /** Arrays of byte strings waiting to be written to output. */
+ this._pendingData = [];
+
+ // start copying
+ try
+ {
+ observer.onStartRequest(this, context);
+ this._waitToReadData();
+ this._waitForSinkClosure();
+ }
+ catch (e)
+ {
+ dumpn("!!! error starting copy: " + e +
+ ("lineNumber" in e ? ", line " + e.lineNumber : ""));
+ dumpn(e.stack);
+ this.cancel(Cr.NS_ERROR_UNEXPECTED);
+ }
+}
+WriteThroughCopier.prototype =
+{
+ /* nsISupports implementation */
+
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIInputStreamCallback) ||
+ iid.equals(Ci.nsIOutputStreamCallback) ||
+ iid.equals(Ci.nsIRequest) ||
+ iid.equals(Ci.nsISupports))
+ {
+ return this;
+ }
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // NSIINPUTSTREAMCALLBACK
+
+ /**
+* Receives a more-data-in-input notification and writes the corresponding
+* data to the output.
+*
+* @param input : nsIAsyncInputStream
+* the input stream on whose data we have been waiting
+*/
+ onInputStreamReady: function(input)
+ {
+ if (this._source === null)
+ return;
+
+ dumpn("*** onInputStreamReady");
+
+ //
+ // Ordinarily we'll read a non-zero amount of data from input, queue it up
+ // to be written and then wait for further callbacks. The complications in
+ // this method are the cases where we deviate from that behavior when errors
+ // occur or when copying is drawing to a finish.
+ //
+ // The edge cases when reading data are:
+ //
+ // Zero data is read
+ // If zero data was read, we're at the end of available data, so we can
+ // should stop reading and move on to writing out what we have (or, if
+ // we've already done that, onto notifying of completion).
+ // A stream-closed exception is thrown
+ // This is effectively a less kind version of zero data being read; the
+ // only difference is that we notify of completion with that result
+ // rather than with NS_OK.
+ // Some other exception is thrown
+ // This is the least kind result. We don't know what happened, so we
+ // act as though the stream closed except that we notify of completion
+ // with the result NS_ERROR_UNEXPECTED.
+ //
+
+ var bytesWanted = 0, bytesConsumed = -1;
+ try
+ {
+ input = new BinaryInputStream(input);
+
+ bytesWanted = Math.min(input.available(), Response.SEGMENT_SIZE);
+ dumpn("*** input wanted: " + bytesWanted);
+
+ if (bytesWanted > 0)
+ {
+ var data = input.readByteArray(bytesWanted);
+ bytesConsumed = data.length;
+ this._pendingData.push(String.fromCharCode.apply(String, data));
+ }
+
+ dumpn("*** " + bytesConsumed + " bytes read");
+
+ // Handle the zero-data edge case in the same place as all other edge
+ // cases are handled.
+ if (bytesWanted === 0)
+ throw Cr.NS_BASE_STREAM_CLOSED;
+ }
+ catch (e)
+ {
+ if (streamClosed(e))
+ {
+ dumpn("*** input stream closed");
+ e = bytesWanted === 0 ? Cr.NS_OK : Cr.NS_ERROR_UNEXPECTED;
+ }
+ else
+ {
+ dumpn("!!! unexpected error reading from input, canceling: " + e);
+ e = Cr.NS_ERROR_UNEXPECTED;
+ }
+
+ this._doneReadingSource(e);
+ return;
+ }
+
+ var pendingData = this._pendingData;
+
+ NS_ASSERT(bytesConsumed > 0);
+ NS_ASSERT(pendingData.length > 0, "no pending data somehow?");
+ NS_ASSERT(pendingData[pendingData.length - 1].length > 0,
+ "buffered zero bytes of data?");
+
+ NS_ASSERT(this._source !== null);
+
+ // Reading has gone great, and we've gotten data to write now. What if we
+ // don't have a place to write that data, because output went away just
+ // before this read? Drop everything on the floor, including new data, and
+ // cancel at this point.
+ if (this._sink === null)
+ {
+ pendingData.length = 0;
+ this._doneReadingSource(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // Okay, we've read the data, and we know we have a place to write it. We
+ // need to queue up the data to be written, but *only* if none is queued
+ // already -- if data's already queued, the code that actually writes the
+ // data will make sure to wait on unconsumed pending data.
+ try
+ {
+ if (pendingData.length === 1)
+ this._waitToWriteData();
+ }
+ catch (e)
+ {
+ dumpn("!!! error waiting to write data just read, swallowing and " +
+ "writing only what we already have: " + e);
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // Whee! We successfully read some data, and it's successfully queued up to
+ // be written. All that remains now is to wait for more data to read.
+ try
+ {
+ this._waitToReadData();
+ }
+ catch (e)
+ {
+ dumpn("!!! error waiting to read more data: " + e);
+ this._doneReadingSource(Cr.NS_ERROR_UNEXPECTED);
+ }
+ },
+
+
+ // NSIOUTPUTSTREAMCALLBACK
+
+ /**
+* Callback when data may be written to the output stream without blocking, or
+* when the output stream has been closed.
+*
+* @param output : nsIAsyncOutputStream
+* the output stream on whose writability we've been waiting, also known as
+* this._sink
+*/
+ onOutputStreamReady: function(output)
+ {
+ if (this._sink === null)
+ return;
+
+ dumpn("*** onOutputStreamReady");
+
+ var pendingData = this._pendingData;
+ if (pendingData.length === 0)
+ {
+ // There's no pending data to write. The only way this can happen is if
+ // we're waiting on the output stream's closure, so we can respond to a
+ // copying failure as quickly as possible (rather than waiting for data to
+ // be available to read and then fail to be copied). Therefore, we must
+ // be done now -- don't bother to attempt to write anything and wrap
+ // things up.
+ dumpn("!!! output stream closed prematurely, ending copy");
+
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+
+ NS_ASSERT(pendingData[0].length > 0, "queued up an empty quantum?");
+
+ //
+ // Write out the first pending quantum of data. The possible errors here
+ // are:
+ //
+ // The write might fail because we can't write that much data
+ // Okay, we've written what we can now, so re-queue what's left and
+ // finish writing it out later.
+ // The write failed because the stream was closed
+ // Discard pending data that we can no longer write, stop reading, and
+ // signal that copying finished.
+ // Some other error occurred.
+ // Same as if the stream were closed, but notify with the status
+ // NS_ERROR_UNEXPECTED so the observer knows something was wonky.
+ //
+
+ try
+ {
+ var quantum = pendingData[0];
+
+ // XXX |quantum| isn't guaranteed to be ASCII, so we're relying on
+ // undefined behavior! We're only using this because writeByteArray
+ // is unusably broken for asynchronous output streams; see bug 532834
+ // for details.
+ var bytesWritten = output.write(quantum, quantum.length);
+ if (bytesWritten === quantum.length)
+ pendingData.shift();
+ else
+ pendingData[0] = quantum.substring(bytesWritten);
+
+ dumpn("*** wrote " + bytesWritten + " bytes of data");
+ }
+ catch (e)
+ {
+ if (wouldBlock(e))
+ {
+ NS_ASSERT(pendingData.length > 0,
+ "stream-blocking exception with no data to write?");
+ NS_ASSERT(pendingData[0].length > 0,
+ "stream-blocking exception with empty quantum?");
+ this._waitToWriteData();
+ return;
+ }
+
+ if (streamClosed(e))
+ dumpn("!!! output stream prematurely closed, signaling error...");
+ else
+ dumpn("!!! unknown error: " + e + ", quantum=" + quantum);
+
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // The day is ours! Quantum written, now let's see if we have more data
+ // still to write.
+ try
+ {
+ if (pendingData.length > 0)
+ {
+ this._waitToWriteData();
+ return;
+ }
+ }
+ catch (e)
+ {
+ dumpn("!!! unexpected error waiting to write pending data: " + e);
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // Okay, we have no more pending data to write -- but might we get more in
+ // the future?
+ if (this._source !== null)
+ {
+ /*
+* If we might, then wait for the output stream to be closed. (We wait
+* only for closure because we have no data to write -- and if we waited
+* for a specific amount of data, we would get repeatedly notified for no
+* reason if over time the output stream permitted more and more data to
+* be written to it without blocking.)
+*/
+ this._waitForSinkClosure();
+ }
+ else
+ {
+ /*
+* On the other hand, if we can't have more data because the input
+* stream's gone away, then it's time to notify of copy completion.
+* Victory!
+*/
+ this._sink = null;
+ this._cancelOrDispatchCancelCallback(Cr.NS_OK);
+ }
+ },
+
+
+ // NSIREQUEST
+
+ /** Returns true if the cancel observer hasn't been notified yet. */
+ isPending: function()
+ {
+ return !this._completed;
+ },
+
+ /** Not implemented, don't use! */
+ suspend: notImplemented,
+ /** Not implemented, don't use! */
+ resume: notImplemented,
+
+ /**
+* Cancels data reading from input, asynchronously writes out any pending
+* data, and causes the observer to be notified with the given error code when
+* all writing has finished.
+*
+* @param status : nsresult
+* the status to pass to the observer when data copying has been canceled
+*/
+ cancel: function(status)
+ {
+ dumpn("*** cancel(" + status.toString(16) + ")");
+
+ if (this._canceled)
+ {
+ dumpn("*** suppressing a late cancel");
+ return;
+ }
+
+ this._canceled = true;
+ this.status = status;
+
+ // We could be in the middle of absolutely anything at this point. Both
+ // input and output might still be around, we might have pending data to
+ // write, and in general we know nothing about the state of the world. We
+ // therefore must assume everything's in progress and take everything to its
+ // final steady state (or so far as it can go before we need to finish
+ // writing out remaining data).
+
+ this._doneReadingSource(status);
+ },
+
+
+ // PRIVATE IMPLEMENTATION
+
+ /**
+* Stop reading input if we haven't already done so, passing e as the status
+* when closing the stream, and kick off a copy-completion notice if no more
+* data remains to be written.
+*
+* @param e : nsresult
+* the status to be used when closing the input stream
+*/
+ _doneReadingSource: function(e)
+ {
+ dumpn("*** _doneReadingSource(0x" + e.toString(16) + ")");
+
+ this._finishSource(e);
+ if (this._pendingData.length === 0)
+ this._sink = null;
+ else
+ NS_ASSERT(this._sink !== null, "null output?");
+
+ // If we've written out all data read up to this point, then it's time to
+ // signal completion.
+ if (this._sink === null)
+ {
+ NS_ASSERT(this._pendingData.length === 0, "pending data still?");
+ this._cancelOrDispatchCancelCallback(e);
+ }
+ },
+
+ /**
+* Stop writing output if we haven't already done so, discard any data that
+* remained to be sent, close off input if it wasn't already closed, and kick
+* off a copy-completion notice.
+*
+* @param e : nsresult
+* the status to be used when closing input if it wasn't already closed
+*/
+ _doneWritingToSink: function(e)
+ {
+ dumpn("*** _doneWritingToSink(0x" + e.toString(16) + ")");
+
+ this._pendingData.length = 0;
+ this._sink = null;
+ this._doneReadingSource(e);
+ },
+
+ /**
+* Completes processing of this copy: either by canceling the copy if it
+* hasn't already been canceled using the provided status, or by dispatching
+* the cancel callback event (with the originally provided status, of course)
+* if it already has been canceled.
+*
+* @param status : nsresult
+* the status code to use to cancel this, if this hasn't already been
+* canceled
+*/
+ _cancelOrDispatchCancelCallback: function(status)
+ {
+ dumpn("*** _cancelOrDispatchCancelCallback(" + status + ")");
+
+ NS_ASSERT(this._source === null, "should have finished input");
+ NS_ASSERT(this._sink === null, "should have finished output");
+ NS_ASSERT(this._pendingData.length === 0, "should have no pending data");
+
+ if (!this._canceled)
+ {
+ this.cancel(status);
+ return;
+ }
+
+ var self = this;
+ var event =
+ {
+ run: function()
+ {
+ dumpn("*** onStopRequest async callback");
+
+ self._completed = true;
+ try
+ {
+ self._observer.onStopRequest(self, self._context, self.status);
+ }
+ catch (e)
+ {
+ NS_ASSERT(false,
+ "how are we throwing an exception here? we control " +
+ "all the callers! " + e);
+ }
+ }
+ };
+
+ gThreadManager.currentThread.dispatch(event, Ci.nsIThread.DISPATCH_NORMAL);
+ },
+
+ /**
+* Kicks off another wait for more data to be available from the input stream.
+*/
+ _waitToReadData: function()
+ {
+ dumpn("*** _waitToReadData");
+ this._source.asyncWait(this, 0, Response.SEGMENT_SIZE,
+ gThreadManager.mainThread);
+ },
+
+ /**
+* Kicks off another wait until data can be written to the output stream.
+*/
+ _waitToWriteData: function()
+ {
+ dumpn("*** _waitToWriteData");
+
+ var pendingData = this._pendingData;
+ NS_ASSERT(pendingData.length > 0, "no pending data to write?");
+ NS_ASSERT(pendingData[0].length > 0, "buffered an empty write?");
+
+ this._sink.asyncWait(this, 0, pendingData[0].length,
+ gThreadManager.mainThread);
+ },
+
+ /**
+* Kicks off a wait for the sink to which data is being copied to be closed.
+* We wait for stream closure when we don't have any data to be copied, rather
+* than waiting to write a specific amount of data. We can't wait to write
+* data because the sink might be infinitely writable, and if no data appears
+* in the source for a long time we might have to spin quite a bit waiting to
+* write, waiting to write again, &c. Waiting on stream closure instead means
+* we'll get just one notification if the sink dies. Note that when data
+* starts arriving from the sink we'll resume waiting for data to be written,
+* dropping this closure-only callback entirely.
+*/
+ _waitForSinkClosure: function()
+ {
+ dumpn("*** _waitForSinkClosure");
+
+ this._sink.asyncWait(this, Ci.nsIAsyncOutputStream.WAIT_CLOSURE_ONLY, 0,
+ gThreadManager.mainThread);
+ },
+
+ /**
+* Closes input with the given status, if it hasn't already been closed;
+* otherwise a no-op.
+*
+* @param status : nsresult
+* status code use to close the source stream if necessary
+*/
+ _finishSource: function(status)
+ {
+ dumpn("*** _finishSource(" + status.toString(16) + ")");
+
+ if (this._source !== null)
+ {
+ this._source.closeWithStatus(status);
+ this._source = null;
+ }
+ }
+};
+
+
+/**
+* A container for utility functions used with HTTP headers.
+*/
+const headerUtils =
+{
+ /**
+* Normalizes fieldName (by converting it to lowercase) and ensures it is a
+* valid header field name (although not necessarily one specified in RFC
+* 2616).
+*
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not match the field-name production in RFC 2616
+* @returns string
+* fieldName converted to lowercase if it is a valid header, for characters
+* where case conversion is possible
+*/
+ normalizeFieldName: function(fieldName)
+ {
+ if (fieldName == "")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ for (var i = 0, sz = fieldName.length; i < sz; i++)
+ {
+ if (!IS_TOKEN_ARRAY[fieldName.charCodeAt(i)])
+ {
+ dumpn(fieldName + " is not a valid header field name!");
+ throw Cr.NS_ERROR_INVALID_ARG;
+ }
+ }
+
+ return fieldName.toLowerCase();
+ },
+
+ /**
+* Ensures that fieldValue is a valid header field value (although not
+* necessarily as specified in RFC 2616 if the corresponding field name is
+* part of the HTTP protocol), normalizes the value if it is, and
+* returns the normalized value.
+*
+* @param fieldValue : string
+* a value to be normalized as an HTTP header field value
+* @throws NS_ERROR_INVALID_ARG
+* if fieldValue does not match the field-value production in RFC 2616
+* @returns string
+* fieldValue as a normalized HTTP header field value
+*/
+ normalizeFieldValue: function(fieldValue)
+ {
+ // field-value = *( field-content | LWS )
+ // field-content = <the OCTETs making up the field-value
+ // and consisting of either *TEXT or combinations
+ // of token, separators, and quoted-string>
+ // TEXT = <any OCTET except CTLs,
+ // but including LWS>
+ // LWS = [CRLF] 1*( SP | HT )
+ //
+ // quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
+ // qdtext = <any TEXT except <">>
+ // quoted-pair = "\" CHAR
+ // CHAR = <any US-ASCII character (octets 0 - 127)>
+
+ // Any LWS that occurs between field-content MAY be replaced with a single
+ // SP before interpreting the field value or forwarding the message
+ // downstream (section 4.2); we replace 1*LWS with a single SP
+ var val = fieldValue.replace(/(?:(?:\r\n)?[ \t]+)+/g, " ");
+
+ // remove leading/trailing LWS (which has been converted to SP)
+ val = val.replace(/^ +/, "").replace(/ +$/, "");
+
+ // that should have taken care of all CTLs, so val should contain no CTLs
+ for (var i = 0, len = val.length; i < len; i++)
+ if (isCTL(val.charCodeAt(i)))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ // XXX disallows quoted-pair where CHAR is a CTL -- will not invalidly
+ // normalize, however, so this can be construed as a tightening of the
+ // spec and not entirely as a bug
+ return val;
+ }
+};
+
+
+
+/**
+* Converts the given string into a string which is safe for use in an HTML
+* context.
+*
+* @param str : string
+* the string to make HTML-safe
+* @returns string
+* an HTML-safe version of str
+*/
+function htmlEscape(str)
+{
+ // this is naive, but it'll work
+ var s = "";
+ for (var i = 0; i < str.length; i++)
+ s += "&#" + str.charCodeAt(i) + ";";
+ return s;
+}
+
+
+/**
+* Constructs an object representing an HTTP version (see section 3.1).
+*
+* @param versionString
+* a string of the form "#.#", where # is an non-negative decimal integer with
+* or without leading zeros
+* @throws
+* if versionString does not specify a valid HTTP version number
+*/
+function nsHttpVersion(versionString)
+{
+ var matches = /^(\d+)\.(\d+)$/.exec(versionString);
+ if (!matches)
+ throw "Not a valid HTTP version!";
+
+ /** The major version number of this, as a number. */
+ this.major = parseInt(matches[1], 10);
+
+ /** The minor version number of this, as a number. */
+ this.minor = parseInt(matches[2], 10);
+
+ if (isNaN(this.major) || isNaN(this.minor) ||
+ this.major < 0 || this.minor < 0)
+ throw "Not a valid HTTP version!";
+}
+nsHttpVersion.prototype =
+{
+ /**
+* Returns the standard string representation of the HTTP version represented
+* by this (e.g., "1.1").
+*/
+ toString: function ()
+ {
+ return this.major + "." + this.minor;
+ },
+
+ /**
+* Returns true if this represents the same HTTP version as otherVersion,
+* false otherwise.
+*
+* @param otherVersion : nsHttpVersion
+* the version to compare against this
+*/
+ equals: function (otherVersion)
+ {
+ return this.major == otherVersion.major &&
+ this.minor == otherVersion.minor;
+ },
+
+ /** True if this >= otherVersion, false otherwise. */
+ atLeast: function(otherVersion)
+ {
+ return this.major > otherVersion.major ||
+ (this.major == otherVersion.major &&
+ this.minor >= otherVersion.minor);
+ }
+};
+
+nsHttpVersion.HTTP_1_0 = new nsHttpVersion("1.0");
+nsHttpVersion.HTTP_1_1 = new nsHttpVersion("1.1");
+
+
+/**
+* An object which stores HTTP headers for a request or response.
+*
+* Note that since headers are case-insensitive, this object converts headers to
+* lowercase before storing them. This allows the getHeader and hasHeader
+* methods to work correctly for any case of a header, but it means that the
+* values returned by .enumerator may not be equal case-sensitively to the
+* values passed to setHeader when adding headers to this.
+*/
+function nsHttpHeaders()
+{
+ /**
+* A hash of headers, with header field names as the keys and header field
+* values as the values. Header field names are case-insensitive, but upon
+* insertion here they are converted to lowercase. Header field values are
+* normalized upon insertion to contain no leading or trailing whitespace.
+*
+* Note also that per RFC 2616, section 4.2, two headers with the same name in
+* a message may be treated as one header with the same field name and a field
+* value consisting of the separate field values joined together with a "," in
+* their original order. This hash stores multiple headers with the same name
+* in this manner.
+*/
+ this._headers = {};
+}
+nsHttpHeaders.prototype =
+{
+ /**
+* Sets the header represented by name and value in this.
+*
+* @param name : string
+* the header name
+* @param value : string
+* the header value
+* @throws NS_ERROR_INVALID_ARG
+* if name or value is not a valid header component
+*/
+ setHeader: function(fieldName, fieldValue, merge)
+ {
+ var name = headerUtils.normalizeFieldName(fieldName);
+ var value = headerUtils.normalizeFieldValue(fieldValue);
+
+ // The following three headers are stored as arrays because their real-world
+ // syntax prevents joining individual headers into a single header using
+ // ",". See also <http://hg.mozilla.org/mozilla-central/diff/9b2a99adc05e/netwerk/protocol/http/src/nsHttpHeaderArray.cpp#l77>
+ if (merge && name in this._headers)
+ {
+ if (name === "www-authenticate" ||
+ name === "proxy-authenticate" ||
+ name === "set-cookie")
+ {
+ this._headers[name].push(value);
+ }
+ else
+ {
+ this._headers[name][0] += "," + value;
+ NS_ASSERT(this._headers[name].length === 1,
+ "how'd a non-special header have multiple values?")
+ }
+ }
+ else
+ {
+ this._headers[name] = [value];
+ }
+ },
+
+ /**
+* Returns the value for the header specified by this.
+*
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not constitute a valid header field name
+* @throws NS_ERROR_NOT_AVAILABLE
+* if the given header does not exist in this
+* @returns string
+* the field value for the given header, possibly with non-semantic changes
+* (i.e., leading/trailing whitespace stripped, whitespace runs replaced
+* with spaces, etc.) at the option of the implementation; multiple
+* instances of the header will be combined with a comma, except for
+* the three headers noted in the description of getHeaderValues
+*/
+ getHeader: function(fieldName)
+ {
+ return this.getHeaderValues(fieldName).join("\n");
+ },
+
+ /**
+* Returns the value for the header specified by fieldName as an array.
+*
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not constitute a valid header field name
+* @throws NS_ERROR_NOT_AVAILABLE
+* if the given header does not exist in this
+* @returns [string]
+* an array of all the header values in this for the given
+* header name. Header values will generally be collapsed
+* into a single header by joining all header values together
+* with commas, but certain headers (Proxy-Authenticate,
+* WWW-Authenticate, and Set-Cookie) violate the HTTP spec
+* and cannot be collapsed in this manner. For these headers
+* only, the returned array may contain multiple elements if
+* that header has been added more than once.
+*/
+ getHeaderValues: function(fieldName)
+ {
+ var name = headerUtils.normalizeFieldName(fieldName);
+
+ if (name in this._headers)
+ return this._headers[name];
+ else
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ },
+
+ /**
+* Returns true if a header with the given field name exists in this, false
+* otherwise.
+*
+* @param fieldName : string
+* the field name whose existence is to be determined in this
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not constitute a valid header field name
+* @returns boolean
+* true if the header's present, false otherwise
+*/
+ hasHeader: function(fieldName)
+ {
+ var name = headerUtils.normalizeFieldName(fieldName);
+ return (name in this._headers);
+ },
+
+ /**
+* Returns a new enumerator over the field names of the headers in this, as
+* nsISupportsStrings. The names returned will be in lowercase, regardless of
+* how they were input using setHeader (header names are case-insensitive per
+* RFC 2616).
+*/
+ get enumerator()
+ {
+ var headers = [];
+ for (var i in this._headers)
+ {
+ var supports = new SupportsString();
+ supports.data = i;
+ headers.push(supports);
+ }
+
+ return new nsSimpleEnumerator(headers);
+ }
+};
+
+
+/**
+* Constructs an nsISimpleEnumerator for the given array of items.
+*
+* @param items : Array
+* the items, which must all implement nsISupports
+*/
+function nsSimpleEnumerator(items)
+{
+ this._items = items;
+ this._nextIndex = 0;
+}
+nsSimpleEnumerator.prototype =
+{
+ hasMoreElements: function()
+ {
+ return this._nextIndex < this._items.length;
+ },
+ getNext: function()
+ {
+ if (!this.hasMoreElements())
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+
+ return this._items[this._nextIndex++];
+ },
+ QueryInterface: function(aIID)
+ {
+ if (Ci.nsISimpleEnumerator.equals(aIID) ||
+ Ci.nsISupports.equals(aIID))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ }
+};
+
+
+/**
+* A representation of the data in an HTTP request.
+*
+* @param port : uint
+* the port on which the server receiving this request runs
+*/
+function Request(port)
+{
+ /** Method of this request, e.g. GET or POST. */
+ this._method = "";
+
+ /** Path of the requested resource; empty paths are converted to '/'. */
+ this._path = "";
+
+ /** Query string, if any, associated with this request (not including '?'). */
+ this._queryString = "";
+
+ /** Scheme of requested resource, usually http, always lowercase. */
+ this._scheme = "http";
+
+ /** Hostname on which the requested resource resides. */
+ this._host = undefined;
+
+ /** Port number over which the request was received. */
+ this._port = port;
+
+ var bodyPipe = new Pipe(false, false, 0, PR_UINT32_MAX, null);
+
+ /** Stream from which data in this request's body may be read. */
+ this._bodyInputStream = bodyPipe.inputStream;
+
+ /** Stream to which data in this request's body is written. */
+ this._bodyOutputStream = bodyPipe.outputStream;
+
+ /**
+* The headers in this request.
+*/
+ this._headers = new nsHttpHeaders();
+
+ /**
+* For the addition of ad-hoc properties and new functionality without having
+* to change nsIHttpRequest every time; currently lazily created, as its only
+* use is in directory listings.
+*/
+ this._bag = null;
+}
+Request.prototype =
+{
+ // SERVER METADATA
+
+ //
+ // see nsIHttpRequest.scheme
+ //
+ get scheme()
+ {
+ return this._scheme;
+ },
+
+ //
+ // see nsIHttpRequest.host
+ //
+ get host()
+ {
+ return this._host;
+ },
+
+ //
+ // see nsIHttpRequest.port
+ //
+ get port()
+ {
+ return this._port;
+ },
+
+ // REQUEST LINE
+
+ //
+ // see nsIHttpRequest.method
+ //
+ get method()
+ {
+ return this._method;
+ },
+
+ //
+ // see nsIHttpRequest.httpVersion
+ //
+ get httpVersion()
+ {
+ return this._httpVersion.toString();
+ },
+
+ //
+ // see nsIHttpRequest.path
+ //
+ get path()
+ {
+ return this._path;
+ },
+
+ //
+ // see nsIHttpRequest.queryString
+ //
+ get queryString()
+ {
+ return this._queryString;
+ },
+
+ // HEADERS
+
+ //
+ // see nsIHttpRequest.getHeader
+ //
+ getHeader: function(name)
+ {
+ return this._headers.getHeader(name);
+ },
+
+ //
+ // see nsIHttpRequest.hasHeader
+ //
+ hasHeader: function(name)
+ {
+ return this._headers.hasHeader(name);
+ },
+
+ //
+ // see nsIHttpRequest.headers
+ //
+ get headers()
+ {
+ return this._headers.enumerator;
+ },
+
+ //
+ // see nsIPropertyBag.enumerator
+ //
+ get enumerator()
+ {
+ this._ensurePropertyBag();
+ return this._bag.enumerator;
+ },
+
+ //
+ // see nsIHttpRequest.headers
+ //
+ get bodyInputStream()
+ {
+ return this._bodyInputStream;
+ },
+
+ //
+ // see nsIPropertyBag.getProperty
+ //
+ getProperty: function(name)
+ {
+ this._ensurePropertyBag();
+ return this._bag.getProperty(name);
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIHttpRequest) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // PRIVATE IMPLEMENTATION
+
+ /** Ensures a property bag has been created for ad-hoc behaviors. */
+ _ensurePropertyBag: function()
+ {
+ if (!this._bag)
+ this._bag = new WritablePropertyBag();
+ }
+};
+
+
+// XPCOM trappings
+if ("XPCOMUtils" in this && // Firefox 3.6 doesn't load XPCOMUtils in this scope for some reason...
+ "generateNSGetFactory" in XPCOMUtils) {
+ var NSGetFactory = XPCOMUtils.generateNSGetFactory([nsHttpServer]);
+}
+
+
+
+/**
+* Creates a new HTTP server listening for loopback traffic on the given port,
+* starts it, and runs the server until the server processes a shutdown request,
+* spinning an event loop so that events posted by the server's socket are
+* processed.
+*
+* This method is primarily intended for use in running this script from within
+* xpcshell and running a functional HTTP server without having to deal with
+* non-essential details.
+*
+* Note that running multiple servers using variants of this method probably
+* doesn't work, simply due to how the internal event loop is spun and stopped.
+*
+* @note
+* This method only works with Mozilla 1.9 (i.e., Firefox 3 or trunk code);
+* you should use this server as a component in Mozilla 1.8.
+* @param port
+* the port on which the server will run, or -1 if there exists no preference
+* for a specific port; note that attempting to use some values for this
+* parameter (particularly those below 1024) may cause this method to throw or
+* may result in the server being prematurely shut down
+* @param basePath
+* a local directory from which requests will be served (i.e., if this is
+* "/home/jwalden/" then a request to /index.html will load
+* /home/jwalden/index.html); if this is omitted, only the default URLs in
+* this server implementation will be functional
+*/
+function server(port, basePath)
+{
+ if (basePath)
+ {
+ var lp = Cc["@mozilla.org/file/local;1"]
+ .createInstance(Ci.nsILocalFile);
+ lp.initWithPath(basePath);
+ }
+
+ // if you're running this, you probably want to see debugging info
+ DEBUG = true;
+
+ var srv = new nsHttpServer();
+ if (lp)
+ srv.registerDirectory("/", lp);
+ srv.registerContentType("sjs", SJS_TYPE);
+ srv.start(port);
+
+ var thread = gThreadManager.currentThread;
+ while (!srv.isStopped())
+ thread.processNextEvent(true);
+
+ // get rid of any pending requests
+ while (thread.hasPendingEvents())
+ thread.processNextEvent(true);
+
+ DEBUG = false;
+}
+
+function startServerAsync(port, basePath)
+{
+ if (basePath)
+ {
+ var lp = Cc["@mozilla.org/file/local;1"]
+ .createInstance(Ci.nsILocalFile);
+ lp.initWithPath(basePath);
+ }
+
+ var srv = new nsHttpServer();
+ if (lp)
+ srv.registerDirectory("/", lp);
+ srv.registerContentType("sjs", "sjs");
+ srv.start(port);
+ return srv;
+}
+
+exports.nsHttpServer = nsHttpServer;
+exports.ScriptableInputStream = ScriptableInputStream;
+exports.server = server;
+exports.startServerAsync = startServerAsync;
diff --git a/addon-sdk/source/test/addons/e10s-content/lib/main.js b/addon-sdk/source/test/addons/e10s-content/lib/main.js
new file mode 100644
index 000000000..6ca7dc48c
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-content/lib/main.js
@@ -0,0 +1,22 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { merge } = require('sdk/util/object');
+const { version } = require('sdk/system');
+
+const SKIPPING_TESTS = {
+ "test skip": (assert) => assert.pass("nothing to test here")
+};
+
+merge(module.exports, require('./test-content-script'));
+merge(module.exports, require('./test-content-worker'));
+merge(module.exports, require('./test-page-worker'));
+
+// run e10s tests only on builds from trunk, fx-team, Nightly..
+if (!version.endsWith('a1')) {
+ module.exports = SKIPPING_TESTS;
+}
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/e10s-content/lib/test-content-script.js b/addon-sdk/source/test/addons/e10s-content/lib/test-content-script.js
new file mode 100644
index 000000000..c24f841b2
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-content/lib/test-content-script.js
@@ -0,0 +1,845 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const hiddenFrames = require("sdk/frame/hidden-frame");
+const { create: makeFrame } = require("sdk/frame/utils");
+const { window } = require("sdk/addon/window");
+const { Loader } = require('sdk/test/loader');
+const { URL } = require("sdk/url");
+const testURI = require("./fixtures").url("test.html");
+const testHost = URL(testURI).scheme + '://' + URL(testURI).host;
+
+/*
+ * Utility function that allow to easily run a proxy test with a clean
+ * new HTML document. See first unit test for usage.
+ */
+function createProxyTest(html, callback) {
+ return function (assert, done) {
+ let url = 'data:text/html;charset=utf-8,' + encodeURIComponent(html);
+ let principalLoaded = false;
+
+ let element = makeFrame(window.document, {
+ nodeName: "iframe",
+ type: "content",
+ allowJavascript: true,
+ allowPlugins: true,
+ allowAuth: true,
+ uri: testURI
+ });
+
+ element.addEventListener("DOMContentLoaded", onDOMReady, false);
+
+ function onDOMReady() {
+ // Reload frame after getting principal from `testURI`
+ if (!principalLoaded) {
+ element.setAttribute("src", url);
+ principalLoaded = true;
+ return;
+ }
+
+ assert.equal(element.getAttribute("src"), url, "correct URL loaded");
+ element.removeEventListener("DOMContentLoaded", onDOMReady,
+ false);
+ let xrayWindow = element.contentWindow;
+ let rawWindow = xrayWindow.wrappedJSObject;
+
+ let isDone = false;
+ let helper = {
+ xrayWindow: xrayWindow,
+ rawWindow: rawWindow,
+ createWorker: function (contentScript) {
+ return createWorker(assert, xrayWindow, contentScript, helper.done);
+ },
+ done: function () {
+ if (isDone)
+ return;
+ isDone = true;
+ element.parentNode.removeChild(element);
+ done();
+ }
+ };
+ callback(helper, assert);
+ }
+ };
+}
+
+function createWorker(assert, xrayWindow, contentScript, done) {
+ let loader = Loader(module);
+ let Worker = loader.require("sdk/content/worker").Worker;
+ let worker = Worker({
+ window: xrayWindow,
+ contentScript: [
+ 'let assert, done; new ' + function () {
+ assert = function assert(v, msg) {
+ self.port.emit("assert", {assertion:v, msg:msg});
+ }
+ done = function done() {
+ self.port.emit("done");
+ }
+ },
+ contentScript
+ ]
+ });
+
+ worker.port.on("done", done);
+ worker.port.on("assert", function (data) {
+ assert.ok(data.assertion, data.msg);
+ });
+
+ return worker;
+}
+
+/* Examples for the `createProxyTest` uses */
+
+var html = "<script>var documentGlobal = true</script>";
+
+exports["test Create Proxy Test"] = createProxyTest(html, function (helper, assert) {
+ // You can get access to regular `test` object in second argument of
+ // `createProxyTest` method:
+ assert.ok(helper.rawWindow.documentGlobal,
+ "You have access to a raw window reference via `helper.rawWindow`");
+ assert.ok(!("documentGlobal" in helper.xrayWindow),
+ "You have access to an XrayWrapper reference via `helper.xrayWindow`");
+
+ // If you do not create a Worker, you have to call helper.done(),
+ // in order to say when your test is finished
+ helper.done();
+});
+
+exports["test Create Proxy Test With Worker"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ "new " + function WorkerScope() {
+ assert(true, "You can do assertions in your content script");
+ // And if you create a worker, you either have to call `done`
+ // from content script or helper.done()
+ done();
+ }
+ );
+
+});
+
+exports["test Create Proxy Test With Events"] = createProxyTest("", function (helper, assert) {
+
+ let worker = helper.createWorker(
+ "new " + function WorkerScope() {
+ self.port.emit("foo");
+ }
+ );
+
+ worker.port.on("foo", function () {
+ assert.pass("You can use events");
+ // And terminate your test with helper.done:
+ helper.done();
+ });
+
+});
+
+/* Disabled due to bug 1038432
+// Bug 714778: There was some issue around `toString` functions
+// that ended up being shared between content scripts
+exports["test Shared To String Proxies"] = createProxyTest("", function(helper) {
+
+ let worker = helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // We ensure that `toString` can't be modified so that nothing could
+ // leak to/from the document and between content scripts
+ // It only applies to JS proxies, there isn't any such issue with xrays.
+ //document.location.toString = function foo() {};
+ document.location.toString.foo = "bar";
+ assert("foo" in document.location.toString, "document.location.toString can be modified");
+ assert(document.location.toString() == "data:text/html;charset=utf-8,",
+ "First document.location.toString()");
+ self.postMessage("next");
+ }
+ );
+ worker.on("message", function () {
+ helper.createWorker(
+ 'new ' + function ContentScriptScope2() {
+ assert(!("foo" in document.location.toString),
+ "document.location.toString is different for each content script");
+ assert(document.location.toString() == "data:text/html;charset=utf-8,",
+ "Second document.location.toString()");
+ done();
+ }
+ );
+ });
+});
+*/
+
+// Ensure that postMessage is working correctly across documents with an iframe
+var html = '<iframe id="iframe" name="test" src="data:text/html;charset=utf-8," />';
+exports["test postMessage"] = createProxyTest(html, function (helper, assert) {
+ let ifWindow = helper.xrayWindow.document.getElementById("iframe").contentWindow;
+ // Listen without proxies, to check that it will work in regular case
+ // simulate listening from a web document.
+ ifWindow.addEventListener("message", function listener(event) {
+ ifWindow.removeEventListener("message", listener, false);
+ // As we are in system principal, event is an XrayWrapper
+ // xrays use current compartments when calling postMessage method.
+ // Whereas js proxies was using postMessage method compartment,
+ // not the caller one.
+ assert.strictEqual(event.source, helper.xrayWindow,
+ "event.source is the top window");
+ assert.equal(event.origin, testHost, "origin matches testHost");
+
+ assert.equal(event.data, "{\"foo\":\"bar\\n \\\"escaped\\\".\"}",
+ "message data is correct");
+
+ helper.done();
+ }, false);
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ var json = JSON.stringify({foo : "bar\n \"escaped\"."});
+
+ document.getElementById("iframe").contentWindow.postMessage(json, "*");
+ }
+ );
+});
+
+var html = '<input id="input2" type="checkbox" />';
+exports["test Object Listener"] = createProxyTest(html, function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // Test objects being given as event listener
+ let input = document.getElementById("input2");
+ let myClickListener = {
+ called: false,
+ handleEvent: function(event) {
+ assert(this === myClickListener, "`this` is the original object");
+ assert(!this.called, "called only once");
+ this.called = true;
+ assert(event.target, input, "event.target is the wrapped window");
+ done();
+ }
+ };
+
+ window.addEventListener("click", myClickListener, true);
+ input.click();
+ window.removeEventListener("click", myClickListener, true);
+ }
+ );
+
+});
+
+exports["test Object Listener 2"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ ('new ' + function ContentScriptScope() {
+ // variable replaced with `testHost`
+ let testHost = "TOKEN";
+ // Verify object as DOM event listener
+ let myMessageListener = {
+ called: false,
+ handleEvent: function(event) {
+ window.removeEventListener("message", myMessageListener, true);
+
+ assert(this == myMessageListener, "`this` is the original object");
+ assert(!this.called, "called only once");
+ this.called = true;
+ assert(event.target == document.defaultView, "event.target is the wrapped window");
+ assert(event.source == document.defaultView, "event.source is the wrapped window");
+ assert(event.origin == testHost, "origin matches testHost");
+ assert(event.data == "ok", "message data is correct");
+ done();
+ }
+ };
+
+ window.addEventListener("message", myMessageListener, true);
+ document.defaultView.postMessage("ok", '*');
+ }
+ ).replace("TOKEN", testHost));
+
+});
+
+var html = '<input id="input" type="text" /><input id="input3" type="checkbox" />' +
+ '<input id="input2" type="checkbox" />';
+
+exports.testStringOverload = createProxyTest(html, function (helper, assert) {
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // RightJS is hacking around String.prototype, and do similar thing:
+ // Pass `this` from a String prototype method.
+ // It is funny because typeof this == "object"!
+ // So that when we pass `this` to a native method,
+ // our proxy code can fail on another even more crazy thing.
+ // See following test to see what fails around proxies.
+ String.prototype.update = function () {
+ assert(typeof this == "object", "in update, `this` is an object");
+ assert(this.toString() == "input", "in update, `this.toString works");
+ return document.querySelectorAll(this);
+ };
+ assert("input".update().length == 3, "String.prototype overload works");
+ done();
+ }
+ );
+});
+
+exports["test Element.matches()"] = createProxyTest("", function (helper) {
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // Check matches XrayWrappers bug (Bug 658909):
+ // Test that Element.matches() does not return bad results when we are
+ // not calling it from the node itself.
+ assert(document.createElement( "div" ).matches("div"),
+ "matches works while being called from the node");
+ assert(document.documentElement.matches.call(
+ document.createElement( "div" ),
+ "div"
+ ),
+ "matches works while being called from a " +
+ "function reference to " +
+ "document.documentElement.matches.call");
+ done();
+ }
+ );
+});
+
+exports["test Events Overload"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // If we add a "____proxy" attribute on XrayWrappers in order to store
+ // the related proxy to create an unique proxy for each wrapper;
+ // we end up setting this attribute to prototype objects :x
+ // And so, instances created with such prototype will be considered
+ // as equal to the prototype ...
+ // // Internal method that return the proxy for a given XrayWrapper
+ // function proxify(obj) {
+ // if (obj._proxy) return obj._proxy;
+ // return obj._proxy = Proxy.create(...);
+ // }
+ //
+ // // Get a proxy of an XrayWrapper prototype object
+ // let proto = proxify(xpcProto);
+ //
+ // // Use this proxy as a prototype
+ // function Constr() {}
+ // Constr.proto = proto;
+ //
+ // // Try to create an instance using this prototype
+ // let xpcInstance = new Constr();
+ // let wrapper = proxify(xpcInstance)
+ //
+ // xpcProto._proxy = proto and as xpcInstance.__proto__ = xpcProto,
+ // xpcInstance._proxy = proto ... and profixy(xpcInstance) = proto :(
+ //
+ let proto = window.document.createEvent('HTMLEvents').__proto__;
+ window.Event.prototype = proto;
+ let event = document.createEvent('HTMLEvents');
+ assert(event !== proto, "Event should not be equal to its prototype");
+ event.initEvent('dataavailable', true, true);
+ assert(event.type === 'dataavailable', "Events are working fine");
+ done();
+ }
+ );
+
+});
+
+exports["test Nested Attributes"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // XrayWrappers has a bug when you set an attribute on it,
+ // in some cases, it creates an unnecessary wrapper that introduces
+ // a different object that refers to the same original object
+ // Check that our wrappers don't reproduce this bug
+ // SEE BUG 658560: Fix identity problem with CrossOriginWrappers
+ let o = {sandboxObject:true};
+ window.nested = o;
+ o.foo = true;
+ assert(o === window.nested, "Nested attribute to sandbox object should not be proxified");
+ window.nested = document;
+ assert(window.nested === document, "Nested attribute to proxy should not be double proxified");
+ done();
+ }
+ );
+
+});
+
+exports["test Form nodeName"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ let body = document.body;
+ // Check form[nodeName]
+ let form = document.createElement("form");
+ let input = document.createElement("input");
+ input.setAttribute("name", "test");
+ form.appendChild(input);
+ body.appendChild(form);
+ assert(form.test == input, "form[nodeName] is valid");
+ body.removeChild(form);
+ done();
+ }
+ );
+
+});
+
+exports["test localStorage"] = createProxyTest("", function (helper, assert) {
+
+ let worker = helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // Check localStorage:
+ assert(window.localStorage, "has access to localStorage");
+ window.localStorage.name = 1;
+ assert(window.localStorage.name == 1, "localStorage appears to work");
+
+ self.port.on("step2", function () {
+ window.localStorage.clear();
+ assert(window.localStorage.name == undefined, "localStorage really, really works");
+ done();
+ });
+ self.port.emit("step1");
+ }
+ );
+
+ worker.port.on("step1", function () {
+ assert.equal(helper.rawWindow.localStorage.name, 1, "localStorage really works");
+ worker.port.emit("step2");
+ });
+
+});
+
+exports["test Auto Unwrap Custom Attributes"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ let body = document.body;
+ // Setting a custom object to a proxy attribute is not wrapped when we get it afterward
+ let object = {custom: true, enumerable: false};
+ body.customAttribute = object;
+ assert(object === body.customAttribute, "custom JS attributes are not wrapped");
+ done();
+ }
+ );
+
+});
+
+exports["test Object Tag"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // <object>, <embed> and other tags return typeof 'function'
+ let flash = document.createElement("object");
+ assert(typeof flash == "function", "<object> is typeof 'function'");
+ assert(flash.toString().match(/\[object HTMLObjectElement.*\]/), "<object> is HTMLObjectElement");
+ assert("setAttribute" in flash, "<object> has a setAttribute method");
+ done();
+ }
+ );
+
+});
+
+exports["test Highlight toString Behavior"] = createProxyTest("", function (helper, assert) {
+ // We do not have any workaround this particular use of toString
+ // applied on <object> elements. So disable this test until we found one!
+ //assert.equal(helper.rawWindow.Object.prototype.toString.call(flash), "[object HTMLObjectElement]", "<object> is HTMLObjectElement");
+ function f() {};
+ let funToString = Object.prototype.toString.call(f);
+ assert.ok(/\[object Function.*\]/.test(funToString), "functions are functions 1");
+
+ // This is how jquery call toString:
+ let strToString = helper.rawWindow.Object.prototype.toString.call("");
+ assert.ok(/\[object String.*\]/.test(strToString), "strings are strings");
+
+ let o = {__exposedProps__:{}};
+ let objToString = helper.rawWindow.Object.prototype.toString.call(o);
+ assert.ok(/\[object Object.*\]/.test(objToString), "objects are objects");
+
+ // Make sure to pass a function from the same compartments
+ // or toString will return [object Object] on FF8+
+ let f2 = helper.rawWindow.eval("(function () {})");
+ let funToString2 = helper.rawWindow.Object.prototype.toString.call(f2);
+ assert.ok(/\[object Function.*\]/.test(funToString2), "functions are functions 2");
+
+ helper.done();
+});
+
+exports["test Document TagName"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ let body = document.body;
+ // Check document[tagName]
+ let div = document.createElement("div");
+ div.setAttribute("name", "test");
+ body.appendChild(div);
+ assert(!document.test, "document[divName] is undefined");
+ body.removeChild(div);
+
+ let form = document.createElement("form");
+ form.setAttribute("name", "test");
+ body.appendChild(form);
+ assert(document.test == form, "document[formName] is valid");
+ body.removeChild(form);
+
+ let img = document.createElement("img");
+ img.setAttribute("name", "test");
+ body.appendChild(img);
+ assert(document.test == img, "document[imgName] is valid");
+ body.removeChild(img);
+ done();
+ }
+ );
+
+});
+
+var html = '<iframe id="iframe" name="test" src="data:text/html;charset=utf-8," />';
+exports["test Window Frames"] = createProxyTest(html, function (helper) {
+
+ helper.createWorker(
+ 'let glob = this; new ' + function ContentScriptScope() {
+ // Check window[frameName] and window.frames[i]
+ let iframe = document.getElementById("iframe");
+ //assert(window.frames.length == 1, "The iframe is reported in window.frames check1");
+ //assert(window.frames[0] == iframe.contentWindow, "The iframe is reported in window.frames check2");
+ assert(window.test == iframe.contentWindow, "window[frameName] is valid");
+ done();
+ }
+ );
+
+});
+
+exports["test Collections"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // Highlight XPCNativeWrapper bug with HTMLCollection
+ // tds[0] is only defined on first access :o
+ let body = document.body;
+ let div = document.createElement("div");
+ body.appendChild(div);
+ div.innerHTML = "<table><tr><td style='padding:0;border:0;display:none'></td><td>t</td></tr></table>";
+ let tds = div.getElementsByTagName("td");
+ assert(tds[0] == tds[0], "We can get array element multiple times");
+ body.removeChild(div);
+ done();
+ }
+ );
+
+});
+
+var html = '<input id="input" type="text" /><input id="input3" type="checkbox" />' +
+ '<input id="input2" type="checkbox" />';
+exports["test Collections 2"] = createProxyTest(html, function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // Verify that NodeList/HTMLCollection are working fine
+ let body = document.body;
+ let inputs = body.getElementsByTagName("input");
+ assert(body.childNodes.length == 3, "body.childNodes length is correct");
+ assert(inputs.length == 3, "inputs.length is correct");
+ assert(body.childNodes[0] == inputs[0], "body.childNodes[0] is correct");
+ assert(body.childNodes[1] == inputs[1], "body.childNodes[1] is correct");
+ assert(body.childNodes[2] == inputs[2], "body.childNodes[2] is correct");
+ let count = 0;
+ for(let i in body.childNodes) {
+ count++;
+ }
+
+ assert(count >= 3, "body.childNodes is iterable");
+ done();
+ }
+ );
+
+});
+
+exports["test XMLHttpRequest"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // XMLHttpRequest doesn't support XMLHttpRequest.apply,
+ // that may break our proxy code
+ assert(new window.XMLHttpRequest(), "we are able to instantiate XMLHttpRequest object");
+ done();
+ }
+ );
+
+});
+
+exports["test XPathResult"] = createProxyTest("", function (helper, assert) {
+ const XPathResultTypes = ["ANY_TYPE",
+ "NUMBER_TYPE", "STRING_TYPE", "BOOLEAN_TYPE",
+ "UNORDERED_NODE_ITERATOR_TYPE",
+ "ORDERED_NODE_ITERATOR_TYPE",
+ "UNORDERED_NODE_SNAPSHOT_TYPE",
+ "ORDERED_NODE_SNAPSHOT_TYPE",
+ "ANY_UNORDERED_NODE_TYPE",
+ "FIRST_ORDERED_NODE_TYPE"];
+
+ // Check XPathResult bug with constants being undefined on XPCNativeWrapper
+ let xpcXPathResult = helper.xrayWindow.XPathResult;
+
+ XPathResultTypes.forEach(function(type, i) {
+ assert.equal(xpcXPathResult.wrappedJSObject[type],
+ helper.rawWindow.XPathResult[type],
+ "XPathResult's constants are valid on unwrapped node");
+
+ assert.equal(xpcXPathResult[type], i,
+ "XPathResult's constants are defined on " +
+ "XPCNativeWrapper (platform bug #)");
+ });
+
+ let value = helper.rawWindow.XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE;
+ let worker = helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ self.port.on("value", function (value) {
+ // Check that our work around is working:
+ assert(window.XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE === value,
+ "XPathResult works correctly on Proxies");
+ done();
+ });
+ }
+ );
+ worker.port.emit("value", value);
+});
+
+exports["test Prototype Inheritance"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // Verify that inherited prototype function like initEvent
+ // are handled correctly. (e2.type will return an error if it's not the case)
+ let event1 = document.createEvent( 'MouseEvents' );
+ event1.initEvent( "click", true, true );
+ let event2 = document.createEvent( 'MouseEvents' );
+ event2.initEvent( "click", true, true );
+ assert(event2.type == "click", "We are able to create an event");
+ done();
+ }
+ );
+
+});
+
+exports["test Functions"] = createProxyTest("", function (helper) {
+
+ helper.rawWindow.callFunction = function callFunction(f) {
+ return f();
+ };
+ helper.rawWindow.isEqual = function isEqual(a, b) {
+ return a == b;
+ };
+ // bug 784116: workaround in order to allow proxy code to cache proxies on
+ // these functions:
+ helper.rawWindow.callFunction.__exposedProps__ = {__proxy: 'rw'};
+ helper.rawWindow.isEqual.__exposedProps__ = {__proxy: 'rw'};
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // Check basic usage of functions
+ let closure2 = function () {return "ok";};
+ assert(window.wrappedJSObject.callFunction(closure2) == "ok", "Function references work");
+
+ // Ensure that functions are cached when being wrapped to native code
+ let closure = function () {};
+ assert(window.wrappedJSObject.isEqual(closure, closure), "Function references are cached before being wrapped to native");
+ done();
+ }
+ );
+
+});
+
+var html = '<input id="input2" type="checkbox" />';
+exports["test Listeners"] = createProxyTest(html, function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // Verify listeners:
+ let input = document.getElementById("input2");
+ assert(input, "proxy.getElementById works");
+
+ function onclick() {};
+ input.onclick = onclick;
+ assert(input.onclick === onclick, "on* attributes are equal to original function set");
+
+ let addEventListenerCalled = false;
+ let expandoCalled = false;
+ input.addEventListener("click", function onclick(event) {
+ input.removeEventListener("click", onclick, true);
+
+ assert(!addEventListenerCalled, "closure given to addEventListener is called once");
+ if (addEventListenerCalled)
+ return;
+ addEventListenerCalled = true;
+
+ assert(!event.target.ownerDocument.defaultView.documentGlobal, "event object is still wrapped and doesn't expose document globals");
+
+ let input2 = document.getElementById("input2");
+
+ input.onclick = function (event) {
+ input.onclick = null;
+ assert(!expandoCalled, "closure set to expando is called once");
+ if (expandoCalled) return;
+ expandoCalled = true;
+
+ assert(!event.target.ownerDocument.defaultView.documentGlobal, "event object is still wrapped and doesn't expose document globals");
+
+ setTimeout(function () {
+ input.click();
+ done();
+ }, 0);
+
+ }
+
+ setTimeout(function () {
+ input.click();
+ }, 0);
+
+ }, true);
+
+ input.click();
+ }
+ );
+
+});
+
+exports["test requestAnimationFrame"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ var self = (function() { return this; })();
+ window.requestAnimationFrame(function callback() {
+ assert(self == this, "self is equal to `this`");
+ done();
+ });
+ }
+ );
+
+});
+
+exports["testGlobalScope"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'let toplevelScope = true;' +
+ 'assert(window.toplevelScope, "variables in toplevel scope are set to `window` object");' +
+ 'assert(this.toplevelScope, "variables in toplevel scope are set to `this` object");' +
+ 'done();'
+ );
+
+});
+
+// Bug 715755: proxy code throw an exception on COW
+// Create an http server in order to simulate real cross domain documents
+exports["test Cross Domain Iframe"] = createProxyTest("", function (helper) {
+ let serverPort = 8099;
+ let server = require("./httpd").startServerAsync(serverPort);
+ server.registerPathHandler("/", function handle(request, response) {
+ // Returns the webpage that receive a message and forward it back to its
+ // parent document by appending ' world'.
+ let content = "<html><head><meta charset='utf-8'></head>\n";
+ content += "<script>\n";
+ content += " window.addEventListener('message', function (event) {\n";
+ content += " parent.postMessage(event.data + ' world', '*');\n";
+ content += " }, true);\n";
+ content += "</script>\n";
+ content += "<body></body>\n";
+ content += "</html>\n";
+ response.write(content);
+ });
+
+ let worker = helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // Waits for the server page url
+ self.on("message", function (url) {
+ // Creates an iframe with this page
+ let iframe = document.createElement("iframe");
+ iframe.addEventListener("load", function onload() {
+ iframe.removeEventListener("load", onload, true);
+ try {
+ // Try to communicate with iframe's content
+ window.addEventListener("message", function onmessage(event) {
+ window.removeEventListener("message", onmessage, true);
+
+ assert(event.data == "hello world", "COW works properly");
+ self.port.emit("end");
+ }, true);
+ iframe.contentWindow.postMessage("hello", "*");
+ } catch(e) {
+ assert(false, "COW fails : "+e.message);
+ }
+ }, true);
+ iframe.setAttribute("src", url);
+ document.body.appendChild(iframe);
+ });
+ }
+ );
+
+ worker.port.on("end", function () {
+ server.stop(helper.done);
+ });
+
+ worker.postMessage("http://localhost:" + serverPort + "/");
+
+});
+
+// Bug 769006: Ensure that MutationObserver works fine with proxies
+var html = '<a href="foo">link</a>';
+exports["test MutationObvserver"] = createProxyTest(html, function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ if (typeof MutationObserver == "undefined") {
+ assert(true, "No MutationObserver for this FF version");
+ done();
+ return;
+ }
+ let link = document.getElementsByTagName("a")[0];
+
+ // Register a Mutation observer
+ let obs = new MutationObserver(function(mutations){
+ // Ensure that mutation data are valid
+ assert(mutations.length == 1, "only one attribute mutation");
+ let mutation = mutations[0];
+ assert(mutation.type == "attributes", "check `type`");
+ assert(mutation.target == link, "check `target`");
+ assert(mutation.attributeName == "href", "check `attributeName`");
+ assert(mutation.oldValue == "foo", "check `oldValue`");
+ obs.disconnect();
+ done();
+ });
+ obs.observe(document, {
+ subtree: true,
+ attributes: true,
+ attributeOldValue: true,
+ attributeFilter: ["href"]
+ });
+
+ // Modify the DOM
+ link.setAttribute("href", "bar");
+ }
+ );
+
+});
+
+var html = '<script>' +
+ 'var accessCheck = function() {' +
+ ' assert(true, "exporting function works");' +
+ ' try{' +
+ ' exportedObj.prop;' +
+ ' assert(false, "content should not have access to content-script");' +
+ ' } catch(e) {' +
+ ' assert(e.toString().indexOf("Permission denied") != -1,' +
+ ' "content should not have access to content-script");' +
+ ' }' +
+ '}</script>';
+exports["test nsEp for content-script"] = createProxyTest(html, function (helper) {
+
+ helper.createWorker(
+ 'let glob = this; new ' + function ContentScriptScope() {
+
+ exportFunction(assert, unsafeWindow, { defineAs: "assert" });
+ window.wrappedJSObject.assert(true, "assert exported");
+ window.wrappedJSObject.exportedObj = { prop: 42 };
+ window.wrappedJSObject.accessCheck();
+ done();
+ }
+ );
+
+});
+
+// require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/addons/e10s-content/lib/test-content-worker.js b/addon-sdk/source/test/addons/e10s-content/lib/test-content-worker.js
new file mode 100644
index 000000000..5eddb826a
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-content/lib/test-content-worker.js
@@ -0,0 +1,1127 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+// Skipping due to window creation being unsupported in Fennec
+module.metadata = {
+ engines: {
+ 'Firefox': '*'
+ }
+};
+
+const { Cc, Ci } = require("chrome");
+const { on } = require("sdk/event/core");
+const { setTimeout } = require("sdk/timers");
+const { LoaderWithHookedConsole, Loader } = require("sdk/test/loader");
+const { Worker } = require("sdk/content/worker");
+const { close } = require("sdk/window/helpers");
+const { set: setPref } = require("sdk/preferences/service");
+const { isArray } = require("sdk/lang/type");
+const { URL } = require('sdk/url');
+const fixtures = require("./fixtures");
+const system = require("sdk/system/events");
+
+const DEPRECATE_PREF = "devtools.errorconsole.deprecation_warnings";
+
+const DEFAULT_CONTENT_URL = "data:text/html;charset=utf-8,foo";
+
+const WINDOW_SCRIPT_URL = "data:text/html;charset=utf-8," +
+ "<script>window.addEventListener('message', function (e) {" +
+ " if (e.data === 'from -> content-script')" +
+ " window.postMessage('from -> window', '*');" +
+ "});</script>";
+
+function makeWindow() {
+ let content =
+ "<?xml version=\"1.0\"?>" +
+ "<window " +
+ "xmlns=\"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul\">" +
+ "<script>var documentValue=true;</script>" +
+ "</window>";
+ var url = "data:application/vnd.mozilla.xul+xml;charset=utf-8," +
+ encodeURIComponent(content);
+ var features = ["chrome", "width=10", "height=10"];
+
+ return Cc["@mozilla.org/embedcomp/window-watcher;1"].
+ getService(Ci.nsIWindowWatcher).
+ openWindow(null, url, null, features.join(","), null);
+}
+
+// Listen for only first one occurence of DOM event
+function listenOnce(node, eventName, callback) {
+ node.addEventListener(eventName, function onevent(event) {
+ node.removeEventListener(eventName, onevent, true);
+ callback(node);
+ }, true);
+}
+
+// Load a given url in a given browser and fires the callback when it is loaded
+function loadAndWait(browser, url, callback) {
+ listenOnce(browser, "load", callback);
+ // We have to wait before calling `loadURI` otherwise, if we call
+ // `loadAndWait` during browser load event, the history will be broken
+ setTimeout(function () {
+ browser.loadURI(url);
+ }, 0);
+}
+
+// Returns a test function that will automatically open a new chrome window
+// with a <browser> element loaded on a given content URL
+// The callback receive 3 arguments:
+// - test: reference to the jetpack test object
+// - browser: a reference to the <browser> xul node
+// - done: a callback to call when test is over
+function WorkerTest(url, callback) {
+ return function testFunction(assert, done) {
+ let chromeWindow = makeWindow();
+ chromeWindow.addEventListener("load", function onload() {
+ chromeWindow.removeEventListener("load", onload, true);
+ let browser = chromeWindow.document.createElement("browser");
+ browser.setAttribute("type", "content");
+ chromeWindow.document.documentElement.appendChild(browser);
+ // Wait for about:blank load event ...
+ listenOnce(browser, "load", function onAboutBlankLoad() {
+ // ... before loading the expected doc and waiting for its load event
+ loadAndWait(browser, url, function onDocumentLoaded() {
+ callback(assert, browser, function onTestDone() {
+
+ close(chromeWindow).then(done);
+ });
+ });
+ });
+ }, true);
+ };
+}
+
+exports["test:sample"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ assert.notEqual(browser.contentWindow.location.href, "about:blank",
+ "window is now on the right document");
+
+ let window = browser.contentWindow
+ let worker = Worker({
+ window: window,
+ contentScript: "new " + function WorkerScope() {
+ // window is accessible
+ let myLocation = window.location.toString();
+ self.on("message", function(data) {
+ if (data == "hi!")
+ self.postMessage("bye!");
+ });
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(msg) {
+ assert.equal("bye!", msg);
+ assert.equal(worker.url, window.location.href,
+ "worker.url still works");
+ done();
+ }
+ });
+
+ assert.equal(worker.url, window.location.href,
+ "worker.url works");
+ assert.equal(worker.contentURL, window.location.href,
+ "worker.contentURL works");
+ worker.postMessage("hi!");
+ }
+);
+
+exports["test:emit"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ // Validate self.on and self.emit
+ self.port.on("addon-to-content", function (data) {
+ self.port.emit("content-to-addon", data);
+ });
+
+ // Check for global pollution
+ //if (typeof on != "undefined")
+ // self.postMessage("`on` is in globals");
+ if (typeof once != "undefined")
+ self.postMessage("`once` is in globals");
+ if (typeof emit != "undefined")
+ self.postMessage("`emit` is in globals");
+
+ },
+ onMessage: function(msg) {
+ assert.fail("Got an unexpected message : "+msg);
+ }
+ });
+
+ // Validate worker.port
+ worker.port.on("content-to-addon", function (data) {
+ assert.equal(data, "event data");
+ done();
+ });
+ worker.port.emit("addon-to-content", "event data");
+ }
+);
+
+exports["test:emit hack message"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ // Validate self.port
+ self.port.on("message", function (data) {
+ self.port.emit("message", data);
+ });
+ // We should not receive message on self, but only on self.port
+ self.on("message", function (data) {
+ self.postMessage("message", data);
+ });
+ },
+ onError: function(e) {
+ assert.fail("Got exception: "+e);
+ }
+ });
+
+ worker.port.on("message", function (data) {
+ assert.equal(data, "event data");
+ done();
+ });
+ worker.on("message", function (data) {
+ assert.fail("Got an unexpected message : "+msg);
+ });
+ worker.port.emit("message", "event data");
+ }
+);
+
+exports["test:n-arguments emit"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let repeat = 0;
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ // Validate self.on and self.emit
+ self.port.on("addon-to-content", function (a1, a2, a3) {
+ self.port.emit("content-to-addon", a1, a2, a3);
+ });
+ }
+ });
+
+ // Validate worker.port
+ worker.port.on("content-to-addon", function (arg1, arg2, arg3) {
+ if (!repeat++) {
+ this.emit("addon-to-content", "first argument", "second", "third");
+ } else {
+ assert.equal(arg1, "first argument");
+ assert.equal(arg2, "second");
+ assert.equal(arg3, "third");
+ done();
+ }
+ });
+ worker.port.emit("addon-to-content", "first argument", "second", "third");
+ }
+);
+
+exports["test:post-json-values-only"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ self.on("message", function (message) {
+ self.postMessage([ message.fun === undefined,
+ typeof message.w,
+ message.w && "port" in message.w,
+ message.w._url,
+ Array.isArray(message.array),
+ JSON.stringify(message.array)]);
+ });
+ }
+ });
+
+ // Validate worker.onMessage
+ let array = [1, 2, 3];
+ worker.on("message", function (message) {
+ assert.ok(message[0], "function becomes undefined");
+ assert.equal(message[1], "object", "object stays object");
+ assert.ok(message[2], "object's attributes are enumerable");
+ assert.equal(message[3], DEFAULT_CONTENT_URL,
+ "jsonable attributes are accessible");
+ // See bug 714891, Arrays may be broken over compartements:
+ assert.ok(message[4], "Array keeps being an array");
+ assert.equal(message[5], JSON.stringify(array),
+ "Array is correctly serialized");
+ done();
+ });
+ // Add a new url property sa the Class function used by
+ // Worker doesn't set enumerables to true for non-functions
+ worker._url = DEFAULT_CONTENT_URL;
+
+ worker.postMessage({ fun: function () {}, w: worker, array: array });
+ }
+);
+
+exports["test:emit-json-values-only"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ // Validate self.on and self.emit
+ self.port.on("addon-to-content", function (fun, w, obj, array) {
+ self.port.emit("content-to-addon", [
+ fun === null,
+ typeof w,
+ "port" in w,
+ w._url,
+ "fun" in obj,
+ Object.keys(obj.dom).length,
+ Array.isArray(array),
+ JSON.stringify(array)
+ ]);
+ });
+ }
+ });
+
+ // Validate worker.port
+ let array = [1, 2, 3];
+ worker.port.on("content-to-addon", function (result) {
+ assert.ok(result[0], "functions become null");
+ assert.equal(result[1], "object", "objects stay objects");
+ assert.ok(result[2], "object's attributes are enumerable");
+ assert.equal(result[3], DEFAULT_CONTENT_URL,
+ "json attribute is accessible");
+ assert.ok(!result[4], "function as object attribute is removed");
+ assert.equal(result[5], 0, "DOM nodes are converted into empty object");
+ // See bug 714891, Arrays may be broken over compartments:
+ assert.ok(result[6], "Array keeps being an array");
+ assert.equal(result[7], JSON.stringify(array),
+ "Array is correctly serialized");
+ done();
+ });
+
+ let obj = {
+ fun: function () {},
+ dom: browser.contentWindow.document.createElement("div")
+ };
+ // Add a new url property sa the Class function used by
+ // Worker doesn't set enumerables to true for non-functions
+ worker._url = DEFAULT_CONTENT_URL;
+ worker.port.emit("addon-to-content", function () {}, worker, obj, array);
+ }
+);
+
+exports["test:content is wrapped"] = WorkerTest(
+ "data:text/html;charset=utf-8,<script>var documentValue=true;</script>",
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ self.postMessage(!window.documentValue);
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(msg) {
+ assert.ok(msg,
+ "content script has a wrapped access to content document");
+ done();
+ }
+ });
+ }
+);
+
+// ContentWorker is not for chrome
+/*
+exports["test:chrome is unwrapped"] = function(assert, done) {
+ let window = makeWindow();
+
+ listenOnce(window, "load", function onload() {
+
+ let worker = Worker({
+ window: window,
+ contentScript: "new " + function WorkerScope() {
+ self.postMessage(window.documentValue);
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(msg) {
+ assert.ok(msg,
+ "content script has an unwrapped access to chrome document");
+ close(window).then(done);
+ }
+ });
+
+ });
+}
+*/
+
+exports["test:nothing is leaked to content script"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ self.postMessage([
+ "ContentWorker" in window,
+ "UNWRAP_ACCESS_KEY" in window,
+ "getProxyForObject" in window
+ ]);
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(list) {
+ assert.ok(!list[0], "worker API contrustor isn't leaked");
+ assert.ok(!list[1], "Proxy API stuff isn't leaked 1/2");
+ assert.ok(!list[2], "Proxy API stuff isn't leaked 2/2");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:ensure console.xxx works in cs"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ const EXPECTED = ["time", "log", "info", "warn", "error", "error", "timeEnd"];
+
+ let calls = [];
+ let levels = [];
+
+ system.on('console-api-log-event', onMessage);
+
+ function onMessage({ subject }) {
+ calls.push(subject.wrappedJSObject.arguments[0]);
+ levels.push(subject.wrappedJSObject.level);
+ }
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ console.time("time");
+ console.log("log");
+ console.info("info");
+ console.warn("warn");
+ console.error("error");
+ console.debug("debug");
+ console.exception("error");
+ console.timeEnd("timeEnd");
+ self.postMessage();
+ },
+ onMessage: function() {
+ system.off('console-api-log-event', onMessage);
+
+ assert.equal(JSON.stringify(calls),
+ JSON.stringify(EXPECTED),
+ "console methods have been called successfully, in expected order");
+
+ assert.equal(JSON.stringify(levels),
+ JSON.stringify(EXPECTED),
+ "console messages have correct log levels, in expected order");
+
+ done();
+ }
+ });
+ }
+);
+
+exports["test:setTimeout works with string argument"] = WorkerTest(
+ "data:text/html;charset=utf-8,<script>var docVal=5;</script>",
+ function(assert, browser, done) {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function ContentScriptScope() {
+ // must use "window.scVal" instead of "var csVal"
+ // since we are inside ContentScriptScope function.
+ // i'm NOT putting code-in-string inside code-in-string </YO DAWG>
+ window.csVal = 13;
+ setTimeout("self.postMessage([" +
+ "csVal, " +
+ "window.docVal, " +
+ "'ContentWorker' in window, " +
+ "'UNWRAP_ACCESS_KEY' in window, " +
+ "'getProxyForObject' in window, " +
+ "])", 1);
+ },
+ contentScriptWhen: "ready",
+ onMessage: function([csVal, docVal, chrome1, chrome2, chrome3]) {
+ // test timer code is executed in the correct context
+ assert.equal(csVal, 13, "accessing content-script values");
+ assert.notEqual(docVal, 5, "can't access document values (directly)");
+ assert.ok(!chrome1 && !chrome2 && !chrome3, "nothing is leaked from chrome");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:setInterval works with string argument"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let count = 0;
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "setInterval('self.postMessage(1)', 50)",
+ contentScriptWhen: "ready",
+ onMessage: function(one) {
+ count++;
+ assert.equal(one, 1, "got " + count + " message(s) from setInterval");
+ if (count >= 3) done();
+ }
+ });
+ }
+);
+
+exports["test:setInterval async Errors passed to .onError"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let count = 0;
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "setInterval(() => { throw Error('ubik') }, 50)",
+ contentScriptWhen: "ready",
+ onError: function(err) {
+ count++;
+ assert.equal(err.message, "ubik",
+ "error (correctly) propagated " + count + " time(s)");
+ if (count >= 3) done();
+ }
+ });
+ }
+);
+
+exports["test:setTimeout throws array, passed to .onError"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "setTimeout(function() { throw ['array', 42] }, 1)",
+ contentScriptWhen: "ready",
+ onError: function(arr) {
+ assert.ok(isArray(arr),
+ "the type of thrown/propagated object is array");
+ assert.ok(arr.length==2,
+ "the propagated thrown array is the right length");
+ assert.equal(arr[1], 42,
+ "element inside the thrown array correctly propagated");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:setTimeout string arg with SyntaxError to .onError"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "setTimeout('syntax 123 error', 1)",
+ contentScriptWhen: "ready",
+ onError: function(err) {
+ assert.equal(err.name, "SyntaxError",
+ "received SyntaxError thrown from bad code in string argument to setTimeout");
+ assert.ok('fileName' in err,
+ "propagated SyntaxError contains a fileName property");
+ assert.ok('stack' in err,
+ "propagated SyntaxError contains a stack property");
+ assert.equal(err.message, "missing ; before statement",
+ "propagated SyntaxError has the correct (helpful) message");
+ assert.equal(err.lineNumber, 1,
+ "propagated SyntaxError was thrown on the right lineNumber");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:setTimeout can't be cancelled by content"] = WorkerTest(
+ "data:text/html;charset=utf-8,<script>var documentValue=true;</script>",
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ let id = setTimeout(function () {
+ self.postMessage("timeout");
+ }, 100);
+ unsafeWindow.eval("clearTimeout("+id+");");
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(msg) {
+ assert.ok(msg,
+ "content didn't managed to cancel our setTimeout");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:clearTimeout"] = WorkerTest(
+ "data:text/html;charset=utf-8,clear timeout",
+ function(assert, browser, done) {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ let id1 = setTimeout(function() {
+ self.postMessage("failed");
+ }, 10);
+ let id2 = setTimeout(function() {
+ self.postMessage("done");
+ }, 100);
+ clearTimeout(id1);
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(msg) {
+ if (msg === "failed") {
+ assert.fail("failed to cancel timer");
+ } else {
+ assert.pass("timer cancelled");
+ done();
+ }
+ }
+ });
+ }
+);
+
+exports["test:clearInterval"] = WorkerTest(
+ "data:text/html;charset=utf-8,clear timeout",
+ function(assert, browser, done) {
+ let called = 0;
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ let id = setInterval(function() {
+ self.postMessage("intreval")
+ clearInterval(id)
+ setTimeout(function() {
+ self.postMessage("done")
+ }, 100)
+ }, 10);
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(msg) {
+ if (msg === "intreval") {
+ called = called + 1;
+ if (called > 1) assert.fail("failed to cancel timer");
+ } else {
+ assert.pass("interval cancelled");
+ done();
+ }
+ }
+ });
+ }
+)
+
+exports["test:setTimeout are unregistered on content unload"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let originalWindow = browser.contentWindow;
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ document.title = "ok";
+ let i = 0;
+ setInterval(function () {
+ document.title = i++;
+ }, 10);
+ },
+ contentScriptWhen: "ready"
+ });
+
+ // Change location so that content script is destroyed,
+ // and all setTimeout/setInterval should be unregistered.
+ // Wait some cycles in order to execute some intervals.
+ setTimeout(function () {
+ // Bug 689621: Wait for the new document load so that we are sure that
+ // previous document cancelled its intervals
+ let url2 = "data:text/html;charset=utf-8,<title>final</title>";
+ loadAndWait(browser, url2, function onload() {
+ let titleAfterLoad = originalWindow.document.title;
+ // Wait additional cycles to verify that intervals are really cancelled
+ setTimeout(function () {
+ assert.equal(browser.contentDocument.title, "final",
+ "New document has not been modified");
+ assert.equal(originalWindow.document.title, titleAfterLoad,
+ "Nor previous one");
+
+ done();
+ }, 100);
+ });
+ }, 100);
+ }
+);
+
+exports['test:check window attribute in iframes'] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ // Create a first iframe and wait for its loading
+ let contentWin = browser.contentWindow;
+ let contentDoc = contentWin.document;
+ let iframe = contentDoc.createElement("iframe");
+ contentDoc.body.appendChild(iframe);
+
+ listenOnce(iframe, "load", function onload() {
+
+ // Create a second iframe inside the first one and wait for its loading
+ let iframeDoc = iframe.contentWindow.document;
+ let subIframe = iframeDoc.createElement("iframe");
+ iframeDoc.body.appendChild(subIframe);
+
+ listenOnce(subIframe, "load", function onload() {
+ subIframe.removeEventListener("load", onload, true);
+
+ // And finally create a worker against this second iframe
+ let worker = Worker({
+ window: subIframe.contentWindow,
+ contentScript: 'new ' + function WorkerScope() {
+ self.postMessage([
+ window.top !== window,
+ frameElement,
+ window.parent !== window,
+ top.location.href,
+ parent.location.href,
+ ]);
+ },
+ onMessage: function(msg) {
+ assert.ok(msg[0], "window.top != window");
+ assert.ok(msg[1], "window.frameElement is defined");
+ assert.ok(msg[2], "window.parent != window");
+ assert.equal(msg[3], contentWin.location.href,
+ "top.location refers to the toplevel content doc");
+ assert.equal(msg[4], iframe.contentWindow.location.href,
+ "parent.location refers to the first iframe doc");
+ done();
+ }
+ });
+
+ });
+ subIframe.setAttribute("src", "data:text/html;charset=utf-8,bar");
+
+ });
+ iframe.setAttribute("src", "data:text/html;charset=utf-8,foo");
+ }
+);
+
+exports['test:check window attribute in toplevel documents'] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: 'new ' + function WorkerScope() {
+ self.postMessage([
+ window.top === window,
+ frameElement,
+ window.parent === window
+ ]);
+ },
+ onMessage: function(msg) {
+ assert.ok(msg[0], "window.top == window");
+ assert.ok(!msg[1], "window.frameElement is null");
+ assert.ok(msg[2], "window.parent == window");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:check worker API with page history"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let url2 = "data:text/html;charset=utf-8,bar";
+
+ loadAndWait(browser, url2, function () {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ // Just before the content script is disable, we register a timeout
+ // that will be disable until the page gets visible again
+ self.on("pagehide", function () {
+ setTimeout(function () {
+ self.port.emit("timeout");
+ }, 0);
+ });
+
+ self.on("message", function() {
+ self.postMessage("saw message");
+ });
+
+ self.on("event", function() {
+ self.port.emit("event", "saw event");
+ });
+ },
+ contentScriptWhen: "start"
+ });
+
+ // postMessage works correctly when the page is visible
+ worker.postMessage("ok");
+
+ // We have to wait before going back into history,
+ // otherwise `goBack` won't do anything.
+ setTimeout(function () {
+ browser.goBack();
+ }, 0);
+
+ // Wait for the document to be hidden
+ browser.addEventListener("pagehide", function onpagehide() {
+ browser.removeEventListener("pagehide", onpagehide, false);
+ // Now any event sent to this worker should be cached
+
+ worker.postMessage("message");
+ worker.port.emit("event");
+
+ // Display the page with attached content script back in order to resume
+ // its timeout and receive the expected message.
+ // We have to delay this in order to not break the history.
+ // We delay for a non-zero amount of time in order to ensure that we
+ // do not receive the message immediatly, so that the timeout is
+ // actually disabled
+ setTimeout(function () {
+ worker.on("pageshow", function() {
+ let promise = Promise.all([
+ new Promise(resolve => {
+ worker.port.on("event", () => {
+ assert.pass("Saw event");
+ resolve();
+ });
+ }),
+ new Promise(resolve => {
+ worker.on("message", () => {
+ assert.pass("Saw message");
+ resolve();
+ });
+ }),
+ new Promise(resolve => {
+ worker.port.on("timeout", () => {
+ assert.pass("Timer fired");
+ resolve();
+ });
+ })
+ ]);
+ promise.then(done);
+ });
+
+ browser.goForward();
+ }, 500);
+
+ }, false);
+ });
+
+ }
+);
+
+exports['test:conentScriptFile as URL instance'] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let url = new URL(fixtures.url("test-contentScriptFile.js"));
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScriptFile: url,
+ onMessage: function(msg) {
+ assert.equal(msg, "msg from contentScriptFile",
+ "received a wrong message from contentScriptFile");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:worker events"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function (assert, browser, done) {
+ let window = browser.contentWindow;
+ let events = [];
+ let worker = Worker({
+ window: window,
+ contentScript: 'new ' + function WorkerScope() {
+ self.postMessage('start');
+ },
+ onAttach: win => {
+ events.push('attach');
+ assert.pass('attach event called when attached');
+ assert.equal(window, win, 'attach event passes in attached window');
+ },
+ onError: err => {
+ assert.equal(err.message, 'Custom',
+ 'Error passed into error event');
+ worker.detach();
+ },
+ onMessage: msg => {
+ assert.pass('`onMessage` handles postMessage')
+ throw new Error('Custom');
+ },
+ onDetach: _ => {
+ assert.pass('`onDetach` called when worker detached');
+ done();
+ }
+ });
+ // `attach` event is called synchronously during instantiation,
+ // so we can't listen to that, TODO FIX?
+ // worker.on('attach', obj => console.log('attach', obj));
+ }
+);
+
+exports["test:onDetach in contentScript on destroy"] = WorkerTest(
+ "data:text/html;charset=utf-8,foo#detach",
+ function(assert, browser, done) {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: 'new ' + function WorkerScope() {
+ self.port.on('detach', function(reason) {
+ window.location.hash += '!' + reason;
+ })
+ },
+ });
+ browser.contentWindow.addEventListener('hashchange', _ => {
+ assert.equal(browser.contentWindow.location.hash, '#detach!',
+ "location.href is as expected");
+ done();
+ })
+ worker.destroy();
+ }
+);
+
+exports["test:onDetach in contentScript on unload"] = WorkerTest(
+ "data:text/html;charset=utf-8,foo#detach",
+ function(assert, browser, done) {
+ let { loader } = LoaderWithHookedConsole(module);
+ let worker = loader.require("sdk/content/worker").Worker({
+ window: browser.contentWindow,
+ contentScript: 'new ' + function WorkerScope() {
+ self.port.on('detach', function(reason) {
+ window.location.hash += '!' + reason;
+ })
+ },
+ });
+ browser.contentWindow.addEventListener('hashchange', _ => {
+ assert.equal(browser.contentWindow.location.hash, '#detach!shutdown',
+ "location.href is as expected");
+ done();
+ })
+ loader.unload('shutdown');
+ }
+);
+
+exports["test:console method log functions properly"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let logs = [];
+
+ system.on('console-api-log-event', onMessage);
+
+ function onMessage({ subject }) {
+ logs.push(clean(subject.wrappedJSObject.arguments[0]));
+ }
+
+ let clean = message =>
+ message.trim().
+ replace(/[\r\n]/g, " ").
+ replace(/ +/g, " ");
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ console.log(Function);
+ console.log((foo) => foo * foo);
+ console.log(function foo(bar) { return bar + bar });
+
+ self.postMessage();
+ },
+ onMessage: () => {
+ system.off('console-api-log-event', onMessage);
+
+ assert.deepEqual(logs, [
+ "function Function() { [native code] }",
+ "(foo) => foo * foo",
+ "function foo(bar) { \"use strict\"; return bar + bar }"
+ ]);
+
+ done();
+ }
+ });
+ }
+);
+
+exports["test:global postMessage"] = WorkerTest(
+ WINDOW_SCRIPT_URL,
+ function(assert, browser, done) {
+ let contentScript = "window.addEventListener('message', function (e) {" +
+ " if (e.data === 'from -> window')" +
+ " self.port.emit('response', e.data, e.origin);" +
+ "});" +
+ "postMessage('from -> content-script', '*');";
+ let { loader } = LoaderWithHookedConsole(module);
+ let worker = loader.require("sdk/content/worker").Worker({
+ window: browser.contentWindow,
+ contentScriptWhen: "ready",
+ contentScript: contentScript
+ });
+
+ worker.port.on("response", (data, origin) => {
+ assert.equal(data, "from -> window", "Communication from content-script to window completed");
+ done();
+ });
+});
+
+exports["test:destroy unbinds listeners from port"] = WorkerTest(
+ "data:text/html;charset=utf-8,portdestroyer",
+ function(assert, browser, done) {
+ let destroyed = false;
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ self.port.emit("destroy");
+ setInterval(self.port.emit, 10, "ping");
+ },
+ onDestroy: done
+ });
+ worker.port.on("ping", () => {
+ if (destroyed) {
+ assert.fail("Should not call events on port after destroy.");
+ }
+ });
+ worker.port.on("destroy", () => {
+ destroyed = true;
+ worker.destroy();
+ assert.pass("Worker destroyed, waiting for no future listeners handling events.");
+ setTimeout(done, 500);
+ });
+ }
+);
+
+
+exports["test:destroy kills child worker"] = WorkerTest(
+ "data:text/html;charset=utf-8,<html><body><p id='detail'></p></body></html>",
+ function(assert, browser, done) {
+ let worker1 = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ self.port.on("ping", detail => {
+ let event = document.createEvent("CustomEvent");
+ event.initCustomEvent("Test:Ping", true, true, detail);
+ document.dispatchEvent(event);
+ self.port.emit("pingsent");
+ });
+
+ let listener = function(event) {
+ self.port.emit("pong", event.detail);
+ };
+
+ self.port.on("detach", () => {
+ window.removeEventListener("Test:Pong", listener);
+ });
+ window.addEventListener("Test:Pong", listener);
+ },
+ onAttach: function() {
+ let worker2 = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ let listener = function(event) {
+ let newEvent = document.createEvent("CustomEvent");
+ newEvent.initCustomEvent("Test:Pong", true, true, event.detail);
+ document.dispatchEvent(newEvent);
+ };
+ self.port.on("detach", () => {
+ window.removeEventListener("Test:Ping", listener);
+ })
+ window.addEventListener("Test:Ping", listener);
+ self.postMessage();
+ },
+ onMessage: function() {
+ worker1.port.emit("ping", "test1");
+ worker1.port.once("pong", detail => {
+ assert.equal(detail, "test1", "Saw the right message");
+ worker1.port.once("pingsent", () => {
+ assert.pass("The message was sent");
+
+ worker2.destroy();
+
+ worker1.port.emit("ping", "test2");
+ worker1.port.once("pong", detail => {
+ assert.fail("worker2 shouldn't have responded");
+ })
+ worker1.port.once("pingsent", () => {
+ assert.pass("The message was sent");
+ worker1.destroy();
+ done();
+ });
+ });
+ })
+ }
+ });
+ }
+ });
+ }
+);
+
+exports["test:unload kills child worker"] = WorkerTest(
+ "data:text/html;charset=utf-8,<html><body><p id='detail'></p></body></html>",
+ function(assert, browser, done) {
+ let loader = Loader(module);
+ let worker1 = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ self.port.on("ping", detail => {
+ let event = document.createEvent("CustomEvent");
+ event.initCustomEvent("Test:Ping", true, true, detail);
+ document.dispatchEvent(event);
+ self.port.emit("pingsent");
+ });
+
+ let listener = function(event) {
+ self.port.emit("pong", event.detail);
+ };
+
+ self.port.on("detach", () => {
+ window.removeEventListener("Test:Pong", listener);
+ });
+ window.addEventListener("Test:Pong", listener);
+ },
+ onAttach: function() {
+ let worker2 = loader.require("sdk/content/worker").Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ let listener = function(event) {
+ let newEvent = document.createEvent("CustomEvent");
+ newEvent.initCustomEvent("Test:Pong", true, true, event.detail);
+ document.dispatchEvent(newEvent);
+ };
+ self.port.on("detach", () => {
+ window.removeEventListener("Test:Ping", listener);
+ })
+ window.addEventListener("Test:Ping", listener);
+ self.postMessage();
+ },
+ onMessage: function() {
+ worker1.port.emit("ping", "test1");
+ worker1.port.once("pong", detail => {
+ assert.equal(detail, "test1", "Saw the right message");
+ worker1.port.once("pingsent", () => {
+ assert.pass("The message was sent");
+
+ loader.unload();
+
+ worker1.port.emit("ping", "test2");
+ worker1.port.once("pong", detail => {
+ assert.fail("worker2 shouldn't have responded");
+ })
+ worker1.port.once("pingsent", () => {
+ assert.pass("The message was sent");
+ worker1.destroy();
+ done();
+ });
+ });
+ })
+ }
+ });
+ }
+ });
+ }
+);
+
+// require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/addons/e10s-content/lib/test-page-worker.js b/addon-sdk/source/test/addons/e10s-content/lib/test-page-worker.js
new file mode 100644
index 000000000..3a9106ec0
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-content/lib/test-page-worker.js
@@ -0,0 +1,524 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Loader } = require('sdk/test/loader');
+const { Page } = require("sdk/page-worker");
+const { URL } = require("sdk/url");
+const fixtures = require("./fixtures");
+const testURI = fixtures.url("test.html");
+
+const ERR_DESTROYED =
+ "Couldn't find the worker to receive this message. " +
+ "The script may not be initialized yet, or may already have been unloaded.";
+
+const Isolate = fn => "(" + fn + ")()";
+
+exports.testSimplePageCreation = function(assert, done) {
+ let page = new Page({
+ contentScript: "self.postMessage(window.location.href)",
+ contentScriptWhen: "end",
+ onMessage: function (message) {
+ assert.equal(message, "about:blank",
+ "Page Worker should start with a blank page by default");
+ assert.equal(this, page, "The 'this' object is the page itself.");
+ done();
+ }
+ });
+}
+
+/*
+ * Tests that we can't be tricked by document overloads as we have access
+ * to wrapped nodes
+ */
+exports.testWrappedDOM = function(assert, done) {
+ let page = Page({
+ allow: { script: true },
+ contentURL: "data:text/html;charset=utf-8,<script>document.getElementById=3;window.scrollTo=3;</script>",
+ contentScript: 'new ' + function() {
+ function send() {
+ self.postMessage([typeof(document.getElementById), typeof(window.scrollTo)]);
+ }
+ if (document.readyState !== 'complete')
+ window.addEventListener('load', send, true)
+ else
+ send();
+ },
+ onMessage: function (message) {
+ assert.equal(message[0],
+ "function",
+ "getElementById from content script is the native one");
+
+ assert.equal(message[1],
+ "function",
+ "scrollTo from content script is the native one");
+
+ done();
+ }
+ });
+}
+
+/*
+// We do not offer unwrapped access to DOM since bug 601295 landed
+// See 660780 to track progress of unwrap feature
+exports.testUnwrappedDOM = function(assert, done) {
+ let page = Page({
+ allow: { script: true },
+ contentURL: "data:text/html;charset=utf-8,<script>document.getElementById=3;window.scrollTo=3;</script>",
+ contentScript: "window.addEventListener('load', function () { " +
+ "return self.postMessage([typeof(unsafeWindow.document.getElementById), " +
+ "typeof(unsafeWindow.scrollTo)]); }, true)",
+ onMessage: function (message) {
+ assert.equal(message[0],
+ "number",
+ "document inside page is free to be changed");
+
+ assert.equal(message[1],
+ "number",
+ "window inside page is free to be changed");
+
+ done();
+ }
+ });
+}
+*/
+
+exports.testPageProperties = function(assert) {
+ let page = new Page();
+
+ for (let prop of ['contentURL', 'allow', 'contentScriptFile',
+ 'contentScript', 'contentScriptWhen', 'on',
+ 'postMessage', 'removeListener']) {
+ assert.ok(prop in page, prop + " property is defined on page.");
+ }
+
+ assert.ok(() => page.postMessage("foo") || true,
+ "postMessage doesn't throw exception on page.");
+}
+
+exports.testConstructorAndDestructor = function(assert, done) {
+ let loader = Loader(module);
+ let { Page } = loader.require("sdk/page-worker");
+ let global = loader.sandbox("sdk/page-worker");
+
+ let pagesReady = 0;
+
+ let page1 = Page({
+ contentScript: "self.postMessage('')",
+ contentScriptWhen: "end",
+ onMessage: pageReady
+ });
+ let page2 = Page({
+ contentScript: "self.postMessage('')",
+ contentScriptWhen: "end",
+ onMessage: pageReady
+ });
+
+ assert.notEqual(page1, page2,
+ "Page 1 and page 2 should be different objects.");
+
+ function pageReady() {
+ if (++pagesReady == 2) {
+ page1.destroy();
+ page2.destroy();
+
+ assert.ok(isDestroyed(page1), "page1 correctly unloaded.");
+ assert.ok(isDestroyed(page2), "page2 correctly unloaded.");
+
+ loader.unload();
+ done();
+ }
+ }
+}
+
+exports.testAutoDestructor = function(assert, done) {
+ let loader = Loader(module);
+ let { Page } = loader.require("sdk/page-worker");
+
+ let page = Page({
+ contentScript: "self.postMessage('')",
+ contentScriptWhen: "end",
+ onMessage: function() {
+ loader.unload();
+ assert.ok(isDestroyed(page), "Page correctly unloaded.");
+ done();
+ }
+ });
+}
+
+exports.testValidateOptions = function(assert) {
+ assert.throws(
+ () => Page({ contentURL: 'home' }),
+ /The `contentURL` option must be a valid URL\./,
+ "Validation correctly denied a non-URL contentURL"
+ );
+
+ assert.throws(
+ () => Page({ onMessage: "This is not a function."}),
+ /The option "onMessage" must be one of the following types: function/,
+ "Validation correctly denied a non-function onMessage."
+ );
+
+ assert.pass("Options validation is working.");
+}
+
+exports.testContentAndAllowGettersAndSetters = function(assert, done) {
+ let content = "data:text/html;charset=utf-8,<script>window.localStorage.allowScript=3;</script>";
+
+ // Load up the page with testURI initially for the resource:// principal,
+ // then load the actual data:* content, as data:* URIs no longer
+ // have localStorage
+ let page = Page({
+ contentURL: testURI,
+ contentScript: "if (window.location.href==='"+testURI+"')" +
+ " self.postMessage('reload');" +
+ "else " +
+ " self.postMessage(window.localStorage.allowScript)",
+ contentScriptWhen: "end",
+ onMessage: step0
+ });
+
+ function step0(message) {
+ if (message === 'reload')
+ return page.contentURL = content;
+ assert.equal(message, "3",
+ "Correct value expected for allowScript - 3");
+ assert.equal(page.contentURL, content,
+ "Correct content expected");
+ page.removeListener('message', step0);
+ page.on('message', step1);
+ page.allow = { script: false };
+ page.contentURL = content =
+ "data:text/html;charset=utf-8,<script>window.localStorage.allowScript='f'</script>";
+ }
+
+ function step1(message) {
+ assert.equal(message, "3",
+ "Correct value expected for allowScript - 3");
+ assert.equal(page.contentURL, content, "Correct content expected");
+ page.removeListener('message', step1);
+ page.on('message', step2);
+ page.allow = { script: true };
+ page.contentURL = content =
+ "data:text/html;charset=utf-8,<script>window.localStorage.allowScript='g'</script>";
+ }
+
+ function step2(message) {
+ assert.equal(message, "g",
+ "Correct value expected for allowScript - g");
+ assert.equal(page.contentURL, content, "Correct content expected");
+ page.removeListener('message', step2);
+ page.on('message', step3);
+ page.allow.script = false;
+ page.contentURL = content =
+ "data:text/html;charset=utf-8,<script>window.localStorage.allowScript=3</script>";
+ }
+
+ function step3(message) {
+ assert.equal(message, "g",
+ "Correct value expected for allowScript - g");
+ assert.equal(page.contentURL, content, "Correct content expected");
+ page.removeListener('message', step3);
+ page.on('message', step4);
+ page.allow.script = true;
+ page.contentURL = content =
+ "data:text/html;charset=utf-8,<script>window.localStorage.allowScript=4</script>";
+ }
+
+ function step4(message) {
+ assert.equal(message, "4",
+ "Correct value expected for allowScript - 4");
+ assert.equal(page.contentURL, content, "Correct content expected");
+ done();
+ }
+
+}
+
+exports.testOnMessageCallback = function(assert, done) {
+ Page({
+ contentScript: "self.postMessage('')",
+ contentScriptWhen: "end",
+ onMessage: function() {
+ assert.pass("onMessage callback called");
+ done();
+ }
+ });
+}
+
+exports.testMultipleOnMessageCallbacks = function(assert, done) {
+ let count = 0;
+ let page = Page({
+ contentScript: "self.postMessage('')",
+ contentScriptWhen: "end",
+ onMessage: () => count += 1
+ });
+ page.on('message', () => count += 2);
+ page.on('message', () => count *= 3);
+ page.on('message', () =>
+ assert.equal(count, 9, "All callbacks were called, in order."));
+ page.on('message', done);
+};
+
+exports.testLoadContentPage = function(assert, done) {
+ let page = Page({
+ onMessage: function(message) {
+ // The message is an array whose first item is the test method to call
+ // and the rest of whose items are arguments to pass it.
+ let msg = message.shift();
+ if (msg == "done")
+ return done();
+ assert[msg].apply(assert, message);
+ },
+ contentURL: fixtures.url("test-page-worker.html"),
+ contentScriptFile: fixtures.url("test-page-worker.js"),
+ contentScriptWhen: "ready"
+ });
+}
+
+exports.testLoadContentPageRelativePath = function(assert, done) {
+ let page = require("sdk/page-worker").Page({
+ onMessage: function(message) {
+ // The message is an array whose first item is the test method to call
+ // and the rest of whose items are arguments to pass it.
+ let msg = message.shift();
+ if (msg == "done")
+ return done();
+ assert[msg].apply(assert, message);
+ },
+ contentURL: "./test-page-worker.html",
+ contentScriptFile: "./test-page-worker.js",
+ contentScriptWhen: "ready"
+ });
+}
+
+exports.testAllowScriptDefault = function(assert, done) {
+ let page = Page({
+ onMessage: function(message) {
+ assert.ok(message, "Script is allowed to run by default.");
+ done();
+ },
+ contentURL: "data:text/html;charset=utf-8,<script>document.documentElement.setAttribute('foo', 3);</script>",
+ contentScript: "self.postMessage(document.documentElement.getAttribute('foo'))",
+ contentScriptWhen: "ready"
+ });
+}
+
+exports.testAllowScript = function(assert, done) {
+ let page = Page({
+ onMessage: function(message) {
+ assert.ok(message, "Script runs when allowed to do so.");
+ done();
+ },
+ allow: { script: true },
+ contentURL: "data:text/html;charset=utf-8,<script>document.documentElement.setAttribute('foo', 3);</script>",
+ contentScript: "self.postMessage(document.documentElement.hasAttribute('foo') && " +
+ " document.documentElement.getAttribute('foo') == 3)",
+ contentScriptWhen: "ready"
+ });
+}
+
+exports.testPingPong = function(assert, done) {
+ let page = Page({
+ contentURL: 'data:text/html;charset=utf-8,ping-pong',
+ contentScript: 'self.on("message", function(message) { return self.postMessage("pong"); });'
+ + 'self.postMessage("ready");',
+ onMessage: function(message) {
+ if ('ready' == message) {
+ page.postMessage('ping');
+ }
+ else {
+ assert.ok(message, 'pong', 'Callback from contentScript');
+ done();
+ }
+ }
+ });
+};
+
+exports.testRedirect = function (assert, done) {
+ let page = Page({
+ contentURL: 'data:text/html;charset=utf-8,first-page',
+ contentScriptWhen: "end",
+ contentScript: '' +
+ 'if (/first-page/.test(document.location.href)) ' +
+ ' document.location.href = "data:text/html;charset=utf-8,redirect";' +
+ 'else ' +
+ ' self.port.emit("redirect", document.location.href);'
+ });
+
+ page.port.on('redirect', function (url) {
+ assert.equal(url, 'data:text/html;charset=utf-8,redirect', 'Reinjects contentScript on reload');
+ done();
+ });
+};
+
+exports.testRedirectIncludeArrays = function (assert, done) {
+ let firstURL = 'data:text/html;charset=utf-8,first-page';
+ let page = Page({
+ contentURL: firstURL,
+ contentScript: '(function () {' +
+ 'self.port.emit("load", document.location.href);' +
+ ' self.port.on("redirect", function (url) {' +
+ ' document.location.href = url;' +
+ ' })' +
+ '})();',
+ include: ['about:blank', 'data:*']
+ });
+
+ page.port.on('load', function (url) {
+ if (url === firstURL) {
+ page.port.emit('redirect', 'about:blank');
+ } else if (url === 'about:blank') {
+ page.port.emit('redirect', 'about:mozilla');
+ assert.ok('`include` property handles arrays');
+ assert.equal(url, 'about:blank', 'Redirects work with accepted domains');
+ done();
+ } else if (url === 'about:mozilla') {
+ assert.fail('Should not redirect to restricted domain');
+ }
+ });
+};
+
+exports.testRedirectFromWorker = function (assert, done) {
+ let firstURL = 'data:text/html;charset=utf-8,first-page';
+ let secondURL = 'data:text/html;charset=utf-8,second-page';
+ let thirdURL = 'data:text/html;charset=utf-8,third-page';
+ let page = Page({
+ contentURL: firstURL,
+ contentScript: '(function () {' +
+ 'self.port.emit("load", document.location.href);' +
+ ' self.port.on("redirect", function (url) {' +
+ ' document.location.href = url;' +
+ ' })' +
+ '})();',
+ include: 'data:*'
+ });
+
+ page.port.on('load', function (url) {
+ if (url === firstURL) {
+ page.port.emit('redirect', secondURL);
+ } else if (url === secondURL) {
+ page.port.emit('redirect', thirdURL);
+ } else if (url === thirdURL) {
+ page.port.emit('redirect', 'about:mozilla');
+ assert.equal(url, thirdURL, 'Redirects work with accepted domains on include strings');
+ done();
+ } else {
+ assert.fail('Should not redirect to unauthorized domains');
+ }
+ });
+};
+
+exports.testRedirectWithContentURL = function (assert, done) {
+ let firstURL = 'data:text/html;charset=utf-8,first-page';
+ let secondURL = 'data:text/html;charset=utf-8,second-page';
+ let thirdURL = 'data:text/html;charset=utf-8,third-page';
+ let page = Page({
+ contentURL: firstURL,
+ contentScript: '(function () {' +
+ 'self.port.emit("load", document.location.href);' +
+ '})();',
+ include: 'data:*'
+ });
+
+ page.port.on('load', function (url) {
+ if (url === firstURL) {
+ page.contentURL = secondURL;
+ } else if (url === secondURL) {
+ page.contentURL = thirdURL;
+ } else if (url === thirdURL) {
+ page.contentURL = 'about:mozilla';
+ assert.equal(url, thirdURL, 'Redirects work with accepted domains on include strings');
+ done();
+ } else {
+ assert.fail('Should not redirect to unauthorized domains');
+ }
+ });
+};
+
+
+exports.testMultipleDestroys = function(assert) {
+ let page = Page();
+ page.destroy();
+ page.destroy();
+ assert.pass("Multiple destroys should not cause an error");
+};
+
+exports.testContentScriptOptionsOption = function(assert, done) {
+ let page = new Page({
+ contentScript: "self.postMessage( [typeof self.options.d, self.options] );",
+ contentScriptWhen: "end",
+ contentScriptOptions: {a: true, b: [1,2,3], c: "string", d: function(){ return 'test'}},
+ onMessage: function(msg) {
+ assert.equal(msg[0], 'undefined', 'functions are stripped from contentScriptOptions');
+ assert.equal(typeof msg[1], 'object', 'object as contentScriptOptions');
+ assert.equal(msg[1].a, true, 'boolean in contentScriptOptions');
+ assert.equal(msg[1].b.join(), '1,2,3', 'array and numbers in contentScriptOptions');
+ assert.equal(msg[1].c, 'string', 'string in contentScriptOptions');
+ done();
+ }
+ });
+};
+
+exports.testMessageQueue = function (assert, done) {
+ let page = new Page({
+ contentScript: 'self.on("message", function (m) {' +
+ 'self.postMessage(m);' +
+ '});',
+ contentURL: 'data:text/html;charset=utf-8,',
+ });
+ page.postMessage('ping');
+ page.on('message', function (m) {
+ assert.equal(m, 'ping', 'postMessage should queue messages');
+ done();
+ });
+};
+
+exports.testWindowStopDontBreak = function (assert, done) {
+ const { Ci, Cc } = require('chrome');
+ const consoleService = Cc['@mozilla.org/consoleservice;1'].
+ getService(Ci.nsIConsoleService);
+ const listener = {
+ observe: ({message}) => {
+ if (message.includes('contentWorker is null'))
+ assert.fail('contentWorker is null');
+ }
+ };
+ consoleService.registerListener(listener)
+
+ let page = new Page({
+ contentURL: 'data:text/html;charset=utf-8,testWindowStopDontBreak',
+ contentScriptWhen: 'ready',
+ contentScript: Isolate(() => {
+ window.stop();
+ self.port.on('ping', () => self.port.emit('pong'));
+ })
+ });
+
+ page.port.on('pong', () => {
+ assert.pass('page-worker works after window.stop');
+ page.destroy();
+ consoleService.unregisterListener(listener);
+ done();
+ });
+
+ page.port.emit("ping");
+};
+
+
+function isDestroyed(page) {
+ try {
+ page.postMessage("foo");
+ }
+ catch (err) {
+ if (err.message == ERR_DESTROYED) {
+ return true;
+ }
+ else {
+ throw err;
+ }
+ }
+ return false;
+}
+
+// require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/addons/e10s-content/package.json b/addon-sdk/source/test/addons/e10s-content/package.json
new file mode 100644
index 000000000..c7cf6847a
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-content/package.json
@@ -0,0 +1,11 @@
+{
+ "name": "e10s-content",
+ "title": "e10s-content",
+ "id": "jid1-ZZaXFHAPlHDDD@jetpack",
+ "description": "run content worker tests in e10s mode",
+ "author": "Tomislav Jovanovic",
+ "license": "MPL-2.0",
+ "version": "0.1.0",
+ "main": "./lib/main.js",
+ "e10s": true
+}
diff --git a/addon-sdk/source/test/addons/e10s-l10n/data/test-localization.html b/addon-sdk/source/test/addons/e10s-l10n/data/test-localization.html
new file mode 100644
index 000000000..5646946da
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-l10n/data/test-localization.html
@@ -0,0 +1,29 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<html>
+ <head>
+ <meta charset="UTF-8">
+ <title>HTML Localization</title>
+ </head>
+ <body>
+ <div data-l10n-id="Not translated">Kept as-is</div>
+ <ul data-l10n-id="Translated">
+ <li>Inner html content is replaced,</li>
+ <li data-l10n-id="text-content">
+ Elements with data-l10n-id attribute whose parent element is translated
+ will be replaced by the content of the translation.
+ </li>
+ </ul>
+ <div data-l10n-id="text-content">No</div>
+ <div data-l10n-id="Translated">
+ A data-l10n-id value can be used in multiple elements
+ </div>
+ <a data-l10n-id="link-attributes" title="Certain whitelisted attributes get translated too" alt="No" accesskey="A"></a>
+ <input data-l10n-id="input" type="text" placeholder="Form placeholders are translateable">
+ <menu>
+ <menuitem data-l10n-id="contextitem" label="Labels of select options and context menus are translateable">
+ </menu>
+ </body>
+</html
diff --git a/addon-sdk/source/test/addons/e10s-l10n/locale/en.properties b/addon-sdk/source/test/addons/e10s-l10n/locale/en.properties
new file mode 100644
index 000000000..c9e53ecb3
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-l10n/locale/en.properties
@@ -0,0 +1,38 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+Translated= Yes
+
+text-content=no <b>HTML</b> injection
+
+downloadsCount=%d downloads
+downloadsCount[one]=one download
+
+pluralTest=fallback to other
+pluralTest[zero]=optional zero form
+
+explicitPlural[one]=one
+explicitPlural[other]=other
+
+# You can use unicode char escaping in order to inject space at the beginning/
+# end of your string. (Regular spaces are automatically ignore by .properties
+# file parser)
+unicodeEscape = \u0020\u0040\u0020
+# this string equals to " @ "
+
+# bug 1033309 plurals with multiple placeholders
+first_identifier[one]=first entry is %s and the second one is %s.
+first_identifier=the entries are %s and %s.
+second_identifier[other]=first entry is %s and the second one is %s.
+third_identifier=first entry is %s and the second one is %s.
+
+# bug 824489 allow translation of element attributes
+link-attributes.title=Yes
+link-attributes.alt=Yes
+link-attributes.accesskey=B
+input.placeholder=Yes
+contextitem.label=Yes
+link-attributes.ariaLabel=Yes
+link-attributes.ariaValueText=Value
+link-attributes.ariaMozHint=Hint
diff --git a/addon-sdk/source/test/addons/e10s-l10n/locale/eo.properties b/addon-sdk/source/test/addons/e10s-l10n/locale/eo.properties
new file mode 100644
index 000000000..a979fca1a
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-l10n/locale/eo.properties
@@ -0,0 +1,5 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+Translated= jes
diff --git a/addon-sdk/source/test/addons/e10s-l10n/locale/fr-FR.properties b/addon-sdk/source/test/addons/e10s-l10n/locale/fr-FR.properties
new file mode 100644
index 000000000..2c5ffbb17
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-l10n/locale/fr-FR.properties
@@ -0,0 +1,14 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+Translated= Oui
+
+placeholderString= Placeholder %s
+
+# Plural forms
+%d downloads=%d téléchargements
+%d downloads[one]=%d téléchargement
+
+downloadsCount=%d téléchargements
+downloadsCount[one]=%d téléchargement
diff --git a/addon-sdk/source/test/addons/e10s-l10n/main.js b/addon-sdk/source/test/addons/e10s-l10n/main.js
new file mode 100644
index 000000000..e12cab87d
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-l10n/main.js
@@ -0,0 +1,289 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const prefs = require("sdk/preferences/service");
+const { Loader } = require('sdk/test/loader');
+const { resolveURI } = require('toolkit/loader');
+const { rootURI, isNative } = require("@loader/options");
+const { usingJSON } = require('sdk/l10n/json/core');
+
+const PREF_MATCH_OS_LOCALE = "intl.locale.matchOS";
+const PREF_SELECTED_LOCALE = "general.useragent.locale";
+
+function setLocale(locale) {
+ prefs.set(PREF_MATCH_OS_LOCALE, false);
+ prefs.set(PREF_SELECTED_LOCALE, locale);
+}
+
+function resetLocale() {
+ prefs.reset(PREF_MATCH_OS_LOCALE);
+ prefs.reset(PREF_SELECTED_LOCALE);
+}
+
+function definePseudo(loader, id, exports) {
+ let uri = resolveURI(id, loader.mapping);
+ loader.modules[uri] = { exports: exports };
+}
+
+function createTest(locale, testFunction) {
+ return function (assert, done) {
+ let loader = Loader(module);
+ // Change the locale before loading new l10n modules in order to load
+ // the right .json file
+ setLocale(locale);
+ // Initialize main l10n module in order to load new locale files
+ loader.require("sdk/l10n/loader").
+ load(rootURI).
+ then(null, function failure(error) {
+ if (!isNative)
+ assert.fail("Unable to load locales: " + error);
+ }).
+ then(function success(data) {
+ definePseudo(loader, '@l10n/data', data ? data : null);
+ // Execute the given test function
+ try {
+ testFunction(assert, loader, function onDone() {
+ loader.unload();
+ resetLocale();
+ done();
+ });
+ }
+ catch(e) {
+ console.exception(e);
+ }
+ },
+ function failure(error) {
+ assert.fail("Unable to load locales: " + error);
+ });
+ };
+}
+
+exports.testExactMatching = createTest("fr-FR", function(assert, loader, done) {
+ let _ = loader.require("sdk/l10n").get;
+ assert.equal(_("Not translated"), "Not translated",
+ "Key not translated");
+ assert.equal(_("Translated"), "Oui",
+ "Simple key translated");
+
+ // Placeholders
+ assert.equal(_("placeholderString", "works"), "Placeholder works",
+ "Value with placeholder");
+ assert.equal(_("Placeholder %s", "works"), "Placeholder works",
+ "Key without value but with placeholder");
+ assert.equal(_("Placeholders %2s %1s %s.", "working", "are", "correctly"),
+ "Placeholders are working correctly.",
+ "Multiple placeholders");
+
+ // Plurals
+ assert.equal(_("downloadsCount", 0),
+ "0 téléchargement",
+ "PluralForm form 'one' for 0 in french");
+ assert.equal(_("downloadsCount", 1),
+ "1 téléchargement",
+ "PluralForm form 'one' for 1 in french");
+ assert.equal(_("downloadsCount", 2),
+ "2 téléchargements",
+ "PluralForm form 'other' for n > 1 in french");
+
+ done();
+});
+
+exports.testHtmlLocalizationPageWorker = createTest("en-GB", function(assert, loader, done) {
+ // Ensure initing html component that watch document creations
+ // Note that this module is automatically initialized in
+ // cuddlefish.js:Loader.main in regular addons. But it isn't for unit tests.
+ let loaderHtmlL10n = loader.require("sdk/l10n/html");
+ loaderHtmlL10n.enable();
+
+ let uri = require("sdk/self").data.url("test-localization.html");
+ let worker = loader.require("sdk/page-worker").Page({
+ contentURL: uri,
+ contentScript: "new " + function ContentScriptScope() {
+ let nodes = document.body.querySelectorAll("*[data-l10n-id]");
+ self.postMessage([nodes[0].innerHTML,
+ nodes[1].innerHTML,
+ nodes[2].innerHTML,
+ nodes[3].innerHTML,
+ nodes[4].title,
+ nodes[4].getAttribute("alt"),
+ nodes[4].getAttribute("accesskey"),
+ nodes[4].getAttribute("aria-label"),
+ nodes[4].getAttribute("aria-valuetext"),
+ nodes[4].getAttribute("aria-moz-hint"),
+ nodes[5].placeholder,
+ nodes[6].label]);
+ },
+ onMessage: function (data) {
+ assert.equal(
+ data[0],
+ "Kept as-is",
+ "Nodes with unknown id in .properties are kept 'as-is'"
+ );
+ assert.equal(data[1], "Yes", "HTML is translated");
+ assert.equal(
+ data[2],
+ "no &lt;b&gt;HTML&lt;/b&gt; injection",
+ "Content from .properties is text content; HTML can't be injected."
+ );
+ assert.equal(data[3], "Yes", "Multiple elements with same data-l10n-id are accepted.");
+
+ // Attribute translation tests
+ assert.equal(data[4], "Yes", "Title attributes gets translated.");
+ assert.equal(data[5], "Yes", "Alt attributes gets translated.");
+ assert.equal(data[6], "B", "Accesskey gets translated.");
+
+ assert.equal(data[7], "Yes", "Aria-Label gets translated.");
+ assert.equal(data[8], "Value", "Aria-valuetext gets translated.");
+ assert.equal(data[9], "Hint", "Aria-moz-hint gets translated.");
+
+ assert.equal(data[10], "Yes", "Form placeholders are translateable.");
+
+ assert.equal(data[11], "Yes", "Labels of select options and context menus are translateable.");
+
+ done();
+ }
+ });
+});
+
+exports.testHtmlLocalization = createTest("en-GB", function(assert, loader, done) {
+ // Ensure initing html component that watch document creations
+ // Note that this module is automatically initialized in
+ // cuddlefish.js:Loader.main in regular addons. But it isn't for unit tests.
+ let loaderHtmlL10n = loader.require("sdk/l10n/html");
+ loaderHtmlL10n.enable();
+
+ let uri = require("sdk/self").data.url("test-localization.html");
+ loader.require("sdk/tabs").open({
+ url: uri,
+ onReady: function(tab) {
+ tab.attach({
+ contentURL: uri,
+ contentScript: "new " + function ContentScriptScope() {
+ let nodes = document.body.querySelectorAll("*[data-l10n-id]");
+ self.postMessage([nodes[0].innerHTML,
+ nodes[1].innerHTML,
+ nodes[2].innerHTML,
+ nodes[3].innerHTML,
+ nodes[4].title,
+ nodes[4].getAttribute("alt"),
+ nodes[4].getAttribute("accesskey"),
+ nodes[4].getAttribute("aria-label"),
+ nodes[4].getAttribute("aria-valuetext"),
+ nodes[4].getAttribute("aria-moz-hint"),
+ nodes[5].placeholder,
+ nodes[6].label]);
+ },
+ onMessage: function (data) {
+ assert.equal(
+ data[0],
+ "Kept as-is",
+ "Nodes with unknown id in .properties are kept 'as-is'"
+ );
+ assert.equal(data[1], "Yes", "HTML is translated");
+ assert.equal(
+ data[2],
+ "no &lt;b&gt;HTML&lt;/b&gt; injection",
+ "Content from .properties is text content; HTML can't be injected."
+ );
+ assert.equal(data[3], "Yes", "Multiple elements with same data-l10n-id are accepted.");
+
+ // Attribute translation tests
+ assert.equal(data[4], "Yes", "Title attributes gets translated.");
+ assert.equal(data[5], "Yes", "Alt attributes gets translated.");
+ assert.equal(data[6], "B", "Accesskey gets translated.");
+
+ assert.equal(data[7], "Yes", "Aria-Label gets translated.");
+ assert.equal(data[8], "Value", "Aria-valuetext gets translated.");
+ assert.equal(data[9], "Hint", "Aria-moz-hint gets translated.");
+
+ assert.equal(data[10], "Yes", "Form placeholders are translateable.");
+
+ assert.equal(data[11], "Yes", "Labels of select options and context menus are translateable.");
+
+ tab.close(done);
+ }
+ });
+ }
+ });
+});
+
+exports.testEnUsLocaleName = createTest("en-US", function(assert, loader, done) {
+ let _ = loader.require("sdk/l10n").get;
+
+ assert.equal(_("Not translated"), "Not translated",
+ "String w/o translation is kept as-is");
+ assert.equal(_("Translated"), "Yes",
+ "String with translation is correctly translated");
+
+ // Check Unicode char escaping sequences
+ assert.equal(_("unicodeEscape"), " @ ",
+ "Unicode escaped sequances are correctly converted");
+
+ // Check plural forms regular matching
+ assert.equal(_("downloadsCount", 0),
+ "0 downloads",
+ "PluralForm form 'other' for 0 in english");
+ assert.equal(_("downloadsCount", 1),
+ "one download",
+ "PluralForm form 'one' for 1 in english");
+ assert.equal(_("downloadsCount", 2),
+ "2 downloads",
+ "PluralForm form 'other' for n != 1 in english");
+
+ // Check optional plural forms
+ assert.equal(_("pluralTest", 0),
+ "optional zero form",
+ "PluralForm form 'zero' can be optionaly specified. (Isn't mandatory in english)");
+ assert.equal(_("pluralTest", 1),
+ "fallback to other",
+ "If the specific plural form is missing, we fallback to 'other'");
+
+ // Ensure that we can omit specifying the generic key without [other]
+ // key[one] = ...
+ // key[other] = ... # Instead of `key = ...`
+ assert.equal(_("explicitPlural", 1),
+ "one",
+ "PluralForm form can be omitting generic key [i.e. without ...[other] at end of key)");
+ assert.equal(_("explicitPlural", 10),
+ "other",
+ "PluralForm form can be omitting generic key [i.e. without ...[other] at end of key)");
+
+ assert.equal(_("first_identifier", "ONE", "TWO"), "the entries are ONE and TWO.", "first_identifier no count");
+ assert.equal(_("first_identifier", 0, "ONE", "TWO"), "the entries are ONE and TWO.", "first_identifier with count = 0");
+ assert.equal(_("first_identifier", 1, "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "first_identifier with count = 1");
+ assert.equal(_("first_identifier", 2, "ONE", "TWO"), "the entries are ONE and TWO.", "first_identifier with count = 2");
+
+ assert.equal(_("second_identifier", "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "second_identifier with no count");
+ assert.equal(_("second_identifier", 0, "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "second_identifier with count = 0");
+ assert.equal(_("second_identifier", 1, "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "second_identifier with count = 1");
+ assert.equal(_("second_identifier", 2, "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "second_identifier with count = 2");
+
+ assert.equal(_("third_identifier", "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "third_identifier with no count");
+ assert.equal(_("third_identifier", 0, "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "third_identifier with count = 0");
+ assert.equal(_("third_identifier", 2, "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "third_identifier with count = 2");
+
+ done();
+});
+
+exports.testUsingJSON = function(assert) {
+ assert.equal(usingJSON, !isNative, 'using json');
+}
+
+exports.testShortLocaleName = createTest("eo", function(assert, loader, done) {
+ let _ = loader.require("sdk/l10n").get;
+ assert.equal(_("Not translated"), "Not translated",
+ "String w/o translation is kept as-is");
+ assert.equal(_("Translated"), "jes",
+ "String with translation is correctly translated");
+
+ done();
+});
+
+
+// Before running tests, disable HTML service which is automatially enabled
+// in api-utils/addon/runner.js
+require('sdk/l10n/html').disable();
+
+require("sdk/test/runner").runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/e10s-l10n/package.json b/addon-sdk/source/test/addons/e10s-l10n/package.json
new file mode 100644
index 000000000..51591a86e
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-l10n/package.json
@@ -0,0 +1,5 @@
+{
+ "id": "e10s-l10n@jetpack",
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/e10s-remote/main.js b/addon-sdk/source/test/addons/e10s-remote/main.js
new file mode 100644
index 000000000..cea27af9b
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-remote/main.js
@@ -0,0 +1,578 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const LOCAL_URI = "about:robots";
+const REMOTE_URI = "data:text/html;charset=utf-8,remote";
+
+const { Cu } = require('chrome');
+const { Loader } = require('sdk/test/loader');
+const { getTabs, openTab, closeTab, setTabURL, getBrowserForTab, getURI } = require('sdk/tabs/utils');
+const { getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { cleanUI } = require("sdk/test/utils");
+const { setTimeout } = require("sdk/timers");
+const { promiseEvent, promiseDOMEvent, promiseEventOnItemAndContainer,
+ waitForProcesses, getChildFrameCount, isE10S } = require("./utils");
+const { after } = require('sdk/test/utils');
+const { processID } = require('sdk/system/runtime');
+
+const { set } = require('sdk/preferences/service');
+// The hidden preload browser messes up our frame counts
+set('browser.newtab.preload', false);
+
+function promiseTabFrameAttach(frames) {
+ return new Promise(resolve => {
+ let listener = function(frame, ...args) {
+ if (!frame.isTab)
+ return;
+ frames.off("attach", listener);
+ resolve([frame, ...args]);
+ }
+
+ frames.on("attach", listener);
+ });
+}
+
+// Check that we see a process stop and start
+exports["test process restart"] = function*(assert) {
+ if (!isE10S) {
+ assert.pass("Skipping test in non-e10s mode");
+ return;
+ }
+
+ let window = getMostRecentBrowserWindow();
+
+ let tabs = getTabs(window);
+ assert.equal(tabs.length, 1, "Should have just the one tab to start with");
+ let tab = tabs[0];
+ let browser = getBrowserForTab(tab);
+
+ let loader = new Loader(module);
+ let { processes, frames } = yield waitForProcesses(loader);
+
+ let remoteProcess = Array.filter(processes, p => p.isRemote)[0];
+ let localProcess = Array.filter(processes, p => !p.isRemote)[0];
+ let remoteFrame = Array.filter(frames, f => f.process == remoteProcess)[0];
+
+ // Switch the remote tab to a local URI which should kill the remote process
+
+ let frameDetach = promiseEventOnItemAndContainer(assert, remoteFrame, frames, 'detach');
+ let frameAttach = promiseTabFrameAttach(frames);
+ let processDetach = promiseEventOnItemAndContainer(assert, remoteProcess, processes, 'detach');
+ let browserLoad = promiseDOMEvent(browser, "load", true);
+ setTabURL(tab, LOCAL_URI);
+ // The load should kill the remote frame
+ yield frameDetach;
+ // And create a new frame in the local process
+ let [newFrame] = yield frameAttach;
+ assert.equal(newFrame.process, localProcess, "New frame should be in the local process");
+ // And kill the process
+ yield processDetach;
+ yield browserLoad;
+
+ frameDetach = promiseEventOnItemAndContainer(assert, newFrame, frames, 'detach');
+ let processAttach = promiseEvent(processes, 'attach');
+ frameAttach = promiseTabFrameAttach(frames);
+ browserLoad = promiseDOMEvent(browser, "load", true);
+ setTabURL(tab, REMOTE_URI);
+ // The load should kill the remote frame
+ yield frameDetach;
+ // And create a new remote process
+ [remoteProcess] = yield processAttach;
+ assert.ok(remoteProcess.isRemote, "Process should be remote");
+ // And create a new frame in the remote process
+ [newFrame] = yield frameAttach;
+ assert.equal(newFrame.process, remoteProcess, "New frame should be in the remote process");
+ yield browserLoad;
+
+ browserLoad = promiseDOMEvent(browser, "load", true);
+ setTabURL(tab, "about:blank");
+ yield browserLoad;
+
+ loader.unload();
+};
+
+// Test that we find the right number of processes and that messaging between
+// them works and none of the streams cross
+exports["test process list"] = function*(assert) {
+ let loader = new Loader(module);
+ let { processes } = loader.require('sdk/remote/parent');
+
+ let processCount = 0;
+ processes.forEvery(processes => processCount++);
+
+ yield waitForProcesses(loader);
+
+ let remoteProcesses = Array.filter(processes, process => process.isRemote);
+ let localProcesses = Array.filter(processes, process => !process.isRemote);
+
+ assert.equal(localProcesses.length, 1, "Should always be one process");
+
+ if (isE10S) {
+ assert.equal(remoteProcesses.length, 1, "Should be one remote process");
+ }
+ else {
+ assert.equal(remoteProcesses.length, 0, "Should be no remote processes");
+ }
+
+ assert.equal(processCount, processes.length, "Should have seen all processes");
+
+ processCount = 0;
+ processes.forEvery(process => processCount++);
+
+ assert.equal(processCount, processes.length, "forEvery should send existing processes to the listener");
+
+ localProcesses[0].port.on('sdk/test/pong', (process, key) => {
+ assert.equal(key, "local", "Should not have seen a pong from the local process with the wrong key");
+ });
+
+ if (isE10S) {
+ remoteProcesses[0].port.on('sdk/test/pong', (process, key) => {
+ assert.equal(key, "remote", "Should not have seen a pong from the remote process with the wrong key");
+ });
+ }
+
+ let promise = promiseEventOnItemAndContainer(assert, localProcesses[0].port, processes.port, 'sdk/test/pong', localProcesses[0]);
+ localProcesses[0].port.emit('sdk/test/ping', "local");
+
+ let reply = yield promise;
+ assert.equal(reply[0], "local", "Saw the process reply with the right key");
+
+ if (isE10S) {
+ promise = promiseEventOnItemAndContainer(assert, remoteProcesses[0].port, processes.port, 'sdk/test/pong', remoteProcesses[0]);
+ remoteProcesses[0].port.emit('sdk/test/ping', "remote");
+
+ reply = yield promise;
+ assert.equal(reply[0], "remote", "Saw the process reply with the right key");
+
+ assert.notEqual(localProcesses[0], remoteProcesses[0], "Processes should be different");
+ }
+
+ loader.unload();
+};
+
+// Test that the frame lists are kept up to date
+exports["test frame list"] = function*(assert) {
+ function browserFrames(list) {
+ return Array.filter(list, b => b.isTab).length;
+ }
+
+ let window = getMostRecentBrowserWindow();
+
+ let tabs = getTabs(window);
+ assert.equal(tabs.length, 1, "Should have just the one tab to start with");
+
+ let loader = new Loader(module);
+ let { processes, frames } = yield waitForProcesses(loader);
+
+ assert.equal(browserFrames(frames), getTabs(window).length, "Should be the right number of browser frames.");
+ assert.equal((yield getChildFrameCount(processes)), frames.length, "Child processes should have the right number of frames");
+
+ let promise = promiseTabFrameAttach(frames);
+ let tab1 = openTab(window, LOCAL_URI);
+ let [frame1] = yield promise;
+ assert.ok(!!frame1, "Should have seen the new frame");
+ assert.ok(!frame1.process.isRemote, "Frame should not be remote");
+
+ assert.equal(browserFrames(frames), getTabs(window).length, "Should be the right number of browser frames.");
+ assert.equal((yield getChildFrameCount(processes)), frames.length, "Child processes should have the right number of frames");
+
+ promise = promiseTabFrameAttach(frames);
+ let tab2 = openTab(window, REMOTE_URI);
+ let [frame2] = yield promise;
+ assert.ok(!!frame2, "Should have seen the new frame");
+ if (isE10S)
+ assert.ok(frame2.process.isRemote, "Frame should be remote");
+ else
+ assert.ok(!frame2.process.isRemote, "Frame should not be remote");
+
+ assert.equal(browserFrames(frames), getTabs(window).length, "Should be the right number of browser frames.");
+ assert.equal((yield getChildFrameCount(processes)), frames.length, "Child processes should have the right number of frames");
+
+ frames.port.emit('sdk/test/ping')
+ yield new Promise(resolve => {
+ let count = 0;
+ let listener = () => {
+ console.log("Saw pong");
+ count++;
+ if (count == frames.length) {
+ frames.port.off('sdk/test/pong', listener);
+ resolve();
+ }
+ };
+ frames.port.on('sdk/test/pong', listener);
+ });
+
+ let badListener = () => {
+ assert.fail("Should not have seen a response through this frame");
+ }
+ frame1.port.on('sdk/test/pong', badListener);
+ frame2.port.emit('sdk/test/ping', 'b');
+ let [key] = yield promiseEventOnItemAndContainer(assert, frame2.port, frames.port, 'sdk/test/pong', frame2);
+ assert.equal(key, 'b', "Should have seen the right response");
+ frame1.port.off('sdk/test/pong', badListener);
+
+ frame2.port.on('sdk/test/pong', badListener);
+ frame1.port.emit('sdk/test/ping', 'b');
+ [key] = yield promiseEventOnItemAndContainer(assert, frame1.port, frames.port, 'sdk/test/pong', frame1);
+ assert.equal(key, 'b', "Should have seen the right response");
+ frame2.port.off('sdk/test/pong', badListener);
+
+ promise = promiseEventOnItemAndContainer(assert, frame1, frames, 'detach');
+ closeTab(tab1);
+ yield promise;
+
+ assert.equal(browserFrames(frames), getTabs(window).length, "Should be the right number of browser frames.");
+ assert.equal((yield getChildFrameCount(processes)), frames.length, "Child processes should have the right number of frames");
+
+ promise = promiseEventOnItemAndContainer(assert, frame2, frames, 'detach');
+ closeTab(tab2);
+ yield promise;
+
+ assert.equal(browserFrames(frames), getTabs(window).length, "Should be the right number of browser frames.");
+ assert.equal((yield getChildFrameCount(processes)), frames.length, "Child processes should have the right number of frames");
+
+ loader.unload();
+};
+
+// Test that multiple loaders get their own loaders in the child and messages
+// don't cross. Unload should work
+exports["test new loader"] = function*(assert) {
+ let loader1 = new Loader(module);
+ let { processes: processes1 } = yield waitForProcesses(loader1);
+
+ let loader2 = new Loader(module);
+ let { processes: processes2 } = yield waitForProcesses(loader2);
+
+ let process1 = [...processes1][0];
+ let process2 = [...processes2][0];
+
+ process1.port.on('sdk/test/pong', (process, key) => {
+ assert.equal(key, "a", "Should have seen the right pong");
+ });
+
+ process2.port.on('sdk/test/pong', (process, key) => {
+ assert.equal(key, "b", "Should have seen the right pong");
+ });
+
+ process1.port.emit('sdk/test/ping', 'a');
+ yield promiseEvent(process1.port, 'sdk/test/pong');
+
+ process2.port.emit('sdk/test/ping', 'b');
+ yield promiseEvent(process2.port, 'sdk/test/pong');
+
+ loader1.unload();
+
+ process2.port.emit('sdk/test/ping', 'b');
+ yield promiseEvent(process2.port, 'sdk/test/pong');
+
+ loader2.unload();
+};
+
+// Test that unloading the loader unloads the child instances
+exports["test unload"] = function*(assert) {
+ let window = getMostRecentBrowserWindow();
+ let loader = new Loader(module);
+ let { frames } = yield waitForProcesses(loader);
+
+ let promise = promiseTabFrameAttach(frames);
+ let tab = openTab(window, "data:,<html/>");
+ let browser = getBrowserForTab(tab);
+ yield promiseDOMEvent(browser, "load", true);
+ let [frame] = yield promise;
+ assert.ok(!!frame, "Should have seen the new frame");
+
+ promise = promiseDOMEvent(browser, 'hashchange');
+ frame.port.emit('sdk/test/testunload');
+ loader.unload("shutdown");
+ yield promise;
+
+ let hash = getURI(tab).replace(/.*#/, "");
+ assert.equal(hash, "unloaded:shutdown", "Saw the correct hash change.")
+
+ closeTab(tab);
+}
+
+// Test that unloading the loader causes the child to see frame detach events
+exports["test frame detach on unload"] = function*(assert) {
+ let window = getMostRecentBrowserWindow();
+ let loader = new Loader(module);
+ let { frames } = yield waitForProcesses(loader);
+
+ let promise = promiseTabFrameAttach(frames);
+ let tab = openTab(window, "data:,<html/>");
+ let browser = getBrowserForTab(tab);
+ yield promiseDOMEvent(browser, "load", true);
+ let [frame] = yield promise;
+ assert.ok(!!frame, "Should have seen the new frame");
+
+ promise = promiseDOMEvent(browser, 'hashchange');
+ frame.port.emit('sdk/test/testdetachonunload');
+ loader.unload("shutdown");
+ yield promise;
+
+ let hash = getURI(tab).replace(/.*#/, "");
+ assert.equal(hash, "unloaded", "Saw the correct hash change.")
+
+ closeTab(tab);
+}
+
+// Test that DOM event listener on the frame object works
+exports["test frame event listeners"] = function*(assert) {
+ let window = getMostRecentBrowserWindow();
+ let loader = new Loader(module);
+ let { frames } = yield waitForProcesses(loader);
+
+ let promise = promiseTabFrameAttach(frames);
+ let tab = openTab(window, "data:text/html,<html></html>");
+ let browser = getBrowserForTab(tab);
+ yield promiseDOMEvent(browser, "load", true);
+ let [frame] = yield promise;
+ assert.ok(!!frame, "Should have seen the new frame");
+
+ frame.port.emit('sdk/test/registerframeevent');
+ promise = Promise.all([
+ promiseEvent(frame.port, 'sdk/test/sawreply'),
+ promiseEvent(frame.port, 'sdk/test/eventsent')
+ ]);
+
+ frame.port.emit('sdk/test/sendevent');
+ yield promise;
+
+ frame.port.emit('sdk/test/unregisterframeevent');
+ promise = promiseEvent(frame.port, 'sdk/test/eventsent');
+ frame.port.on('sdk/test/sawreply', () => {
+ assert.fail("Should not have seen the event listener reply");
+ });
+
+ frame.port.emit('sdk/test/sendevent');
+ yield promise;
+
+ closeTab(tab);
+ loader.unload();
+}
+
+// Test that DOM event listener on the frames object works
+exports["test frames event listeners"] = function*(assert) {
+ let window = getMostRecentBrowserWindow();
+ let loader = new Loader(module);
+ let { frames } = yield waitForProcesses(loader);
+
+ let promise = promiseTabFrameAttach(frames);
+ let tab = openTab(window, "data:text/html,<html></html>");
+ let browser = getBrowserForTab(tab);
+ yield promiseDOMEvent(browser, "load", true);
+ let [frame] = yield promise;
+ assert.ok(!!frame, "Should have seen the new frame");
+
+ frame.port.emit('sdk/test/registerframesevent');
+ promise = Promise.all([
+ promiseEvent(frame.port, 'sdk/test/sawreply'),
+ promiseEvent(frame.port, 'sdk/test/eventsent')
+ ]);
+
+ frame.port.emit('sdk/test/sendevent');
+ yield promise;
+
+ frame.port.emit('sdk/test/unregisterframesevent');
+ promise = promiseEvent(frame.port, 'sdk/test/eventsent');
+ frame.port.on('sdk/test/sawreply', () => {
+ assert.fail("Should not have seen the event listener reply");
+ });
+
+ frame.port.emit('sdk/test/sendevent');
+ yield promise;
+
+ closeTab(tab);
+ loader.unload();
+}
+
+// Test that unloading unregisters frame DOM events
+exports["test unload removes frame event listeners"] = function*(assert) {
+ let window = getMostRecentBrowserWindow();
+ let loader = new Loader(module);
+ let { frames } = yield waitForProcesses(loader);
+
+ let loader2 = new Loader(module);
+ let { frames: frames2 } = yield waitForProcesses(loader2);
+
+ let promise = promiseTabFrameAttach(frames);
+ let promise2 = promiseTabFrameAttach(frames2);
+ let tab = openTab(window, "data:text/html,<html></html>");
+ let browser = getBrowserForTab(tab);
+ yield promiseDOMEvent(browser, "load", true);
+ let [frame] = yield promise;
+ let [frame2] = yield promise2;
+ assert.ok(!!frame && !!frame2, "Should have seen the new frame");
+
+ frame.port.emit('sdk/test/registerframeevent');
+ promise = Promise.all([
+ promiseEvent(frame2.port, 'sdk/test/sawreply'),
+ promiseEvent(frame2.port, 'sdk/test/eventsent')
+ ]);
+
+ frame2.port.emit('sdk/test/sendevent');
+ yield promise;
+
+ loader.unload();
+
+ promise = promiseEvent(frame2.port, 'sdk/test/eventsent');
+ frame2.port.on('sdk/test/sawreply', () => {
+ assert.fail("Should not have seen the event listener reply");
+ });
+
+ frame2.port.emit('sdk/test/sendevent');
+ yield promise;
+
+ closeTab(tab);
+ loader2.unload();
+}
+
+// Test that unloading unregisters frames DOM events
+exports["test unload removes frames event listeners"] = function*(assert) {
+ let window = getMostRecentBrowserWindow();
+ let loader = new Loader(module);
+ let { frames } = yield waitForProcesses(loader);
+
+ let loader2 = new Loader(module);
+ let { frames: frames2 } = yield waitForProcesses(loader2);
+
+ let promise = promiseTabFrameAttach(frames);
+ let promise2 = promiseTabFrameAttach(frames2);
+ let tab = openTab(window, "data:text/html,<html></html>");
+ let browser = getBrowserForTab(tab);
+ yield promiseDOMEvent(browser, "load", true);
+ let [frame] = yield promise;
+ let [frame2] = yield promise2;
+ assert.ok(!!frame && !!frame2, "Should have seen the new frame");
+
+ frame.port.emit('sdk/test/registerframesevent');
+ promise = Promise.all([
+ promiseEvent(frame2.port, 'sdk/test/sawreply'),
+ promiseEvent(frame2.port, 'sdk/test/eventsent')
+ ]);
+
+ frame2.port.emit('sdk/test/sendevent');
+ yield promise;
+
+ loader.unload();
+
+ promise = promiseEvent(frame2.port, 'sdk/test/eventsent');
+ frame2.port.on('sdk/test/sawreply', () => {
+ assert.fail("Should not have seen the event listener reply");
+ });
+
+ frame2.port.emit('sdk/test/sendevent');
+ yield promise;
+
+ closeTab(tab);
+ loader2.unload();
+}
+
+// Check that the child frame has the right properties
+exports["test frame properties"] = function*(assert) {
+ let loader = new Loader(module);
+ let { frames } = yield waitForProcesses(loader);
+
+ let promise = new Promise(resolve => {
+ let count = frames.length;
+ let listener = (frame, properties) => {
+ assert.equal(properties.isTab, frame.isTab,
+ "Child frame should have the same isTab property");
+
+ if (--count == 0) {
+ frames.port.off('sdk/test/replyproperties', listener);
+ resolve();
+ }
+ }
+
+ frames.port.on('sdk/test/replyproperties', listener);
+ })
+
+ frames.port.emit('sdk/test/checkproperties');
+ yield promise;
+
+ loader.unload();
+}
+
+// Check that non-remote processes have the same process ID and remote processes
+// have different IDs
+exports["test processID"] = function*(assert) {
+ let loader = new Loader(module);
+ let { processes } = yield waitForProcesses(loader);
+
+ for (let process of processes) {
+ process.port.emit('sdk/test/getprocessid');
+ let [p, ID] = yield promiseEvent(process.port, 'sdk/test/processid');
+ if (process.isRemote) {
+ assert.notEqual(ID, processID, "Remote processes should have a different process ID");
+ }
+ else {
+ assert.equal(ID, processID, "Remote processes should have the same process ID");
+ }
+ }
+
+ loader.unload();
+}
+
+// Check that sdk/remote/parent and sdk/remote/child can only be loaded in the
+// appropriate loaders
+exports["test cannot load in wrong loader"] = function*(assert) {
+ let loader = new Loader(module);
+ let { processes } = yield waitForProcesses(loader);
+
+ try {
+ require('sdk/remote/child');
+ assert.fail("Should not have been able to load sdk/remote/child");
+ }
+ catch (e) {
+ assert.ok(/Cannot load sdk\/remote\/child in a main process loader/.test(e),
+ "Should have seen the right exception.");
+ }
+
+ for (let process of processes) {
+ processes.port.emit('sdk/test/parentload');
+ let [_, isChildLoader, loaded, message] = yield promiseEvent(processes.port, 'sdk/test/parentload');
+ assert.ok(isChildLoader, "Process should see itself in a child loader.");
+ assert.ok(!loaded, "Process couldn't load sdk/remote/parent.");
+ assert.ok(/Cannot load sdk\/remote\/parent in a child loader/.test(message),
+ "Should have seen the right exception.");
+ }
+
+ loader.unload();
+};
+
+exports["test send cpow"] = function*(assert) {
+ if (!isE10S) {
+ assert.pass("Skipping test in non-e10s mode");
+ return;
+ }
+
+ let window = getMostRecentBrowserWindow();
+
+ let tabs = getTabs(window);
+ assert.equal(tabs.length, 1, "Should have just the one tab to start with");
+ let tab = tabs[0];
+ let browser = getBrowserForTab(tab);
+
+ assert.ok(Cu.isCrossProcessWrapper(browser.contentWindow),
+ "Should have a CPOW for the browser content window");
+
+ let loader = new Loader(module);
+ let { processes } = yield waitForProcesses(loader);
+
+ processes.port.emitCPOW('sdk/test/cpow', ['foobar'], { window: browser.contentWindow });
+ let [process, arg, id] = yield promiseEvent(processes.port, 'sdk/test/cpow');
+
+ assert.ok(process.isRemote, "Response should come from the remote process");
+ assert.equal(arg, "foobar", "Argument should have passed through");
+ assert.equal(id, browser.outerWindowID, "Should have got the ID from the child");
+};
+
+after(exports, function*(name, assert) {
+ yield cleanUI();
+});
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/e10s-remote/package.json b/addon-sdk/source/test/addons/e10s-remote/package.json
new file mode 100644
index 000000000..88626d38d
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-remote/package.json
@@ -0,0 +1,9 @@
+{
+ "name": "e10s-remote",
+ "title": "e10s-remote",
+ "id": "remote@jetpack",
+ "description": "Run remote tests",
+ "version": "1.0.0",
+ "main": "main.js",
+ "e10s": true
+}
diff --git a/addon-sdk/source/test/addons/e10s-remote/remote-module.js b/addon-sdk/source/test/addons/e10s-remote/remote-module.js
new file mode 100644
index 000000000..cedf005a9
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-remote/remote-module.js
@@ -0,0 +1,129 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const { when } = require('sdk/system/unload');
+const { process, frames } = require('sdk/remote/child');
+const { loaderID } = require('@loader/options');
+const { processID } = require('sdk/system/runtime');
+const system = require('sdk/system/events');
+const { Cu } = require('chrome');
+const { isChildLoader } = require('sdk/remote/core');
+const { getOuterId } = require('sdk/window/utils');
+
+function log(str) {
+ console.log("remote[" + loaderID + "][" + processID + "]: " + str);
+}
+
+log("module loaded");
+
+process.port.emit('sdk/test/load');
+
+process.port.on('sdk/test/ping', (process, key) => {
+ log("received process ping");
+ process.port.emit('sdk/test/pong', key);
+});
+
+var frameCount = 0;
+frames.forEvery(frame => {
+ frameCount++;
+ frame.on('detach', () => {
+ frameCount--;
+ });
+
+ frame.port.on('sdk/test/ping', (frame, key) => {
+ log("received frame ping");
+ frame.port.emit('sdk/test/pong', key);
+ });
+});
+
+frames.port.on('sdk/test/checkproperties', frame => {
+ frame.port.emit('sdk/test/replyproperties', {
+ isTab: frame.isTab
+ });
+});
+
+process.port.on('sdk/test/count', () => {
+ log("received count ping");
+ process.port.emit('sdk/test/count', frameCount);
+});
+
+process.port.on('sdk/test/getprocessid', () => {
+ process.port.emit('sdk/test/processid', processID);
+});
+
+frames.port.on('sdk/test/testunload', (frame) => {
+ // Cache the content since the frame will have been destroyed by the time
+ // we see the unload event.
+ let content = frame.content;
+ when((reason) => {
+ content.location = "#unloaded:" + reason;
+ });
+});
+
+frames.port.on('sdk/test/testdetachonunload', (frame) => {
+ let content = frame.content;
+ frame.on('detach', () => {
+ console.log("Detach from " + frame.content.location);
+ frame.content.location = "#unloaded";
+ });
+});
+
+frames.port.on('sdk/test/sendevent', (frame) => {
+ let doc = frame.content.document;
+
+ let listener = () => {
+ frame.port.emit('sdk/test/sawreply');
+ }
+
+ system.on("Test:Reply", listener);
+ let event = new frame.content.CustomEvent("Test:Event");
+ doc.dispatchEvent(event);
+ system.off("Test:Reply", listener);
+ frame.port.emit('sdk/test/eventsent');
+});
+
+process.port.on('sdk/test/parentload', () => {
+ let loaded = false;
+ let message = "";
+ try {
+ require('sdk/remote/parent');
+ loaded = true;
+ }
+ catch (e) {
+ message = "" + e;
+ }
+
+ process.port.emit('sdk/test/parentload',
+ isChildLoader,
+ loaded,
+ message
+ )
+});
+
+function listener(event) {
+ // Use the raw observer service here since it will be usable even if the
+ // loader has unloaded
+ let { Services } = Cu.import("resource://gre/modules/Services.jsm", {});
+ Services.obs.notifyObservers(null, "Test:Reply", "");
+}
+
+frames.port.on('sdk/test/registerframesevent', (frame) => {
+ frames.addEventListener("Test:Event", listener, true);
+});
+
+frames.port.on('sdk/test/unregisterframesevent', (frame) => {
+ frames.removeEventListener("Test:Event", listener, true);
+});
+
+frames.port.on('sdk/test/registerframeevent', (frame) => {
+ frame.addEventListener("Test:Event", listener, true);
+});
+
+frames.port.on('sdk/test/unregisterframeevent', (frame) => {
+ frame.removeEventListener("Test:Event", listener, true);
+});
+
+process.port.on('sdk/test/cpow', (process, arg, cpows) => {
+ process.port.emit('sdk/test/cpow', arg, getOuterId(cpows.window));
+});
diff --git a/addon-sdk/source/test/addons/e10s-remote/utils.js b/addon-sdk/source/test/addons/e10s-remote/utils.js
new file mode 100644
index 000000000..f30f4f3a4
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-remote/utils.js
@@ -0,0 +1,110 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const { Cu } = require('chrome');
+const { Task: { async } } = Cu.import('resource://gre/modules/Task.jsm', {});
+const { getMostRecentBrowserWindow } = require('sdk/window/utils');
+
+const REMOTE_MODULE = "./remote-module";
+
+function promiseEvent(emitter, event) {
+ console.log("Waiting for " + event);
+ return new Promise(resolve => {
+ emitter.once(event, (...args) => {
+ console.log("Saw " + event);
+ resolve(args);
+ });
+ });
+}
+exports.promiseEvent = promiseEvent;
+
+function promiseDOMEvent(target, event, isCapturing = false) {
+ console.log("Waiting for " + event);
+ return new Promise(resolve => {
+ let listener = (event) => {
+ target.removeEventListener(event, listener, isCapturing);
+ resolve(event);
+ };
+ target.addEventListener(event, listener, isCapturing);
+ })
+}
+exports.promiseDOMEvent = promiseDOMEvent;
+
+const promiseEventOnItemAndContainer = async(function*(assert, itemport, container, event, item = itemport) {
+ let itemEvent = promiseEvent(itemport, event);
+ let containerEvent = promiseEvent(container, event);
+
+ let itemArgs = yield itemEvent;
+ let containerArgs = yield containerEvent;
+
+ assert.equal(containerArgs[0], item, "Should have seen a container event for the right item");
+ assert.equal(JSON.stringify(itemArgs), JSON.stringify(containerArgs), "Arguments should have matched");
+
+ // Strip off the item from the returned arguments
+ return itemArgs.slice(1);
+});
+exports.promiseEventOnItemAndContainer = promiseEventOnItemAndContainer;
+
+const waitForProcesses = async(function*(loader) {
+ console.log("Starting remote");
+ let { processes, frames, remoteRequire } = loader.require('sdk/remote/parent');
+ remoteRequire(REMOTE_MODULE, module);
+
+ let events = [];
+
+ // In e10s we should expect to see two processes
+ let expectedCount = isE10S ? 2 : 1;
+
+ yield new Promise(resolve => {
+ let count = 0;
+
+ // Wait for a process to be detected
+ let listener = process => {
+ console.log("Saw a process attach");
+ // Wait for the remote module to load in this process
+ process.port.once('sdk/test/load', () => {
+ console.log("Saw a remote module load");
+ count++;
+ if (count == expectedCount) {
+ processes.off('attach', listener);
+ resolve();
+ }
+ });
+ }
+ processes.on('attach', listener);
+ });
+
+ console.log("Remote ready");
+ return { processes, frames, remoteRequire };
+});
+exports.waitForProcesses = waitForProcesses;
+
+// Counts the frames in all the child processes
+const getChildFrameCount = async(function*(processes) {
+ let frameCount = 0;
+
+ for (let process of processes) {
+ process.port.emit('sdk/test/count');
+ let [p, count] = yield promiseEvent(process.port, 'sdk/test/count');
+ frameCount += count;
+ }
+
+ return frameCount;
+});
+exports.getChildFrameCount = getChildFrameCount;
+
+const mainWindow = getMostRecentBrowserWindow();
+const isE10S = mainWindow.gMultiProcessBrowser;
+exports.isE10S = isE10S;
+
+if (isE10S) {
+ console.log("Testing in E10S mode");
+ // We expect a child process to already be present, make sure that is the case
+ mainWindow.XULBrowserWindow.forceInitialBrowserRemote();
+}
+else {
+ console.log("Testing in non-E10S mode");
+}
diff --git a/addon-sdk/source/test/addons/e10s-tabs/lib/main.js b/addon-sdk/source/test/addons/e10s-tabs/lib/main.js
new file mode 100644
index 000000000..09ccf5008
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-tabs/lib/main.js
@@ -0,0 +1,23 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { merge } = require('sdk/util/object');
+const { version } = require('sdk/system');
+
+const SKIPPING_TESTS = {
+ "test skip": (assert) => assert.pass("nothing to test here")
+};
+
+merge(module.exports, require('./test-tab'));
+merge(module.exports, require('./test-tab-events'));
+merge(module.exports, require('./test-tab-observer'));
+merge(module.exports, require('./test-tab-utils'));
+
+// run e10s tests only on builds from trunk, fx-team, Nightly..
+if (!version.endsWith('a1')) {
+ module.exports = SKIPPING_TESTS;
+}
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/e10s-tabs/lib/private-browsing/helper.js b/addon-sdk/source/test/addons/e10s-tabs/lib/private-browsing/helper.js
new file mode 100644
index 000000000..f581c92c0
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-tabs/lib/private-browsing/helper.js
@@ -0,0 +1,91 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Loader } = require('sdk/test/loader');
+
+const { loader } = LoaderWithHookedConsole(module);
+
+const pb = loader.require('sdk/private-browsing');
+const pbUtils = loader.require('sdk/private-browsing/utils');
+const xulApp = require("sdk/system/xul-app");
+const { open: openWindow, getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { openTab, getTabContentWindow, getActiveTab, setTabURL, closeTab } = require('sdk/tabs/utils');
+const promise = require("sdk/core/promise");
+const windowHelpers = require('sdk/window/helpers');
+const events = require("sdk/system/events");
+
+function LoaderWithHookedConsole(module) {
+ let globals = {};
+ let errors = [];
+
+ globals.console = Object.create(console, {
+ error: {
+ value: function(e) {
+ errors.push(e);
+ if (!/DEPRECATED:/.test(e)) {
+ console.error(e);
+ }
+ }
+ }
+ });
+
+ let loader = Loader(module, globals);
+
+ return {
+ loader: loader,
+ errors: errors
+ }
+}
+
+exports.pb = pb;
+exports.pbUtils = pbUtils;
+exports.LoaderWithHookedConsole = LoaderWithHookedConsole;
+
+exports.openWebpage = function openWebpage(url, enablePrivate) {
+ if (xulApp.is("Fennec")) {
+ let chromeWindow = getMostRecentBrowserWindow();
+ let rawTab = openTab(chromeWindow, url, {
+ isPrivate: enablePrivate
+ });
+
+ return {
+ ready: promise.resolve(getTabContentWindow(rawTab)),
+ close: function () {
+ closeTab(rawTab);
+ // Returns a resolved promise as there is no need to wait
+ return promise.resolve();
+ }
+ };
+ }
+ else {
+ let win = openWindow(null, {
+ features: {
+ private: enablePrivate
+ }
+ });
+ let deferred = promise.defer();
+
+ // Wait for delayed startup code to be executed, in order to ensure
+ // that the window is really ready
+ events.on("browser-delayed-startup-finished", function onReady({subject}) {
+ if (subject == win) {
+ events.off("browser-delayed-startup-finished", onReady);
+ deferred.resolve(win);
+
+ let rawTab = getActiveTab(win);
+ setTabURL(rawTab, url);
+ deferred.resolve(getTabContentWindow(rawTab));
+ }
+ }, true);
+
+ return {
+ ready: deferred.promise,
+ close: function () {
+ return windowHelpers.close(win);
+ }
+ };
+ }
+ return null;
+}
diff --git a/addon-sdk/source/test/addons/e10s-tabs/lib/test-tab-events.js b/addon-sdk/source/test/addons/e10s-tabs/lib/test-tab-events.js
new file mode 100644
index 000000000..4d4eae347
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-tabs/lib/test-tab-events.js
@@ -0,0 +1,238 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const { Loader } = require("sdk/test/loader");
+const utils = require("sdk/tabs/utils");
+const { open, close } = require("sdk/window/helpers");
+const { getMostRecentBrowserWindow } = require("sdk/window/utils");
+const { events } = require("sdk/tab/events");
+const { on, off } = require("sdk/event/core");
+const { resolve, defer } = require("sdk/core/promise");
+
+var isFennec = require("sdk/system/xul-app").is("Fennec");
+
+function test(options) {
+ return function(assert, done) {
+ let tabEvents = [];
+ let tabs = [];
+ let { promise, resolve: resolveP } = defer();
+ let win = isFennec ? resolve(getMostRecentBrowserWindow()) :
+ open(null, {
+ features: { private: true, toolbar:true, chrome: true }
+ });
+ let window = null;
+
+ // Firefox events are fired sync; Fennec events async
+ // this normalizes the tests
+ function handler (event) {
+ tabEvents.push(event);
+ runIfReady();
+ }
+
+ function runIfReady () {
+ let releventEvents = getRelatedEvents(tabEvents, tabs);
+ if (options.readyWhen(releventEvents))
+ options.end({
+ tabs: tabs,
+ events: releventEvents,
+ assert: assert,
+ done: resolveP
+ });
+ }
+
+ win.then(function(w) {
+ window = w;
+ on(events, "data", handler);
+ options.start({ tabs: tabs, window: window });
+
+ // Execute here for synchronous FF events, as the handlers
+ // were called before tabs were pushed to `tabs`
+ runIfReady();
+ return promise;
+ }).then(function() {
+ off(events, "data", handler);
+ return isFennec ? null : close(window);
+ }).then(done, assert.fail);
+ };
+}
+
+// Just making sure that tab events work for already opened tabs not only
+// for new windows.
+exports["test current window"] = test({
+ readyWhen: events => events.length === 3,
+ start: ({ tabs, window }) => {
+ let tab = utils.openTab(window, 'data:text/plain,open');
+ tabs.push(tab);
+ utils.closeTab(tab);
+ },
+ end: ({ tabs, events, assert, done }) => {
+ let [open, select, close] = events;
+ let tab = tabs[0];
+
+ assert.equal(open.type, "TabOpen");
+ assert.equal(open.target, tab);
+
+ assert.equal(select.type, "TabSelect");
+ assert.equal(select.target, tab);
+
+ assert.equal(close.type, "TabClose");
+ assert.equal(close.target, tab);
+ done();
+ }
+});
+
+exports["test open"] = test({
+ readyWhen: events => events.length === 2,
+ start: ({ tabs, window }) => {
+ tabs.push(utils.openTab(window, 'data:text/plain,open'));
+ },
+ end: ({ tabs, events, assert, done }) => {
+ let [open, select] = events;
+ let tab = tabs[0];
+
+ assert.equal(open.type, "TabOpen");
+ assert.equal(open.target, tab);
+
+ assert.equal(select.type, "TabSelect");
+ assert.equal(select.target, tab);
+ done();
+ }
+});
+
+exports["test open -> close"] = test({
+ readyWhen: events => events.length === 3,
+ start: ({ tabs, window }) => {
+ // First tab is useless we just open it so that closing second tab won't
+ // close window on some platforms.
+ utils.openTab(window, 'data:text/plain,ignore');
+ let tab = utils.openTab(window, 'data:text/plain,open-close');
+ tabs.push(tab);
+ utils.closeTab(tab);
+ },
+ end: ({ tabs, events, assert, done }) => {
+ let [open, select, close] = events;
+ let tab = tabs[0];
+
+ assert.equal(open.type, "TabOpen");
+ assert.equal(open.target, tab);
+
+ assert.equal(select.type, "TabSelect");
+ assert.equal(select.target, tab);
+
+ assert.equal(close.type, "TabClose");
+ assert.equal(close.target, tab);
+ done();
+ }
+});
+
+exports["test open -> open -> select"] = test({
+ readyWhen: events => events.length === 5,
+ start: ({tabs, window}) => {
+ tabs.push(utils.openTab(window, 'data:text/plain,Tab-1'));
+ tabs.push(utils.openTab(window, 'data:text/plain,Tab-2'));
+ utils.activateTab(tabs[0], window);
+ },
+ end: ({ tabs, events, assert, done }) => {
+ let [ tab1, tab2 ] = tabs;
+ let tab1Events = 0;
+ getRelatedEvents(events, tab1).map(event => {
+ tab1Events++;
+ if (tab1Events === 1)
+ assert.equal(event.type, "TabOpen", "first tab opened");
+ else
+ assert.equal(event.type, "TabSelect", "first tab selected");
+ assert.equal(event.target, tab1);
+ });
+ assert.equal(tab1Events, 3, "first tab has 3 events");
+
+ let tab2Opened;
+ getRelatedEvents(events, tab2).map(event => {
+ if (!tab2Opened)
+ assert.equal(event.type, "TabOpen", "second tab opened");
+ else
+ assert.equal(event.type, "TabSelect", "second tab selected");
+ tab2Opened = true;
+ assert.equal(event.target, tab2);
+ });
+ done();
+ }
+});
+
+exports["test open -> pin -> unpin"] = test({
+ readyWhen: events => events.length === (isFennec ? 2 : 5),
+ start: ({ tabs, window }) => {
+ tabs.push(utils.openTab(window, 'data:text/plain,pin-unpin'));
+ utils.pin(tabs[0]);
+ utils.unpin(tabs[0]);
+ },
+ end: ({ tabs, events, assert, done }) => {
+ let [open, select, move, pin, unpin] = events;
+ let tab = tabs[0];
+
+ assert.equal(open.type, "TabOpen");
+ assert.equal(open.target, tab);
+
+ assert.equal(select.type, "TabSelect");
+ assert.equal(select.target, tab);
+
+ if (isFennec) {
+ assert.pass("Tab pin / unpin is not supported by Fennec");
+ }
+ else {
+ assert.equal(move.type, "TabMove");
+ assert.equal(move.target, tab);
+
+ assert.equal(pin.type, "TabPinned");
+ assert.equal(pin.target, tab);
+
+ assert.equal(unpin.type, "TabUnpinned");
+ assert.equal(unpin.target, tab);
+ }
+ done();
+ }
+});
+
+exports["test open -> open -> move "] = test({
+ readyWhen: events => events.length === (isFennec ? 4 : 5),
+ start: ({tabs, window}) => {
+ tabs.push(utils.openTab(window, 'data:text/plain,Tab-1'));
+ tabs.push(utils.openTab(window, 'data:text/plain,Tab-2'));
+ utils.move(tabs[0], 2);
+ },
+ end: ({ tabs, events, assert, done }) => {
+ let [ tab1, tab2 ] = tabs;
+ let tab1Events = 0;
+ getRelatedEvents(events, tab1).map(event => {
+ tab1Events++;
+ if (tab1Events === 1)
+ assert.equal(event.type, "TabOpen", "first tab opened");
+ else if (tab1Events === 2)
+ assert.equal(event.type, "TabSelect", "first tab selected");
+ else if (tab1Events === 3 && isFennec)
+ assert.equal(event.type, "TabMove", "first tab moved");
+ assert.equal(event.target, tab1);
+ });
+ assert.equal(tab1Events, isFennec ? 2 : 3,
+ "correct number of events for first tab");
+
+ let tab2Events = 0;
+ getRelatedEvents(events, tab2).map(event => {
+ tab2Events++;
+ if (tab2Events === 1)
+ assert.equal(event.type, "TabOpen", "second tab opened");
+ else
+ assert.equal(event.type, "TabSelect", "second tab selected");
+ assert.equal(event.target, tab2);
+ });
+ done();
+ }
+});
+
+function getRelatedEvents (events, tabs) {
+ return events.filter(({target}) => ~([].concat(tabs)).indexOf(target));
+}
+
+// require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/addons/e10s-tabs/lib/test-tab-observer.js b/addon-sdk/source/test/addons/e10s-tabs/lib/test-tab-observer.js
new file mode 100644
index 000000000..b0e1753a2
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-tabs/lib/test-tab-observer.js
@@ -0,0 +1,46 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+// TODO Fennec support in Bug #894525
+module.metadata = {
+ "engines": {
+ "Firefox": "*"
+ }
+}
+
+const { openTab, closeTab } = require("sdk/tabs/utils");
+const { Loader } = require("sdk/test/loader");
+const { setTimeout } = require("sdk/timers");
+
+exports["test unload tab observer"] = function(assert, done) {
+ let loader = Loader(module);
+
+ let window = loader.require("sdk/deprecated/window-utils").activeBrowserWindow;
+ let observer = loader.require("sdk/tabs/observer").observer;
+ let opened = 0;
+ let closed = 0;
+
+ observer.on("open", function onOpen(window) { opened++; });
+ observer.on("close", function onClose(window) { closed++; });
+
+ // Open and close tab to trigger observers.
+ closeTab(openTab(window, "data:text/html;charset=utf-8,tab-1"));
+
+ // Unload the module so that all listeners set by observer are removed.
+ loader.unload();
+
+ // Open and close tab once again.
+ closeTab(openTab(window, "data:text/html;charset=utf-8,tab-2"));
+
+ // Enqueuing asserts to make sure that assertion is not performed early.
+ setTimeout(function () {
+ assert.equal(1, opened, "observer open was called before unload only");
+ assert.equal(1, closed, "observer close was called before unload only");
+ done();
+ }, 0);
+};
+
+// require("test").run(exports);
diff --git a/addon-sdk/source/test/addons/e10s-tabs/lib/test-tab-utils.js b/addon-sdk/source/test/addons/e10s-tabs/lib/test-tab-utils.js
new file mode 100644
index 000000000..97c388b3c
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-tabs/lib/test-tab-utils.js
@@ -0,0 +1,67 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { getTabs } = require('sdk/tabs/utils');
+const { isWindowPBSupported, isTabPBSupported } = require('sdk/private-browsing/utils');
+const { browserWindows } = require('sdk/windows');
+const tabs = require('sdk/tabs');
+const { isPrivate } = require('sdk/private-browsing');
+const { openTab, closeTab, getTabContentWindow, getOwnerWindow } = require('sdk/tabs/utils');
+const { open, close } = require('sdk/window/helpers');
+const { windows } = require('sdk/window/utils');
+const { getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { fromIterator } = require('sdk/util/array');
+
+if (isWindowPBSupported) {
+ exports.testGetTabsPWPB = function(assert, done) {
+ let tabCount = getTabs().length;
+ let windowCount = browserWindows.length;
+
+ open(null, {
+ features: {
+ private: true,
+ toolbar: true,
+ chrome: true
+ }
+ }).then(function(window) {
+ assert.ok(isPrivate(window), 'new tab is private');
+
+ assert.equal(getTabs().length, tabCount, 'there are no new tabs found');
+ getTabs().forEach(function(tab) {
+ assert.equal(isPrivate(tab), false, 'all found tabs are not private');
+ assert.equal(isPrivate(getOwnerWindow(tab)), false, 'all found tabs are not private');
+ assert.equal(isPrivate(getTabContentWindow(tab)), false, 'all found tabs are not private');
+ });
+
+ assert.equal(browserWindows.length, windowCount, 'there are no new windows found');
+ fromIterator(browserWindows).forEach(function(window) {
+ assert.equal(isPrivate(window), false, 'all found windows are not private');
+ });
+
+ assert.equal(windows(null, {includePrivate: true}).length, 2, 'there are really two windows');
+
+ close(window).then(done);
+ });
+ };
+}
+else if (isTabPBSupported) {
+ exports.testGetTabsPTPB = function(assert, done) {
+ let startTabCount = getTabs().length;
+ let tab = openTab(getMostRecentBrowserWindow(), 'about:blank', {
+ isPrivate: true
+ });
+
+ assert.ok(isPrivate(getTabContentWindow(tab)), 'new tab is private');
+ let utils_tabs = getTabs();
+ assert.equal(utils_tabs.length, startTabCount + 1,
+ 'there are two tabs found');
+ assert.equal(utils_tabs[utils_tabs.length-1], tab,
+ 'the last tab is the opened tab');
+ assert.equal(browserWindows.length, 1, 'there is only one window');
+ closeTab(tab);
+
+ done();
+ };
+}
diff --git a/addon-sdk/source/test/addons/e10s-tabs/lib/test-tab.js b/addon-sdk/source/test/addons/e10s-tabs/lib/test-tab.js
new file mode 100644
index 000000000..0a94984a6
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-tabs/lib/test-tab.js
@@ -0,0 +1,87 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const tabs = require("sdk/tabs"); // From addon-kit
+const windowUtils = require("sdk/deprecated/window-utils");
+const app = require("sdk/system/xul-app");
+const { viewFor } = require("sdk/view/core");
+const { modelFor } = require("sdk/model/core");
+const { getTabId, isTab } = require("sdk/tabs/utils");
+const { defer } = require("sdk/lang/functional");
+
+exports["test behavior on close"] = function(assert, done) {
+ tabs.open({
+ url: "about:mozilla",
+ onReady: function(tab) {
+ assert.equal(tab.url, "about:mozilla", "Tab has the expected url");
+ // if another test ends before closing a tab then index != 1 here
+ assert.ok(tab.index >= 1, "Tab has the expected index, a value greater than 0");
+ tab.close(function () {
+ assert.equal(tab.url, undefined,
+ "After being closed, tab attributes are undefined (url)");
+ assert.equal(tab.index, undefined,
+ "After being closed, tab attributes are undefined (index)");
+ if (app.is("Firefox")) {
+ // Ensure that we can call destroy multiple times without throwing;
+ // Fennec doesn't use this internal utility
+ tab.destroy();
+ tab.destroy();
+ }
+
+ done();
+ });
+ }
+ });
+};
+
+exports["test viewFor(tab)"] = (assert, done) => {
+ // Note we defer handlers as length collection is updated after
+ // handler is invoked, so if test is finished before counnts are
+ // updated wrong length will show up in followup tests.
+ tabs.once("open", defer(tab => {
+ const view = viewFor(tab);
+ assert.ok(view, "view is returned");
+ assert.equal(getTabId(view), tab.id, "tab has a same id");
+
+ tab.close(defer(done));
+ }));
+
+ tabs.open({ url: "about:mozilla" });
+};
+
+exports["test modelFor(xulTab)"] = (assert, done) => {
+ tabs.open({
+ url: "about:mozilla",
+ onReady: tab => {
+ const view = viewFor(tab);
+ assert.ok(view, "view is returned");
+ assert.ok(isTab(view), "view is underlaying tab");
+ assert.equal(getTabId(view), tab.id, "tab has a same id");
+ assert.equal(modelFor(view), tab, "modelFor(view) is SDK tab");
+
+ tab.close(defer(done));
+ }
+ });
+};
+
+exports["test tab.readyState"] = (assert, done) => {
+ tabs.open({
+ url: "data:text/html;charset=utf-8,test_readyState",
+ onOpen: (tab) => {
+ assert.notEqual(["uninitialized", "loading"].indexOf(tab.readyState), -1,
+ "tab is either uninitialized or loading when onOpen");
+ },
+ onReady: (tab) => {
+ assert.notEqual(["interactive", "complete"].indexOf(tab.readyState), -1,
+ "tab is either interactive or complete when onReady");
+ },
+ onLoad: (tab) => {
+ assert.equal(tab.readyState, "complete", "tab is complete onLoad");
+ tab.close(defer(done));
+ }
+ });
+}
+
+// require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/addons/e10s-tabs/package.json b/addon-sdk/source/test/addons/e10s-tabs/package.json
new file mode 100644
index 000000000..45a11419d
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s-tabs/package.json
@@ -0,0 +1,11 @@
+{
+ "name": "e10s-tabs",
+ "title": "e10s-tabs",
+ "id": "jid1-ZZaXFHAPlHwbgw@jetpack",
+ "description": "run tab tests in e10s mode",
+ "author": "Tomislav Jovanovic",
+ "license": "MPL-2.0",
+ "version": "0.1.0",
+ "main": "./lib/main.js",
+ "e10s": true
+}
diff --git a/addon-sdk/source/test/addons/e10s/lib/main.js b/addon-sdk/source/test/addons/e10s/lib/main.js
new file mode 100644
index 000000000..1eee511c3
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s/lib/main.js
@@ -0,0 +1,65 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { getMostRecentBrowserWindow, isBrowser } = require('sdk/window/utils');
+const { promise: windowPromise, close, focus } = require('sdk/window/helpers');
+const { openTab, closeTab, getBrowserForTab } = require('sdk/tabs/utils');
+const { WindowTracker } = require('sdk/deprecated/window-utils');
+const { version, platform } = require('sdk/system');
+const { when } = require('sdk/system/unload');
+const tabs = require('sdk/tabs');
+
+const SKIPPING_TESTS = {
+ "test skip": (assert) => assert.pass("nothing to test here")
+};
+
+exports.testTabIsRemote = function(assert, done) {
+ const url = 'data:text/html,test-tab-is-remote';
+ let tab = openTab(getMostRecentBrowserWindow(), url);
+ assert.ok(tab.linkedBrowser.isRemoteBrowser, "The new tab should be remote");
+
+ // can't simply close a remote tab before it is loaded, bug 1006043
+ let mm = getBrowserForTab(tab).messageManager;
+ mm.addMessageListener('7', function listener() {
+ mm.removeMessageListener('7', listener);
+ tabs.once('close', done);
+ closeTab(tab);
+ })
+ mm.loadFrameScript('data:,sendAsyncMessage("7")', true);
+}
+
+// run e10s tests only on builds from trunk, fx-team, Nightly..
+if (!version.endsWith('a1')) {
+ module.exports = {};
+}
+
+function replaceWindow(remote) {
+ let next = null;
+ let old = getMostRecentBrowserWindow();
+ let promise = new Promise(resolve => {
+ let tracker = WindowTracker({
+ onTrack: window => {
+ if (window !== next)
+ return;
+ resolve(window);
+ tracker.unload();
+ }
+ });
+ })
+ next = old.OpenBrowserWindow({ remote });
+ return promise.then(focus).then(_ => close(old));
+}
+
+// bug 1054482 - e10s test addons time out on linux
+if (platform === 'linux') {
+ module.exports = SKIPPING_TESTS;
+ require('sdk/test/runner').runTestsFromModule(module);
+}
+else {
+ replaceWindow(true).then(_ =>
+ require('sdk/test/runner').runTestsFromModule(module));
+
+ when(_ => replaceWindow(false));
+}
diff --git a/addon-sdk/source/test/addons/e10s/package.json b/addon-sdk/source/test/addons/e10s/package.json
new file mode 100644
index 000000000..93039749a
--- /dev/null
+++ b/addon-sdk/source/test/addons/e10s/package.json
@@ -0,0 +1,10 @@
+{
+ "name": "e10s",
+ "title": "e10s",
+ "id": "jid1-DYaXFHAPlHwbgw@jetpack",
+ "description": "a basic e10s test add-on",
+ "author": "Tomislav Jovanovic",
+ "license": "MPL-2.0",
+ "version": "0.1.0",
+ "main": "./lib/main.js"
+}
diff --git a/addon-sdk/source/test/addons/embedded-webextension/main.js b/addon-sdk/source/test/addons/embedded-webextension/main.js
new file mode 100644
index 000000000..11249c504
--- /dev/null
+++ b/addon-sdk/source/test/addons/embedded-webextension/main.js
@@ -0,0 +1,159 @@
+const tabs = require("sdk/tabs");
+const webExtension = require('sdk/webextension');
+
+exports.testEmbeddedWebExtensionModuleInitializedException = function (assert) {
+ let actualErr;
+
+ assert.throws(
+ () => webExtension.initFromBootstrapAddonParam({webExtension: null}),
+ /'sdk\/webextension' module has been already initialized/,
+ "Got the expected exception if the module is initialized twice"
+ );
+};
+
+exports.testEmbeddedWebExtensionBackgroungPage = function* (assert) {
+ try {
+ const api = yield webExtension.startup();
+ assert.ok(api, `webextension waitForStartup promise successfully resolved`);
+
+ const apiSecondStartup = yield webExtension.startup();
+ assert.equal(api, apiSecondStartup, "Got the same API object from the second startup call");
+
+ const {browser} = api;
+
+ let messageListener;
+ let waitForBackgroundPageMessage = new Promise((resolve, reject) => {
+ let numExpectedMessage = 2;
+ messageListener = (msg, sender, sendReply) => {
+ numExpectedMessage -= 1;
+ if (numExpectedMessage == 1) {
+ assert.equal(msg, "bg->sdk message",
+ "Got the expected message from the background page");
+ sendReply("sdk reply");
+ } else if (numExpectedMessage == 0) {
+ assert.equal(msg, "sdk reply",
+ "The background page received the expected reply message");
+ resolve();
+ } else {
+ console.error("Unexpected message received", {msg,sender, numExpectedMessage});
+ assert.ok(false, `unexpected message received`);
+ reject();
+ }
+ };
+ browser.runtime.onMessage.addListener(messageListener);
+ });
+
+ let portListener;
+ let waitForBackgroundPagePort = new Promise((resolve, reject) => {
+ portListener = (port) => {
+ let numExpectedMessages = 2;
+ port.onMessage.addListener((msg) => {
+ numExpectedMessages -= 1;
+
+ if (numExpectedMessages == 1) {
+ // Check that the legacy context has been able to receive the first port message
+ // and reply with a port message to the background page.
+ assert.equal(msg, "bg->sdk port message",
+ "Got the expected port message from the background page");
+ port.postMessage("sdk->bg port message");
+ } else if (numExpectedMessages == 0) {
+ // Check that the background page has received the above port message.
+ assert.equal(msg, "bg received sdk->bg port message",
+ "The background page received the expected port message");
+ }
+ });
+
+ port.onDisconnect.addListener(() => {
+ assert.equal(numExpectedMessages, 0, "Got the expected number of port messages");
+ resolve();
+ });
+ };
+ browser.runtime.onConnect.addListener(portListener);
+ });
+
+ yield Promise.all([
+ waitForBackgroundPageMessage,
+ waitForBackgroundPagePort,
+ ]).then(() => {
+ browser.runtime.onMessage.removeListener(messageListener);
+ browser.runtime.onConnect.removeListener(portListener);
+ });
+
+ } catch (err) {
+ assert.fail(`Unexpected webextension startup exception: ${err} - ${err.stack}`);
+ }
+};
+
+exports.testEmbeddedWebExtensionContentScript = function* (assert, done) {
+ try {
+ const {browser} = yield webExtension.startup();
+ assert.ok(browser, `webextension startup promise resolved successfully to the API object`);
+
+ let messageListener;
+ let waitForContentScriptMessage = new Promise((resolve, reject) => {
+ let numExpectedMessage = 2;
+ messageListener = (msg, sender, sendReply) => {
+ numExpectedMessage -= 1;
+ if (numExpectedMessage == 1) {
+ assert.equal(msg, "content script->sdk message",
+ "Got the expected message from the content script");
+ sendReply("sdk reply");
+ } else if (numExpectedMessage == 0) {
+ assert.equal(msg, "sdk reply",
+ "The content script received the expected reply message");
+ resolve();
+ } else {
+ console.error("Unexpected message received", {msg,sender, numExpectedMessage});
+ assert.ok(false, `unexpected message received`);
+ reject();
+ }
+ };
+ browser.runtime.onMessage.addListener(messageListener);
+ });
+
+ let portListener;
+ let waitForContentScriptPort = new Promise((resolve, reject) => {
+ portListener = (port) => {
+ let numExpectedMessages = 2;
+ port.onMessage.addListener((msg) => {
+ numExpectedMessages -= 1;
+
+ if (numExpectedMessages == 1) {
+ assert.equal(msg, "content script->sdk port message",
+ "Got the expected message from the content script port");
+ port.postMessage("sdk->content script port message");
+ } else if (numExpectedMessages == 0) {
+ assert.equal(msg, "content script received sdk->content script port message",
+ "The content script received the expected port message");
+ }
+ });
+ port.onDisconnect.addListener(() => {
+ assert.equal(numExpectedMessages, 0, "Got the epected number of port messages");
+ resolve();
+ });
+ };
+ browser.runtime.onConnect.addListener(portListener);
+ });
+
+ let url = "http://example.org/";
+
+ var openedTab;
+ tabs.once('open', function onOpen(tab) {
+ openedTab = tab;
+ });
+ tabs.open(url);
+
+ yield Promise.all([
+ waitForContentScriptMessage,
+ waitForContentScriptPort,
+ ]).then(() => {
+ browser.runtime.onMessage.removeListener(messageListener);
+ browser.runtime.onConnect.removeListener(portListener);
+ openedTab.close();
+ });
+ } catch (err) {
+ assert.fail(`Unexpected webextension startup exception: ${err} - ${err.stack}`);
+ }
+};
+
+require("sdk/test/runner").runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/embedded-webextension/package.json b/addon-sdk/source/test/addons/embedded-webextension/package.json
new file mode 100644
index 000000000..25dec41c3
--- /dev/null
+++ b/addon-sdk/source/test/addons/embedded-webextension/package.json
@@ -0,0 +1,6 @@
+{
+ "id": "embedded-webextension@jetpack",
+ "version": "0.1.0",
+ "main": "./main.js",
+ "hasEmbeddedWebExtension": true
+}
diff --git a/addon-sdk/source/test/addons/embedded-webextension/webextension/background-page.js b/addon-sdk/source/test/addons/embedded-webextension/webextension/background-page.js
new file mode 100644
index 000000000..05e7a613b
--- /dev/null
+++ b/addon-sdk/source/test/addons/embedded-webextension/webextension/background-page.js
@@ -0,0 +1,10 @@
+browser.runtime.sendMessage("bg->sdk message", (reply) => {
+ browser.runtime.sendMessage(reply);
+});
+
+let port = browser.runtime.connect();
+port.onMessage.addListener((msg) => {
+ port.postMessage(`bg received ${msg}`);
+ port.disconnect();
+});
+port.postMessage("bg->sdk port message");
diff --git a/addon-sdk/source/test/addons/embedded-webextension/webextension/content-script.js b/addon-sdk/source/test/addons/embedded-webextension/webextension/content-script.js
new file mode 100644
index 000000000..a8770e623
--- /dev/null
+++ b/addon-sdk/source/test/addons/embedded-webextension/webextension/content-script.js
@@ -0,0 +1,10 @@
+browser.runtime.sendMessage("content script->sdk message", (reply) => {
+ browser.runtime.sendMessage(reply);
+});
+
+let port = browser.runtime.connect();
+port.onMessage.addListener((msg) => {
+ port.postMessage(`content script received ${msg}`);
+ port.disconnect();
+});
+port.postMessage("content script->sdk port message");
diff --git a/addon-sdk/source/test/addons/embedded-webextension/webextension/manifest.json b/addon-sdk/source/test/addons/embedded-webextension/webextension/manifest.json
new file mode 100644
index 000000000..d2188e7ba
--- /dev/null
+++ b/addon-sdk/source/test/addons/embedded-webextension/webextension/manifest.json
@@ -0,0 +1,18 @@
+{
+ "name": "Test SDK Embedded WebExtension",
+ "description": "",
+ "version": "0.1.0",
+ "applications": {
+ "gecko": {
+ "id": "embedded-webextension@jetpack"
+ }
+ },
+ "manifest_version": 2,
+ "permissions": ["tabs"],
+ "background": {
+ "scripts": ["background-page.js"]
+ },
+ "content_scripts": [
+ {"matches": ["<all_urls>"], "js": ["content-script.js"]}
+ ]
+}
diff --git a/addon-sdk/source/test/addons/jetpack-addon.ini b/addon-sdk/source/test/addons/jetpack-addon.ini
new file mode 100644
index 000000000..c1c8f060d
--- /dev/null
+++ b/addon-sdk/source/test/addons/jetpack-addon.ini
@@ -0,0 +1,48 @@
+[addon-manager.xpi]
+[author-email.xpi]
+[child_process.xpi]
+[chrome.xpi]
+[content-permissions.xpi]
+[content-script-messages-latency.xpi]
+[contributors.xpi]
+[curly-id.xpi]
+[developers.xpi]
+[e10s.xpi]
+skip-if = true
+[e10s-content.xpi]
+skip-if = true
+[e10s-l10n.xpi]
+skip-if = true
+[e10s-remote.xpi]
+skip-if = true
+[e10s-tabs.xpi]
+skip-if = true
+[embedded-webextension.xpi]
+[l10n.xpi]
+[l10n-properties.xpi]
+[layout-change.xpi]
+[main.xpi]
+[name-in-numbers.xpi]
+[name-in-numbers-plus.xpi]
+[packaging.xpi]
+[packed.xpi]
+[page-mod-debugger-post.xpi]
+[page-mod-debugger-pre.xpi]
+[page-worker.xpi]
+skip-if = true # Bug 1288619 and Bug 1288708
+[places.xpi]
+[predefined-id-with-at.xpi]
+[preferences-branch.xpi]
+[private-browsing-supported.xpi]
+skip-if = true
+[remote.xpi]
+[require.xpi]
+[self.xpi]
+[simple-prefs.xpi]
+[simple-prefs-l10n.xpi]
+[simple-prefs-regression.xpi]
+[standard-id.xpi]
+[tab-close-on-startup.xpi]
+[toolkit-require-reload.xpi]
+[translators.xpi]
+[unsafe-content-script.xpi]
diff --git a/addon-sdk/source/test/addons/l10n-properties/app-extension/application.ini b/addon-sdk/source/test/addons/l10n-properties/app-extension/application.ini
new file mode 100644
index 000000000..6cec69a16
--- /dev/null
+++ b/addon-sdk/source/test/addons/l10n-properties/app-extension/application.ini
@@ -0,0 +1,11 @@
+[App]
+Vendor=Varma
+Name=Test App
+Version=1.0
+BuildID=20060101
+Copyright=Copyright (c) 2009 Atul Varma
+ID=xulapp@toolness.com
+
+[Gecko]
+MinVersion=1.9.2.0
+MaxVersion=2.0.*
diff --git a/addon-sdk/source/test/addons/l10n-properties/app-extension/bootstrap.js b/addon-sdk/source/test/addons/l10n-properties/app-extension/bootstrap.js
new file mode 100644
index 000000000..fbb9b5186
--- /dev/null
+++ b/addon-sdk/source/test/addons/l10n-properties/app-extension/bootstrap.js
@@ -0,0 +1,339 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// @see http://dxr.mozilla.org/mozilla-central/source/js/src/xpconnect/loader/mozJSComponentLoader.cpp
+
+'use strict';
+
+// IMPORTANT: Avoid adding any initialization tasks here, if you need to do
+// something before add-on is loaded consider addon/runner module instead!
+
+const { classes: Cc, Constructor: CC, interfaces: Ci, utils: Cu,
+ results: Cr, manager: Cm } = Components;
+const ioService = Cc['@mozilla.org/network/io-service;1'].
+ getService(Ci.nsIIOService);
+const resourceHandler = ioService.getProtocolHandler('resource').
+ QueryInterface(Ci.nsIResProtocolHandler);
+const systemPrincipal = CC('@mozilla.org/systemprincipal;1', 'nsIPrincipal')();
+const scriptLoader = Cc['@mozilla.org/moz/jssubscript-loader;1'].
+ getService(Ci.mozIJSSubScriptLoader);
+const prefService = Cc['@mozilla.org/preferences-service;1'].
+ getService(Ci.nsIPrefService).
+ QueryInterface(Ci.nsIPrefBranch);
+const appInfo = Cc["@mozilla.org/xre/app-info;1"].
+ getService(Ci.nsIXULAppInfo);
+const vc = Cc["@mozilla.org/xpcom/version-comparator;1"].
+ getService(Ci.nsIVersionComparator);
+
+const { NetUtil } = Cu.import("resource://gre/modules/NetUtil.jsm");
+
+const REASON = [ 'unknown', 'startup', 'shutdown', 'enable', 'disable',
+ 'install', 'uninstall', 'upgrade', 'downgrade' ];
+
+const bind = Function.call.bind(Function.bind);
+
+var loader = null;
+var unload = null;
+var cuddlefishSandbox = null;
+var nukeTimer = null;
+
+// Utility function that synchronously reads local resource from the given
+// `uri` and returns content string.
+function readURI(uri) {
+ let channel = NetUtil.newChannel({
+ uri: NetUtil.newURI(uri, "UTF-8"),
+ loadUsingSystemPrincipal: true
+ });
+ let stream = channel.open2();
+
+ let cstream = Cc['@mozilla.org/intl/converter-input-stream;1'].
+ createInstance(Ci.nsIConverterInputStream);
+ cstream.init(stream, 'UTF-8', 0, 0);
+
+ let str = {};
+ let data = '';
+ let read = 0;
+ do {
+ read = cstream.readString(0xffffffff, str);
+ data += str.value;
+ } while (read != 0);
+
+ cstream.close();
+
+ return data;
+}
+
+// We don't do anything on install & uninstall yet, but in a future
+// we should allow add-ons to cleanup after uninstall.
+function install(data, reason) {}
+function uninstall(data, reason) {}
+
+function startup(data, reasonCode) {
+ try {
+ let reason = REASON[reasonCode];
+ // URI for the root of the XPI file.
+ // 'jar:' URI if the addon is packed, 'file:' URI otherwise.
+ // (Used by l10n module in order to fetch `locale` folder)
+ let rootURI = data.resourceURI.spec;
+
+ // TODO: Maybe we should perform read harness-options.json asynchronously,
+ // since we can't do anything until 'sessionstore-windows-restored' anyway.
+ let options = JSON.parse(readURI(rootURI + './harness-options.json'));
+
+ let id = options.jetpackID;
+ let name = options.name;
+
+ // Clean the metadata
+ options.metadata[name]['permissions'] = options.metadata[name]['permissions'] || {};
+
+ // freeze the permissionss
+ Object.freeze(options.metadata[name]['permissions']);
+ // freeze the metadata
+ Object.freeze(options.metadata[name]);
+
+ // Register a new resource 'domain' for this addon which is mapping to
+ // XPI's `resources` folder.
+ // Generate the domain name by using jetpack ID, which is the extension ID
+ // by stripping common characters that doesn't work as a domain name:
+ let uuidRe =
+ /^\{([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\}$/;
+
+ let domain = id.
+ toLowerCase().
+ replace(/@/g, '-at-').
+ replace(/\./g, '-dot-').
+ replace(uuidRe, '$1');
+
+ let prefixURI = 'resource://' + domain + '/';
+ let resourcesURI = ioService.newURI(rootURI + '/resources/', null, null);
+ resourceHandler.setSubstitution(domain, resourcesURI);
+
+ // Create path to URLs mapping supported by loader.
+ let paths = {
+ // Relative modules resolve to add-on package lib
+ './': prefixURI + name + '/lib/',
+ './tests/': prefixURI + name + '/tests/',
+ '': 'resource://gre/modules/commonjs/'
+ };
+
+ // Maps addon lib and tests ressource folders for each package
+ paths = Object.keys(options.metadata).reduce(function(result, name) {
+ result[name + '/'] = prefixURI + name + '/lib/'
+ result[name + '/tests/'] = prefixURI + name + '/tests/'
+ return result;
+ }, paths);
+
+ // We need to map tests folder when we run sdk tests whose package name
+ // is stripped
+ if (name == 'addon-sdk')
+ paths['tests/'] = prefixURI + name + '/tests/';
+
+ let useBundledSDK = options['force-use-bundled-sdk'];
+ if (!useBundledSDK) {
+ try {
+ useBundledSDK = prefService.getBoolPref("extensions.addon-sdk.useBundledSDK");
+ }
+ catch (e) {
+ // Pref doesn't exist, allow using Firefox shipped SDK
+ }
+ }
+
+ // Starting with Firefox 21.0a1, we start using modules shipped into firefox
+ // Still allow using modules from the xpi if the manifest tell us to do so.
+ // And only try to look for sdk modules in xpi if the xpi actually ship them
+ if (options['is-sdk-bundled'] &&
+ (vc.compare(appInfo.version, '21.0a1') < 0 || useBundledSDK)) {
+ // Maps sdk module folders to their resource folder
+ paths[''] = prefixURI + 'addon-sdk/lib/';
+ // test.js is usually found in root commonjs or SDK_ROOT/lib/ folder,
+ // so that it isn't shipped in the xpi. Keep a copy of it in sdk/ folder
+ // until we no longer support SDK modules in XPI:
+ paths['test'] = prefixURI + 'addon-sdk/lib/sdk/test.js';
+ }
+
+ // Retrieve list of module folder overloads based on preferences in order to
+ // eventually used a local modules instead of files shipped into Firefox.
+ let branch = prefService.getBranch('extensions.modules.' + id + '.path');
+ paths = branch.getChildList('', {}).reduce(function (result, name) {
+ // Allows overloading of any sub folder by replacing . by / in pref name
+ let path = name.substr(1).split('.').join('/');
+ // Only accept overloading folder by ensuring always ending with `/`
+ if (path) path += '/';
+ let fileURI = branch.getCharPref(name);
+
+ // On mobile, file URI has to end with a `/` otherwise, setSubstitution
+ // takes the parent folder instead.
+ if (fileURI[fileURI.length-1] !== '/')
+ fileURI += '/';
+
+ // Maps the given file:// URI to a resource:// in order to avoid various
+ // failure that happens with file:// URI and be close to production env
+ let resourcesURI = ioService.newURI(fileURI, null, null);
+ let resName = 'extensions.modules.' + domain + '.commonjs.path' + name;
+ resourceHandler.setSubstitution(resName, resourcesURI);
+
+ result[path] = 'resource://' + resName + '/';
+ return result;
+ }, paths);
+
+ // Make version 2 of the manifest
+ let manifest = options.manifest;
+
+ // Import `cuddlefish.js` module using a Sandbox and bootstrap loader.
+ let cuddlefishPath = 'loader/cuddlefish.js';
+ let cuddlefishURI = 'resource://gre/modules/commonjs/sdk/' + cuddlefishPath;
+ if (paths['sdk/']) { // sdk folder has been overloaded
+ // (from pref, or cuddlefish is still in the xpi)
+ cuddlefishURI = paths['sdk/'] + cuddlefishPath;
+ }
+ else if (paths['']) { // root modules folder has been overloaded
+ cuddlefishURI = paths[''] + 'sdk/' + cuddlefishPath;
+ }
+
+ cuddlefishSandbox = loadSandbox(cuddlefishURI);
+ let cuddlefish = cuddlefishSandbox.exports;
+
+ // Normalize `options.mainPath` so that it looks like one that will come
+ // in a new version of linker.
+ let main = options.mainPath;
+
+ unload = cuddlefish.unload;
+ loader = cuddlefish.Loader({
+ paths: paths,
+ // modules manifest.
+ manifest: manifest,
+
+ // Add-on ID used by different APIs as a unique identifier.
+ id: id,
+ // Add-on name.
+ name: name,
+ // Add-on version.
+ version: options.metadata[name].version,
+ // Add-on package descriptor.
+ metadata: options.metadata[name],
+ // Add-on load reason.
+ loadReason: reason,
+
+ prefixURI: prefixURI,
+ // Add-on URI.
+ rootURI: rootURI,
+ // options used by system module.
+ // File to write 'OK' or 'FAIL' (exit code emulation).
+ resultFile: options.resultFile,
+ // Arguments passed as --static-args
+ staticArgs: options.staticArgs,
+
+ // Arguments related to test runner.
+ modules: {
+ '@test/options': {
+ allTestModules: options.allTestModules,
+ iterations: options.iterations,
+ filter: options.filter,
+ profileMemory: options.profileMemory,
+ stopOnError: options.stopOnError,
+ verbose: options.verbose,
+ parseable: options.parseable,
+ checkMemory: options.check_memory,
+ }
+ }
+ });
+
+ let module = cuddlefish.Module('sdk/loader/cuddlefish', cuddlefishURI);
+ let require = cuddlefish.Require(loader, module);
+
+ require('sdk/addon/runner').startup(reason, {
+ loader: loader,
+ main: main,
+ prefsURI: rootURI + 'defaults/preferences/prefs.js'
+ });
+ } catch (error) {
+ dump('Bootstrap error: ' +
+ (error.message ? error.message : String(error)) + '\n' +
+ (error.stack || error.fileName + ': ' + error.lineNumber) + '\n');
+ throw error;
+ }
+};
+
+function loadSandbox(uri) {
+ let proto = {
+ sandboxPrototype: {
+ loadSandbox: loadSandbox,
+ ChromeWorker: ChromeWorker
+ }
+ };
+ let sandbox = Cu.Sandbox(systemPrincipal, proto);
+ // Create a fake commonjs environnement just to enable loading loader.js
+ // correctly
+ sandbox.exports = {};
+ sandbox.module = { uri: uri, exports: sandbox.exports };
+ sandbox.require = function (id) {
+ if (id !== "chrome")
+ throw new Error("Bootstrap sandbox `require` method isn't implemented.");
+
+ return Object.freeze({ Cc: Cc, Ci: Ci, Cu: Cu, Cr: Cr, Cm: Cm,
+ CC: bind(CC, Components), components: Components,
+ ChromeWorker: ChromeWorker });
+ };
+ scriptLoader.loadSubScript(uri, sandbox, 'UTF-8');
+ return sandbox;
+}
+
+function unloadSandbox(sandbox) {
+ if (Cu.getClassName(sandbox, true) == "Sandbox")
+ Cu.nukeSandbox(sandbox);
+}
+
+function setTimeout(callback, delay) {
+ let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
+ timer.initWithCallback({ notify: callback }, delay,
+ Ci.nsITimer.TYPE_ONE_SHOT);
+ return timer;
+}
+
+function shutdown(data, reasonCode) {
+ let reason = REASON[reasonCode];
+ if (loader) {
+ unload(loader, reason);
+ unload = null;
+
+ // Don't waste time cleaning up if the application is shutting down
+ if (reason != "shutdown") {
+ // Avoid leaking all modules when something goes wrong with one particular
+ // module. Do not clean it up immediatly in order to allow executing some
+ // actions on addon disabling.
+ // We need to keep a reference to the timer, otherwise it is collected
+ // and won't ever fire.
+ nukeTimer = setTimeout(nukeModules, 1000);
+ }
+ }
+};
+
+function nukeModules() {
+ nukeTimer = null;
+ // module objects store `exports` which comes from sandboxes
+ // We should avoid keeping link to these object to avoid leaking sandboxes
+ for (let key in loader.modules) {
+ delete loader.modules[key];
+ }
+ // Direct links to sandboxes should be removed too
+ for (let key in loader.sandboxes) {
+ let sandbox = loader.sandboxes[key];
+ delete loader.sandboxes[key];
+ // Bug 775067: From FF17 we can kill all CCW from a given sandbox
+ unloadSandbox(sandbox);
+ }
+ loader = null;
+
+ // both `toolkit/loader` and `system/xul-app` are loaded as JSM's via
+ // `cuddlefish.js`, and needs to be unloaded to avoid memory leaks, when
+ // the addon is unload.
+
+ unloadSandbox(cuddlefishSandbox.loaderSandbox);
+ unloadSandbox(cuddlefishSandbox.xulappSandbox);
+
+ // Bug 764840: We need to unload cuddlefish otherwise it will stay alive
+ // and keep a reference to this compartment.
+ unloadSandbox(cuddlefishSandbox);
+ cuddlefishSandbox = null;
+}
diff --git a/addon-sdk/source/test/addons/l10n-properties/app-extension/install.rdf b/addon-sdk/source/test/addons/l10n-properties/app-extension/install.rdf
new file mode 100644
index 000000000..8fc710557
--- /dev/null
+++ b/addon-sdk/source/test/addons/l10n-properties/app-extension/install.rdf
@@ -0,0 +1,33 @@
+<?xml version="1.0"?>
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+
+<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ xmlns:em="http://www.mozilla.org/2004/em-rdf#">
+ <Description about="urn:mozilla:install-manifest">
+ <em:id>xulapp@toolness.com</em:id>
+ <em:version>1.0</em:version>
+ <em:type>2</em:type>
+ <em:bootstrap>true</em:bootstrap>
+ <em:unpack>false</em:unpack>
+
+ <!-- Firefox -->
+ <em:targetApplication>
+ <Description>
+ <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
+ <em:minVersion>21.0</em:minVersion>
+ <em:maxVersion>25.0a1</em:maxVersion>
+ </Description>
+ </em:targetApplication>
+
+ <!-- Front End MetaData -->
+ <em:name>Test App</em:name>
+ <em:description>Harness for tests.</em:description>
+ <em:creator>Mozilla Corporation</em:creator>
+ <em:homepageURL></em:homepageURL>
+ <em:optionsType></em:optionsType>
+ <em:updateURL></em:updateURL>
+ </Description>
+</RDF>
diff --git a/addon-sdk/source/test/addons/l10n-properties/app-extension/locale/en-GB.properties b/addon-sdk/source/test/addons/l10n-properties/app-extension/locale/en-GB.properties
new file mode 100644
index 000000000..d14e6de0b
--- /dev/null
+++ b/addon-sdk/source/test/addons/l10n-properties/app-extension/locale/en-GB.properties
@@ -0,0 +1,28 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+Translated= Yes
+
+text-content=no <b>HTML</b> injection
+
+downloadsCount=%d downloads
+downloadsCount[one]=one download
+
+pluralTest=fallback to other
+pluralTest[zero]=optional zero form
+
+explicitPlural[one]=one
+explicitPlural[other]=other
+
+# You can use unicode char escaping in order to inject space at the beginning/
+# end of your string. (Regular spaces are automatically ignore by .properties
+# file parser)
+unicodeEscape = \u0020\u0040\u0020
+# this string equals to " @ "
+
+# bug 1033309 plurals with multiple placeholders
+first_identifier[one]=first entry is %s and the second one is %s.
+first_identifier=the entries are %s and %s.
+second_identifier[other]=first entry is %s and the second one is %s.
+third_identifier=first entry is %s and the second one is %s.
diff --git a/addon-sdk/source/test/addons/l10n-properties/app-extension/locale/en-US.properties b/addon-sdk/source/test/addons/l10n-properties/app-extension/locale/en-US.properties
new file mode 100644
index 000000000..487fceb1d
--- /dev/null
+++ b/addon-sdk/source/test/addons/l10n-properties/app-extension/locale/en-US.properties
@@ -0,0 +1,22 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+Translated= Yes
+
+text-content=no <b>HTML</b> injection
+
+downloadsCount=%d downloads
+downloadsCount[one]=one download
+
+pluralTest=fallback to other
+pluralTest[zero]=optional zero form
+
+explicitPlural[one]=one
+explicitPlural[other]=other
+
+# You can use unicode char escaping in order to inject space at the beginning/
+# end of your string. (Regular spaces are automatically ignore by .properties
+# file parser)
+unicodeEscape = \u0020\u0040\u0020
+# this string equals to " @ "
diff --git a/addon-sdk/source/test/addons/l10n-properties/app-extension/locale/eo.properties b/addon-sdk/source/test/addons/l10n-properties/app-extension/locale/eo.properties
new file mode 100644
index 000000000..a979fca1a
--- /dev/null
+++ b/addon-sdk/source/test/addons/l10n-properties/app-extension/locale/eo.properties
@@ -0,0 +1,5 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+Translated= jes
diff --git a/addon-sdk/source/test/addons/l10n-properties/app-extension/locale/fr-FR.properties b/addon-sdk/source/test/addons/l10n-properties/app-extension/locale/fr-FR.properties
new file mode 100644
index 000000000..2c5ffbb17
--- /dev/null
+++ b/addon-sdk/source/test/addons/l10n-properties/app-extension/locale/fr-FR.properties
@@ -0,0 +1,14 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+Translated= Oui
+
+placeholderString= Placeholder %s
+
+# Plural forms
+%d downloads=%d téléchargements
+%d downloads[one]=%d téléchargement
+
+downloadsCount=%d téléchargements
+downloadsCount[one]=%d téléchargement
diff --git a/addon-sdk/source/test/addons/l10n-properties/data/test-localization.html b/addon-sdk/source/test/addons/l10n-properties/data/test-localization.html
new file mode 100644
index 000000000..5428863ad
--- /dev/null
+++ b/addon-sdk/source/test/addons/l10n-properties/data/test-localization.html
@@ -0,0 +1,24 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<html>
+ <head>
+ <meta charset="UTF-8">
+ <title>HTML Localization</title>
+ </head>
+ <body>
+ <div data-l10n-id="Not translated">Kept as-is</div>
+ <ul data-l10n-id="Translated">
+ <li>Inner html content is replaced,</li>
+ <li data-l10n-id="text-content">
+ Elements with data-l10n-id attribute whose parent element is translated
+ will be replaced by the content of the translation.
+ </li>
+ </ul>
+ <div data-l10n-id="text-content">No</div>
+ <div data-l10n-id="Translated">
+ A data-l10n-id value can be used in multiple elements
+ </div>
+ </body>
+</html
diff --git a/addon-sdk/source/test/addons/l10n-properties/main.js b/addon-sdk/source/test/addons/l10n-properties/main.js
new file mode 100644
index 000000000..b2ca0b191
--- /dev/null
+++ b/addon-sdk/source/test/addons/l10n-properties/main.js
@@ -0,0 +1,202 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const prefs = require("sdk/preferences/service");
+const { Loader } = require('sdk/test/loader');
+const { resolveURI } = require('toolkit/loader');
+const { rootURI } = require("@loader/options");
+const { usingJSON } = require('sdk/l10n/json/core');
+
+const PREF_MATCH_OS_LOCALE = "intl.locale.matchOS";
+const PREF_SELECTED_LOCALE = "general.useragent.locale";
+
+function setLocale(locale) {
+ prefs.set(PREF_MATCH_OS_LOCALE, false);
+ prefs.set(PREF_SELECTED_LOCALE, locale);
+}
+
+function resetLocale() {
+ prefs.reset(PREF_MATCH_OS_LOCALE);
+ prefs.reset(PREF_SELECTED_LOCALE);
+}
+
+function definePseudo(loader, id, exports) {
+ let uri = resolveURI(id, loader.mapping);
+ loader.modules[uri] = { exports: exports };
+}
+
+function createTest(locale, testFunction) {
+ return function (assert, done) {
+ let loader = Loader(module);
+ // Change the locale before loading new l10n modules in order to load
+ // the right .json file
+ setLocale(locale);
+ // Initialize main l10n module in order to load new locale files
+ loader.require("sdk/l10n/loader").
+ load(rootURI).
+ then(function success(data) {
+ definePseudo(loader, '@l10n/data', data);
+ // Execute the given test function
+ try {
+ testFunction(assert, loader, function onDone() {
+ loader.unload();
+ resetLocale();
+ done();
+ });
+ }
+ catch(e) {
+ console.exception(e);
+ }
+ },
+ function failure(error) {
+ assert.fail("Unable to load locales: " + error);
+ });
+ };
+}
+
+exports.testExactMatching = createTest("fr-FR", function(assert, loader, done) {
+ let _ = loader.require("sdk/l10n").get;
+ assert.equal(_("Not translated"), "Not translated",
+ "Key not translated");
+ assert.equal(_("Translated"), "Oui",
+ "Simple key translated");
+
+ // Placeholders
+ assert.equal(_("placeholderString", "works"), "Placeholder works",
+ "Value with placeholder");
+ assert.equal(_("Placeholder %s", "works"), "Placeholder works",
+ "Key without value but with placeholder");
+ assert.equal(_("Placeholders %2s %1s %s.", "working", "are", "correctly"),
+ "Placeholders are working correctly.",
+ "Multiple placeholders");
+
+ // Plurals
+ assert.equal(_("downloadsCount", 0),
+ "0 téléchargement",
+ "PluralForm form 'one' for 0 in french");
+ assert.equal(_("downloadsCount", 1),
+ "1 téléchargement",
+ "PluralForm form 'one' for 1 in french");
+ assert.equal(_("downloadsCount", 2),
+ "2 téléchargements",
+ "PluralForm form 'other' for n > 1 in french");
+
+ done();
+});
+
+exports.testHtmlLocalization = createTest("en-GB", function(assert, loader, done) {
+ // Ensure initing html component that watch document creations
+ // Note that this module is automatically initialized in
+ // cuddlefish.js:Loader.main in regular addons. But it isn't for unit tests.
+ let loaderHtmlL10n = loader.require("sdk/l10n/html");
+ loaderHtmlL10n.enable();
+
+ let uri = require("sdk/self").data.url("test-localization.html");
+ let worker = loader.require("sdk/page-worker").Page({
+ contentURL: uri,
+ contentScript: "new " + function ContentScriptScope() {
+ let nodes = document.body.querySelectorAll("*[data-l10n-id]");
+ self.postMessage([nodes[0].innerHTML,
+ nodes[1].innerHTML,
+ nodes[2].innerHTML,
+ nodes[3].innerHTML]);
+ },
+ onMessage: function (data) {
+ assert.equal(
+ data[0],
+ "Kept as-is",
+ "Nodes with unknown id in .properties are kept 'as-is'"
+ );
+ assert.equal(data[1], "Yes", "HTML is translated");
+ assert.equal(
+ data[2],
+ "no &lt;b&gt;HTML&lt;/b&gt; injection",
+ "Content from .properties is text content; HTML can't be injected."
+ );
+ assert.equal(data[3], "Yes", "Multiple elements with same data-l10n-id are accepted.");
+
+ done();
+ }
+ });
+});
+
+exports.testEnUsLocaleName = createTest("en-GB", function(assert, loader, done) {
+ let _ = loader.require("sdk/l10n").get;
+
+ assert.equal(_("Not translated"), "Not translated",
+ "String w/o translation is kept as-is");
+ assert.equal(_("Translated"), "Yes",
+ "String with translation is correctly translated");
+
+ // Check Unicode char escaping sequences
+ assert.equal(_("unicodeEscape"), " @ ",
+ "Unicode escaped sequances are correctly converted");
+
+ // Check plural forms regular matching
+ assert.equal(_("downloadsCount", 0),
+ "0 downloads",
+ "PluralForm form 'other' for 0 in english");
+ assert.equal(_("downloadsCount", 1),
+ "one download",
+ "PluralForm form 'one' for 1 in english");
+ assert.equal(_("downloadsCount", 2),
+ "2 downloads",
+ "PluralForm form 'other' for n != 1 in english");
+
+ // Check optional plural forms
+ assert.equal(_("pluralTest", 0),
+ "optional zero form",
+ "PluralForm form 'zero' can be optionaly specified. (Isn't mandatory in english)");
+ assert.equal(_("pluralTest", 1),
+ "fallback to other",
+ "If the specific plural form is missing, we fallback to 'other'");
+
+ // Ensure that we can omit specifying the generic key without [other]
+ // key[one] = ...
+ // key[other] = ... # Instead of `key = ...`
+ assert.equal(_("explicitPlural", 1),
+ "one",
+ "PluralForm form can be omitting generic key [i.e. without ...[other] at end of key)");
+ assert.equal(_("explicitPlural", 10),
+ "other",
+ "PluralForm form can be omitting generic key [i.e. without ...[other] at end of key)");
+
+ assert.equal(_("first_identifier", "ONE", "TWO"), "the entries are ONE and TWO.", "first_identifier no count");
+ assert.equal(_("first_identifier", 0, "ONE", "TWO"), "the entries are ONE and TWO.", "first_identifier with count = 0");
+ assert.equal(_("first_identifier", 1, "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "first_identifier with count = 1");
+ assert.equal(_("first_identifier", 2, "ONE", "TWO"), "the entries are ONE and TWO.", "first_identifier with count = 2");
+
+ assert.equal(_("second_identifier", "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "second_identifier with no count");
+ assert.equal(_("second_identifier", 0, "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "second_identifier with count = 0");
+ assert.equal(_("second_identifier", 1, "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "second_identifier with count = 1");
+ assert.equal(_("second_identifier", 2, "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "second_identifier with count = 2");
+
+ assert.equal(_("third_identifier", "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "third_identifier with no count");
+ assert.equal(_("third_identifier", 0, "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "third_identifier with count = 0");
+ assert.equal(_("third_identifier", 2, "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "third_identifier with count = 2");
+
+ done();
+});
+
+exports.testUsingJSON = function(assert) {
+ assert.equal(usingJSON, false, 'not using json');
+}
+
+exports.testShortLocaleName = createTest("eo", function(assert, loader, done) {
+ let _ = loader.require("sdk/l10n").get;
+ assert.equal(_("Not translated"), "Not translated",
+ "String w/o translation is kept as-is");
+ assert.equal(_("Translated"), "jes",
+ "String with translation is correctly translated");
+
+ done();
+});
+
+
+// Before running tests, disable HTML service which is automatially enabled
+// in api-utils/addon/runner.js
+require('sdk/l10n/html').disable();
+
+require("sdk/test/runner").runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/l10n-properties/package.json b/addon-sdk/source/test/addons/l10n-properties/package.json
new file mode 100644
index 000000000..1747298cb
--- /dev/null
+++ b/addon-sdk/source/test/addons/l10n-properties/package.json
@@ -0,0 +1,6 @@
+{
+ "id": "test-l10n@jetpack",
+ "title": "Test L10n",
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/l10n/data/test-localization.html b/addon-sdk/source/test/addons/l10n/data/test-localization.html
new file mode 100644
index 000000000..5646946da
--- /dev/null
+++ b/addon-sdk/source/test/addons/l10n/data/test-localization.html
@@ -0,0 +1,29 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<html>
+ <head>
+ <meta charset="UTF-8">
+ <title>HTML Localization</title>
+ </head>
+ <body>
+ <div data-l10n-id="Not translated">Kept as-is</div>
+ <ul data-l10n-id="Translated">
+ <li>Inner html content is replaced,</li>
+ <li data-l10n-id="text-content">
+ Elements with data-l10n-id attribute whose parent element is translated
+ will be replaced by the content of the translation.
+ </li>
+ </ul>
+ <div data-l10n-id="text-content">No</div>
+ <div data-l10n-id="Translated">
+ A data-l10n-id value can be used in multiple elements
+ </div>
+ <a data-l10n-id="link-attributes" title="Certain whitelisted attributes get translated too" alt="No" accesskey="A"></a>
+ <input data-l10n-id="input" type="text" placeholder="Form placeholders are translateable">
+ <menu>
+ <menuitem data-l10n-id="contextitem" label="Labels of select options and context menus are translateable">
+ </menu>
+ </body>
+</html
diff --git a/addon-sdk/source/test/addons/l10n/locale/en.properties b/addon-sdk/source/test/addons/l10n/locale/en.properties
new file mode 100644
index 000000000..c9e53ecb3
--- /dev/null
+++ b/addon-sdk/source/test/addons/l10n/locale/en.properties
@@ -0,0 +1,38 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+Translated= Yes
+
+text-content=no <b>HTML</b> injection
+
+downloadsCount=%d downloads
+downloadsCount[one]=one download
+
+pluralTest=fallback to other
+pluralTest[zero]=optional zero form
+
+explicitPlural[one]=one
+explicitPlural[other]=other
+
+# You can use unicode char escaping in order to inject space at the beginning/
+# end of your string. (Regular spaces are automatically ignore by .properties
+# file parser)
+unicodeEscape = \u0020\u0040\u0020
+# this string equals to " @ "
+
+# bug 1033309 plurals with multiple placeholders
+first_identifier[one]=first entry is %s and the second one is %s.
+first_identifier=the entries are %s and %s.
+second_identifier[other]=first entry is %s and the second one is %s.
+third_identifier=first entry is %s and the second one is %s.
+
+# bug 824489 allow translation of element attributes
+link-attributes.title=Yes
+link-attributes.alt=Yes
+link-attributes.accesskey=B
+input.placeholder=Yes
+contextitem.label=Yes
+link-attributes.ariaLabel=Yes
+link-attributes.ariaValueText=Value
+link-attributes.ariaMozHint=Hint
diff --git a/addon-sdk/source/test/addons/l10n/locale/eo.properties b/addon-sdk/source/test/addons/l10n/locale/eo.properties
new file mode 100644
index 000000000..a979fca1a
--- /dev/null
+++ b/addon-sdk/source/test/addons/l10n/locale/eo.properties
@@ -0,0 +1,5 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+Translated= jes
diff --git a/addon-sdk/source/test/addons/l10n/locale/fr-FR.properties b/addon-sdk/source/test/addons/l10n/locale/fr-FR.properties
new file mode 100644
index 000000000..2c5ffbb17
--- /dev/null
+++ b/addon-sdk/source/test/addons/l10n/locale/fr-FR.properties
@@ -0,0 +1,14 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+Translated= Oui
+
+placeholderString= Placeholder %s
+
+# Plural forms
+%d downloads=%d téléchargements
+%d downloads[one]=%d téléchargement
+
+downloadsCount=%d téléchargements
+downloadsCount[one]=%d téléchargement
diff --git a/addon-sdk/source/test/addons/l10n/main.js b/addon-sdk/source/test/addons/l10n/main.js
new file mode 100644
index 000000000..9409df7ef
--- /dev/null
+++ b/addon-sdk/source/test/addons/l10n/main.js
@@ -0,0 +1,289 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const prefs = require("sdk/preferences/service");
+const { Loader } = require('sdk/test/loader');
+const { resolveURI } = require('toolkit/loader');
+const { rootURI, isNative } = require("@loader/options");
+const { usingJSON } = require('sdk/l10n/json/core');
+
+const PREF_MATCH_OS_LOCALE = "intl.locale.matchOS";
+const PREF_SELECTED_LOCALE = "general.useragent.locale";
+
+function setLocale(locale) {
+ prefs.set(PREF_MATCH_OS_LOCALE, false);
+ prefs.set(PREF_SELECTED_LOCALE, locale);
+}
+
+function resetLocale() {
+ prefs.reset(PREF_MATCH_OS_LOCALE);
+ prefs.reset(PREF_SELECTED_LOCALE);
+}
+
+function definePseudo(loader, id, exports) {
+ let uri = resolveURI(id, loader.mapping);
+ loader.modules[uri] = { exports: exports };
+}
+
+function createTest(locale, testFunction) {
+ return function (assert, done) {
+ let loader = Loader(module);
+ // Change the locale before loading new l10n modules in order to load
+ // the right .json file
+ setLocale(locale);
+ // Initialize main l10n module in order to load new locale files
+ loader.require("sdk/l10n/loader").
+ load(rootURI).
+ then(null, function failure(error) {
+ if (!isNative)
+ assert.fail("Unable to load locales: " + error);
+ }).
+ then(function success(data) {
+ definePseudo(loader, '@l10n/data', data ? data : null);
+ // Execute the given test function
+ try {
+ testFunction(assert, loader, function onDone() {
+ loader.unload();
+ resetLocale();
+ done();
+ });
+ }
+ catch(e) {
+ console.exception(e);
+ }
+ },
+ function failure(error) {
+ assert.fail("Unable to load locales: " + error);
+ });
+ };
+}
+
+exports.testExactMatching = createTest("fr-FR", function(assert, loader, done) {
+ let _ = loader.require("sdk/l10n").get;
+ assert.equal(_("Not translated"), "Not translated",
+ "Key not translated");
+ assert.equal(_("Translated"), "Oui",
+ "Simple key translated");
+
+ // Placeholders
+ assert.equal(_("placeholderString", "works"), "Placeholder works",
+ "Value with placeholder");
+ assert.equal(_("Placeholder %s", "works"), "Placeholder works",
+ "Key without value but with placeholder");
+ assert.equal(_("Placeholders %2s %1s %s.", "working", "are", "correctly"),
+ "Placeholders are working correctly.",
+ "Multiple placeholders");
+
+ // Plurals
+ assert.equal(_("downloadsCount", 0),
+ "0 téléchargement",
+ "PluralForm form 'one' for 0 in french");
+ assert.equal(_("downloadsCount", 1),
+ "1 téléchargement",
+ "PluralForm form 'one' for 1 in french");
+ assert.equal(_("downloadsCount", 2),
+ "2 téléchargements",
+ "PluralForm form 'other' for n > 1 in french");
+
+ done();
+});
+
+exports.testHtmlLocalizationPageWorker = createTest("en-GB", function(assert, loader, done) {
+ // Ensure initing html component that watch document creations
+ // Note that this module is automatically initialized in
+ // cuddlefish.js:Loader.main in regular addons. But it isn't for unit tests.
+ let loaderHtmlL10n = loader.require("sdk/l10n/html");
+ loaderHtmlL10n.enable();
+
+ let uri = require("sdk/self").data.url("test-localization.html");
+ let worker = loader.require("sdk/page-worker").Page({
+ contentURL: uri,
+ contentScript: "new " + function ContentScriptScope() {
+ let nodes = document.body.querySelectorAll("*[data-l10n-id]");
+ self.postMessage([nodes[0].innerHTML,
+ nodes[1].innerHTML,
+ nodes[2].innerHTML,
+ nodes[3].innerHTML,
+ nodes[4].title,
+ nodes[4].getAttribute("alt"),
+ nodes[4].getAttribute("accesskey"),
+ nodes[4].getAttribute("aria-label"),
+ nodes[4].getAttribute("aria-valuetext"),
+ nodes[4].getAttribute("aria-moz-hint"),
+ nodes[5].placeholder,
+ nodes[6].label]);
+ },
+ onMessage: function (data) {
+ assert.equal(
+ data[0],
+ "Kept as-is",
+ "Nodes with unknown id in .properties are kept 'as-is'"
+ );
+ assert.equal(data[1], "Yes", "HTML is translated");
+ assert.equal(
+ data[2],
+ "no &lt;b&gt;HTML&lt;/b&gt; injection",
+ "Content from .properties is text content; HTML can't be injected."
+ );
+ assert.equal(data[3], "Yes", "Multiple elements with same data-l10n-id are accepted.");
+
+ // Attribute translation tests
+ assert.equal(data[4], "Yes", "Title attributes gets translated.");
+ assert.equal(data[5], "Yes", "Alt attributes gets translated.");
+ assert.equal(data[6], "B", "Accesskey gets translated.");
+
+ assert.equal(data[7], "Yes", "Aria-Label gets translated.");
+ assert.equal(data[8], "Value", "Aria-valuetext gets translated.");
+ assert.equal(data[9], "Hint", "Aria-moz-hint gets translated.");
+
+ assert.equal(data[10], "Yes", "Form placeholders are translateable.");
+
+ assert.equal(data[11], "Yes", "Labels of select options and context menus are translateable.");
+
+ done();
+ }
+ });
+});
+
+exports.testHtmlLocalization = createTest("en-GB", function(assert, loader, done) {
+ // Ensure initing html component that watch document creations
+ // Note that this module is automatically initialized in
+ // cuddlefish.js:Loader.main in regular addons. But it isn't for unit tests.
+ let loaderHtmlL10n = loader.require("sdk/l10n/html");
+ loaderHtmlL10n.enable();
+
+ let uri = require("sdk/self").data.url("test-localization.html");
+ loader.require("sdk/tabs").open({
+ url: uri,
+ onReady: function(tab) {
+ tab.attach({
+ contentURL: uri,
+ contentScript: "new " + function ContentScriptScope() {
+ let nodes = document.body.querySelectorAll("*[data-l10n-id]");
+ self.postMessage([nodes[0].innerHTML,
+ nodes[1].innerHTML,
+ nodes[2].innerHTML,
+ nodes[3].innerHTML,
+ nodes[4].title,
+ nodes[4].getAttribute("alt"),
+ nodes[4].getAttribute("accesskey"),
+ nodes[4].getAttribute("aria-label"),
+ nodes[4].getAttribute("aria-valuetext"),
+ nodes[4].getAttribute("aria-moz-hint"),
+ nodes[5].placeholder,
+ nodes[6].label]);
+ },
+ onMessage: function (data) {
+ assert.equal(
+ data[0],
+ "Kept as-is",
+ "Nodes with unknown id in .properties are kept 'as-is'"
+ );
+ assert.equal(data[1], "Yes", "HTML is translated");
+ assert.equal(
+ data[2],
+ "no &lt;b&gt;HTML&lt;/b&gt; injection",
+ "Content from .properties is text content; HTML can't be injected."
+ );
+ assert.equal(data[3], "Yes", "Multiple elements with same data-l10n-id are accepted.");
+
+ // Attribute translation tests
+ assert.equal(data[4], "Yes", "Title attributes gets translated.");
+ assert.equal(data[5], "Yes", "Alt attributes gets translated.");
+ assert.equal(data[6], "B", "Accesskey gets translated.");
+
+ assert.equal(data[7], "Yes", "Aria-Label gets translated.");
+ assert.equal(data[8], "Value", "Aria-valuetext gets translated.");
+ assert.equal(data[9], "Hint", "Aria-moz-hint gets translated.");
+
+ assert.equal(data[10], "Yes", "Form placeholders are translateable.");
+
+ assert.equal(data[11], "Yes", "Labels of select options and context menus are translateable.");
+
+ tab.close(done);
+ }
+ });
+ }
+ });
+});
+
+exports.testEnUsLocaleName = createTest("en-US", function(assert, loader, done) {
+ let _ = loader.require("sdk/l10n").get;
+
+ assert.equal(_("Not translated"), "Not translated",
+ "String w/o translation is kept as-is");
+ assert.equal(_("Translated"), "Yes",
+ "String with translation is correctly translated");
+
+ // Check Unicode char escaping sequences
+ assert.equal(_("unicodeEscape"), " @ ",
+ "Unicode escaped sequances are correctly converted");
+
+ // Check plural forms regular matching
+ assert.equal(_("downloadsCount", 0),
+ "0 downloads",
+ "PluralForm form 'other' for 0 in english");
+ assert.equal(_("downloadsCount", 1),
+ "one download",
+ "PluralForm form 'one' for 1 in english");
+ assert.equal(_("downloadsCount", 2),
+ "2 downloads",
+ "PluralForm form 'other' for n != 1 in english");
+
+ // Check optional plural forms
+ assert.equal(_("pluralTest", 0),
+ "optional zero form",
+ "PluralForm form 'zero' can be optionaly specified. (Isn't mandatory in english)");
+ assert.equal(_("pluralTest", 1),
+ "fallback to other",
+ "If the specific plural form is missing, we fallback to 'other'");
+
+ // Ensure that we can omit specifying the generic key without [other]
+ // key[one] = ...
+ // key[other] = ... # Instead of `key = ...`
+ assert.equal(_("explicitPlural", 1),
+ "one",
+ "PluralForm form can be omitting generic key [i.e. without ...[other] at end of key)");
+ assert.equal(_("explicitPlural", 10),
+ "other",
+ "PluralForm form can be omitting generic key [i.e. without ...[other] at end of key)");
+
+ assert.equal(_("first_identifier", "ONE", "TWO"), "the entries are ONE and TWO.", "first_identifier no count");
+ assert.equal(_("first_identifier", 0, "ONE", "TWO"), "the entries are ONE and TWO.", "first_identifier with count = 0");
+ assert.equal(_("first_identifier", 1, "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "first_identifier with count = 1");
+ assert.equal(_("first_identifier", 2, "ONE", "TWO"), "the entries are ONE and TWO.", "first_identifier with count = 2");
+
+ assert.equal(_("second_identifier", "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "second_identifier with no count");
+ assert.equal(_("second_identifier", 0, "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "second_identifier with count = 0");
+ assert.equal(_("second_identifier", 1, "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "second_identifier with count = 1");
+ assert.equal(_("second_identifier", 2, "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "second_identifier with count = 2");
+
+ assert.equal(_("third_identifier", "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "third_identifier with no count");
+ assert.equal(_("third_identifier", 0, "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "third_identifier with count = 0");
+ assert.equal(_("third_identifier", 2, "ONE", "TWO"), "first entry is ONE and the second one is TWO.", "third_identifier with count = 2");
+
+ done();
+});
+
+exports.testUsingJSON = function(assert) {
+ assert.equal(usingJSON, !isNative, 'using json');
+}
+
+exports.testShortLocaleName = createTest("eo", function(assert, loader, done) {
+ let _ = loader.require("sdk/l10n").get;
+ assert.equal(_("Not translated"), "Not translated",
+ "String w/o translation is kept as-is");
+ assert.equal(_("Translated"), "jes",
+ "String with translation is correctly translated");
+
+ done();
+});
+
+
+// Before running tests, disable HTML service which is automatially enabled
+// in api-utils/addon/runner.js
+require('sdk/l10n/html').disable();
+
+require("sdk/test/runner").runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/l10n/package.json b/addon-sdk/source/test/addons/l10n/package.json
new file mode 100644
index 000000000..4847e1471
--- /dev/null
+++ b/addon-sdk/source/test/addons/l10n/package.json
@@ -0,0 +1,5 @@
+{
+ "id": "test-l10n@jetpack",
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/layout-change/lib/main.js b/addon-sdk/source/test/addons/layout-change/lib/main.js
new file mode 100644
index 000000000..9cae9ab31
--- /dev/null
+++ b/addon-sdk/source/test/addons/layout-change/lib/main.js
@@ -0,0 +1,15 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { isNative } = require("@loader/options");
+
+if (isNative) {
+ module.exports = require("./test-toolkit-loader");
+}
+else {
+ module.exports = require("./test-cuddlefish-loader");
+}
+
+require("sdk/test/runner").runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/layout-change/lib/test-cuddlefish-loader.js b/addon-sdk/source/test/addons/layout-change/lib/test-cuddlefish-loader.js
new file mode 100644
index 000000000..8b9f8dc56
--- /dev/null
+++ b/addon-sdk/source/test/addons/layout-change/lib/test-cuddlefish-loader.js
@@ -0,0 +1,164 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { LoaderWithHookedConsole } = require('sdk/test/loader');
+const { loader } = LoaderWithHookedConsole(module);
+const app = require("sdk/system/xul-app");
+
+// This test makes sure that require statements used by all AMO hosted
+// add-ons will be able to use old require statements.
+// Tests are based on following usage data:
+// https://docs.google.com/spreadsheet/ccc?key=0ApEBy-GRnGxzdHlRMHJ5RXN1aWJ4RGhINkxSd0FCQXc#gid=0
+
+exports["test compatibility"] = function(assert) {
+ let { require } = loader;
+
+ assert.equal(require("self"),
+ require("sdk/self"), "sdk/self -> self");
+
+ assert.equal(require("tabs"),
+ require("sdk/tabs"), "sdk/tabs -> tabs");
+
+ if (app.is("Firefox")) {
+ assert.throws(() => require("widget"),
+ /Module `widget` is not found at/,
+ "There is no widget module");
+
+ assert.throws(() => require("sdk/widget"),
+ /Module `sdk\/widget` is not found at/,
+ "There is no sdk/widget module");
+ }
+
+ assert.equal(require("page-mod"),
+ require("sdk/page-mod"), "sdk/page-mod -> page-mod");
+
+ if (app.is("Firefox")) {
+ assert.equal(require("panel"),
+ require("sdk/panel"), "sdk/panel -> panel");
+ }
+
+ assert.equal(require("request"),
+ require("sdk/request"), "sdk/request -> request");
+
+ assert.equal(require("chrome"),
+ require("chrome"), "chrome -> chrome");
+
+ assert.equal(require("simple-storage"),
+ require("sdk/simple-storage"), "sdk/simple-storage -> simple-storage");
+
+ if (app.is("Firefox")) {
+ assert.equal(require("context-menu"),
+ require("sdk/context-menu"), "sdk/context-menu -> context-menu");
+ }
+
+ assert.equal(require("notifications"),
+ require("sdk/notifications"), "sdk/notifications -> notifications");
+
+ assert.equal(require("preferences-service"),
+ require("sdk/preferences/service"), "sdk/preferences/service -> preferences-service");
+
+ assert.equal(require("window-utils"),
+ require("sdk/deprecated/window-utils"), "sdk/deprecated/window-utils -> window-utils");
+
+ assert.equal(require("url"),
+ require("sdk/url"), "sdk/url -> url");
+
+ if (app.is("Firefox")) {
+ assert.equal(require("selection"),
+ require("sdk/selection"), "sdk/selection -> selection");
+ }
+
+ assert.equal(require("timers"),
+ require("sdk/timers"), "sdk/timers -> timers");
+
+ assert.equal(require("simple-prefs"),
+ require("sdk/simple-prefs"), "sdk/simple-prefs -> simple-prefs");
+
+ assert.equal(require("traceback"),
+ require("sdk/console/traceback"), "sdk/console/traceback -> traceback");
+
+ assert.equal(require("unload"),
+ require("sdk/system/unload"), "sdk/system/unload -> unload");
+
+ assert.equal(require("hotkeys"),
+ require("sdk/hotkeys"), "sdk/hotkeys -> hotkeys");
+
+ if (app.is("Firefox")) {
+ assert.equal(require("clipboard"),
+ require("sdk/clipboard"), "sdk/clipboard -> clipboard");
+ }
+
+ assert.equal(require("windows"),
+ require("sdk/windows"), "sdk/windows -> windows");
+
+ assert.equal(require("page-worker"),
+ require("sdk/page-worker"), "sdk/page-worker -> page-worker");
+
+ assert.equal(require("timer"),
+ require("sdk/timers"), "sdk/timers -> timer");
+
+ assert.equal(require("xhr"),
+ require("sdk/net/xhr"), "sdk/io/xhr -> xhr");
+
+ assert.equal(require("private-browsing"),
+ require("sdk/private-browsing"), "sdk/private-browsing -> private-browsing");
+
+ assert.equal(require("passwords"),
+ require("sdk/passwords"), "sdk/passwords -> passwords");
+
+ assert.equal(require("match-pattern"),
+ require("sdk/util/match-pattern"), "sdk/util/match-pattern -> match-pattern");
+
+ assert.equal(require("file"),
+ require("sdk/io/file"), "sdk/io/file -> file");
+
+ assert.equal(require("xul-app"),
+ require("sdk/system/xul-app"), "sdk/system/xul-app -> xul-app");
+
+ assert.equal(require("api-utils"),
+ require("sdk/deprecated/api-utils"), "sdk/deprecated/api-utils -> api-utils");
+
+ assert.equal(require("runtime"),
+ require("sdk/system/runtime"), "sdk/system/runtime -> runtime");
+
+ assert.equal(require("base64"),
+ require("sdk/base64"), "sdk/base64 -> base64");
+
+ assert.equal(require("xpcom"),
+ require("sdk/platform/xpcom"), "sdk/platform/xpcom -> xpcom");
+
+ assert.equal(require("keyboard/utils"),
+ require("sdk/keyboard/utils"), "sdk/keyboard/utils -> keyboard/utils");
+
+ assert.equal(require("system"),
+ require("sdk/system"), "sdk/system -> system");
+
+ assert.equal(require("querystring"),
+ require("sdk/querystring"), "sdk/querystring -> querystring");
+
+ assert.equal(require("tabs/utils"),
+ require("sdk/tabs/utils"), "sdk/tabs/utils -> tabs/utils");
+
+ assert.equal(require("dom/events"),
+ require("sdk/dom/events-shimmed"), "sdk/dom/events-shimmed -> dom/events");
+
+ assert.equal(require("tabs/tab.js"),
+ require("sdk/tabs/tab"), "sdk/tabs/tab -> tabs/tab.js");
+
+ assert.equal(require("environment"),
+ require("sdk/system/environment"), "sdk/system/environment -> environment");
+
+ assert.equal(require("test/assert"),
+ require("sdk/test/assert"), "sdk/test/assert -> test/assert");
+
+ assert.equal(require("hidden-frame"),
+ require("sdk/frame/hidden-frame"), "sdk/frame/hidden-frame -> hidden-frame");
+
+ assert.equal(require("collection"),
+ require("sdk/util/collection"), "sdk/util/collection -> collection");
+
+ assert.equal(require("array"),
+ require("sdk/util/array"), "sdk/util/array -> array");
+};
diff --git a/addon-sdk/source/test/addons/layout-change/lib/test-toolkit-loader.js b/addon-sdk/source/test/addons/layout-change/lib/test-toolkit-loader.js
new file mode 100644
index 000000000..3f8123471
--- /dev/null
+++ b/addon-sdk/source/test/addons/layout-change/lib/test-toolkit-loader.js
@@ -0,0 +1,10 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+exports["test compatibility"] = function(assert) {
+ assert.throws(() => require("self"),
+ /^Module `self` is not found/,
+ "sdk/self -> self");
+};
diff --git a/addon-sdk/source/test/addons/layout-change/package.json b/addon-sdk/source/test/addons/layout-change/package.json
new file mode 100644
index 000000000..fe370f5a6
--- /dev/null
+++ b/addon-sdk/source/test/addons/layout-change/package.json
@@ -0,0 +1,7 @@
+{
+ "id": "test-layout-change@jetpack",
+ "name": "test-layout-change",
+ "ignore-deprecated-path": true,
+ "version": "0.0.2",
+ "main": "./lib/main.js"
+}
diff --git a/addon-sdk/source/test/addons/main/main.js b/addon-sdk/source/test/addons/main/main.js
new file mode 100644
index 000000000..6cab5d005
--- /dev/null
+++ b/addon-sdk/source/test/addons/main/main.js
@@ -0,0 +1,37 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { setTimeout } = require('sdk/timers');
+
+var mainStarted = false;
+
+exports.main = function main(options, callbacks) {
+ mainStarted = true;
+
+ let tests = {};
+
+ tests.testMainArguments = function(assert) {
+ assert.ok(!!options, 'options argument provided to main');
+ assert.ok('loadReason' in options, 'loadReason is in options provided by main');
+ assert.equal(typeof callbacks.print, 'function', 'callbacks.print is a function');
+ assert.equal(typeof callbacks.quit, 'function', 'callbacks.quit is a function');
+
+ // Re-enable when bug 1251664 is fixed
+ //assert.equal(options.loadReason, 'install', 'options.loadReason is install');
+ }
+
+ require('sdk/test/runner').runTestsFromModule({exports: tests});
+}
+
+// this causes a fail if main does not start
+setTimeout(function() {
+ if (mainStarted)
+ return;
+
+ // main didn't start, fail..
+ require("sdk/test/runner").runTestsFromModule({exports: {
+ testFail: assert => assert.fail('Main did not start..')
+ }});
+}, 500);
diff --git a/addon-sdk/source/test/addons/main/package.json b/addon-sdk/source/test/addons/main/package.json
new file mode 100644
index 000000000..9698810b2
--- /dev/null
+++ b/addon-sdk/source/test/addons/main/package.json
@@ -0,0 +1,5 @@
+{
+ "id": "test-main@jetpack",
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/name-in-numbers-plus/index.js b/addon-sdk/source/test/addons/name-in-numbers-plus/index.js
new file mode 100644
index 000000000..461652dbe
--- /dev/null
+++ b/addon-sdk/source/test/addons/name-in-numbers-plus/index.js
@@ -0,0 +1,12 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { name } = require("sdk/self");
+
+exports["test self.name"] = (assert) => {
+ assert.equal(name, "0-0", "using '0-0' works.");
+}
+
+require("sdk/test/runner").runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/name-in-numbers-plus/package.json b/addon-sdk/source/test/addons/name-in-numbers-plus/package.json
new file mode 100644
index 000000000..f43793b27
--- /dev/null
+++ b/addon-sdk/source/test/addons/name-in-numbers-plus/package.json
@@ -0,0 +1,6 @@
+{
+ "id": "name-in-numbers-plus@jetpack",
+ "name": "0-0",
+ "main": "./index.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/name-in-numbers/index.js b/addon-sdk/source/test/addons/name-in-numbers/index.js
new file mode 100644
index 000000000..e3b31243d
--- /dev/null
+++ b/addon-sdk/source/test/addons/name-in-numbers/index.js
@@ -0,0 +1,12 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { name } = require("sdk/self");
+
+exports["test self.name"] = (assert) => {
+ assert.equal(name, "5", "using '5' works.");
+}
+
+require("sdk/test/runner").runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/name-in-numbers/package.json b/addon-sdk/source/test/addons/name-in-numbers/package.json
new file mode 100644
index 000000000..e2be0b628
--- /dev/null
+++ b/addon-sdk/source/test/addons/name-in-numbers/package.json
@@ -0,0 +1,6 @@
+{
+ "id": "name-in-numbers@jetpack",
+ "name": "5",
+ "main": "./index.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/packaging/main.js b/addon-sdk/source/test/addons/packaging/main.js
new file mode 100644
index 000000000..87b2a0347
--- /dev/null
+++ b/addon-sdk/source/test/addons/packaging/main.js
@@ -0,0 +1,57 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+var options = require("@loader/options");
+var metadata = {};
+if (options.isNative) {
+ metadata = require("./package.json");
+ metadata.permissions = {};
+ Object.freeze(metadata);
+}
+else {
+ metadata = options.metadata;
+}
+
+exports.testPackaging = function(assert) {
+ assert.equal(metadata.description,
+ "Add-on development made easy.",
+ "packaging metadata should be available");
+ try {
+ metadata.description = 'new description';
+ assert.fail('should not have been able to set options.metadata property');
+ }
+ catch (e) {}
+
+ assert.equal(metadata.description,
+ "Add-on development made easy.",
+ "packaging metadata should be frozen");
+
+ assert.equal(metadata.permissions['private-browsing'], undefined,
+ "private browsing metadata should be undefined");
+
+ assert.equal(metadata['private-browsing'], undefined,
+ "private browsing metadata should be be frozen");
+
+ assert.equal(options['private-browsing'], undefined,
+ "private browsing metadata should be be frozen");
+
+ try {
+ metadata['private-browsing'] = true;
+ assert.fail('should not have been able to set options.metadata property');
+ }
+ catch(e) {}
+ assert.equal(metadata['private-browsing'], undefined,
+ "private browsing metadata should be be frozen");
+
+ try {
+ options.permissions['private-browsing'] = true;
+ assert.fail('should not have been able to set options.metadata.permissions property');
+ }
+ catch (e) {}
+ assert.equal(metadata.permissions['private-browsing'], undefined,
+ "private browsing metadata should be be frozen");
+};
+
+require("sdk/test/runner").runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/packaging/package.json b/addon-sdk/source/test/addons/packaging/package.json
new file mode 100644
index 000000000..1514f387e
--- /dev/null
+++ b/addon-sdk/source/test/addons/packaging/package.json
@@ -0,0 +1,6 @@
+{
+ "id": "test-packaging@jetpack",
+ "description": "Add-on development made easy.",
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/packed/main.js b/addon-sdk/source/test/addons/packed/main.js
new file mode 100644
index 000000000..9be0cc4b0
--- /dev/null
+++ b/addon-sdk/source/test/addons/packed/main.js
@@ -0,0 +1,20 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { packed } = require("sdk/self");
+const url = require("sdk/url");
+
+exports["test self.packed"] = function (assert) {
+ assert.ok(packed, "require('sdk/self').packed is correct");
+}
+
+exports["test url.toFilename"] = function (assert) {
+ assert.throws(
+ function() { url.toFilename(module.uri); },
+ /cannot map to filename: /,
+ "url.toFilename() can fail for packed XPIs");
+}
+
+require("sdk/test/runner").runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/packed/package.json b/addon-sdk/source/test/addons/packed/package.json
new file mode 100644
index 000000000..1c6556e44
--- /dev/null
+++ b/addon-sdk/source/test/addons/packed/package.json
@@ -0,0 +1,6 @@
+{
+ "id": "test-url@jetpack",
+ "unpack": false,
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/page-mod-debugger-post/data/index.html b/addon-sdk/source/test/addons/page-mod-debugger-post/data/index.html
new file mode 100644
index 000000000..4128d6de2
--- /dev/null
+++ b/addon-sdk/source/test/addons/page-mod-debugger-post/data/index.html
@@ -0,0 +1,11 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<html>
+ <head>
+ <meta charset="UTF-8">
+ <title>Page Mod Debugger Test</title>
+ </head>
+ <body></body>
+</html>
diff --git a/addon-sdk/source/test/addons/page-mod-debugger-post/data/script.js b/addon-sdk/source/test/addons/page-mod-debugger-post/data/script.js
new file mode 100644
index 000000000..ee248d461
--- /dev/null
+++ b/addon-sdk/source/test/addons/page-mod-debugger-post/data/script.js
@@ -0,0 +1,16 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+function runDebuggerStatement () {
+ window.document.body.setAttribute('style', 'background-color: red');
+ debugger;
+ window.document.body.setAttribute('style', 'background-color: green');
+}
+
+exportFunction(
+ runDebuggerStatement,
+ document.defaultView,
+ { defineAs: "runDebuggerStatement" }
+);
diff --git a/addon-sdk/source/test/addons/page-mod-debugger-post/main.js b/addon-sdk/source/test/addons/page-mod-debugger-post/main.js
new file mode 100644
index 000000000..703399c21
--- /dev/null
+++ b/addon-sdk/source/test/addons/page-mod-debugger-post/main.js
@@ -0,0 +1,136 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Cu } = require('chrome');
+const { PageMod } = require('sdk/page-mod');
+const tabs = require('sdk/tabs');
+const { closeTab } = require('sdk/tabs/utils');
+const promise = require('sdk/core/promise')
+const { getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { data } = require('sdk/self');
+const { set } = require('sdk/preferences/service');
+
+const { require: devtoolsRequire } = Cu.import("resource://devtools/shared/Loader.jsm", {});
+const { DebuggerServer } = devtoolsRequire("devtools/server/main");
+const { DebuggerClient } = devtoolsRequire("devtools/shared/client/main");
+
+var gClient;
+var ok;
+var testName = 'testDebugger';
+var iframeURL = 'data:text/html;charset=utf-8,' + testName;
+var TAB_URL = 'data:text/html;charset=utf-8,' + encodeURIComponent('<iframe src="' + iframeURL + '" />');
+TAB_URL = data.url('index.html');
+var mod;
+
+exports.testDebugger = function(assert, done) {
+ ok = assert.ok.bind(assert);
+ assert.pass('starting test');
+ set('devtools.debugger.log', true);
+
+ if (!DebuggerServer.initialized) {
+ DebuggerServer.init();
+ DebuggerServer.addBrowserActors();
+ }
+
+ let transport = DebuggerServer.connectPipe();
+ gClient = new DebuggerClient(transport);
+ gClient.connect((aType, aTraits) => {
+ tabs.open({
+ url: TAB_URL,
+ onLoad: function(tab) {
+ assert.pass('tab loaded');
+
+ attachTabActorForUrl(gClient, TAB_URL).
+ then(_ => { assert.pass('attachTabActorForUrl called'); return _; }).
+ then(attachThread).
+ then(testDebuggerStatement).
+ then(_ => { assert.pass('testDebuggerStatement called') }).
+ then(closeConnection).
+ then(_ => { assert.pass('closeConnection called') }).
+ then(_ => { tab.close() }).
+ then(done).
+ then(null, aError => {
+ ok(false, "Got an error: " + aError.message + "\n" + aError.stack);
+ });
+ }
+ });
+ });
+}
+
+function attachThread([aGrip, aResponse]) {
+ let deferred = promise.defer();
+
+ // Now attach and resume...
+ gClient.request({ to: aResponse.threadActor, type: "attach" }, () => {
+ gClient.request({ to: aResponse.threadActor, type: "resume" }, () => {
+ ok(true, "Pause wasn't called before we've attached.");
+ deferred.resolve([aGrip, aResponse]);
+ });
+ });
+
+ return deferred.promise;
+}
+
+function testDebuggerStatement([aGrip, aResponse]) {
+ let deferred = promise.defer();
+ ok(aGrip, 'aGrip existss')
+
+ gClient.addListener("paused", (aEvent, aPacket) => {
+ ok(true, 'there was a pause event');
+ gClient.request({ to: aResponse.threadActor, type: "resume" }, () => {
+ ok(true, "The pause handler was triggered on a debugger statement.");
+ deferred.resolve();
+ });
+ });
+
+ mod = PageMod({
+ include: TAB_URL,
+ attachTo: ['existing', 'top', 'frame'],
+ contentScriptFile: data.url('script.js'),
+ onAttach: function(mod) {
+ ok(true, 'the page-mod was attached to ' + mod.tab.url);
+
+ require('sdk/timers').setTimeout(function() {
+ let debuggee = getMostRecentBrowserWindow().gBrowser.selectedBrowser.contentWindow.wrappedJSObject;
+ debuggee.runDebuggerStatement();
+ ok(true, 'called runDebuggerStatement');
+ }, 500)
+ }
+ });
+ ok(true, 'PageMod was created');
+
+ return deferred.promise;
+}
+
+function getTabActorForUrl(aClient, aUrl) {
+ let deferred = promise.defer();
+
+ aClient.listTabs(aResponse => {
+ let tabActor = aResponse.tabs.filter(aGrip => aGrip.url == aUrl).pop();
+ deferred.resolve(tabActor);
+ });
+
+ return deferred.promise;
+}
+
+function attachTabActorForUrl(aClient, aUrl) {
+ let deferred = promise.defer();
+
+ getTabActorForUrl(aClient, aUrl).then(aGrip => {
+ aClient.attachTab(aGrip.actor, aResponse => {
+ deferred.resolve([aGrip, aResponse]);
+ });
+ });
+
+ return deferred.promise;
+}
+
+function closeConnection() {
+ let deferred = promise.defer();
+ gClient.close(deferred.resolve);
+ return deferred.promise;
+}
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/page-mod-debugger-post/package.json b/addon-sdk/source/test/addons/page-mod-debugger-post/package.json
new file mode 100644
index 000000000..c687ce0ab
--- /dev/null
+++ b/addon-sdk/source/test/addons/page-mod-debugger-post/package.json
@@ -0,0 +1,6 @@
+{
+ "id": "test-page-mod-debugger@jetpack",
+ "author": "Erik Vold",
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/page-mod-debugger-pre/data/index.html b/addon-sdk/source/test/addons/page-mod-debugger-pre/data/index.html
new file mode 100644
index 000000000..4128d6de2
--- /dev/null
+++ b/addon-sdk/source/test/addons/page-mod-debugger-pre/data/index.html
@@ -0,0 +1,11 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<html>
+ <head>
+ <meta charset="UTF-8">
+ <title>Page Mod Debugger Test</title>
+ </head>
+ <body></body>
+</html>
diff --git a/addon-sdk/source/test/addons/page-mod-debugger-pre/data/script.js b/addon-sdk/source/test/addons/page-mod-debugger-pre/data/script.js
new file mode 100644
index 000000000..ee248d461
--- /dev/null
+++ b/addon-sdk/source/test/addons/page-mod-debugger-pre/data/script.js
@@ -0,0 +1,16 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+function runDebuggerStatement () {
+ window.document.body.setAttribute('style', 'background-color: red');
+ debugger;
+ window.document.body.setAttribute('style', 'background-color: green');
+}
+
+exportFunction(
+ runDebuggerStatement,
+ document.defaultView,
+ { defineAs: "runDebuggerStatement" }
+);
diff --git a/addon-sdk/source/test/addons/page-mod-debugger-pre/main.js b/addon-sdk/source/test/addons/page-mod-debugger-pre/main.js
new file mode 100644
index 000000000..366e9e437
--- /dev/null
+++ b/addon-sdk/source/test/addons/page-mod-debugger-pre/main.js
@@ -0,0 +1,134 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Cu } = require('chrome');
+const { PageMod } = require('sdk/page-mod');
+const tabs = require('sdk/tabs');
+const { closeTab } = require('sdk/tabs/utils');
+const promise = require('sdk/core/promise')
+const { getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { data } = require('sdk/self');
+const { set } = require('sdk/preferences/service');
+
+const { require: devtoolsRequire } = Cu.import("resource://devtools/shared/Loader.jsm", {});
+const { DebuggerServer } = devtoolsRequire("devtools/server/main");
+const { DebuggerClient } = devtoolsRequire("devtools/shared/client/main");
+
+var gClient;
+var ok;
+var testName = 'testDebugger';
+var iframeURL = 'data:text/html;charset=utf-8,' + testName;
+var TAB_URL = 'data:text/html;charset=utf-8,' + encodeURIComponent('<iframe src="' + iframeURL + '" />');
+TAB_URL = data.url('index.html');
+var mod;
+
+exports.testDebugger = function(assert, done) {
+ ok = assert.ok.bind(assert);
+ assert.pass('starting test');
+ set('devtools.debugger.log', true);
+
+ mod = PageMod({
+ include: TAB_URL,
+ attachTo: ['existing', 'top', 'frame'],
+ contentScriptFile: data.url('script.js'),
+ });
+ ok(true, 'PageMod was created');
+
+ if (!DebuggerServer.initialized) {
+ DebuggerServer.init();
+ DebuggerServer.addBrowserActors();
+ }
+
+ let transport = DebuggerServer.connectPipe();
+ gClient = new DebuggerClient(transport);
+ gClient.connect((aType, aTraits) => {
+ tabs.open({
+ url: TAB_URL,
+ onLoad: function(tab) {
+ assert.pass('tab loaded');
+
+ attachTabActorForUrl(gClient, TAB_URL).
+ then(_ => { assert.pass('attachTabActorForUrl called'); return _; }).
+ then(attachThread).
+ then(testDebuggerStatement).
+ then(_ => { assert.pass('testDebuggerStatement called') }).
+ then(closeConnection).
+ then(_ => { assert.pass('closeConnection called') }).
+ then(_ => { tab.close() }).
+ then(done).
+ then(null, aError => {
+ ok(false, "Got an error: " + aError.message + "\n" + aError.stack);
+ });
+ }
+ });
+ });
+}
+
+function attachThread([aGrip, aResponse]) {
+ let deferred = promise.defer();
+
+ // Now attach and resume...
+ gClient.request({ to: aResponse.threadActor, type: "attach" }, () => {
+ gClient.request({ to: aResponse.threadActor, type: "resume" }, () => {
+ ok(true, "Pause wasn't called before we've attached.");
+ deferred.resolve([aGrip, aResponse]);
+ });
+ });
+
+ return deferred.promise;
+}
+
+function testDebuggerStatement([aGrip, aResponse]) {
+ let deferred = promise.defer();
+ ok(aGrip, 'aGrip existss')
+
+ gClient.addListener("paused", (aEvent, aPacket) => {
+ ok(true, 'there was a pause event');
+ gClient.request({ to: aResponse.threadActor, type: "resume" }, () => {
+ ok(true, "The pause handler was triggered on a debugger statement.");
+ deferred.resolve();
+ });
+ });
+
+ let debuggee = getMostRecentBrowserWindow().gBrowser.selectedBrowser.contentWindow.wrappedJSObject;
+ debuggee.runDebuggerStatement();
+ ok(true, 'called runDebuggerStatement');
+
+ return deferred.promise;
+}
+
+function getTabActorForUrl(aClient, aUrl) {
+ let deferred = promise.defer();
+
+ aClient.listTabs(aResponse => {
+ let tabActor = aResponse.tabs.filter(aGrip => aGrip.url == aUrl).pop();
+ deferred.resolve(tabActor);
+ });
+
+ return deferred.promise;
+}
+
+function attachTabActorForUrl(aClient, aUrl) {
+ let deferred = promise.defer();
+
+ getTabActorForUrl(aClient, aUrl).then(aGrip => {
+ aClient.attachTab(aGrip.actor, aResponse => {
+ deferred.resolve([aGrip, aResponse]);
+ });
+ });
+
+ return deferred.promise;
+}
+
+function closeConnection() {
+ let deferred = promise.defer();
+ gClient.close(deferred.resolve);
+ return deferred.promise;
+}
+
+// bug 1042976 - temporary test disable
+module.exports = {};
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/page-mod-debugger-pre/package.json b/addon-sdk/source/test/addons/page-mod-debugger-pre/package.json
new file mode 100644
index 000000000..c687ce0ab
--- /dev/null
+++ b/addon-sdk/source/test/addons/page-mod-debugger-pre/package.json
@@ -0,0 +1,6 @@
+{
+ "id": "test-page-mod-debugger@jetpack",
+ "author": "Erik Vold",
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/page-worker/data/page.html b/addon-sdk/source/test/addons/page-worker/data/page.html
new file mode 100644
index 000000000..3b59d3963
--- /dev/null
+++ b/addon-sdk/source/test/addons/page-worker/data/page.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+
+<html>
+<head>
+<script type="text/javascript" src="page.js"></script>
+</head>
+<body>
+</body>
+</html>
diff --git a/addon-sdk/source/test/addons/page-worker/data/page.js b/addon-sdk/source/test/addons/page-worker/data/page.js
new file mode 100644
index 000000000..04d062497
--- /dev/null
+++ b/addon-sdk/source/test/addons/page-worker/data/page.js
@@ -0,0 +1,13 @@
+window.addEventListener("load", function() {
+ addon.port.emit("load", "ok");
+});
+
+addon.postMessage("first message");
+addon.on("message", function(msg) {
+ if (msg == "ping")
+ addon.postMessage("pong");
+});
+
+addon.port.on("ping", function() {
+ addon.port.emit("pong");
+});
diff --git a/addon-sdk/source/test/addons/page-worker/main.js b/addon-sdk/source/test/addons/page-worker/main.js
new file mode 100644
index 000000000..46b203d2e
--- /dev/null
+++ b/addon-sdk/source/test/addons/page-worker/main.js
@@ -0,0 +1,53 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const { Page } = require("sdk/page-worker");
+const { data } = require("sdk/self");
+
+exports["test page load"] = function(assert, done) {
+ let page = Page({
+ contentURL: data.url("page.html")
+ });
+
+ page.port.on("load", function(check) {
+ assert.equal(check, "ok", "saw the load message");
+ page.destroy();
+ done();
+ });
+};
+
+exports["test postMessage"] = function(assert, done) {
+ let page = Page({
+ contentURL: data.url("page.html"),
+ onMessage: function(msg) {
+ if (msg == "pong") {
+ assert.ok(true, "saw the second message");
+ page.destroy();
+ done();
+ return;
+ }
+
+ assert.equal(msg, "first message", "saw the first message");
+ this.postMessage("ping");
+ }
+ });
+};
+
+exports["test port"] = function(assert, done) {
+ let page = Page({
+ contentURL: data.url("page.html")
+ });
+
+ page.port.on("pong", function() {
+ assert.ok(true, "saw the response");
+ page.destroy();
+ done();
+ });
+
+ page.port.emit("ping");
+};
+
+require("sdk/test/runner").runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/page-worker/package.json b/addon-sdk/source/test/addons/page-worker/package.json
new file mode 100644
index 000000000..d7856fac5
--- /dev/null
+++ b/addon-sdk/source/test/addons/page-worker/package.json
@@ -0,0 +1,3 @@
+{
+ "id": "test-page-worker"
+}
diff --git a/addon-sdk/source/test/addons/places/lib/favicon-helpers.js b/addon-sdk/source/test/addons/places/lib/favicon-helpers.js
new file mode 100644
index 000000000..d6b387bca
--- /dev/null
+++ b/addon-sdk/source/test/addons/places/lib/favicon-helpers.js
@@ -0,0 +1,54 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const { Cc, Ci, Cu } = require('chrome');
+const { Loader } = require('sdk/test/loader');
+const loader = Loader(module);
+const httpd = loader.require('./httpd');
+const { pathFor } = require('sdk/system');
+const { startServerAsync } = httpd;
+const basePath = pathFor('ProfD');
+const { atob } = Cu.import("resource://gre/modules/Services.jsm", {});
+const historyService = Cc["@mozilla.org/browser/nav-history-service;1"]
+ .getService(Ci.nsINavHistoryService);
+const { events } = require('sdk/places/events');
+const { OS } = Cu.import("resource://gre/modules/osfile.jsm", {});
+
+function onFaviconChange (url) {
+ return new Promise(resolve => {
+ function handler ({data, type}) {
+ if (type !== 'history-page-changed' ||
+ data.url !== url ||
+ data.property !== Ci.nsINavHistoryObserver.ATTRIBUTE_FAVICON)
+ return;
+ events.off('data', handler);
+ resolve(data.value);
+ }
+
+ events.on('data', handler);
+ });
+}
+exports.onFaviconChange = onFaviconChange;
+
+/*
+ * Takes page content, a page path, and favicon binary data
+ */
+function serve ({name, favicon, port, host}) {
+ let faviconTag = '<link rel="icon" type="image/x-icon" href="/'+ name +'.ico"/>';
+ let content = '<html><head>' + faviconTag + '<title>'+name+'</title></head><body></body></html>';
+ let srv = startServerAsync(port, basePath);
+
+ let pagePath = OS.Path.join(basePath, name + '.html');
+ let iconPath = OS.Path.join(basePath, name + '.ico');
+
+
+ return OS.File.writeAtomic(iconPath, favicon).
+ then(() => {
+ return OS.File.writeAtomic(pagePath, content);
+ }).
+ then(() => srv);
+}
+exports.serve = serve;
+
+var binFavicon = exports.binFavicon = atob('AAABAAEAEBAAAAAAAABoBQAAFgAAACgAAAAQAAAAIAAAAAEACAAAAAAAAAEAAAAAAAAAAAAAAAEAAAABAAAAAAAAAACAAACAAAAAgIAAgAAAAIAAgACAgAAAwMDAAMDcwADwyqYABAQEAAgICAAMDAwAERERABYWFgAcHBwAIiIiACkpKQBVVVUATU1NAEJCQgA5OTkAgHz/AFBQ/wCTANYA/+zMAMbW7wDW5+cAkKmtAAAAMwAAAGYAAACZAAAAzAAAMwAAADMzAAAzZgAAM5kAADPMAAAz/wAAZgAAAGYzAABmZgAAZpkAAGbMAABm/wAAmQAAAJkzAACZZgAAmZkAAJnMAACZ/wAAzAAAAMwzAADMZgAAzJkAAMzMAADM/wAA/2YAAP+ZAAD/zAAzAAAAMwAzADMAZgAzAJkAMwDMADMA/wAzMwAAMzMzADMzZgAzM5kAMzPMADMz/wAzZgAAM2YzADNmZgAzZpkAM2bMADNm/wAzmQAAM5kzADOZZgAzmZkAM5nMADOZ/wAzzAAAM8wzADPMZgAzzJkAM8zMADPM/wAz/zMAM/9mADP/mQAz/8wAM///AGYAAABmADMAZgBmAGYAmQBmAMwAZgD/AGYzAABmMzMAZjNmAGYzmQBmM8wAZjP/AGZmAABmZjMAZmZmAGZmmQBmZswAZpkAAGaZMwBmmWYAZpmZAGaZzABmmf8AZswAAGbMMwBmzJkAZszMAGbM/wBm/wAAZv8zAGb/mQBm/8wAzAD/AP8AzACZmQAAmTOZAJkAmQCZAMwAmQAAAJkzMwCZAGYAmTPMAJkA/wCZZgAAmWYzAJkzZgCZZpkAmWbMAJkz/wCZmTMAmZlmAJmZmQCZmcwAmZn/AJnMAACZzDMAZsxmAJnMmQCZzMwAmcz/AJn/AACZ/zMAmcxmAJn/mQCZ/8wAmf//AMwAAACZADMAzABmAMwAmQDMAMwAmTMAAMwzMwDMM2YAzDOZAMwzzADMM/8AzGYAAMxmMwCZZmYAzGaZAMxmzACZZv8AzJkAAMyZMwDMmWYAzJmZAMyZzADMmf8AzMwAAMzMMwDMzGYAzMyZAMzMzADMzP8AzP8AAMz/MwCZ/2YAzP+ZAMz/zADM//8AzAAzAP8AZgD/AJkAzDMAAP8zMwD/M2YA/zOZAP8zzAD/M/8A/2YAAP9mMwDMZmYA/2aZAP9mzADMZv8A/5kAAP+ZMwD/mWYA/5mZAP+ZzAD/mf8A/8wAAP/MMwD/zGYA/8yZAP/MzAD/zP8A//8zAMz/ZgD//5kA///MAGZm/wBm/2YAZv//AP9mZgD/Zv8A//9mACEApQBfX18Ad3d3AIaGhgCWlpYAy8vLALKysgDX19cA3d3dAOPj4wDq6uoA8fHxAPj4+ADw+/8ApKCgAICAgAAAAP8AAP8AAAD//wD/AAAA/wD/AP//AAD///8ACgoKCgoKCgoKCgoKCgoKCgoKCgoHAQEMbQoKCgoKCgoAAAdDH/kgHRIAAAAAAAAAAADrHfn5ASQQAAAAAAAAAArsBx0B+fkgHesAAAAAAAD/Cgwf+fn5IA4dEus/IvcACgcMAfkg+QEB+SABHushbf8QHR/5HQH5+QEdHetEHx4K7B/5+QH5+fkdDBL5+SBE/wwdJfkf+fn5AR8g+fkfEArsCh/5+QEeJR/5+SAeBwAACgoe+SAlHwFAEhAfAAAAAPcKHh8eASYBHhAMAAAAAAAA9EMdIB8gHh0dBwAAAAAAAAAA7BAdQ+wHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//AADwfwAAwH8AAMB/AAAAPwAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAQAAgAcAAIAPAADADwAA8D8AAP//AAA');
diff --git a/addon-sdk/source/test/addons/places/lib/httpd.js b/addon-sdk/source/test/addons/places/lib/httpd.js
new file mode 100644
index 000000000..964dc9bbd
--- /dev/null
+++ b/addon-sdk/source/test/addons/places/lib/httpd.js
@@ -0,0 +1,5211 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/*
+* NOTE: do not edit this file, this is copied from:
+* https://github.com/mozilla/addon-sdk/blob/master/test/lib/httpd.js
+*/
+
+module.metadata = {
+ "stability": "experimental"
+};
+
+const { components, CC, Cc, Ci, Cr, Cu } = require("chrome");
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+
+const PR_UINT32_MAX = Math.pow(2, 32) - 1;
+
+/** True if debugging output is enabled, false otherwise. */
+var DEBUG = false; // non-const *only* so tweakable in server tests
+
+/** True if debugging output should be timestamped. */
+var DEBUG_TIMESTAMP = false; // non-const so tweakable in server tests
+
+var gGlobalObject = Cc["@mozilla.org/systemprincipal;1"].createInstance();
+
+/**
+* Asserts that the given condition holds. If it doesn't, the given message is
+* dumped, a stack trace is printed, and an exception is thrown to attempt to
+* stop execution (which unfortunately must rely upon the exception not being
+* accidentally swallowed by the code that uses it).
+*/
+function NS_ASSERT(cond, msg)
+{
+ if (DEBUG && !cond)
+ {
+ dumpn("###!!!");
+ dumpn("###!!! ASSERTION" + (msg ? ": " + msg : "!"));
+ dumpn("###!!! Stack follows:");
+
+ var stack = new Error().stack.split(/\n/);
+ dumpn(stack.map(function(val) { return "###!!! " + val; }).join("\n"));
+
+ throw Cr.NS_ERROR_ABORT;
+ }
+}
+
+/** Constructs an HTTP error object. */
+function HttpError(code, description)
+{
+ this.code = code;
+ this.description = description;
+}
+HttpError.prototype =
+{
+ toString: function()
+ {
+ return this.code + " " + this.description;
+ }
+};
+
+/**
+* Errors thrown to trigger specific HTTP server responses.
+*/
+const HTTP_400 = new HttpError(400, "Bad Request");
+const HTTP_401 = new HttpError(401, "Unauthorized");
+const HTTP_402 = new HttpError(402, "Payment Required");
+const HTTP_403 = new HttpError(403, "Forbidden");
+const HTTP_404 = new HttpError(404, "Not Found");
+const HTTP_405 = new HttpError(405, "Method Not Allowed");
+const HTTP_406 = new HttpError(406, "Not Acceptable");
+const HTTP_407 = new HttpError(407, "Proxy Authentication Required");
+const HTTP_408 = new HttpError(408, "Request Timeout");
+const HTTP_409 = new HttpError(409, "Conflict");
+const HTTP_410 = new HttpError(410, "Gone");
+const HTTP_411 = new HttpError(411, "Length Required");
+const HTTP_412 = new HttpError(412, "Precondition Failed");
+const HTTP_413 = new HttpError(413, "Request Entity Too Large");
+const HTTP_414 = new HttpError(414, "Request-URI Too Long");
+const HTTP_415 = new HttpError(415, "Unsupported Media Type");
+const HTTP_417 = new HttpError(417, "Expectation Failed");
+
+const HTTP_500 = new HttpError(500, "Internal Server Error");
+const HTTP_501 = new HttpError(501, "Not Implemented");
+const HTTP_502 = new HttpError(502, "Bad Gateway");
+const HTTP_503 = new HttpError(503, "Service Unavailable");
+const HTTP_504 = new HttpError(504, "Gateway Timeout");
+const HTTP_505 = new HttpError(505, "HTTP Version Not Supported");
+
+/** Creates a hash with fields corresponding to the values in arr. */
+function array2obj(arr)
+{
+ var obj = {};
+ for (var i = 0; i < arr.length; i++)
+ obj[arr[i]] = arr[i];
+ return obj;
+}
+
+/** Returns an array of the integers x through y, inclusive. */
+function range(x, y)
+{
+ var arr = [];
+ for (var i = x; i <= y; i++)
+ arr.push(i);
+ return arr;
+}
+
+/** An object (hash) whose fields are the numbers of all HTTP error codes. */
+const HTTP_ERROR_CODES = array2obj(range(400, 417).concat(range(500, 505)));
+
+
+/**
+* The character used to distinguish hidden files from non-hidden files, a la
+* the leading dot in Apache. Since that mechanism also hides files from
+* easy display in LXR, ls output, etc. however, we choose instead to use a
+* suffix character. If a requested file ends with it, we append another
+* when getting the file on the server. If it doesn't, we just look up that
+* file. Therefore, any file whose name ends with exactly one of the character
+* is "hidden" and available for use by the server.
+*/
+const HIDDEN_CHAR = "^";
+
+/**
+* The file name suffix indicating the file containing overridden headers for
+* a requested file.
+*/
+const HEADERS_SUFFIX = HIDDEN_CHAR + "headers" + HIDDEN_CHAR;
+
+/** Type used to denote SJS scripts for CGI-like functionality. */
+const SJS_TYPE = "sjs";
+
+/** Base for relative timestamps produced by dumpn(). */
+var firstStamp = 0;
+
+/** dump(str) with a trailing "\n" -- only outputs if DEBUG. */
+function dumpn(str)
+{
+ if (DEBUG)
+ {
+ var prefix = "HTTPD-INFO | ";
+ if (DEBUG_TIMESTAMP)
+ {
+ if (firstStamp === 0)
+ firstStamp = Date.now();
+
+ var elapsed = Date.now() - firstStamp; // milliseconds
+ var min = Math.floor(elapsed / 60000);
+ var sec = (elapsed % 60000) / 1000;
+
+ if (sec < 10)
+ prefix += min + ":0" + sec.toFixed(3) + " | ";
+ else
+ prefix += min + ":" + sec.toFixed(3) + " | ";
+ }
+
+ dump(prefix + str + "\n");
+ }
+}
+
+/** Dumps the current JS stack if DEBUG. */
+function dumpStack()
+{
+ // peel off the frames for dumpStack() and Error()
+ var stack = new Error().stack.split(/\n/).slice(2);
+ stack.forEach(dumpn);
+}
+
+
+/** The XPCOM thread manager. */
+var gThreadManager = null;
+
+/** The XPCOM prefs service. */
+var gRootPrefBranch = null;
+function getRootPrefBranch()
+{
+ if (!gRootPrefBranch)
+ {
+ gRootPrefBranch = Cc["@mozilla.org/preferences-service;1"]
+ .getService(Ci.nsIPrefBranch);
+ }
+ return gRootPrefBranch;
+}
+
+/**
+* JavaScript constructors for commonly-used classes; precreating these is a
+* speedup over doing the same from base principles. See the docs at
+* http://developer.mozilla.org/en/docs/components.Constructor for details.
+*/
+const ServerSocket = CC("@mozilla.org/network/server-socket;1",
+ "nsIServerSocket",
+ "init");
+const ScriptableInputStream = CC("@mozilla.org/scriptableinputstream;1",
+ "nsIScriptableInputStream",
+ "init");
+const Pipe = CC("@mozilla.org/pipe;1",
+ "nsIPipe",
+ "init");
+const FileInputStream = CC("@mozilla.org/network/file-input-stream;1",
+ "nsIFileInputStream",
+ "init");
+const ConverterInputStream = CC("@mozilla.org/intl/converter-input-stream;1",
+ "nsIConverterInputStream",
+ "init");
+const WritablePropertyBag = CC("@mozilla.org/hash-property-bag;1",
+ "nsIWritablePropertyBag2");
+const SupportsString = CC("@mozilla.org/supports-string;1",
+ "nsISupportsString");
+
+/* These two are non-const only so a test can overwrite them. */
+var BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
+ "nsIBinaryInputStream",
+ "setInputStream");
+var BinaryOutputStream = CC("@mozilla.org/binaryoutputstream;1",
+ "nsIBinaryOutputStream",
+ "setOutputStream");
+
+/**
+* Returns the RFC 822/1123 representation of a date.
+*
+* @param date : Number
+* the date, in milliseconds from midnight (00:00:00), January 1, 1970 GMT
+* @returns string
+* the representation of the given date
+*/
+function toDateString(date)
+{
+ //
+ // rfc1123-date = wkday "," SP date1 SP time SP "GMT"
+ // date1 = 2DIGIT SP month SP 4DIGIT
+ // ; day month year (e.g., 02 Jun 1982)
+ // time = 2DIGIT ":" 2DIGIT ":" 2DIGIT
+ // ; 00:00:00 - 23:59:59
+ // wkday = "Mon" | "Tue" | "Wed"
+ // | "Thu" | "Fri" | "Sat" | "Sun"
+ // month = "Jan" | "Feb" | "Mar" | "Apr"
+ // | "May" | "Jun" | "Jul" | "Aug"
+ // | "Sep" | "Oct" | "Nov" | "Dec"
+ //
+
+ const wkdayStrings = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
+ const monthStrings = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
+
+ /**
+* Processes a date and returns the encoded UTC time as a string according to
+* the format specified in RFC 2616.
+*
+* @param date : Date
+* the date to process
+* @returns string
+* a string of the form "HH:MM:SS", ranging from "00:00:00" to "23:59:59"
+*/
+ function toTime(date)
+ {
+ var hrs = date.getUTCHours();
+ var rv = (hrs < 10) ? "0" + hrs : hrs;
+
+ var mins = date.getUTCMinutes();
+ rv += ":";
+ rv += (mins < 10) ? "0" + mins : mins;
+
+ var secs = date.getUTCSeconds();
+ rv += ":";
+ rv += (secs < 10) ? "0" + secs : secs;
+
+ return rv;
+ }
+
+ /**
+* Processes a date and returns the encoded UTC date as a string according to
+* the date1 format specified in RFC 2616.
+*
+* @param date : Date
+* the date to process
+* @returns string
+* a string of the form "HH:MM:SS", ranging from "00:00:00" to "23:59:59"
+*/
+ function toDate1(date)
+ {
+ var day = date.getUTCDate();
+ var month = date.getUTCMonth();
+ var year = date.getUTCFullYear();
+
+ var rv = (day < 10) ? "0" + day : day;
+ rv += " " + monthStrings[month];
+ rv += " " + year;
+
+ return rv;
+ }
+
+ date = new Date(date);
+
+ const fmtString = "%wkday%, %date1% %time% GMT";
+ var rv = fmtString.replace("%wkday%", wkdayStrings[date.getUTCDay()]);
+ rv = rv.replace("%time%", toTime(date));
+ return rv.replace("%date1%", toDate1(date));
+}
+
+/**
+* Prints out a human-readable representation of the object o and its fields,
+* omitting those whose names begin with "_" if showMembers != true (to ignore
+* "private" properties exposed via getters/setters).
+*/
+function printObj(o, showMembers)
+{
+ var s = "******************************\n";
+ s += "o = {\n";
+ for (var i in o)
+ {
+ if (typeof(i) != "string" ||
+ (showMembers || (i.length > 0 && i[0] != "_")))
+ s+= " " + i + ": " + o[i] + ",\n";
+ }
+ s += " };\n";
+ s += "******************************";
+ dumpn(s);
+}
+
+/**
+* Instantiates a new HTTP server.
+*/
+function nsHttpServer()
+{
+ if (!gThreadManager)
+ gThreadManager = Cc["@mozilla.org/thread-manager;1"].getService();
+
+ /** The port on which this server listens. */
+ this._port = undefined;
+
+ /** The socket associated with this. */
+ this._socket = null;
+
+ /** The handler used to process requests to this server. */
+ this._handler = new ServerHandler(this);
+
+ /** Naming information for this server. */
+ this._identity = new ServerIdentity();
+
+ /**
+* Indicates when the server is to be shut down at the end of the request.
+*/
+ this._doQuit = false;
+
+ /**
+* True if the socket in this is closed (and closure notifications have been
+* sent and processed if the socket was ever opened), false otherwise.
+*/
+ this._socketClosed = true;
+
+ /**
+* Used for tracking existing connections and ensuring that all connections
+* are properly cleaned up before server shutdown; increases by 1 for every
+* new incoming connection.
+*/
+ this._connectionGen = 0;
+
+ /**
+* Hash of all open connections, indexed by connection number at time of
+* creation.
+*/
+ this._connections = {};
+}
+nsHttpServer.prototype =
+{
+ classID: components.ID("{54ef6f81-30af-4b1d-ac55-8ba811293e41}"),
+
+ // NSISERVERSOCKETLISTENER
+
+ /**
+* Processes an incoming request coming in on the given socket and contained
+* in the given transport.
+*
+* @param socket : nsIServerSocket
+* the socket through which the request was served
+* @param trans : nsISocketTransport
+* the transport for the request/response
+* @see nsIServerSocketListener.onSocketAccepted
+*/
+ onSocketAccepted: function(socket, trans)
+ {
+ dumpn("*** onSocketAccepted(socket=" + socket + ", trans=" + trans + ")");
+
+ dumpn(">>> new connection on " + trans.host + ":" + trans.port);
+
+ const SEGMENT_SIZE = 8192;
+ const SEGMENT_COUNT = 1024;
+ try
+ {
+ var input = trans.openInputStream(0, SEGMENT_SIZE, SEGMENT_COUNT)
+ .QueryInterface(Ci.nsIAsyncInputStream);
+ var output = trans.openOutputStream(0, 0, 0);
+ }
+ catch (e)
+ {
+ dumpn("*** error opening transport streams: " + e);
+ trans.close(Cr.NS_BINDING_ABORTED);
+ return;
+ }
+
+ var connectionNumber = ++this._connectionGen;
+
+ try
+ {
+ var conn = new Connection(input, output, this, socket.port, trans.port,
+ connectionNumber);
+ var reader = new RequestReader(conn);
+
+ // XXX add request timeout functionality here!
+
+ // Note: must use main thread here, or we might get a GC that will cause
+ // threadsafety assertions. We really need to fix XPConnect so that
+ // you can actually do things in multi-threaded JS. :-(
+ input.asyncWait(reader, 0, 0, gThreadManager.mainThread);
+ }
+ catch (e)
+ {
+ // Assume this connection can't be salvaged and bail on it completely;
+ // don't attempt to close it so that we can assert that any connection
+ // being closed is in this._connections.
+ dumpn("*** error in initial request-processing stages: " + e);
+ trans.close(Cr.NS_BINDING_ABORTED);
+ return;
+ }
+
+ this._connections[connectionNumber] = conn;
+ dumpn("*** starting connection " + connectionNumber);
+ },
+
+ /**
+* Called when the socket associated with this is closed.
+*
+* @param socket : nsIServerSocket
+* the socket being closed
+* @param status : nsresult
+* the reason the socket stopped listening (NS_BINDING_ABORTED if the server
+* was stopped using nsIHttpServer.stop)
+* @see nsIServerSocketListener.onStopListening
+*/
+ onStopListening: function(socket, status)
+ {
+ dumpn(">>> shutting down server on port " + socket.port);
+ this._socketClosed = true;
+ if (!this._hasOpenConnections())
+ {
+ dumpn("*** no open connections, notifying async from onStopListening");
+
+ // Notify asynchronously so that any pending teardown in stop() has a
+ // chance to run first.
+ var self = this;
+ var stopEvent =
+ {
+ run: function()
+ {
+ dumpn("*** _notifyStopped async callback");
+ self._notifyStopped();
+ }
+ };
+ gThreadManager.currentThread
+ .dispatch(stopEvent, Ci.nsIThread.DISPATCH_NORMAL);
+ }
+ },
+
+ // NSIHTTPSERVER
+
+ //
+ // see nsIHttpServer.start
+ //
+ start: function(port)
+ {
+ this._start(port, "localhost")
+ },
+
+ _start: function(port, host)
+ {
+ if (this._socket)
+ throw Cr.NS_ERROR_ALREADY_INITIALIZED;
+
+ this._port = port;
+ this._doQuit = this._socketClosed = false;
+
+ this._host = host;
+
+ // The listen queue needs to be long enough to handle
+ // network.http.max-persistent-connections-per-server concurrent connections,
+ // plus a safety margin in case some other process is talking to
+ // the server as well.
+ var prefs = getRootPrefBranch();
+ var maxConnections;
+ try {
+ // Bug 776860: The original pref was removed in favor of this new one:
+ maxConnections = prefs.getIntPref("network.http.max-persistent-connections-per-server") + 5;
+ }
+ catch(e) {
+ maxConnections = prefs.getIntPref("network.http.max-connections-per-server") + 5;
+ }
+
+ try
+ {
+ var loopback = true;
+ if (this._host != "127.0.0.1" && this._host != "localhost") {
+ var loopback = false;
+ }
+
+ var socket = new ServerSocket(this._port,
+ loopback, // true = localhost, false = everybody
+ maxConnections);
+ dumpn(">>> listening on port " + socket.port + ", " + maxConnections +
+ " pending connections");
+ socket.asyncListen(this);
+ this._identity._initialize(socket.port, host, true);
+ this._socket = socket;
+ }
+ catch (e)
+ {
+ dumpn("!!! could not start server on port " + port + ": " + e);
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ }
+ },
+
+ //
+ // see nsIHttpServer.stop
+ //
+ stop: function(callback)
+ {
+ if (!callback)
+ throw Cr.NS_ERROR_NULL_POINTER;
+ if (!this._socket)
+ throw Cr.NS_ERROR_UNEXPECTED;
+
+ this._stopCallback = typeof callback === "function"
+ ? callback
+ : function() { callback.onStopped(); };
+
+ dumpn(">>> stopping listening on port " + this._socket.port);
+ this._socket.close();
+ this._socket = null;
+
+ // We can't have this identity any more, and the port on which we're running
+ // this server now could be meaningless the next time around.
+ this._identity._teardown();
+
+ this._doQuit = false;
+
+ // socket-close notification and pending request completion happen async
+ },
+
+ //
+ // see nsIHttpServer.registerFile
+ //
+ registerFile: function(path, file)
+ {
+ if (file && (!file.exists() || file.isDirectory()))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ this._handler.registerFile(path, file);
+ },
+
+ //
+ // see nsIHttpServer.registerDirectory
+ //
+ registerDirectory: function(path, directory)
+ {
+ // XXX true path validation!
+ if (path.charAt(0) != "/" ||
+ path.charAt(path.length - 1) != "/" ||
+ (directory &&
+ (!directory.exists() || !directory.isDirectory())))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ // XXX determine behavior of nonexistent /foo/bar when a /foo/bar/ mapping
+ // exists!
+
+ this._handler.registerDirectory(path, directory);
+ },
+
+ //
+ // see nsIHttpServer.registerPathHandler
+ //
+ registerPathHandler: function(path, handler)
+ {
+ this._handler.registerPathHandler(path, handler);
+ },
+
+ //
+ // see nsIHttpServer.registerPrefixHandler
+ //
+ registerPrefixHandler: function(prefix, handler)
+ {
+ this._handler.registerPrefixHandler(prefix, handler);
+ },
+
+ //
+ // see nsIHttpServer.registerErrorHandler
+ //
+ registerErrorHandler: function(code, handler)
+ {
+ this._handler.registerErrorHandler(code, handler);
+ },
+
+ //
+ // see nsIHttpServer.setIndexHandler
+ //
+ setIndexHandler: function(handler)
+ {
+ this._handler.setIndexHandler(handler);
+ },
+
+ //
+ // see nsIHttpServer.registerContentType
+ //
+ registerContentType: function(ext, type)
+ {
+ this._handler.registerContentType(ext, type);
+ },
+
+ //
+ // see nsIHttpServer.serverIdentity
+ //
+ get identity()
+ {
+ return this._identity;
+ },
+
+ //
+ // see nsIHttpServer.getState
+ //
+ getState: function(path, k)
+ {
+ return this._handler._getState(path, k);
+ },
+
+ //
+ // see nsIHttpServer.setState
+ //
+ setState: function(path, k, v)
+ {
+ return this._handler._setState(path, k, v);
+ },
+
+ //
+ // see nsIHttpServer.getSharedState
+ //
+ getSharedState: function(k)
+ {
+ return this._handler._getSharedState(k);
+ },
+
+ //
+ // see nsIHttpServer.setSharedState
+ //
+ setSharedState: function(k, v)
+ {
+ return this._handler._setSharedState(k, v);
+ },
+
+ //
+ // see nsIHttpServer.getObjectState
+ //
+ getObjectState: function(k)
+ {
+ return this._handler._getObjectState(k);
+ },
+
+ //
+ // see nsIHttpServer.setObjectState
+ //
+ setObjectState: function(k, v)
+ {
+ return this._handler._setObjectState(k, v);
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIServerSocketListener) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // NON-XPCOM PUBLIC API
+
+ /**
+* Returns true iff this server is not running (and is not in the process of
+* serving any requests still to be processed when the server was last
+* stopped after being run).
+*/
+ isStopped: function()
+ {
+ return this._socketClosed && !this._hasOpenConnections();
+ },
+
+ // PRIVATE IMPLEMENTATION
+
+ /** True if this server has any open connections to it, false otherwise. */
+ _hasOpenConnections: function()
+ {
+ //
+ // If we have any open connections, they're tracked as numeric properties on
+ // |this._connections|. The non-standard __count__ property could be used
+ // to check whether there are any properties, but standard-wise, even
+ // looking forward to ES5, there's no less ugly yet still O(1) way to do
+ // this.
+ //
+ for (var n in this._connections)
+ return true;
+ return false;
+ },
+
+ /** Calls the server-stopped callback provided when stop() was called. */
+ _notifyStopped: function()
+ {
+ NS_ASSERT(this._stopCallback !== null, "double-notifying?");
+ NS_ASSERT(!this._hasOpenConnections(), "should be done serving by now");
+
+ //
+ // NB: We have to grab this now, null out the member, *then* call the
+ // callback here, or otherwise the callback could (indirectly) futz with
+ // this._stopCallback by starting and immediately stopping this, at
+ // which point we'd be nulling out a field we no longer have a right to
+ // modify.
+ //
+ var callback = this._stopCallback;
+ this._stopCallback = null;
+ try
+ {
+ callback();
+ }
+ catch (e)
+ {
+ // not throwing because this is specified as being usually (but not
+ // always) asynchronous
+ dump("!!! error running onStopped callback: " + e + "\n");
+ }
+ },
+
+ /**
+* Notifies this server that the given connection has been closed.
+*
+* @param connection : Connection
+* the connection that was closed
+*/
+ _connectionClosed: function(connection)
+ {
+ NS_ASSERT(connection.number in this._connections,
+ "closing a connection " + this + " that we never added to the " +
+ "set of open connections?");
+ NS_ASSERT(this._connections[connection.number] === connection,
+ "connection number mismatch? " +
+ this._connections[connection.number]);
+ delete this._connections[connection.number];
+
+ // Fire a pending server-stopped notification if it's our responsibility.
+ if (!this._hasOpenConnections() && this._socketClosed)
+ this._notifyStopped();
+ },
+
+ /**
+* Requests that the server be shut down when possible.
+*/
+ _requestQuit: function()
+ {
+ dumpn(">>> requesting a quit");
+ dumpStack();
+ this._doQuit = true;
+ }
+};
+
+
+//
+// RFC 2396 section 3.2.2:
+//
+// host = hostname | IPv4address
+// hostname = *( domainlabel "." ) toplabel [ "." ]
+// domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
+// toplabel = alpha | alpha *( alphanum | "-" ) alphanum
+// IPv4address = 1*digit "." 1*digit "." 1*digit "." 1*digit
+//
+
+const HOST_REGEX =
+ new RegExp("^(?:" +
+ // *( domainlabel "." )
+ "(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)*" +
+ // toplabel
+ "[a-z](?:[a-z0-9-]*[a-z0-9])?" +
+ "|" +
+ // IPv4 address
+ "\\d+\\.\\d+\\.\\d+\\.\\d+" +
+ ")$",
+ "i");
+
+
+/**
+* Represents the identity of a server. An identity consists of a set of
+* (scheme, host, port) tuples denoted as locations (allowing a single server to
+* serve multiple sites or to be used behind both HTTP and HTTPS proxies for any
+* host/port). Any incoming request must be to one of these locations, or it
+* will be rejected with an HTTP 400 error. One location, denoted as the
+* primary location, is the location assigned in contexts where a location
+* cannot otherwise be endogenously derived, such as for HTTP/1.0 requests.
+*
+* A single identity may contain at most one location per unique host/port pair;
+* other than that, no restrictions are placed upon what locations may
+* constitute an identity.
+*/
+function ServerIdentity()
+{
+ /** The scheme of the primary location. */
+ this._primaryScheme = "http";
+
+ /** The hostname of the primary location. */
+ this._primaryHost = "127.0.0.1"
+
+ /** The port number of the primary location. */
+ this._primaryPort = -1;
+
+ /**
+* The current port number for the corresponding server, stored so that a new
+* primary location can always be set if the current one is removed.
+*/
+ this._defaultPort = -1;
+
+ /**
+* Maps hosts to maps of ports to schemes, e.g. the following would represent
+* https://example.com:789/ and http://example.org/:
+*
+* {
+* "xexample.com": { 789: "https" },
+* "xexample.org": { 80: "http" }
+* }
+*
+* Note the "x" prefix on hostnames, which prevents collisions with special
+* JS names like "prototype".
+*/
+ this._locations = { "xlocalhost": {} };
+}
+ServerIdentity.prototype =
+{
+ // NSIHTTPSERVERIDENTITY
+
+ //
+ // see nsIHttpServerIdentity.primaryScheme
+ //
+ get primaryScheme()
+ {
+ if (this._primaryPort === -1)
+ throw Cr.NS_ERROR_NOT_INITIALIZED;
+ return this._primaryScheme;
+ },
+
+ //
+ // see nsIHttpServerIdentity.primaryHost
+ //
+ get primaryHost()
+ {
+ if (this._primaryPort === -1)
+ throw Cr.NS_ERROR_NOT_INITIALIZED;
+ return this._primaryHost;
+ },
+
+ //
+ // see nsIHttpServerIdentity.primaryPort
+ //
+ get primaryPort()
+ {
+ if (this._primaryPort === -1)
+ throw Cr.NS_ERROR_NOT_INITIALIZED;
+ return this._primaryPort;
+ },
+
+ //
+ // see nsIHttpServerIdentity.add
+ //
+ add: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ var entry = this._locations["x" + host];
+ if (!entry)
+ this._locations["x" + host] = entry = {};
+
+ entry[port] = scheme;
+ },
+
+ //
+ // see nsIHttpServerIdentity.remove
+ //
+ remove: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ var entry = this._locations["x" + host];
+ if (!entry)
+ return false;
+
+ var present = port in entry;
+ delete entry[port];
+
+ if (this._primaryScheme == scheme &&
+ this._primaryHost == host &&
+ this._primaryPort == port &&
+ this._defaultPort !== -1)
+ {
+ // Always keep at least one identity in existence at any time, unless
+ // we're in the process of shutting down (the last condition above).
+ this._primaryPort = -1;
+ this._initialize(this._defaultPort, host, false);
+ }
+
+ return present;
+ },
+
+ //
+ // see nsIHttpServerIdentity.has
+ //
+ has: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ return "x" + host in this._locations &&
+ scheme === this._locations["x" + host][port];
+ },
+
+ //
+ // see nsIHttpServerIdentity.has
+ //
+ getScheme: function(host, port)
+ {
+ this._validate("http", host, port);
+
+ var entry = this._locations["x" + host];
+ if (!entry)
+ return "";
+
+ return entry[port] || "";
+ },
+
+ //
+ // see nsIHttpServerIdentity.setPrimary
+ //
+ setPrimary: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ this.add(scheme, host, port);
+
+ this._primaryScheme = scheme;
+ this._primaryHost = host;
+ this._primaryPort = port;
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIHttpServerIdentity) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // PRIVATE IMPLEMENTATION
+
+ /**
+* Initializes the primary name for the corresponding server, based on the
+* provided port number.
+*/
+ _initialize: function(port, host, addSecondaryDefault)
+ {
+ this._host = host;
+ if (this._primaryPort !== -1)
+ this.add("http", host, port);
+ else
+ this.setPrimary("http", "localhost", port);
+ this._defaultPort = port;
+
+ // Only add this if we're being called at server startup
+ if (addSecondaryDefault && host != "127.0.0.1")
+ this.add("http", "127.0.0.1", port);
+ },
+
+ /**
+* Called at server shutdown time, unsets the primary location only if it was
+* the default-assigned location and removes the default location from the
+* set of locations used.
+*/
+ _teardown: function()
+ {
+ if (this._host != "127.0.0.1") {
+ // Not the default primary location, nothing special to do here
+ this.remove("http", "127.0.0.1", this._defaultPort);
+ }
+
+ // This is a *very* tricky bit of reasoning here; make absolutely sure the
+ // tests for this code pass before you commit changes to it.
+ if (this._primaryScheme == "http" &&
+ this._primaryHost == this._host &&
+ this._primaryPort == this._defaultPort)
+ {
+ // Make sure we don't trigger the readding logic in .remove(), then remove
+ // the default location.
+ var port = this._defaultPort;
+ this._defaultPort = -1;
+ this.remove("http", this._host, port);
+
+ // Ensure a server start triggers the setPrimary() path in ._initialize()
+ this._primaryPort = -1;
+ }
+ else
+ {
+ // No reason not to remove directly as it's not our primary location
+ this.remove("http", this._host, this._defaultPort);
+ }
+ },
+
+ /**
+* Ensures scheme, host, and port are all valid with respect to RFC 2396.
+*
+* @throws NS_ERROR_ILLEGAL_VALUE
+* if any argument doesn't match the corresponding production
+*/
+ _validate: function(scheme, host, port)
+ {
+ if (scheme !== "http" && scheme !== "https")
+ {
+ dumpn("*** server only supports http/https schemes: '" + scheme + "'");
+ dumpStack();
+ throw Cr.NS_ERROR_ILLEGAL_VALUE;
+ }
+ if (!HOST_REGEX.test(host))
+ {
+ dumpn("*** unexpected host: '" + host + "'");
+ throw Cr.NS_ERROR_ILLEGAL_VALUE;
+ }
+ if (port < 0 || port > 65535)
+ {
+ dumpn("*** unexpected port: '" + port + "'");
+ throw Cr.NS_ERROR_ILLEGAL_VALUE;
+ }
+ }
+};
+
+
+/**
+* Represents a connection to the server (and possibly in the future the thread
+* on which the connection is processed).
+*
+* @param input : nsIInputStream
+* stream from which incoming data on the connection is read
+* @param output : nsIOutputStream
+* stream to write data out the connection
+* @param server : nsHttpServer
+* the server handling the connection
+* @param port : int
+* the port on which the server is running
+* @param outgoingPort : int
+* the outgoing port used by this connection
+* @param number : uint
+* a serial number used to uniquely identify this connection
+*/
+function Connection(input, output, server, port, outgoingPort, number)
+{
+ dumpn("*** opening new connection " + number + " on port " + outgoingPort);
+
+ /** Stream of incoming data. */
+ this.input = input;
+
+ /** Stream for outgoing data. */
+ this.output = output;
+
+ /** The server associated with this request. */
+ this.server = server;
+
+ /** The port on which the server is running. */
+ this.port = port;
+
+ /** The outgoing poort used by this connection. */
+ this._outgoingPort = outgoingPort;
+
+ /** The serial number of this connection. */
+ this.number = number;
+
+ /**
+* The request for which a response is being generated, null if the
+* incoming request has not been fully received or if it had errors.
+*/
+ this.request = null;
+
+ /** State variables for debugging. */
+ this._closed = this._processed = false;
+}
+Connection.prototype =
+{
+ /** Closes this connection's input/output streams. */
+ close: function()
+ {
+ dumpn("*** closing connection " + this.number +
+ " on port " + this._outgoingPort);
+
+ this.input.close();
+ this.output.close();
+ this._closed = true;
+
+ var server = this.server;
+ server._connectionClosed(this);
+
+ // If an error triggered a server shutdown, act on it now
+ if (server._doQuit)
+ server.stop(function() { /* not like we can do anything better */ });
+ },
+
+ /**
+* Initiates processing of this connection, using the data in the given
+* request.
+*
+* @param request : Request
+* the request which should be processed
+*/
+ process: function(request)
+ {
+ NS_ASSERT(!this._closed && !this._processed);
+
+ this._processed = true;
+
+ this.request = request;
+ this.server._handler.handleResponse(this);
+ },
+
+ /**
+* Initiates processing of this connection, generating a response with the
+* given HTTP error code.
+*
+* @param code : uint
+* an HTTP code, so in the range [0, 1000)
+* @param request : Request
+* incomplete data about the incoming request (since there were errors
+* during its processing
+*/
+ processError: function(code, request)
+ {
+ NS_ASSERT(!this._closed && !this._processed);
+
+ this._processed = true;
+ this.request = request;
+ this.server._handler.handleError(code, this);
+ },
+
+ /** Converts this to a string for debugging purposes. */
+ toString: function()
+ {
+ return "<Connection(" + this.number +
+ (this.request ? ", " + this.request.path : "") +"): " +
+ (this._closed ? "closed" : "open") + ">";
+ }
+};
+
+
+
+/** Returns an array of count bytes from the given input stream. */
+function readBytes(inputStream, count)
+{
+ return new BinaryInputStream(inputStream).readByteArray(count);
+}
+
+
+
+/** Request reader processing states; see RequestReader for details. */
+const READER_IN_REQUEST_LINE = 0;
+const READER_IN_HEADERS = 1;
+const READER_IN_BODY = 2;
+const READER_FINISHED = 3;
+
+
+/**
+* Reads incoming request data asynchronously, does any necessary preprocessing,
+* and forwards it to the request handler. Processing occurs in three states:
+*
+* READER_IN_REQUEST_LINE Reading the request's status line
+* READER_IN_HEADERS Reading headers in the request
+* READER_IN_BODY Reading the body of the request
+* READER_FINISHED Entire request has been read and processed
+*
+* During the first two stages, initial metadata about the request is gathered
+* into a Request object. Once the status line and headers have been processed,
+* we start processing the body of the request into the Request. Finally, when
+* the entire body has been read, we create a Response and hand it off to the
+* ServerHandler to be given to the appropriate request handler.
+*
+* @param connection : Connection
+* the connection for the request being read
+*/
+function RequestReader(connection)
+{
+ /** Connection metadata for this request. */
+ this._connection = connection;
+
+ /**
+* A container providing line-by-line access to the raw bytes that make up the
+* data which has been read from the connection but has not yet been acted
+* upon (by passing it to the request handler or by extracting request
+* metadata from it).
+*/
+ this._data = new LineData();
+
+ /**
+* The amount of data remaining to be read from the body of this request.
+* After all headers in the request have been read this is the value in the
+* Content-Length header, but as the body is read its value decreases to zero.
+*/
+ this._contentLength = 0;
+
+ /** The current state of parsing the incoming request. */
+ this._state = READER_IN_REQUEST_LINE;
+
+ /** Metadata constructed from the incoming request for the request handler. */
+ this._metadata = new Request(connection.port);
+
+ /**
+* Used to preserve state if we run out of line data midway through a
+* multi-line header. _lastHeaderName stores the name of the header, while
+* _lastHeaderValue stores the value we've seen so far for the header.
+*
+* These fields are always either both undefined or both strings.
+*/
+ this._lastHeaderName = this._lastHeaderValue = undefined;
+}
+RequestReader.prototype =
+{
+ // NSIINPUTSTREAMCALLBACK
+
+ /**
+* Called when more data from the incoming request is available. This method
+* then reads the available data from input and deals with that data as
+* necessary, depending upon the syntax of already-downloaded data.
+*
+* @param input : nsIAsyncInputStream
+* the stream of incoming data from the connection
+*/
+ onInputStreamReady: function(input)
+ {
+ dumpn("*** onInputStreamReady(input=" + input + ") on thread " +
+ gThreadManager.currentThread + " (main is " +
+ gThreadManager.mainThread + ")");
+ dumpn("*** this._state == " + this._state);
+
+ // Handle cases where we get more data after a request error has been
+ // discovered but *before* we can close the connection.
+ var data = this._data;
+ if (!data)
+ return;
+
+ try
+ {
+ data.appendBytes(readBytes(input, input.available()));
+ }
+ catch (e)
+ {
+ if (streamClosed(e))
+ {
+ dumpn("*** WARNING: unexpected error when reading from socket; will " +
+ "be treated as if the input stream had been closed");
+ dumpn("*** WARNING: actual error was: " + e);
+ }
+
+ // We've lost a race -- input has been closed, but we're still expecting
+ // to read more data. available() will throw in this case, and since
+ // we're dead in the water now, destroy the connection.
+ dumpn("*** onInputStreamReady called on a closed input, destroying " +
+ "connection");
+ this._connection.close();
+ return;
+ }
+
+ switch (this._state)
+ {
+ default:
+ NS_ASSERT(false, "invalid state: " + this._state);
+ break;
+
+ case READER_IN_REQUEST_LINE:
+ if (!this._processRequestLine())
+ break;
+ /* fall through */
+
+ case READER_IN_HEADERS:
+ if (!this._processHeaders())
+ break;
+ /* fall through */
+
+ case READER_IN_BODY:
+ this._processBody();
+ }
+
+ if (this._state != READER_FINISHED)
+ input.asyncWait(this, 0, 0, gThreadManager.currentThread);
+ },
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(aIID)
+ {
+ if (aIID.equals(Ci.nsIInputStreamCallback) ||
+ aIID.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // PRIVATE API
+
+ /**
+* Processes unprocessed, downloaded data as a request line.
+*
+* @returns boolean
+* true iff the request line has been fully processed
+*/
+ _processRequestLine: function()
+ {
+ NS_ASSERT(this._state == READER_IN_REQUEST_LINE);
+
+ // Servers SHOULD ignore any empty line(s) received where a Request-Line
+ // is expected (section 4.1).
+ var data = this._data;
+ var line = {};
+ var readSuccess;
+ while ((readSuccess = data.readLine(line)) && line.value == "")
+ dumpn("*** ignoring beginning blank line...");
+
+ // if we don't have a full line, wait until we do
+ if (!readSuccess)
+ return false;
+
+ // we have the first non-blank line
+ try
+ {
+ this._parseRequestLine(line.value);
+ this._state = READER_IN_HEADERS;
+ return true;
+ }
+ catch (e)
+ {
+ this._handleError(e);
+ return false;
+ }
+ },
+
+ /**
+* Processes stored data, assuming it is either at the beginning or in
+* the middle of processing request headers.
+*
+* @returns boolean
+* true iff header data in the request has been fully processed
+*/
+ _processHeaders: function()
+ {
+ NS_ASSERT(this._state == READER_IN_HEADERS);
+
+ // XXX things to fix here:
+ //
+ // - need to support RFC 2047-encoded non-US-ASCII characters
+
+ try
+ {
+ var done = this._parseHeaders();
+ if (done)
+ {
+ var request = this._metadata;
+
+ // XXX this is wrong for requests with transfer-encodings applied to
+ // them, particularly chunked (which by its nature can have no
+ // meaningful Content-Length header)!
+ this._contentLength = request.hasHeader("Content-Length")
+ ? parseInt(request.getHeader("Content-Length"), 10)
+ : 0;
+ dumpn("_processHeaders, Content-length=" + this._contentLength);
+
+ this._state = READER_IN_BODY;
+ }
+ return done;
+ }
+ catch (e)
+ {
+ this._handleError(e);
+ return false;
+ }
+ },
+
+ /**
+* Processes stored data, assuming it is either at the beginning or in
+* the middle of processing the request body.
+*
+* @returns boolean
+* true iff the request body has been fully processed
+*/
+ _processBody: function()
+ {
+ NS_ASSERT(this._state == READER_IN_BODY);
+
+ // XXX handle chunked transfer-coding request bodies!
+
+ try
+ {
+ if (this._contentLength > 0)
+ {
+ var data = this._data.purge();
+ var count = Math.min(data.length, this._contentLength);
+ dumpn("*** loading data=" + data + " len=" + data.length +
+ " excess=" + (data.length - count));
+
+ var bos = new BinaryOutputStream(this._metadata._bodyOutputStream);
+ bos.writeByteArray(data, count);
+ this._contentLength -= count;
+ }
+
+ dumpn("*** remaining body data len=" + this._contentLength);
+ if (this._contentLength == 0)
+ {
+ this._validateRequest();
+ this._state = READER_FINISHED;
+ this._handleResponse();
+ return true;
+ }
+
+ return false;
+ }
+ catch (e)
+ {
+ this._handleError(e);
+ return false;
+ }
+ },
+
+ /**
+* Does various post-header checks on the data in this request.
+*
+* @throws : HttpError
+* if the request was malformed in some way
+*/
+ _validateRequest: function()
+ {
+ NS_ASSERT(this._state == READER_IN_BODY);
+
+ dumpn("*** _validateRequest");
+
+ var metadata = this._metadata;
+ var headers = metadata._headers;
+
+ // 19.6.1.1 -- servers MUST report 400 to HTTP/1.1 requests w/o Host header
+ var identity = this._connection.server.identity;
+ if (metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1))
+ {
+ if (!headers.hasHeader("Host"))
+ {
+ dumpn("*** malformed HTTP/1.1 or greater request with no Host header!");
+ throw HTTP_400;
+ }
+
+ // If the Request-URI wasn't absolute, then we need to determine our host.
+ // We have to determine what scheme was used to access us based on the
+ // server identity data at this point, because the request just doesn't
+ // contain enough data on its own to do this, sadly.
+ if (!metadata._host)
+ {
+ var host, port;
+ var hostPort = headers.getHeader("Host");
+ var colon = hostPort.indexOf(":");
+ if (colon < 0)
+ {
+ host = hostPort;
+ port = "";
+ }
+ else
+ {
+ host = hostPort.substring(0, colon);
+ port = hostPort.substring(colon + 1);
+ }
+
+ // NB: We allow an empty port here because, oddly, a colon may be
+ // present even without a port number, e.g. "example.com:"; in this
+ // case the default port applies.
+ if (!HOST_REGEX.test(host) || !/^\d*$/.test(port))
+ {
+ dumpn("*** malformed hostname (" + hostPort + ") in Host " +
+ "header, 400 time");
+ throw HTTP_400;
+ }
+
+ // If we're not given a port, we're stuck, because we don't know what
+ // scheme to use to look up the correct port here, in general. Since
+ // the HTTPS case requires a tunnel/proxy and thus requires that the
+ // requested URI be absolute (and thus contain the necessary
+ // information), let's assume HTTP will prevail and use that.
+ port = +port || 80;
+
+ var scheme = identity.getScheme(host, port);
+ if (!scheme)
+ {
+ dumpn("*** unrecognized hostname (" + hostPort + ") in Host " +
+ "header, 400 time");
+ throw HTTP_400;
+ }
+
+ metadata._scheme = scheme;
+ metadata._host = host;
+ metadata._port = port;
+ }
+ }
+ else
+ {
+ NS_ASSERT(metadata._host === undefined,
+ "HTTP/1.0 doesn't allow absolute paths in the request line!");
+
+ metadata._scheme = identity.primaryScheme;
+ metadata._host = identity.primaryHost;
+ metadata._port = identity.primaryPort;
+ }
+
+ NS_ASSERT(identity.has(metadata._scheme, metadata._host, metadata._port),
+ "must have a location we recognize by now!");
+ },
+
+ /**
+* Handles responses in case of error, either in the server or in the request.
+*
+* @param e
+* the specific error encountered, which is an HttpError in the case where
+* the request is in some way invalid or cannot be fulfilled; if this isn't
+* an HttpError we're going to be paranoid and shut down, because that
+* shouldn't happen, ever
+*/
+ _handleError: function(e)
+ {
+ // Don't fall back into normal processing!
+ this._state = READER_FINISHED;
+
+ var server = this._connection.server;
+ if (e instanceof HttpError)
+ {
+ var code = e.code;
+ }
+ else
+ {
+ dumpn("!!! UNEXPECTED ERROR: " + e +
+ (e.lineNumber ? ", line " + e.lineNumber : ""));
+
+ // no idea what happened -- be paranoid and shut down
+ code = 500;
+ server._requestQuit();
+ }
+
+ // make attempted reuse of data an error
+ this._data = null;
+
+ this._connection.processError(code, this._metadata);
+ },
+
+ /**
+* Now that we've read the request line and headers, we can actually hand off
+* the request to be handled.
+*
+* This method is called once per request, after the request line and all
+* headers and the body, if any, have been received.
+*/
+ _handleResponse: function()
+ {
+ NS_ASSERT(this._state == READER_FINISHED);
+
+ // We don't need the line-based data any more, so make attempted reuse an
+ // error.
+ this._data = null;
+
+ this._connection.process(this._metadata);
+ },
+
+
+ // PARSING
+
+ /**
+* Parses the request line for the HTTP request associated with this.
+*
+* @param line : string
+* the request line
+*/
+ _parseRequestLine: function(line)
+ {
+ NS_ASSERT(this._state == READER_IN_REQUEST_LINE);
+
+ dumpn("*** _parseRequestLine('" + line + "')");
+
+ var metadata = this._metadata;
+
+ // clients and servers SHOULD accept any amount of SP or HT characters
+ // between fields, even though only a single SP is required (section 19.3)
+ var request = line.split(/[ \t]+/);
+ if (!request || request.length != 3)
+ throw HTTP_400;
+
+ metadata._method = request[0];
+
+ // get the HTTP version
+ var ver = request[2];
+ var match = ver.match(/^HTTP\/(\d+\.\d+)$/);
+ if (!match)
+ throw HTTP_400;
+
+ // determine HTTP version
+ try
+ {
+ metadata._httpVersion = new nsHttpVersion(match[1]);
+ if (!metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_0))
+ throw "unsupported HTTP version";
+ }
+ catch (e)
+ {
+ // we support HTTP/1.0 and HTTP/1.1 only
+ throw HTTP_501;
+ }
+
+
+ var fullPath = request[1];
+ var serverIdentity = this._connection.server.identity;
+
+ var scheme, host, port;
+
+ if (fullPath.charAt(0) != "/")
+ {
+ // No absolute paths in the request line in HTTP prior to 1.1
+ if (!metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1))
+ throw HTTP_400;
+
+ try
+ {
+ var uri = Cc["@mozilla.org/network/io-service;1"]
+ .getService(Ci.nsIIOService)
+ .newURI(fullPath, null, null);
+ fullPath = uri.path;
+ scheme = uri.scheme;
+ host = metadata._host = uri.asciiHost;
+ port = uri.port;
+ if (port === -1)
+ {
+ if (scheme === "http")
+ port = 80;
+ else if (scheme === "https")
+ port = 443;
+ else
+ throw HTTP_400;
+ }
+ }
+ catch (e)
+ {
+ // If the host is not a valid host on the server, the response MUST be a
+ // 400 (Bad Request) error message (section 5.2). Alternately, the URI
+ // is malformed.
+ throw HTTP_400;
+ }
+
+ if (!serverIdentity.has(scheme, host, port) || fullPath.charAt(0) != "/")
+ throw HTTP_400;
+ }
+
+ var splitter = fullPath.indexOf("?");
+ if (splitter < 0)
+ {
+ // _queryString already set in ctor
+ metadata._path = fullPath;
+ }
+ else
+ {
+ metadata._path = fullPath.substring(0, splitter);
+ metadata._queryString = fullPath.substring(splitter + 1);
+ }
+
+ metadata._scheme = scheme;
+ metadata._host = host;
+ metadata._port = port;
+ },
+
+ /**
+* Parses all available HTTP headers in this until the header-ending CRLFCRLF,
+* adding them to the store of headers in the request.
+*
+* @throws
+* HTTP_400 if the headers are malformed
+* @returns boolean
+* true if all headers have now been processed, false otherwise
+*/
+ _parseHeaders: function()
+ {
+ NS_ASSERT(this._state == READER_IN_HEADERS);
+
+ dumpn("*** _parseHeaders");
+
+ var data = this._data;
+
+ var headers = this._metadata._headers;
+ var lastName = this._lastHeaderName;
+ var lastVal = this._lastHeaderValue;
+
+ var line = {};
+ while (true)
+ {
+ NS_ASSERT(!((lastVal === undefined) ^ (lastName === undefined)),
+ lastName === undefined ?
+ "lastVal without lastName? lastVal: '" + lastVal + "'" :
+ "lastName without lastVal? lastName: '" + lastName + "'");
+
+ if (!data.readLine(line))
+ {
+ // save any data we have from the header we might still be processing
+ this._lastHeaderName = lastName;
+ this._lastHeaderValue = lastVal;
+ return false;
+ }
+
+ var lineText = line.value;
+ var firstChar = lineText.charAt(0);
+
+ // blank line means end of headers
+ if (lineText == "")
+ {
+ // we're finished with the previous header
+ if (lastName)
+ {
+ try
+ {
+ headers.setHeader(lastName, lastVal, true);
+ }
+ catch (e)
+ {
+ dumpn("*** e == " + e);
+ throw HTTP_400;
+ }
+ }
+ else
+ {
+ // no headers in request -- valid for HTTP/1.0 requests
+ }
+
+ // either way, we're done processing headers
+ this._state = READER_IN_BODY;
+ return true;
+ }
+ else if (firstChar == " " || firstChar == "\t")
+ {
+ // multi-line header if we've already seen a header line
+ if (!lastName)
+ {
+ // we don't have a header to continue!
+ throw HTTP_400;
+ }
+
+ // append this line's text to the value; starts with SP/HT, so no need
+ // for separating whitespace
+ lastVal += lineText;
+ }
+ else
+ {
+ // we have a new header, so set the old one (if one existed)
+ if (lastName)
+ {
+ try
+ {
+ headers.setHeader(lastName, lastVal, true);
+ }
+ catch (e)
+ {
+ dumpn("*** e == " + e);
+ throw HTTP_400;
+ }
+ }
+
+ var colon = lineText.indexOf(":"); // first colon must be splitter
+ if (colon < 1)
+ {
+ // no colon or missing header field-name
+ throw HTTP_400;
+ }
+
+ // set header name, value (to be set in the next loop, usually)
+ lastName = lineText.substring(0, colon);
+ lastVal = lineText.substring(colon + 1);
+ } // empty, continuation, start of header
+ } // while (true)
+ }
+};
+
+
+/** The character codes for CR and LF. */
+const CR = 0x0D, LF = 0x0A;
+
+/**
+* Calculates the number of characters before the first CRLF pair in array, or
+* -1 if the array contains no CRLF pair.
+*
+* @param array : Array
+* an array of numbers in the range [0, 256), each representing a single
+* character; the first CRLF is the lowest index i where
+* |array[i] == "\r".charCodeAt(0)| and |array[i+1] == "\n".charCodeAt(0)|,
+* if such an |i| exists, and -1 otherwise
+* @returns int
+* the index of the first CRLF if any were present, -1 otherwise
+*/
+function findCRLF(array)
+{
+ for (var i = array.indexOf(CR); i >= 0; i = array.indexOf(CR, i + 1))
+ {
+ if (array[i + 1] == LF)
+ return i;
+ }
+ return -1;
+}
+
+
+/**
+* A container which provides line-by-line access to the arrays of bytes with
+* which it is seeded.
+*/
+function LineData()
+{
+ /** An array of queued bytes from which to get line-based characters. */
+ this._data = [];
+}
+LineData.prototype =
+{
+ /**
+* Appends the bytes in the given array to the internal data cache maintained
+* by this.
+*/
+ appendBytes: function(bytes)
+ {
+ Array.prototype.push.apply(this._data, bytes);
+ },
+
+ /**
+* Removes and returns a line of data, delimited by CRLF, from this.
+*
+* @param out
+* an object whose "value" property will be set to the first line of text
+* present in this, sans CRLF, if this contains a full CRLF-delimited line
+* of text; if this doesn't contain enough data, the value of the property
+* is undefined
+* @returns boolean
+* true if a full line of data could be read from the data in this, false
+* otherwise
+*/
+ readLine: function(out)
+ {
+ var data = this._data;
+ var length = findCRLF(data);
+ if (length < 0)
+ return false;
+
+ //
+ // We have the index of the CR, so remove all the characters, including
+ // CRLF, from the array with splice, and convert the removed array into the
+ // corresponding string, from which we then strip the trailing CRLF.
+ //
+ // Getting the line in this matter acknowledges that substring is an O(1)
+ // operation in SpiderMonkey because strings are immutable, whereas two
+ // splices, both from the beginning of the data, are less likely to be as
+ // cheap as a single splice plus two extra character conversions.
+ //
+ var line = String.fromCharCode.apply(null, data.splice(0, length + 2));
+ out.value = line.substring(0, length);
+
+ return true;
+ },
+
+ /**
+* Removes the bytes currently within this and returns them in an array.
+*
+* @returns Array
+* the bytes within this when this method is called
+*/
+ purge: function()
+ {
+ var data = this._data;
+ this._data = [];
+ return data;
+ }
+};
+
+
+
+/**
+* Creates a request-handling function for an nsIHttpRequestHandler object.
+*/
+function createHandlerFunc(handler)
+{
+ return function(metadata, response) { handler.handle(metadata, response); };
+}
+
+
+/**
+* The default handler for directories; writes an HTML response containing a
+* slightly-formatted directory listing.
+*/
+function defaultIndexHandler(metadata, response)
+{
+ response.setHeader("Content-Type", "text/html", false);
+
+ var path = htmlEscape(decodeURI(metadata.path));
+
+ //
+ // Just do a very basic bit of directory listings -- no need for too much
+ // fanciness, especially since we don't have a style sheet in which we can
+ // stick rules (don't want to pollute the default path-space).
+ //
+
+ var body = '<html>\
+<head>\
+<title>' + path + '</title>\
+</head>\
+<body>\
+<h1>' + path + '</h1>\
+<ol style="list-style-type: none">';
+
+ var directory = metadata.getProperty("directory").QueryInterface(Ci.nsILocalFile);
+ NS_ASSERT(directory && directory.isDirectory());
+
+ var fileList = [];
+ var files = directory.directoryEntries;
+ while (files.hasMoreElements())
+ {
+ var f = files.getNext().QueryInterface(Ci.nsIFile);
+ var name = f.leafName;
+ if (!f.isHidden() &&
+ (name.charAt(name.length - 1) != HIDDEN_CHAR ||
+ name.charAt(name.length - 2) == HIDDEN_CHAR))
+ fileList.push(f);
+ }
+
+ fileList.sort(fileSort);
+
+ for (var i = 0; i < fileList.length; i++)
+ {
+ var file = fileList[i];
+ try
+ {
+ var name = file.leafName;
+ if (name.charAt(name.length - 1) == HIDDEN_CHAR)
+ name = name.substring(0, name.length - 1);
+ var sep = file.isDirectory() ? "/" : "";
+
+ // Note: using " to delimit the attribute here because encodeURIComponent
+ // passes through '.
+ var item = '<li><a href="' + encodeURIComponent(name) + sep + '">' +
+ htmlEscape(name) + sep +
+ '</a></li>';
+
+ body += item;
+ }
+ catch (e) { /* some file system error, ignore the file */ }
+ }
+
+ body += ' </ol>\
+</body>\
+</html>';
+
+ response.bodyOutputStream.write(body, body.length);
+}
+
+/**
+* Sorts a and b (nsIFile objects) into an aesthetically pleasing order.
+*/
+function fileSort(a, b)
+{
+ var dira = a.isDirectory(), dirb = b.isDirectory();
+
+ if (dira && !dirb)
+ return -1;
+ if (dirb && !dira)
+ return 1;
+
+ var namea = a.leafName.toLowerCase(), nameb = b.leafName.toLowerCase();
+ return nameb > namea ? -1 : 1;
+}
+
+
+/**
+* Converts an externally-provided path into an internal path for use in
+* determining file mappings.
+*
+* @param path
+* the path to convert
+* @param encoded
+* true if the given path should be passed through decodeURI prior to
+* conversion
+* @throws URIError
+* if path is incorrectly encoded
+*/
+function toInternalPath(path, encoded)
+{
+ if (encoded)
+ path = decodeURI(path);
+
+ var comps = path.split("/");
+ for (var i = 0, sz = comps.length; i < sz; i++)
+ {
+ var comp = comps[i];
+ if (comp.charAt(comp.length - 1) == HIDDEN_CHAR)
+ comps[i] = comp + HIDDEN_CHAR;
+ }
+ return comps.join("/");
+}
+
+
+/**
+* Adds custom-specified headers for the given file to the given response, if
+* any such headers are specified.
+*
+* @param file
+* the file on the disk which is to be written
+* @param metadata
+* metadata about the incoming request
+* @param response
+* the Response to which any specified headers/data should be written
+* @throws HTTP_500
+* if an error occurred while processing custom-specified headers
+*/
+function maybeAddHeaders(file, metadata, response)
+{
+ var name = file.leafName;
+ if (name.charAt(name.length - 1) == HIDDEN_CHAR)
+ name = name.substring(0, name.length - 1);
+
+ var headerFile = file.parent;
+ headerFile.append(name + HEADERS_SUFFIX);
+
+ if (!headerFile.exists())
+ return;
+
+ const PR_RDONLY = 0x01;
+ var fis = new FileInputStream(headerFile, PR_RDONLY, 0o444,
+ Ci.nsIFileInputStream.CLOSE_ON_EOF);
+
+ try
+ {
+ var lis = new ConverterInputStream(fis, "UTF-8", 1024, 0x0);
+ lis.QueryInterface(Ci.nsIUnicharLineInputStream);
+
+ var line = {value: ""};
+ var more = lis.readLine(line);
+
+ if (!more && line.value == "")
+ return;
+
+
+ // request line
+
+ var status = line.value;
+ if (status.indexOf("HTTP ") == 0)
+ {
+ status = status.substring(5);
+ var space = status.indexOf(" ");
+ var code, description;
+ if (space < 0)
+ {
+ code = status;
+ description = "";
+ }
+ else
+ {
+ code = status.substring(0, space);
+ description = status.substring(space + 1, status.length);
+ }
+
+ response.setStatusLine(metadata.httpVersion, parseInt(code, 10), description);
+
+ line.value = "";
+ more = lis.readLine(line);
+ }
+
+ // headers
+ while (more || line.value != "")
+ {
+ var header = line.value;
+ var colon = header.indexOf(":");
+
+ response.setHeader(header.substring(0, colon),
+ header.substring(colon + 1, header.length),
+ false); // allow overriding server-set headers
+
+ line.value = "";
+ more = lis.readLine(line);
+ }
+ }
+ catch (e)
+ {
+ dumpn("WARNING: error in headers for " + metadata.path + ": " + e);
+ throw HTTP_500;
+ }
+ finally
+ {
+ fis.close();
+ }
+}
+
+
+/**
+* An object which handles requests for a server, executing default and
+* overridden behaviors as instructed by the code which uses and manipulates it.
+* Default behavior includes the paths / and /trace (diagnostics), with some
+* support for HTTP error pages for various codes and fallback to HTTP 500 if
+* those codes fail for any reason.
+*
+* @param server : nsHttpServer
+* the server in which this handler is being used
+*/
+function ServerHandler(server)
+{
+ // FIELDS
+
+ /**
+* The nsHttpServer instance associated with this handler.
+*/
+ this._server = server;
+
+ /**
+* A FileMap object containing the set of path->nsILocalFile mappings for
+* all directory mappings set in the server (e.g., "/" for /var/www/html/,
+* "/foo/bar/" for /local/path/, and "/foo/bar/baz/" for /local/path2).
+*
+* Note carefully: the leading and trailing "/" in each path (not file) are
+* removed before insertion to simplify the code which uses this. You have
+* been warned!
+*/
+ this._pathDirectoryMap = new FileMap();
+
+ /**
+* Custom request handlers for the server in which this resides. Path-handler
+* pairs are stored as property-value pairs in this property.
+*
+* @see ServerHandler.prototype._defaultPaths
+*/
+ this._overridePaths = {};
+
+ /**
+* Custom request handlers for the server in which this resides. Prefix-handler
+* pairs are stored as property-value pairs in this property.
+*/
+ this._overridePrefixes = {};
+
+ /**
+* Custom request handlers for the error handlers in the server in which this
+* resides. Path-handler pairs are stored as property-value pairs in this
+* property.
+*
+* @see ServerHandler.prototype._defaultErrors
+*/
+ this._overrideErrors = {};
+
+ /**
+* Maps file extensions to their MIME types in the server, overriding any
+* mapping that might or might not exist in the MIME service.
+*/
+ this._mimeMappings = {};
+
+ /**
+* The default handler for requests for directories, used to serve directories
+* when no index file is present.
+*/
+ this._indexHandler = defaultIndexHandler;
+
+ /** Per-path state storage for the server. */
+ this._state = {};
+
+ /** Entire-server state storage. */
+ this._sharedState = {};
+
+ /** Entire-server state storage for nsISupports values. */
+ this._objectState = {};
+}
+ServerHandler.prototype =
+{
+ // PUBLIC API
+
+ /**
+* Handles a request to this server, responding to the request appropriately
+* and initiating server shutdown if necessary.
+*
+* This method never throws an exception.
+*
+* @param connection : Connection
+* the connection for this request
+*/
+ handleResponse: function(connection)
+ {
+ var request = connection.request;
+ var response = new Response(connection);
+
+ var path = request.path;
+ dumpn("*** path == " + path);
+
+ try
+ {
+ try
+ {
+ if (path in this._overridePaths)
+ {
+ // explicit paths first, then files based on existing directory mappings,
+ // then (if the file doesn't exist) built-in server default paths
+ dumpn("calling override for " + path);
+ this._overridePaths[path](request, response);
+ }
+ else
+ {
+ let longestPrefix = "";
+ for (let prefix in this._overridePrefixes)
+ {
+ if (prefix.length > longestPrefix.length && path.startsWith(prefix))
+ {
+ longestPrefix = prefix;
+ }
+ }
+ if (longestPrefix.length > 0)
+ {
+ dumpn("calling prefix override for " + longestPrefix);
+ this._overridePrefixes[longestPrefix](request, response);
+ }
+ else
+ {
+ this._handleDefault(request, response);
+ }
+ }
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort(e);
+ return;
+ }
+
+ if (!(e instanceof HttpError))
+ {
+ dumpn("*** unexpected error: e == " + e);
+ throw HTTP_500;
+ }
+ if (e.code !== 404)
+ throw e;
+
+ dumpn("*** default: " + (path in this._defaultPaths));
+
+ response = new Response(connection);
+ if (path in this._defaultPaths)
+ this._defaultPaths[path](request, response);
+ else
+ throw HTTP_404;
+ }
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort(e);
+ return;
+ }
+
+ var errorCode = "internal";
+
+ try
+ {
+ if (!(e instanceof HttpError))
+ throw e;
+
+ errorCode = e.code;
+ dumpn("*** errorCode == " + errorCode);
+
+ response = new Response(connection);
+ if (e.customErrorHandling)
+ e.customErrorHandling(response);
+ this._handleError(errorCode, request, response);
+ return;
+ }
+ catch (e2)
+ {
+ dumpn("*** error handling " + errorCode + " error: " +
+ "e2 == " + e2 + ", shutting down server");
+
+ connection.server._requestQuit();
+ response.abort(e2);
+ return;
+ }
+ }
+
+ response.complete();
+ },
+
+ //
+ // see nsIHttpServer.registerFile
+ //
+ registerFile: function(path, file)
+ {
+ if (!file)
+ {
+ dumpn("*** unregistering '" + path + "' mapping");
+ delete this._overridePaths[path];
+ return;
+ }
+
+ dumpn("*** registering '" + path + "' as mapping to " + file.path);
+ file = file.clone();
+
+ var self = this;
+ this._overridePaths[path] =
+ function(request, response)
+ {
+ if (!file.exists())
+ throw HTTP_404;
+
+ response.setStatusLine(request.httpVersion, 200, "OK");
+ self._writeFileResponse(request, file, response, 0, file.fileSize);
+ };
+ },
+
+ //
+ // see nsIHttpServer.registerPathHandler
+ //
+ registerPathHandler: function(path, handler)
+ {
+ // XXX true path validation!
+ if (path.charAt(0) != "/")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ this._handlerToField(handler, this._overridePaths, path);
+ },
+
+ //
+ // see nsIHttpServer.registerPrefixHandler
+ //
+ registerPrefixHandler: function(prefix, handler)
+ {
+ // XXX true prefix validation!
+ if (!(prefix.startsWith("/") && prefix.endsWith("/")))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ this._handlerToField(handler, this._overridePrefixes, prefix);
+ },
+
+ //
+ // see nsIHttpServer.registerDirectory
+ //
+ registerDirectory: function(path, directory)
+ {
+ // strip off leading and trailing '/' so that we can use lastIndexOf when
+ // determining exactly how a path maps onto a mapped directory --
+ // conditional is required here to deal with "/".substring(1, 0) being
+ // converted to "/".substring(0, 1) per the JS specification
+ var key = path.length == 1 ? "" : path.substring(1, path.length - 1);
+
+ // the path-to-directory mapping code requires that the first character not
+ // be "/", or it will go into an infinite loop
+ if (key.charAt(0) == "/")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ key = toInternalPath(key, false);
+
+ if (directory)
+ {
+ dumpn("*** mapping '" + path + "' to the location " + directory.path);
+ this._pathDirectoryMap.put(key, directory);
+ }
+ else
+ {
+ dumpn("*** removing mapping for '" + path + "'");
+ this._pathDirectoryMap.put(key, null);
+ }
+ },
+
+ //
+ // see nsIHttpServer.registerErrorHandler
+ //
+ registerErrorHandler: function(err, handler)
+ {
+ if (!(err in HTTP_ERROR_CODES))
+ dumpn("*** WARNING: registering non-HTTP/1.1 error code " +
+ "(" + err + ") handler -- was this intentional?");
+
+ this._handlerToField(handler, this._overrideErrors, err);
+ },
+
+ //
+ // see nsIHttpServer.setIndexHandler
+ //
+ setIndexHandler: function(handler)
+ {
+ if (!handler)
+ handler = defaultIndexHandler;
+ else if (typeof(handler) != "function")
+ handler = createHandlerFunc(handler);
+
+ this._indexHandler = handler;
+ },
+
+ //
+ // see nsIHttpServer.registerContentType
+ //
+ registerContentType: function(ext, type)
+ {
+ if (!type)
+ delete this._mimeMappings[ext];
+ else
+ this._mimeMappings[ext] = headerUtils.normalizeFieldValue(type);
+ },
+
+ // PRIVATE API
+
+ /**
+* Sets or remove (if handler is null) a handler in an object with a key.
+*
+* @param handler
+* a handler, either function or an nsIHttpRequestHandler
+* @param dict
+* The object to attach the handler to.
+* @param key
+* The field name of the handler.
+*/
+ _handlerToField: function(handler, dict, key)
+ {
+ // for convenience, handler can be a function if this is run from xpcshell
+ if (typeof(handler) == "function")
+ dict[key] = handler;
+ else if (handler)
+ dict[key] = createHandlerFunc(handler);
+ else
+ delete dict[key];
+ },
+
+ /**
+* Handles a request which maps to a file in the local filesystem (if a base
+* path has already been set; otherwise the 404 error is thrown).
+*
+* @param metadata : Request
+* metadata for the incoming request
+* @param response : Response
+* an uninitialized Response to the given request, to be initialized by a
+* request handler
+* @throws HTTP_###
+* if an HTTP error occurred (usually HTTP_404); note that in this case the
+* calling code must handle post-processing of the response
+*/
+ _handleDefault: function(metadata, response)
+ {
+ dumpn("*** _handleDefault()");
+
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+
+ var path = metadata.path;
+ NS_ASSERT(path.charAt(0) == "/", "invalid path: <" + path + ">");
+
+ // determine the actual on-disk file; this requires finding the deepest
+ // path-to-directory mapping in the requested URL
+ var file = this._getFileForPath(path);
+
+ // the "file" might be a directory, in which case we either serve the
+ // contained index.html or make the index handler write the response
+ if (file.exists() && file.isDirectory())
+ {
+ file.append("index.html"); // make configurable?
+ if (!file.exists() || file.isDirectory())
+ {
+ metadata._ensurePropertyBag();
+ metadata._bag.setPropertyAsInterface("directory", file.parent);
+ this._indexHandler(metadata, response);
+ return;
+ }
+ }
+
+ // alternately, the file might not exist
+ if (!file.exists())
+ throw HTTP_404;
+
+ var start, end;
+ if (metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1) &&
+ metadata.hasHeader("Range") &&
+ this._getTypeFromFile(file) !== SJS_TYPE)
+ {
+ var rangeMatch = metadata.getHeader("Range").match(/^bytes=(\d+)?-(\d+)?$/);
+ if (!rangeMatch)
+ throw HTTP_400;
+
+ if (rangeMatch[1] !== undefined)
+ start = parseInt(rangeMatch[1], 10);
+
+ if (rangeMatch[2] !== undefined)
+ end = parseInt(rangeMatch[2], 10);
+
+ if (start === undefined && end === undefined)
+ throw HTTP_400;
+
+ // No start given, so the end is really the count of bytes from the
+ // end of the file.
+ if (start === undefined)
+ {
+ start = Math.max(0, file.fileSize - end);
+ end = file.fileSize - 1;
+ }
+
+ // start and end are inclusive
+ if (end === undefined || end >= file.fileSize)
+ end = file.fileSize - 1;
+
+ if (start !== undefined && start >= file.fileSize) {
+ var HTTP_416 = new HttpError(416, "Requested Range Not Satisfiable");
+ HTTP_416.customErrorHandling = function(errorResponse)
+ {
+ maybeAddHeaders(file, metadata, errorResponse);
+ };
+ throw HTTP_416;
+ }
+
+ if (end < start)
+ {
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ start = 0;
+ end = file.fileSize - 1;
+ }
+ else
+ {
+ response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
+ var contentRange = "bytes " + start + "-" + end + "/" + file.fileSize;
+ response.setHeader("Content-Range", contentRange);
+ }
+ }
+ else
+ {
+ start = 0;
+ end = file.fileSize - 1;
+ }
+
+ // finally...
+ dumpn("*** handling '" + path + "' as mapping to " + file.path + " from " +
+ start + " to " + end + " inclusive");
+ this._writeFileResponse(metadata, file, response, start, end - start + 1);
+ },
+
+ /**
+* Writes an HTTP response for the given file, including setting headers for
+* file metadata.
+*
+* @param metadata : Request
+* the Request for which a response is being generated
+* @param file : nsILocalFile
+* the file which is to be sent in the response
+* @param response : Response
+* the response to which the file should be written
+* @param offset: uint
+* the byte offset to skip to when writing
+* @param count: uint
+* the number of bytes to write
+*/
+ _writeFileResponse: function(metadata, file, response, offset, count)
+ {
+ const PR_RDONLY = 0x01;
+
+ var type = this._getTypeFromFile(file);
+ if (type === SJS_TYPE)
+ {
+ var fis = new FileInputStream(file, PR_RDONLY, 0o444,
+ Ci.nsIFileInputStream.CLOSE_ON_EOF);
+
+ try
+ {
+ var sis = new ScriptableInputStream(fis);
+ var s = Cu.Sandbox(gGlobalObject);
+ s.importFunction(dump, "dump");
+
+ // Define a basic key-value state-preservation API across requests, with
+ // keys initially corresponding to the empty string.
+ var self = this;
+ var path = metadata.path;
+ s.importFunction(function getState(k)
+ {
+ return self._getState(path, k);
+ });
+ s.importFunction(function setState(k, v)
+ {
+ self._setState(path, k, v);
+ });
+ s.importFunction(function getSharedState(k)
+ {
+ return self._getSharedState(k);
+ });
+ s.importFunction(function setSharedState(k, v)
+ {
+ self._setSharedState(k, v);
+ });
+ s.importFunction(function getObjectState(k, callback)
+ {
+ callback(self._getObjectState(k));
+ });
+ s.importFunction(function setObjectState(k, v)
+ {
+ self._setObjectState(k, v);
+ });
+ s.importFunction(function registerPathHandler(p, h)
+ {
+ self.registerPathHandler(p, h);
+ });
+
+ // Make it possible for sjs files to access their location
+ this._setState(path, "__LOCATION__", file.path);
+
+ try
+ {
+ // Alas, the line number in errors dumped to console when calling the
+ // request handler is simply an offset from where we load the SJS file.
+ // Work around this in a reasonably non-fragile way by dynamically
+ // getting the line number where we evaluate the SJS file. Don't
+ // separate these two lines!
+ var line = new Error().lineNumber;
+ Cu.evalInSandbox(sis.read(file.fileSize), s);
+ }
+ catch (e)
+ {
+ dumpn("*** syntax error in SJS at " + file.path + ": " + e);
+ throw HTTP_500;
+ }
+
+ try
+ {
+ s.handleRequest(metadata, response);
+ }
+ catch (e)
+ {
+ dump("*** error running SJS at " + file.path + ": " +
+ e + " on line " +
+ (e instanceof Error
+ ? e.lineNumber + " in httpd.js"
+ : (e.lineNumber - line)) + "\n");
+ throw HTTP_500;
+ }
+ }
+ finally
+ {
+ fis.close();
+ }
+ }
+ else
+ {
+ try
+ {
+ response.setHeader("Last-Modified",
+ toDateString(file.lastModifiedTime),
+ false);
+ }
+ catch (e) { /* lastModifiedTime threw, ignore */ }
+
+ response.setHeader("Content-Type", type, false);
+ maybeAddHeaders(file, metadata, response);
+ response.setHeader("Content-Length", "" + count, false);
+
+ var fis = new FileInputStream(file, PR_RDONLY, 0o444,
+ Ci.nsIFileInputStream.CLOSE_ON_EOF);
+
+ offset = offset || 0;
+ count = count || file.fileSize;
+ NS_ASSERT(offset === 0 || offset < file.fileSize, "bad offset");
+ NS_ASSERT(count >= 0, "bad count");
+ NS_ASSERT(offset + count <= file.fileSize, "bad total data size");
+
+ try
+ {
+ if (offset !== 0)
+ {
+ // Seek (or read, if seeking isn't supported) to the correct offset so
+ // the data sent to the client matches the requested range.
+ if (fis instanceof Ci.nsISeekableStream)
+ fis.seek(Ci.nsISeekableStream.NS_SEEK_SET, offset);
+ else
+ new ScriptableInputStream(fis).read(offset);
+ }
+ }
+ catch (e)
+ {
+ fis.close();
+ throw e;
+ }
+
+ let writeMore = function writeMore()
+ {
+ gThreadManager.currentThread
+ .dispatch(writeData, Ci.nsIThread.DISPATCH_NORMAL);
+ }
+
+ var input = new BinaryInputStream(fis);
+ var output = new BinaryOutputStream(response.bodyOutputStream);
+ var writeData =
+ {
+ run: function()
+ {
+ var chunkSize = Math.min(65536, count);
+ count -= chunkSize;
+ NS_ASSERT(count >= 0, "underflow");
+
+ try
+ {
+ var data = input.readByteArray(chunkSize);
+ NS_ASSERT(data.length === chunkSize,
+ "incorrect data returned? got " + data.length +
+ ", expected " + chunkSize);
+ output.writeByteArray(data, data.length);
+ if (count === 0)
+ {
+ fis.close();
+ response.finish();
+ }
+ else
+ {
+ writeMore();
+ }
+ }
+ catch (e)
+ {
+ try
+ {
+ fis.close();
+ }
+ finally
+ {
+ response.finish();
+ }
+ throw e;
+ }
+ }
+ };
+
+ writeMore();
+
+ // Now that we know copying will start, flag the response as async.
+ response.processAsync();
+ }
+ },
+
+ /**
+* Get the value corresponding to a given key for the given path for SJS state
+* preservation across requests.
+*
+* @param path : string
+* the path from which the given state is to be retrieved
+* @param k : string
+* the key whose corresponding value is to be returned
+* @returns string
+* the corresponding value, which is initially the empty string
+*/
+ _getState: function(path, k)
+ {
+ var state = this._state;
+ if (path in state && k in state[path])
+ return state[path][k];
+ return "";
+ },
+
+ /**
+* Set the value corresponding to a given key for the given path for SJS state
+* preservation across requests.
+*
+* @param path : string
+* the path from which the given state is to be retrieved
+* @param k : string
+* the key whose corresponding value is to be set
+* @param v : string
+* the value to be set
+*/
+ _setState: function(path, k, v)
+ {
+ if (typeof v !== "string")
+ throw new Error("non-string value passed");
+ var state = this._state;
+ if (!(path in state))
+ state[path] = {};
+ state[path][k] = v;
+ },
+
+ /**
+* Get the value corresponding to a given key for SJS state preservation
+* across requests.
+*
+* @param k : string
+* the key whose corresponding value is to be returned
+* @returns string
+* the corresponding value, which is initially the empty string
+*/
+ _getSharedState: function(k)
+ {
+ var state = this._sharedState;
+ if (k in state)
+ return state[k];
+ return "";
+ },
+
+ /**
+* Set the value corresponding to a given key for SJS state preservation
+* across requests.
+*
+* @param k : string
+* the key whose corresponding value is to be set
+* @param v : string
+* the value to be set
+*/
+ _setSharedState: function(k, v)
+ {
+ if (typeof v !== "string")
+ throw new Error("non-string value passed");
+ this._sharedState[k] = v;
+ },
+
+ /**
+* Returns the object associated with the given key in the server for SJS
+* state preservation across requests.
+*
+* @param k : string
+* the key whose corresponding object is to be returned
+* @returns nsISupports
+* the corresponding object, or null if none was present
+*/
+ _getObjectState: function(k)
+ {
+ if (typeof k !== "string")
+ throw new Error("non-string key passed");
+ return this._objectState[k] || null;
+ },
+
+ /**
+* Sets the object associated with the given key in the server for SJS
+* state preservation across requests.
+*
+* @param k : string
+* the key whose corresponding object is to be set
+* @param v : nsISupports
+* the object to be associated with the given key; may be null
+*/
+ _setObjectState: function(k, v)
+ {
+ if (typeof k !== "string")
+ throw new Error("non-string key passed");
+ if (typeof v !== "object")
+ throw new Error("non-object value passed");
+ if (v && !("QueryInterface" in v))
+ {
+ throw new Error("must pass an nsISupports; use wrappedJSObject to ease " +
+ "pain when using the server from JS");
+ }
+
+ this._objectState[k] = v;
+ },
+
+ /**
+* Gets a content-type for the given file, first by checking for any custom
+* MIME-types registered with this handler for the file's extension, second by
+* asking the global MIME service for a content-type, and finally by failing
+* over to application/octet-stream.
+*
+* @param file : nsIFile
+* the nsIFile for which to get a file type
+* @returns string
+* the best content-type which can be determined for the file
+*/
+ _getTypeFromFile: function(file)
+ {
+ try
+ {
+ var name = file.leafName;
+ var dot = name.lastIndexOf(".");
+ if (dot > 0)
+ {
+ var ext = name.slice(dot + 1);
+ if (ext in this._mimeMappings)
+ return this._mimeMappings[ext];
+ }
+ return Cc["@mozilla.org/uriloader/external-helper-app-service;1"]
+ .getService(Ci.nsIMIMEService)
+ .getTypeFromFile(file);
+ }
+ catch (e)
+ {
+ return "application/octet-stream";
+ }
+ },
+
+ /**
+* Returns the nsILocalFile which corresponds to the path, as determined using
+* all registered path->directory mappings and any paths which are explicitly
+* overridden.
+*
+* @param path : string
+* the server path for which a file should be retrieved, e.g. "/foo/bar"
+* @throws HttpError
+* when the correct action is the corresponding HTTP error (i.e., because no
+* mapping was found for a directory in path, the referenced file doesn't
+* exist, etc.)
+* @returns nsILocalFile
+* the file to be sent as the response to a request for the path
+*/
+ _getFileForPath: function(path)
+ {
+ // decode and add underscores as necessary
+ try
+ {
+ path = toInternalPath(path, true);
+ }
+ catch (e)
+ {
+ throw HTTP_400; // malformed path
+ }
+
+ // next, get the directory which contains this path
+ var pathMap = this._pathDirectoryMap;
+
+ // An example progression of tmp for a path "/foo/bar/baz/" might be:
+ // "foo/bar/baz/", "foo/bar/baz", "foo/bar", "foo", ""
+ var tmp = path.substring(1);
+ while (true)
+ {
+ // do we have a match for current head of the path?
+ var file = pathMap.get(tmp);
+ if (file)
+ {
+ // XXX hack; basically disable showing mapping for /foo/bar/ when the
+ // requested path was /foo/bar, because relative links on the page
+ // will all be incorrect -- we really need the ability to easily
+ // redirect here instead
+ if (tmp == path.substring(1) &&
+ tmp.length != 0 &&
+ tmp.charAt(tmp.length - 1) != "/")
+ file = null;
+ else
+ break;
+ }
+
+ // if we've finished trying all prefixes, exit
+ if (tmp == "")
+ break;
+
+ tmp = tmp.substring(0, tmp.lastIndexOf("/"));
+ }
+
+ // no mapping applies, so 404
+ if (!file)
+ throw HTTP_404;
+
+
+ // last, get the file for the path within the determined directory
+ var parentFolder = file.parent;
+ var dirIsRoot = (parentFolder == null);
+
+ // Strategy here is to append components individually, making sure we
+ // never move above the given directory; this allows paths such as
+ // "<file>/foo/../bar" but prevents paths such as "<file>/../base-sibling";
+ // this component-wise approach also means the code works even on platforms
+ // which don't use "/" as the directory separator, such as Windows
+ var leafPath = path.substring(tmp.length + 1);
+ var comps = leafPath.split("/");
+ for (var i = 0, sz = comps.length; i < sz; i++)
+ {
+ var comp = comps[i];
+
+ if (comp == "..")
+ file = file.parent;
+ else if (comp == "." || comp == "")
+ continue;
+ else
+ file.append(comp);
+
+ if (!dirIsRoot && file.equals(parentFolder))
+ throw HTTP_403;
+ }
+
+ return file;
+ },
+
+ /**
+* Writes the error page for the given HTTP error code over the given
+* connection.
+*
+* @param errorCode : uint
+* the HTTP error code to be used
+* @param connection : Connection
+* the connection on which the error occurred
+*/
+ handleError: function(errorCode, connection)
+ {
+ var response = new Response(connection);
+
+ dumpn("*** error in request: " + errorCode);
+
+ this._handleError(errorCode, new Request(connection.port), response);
+ },
+
+ /**
+* Handles a request which generates the given error code, using the
+* user-defined error handler if one has been set, gracefully falling back to
+* the x00 status code if the code has no handler, and failing to status code
+* 500 if all else fails.
+*
+* @param errorCode : uint
+* the HTTP error which is to be returned
+* @param metadata : Request
+* metadata for the request, which will often be incomplete since this is an
+* error
+* @param response : Response
+* an uninitialized Response should be initialized when this method
+* completes with information which represents the desired error code in the
+* ideal case or a fallback code in abnormal circumstances (i.e., 500 is a
+* fallback for 505, per HTTP specs)
+*/
+ _handleError: function(errorCode, metadata, response)
+ {
+ if (!metadata)
+ throw Cr.NS_ERROR_NULL_POINTER;
+
+ var errorX00 = errorCode - (errorCode % 100);
+
+ try
+ {
+ if (!(errorCode in HTTP_ERROR_CODES))
+ dumpn("*** WARNING: requested invalid error: " + errorCode);
+
+ // RFC 2616 says that we should try to handle an error by its class if we
+ // can't otherwise handle it -- if that fails, we revert to handling it as
+ // a 500 internal server error, and if that fails we throw and shut down
+ // the server
+
+ // actually handle the error
+ try
+ {
+ if (errorCode in this._overrideErrors)
+ this._overrideErrors[errorCode](metadata, response);
+ else
+ this._defaultErrors[errorCode](metadata, response);
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort(e);
+ return;
+ }
+
+ // don't retry the handler that threw
+ if (errorX00 == errorCode)
+ throw HTTP_500;
+
+ dumpn("*** error in handling for error code " + errorCode + ", " +
+ "falling back to " + errorX00 + "...");
+ response = new Response(response._connection);
+ if (errorX00 in this._overrideErrors)
+ this._overrideErrors[errorX00](metadata, response);
+ else if (errorX00 in this._defaultErrors)
+ this._defaultErrors[errorX00](metadata, response);
+ else
+ throw HTTP_500;
+ }
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort();
+ return;
+ }
+
+ // we've tried everything possible for a meaningful error -- now try 500
+ dumpn("*** error in handling for error code " + errorX00 + ", falling " +
+ "back to 500...");
+
+ try
+ {
+ response = new Response(response._connection);
+ if (500 in this._overrideErrors)
+ this._overrideErrors[500](metadata, response);
+ else
+ this._defaultErrors[500](metadata, response);
+ }
+ catch (e2)
+ {
+ dumpn("*** multiple errors in default error handlers!");
+ dumpn("*** e == " + e + ", e2 == " + e2);
+ response.abort(e2);
+ return;
+ }
+ }
+
+ response.complete();
+ },
+
+ // FIELDS
+
+ /**
+* This object contains the default handlers for the various HTTP error codes.
+*/
+ _defaultErrors:
+ {
+ 400: function(metadata, response)
+ {
+ // none of the data in metadata is reliable, so hard-code everything here
+ response.setStatusLine("1.1", 400, "Bad Request");
+ response.setHeader("Content-Type", "text/plain", false);
+
+ var body = "Bad request\n";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 403: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 403, "Forbidden");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>403 Forbidden</title></head>\
+<body>\
+<h1>403 Forbidden</h1>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 404: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 404, "Not Found");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>404 Not Found</title></head>\
+<body>\
+<h1>404 Not Found</h1>\
+<p>\
+<span style='font-family: monospace;'>" +
+ htmlEscape(metadata.path) +
+ "</span> was not found.\
+</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 416: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion,
+ 416,
+ "Requested Range Not Satisfiable");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head>\
+<title>416 Requested Range Not Satisfiable</title></head>\
+<body>\
+<h1>416 Requested Range Not Satisfiable</h1>\
+<p>The byte range was not valid for the\
+requested resource.\
+</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 500: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion,
+ 500,
+ "Internal Server Error");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>500 Internal Server Error</title></head>\
+<body>\
+<h1>500 Internal Server Error</h1>\
+<p>Something's broken in this server and\
+needs to be fixed.</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 501: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 501, "Not Implemented");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>501 Not Implemented</title></head>\
+<body>\
+<h1>501 Not Implemented</h1>\
+<p>This server is not (yet) Apache.</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 505: function(metadata, response)
+ {
+ response.setStatusLine("1.1", 505, "HTTP Version Not Supported");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>505 HTTP Version Not Supported</title></head>\
+<body>\
+<h1>505 HTTP Version Not Supported</h1>\
+<p>This server only supports HTTP/1.0 and HTTP/1.1\
+connections.</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ }
+ },
+
+ /**
+* Contains handlers for the default set of URIs contained in this server.
+*/
+ _defaultPaths:
+ {
+ "/": function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>httpd.js</title></head>\
+<body>\
+<h1>httpd.js</h1>\
+<p>If you're seeing this page, httpd.js is up and\
+serving requests! Now set a base path and serve some\
+files!</p>\
+</body>\
+</html>";
+
+ response.bodyOutputStream.write(body, body.length);
+ },
+
+ "/trace": function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ response.setHeader("Content-Type", "text/plain", false);
+
+ var body = "Request-URI: " +
+ metadata.scheme + "://" + metadata.host + ":" + metadata.port +
+ metadata.path + "\n\n";
+ body += "Request (semantically equivalent, slightly reformatted):\n\n";
+ body += metadata.method + " " + metadata.path;
+
+ if (metadata.queryString)
+ body += "?" + metadata.queryString;
+
+ body += " HTTP/" + metadata.httpVersion + "\r\n";
+
+ var headEnum = metadata.headers;
+ while (headEnum.hasMoreElements())
+ {
+ var fieldName = headEnum.getNext()
+ .QueryInterface(Ci.nsISupportsString)
+ .data;
+ body += fieldName + ": " + metadata.getHeader(fieldName) + "\r\n";
+ }
+
+ response.bodyOutputStream.write(body, body.length);
+ }
+ }
+};
+
+
+/**
+* Maps absolute paths to files on the local file system (as nsILocalFiles).
+*/
+function FileMap()
+{
+ /** Hash which will map paths to nsILocalFiles. */
+ this._map = {};
+}
+FileMap.prototype =
+{
+ // PUBLIC API
+
+ /**
+* Maps key to a clone of the nsILocalFile value if value is non-null;
+* otherwise, removes any extant mapping for key.
+*
+* @param key : string
+* string to which a clone of value is mapped
+* @param value : nsILocalFile
+* the file to map to key, or null to remove a mapping
+*/
+ put: function(key, value)
+ {
+ if (value)
+ this._map[key] = value.clone();
+ else
+ delete this._map[key];
+ },
+
+ /**
+* Returns a clone of the nsILocalFile mapped to key, or null if no such
+* mapping exists.
+*
+* @param key : string
+* key to which the returned file maps
+* @returns nsILocalFile
+* a clone of the mapped file, or null if no mapping exists
+*/
+ get: function(key)
+ {
+ var val = this._map[key];
+ return val ? val.clone() : null;
+ }
+};
+
+
+// Response CONSTANTS
+
+// token = *<any CHAR except CTLs or separators>
+// CHAR = <any US-ASCII character (0-127)>
+// CTL = <any US-ASCII control character (0-31) and DEL (127)>
+// separators = "(" | ")" | "<" | ">" | "@"
+// | "," | ";" | ":" | "\" | <">
+// | "/" | "[" | "]" | "?" | "="
+// | "{" | "}" | SP | HT
+const IS_TOKEN_ARRAY =
+ [0, 0, 0, 0, 0, 0, 0, 0, // 0
+ 0, 0, 0, 0, 0, 0, 0, 0, // 8
+ 0, 0, 0, 0, 0, 0, 0, 0, // 16
+ 0, 0, 0, 0, 0, 0, 0, 0, // 24
+
+ 0, 1, 0, 1, 1, 1, 1, 1, // 32
+ 0, 0, 1, 1, 0, 1, 1, 0, // 40
+ 1, 1, 1, 1, 1, 1, 1, 1, // 48
+ 1, 1, 0, 0, 0, 0, 0, 0, // 56
+
+ 0, 1, 1, 1, 1, 1, 1, 1, // 64
+ 1, 1, 1, 1, 1, 1, 1, 1, // 72
+ 1, 1, 1, 1, 1, 1, 1, 1, // 80
+ 1, 1, 1, 0, 0, 0, 1, 1, // 88
+
+ 1, 1, 1, 1, 1, 1, 1, 1, // 96
+ 1, 1, 1, 1, 1, 1, 1, 1, // 104
+ 1, 1, 1, 1, 1, 1, 1, 1, // 112
+ 1, 1, 1, 0, 1, 0, 1]; // 120
+
+
+/**
+* Determines whether the given character code is a CTL.
+*
+* @param code : uint
+* the character code
+* @returns boolean
+* true if code is a CTL, false otherwise
+*/
+function isCTL(code)
+{
+ return (code >= 0 && code <= 31) || (code == 127);
+}
+
+/**
+* Represents a response to an HTTP request, encapsulating all details of that
+* response. This includes all headers, the HTTP version, status code and
+* explanation, and the entity itself.
+*
+* @param connection : Connection
+* the connection over which this response is to be written
+*/
+function Response(connection)
+{
+ /** The connection over which this response will be written. */
+ this._connection = connection;
+
+ /**
+* The HTTP version of this response; defaults to 1.1 if not set by the
+* handler.
+*/
+ this._httpVersion = nsHttpVersion.HTTP_1_1;
+
+ /**
+* The HTTP code of this response; defaults to 200.
+*/
+ this._httpCode = 200;
+
+ /**
+* The description of the HTTP code in this response; defaults to "OK".
+*/
+ this._httpDescription = "OK";
+
+ /**
+* An nsIHttpHeaders object in which the headers in this response should be
+* stored. This property is null after the status line and headers have been
+* written to the network, and it may be modified up until it is cleared,
+* except if this._finished is set first (in which case headers are written
+* asynchronously in response to a finish() call not preceded by
+* flushHeaders()).
+*/
+ this._headers = new nsHttpHeaders();
+
+ /**
+* Set to true when this response is ended (completely constructed if possible
+* and the connection closed); further actions on this will then fail.
+*/
+ this._ended = false;
+
+ /**
+* A stream used to hold data written to the body of this response.
+*/
+ this._bodyOutputStream = null;
+
+ /**
+* A stream containing all data that has been written to the body of this
+* response so far. (Async handlers make the data contained in this
+* unreliable as a way of determining content length in general, but auxiliary
+* saved information can sometimes be used to guarantee reliability.)
+*/
+ this._bodyInputStream = null;
+
+ /**
+* A stream copier which copies data to the network. It is initially null
+* until replaced with a copier for response headers; when headers have been
+* fully sent it is replaced with a copier for the response body, remaining
+* so for the duration of response processing.
+*/
+ this._asyncCopier = null;
+
+ /**
+* True if this response has been designated as being processed
+* asynchronously rather than for the duration of a single call to
+* nsIHttpRequestHandler.handle.
+*/
+ this._processAsync = false;
+
+ /**
+* True iff finish() has been called on this, signaling that no more changes
+* to this may be made.
+*/
+ this._finished = false;
+
+ /**
+* True iff powerSeized() has been called on this, signaling that this
+* response is to be handled manually by the response handler (which may then
+* send arbitrary data in response, even non-HTTP responses).
+*/
+ this._powerSeized = false;
+}
+Response.prototype =
+{
+ // PUBLIC CONSTRUCTION API
+
+ //
+ // see nsIHttpResponse.bodyOutputStream
+ //
+ get bodyOutputStream()
+ {
+ if (this._finished)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+
+ if (!this._bodyOutputStream)
+ {
+ var pipe = new Pipe(true, false, Response.SEGMENT_SIZE, PR_UINT32_MAX,
+ null);
+ this._bodyOutputStream = pipe.outputStream;
+ this._bodyInputStream = pipe.inputStream;
+ if (this._processAsync || this._powerSeized)
+ this._startAsyncProcessor();
+ }
+
+ return this._bodyOutputStream;
+ },
+
+ //
+ // see nsIHttpResponse.write
+ //
+ write: function(data)
+ {
+ if (this._finished)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+
+ var dataAsString = String(data);
+ this.bodyOutputStream.write(dataAsString, dataAsString.length);
+ },
+
+ //
+ // see nsIHttpResponse.setStatusLine
+ //
+ setStatusLine: function(httpVersion, code, description)
+ {
+ if (!this._headers || this._finished || this._powerSeized)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ this._ensureAlive();
+
+ if (!(code >= 0 && code < 1000))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ try
+ {
+ var httpVer;
+ // avoid version construction for the most common cases
+ if (!httpVersion || httpVersion == "1.1")
+ httpVer = nsHttpVersion.HTTP_1_1;
+ else if (httpVersion == "1.0")
+ httpVer = nsHttpVersion.HTTP_1_0;
+ else
+ httpVer = new nsHttpVersion(httpVersion);
+ }
+ catch (e)
+ {
+ throw Cr.NS_ERROR_INVALID_ARG;
+ }
+
+ // Reason-Phrase = *<TEXT, excluding CR, LF>
+ // TEXT = <any OCTET except CTLs, but including LWS>
+ //
+ // XXX this ends up disallowing octets which aren't Unicode, I think -- not
+ // much to do if description is IDL'd as string
+ if (!description)
+ description = "";
+ for (var i = 0; i < description.length; i++)
+ if (isCTL(description.charCodeAt(i)) && description.charAt(i) != "\t")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ // set the values only after validation to preserve atomicity
+ this._httpDescription = description;
+ this._httpCode = code;
+ this._httpVersion = httpVer;
+ },
+
+ //
+ // see nsIHttpResponse.setHeader
+ //
+ setHeader: function(name, value, merge)
+ {
+ if (!this._headers || this._finished || this._powerSeized)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ this._ensureAlive();
+
+ this._headers.setHeader(name, value, merge);
+ },
+
+ //
+ // see nsIHttpResponse.processAsync
+ //
+ processAsync: function()
+ {
+ if (this._finished)
+ throw Cr.NS_ERROR_UNEXPECTED;
+ if (this._powerSeized)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ if (this._processAsync)
+ return;
+ this._ensureAlive();
+
+ dumpn("*** processing connection " + this._connection.number + " async");
+ this._processAsync = true;
+
+ /*
+* Either the bodyOutputStream getter or this method is responsible for
+* starting the asynchronous processor and catching writes of data to the
+* response body of async responses as they happen, for the purpose of
+* forwarding those writes to the actual connection's output stream.
+* If bodyOutputStream is accessed first, calling this method will create
+* the processor (when it first is clear that body data is to be written
+* immediately, not buffered). If this method is called first, accessing
+* bodyOutputStream will create the processor. If only this method is
+* called, we'll write nothing, neither headers nor the nonexistent body,
+* until finish() is called. Since that delay is easily avoided by simply
+* getting bodyOutputStream or calling write(""), we don't worry about it.
+*/
+ if (this._bodyOutputStream && !this._asyncCopier)
+ this._startAsyncProcessor();
+ },
+
+ //
+ // see nsIHttpResponse.seizePower
+ //
+ seizePower: function()
+ {
+ if (this._processAsync)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ if (this._finished)
+ throw Cr.NS_ERROR_UNEXPECTED;
+ if (this._powerSeized)
+ return;
+ this._ensureAlive();
+
+ dumpn("*** forcefully seizing power over connection " +
+ this._connection.number + "...");
+
+ // Purge any already-written data without sending it. We could as easily
+ // swap out the streams entirely, but that makes it possible to acquire and
+ // unknowingly use a stale reference, so we require there only be one of
+ // each stream ever for any response to avoid this complication.
+ if (this._asyncCopier)
+ this._asyncCopier.cancel(Cr.NS_BINDING_ABORTED);
+ this._asyncCopier = null;
+ if (this._bodyOutputStream)
+ {
+ var input = new BinaryInputStream(this._bodyInputStream);
+ var avail;
+ while ((avail = input.available()) > 0)
+ input.readByteArray(avail);
+ }
+
+ this._powerSeized = true;
+ if (this._bodyOutputStream)
+ this._startAsyncProcessor();
+ },
+
+ //
+ // see nsIHttpResponse.finish
+ //
+ finish: function()
+ {
+ if (!this._processAsync && !this._powerSeized)
+ throw Cr.NS_ERROR_UNEXPECTED;
+ if (this._finished)
+ return;
+
+ dumpn("*** finishing connection " + this._connection.number);
+ this._startAsyncProcessor(); // in case bodyOutputStream was never accessed
+ if (this._bodyOutputStream)
+ this._bodyOutputStream.close();
+ this._finished = true;
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIHttpResponse) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // POST-CONSTRUCTION API (not exposed externally)
+
+ /**
+* The HTTP version number of this, as a string (e.g. "1.1").
+*/
+ get httpVersion()
+ {
+ this._ensureAlive();
+ return this._httpVersion.toString();
+ },
+
+ /**
+* The HTTP status code of this response, as a string of three characters per
+* RFC 2616.
+*/
+ get httpCode()
+ {
+ this._ensureAlive();
+
+ var codeString = (this._httpCode < 10 ? "0" : "") +
+ (this._httpCode < 100 ? "0" : "") +
+ this._httpCode;
+ return codeString;
+ },
+
+ /**
+* The description of the HTTP status code of this response, or "" if none is
+* set.
+*/
+ get httpDescription()
+ {
+ this._ensureAlive();
+
+ return this._httpDescription;
+ },
+
+ /**
+* The headers in this response, as an nsHttpHeaders object.
+*/
+ get headers()
+ {
+ this._ensureAlive();
+
+ return this._headers;
+ },
+
+ //
+ // see nsHttpHeaders.getHeader
+ //
+ getHeader: function(name)
+ {
+ this._ensureAlive();
+
+ return this._headers.getHeader(name);
+ },
+
+ /**
+* Determines whether this response may be abandoned in favor of a newly
+* constructed response. A response may be abandoned only if it is not being
+* sent asynchronously and if raw control over it has not been taken from the
+* server.
+*
+* @returns boolean
+* true iff no data has been written to the network
+*/
+ partiallySent: function()
+ {
+ dumpn("*** partiallySent()");
+ return this._processAsync || this._powerSeized;
+ },
+
+ /**
+* If necessary, kicks off the remaining request processing needed to be done
+* after a request handler performs its initial work upon this response.
+*/
+ complete: function()
+ {
+ dumpn("*** complete()");
+ if (this._processAsync || this._powerSeized)
+ {
+ NS_ASSERT(this._processAsync ^ this._powerSeized,
+ "can't both send async and relinquish power");
+ return;
+ }
+
+ NS_ASSERT(!this.partiallySent(), "completing a partially-sent response?");
+
+ this._startAsyncProcessor();
+
+ // Now make sure we finish processing this request!
+ if (this._bodyOutputStream)
+ this._bodyOutputStream.close();
+ },
+
+ /**
+* Abruptly ends processing of this response, usually due to an error in an
+* incoming request but potentially due to a bad error handler. Since we
+* cannot handle the error in the usual way (giving an HTTP error page in
+* response) because data may already have been sent (or because the response
+* might be expected to have been generated asynchronously or completely from
+* scratch by the handler), we stop processing this response and abruptly
+* close the connection.
+*
+* @param e : Error
+* the exception which precipitated this abort, or null if no such exception
+* was generated
+*/
+ abort: function(e)
+ {
+ dumpn("*** abort(<" + e + ">)");
+
+ // This response will be ended by the processor if one was created.
+ var copier = this._asyncCopier;
+ if (copier)
+ {
+ // We dispatch asynchronously here so that any pending writes of data to
+ // the connection will be deterministically written. This makes it easier
+ // to specify exact behavior, and it makes observable behavior more
+ // predictable for clients. Note that the correctness of this depends on
+ // callbacks in response to _waitToReadData in WriteThroughCopier
+ // happening asynchronously with respect to the actual writing of data to
+ // bodyOutputStream, as they currently do; if they happened synchronously,
+ // an event which ran before this one could write more data to the
+ // response body before we get around to canceling the copier. We have
+ // tests for this in test_seizepower.js, however, and I can't think of a
+ // way to handle both cases without removing bodyOutputStream access and
+ // moving its effective write(data, length) method onto Response, which
+ // would be slower and require more code than this anyway.
+ gThreadManager.currentThread.dispatch({
+ run: function()
+ {
+ dumpn("*** canceling copy asynchronously...");
+ copier.cancel(Cr.NS_ERROR_UNEXPECTED);
+ }
+ }, Ci.nsIThread.DISPATCH_NORMAL);
+ }
+ else
+ {
+ this.end();
+ }
+ },
+
+ /**
+* Closes this response's network connection, marks the response as finished,
+* and notifies the server handler that the request is done being processed.
+*/
+ end: function()
+ {
+ NS_ASSERT(!this._ended, "ending this response twice?!?!");
+
+ this._connection.close();
+ if (this._bodyOutputStream)
+ this._bodyOutputStream.close();
+
+ this._finished = true;
+ this._ended = true;
+ },
+
+ // PRIVATE IMPLEMENTATION
+
+ /**
+* Sends the status line and headers of this response if they haven't been
+* sent and initiates the process of copying data written to this response's
+* body to the network.
+*/
+ _startAsyncProcessor: function()
+ {
+ dumpn("*** _startAsyncProcessor()");
+
+ // Handle cases where we're being called a second time. The former case
+ // happens when this is triggered both by complete() and by processAsync(),
+ // while the latter happens when processAsync() in conjunction with sent
+ // data causes abort() to be called.
+ if (this._asyncCopier || this._ended)
+ {
+ dumpn("*** ignoring second call to _startAsyncProcessor");
+ return;
+ }
+
+ // Send headers if they haven't been sent already and should be sent, then
+ // asynchronously continue to send the body.
+ if (this._headers && !this._powerSeized)
+ {
+ this._sendHeaders();
+ return;
+ }
+
+ this._headers = null;
+ this._sendBody();
+ },
+
+ /**
+* Signals that all modifications to the response status line and headers are
+* complete and then sends that data over the network to the client. Once
+* this method completes, a different response to the request that resulted
+* in this response cannot be sent -- the only possible action in case of
+* error is to abort the response and close the connection.
+*/
+ _sendHeaders: function()
+ {
+ dumpn("*** _sendHeaders()");
+
+ NS_ASSERT(this._headers);
+ NS_ASSERT(!this._powerSeized);
+
+ // request-line
+ var statusLine = "HTTP/" + this.httpVersion + " " +
+ this.httpCode + " " +
+ this.httpDescription + "\r\n";
+
+ // header post-processing
+
+ var headers = this._headers;
+ headers.setHeader("Connection", "close", false);
+ headers.setHeader("Server", "httpd.js", false);
+ if (!headers.hasHeader("Date"))
+ headers.setHeader("Date", toDateString(Date.now()), false);
+
+ // Any response not being processed asynchronously must have an associated
+ // Content-Length header for reasons of backwards compatibility with the
+ // initial server, which fully buffered every response before sending it.
+ // Beyond that, however, it's good to do this anyway because otherwise it's
+ // impossible to test behaviors that depend on the presence or absence of a
+ // Content-Length header.
+ if (!this._processAsync)
+ {
+ dumpn("*** non-async response, set Content-Length");
+
+ var bodyStream = this._bodyInputStream;
+ var avail = bodyStream ? bodyStream.available() : 0;
+
+ // XXX assumes stream will always report the full amount of data available
+ headers.setHeader("Content-Length", "" + avail, false);
+ }
+
+
+ // construct and send response
+ dumpn("*** header post-processing completed, sending response head...");
+
+ // request-line
+ var preambleData = [statusLine];
+
+ // headers
+ var headEnum = headers.enumerator;
+ while (headEnum.hasMoreElements())
+ {
+ var fieldName = headEnum.getNext()
+ .QueryInterface(Ci.nsISupportsString)
+ .data;
+ var values = headers.getHeaderValues(fieldName);
+ for (var i = 0, sz = values.length; i < sz; i++)
+ preambleData.push(fieldName + ": " + values[i] + "\r\n");
+ }
+
+ // end request-line/headers
+ preambleData.push("\r\n");
+
+ var preamble = preambleData.join("");
+
+ var responseHeadPipe = new Pipe(true, false, 0, PR_UINT32_MAX, null);
+ responseHeadPipe.outputStream.write(preamble, preamble.length);
+
+ var response = this;
+ var copyObserver =
+ {
+ onStartRequest: function(request, cx)
+ {
+ dumpn("*** preamble copying started");
+ },
+
+ onStopRequest: function(request, cx, statusCode)
+ {
+ dumpn("*** preamble copying complete " +
+ "[status=0x" + statusCode.toString(16) + "]");
+
+ if (!components.isSuccessCode(statusCode))
+ {
+ dumpn("!!! header copying problems: non-success statusCode, " +
+ "ending response");
+
+ response.end();
+ }
+ else
+ {
+ response._sendBody();
+ }
+ },
+
+ QueryInterface: function(aIID)
+ {
+ if (aIID.equals(Ci.nsIRequestObserver) || aIID.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ }
+ };
+
+ var headerCopier = this._asyncCopier =
+ new WriteThroughCopier(responseHeadPipe.inputStream,
+ this._connection.output,
+ copyObserver, null);
+
+ responseHeadPipe.outputStream.close();
+
+ // Forbid setting any more headers or modifying the request line.
+ this._headers = null;
+ },
+
+ /**
+* Asynchronously writes the body of the response (or the entire response, if
+* seizePower() has been called) to the network.
+*/
+ _sendBody: function()
+ {
+ dumpn("*** _sendBody");
+
+ NS_ASSERT(!this._headers, "still have headers around but sending body?");
+
+ // If no body data was written, we're done
+ if (!this._bodyInputStream)
+ {
+ dumpn("*** empty body, response finished");
+ this.end();
+ return;
+ }
+
+ var response = this;
+ var copyObserver =
+ {
+ onStartRequest: function(request, context)
+ {
+ dumpn("*** onStartRequest");
+ },
+
+ onStopRequest: function(request, cx, statusCode)
+ {
+ dumpn("*** onStopRequest [status=0x" + statusCode.toString(16) + "]");
+
+ if (statusCode === Cr.NS_BINDING_ABORTED)
+ {
+ dumpn("*** terminating copy observer without ending the response");
+ }
+ else
+ {
+ if (!components.isSuccessCode(statusCode))
+ dumpn("*** WARNING: non-success statusCode in onStopRequest");
+
+ response.end();
+ }
+ },
+
+ QueryInterface: function(aIID)
+ {
+ if (aIID.equals(Ci.nsIRequestObserver) || aIID.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ }
+ };
+
+ dumpn("*** starting async copier of body data...");
+ this._asyncCopier =
+ new WriteThroughCopier(this._bodyInputStream, this._connection.output,
+ copyObserver, null);
+ },
+
+ /** Ensures that this hasn't been ended. */
+ _ensureAlive: function()
+ {
+ NS_ASSERT(!this._ended, "not handling response lifetime correctly");
+ }
+};
+
+/**
+* Size of the segments in the buffer used in storing response data and writing
+* it to the socket.
+*/
+Response.SEGMENT_SIZE = 8192;
+
+/** Serves double duty in WriteThroughCopier implementation. */
+function notImplemented()
+{
+ throw Cr.NS_ERROR_NOT_IMPLEMENTED;
+}
+
+/** Returns true iff the given exception represents stream closure. */
+function streamClosed(e)
+{
+ return e === Cr.NS_BASE_STREAM_CLOSED ||
+ (typeof e === "object" && e.result === Cr.NS_BASE_STREAM_CLOSED);
+}
+
+/** Returns true iff the given exception represents a blocked stream. */
+function wouldBlock(e)
+{
+ return e === Cr.NS_BASE_STREAM_WOULD_BLOCK ||
+ (typeof e === "object" && e.result === Cr.NS_BASE_STREAM_WOULD_BLOCK);
+}
+
+/**
+* Copies data from source to sink as it becomes available, when that data can
+* be written to sink without blocking.
+*
+* @param source : nsIAsyncInputStream
+* the stream from which data is to be read
+* @param sink : nsIAsyncOutputStream
+* the stream to which data is to be copied
+* @param observer : nsIRequestObserver
+* an observer which will be notified when the copy starts and finishes
+* @param context : nsISupports
+* context passed to observer when notified of start/stop
+* @throws NS_ERROR_NULL_POINTER
+* if source, sink, or observer are null
+*/
+function WriteThroughCopier(source, sink, observer, context)
+{
+ if (!source || !sink || !observer)
+ throw Cr.NS_ERROR_NULL_POINTER;
+
+ /** Stream from which data is being read. */
+ this._source = source;
+
+ /** Stream to which data is being written. */
+ this._sink = sink;
+
+ /** Observer watching this copy. */
+ this._observer = observer;
+
+ /** Context for the observer watching this. */
+ this._context = context;
+
+ /**
+* True iff this is currently being canceled (cancel has been called, the
+* callback may not yet have been made).
+*/
+ this._canceled = false;
+
+ /**
+* False until all data has been read from input and written to output, at
+* which point this copy is completed and cancel() is asynchronously called.
+*/
+ this._completed = false;
+
+ /** Required by nsIRequest, meaningless. */
+ this.loadFlags = 0;
+ /** Required by nsIRequest, meaningless. */
+ this.loadGroup = null;
+ /** Required by nsIRequest, meaningless. */
+ this.name = "response-body-copy";
+
+ /** Status of this request. */
+ this.status = Cr.NS_OK;
+
+ /** Arrays of byte strings waiting to be written to output. */
+ this._pendingData = [];
+
+ // start copying
+ try
+ {
+ observer.onStartRequest(this, context);
+ this._waitToReadData();
+ this._waitForSinkClosure();
+ }
+ catch (e)
+ {
+ dumpn("!!! error starting copy: " + e +
+ ("lineNumber" in e ? ", line " + e.lineNumber : ""));
+ dumpn(e.stack);
+ this.cancel(Cr.NS_ERROR_UNEXPECTED);
+ }
+}
+WriteThroughCopier.prototype =
+{
+ /* nsISupports implementation */
+
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIInputStreamCallback) ||
+ iid.equals(Ci.nsIOutputStreamCallback) ||
+ iid.equals(Ci.nsIRequest) ||
+ iid.equals(Ci.nsISupports))
+ {
+ return this;
+ }
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // NSIINPUTSTREAMCALLBACK
+
+ /**
+* Receives a more-data-in-input notification and writes the corresponding
+* data to the output.
+*
+* @param input : nsIAsyncInputStream
+* the input stream on whose data we have been waiting
+*/
+ onInputStreamReady: function(input)
+ {
+ if (this._source === null)
+ return;
+
+ dumpn("*** onInputStreamReady");
+
+ //
+ // Ordinarily we'll read a non-zero amount of data from input, queue it up
+ // to be written and then wait for further callbacks. The complications in
+ // this method are the cases where we deviate from that behavior when errors
+ // occur or when copying is drawing to a finish.
+ //
+ // The edge cases when reading data are:
+ //
+ // Zero data is read
+ // If zero data was read, we're at the end of available data, so we can
+ // should stop reading and move on to writing out what we have (or, if
+ // we've already done that, onto notifying of completion).
+ // A stream-closed exception is thrown
+ // This is effectively a less kind version of zero data being read; the
+ // only difference is that we notify of completion with that result
+ // rather than with NS_OK.
+ // Some other exception is thrown
+ // This is the least kind result. We don't know what happened, so we
+ // act as though the stream closed except that we notify of completion
+ // with the result NS_ERROR_UNEXPECTED.
+ //
+
+ var bytesWanted = 0, bytesConsumed = -1;
+ try
+ {
+ input = new BinaryInputStream(input);
+
+ bytesWanted = Math.min(input.available(), Response.SEGMENT_SIZE);
+ dumpn("*** input wanted: " + bytesWanted);
+
+ if (bytesWanted > 0)
+ {
+ var data = input.readByteArray(bytesWanted);
+ bytesConsumed = data.length;
+ this._pendingData.push(String.fromCharCode.apply(String, data));
+ }
+
+ dumpn("*** " + bytesConsumed + " bytes read");
+
+ // Handle the zero-data edge case in the same place as all other edge
+ // cases are handled.
+ if (bytesWanted === 0)
+ throw Cr.NS_BASE_STREAM_CLOSED;
+ }
+ catch (e)
+ {
+ if (streamClosed(e))
+ {
+ dumpn("*** input stream closed");
+ e = bytesWanted === 0 ? Cr.NS_OK : Cr.NS_ERROR_UNEXPECTED;
+ }
+ else
+ {
+ dumpn("!!! unexpected error reading from input, canceling: " + e);
+ e = Cr.NS_ERROR_UNEXPECTED;
+ }
+
+ this._doneReadingSource(e);
+ return;
+ }
+
+ var pendingData = this._pendingData;
+
+ NS_ASSERT(bytesConsumed > 0);
+ NS_ASSERT(pendingData.length > 0, "no pending data somehow?");
+ NS_ASSERT(pendingData[pendingData.length - 1].length > 0,
+ "buffered zero bytes of data?");
+
+ NS_ASSERT(this._source !== null);
+
+ // Reading has gone great, and we've gotten data to write now. What if we
+ // don't have a place to write that data, because output went away just
+ // before this read? Drop everything on the floor, including new data, and
+ // cancel at this point.
+ if (this._sink === null)
+ {
+ pendingData.length = 0;
+ this._doneReadingSource(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // Okay, we've read the data, and we know we have a place to write it. We
+ // need to queue up the data to be written, but *only* if none is queued
+ // already -- if data's already queued, the code that actually writes the
+ // data will make sure to wait on unconsumed pending data.
+ try
+ {
+ if (pendingData.length === 1)
+ this._waitToWriteData();
+ }
+ catch (e)
+ {
+ dumpn("!!! error waiting to write data just read, swallowing and " +
+ "writing only what we already have: " + e);
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // Whee! We successfully read some data, and it's successfully queued up to
+ // be written. All that remains now is to wait for more data to read.
+ try
+ {
+ this._waitToReadData();
+ }
+ catch (e)
+ {
+ dumpn("!!! error waiting to read more data: " + e);
+ this._doneReadingSource(Cr.NS_ERROR_UNEXPECTED);
+ }
+ },
+
+
+ // NSIOUTPUTSTREAMCALLBACK
+
+ /**
+* Callback when data may be written to the output stream without blocking, or
+* when the output stream has been closed.
+*
+* @param output : nsIAsyncOutputStream
+* the output stream on whose writability we've been waiting, also known as
+* this._sink
+*/
+ onOutputStreamReady: function(output)
+ {
+ if (this._sink === null)
+ return;
+
+ dumpn("*** onOutputStreamReady");
+
+ var pendingData = this._pendingData;
+ if (pendingData.length === 0)
+ {
+ // There's no pending data to write. The only way this can happen is if
+ // we're waiting on the output stream's closure, so we can respond to a
+ // copying failure as quickly as possible (rather than waiting for data to
+ // be available to read and then fail to be copied). Therefore, we must
+ // be done now -- don't bother to attempt to write anything and wrap
+ // things up.
+ dumpn("!!! output stream closed prematurely, ending copy");
+
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+
+ NS_ASSERT(pendingData[0].length > 0, "queued up an empty quantum?");
+
+ //
+ // Write out the first pending quantum of data. The possible errors here
+ // are:
+ //
+ // The write might fail because we can't write that much data
+ // Okay, we've written what we can now, so re-queue what's left and
+ // finish writing it out later.
+ // The write failed because the stream was closed
+ // Discard pending data that we can no longer write, stop reading, and
+ // signal that copying finished.
+ // Some other error occurred.
+ // Same as if the stream were closed, but notify with the status
+ // NS_ERROR_UNEXPECTED so the observer knows something was wonky.
+ //
+
+ try
+ {
+ var quantum = pendingData[0];
+
+ // XXX |quantum| isn't guaranteed to be ASCII, so we're relying on
+ // undefined behavior! We're only using this because writeByteArray
+ // is unusably broken for asynchronous output streams; see bug 532834
+ // for details.
+ var bytesWritten = output.write(quantum, quantum.length);
+ if (bytesWritten === quantum.length)
+ pendingData.shift();
+ else
+ pendingData[0] = quantum.substring(bytesWritten);
+
+ dumpn("*** wrote " + bytesWritten + " bytes of data");
+ }
+ catch (e)
+ {
+ if (wouldBlock(e))
+ {
+ NS_ASSERT(pendingData.length > 0,
+ "stream-blocking exception with no data to write?");
+ NS_ASSERT(pendingData[0].length > 0,
+ "stream-blocking exception with empty quantum?");
+ this._waitToWriteData();
+ return;
+ }
+
+ if (streamClosed(e))
+ dumpn("!!! output stream prematurely closed, signaling error...");
+ else
+ dumpn("!!! unknown error: " + e + ", quantum=" + quantum);
+
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // The day is ours! Quantum written, now let's see if we have more data
+ // still to write.
+ try
+ {
+ if (pendingData.length > 0)
+ {
+ this._waitToWriteData();
+ return;
+ }
+ }
+ catch (e)
+ {
+ dumpn("!!! unexpected error waiting to write pending data: " + e);
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // Okay, we have no more pending data to write -- but might we get more in
+ // the future?
+ if (this._source !== null)
+ {
+ /*
+* If we might, then wait for the output stream to be closed. (We wait
+* only for closure because we have no data to write -- and if we waited
+* for a specific amount of data, we would get repeatedly notified for no
+* reason if over time the output stream permitted more and more data to
+* be written to it without blocking.)
+*/
+ this._waitForSinkClosure();
+ }
+ else
+ {
+ /*
+* On the other hand, if we can't have more data because the input
+* stream's gone away, then it's time to notify of copy completion.
+* Victory!
+*/
+ this._sink = null;
+ this._cancelOrDispatchCancelCallback(Cr.NS_OK);
+ }
+ },
+
+
+ // NSIREQUEST
+
+ /** Returns true if the cancel observer hasn't been notified yet. */
+ isPending: function()
+ {
+ return !this._completed;
+ },
+
+ /** Not implemented, don't use! */
+ suspend: notImplemented,
+ /** Not implemented, don't use! */
+ resume: notImplemented,
+
+ /**
+* Cancels data reading from input, asynchronously writes out any pending
+* data, and causes the observer to be notified with the given error code when
+* all writing has finished.
+*
+* @param status : nsresult
+* the status to pass to the observer when data copying has been canceled
+*/
+ cancel: function(status)
+ {
+ dumpn("*** cancel(" + status.toString(16) + ")");
+
+ if (this._canceled)
+ {
+ dumpn("*** suppressing a late cancel");
+ return;
+ }
+
+ this._canceled = true;
+ this.status = status;
+
+ // We could be in the middle of absolutely anything at this point. Both
+ // input and output might still be around, we might have pending data to
+ // write, and in general we know nothing about the state of the world. We
+ // therefore must assume everything's in progress and take everything to its
+ // final steady state (or so far as it can go before we need to finish
+ // writing out remaining data).
+
+ this._doneReadingSource(status);
+ },
+
+
+ // PRIVATE IMPLEMENTATION
+
+ /**
+* Stop reading input if we haven't already done so, passing e as the status
+* when closing the stream, and kick off a copy-completion notice if no more
+* data remains to be written.
+*
+* @param e : nsresult
+* the status to be used when closing the input stream
+*/
+ _doneReadingSource: function(e)
+ {
+ dumpn("*** _doneReadingSource(0x" + e.toString(16) + ")");
+
+ this._finishSource(e);
+ if (this._pendingData.length === 0)
+ this._sink = null;
+ else
+ NS_ASSERT(this._sink !== null, "null output?");
+
+ // If we've written out all data read up to this point, then it's time to
+ // signal completion.
+ if (this._sink === null)
+ {
+ NS_ASSERT(this._pendingData.length === 0, "pending data still?");
+ this._cancelOrDispatchCancelCallback(e);
+ }
+ },
+
+ /**
+* Stop writing output if we haven't already done so, discard any data that
+* remained to be sent, close off input if it wasn't already closed, and kick
+* off a copy-completion notice.
+*
+* @param e : nsresult
+* the status to be used when closing input if it wasn't already closed
+*/
+ _doneWritingToSink: function(e)
+ {
+ dumpn("*** _doneWritingToSink(0x" + e.toString(16) + ")");
+
+ this._pendingData.length = 0;
+ this._sink = null;
+ this._doneReadingSource(e);
+ },
+
+ /**
+* Completes processing of this copy: either by canceling the copy if it
+* hasn't already been canceled using the provided status, or by dispatching
+* the cancel callback event (with the originally provided status, of course)
+* if it already has been canceled.
+*
+* @param status : nsresult
+* the status code to use to cancel this, if this hasn't already been
+* canceled
+*/
+ _cancelOrDispatchCancelCallback: function(status)
+ {
+ dumpn("*** _cancelOrDispatchCancelCallback(" + status + ")");
+
+ NS_ASSERT(this._source === null, "should have finished input");
+ NS_ASSERT(this._sink === null, "should have finished output");
+ NS_ASSERT(this._pendingData.length === 0, "should have no pending data");
+
+ if (!this._canceled)
+ {
+ this.cancel(status);
+ return;
+ }
+
+ var self = this;
+ var event =
+ {
+ run: function()
+ {
+ dumpn("*** onStopRequest async callback");
+
+ self._completed = true;
+ try
+ {
+ self._observer.onStopRequest(self, self._context, self.status);
+ }
+ catch (e)
+ {
+ NS_ASSERT(false,
+ "how are we throwing an exception here? we control " +
+ "all the callers! " + e);
+ }
+ }
+ };
+
+ gThreadManager.currentThread.dispatch(event, Ci.nsIThread.DISPATCH_NORMAL);
+ },
+
+ /**
+* Kicks off another wait for more data to be available from the input stream.
+*/
+ _waitToReadData: function()
+ {
+ dumpn("*** _waitToReadData");
+ this._source.asyncWait(this, 0, Response.SEGMENT_SIZE,
+ gThreadManager.mainThread);
+ },
+
+ /**
+* Kicks off another wait until data can be written to the output stream.
+*/
+ _waitToWriteData: function()
+ {
+ dumpn("*** _waitToWriteData");
+
+ var pendingData = this._pendingData;
+ NS_ASSERT(pendingData.length > 0, "no pending data to write?");
+ NS_ASSERT(pendingData[0].length > 0, "buffered an empty write?");
+
+ this._sink.asyncWait(this, 0, pendingData[0].length,
+ gThreadManager.mainThread);
+ },
+
+ /**
+* Kicks off a wait for the sink to which data is being copied to be closed.
+* We wait for stream closure when we don't have any data to be copied, rather
+* than waiting to write a specific amount of data. We can't wait to write
+* data because the sink might be infinitely writable, and if no data appears
+* in the source for a long time we might have to spin quite a bit waiting to
+* write, waiting to write again, &c. Waiting on stream closure instead means
+* we'll get just one notification if the sink dies. Note that when data
+* starts arriving from the sink we'll resume waiting for data to be written,
+* dropping this closure-only callback entirely.
+*/
+ _waitForSinkClosure: function()
+ {
+ dumpn("*** _waitForSinkClosure");
+
+ this._sink.asyncWait(this, Ci.nsIAsyncOutputStream.WAIT_CLOSURE_ONLY, 0,
+ gThreadManager.mainThread);
+ },
+
+ /**
+* Closes input with the given status, if it hasn't already been closed;
+* otherwise a no-op.
+*
+* @param status : nsresult
+* status code use to close the source stream if necessary
+*/
+ _finishSource: function(status)
+ {
+ dumpn("*** _finishSource(" + status.toString(16) + ")");
+
+ if (this._source !== null)
+ {
+ this._source.closeWithStatus(status);
+ this._source = null;
+ }
+ }
+};
+
+
+/**
+* A container for utility functions used with HTTP headers.
+*/
+const headerUtils =
+{
+ /**
+* Normalizes fieldName (by converting it to lowercase) and ensures it is a
+* valid header field name (although not necessarily one specified in RFC
+* 2616).
+*
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not match the field-name production in RFC 2616
+* @returns string
+* fieldName converted to lowercase if it is a valid header, for characters
+* where case conversion is possible
+*/
+ normalizeFieldName: function(fieldName)
+ {
+ if (fieldName == "")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ for (var i = 0, sz = fieldName.length; i < sz; i++)
+ {
+ if (!IS_TOKEN_ARRAY[fieldName.charCodeAt(i)])
+ {
+ dumpn(fieldName + " is not a valid header field name!");
+ throw Cr.NS_ERROR_INVALID_ARG;
+ }
+ }
+
+ return fieldName.toLowerCase();
+ },
+
+ /**
+* Ensures that fieldValue is a valid header field value (although not
+* necessarily as specified in RFC 2616 if the corresponding field name is
+* part of the HTTP protocol), normalizes the value if it is, and
+* returns the normalized value.
+*
+* @param fieldValue : string
+* a value to be normalized as an HTTP header field value
+* @throws NS_ERROR_INVALID_ARG
+* if fieldValue does not match the field-value production in RFC 2616
+* @returns string
+* fieldValue as a normalized HTTP header field value
+*/
+ normalizeFieldValue: function(fieldValue)
+ {
+ // field-value = *( field-content | LWS )
+ // field-content = <the OCTETs making up the field-value
+ // and consisting of either *TEXT or combinations
+ // of token, separators, and quoted-string>
+ // TEXT = <any OCTET except CTLs,
+ // but including LWS>
+ // LWS = [CRLF] 1*( SP | HT )
+ //
+ // quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
+ // qdtext = <any TEXT except <">>
+ // quoted-pair = "\" CHAR
+ // CHAR = <any US-ASCII character (octets 0 - 127)>
+
+ // Any LWS that occurs between field-content MAY be replaced with a single
+ // SP before interpreting the field value or forwarding the message
+ // downstream (section 4.2); we replace 1*LWS with a single SP
+ var val = fieldValue.replace(/(?:(?:\r\n)?[ \t]+)+/g, " ");
+
+ // remove leading/trailing LWS (which has been converted to SP)
+ val = val.replace(/^ +/, "").replace(/ +$/, "");
+
+ // that should have taken care of all CTLs, so val should contain no CTLs
+ for (var i = 0, len = val.length; i < len; i++)
+ if (isCTL(val.charCodeAt(i)))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ // XXX disallows quoted-pair where CHAR is a CTL -- will not invalidly
+ // normalize, however, so this can be construed as a tightening of the
+ // spec and not entirely as a bug
+ return val;
+ }
+};
+
+
+
+/**
+* Converts the given string into a string which is safe for use in an HTML
+* context.
+*
+* @param str : string
+* the string to make HTML-safe
+* @returns string
+* an HTML-safe version of str
+*/
+function htmlEscape(str)
+{
+ // this is naive, but it'll work
+ var s = "";
+ for (var i = 0; i < str.length; i++)
+ s += "&#" + str.charCodeAt(i) + ";";
+ return s;
+}
+
+
+/**
+* Constructs an object representing an HTTP version (see section 3.1).
+*
+* @param versionString
+* a string of the form "#.#", where # is an non-negative decimal integer with
+* or without leading zeros
+* @throws
+* if versionString does not specify a valid HTTP version number
+*/
+function nsHttpVersion(versionString)
+{
+ var matches = /^(\d+)\.(\d+)$/.exec(versionString);
+ if (!matches)
+ throw "Not a valid HTTP version!";
+
+ /** The major version number of this, as a number. */
+ this.major = parseInt(matches[1], 10);
+
+ /** The minor version number of this, as a number. */
+ this.minor = parseInt(matches[2], 10);
+
+ if (isNaN(this.major) || isNaN(this.minor) ||
+ this.major < 0 || this.minor < 0)
+ throw "Not a valid HTTP version!";
+}
+nsHttpVersion.prototype =
+{
+ /**
+* Returns the standard string representation of the HTTP version represented
+* by this (e.g., "1.1").
+*/
+ toString: function ()
+ {
+ return this.major + "." + this.minor;
+ },
+
+ /**
+* Returns true if this represents the same HTTP version as otherVersion,
+* false otherwise.
+*
+* @param otherVersion : nsHttpVersion
+* the version to compare against this
+*/
+ equals: function (otherVersion)
+ {
+ return this.major == otherVersion.major &&
+ this.minor == otherVersion.minor;
+ },
+
+ /** True if this >= otherVersion, false otherwise. */
+ atLeast: function(otherVersion)
+ {
+ return this.major > otherVersion.major ||
+ (this.major == otherVersion.major &&
+ this.minor >= otherVersion.minor);
+ }
+};
+
+nsHttpVersion.HTTP_1_0 = new nsHttpVersion("1.0");
+nsHttpVersion.HTTP_1_1 = new nsHttpVersion("1.1");
+
+
+/**
+* An object which stores HTTP headers for a request or response.
+*
+* Note that since headers are case-insensitive, this object converts headers to
+* lowercase before storing them. This allows the getHeader and hasHeader
+* methods to work correctly for any case of a header, but it means that the
+* values returned by .enumerator may not be equal case-sensitively to the
+* values passed to setHeader when adding headers to this.
+*/
+function nsHttpHeaders()
+{
+ /**
+* A hash of headers, with header field names as the keys and header field
+* values as the values. Header field names are case-insensitive, but upon
+* insertion here they are converted to lowercase. Header field values are
+* normalized upon insertion to contain no leading or trailing whitespace.
+*
+* Note also that per RFC 2616, section 4.2, two headers with the same name in
+* a message may be treated as one header with the same field name and a field
+* value consisting of the separate field values joined together with a "," in
+* their original order. This hash stores multiple headers with the same name
+* in this manner.
+*/
+ this._headers = {};
+}
+nsHttpHeaders.prototype =
+{
+ /**
+* Sets the header represented by name and value in this.
+*
+* @param name : string
+* the header name
+* @param value : string
+* the header value
+* @throws NS_ERROR_INVALID_ARG
+* if name or value is not a valid header component
+*/
+ setHeader: function(fieldName, fieldValue, merge)
+ {
+ var name = headerUtils.normalizeFieldName(fieldName);
+ var value = headerUtils.normalizeFieldValue(fieldValue);
+
+ // The following three headers are stored as arrays because their real-world
+ // syntax prevents joining individual headers into a single header using
+ // ",". See also <http://hg.mozilla.org/mozilla-central/diff/9b2a99adc05e/netwerk/protocol/http/src/nsHttpHeaderArray.cpp#l77>
+ if (merge && name in this._headers)
+ {
+ if (name === "www-authenticate" ||
+ name === "proxy-authenticate" ||
+ name === "set-cookie")
+ {
+ this._headers[name].push(value);
+ }
+ else
+ {
+ this._headers[name][0] += "," + value;
+ NS_ASSERT(this._headers[name].length === 1,
+ "how'd a non-special header have multiple values?")
+ }
+ }
+ else
+ {
+ this._headers[name] = [value];
+ }
+ },
+
+ /**
+* Returns the value for the header specified by this.
+*
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not constitute a valid header field name
+* @throws NS_ERROR_NOT_AVAILABLE
+* if the given header does not exist in this
+* @returns string
+* the field value for the given header, possibly with non-semantic changes
+* (i.e., leading/trailing whitespace stripped, whitespace runs replaced
+* with spaces, etc.) at the option of the implementation; multiple
+* instances of the header will be combined with a comma, except for
+* the three headers noted in the description of getHeaderValues
+*/
+ getHeader: function(fieldName)
+ {
+ return this.getHeaderValues(fieldName).join("\n");
+ },
+
+ /**
+* Returns the value for the header specified by fieldName as an array.
+*
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not constitute a valid header field name
+* @throws NS_ERROR_NOT_AVAILABLE
+* if the given header does not exist in this
+* @returns [string]
+* an array of all the header values in this for the given
+* header name. Header values will generally be collapsed
+* into a single header by joining all header values together
+* with commas, but certain headers (Proxy-Authenticate,
+* WWW-Authenticate, and Set-Cookie) violate the HTTP spec
+* and cannot be collapsed in this manner. For these headers
+* only, the returned array may contain multiple elements if
+* that header has been added more than once.
+*/
+ getHeaderValues: function(fieldName)
+ {
+ var name = headerUtils.normalizeFieldName(fieldName);
+
+ if (name in this._headers)
+ return this._headers[name];
+ else
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ },
+
+ /**
+* Returns true if a header with the given field name exists in this, false
+* otherwise.
+*
+* @param fieldName : string
+* the field name whose existence is to be determined in this
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not constitute a valid header field name
+* @returns boolean
+* true if the header's present, false otherwise
+*/
+ hasHeader: function(fieldName)
+ {
+ var name = headerUtils.normalizeFieldName(fieldName);
+ return (name in this._headers);
+ },
+
+ /**
+* Returns a new enumerator over the field names of the headers in this, as
+* nsISupportsStrings. The names returned will be in lowercase, regardless of
+* how they were input using setHeader (header names are case-insensitive per
+* RFC 2616).
+*/
+ get enumerator()
+ {
+ var headers = [];
+ for (var i in this._headers)
+ {
+ var supports = new SupportsString();
+ supports.data = i;
+ headers.push(supports);
+ }
+
+ return new nsSimpleEnumerator(headers);
+ }
+};
+
+
+/**
+* Constructs an nsISimpleEnumerator for the given array of items.
+*
+* @param items : Array
+* the items, which must all implement nsISupports
+*/
+function nsSimpleEnumerator(items)
+{
+ this._items = items;
+ this._nextIndex = 0;
+}
+nsSimpleEnumerator.prototype =
+{
+ hasMoreElements: function()
+ {
+ return this._nextIndex < this._items.length;
+ },
+ getNext: function()
+ {
+ if (!this.hasMoreElements())
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+
+ return this._items[this._nextIndex++];
+ },
+ QueryInterface: function(aIID)
+ {
+ if (Ci.nsISimpleEnumerator.equals(aIID) ||
+ Ci.nsISupports.equals(aIID))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ }
+};
+
+
+/**
+* A representation of the data in an HTTP request.
+*
+* @param port : uint
+* the port on which the server receiving this request runs
+*/
+function Request(port)
+{
+ /** Method of this request, e.g. GET or POST. */
+ this._method = "";
+
+ /** Path of the requested resource; empty paths are converted to '/'. */
+ this._path = "";
+
+ /** Query string, if any, associated with this request (not including '?'). */
+ this._queryString = "";
+
+ /** Scheme of requested resource, usually http, always lowercase. */
+ this._scheme = "http";
+
+ /** Hostname on which the requested resource resides. */
+ this._host = undefined;
+
+ /** Port number over which the request was received. */
+ this._port = port;
+
+ var bodyPipe = new Pipe(false, false, 0, PR_UINT32_MAX, null);
+
+ /** Stream from which data in this request's body may be read. */
+ this._bodyInputStream = bodyPipe.inputStream;
+
+ /** Stream to which data in this request's body is written. */
+ this._bodyOutputStream = bodyPipe.outputStream;
+
+ /**
+* The headers in this request.
+*/
+ this._headers = new nsHttpHeaders();
+
+ /**
+* For the addition of ad-hoc properties and new functionality without having
+* to change nsIHttpRequest every time; currently lazily created, as its only
+* use is in directory listings.
+*/
+ this._bag = null;
+}
+Request.prototype =
+{
+ // SERVER METADATA
+
+ //
+ // see nsIHttpRequest.scheme
+ //
+ get scheme()
+ {
+ return this._scheme;
+ },
+
+ //
+ // see nsIHttpRequest.host
+ //
+ get host()
+ {
+ return this._host;
+ },
+
+ //
+ // see nsIHttpRequest.port
+ //
+ get port()
+ {
+ return this._port;
+ },
+
+ // REQUEST LINE
+
+ //
+ // see nsIHttpRequest.method
+ //
+ get method()
+ {
+ return this._method;
+ },
+
+ //
+ // see nsIHttpRequest.httpVersion
+ //
+ get httpVersion()
+ {
+ return this._httpVersion.toString();
+ },
+
+ //
+ // see nsIHttpRequest.path
+ //
+ get path()
+ {
+ return this._path;
+ },
+
+ //
+ // see nsIHttpRequest.queryString
+ //
+ get queryString()
+ {
+ return this._queryString;
+ },
+
+ // HEADERS
+
+ //
+ // see nsIHttpRequest.getHeader
+ //
+ getHeader: function(name)
+ {
+ return this._headers.getHeader(name);
+ },
+
+ //
+ // see nsIHttpRequest.hasHeader
+ //
+ hasHeader: function(name)
+ {
+ return this._headers.hasHeader(name);
+ },
+
+ //
+ // see nsIHttpRequest.headers
+ //
+ get headers()
+ {
+ return this._headers.enumerator;
+ },
+
+ //
+ // see nsIPropertyBag.enumerator
+ //
+ get enumerator()
+ {
+ this._ensurePropertyBag();
+ return this._bag.enumerator;
+ },
+
+ //
+ // see nsIHttpRequest.headers
+ //
+ get bodyInputStream()
+ {
+ return this._bodyInputStream;
+ },
+
+ //
+ // see nsIPropertyBag.getProperty
+ //
+ getProperty: function(name)
+ {
+ this._ensurePropertyBag();
+ return this._bag.getProperty(name);
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIHttpRequest) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // PRIVATE IMPLEMENTATION
+
+ /** Ensures a property bag has been created for ad-hoc behaviors. */
+ _ensurePropertyBag: function()
+ {
+ if (!this._bag)
+ this._bag = new WritablePropertyBag();
+ }
+};
+
+
+// XPCOM trappings
+if ("XPCOMUtils" in this && // Firefox 3.6 doesn't load XPCOMUtils in this scope for some reason...
+ "generateNSGetFactory" in XPCOMUtils) {
+ var NSGetFactory = XPCOMUtils.generateNSGetFactory([nsHttpServer]);
+}
+
+
+
+/**
+* Creates a new HTTP server listening for loopback traffic on the given port,
+* starts it, and runs the server until the server processes a shutdown request,
+* spinning an event loop so that events posted by the server's socket are
+* processed.
+*
+* This method is primarily intended for use in running this script from within
+* xpcshell and running a functional HTTP server without having to deal with
+* non-essential details.
+*
+* Note that running multiple servers using variants of this method probably
+* doesn't work, simply due to how the internal event loop is spun and stopped.
+*
+* @note
+* This method only works with Mozilla 1.9 (i.e., Firefox 3 or trunk code);
+* you should use this server as a component in Mozilla 1.8.
+* @param port
+* the port on which the server will run, or -1 if there exists no preference
+* for a specific port; note that attempting to use some values for this
+* parameter (particularly those below 1024) may cause this method to throw or
+* may result in the server being prematurely shut down
+* @param basePath
+* a local directory from which requests will be served (i.e., if this is
+* "/home/jwalden/" then a request to /index.html will load
+* /home/jwalden/index.html); if this is omitted, only the default URLs in
+* this server implementation will be functional
+*/
+function server(port, basePath)
+{
+ if (basePath)
+ {
+ var lp = Cc["@mozilla.org/file/local;1"]
+ .createInstance(Ci.nsILocalFile);
+ lp.initWithPath(basePath);
+ }
+
+ // if you're running this, you probably want to see debugging info
+ DEBUG = true;
+
+ var srv = new nsHttpServer();
+ if (lp)
+ srv.registerDirectory("/", lp);
+ srv.registerContentType("sjs", SJS_TYPE);
+ srv.start(port);
+
+ var thread = gThreadManager.currentThread;
+ while (!srv.isStopped())
+ thread.processNextEvent(true);
+
+ // get rid of any pending requests
+ while (thread.hasPendingEvents())
+ thread.processNextEvent(true);
+
+ DEBUG = false;
+}
+
+function startServerAsync(port, basePath)
+{
+ if (basePath)
+ {
+ var lp = Cc["@mozilla.org/file/local;1"]
+ .createInstance(Ci.nsILocalFile);
+ lp.initWithPath(basePath);
+ }
+
+ var srv = new nsHttpServer();
+ if (lp)
+ srv.registerDirectory("/", lp);
+ srv.registerContentType("sjs", "sjs");
+ srv.start(port);
+ return srv;
+}
+
+exports.nsHttpServer = nsHttpServer;
+exports.ScriptableInputStream = ScriptableInputStream;
+exports.server = server;
+exports.startServerAsync = startServerAsync;
diff --git a/addon-sdk/source/test/addons/places/lib/main.js b/addon-sdk/source/test/addons/places/lib/main.js
new file mode 100644
index 000000000..289cba4b5
--- /dev/null
+++ b/addon-sdk/source/test/addons/places/lib/main.js
@@ -0,0 +1,27 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+
+const { safeMerge: merge } = require('sdk/util/object');
+const app = require("sdk/system/xul-app");
+
+// Once Bug 903018 is resolved, just move the application testing to
+// module.metadata.engines
+if (app.is('Firefox')) {
+ merge(module.exports,
+ require('./test-places-events'),
+ require('./test-places-bookmarks'),
+ require('./test-places-favicon'),
+ require('./test-places-history'),
+ require('./test-places-host'),
+ require('./test-places-utils')
+ );
+} else {
+ exports['test unsupported'] = (assert) => {
+ assert.pass('This application is unsupported.');
+ };
+}
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/places/lib/places-helper.js b/addon-sdk/source/test/addons/places/lib/places-helper.js
new file mode 100644
index 000000000..a8545b24a
--- /dev/null
+++ b/addon-sdk/source/test/addons/places/lib/places-helper.js
@@ -0,0 +1,239 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+ 'use strict'
+
+const { Cc, Ci, Cu } = require('chrome');
+const bmsrv = Cc['@mozilla.org/browser/nav-bookmarks-service;1'].
+ getService(Ci.nsINavBookmarksService);
+const hsrv = Cc['@mozilla.org/browser/nav-history-service;1'].
+ getService(Ci.nsINavHistoryService);
+const brsrv = Cc["@mozilla.org/browser/nav-history-service;1"]
+ .getService(Ci.nsIBrowserHistory);
+const tagsrv = Cc['@mozilla.org/browser/tagging-service;1'].
+ getService(Ci.nsITaggingService);
+const asyncHistory = Cc['@mozilla.org/browser/history;1'].
+ getService(Ci.mozIAsyncHistory);
+const { send } = require('sdk/addon/events');
+const { setTimeout } = require('sdk/timers');
+const { newURI } = require('sdk/url/utils');
+const { defer, all } = require('sdk/core/promise');
+const { once } = require('sdk/system/events');
+const { set } = require('sdk/preferences/service');
+const {
+ Bookmark, Group, Separator,
+ save, search,
+ MENU, TOOLBAR, UNSORTED
+} = require('sdk/places/bookmarks');
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
+ "resource://gre/modules/PlacesUtils.jsm");
+
+function invalidResolve (assert) {
+ return function (e) {
+ assert.fail('Resolve state should not be called: ' + e);
+ };
+}
+exports.invalidResolve = invalidResolve;
+
+// Removes all children of group
+function clearBookmarks (group) {
+ group
+ ? bmsrv.removeFolderChildren(group.id)
+ : clearAllBookmarks();
+}
+
+function clearAllBookmarks () {
+ [MENU, TOOLBAR, UNSORTED].forEach(clearBookmarks);
+}
+
+function clearHistory (done) {
+ PlacesUtils.history.clear().catch(Cu.reportError).then(done);
+}
+
+// Cleans bookmarks and history and disables maintanance
+function resetPlaces (done) {
+ // Set last maintenance to current time to prevent
+ // Places DB maintenance occuring and locking DB
+ set('places.database.lastMaintenance', Math.floor(Date.now() / 1000));
+ clearAllBookmarks();
+ clearHistory(done);
+}
+exports.resetPlaces = resetPlaces;
+
+function compareWithHost (assert, item) {
+ let id = item.id;
+ let type = item.type === 'group' ? bmsrv.TYPE_FOLDER : bmsrv['TYPE_' + item.type.toUpperCase()];
+ let url = item.url && !item.url.endsWith('/') ? item.url + '/' : item.url;
+
+ if (type === bmsrv.TYPE_BOOKMARK) {
+ assert.equal(url, bmsrv.getBookmarkURI(id).spec.toString(), 'Matches host url');
+ let tags = tagsrv.getTagsForURI(newURI(item.url));
+ for (let tag of tags) {
+ // Handle both array for raw data and set for instances
+ if (Array.isArray(item.tags))
+ assert.ok(~item.tags.indexOf(tag), 'has correct tag');
+ else
+ assert.ok(item.tags.has(tag), 'has correct tag');
+ }
+ assert.equal(tags.length,
+ Array.isArray(item.tags) ? item.tags.length : item.tags.size,
+ 'matches tag count');
+ }
+ if (type !== bmsrv.TYPE_SEPARATOR) {
+ assert.equal(item.title, bmsrv.getItemTitle(id), 'Matches host title');
+ }
+ assert.equal(item.index, bmsrv.getItemIndex(id), 'Matches host index');
+ assert.equal(item.group.id || item.group, bmsrv.getFolderIdForItem(id), 'Matches host group id');
+ assert.equal(type, bmsrv.getItemType(id), 'Matches host type');
+}
+exports.compareWithHost = compareWithHost;
+
+function addVisits (urls) {
+ var deferred = defer();
+ asyncHistory.updatePlaces([].concat(urls).map(createVisit), {
+ handleResult: function () {},
+ handleError: deferred.reject,
+ handleCompletion: deferred.resolve
+ });
+
+ return deferred.promise;
+}
+exports.addVisits = addVisits;
+
+function removeVisits (urls) {
+ [].concat(urls).map(url => {
+ hsrv.removePage(newURI(url));
+ });
+}
+exports.removeVisits = removeVisits;
+
+// Creates a mozIVisitInfo object
+function createVisit (url) {
+ let place = {}
+ place.uri = newURI(url);
+ place.title = "Test visit for " + place.uri.spec;
+ place.visits = [{
+ transitionType: hsrv.TRANSITION_LINK,
+ visitDate: +(new Date()) * 1000,
+ referredURI: undefined
+ }];
+ return place;
+}
+
+function createBookmark (data) {
+ data = data || {};
+ let item = {
+ title: data.title || 'Moz',
+ url: data.url || (!data.type || data.type === 'bookmark' ?
+ 'http://moz.com/' :
+ undefined),
+ tags: data.tags || (!data.type || data.type === 'bookmark' ?
+ ['firefox'] :
+ undefined),
+ type: data.type || 'bookmark',
+ group: data.group
+ };
+ return send('sdk-places-bookmarks-create', item);
+}
+exports.createBookmark = createBookmark;
+
+function historyBatch () {
+ hsrv.runInBatchMode(() => {}, null);
+}
+exports.historyBatch = historyBatch;
+
+function createBookmarkItem (data) {
+ let deferred = defer();
+ data = data || {};
+ save({
+ title: data.title || 'Moz',
+ url: data.url || 'http://moz.com/',
+ tags: data.tags || (!data.type || data.type === 'bookmark' ?
+ ['firefox'] :
+ undefined),
+ type: data.type || 'bookmark',
+ group: data.group
+ }).on('end', function (bookmark) {
+ deferred.resolve(bookmark[0]);
+ });
+ return deferred.promise;
+}
+exports.createBookmarkItem = createBookmarkItem;
+
+function createBookmarkTree () {
+ let agg = [];
+ return createBookmarkItem({ type: 'group', title: 'mozgroup' })
+ .then(group => {
+ agg.push(group);
+ return all([createBookmarkItem({
+ title: 'mozilla.com',
+ url: 'http://mozilla.com/',
+ group: group,
+ tags: ['mozilla', 'firefox', 'thunderbird', 'rust']
+ }), createBookmarkItem({
+ title: 'mozilla.org',
+ url: 'http://mozilla.org/',
+ group: group,
+ tags: ['mozilla', 'firefox', 'thunderbird', 'rust']
+ }), createBookmarkItem({
+ title: 'firefox',
+ url: 'http://firefox.com/',
+ group: group,
+ tags: ['mozilla', 'firefox', 'browser']
+ }), createBookmarkItem({
+ title: 'thunderbird',
+ url: 'http://mozilla.org/thunderbird/',
+ group: group,
+ tags: ['mozilla', 'thunderbird', 'email']
+ }), createBookmarkItem({
+ title: 'moz subfolder',
+ group: group,
+ type: 'group'
+ })
+ ]);
+ })
+ .then(results => {
+ agg = agg.concat(results);
+ let subfolder = results.filter(item => item.type === 'group')[0];
+ return createBookmarkItem({
+ title: 'dark javascript secrets',
+ url: 'http://w3schools.com',
+ group: subfolder,
+ tags: []
+ });
+ }).then(item => {
+ agg.push(item);
+ return createBookmarkItem(
+ { type: 'group', group: MENU, title: 'other stuff' }
+ );
+ }).then(newGroup => {
+ agg.push(newGroup);
+ return all([
+ createBookmarkItem({
+ title: 'mdn',
+ url: 'http://developer.mozilla.org/en-US/',
+ group: newGroup,
+ tags: ['javascript']
+ }),
+ createBookmarkItem({
+ title: 'web audio',
+ url: 'http://webaud.io',
+ group: newGroup,
+ tags: ['javascript', 'web audio']
+ }),
+ createBookmarkItem({
+ title: 'web audio components',
+ url: 'http://component.fm',
+ group: newGroup,
+ tags: ['javascript', 'web audio', 'components']
+ })
+ ]);
+ }).then(results => {
+ agg = agg.concat(results);
+ return agg;
+ });
+}
+exports.createBookmarkTree = createBookmarkTree;
diff --git a/addon-sdk/source/test/addons/places/lib/test-places-bookmarks.js b/addon-sdk/source/test/addons/places/lib/test-places-bookmarks.js
new file mode 100644
index 000000000..ff490f6a4
--- /dev/null
+++ b/addon-sdk/source/test/addons/places/lib/test-places-bookmarks.js
@@ -0,0 +1,948 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+module.metadata = {
+ 'engines': {
+ 'Firefox': '*'
+ }
+};
+
+const { Cc, Ci } = require('chrome');
+const { request } = require('sdk/addon/host');
+const { filter } = require('sdk/event/utils');
+const { on, off } = require('sdk/event/core');
+const { setTimeout } = require('sdk/timers');
+const { newURI } = require('sdk/url/utils');
+const { defer, all, resolve } = require('sdk/core/promise');
+const { before, after } = require('sdk/test/utils');
+
+const {
+ Bookmark, Group, Separator,
+ save, search, remove,
+ MENU, TOOLBAR, UNSORTED
+} = require('sdk/places/bookmarks');
+const {
+ invalidResolve, createTree,
+ compareWithHost, createBookmark, createBookmarkItem,
+ createBookmarkTree, addVisits, resetPlaces
+} = require('./places-helper');
+const { promisedEmitter } = require('sdk/places/utils');
+const bmsrv = Cc['@mozilla.org/browser/nav-bookmarks-service;1'].
+ getService(Ci.nsINavBookmarksService);
+const tagsrv = Cc['@mozilla.org/browser/tagging-service;1'].
+ getService(Ci.nsITaggingService);
+
+exports.testDefaultFolders = function (assert) {
+ var ids = [
+ bmsrv.bookmarksMenuFolder,
+ bmsrv.toolbarFolder,
+ bmsrv.unfiledBookmarksFolder
+ ];
+
+ [MENU, TOOLBAR, UNSORTED].forEach(function (g, i) {
+ assert.ok(g.id === ids[i], ' default group matches id');
+ });
+};
+
+exports.testValidation = function (assert) {
+ assert.throws(() => {
+ Bookmark({ title: 'a title' });
+ }, /The `url` property must be a valid URL/, 'throws empty URL error');
+
+ assert.throws(() => {
+ Bookmark({ title: 'a title', url: 'not.a.url' });
+ }, /The `url` property must be a valid URL/, 'throws invalid URL error');
+
+ assert.throws(() => {
+ Bookmark({ url: 'http://foo.com' });
+ }, /The `title` property must be defined/, 'throws title error');
+
+ assert.throws(() => {
+ Bookmark();
+ }, /./, 'throws any error');
+
+ assert.throws(() => {
+ Group();
+ }, /The `title` property must be defined/, 'throws title error for group');
+
+ assert.throws(() => {
+ Bookmark({ url: 'http://foo.com', title: 'my title', tags: 'a tag' });
+ }, /The `tags` property must be a Set, or an array/, 'throws error for non set/array tag');
+};
+
+exports.testCreateBookmarks = function (assert, done) {
+ var bm = Bookmark({
+ title: 'moz',
+ url: 'http://mozilla.org',
+ tags: ['moz1', 'moz2', 'moz3']
+ });
+
+ save(bm).on('data', (bookmark, input) => {
+ assert.equal(input, bm, 'input is original input item');
+ assert.ok(bookmark.id, 'Bookmark has ID');
+ assert.equal(bookmark.title, 'moz');
+ assert.equal(bookmark.url, 'http://mozilla.org');
+ assert.equal(bookmark.group, UNSORTED, 'Unsorted folder is default parent');
+ assert.ok(bookmark !== bm, 'bookmark should be a new instance');
+ compareWithHost(assert, bookmark);
+ }).on('end', bookmarks => {
+ assert.equal(bookmarks.length, 1, 'returned bookmarks in end');
+ assert.equal(bookmarks[0].url, 'http://mozilla.org');
+ assert.equal(bookmarks[0].tags.has('moz1'), true, 'has first tag');
+ assert.equal(bookmarks[0].tags.has('moz2'), true, 'has second tag');
+ assert.equal(bookmarks[0].tags.has('moz3'), true, 'has third tag');
+ assert.pass('end event is called');
+ done();
+ });
+};
+
+exports.testCreateGroup = function (assert, done) {
+ save(Group({ title: 'mygroup', group: MENU })).on('data', g => {
+ assert.ok(g.id, 'Bookmark has ID');
+ assert.equal(g.title, 'mygroup', 'matches title');
+ assert.equal(g.group, MENU, 'Menu folder matches');
+ compareWithHost(assert, g);
+ }).on('end', results => {
+ assert.equal(results.length, 1);
+ assert.pass('end event is called');
+ done();
+ });
+};
+
+exports.testCreateSeparator = function (assert, done) {
+ save(Separator({ group: MENU })).on('data', function (s) {
+ assert.ok(s.id, 'Separator has id');
+ assert.equal(s.group, MENU, 'Parent group matches');
+ compareWithHost(assert, s);
+ }).on('end', function (results) {
+ assert.equal(results.length, 1);
+ assert.pass('end event is called');
+ done();
+ });
+};
+
+exports.testCreateError = function (assert, done) {
+ let bookmarks = [
+ { title: 'moz1', url: 'http://moz1.com', type: 'bookmark'},
+ { title: 'moz2', url: 'invalidurl', type: 'bookmark'},
+ { title: 'moz3', url: 'http://moz3.com', type: 'bookmark'}
+ ];
+
+ let dataCount = 0, errorCount = 0;
+ save(bookmarks).on('data', bookmark => {
+ assert.ok(/moz[1|3]/.test(bookmark.title), 'valid bookmarks complete');
+ dataCount++;
+ }).on('error', (reason, item) => {
+ assert.ok(
+ /The `url` property must be a valid URL/.test(reason),
+ 'Error event called with correct reason');
+ assert.equal(item, bookmarks[1], 'returns input that failed in event');
+ errorCount++;
+ }).on('end', items => {
+ assert.equal(dataCount, 2, 'data event called twice');
+ assert.equal(errorCount, 1, 'error event called once');
+ assert.equal(items.length, bookmarks.length, 'all items should be in result');
+ assert.equal(items[0].toString(), '[object Bookmark]',
+ 'should be a saved instance');
+ assert.equal(items[2].toString(), '[object Bookmark]',
+ 'should be a saved instance');
+ assert.equal(items[1], bookmarks[1], 'should be original, unsaved object');
+
+ search({ query: 'moz' }).on('end', items => {
+ assert.equal(items.length, 2, 'only two items were successfully saved');
+ bookmarks[1].url = 'http://moz2.com/';
+ dataCount = errorCount = 0;
+ save(bookmarks).on('data', bookmark => {
+ dataCount++;
+ }).on('error', reason => errorCount++)
+ .on('end', items => {
+ assert.equal(items.length, 3, 'all 3 items saved');
+ assert.equal(dataCount, 3, '3 data events called');
+ assert.equal(errorCount, 0, 'no error events called');
+ search({ query: 'moz' }).on('end', items => {
+ assert.equal(items.length, 3, 'only 3 items saved');
+ items.map(item =>
+ assert.ok(/moz\d\.com/.test(item.url), 'correct item'))
+ done();
+ });
+ });
+ });
+ });
+};
+
+exports.testSaveDucktypes = function (assert, done) {
+ save({
+ title: 'moz',
+ url: 'http://mozilla.org',
+ type: 'bookmark'
+ }).on('data', (bookmark) => {
+ compareWithHost(assert, bookmark);
+ done();
+ });
+};
+
+exports.testSaveDucktypesParent = function (assert, done) {
+ let folder = { title: 'myfolder', type: 'group' };
+ let bookmark = { title: 'mozzie', url: 'http://moz.com', group: folder, type: 'bookmark' };
+ let sep = { type: 'separator', group: folder };
+ save([sep, bookmark]).on('end', (res) => {
+ compareWithHost(assert, res[0]);
+ compareWithHost(assert, res[1]);
+ assert.equal(res[0].group.title, 'myfolder', 'parent is ducktyped group');
+ assert.equal(res[1].group.title, 'myfolder', 'parent is ducktyped group');
+ done();
+ });
+};
+
+/*
+ * Tests the scenario where the original bookmark item is resaved
+ * and does not have an ID or an updated date, but should still be
+ * mapped to the item it created previously
+ */
+exports.testResaveOriginalItemMapping = function (assert, done) {
+ let bookmark = Bookmark({ title: 'moz', url: 'http://moz.org' });
+ save(bookmark).on('data', newBookmark => {
+ bookmark.title = 'new moz';
+ save(bookmark).on('data', newNewBookmark => {
+ assert.equal(newBookmark.id, newNewBookmark.id, 'should be the same bookmark item');
+ assert.equal(bmsrv.getItemTitle(newBookmark.id), 'new moz', 'should have updated title');
+ done();
+ });
+ });
+};
+
+exports.testCreateMultipleBookmarks = function (assert, done) {
+ let data = [
+ Bookmark({title: 'bm1', url: 'http://bm1.com'}),
+ Bookmark({title: 'bm2', url: 'http://bm2.com'}),
+ Bookmark({title: 'bm3', url: 'http://bm3.com'}),
+ ];
+ save(data).on('data', function (bookmark, input) {
+ let stored = data.filter(({title}) => title === bookmark.title)[0];
+ assert.equal(input, stored, 'input is original input item');
+ assert.equal(bookmark.title, stored.title, 'titles match');
+ assert.equal(bookmark.url, stored.url, 'urls match');
+ compareWithHost(assert, bookmark);
+ }).on('end', function (bookmarks) {
+ assert.equal(bookmarks.length, 3, 'all bookmarks returned');
+ done();
+ });
+};
+
+exports.testCreateImplicitParent = function (assert, done) {
+ let folder = Group({ title: 'my parent' });
+ let bookmarks = [
+ Bookmark({ title: 'moz1', url: 'http://moz1.com', group: folder }),
+ Bookmark({ title: 'moz2', url: 'http://moz2.com', group: folder }),
+ Bookmark({ title: 'moz3', url: 'http://moz3.com', group: folder })
+ ];
+ save(bookmarks).on('data', function (bookmark) {
+ if (bookmark.type === 'bookmark') {
+ assert.equal(bookmark.group.title, folder.title, 'parent is linked');
+ compareWithHost(assert, bookmark);
+ } else if (bookmark.type === 'group') {
+ assert.equal(bookmark.group.id, UNSORTED.id, 'parent ID of group is correct');
+ compareWithHost(assert, bookmark);
+ }
+ }).on('end', function (results) {
+ assert.equal(results.length, 3, 'results should only hold explicit saves');
+ done();
+ });
+};
+
+exports.testCreateExplicitParent = function (assert, done) {
+ let folder = Group({ title: 'my parent' });
+ let bookmarks = [
+ Bookmark({ title: 'moz1', url: 'http://moz1.com', group: folder }),
+ Bookmark({ title: 'moz2', url: 'http://moz2.com', group: folder }),
+ Bookmark({ title: 'moz3', url: 'http://moz3.com', group: folder })
+ ];
+ save(bookmarks.concat(folder)).on('data', function (bookmark) {
+ if (bookmark.type === 'bookmark') {
+ assert.equal(bookmark.group.title, folder.title, 'parent is linked');
+ compareWithHost(assert, bookmark);
+ } else if (bookmark.type === 'group') {
+ assert.equal(bookmark.group.id, UNSORTED.id, 'parent ID of group is correct');
+ compareWithHost(assert, bookmark);
+ }
+ }).on('end', function () {
+ done();
+ });
+};
+
+exports.testCreateNested = function (assert, done) {
+ let topFolder = Group({ title: 'top', group: MENU });
+ let midFolder = Group({ title: 'middle', group: topFolder });
+ let bookmarks = [
+ Bookmark({ title: 'moz1', url: 'http://moz1.com', group: midFolder }),
+ Bookmark({ title: 'moz2', url: 'http://moz2.com', group: midFolder }),
+ Bookmark({ title: 'moz3', url: 'http://moz3.com', group: midFolder })
+ ];
+ let dataEventCount = 0;
+ save(bookmarks).on('data', function (bookmark) {
+ if (bookmark.type === 'bookmark') {
+ assert.equal(bookmark.group.title, midFolder.title, 'parent is linked');
+ } else if (bookmark.title === 'top') {
+ assert.equal(bookmark.group.id, MENU.id, 'parent ID of top group is correct');
+ } else {
+ assert.equal(bookmark.group.title, topFolder.title, 'parent title of middle group is correct');
+ }
+ dataEventCount++;
+ compareWithHost(assert, bookmark);
+ }).on('end', () => {
+ assert.equal(dataEventCount, 5, 'data events for all saves have occurred');
+ assert.ok('end event called');
+ done();
+ });
+};
+
+/*
+ * Was a scenario when implicitly saving a bookmark that was already created,
+ * it was not being properly fetched and attempted to recreate
+ */
+exports.testAddingToExistingParent = function (assert, done) {
+ let group = { type: 'group', title: 'mozgroup' };
+ let bookmarks = [
+ { title: 'moz1', url: 'http://moz1.com', type: 'bookmark', group: group },
+ { title: 'moz2', url: 'http://moz2.com', type: 'bookmark', group: group },
+ { title: 'moz3', url: 'http://moz3.com', type: 'bookmark', group: group }
+ ],
+ firstBatch, secondBatch;
+
+ saveP(bookmarks).then(data => {
+ firstBatch = data;
+ return saveP([
+ { title: 'moz4', url: 'http://moz4.com', type: 'bookmark', group: group },
+ { title: 'moz5', url: 'http://moz5.com', type: 'bookmark', group: group }
+ ]);
+ }, assert.fail).then(data => {
+ secondBatch = data;
+ assert.equal(firstBatch[0].group.id, secondBatch[0].group.id,
+ 'successfully saved to the same parent');
+ }).then(done).catch(assert.fail);
+};
+
+exports.testUpdateParent = function (assert, done) {
+ let group = { type: 'group', title: 'mozgroup' };
+ saveP(group).then(item => {
+ item[0].title = 'mozgroup-resave';
+ return saveP(item[0]);
+ }).then(item => {
+ assert.equal(item[0].title, 'mozgroup-resave', 'group saved successfully');
+ }).then(done).catch(assert.fail);
+};
+
+exports.testUpdateSeparator = function (assert, done) {
+ let sep = [Separator(), Separator(), Separator()];
+ saveP(sep).then(item => {
+ item[0].index = 2;
+ return saveP(item[0]);
+ }).then(item => {
+ assert.equal(item[0].index, 2, 'updated index of separator');
+ }).then(done).catch(assert.fail);
+};
+
+exports.testPromisedSave = function (assert, done) {
+ let topFolder = Group({ title: 'top', group: MENU });
+ let midFolder = Group({ title: 'middle', group: topFolder });
+ let bookmarks = [
+ Bookmark({ title: 'moz1', url: 'http://moz1.com', group: midFolder}),
+ Bookmark({ title: 'moz2', url: 'http://moz2.com', group: midFolder}),
+ Bookmark({ title: 'moz3', url: 'http://moz3.com', group: midFolder})
+ ];
+ let first, second, third;
+ saveP(bookmarks).then(bms => {
+ first = bms.filter(b => b.title === 'moz1')[0];
+ second = bms.filter(b => b.title === 'moz2')[0];
+ third = bms.filter(b => b.title === 'moz3')[0];
+ assert.equal(first.index, 0);
+ assert.equal(second.index, 1);
+ assert.equal(third.index, 2);
+ first.index = 3;
+ return saveP(first);
+ }).then(() => {
+ assert.equal(bmsrv.getItemIndex(first.id), 2, 'properly moved bookmark');
+ assert.equal(bmsrv.getItemIndex(second.id), 0, 'other bookmarks adjusted');
+ assert.equal(bmsrv.getItemIndex(third.id), 1, 'other bookmarks adjusted');
+ }).then(done).catch(assert.fail);
+};
+
+exports.testPromisedErrorSave = function*(assert) {
+ let bookmarks = [
+ { title: 'moz1', url: 'http://moz1.com', type: 'bookmark'},
+ { title: 'moz2', url: 'invalidurl', type: 'bookmark'},
+ { title: 'moz3', url: 'http://moz3.com', type: 'bookmark'}
+ ];
+
+ yield saveP(bookmarks).then(() => {
+ assert.fail("should not resolve");
+ }, reason => {
+ assert.ok(
+ /The `url` property must be a valid URL/.test(reason),
+ 'Error event called with correct reason');
+ });
+
+ bookmarks[1].url = 'http://moz2.com';
+ yield saveP(bookmarks);
+
+ let res = yield searchP({ query: 'moz' });
+ assert.equal(res.length, 3, 'all 3 should be saved upon retry');
+ res.map(item => assert.ok(/moz\d\.com/.test(item.url), 'correct item'));
+};
+
+exports.testMovingChildren = function (assert, done) {
+ let topFolder = Group({ title: 'top', group: MENU });
+ let midFolder = Group({ title: 'middle', group: topFolder });
+ let bookmarks = [
+ Bookmark({ title: 'moz1', url: 'http://moz1.com', group: midFolder}),
+ Bookmark({ title: 'moz2', url: 'http://moz2.com', group: midFolder}),
+ Bookmark({ title: 'moz3', url: 'http://moz3.com', group: midFolder})
+ ];
+
+ save(bookmarks).on('end', bms => {
+ let first = bms.filter(b => b.title === 'moz1')[0];
+ let second = bms.filter(b => b.title === 'moz2')[0];
+ let third = bms.filter(b => b.title === 'moz3')[0];
+ assert.equal(first.index, 0);
+ assert.equal(second.index, 1);
+ assert.equal(third.index, 2);
+ /* When moving down in the same container we take
+ * into account the removal of the original item. If you want
+ * to move from index X to index Y > X you must use
+ * moveItem(id, folder, Y + 1)
+ */
+ first.index = 3;
+ save(first).on('end', () => {
+ assert.equal(bmsrv.getItemIndex(first.id), 2, 'properly moved bookmark');
+ assert.equal(bmsrv.getItemIndex(second.id), 0, 'other bookmarks adjusted');
+ assert.equal(bmsrv.getItemIndex(third.id), 1, 'other bookmarks adjusted');
+ done();
+ });
+ });
+};
+
+exports.testMovingChildrenNewFolder = function (assert, done) {
+ let topFolder = Group({ title: 'top', group: MENU });
+ let midFolder = Group({ title: 'middle', group: topFolder });
+ let newFolder = Group({ title: 'new', group: MENU });
+ let bookmarks = [
+ Bookmark({ title: 'moz1', url: 'http://moz1.com', group: midFolder}),
+ Bookmark({ title: 'moz2', url: 'http://moz2.com', group: midFolder}),
+ Bookmark({ title: 'moz3', url: 'http://moz3.com', group: midFolder})
+ ];
+ save(bookmarks).on('end', bms => {
+ let first = bms.filter(b => b.title === 'moz1')[0];
+ let second = bms.filter(b => b.title === 'moz2')[0];
+ let third = bms.filter(b => b.title === 'moz3')[0];
+ let definedMidFolder = first.group;
+ let definedNewFolder;
+ first.group = newFolder;
+ assert.equal(first.index, 0);
+ assert.equal(second.index, 1);
+ assert.equal(third.index, 2);
+ save(first).on('data', (data) => {
+ if (data.type === 'group') definedNewFolder = data;
+ }).on('end', (moved) => {
+ assert.equal(bmsrv.getItemIndex(second.id), 0, 'other bookmarks adjusted');
+ assert.equal(bmsrv.getItemIndex(third.id), 1, 'other bookmarks adjusted');
+ assert.equal(bmsrv.getItemIndex(first.id), 0, 'properly moved bookmark');
+ assert.equal(bmsrv.getFolderIdForItem(first.id), definedNewFolder.id,
+ 'bookmark has new parent');
+ assert.equal(bmsrv.getFolderIdForItem(second.id), definedMidFolder.id,
+ 'sibling bookmarks did not move');
+ assert.equal(bmsrv.getFolderIdForItem(third.id), definedMidFolder.id,
+ 'sibling bookmarks did not move');
+ done();
+ });
+ });
+};
+
+exports.testRemoveFunction = function (assert) {
+ let topFolder = Group({ title: 'new', group: MENU });
+ let midFolder = Group({ title: 'middle', group: topFolder });
+ let bookmarks = [
+ Bookmark({ title: 'moz1', url: 'http://moz1.com', group: midFolder}),
+ Bookmark({ title: 'moz2', url: 'http://moz2.com', group: midFolder}),
+ Bookmark({ title: 'moz3', url: 'http://moz3.com', group: midFolder})
+ ];
+ remove([midFolder, topFolder].concat(bookmarks)).map(item => {
+ assert.equal(item.remove, true, 'remove toggled `remove` property to true');
+ });
+};
+
+exports.testRemove = function (assert, done) {
+ let id;
+ createBookmarkItem().then(data => {
+ id = data.id;
+ compareWithHost(assert, data); // ensure bookmark exists
+ save(remove(data)).on('data', (res) => {
+ assert.pass('data event should be called');
+ assert.ok(!res, 'response should be empty');
+ }).on('end', () => {
+ assert.throws(function () {
+ bmsrv.getItemTitle(id);
+ }, 'item should no longer exist');
+ done();
+ });
+ }).catch(assert.fail);
+};
+
+/*
+ * Tests recursively removing children when removing a group
+ */
+exports.testRemoveAllChildren = function (assert, done) {
+ let topFolder = Group({ title: 'new', group: MENU });
+ let midFolder = Group({ title: 'middle', group: topFolder });
+ let bookmarks = [
+ Bookmark({ title: 'moz1', url: 'http://moz1.com', group: midFolder}),
+ Bookmark({ title: 'moz2', url: 'http://moz2.com', group: midFolder}),
+ Bookmark({ title: 'moz3', url: 'http://moz3.com', group: midFolder})
+ ];
+
+ let saved = [];
+ save(bookmarks).on('data', (data) => saved.push(data)).on('end', () => {
+ save(remove(topFolder)).on('end', () => {
+ assert.equal(saved.length, 5, 'all items should have been saved');
+ saved.map((item) => {
+ assert.throws(function () {
+ bmsrv.getItemTitle(item.id);
+ }, 'item should no longer exist');
+ });
+ done();
+ });
+ });
+};
+
+exports.testResolution = function (assert, done) {
+ let firstSave, secondSave;
+ createBookmarkItem().then((item) => {
+ firstSave = item;
+ assert.ok(item.updated, 'bookmark has updated time');
+ item.title = 'my title';
+ // Ensure delay so a different save time is set
+ return resolve(item);
+ }).then(saveP)
+ .then(items => {
+ let item = items[0];
+ secondSave = item;
+ assert.ok(firstSave.updated < secondSave.updated, 'snapshots have different update times');
+ firstSave.title = 'updated title';
+ return saveP(firstSave, { resolve: (mine, theirs) => {
+ assert.equal(mine.title, 'updated title', 'correct data for my object');
+ assert.equal(theirs.title, 'my title', 'correct data for their object');
+ assert.equal(mine.url, theirs.url, 'other data is equal');
+ assert.equal(mine.group, theirs.group, 'other data is equal');
+ assert.ok(mine !== firstSave, 'instance is not passed in');
+ assert.ok(theirs !== secondSave, 'instance is not passed in');
+ assert.equal(mine.toString(), '[object Object]', 'serialized objects');
+ assert.equal(theirs.toString(), '[object Object]', 'serialized objects');
+ mine.title = 'a new title';
+ return mine;
+ }});
+ }).then((results) => {
+ let result = results[0];
+ assert.equal(result.title, 'a new title', 'resolve handles results');
+ }).then(done).catch(assert.fail);
+};
+
+/*
+ * Same as the resolution test, but with the 'unsaved' snapshot
+ */
+exports.testResolutionMapping = function (assert, done) {
+ let bookmark = Bookmark({ title: 'moz', url: 'http://bookmarks4life.com/' });
+ let saved;
+
+ saveP(bookmark).then(data => {
+ saved = data[0];
+ saved.title = 'updated title';
+ // Ensure a delay for different updated times
+ return resolve(saved);
+ }).
+ then(saveP).
+ then(() => {
+ bookmark.title = 'conflicting title';
+ return saveP(bookmark, { resolve: (mine, theirs) => {
+ assert.equal(mine.title, 'conflicting title', 'correct data for my object');
+ assert.equal(theirs.title, 'updated title', 'correct data for their object');
+ assert.equal(mine.url, theirs.url, 'other data is equal');
+ assert.equal(mine.group, theirs.group, 'other data is equal');
+ assert.ok(mine !== bookmark, 'instance is not passed in');
+ assert.ok(theirs !== saved, 'instance is not passed in');
+ assert.equal(mine.toString(), '[object Object]', 'serialized objects');
+ assert.equal(theirs.toString(), '[object Object]', 'serialized objects');
+ mine.title = 'a new title';
+ return mine;
+ }});
+ }).then((results) => {
+ let result = results[0];
+ assert.equal(result.title, 'a new title', 'resolve handles results');
+ }).then(done).catch(assert.fail);
+};
+
+exports.testUpdateTags = function (assert, done) {
+ createBookmarkItem({ tags: ['spidermonkey'] }).then(bookmark => {
+ bookmark.tags.add('jagermonkey');
+ bookmark.tags.add('ionmonkey');
+ bookmark.tags.delete('spidermonkey');
+ save(bookmark).on('data', saved => {
+ assert.equal(saved.tags.size, 2, 'should have 2 tags');
+ assert.ok(saved.tags.has('jagermonkey'), 'should have added tag');
+ assert.ok(saved.tags.has('ionmonkey'), 'should have added tag');
+ assert.ok(!saved.tags.has('spidermonkey'), 'should not have removed tag');
+ done();
+ });
+ }).catch(assert.fail);
+};
+
+/*
+ * View `createBookmarkTree` in `./places-helper.js` to see
+ * expected tree construction
+ */
+
+exports.testSearchByGroupSimple = function (assert, done) {
+ createBookmarkTree().then(() => {
+ // In initial release of Places API, groups can only be queried
+ // via a 'simple query', which is one folder set, and no other
+ // parameters
+ return searchP({ group: UNSORTED });
+ }).then(results => {
+ let groups = results.filter(({type}) => type === 'group');
+ assert.equal(groups.length, 2, 'returns folders');
+ assert.equal(results.length, 7,
+ 'should return all bookmarks and folders under UNSORTED');
+ assert.equal(groups[0].toString(), '[object Group]', 'returns instance');
+ return searchP({
+ group: groups.filter(({title}) => title === 'mozgroup')[0]
+ });
+ }).then(results => {
+ let groups = results.filter(({type}) => type === 'group');
+ assert.equal(groups.length, 1, 'returns one subfolder');
+ assert.equal(results.length, 6,
+ 'returns all children bookmarks/folders');
+ assert.ok(results.filter(({url}) => url === 'http://w3schools.com/'),
+ 'returns nested children');
+ }).then(done).catch(assert.fail);
+};
+
+exports.testSearchByGroupComplex = function (assert, done) {
+ let mozgroup;
+ createBookmarkTree().then(results => {
+ mozgroup = results.filter(({title}) => title === 'mozgroup')[0];
+ return searchP({ group: mozgroup, query: 'javascript' });
+ }).then(results => {
+ assert.equal(results.length, 1, 'only one javascript result under mozgroup');
+ assert.equal(results[0].url, 'http://w3schools.com/', 'correct result');
+ return searchP({ group: mozgroup, url: '*.mozilla.org' });
+ }).then(results => {
+ assert.equal(results.length, 2, 'expected results');
+ assert.ok(
+ !results.filter(({url}) => /developer.mozilla/.test(url)).length,
+ 'does not find results from other folders');
+ }).then(done).catch(assert.fail);
+};
+
+exports.testSearchEmitters = function (assert, done) {
+ createBookmarkTree().then(() => {
+ let count = 0;
+ search({ tags: ['mozilla', 'firefox'] }).on('data', data => {
+ assert.ok(/mozilla|firefox/.test(data.title), 'one of the correct items');
+ assert.ok(data.tags.has('firefox'), 'has firefox tag');
+ assert.ok(data.tags.has('mozilla'), 'has mozilla tag');
+ assert.equal(data + '', '[object Bookmark]', 'returns bookmark');
+ count++;
+ }).on('end', data => {
+ assert.equal(count, 3, 'data event was called for each item');
+ assert.equal(data.length, 3,
+ 'should return two bookmarks that have both mozilla AND firefox');
+ assert.equal(data[0].title, 'mozilla.com', 'returns correct bookmark');
+ assert.equal(data[1].title, 'mozilla.org', 'returns correct bookmark');
+ assert.equal(data[2].title, 'firefox', 'returns correct bookmark');
+ assert.equal(data[0] + '', '[object Bookmark]', 'returns bookmarks');
+ done();
+ });
+ }).catch(assert.fail);
+};
+
+exports.testSearchTags = function (assert, done) {
+ createBookmarkTree().then(() => {
+ // AND tags
+ return searchP({ tags: ['mozilla', 'firefox'] });
+ }).then(data => {
+ assert.equal(data.length, 3,
+ 'should return two bookmarks that have both mozilla AND firefox');
+ assert.equal(data[0].title, 'mozilla.com', 'returns correct bookmark');
+ assert.equal(data[1].title, 'mozilla.org', 'returns correct bookmark');
+ assert.equal(data[2].title, 'firefox', 'returns correct bookmark');
+ assert.equal(data[0] + '', '[object Bookmark]', 'returns bookmarks');
+ return searchP([{tags: ['firefox']}, {tags: ['javascript']}]);
+ }).then(data => {
+ // OR tags
+ assert.equal(data.length, 6,
+ 'should return all bookmarks with firefox OR javascript tag');
+ }).then(done).catch(assert.fail);
+};
+
+/*
+ * Tests 4 scenarios
+ * '*.mozilla.com'
+ * 'mozilla.com'
+ * 'http://mozilla.com/'
+ * 'http://mozilla.com/*'
+ */
+exports.testSearchURLForBookmarks = function*(assert) {
+ yield createBookmarkTree()
+ let data = yield searchP({ url: 'mozilla.org' });
+
+ assert.equal(data.length, 2, 'only URLs with host domain');
+ assert.equal(data[0].url, 'http://mozilla.org/');
+ assert.equal(data[1].url, 'http://mozilla.org/thunderbird/');
+
+ data = yield searchP({ url: '*.mozilla.org' });
+
+ assert.equal(data.length, 3, 'returns domain and when host is other than domain');
+ assert.equal(data[0].url, 'http://mozilla.org/');
+ assert.equal(data[1].url, 'http://mozilla.org/thunderbird/');
+ assert.equal(data[2].url, 'http://developer.mozilla.org/en-US/');
+
+ data = yield searchP({ url: 'http://mozilla.org' });
+
+ assert.equal(data.length, 1, 'only exact URL match');
+ assert.equal(data[0].url, 'http://mozilla.org/');
+
+ data = yield searchP({ url: 'http://mozilla.org/*' });
+
+ assert.equal(data.length, 2, 'only URLs that begin with query');
+ assert.equal(data[0].url, 'http://mozilla.org/');
+ assert.equal(data[1].url, 'http://mozilla.org/thunderbird/');
+
+ data = yield searchP([{ url: 'mozilla.org' }, { url: 'component.fm' }]);
+
+ assert.equal(data.length, 3, 'returns URLs that match EITHER query');
+ assert.equal(data[0].url, 'http://mozilla.org/');
+ assert.equal(data[1].url, 'http://mozilla.org/thunderbird/');
+ assert.equal(data[2].url, 'http://component.fm/');
+};
+
+/*
+ * Searches url, title, tags
+ */
+exports.testSearchQueryForBookmarks = function*(assert) {
+ yield createBookmarkTree();
+
+ let data = yield searchP({ query: 'thunder' });
+ assert.equal(data.length, 3);
+ assert.equal(data[0].title, 'mozilla.com', 'query matches tag, url, or title');
+ assert.equal(data[1].title, 'mozilla.org', 'query matches tag, url, or title');
+ assert.equal(data[2].title, 'thunderbird', 'query matches tag, url, or title');
+
+ data = yield searchP([{ query: 'rust' }, { query: 'component' }]);
+ // rust OR component
+ assert.equal(data.length, 3);
+ assert.equal(data[0].title, 'mozilla.com', 'query matches tag, url, or title');
+ assert.equal(data[1].title, 'mozilla.org', 'query matches tag, url, or title');
+ assert.equal(data[2].title, 'web audio components', 'query matches tag, url, or title');
+
+ data = yield searchP([{ query: 'moz', tags: ['javascript']}]);
+ assert.equal(data.length, 1);
+ assert.equal(data[0].title, 'mdn',
+ 'only one item matches moz query AND has a javascript tag');
+};
+
+/*
+ * Test caching on bulk calls.
+ * Each construction of a bookmark item snapshot results in
+ * the recursive lookup of parent groups up to the root groups --
+ * ensure that the appropriate instances equal each other, and no duplicate
+ * fetches are called
+ *
+ * Implementation-dependent, this checks the host event `sdk-places-bookmarks-get`,
+ * and if implementation changes, this could increase or decrease
+ */
+
+exports.testCaching = function (assert, done) {
+ let count = 0;
+ let stream = filter(request, ({event}) =>
+ /sdk-places-bookmarks-get/.test(event));
+ on(stream, 'data', handle);
+
+ let group = { type: 'group', title: 'mozgroup' };
+ let bookmarks = [
+ { title: 'moz1', url: 'http://moz1.com', type: 'bookmark', group: group },
+ { title: 'moz2', url: 'http://moz2.com', type: 'bookmark', group: group },
+ { title: 'moz3', url: 'http://moz3.com', type: 'bookmark', group: group }
+ ];
+
+ /*
+ * Use timeout in tests since the platform calls are synchronous
+ * and the counting event shim may not have occurred yet
+ */
+
+ saveP(bookmarks).then(() => {
+ assert.equal(count, 0, 'all new items and root group, no fetches should occur');
+ count = 0;
+ return saveP([
+ { title: 'moz4', url: 'http://moz4.com', type: 'bookmark', group: group },
+ { title: 'moz5', url: 'http://moz5.com', type: 'bookmark', group: group }
+ ]);
+ // Test `save` look-up
+ }).then(() => {
+ assert.equal(count, 1, 'should only look up parent once');
+ count = 0;
+ return searchP({ query: 'moz' });
+ }).then(results => {
+ // Should query for each bookmark (5) from the query (id -> data),
+ // their parent during `construct` (1) and the root shouldn't
+ // require a lookup
+ assert.equal(count, 6, 'lookup occurs once for each item and parent');
+ off(stream, 'data', handle);
+ }).then(done).catch(assert.fail);
+
+ function handle ({data}) {
+ return count++;
+ }
+};
+
+/*
+ * Search Query Options
+ */
+
+exports.testSearchCount = function (assert, done) {
+ let max = 8;
+ createBookmarkTree()
+ .then(testCount(1))
+ .then(testCount(2))
+ .then(testCount(3))
+ .then(testCount(5))
+ .then(testCount(10))
+ .then(done)
+ .catch(assert.fail);
+
+ function testCount (n) {
+ return function () {
+ return searchP({}, { count: n }).then(results => {
+ if (n > max) n = max;
+ assert.equal(results.length, n,
+ 'count ' + n + ' returns ' + n + ' results');
+ });
+ };
+ }
+};
+
+exports.testSearchSortForBookmarks = function (assert, done) {
+ let urls = [
+ 'http://mozilla.com/', 'http://webaud.io/', 'http://mozilla.com/webfwd/',
+ 'http://developer.mozilla.com/', 'http://bandcamp.com/'
+ ];
+
+ saveP(
+ urls.map(url =>
+ Bookmark({ url: url, title: url.replace(/http:\/\/|\//g,'')}))
+ ).then(() => {
+ return searchP({}, { sort: 'title' });
+ }).then(results => {
+ checkOrder(results, [4,3,0,2,1]);
+ return searchP({}, { sort: 'title', descending: true });
+ }).then(results => {
+ checkOrder(results, [1,2,0,3,4]);
+ return searchP({}, { sort: 'url' });
+ }).then(results => {
+ checkOrder(results, [4,3,0,2,1]);
+ return searchP({}, { sort: 'url', descending: true });
+ }).then(results => {
+ checkOrder(results, [1,2,0,3,4]);
+ return addVisits(['http://mozilla.com/', 'http://mozilla.com']);
+ }).then(() =>
+ saveP(Bookmark({ url: 'http://github.com', title: 'github.com' }))
+ ).then(() => addVisits('http://bandcamp.com/'))
+ .then(() => searchP({ query: 'webfwd' }))
+ .then(results => {
+ results[0].title = 'new title for webfwd';
+ return saveP(results[0]);
+ })
+ .then(() =>
+ searchP({}, { sort: 'visitCount' })
+ ).then(results => {
+ assert.equal(results[5].url, 'http://mozilla.com/',
+ 'last entry is the highest visit count');
+ return searchP({}, { sort: 'visitCount', descending: true });
+ }).then(results => {
+ assert.equal(results[0].url, 'http://mozilla.com/',
+ 'first entry is the highest visit count');
+ return searchP({}, { sort: 'date' });
+ }).then(results => {
+ assert.equal(results[5].url, 'http://bandcamp.com/',
+ 'latest visited should be first');
+ return searchP({}, { sort: 'date', descending: true });
+ }).then(results => {
+ assert.equal(results[0].url, 'http://bandcamp.com/',
+ 'latest visited should be at the end');
+ return searchP({}, { sort: 'dateAdded' });
+ }).then(results => {
+ assert.equal(results[5].url, 'http://github.com/',
+ 'last added should be at the end');
+ return searchP({}, { sort: 'dateAdded', descending: true });
+ }).then(results => {
+ assert.equal(results[0].url, 'http://github.com/',
+ 'last added should be first');
+ return searchP({}, { sort: 'lastModified' });
+ }).then(results => {
+ assert.equal(results[5].url, 'http://mozilla.com/webfwd/',
+ 'last modified should be last');
+ return searchP({}, { sort: 'lastModified', descending: true });
+ }).then(results => {
+ assert.equal(results[0].url, 'http://mozilla.com/webfwd/',
+ 'last modified should be first');
+ }).then(done).catch(assert.fail);
+
+ function checkOrder (results, nums) {
+ assert.equal(results.length, nums.length, 'expected return count');
+ for (let i = 0; i < nums.length; i++) {
+ assert.equal(results[i].url, urls[nums[i]], 'successful order');
+ }
+ }
+};
+
+exports.testSearchComplexQueryWithOptions = function (assert, done) {
+ createBookmarkTree().then(() => {
+ return searchP([
+ { tags: ['rust'], url: '*.mozilla.org' },
+ { tags: ['javascript'], query: 'mozilla' }
+ ], { sort: 'title' });
+ }).then(results => {
+ let expected = [
+ 'http://developer.mozilla.org/en-US/',
+ 'http://mozilla.org/'
+ ];
+ for (let i = 0; i < expected.length; i++)
+ assert.equal(results[i].url, expected[i], 'correct ordering and item');
+ }).then(done).catch(assert.fail);
+};
+
+exports.testCheckSaveOrder = function (assert, done) {
+ let group = Group({ title: 'mygroup' });
+ let bookmarks = [
+ Bookmark({ url: 'http://url1.com', title: 'url1', group: group }),
+ Bookmark({ url: 'http://url2.com', title: 'url2', group: group }),
+ Bookmark({ url: 'http://url3.com', title: 'url3', group: group }),
+ Bookmark({ url: 'http://url4.com', title: 'url4', group: group }),
+ Bookmark({ url: 'http://url5.com', title: 'url5', group: group })
+ ];
+ saveP(bookmarks).then(results => {
+ for (let i = 0; i < bookmarks.length; i++)
+ assert.equal(results[i].url, bookmarks[i].url,
+ 'correct ordering of bookmark results');
+ }).then(done).catch(assert.fail);
+};
+
+before(exports, (name, assert, done) => resetPlaces(done));
+after(exports, (name, assert, done) => resetPlaces(done));
+
+function saveP () {
+ return promisedEmitter(save.apply(null, Array.prototype.slice.call(arguments)));
+}
+
+function searchP () {
+ return promisedEmitter(search.apply(null, Array.prototype.slice.call(arguments)));
+}
diff --git a/addon-sdk/source/test/addons/places/lib/test-places-events.js b/addon-sdk/source/test/addons/places/lib/test-places-events.js
new file mode 100644
index 000000000..3033f78d4
--- /dev/null
+++ b/addon-sdk/source/test/addons/places/lib/test-places-events.js
@@ -0,0 +1,328 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+module.metadata = {
+ 'engines': {
+ 'Firefox': '*'
+ }
+};
+
+const { Cc, Ci } = require('chrome');
+const { defer, all } = require('sdk/core/promise');
+const { filter } = require('sdk/event/utils');
+const { on, off } = require('sdk/event/core');
+const { events } = require('sdk/places/events');
+const { setTimeout } = require('sdk/timers');
+const { before, after } = require('sdk/test/utils');
+const bmsrv = Cc['@mozilla.org/browser/nav-bookmarks-service;1'].
+ getService(Ci.nsINavBookmarksService);
+const { release, platform } = require('node/os');
+
+const isOSX10_6 = (() => {
+ let vString = release();
+ return vString && /darwin/.test(platform()) && /10\.6/.test(vString);
+})();
+
+const { search } = require('sdk/places/history');
+const {
+ invalidResolve, createTree, createBookmark,
+ compareWithHost, addVisits, resetPlaces, createBookmarkItem,
+ removeVisits, historyBatch
+} = require('./places-helper');
+const { save, MENU, UNSORTED } = require('sdk/places/bookmarks');
+const { promisedEmitter } = require('sdk/places/utils');
+
+exports['test bookmark-item-added'] = function (assert, done) {
+ events.on('data', function handler ({type, data}) {
+ if (type !== 'bookmark-item-added') return;
+ if (data.title !== 'bookmark-added-title') return;
+ events.off('data', handler);
+
+ assert.equal(type, 'bookmark-item-added', 'correct type in bookmark-added event');
+ assert.equal(data.type, 'bookmark', 'correct data.type in bookmark-added event');
+ assert.ok(data.id != null, 'correct data.id in bookmark-added event');
+ assert.notEqual(data.parentId, null, 'correct data.parentId in bookmark-added event');
+ assert.ok(data.index >= 0, 'correct data.index in bookmark-added event');
+ assert.equal(data.url, 'http://moz.com/', 'correct data.url in bookmark-added event');
+ assert.notEqual(data.dateAdded, null, 'correct data.dateAdded in bookmark-added event');
+ done();
+ });
+ createBookmark({ title: 'bookmark-added-title' });
+};
+
+exports['test bookmark-item-changed'] = function (assert, done) {
+ let id;
+ let complete = makeCompleted(done);
+
+ // Due to bug 969616 and bug 971964, disabling tests in 10.6 (happens only
+ // in debug builds) to prevent intermittent failures
+ if (isOSX10_6) {
+ assert.pass('skipping test in OSX 10.6');
+ return done();
+ }
+
+ function handler ({type, data}) {
+ if (type !== 'bookmark-item-changed') return;
+ if (data.id !== id) return;
+ // Abort if the 'bookmark-item-changed' event isn't for the `title` property,
+ // as sometimes the event can be for the `url` property.
+ // Intermittent failure, bug 969616
+ if (data.property !== 'title') return;
+
+ assert.equal(type, 'bookmark-item-changed',
+ 'correct type in bookmark-item-changed event');
+ assert.equal(data.type, 'bookmark',
+ 'correct data in bookmark-item-changed event');
+ assert.equal(data.property, 'title',
+ 'correct property in bookmark-item-changed event');
+ assert.equal(data.value, 'bookmark-changed-title-2',
+ 'correct value in bookmark-item-changed event');
+ assert.ok(data.id === id, 'correct id in bookmark-item-changed event');
+ assert.ok(data.parentId != null, 'correct data in bookmark-added event');
+
+ events.off('data', handler);
+ complete();
+ }
+ events.on('data', handler);
+
+ createBookmarkItem({ title: 'bookmark-changed-title' }).then(item => {
+ id = item.id;
+ item.title = 'bookmark-changed-title-2';
+ return saveP(item);
+ }).then(complete).catch(assert.fail);
+};
+
+exports['test bookmark-item-moved'] = function (assert, done) {
+ let id;
+ let complete = makeCompleted(done);
+ let previousIndex, previousParentId;
+
+ // Due to bug 969616 and bug 971964, disabling tests in 10.6 (happens only
+ // in debug builds) to prevent intermittent failures
+ if (isOSX10_6) {
+ assert.ok(true, 'skipping test in OSX 10.6');
+ return done();
+ }
+
+ function handler ({type, data}) {
+ if (type !== 'bookmark-item-moved') return;
+ if (data.id !== id) return;
+ assert.equal(type, 'bookmark-item-moved',
+ 'correct type in bookmark-item-moved event');
+ assert.equal(data.type, 'bookmark',
+ 'correct data in bookmark-item-moved event');
+ assert.ok(data.id === id, 'correct id in bookmark-item-moved event');
+ assert.equal(data.previousParentId, previousParentId,
+ 'correct previousParentId');
+ assert.equal(data.currentParentId, bmsrv.getFolderIdForItem(id),
+ 'correct currentParentId');
+ assert.equal(data.previousIndex, previousIndex, 'correct previousIndex');
+ assert.equal(data.currentIndex, bmsrv.getItemIndex(id), 'correct currentIndex');
+
+ events.off('data', handler);
+ complete();
+ }
+ events.on('data', handler);
+
+ createBookmarkItem({
+ title: 'bookmark-moved-title',
+ group: UNSORTED
+ }).then(item => {
+ id = item.id;
+ previousIndex = bmsrv.getItemIndex(id);
+ previousParentId = bmsrv.getFolderIdForItem(id);
+ item.group = MENU;
+ return saveP(item);
+ }).then(complete).catch(assert.fail);
+};
+
+exports['test bookmark-item-removed'] = function (assert, done) {
+ let id;
+ let complete = makeCompleted(done);
+ function handler ({type, data}) {
+ if (type !== 'bookmark-item-removed') return;
+ if (data.id !== id) return;
+ assert.equal(type, 'bookmark-item-removed',
+ 'correct type in bookmark-item-removed event');
+ assert.equal(data.type, 'bookmark',
+ 'correct data in bookmark-item-removed event');
+ assert.ok(data.id === id, 'correct id in bookmark-item-removed event');
+ assert.equal(data.parentId, UNSORTED.id,
+ 'correct parentId in bookmark-item-removed');
+ assert.equal(data.url, 'http://moz.com/',
+ 'correct url in bookmark-item-removed event');
+ assert.equal(data.index, 0,
+ 'correct index in bookmark-item-removed event');
+
+ events.off('data', handler);
+ complete();
+ }
+ events.on('data', handler);
+
+ createBookmarkItem({
+ title: 'bookmark-item-remove-title',
+ group: UNSORTED
+ }).then(item => {
+ id = item.id;
+ item.remove = true;
+ return saveP(item);
+ }).then(complete).catch(assert.fail);
+};
+
+exports['test bookmark-item-visited'] = function (assert, done) {
+ let id;
+ let complete = makeCompleted(done);
+ function handler ({type, data}) {
+ if (type !== 'bookmark-item-visited') return;
+ if (data.id !== id) return;
+ assert.equal(type, 'bookmark-item-visited',
+ 'correct type in bookmark-item-visited event');
+ assert.ok(data.id === id, 'correct id in bookmark-item-visited event');
+ assert.equal(data.parentId, UNSORTED.id,
+ 'correct parentId in bookmark-item-visited');
+ assert.ok(data.transitionType != null,
+ 'has a transition type in bookmark-item-visited event');
+ assert.ok(data.time != null,
+ 'has a time in bookmark-item-visited event');
+ assert.ok(data.visitId != null,
+ 'has a visitId in bookmark-item-visited event');
+ assert.equal(data.url, 'http://bookmark-item-visited.com/',
+ 'correct url in bookmark-item-visited event');
+
+ events.off('data', handler);
+ complete();
+ }
+ events.on('data', handler);
+
+ createBookmarkItem({
+ title: 'bookmark-item-visited',
+ url: 'http://bookmark-item-visited.com/'
+ }).then(item => {
+ id = item.id;
+ return addVisits('http://bookmark-item-visited.com/');
+ }).then(complete).catch(assert.fail);
+};
+
+exports['test history-start-batch, history-end-batch, history-start-clear'] = function (assert, done) {
+ let complete = makeCompleted(done, 4);
+ let startEvent = filter(events, ({type}) => type === 'history-start-batch');
+ let endEvent = filter(events, ({type}) => type === 'history-end-batch');
+ let clearEvent = filter(events, ({type}) => type === 'history-start-clear');
+ function startHandler ({type, data}) {
+ assert.pass('history-start-batch called');
+ assert.equal(type, 'history-start-batch',
+ 'history-start-batch has correct type');
+ off(startEvent, 'data', startHandler);
+ on(endEvent, 'data', endHandler);
+ complete();
+ }
+ function endHandler ({type, data}) {
+ assert.pass('history-end-batch called');
+ assert.equal(type, 'history-end-batch',
+ 'history-end-batch has correct type');
+ off(endEvent, 'data', endHandler);
+ complete();
+ }
+ function clearHandler ({type, data}) {
+ assert.pass('history-start-clear called');
+ assert.equal(type, 'history-start-clear',
+ 'history-start-clear has correct type');
+ off(clearEvent, 'data', clearHandler);
+ complete();
+ }
+
+ on(startEvent, 'data', startHandler);
+ on(clearEvent, 'data', clearHandler);
+
+ historyBatch();
+ resetPlaces(complete);
+};
+
+exports['test history-visit, history-title-changed'] = function (assert, done) {
+ let complete = makeCompleted(() => {
+ off(titleEvents, 'data', titleHandler);
+ off(visitEvents, 'data', visitHandler);
+ done();
+ }, 6);
+ let visitEvents = filter(events, ({type}) => type === 'history-visit');
+ let titleEvents = filter(events, ({type}) => type === 'history-title-changed');
+
+ let urls = ['http://moz.com/', 'http://firefox.com/', 'http://mdn.com/'];
+
+ function visitHandler ({type, data}) {
+ assert.equal(type, 'history-visit', 'correct type in history-visit');
+ assert.ok(~urls.indexOf(data.url), 'history-visit has correct url');
+ assert.ok(data.visitId != null, 'history-visit has a visitId');
+ assert.ok(data.time != null, 'history-visit has a time');
+ assert.ok(data.sessionId != null, 'history-visit has a sessionId');
+ assert.ok(data.referringId != null, 'history-visit has a referringId');
+ assert.ok(data.transitionType != null, 'history-visit has a transitionType');
+ complete();
+ }
+
+ function titleHandler ({type, data}) {
+ assert.equal(type, 'history-title-changed',
+ 'correct type in history-title-changed');
+ assert.ok(~urls.indexOf(data.url),
+ 'history-title-changed has correct url');
+ assert.ok(data.title, 'history-title-changed has title');
+ complete();
+ }
+
+ on(titleEvents, 'data', titleHandler);
+ on(visitEvents, 'data', visitHandler);
+ addVisits(urls);
+}
+
+exports['test history-delete-url'] = function (assert, done) {
+ let complete = makeCompleted(() => {
+ events.off('data', handler);
+ done();
+ }, 3);
+ let urls = ['http://moz.com/', 'http://firefox.com/', 'http://mdn.com/'];
+ function handler({type, data}) {
+ if (type !== 'history-delete-url') return;
+ assert.equal(type, 'history-delete-url',
+ 'history-delete-url has correct type');
+ assert.ok(~urls.indexOf(data.url), 'history-delete-url has correct url');
+ complete();
+ }
+
+ events.on('data', handler);
+ addVisits(urls).then(() => {
+ removeVisits(urls);
+ });
+};
+
+exports['test history-page-changed'] = function (assert) {
+ assert.pass('history-page-changed tested in test-places-favicons');
+};
+
+exports['test history-delete-visits'] = function (assert) {
+ assert.pass('TODO test history-delete-visits');
+};
+
+// Bug 1060843
+// Wait a tick before finishing tests, as some bookmark activities require
+// completion of a result for events. For example, when creating a bookmark,
+// a `bookmark-item-added` event is fired, listened to by the first test here,
+// while constructing the bookmark item requires subsequent calls to that bookmark item.
+// If we destroy the underlying bookmark immediately, these calls will fail.
+//
+// The places SDK abstraction around this alleviates it, but these are low level events.
+after(exports, (name, assert, done) => setTimeout(() => resetPlaces(done), 1));
+before(exports, (name, assert, done) => resetPlaces(done));
+
+function saveP () {
+ return promisedEmitter(save.apply(null, Array.prototype.slice.call(arguments)));
+}
+
+function makeCompleted (done, countTo) {
+ let count = 0;
+ countTo = countTo || 2;
+ return function () {
+ if (++count === countTo) done();
+ };
+}
diff --git a/addon-sdk/source/test/addons/places/lib/test-places-favicon.js b/addon-sdk/source/test/addons/places/lib/test-places-favicon.js
new file mode 100644
index 000000000..669c66e64
--- /dev/null
+++ b/addon-sdk/source/test/addons/places/lib/test-places-favicon.js
@@ -0,0 +1,242 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+
+module.metadata = {
+ 'engines': {
+ 'Firefox': '*'
+ }
+};
+
+const { Cc, Ci, Cu } = require('chrome');
+const { getFavicon } = require('sdk/places/favicon');
+const tabs = require('sdk/tabs');
+const open = tabs.open;
+const port = 8099;
+const host = 'http://localhost:' + port;
+const { onFaviconChange, serve, binFavicon } = require('./favicon-helpers');
+const { once } = require('sdk/system/events');
+const { resetPlaces } = require('./places-helper');
+const faviconService = Cc["@mozilla.org/browser/favicon-service;1"].
+ getService(Ci.nsIFaviconService);
+
+exports.testStringGetFaviconCallbackSuccess = function*(assert) {
+ let name = 'callbacksuccess'
+ let srv = yield makeServer(name);
+ let url = host + '/' + name + '.html';
+ let favicon = host + '/' + name + '.ico';
+ let tab;
+
+ let wait = new Promise(resolve => {
+ onFaviconChange(url).then((faviconUrl) => {
+ getFavicon(url, (url) => {
+ assert.equal(favicon, url, 'Callback returns correct favicon url');
+ resolve();
+ });
+ });
+ });
+
+ assert.pass("Opening tab");
+
+ open({
+ url: url,
+ onOpen: (newTab) => tab = newTab,
+ inBackground: true
+ });
+
+ yield wait;
+
+ assert.pass("Complete");
+
+ yield complete(tab, srv);
+};
+
+exports.testStringGetFaviconCallbackFailure = function*(assert) {
+ let name = 'callbackfailure';
+ let srv = yield makeServer(name);
+ let url = host + '/' + name + '.html';
+ let tab;
+
+ let wait = waitAndExpire(url);
+
+ assert.pass("Opening tab");
+
+ open({
+ url: url,
+ onOpen: (newTab) => tab = newTab,
+ inBackground: true
+ });
+
+ yield wait;
+
+ assert.pass("Getting favicon");
+
+ yield new Promise(resolve => {
+ getFavicon(url, (url) => {
+ assert.equal(url, null, 'Callback returns null');
+ resolve();
+ });
+ });
+
+ assert.pass("Complete");
+
+ yield complete(tab, srv);
+};
+
+exports.testStringGetFaviconPromiseSuccess = function*(assert) {
+ let name = 'promisesuccess'
+ let srv = yield makeServer(name);
+ let url = host + '/' + name + '.html';
+ let favicon = host + '/' + name + '.ico';
+ let tab;
+
+ let wait = onFaviconChange(url);
+
+ assert.pass("Opening tab");
+
+ open({
+ url: url,
+ onOpen: (newTab) => tab = newTab,
+ inBackground: true
+ });
+
+ yield wait;
+
+ assert.pass("Getting favicon");
+
+ yield getFavicon(url).then((url) => {
+ assert.equal(url, favicon, 'Callback returns null');
+ }, () => {
+ assert.fail('Reject should not be called');
+ });
+
+ assert.pass("Complete");
+
+ yield complete(tab, srv);
+};
+
+exports.testStringGetFaviconPromiseFailure = function*(assert) {
+ let name = 'promisefailure'
+ let srv = yield makeServer(name);
+ let url = host + '/' + name + '.html';
+ let tab;
+
+ let wait = waitAndExpire(url);
+
+ assert.pass("Opening tab");
+
+ open({
+ url: url,
+ onOpen: (newTab) => tab = newTab,
+ inBackground: true
+ });
+
+ yield wait;
+
+ assert.pass("Getting favicon");
+
+ yield getFavicon(url).then(invalidResolve(assert), validReject(assert, 'expired url'));
+
+ assert.pass("Complete");
+
+ yield complete(tab, srv);
+};
+
+exports.testTabsGetFaviconPromiseSuccess = function*(assert) {
+ let name = 'tabs-success'
+ let srv = yield makeServer(name);
+ let url = host + '/' + name + '.html';
+ let favicon = host + '/' + name + '.ico';
+ let tab;
+
+ let iconPromise = onFaviconChange(url);
+
+ assert.pass("Opening tab");
+
+ open({
+ url: url,
+ onOpen: (newTab) => tab = newTab,
+ inBackground: true
+ });
+
+ yield iconPromise;
+
+ assert.pass("Getting favicon");
+
+ yield getFavicon(tab).then((url) => {
+ assert.equal(url, favicon, "getFavicon should return url for tab");
+ });
+
+ assert.pass("Complete");
+
+ yield complete(tab, srv);
+};
+
+
+exports.testTabsGetFaviconPromiseFailure = function*(assert) {
+ let name = 'tabs-failure'
+ let srv = yield makeServer(name);
+ let url = host + '/' + name + '.html';
+ let tab;
+
+ let wait = waitAndExpire(url);
+
+ assert.pass("Opening tab");
+
+ open({
+ url: url,
+ onOpen: (newTab) => tab = newTab,
+ inBackground: true
+ });
+
+ yield wait;
+
+ assert.pass("Getting favicon");
+
+ yield getFavicon(tab).then(invalidResolve(assert), validReject(assert, 'expired tab'));
+
+ assert.pass("Complete");
+
+ yield complete(tab, srv);
+};
+
+exports.testRejects = function*(assert) {
+ yield getFavicon({})
+ .then(invalidResolve(assert), validReject(assert, 'Object'));
+
+ yield getFavicon(null)
+ .then(invalidResolve(assert), validReject(assert, 'null'));
+
+ yield getFavicon(undefined)
+ .then(invalidResolve(assert), validReject(assert, 'undefined'));
+
+ yield getFavicon([])
+ .then(invalidResolve(assert), validReject(assert, 'Array'));
+};
+
+var invalidResolve = (assert) => () => assert.fail('Promise should not be resolved successfully');
+var validReject = (assert, name) => () => assert.pass(name + ' correctly rejected');
+
+var makeServer = (name) => serve({
+ name: name,
+ favicon: binFavicon,
+ port: port,
+ host: host
+});
+
+var waitAndExpire = (url) => new Promise(resolve => {
+ onFaviconChange(url).then(() => {
+ once('places-favicons-expired', resolve);
+ faviconService.expireAllFavicons();
+ });
+});
+
+var complete = (tab, srv) => new Promise(resolve => {
+ tab.close(() => {
+ resetPlaces(() => {
+ srv.stop(resolve);
+ });
+ });
+});
diff --git a/addon-sdk/source/test/addons/places/lib/test-places-history.js b/addon-sdk/source/test/addons/places/lib/test-places-history.js
new file mode 100644
index 000000000..0a1e2b8cc
--- /dev/null
+++ b/addon-sdk/source/test/addons/places/lib/test-places-history.js
@@ -0,0 +1,244 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+module.metadata = {
+ 'engines': {
+ 'Firefox': '*'
+ }
+};
+
+const { Cc, Ci } = require('chrome');
+const { defer, all } = require('sdk/core/promise');
+const { has } = require('sdk/util/array');
+const { setTimeout } = require('sdk/timers');
+const { before, after } = require('sdk/test/utils');
+const { set } = require('sdk/preferences/service');
+const {
+ search
+} = require('sdk/places/history');
+const {
+ invalidResolve, createTree,
+ compareWithHost, addVisits, resetPlaces
+} = require('./places-helper');
+const { promisedEmitter } = require('sdk/places/utils');
+
+exports.testEmptyQuery = function*(assert) {
+ let within = toBeWithin();
+ yield addVisits([
+ 'http://simplequery-1.com', 'http://simplequery-2.com'
+ ]);
+
+ let results = yield searchP();
+ assert.equal(results.length, 2, 'Correct number of entries returned');
+ assert.equal(results[0].url, 'http://simplequery-1.com/',
+ 'matches url');
+ assert.equal(results[1].url, 'http://simplequery-2.com/',
+ 'matches url');
+ assert.equal(results[0].title, 'Test visit for ' + results[0].url,
+ 'title matches');
+ assert.equal(results[1].title, 'Test visit for ' + results[1].url,
+ 'title matches');
+ assert.equal(results[0].visitCount, 1, 'matches access');
+ assert.equal(results[1].visitCount, 1, 'matches access');
+ assert.ok(within(results[0].time), 'accurate access time');
+ assert.ok(within(results[1].time), 'accurate access time');
+ assert.equal(Object.keys(results[0]).length, 4,
+ 'no addition exposed properties on history result');
+};
+
+exports.testVisitCount = function*(assert) {
+ yield addVisits([
+ 'http://simplequery-1.com', 'http://simplequery-1.com',
+ 'http://simplequery-1.com', 'http://simplequery-1.com'
+ ]);
+ let results = yield searchP();
+ assert.equal(results.length, 1, 'Correct number of entries returned');
+ assert.equal(results[0].url, 'http://simplequery-1.com/', 'correct url');
+ assert.equal(results[0].visitCount, 4, 'matches access count');
+};
+
+/*
+ * Tests 4 scenarios
+ * '*.mozilla.org'
+ * 'mozilla.org'
+ * 'http://mozilla.org/'
+ * 'http://mozilla.org/*'
+ */
+exports.testSearchURLForHistory = function*(assert) {
+ yield addVisits([
+ 'http://developer.mozilla.org', 'http://mozilla.org',
+ 'http://mozilla.org/index', 'https://mozilla.org'
+ ]);
+
+ let results = yield searchP({ url: 'http://mozilla.org/' });
+ assert.equal(results.length, 1, 'should just be an exact match');
+
+ results = yield searchP({ url: '*.mozilla.org' });
+ assert.equal(results.length, 4, 'returns all entries');
+
+ results = yield searchP({ url: 'mozilla.org' });
+ assert.equal(results.length, 3, 'returns entries where mozilla.org is host');
+
+ results = yield searchP({ url: 'http://mozilla.org/*' });
+ assert.equal(results.length, 2, 'should match anything starting with substring');
+};
+
+// Disabling due to intermittent Bug 892619
+// TODO solve this
+/*
+exports.testSearchTimeRange = function (assert, done) {
+ let firstTime, secondTime;
+ addVisits([
+ 'http://earlyvisit.org', 'http://earlyvisit.org/earlytown.html'
+ ]).then(searchP).then(results => {
+ firstTime = results[0].time;
+ var deferred = defer();
+ setTimeout(() => deferred.resolve(), 1000);
+ return deferred.promise;
+ }).then(() => {
+ return addVisits(['http://newvisit.org', 'http://newvisit.org/whoawhoa.html']);
+ }).then(searchP).then(results => {
+ results.filter(({url, time}) => {
+ if (/newvisit/.test(url)) secondTime = time;
+ });
+ return searchP({ from: firstTime - 1000 });
+ }).then(results => {
+ assert.equal(results.length, 4, 'should return all entries');
+ return searchP({ to: firstTime + 500 });
+ }).then(results => {
+ assert.equal(results.length, 2, 'should return only first entries');
+ results.map(item => {
+ assert.ok(/earlyvisit/.test(item.url), 'correct entry');
+ });
+ return searchP({ from: firstTime + 500 });
+ }).then(results => {
+ assert.equal(results.length, 2, 'should return only last entries');
+ results.map(item => {
+ assert.ok(/newvisit/.test(item.url), 'correct entry');
+ });
+ done();
+ });
+};
+*/
+exports.testSearchQueryForHistory = function*(assert) {
+ yield addVisits([
+ 'http://mozilla.com', 'http://webaud.io', 'http://mozilla.com/webfwd'
+ ]);
+
+ let results = yield searchP({ query: 'moz' });
+ assert.equal(results.length, 2, 'should return urls that match substring');
+ results.map(({url}) => {
+ assert.ok(/moz/.test(url), 'correct item');
+ });
+
+ results = yield searchP([{ query: 'webfwd' }, { query: 'aud.io' }]);
+ assert.equal(results.length, 2, 'should OR separate queries');
+ results.map(({url}) => {
+ assert.ok(/webfwd|aud\.io/.test(url), 'correct item');
+ });
+};
+
+/*
+ * Query Options
+ */
+
+exports.testSearchCount = function (assert, done) {
+ addVisits([
+ 'http://mozilla.com', 'http://webaud.io', 'http://mozilla.com/webfwd',
+ 'http://developer.mozilla.com', 'http://bandcamp.com'
+ ]).then(testCount(1))
+ .then(testCount(2))
+ .then(testCount(3))
+ .then(testCount(5))
+ .then(done);
+
+ function testCount (n) {
+ return function () {
+ return searchP({}, { count: n }).then(results => {
+ assert.equal(results.length, n,
+ 'count ' + n + ' returns ' + n + ' results');
+ });
+ };
+ }
+};
+
+exports.testSearchSortForHistory = function*(assert) {
+ function checkOrder (results, nums) {
+ assert.equal(results.length, nums.length, 'expected return count');
+ for (let i = 0; i < nums.length; i++) {
+ assert.equal(results[i].url, places[nums[i]], 'successful order');
+ }
+ }
+
+ let places = [
+ 'http://mozilla.com/', 'http://webaud.io/', 'http://mozilla.com/webfwd/',
+ 'http://developer.mozilla.com/', 'http://bandcamp.com/'
+ ];
+ yield addVisits(places);
+
+ let results = yield searchP({}, { sort: 'title' });
+ checkOrder(results, [4,3,0,2,1]);
+
+ results = yield searchP({}, { sort: 'title', descending: true });
+ checkOrder(results, [1,2,0,3,4]);
+
+ results = yield searchP({}, { sort: 'url' });
+ checkOrder(results, [4,3,0,2,1]);
+
+ results = yield searchP({}, { sort: 'url', descending: true });
+ checkOrder(results, [1,2,0,3,4]);
+
+ yield addVisits('http://mozilla.com'); // for visit conut
+ yield addVisits('http://github.com'); // for checking date
+
+ results = yield searchP({}, { sort: 'visitCount' });
+ assert.equal(results[5].url, 'http://mozilla.com/',
+ 'last entry is the highest visit count');
+
+ results = yield searchP({}, { sort: 'visitCount', descending: true });
+ assert.equal(results[0].url, 'http://mozilla.com/',
+ 'first entry is the highest visit count');
+
+ results = yield searchP({}, { sort: 'date' });
+ assert.equal(results[5].url, 'http://github.com/',
+ 'latest visited should be first');
+
+ results = yield searchP({}, { sort: 'date', descending: true });
+ assert.equal(results[0].url, 'http://github.com/',
+ 'latest visited should be at the end');
+};
+
+exports.testEmitters = function (assert, done) {
+ let urls = [
+ 'http://mozilla.com/', 'http://webaud.io/', 'http://mozilla.com/webfwd/',
+ 'http://developer.mozilla.com/', 'http://bandcamp.com/'
+ ];
+ addVisits(urls).then(() => {
+ let count = 0;
+ search().on('data', item => {
+ assert.ok(~urls.indexOf(item.url), 'data value found in url list');
+ count++;
+ }).on('end', results => {
+ assert.equal(results.length, 5, 'correct count of items');
+ assert.equal(count, 5, 'data event called 5 times');
+ done();
+ });
+ });
+};
+
+function toBeWithin (range) {
+ range = range || 2000;
+ var current = new Date() * 1000; // convert to microseconds
+ return compared => {
+ return compared - current < range;
+ };
+}
+
+function searchP () {
+ return promisedEmitter(search.apply(null, Array.prototype.slice.call(arguments)));
+}
+
+before(exports, (name, assert, done) => resetPlaces(done));
+after(exports, (name, assert, done) => resetPlaces(done));
diff --git a/addon-sdk/source/test/addons/places/lib/test-places-host.js b/addon-sdk/source/test/addons/places/lib/test-places-host.js
new file mode 100644
index 000000000..3d0b2b3f4
--- /dev/null
+++ b/addon-sdk/source/test/addons/places/lib/test-places-host.js
@@ -0,0 +1,301 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+module.metadata = {
+ 'engines': {
+ 'Firefox': '*'
+ }
+};
+
+const { Cc, Ci } = require('chrome');
+const { defer, all } = require('sdk/core/promise');
+const { setTimeout } = require('sdk/timers');
+const { newURI } = require('sdk/url/utils');
+const { send } = require('sdk/addon/events');
+const { set } = require('sdk/preferences/service');
+const { before, after } = require('sdk/test/utils');
+
+require('sdk/places/host/host-bookmarks');
+require('sdk/places/host/host-tags');
+require('sdk/places/host/host-query');
+const {
+ invalidResolve, createTree,
+ compareWithHost, createBookmark, createBookmarkTree, resetPlaces
+} = require('./places-helper');
+
+const bmsrv = Cc['@mozilla.org/browser/nav-bookmarks-service;1'].
+ getService(Ci.nsINavBookmarksService);
+const hsrv = Cc['@mozilla.org/browser/nav-history-service;1'].
+ getService(Ci.nsINavHistoryService);
+const tagsrv = Cc['@mozilla.org/browser/tagging-service;1'].
+ getService(Ci.nsITaggingService);
+
+exports.testBookmarksCreate = function*(assert) {
+ let items = [{
+ title: 'my title',
+ url: 'http://test-places-host.com/testBookmarksCreate/',
+ tags: ['some', 'tags', 'yeah'],
+ type: 'bookmark'
+ }, {
+ title: 'my folder',
+ type: 'group',
+ group: bmsrv.bookmarksMenuFolder
+ }, {
+ type: 'separator',
+ group: bmsrv.unfiledBookmarksFolder
+ }];
+
+ yield all(items.map((item) => {
+ return send('sdk-places-bookmarks-create', item).then((data) => {
+ compareWithHost(assert, data);
+ });
+ }));
+};
+
+exports.testBookmarksCreateFail = function (assert, done) {
+ let items = [{
+ title: 'my title',
+ url: 'not-a-url',
+ type: 'bookmark'
+ }, {
+ type: 'group',
+ group: bmsrv.bookmarksMenuFolder
+ }, {
+ group: bmsrv.unfiledBookmarksFolder
+ }];
+ all(items.map(function (item) {
+ return send('sdk-places-bookmarks-create', item).then(null, function (reason) {
+ assert.ok(reason, 'bookmark create should fail');
+ });
+ })).then(done);
+};
+
+exports.testBookmarkLastUpdated = function (assert, done) {
+ let timestamp;
+ let item;
+ createBookmark({
+ url: 'http://test-places-host.com/testBookmarkLastUpdated'
+ }).then(function (data) {
+ item = data;
+ timestamp = item.updated;
+ return send('sdk-places-bookmarks-last-updated', { id: item.id });
+ }).then(function (updated) {
+ let { resolve, promise } = defer();
+ assert.equal(timestamp, updated, 'should return last updated time');
+ item.title = 'updated mozilla';
+ setTimeout(() => {
+ resolve(send('sdk-places-bookmarks-save', item));
+ }, 100);
+ return promise;
+ }).then(function (data) {
+ assert.ok(data.updated > timestamp, 'time has elapsed and updated the updated property');
+ done();
+ });
+};
+
+exports.testBookmarkRemove = function (assert, done) {
+ let id;
+ createBookmark({
+ url: 'http://test-places-host.com/testBookmarkRemove/'
+ }).then(function (data) {
+ id = data.id;
+ compareWithHost(assert, data); // ensure bookmark exists
+ bmsrv.getItemTitle(id); // does not throw an error
+ return send('sdk-places-bookmarks-remove', data);
+ }).then(function () {
+ assert.throws(function () {
+ bmsrv.getItemTitle(id);
+ }, 'item should no longer exist');
+ done();
+ }, assert.fail);
+};
+
+exports.testBookmarkGet = function (assert, done) {
+ let bookmark;
+ createBookmark({
+ url: 'http://test-places-host.com/testBookmarkGet/'
+ }).then(function (data) {
+ bookmark = data;
+ return send('sdk-places-bookmarks-get', { id: data.id });
+ }).then(function (data) {
+ 'title url index group updated type tags'.split(' ').map(function (prop) {
+ if (prop === 'tags') {
+ for (let tag of bookmark.tags) {
+ assert.ok(~data.tags.indexOf(tag),
+ 'correctly fetched tag ' + tag);
+ }
+ assert.equal(bookmark.tags.length, data.tags.length,
+ 'same amount of tags');
+ }
+ else
+ assert.equal(bookmark[prop], data[prop], 'correctly fetched ' + prop);
+ });
+ done();
+ });
+};
+
+exports.testTagsTag = function (assert, done) {
+ let url;
+ createBookmark({
+ url: 'http://test-places-host.com/testTagsTag/',
+ }).then(function (data) {
+ url = data.url;
+ return send('sdk-places-tags-tag', {
+ url: data.url, tags: ['mozzerella', 'foxfire']
+ });
+ }).then(function () {
+ let tags = tagsrv.getTagsForURI(newURI(url));
+ assert.ok(~tags.indexOf('mozzerella'), 'first tag found');
+ assert.ok(~tags.indexOf('foxfire'), 'second tag found');
+ assert.ok(~tags.indexOf('firefox'), 'default tag found');
+ assert.equal(tags.length, 3, 'no extra tags');
+ done();
+ });
+};
+
+exports.testTagsUntag = function (assert, done) {
+ let item;
+ createBookmark({
+ url: 'http://test-places-host.com/testTagsUntag/',
+ tags: ['tag1', 'tag2', 'tag3']
+ }).then(data => {
+ item = data;
+ return send('sdk-places-tags-untag', {
+ url: item.url,
+ tags: ['tag2', 'firefox']
+ });
+ }).then(function () {
+ let tags = tagsrv.getTagsForURI(newURI(item.url));
+ assert.ok(~tags.indexOf('tag1'), 'first tag persisted');
+ assert.ok(~tags.indexOf('tag3'), 'second tag persisted');
+ assert.ok(!~tags.indexOf('firefox'), 'first tag removed');
+ assert.ok(!~tags.indexOf('tag2'), 'second tag removed');
+ assert.equal(tags.length, 2, 'no extra tags');
+ done();
+ });
+};
+
+exports.testTagsGetURLsByTag = function (assert, done) {
+ let item;
+ createBookmark({
+ url: 'http://test-places-host.com/testTagsGetURLsByTag/'
+ }).then(function (data) {
+ item = data;
+ return send('sdk-places-tags-get-urls-by-tag', {
+ tag: 'firefox'
+ });
+ }).then(function(urls) {
+ assert.equal(item.url, urls[0], 'returned correct url');
+ assert.equal(urls.length, 1, 'returned only one url');
+ done();
+ });
+};
+
+exports.testTagsGetTagsByURL = function (assert, done) {
+ let item;
+ createBookmark({
+ url: 'http://test-places-host.com/testTagsGetURLsByTag/',
+ tags: ['firefox', 'mozilla', 'metal']
+ }).then(function (data) {
+ item = data;
+ return send('sdk-places-tags-get-tags-by-url', {
+ url: data.url,
+ });
+ }).then(function(tags) {
+ assert.ok(~tags.indexOf('firefox'), 'returned first tag');
+ assert.ok(~tags.indexOf('mozilla'), 'returned second tag');
+ assert.ok(~tags.indexOf('metal'), 'returned third tag');
+ assert.equal(tags.length, 3, 'returned all tags');
+ done();
+ });
+};
+
+exports.testHostQuery = function (assert, done) {
+ all([
+ createBookmark({
+ url: 'http://firefox.com/testHostQuery/',
+ tags: ['firefox', 'mozilla']
+ }),
+ createBookmark({
+ url: 'http://mozilla.com/testHostQuery/',
+ tags: ['mozilla']
+ }),
+ createBookmark({ url: 'http://thunderbird.com/testHostQuery/' })
+ ]).then(data => {
+ return send('sdk-places-query', {
+ queries: { tags: ['mozilla'] },
+ options: { sortingMode: 6, queryType: 1 } // sort by URI ascending, bookmarks only
+ });
+ }).then(results => {
+ assert.equal(results.length, 2, 'should only return two');
+ assert.equal(results[0].url,
+ 'http://mozilla.com/testHostQuery/', 'is sorted by URI asc');
+ return send('sdk-places-query', {
+ queries: { tags: ['mozilla'] },
+ options: { sortingMode: 5, queryType: 1 } // sort by URI descending, bookmarks only
+ });
+ }).then(results => {
+ assert.equal(results.length, 2, 'should only return two');
+ assert.equal(results[0].url,
+ 'http://firefox.com/testHostQuery/', 'is sorted by URI desc');
+ done();
+ });
+};
+
+exports.testHostMultiQuery = function (assert, done) {
+ all([
+ createBookmark({
+ url: 'http://firefox.com/testHostMultiQuery/',
+ tags: ['firefox', 'mozilla']
+ }),
+ createBookmark({
+ url: 'http://mozilla.com/testHostMultiQuery/',
+ tags: ['mozilla']
+ }),
+ createBookmark({ url: 'http://thunderbird.com/testHostMultiQuery/' })
+ ]).then(data => {
+ return send('sdk-places-query', {
+ queries: [{ tags: ['firefox'] }, { uri: 'http://thunderbird.com/testHostMultiQuery/' }],
+ options: { sortingMode: 5, queryType: 1 } // sort by URI descending, bookmarks only
+ });
+ }).then(results => {
+ assert.equal(results.length, 2, 'should return 2 results ORing queries');
+ assert.equal(results[0].url,
+ 'http://firefox.com/testHostMultiQuery/', 'should match URL or tag');
+ assert.equal(results[1].url,
+ 'http://thunderbird.com/testHostMultiQuery/', 'should match URL or tag');
+ return send('sdk-places-query', {
+ queries: [{ tags: ['firefox'], url: 'http://mozilla.com/testHostMultiQuery/' }],
+ options: { sortingMode: 5, queryType: 1 } // sort by URI descending, bookmarks only
+ });
+ }).then(results => {
+ assert.equal(results.length, 0, 'query props should be AND\'d');
+ done();
+ });
+};
+
+exports.testGetAllBookmarks = function (assert, done) {
+ createBookmarkTree().then(() => {
+ return send('sdk-places-bookmarks-get-all', {});
+ }).then(res => {
+ assert.equal(res.length, 8, 'all bookmarks returned');
+ done();
+ }, assert.fail);
+};
+
+exports.testGetAllChildren = function (assert, done) {
+ createBookmarkTree().then(results => {
+ return send('sdk-places-bookmarks-get-children', {
+ id: results.filter(({title}) => title === 'mozgroup')[0].id
+ });
+ }).then(results => {
+ assert.equal(results.length, 5,
+ 'should return all children and folders at a single depth');
+ done();
+ });
+};
+
+before(exports, (name, assert, done) => resetPlaces(done));
+after(exports, (name, assert, done) => resetPlaces(done));
diff --git a/addon-sdk/source/test/addons/places/lib/test-places-utils.js b/addon-sdk/source/test/addons/places/lib/test-places-utils.js
new file mode 100644
index 000000000..c909a2cbb
--- /dev/null
+++ b/addon-sdk/source/test/addons/places/lib/test-places-utils.js
@@ -0,0 +1,78 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+module.metadata = {
+ 'engines': {
+ 'Firefox': '*'
+ }
+};
+
+const { defer, all } = require('sdk/core/promise');
+const { setTimeout } = require('sdk/timers');
+const { TreeNode } = require('sdk/places/utils');
+
+exports['test construct tree'] = function (assert) {
+ let tree = TreeNode(1);
+ tree.add([2, 3, 4]);
+ tree.get(2).add([2.1, 2.2, 2.3]);
+ let newTreeNode = TreeNode(4.3);
+ newTreeNode.add([4.31, 4.32]);
+ tree.get(4).add([4.1, 4.2, newTreeNode]);
+
+ assert.equal(tree.get(2).value, 2, 'get returns node with correct value');
+ assert.equal(tree.get(2.3).value, 2.3, 'get returns node with correct value');
+ assert.equal(tree.get(4.32).value, 4.32, 'get returns node even if created from nested node');
+ assert.equal(tree.get(4).children.length, 3, 'nodes have correct children length');
+ assert.equal(tree.get(3).children.length, 0, 'nodes have correct children length');
+
+ assert.equal(tree.get(4).get(4.32).value, 4.32, 'node.get descends from itself');
+ assert.equal(tree.get(4).get(2), null, 'node.get descends from itself fails if not descendant');
+};
+
+exports['test walk'] = function (assert, done) {
+ let resultsAll = [];
+ let resultsNode = [];
+ let tree = TreeNode(1);
+ tree.add([2, 3, 4]);
+ tree.get(2).add([2.1, 2.2]);
+
+ tree.walk(function (node) {
+ resultsAll.push(node.value);
+ }).then(() => {
+ [1, 2, 2.1, 2.2, 3, 4].forEach(num => {
+ assert.ok(~resultsAll.indexOf(num), 'function applied to each node from root');
+ });
+ return tree.get(2).walk(node => resultsNode.push(node.value));
+ }).then(() => {
+ [2, 2.1, 2.2].forEach(function (num) {
+ assert.ok(~resultsNode.indexOf(num), 'function applied to each node from node');
+ });
+ }).catch(assert.fail).then(done);
+};
+
+exports['test async walk'] = function (assert, done) {
+ let resultsAll = [];
+ let tree = TreeNode(1);
+ tree.add([2, 3, 4]);
+ tree.get(2).add([2.1, 2.2]);
+
+ tree.walk(function (node) {
+ let deferred = defer();
+ setTimeout(function () {
+ resultsAll.push(node.value);
+ deferred.resolve(node.value);
+ }, node.value === 2 ? 50 : 5);
+ return deferred.promise;
+ }).then(function () {
+ [1, 2, 2.1, 2.2, 3, 4].forEach(function (num) {
+ assert.ok(~resultsAll.indexOf(num), 'function applied to each node from root');
+ });
+ assert.ok(resultsAll.indexOf(2) < resultsAll.indexOf(2.1),
+ 'child should wait for parent to complete');
+ assert.ok(resultsAll.indexOf(2) < resultsAll.indexOf(2.2),
+ 'child should wait for parent to complete');
+ done();
+ });
+};
diff --git a/addon-sdk/source/test/addons/places/package.json b/addon-sdk/source/test/addons/places/package.json
new file mode 100644
index 000000000..97ce28c88
--- /dev/null
+++ b/addon-sdk/source/test/addons/places/package.json
@@ -0,0 +1,5 @@
+{
+ "id": "test-places@jetpack",
+ "main": "./lib/main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/predefined-id-with-at/lib/main.js b/addon-sdk/source/test/addons/predefined-id-with-at/lib/main.js
new file mode 100644
index 000000000..12748f4eb
--- /dev/null
+++ b/addon-sdk/source/test/addons/predefined-id-with-at/lib/main.js
@@ -0,0 +1,32 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { id, preferencesBranch } = require('sdk/self');
+const simple = require('sdk/simple-prefs');
+const service = require('sdk/preferences/service');
+const { getAddonByID } = require('sdk/addon/manager');
+
+const expected_id = 'predefined-id@test';
+
+exports.testExpectedID = function(assert) {
+ assert.equal(id, expected_id, 'ID is as expected');
+ assert.equal(preferencesBranch, expected_id, 'preferences-branch is ' + expected_id);
+
+ assert.equal(simple.prefs.test, 5, 'test pref is 5');
+
+ simple.prefs.test2 = '25';
+ assert.equal(service.get('extensions.'+expected_id+'.test2'), '25', 'test pref is 25');
+ assert.equal(service.get('extensions.'+expected_id+'.test2'), simple.prefs.test2, 'test pref is 25');
+}
+
+exports.testSelfID = function*(assert) {
+ assert.equal(typeof(id), 'string', 'self.id is a string');
+ assert.ok(id.length > 0, 'self.id not empty');
+
+ let addon = yield getAddonByID(id);
+ assert.equal(addon.id, id, 'found addon with self.id');
+}
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/predefined-id-with-at/package.json b/addon-sdk/source/test/addons/predefined-id-with-at/package.json
new file mode 100644
index 000000000..c20d8715b
--- /dev/null
+++ b/addon-sdk/source/test/addons/predefined-id-with-at/package.json
@@ -0,0 +1,13 @@
+{
+ "id": "predefined-id@test",
+ "fullName": "predefined ID test",
+ "author": "Erik Vold",
+ "preferences": [{
+ "name": "test",
+ "type": "integer",
+ "title": "test",
+ "value": 5
+ }],
+ "main": "./lib/main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/preferences-branch/lib/main.js b/addon-sdk/source/test/addons/preferences-branch/lib/main.js
new file mode 100644
index 000000000..659a57e92
--- /dev/null
+++ b/addon-sdk/source/test/addons/preferences-branch/lib/main.js
@@ -0,0 +1,28 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { id, preferencesBranch } = require('sdk/self');
+const simple = require('sdk/simple-prefs');
+const service = require('sdk/preferences/service');
+const { getAddonByID } = require('sdk/addon/manager');
+
+exports.testPreferencesBranch = function(assert) {
+ assert.equal(preferencesBranch, 'human-readable', 'preferencesBranch is human-readable');
+ assert.equal(simple.prefs.test42, true, 'test42 is true');
+
+ simple.prefs.test43 = 'movie';
+ assert.equal(service.get('extensions.human-readable.test43'), 'movie', 'test43 is a movie');
+
+}
+
+// from `/test/test-self.js`, adapted to `sdk/test/assert` API
+exports.testSelfID = function*(assert) {
+ assert.equal(typeof(id), 'string', 'self.id is a string');
+ assert.ok(id.length > 0, 'self.id not empty');
+ let addon = yield getAddonByID(id);
+ assert.ok(addon, 'found addon with self.id');
+}
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/preferences-branch/package.json b/addon-sdk/source/test/addons/preferences-branch/package.json
new file mode 100644
index 000000000..8e9858558
--- /dev/null
+++ b/addon-sdk/source/test/addons/preferences-branch/package.json
@@ -0,0 +1,14 @@
+{
+ "id": "test-preferences-branch@jetpack",
+ "fullName": "preferences-branch test",
+ "author": "Tomislav Jovanovic",
+ "preferences": [{
+ "name": "test42",
+ "type": "bool",
+ "title": "test42",
+ "value": true
+ }],
+ "preferences-branch": "human-readable",
+ "main": "./lib/main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/private-browsing-supported/main.js b/addon-sdk/source/test/addons/private-browsing-supported/main.js
new file mode 100644
index 000000000..290427dc2
--- /dev/null
+++ b/addon-sdk/source/test/addons/private-browsing-supported/main.js
@@ -0,0 +1,28 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { merge } = require('sdk/util/object');
+const app = require('sdk/system/xul-app');
+
+merge(module.exports,
+ require('./test-tabs'),
+ require('./test-page-mod'),
+ require('./test-private-browsing'),
+ require('./test-sidebar')
+);
+
+// Doesn't make sense to test window-utils and windows on fennec,
+// as there is only one window which is never private. Also ignore
+// unsupported modules (panel, selection)
+if (!app.is('Fennec')) {
+ merge(module.exports,
+ require('./test-selection'),
+ require('./test-panel'),
+ require('./test-window-tabs'),
+ require('./test-windows')
+ );
+}
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/private-browsing-supported/package.json b/addon-sdk/source/test/addons/private-browsing-supported/package.json
new file mode 100644
index 000000000..87b96017d
--- /dev/null
+++ b/addon-sdk/source/test/addons/private-browsing-supported/package.json
@@ -0,0 +1,8 @@
+{
+ "id": "private-browsing-mode-test@jetpack",
+ "permissions": {
+ "private-browsing": true
+ },
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/private-browsing-supported/sidebar/utils.js b/addon-sdk/source/test/addons/private-browsing-supported/sidebar/utils.js
new file mode 100644
index 000000000..e0f6234f6
--- /dev/null
+++ b/addon-sdk/source/test/addons/private-browsing-supported/sidebar/utils.js
@@ -0,0 +1,67 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Cu } = require('chrome');
+const { getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { fromIterator } = require('sdk/util/array');
+
+const BUILTIN_SIDEBAR_MENUITEMS = exports.BUILTIN_SIDEBAR_MENUITEMS = [
+ 'menu_socialSidebar',
+ 'menu_historySidebar',
+ 'menu_bookmarksSidebar',
+];
+
+function isSidebarShowing(window) {
+ window = window || getMostRecentBrowserWindow();
+ let sidebar = window.document.getElementById('sidebar-box');
+ return !sidebar.hidden;
+}
+exports.isSidebarShowing = isSidebarShowing;
+
+function getSidebarMenuitems(window) {
+ window = window || getMostRecentBrowserWindow();
+ return fromIterator(window.document.querySelectorAll('#viewSidebarMenu menuitem'));
+}
+exports.getSidebarMenuitems = getSidebarMenuitems;
+
+function getExtraSidebarMenuitems() {
+ let menuitems = getSidebarMenuitems();
+ return menuitems.filter(function(mi) {
+ return BUILTIN_SIDEBAR_MENUITEMS.indexOf(mi.getAttribute('id')) < 0;
+ });
+}
+exports.getExtraSidebarMenuitems = getExtraSidebarMenuitems;
+
+function makeID(id) {
+ return 'jetpack-sidebar-' + id;
+}
+exports.makeID = makeID;
+
+function simulateCommand(ele) {
+ let window = ele.ownerDocument.defaultView;
+ let { document } = window;
+ var evt = document.createEvent('XULCommandEvent');
+ evt.initCommandEvent('command', true, true, window,
+ 0, false, false, false, false, null);
+ ele.dispatchEvent(evt);
+}
+exports.simulateCommand = simulateCommand;
+
+function simulateClick(ele) {
+ let window = ele.ownerDocument.defaultView;
+ let { document } = window;
+ let evt = document.createEvent('MouseEvents');
+ evt.initMouseEvent('click', true, true, window,
+ 0, 0, 0, 0, 0, false, false, false, false, 0, null);
+ ele.dispatchEvent(evt);
+}
+exports.simulateClick = simulateClick;
+
+// OSX and Windows exhibit different behaviors when 'checked' is false,
+// so compare against the consistent 'true'. See bug 894809.
+function isChecked(node) {
+ return node.getAttribute('checked') === 'true';
+};
+exports.isChecked = isChecked;
diff --git a/addon-sdk/source/test/addons/private-browsing-supported/test-page-mod.js b/addon-sdk/source/test/addons/private-browsing-supported/test-page-mod.js
new file mode 100644
index 000000000..ca5122013
--- /dev/null
+++ b/addon-sdk/source/test/addons/private-browsing-supported/test-page-mod.js
@@ -0,0 +1,119 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { PageMod } = require("sdk/page-mod");
+const { getActiveTab, setTabURL, openTab, closeTab } = require('sdk/tabs/utils');
+const xulApp = require('sdk/system/xul-app');
+const windowHelpers = require('sdk/window/helpers');
+const { defer } = require("sdk/core/promise");
+const { isPrivate } = require('sdk/private-browsing');
+const { isTabPBSupported, isWindowPBSupported } = require('sdk/private-browsing/utils');
+const { cleanUI } = require('sdk/test/utils');
+
+function openWebpage(url, enablePrivate) {
+ return new Promise((resolve, reject) => {
+ if (xulApp.is("Fennec")) {
+ let chromeWindow = getMostRecentBrowserWindow();
+ let rawTab = openTab(chromeWindow, url, {
+ isPrivate: enablePrivate
+ });
+
+ resolve(() => new Promise(resolve => {
+ closeTab(rawTab);
+ resolve();
+ }));
+ }
+ else {
+ windowHelpers.open("", {
+ features: {
+ toolbar: true,
+ private: enablePrivate
+ }
+ }).
+ then((chromeWindow) => {
+ if (isPrivate(chromeWindow) !== !!enablePrivate) {
+ reject(new Error("Window should have Private set to " + !!enablePrivate));
+ }
+
+ let tab = getActiveTab(chromeWindow);
+ setTabURL(tab, url);
+
+ resolve(() => windowHelpers.close(chromeWindow));
+ }).
+ catch(reject);
+ }
+ });
+}
+
+exports["test page-mod on private tab"] = function*(assert) {
+ // Only set private mode when explicitely supported.
+ // (fennec 19 has some intermediate PB support where isTabPBSupported
+ // will be false, but isPrivate(worker.tab) will be true if we open a private
+ // tab)
+ let setPrivate = isTabPBSupported || isWindowPBSupported;
+
+ let id = Date.now().toString(36);
+ let frameUri = "data:text/html;charset=utf-8," + id;
+ let uri = "data:text/html;charset=utf-8," +
+ encodeURIComponent(id + "<iframe src='" + frameUri + "'><iframe>");
+
+ let count = 0;
+
+ let close = yield openWebpage(uri, setPrivate);
+ yield new Promise(resolve => {
+ PageMod({
+ include: [uri, frameUri],
+
+ onAttach: function(worker) {
+ assert.ok(worker.tab.url == uri || worker.tab.url == frameUri,
+ "Got a worker attached to the private window tab");
+
+ if (setPrivate) {
+ assert.ok(isPrivate(worker), "The worker is really private");
+ assert.ok(isPrivate(worker.tab), "The document is really private");
+ }
+ else {
+ assert.ok(!isPrivate(worker),
+ "private browsing isn't supported, " +
+ "so that the worker isn't private");
+ assert.ok(!isPrivate(worker.tab),
+ "private browsing isn't supported, " +
+ "so that the document isn't private");
+ }
+
+ if (++count == 2) {
+ this.destroy();
+ resolve();
+ }
+ }
+ });
+ });
+ yield close();
+ yield cleanUI();
+};
+
+exports["test page-mod on non-private tab"] = function*(assert) {
+ let id = Date.now().toString(36);
+ let url = "data:text/html;charset=utf-8," + encodeURIComponent(id);
+
+ let close = yield openWebpage(url, false);
+ let mod;
+ let worker = yield new Promise(resolve => {
+ mod = PageMod({
+ include: url,
+ onAttach: resolve
+ });
+ });
+
+ assert.equal(worker.tab.url, url,
+ "Got a worker attached to the private window tab");
+ assert.ok(!isPrivate(worker), "The worker is really non-private");
+ assert.ok(!isPrivate(worker.tab), "The document is really non-private");
+
+ mod.destroy();
+ yield close();
+ yield cleanUI();
+}
diff --git a/addon-sdk/source/test/addons/private-browsing-supported/test-panel.js b/addon-sdk/source/test/addons/private-browsing-supported/test-panel.js
new file mode 100644
index 000000000..1ba3e9554
--- /dev/null
+++ b/addon-sdk/source/test/addons/private-browsing-supported/test-panel.js
@@ -0,0 +1,99 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { open, focus, close } = require('sdk/window/helpers');
+const { isPrivate } = require('sdk/private-browsing');
+const { defer } = require('sdk/core/promise');
+const { browserWindows: windows } = require('sdk/windows');
+const { getInnerId, getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { getActiveView } = require('sdk/view/core');
+
+const BROWSER = 'chrome://browser/content/browser.xul';
+
+exports.testRequirePanel = function(assert) {
+ require('sdk/panel');
+ assert.ok('the panel module should not throw an error');
+};
+
+exports.testShowPanelInPrivateWindow = function(assert, done) {
+ let panel = require('sdk/panel').Panel({
+ contentURL: "data:text/html;charset=utf-8,I'm a leaf on the wind"
+ });
+
+ assert.ok(windows.length > 0, 'there is at least one open window');
+ for (let window of windows) {
+ assert.equal(isPrivate(window), false, 'open window is private');
+ }
+
+ let panelView = getActiveView(panel);
+ let expectedWindowId = getInnerId(panelView.backgroundFrame.contentWindow);
+
+ function checkPanelFrame() {
+ let iframe = panelView.firstChild;
+
+ assert.equal(panelView.viewFrame, iframe, 'panel has the correct viewFrame value');
+
+ let windowId = getInnerId(iframe.contentWindow);
+
+ assert.equal(windowId, expectedWindowId, 'panel has the correct window visible');
+
+ assert.equal(iframe.contentDocument.body.textContent,
+ "I'm a leaf on the wind",
+ 'the panel has the expected content');
+ }
+
+ function testPanel(window) {
+ let { promise, resolve } = defer();
+
+ assert.ok(!panel.isShowing, 'the panel is not showing [1]');
+
+ panel.once('show', function() {
+ assert.ok(panel.isShowing, 'the panel is showing');
+
+ checkPanelFrame();
+
+ panel.once('hide', function() {
+ assert.ok(!panel.isShowing, 'the panel is not showing [2]');
+
+ resolve(window);
+ });
+
+ panel.hide();
+ });
+
+ panel.show();
+
+ return promise;
+ };
+
+ let initialWindow = getMostRecentBrowserWindow();
+
+ testPanel(initialWindow).
+ then(makeEmptyPrivateBrowserWindow).
+ then(focus).
+ then(function(window) {
+ assert.equal(isPrivate(window), true, 'opened window is private');
+ assert.pass('private window was focused');
+ return window;
+ }).
+ then(testPanel).
+ then(close).
+ then(() => focus(initialWindow)).
+ then(testPanel).
+ then(done).
+ then(null, assert.fail);
+};
+
+
+function makeEmptyPrivateBrowserWindow(options) {
+ options = options || {};
+ return open(BROWSER, {
+ features: {
+ chrome: true,
+ toolbar: true,
+ private: true
+ }
+ });
+}
diff --git a/addon-sdk/source/test/addons/private-browsing-supported/test-private-browsing.js b/addon-sdk/source/test/addons/private-browsing-supported/test-private-browsing.js
new file mode 100644
index 000000000..a7b1e26ca
--- /dev/null
+++ b/addon-sdk/source/test/addons/private-browsing-supported/test-private-browsing.js
@@ -0,0 +1,111 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Ci } = require('chrome');
+const { isPrivateBrowsingSupported } = require('sdk/self');
+const tabs = require('sdk/tabs');
+const { browserWindows: windows } = require('sdk/windows');
+const { isPrivate } = require('sdk/private-browsing');
+const { is } = require('sdk/system/xul-app');
+const { isWindowPBSupported, isTabPBSupported } = require('sdk/private-browsing/utils');
+const { cleanUI } = require('sdk/test/utils');
+
+const TAB_URL = 'about:addons';
+
+exports.testIsPrivateBrowsingTrue = function(assert) {
+ assert.ok(isPrivateBrowsingSupported,
+ 'isPrivateBrowsingSupported property is true');
+};
+
+// test that it is possible to open a private tab
+exports.testTabOpenPrivate = function(assert, done) {
+ tabs.open({
+ url: TAB_URL,
+ isPrivate: true,
+ onReady: function(tab) {
+ assert.equal(tab.url, TAB_URL, 'opened correct tab');
+ assert.equal(isPrivate(tab), (isWindowPBSupported || isTabPBSupported), "tab is private");
+ cleanUI().then(done).catch(console.exception);
+ }
+ });
+}
+
+
+// test that it is possible to open a non private tab
+exports.testTabOpenPrivateDefault = function(assert, done) {
+ tabs.open({
+ url: TAB_URL,
+ onReady: function(tab) {
+ assert.equal(tab.url, TAB_URL, 'opened correct tab');
+ assert.equal(isPrivate(tab), false, "tab is not private");
+ cleanUI().then(done).catch(console.exception);
+ }
+ });
+}
+
+// test that it is possible to open a non private tab in explicit case
+exports.testTabOpenPrivateOffExplicit = function(assert, done) {
+ tabs.open({
+ url: TAB_URL,
+ isPrivate: false,
+ onReady: function(tab) {
+ assert.equal(tab.url, TAB_URL, 'opened correct tab');
+ assert.equal(isPrivate(tab), false, "tab is not private");
+ cleanUI().then(done).catch(console.exception);
+ }
+ });
+}
+
+// test windows.open with isPrivate: true
+// test isPrivate on a window
+if (!is('Fennec')) {
+ // test that it is possible to open a private window
+ exports.testWindowOpenPrivate = function(assert, done) {
+ windows.open({
+ url: TAB_URL,
+ isPrivate: true,
+ onOpen: function(window) {
+ let tab = window.tabs[0];
+ tab.once('ready', function() {
+ assert.equal(tab.url, TAB_URL, 'opened correct tab');
+ assert.equal(isPrivate(tab), isWindowPBSupported, 'tab is private');
+ cleanUI().then(done).catch(console.exception);
+ });
+ }
+ });
+ };
+
+ exports.testIsPrivateOnWindowOn = function(assert, done) {
+ windows.open({
+ isPrivate: true,
+ onOpen: function(window) {
+ assert.equal(isPrivate(window), isWindowPBSupported, 'isPrivate for a window is true when it should be');
+ assert.equal(isPrivate(window.tabs[0]), isWindowPBSupported, 'isPrivate for a tab is false when it should be');
+ cleanUI().then(done).catch(console.exception);
+ }
+ });
+ };
+
+ exports.testIsPrivateOnWindowOffImplicit = function(assert, done) {
+ windows.open({
+ onOpen: function(window) {
+ assert.equal(isPrivate(window), false, 'isPrivate for a window is false when it should be');
+ assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be');
+ cleanUI().then(done).catch(console.exception);
+ }
+ })
+ }
+
+ exports.testIsPrivateOnWindowOffExplicit = function(assert, done) {
+ windows.open({
+ isPrivate: false,
+ onOpen: function(window) {
+ assert.equal(isPrivate(window), false, 'isPrivate for a window is false when it should be');
+ assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be');
+ cleanUI().then(done).catch(console.exception);
+ }
+ })
+ }
+}
diff --git a/addon-sdk/source/test/addons/private-browsing-supported/test-selection.js b/addon-sdk/source/test/addons/private-browsing-supported/test-selection.js
new file mode 100644
index 000000000..3fa1c1b5d
--- /dev/null
+++ b/addon-sdk/source/test/addons/private-browsing-supported/test-selection.js
@@ -0,0 +1,447 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const HTML = "<html>\
+ <body>\
+ <div>foo</div>\
+ <div>and</div>\
+ <textarea>noodles</textarea>\
+ </body>\
+</html>";
+
+const URL = "data:text/html;charset=utf-8," + encodeURIComponent(HTML);
+
+const FRAME_HTML = "<iframe src='" + URL + "'><iframe>";
+const FRAME_URL = "data:text/html;charset=utf-8," + encodeURIComponent(FRAME_HTML);
+
+const { defer } = require("sdk/core/promise");
+const { browserWindows } = require("sdk/windows");
+const tabs = require("sdk/tabs");
+const { setTabURL, getActiveTab, getTabContentWindow, closeTab, getTabs,
+ getTabTitle } = require("sdk/tabs/utils");
+const { getMostRecentBrowserWindow, isFocused } = require("sdk/window/utils");
+const { open: openNewWindow, close: closeWindow, focus } = require("sdk/window/helpers");
+const { Loader } = require("sdk/test/loader");
+const { merge } = require("sdk/util/object");
+const { isPrivate } = require("sdk/private-browsing");
+
+// General purpose utility functions
+
+/**
+ * Opens the url given and return a promise, that will be resolved with the
+ * content window when the document is ready.
+ *
+ * I believe this approach could be useful in most of our unit test, that
+ * requires to open a tab and need to access to its content.
+ */
+function open(url, options) {
+ let { promise, resolve } = defer();
+
+ if (options && typeof(options) === "object") {
+ openNewWindow("", {
+ features: merge({ toolbar: true }, options)
+ }).then(function(chromeWindow) {
+ if (isPrivate(chromeWindow) !== !!options.private)
+ throw new Error("Window should have Private set to " + !!options.private);
+
+ let tab = getActiveTab(chromeWindow);
+
+ tab.linkedBrowser.addEventListener("load", function ready(event) {
+ let { document } = getTabContentWindow(tab);
+
+ if (document.readyState === "complete" && document.URL === url) {
+ this.removeEventListener(event.type, ready);
+
+ if (options.title)
+ document.title = options.title;
+
+ resolve(document.defaultView);
+ }
+ }, true);
+
+ setTabURL(tab, url);
+ });
+
+ return promise;
+ };
+
+ tabs.open({
+ url: url,
+ onReady: function(tab) {
+ // Unfortunately there is no way to get a XUL Tab from SDK Tab on Firefox,
+ // only on Fennec. We should implement `tabNS` also on Firefox in order
+ // to have that.
+
+ // Here we assuming that the most recent browser window is the one we're
+ // doing the test, and the active tab is the one we just opened.
+ let window = getTabContentWindow(getActiveTab(getMostRecentBrowserWindow()));
+
+ resolve(window);
+ }
+ });
+
+ return promise;
+};
+
+/**
+ * Reload the window given and return a promise, that will be resolved with the
+ * content window after a small delay.
+ */
+function reload(window) {
+ let { promise, resolve } = defer();
+
+ // Here we assuming that the most recent browser window is the one we're
+ // doing the test, and the active tab is the one we just opened.
+ let tab = tabs.activeTab;
+
+ tab.once("ready", function () {
+ resolve(window);
+ });
+
+ window.location.reload(true);
+
+ return promise;
+}
+
+// Selection's unit test utility function
+
+/**
+ * Select the first div in the page, adding the range to the selection.
+ */
+function selectFirstDiv(window) {
+ let div = window.document.querySelector("div");
+ let selection = window.getSelection();
+ let range = window.document.createRange();
+
+ if (selection.rangeCount > 0)
+ selection.removeAllRanges();
+
+ range.selectNode(div);
+ selection.addRange(range);
+
+ return window;
+}
+
+/**
+ * Select all divs in the page, adding the ranges to the selection.
+ */
+function selectAllDivs(window) {
+ let divs = window.document.getElementsByTagName("div");
+ let selection = window.getSelection();
+
+ if (selection.rangeCount > 0)
+ selection.removeAllRanges();
+
+ for (let i = 0; i < divs.length; i++) {
+ let range = window.document.createRange();
+
+ range.selectNode(divs[i]);
+ selection.addRange(range);
+ }
+
+ return window;
+}
+
+/**
+ * Select the textarea content
+ */
+function selectTextarea(window) {
+ let selection = window.getSelection();
+ let textarea = window.document.querySelector("textarea");
+
+ if (selection.rangeCount > 0)
+ selection.removeAllRanges();
+
+ textarea.setSelectionRange(0, textarea.value.length);
+ textarea.focus();
+
+ return window;
+}
+
+/**
+ * Select the content of the first div
+ */
+function selectContentFirstDiv(window) {
+ let div = window.document.querySelector("div");
+ let selection = window.getSelection();
+ let range = window.document.createRange();
+
+ if (selection.rangeCount > 0)
+ selection.removeAllRanges();
+
+ range.selectNodeContents(div);
+ selection.addRange(range);
+
+ return window;
+}
+
+/**
+ * Dispatch the selection event for the selection listener added by
+ * `nsISelectionPrivate.addSelectionListener`
+ */
+function dispatchSelectionEvent(window) {
+ // We modify the selection in order to dispatch the selection's event, by
+ // contract the selection by one character. So if the text selected is "foo"
+ // will be "fo".
+ window.getSelection().modify("extend", "backward", "character");
+
+ return window;
+}
+
+/**
+ * Dispatch the selection event for the selection listener added by
+ * `window.onselect` / `window.addEventListener`
+ */
+function dispatchOnSelectEvent(window) {
+ let { document } = window;
+ let textarea = document.querySelector("textarea");
+ let event = document.createEvent("UIEvents");
+
+ event.initUIEvent("select", true, true, window, 1);
+
+ textarea.dispatchEvent(event);
+
+ return window;
+}
+
+// Test cases
+
+exports["test PWPB Selection Listener"] = function(assert, done) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+
+ open(URL, {private: true, title: "PWPB Selection Listener"}).
+ then(function(window) {
+ selection.once("select", function() {
+ assert.equal(browserWindows.length, 2, "there should be only two windows open.");
+ assert.equal(getTabs().length, 2, "there should be only two tabs open: '" +
+ getTabs().map(tab => getTabTitle(tab)).join("', '") +
+ "'."
+ );
+
+ // window should be focused, but force the focus anyhow.. see bug 841823
+ focus(window).then(function() {
+ // check state of window
+ assert.ok(isFocused(window), "the window is focused");
+ assert.ok(isPrivate(window), "the window should be a private window");
+
+ assert.equal(selection.text, "fo");
+
+ closeWindow(window).
+ then(loader.unload).
+ then(done).
+ then(null, assert.fail);
+ });
+ });
+ return window;
+ }).
+ then(selectContentFirstDiv).
+ then(dispatchSelectionEvent).
+ then(null, assert.fail);
+};
+
+exports["test PWPB Textarea OnSelect Listener"] = function(assert, done) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+
+ open(URL, {private: true, title: "PWPB OnSelect Listener"}).
+ then(function(window) {
+ selection.once("select", function() {
+ assert.equal(browserWindows.length, 2, "there should be only two windows open.");
+ assert.equal(getTabs().length, 2, "there should be only two tabs open: '" +
+ getTabs().map(tab => getTabTitle(tab)).join("', '") +
+ "'."
+ );
+
+ // window should be focused, but force the focus anyhow.. see bug 841823
+ focus(window).then(function() {
+ assert.equal(selection.text, "noodles");
+
+ closeWindow(window).
+ then(loader.unload).
+ then(done).
+ then(null, assert.fail);
+ });
+ });
+ return window;
+ }).
+ then(selectTextarea).
+ then(dispatchOnSelectEvent).
+ then(null, assert.fail);
+};
+
+exports["test PWPB Single DOM Selection"] = function(assert, done) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+
+ open(URL, {private: true, title: "PWPB Single DOM Selection"}).
+ then(selectFirstDiv).
+ then(focus).then(function(window) {
+ assert.equal(selection.isContiguous, true,
+ "selection.isContiguous with single DOM Selection works.");
+
+ assert.equal(selection.text, "foo",
+ "selection.text with single DOM Selection works.");
+
+ assert.equal(selection.html, "<div>foo</div>",
+ "selection.html with single DOM Selection works.");
+
+ let selectionCount = 0;
+ for (let sel of selection) {
+ selectionCount++;
+
+ assert.equal(sel.text, "foo",
+ "iterable selection.text with single DOM Selection works.");
+
+ assert.equal(sel.html, "<div>foo</div>",
+ "iterable selection.html with single DOM Selection works.");
+ }
+
+ assert.equal(selectionCount, 1,
+ "One iterable selection");
+
+ return closeWindow(window);
+ }).then(loader.unload).then(done).then(null, assert.fail);
+}
+
+exports["test PWPB Textarea Selection"] = function(assert, done) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+
+ open(URL, {private: true, title: "PWPB Textarea Listener"}).
+ then(selectTextarea).
+ then(focus).
+ then(function(window) {
+
+ assert.equal(selection.isContiguous, true,
+ "selection.isContiguous with Textarea Selection works.");
+
+ assert.equal(selection.text, "noodles",
+ "selection.text with Textarea Selection works.");
+
+ assert.strictEqual(selection.html, null,
+ "selection.html with Textarea Selection works.");
+
+ let selectionCount = 0;
+ for (let sel of selection) {
+ selectionCount++;
+
+ assert.equal(sel.text, "noodles",
+ "iterable selection.text with Textarea Selection works.");
+
+ assert.strictEqual(sel.html, null,
+ "iterable selection.html with Textarea Selection works.");
+ }
+
+ assert.equal(selectionCount, 1,
+ "One iterable selection");
+
+ return closeWindow(window);
+ }).then(loader.unload).then(done).then(null, assert.fail);
+};
+
+exports["test PWPB Set HTML in Multiple DOM Selection"] = function(assert, done) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+
+ open(URL, {private: true, title: "PWPB Set HTML in Multiple DOM Selection"}).
+ then(selectAllDivs).
+ then(focus).
+ then(function(window) {
+ let html = "<span>b<b>a</b>r</span>";
+
+ let expectedText = ["bar", "and"];
+ let expectedHTML = [html, "<div>and</div>"];
+
+ selection.html = html;
+
+ assert.equal(selection.text, expectedText[0],
+ "set selection.text with DOM Selection works.");
+
+ assert.equal(selection.html, expectedHTML[0],
+ "selection.html with DOM Selection works.");
+
+ let selectionCount = 0;
+ for (let sel of selection) {
+
+ assert.equal(sel.text, expectedText[selectionCount],
+ "iterable selection.text with multiple DOM Selection works.");
+
+ assert.equal(sel.html, expectedHTML[selectionCount],
+ "iterable selection.html with multiple DOM Selection works.");
+
+ selectionCount++;
+ }
+
+ assert.equal(selectionCount, 2,
+ "Two iterable selections");
+
+ return closeWindow(window);
+ }).then(loader.unload).then(done).then(null, assert.fail);
+};
+
+exports["test PWPB Set Text in Textarea Selection"] = function(assert, done) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+
+ open(URL, {private: true, title: "test PWPB Set Text in Textarea Selection"}).
+ then(selectTextarea).
+ then(focus).
+ then(function(window) {
+
+ let text = "bar";
+
+ selection.text = text;
+
+ assert.equal(selection.text, text,
+ "set selection.text with Textarea Selection works.");
+
+ assert.strictEqual(selection.html, null,
+ "selection.html with Textarea Selection works.");
+
+ let selectionCount = 0;
+ for (let sel of selection) {
+ selectionCount++;
+
+ assert.equal(sel.text, text,
+ "iterable selection.text with Textarea Selection works.");
+
+ assert.strictEqual(sel.html, null,
+ "iterable selection.html with Textarea Selection works.");
+ }
+
+ assert.equal(selectionCount, 1,
+ "One iterable selection");
+
+ return closeWindow(window);
+ }).then(loader.unload).then(done).then(null, assert.fail);
+};
+
+// If the platform doesn't support the PBPW, we're replacing PBPW tests
+if (!require("sdk/private-browsing/utils").isWindowPBSupported) {
+ module.exports = {
+ "test PBPW Unsupported": function Unsupported (assert) {
+ assert.pass("Private Window Per Browsing is not supported on this platform.");
+ }
+ }
+}
+
+// If the module doesn't support the app we're being run in, require() will
+// throw. In that case, remove all tests above from exports, and add one dummy
+// test that passes.
+try {
+ require("sdk/selection");
+}
+catch (err) {
+ if (!/^Unsupported Application/.test(err.message))
+ throw err;
+
+ module.exports = {
+ "test Unsupported Application": function Unsupported (assert) {
+ assert.pass(err.message);
+ }
+ }
+}
diff --git a/addon-sdk/source/test/addons/private-browsing-supported/test-sidebar.js b/addon-sdk/source/test/addons/private-browsing-supported/test-sidebar.js
new file mode 100644
index 000000000..410e64ff5
--- /dev/null
+++ b/addon-sdk/source/test/addons/private-browsing-supported/test-sidebar.js
@@ -0,0 +1,212 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Loader } = require('sdk/test/loader');
+const { show, hide } = require('sdk/ui/sidebar/actions');
+const { isShowing } = require('sdk/ui/sidebar/utils');
+const { getMostRecentBrowserWindow, isWindowPrivate } = require('sdk/window/utils');
+const { open, close, focus, promise: windowPromise } = require('sdk/window/helpers');
+const { setTimeout } = require('sdk/timers');
+const { isPrivate } = require('sdk/private-browsing');
+const { data } = require('sdk/self');
+const { URL } = require('sdk/url');
+
+const { BUILTIN_SIDEBAR_MENUITEMS, isSidebarShowing,
+ getSidebarMenuitems, getExtraSidebarMenuitems, makeID, simulateCommand,
+ simulateClick, isChecked } = require('./sidebar/utils');
+
+exports.testSideBarIsInNewPrivateWindows = function(assert, done) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testSideBarIsInNewPrivateWindows';
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+testName
+ });
+
+ let startWindow = getMostRecentBrowserWindow();
+ let ele = startWindow.document.getElementById(makeID(testName));
+ assert.ok(ele, 'sidebar element was added');
+
+ open(null, { features: { private: true } }).then(function(window) {
+ let ele = window.document.getElementById(makeID(testName));
+ assert.ok(isPrivate(window), 'the new window is private');
+ assert.ok(!!ele, 'sidebar element was added');
+
+ sidebar.destroy();
+ assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE');
+ assert.ok(!startWindow.document.getElementById(makeID(testName)), 'sidebar id DNE');
+
+ return close(window);
+ }).then(done).then(null, assert.fail);
+}
+
+// Disabled in order to land other fixes, see bug 910647 for further details.
+/*
+exports.testSidebarIsOpenInNewPrivateWindow = function(assert, done) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testSidebarIsOpenInNewPrivateWindow';
+ let window = getMostRecentBrowserWindow();
+
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+testName
+ });
+
+ assert.equal(isPrivate(window), false, 'the window is not private');
+
+ sidebar.on('show', function() {
+ assert.equal(isSidebarShowing(window), true, 'the sidebar is showing');
+ assert.equal(isShowing(sidebar), true, 'the sidebar is showing');
+
+ windowPromise(window.OpenBrowserWindow({private: true}), 'DOMContentLoaded').then(function(window2) {
+ assert.equal(isPrivate(window2), true, 'the new window is private');
+
+ let sidebarEle = window2.document.getElementById('sidebar');
+
+ // wait for the sidebar to load something
+ function onSBLoad() {
+ sidebarEle.contentDocument.getElementById('web-panels-browser').addEventListener('load', function() {
+ assert.equal(isSidebarShowing(window), true, 'the sidebar is showing in old window still');
+ assert.equal(isSidebarShowing(window2), true, 'the sidebar is showing in the new private window');
+ assert.equal(isShowing(sidebar), true, 'the sidebar is showing');
+
+ sidebar.destroy();
+ close(window2).then(done);
+ }, true);
+ }
+
+ sidebarEle.addEventListener('load', onSBLoad, true);
+
+ assert.pass('waiting for the sidebar to open...');
+ }, assert.fail).then(null, assert.fail);
+ });
+
+ sidebar.show();
+}
+*/
+// TEST: edge case where web panel is destroyed while loading
+exports.testDestroyEdgeCaseBugWithPrivateWindow = function(assert, done) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testDestroyEdgeCaseBug';
+ let window = getMostRecentBrowserWindow();
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+testName
+ });
+
+ // NOTE: purposely not listening to show event b/c the event happens
+ // between now and then.
+ sidebar.show();
+
+ assert.equal(isPrivate(window), false, 'the new window is not private');
+ assert.equal(isSidebarShowing(window), true, 'the sidebar is showing');
+
+ //assert.equal(isShowing(sidebar), true, 'the sidebar is showing');
+
+ open(null, { features: { private: true } }).then(focus).then(function(window2) {
+ assert.equal(isPrivate(window2), true, 'the new window is private');
+ assert.equal(isSidebarShowing(window2), false, 'the sidebar is not showing');
+ assert.equal(isShowing(sidebar), false, 'the sidebar is not showing');
+
+ sidebar.destroy();
+ assert.pass('destroying the sidebar');
+
+ close(window2).then(function() {
+ let loader = Loader(module);
+
+ assert.equal(isPrivate(window), false, 'the current window is not private');
+
+ let sidebar = loader.require('sdk/ui/sidebar').Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+ testName,
+ onShow: function() {
+ assert.pass('onShow works for Sidebar');
+ loader.unload();
+
+ for (let mi of getSidebarMenuitems()) {
+ assert.ok(BUILTIN_SIDEBAR_MENUITEMS.indexOf(mi.getAttribute('id')) >= 0, 'the menuitem is for a built-in sidebar')
+ assert.ok(!isChecked(mi), 'no sidebar menuitem is checked');
+ }
+ assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE');
+ assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing');
+
+ done();
+ }
+ })
+
+ sidebar.show();
+ assert.pass('showing the sidebar');
+ }).then(null, assert.fail);
+ }).then(null, assert.fail);
+}
+
+exports.testShowInPrivateWindow = function(assert, done) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testShowInPrivateWindow';
+ let window1 = getMostRecentBrowserWindow();
+ let url = 'data:text/html;charset=utf-8,'+testName;
+
+ let sidebar1 = Sidebar({
+ id: testName,
+ title: testName,
+ url: url
+ });
+ let menuitemID = makeID(sidebar1.id);
+
+ assert.equal(sidebar1.url, url, 'url getter works');
+ assert.equal(isShowing(sidebar1), false, 'the sidebar is not showing');
+ assert.ok(!isChecked(window1.document.getElementById(menuitemID)),
+ 'the menuitem is not checked');
+ assert.equal(isSidebarShowing(window1), false, 'the new window sidebar is not showing');
+
+ windowPromise(window1.OpenBrowserWindow({ private: true }), 'load').then(function(window) {
+ let { document } = window;
+ assert.equal(isWindowPrivate(window), true, 'new window is private');
+ assert.equal(isPrivate(window), true, 'new window is private');
+
+ sidebar1.show().then(
+ function good() {
+ assert.equal(isShowing(sidebar1), true, 'the sidebar is showing');
+ assert.ok(!!document.getElementById(menuitemID),
+ 'the menuitem exists on the private window');
+ assert.equal(isSidebarShowing(window), true, 'the new window sidebar is showing');
+
+ sidebar1.destroy();
+ assert.equal(isSidebarShowing(window), false, 'the new window sidebar is showing');
+ assert.ok(!window1.document.getElementById(menuitemID),
+ 'the menuitem on the new window dne');
+
+ // test old window state
+ assert.equal(isSidebarShowing(window1), false, 'the old window sidebar is not showing');
+ assert.equal(window1.document.getElementById(menuitemID),
+ null,
+ 'the menuitem on the old window dne');
+
+ close(window).then(done).then(null, assert.fail);
+ },
+ function bad() {
+ assert.fail('a successful show should not happen here..');
+ });
+ }).then(null, assert.fail);
+}
+
+// If the module doesn't support the app we're being run in, require() will
+// throw. In that case, remove all tests above from exports, and add one dummy
+// test that passes.
+try {
+ require('sdk/ui/sidebar');
+}
+catch (err) {
+ if (!/^Unsupported Application/.test(err.message))
+ throw err;
+
+ module.exports = {
+ 'test Unsupported Application': assert => assert.pass(err.message)
+ }
+}
diff --git a/addon-sdk/source/test/addons/private-browsing-supported/test-tabs.js b/addon-sdk/source/test/addons/private-browsing-supported/test-tabs.js
new file mode 100644
index 000000000..c9cb34f0e
--- /dev/null
+++ b/addon-sdk/source/test/addons/private-browsing-supported/test-tabs.js
@@ -0,0 +1,34 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const tabs = require('sdk/tabs');
+const { isPrivate } = require('sdk/private-browsing');
+const pbUtils = require('sdk/private-browsing/utils');
+
+exports.testPrivateTabsAreListed = function (assert, done) {
+ let originalTabCount = tabs.length;
+
+ tabs.open({
+ url: 'about:blank',
+ isPrivate: true,
+ onOpen: function(tab) {
+ // PWPB case
+ if (pbUtils.isWindowPBSupported || pbUtils.isTabPBSupported) {
+ assert.ok(isPrivate(tab), "tab is private");
+ assert.equal(tabs.length, originalTabCount + 1,
+ 'New private window\'s tab are visible in tabs list');
+ }
+ else {
+ // Global case, openDialog didn't opened a private window/tab
+ assert.ok(!isPrivate(tab), "tab isn't private");
+ assert.equal(tabs.length, originalTabCount + 1,
+ 'New non-private window\'s tab is visible in tabs list');
+ }
+
+ tab.close(done);
+ }
+ });
+};
+
diff --git a/addon-sdk/source/test/addons/private-browsing-supported/test-window-tabs.js b/addon-sdk/source/test/addons/private-browsing-supported/test-window-tabs.js
new file mode 100644
index 000000000..647a73741
--- /dev/null
+++ b/addon-sdk/source/test/addons/private-browsing-supported/test-window-tabs.js
@@ -0,0 +1,75 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const tabs = require('sdk/tabs');
+const { isPrivate } = require('sdk/private-browsing');
+const { promise: windowPromise, close, focus } = require('sdk/window/helpers');
+const { getMostRecentBrowserWindow } = require('sdk/window/utils');
+
+exports.testOpenTabWithPrivateActiveWindowNoIsPrivateOption = function(assert, done) {
+ let window = getMostRecentBrowserWindow().OpenBrowserWindow({ private: true });
+
+ windowPromise(window, 'load').then(focus).then(function (window) {
+ assert.ok(isPrivate(window), 'new window is private');
+
+ tabs.open({
+ url: 'about:blank',
+ onOpen: function(tab) {
+ assert.ok(isPrivate(tab), 'new tab is private');
+ close(window).then(done).then(null, assert.fail);
+ }
+ })
+ }).then(null, assert.fail);
+}
+
+exports.testOpenTabWithNonPrivateActiveWindowNoIsPrivateOption = function(assert, done) {
+ let window = getMostRecentBrowserWindow().OpenBrowserWindow({ private: false });
+
+ windowPromise(window, 'load').then(focus).then(function (window) {
+ assert.equal(isPrivate(window), false, 'new window is not private');
+
+ tabs.open({
+ url: 'about:blank',
+ onOpen: function(tab) {
+ assert.equal(isPrivate(tab), false, 'new tab is not private');
+ close(window).then(done).then(null, assert.fail);
+ }
+ })
+ }).then(null, assert.fail);
+}
+
+exports.testOpenTabWithPrivateActiveWindowWithIsPrivateOptionTrue = function(assert, done) {
+ let window = getMostRecentBrowserWindow().OpenBrowserWindow({ private: true });
+
+ windowPromise(window, 'load').then(focus).then(function (window) {
+ assert.ok(isPrivate(window), 'new window is private');
+
+ tabs.open({
+ url: 'about:blank',
+ isPrivate: true,
+ onOpen: function(tab) {
+ assert.ok(isPrivate(tab), 'new tab is private');
+ close(window).then(done).then(null, assert.fail);
+ }
+ })
+ }).then(null, assert.fail);
+}
+
+exports.testOpenTabWithNonPrivateActiveWindowWithIsPrivateOptionFalse = function(assert, done) {
+ let window = getMostRecentBrowserWindow().OpenBrowserWindow({ private: false });
+
+ windowPromise(window, 'load').then(focus).then(function (window) {
+ assert.equal(isPrivate(window), false, 'new window is not private');
+
+ tabs.open({
+ url: 'about:blank',
+ isPrivate: false,
+ onOpen: function(tab) {
+ assert.equal(isPrivate(tab), false, 'new tab is not private');
+ close(window).then(done).then(null, assert.fail);
+ }
+ })
+ }).then(null, assert.fail);
+}
diff --git a/addon-sdk/source/test/addons/private-browsing-supported/test-windows.js b/addon-sdk/source/test/addons/private-browsing-supported/test-windows.js
new file mode 100644
index 000000000..ce4e69cae
--- /dev/null
+++ b/addon-sdk/source/test/addons/private-browsing-supported/test-windows.js
@@ -0,0 +1,240 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Cc, Ci } = require('chrome');
+const { isPrivate } = require('sdk/private-browsing');
+const { isWindowPBSupported } = require('sdk/private-browsing/utils');
+const { onFocus, getMostRecentWindow, getWindowTitle, getInnerId,
+ getFrames, windows, open: openWindow, isWindowPrivate } = require('sdk/window/utils');
+const { open, close, focus, promise } = require('sdk/window/helpers');
+const { browserWindows } = require("sdk/windows");
+const winUtils = require("sdk/deprecated/window-utils");
+const { fromIterator: toArray } = require('sdk/util/array');
+const tabs = require('sdk/tabs');
+const { cleanUI } = require('sdk/test/utils');
+
+const WM = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator);
+
+const BROWSER = 'chrome://browser/content/browser.xul';
+
+function makeEmptyBrowserWindow(options) {
+ options = options || {};
+ return open(BROWSER, {
+ features: {
+ chrome: true,
+ private: !!options.private
+ }
+ }).then(focus);
+}
+
+exports.testWindowTrackerIgnoresPrivateWindows = function(assert, done) {
+ var myNonPrivateWindowId, myPrivateWindowId;
+ var privateWindowClosed = false;
+ var privateWindowOpened = false;
+ var trackedWindowIds = [];
+
+ let wt = winUtils.WindowTracker({
+ onTrack: function(window) {
+ let id = getInnerId(window);
+ trackedWindowIds.push(id);
+ },
+ onUntrack: function(window) {
+ let id = getInnerId(window);
+ if (id === myPrivateWindowId) {
+ privateWindowClosed = true;
+ }
+
+ if (id === myNonPrivateWindowId) {
+ assert.equal(privateWindowClosed, true, 'private window was untracked');
+ wt.unload();
+ done();
+ }
+ }
+ });
+
+ // make a new private window
+ makeEmptyBrowserWindow({ private: true }).then(function(window) {
+ myPrivateWindowId = getInnerId(window);
+
+ assert.ok(trackedWindowIds.indexOf(myPrivateWindowId) >= 0, 'private window was tracked');
+ assert.equal(isPrivate(window), isWindowPBSupported, 'private window isPrivate');
+ assert.equal(isWindowPrivate(window), isWindowPBSupported);
+ assert.ok(getFrames(window).length > 1, 'there are frames for private window');
+ assert.equal(getWindowTitle(window), window.document.title,
+ 'getWindowTitle works');
+
+ return close(window).then(function() {
+ assert.pass('private window was closed');
+
+ return makeEmptyBrowserWindow().then(function(window) {
+ myNonPrivateWindowId = getInnerId(window);
+ assert.notEqual(myPrivateWindowId, myNonPrivateWindowId, 'non private window was opened');
+ return close(window);
+ });
+ });
+ }).then(null, assert.fail);
+};
+
+// Test setting activeWIndow and onFocus for private windows
+exports.testSettingActiveWindowDoesNotIgnorePrivateWindow = function(assert, done) {
+ let browserWindow = WM.getMostRecentWindow("navigator:browser");
+ let testSteps;
+
+ assert.equal(winUtils.activeBrowserWindow, browserWindow,
+ "Browser window is the active browser window.");
+ assert.ok(!isPrivate(browserWindow), "Browser window is not private.");
+
+ // make a new private window
+ makeEmptyBrowserWindow({
+ private: true
+ }).then(function(window) {
+ let continueAfterFocus = window => onFocus(window).then(nextTest);
+
+ // PWPB case
+ if (isWindowPBSupported) {
+ assert.ok(isPrivate(window), "window is private");
+ assert.notStrictEqual(winUtils.activeBrowserWindow, browserWindow);
+ }
+ // Global case
+ else {
+ assert.ok(!isPrivate(window), "window is not private");
+ }
+
+ assert.strictEqual(winUtils.activeBrowserWindow, window,
+ "Correct active browser window pb supported");
+ assert.notStrictEqual(browserWindow, window,
+ "The window is not the old browser window");
+
+ testSteps = [
+ function() {
+ // test setting a non private window
+ continueAfterFocus(winUtils.activeWindow = browserWindow);
+ },
+ function() {
+ assert.strictEqual(winUtils.activeWindow, browserWindow,
+ "Correct active window [1]");
+ assert.strictEqual(winUtils.activeBrowserWindow, browserWindow,
+ "Correct active browser window [1]");
+
+ // test focus(window)
+ focus(window).then(nextTest);
+ },
+ function(w) {
+ assert.strictEqual(w, window, 'require("sdk/window/helpers").focus on window works');
+ assert.strictEqual(winUtils.activeBrowserWindow, window,
+ "Correct active browser window [2]");
+ assert.strictEqual(winUtils.activeWindow, window,
+ "Correct active window [2]");
+
+ // test setting a private window
+ continueAfterFocus(winUtils.activeWindow = window);
+ },
+ function() {
+ assert.strictEqual(winUtils.activeBrowserWindow, window,
+ "Correct active browser window [3]");
+ assert.strictEqual(winUtils.activeWindow, window,
+ "Correct active window [3]");
+
+ // just to get back to original state
+ continueAfterFocus(winUtils.activeWindow = browserWindow);
+ },
+ function() {
+ assert.strictEqual(winUtils.activeBrowserWindow, browserWindow,
+ "Correct active browser window when pb mode is supported [4]");
+ assert.strictEqual(winUtils.activeWindow, browserWindow,
+ "Correct active window when pb mode is supported [4]");
+
+ close(window).then(done).then(null, assert.fail);
+ }
+ ];
+
+ function nextTest() {
+ let args = arguments;
+ if (testSteps.length) {
+ require('sdk/timers').setTimeout(function() {
+ (testSteps.shift()).apply(null, args);
+ }, 0);
+ }
+ }
+ nextTest();
+ });
+};
+
+exports.testActiveWindowDoesNotIgnorePrivateWindow = function*(assert) {
+ // make a new private window
+ let window = yield makeEmptyBrowserWindow({
+ private: true
+ });
+
+ // PWPB case
+ if (isWindowPBSupported) {
+ assert.equal(isPrivate(winUtils.activeWindow), true,
+ "active window is private");
+ assert.equal(isPrivate(winUtils.activeBrowserWindow), true,
+ "active browser window is private");
+ assert.ok(isWindowPrivate(window), "window is private");
+ assert.ok(isPrivate(window), "window is private");
+
+ // pb mode is supported
+ assert.ok(
+ isWindowPrivate(winUtils.activeWindow),
+ "active window is private when pb mode is supported");
+ assert.ok(
+ isWindowPrivate(winUtils.activeBrowserWindow),
+ "active browser window is private when pb mode is supported");
+ assert.ok(isPrivate(winUtils.activeWindow),
+ "active window is private when pb mode is supported");
+ assert.ok(isPrivate(winUtils.activeBrowserWindow),
+ "active browser window is private when pb mode is supported");
+ }
+
+ yield cleanUI();
+}
+
+exports.testWindowIteratorIgnoresPrivateWindows = function*(assert) {
+ // make a new private window
+ let window = yield makeEmptyBrowserWindow({
+ private: true
+ });
+
+ assert.equal(isWindowPrivate(window), isWindowPBSupported);
+ assert.ok(toArray(winUtils.windowIterator()).indexOf(window) > -1,
+ "window is in windowIterator()");
+
+ yield cleanUI();
+};
+
+// test that it is not possible to find a private window in
+// windows module's iterator
+exports.testWindowIteratorPrivateDefault = function(assert, done) {
+ // there should only be one window open here, if not give us the
+ // the urls
+ if (browserWindows.length > 1) {
+ for (let tab of tabs) {
+ assert.fail("TAB URL: " + tab.url);
+ }
+ }
+ else {
+ assert.equal(browserWindows.length, 1, 'only one window open');
+ }
+
+ open('chrome://browser/content/browser.xul', {
+ features: {
+ private: true,
+ chrome: true
+ }
+ }).then(focus).then(function(window) {
+ // test that there is a private window opened
+ assert.equal(isPrivate(window), isWindowPBSupported, 'there is a private window open');
+ assert.equal(isPrivate(winUtils.activeWindow), isWindowPBSupported);
+ assert.equal(isPrivate(getMostRecentWindow()), isWindowPBSupported);
+ assert.equal(isPrivate(browserWindows.activeWindow), isWindowPBSupported);
+
+ assert.equal(browserWindows.length, 2, '2 windows open');
+ assert.equal(windows(null, { includePrivate: true }).length, 2);
+
+ return close(window);
+ }).then(done).then(null, assert.fail);
+};
diff --git a/addon-sdk/source/test/addons/remote/main.js b/addon-sdk/source/test/addons/remote/main.js
new file mode 100644
index 000000000..cea27af9b
--- /dev/null
+++ b/addon-sdk/source/test/addons/remote/main.js
@@ -0,0 +1,578 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const LOCAL_URI = "about:robots";
+const REMOTE_URI = "data:text/html;charset=utf-8,remote";
+
+const { Cu } = require('chrome');
+const { Loader } = require('sdk/test/loader');
+const { getTabs, openTab, closeTab, setTabURL, getBrowserForTab, getURI } = require('sdk/tabs/utils');
+const { getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { cleanUI } = require("sdk/test/utils");
+const { setTimeout } = require("sdk/timers");
+const { promiseEvent, promiseDOMEvent, promiseEventOnItemAndContainer,
+ waitForProcesses, getChildFrameCount, isE10S } = require("./utils");
+const { after } = require('sdk/test/utils');
+const { processID } = require('sdk/system/runtime');
+
+const { set } = require('sdk/preferences/service');
+// The hidden preload browser messes up our frame counts
+set('browser.newtab.preload', false);
+
+function promiseTabFrameAttach(frames) {
+ return new Promise(resolve => {
+ let listener = function(frame, ...args) {
+ if (!frame.isTab)
+ return;
+ frames.off("attach", listener);
+ resolve([frame, ...args]);
+ }
+
+ frames.on("attach", listener);
+ });
+}
+
+// Check that we see a process stop and start
+exports["test process restart"] = function*(assert) {
+ if (!isE10S) {
+ assert.pass("Skipping test in non-e10s mode");
+ return;
+ }
+
+ let window = getMostRecentBrowserWindow();
+
+ let tabs = getTabs(window);
+ assert.equal(tabs.length, 1, "Should have just the one tab to start with");
+ let tab = tabs[0];
+ let browser = getBrowserForTab(tab);
+
+ let loader = new Loader(module);
+ let { processes, frames } = yield waitForProcesses(loader);
+
+ let remoteProcess = Array.filter(processes, p => p.isRemote)[0];
+ let localProcess = Array.filter(processes, p => !p.isRemote)[0];
+ let remoteFrame = Array.filter(frames, f => f.process == remoteProcess)[0];
+
+ // Switch the remote tab to a local URI which should kill the remote process
+
+ let frameDetach = promiseEventOnItemAndContainer(assert, remoteFrame, frames, 'detach');
+ let frameAttach = promiseTabFrameAttach(frames);
+ let processDetach = promiseEventOnItemAndContainer(assert, remoteProcess, processes, 'detach');
+ let browserLoad = promiseDOMEvent(browser, "load", true);
+ setTabURL(tab, LOCAL_URI);
+ // The load should kill the remote frame
+ yield frameDetach;
+ // And create a new frame in the local process
+ let [newFrame] = yield frameAttach;
+ assert.equal(newFrame.process, localProcess, "New frame should be in the local process");
+ // And kill the process
+ yield processDetach;
+ yield browserLoad;
+
+ frameDetach = promiseEventOnItemAndContainer(assert, newFrame, frames, 'detach');
+ let processAttach = promiseEvent(processes, 'attach');
+ frameAttach = promiseTabFrameAttach(frames);
+ browserLoad = promiseDOMEvent(browser, "load", true);
+ setTabURL(tab, REMOTE_URI);
+ // The load should kill the remote frame
+ yield frameDetach;
+ // And create a new remote process
+ [remoteProcess] = yield processAttach;
+ assert.ok(remoteProcess.isRemote, "Process should be remote");
+ // And create a new frame in the remote process
+ [newFrame] = yield frameAttach;
+ assert.equal(newFrame.process, remoteProcess, "New frame should be in the remote process");
+ yield browserLoad;
+
+ browserLoad = promiseDOMEvent(browser, "load", true);
+ setTabURL(tab, "about:blank");
+ yield browserLoad;
+
+ loader.unload();
+};
+
+// Test that we find the right number of processes and that messaging between
+// them works and none of the streams cross
+exports["test process list"] = function*(assert) {
+ let loader = new Loader(module);
+ let { processes } = loader.require('sdk/remote/parent');
+
+ let processCount = 0;
+ processes.forEvery(processes => processCount++);
+
+ yield waitForProcesses(loader);
+
+ let remoteProcesses = Array.filter(processes, process => process.isRemote);
+ let localProcesses = Array.filter(processes, process => !process.isRemote);
+
+ assert.equal(localProcesses.length, 1, "Should always be one process");
+
+ if (isE10S) {
+ assert.equal(remoteProcesses.length, 1, "Should be one remote process");
+ }
+ else {
+ assert.equal(remoteProcesses.length, 0, "Should be no remote processes");
+ }
+
+ assert.equal(processCount, processes.length, "Should have seen all processes");
+
+ processCount = 0;
+ processes.forEvery(process => processCount++);
+
+ assert.equal(processCount, processes.length, "forEvery should send existing processes to the listener");
+
+ localProcesses[0].port.on('sdk/test/pong', (process, key) => {
+ assert.equal(key, "local", "Should not have seen a pong from the local process with the wrong key");
+ });
+
+ if (isE10S) {
+ remoteProcesses[0].port.on('sdk/test/pong', (process, key) => {
+ assert.equal(key, "remote", "Should not have seen a pong from the remote process with the wrong key");
+ });
+ }
+
+ let promise = promiseEventOnItemAndContainer(assert, localProcesses[0].port, processes.port, 'sdk/test/pong', localProcesses[0]);
+ localProcesses[0].port.emit('sdk/test/ping', "local");
+
+ let reply = yield promise;
+ assert.equal(reply[0], "local", "Saw the process reply with the right key");
+
+ if (isE10S) {
+ promise = promiseEventOnItemAndContainer(assert, remoteProcesses[0].port, processes.port, 'sdk/test/pong', remoteProcesses[0]);
+ remoteProcesses[0].port.emit('sdk/test/ping', "remote");
+
+ reply = yield promise;
+ assert.equal(reply[0], "remote", "Saw the process reply with the right key");
+
+ assert.notEqual(localProcesses[0], remoteProcesses[0], "Processes should be different");
+ }
+
+ loader.unload();
+};
+
+// Test that the frame lists are kept up to date
+exports["test frame list"] = function*(assert) {
+ function browserFrames(list) {
+ return Array.filter(list, b => b.isTab).length;
+ }
+
+ let window = getMostRecentBrowserWindow();
+
+ let tabs = getTabs(window);
+ assert.equal(tabs.length, 1, "Should have just the one tab to start with");
+
+ let loader = new Loader(module);
+ let { processes, frames } = yield waitForProcesses(loader);
+
+ assert.equal(browserFrames(frames), getTabs(window).length, "Should be the right number of browser frames.");
+ assert.equal((yield getChildFrameCount(processes)), frames.length, "Child processes should have the right number of frames");
+
+ let promise = promiseTabFrameAttach(frames);
+ let tab1 = openTab(window, LOCAL_URI);
+ let [frame1] = yield promise;
+ assert.ok(!!frame1, "Should have seen the new frame");
+ assert.ok(!frame1.process.isRemote, "Frame should not be remote");
+
+ assert.equal(browserFrames(frames), getTabs(window).length, "Should be the right number of browser frames.");
+ assert.equal((yield getChildFrameCount(processes)), frames.length, "Child processes should have the right number of frames");
+
+ promise = promiseTabFrameAttach(frames);
+ let tab2 = openTab(window, REMOTE_URI);
+ let [frame2] = yield promise;
+ assert.ok(!!frame2, "Should have seen the new frame");
+ if (isE10S)
+ assert.ok(frame2.process.isRemote, "Frame should be remote");
+ else
+ assert.ok(!frame2.process.isRemote, "Frame should not be remote");
+
+ assert.equal(browserFrames(frames), getTabs(window).length, "Should be the right number of browser frames.");
+ assert.equal((yield getChildFrameCount(processes)), frames.length, "Child processes should have the right number of frames");
+
+ frames.port.emit('sdk/test/ping')
+ yield new Promise(resolve => {
+ let count = 0;
+ let listener = () => {
+ console.log("Saw pong");
+ count++;
+ if (count == frames.length) {
+ frames.port.off('sdk/test/pong', listener);
+ resolve();
+ }
+ };
+ frames.port.on('sdk/test/pong', listener);
+ });
+
+ let badListener = () => {
+ assert.fail("Should not have seen a response through this frame");
+ }
+ frame1.port.on('sdk/test/pong', badListener);
+ frame2.port.emit('sdk/test/ping', 'b');
+ let [key] = yield promiseEventOnItemAndContainer(assert, frame2.port, frames.port, 'sdk/test/pong', frame2);
+ assert.equal(key, 'b', "Should have seen the right response");
+ frame1.port.off('sdk/test/pong', badListener);
+
+ frame2.port.on('sdk/test/pong', badListener);
+ frame1.port.emit('sdk/test/ping', 'b');
+ [key] = yield promiseEventOnItemAndContainer(assert, frame1.port, frames.port, 'sdk/test/pong', frame1);
+ assert.equal(key, 'b', "Should have seen the right response");
+ frame2.port.off('sdk/test/pong', badListener);
+
+ promise = promiseEventOnItemAndContainer(assert, frame1, frames, 'detach');
+ closeTab(tab1);
+ yield promise;
+
+ assert.equal(browserFrames(frames), getTabs(window).length, "Should be the right number of browser frames.");
+ assert.equal((yield getChildFrameCount(processes)), frames.length, "Child processes should have the right number of frames");
+
+ promise = promiseEventOnItemAndContainer(assert, frame2, frames, 'detach');
+ closeTab(tab2);
+ yield promise;
+
+ assert.equal(browserFrames(frames), getTabs(window).length, "Should be the right number of browser frames.");
+ assert.equal((yield getChildFrameCount(processes)), frames.length, "Child processes should have the right number of frames");
+
+ loader.unload();
+};
+
+// Test that multiple loaders get their own loaders in the child and messages
+// don't cross. Unload should work
+exports["test new loader"] = function*(assert) {
+ let loader1 = new Loader(module);
+ let { processes: processes1 } = yield waitForProcesses(loader1);
+
+ let loader2 = new Loader(module);
+ let { processes: processes2 } = yield waitForProcesses(loader2);
+
+ let process1 = [...processes1][0];
+ let process2 = [...processes2][0];
+
+ process1.port.on('sdk/test/pong', (process, key) => {
+ assert.equal(key, "a", "Should have seen the right pong");
+ });
+
+ process2.port.on('sdk/test/pong', (process, key) => {
+ assert.equal(key, "b", "Should have seen the right pong");
+ });
+
+ process1.port.emit('sdk/test/ping', 'a');
+ yield promiseEvent(process1.port, 'sdk/test/pong');
+
+ process2.port.emit('sdk/test/ping', 'b');
+ yield promiseEvent(process2.port, 'sdk/test/pong');
+
+ loader1.unload();
+
+ process2.port.emit('sdk/test/ping', 'b');
+ yield promiseEvent(process2.port, 'sdk/test/pong');
+
+ loader2.unload();
+};
+
+// Test that unloading the loader unloads the child instances
+exports["test unload"] = function*(assert) {
+ let window = getMostRecentBrowserWindow();
+ let loader = new Loader(module);
+ let { frames } = yield waitForProcesses(loader);
+
+ let promise = promiseTabFrameAttach(frames);
+ let tab = openTab(window, "data:,<html/>");
+ let browser = getBrowserForTab(tab);
+ yield promiseDOMEvent(browser, "load", true);
+ let [frame] = yield promise;
+ assert.ok(!!frame, "Should have seen the new frame");
+
+ promise = promiseDOMEvent(browser, 'hashchange');
+ frame.port.emit('sdk/test/testunload');
+ loader.unload("shutdown");
+ yield promise;
+
+ let hash = getURI(tab).replace(/.*#/, "");
+ assert.equal(hash, "unloaded:shutdown", "Saw the correct hash change.")
+
+ closeTab(tab);
+}
+
+// Test that unloading the loader causes the child to see frame detach events
+exports["test frame detach on unload"] = function*(assert) {
+ let window = getMostRecentBrowserWindow();
+ let loader = new Loader(module);
+ let { frames } = yield waitForProcesses(loader);
+
+ let promise = promiseTabFrameAttach(frames);
+ let tab = openTab(window, "data:,<html/>");
+ let browser = getBrowserForTab(tab);
+ yield promiseDOMEvent(browser, "load", true);
+ let [frame] = yield promise;
+ assert.ok(!!frame, "Should have seen the new frame");
+
+ promise = promiseDOMEvent(browser, 'hashchange');
+ frame.port.emit('sdk/test/testdetachonunload');
+ loader.unload("shutdown");
+ yield promise;
+
+ let hash = getURI(tab).replace(/.*#/, "");
+ assert.equal(hash, "unloaded", "Saw the correct hash change.")
+
+ closeTab(tab);
+}
+
+// Test that DOM event listener on the frame object works
+exports["test frame event listeners"] = function*(assert) {
+ let window = getMostRecentBrowserWindow();
+ let loader = new Loader(module);
+ let { frames } = yield waitForProcesses(loader);
+
+ let promise = promiseTabFrameAttach(frames);
+ let tab = openTab(window, "data:text/html,<html></html>");
+ let browser = getBrowserForTab(tab);
+ yield promiseDOMEvent(browser, "load", true);
+ let [frame] = yield promise;
+ assert.ok(!!frame, "Should have seen the new frame");
+
+ frame.port.emit('sdk/test/registerframeevent');
+ promise = Promise.all([
+ promiseEvent(frame.port, 'sdk/test/sawreply'),
+ promiseEvent(frame.port, 'sdk/test/eventsent')
+ ]);
+
+ frame.port.emit('sdk/test/sendevent');
+ yield promise;
+
+ frame.port.emit('sdk/test/unregisterframeevent');
+ promise = promiseEvent(frame.port, 'sdk/test/eventsent');
+ frame.port.on('sdk/test/sawreply', () => {
+ assert.fail("Should not have seen the event listener reply");
+ });
+
+ frame.port.emit('sdk/test/sendevent');
+ yield promise;
+
+ closeTab(tab);
+ loader.unload();
+}
+
+// Test that DOM event listener on the frames object works
+exports["test frames event listeners"] = function*(assert) {
+ let window = getMostRecentBrowserWindow();
+ let loader = new Loader(module);
+ let { frames } = yield waitForProcesses(loader);
+
+ let promise = promiseTabFrameAttach(frames);
+ let tab = openTab(window, "data:text/html,<html></html>");
+ let browser = getBrowserForTab(tab);
+ yield promiseDOMEvent(browser, "load", true);
+ let [frame] = yield promise;
+ assert.ok(!!frame, "Should have seen the new frame");
+
+ frame.port.emit('sdk/test/registerframesevent');
+ promise = Promise.all([
+ promiseEvent(frame.port, 'sdk/test/sawreply'),
+ promiseEvent(frame.port, 'sdk/test/eventsent')
+ ]);
+
+ frame.port.emit('sdk/test/sendevent');
+ yield promise;
+
+ frame.port.emit('sdk/test/unregisterframesevent');
+ promise = promiseEvent(frame.port, 'sdk/test/eventsent');
+ frame.port.on('sdk/test/sawreply', () => {
+ assert.fail("Should not have seen the event listener reply");
+ });
+
+ frame.port.emit('sdk/test/sendevent');
+ yield promise;
+
+ closeTab(tab);
+ loader.unload();
+}
+
+// Test that unloading unregisters frame DOM events
+exports["test unload removes frame event listeners"] = function*(assert) {
+ let window = getMostRecentBrowserWindow();
+ let loader = new Loader(module);
+ let { frames } = yield waitForProcesses(loader);
+
+ let loader2 = new Loader(module);
+ let { frames: frames2 } = yield waitForProcesses(loader2);
+
+ let promise = promiseTabFrameAttach(frames);
+ let promise2 = promiseTabFrameAttach(frames2);
+ let tab = openTab(window, "data:text/html,<html></html>");
+ let browser = getBrowserForTab(tab);
+ yield promiseDOMEvent(browser, "load", true);
+ let [frame] = yield promise;
+ let [frame2] = yield promise2;
+ assert.ok(!!frame && !!frame2, "Should have seen the new frame");
+
+ frame.port.emit('sdk/test/registerframeevent');
+ promise = Promise.all([
+ promiseEvent(frame2.port, 'sdk/test/sawreply'),
+ promiseEvent(frame2.port, 'sdk/test/eventsent')
+ ]);
+
+ frame2.port.emit('sdk/test/sendevent');
+ yield promise;
+
+ loader.unload();
+
+ promise = promiseEvent(frame2.port, 'sdk/test/eventsent');
+ frame2.port.on('sdk/test/sawreply', () => {
+ assert.fail("Should not have seen the event listener reply");
+ });
+
+ frame2.port.emit('sdk/test/sendevent');
+ yield promise;
+
+ closeTab(tab);
+ loader2.unload();
+}
+
+// Test that unloading unregisters frames DOM events
+exports["test unload removes frames event listeners"] = function*(assert) {
+ let window = getMostRecentBrowserWindow();
+ let loader = new Loader(module);
+ let { frames } = yield waitForProcesses(loader);
+
+ let loader2 = new Loader(module);
+ let { frames: frames2 } = yield waitForProcesses(loader2);
+
+ let promise = promiseTabFrameAttach(frames);
+ let promise2 = promiseTabFrameAttach(frames2);
+ let tab = openTab(window, "data:text/html,<html></html>");
+ let browser = getBrowserForTab(tab);
+ yield promiseDOMEvent(browser, "load", true);
+ let [frame] = yield promise;
+ let [frame2] = yield promise2;
+ assert.ok(!!frame && !!frame2, "Should have seen the new frame");
+
+ frame.port.emit('sdk/test/registerframesevent');
+ promise = Promise.all([
+ promiseEvent(frame2.port, 'sdk/test/sawreply'),
+ promiseEvent(frame2.port, 'sdk/test/eventsent')
+ ]);
+
+ frame2.port.emit('sdk/test/sendevent');
+ yield promise;
+
+ loader.unload();
+
+ promise = promiseEvent(frame2.port, 'sdk/test/eventsent');
+ frame2.port.on('sdk/test/sawreply', () => {
+ assert.fail("Should not have seen the event listener reply");
+ });
+
+ frame2.port.emit('sdk/test/sendevent');
+ yield promise;
+
+ closeTab(tab);
+ loader2.unload();
+}
+
+// Check that the child frame has the right properties
+exports["test frame properties"] = function*(assert) {
+ let loader = new Loader(module);
+ let { frames } = yield waitForProcesses(loader);
+
+ let promise = new Promise(resolve => {
+ let count = frames.length;
+ let listener = (frame, properties) => {
+ assert.equal(properties.isTab, frame.isTab,
+ "Child frame should have the same isTab property");
+
+ if (--count == 0) {
+ frames.port.off('sdk/test/replyproperties', listener);
+ resolve();
+ }
+ }
+
+ frames.port.on('sdk/test/replyproperties', listener);
+ })
+
+ frames.port.emit('sdk/test/checkproperties');
+ yield promise;
+
+ loader.unload();
+}
+
+// Check that non-remote processes have the same process ID and remote processes
+// have different IDs
+exports["test processID"] = function*(assert) {
+ let loader = new Loader(module);
+ let { processes } = yield waitForProcesses(loader);
+
+ for (let process of processes) {
+ process.port.emit('sdk/test/getprocessid');
+ let [p, ID] = yield promiseEvent(process.port, 'sdk/test/processid');
+ if (process.isRemote) {
+ assert.notEqual(ID, processID, "Remote processes should have a different process ID");
+ }
+ else {
+ assert.equal(ID, processID, "Remote processes should have the same process ID");
+ }
+ }
+
+ loader.unload();
+}
+
+// Check that sdk/remote/parent and sdk/remote/child can only be loaded in the
+// appropriate loaders
+exports["test cannot load in wrong loader"] = function*(assert) {
+ let loader = new Loader(module);
+ let { processes } = yield waitForProcesses(loader);
+
+ try {
+ require('sdk/remote/child');
+ assert.fail("Should not have been able to load sdk/remote/child");
+ }
+ catch (e) {
+ assert.ok(/Cannot load sdk\/remote\/child in a main process loader/.test(e),
+ "Should have seen the right exception.");
+ }
+
+ for (let process of processes) {
+ processes.port.emit('sdk/test/parentload');
+ let [_, isChildLoader, loaded, message] = yield promiseEvent(processes.port, 'sdk/test/parentload');
+ assert.ok(isChildLoader, "Process should see itself in a child loader.");
+ assert.ok(!loaded, "Process couldn't load sdk/remote/parent.");
+ assert.ok(/Cannot load sdk\/remote\/parent in a child loader/.test(message),
+ "Should have seen the right exception.");
+ }
+
+ loader.unload();
+};
+
+exports["test send cpow"] = function*(assert) {
+ if (!isE10S) {
+ assert.pass("Skipping test in non-e10s mode");
+ return;
+ }
+
+ let window = getMostRecentBrowserWindow();
+
+ let tabs = getTabs(window);
+ assert.equal(tabs.length, 1, "Should have just the one tab to start with");
+ let tab = tabs[0];
+ let browser = getBrowserForTab(tab);
+
+ assert.ok(Cu.isCrossProcessWrapper(browser.contentWindow),
+ "Should have a CPOW for the browser content window");
+
+ let loader = new Loader(module);
+ let { processes } = yield waitForProcesses(loader);
+
+ processes.port.emitCPOW('sdk/test/cpow', ['foobar'], { window: browser.contentWindow });
+ let [process, arg, id] = yield promiseEvent(processes.port, 'sdk/test/cpow');
+
+ assert.ok(process.isRemote, "Response should come from the remote process");
+ assert.equal(arg, "foobar", "Argument should have passed through");
+ assert.equal(id, browser.outerWindowID, "Should have got the ID from the child");
+};
+
+after(exports, function*(name, assert) {
+ yield cleanUI();
+});
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/remote/package.json b/addon-sdk/source/test/addons/remote/package.json
new file mode 100644
index 000000000..b147b6fda
--- /dev/null
+++ b/addon-sdk/source/test/addons/remote/package.json
@@ -0,0 +1,8 @@
+{
+ "name": "remote",
+ "title": "remote",
+ "id": "remote@jetpack",
+ "description": "Run remote tests",
+ "version": "1.0.0",
+ "main": "main.js"
+}
diff --git a/addon-sdk/source/test/addons/remote/remote-module.js b/addon-sdk/source/test/addons/remote/remote-module.js
new file mode 100644
index 000000000..cedf005a9
--- /dev/null
+++ b/addon-sdk/source/test/addons/remote/remote-module.js
@@ -0,0 +1,129 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const { when } = require('sdk/system/unload');
+const { process, frames } = require('sdk/remote/child');
+const { loaderID } = require('@loader/options');
+const { processID } = require('sdk/system/runtime');
+const system = require('sdk/system/events');
+const { Cu } = require('chrome');
+const { isChildLoader } = require('sdk/remote/core');
+const { getOuterId } = require('sdk/window/utils');
+
+function log(str) {
+ console.log("remote[" + loaderID + "][" + processID + "]: " + str);
+}
+
+log("module loaded");
+
+process.port.emit('sdk/test/load');
+
+process.port.on('sdk/test/ping', (process, key) => {
+ log("received process ping");
+ process.port.emit('sdk/test/pong', key);
+});
+
+var frameCount = 0;
+frames.forEvery(frame => {
+ frameCount++;
+ frame.on('detach', () => {
+ frameCount--;
+ });
+
+ frame.port.on('sdk/test/ping', (frame, key) => {
+ log("received frame ping");
+ frame.port.emit('sdk/test/pong', key);
+ });
+});
+
+frames.port.on('sdk/test/checkproperties', frame => {
+ frame.port.emit('sdk/test/replyproperties', {
+ isTab: frame.isTab
+ });
+});
+
+process.port.on('sdk/test/count', () => {
+ log("received count ping");
+ process.port.emit('sdk/test/count', frameCount);
+});
+
+process.port.on('sdk/test/getprocessid', () => {
+ process.port.emit('sdk/test/processid', processID);
+});
+
+frames.port.on('sdk/test/testunload', (frame) => {
+ // Cache the content since the frame will have been destroyed by the time
+ // we see the unload event.
+ let content = frame.content;
+ when((reason) => {
+ content.location = "#unloaded:" + reason;
+ });
+});
+
+frames.port.on('sdk/test/testdetachonunload', (frame) => {
+ let content = frame.content;
+ frame.on('detach', () => {
+ console.log("Detach from " + frame.content.location);
+ frame.content.location = "#unloaded";
+ });
+});
+
+frames.port.on('sdk/test/sendevent', (frame) => {
+ let doc = frame.content.document;
+
+ let listener = () => {
+ frame.port.emit('sdk/test/sawreply');
+ }
+
+ system.on("Test:Reply", listener);
+ let event = new frame.content.CustomEvent("Test:Event");
+ doc.dispatchEvent(event);
+ system.off("Test:Reply", listener);
+ frame.port.emit('sdk/test/eventsent');
+});
+
+process.port.on('sdk/test/parentload', () => {
+ let loaded = false;
+ let message = "";
+ try {
+ require('sdk/remote/parent');
+ loaded = true;
+ }
+ catch (e) {
+ message = "" + e;
+ }
+
+ process.port.emit('sdk/test/parentload',
+ isChildLoader,
+ loaded,
+ message
+ )
+});
+
+function listener(event) {
+ // Use the raw observer service here since it will be usable even if the
+ // loader has unloaded
+ let { Services } = Cu.import("resource://gre/modules/Services.jsm", {});
+ Services.obs.notifyObservers(null, "Test:Reply", "");
+}
+
+frames.port.on('sdk/test/registerframesevent', (frame) => {
+ frames.addEventListener("Test:Event", listener, true);
+});
+
+frames.port.on('sdk/test/unregisterframesevent', (frame) => {
+ frames.removeEventListener("Test:Event", listener, true);
+});
+
+frames.port.on('sdk/test/registerframeevent', (frame) => {
+ frame.addEventListener("Test:Event", listener, true);
+});
+
+frames.port.on('sdk/test/unregisterframeevent', (frame) => {
+ frame.removeEventListener("Test:Event", listener, true);
+});
+
+process.port.on('sdk/test/cpow', (process, arg, cpows) => {
+ process.port.emit('sdk/test/cpow', arg, getOuterId(cpows.window));
+});
diff --git a/addon-sdk/source/test/addons/remote/utils.js b/addon-sdk/source/test/addons/remote/utils.js
new file mode 100644
index 000000000..f30f4f3a4
--- /dev/null
+++ b/addon-sdk/source/test/addons/remote/utils.js
@@ -0,0 +1,110 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const { Cu } = require('chrome');
+const { Task: { async } } = Cu.import('resource://gre/modules/Task.jsm', {});
+const { getMostRecentBrowserWindow } = require('sdk/window/utils');
+
+const REMOTE_MODULE = "./remote-module";
+
+function promiseEvent(emitter, event) {
+ console.log("Waiting for " + event);
+ return new Promise(resolve => {
+ emitter.once(event, (...args) => {
+ console.log("Saw " + event);
+ resolve(args);
+ });
+ });
+}
+exports.promiseEvent = promiseEvent;
+
+function promiseDOMEvent(target, event, isCapturing = false) {
+ console.log("Waiting for " + event);
+ return new Promise(resolve => {
+ let listener = (event) => {
+ target.removeEventListener(event, listener, isCapturing);
+ resolve(event);
+ };
+ target.addEventListener(event, listener, isCapturing);
+ })
+}
+exports.promiseDOMEvent = promiseDOMEvent;
+
+const promiseEventOnItemAndContainer = async(function*(assert, itemport, container, event, item = itemport) {
+ let itemEvent = promiseEvent(itemport, event);
+ let containerEvent = promiseEvent(container, event);
+
+ let itemArgs = yield itemEvent;
+ let containerArgs = yield containerEvent;
+
+ assert.equal(containerArgs[0], item, "Should have seen a container event for the right item");
+ assert.equal(JSON.stringify(itemArgs), JSON.stringify(containerArgs), "Arguments should have matched");
+
+ // Strip off the item from the returned arguments
+ return itemArgs.slice(1);
+});
+exports.promiseEventOnItemAndContainer = promiseEventOnItemAndContainer;
+
+const waitForProcesses = async(function*(loader) {
+ console.log("Starting remote");
+ let { processes, frames, remoteRequire } = loader.require('sdk/remote/parent');
+ remoteRequire(REMOTE_MODULE, module);
+
+ let events = [];
+
+ // In e10s we should expect to see two processes
+ let expectedCount = isE10S ? 2 : 1;
+
+ yield new Promise(resolve => {
+ let count = 0;
+
+ // Wait for a process to be detected
+ let listener = process => {
+ console.log("Saw a process attach");
+ // Wait for the remote module to load in this process
+ process.port.once('sdk/test/load', () => {
+ console.log("Saw a remote module load");
+ count++;
+ if (count == expectedCount) {
+ processes.off('attach', listener);
+ resolve();
+ }
+ });
+ }
+ processes.on('attach', listener);
+ });
+
+ console.log("Remote ready");
+ return { processes, frames, remoteRequire };
+});
+exports.waitForProcesses = waitForProcesses;
+
+// Counts the frames in all the child processes
+const getChildFrameCount = async(function*(processes) {
+ let frameCount = 0;
+
+ for (let process of processes) {
+ process.port.emit('sdk/test/count');
+ let [p, count] = yield promiseEvent(process.port, 'sdk/test/count');
+ frameCount += count;
+ }
+
+ return frameCount;
+});
+exports.getChildFrameCount = getChildFrameCount;
+
+const mainWindow = getMostRecentBrowserWindow();
+const isE10S = mainWindow.gMultiProcessBrowser;
+exports.isE10S = isE10S;
+
+if (isE10S) {
+ console.log("Testing in E10S mode");
+ // We expect a child process to already be present, make sure that is the case
+ mainWindow.XULBrowserWindow.forceInitialBrowserRemote();
+}
+else {
+ console.log("Testing in non-E10S mode");
+}
diff --git a/addon-sdk/source/test/addons/require/list.js b/addon-sdk/source/test/addons/require/list.js
new file mode 100644
index 000000000..9d2566a87
--- /dev/null
+++ b/addon-sdk/source/test/addons/require/list.js
@@ -0,0 +1,6 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+exports.local = true;
diff --git a/addon-sdk/source/test/addons/require/main.js b/addon-sdk/source/test/addons/require/main.js
new file mode 100644
index 000000000..53391f08b
--- /dev/null
+++ b/addon-sdk/source/test/addons/require/main.js
@@ -0,0 +1,87 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+var { isNative } = require("@loader/options");
+
+exports["test local vs sdk module"] = function (assert) {
+ assert.notEqual(require("list"),
+ require("sdk/util/list"),
+ "Local module takes the priority over sdk modules");
+ assert.ok(require("list").local,
+ "this module is really the local one");
+}
+
+if (!isNative) {
+ exports["test 3rd party vs sdk module"] = function (assert) {
+ // We are testing with a 3rd party package called `tabs` with 3 modules
+ // main, page-mod and third-party
+
+ // the only way to require 3rd party package modules are to use absolute paths
+ // require("tabs/main"), require("tabs/page-mod"),
+ // require("tabs/third-party") and also require("tabs") which will refer
+ // to tabs's main package module.
+
+ // So require(page-mod) shouldn't map the 3rd party
+ assert.equal(require("page-mod"),
+ require("sdk/page-mod"),
+ "Third party modules don't overload sdk modules");
+
+ assert.ok(require("page-mod").PageMod,
+ "page-mod module is really the sdk one");
+
+ assert.equal(require("tabs/page-mod").id, "page-mod",
+ "tabs/page-mod is the 3rd party");
+
+ // But require(tabs) will map to 3rd party main module
+ // *and* overload the sdk module
+ // and also any local module with the same name
+ assert.equal(require("tabs").id, "tabs-main",
+ "Third party main module overload sdk modules");
+
+ assert.equal(require("tabs"),
+ require("tabs/main"),
+ "require(tabs) maps to require(tabs/main)");
+
+ // So that you have to use relative path to ensure getting the local module
+ assert.equal(require("./tabs").id,
+ "local-tabs",
+ "require(./tabs) maps to the local module");
+
+ // It should still be possible to require sdk module with absolute path
+ assert.ok(require("sdk/tabs").open,
+ "We can bypass this overloading with absolute path to sdk modules");
+
+ assert.equal(require("sdk/tabs"),
+ require("addon-kit/tabs"),
+ "Old and new layout both work");
+ }
+}
+
+// /!\ Always use distinct module for each test.
+// Otherwise, the linker can correctly parse and allow the first usage of it
+// but still silently fail on the second.
+
+exports.testRelativeRequire = function (assert) {
+ assert.equal(require('./same-folder').id, 'same-folder');
+}
+
+exports.testRelativeSubFolderRequire = function (assert) {
+ assert.equal(require('./sub-folder/module').id, 'sub-folder');
+}
+
+exports.testMultipleRequirePerLine = function (assert) {
+ var a=require('./multiple/a'),b=require('./multiple/b');
+ assert.equal(a.id, 'a');
+ assert.equal(b.id, 'b');
+}
+
+exports.testSDKRequire = function (assert) {
+ assert.deepEqual(Object.keys(require('sdk/page-worker')), ['Page']);
+ if (!isNative) {
+ assert.equal(require('page-worker'), require('sdk/page-worker'));
+ }
+}
+
+require("sdk/test/runner").runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/require/multiple/a.js b/addon-sdk/source/test/addons/require/multiple/a.js
new file mode 100644
index 000000000..737cce2b0
--- /dev/null
+++ b/addon-sdk/source/test/addons/require/multiple/a.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+exports.id = 'a';
diff --git a/addon-sdk/source/test/addons/require/multiple/b.js b/addon-sdk/source/test/addons/require/multiple/b.js
new file mode 100644
index 000000000..382a7d22c
--- /dev/null
+++ b/addon-sdk/source/test/addons/require/multiple/b.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+exports.id = 'b';
diff --git a/addon-sdk/source/test/addons/require/package.json b/addon-sdk/source/test/addons/require/package.json
new file mode 100644
index 000000000..828853f56
--- /dev/null
+++ b/addon-sdk/source/test/addons/require/package.json
@@ -0,0 +1,8 @@
+{
+ "id": "test-require@jetpack",
+ "name": "test-require",
+ "packages": "packages",
+ "ignore-deprecated-path": true,
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/require/packages/tabs/main.js b/addon-sdk/source/test/addons/require/packages/tabs/main.js
new file mode 100644
index 000000000..871c9e4de
--- /dev/null
+++ b/addon-sdk/source/test/addons/require/packages/tabs/main.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+exports.id = "tabs-main";
diff --git a/addon-sdk/source/test/addons/require/packages/tabs/package.json b/addon-sdk/source/test/addons/require/packages/tabs/package.json
new file mode 100644
index 000000000..2446c2e53
--- /dev/null
+++ b/addon-sdk/source/test/addons/require/packages/tabs/package.json
@@ -0,0 +1,3 @@
+{
+ "id": "test-panel"
+} \ No newline at end of file
diff --git a/addon-sdk/source/test/addons/require/packages/tabs/page-mod.js b/addon-sdk/source/test/addons/require/packages/tabs/page-mod.js
new file mode 100644
index 000000000..6c90f46c1
--- /dev/null
+++ b/addon-sdk/source/test/addons/require/packages/tabs/page-mod.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+exports.id = "page-mod";
diff --git a/addon-sdk/source/test/addons/require/same-folder.js b/addon-sdk/source/test/addons/require/same-folder.js
new file mode 100644
index 000000000..d2f9b017d
--- /dev/null
+++ b/addon-sdk/source/test/addons/require/same-folder.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+exports.id = 'same-folder';
diff --git a/addon-sdk/source/test/addons/require/sub-folder/module.js b/addon-sdk/source/test/addons/require/sub-folder/module.js
new file mode 100644
index 000000000..8ce8181b2
--- /dev/null
+++ b/addon-sdk/source/test/addons/require/sub-folder/module.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+exports.id = 'sub-folder';
diff --git a/addon-sdk/source/test/addons/require/tabs.js b/addon-sdk/source/test/addons/require/tabs.js
new file mode 100644
index 000000000..5a46e63b8
--- /dev/null
+++ b/addon-sdk/source/test/addons/require/tabs.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+exports.id = "local-tabs";
diff --git a/addon-sdk/source/test/addons/self/data/data.md b/addon-sdk/source/test/addons/self/data/data.md
new file mode 100644
index 000000000..2be7c65ae
--- /dev/null
+++ b/addon-sdk/source/test/addons/self/data/data.md
@@ -0,0 +1 @@
+# hello world
diff --git a/addon-sdk/source/test/addons/self/main.js b/addon-sdk/source/test/addons/self/main.js
new file mode 100644
index 000000000..789f27899
--- /dev/null
+++ b/addon-sdk/source/test/addons/self/main.js
@@ -0,0 +1,23 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const self = require("sdk/self");
+
+exports["test self.data.load"] = assert => {
+
+ assert.equal(self.data.load("data.md").trim(),
+ "# hello world",
+ "paths work");
+
+ assert.equal(self.data.load("./data.md").trim(),
+ "# hello world",
+ "relative paths work");
+};
+
+exports["test self.id"] = assert => {
+ assert.equal(self.id, "test-self@jetpack", "self.id should be correct.");
+};
+
+require("sdk/test/runner").runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/self/package.json b/addon-sdk/source/test/addons/self/package.json
new file mode 100644
index 000000000..fec21298b
--- /dev/null
+++ b/addon-sdk/source/test/addons/self/package.json
@@ -0,0 +1,5 @@
+{
+ "id": "test-self@jetpack",
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/simple-prefs-l10n/locale/en.properties b/addon-sdk/source/test/addons/simple-prefs-l10n/locale/en.properties
new file mode 100644
index 000000000..ec458f1f7
--- /dev/null
+++ b/addon-sdk/source/test/addons/simple-prefs-l10n/locale/en.properties
@@ -0,0 +1,5 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+somePreference_title=A
diff --git a/addon-sdk/source/test/addons/simple-prefs-l10n/main.js b/addon-sdk/source/test/addons/simple-prefs-l10n/main.js
new file mode 100644
index 000000000..ce8235d19
--- /dev/null
+++ b/addon-sdk/source/test/addons/simple-prefs-l10n/main.js
@@ -0,0 +1,65 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Cu } = require('chrome');
+const sp = require('sdk/simple-prefs');
+const app = require('sdk/system/xul-app');
+const self = require('sdk/self');
+const tabs = require('sdk/tabs');
+const { preferencesBranch } = require('sdk/self');
+
+const { AddonManager } = Cu.import('resource://gre/modules/AddonManager.jsm', {});
+
+// Once Bug 903018 is resolved, just move the application testing to
+// module.metadata.engines
+//
+// This should work in Fennec, needs to be refactored to work, via bug 979645
+if (app.is('Firefox')) {
+ exports.testAOMLocalization = function(assert, done) {
+ tabs.open({
+ url: 'about:addons',
+ onReady: function(tab) {
+ tab.attach({
+ contentScriptWhen: 'end',
+ contentScript: 'function onLoad() {\n' +
+ 'unsafeWindow.removeEventListener("load", onLoad, false);\n' +
+ 'AddonManager.getAddonByID("' + self.id + '", function(aAddon) {\n' +
+ 'unsafeWindow.gViewController.viewObjects.detail.node.addEventListener("ViewChanged", function whenViewChanges() {\n' +
+ 'unsafeWindow.gViewController.viewObjects.detail.node.removeEventListener("ViewChanged", whenViewChanges, false);\n' +
+ 'setTimeout(function() {\n' + // TODO: figure out why this is necessary..
+ 'self.postMessage({\n' +
+ 'somePreference: getAttributes(unsafeWindow.document.querySelector("setting[data-jetpack-id=\'' + self.id + '\']"))\n' +
+ '});\n' +
+ '}, 250);\n' +
+ '}, false);\n' +
+ 'unsafeWindow.gViewController.commands.cmd_showItemDetails.doCommand(aAddon, true);\n' +
+ '});\n' +
+ 'function getAttributes(ele) {\n' +
+ 'if (!ele) return {};\n' +
+ 'return {\n' +
+ 'title: ele.getAttribute("title")\n' +
+ '}\n' +
+ '}\n' +
+ '}\n' +
+ // Wait for the load event ?
+ 'if (document.readyState == "complete") {\n' +
+ 'onLoad()\n' +
+ '} else {\n' +
+ 'unsafeWindow.addEventListener("load", onLoad, false);\n' +
+ '}\n',
+ onMessage: function(msg) {
+ // test somePreference
+ assert.equal(msg.somePreference.title, 'A', 'somePreference title is correct');
+ tab.close(done);
+ }
+ });
+ }
+ });
+ }
+} else {
+ exports['test unsupported'] = (assert) => assert.pass('This test is unsupported.');
+}
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/simple-prefs-l10n/package.json b/addon-sdk/source/test/addons/simple-prefs-l10n/package.json
new file mode 100644
index 000000000..540033cf3
--- /dev/null
+++ b/addon-sdk/source/test/addons/simple-prefs-l10n/package.json
@@ -0,0 +1,10 @@
+{
+ "id": "test-simple-prefs-l10n",
+ "preferences": [{
+ "name": "somePreference",
+ "title": "some-title",
+ "description": "Some short description for the preference",
+ "type": "string",
+ "value": "TEST"
+ }]
+} \ No newline at end of file
diff --git a/addon-sdk/source/test/addons/simple-prefs-regression/app-extension/application.ini b/addon-sdk/source/test/addons/simple-prefs-regression/app-extension/application.ini
new file mode 100644
index 000000000..6cec69a16
--- /dev/null
+++ b/addon-sdk/source/test/addons/simple-prefs-regression/app-extension/application.ini
@@ -0,0 +1,11 @@
+[App]
+Vendor=Varma
+Name=Test App
+Version=1.0
+BuildID=20060101
+Copyright=Copyright (c) 2009 Atul Varma
+ID=xulapp@toolness.com
+
+[Gecko]
+MinVersion=1.9.2.0
+MaxVersion=2.0.*
diff --git a/addon-sdk/source/test/addons/simple-prefs-regression/app-extension/bootstrap.js b/addon-sdk/source/test/addons/simple-prefs-regression/app-extension/bootstrap.js
new file mode 100644
index 000000000..fbb9b5186
--- /dev/null
+++ b/addon-sdk/source/test/addons/simple-prefs-regression/app-extension/bootstrap.js
@@ -0,0 +1,339 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// @see http://dxr.mozilla.org/mozilla-central/source/js/src/xpconnect/loader/mozJSComponentLoader.cpp
+
+'use strict';
+
+// IMPORTANT: Avoid adding any initialization tasks here, if you need to do
+// something before add-on is loaded consider addon/runner module instead!
+
+const { classes: Cc, Constructor: CC, interfaces: Ci, utils: Cu,
+ results: Cr, manager: Cm } = Components;
+const ioService = Cc['@mozilla.org/network/io-service;1'].
+ getService(Ci.nsIIOService);
+const resourceHandler = ioService.getProtocolHandler('resource').
+ QueryInterface(Ci.nsIResProtocolHandler);
+const systemPrincipal = CC('@mozilla.org/systemprincipal;1', 'nsIPrincipal')();
+const scriptLoader = Cc['@mozilla.org/moz/jssubscript-loader;1'].
+ getService(Ci.mozIJSSubScriptLoader);
+const prefService = Cc['@mozilla.org/preferences-service;1'].
+ getService(Ci.nsIPrefService).
+ QueryInterface(Ci.nsIPrefBranch);
+const appInfo = Cc["@mozilla.org/xre/app-info;1"].
+ getService(Ci.nsIXULAppInfo);
+const vc = Cc["@mozilla.org/xpcom/version-comparator;1"].
+ getService(Ci.nsIVersionComparator);
+
+const { NetUtil } = Cu.import("resource://gre/modules/NetUtil.jsm");
+
+const REASON = [ 'unknown', 'startup', 'shutdown', 'enable', 'disable',
+ 'install', 'uninstall', 'upgrade', 'downgrade' ];
+
+const bind = Function.call.bind(Function.bind);
+
+var loader = null;
+var unload = null;
+var cuddlefishSandbox = null;
+var nukeTimer = null;
+
+// Utility function that synchronously reads local resource from the given
+// `uri` and returns content string.
+function readURI(uri) {
+ let channel = NetUtil.newChannel({
+ uri: NetUtil.newURI(uri, "UTF-8"),
+ loadUsingSystemPrincipal: true
+ });
+ let stream = channel.open2();
+
+ let cstream = Cc['@mozilla.org/intl/converter-input-stream;1'].
+ createInstance(Ci.nsIConverterInputStream);
+ cstream.init(stream, 'UTF-8', 0, 0);
+
+ let str = {};
+ let data = '';
+ let read = 0;
+ do {
+ read = cstream.readString(0xffffffff, str);
+ data += str.value;
+ } while (read != 0);
+
+ cstream.close();
+
+ return data;
+}
+
+// We don't do anything on install & uninstall yet, but in a future
+// we should allow add-ons to cleanup after uninstall.
+function install(data, reason) {}
+function uninstall(data, reason) {}
+
+function startup(data, reasonCode) {
+ try {
+ let reason = REASON[reasonCode];
+ // URI for the root of the XPI file.
+ // 'jar:' URI if the addon is packed, 'file:' URI otherwise.
+ // (Used by l10n module in order to fetch `locale` folder)
+ let rootURI = data.resourceURI.spec;
+
+ // TODO: Maybe we should perform read harness-options.json asynchronously,
+ // since we can't do anything until 'sessionstore-windows-restored' anyway.
+ let options = JSON.parse(readURI(rootURI + './harness-options.json'));
+
+ let id = options.jetpackID;
+ let name = options.name;
+
+ // Clean the metadata
+ options.metadata[name]['permissions'] = options.metadata[name]['permissions'] || {};
+
+ // freeze the permissionss
+ Object.freeze(options.metadata[name]['permissions']);
+ // freeze the metadata
+ Object.freeze(options.metadata[name]);
+
+ // Register a new resource 'domain' for this addon which is mapping to
+ // XPI's `resources` folder.
+ // Generate the domain name by using jetpack ID, which is the extension ID
+ // by stripping common characters that doesn't work as a domain name:
+ let uuidRe =
+ /^\{([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\}$/;
+
+ let domain = id.
+ toLowerCase().
+ replace(/@/g, '-at-').
+ replace(/\./g, '-dot-').
+ replace(uuidRe, '$1');
+
+ let prefixURI = 'resource://' + domain + '/';
+ let resourcesURI = ioService.newURI(rootURI + '/resources/', null, null);
+ resourceHandler.setSubstitution(domain, resourcesURI);
+
+ // Create path to URLs mapping supported by loader.
+ let paths = {
+ // Relative modules resolve to add-on package lib
+ './': prefixURI + name + '/lib/',
+ './tests/': prefixURI + name + '/tests/',
+ '': 'resource://gre/modules/commonjs/'
+ };
+
+ // Maps addon lib and tests ressource folders for each package
+ paths = Object.keys(options.metadata).reduce(function(result, name) {
+ result[name + '/'] = prefixURI + name + '/lib/'
+ result[name + '/tests/'] = prefixURI + name + '/tests/'
+ return result;
+ }, paths);
+
+ // We need to map tests folder when we run sdk tests whose package name
+ // is stripped
+ if (name == 'addon-sdk')
+ paths['tests/'] = prefixURI + name + '/tests/';
+
+ let useBundledSDK = options['force-use-bundled-sdk'];
+ if (!useBundledSDK) {
+ try {
+ useBundledSDK = prefService.getBoolPref("extensions.addon-sdk.useBundledSDK");
+ }
+ catch (e) {
+ // Pref doesn't exist, allow using Firefox shipped SDK
+ }
+ }
+
+ // Starting with Firefox 21.0a1, we start using modules shipped into firefox
+ // Still allow using modules from the xpi if the manifest tell us to do so.
+ // And only try to look for sdk modules in xpi if the xpi actually ship them
+ if (options['is-sdk-bundled'] &&
+ (vc.compare(appInfo.version, '21.0a1') < 0 || useBundledSDK)) {
+ // Maps sdk module folders to their resource folder
+ paths[''] = prefixURI + 'addon-sdk/lib/';
+ // test.js is usually found in root commonjs or SDK_ROOT/lib/ folder,
+ // so that it isn't shipped in the xpi. Keep a copy of it in sdk/ folder
+ // until we no longer support SDK modules in XPI:
+ paths['test'] = prefixURI + 'addon-sdk/lib/sdk/test.js';
+ }
+
+ // Retrieve list of module folder overloads based on preferences in order to
+ // eventually used a local modules instead of files shipped into Firefox.
+ let branch = prefService.getBranch('extensions.modules.' + id + '.path');
+ paths = branch.getChildList('', {}).reduce(function (result, name) {
+ // Allows overloading of any sub folder by replacing . by / in pref name
+ let path = name.substr(1).split('.').join('/');
+ // Only accept overloading folder by ensuring always ending with `/`
+ if (path) path += '/';
+ let fileURI = branch.getCharPref(name);
+
+ // On mobile, file URI has to end with a `/` otherwise, setSubstitution
+ // takes the parent folder instead.
+ if (fileURI[fileURI.length-1] !== '/')
+ fileURI += '/';
+
+ // Maps the given file:// URI to a resource:// in order to avoid various
+ // failure that happens with file:// URI and be close to production env
+ let resourcesURI = ioService.newURI(fileURI, null, null);
+ let resName = 'extensions.modules.' + domain + '.commonjs.path' + name;
+ resourceHandler.setSubstitution(resName, resourcesURI);
+
+ result[path] = 'resource://' + resName + '/';
+ return result;
+ }, paths);
+
+ // Make version 2 of the manifest
+ let manifest = options.manifest;
+
+ // Import `cuddlefish.js` module using a Sandbox and bootstrap loader.
+ let cuddlefishPath = 'loader/cuddlefish.js';
+ let cuddlefishURI = 'resource://gre/modules/commonjs/sdk/' + cuddlefishPath;
+ if (paths['sdk/']) { // sdk folder has been overloaded
+ // (from pref, or cuddlefish is still in the xpi)
+ cuddlefishURI = paths['sdk/'] + cuddlefishPath;
+ }
+ else if (paths['']) { // root modules folder has been overloaded
+ cuddlefishURI = paths[''] + 'sdk/' + cuddlefishPath;
+ }
+
+ cuddlefishSandbox = loadSandbox(cuddlefishURI);
+ let cuddlefish = cuddlefishSandbox.exports;
+
+ // Normalize `options.mainPath` so that it looks like one that will come
+ // in a new version of linker.
+ let main = options.mainPath;
+
+ unload = cuddlefish.unload;
+ loader = cuddlefish.Loader({
+ paths: paths,
+ // modules manifest.
+ manifest: manifest,
+
+ // Add-on ID used by different APIs as a unique identifier.
+ id: id,
+ // Add-on name.
+ name: name,
+ // Add-on version.
+ version: options.metadata[name].version,
+ // Add-on package descriptor.
+ metadata: options.metadata[name],
+ // Add-on load reason.
+ loadReason: reason,
+
+ prefixURI: prefixURI,
+ // Add-on URI.
+ rootURI: rootURI,
+ // options used by system module.
+ // File to write 'OK' or 'FAIL' (exit code emulation).
+ resultFile: options.resultFile,
+ // Arguments passed as --static-args
+ staticArgs: options.staticArgs,
+
+ // Arguments related to test runner.
+ modules: {
+ '@test/options': {
+ allTestModules: options.allTestModules,
+ iterations: options.iterations,
+ filter: options.filter,
+ profileMemory: options.profileMemory,
+ stopOnError: options.stopOnError,
+ verbose: options.verbose,
+ parseable: options.parseable,
+ checkMemory: options.check_memory,
+ }
+ }
+ });
+
+ let module = cuddlefish.Module('sdk/loader/cuddlefish', cuddlefishURI);
+ let require = cuddlefish.Require(loader, module);
+
+ require('sdk/addon/runner').startup(reason, {
+ loader: loader,
+ main: main,
+ prefsURI: rootURI + 'defaults/preferences/prefs.js'
+ });
+ } catch (error) {
+ dump('Bootstrap error: ' +
+ (error.message ? error.message : String(error)) + '\n' +
+ (error.stack || error.fileName + ': ' + error.lineNumber) + '\n');
+ throw error;
+ }
+};
+
+function loadSandbox(uri) {
+ let proto = {
+ sandboxPrototype: {
+ loadSandbox: loadSandbox,
+ ChromeWorker: ChromeWorker
+ }
+ };
+ let sandbox = Cu.Sandbox(systemPrincipal, proto);
+ // Create a fake commonjs environnement just to enable loading loader.js
+ // correctly
+ sandbox.exports = {};
+ sandbox.module = { uri: uri, exports: sandbox.exports };
+ sandbox.require = function (id) {
+ if (id !== "chrome")
+ throw new Error("Bootstrap sandbox `require` method isn't implemented.");
+
+ return Object.freeze({ Cc: Cc, Ci: Ci, Cu: Cu, Cr: Cr, Cm: Cm,
+ CC: bind(CC, Components), components: Components,
+ ChromeWorker: ChromeWorker });
+ };
+ scriptLoader.loadSubScript(uri, sandbox, 'UTF-8');
+ return sandbox;
+}
+
+function unloadSandbox(sandbox) {
+ if (Cu.getClassName(sandbox, true) == "Sandbox")
+ Cu.nukeSandbox(sandbox);
+}
+
+function setTimeout(callback, delay) {
+ let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
+ timer.initWithCallback({ notify: callback }, delay,
+ Ci.nsITimer.TYPE_ONE_SHOT);
+ return timer;
+}
+
+function shutdown(data, reasonCode) {
+ let reason = REASON[reasonCode];
+ if (loader) {
+ unload(loader, reason);
+ unload = null;
+
+ // Don't waste time cleaning up if the application is shutting down
+ if (reason != "shutdown") {
+ // Avoid leaking all modules when something goes wrong with one particular
+ // module. Do not clean it up immediatly in order to allow executing some
+ // actions on addon disabling.
+ // We need to keep a reference to the timer, otherwise it is collected
+ // and won't ever fire.
+ nukeTimer = setTimeout(nukeModules, 1000);
+ }
+ }
+};
+
+function nukeModules() {
+ nukeTimer = null;
+ // module objects store `exports` which comes from sandboxes
+ // We should avoid keeping link to these object to avoid leaking sandboxes
+ for (let key in loader.modules) {
+ delete loader.modules[key];
+ }
+ // Direct links to sandboxes should be removed too
+ for (let key in loader.sandboxes) {
+ let sandbox = loader.sandboxes[key];
+ delete loader.sandboxes[key];
+ // Bug 775067: From FF17 we can kill all CCW from a given sandbox
+ unloadSandbox(sandbox);
+ }
+ loader = null;
+
+ // both `toolkit/loader` and `system/xul-app` are loaded as JSM's via
+ // `cuddlefish.js`, and needs to be unloaded to avoid memory leaks, when
+ // the addon is unload.
+
+ unloadSandbox(cuddlefishSandbox.loaderSandbox);
+ unloadSandbox(cuddlefishSandbox.xulappSandbox);
+
+ // Bug 764840: We need to unload cuddlefish otherwise it will stay alive
+ // and keep a reference to this compartment.
+ unloadSandbox(cuddlefishSandbox);
+ cuddlefishSandbox = null;
+}
diff --git a/addon-sdk/source/test/addons/simple-prefs-regression/app-extension/defaults/preferences/prefs.js b/addon-sdk/source/test/addons/simple-prefs-regression/app-extension/defaults/preferences/prefs.js
new file mode 100644
index 000000000..86f724a53
--- /dev/null
+++ b/addon-sdk/source/test/addons/simple-prefs-regression/app-extension/defaults/preferences/prefs.js
@@ -0,0 +1,7 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+pref("extensions.test-simple-prefs@jetpack.somePreference", "TEST");
+pref("extensions.test-simple-prefs@jetpack.myInteger", 8);
+pref("extensions.test-simple-prefs@jetpack.myHiddenInt", 5);
diff --git a/addon-sdk/source/test/addons/simple-prefs-regression/app-extension/install.rdf b/addon-sdk/source/test/addons/simple-prefs-regression/app-extension/install.rdf
new file mode 100644
index 000000000..5e3aae0d7
--- /dev/null
+++ b/addon-sdk/source/test/addons/simple-prefs-regression/app-extension/install.rdf
@@ -0,0 +1,34 @@
+<?xml version="1.0"?>
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+
+<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ xmlns:em="http://www.mozilla.org/2004/em-rdf#">
+ <Description about="urn:mozilla:install-manifest">
+ <em:id>xulapp@toolness.com</em:id>
+ <em:version>1.0</em:version>
+ <em:type>2</em:type>
+ <em:bootstrap>true</em:bootstrap>
+ <em:unpack>false</em:unpack>
+
+ <!-- Firefox -->
+ <em:targetApplication>
+ <Description>
+ <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
+ <em:minVersion>21.0</em:minVersion>
+ <em:maxVersion>25.0a1</em:maxVersion>
+ </Description>
+ </em:targetApplication>
+
+ <!-- Front End MetaData -->
+ <em:name>Test App</em:name>
+ <em:description>Harness for tests.</em:description>
+ <em:creator>Mozilla Corporation</em:creator>
+ <em:homepageURL></em:homepageURL>
+ <em:optionsType></em:optionsType>
+ <em:optionsURL></em:optionsURL>
+ <em:updateURL></em:updateURL>
+ </Description>
+</RDF>
diff --git a/addon-sdk/source/test/addons/simple-prefs-regression/app-extension/options.xul b/addon-sdk/source/test/addons/simple-prefs-regression/app-extension/options.xul
new file mode 100644
index 000000000..89b95e8d7
--- /dev/null
+++ b/addon-sdk/source/test/addons/simple-prefs-regression/app-extension/options.xul
@@ -0,0 +1,5 @@
+<?xml version="1.0" ?>
+<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+ <setting data-jetpack-id="test-simple-prefs@jetpack" pref="extensions.test-simple-prefs@jetpack.somePreference" pref-name="somePreference" title="some-title" type="string">Some short description for the preference</setting>
+ <setting data-jetpack-id="test-simple-prefs@jetpack" pref="extensions.test-simple-prefs@jetpack.myInteger" pref-name="myInteger" title="my-int" type="integer">How many of them we have.</setting>
+</vbox>
diff --git a/addon-sdk/source/test/addons/simple-prefs-regression/lib/main.js b/addon-sdk/source/test/addons/simple-prefs-regression/lib/main.js
new file mode 100644
index 000000000..757759dcb
--- /dev/null
+++ b/addon-sdk/source/test/addons/simple-prefs-regression/lib/main.js
@@ -0,0 +1,94 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Cu } = require('chrome');
+const sp = require('sdk/simple-prefs');
+const app = require('sdk/system/xul-app');
+const tabs = require('sdk/tabs');
+const { preferencesBranch, id } = require('sdk/self');
+const { getAddonByID } = require('sdk/addon/manager');
+const { AddonManager } = Cu.import('resource://gre/modules/AddonManager.jsm', {});
+
+exports.testRegression = (assert) => {
+ assert.equal(preferencesBranch, id, 'preferencesBranch returns id here');
+}
+
+exports.testDefaultValues = (assert) => {
+ assert.equal(sp.prefs.myHiddenInt, 5, 'myHiddenInt default is 5');
+ assert.equal(sp.prefs.myInteger, 8, 'myInteger default is 8');
+ assert.equal(sp.prefs.somePreference, 'TEST', 'somePreference default is correct');
+}
+
+exports.testOptionsType = function*(assert) {
+ let addon = yield getAddonByID(id);
+ assert.equal(addon.optionsType, AddonManager.OPTIONS_TYPE_INLINE, 'options type is inline');
+}
+
+if (app.is('Firefox')) {
+ exports.testAOM = function(assert, done) {
+ tabs.open({
+ url: 'about:addons',
+ onReady: function(tab) {
+ tab.attach({
+ contentScriptWhen: 'end',
+ contentScript: 'function onLoad() {\n' +
+ 'unsafeWindow.removeEventListener("load", onLoad, false);\n' +
+ 'AddonManager.getAddonByID("' + id + '", function(aAddon) {\n' +
+ 'unsafeWindow.gViewController.viewObjects.detail.node.addEventListener("ViewChanged", function whenViewChanges() {\n' +
+ 'unsafeWindow.gViewController.viewObjects.detail.node.removeEventListener("ViewChanged", whenViewChanges, false);\n' +
+ 'setTimeout(function() {\n' + // TODO: figure out why this is necessary..
+ 'self.postMessage({\n' +
+ 'somePreference: getAttributes(unsafeWindow.document.querySelector("setting[title=\'some-title\']")),\n' +
+ 'myInteger: getAttributes(unsafeWindow.document.querySelector("setting[title=\'my-int\']")),\n' +
+ 'myHiddenInt: getAttributes(unsafeWindow.document.querySelector("setting[title=\'hidden-int\']"))\n' +
+ '});\n' +
+ '}, 250);\n' +
+ '}, false);\n' +
+ 'unsafeWindow.gViewController.commands.cmd_showItemDetails.doCommand(aAddon, true);\n' +
+ '});\n' +
+ 'function getAttributes(ele) {\n' +
+ 'if (!ele) return {};\n' +
+ 'return {\n' +
+ 'pref: ele.getAttribute("pref"),\n' +
+ 'type: ele.getAttribute("type"),\n' +
+ 'title: ele.getAttribute("title"),\n' +
+ 'desc: ele.getAttribute("desc")\n' +
+ '}\n' +
+ '}\n' +
+ '}\n' +
+ // Wait for the load event ?
+ 'if (document.readyState == "complete") {\n' +
+ 'onLoad()\n' +
+ '} else {\n' +
+ 'unsafeWindow.addEventListener("load", onLoad, false);\n' +
+ '}\n',
+ onMessage: function(msg) {
+ // test somePreference
+ assert.equal(msg.somePreference.type, 'string', 'some pref is a string');
+ assert.equal(msg.somePreference.pref, 'extensions.'+preferencesBranch+'.somePreference', 'somePreference path is correct');
+ assert.equal(msg.somePreference.title, 'some-title', 'somePreference title is correct');
+ assert.equal(msg.somePreference.desc, 'Some short description for the preference', 'somePreference description is correct');
+
+ // test myInteger
+ assert.equal(msg.myInteger.type, 'integer', 'myInteger is a int');
+ assert.equal(msg.myInteger.pref, 'extensions.'+preferencesBranch+'.myInteger', 'extensions.test-simple-prefs.myInteger');
+ assert.equal(msg.myInteger.title, 'my-int', 'myInteger title is correct');
+ assert.equal(msg.myInteger.desc, 'How many of them we have.', 'myInteger desc is correct');
+
+ // test myHiddenInt
+ assert.equal(msg.myHiddenInt.type, undefined, 'myHiddenInt was not displayed');
+ assert.equal(msg.myHiddenInt.pref, undefined, 'myHiddenInt was not displayed');
+ assert.equal(msg.myHiddenInt.title, undefined, 'myHiddenInt was not displayed');
+ assert.equal(msg.myHiddenInt.desc, undefined, 'myHiddenInt was not displayed');
+
+ tab.close(done);
+ }
+ });
+ }
+ });
+ }
+}
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/simple-prefs-regression/package.json b/addon-sdk/source/test/addons/simple-prefs-regression/package.json
new file mode 100644
index 000000000..1de5e8ac2
--- /dev/null
+++ b/addon-sdk/source/test/addons/simple-prefs-regression/package.json
@@ -0,0 +1,24 @@
+{
+ "id": "test-simple-prefs",
+ "preferences": [{
+ "name": "somePreference",
+ "title": "some-title",
+ "description": "Some short description for the preference",
+ "type": "string",
+ "value": "TEST"
+ },
+ {
+ "description": "How many of them we have.",
+ "name": "myInteger",
+ "type": "integer",
+ "value": 8,
+ "title": "my-int"
+ }, {
+ "name": "myHiddenInt",
+ "type": "integer",
+ "hidden": true,
+ "value": 5,
+ "title": "hidden-int"
+ }],
+ "preferences-branch": "simple-prefs-regression"
+}
diff --git a/addon-sdk/source/test/addons/simple-prefs/lib/main.js b/addon-sdk/source/test/addons/simple-prefs/lib/main.js
new file mode 100644
index 000000000..65d25b381
--- /dev/null
+++ b/addon-sdk/source/test/addons/simple-prefs/lib/main.js
@@ -0,0 +1,109 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Cu } = require('chrome');
+const sp = require('sdk/simple-prefs');
+const app = require('sdk/system/xul-app');
+const { id, preferencesBranch } = require('sdk/self');
+const { open } = require('sdk/preferences/utils');
+const { getTabForId } = require('sdk/tabs/utils');
+const { modelFor } = require('sdk/model/core');
+const { getAddonByID } = require('sdk/addon/manager');
+const { AddonManager } = Cu.import('resource://gre/modules/AddonManager.jsm', {});
+require('sdk/tabs');
+
+exports.testDefaultValues = function (assert) {
+ assert.equal(sp.prefs.myHiddenInt, 5, 'myHiddenInt default is 5');
+ assert.equal(sp.prefs.myInteger, 8, 'myInteger default is 8');
+ assert.equal(sp.prefs.somePreference, 'TEST', 'somePreference default is correct');
+}
+
+exports.testOptionsType = function*(assert) {
+ let addon = yield getAddonByID(id);
+ assert.equal(addon.optionsType, AddonManager.OPTIONS_TYPE_INLINE, 'options type is inline');
+}
+
+exports.testButton = function(assert, done) {
+ open({ id: id }).then(({ tabId, document }) => {
+ let tab = modelFor(getTabForId(tabId));
+ sp.once('sayHello', _ => {
+ assert.pass('The button was pressed!');
+ tab.close(done);
+ });
+
+ tab.attach({
+ contentScript: 'unsafeWindow.document.querySelector("button[label=\'Click me!\']").click();'
+ });
+ });
+}
+
+if (app.is('Firefox')) {
+ exports.testAOM = function(assert, done) {
+ open({ id: id }).then(({ tabId }) => {
+ let tab = modelFor(getTabForId(tabId));
+ assert.pass('the add-on prefs page was opened.');
+
+ tab.attach({
+ contentScriptWhen: "end",
+ contentScript: 'self.postMessage({\n' +
+ 'someCount: unsafeWindow.document.querySelectorAll("setting[title=\'some-title\']").length,\n' +
+ 'somePreference: getAttributes(unsafeWindow.document.querySelector("setting[title=\'some-title\']")),\n' +
+ 'myInteger: getAttributes(unsafeWindow.document.querySelector("setting[title=\'my-int\']")),\n' +
+ 'myHiddenInt: getAttributes(unsafeWindow.document.querySelector("setting[title=\'hidden-int\']")),\n' +
+ 'sayHello: getAttributes(unsafeWindow.document.querySelector("button[label=\'Click me!\']"))\n' +
+ '});\n' +
+ 'function getAttributes(ele) {\n' +
+ 'if (!ele) return {};\n' +
+ 'return {\n' +
+ 'pref: ele.getAttribute("pref"),\n' +
+ 'type: ele.getAttribute("type"),\n' +
+ 'title: ele.getAttribute("title"),\n' +
+ 'desc: ele.getAttribute("desc"),\n' +
+ '"data-jetpack-id": ele.getAttribute(\'data-jetpack-id\')\n' +
+ '}\n' +
+ '}\n',
+ onMessage: msg => {
+ // test against doc caching
+ assert.equal(msg.someCount, 1, 'there is exactly one <setting> node for somePreference');
+
+ // test somePreference
+ assert.equal(msg.somePreference.type, 'string', 'some pref is a string');
+ assert.equal(msg.somePreference.pref, 'extensions.' + id + '.somePreference', 'somePreference path is correct');
+ assert.equal(msg.somePreference.title, 'some-title', 'somePreference title is correct');
+ assert.equal(msg.somePreference.desc, 'Some short description for the preference', 'somePreference description is correct');
+ assert.equal(msg.somePreference['data-jetpack-id'], id, 'data-jetpack-id attribute value is correct');
+
+ // test myInteger
+ assert.equal(msg.myInteger.type, 'integer', 'myInteger is a int');
+ assert.equal(msg.myInteger.pref, 'extensions.' + id + '.myInteger', 'extensions.test-simple-prefs.myInteger');
+ assert.equal(msg.myInteger.title, 'my-int', 'myInteger title is correct');
+ assert.equal(msg.myInteger.desc, 'How many of them we have.', 'myInteger desc is correct');
+ assert.equal(msg.myInteger['data-jetpack-id'], id, 'data-jetpack-id attribute value is correct');
+
+ // test myHiddenInt
+ assert.equal(msg.myHiddenInt.type, undefined, 'myHiddenInt was not displayed');
+ assert.equal(msg.myHiddenInt.pref, undefined, 'myHiddenInt was not displayed');
+ assert.equal(msg.myHiddenInt.title, undefined, 'myHiddenInt was not displayed');
+ assert.equal(msg.myHiddenInt.desc, undefined, 'myHiddenInt was not displayed');
+
+ // test sayHello
+ assert.equal(msg.sayHello['data-jetpack-id'], id, 'data-jetpack-id attribute value is correct');
+
+ tab.close(done);
+ }
+ });
+ })
+ }
+
+ // run it again, to test against inline options document caching
+ // and duplication of <setting> nodes upon re-entry to about:addons
+ exports.testAgainstDocCaching = exports.testAOM;
+}
+
+exports.testDefaultPreferencesBranch = function(assert) {
+ assert.equal(preferencesBranch, id, 'preferencesBranch default the same as self.id');
+}
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/simple-prefs/package.json b/addon-sdk/source/test/addons/simple-prefs/package.json
new file mode 100644
index 000000000..6fc1df79a
--- /dev/null
+++ b/addon-sdk/source/test/addons/simple-prefs/package.json
@@ -0,0 +1,32 @@
+{
+ "id": "test-simple-prefs@jetpack",
+ "preferences": [{
+ "name": "somePreference",
+ "title": "some-title",
+ "description": "Some short description for the preference",
+ "type": "string",
+ "value": "TEST"
+ },
+ {
+ "description": "How many of them we have.",
+ "name": "myInteger",
+ "type": "integer",
+ "value": 8,
+ "title": "my-int"
+ },
+ {
+ "name": "sayHello",
+ "type": "control",
+ "label": "Click me!",
+ "title": "hello"
+ },
+ {
+ "name": "myHiddenInt",
+ "type": "integer",
+ "hidden": true,
+ "value": 5,
+ "title": "hidden-int"
+ }],
+ "main": "./lib/main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/standard-id/lib/main.js b/addon-sdk/source/test/addons/standard-id/lib/main.js
new file mode 100644
index 000000000..bd1b5f9ce
--- /dev/null
+++ b/addon-sdk/source/test/addons/standard-id/lib/main.js
@@ -0,0 +1,30 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+
+const { id, preferencesBranch } = require('sdk/self');
+const simple = require('sdk/simple-prefs');
+const service = require('sdk/preferences/service');
+const { getAddonByID } = require('sdk/addon/manager');
+
+exports.testStandardID = function(assert) {
+ assert.equal(id, 'standard-id@jetpack', 'standard ID is standard');
+
+ assert.equal(simple.prefs.test13, 26, 'test13 is 26');
+
+ simple.prefs.test14 = '15';
+ assert.equal(service.get('extensions.standard-id@jetpack.test14'), '15', 'test14 is 15');
+ assert.equal(service.get('extensions.standard-id@jetpack.test14'), simple.prefs.test14, 'simple test14 also 15');
+}
+
+// from `/test/test-self.js`, adapted to `sdk/test/assert` API
+exports.testSelfID = function*(assert) {
+ assert.equal(typeof(id), 'string', 'self.id is a string');
+ assert.ok(id.length > 0, 'self.id not empty');
+ let addon = yield getAddonByID(id);
+ assert.ok(addon, 'found addon with self.id');
+}
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/standard-id/package.json b/addon-sdk/source/test/addons/standard-id/package.json
new file mode 100644
index 000000000..7a8f7a77c
--- /dev/null
+++ b/addon-sdk/source/test/addons/standard-id/package.json
@@ -0,0 +1,13 @@
+{
+ "id": "standard-id@jetpack",
+ "fullName": "standard ID test",
+ "author": "Tomislav Jovanovic",
+ "preferences": [{
+ "name": "test13",
+ "type": "integer",
+ "title": "test13",
+ "value": 26
+ }],
+ "main": "./lib/main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/tab-close-on-startup/main.js b/addon-sdk/source/test/addons/tab-close-on-startup/main.js
new file mode 100644
index 000000000..ad1e039e6
--- /dev/null
+++ b/addon-sdk/source/test/addons/tab-close-on-startup/main.js
@@ -0,0 +1,31 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { setTimeout } = require('sdk/timers');
+const tabs = require('sdk/tabs');
+
+var closeEvents = 0;
+const closeEventDetector = _ => closeEvents++;
+
+exports.testNoTabCloseOnStartup = function(assert, done) {
+ setTimeout(_ => {
+ assert.equal(closeEvents, 0, 'there were no tab close events detected');
+ tabs.open({
+ url: 'about:mozilla',
+ inBackground: true,
+ onReady: tab => tab.close(),
+ onClose: _ => {
+ assert.equal(closeEvents, 1, 'there was one tab close event detected');
+ done();
+ }
+ })
+ });
+}
+
+exports.main = function() {
+ tabs.on('close', closeEventDetector);
+
+ require("sdk/test/runner").runTestsFromModule(module);
+}
diff --git a/addon-sdk/source/test/addons/tab-close-on-startup/package.json b/addon-sdk/source/test/addons/tab-close-on-startup/package.json
new file mode 100644
index 000000000..926f28085
--- /dev/null
+++ b/addon-sdk/source/test/addons/tab-close-on-startup/package.json
@@ -0,0 +1,5 @@
+{
+ "id": "test-tabs@jetpack",
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/toolkit-require-reload/main.js b/addon-sdk/source/test/addons/toolkit-require-reload/main.js
new file mode 100644
index 000000000..5f5827f97
--- /dev/null
+++ b/addon-sdk/source/test/addons/toolkit-require-reload/main.js
@@ -0,0 +1,77 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Cu } = require("chrome");
+
+const toolkit = require("toolkit/require");
+
+const {tmpdir} = require("node/os");
+const {join} = require("sdk/fs/path");
+const {writeFile, unlink} = require("sdk/io/fs");
+const {fromFilename} = require("sdk/url");
+
+const makeCallback = (resolve, reject) => (error, result) => {
+ if (error) reject(error);
+ else resolve(result);
+};
+
+const remove = path => new Promise((resolve, reject) =>
+ unlink(path, makeCallback(resolve, reject)));
+
+const write = (...params) => new Promise((resolve, reject) =>
+ writeFile(...params, makeCallback(resolve, reject)));
+
+exports.testReload = function*(assert) {
+ const modulePath = join(tmpdir(), "toolkit-require-reload.js");
+ const moduleURI = fromFilename(modulePath);
+
+ yield write(modulePath, `exports.version = () => 1;`);
+
+ const v1 = toolkit.require(moduleURI);
+
+ assert.equal(v1.version(), 1, "module exports version");
+
+ yield write(modulePath, `exports.version = () => 2;`);
+
+ assert.equal(v1, toolkit.require(moduleURI),
+ "require does not reload modules");
+
+ const v2 = toolkit.require(moduleURI, {reload: true});
+ assert.equal(v2.version(), 2, "module was updated");
+
+ yield remove(modulePath);
+};
+
+exports.testReloadAll = function*(assert) {
+ const parentPath = join(tmpdir(), "toolkit-require-reload-parent.js");
+ const childPath = join(tmpdir(), "toolkit-require-reload-child.js");
+
+ const parentURI = fromFilename(parentPath);
+ const childURI = fromFilename(childPath);
+
+ yield write(childPath, `exports.name = () => "child"`);
+ yield write(parentPath, `const child = require("./toolkit-require-reload-child");
+ exports.greet = () => "Hello " + child.name();`);
+
+ const parent1 = toolkit.require(parentURI);
+ assert.equal(parent1.greet(), "Hello child");
+
+ yield write(childPath, `exports.name = () => "father"`);
+ yield write(parentPath, `const child = require("./toolkit-require-reload-child");
+ exports.greet = () => "Hello " + child.name() + "!";`);
+
+ const parent2 = toolkit.require(parentURI, {reload: true});
+ assert.equal(parent2.greet(), "Hello child!",
+ "only parent changes were picked up");
+
+ const parent3 = toolkit.require(parentURI, {reload: true, all: true});
+ assert.equal(parent3.greet(), "Hello father!",
+ "all changes were picked up");
+
+ yield remove(childPath);
+ yield remove(parentPath);
+};
+
+exports.main = _ => require("sdk/test/runner").runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/toolkit-require-reload/package.json b/addon-sdk/source/test/addons/toolkit-require-reload/package.json
new file mode 100644
index 000000000..25e885340
--- /dev/null
+++ b/addon-sdk/source/test/addons/toolkit-require-reload/package.json
@@ -0,0 +1,5 @@
+{
+ "id": "@toolkit-require-reload",
+ "version": "0.0.1",
+ "main": "main.js"
+}
diff --git a/addon-sdk/source/test/addons/translators/main.js b/addon-sdk/source/test/addons/translators/main.js
new file mode 100644
index 000000000..9c7cfff09
--- /dev/null
+++ b/addon-sdk/source/test/addons/translators/main.js
@@ -0,0 +1,20 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { id } = require('sdk/self');
+const { getAddonByID } = require('sdk/addon/manager');
+
+exports.testTranslators = function*(assert) {
+ let addon = yield getAddonByID(id);
+ let count = 0;
+ addon.translators.forEach(function({ name }) {
+ count++;
+ assert.equal(name, 'Erik Vold', 'The translator keys are correct');
+ });
+ assert.equal(count, 1, 'The translator key count is correct');
+ assert.equal(addon.translators.length, 1, 'The translator key length is correct');
+}
+
+require('sdk/test/runner').runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/translators/package.json b/addon-sdk/source/test/addons/translators/package.json
new file mode 100644
index 000000000..56fc2f266
--- /dev/null
+++ b/addon-sdk/source/test/addons/translators/package.json
@@ -0,0 +1,8 @@
+{
+ "id": "test-translators@jetpack",
+ "translators": [
+ "Erik Vold"
+ ],
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/addons/unsafe-content-script/main.js b/addon-sdk/source/test/addons/unsafe-content-script/main.js
new file mode 100644
index 000000000..b06810117
--- /dev/null
+++ b/addon-sdk/source/test/addons/unsafe-content-script/main.js
@@ -0,0 +1,68 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { create: makeFrame } = require("sdk/frame/utils");
+const { window } = require("sdk/addon/window");
+const { Loader } = require('sdk/test/loader');
+
+exports.testMembranelessMode = function(assert, done) {
+ const loader = Loader(module);
+ const Worker = loader.require("sdk/content/worker").Worker;
+
+ let url = "data:text/html;charset=utf-8," + encodeURIComponent(
+ '<script>' +
+ 'function runTest() {' +
+ ' assert(fuu.bar == 42, "Content-script objects should be accessible to content with' +
+ ' the unsafe-content-script flag on.");' +
+ '}' +
+ '</script>'
+ );
+
+ let element = makeFrame(window.document, {
+ nodeName: "iframe",
+ type: "content",
+ allowJavascript: true,
+ allowPlugins: true,
+ allowAuth: true,
+ uri: url
+ });
+
+ element.addEventListener("DOMContentLoaded", onDOMReady, false);
+
+ function onDOMReady() {
+ let worker = Worker({
+ window: element.contentWindow,
+ contentScript:
+ 'new ' + function () {
+ var assert = function assert(v, msg) {
+ self.port.emit("assert", { assertion: v, msg: msg });
+ }
+ var done = function done() {
+ self.port.emit("done");
+ }
+ window.wrappedJSObject.fuu = { bar: 42 };
+ window.wrappedJSObject.assert = assert;
+ window.wrappedJSObject.runTest();
+ done();
+ }
+ });
+
+ worker.port.on("done", () => {
+ // cleanup
+ element.parentNode.removeChild(element);
+ worker.destroy();
+ loader.unload();
+
+ done();
+ });
+
+ worker.port.on("assert", function (data) {
+ assert.ok(data.assertion, data.msg);
+ });
+
+ }
+};
+
+require("sdk/test/runner").runTestsFromModule(module);
diff --git a/addon-sdk/source/test/addons/unsafe-content-script/package.json b/addon-sdk/source/test/addons/unsafe-content-script/package.json
new file mode 100644
index 000000000..4e671dfc4
--- /dev/null
+++ b/addon-sdk/source/test/addons/unsafe-content-script/package.json
@@ -0,0 +1,8 @@
+{
+ "id": "content-permissions@jetpack",
+ "permissions": {
+ "unsafe-content-script": true
+ },
+ "main": "./main.js",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/buffers/test-read-types.js b/addon-sdk/source/test/buffers/test-read-types.js
new file mode 100644
index 000000000..35aca16ad
--- /dev/null
+++ b/addon-sdk/source/test/buffers/test-read-types.js
@@ -0,0 +1,368 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+const { Buffer } = require('sdk/io/buffer');
+
+exports.testReadDouble = helper('readDoubleLE/readDoubleBE', function (assert) {
+ var buffer = new Buffer(8);
+
+ buffer[0] = 0x55;
+ buffer[1] = 0x55;
+ buffer[2] = 0x55;
+ buffer[3] = 0x55;
+ buffer[4] = 0x55;
+ buffer[5] = 0x55;
+ buffer[6] = 0xd5;
+ buffer[7] = 0x3f;
+ assert.equal(1.1945305291680097e+103, buffer.readDoubleBE(0));
+ assert.equal(0.3333333333333333, buffer.readDoubleLE(0));
+
+ buffer[0] = 1;
+ buffer[1] = 0;
+ buffer[2] = 0;
+ buffer[3] = 0;
+ buffer[4] = 0;
+ buffer[5] = 0;
+ buffer[6] = 0xf0;
+ buffer[7] = 0x3f;
+ assert.equal(7.291122019655968e-304, buffer.readDoubleBE(0));
+ assert.equal(1.0000000000000002, buffer.readDoubleLE(0));
+
+ buffer[0] = 2;
+ assert.equal(4.778309726801735e-299, buffer.readDoubleBE(0));
+ assert.equal(1.0000000000000004, buffer.readDoubleLE(0));
+
+ buffer[0] = 1;
+ buffer[6] = 0;
+ buffer[7] = 0;
+ assert.equal(7.291122019556398e-304, buffer.readDoubleBE(0));
+ assert.equal(5e-324, buffer.readDoubleLE(0));
+
+ buffer[0] = 0xff;
+ buffer[1] = 0xff;
+ buffer[2] = 0xff;
+ buffer[3] = 0xff;
+ buffer[4] = 0xff;
+ buffer[5] = 0xff;
+ buffer[6] = 0x0f;
+ buffer[7] = 0x00;
+ assert.ok(isNaN(buffer.readDoubleBE(0)));
+ assert.equal(2.225073858507201e-308, buffer.readDoubleLE(0));
+
+ buffer[6] = 0xef;
+ buffer[7] = 0x7f;
+ assert.ok(isNaN(buffer.readDoubleBE(0)));
+ assert.equal(1.7976931348623157e+308, buffer.readDoubleLE(0));
+
+ buffer[0] = 0;
+ buffer[1] = 0;
+ buffer[2] = 0;
+ buffer[3] = 0;
+ buffer[4] = 0;
+ buffer[5] = 0;
+ buffer[6] = 0xf0;
+ buffer[7] = 0x3f;
+ assert.equal(3.03865e-319, buffer.readDoubleBE(0));
+ assert.equal(1, buffer.readDoubleLE(0));
+
+ buffer[6] = 0;
+ buffer[7] = 0x40;
+ assert.equal(3.16e-322, buffer.readDoubleBE(0));
+ assert.equal(2, buffer.readDoubleLE(0));
+
+ buffer[7] = 0xc0;
+ assert.equal(9.5e-322, buffer.readDoubleBE(0));
+ assert.equal(-2, buffer.readDoubleLE(0));
+
+ buffer[6] = 0x10;
+ buffer[7] = 0;
+ assert.equal(2.0237e-320, buffer.readDoubleBE(0));
+ assert.equal(2.2250738585072014e-308, buffer.readDoubleLE(0));
+
+ buffer[6] = 0;
+ assert.equal(0, buffer.readDoubleBE(0));
+ assert.equal(0, buffer.readDoubleLE(0));
+ assert.equal(false, 1 / buffer.readDoubleLE(0) < 0);
+
+ buffer[7] = 0x80;
+ assert.equal(6.3e-322, buffer.readDoubleBE(0));
+ assert.equal(0, buffer.readDoubleLE(0));
+ assert.equal(true, 1 / buffer.readDoubleLE(0) < 0);
+
+ buffer[6] = 0xf0;
+ buffer[7] = 0x7f;
+ assert.equal(3.0418e-319, buffer.readDoubleBE(0));
+ assert.equal(Infinity, buffer.readDoubleLE(0));
+
+ buffer[6] = 0xf0;
+ buffer[7] = 0xff;
+ assert.equal(3.04814e-319, buffer.readDoubleBE(0));
+ assert.equal(-Infinity, buffer.readDoubleLE(0));
+});
+
+
+exports.testReadFloat = helper('readFloatLE/readFloatBE', function (assert) {
+ var buffer = new Buffer(4);
+
+ buffer[0] = 0;
+ buffer[1] = 0;
+ buffer[2] = 0x80;
+ buffer[3] = 0x3f;
+ assert.equal(4.600602988224807e-41, buffer.readFloatBE(0));
+ assert.equal(1, buffer.readFloatLE(0));
+
+ buffer[0] = 0;
+ buffer[1] = 0;
+ buffer[2] = 0;
+ buffer[3] = 0xc0;
+ assert.equal(2.6904930515036488e-43, buffer.readFloatBE(0));
+ assert.equal(-2, buffer.readFloatLE(0));
+
+ buffer[0] = 0xff;
+ buffer[1] = 0xff;
+ buffer[2] = 0x7f;
+ buffer[3] = 0x7f;
+ assert.ok(isNaN(buffer.readFloatBE(0)));
+ assert.equal(3.4028234663852886e+38, buffer.readFloatLE(0));
+
+ buffer[0] = 0xab;
+ buffer[1] = 0xaa;
+ buffer[2] = 0xaa;
+ buffer[3] = 0x3e;
+ assert.equal(-1.2126478207002966e-12, buffer.readFloatBE(0));
+ assert.equal(0.3333333432674408, buffer.readFloatLE(0));
+
+ buffer[0] = 0;
+ buffer[1] = 0;
+ buffer[2] = 0;
+ buffer[3] = 0;
+ assert.equal(0, buffer.readFloatBE(0));
+ assert.equal(0, buffer.readFloatLE(0));
+ assert.equal(false, 1 / buffer.readFloatLE(0) < 0);
+
+ buffer[3] = 0x80;
+ assert.equal(1.793662034335766e-43, buffer.readFloatBE(0));
+ assert.equal(0, buffer.readFloatLE(0));
+ assert.equal(true, 1 / buffer.readFloatLE(0) < 0);
+
+ buffer[0] = 0;
+ buffer[1] = 0;
+ buffer[2] = 0x80;
+ buffer[3] = 0x7f;
+ assert.equal(4.609571298396486e-41, buffer.readFloatBE(0));
+ assert.equal(Infinity, buffer.readFloatLE(0));
+
+ buffer[0] = 0;
+ buffer[1] = 0;
+ buffer[2] = 0x80;
+ buffer[3] = 0xff;
+ assert.equal(4.627507918739843e-41, buffer.readFloatBE(0));
+ assert.equal(-Infinity, buffer.readFloatLE(0));
+});
+
+
+exports.testReadInt8 = helper('readInt8', function (assert) {
+ var data = new Buffer(4);
+
+ data[0] = 0x23;
+ assert.equal(0x23, data.readInt8(0));
+
+ data[0] = 0xff;
+ assert.equal(-1, data.readInt8(0));
+
+ data[0] = 0x87;
+ data[1] = 0xab;
+ data[2] = 0x7c;
+ data[3] = 0xef;
+ assert.equal(-121, data.readInt8(0));
+ assert.equal(-85, data.readInt8(1));
+ assert.equal(124, data.readInt8(2));
+ assert.equal(-17, data.readInt8(3));
+});
+
+
+exports.testReadInt16 = helper('readInt16BE/readInt16LE', function (assert) {
+ var buffer = new Buffer(6);
+
+ buffer[0] = 0x16;
+ buffer[1] = 0x79;
+ assert.equal(0x1679, buffer.readInt16BE(0));
+ assert.equal(0x7916, buffer.readInt16LE(0));
+
+ buffer[0] = 0xff;
+ buffer[1] = 0x80;
+ assert.equal(-128, buffer.readInt16BE(0));
+ assert.equal(-32513, buffer.readInt16LE(0));
+
+ /* test offset with weenix */
+ buffer[0] = 0x77;
+ buffer[1] = 0x65;
+ buffer[2] = 0x65;
+ buffer[3] = 0x6e;
+ buffer[4] = 0x69;
+ buffer[5] = 0x78;
+ assert.equal(0x7765, buffer.readInt16BE(0));
+ assert.equal(0x6565, buffer.readInt16BE(1));
+ assert.equal(0x656e, buffer.readInt16BE(2));
+ assert.equal(0x6e69, buffer.readInt16BE(3));
+ assert.equal(0x6978, buffer.readInt16BE(4));
+ assert.equal(0x6577, buffer.readInt16LE(0));
+ assert.equal(0x6565, buffer.readInt16LE(1));
+ assert.equal(0x6e65, buffer.readInt16LE(2));
+ assert.equal(0x696e, buffer.readInt16LE(3));
+ assert.equal(0x7869, buffer.readInt16LE(4));
+});
+
+
+exports.testReadInt32 = helper('readInt32BE/readInt32LE', function (assert) {
+ var buffer = new Buffer(6);
+
+ buffer[0] = 0x43;
+ buffer[1] = 0x53;
+ buffer[2] = 0x16;
+ buffer[3] = 0x79;
+ assert.equal(0x43531679, buffer.readInt32BE(0));
+ assert.equal(0x79165343, buffer.readInt32LE(0));
+
+ buffer[0] = 0xff;
+ buffer[1] = 0xfe;
+ buffer[2] = 0xef;
+ buffer[3] = 0xfa;
+ assert.equal(-69638, buffer.readInt32BE(0));
+ assert.equal(-84934913, buffer.readInt32LE(0));
+
+ buffer[0] = 0x42;
+ buffer[1] = 0xc3;
+ buffer[2] = 0x95;
+ buffer[3] = 0xa9;
+ buffer[4] = 0x36;
+ buffer[5] = 0x17;
+ assert.equal(0x42c395a9, buffer.readInt32BE(0));
+ assert.equal(-1013601994, buffer.readInt32BE(1));
+ assert.equal(-1784072681, buffer.readInt32BE(2));
+ assert.equal(-1449802942, buffer.readInt32LE(0));
+ assert.equal(917083587, buffer.readInt32LE(1));
+ assert.equal(389458325, buffer.readInt32LE(2));
+});
+
+
+/*
+ * We need to check the following things:
+ * - We are correctly resolving big endian (doesn't mean anything for 8 bit)
+ * - Correctly resolving little endian (doesn't mean anything for 8 bit)
+ * - Correctly using the offsets
+ * - Correctly interpreting values that are beyond the signed range as unsigned
+ */
+exports.testReadUInt8 = helper('readUInt8', function (assert) {
+ var data = new Buffer(4);
+
+ data[0] = 23;
+ data[1] = 23;
+ data[2] = 23;
+ data[3] = 23;
+ assert.equal(23, data.readUInt8(0));
+ assert.equal(23, data.readUInt8(1));
+ assert.equal(23, data.readUInt8(2));
+ assert.equal(23, data.readUInt8(3));
+
+ data[0] = 255; /* If it became a signed int, would be -1 */
+ assert.equal(255, data.readUInt8(0));
+});
+
+
+/*
+ * Test 16 bit unsigned integers. We need to verify the same set as 8 bit, only
+ * now some of the issues actually matter:
+ * - We are correctly resolving big endian
+ * - Correctly resolving little endian
+ * - Correctly using the offsets
+ * - Correctly interpreting values that are beyond the signed range as unsigned
+ */
+exports.testReadUInt16 = helper('readUInt16LE/readUInt16BE', function (assert) {
+ var data = new Buffer(4);
+
+ data[0] = 0;
+ data[1] = 0x23;
+ data[2] = 0x42;
+ data[3] = 0x3f;
+ assert.equal(0x23, data.readUInt16BE(0));
+ assert.equal(0x2342, data.readUInt16BE(1));
+ assert.equal(0x423f, data.readUInt16BE(2));
+ assert.equal(0x2300, data.readUInt16LE(0));
+ assert.equal(0x4223, data.readUInt16LE(1));
+ assert.equal(0x3f42, data.readUInt16LE(2));
+
+ data[0] = 0xfe;
+ data[1] = 0xfe;
+ assert.equal(0xfefe, data.readUInt16BE(0));
+ assert.equal(0xfefe, data.readUInt16LE(0));
+});
+
+
+/*
+ * Test 32 bit unsigned integers. We need to verify the same set as 8 bit, only
+ * now some of the issues actually matter:
+ * - We are correctly resolving big endian
+ * - Correctly using the offsets
+ * - Correctly interpreting values that are beyond the signed range as unsigned
+ */
+exports.testReadUInt32 = helper('readUInt32LE/readUInt32BE', function (assert) {
+ var data = new Buffer(8);
+
+ data[0] = 0x32;
+ data[1] = 0x65;
+ data[2] = 0x42;
+ data[3] = 0x56;
+ data[4] = 0x23;
+ data[5] = 0xff;
+ assert.equal(0x32654256, data.readUInt32BE(0));
+ assert.equal(0x65425623, data.readUInt32BE(1));
+ assert.equal(0x425623ff, data.readUInt32BE(2));
+ assert.equal(0x56426532, data.readUInt32LE(0));
+ assert.equal(0x23564265, data.readUInt32LE(1));
+ assert.equal(0xff235642, data.readUInt32LE(2));
+});
+
+function helper (description, fn) {
+ let bulkAssert = {
+ equal: function (a, b) {
+ if (a !== b) throw new Error('Error found in ' + description);
+ },
+ ok: function (value) {
+ if (!value) throw new Error('Error found in ' + description);
+ },
+ throws: function (shouldThrow) {
+ let didItThrow = false;
+ try {
+ shouldThrow();
+ } catch (e) {
+ didItThrow = e;
+ }
+ if (!didItThrow)
+ throw new Error('Error found in ' + description + ': ' + shouldThrow + ' should have thrown');
+ }
+ };
+ return function (assert) {
+ fn(bulkAssert);
+ // If we get here, no errors thrown
+ assert.pass('All tests passed for ' + description);
+ };
+}
diff --git a/addon-sdk/source/test/buffers/test-write-types.js b/addon-sdk/source/test/buffers/test-write-types.js
new file mode 100644
index 000000000..f7872aaaa
--- /dev/null
+++ b/addon-sdk/source/test/buffers/test-write-types.js
@@ -0,0 +1,602 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+const { Buffer } = require('sdk/io/buffer');
+
+exports.testWriteDouble = helper('writeDoubleBE/writeDoubleLE', function (assert) {
+ var buffer = new Buffer(16);
+
+ buffer.writeDoubleBE(2.225073858507201e-308, 0);
+ buffer.writeDoubleLE(2.225073858507201e-308, 8);
+ assert.equal(0x00, buffer[0]);
+ assert.equal(0x0f, buffer[1]);
+ assert.equal(0xff, buffer[2]);
+ assert.equal(0xff, buffer[3]);
+ assert.equal(0xff, buffer[4]);
+ assert.equal(0xff, buffer[5]);
+ assert.equal(0xff, buffer[6]);
+ assert.equal(0xff, buffer[7]);
+ assert.equal(0xff, buffer[8]);
+ assert.equal(0xff, buffer[9]);
+ assert.equal(0xff, buffer[10]);
+ assert.equal(0xff, buffer[11]);
+ assert.equal(0xff, buffer[12]);
+ assert.equal(0xff, buffer[13]);
+ assert.equal(0x0f, buffer[14]);
+ assert.equal(0x00, buffer[15]);
+
+ buffer.writeDoubleBE(1.0000000000000004, 0);
+ buffer.writeDoubleLE(1.0000000000000004, 8);
+ assert.equal(0x3f, buffer[0]);
+ assert.equal(0xf0, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x02, buffer[7]);
+ assert.equal(0x02, buffer[8]);
+ assert.equal(0x00, buffer[9]);
+ assert.equal(0x00, buffer[10]);
+ assert.equal(0x00, buffer[11]);
+ assert.equal(0x00, buffer[12]);
+ assert.equal(0x00, buffer[13]);
+ assert.equal(0xf0, buffer[14]);
+ assert.equal(0x3f, buffer[15]);
+
+ buffer.writeDoubleBE(-2, 0);
+ buffer.writeDoubleLE(-2, 8);
+ assert.equal(0xc0, buffer[0]);
+ assert.equal(0x00, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x00, buffer[7]);
+ assert.equal(0x00, buffer[8]);
+ assert.equal(0x00, buffer[9]);
+ assert.equal(0x00, buffer[10]);
+ assert.equal(0x00, buffer[11]);
+ assert.equal(0x00, buffer[12]);
+ assert.equal(0x00, buffer[13]);
+ assert.equal(0x00, buffer[14]);
+ assert.equal(0xc0, buffer[15]);
+
+ buffer.writeDoubleBE(1.7976931348623157e+308, 0);
+ buffer.writeDoubleLE(1.7976931348623157e+308, 8);
+ assert.equal(0x7f, buffer[0]);
+ assert.equal(0xef, buffer[1]);
+ assert.equal(0xff, buffer[2]);
+ assert.equal(0xff, buffer[3]);
+ assert.equal(0xff, buffer[4]);
+ assert.equal(0xff, buffer[5]);
+ assert.equal(0xff, buffer[6]);
+ assert.equal(0xff, buffer[7]);
+ assert.equal(0xff, buffer[8]);
+ assert.equal(0xff, buffer[9]);
+ assert.equal(0xff, buffer[10]);
+ assert.equal(0xff, buffer[11]);
+ assert.equal(0xff, buffer[12]);
+ assert.equal(0xff, buffer[13]);
+ assert.equal(0xef, buffer[14]);
+ assert.equal(0x7f, buffer[15]);
+
+ buffer.writeDoubleBE(0 * -1, 0);
+ buffer.writeDoubleLE(0 * -1, 8);
+ assert.equal(0x80, buffer[0]);
+ assert.equal(0x00, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x00, buffer[7]);
+ assert.equal(0x00, buffer[8]);
+ assert.equal(0x00, buffer[9]);
+ assert.equal(0x00, buffer[10]);
+ assert.equal(0x00, buffer[11]);
+ assert.equal(0x00, buffer[12]);
+ assert.equal(0x00, buffer[13]);
+ assert.equal(0x00, buffer[14]);
+ assert.equal(0x80, buffer[15]);
+
+ buffer.writeDoubleBE(Infinity, 0);
+ buffer.writeDoubleLE(Infinity, 8);
+ assert.equal(0x7F, buffer[0]);
+ assert.equal(0xF0, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x00, buffer[7]);
+ assert.equal(0x00, buffer[8]);
+ assert.equal(0x00, buffer[9]);
+ assert.equal(0x00, buffer[10]);
+ assert.equal(0x00, buffer[11]);
+ assert.equal(0x00, buffer[12]);
+ assert.equal(0x00, buffer[13]);
+ assert.equal(0xF0, buffer[14]);
+ assert.equal(0x7F, buffer[15]);
+ assert.equal(Infinity, buffer.readDoubleBE(0));
+ assert.equal(Infinity, buffer.readDoubleLE(8));
+
+ buffer.writeDoubleBE(-Infinity, 0);
+ buffer.writeDoubleLE(-Infinity, 8);
+ assert.equal(0xFF, buffer[0]);
+ assert.equal(0xF0, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x00, buffer[7]);
+ assert.equal(0x00, buffer[8]);
+ assert.equal(0x00, buffer[9]);
+ assert.equal(0x00, buffer[10]);
+ assert.equal(0x00, buffer[11]);
+ assert.equal(0x00, buffer[12]);
+ assert.equal(0x00, buffer[13]);
+ assert.equal(0xF0, buffer[14]);
+ assert.equal(0xFF, buffer[15]);
+ assert.equal(-Infinity, buffer.readDoubleBE(0));
+ assert.equal(-Infinity, buffer.readDoubleLE(8));
+
+ buffer.writeDoubleBE(NaN, 0);
+ buffer.writeDoubleLE(NaN, 8);
+ // Darwin ia32 does the other kind of NaN.
+ // Compiler bug. No one really cares.
+ assert.ok(0x7F === buffer[0] || 0xFF === buffer[0]);
+ assert.equal(0xF8, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x00, buffer[7]);
+ assert.equal(0x00, buffer[8]);
+ assert.equal(0x00, buffer[9]);
+ assert.equal(0x00, buffer[10]);
+ assert.equal(0x00, buffer[11]);
+ assert.equal(0x00, buffer[12]);
+ assert.equal(0x00, buffer[13]);
+ assert.equal(0xF8, buffer[14]);
+ // Darwin ia32 does the other kind of NaN.
+ // Compiler bug. No one really cares.
+ assert.ok(0x7F === buffer[15] || 0xFF === buffer[15]);
+ assert.ok(isNaN(buffer.readDoubleBE(0)));
+ assert.ok(isNaN(buffer.readDoubleLE(8)));
+});
+
+exports.testWriteFloat = helper('writeFloatBE/writeFloatLE', function (assert) {
+ var buffer = new Buffer(8);
+
+ buffer.writeFloatBE(1, 0);
+ buffer.writeFloatLE(1, 4);
+ assert.equal(0x3f, buffer[0]);
+ assert.equal(0x80, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x80, buffer[6]);
+ assert.equal(0x3f, buffer[7]);
+
+ buffer.writeFloatBE(1 / 3, 0);
+ buffer.writeFloatLE(1 / 3, 4);
+ assert.equal(0x3e, buffer[0]);
+ assert.equal(0xaa, buffer[1]);
+ assert.equal(0xaa, buffer[2]);
+ assert.equal(0xab, buffer[3]);
+ assert.equal(0xab, buffer[4]);
+ assert.equal(0xaa, buffer[5]);
+ assert.equal(0xaa, buffer[6]);
+ assert.equal(0x3e, buffer[7]);
+
+ buffer.writeFloatBE(3.4028234663852886e+38, 0);
+ buffer.writeFloatLE(3.4028234663852886e+38, 4);
+ assert.equal(0x7f, buffer[0]);
+ assert.equal(0x7f, buffer[1]);
+ assert.equal(0xff, buffer[2]);
+ assert.equal(0xff, buffer[3]);
+ assert.equal(0xff, buffer[4]);
+ assert.equal(0xff, buffer[5]);
+ assert.equal(0x7f, buffer[6]);
+ assert.equal(0x7f, buffer[7]);
+
+ buffer.writeFloatLE(1.1754943508222875e-38, 0);
+ buffer.writeFloatBE(1.1754943508222875e-38, 4);
+ assert.equal(0x00, buffer[0]);
+ assert.equal(0x00, buffer[1]);
+ assert.equal(0x80, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x80, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x00, buffer[7]);
+
+ buffer.writeFloatBE(0 * -1, 0);
+ buffer.writeFloatLE(0 * -1, 4);
+ assert.equal(0x80, buffer[0]);
+ assert.equal(0x00, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x80, buffer[7]);
+
+ buffer.writeFloatBE(Infinity, 0);
+ buffer.writeFloatLE(Infinity, 4);
+ assert.equal(0x7F, buffer[0]);
+ assert.equal(0x80, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x80, buffer[6]);
+ assert.equal(0x7F, buffer[7]);
+ assert.equal(Infinity, buffer.readFloatBE(0));
+ assert.equal(Infinity, buffer.readFloatLE(4));
+
+ buffer.writeFloatBE(-Infinity, 0);
+ buffer.writeFloatLE(-Infinity, 4);
+ // Darwin ia32 does the other kind of NaN.
+ // Compiler bug. No one really cares.
+ assert.ok(0xFF === buffer[0] || 0x7F === buffer[0]);
+ assert.equal(0x80, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x80, buffer[6]);
+ assert.equal(0xFF, buffer[7]);
+ assert.equal(-Infinity, buffer.readFloatBE(0));
+ assert.equal(-Infinity, buffer.readFloatLE(4));
+
+ buffer.writeFloatBE(NaN, 0);
+ buffer.writeFloatLE(NaN, 4);
+ // Darwin ia32 does the other kind of NaN.
+ // Compiler bug. No one really cares.
+ assert.ok(0x7F === buffer[0] || 0xFF === buffer[0]);
+ assert.equal(0xc0, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0xc0, buffer[6]);
+ // Darwin ia32 does the other kind of NaN.
+ // Compiler bug. No one really cares.
+ assert.ok(0x7F === buffer[7] || 0xFF === buffer[7]);
+ assert.ok(isNaN(buffer.readFloatBE(0)));
+ assert.ok(isNaN(buffer.readFloatLE(4)));
+});
+
+
+exports.testWriteInt8 = helper('writeInt8', function (assert) {
+ var buffer = new Buffer(2);
+
+ buffer.writeInt8(0x23, 0);
+ buffer.writeInt8(-5, 1);
+
+ assert.equal(0x23, buffer[0]);
+ assert.equal(0xfb, buffer[1]);
+
+ /* Make sure we handle truncation correctly */
+ assert.throws(function() {
+ buffer.writeInt8(0xabc, 0);
+ });
+ assert.throws(function() {
+ buffer.writeInt8(0xabc, 0);
+ });
+
+ /* Make sure we handle min/max correctly */
+ buffer.writeInt8(0x7f, 0);
+ buffer.writeInt8(-0x80, 1);
+
+ assert.equal(0x7f, buffer[0]);
+ assert.equal(0x80, buffer[1]);
+ assert.throws(function() {
+ buffer.writeInt8(0x7f + 1, 0);
+ });
+ assert.throws(function() {
+ buffer.writeInt8(-0x80 - 1, 0);
+ });
+});
+
+
+exports.testWriteInt16 = helper('writeInt16LE/writeInt16BE', function (assert) {
+ var buffer = new Buffer(6);
+
+ buffer.writeInt16BE(0x0023, 0);
+ buffer.writeInt16LE(0x0023, 2);
+ assert.equal(0x00, buffer[0]);
+ assert.equal(0x23, buffer[1]);
+ assert.equal(0x23, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+
+ buffer.writeInt16BE(-5, 0);
+ buffer.writeInt16LE(-5, 2);
+ assert.equal(0xff, buffer[0]);
+ assert.equal(0xfb, buffer[1]);
+ assert.equal(0xfb, buffer[2]);
+ assert.equal(0xff, buffer[3]);
+
+ buffer.writeInt16BE(-1679, 1);
+ buffer.writeInt16LE(-1679, 3);
+ assert.equal(0xf9, buffer[1]);
+ assert.equal(0x71, buffer[2]);
+ assert.equal(0x71, buffer[3]);
+ assert.equal(0xf9, buffer[4]);
+
+ /* Make sure we handle min/max correctly */
+ buffer.writeInt16BE(0x7fff, 0);
+ buffer.writeInt16BE(-0x8000, 2);
+ assert.equal(0x7f, buffer[0]);
+ assert.equal(0xff, buffer[1]);
+ assert.equal(0x80, buffer[2]);
+ assert.equal(0x00, buffer[3]);
+ assert.throws(function() {
+ buffer.writeInt16BE(0x7fff + 1, 0);
+ });
+ assert.throws(function() {
+ buffer.writeInt16BE(-0x8000 - 1, 0);
+ });
+
+ buffer.writeInt16LE(0x7fff, 0);
+ buffer.writeInt16LE(-0x8000, 2);
+ assert.equal(0xff, buffer[0]);
+ assert.equal(0x7f, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x80, buffer[3]);
+ assert.throws(function() {
+ buffer.writeInt16LE(0x7fff + 1, 0);
+ });
+ assert.throws(function() {
+ buffer.writeInt16LE(-0x8000 - 1, 0);
+ });
+});
+
+exports.testWriteInt32 = helper('writeInt32BE/writeInt32LE', function (assert) {
+ var buffer = new Buffer(8);
+
+ buffer.writeInt32BE(0x23, 0);
+ buffer.writeInt32LE(0x23, 4);
+ assert.equal(0x00, buffer[0]);
+ assert.equal(0x00, buffer[1]);
+ assert.equal(0x00, buffer[2]);
+ assert.equal(0x23, buffer[3]);
+ assert.equal(0x23, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x00, buffer[7]);
+
+ buffer.writeInt32BE(-5, 0);
+ buffer.writeInt32LE(-5, 4);
+ assert.equal(0xff, buffer[0]);
+ assert.equal(0xff, buffer[1]);
+ assert.equal(0xff, buffer[2]);
+ assert.equal(0xfb, buffer[3]);
+ assert.equal(0xfb, buffer[4]);
+ assert.equal(0xff, buffer[5]);
+ assert.equal(0xff, buffer[6]);
+ assert.equal(0xff, buffer[7]);
+
+ buffer.writeInt32BE(-805306713, 0);
+ buffer.writeInt32LE(-805306713, 4);
+ assert.equal(0xcf, buffer[0]);
+ assert.equal(0xff, buffer[1]);
+ assert.equal(0xfe, buffer[2]);
+ assert.equal(0xa7, buffer[3]);
+ assert.equal(0xa7, buffer[4]);
+ assert.equal(0xfe, buffer[5]);
+ assert.equal(0xff, buffer[6]);
+ assert.equal(0xcf, buffer[7]);
+
+ /* Make sure we handle min/max correctly */
+ buffer.writeInt32BE(0x7fffffff, 0);
+ buffer.writeInt32BE(-0x80000000, 4);
+ assert.equal(0x7f, buffer[0]);
+ assert.equal(0xff, buffer[1]);
+ assert.equal(0xff, buffer[2]);
+ assert.equal(0xff, buffer[3]);
+ assert.equal(0x80, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x00, buffer[7]);
+ assert.throws(function() {
+ buffer.writeInt32BE(0x7fffffff + 1, 0);
+ });
+ assert.throws(function() {
+ buffer.writeInt32BE(-0x80000000 - 1, 0);
+ });
+
+ buffer.writeInt32LE(0x7fffffff, 0);
+ buffer.writeInt32LE(-0x80000000, 4);
+ assert.equal(0xff, buffer[0]);
+ assert.equal(0xff, buffer[1]);
+ assert.equal(0xff, buffer[2]);
+ assert.equal(0x7f, buffer[3]);
+ assert.equal(0x00, buffer[4]);
+ assert.equal(0x00, buffer[5]);
+ assert.equal(0x00, buffer[6]);
+ assert.equal(0x80, buffer[7]);
+ assert.throws(function() {
+ buffer.writeInt32LE(0x7fffffff + 1, 0);
+ });
+ assert.throws(function() {
+ buffer.writeInt32LE(-0x80000000 - 1, 0);
+ });
+});
+
+/*
+ * We need to check the following things:
+ * - We are correctly resolving big endian (doesn't mean anything for 8 bit)
+ * - Correctly resolving little endian (doesn't mean anything for 8 bit)
+ * - Correctly using the offsets
+ * - Correctly interpreting values that are beyond the signed range as unsigned
+ */
+exports.testWriteUInt8 = helper('writeUInt8', function (assert) {
+ var data = new Buffer(4);
+
+ data.writeUInt8(23, 0);
+ data.writeUInt8(23, 1);
+ data.writeUInt8(23, 2);
+ data.writeUInt8(23, 3);
+ assert.equal(23, data[0]);
+ assert.equal(23, data[1]);
+ assert.equal(23, data[2]);
+ assert.equal(23, data[3]);
+
+ data.writeUInt8(23, 0);
+ data.writeUInt8(23, 1);
+ data.writeUInt8(23, 2);
+ data.writeUInt8(23, 3);
+ assert.equal(23, data[0]);
+ assert.equal(23, data[1]);
+ assert.equal(23, data[2]);
+ assert.equal(23, data[3]);
+
+ data.writeUInt8(255, 0);
+ assert.equal(255, data[0]);
+
+ data.writeUInt8(255, 0);
+ assert.equal(255, data[0]);
+});
+
+
+exports.testWriteUInt16 = helper('writeUInt16BE/writeUInt16LE', function (assert) {
+ var value = 0x2343;
+ var data = new Buffer(4);
+
+ data.writeUInt16BE(value, 0);
+ assert.equal(0x23, data[0]);
+ assert.equal(0x43, data[1]);
+
+ data.writeUInt16BE(value, 1);
+ assert.equal(0x23, data[1]);
+ assert.equal(0x43, data[2]);
+
+ data.writeUInt16BE(value, 2);
+ assert.equal(0x23, data[2]);
+ assert.equal(0x43, data[3]);
+
+ data.writeUInt16LE(value, 0);
+ assert.equal(0x23, data[1]);
+ assert.equal(0x43, data[0]);
+
+ data.writeUInt16LE(value, 1);
+ assert.equal(0x23, data[2]);
+ assert.equal(0x43, data[1]);
+
+ data.writeUInt16LE(value, 2);
+ assert.equal(0x23, data[3]);
+ assert.equal(0x43, data[2]);
+
+ value = 0xff80;
+ data.writeUInt16LE(value, 0);
+ assert.equal(0xff, data[1]);
+ assert.equal(0x80, data[0]);
+
+ data.writeUInt16BE(value, 0);
+ assert.equal(0xff, data[0]);
+ assert.equal(0x80, data[1]);
+});
+
+
+exports.testWriteUInt32 = helper('writeUInt32BE/writeUInt32LE', function (assert) {
+ var data = new Buffer(6);
+ var value = 0xe7f90a6d;
+
+ data.writeUInt32BE(value, 0);
+ assert.equal(0xe7, data[0]);
+ assert.equal(0xf9, data[1]);
+ assert.equal(0x0a, data[2]);
+ assert.equal(0x6d, data[3]);
+
+ data.writeUInt32BE(value, 1);
+ assert.equal(0xe7, data[1]);
+ assert.equal(0xf9, data[2]);
+ assert.equal(0x0a, data[3]);
+ assert.equal(0x6d, data[4]);
+
+ data.writeUInt32BE(value, 2);
+ assert.equal(0xe7, data[2]);
+ assert.equal(0xf9, data[3]);
+ assert.equal(0x0a, data[4]);
+ assert.equal(0x6d, data[5]);
+
+ data.writeUInt32LE(value, 0);
+ assert.equal(0xe7, data[3]);
+ assert.equal(0xf9, data[2]);
+ assert.equal(0x0a, data[1]);
+ assert.equal(0x6d, data[0]);
+
+ data.writeUInt32LE(value, 1);
+ assert.equal(0xe7, data[4]);
+ assert.equal(0xf9, data[3]);
+ assert.equal(0x0a, data[2]);
+ assert.equal(0x6d, data[1]);
+
+ data.writeUInt32LE(value, 2);
+ assert.equal(0xe7, data[5]);
+ assert.equal(0xf9, data[4]);
+ assert.equal(0x0a, data[3]);
+ assert.equal(0x6d, data[2]);
+});
+
+function helper (description, fn) {
+ return function (assert) {
+ let bulkAssert = {
+ equal: function (a, b) {
+ if (a !== b) throw new Error('Error found in ' + description);
+ },
+ ok: function (value) {
+ if (!value) throw new Error('Error found in ' + description);
+ },
+ /*
+ * TODO
+ * There should be errors when setting outside of the value range
+ * of the data type (like writeInt8 with value of 1000), but DataView
+ * does not throw; seems to just grab the appropriate max bits.
+ * So ignoring this test for now
+ */
+ throws: function (shouldThrow) {
+ assert.pass(description + ': Need to implement error handling for setting buffer values ' +
+ 'outside of the data types\' range.');
+ /*
+ let didItThrow = false;
+ try {
+ shouldThrow();
+ } catch (e) {
+ didItThrow = e;
+ }
+ if (!didItThrow)
+ throw new Error('Error found in ' + description + ': ' + shouldThrow + ' should have thrown');
+ */
+ }
+ };
+ fn(bulkAssert);
+ // If we get here, no errors thrown
+ assert.pass('All tests passed for ' + description);
+ };
+}
diff --git a/addon-sdk/source/test/commonjs-test-adapter/asserts.js b/addon-sdk/source/test/commonjs-test-adapter/asserts.js
new file mode 100644
index 000000000..c0d0e1ca5
--- /dev/null
+++ b/addon-sdk/source/test/commonjs-test-adapter/asserts.js
@@ -0,0 +1,54 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const AssertBase = require("sdk/test/assert").Assert;
+
+/**
+ * Generates custom assertion constructors that may be bundled with a test
+ * suite.
+ * @params {String}
+ * names of assertion function to be added to the generated Assert.
+ */
+function Assert() {
+ let assertDescriptor = {};
+ Array.forEach(arguments, function(name) {
+ assertDescriptor[name] = { value: function(message) {
+ this.pass(message);
+ }}
+ });
+
+ return function Assert() {
+ return Object.create(AssertBase.apply(null, arguments), assertDescriptor);
+ };
+}
+
+exports["test suite"] = {
+ Assert: Assert("foo"),
+ "test that custom assertor is passed to test function": function(assert) {
+ assert.ok("foo" in assert, "custom assertion function `foo` is defined");
+ assert.foo("custom assertion function `foo` is called");
+ },
+ "test sub suite": {
+ "test that `Assert` is inherited by sub suits": function(assert) {
+ assert.ok("foo" in assert, "assertion function `foo` is not defined");
+ },
+ "test sub sub suite": {
+ Assert: Assert("bar"),
+ "test that custom assertor is passed to test function": function(assert) {
+ assert.ok("bar" in assert,
+ "custom assertion function `bar` is defined");
+ assert.bar("custom assertion function `bar` is called");
+ },
+ "test that `Assert` is not inherited by sub sub suits": function(assert) {
+ assert.ok(!("foo" in assert),
+ "assertion function `foo` is not defined");
+ }
+ }
+ }
+};
+
+if (module == require.main)
+ require("test").run(exports);
diff --git a/addon-sdk/source/test/context-menu/framescript.js b/addon-sdk/source/test/context-menu/framescript.js
new file mode 100644
index 000000000..c3e038a5a
--- /dev/null
+++ b/addon-sdk/source/test/context-menu/framescript.js
@@ -0,0 +1,44 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const Ci = Components.interfaces;
+
+addMessageListener("sdk/test/context-menu/open", message => {
+ const {data, name} = message;
+ const target = data.target && content.document.querySelector(data.target);
+ if (target) {
+ target.scrollIntoView();
+ }
+ const rect = target ? target.getBoundingClientRect() :
+ {left:0, top:0, width:0, height:0};
+
+
+ content.
+ QueryInterface(Ci.nsIInterfaceRequestor).
+ getInterface(Ci.nsIDOMWindowUtils).
+ sendMouseEvent("contextmenu",
+ rect.left + (rect.width / 2),
+ rect.top + (rect.height / 2),
+ 2, 1, 0);
+});
+
+addMessageListener("sdk/test/context-menu/select", message => {
+ const {data, name} = message;
+ const {document} = content;
+ if (data) {
+ if (typeof(data) === "string") {
+ const target = document.querySelector(data);
+ document.getSelection().selectAllChildren(target);
+ } else {
+ const target = document.querySelector(data.target);
+ target.focus();
+ target.setSelectionRange(data.start, data.end);
+ }
+ } else {
+ document.getSelection().collapse(document.documentElement, 0);
+ }
+
+ sendAsyncMessage("sdk/test/context-menu/selected");
+});
diff --git a/addon-sdk/source/test/context-menu/test-helper.js b/addon-sdk/source/test/context-menu/test-helper.js
new file mode 100644
index 000000000..fb315a5d3
--- /dev/null
+++ b/addon-sdk/source/test/context-menu/test-helper.js
@@ -0,0 +1,539 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+ "use strict";
+
+const { Ci } = require("chrome");
+const { getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { Loader } = require('sdk/test/loader');
+const { merge } = require("sdk/util/object");
+const observers = require("sdk/system/events");
+const { defer } = require("sdk/core/promise");
+const timer = require("sdk/timers");
+
+// These should match the same constants in the module.
+const ITEM_CLASS = "addon-context-menu-item";
+const SEPARATOR_CLASS = "addon-context-menu-separator";
+const OVERFLOW_THRESH_DEFAULT = 10;
+const OVERFLOW_THRESH_PREF =
+ "extensions.addon-sdk.context-menu.overflowThreshold";
+const OVERFLOW_MENU_CLASS = "addon-content-menu-overflow-menu";
+const OVERFLOW_POPUP_CLASS = "addon-content-menu-overflow-popup";
+
+const TEST_DOC_URL = module.uri.replace(/context-menu\/test-helper\.js$/, "test-context-menu.html");
+
+// This makes it easier to run tests by handling things like opening the menu,
+// opening new windows, making assertions, etc. Methods on |test| can be called
+// on instances of this class. Don't forget to call done() to end the test!
+// WARNING: This looks up items in popups by comparing labels, so don't give two
+// items the same label.
+function TestHelper(assert, done) {
+ // Methods on the wrapped test can be called on this object.
+ for (var prop in assert)
+ this[prop] = (...args) => assert[prop].apply(assert, args);
+ this.assert = assert;
+ this.end = done;
+ this.loaders = [];
+ this.browserWindow = getMostRecentBrowserWindow();
+ this.overflowThreshValue = require("sdk/preferences/service").
+ get(OVERFLOW_THRESH_PREF, OVERFLOW_THRESH_DEFAULT);
+ this.done = this.done.bind(this);
+}
+
+TestHelper.prototype = {
+ get contextMenuPopup() {
+ return this.browserWindow.document.getElementById("contentAreaContextMenu");
+ },
+
+ get contextMenuSeparator() {
+ return this.browserWindow.document.querySelector("." + SEPARATOR_CLASS);
+ },
+
+ get overflowPopup() {
+ return this.browserWindow.document.querySelector("." + OVERFLOW_POPUP_CLASS);
+ },
+
+ get overflowSubmenu() {
+ return this.browserWindow.document.querySelector("." + OVERFLOW_MENU_CLASS);
+ },
+
+ get tabBrowser() {
+ return this.browserWindow.gBrowser;
+ },
+
+ // Asserts that elt, a DOM element representing item, looks OK.
+ checkItemElt: function (elt, item) {
+ let itemType = this.getItemType(item);
+
+ switch (itemType) {
+ case "Item":
+ this.assert.equal(elt.localName, "menuitem",
+ "Item DOM element should be a xul:menuitem");
+ if (typeof(item.data) === "string") {
+ this.assert.equal(elt.getAttribute("value"), item.data,
+ "Item should have correct data");
+ }
+ break
+ case "Menu":
+ this.assert.equal(elt.localName, "menu",
+ "Menu DOM element should be a xul:menu");
+ let subPopup = elt.firstChild;
+ this.assert.ok(subPopup, "xul:menu should have a child");
+ this.assert.equal(subPopup.localName, "menupopup",
+ "xul:menu's first child should be a menupopup");
+ break;
+ case "Separator":
+ this.assert.equal(elt.localName, "menuseparator",
+ "Separator DOM element should be a xul:menuseparator");
+ break;
+ }
+
+ if (itemType === "Item" || itemType === "Menu") {
+ this.assert.equal(elt.getAttribute("label"), item.label,
+ "Item should have correct title");
+
+ // validate accesskey prop
+ if (item.accesskey) {
+ this.assert.equal(elt.getAttribute("accesskey"),
+ item.accesskey,
+ "Item should have correct accesskey");
+ }
+ else {
+ this.assert.equal(elt.getAttribute("accesskey"),
+ "",
+ "Item should not have accesskey");
+ }
+
+ // validate image prop
+ if (typeof(item.image) === "string") {
+ this.assert.equal(elt.getAttribute("image"), item.image,
+ "Item should have correct image");
+ if (itemType === "Menu")
+ this.assert.ok(elt.classList.contains("menu-iconic"),
+ "Menus with images should have the correct class")
+ else
+ this.assert.ok(elt.classList.contains("menuitem-iconic"),
+ "Items with images should have the correct class")
+ }
+ else {
+ this.assert.ok(!elt.getAttribute("image"),
+ "Item should not have image");
+ this.assert.ok(!elt.classList.contains("menu-iconic") && !elt.classList.contains("menuitem-iconic"),
+ "The iconic classes should not be present")
+ }
+ }
+ },
+
+ // Asserts that the context menu looks OK given the arguments. presentItems
+ // are items that have been added to the menu. absentItems are items that
+ // shouldn't match the current context. removedItems are items that have been
+ // removed from the menu.
+ checkMenu: function (presentItems, absentItems, removedItems) {
+ // Count up how many top-level items there are
+ let total = 0;
+ for (let item of presentItems) {
+ if (absentItems.indexOf(item) < 0 && removedItems.indexOf(item) < 0)
+ total++;
+ }
+
+ let separator = this.contextMenuSeparator;
+ if (total == 0) {
+ this.assert.ok(!separator || separator.hidden,
+ "separator should not be present");
+ }
+ else {
+ this.assert.ok(separator && !separator.hidden,
+ "separator should be present");
+ }
+
+ let mainNodes = this.browserWindow.document.querySelectorAll("#contentAreaContextMenu > ." + ITEM_CLASS);
+ let overflowNodes = this.browserWindow.document.querySelectorAll("." + OVERFLOW_POPUP_CLASS + " > ." + ITEM_CLASS);
+
+ this.assert.ok(mainNodes.length == 0 || overflowNodes.length == 0,
+ "Should only see nodes at the top level or in overflow");
+
+ let overflow = this.overflowSubmenu;
+ if (this.shouldOverflow(total)) {
+ this.assert.ok(overflow && !overflow.hidden,
+ "overflow menu should be present");
+ this.assert.equal(mainNodes.length, 0,
+ "should be no items in the main context menu");
+ }
+ else {
+ this.assert.ok(!overflow || overflow.hidden,
+ "overflow menu should not be present");
+ // When visible nodes == 0 they could be in overflow or top level
+ if (total > 0) {
+ this.assert.equal(overflowNodes.length, 0,
+ "should be no items in the overflow context menu");
+ }
+ }
+
+ // Iterate over wherever the nodes have ended up
+ let nodes = mainNodes.length ? mainNodes : overflowNodes;
+ this.checkNodes(nodes, presentItems, absentItems, removedItems)
+ let pos = 0;
+ },
+
+ // Recurses through the item hierarchy of presentItems comparing it to the
+ // node hierarchy of nodes. Any items in removedItems will be skipped (so
+ // should not exist in the XUL), any items in absentItems must exist and be
+ // hidden
+ checkNodes: function (nodes, presentItems, absentItems, removedItems) {
+ let pos = 0;
+ for (let item of presentItems) {
+ // Removed items shouldn't be in the list
+ if (removedItems.indexOf(item) >= 0)
+ continue;
+
+ if (nodes.length <= pos) {
+ this.assert.ok(false, "Not enough nodes");
+ return;
+ }
+
+ let hidden = absentItems.indexOf(item) >= 0;
+
+ this.checkItemElt(nodes[pos], item);
+ this.assert.equal(nodes[pos].hidden, hidden,
+ "hidden should be set correctly");
+
+ // The contents of hidden menus doesn't matter so much
+ if (!hidden && this.getItemType(item) == "Menu") {
+ this.assert.equal(nodes[pos].firstChild.localName, "menupopup",
+ "menu XUL should contain a menupopup");
+ this.checkNodes(nodes[pos].firstChild.childNodes, item.items, absentItems, removedItems);
+ }
+
+ if (pos > 0)
+ this.assert.equal(nodes[pos].previousSibling, nodes[pos - 1],
+ "nodes should all be in the same group");
+ pos++;
+ }
+
+ this.assert.equal(nodes.length, pos,
+ "should have checked all the XUL nodes");
+ },
+
+ // Attaches an event listener to node. The listener is automatically removed
+ // when it's fired (so it's assumed it will fire), and callback is called
+ // after a short delay. Since the module we're testing relies on the same
+ // event listeners to do its work, this is to give them a little breathing
+ // room before callback runs. Inside callback |this| is this object.
+ // Optionally you can pass a function to test if the event is the event you
+ // want.
+ delayedEventListener: function (node, event, callback, useCapture, isValid) {
+ const self = this;
+ node.addEventListener(event, function handler(evt) {
+ if (isValid && !isValid(evt))
+ return;
+ node.removeEventListener(event, handler, useCapture);
+ timer.setTimeout(function () {
+ try {
+ callback.call(self, evt);
+ }
+ catch (err) {
+ self.assert.fail(err);
+ self.end();
+ }
+ }, 20);
+ }, useCapture);
+ },
+
+ // Call to finish the test.
+ done: function () {
+ const self = this;
+ function commonDone() {
+ this.closeTab();
+
+ while (this.loaders.length) {
+ this.loaders[0].unload();
+ }
+
+ require("sdk/preferences/service").set(OVERFLOW_THRESH_PREF, self.overflowThreshValue);
+
+ this.end();
+ }
+
+ function closeBrowserWindow() {
+ if (this.oldBrowserWindow) {
+ this.delayedEventListener(this.browserWindow, "unload", commonDone,
+ false);
+ this.browserWindow.close();
+ this.browserWindow = this.oldBrowserWindow;
+ delete this.oldBrowserWindow;
+ }
+ else {
+ commonDone.call(this);
+ }
+ };
+
+ if (this.contextMenuPopup.state == "closed") {
+ closeBrowserWindow.call(this);
+ }
+ else {
+ this.delayedEventListener(this.contextMenuPopup, "popuphidden",
+ function () {
+ return closeBrowserWindow.call(this);
+ },
+ false);
+ this.contextMenuPopup.hidePopup();
+ }
+ },
+
+ closeTab: function() {
+ if (this.tab) {
+ this.tabBrowser.removeTab(this.tab);
+ this.tabBrowser.selectedTab = this.oldSelectedTab;
+ this.tab = null;
+ }
+ },
+
+ // Returns the DOM element in popup corresponding to item.
+ // WARNING: The element is found by comparing labels, so don't give two items
+ // the same label.
+ getItemElt: function (popup, item) {
+ let nodes = popup.childNodes;
+ for (let i = nodes.length - 1; i >= 0; i--) {
+ if (this.getItemType(item) === "Separator") {
+ if (nodes[i].localName === "menuseparator")
+ return nodes[i];
+ }
+ else if (nodes[i].getAttribute("label") === item.label)
+ return nodes[i];
+ }
+ return null;
+ },
+
+ // Returns "Item", "Menu", or "Separator".
+ getItemType: function (item) {
+ // Could use instanceof here, but that would require accessing the loader
+ // that created the item, and I don't want to A) somehow search through the
+ // this.loaders list to find it, and B) assume there are any live loaders at
+ // all.
+ return /^\[object (Item|Menu|Separator)/.exec(item.toString())[1];
+ },
+
+ // Returns a wrapper around a new loader: { loader, cm, unload, globalScope }.
+ // loader is a Cuddlefish sandboxed loader, cm is the context menu module,
+ // globalScope is the context menu module's global scope, and unload is a
+ // function that unloads the loader and associated resources.
+ newLoader: function () {
+ const self = this;
+ const selfModule = require('sdk/self');
+ let loader = Loader(module, null, null, {
+ modules: {
+ "sdk/self": merge({}, selfModule, {
+ data: merge({}, selfModule.data, require("../fixtures"))
+ })
+ }
+ });
+
+ let wrapper = {
+ loader: loader,
+ cm: loader.require("sdk/context-menu"),
+ globalScope: loader.sandbox("sdk/context-menu"),
+ unload: function () {
+ loader.unload();
+ let idx = self.loaders.indexOf(wrapper);
+ if (idx < 0)
+ throw new Error("Test error: tried to unload nonexistent loader");
+ self.loaders.splice(idx, 1);
+ }
+ };
+ this.loaders.push(wrapper);
+ return wrapper;
+ },
+
+ // As above but the loader has private-browsing support enabled.
+ newPrivateLoader: function() {
+ let base = require("@loader/options");
+
+ // Clone current loader's options adding the private-browsing permission
+ let options = merge({}, base, {
+ metadata: merge({}, base.metadata || {}, {
+ permissions: merge({}, base.metadata.permissions || {}, {
+ 'private-browsing': true
+ })
+ })
+ });
+
+ const self = this;
+ let loader = Loader(module, null, options);
+ let wrapper = {
+ loader: loader,
+ cm: loader.require("sdk/context-menu"),
+ globalScope: loader.sandbox("sdk/context-menu"),
+ unload: function () {
+ loader.unload();
+ let idx = self.loaders.indexOf(wrapper);
+ if (idx < 0)
+ throw new Error("Test error: tried to unload nonexistent loader");
+ self.loaders.splice(idx, 1);
+ }
+ };
+ this.loaders.push(wrapper);
+ return wrapper;
+ },
+
+ // Returns true if the count crosses the overflow threshold.
+ shouldOverflow: function (count) {
+ return count >
+ (this.loaders.length ?
+ this.loaders[0].loader.require("sdk/preferences/service").
+ get(OVERFLOW_THRESH_PREF, OVERFLOW_THRESH_DEFAULT) :
+ OVERFLOW_THRESH_DEFAULT);
+ },
+
+ // Loads scripts necessary in the content process
+ loadFrameScript: function(browser = this.browserWindow.gBrowser.selectedBrowser) {
+ function frame_script() {
+ let { interfaces: Ci } = Components;
+ addMessageListener('test:contextmenu', ({ data: { selectors } }) => {
+ let targetNode = null;
+ let contentWin = content;
+ if (selectors) {
+ while (selectors.length) {
+ targetNode = contentWin.document.querySelector(selectors.shift());
+ if (selectors.length)
+ contentWin = targetNode.contentWindow;
+ }
+ }
+
+ let rect = targetNode ?
+ targetNode.getBoundingClientRect() :
+ { left: 0, top: 0, width: 0, height: 0 };
+ contentWin.QueryInterface(Ci.nsIInterfaceRequestor)
+ .getInterface(Ci.nsIDOMWindowUtils)
+ .sendMouseEvent('contextmenu',
+ rect.left + (rect.width / 2),
+ rect.top + (rect.height / 2),
+ 2, 1, 0);
+ });
+
+ addMessageListener('test:ping', () => {
+ sendAsyncMessage('test:pong');
+ });
+
+ addMessageListener('test:select', ({ data: { selector, start, end } }) => {
+ let element = content.document.querySelector(selector);
+ element.focus();
+ if (end === null)
+ end = element.value.length;
+ element.setSelectionRange(start, end);
+ });
+ }
+
+ let messageManager = browser.messageManager;
+ messageManager.loadFrameScript("data:,(" + frame_script.toString() + ")();", true);
+ },
+
+ selectRange: function(selector, start, end) {
+ let messageManager = this.browserWindow.gBrowser.selectedBrowser.messageManager;
+ messageManager.sendAsyncMessage('test:select', { selector, start, end });
+ },
+
+ // Opens the context menu on the current page. If selectors is null, the
+ // menu is opened in the top-left corner. onShowncallback is passed the
+ // popup. selectors is an array of selectors. Starting from the main document
+ // each selector points to an iframe, the last selector gives the target node.
+ // In the simple case of a single selector just that string can be passed
+ // instead of an array
+ showMenu: function(selectors, onshownCallback) {
+ let { promise, resolve } = defer();
+
+ if (selectors && !Array.isArray(selectors))
+ selectors = [selectors];
+
+ let sendEvent = () => {
+ let menu = this.browserWindow.document.getElementById("contentAreaContextMenu");
+ this.delayedEventListener(menu, "popupshowing",
+ function (e) {
+ let popup = e.target;
+ if (onshownCallback) {
+ onshownCallback.call(this, popup);
+ }
+ resolve(popup);
+ }, false);
+
+ let messageManager = this.browserWindow.gBrowser.selectedBrowser.messageManager;
+ messageManager.sendAsyncMessage('test:contextmenu', { selectors });
+ }
+
+ // Bounces an asynchronous message through the browser message manager.
+ // This ensures that any pending messages have been delivered to the frame
+ // scripts and so the remote proxies have been updated
+ let flushMessages = () => {
+ let listener = () => {
+ messageManager.removeMessageListener('test:pong', listener);
+ sendEvent();
+ };
+
+ let messageManager = this.browserWindow.gBrowser.selectedBrowser.messageManager;
+ messageManager.addMessageListener('test:pong', listener);
+ messageManager.sendAsyncMessage('test:ping');
+ }
+
+ // If a new tab or window has not yet been opened, open a new tab now. For
+ // some reason using the tab already opened when the test starts causes
+ // leaks. See bug 566351 for details.
+ if (!selectors && !this.oldSelectedTab && !this.oldBrowserWindow) {
+ this.oldSelectedTab = this.tabBrowser.selectedTab;
+ this.tab = this.tabBrowser.addTab("about:blank");
+ let browser = this.tabBrowser.getBrowserForTab(this.tab);
+
+ this.delayedEventListener(browser, "load", function () {
+ this.tabBrowser.selectedTab = this.tab;
+ this.loadFrameScript();
+ flushMessages();
+ }, true);
+ }
+ else {
+ flushMessages();
+ }
+
+ return promise;
+ },
+
+ hideMenu: function(onhiddenCallback) {
+ this.delayedEventListener(this.browserWindow, "popuphidden", onhiddenCallback);
+
+ this.contextMenuPopup.hidePopup();
+ },
+
+ // Opens a new browser window. The window will be closed automatically when
+ // done() is called.
+ withNewWindow: function (onloadCallback, makePrivate = false) {
+ let win = this.browserWindow.OpenBrowserWindow({ private: makePrivate });
+ observers.once("browser-delayed-startup-finished", () => {
+ // Open a new tab so we can make sure it is remote and loaded
+ win.gBrowser.selectedTab = win.gBrowser.addTab();
+ this.loadFrameScript();
+ this.delayedEventListener(win.gBrowser.selectedBrowser, "load", onloadCallback, true);
+ });
+ this.oldBrowserWindow = this.browserWindow;
+ this.browserWindow = win;
+ },
+
+ // Opens a new private browser window. The window will be closed
+ // automatically when done() is called.
+ withNewPrivateWindow: function (onloadCallback) {
+ this.withNewWindow(onloadCallback, true);
+ },
+
+ // Opens a new tab with our test page in the current window. The tab will
+ // be closed automatically when done() is called.
+ withTestDoc: function (onloadCallback) {
+ this.oldSelectedTab = this.tabBrowser.selectedTab;
+ this.tab = this.tabBrowser.addTab(TEST_DOC_URL);
+ let browser = this.tabBrowser.getBrowserForTab(this.tab);
+
+ this.delayedEventListener(browser, "load", function () {
+ this.tabBrowser.selectedTab = this.tab;
+ this.loadFrameScript();
+ onloadCallback.call(this, browser.contentWindow, browser.contentDocument);
+ }, true, function(evt) {
+ return evt.target.location == TEST_DOC_URL;
+ });
+ }
+};
+exports.TestHelper = TestHelper;
diff --git a/addon-sdk/source/test/context-menu/util.js b/addon-sdk/source/test/context-menu/util.js
new file mode 100644
index 000000000..af9de938d
--- /dev/null
+++ b/addon-sdk/source/test/context-menu/util.js
@@ -0,0 +1,141 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Cc, Ci } = require("chrome");
+const { getMostRecentBrowserWindow } = require("sdk/window/utils");
+const { open: openWindow, close: closeWindow } = require("sdk/window/helpers");
+const tabUtils = require("sdk/tabs/utils");
+const { map, filter, object, reduce, keys, symbols,
+ pairs, values, each, some, isEvery, count } = require("sdk/util/sequence");
+const { when } = require("sdk/dom/events");
+
+const { Task } = require("resource://gre/modules/Task.jsm");
+
+var observerService = Cc["@mozilla.org/observer-service;1"]
+ .getService(Ci.nsIObserverService);
+
+const framescriptURI = require.resolve("./framescript");
+
+const _target = ({target}) => target;
+
+exports.openWindow = openWindow;
+exports.closeWindow = closeWindow;
+
+const getActiveTab = (window=getMostRecentBrowserWindow()) =>
+ tabUtils.getActiveTab(window)
+
+const openTab = (url, window=getMostRecentBrowserWindow()) => {
+ const tab = tabUtils.openTab(window, url);
+ const browser = tabUtils.getBrowserForTab(tab);
+ browser.messageManager.loadFrameScript(framescriptURI, false);
+
+ return when(browser, "load", true).then(_ => tab);
+};
+exports.openTab = openTab;
+
+const openContextMenu = (selector, tab=getActiveTab()) => {
+ const browser = tabUtils.getBrowserForTab(tab);
+ browser.
+ messageManager.
+ sendAsyncMessage("sdk/test/context-menu/open",
+ {target: selector});
+
+ return when(tab.ownerDocument.defaultView, "popupshown").
+ then(_target);
+};
+exports.openContextMenu = openContextMenu;
+
+const closeContextMenu = (menu) => {
+ const result = when(menu.ownerDocument.defaultView, "popuphidden").
+ then(_target);
+
+ menu.hidePopup();
+ return result;
+};
+exports.closeContextMenu = closeContextMenu;
+
+const closeTab = (tab) => {
+ const result = when(tab, "TabClose").then(_ => tab);
+ tabUtils.closeTab(tab);
+
+ return result;
+};
+exports.closeTab = closeTab;
+
+const select = (target, tab=getActiveTab()) =>
+ new Promise(resolve => {
+ const {messageManager} = tabUtils.getBrowserForTab(tab);
+ messageManager.
+ sendAsyncMessage("sdk/test/context-menu/select",
+ target);
+
+ messageManager.addMessageListener("sdk/test/context-menu/selected", {
+ receiveMessage({name}) {
+ messageManager.removeMessageListener(name, this);
+ resolve();
+ }
+ });
+ });
+exports.select = select;
+
+const attributeBlocklist = new Set(["data-component-path"]);
+const attributeRenameTable = Object.assign(Object.create(null), {
+ class: "className"
+});
+const readAttributes = node =>
+ object(...map(({name, value}) => [attributeRenameTable[name] || name, value],
+ filter(({name}) => !attributeBlocklist.has(name),
+ node.attributes)));
+exports.readAttributes = readAttributes;
+
+const readNode = node =>
+ Object.assign(readAttributes(node),
+ {tagName: node.tagName, namespaceURI: node.namespaceURI},
+ node.children.length ?
+ {children: [...map(readNode, node.children)]} :
+ {});
+exports.readNode = readNode;
+
+const captureContextMenu = (target=":root", options={}) => Task.spawn(function*() {
+ const window = options.window || getMostRecentBrowserWindow();
+ const tab = options.tab || tabUtils.getActiveTab(window);
+
+ const menu = yield openContextMenu(target, tab);
+ const tree = readNode(menu.querySelector(".sdk-context-menu-extension"));
+ yield closeContextMenu(menu);
+ return tree;
+});
+exports.captureContextMenu = captureContextMenu;
+
+const withTab = (test, uri="about:blank") => function*(assert) {
+ const tab = yield openTab(uri);
+ try {
+ yield* test(assert, tab);
+ }
+ finally {
+ yield closeTab(tab);
+ }
+};
+exports.withTab = withTab;
+
+const withWindow = () => function*(assert) {
+ const window = yield openWindow();
+ try {
+ yield* test(assert, window);
+ }
+ finally {
+ yield closeWindow(window);
+ }
+};
+exports.withWindow = withWindow;
+
+const withItems = (items, body) => function*() {
+ try {
+ yield* body(items);
+ } finally {
+ Object.keys(items).forEach(key => items[key].destroy());
+ }
+}();
+exports.withItems = withItems;
diff --git a/addon-sdk/source/test/event/helpers.js b/addon-sdk/source/test/event/helpers.js
new file mode 100644
index 000000000..d0d63d46f
--- /dev/null
+++ b/addon-sdk/source/test/event/helpers.js
@@ -0,0 +1,112 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const { on, once, off, emit, count } = require("sdk/event/core");
+
+const { setImmediate, setTimeout } = require("sdk/timers");
+const { defer } = require("sdk/core/promise");
+
+/**
+ * Utility function that returns a promise once the specified event's `type`
+ * is emitted on the given `target`, or the delay specified is passed.
+ *
+ * @param {Object|Number} [target]
+ * The delay to wait, or the object that receives the event.
+ * If not given, the function returns a promise that will be resolved
+ * as soon as possible.
+ * @param {String} [type]
+ * A string representing the event type to waiting for.
+ * @param {Boolean} [capture]
+ * If `true`, `capture` indicates that the user wishes to initiate capture.
+ *
+ * @returns {Promise}
+ * A promise resolved once the delay given is passed, or the object
+ * receives the event specified
+ */
+const wait = function(target, type, capture) {
+ let { promise, resolve, reject } = defer();
+
+ if (!arguments.length) {
+ setImmediate(resolve);
+ }
+ else if (typeof(target) === "number") {
+ setTimeout(resolve, target);
+ }
+ else if (typeof(target.once) === "function") {
+ target.once(type, resolve);
+ }
+ else if (typeof(target.addEventListener) === "function") {
+ target.addEventListener(type, function listener(...args) {
+ this.removeEventListener(type, listener, capture);
+ resolve(...args);
+ }, capture);
+ }
+ else if (typeof(target) === "object" && target !== null) {
+ once(target, type, resolve);
+ }
+ else {
+ reject('Invalid target given.');
+ }
+
+ return promise;
+};
+exports.wait = wait;
+
+function scenario(setup) {
+ return function(unit) {
+ return function(assert) {
+ let actual = [];
+ let input = {};
+ unit(input, function(output, events, expected, message) {
+ let result = setup(output, expected, actual);
+
+ events.forEach(event => emit(input, "data", event));
+
+ assert.deepEqual(actual, result, message);
+ });
+ }
+ }
+}
+
+exports.emits = scenario(function(output, expected, actual) {
+ on(output, "data", function(data) {
+ return actual.push(this, data);
+ });
+
+ return expected.reduce(($$, $) => $$.concat(output, $), []);
+});
+
+exports.registerOnce = scenario(function(output, expected, actual) {
+ function listener(data) {
+ return actual.push(data);
+ }
+ on(output, "data", listener);
+ on(output, "data", listener);
+ on(output, "data", listener);
+
+ return expected;
+});
+
+exports.ignoreNew = scenario(function(output, expected, actual) {
+ on(output, "data", function(data) {
+ actual.push(data + "#1");
+ on(output, "data", function(data) {
+ actual.push(data + "#2");
+ });
+ });
+
+ return expected.map($ => $ + "#1");
+});
+
+exports.FIFO = scenario(function(target, expected, actual) {
+ on(target, "data", $ => actual.push($ + "#1"));
+ on(target, "data", $ => actual.push($ + "#2"));
+ on(target, "data", $ => actual.push($ + "#3"));
+
+ return expected.reduce(function(result, value) {
+ return result.concat(value + "#1", value + "#2", value + "#3");
+ }, []);
+});
diff --git a/addon-sdk/source/test/fixtures.js b/addon-sdk/source/test/fixtures.js
new file mode 100644
index 000000000..a1790113d
--- /dev/null
+++ b/addon-sdk/source/test/fixtures.js
@@ -0,0 +1,41 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const { uri } = module;
+const prefix = uri.substr(0, uri.lastIndexOf("/") + 1) + "fixtures/";
+
+exports.url = (path="") => path && path.includes(":")
+ ? path
+ : prefix + path.replace(/^\.\//, "");
+
+const base64jpeg = "data:image/jpeg;base64,%2F9j%2F4AAQSkZJRgABAQAAAQABAAD%2F" +
+ "2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCg" +
+ "sOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD%2F2wBDAQMDAwQDBAgEBAgQCw" +
+ "kLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ" +
+ "EBAQEBAQEBD%2FwAARCAAgACADAREAAhEBAxEB%2F8QAHwAAAQUBAQEBAQ" +
+ "EAAAAAAAAAAAECAwQFBgcICQoL%2F8QAtRAAAgEDAwIEAwUFBAQAAAF9AQ" +
+ "IDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRol" +
+ "JicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eX" +
+ "qDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJ" +
+ "ytLT1NXW19jZ2uHi4%2BTl5ufo6erx8vP09fb3%2BPn6%2F8QAHwEAAwEB" +
+ "AQEBAQEBAQAAAAAAAAECAwQFBgcICQoL%2F8QAtREAAgECBAQDBAcFBAQA" +
+ "AQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNO" +
+ "El8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0" +
+ "dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6ws" +
+ "PExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3%2BPn6%2F9oADAMB" +
+ "AAIRAxEAPwD5Kr8kP9CwoA5f4m%2F8iRqX%2FbH%2FANHJXr5F%2FwAjCn" +
+ "8%2F%2FSWfnnir%2FwAkji%2F%2B4f8A6dgeD1%2BiH8bn1BX5If6FmFqW" +
+ "pXtveyQwzbUXGBtB7D2r9l4U4UyjMsoo4rFUeacua75pLaUktFJLZH5NxN" +
+ "xNmmX5pVw2Gq8sI8tlyxe8U3q03uzD8S3dxqOi3NneSeZDJs3LgDOHBHI5" +
+ "6gV%2BkcG%2BH%2FDmJzuhSq4e8XzfbqfyS%2FvH5rx1xTm2MyDEUa1W8X" +
+ "yXXLFbTi%2BkThv7B0r%2FAJ9f%2FH2%2Fxr90%2FwCIVcI%2F9An%2FAJ" +
+ "Uq%2FwDyZ%2FO%2F16v%2FADfgv8j0r%2FhZvgj%2FAKDf%2FktN%2FwDE" +
+ "V%2Fnr%2FYWYf8%2B%2Fxj%2Fmf3R%2FxFXhH%2FoL%2FwDKdX%2F5Azrv" +
+ "xLouo3D3lne%2BZDJja3luM4GDwRnqDX9LeH%2FBud4nhzD1aVC8Xz%2Fa" +
+ "h%2Fz8l%2FePx%2FinjrIMZm1WtRxF4vls%2BSa2jFdYlDUdRsp7OSKKbc" +
+ "7YwNpHce1fqfCvCub5bm9HFYqjywjzXfNF7xklopN7s%2BC4l4lyvMMrq4" +
+ "fD1bzfLZcsltJPqktkYlfsZ%2BUnBV%2FnufVnXaD%2FAMgqD%2FgX%2Fo" +
+ "Rr%2BxvCr%2FkkcJ%2F3E%2F8ATsz5%2FHfx5fL8kX6%2FQjkCgD%2F%2F" +
+ "2Q%3D%3D";
+exports.base64jpeg = base64jpeg;
diff --git a/addon-sdk/source/test/fixtures/addon-install-unit-test@mozilla.com.xpi b/addon-sdk/source/test/fixtures/addon-install-unit-test@mozilla.com.xpi
new file mode 100644
index 000000000..516c1fc3c
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/addon-install-unit-test@mozilla.com.xpi
Binary files differ
diff --git a/addon-sdk/source/test/fixtures/addon-sdk/data/border-style.css b/addon-sdk/source/test/fixtures/addon-sdk/data/border-style.css
new file mode 100644
index 000000000..6941ccb20
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/addon-sdk/data/border-style.css
@@ -0,0 +1,4 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+div { border-style: dashed; }
diff --git a/addon-sdk/source/test/fixtures/addon-sdk/data/test-contentScriptFile.js b/addon-sdk/source/test/fixtures/addon-sdk/data/test-contentScriptFile.js
new file mode 100644
index 000000000..7dc0e3f24
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/addon-sdk/data/test-contentScriptFile.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+self.postMessage("msg from contentScriptFile");
diff --git a/addon-sdk/source/test/fixtures/addon-sdk/data/test-page-worker.html b/addon-sdk/source/test/fixtures/addon-sdk/data/test-page-worker.html
new file mode 100644
index 000000000..85264034a
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/addon-sdk/data/test-page-worker.html
@@ -0,0 +1,13 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<html>
+<head>
+ <meta charset="UTF-8">
+ <title>Page Worker test</title>
+</head>
+<body>
+ <p id="paragraph">Lorem ipsum dolor sit amet.</p>
+</body>
+</html>
diff --git a/addon-sdk/source/test/fixtures/addon-sdk/data/test-page-worker.js b/addon-sdk/source/test/fixtures/addon-sdk/data/test-page-worker.js
new file mode 100644
index 000000000..5114fe4e0
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/addon-sdk/data/test-page-worker.js
@@ -0,0 +1,29 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+
+// get title directly
+self.postMessage(["equal", document.title, "Page Worker test",
+ "Correct page title accessed directly"]);
+
+// get <p> directly
+var p = document.getElementById("paragraph");
+self.postMessage(["ok", !!p, "<p> can be accessed directly"]);
+self.postMessage(["equal", p.firstChild.nodeValue,
+ "Lorem ipsum dolor sit amet.",
+ "Correct text node expected"]);
+
+// Modify page
+var div = document.createElement("div");
+div.setAttribute("id", "block");
+div.appendChild(document.createTextNode("Test text created"));
+document.body.appendChild(div);
+
+// Check back the modification
+div = document.getElementById("block");
+self.postMessage(["ok", !!div, "<div> can be accessed directly"]);
+self.postMessage(["equal", div.firstChild.nodeValue,
+ "Test text created", "Correct text node expected"]);
+self.postMessage(["done"]);
+
diff --git a/addon-sdk/source/test/fixtures/addon-sdk/data/test.html b/addon-sdk/source/test/fixtures/addon-sdk/data/test.html
new file mode 100644
index 000000000..181e85f9b
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/addon-sdk/data/test.html
@@ -0,0 +1,13 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<html>
+ <head>
+ <meta charset="UTF-8">
+ <title>foo</title>
+ </head>
+ <body>
+ <p>bar</p>
+ </body>
+</html>
diff --git a/addon-sdk/source/test/fixtures/addon/bootstrap.js b/addon-sdk/source/test/fixtures/addon/bootstrap.js
new file mode 100644
index 000000000..9134b4d92
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/addon/bootstrap.js
@@ -0,0 +1,9 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { utils: Cu } = Components;
+const {require} = Cu.import(`${ROOT}/toolkit/require.js`, {});
+const {Bootstrap} = require(`${ROOT}/sdk/addon/bootstrap.js`);
+var {startup, shutdown, install, uninstall} = new Bootstrap();
diff --git a/addon-sdk/source/test/fixtures/addon/index.js b/addon-sdk/source/test/fixtures/addon/index.js
new file mode 100644
index 000000000..c507c9624
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/addon/index.js
@@ -0,0 +1,4 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
diff --git a/addon-sdk/source/test/fixtures/addon/package.json b/addon-sdk/source/test/fixtures/addon/package.json
new file mode 100644
index 000000000..488ec5b3b
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/addon/package.json
@@ -0,0 +1,5 @@
+{
+ "id": "addon@jetpack",
+ "name": "addon",
+ "version": "0.0.1"
+}
diff --git a/addon-sdk/source/test/fixtures/bootstrap-addon/META-INF/manifest.mf b/addon-sdk/source/test/fixtures/bootstrap-addon/META-INF/manifest.mf
new file mode 100644
index 000000000..5105792a7
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/bootstrap-addon/META-INF/manifest.mf
@@ -0,0 +1,17 @@
+Manifest-Version: 1.0
+
+Name: install.rdf
+Digest-Algorithms: MD5 SHA1
+MD5-Digest: N643P4YjKKlwZUqrfLi4ow==
+SHA1-Digest: XK/2qoOrnuYo8xNYeLvB8DlUIik=
+
+Name: bootstrap.js
+Digest-Algorithms: MD5 SHA1
+MD5-Digest: XH+mMa/H9aj3hm/ZtVKviw==
+SHA1-Digest: LMmd1aTash/onjS1eAYIshgrdnM=
+
+Name: options.xul
+Digest-Algorithms: MD5 SHA1
+MD5-Digest: XeELNGdttv8Lq66lT8ykbQ==
+SHA1-Digest: 4KO6/RBoe10rYOGS+gFSHuuWi4Y=
+
diff --git a/addon-sdk/source/test/fixtures/bootstrap-addon/META-INF/mozilla.rsa b/addon-sdk/source/test/fixtures/bootstrap-addon/META-INF/mozilla.rsa
new file mode 100644
index 000000000..6b1c3a9ce
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/bootstrap-addon/META-INF/mozilla.rsa
Binary files differ
diff --git a/addon-sdk/source/test/fixtures/bootstrap-addon/META-INF/mozilla.sf b/addon-sdk/source/test/fixtures/bootstrap-addon/META-INF/mozilla.sf
new file mode 100644
index 000000000..99f343296
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/bootstrap-addon/META-INF/mozilla.sf
@@ -0,0 +1,4 @@
+Signature-Version: 1.0
+MD5-Digest-Manifest: cnqbpqBEVoHgJi/ocCsKKA==
+SHA1-Digest-Manifest: eDmvBAkodeNbk/0ujttYgF8KDgI=
+
diff --git a/addon-sdk/source/test/fixtures/bootstrap-addon/bootstrap.js b/addon-sdk/source/test/fixtures/bootstrap-addon/bootstrap.js
new file mode 100644
index 000000000..dea1d7b90
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/bootstrap-addon/bootstrap.js
@@ -0,0 +1,10 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+function install(data, reason) {}
+function uninstall(data, reason) {}
+
+function startup(data, reasonCode) {};
+function shutdown(data, reasonCode) {};
diff --git a/addon-sdk/source/test/fixtures/bootstrap-addon/install.rdf b/addon-sdk/source/test/fixtures/bootstrap-addon/install.rdf
new file mode 100644
index 000000000..221dc9c3d
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/bootstrap-addon/install.rdf
@@ -0,0 +1,27 @@
+<?xml version="1.0"?>
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
+ xmlns:em="http://www.mozilla.org/2004/em-rdf#">
+ <Description about="urn:mozilla:install-manifest">
+ <em:id>test-bootstrap-addon@mozilla.com</em:id>
+ <em:name>Test Bootstrap Add-on</em:name>
+ <em:creator>Erik Vold</em:creator>
+ <em:optionsType>2</em:optionsType>
+ <em:version>1.0</em:version>
+ <em:type>2</em:type>
+ <em:bootstrap>true</em:bootstrap>
+ <em:unpack>false</em:unpack>
+
+ <!-- Firefox -->
+ <em:targetApplication>
+ <Description>
+ <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
+ <em:minVersion>26.0</em:minVersion>
+ <em:maxVersion>*</em:maxVersion>
+ </Description>
+ </em:targetApplication>
+ </Description>
+</RDF>
diff --git a/addon-sdk/source/test/fixtures/bootstrap-addon/options.xul b/addon-sdk/source/test/fixtures/bootstrap-addon/options.xul
new file mode 100644
index 000000000..aafa46d88
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/bootstrap-addon/options.xul
@@ -0,0 +1,3 @@
+<?xml version="1.0"?>
+<!DOCTYPE mydialog SYSTEM "chrome://myaddon/locale/mydialog.dtd">
+<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"></vbox>
diff --git a/addon-sdk/source/test/fixtures/bootstrap/utils.js b/addon-sdk/source/test/fixtures/bootstrap/utils.js
new file mode 100644
index 000000000..aa7ea2d18
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/bootstrap/utils.js
@@ -0,0 +1,52 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Cu, Cc, Ci } = require("chrome");
+const { evaluate } = require("sdk/loader/sandbox");
+
+const ROOT = require.resolve("sdk/base64").replace("/sdk/base64.js", "");
+
+// Note: much of this test code is from
+// http://dxr.mozilla.org/mozilla-central/source/toolkit/mozapps/extensions/internal/XPIProvider.jsm
+const BOOTSTRAP_REASONS = {
+ APP_STARTUP : 1,
+ APP_SHUTDOWN : 2,
+ ADDON_ENABLE : 3,
+ ADDON_DISABLE : 4,
+ ADDON_INSTALL : 5,
+ ADDON_UNINSTALL : 6,
+ ADDON_UPGRADE : 7,
+ ADDON_DOWNGRADE : 8
+};
+
+function createBootstrapScope(options) {
+ let { uri, id: aId } = options;
+ let principal = Cc["@mozilla.org/systemprincipal;1"].
+ createInstance(Ci.nsIPrincipal);
+
+ let bootstrapScope = new Cu.Sandbox(principal, {
+ sandboxName: uri,
+ wantGlobalProperties: ["indexedDB"],
+ addonId: aId,
+ metadata: { addonID: aId, URI: uri }
+ });
+
+ // Copy the reason values from the global object into the bootstrap scope.
+ for (let name in BOOTSTRAP_REASONS)
+ bootstrapScope[name] = BOOTSTRAP_REASONS[name];
+
+ return bootstrapScope;
+}
+exports.create = createBootstrapScope;
+
+function evaluateBootstrap(options) {
+ let { uri, scope } = options;
+
+ evaluate(scope,
+ `${"Components"}.classes['@mozilla.org/moz/jssubscript-loader;1']
+ .createInstance(${"Components"}.interfaces.mozIJSSubScriptLoader)
+ .loadSubScript("${uri}");`, "ECMAv5");
+}
+exports.evaluate = evaluateBootstrap;
diff --git a/addon-sdk/source/test/fixtures/border-style.css b/addon-sdk/source/test/fixtures/border-style.css
new file mode 100644
index 000000000..6941ccb20
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/border-style.css
@@ -0,0 +1,4 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+div { border-style: dashed; }
diff --git a/addon-sdk/source/test/fixtures/child-process-scripts.js b/addon-sdk/source/test/fixtures/child-process-scripts.js
new file mode 100644
index 000000000..d808e56cb
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/child-process-scripts.js
@@ -0,0 +1,81 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+
+const { platform, pathFor } = require('sdk/system');
+const { defer } = require('sdk/core/promise');
+const { emit } = require('sdk/event/core');
+const { join } = require('sdk/fs/path');
+const { writeFile, unlinkSync, existsSync } = require('sdk/io/fs');
+const PROFILE_DIR= pathFor('ProfD');
+const isWindows = platform.toLowerCase().indexOf('win') === 0;
+const isOSX = platform.toLowerCase().indexOf('darwin') === 0;
+
+var scripts = {
+ 'args.sh': 'echo $1 $2 $3 $4',
+ 'args.bat': 'echo %1 %2 %3 %4',
+ 'check-env.sh': 'echo $CHILD_PROCESS_ENV_TEST',
+ 'check-env.bat': 'echo %CHILD_PROCESS_ENV_TEST%',
+ 'check-pwd.sh': 'echo $PWD',
+ 'check-pwd.bat': 'cd',
+ 'large-err.sh': 'for n in `seq 0 $1` ; do echo "E" 1>&2; done',
+ 'large-err-mac.sh': 'for ((i=0; i<$1; i=i+1)); do echo "E" 1>&2; done',
+ 'large-err.bat': 'FOR /l %%i in (0,1,%1) DO echo "E" 1>&2',
+ 'large-out.sh': 'for n in `seq 0 $1` ; do echo "O"; done',
+ 'large-out-mac.sh': 'for ((i=0; i<$1; i=i+1)); do echo "O"; done',
+ 'large-out.bat': 'FOR /l %%i in (0,1,%1) DO echo "O"',
+ 'wait.sh': 'sleep 2',
+ // Use `ping` to an invalid IP address because `timeout` isn't
+ // on all environments? http://stackoverflow.com/a/1672349/1785755
+ 'wait.bat': 'ping 1.1.1.1 -n 1 -w 2000 > nul'
+};
+
+Object.keys(scripts).forEach(filename => {
+ if (/\.sh$/.test(filename))
+ scripts[filename] = '#!/bin/sh\n' + scripts[filename];
+ else if (/\.bat$/.test(filename))
+ scripts[filename] = '@echo off\n' + scripts[filename];
+});
+
+function getScript (name) {
+ // Use specific OSX script if exists
+ if (isOSX && scripts[name + '-mac.sh'])
+ name = name + '-mac';
+ let fileName = name + (isWindows ? '.bat' : '.sh');
+ return createFile(fileName, scripts[fileName]);
+}
+exports.getScript = getScript;
+
+function createFile (name, data) {
+ let { promise, resolve, reject } = defer();
+ let fileName = join(PROFILE_DIR, name);
+ writeFile(fileName, data, function (err) {
+ if (err) reject();
+ else {
+ makeExecutable(fileName);
+ resolve(fileName);
+ }
+ });
+ return promise;
+}
+
+// TODO Use fs.chmod once implemented, bug 914606
+function makeExecutable (name) {
+ let { CC } = require('chrome');
+ let nsILocalFile = CC('@mozilla.org/file/local;1', 'nsILocalFile', 'initWithPath');
+ let file = nsILocalFile(name);
+ file.permissions = 0o777;
+}
+
+function deleteFile (name) {
+ let file = join(PROFILE_DIR, name);
+ if (existsSync(file))
+ unlinkSync(file);
+}
+
+function cleanUp () {
+ Object.keys(scripts).forEach(deleteFile);
+}
+exports.cleanUp = cleanUp;
diff --git a/addon-sdk/source/test/fixtures/chrome-worker/addEventListener.js b/addon-sdk/source/test/fixtures/chrome-worker/addEventListener.js
new file mode 100644
index 000000000..baf28b452
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/chrome-worker/addEventListener.js
@@ -0,0 +1,6 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+postMessage('Hello');
diff --git a/addon-sdk/source/test/fixtures/chrome-worker/jsctypes.js b/addon-sdk/source/test/fixtures/chrome-worker/jsctypes.js
new file mode 100644
index 000000000..6345305ce
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/chrome-worker/jsctypes.js
@@ -0,0 +1,6 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+postMessage(typeof ctypes.open);
diff --git a/addon-sdk/source/test/fixtures/chrome-worker/onerror.js b/addon-sdk/source/test/fixtures/chrome-worker/onerror.js
new file mode 100644
index 000000000..b3e820b28
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/chrome-worker/onerror.js
@@ -0,0 +1,6 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+throw new Error('ok');
diff --git a/addon-sdk/source/test/fixtures/chrome-worker/onmessage.js b/addon-sdk/source/test/fixtures/chrome-worker/onmessage.js
new file mode 100644
index 000000000..8fd95fb6d
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/chrome-worker/onmessage.js
@@ -0,0 +1,8 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+onmessage = function (event) {
+ postMessage(event.data);
+};
diff --git a/addon-sdk/source/test/fixtures/chrome-worker/setTimeout.js b/addon-sdk/source/test/fixtures/chrome-worker/setTimeout.js
new file mode 100644
index 000000000..d3885243b
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/chrome-worker/setTimeout.js
@@ -0,0 +1,8 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+setTimeout(function () {
+ postMessage('ok');
+}, 0);
diff --git a/addon-sdk/source/test/fixtures/chrome-worker/xhr.js b/addon-sdk/source/test/fixtures/chrome-worker/xhr.js
new file mode 100644
index 000000000..b19906897
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/chrome-worker/xhr.js
@@ -0,0 +1,11 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+var xhr = new XMLHttpRequest();
+xhr.open("GET", "data:text/plain,ok", true);
+xhr.onload = function () {
+ postMessage(xhr.responseText);
+};
+xhr.send(null);
diff --git a/addon-sdk/source/test/fixtures/create_xpi.py b/addon-sdk/source/test/fixtures/create_xpi.py
new file mode 100644
index 000000000..ed0f983d9
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/create_xpi.py
@@ -0,0 +1,15 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+from os.path import abspath
+
+# TODO replace this script with a direct Python action invocation
+from mozbuild.action.zip import main as create_zip
+
+def main(output, input_dir):
+ output.close()
+
+ return create_zip(['-C', input_dir, abspath(output.name), '**'])
diff --git a/addon-sdk/source/test/fixtures/es5.js b/addon-sdk/source/test/fixtures/es5.js
new file mode 100644
index 000000000..746cae3c8
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/es5.js
@@ -0,0 +1,8 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+exports.frozen = Object.freeze({});
+exports.sealed = Object.seal({});
+exports.inextensible = Object.preventExtensions({});
diff --git a/addon-sdk/source/test/fixtures/include-file.css b/addon-sdk/source/test/fixtures/include-file.css
new file mode 100644
index 000000000..20d3e8294
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/include-file.css
@@ -0,0 +1,4 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+div { border: 10px solid black; }
diff --git a/addon-sdk/source/test/fixtures/index.html b/addon-sdk/source/test/fixtures/index.html
new file mode 100644
index 000000000..ba77c6a26
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/index.html
@@ -0,0 +1,18 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<html>
+ <head>
+ <meta charset="UTF-8">
+ <title>Add-on Page</title>
+ </head>
+ <body>
+ <p>This is an add-on page test!</p>
+ <script>
+ function getTestURL() {
+ return window.document.documentURI + "";
+ }
+ </script>
+ </body>
+</html>
diff --git a/addon-sdk/source/test/fixtures/jsm-package/Test.jsm b/addon-sdk/source/test/fixtures/jsm-package/Test.jsm
new file mode 100644
index 000000000..3e71b456a
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/jsm-package/Test.jsm
@@ -0,0 +1,11 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+this.EXPORTED_SYMBOLS = ["Test"];
+
+this.Test = {
+ square: function (x) { return x * x; }
+};
diff --git a/addon-sdk/source/test/fixtures/jsm-package/index.js b/addon-sdk/source/test/fixtures/jsm-package/index.js
new file mode 100644
index 000000000..e20721c76
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/jsm-package/index.js
@@ -0,0 +1,46 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+
+var { Test } = require('./Test.jsm');
+var { Test: Test2 } = require('./Test.jsm');
+exports.localJSM = Test.square(16) === 256;
+exports.localJSMCached = Test === Test2;
+
+(function () {
+var { Promise } = require('resource://gre/modules/Promise.jsm');
+var { defer } = require('resource://gre/modules/Promise.jsm').Promise;
+
+exports.isCachedAbsolute = Promise.defer === defer;
+
+exports.isLoadedAbsolute = function (val) {
+ let { promise, resolve } = Promise.defer();
+ resolve(val);
+ return promise;
+};
+})();
+
+(function () {
+var { Promise } = require('modules/Promise.jsm');
+var { defer } = require('modules/Promise.jsm').Promise;
+exports.isCachedPath = Promise.defer === defer;
+
+exports.isLoadedPath = function (val) {
+ let { promise, resolve } = Promise.defer();
+ resolve(val);
+ return promise;
+};
+})();
+
+(function () {
+var { defer } = require('resource://gre/modules/commonjs/sdk/core/promise.js');
+var { defer: defer2 } = require('resource://gre/modules/commonjs/sdk/core/promise.js');
+exports.isCachedJSAbsolute = defer === defer2;
+exports.isLoadedJSAbsolute = function (val) {
+ let { promise, resolve } = defer();
+ resolve(val);
+ return promise;
+};
+})();
diff --git a/addon-sdk/source/test/fixtures/jsm-package/package.json b/addon-sdk/source/test/fixtures/jsm-package/package.json
new file mode 100644
index 000000000..2e8caf211
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/jsm-package/package.json
@@ -0,0 +1,3 @@
+{
+ "name": "jsm-package"
+}
diff --git a/addon-sdk/source/test/fixtures/loader/cycles/a.js b/addon-sdk/source/test/fixtures/loader/cycles/a.js
new file mode 100644
index 000000000..f6e13ccb7
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/cycles/a.js
@@ -0,0 +1,7 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+
+exports.b = require('b');
diff --git a/addon-sdk/source/test/fixtures/loader/cycles/b.js b/addon-sdk/source/test/fixtures/loader/cycles/b.js
new file mode 100644
index 000000000..69e23f11a
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/cycles/b.js
@@ -0,0 +1,7 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+
+exports.a = require('a');
diff --git a/addon-sdk/source/test/fixtures/loader/cycles/c.js b/addon-sdk/source/test/fixtures/loader/cycles/c.js
new file mode 100644
index 000000000..ce34b3cf4
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/cycles/c.js
@@ -0,0 +1,7 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+
+exports.main = require('main');
diff --git a/addon-sdk/source/test/fixtures/loader/cycles/main.js b/addon-sdk/source/test/fixtures/loader/cycles/main.js
new file mode 100644
index 000000000..6d13200c8
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/cycles/main.js
@@ -0,0 +1,14 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+
+var a = require('a');
+var b = require('b');
+var c = require('c');
+
+exports.a = a;
+exports.b = b;
+exports.c = c;
+exports.main = exports;
diff --git a/addon-sdk/source/test/fixtures/loader/errors/boomer.js b/addon-sdk/source/test/fixtures/loader/errors/boomer.js
new file mode 100644
index 000000000..c794cda33
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/errors/boomer.js
@@ -0,0 +1,7 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+throw Error("opening input stream (invalid filename?)");
diff --git a/addon-sdk/source/test/fixtures/loader/errors/main.js b/addon-sdk/source/test/fixtures/loader/errors/main.js
new file mode 100644
index 000000000..1fdb56790
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/errors/main.js
@@ -0,0 +1,9 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+
+var error = require('./boomer');
+
+exports.main = exports;
diff --git a/addon-sdk/source/test/fixtures/loader/exceptions/boomer.js b/addon-sdk/source/test/fixtures/loader/exceptions/boomer.js
new file mode 100644
index 000000000..e26817984
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/exceptions/boomer.js
@@ -0,0 +1,9 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+
+exports.boom = function() {
+ throw Error("Boom!");
+};
diff --git a/addon-sdk/source/test/fixtures/loader/exceptions/main.js b/addon-sdk/source/test/fixtures/loader/exceptions/main.js
new file mode 100644
index 000000000..4b6279d7d
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/exceptions/main.js
@@ -0,0 +1,11 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+
+var boomer = require('./boomer');
+
+boomer.boom();
+
+exports.main = exports;
diff --git a/addon-sdk/source/test/fixtures/loader/globals/main.js b/addon-sdk/source/test/fixtures/loader/globals/main.js
new file mode 100644
index 000000000..6d34ddbd3
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/globals/main.js
@@ -0,0 +1,7 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+exports.console = console;
+exports.globalFoo = (typeof globalFoo !== "undefined") ? globalFoo : null;
diff --git a/addon-sdk/source/test/fixtures/loader/json/invalid.json b/addon-sdk/source/test/fixtures/loader/json/invalid.json
new file mode 100644
index 000000000..02ea4b0b4
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/json/invalid.json
@@ -0,0 +1,3 @@
+{
+ invalidjson
+}
diff --git a/addon-sdk/source/test/fixtures/loader/json/manifest.json b/addon-sdk/source/test/fixtures/loader/json/manifest.json
new file mode 100644
index 000000000..5ae25bb9d
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/json/manifest.json
@@ -0,0 +1,14 @@
+{
+ "name": "Jetpack Loader Test",
+ "version": "1.0.1",
+ "dependencies": {
+ "async": "*",
+ "underscore": "*"
+ },
+ "contributors": [
+ "ash nazg durbatulûk",
+ "ash nazg gimbatul",
+ "ash nazg thrakatulûk",
+ "agh burzum-ishi krimpatul"
+ ]
+}
diff --git a/addon-sdk/source/test/fixtures/loader/json/mutation.json b/addon-sdk/source/test/fixtures/loader/json/mutation.json
new file mode 100644
index 000000000..e1cbe3c15
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/json/mutation.json
@@ -0,0 +1 @@
+{ "value": 1 }
diff --git a/addon-sdk/source/test/fixtures/loader/json/nodotjson.json.js b/addon-sdk/source/test/fixtures/loader/json/nodotjson.json.js
new file mode 100644
index 000000000..af046caa6
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/json/nodotjson.json.js
@@ -0,0 +1,8 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+module.exports = {
+ "filename": "nodotjson.json.js",
+ "data": {}
+};
diff --git a/addon-sdk/source/test/fixtures/loader/json/test.json b/addon-sdk/source/test/fixtures/loader/json/test.json
new file mode 100644
index 000000000..d19dc7117
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/json/test.json
@@ -0,0 +1,3 @@
+{
+ "filename": "test.json"
+}
diff --git a/addon-sdk/source/test/fixtures/loader/json/test.json.js b/addon-sdk/source/test/fixtures/loader/json/test.json.js
new file mode 100644
index 000000000..b14364bff
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/json/test.json.js
@@ -0,0 +1,7 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+module.exports = {
+ "filename": "test.json.js"
+};
diff --git a/addon-sdk/source/test/fixtures/loader/lazy/main.js b/addon-sdk/source/test/fixtures/loader/lazy/main.js
new file mode 100644
index 000000000..04525f972
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/lazy/main.js
@@ -0,0 +1,9 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+exports.useFoo= function () {
+ return require('foo');
+}
diff --git a/addon-sdk/source/test/fixtures/loader/missing-twice/file.json b/addon-sdk/source/test/fixtures/loader/missing-twice/file.json
new file mode 100644
index 000000000..ebd6f791b
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/missing-twice/file.json
@@ -0,0 +1 @@
+an invalid json file
diff --git a/addon-sdk/source/test/fixtures/loader/missing-twice/main.js b/addon-sdk/source/test/fixtures/loader/missing-twice/main.js
new file mode 100644
index 000000000..20e453736
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/missing-twice/main.js
@@ -0,0 +1,32 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+
+try {
+ require('./not-found');
+}
+catch (e1) {
+ exports.firstError = e1;
+ // It should throw again and not be cached
+ try {
+ require('./not-found');
+ }
+ catch (e2) {
+ exports.secondError = e2;
+ }
+}
+
+try {
+ require('./file.json');
+}
+catch (e) {
+ exports.invalidJSON1 = e;
+ try {
+ require('./file.json');
+ }
+ catch (e) {
+ exports.invalidJSON2 = e;
+ }
+}
diff --git a/addon-sdk/source/test/fixtures/loader/missing/main.js b/addon-sdk/source/test/fixtures/loader/missing/main.js
new file mode 100644
index 000000000..dde9ee77c
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/missing/main.js
@@ -0,0 +1,10 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+
+var a = require('./not-found');
+
+exports.a = a;
+exports.main = exports;
diff --git a/addon-sdk/source/test/fixtures/loader/self/main.js b/addon-sdk/source/test/fixtures/loader/self/main.js
new file mode 100644
index 000000000..ab2b159da
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/self/main.js
@@ -0,0 +1,8 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+var self = require("sdk/self");
+exports.self = self;
diff --git a/addon-sdk/source/test/fixtures/loader/syntax-error/error.js b/addon-sdk/source/test/fixtures/loader/syntax-error/error.js
new file mode 100644
index 000000000..56c7c524f
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/syntax-error/error.js
@@ -0,0 +1,11 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+
+module.metadata = {
+ "status": "experimental"
+};
+
+exports.b = @ require('b');
diff --git a/addon-sdk/source/test/fixtures/loader/syntax-error/main.js b/addon-sdk/source/test/fixtures/loader/syntax-error/main.js
new file mode 100644
index 000000000..4eadef870
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/syntax-error/main.js
@@ -0,0 +1,10 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+
+var a = require('./error');
+
+exports.a = a;
+exports.main = exports;
diff --git a/addon-sdk/source/test/fixtures/loader/unsupported/fennec.js b/addon-sdk/source/test/fixtures/loader/unsupported/fennec.js
new file mode 100644
index 000000000..9edc63c14
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/unsupported/fennec.js
@@ -0,0 +1,10 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+module.metadata = {
+ "engines": {
+ "Fennec": "*"
+ }
+};
+module.exports = {};
diff --git a/addon-sdk/source/test/fixtures/loader/unsupported/firefox.js b/addon-sdk/source/test/fixtures/loader/unsupported/firefox.js
new file mode 100644
index 000000000..5a08280a4
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/loader/unsupported/firefox.js
@@ -0,0 +1,10 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+module.metadata = {
+ "engines": {
+ "Firefox": "*"
+ }
+};
+module.exports = {};
diff --git a/addon-sdk/source/test/fixtures/mofo_logo.SVG b/addon-sdk/source/test/fixtures/mofo_logo.SVG
new file mode 100644
index 000000000..2edeb9e6f
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/mofo_logo.SVG
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Generator: Adobe Illustrator 12.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 51448) -->
+<svg version="1.1" id="svg2997" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200" viewBox="-0.5 -0.5 200 200" overflow="visible" enable-background="new -0.5 -0.5 200 200" xml:space="preserve">
+<path id="path85" fill="#FFFFFF" d="M-2559.936,6359.65c19.127,26.489,46.371,47.744,80.988,44.707 C-2479.339,6371.648-2523.898,6352.641-2559.936,6359.65z"/>
+<path id="path93" fill="#FFFFFF" d="M-2634.014,6073.506c4.082-7.232,2.571-17.81,3.831-26.684 c-9.019,4.635-15.506,14.72-24.551,18.641C-2652.134,6070.818-2640.447,6070.829-2634.014,6073.506z"/>
+<path id="path95" fill="#FFFFFF" d="M-2687.179,6018.109l-18.654,18.212c4.158,3.301,9.676,8.132,17.11,6.36L-2687.179,6018.109z"/>
+<path id="path97" fill="#FFFFFF" d="M-2731.117,5998.391c-4.356-0.363-14.41,20.156-18.785,19.857l14.762,8.728 C-2730.055,6027.281-2732.816,6001.961-2731.117,5998.391L-2731.117,5998.391z"/>
+<path id="path99" fill="#FFFFFF" d="M-2776.505,5989.09l4.736-11.668c-10.245,0.813-15.511,20.68-21.719,26.244 C-2776.37,6007.109-2775.727,6011.81-2776.505,5989.09z"/>
+<path id="path101" fill="#FFFFFF" d="M-2825.16,5962.186l-18.405,31.539l17.091,3.962L-2825.16,5962.186L-2825.16,5962.186z"/>
+<path id="path103" fill="#FFFFFF" d="M-2872.173,5967.636l-21.059,16.139C-2872.035,5992.07-2870.821,5995.181-2872.173,5967.636z"/>
+<path id="path105" fill="#FFFFFF" d="M-2922.987,5980.398c-3.084-9.172-0.18-20.226,0.396-29.914 c-4.952,9.422-16.338,16.166-17.745,27.299C-2935.489,5981.802-2928.796,5980.021-2922.987,5980.398L-2922.987,5980.398z"/>
+<g>
+ <path id="path9" d="M70.165,21.946C62.09,27.292,49.432,38.35,43.382,44.795c1.491,0.208,3.068-0.957,4.751-1.179l11.635-3.192 c-4.833,4.151-15.001,11.47-17.946,17.234c3.632-1.309,11.254-5.699,15.043-6.059c-3.309,3.038-10.214,8.801-12.378,12.469 c0.261,0.124,0.08,0.445,0.12,0.669c2.846-1.635,9.763-5.91,12.9-6.199c-3.229,3.484-9.169,10.193-10.47,14.688 c2.804-1.913,9.856-5.792,12.944-6.969c-0.703,1.773-2.262,3.09-3.276,4.864c-1.399,2.453-4.711,5.246-4.834,8.161 c2.46-2.375,5.51-4.474,8.571-6.148c-1.365,2.947-3.876,5.384-4.561,8.892c2.007-2.02,5.813-3.071,8.399-4.005l-0.312,0.311 c-0.224,0.191-3.136,2.335-5.121,4.727c1.765-0.114,0.449-0.334,2.707,0.18l-0.043,0.114 c-0.154,13.494,25.501,18.427,41.224,17.743c3.314-6.419,1.525-16.789,10.088-21.247c5.764-3.067,6.416-4.742,13.068-3.491 c3.596,0.674,11.461,3.392,15.227,2.534c3.514-2.033,5.221-2.928,8.332-5.37c3.01-4.574-23.988-19.398-2.164-0.171 c5.236,1.121,6.588,0.954,8.139-1.376c0.693-1.88,2.348-6.877,3.859-10.133l0.107-1.76c-1.467-4.414-1.111-11.965-1.852-13.053 c-4.842-6.713-22.34-7.764-30.256-16.596c1.35-4.384-1.482-7.997-4.367-10.738c-4.332-3.187-10.313-1.722-14.925-0.399 c-5.255-1.099-10.117-2.986-15.233-4.48C81.381,13.626,70.165,21.946,70.165,21.946L70.165,21.946z"/>
+ <path id="path11" fill="#FF1A00" d="M103.517,25.68l-3.771,1.438c-0.453-0.901,0.676-1.757,1.126-2.599 c0.322-0.616,1.247-0.961,1.465-1.705c-4.877-0.786-9.486-3.178-14.688-2.354c-5.563,0.878-8.802,3.003-12.005,5.54 c-1.617,0.885-2.396,2.142-3.968,3.201c-1.92,1.729-2.755,5.176-3.233,5.833c2.785-1.206,5.587-1.026,7.479-1.663 c-7.115,4.447-8.358,13.134-6.39,20.38l-0.116,0.159c-2.391-1.429-3.083-4.065-3.745-6.371c-0.471,1.515-0.538,3.44-0.859,5.132 c-0.764-0.713-1.184-1.734-1.61-2.644c-0.799,7.668,2.938,13.913,7.521,19.966c3.068,4.058-2.923,6.744-1.512,11.838l7.206-0.404 c0.929-0.348,0.64-0.223,0.64-0.223c-1.806,3.613-11.365,6.352-13.201,8.417c0.513,0.425,8.917-2.164,11.991-2.687l-9.045,5.112 c0.899,0.277-0.028,3.311,1.946,3.023c12.508,3.713,26.597,8.866,28.792,3.488c1.831-3.419,4.541-9.21,4.122-12.49l1.941-2.949 c-2.59,0.643-5.199,1.691-7.202,3.823c-0.599-1.256,0.342-2.763,0.81-3.951c1.343-2.41,4.026-3.686,6.151-5.056l-4.464-1.976 c-2.25-3.864-6.523-7.047-6.639-11.586l1.057,0.36c4.069,4.621,9.854,8.371,15.647,8.639c-3.943-0.338,16.471-2.743,15.271-4.472 c2.17,3.121,8.729,4.432,13.52,6.071c2.201-0.792,3.162-0.566,4.816-2.057c-0.705-0.765-1.795-1.39-2.844-1.787 c-6.295-1.738-21.162-4.796-24.365-10.986c-0.604,2.637,1.838,4.812,4.566,7.061l-5.873-0.826 c-10.331-4.782-9.083-12.984-6.813-20.78l0.785,6.508l2.025,3.548c3.851-6.966,1.835-6.1-5.898-15.494l5.832,2.228 c2.663,2.142,7.04,5.845,10.921,8.417c10.453,6.845,15.625,11.381,28.891,12.311l4.092,0.908c2.896-7.59-2.719-14.597-1.021-3.989 c-2.166-2.631-4.283-5.066-3.842-6.439c-0.76-1.147,3.143-4.014,2.811-5.319c-3.83-3.894-20.389-5.984-29.381-14.907 c-0.264,1.492,4.158,5.354,4.924,6.683c-8.918,0.088-14.174-2.651-15.127-5.542c-0.195-0.481,0.047-2.623,0.998-3.313 c1.287-0.672,2.541-1.693,4.02-1.83c-2.316-1.891-5.25,0.643-7.302,1.788c-1.594,2.737,0.889,5.788,1.202,8.014 c-2.175-0.549-5.111-4.378-5.471-6.782c-3.306,0.362-6.34-0.065-8.881-1.974c2.243-0.492,4.56-1.331,6.906-1.755 c6.143-1.34,12.598-7.655,18.803-2.72l-2.889-3.672C113.848,21.654,108.812,24.342,103.517,25.68L103.517,25.68z"/>
+ <path id="path13" d="M83.254,28.459c-1.649,1.924-3.302,3.85-3.687,6.437l0.369,0.434c1.102-1.326,2.753-2.437,4.274-3.207 c0.002,2.726,1.76,5.399,4.11,6.713l0.395,0.028c-0.498-2.879-0.166-6.399,0.96-8.998c1.793-1.508,4.151-2.106,5.668-3.692 c-3.797-0.888-7.411-0.136-10.658,1.1L83.254,28.459L83.254,28.459z"/>
+ <path id="path15" fill="#FFFFFF" d="M121.379,36.471c-1.561-2.157-3.777-3.888-6.594-3.642 C114.814,35.491,118.443,37.043,121.379,36.471z"/>
+ <path id="path17" d="M98.854,36.815c-2.445,0.187-4.736,0.622-7.041,1.224c1.017,0.937,2.302,1.252,3.266,2.129 c-3.017,1.137-6.176,1.861-8.813,4.186l0.546,0.325c4.878-1.015,10.645-0.929,15.732,0.16l0.294-0.212l-2.948-5.361l4.533-0.981 c-2.405-2.249-5.156-4.46-8.322-5.422C96.585,34.171,97.917,35.479,98.854,36.815L98.854,36.815L98.854,36.815z"/>
+ <path id="path19" d="M116.564,42.189c2.406,0.445,5.064,0.503,7.357,0.074c-3.787-1.932-8.625-1.552-11.639-4.944 C112.852,39.099,114.086,41.968,116.564,42.189z"/>
+ <path id="path21" d="M80.567,55.828c0.943,3.023-2.23,6.707,0.312,9.485c2.165,2.522,4.81,4.614,7.354,6.464l-0.225-0.943 c-2.266-2.647-4.349-5.569-5.396-8.716c2.656,0.928,5.463,2.216,8.496,2.646c-3.416-5.104-9.678-9.984-7.319-16.97 c0.129-1.154,1.655-2.041,1.271-3.113C81.49,47.408,79.996,51.435,80.567,55.828L80.567,55.828z"/>
+ <path id="path23" fill="#FFFFFF" d="M127.412,59.77c-0.334,0.589-0.213,1.45-0.316,2.174c0.738-0.379,1.264-1.199,2-1.518 C128.883,59.988,127.934,59.986,127.412,59.77z"/>
+ <path id="path25" fill="#FFFFFF" d="M131.738,64.284l1.518-1.487c-0.336-0.267-0.785-0.661-1.395-0.518L131.738,64.284z"/>
+ <path id="path27" fill="#FFFFFF" d="M135.316,65.887c0.355,0.026,1.174-1.643,1.531-1.618l-1.203-0.711 C135.229,63.532,135.455,65.593,135.316,65.887z"/>
+ <path id="path29" fill="#FFFFFF" d="M139.012,66.644l-0.385,0.95c0.836-0.067,1.264-1.684,1.766-2.138 C139.002,65.177,138.949,64.793,139.012,66.644z"/>
+ <path id="path31" fill="#FFFFFF" d="M142.977,68.834l1.498-2.569l-1.395-0.32L142.977,68.834z"/>
+ <path id="path33" fill="#FFFFFF" d="M146.801,68.39l1.715-1.312C146.789,66.4,146.691,66.147,146.801,68.39z"/>
+ <path id="path35" fill="#FFFFFF" d="M150.938,67.35c0.254,0.748,0.02,1.647-0.031,2.436c0.404-0.766,1.332-1.313,1.445-2.221 C151.957,67.233,151.412,67.382,150.938,67.35L150.938,67.35z"/>
+ <path id="path37" d="M99.399,96.021c0.547,1.031,0.572,2.285,1.119,3.315c1.295-2.793,1.147-6.148-0.533-8.659 c-2.923-1.288-5.745-2.625-8.264-4.671C92.768,89.577,96.192,93.513,99.399,96.021L99.399,96.021z"/>
+ <path id="path71" d="M117.387,36.775c-0.953-0.248-1.51-1.317-1.25-2.393c0.268-1.079,1.252-1.75,2.207-1.502 c0.955,0.245,1.512,1.318,1.25,2.393C119.328,36.348,118.34,37.021,117.387,36.775z"/>
+ <path id="path75" d="M98.356,103.161l2.835-2.31c-2.758-1.791-7.426-5.004-9.95-7.387C93.224,97.589,93.284,102.152,98.356,103.161 z"/>
+ <path id="path2225" d="M57.859,84.722c11.552,15.175,27.262,21.19,46.412,19.542l-1.629-3.542 c-17.031,0.631-31.21-3.592-40.875-17.588L57.859,84.722z"/>
+ <path id="path2343" d="M10.063,170.061c2.903,0.027,5.809-0.039,8.712,0.014l-0.375,2.293l-5.096,0.02l-0.021,3.24h4.088v2.342 h-4.088v6.334h-3.22V170.061z"/>
+ <path id="path3315" d="M30.648,184.227c-2.242-0.338-3.854-1.586-4.75-3.68c-0.511-1.191-0.656-2.105-0.596-3.748 c0.061-1.664,0.254-2.451,0.886-3.605c0.814-1.488,1.881-2.344,3.555-2.846c0.877-0.266,1.065-0.291,2.274-0.293 c1.095-0.004,1.446,0.029,2.078,0.201c2.303,0.627,3.802,2.172,4.426,4.559c0.288,1.105,0.265,3.764-0.042,4.846 c-0.697,2.463-2.385,4.098-4.674,4.527C33.093,184.322,31.412,184.342,30.648,184.227z M32.991,181.963 c0.926-0.281,1.579-1.137,1.876-2.463c0.205-0.91,0.238-3.596,0.058-4.547c-0.335-1.762-1.328-2.654-2.957-2.656 c-1.589-0.002-2.456,0.793-2.912,2.67c-0.214,0.883-0.238,3.438-0.041,4.396c0.279,1.357,0.877,2.211,1.761,2.512 C31.381,182.082,32.455,182.123,32.991,181.963z"/>
+ <path id="flowRoot5258" d="M47.563,170.053h3.446v8.381c0,1.154,0.179,1.98,0.537,2.48c0.364,0.492,0.955,0.883,1.772,0.883 c0.823,0,1.414-0.391,1.772-0.883c0.364-0.5,0.546-1.326,0.546-2.48v-8.381h3.445v8.381c0,1.979-0.474,3.451-1.423,4.418 c-0.948,0.967-2.396,1.451-4.34,1.451c-1.939,0-3.383-0.484-4.331-1.451c-0.949-0.967-1.423-2.439-1.423-4.418V170.053"/>
+ <path id="path6262" d="M68.606,170.053l3.509,0.051c1.381,2.793,2.899,5.531,4.078,8.41l0.197,0.504l-0.049-1.01l-0.18-7.951h2.948 v14.246h-3.182l-0.363-0.758c-1.15-2.398-3.13-6.629-3.602-7.697c-0.302-0.684-0.524-1.137-0.495-1.012 c0.029,0.127,0.083,2.309,0.119,4.848l0.065,4.619h-3.047V170.053z"/>
+ <path id="text8203" d="M92.398,182.191l0.931,0.002c0.986,0,1.843-1.076,2.366-1.844c0.522-0.766,0.977-1.873,0.977-3.322 c0-1.424-0.431-2.496-0.905-3.213c-0.469-0.725-1.17-1.424-2.104-1.424l-1.265-0.002v9.805 M88.878,184.303v-14.25h4.209 c1.173,0,2.12,0.104,2.84,0.311c0.72,0.201,1.357,0.529,1.912,0.986c0.8,0.65,1.393,1.455,1.776,2.41 c0.39,0.949,0.584,2.084,0.584,3.404c0,1.223-0.2,2.328-0.6,3.314c-0.4,0.986-0.981,1.813-1.744,2.482 c-0.544,0.475-1.155,0.818-1.832,1.031c-0.678,0.207-1.558,0.311-2.641,0.311H88.878"/>
+ <path id="path9182" d="M106.307,184.277l5.092-14.225h3.457c1.617,4.746,3.281,9.477,4.885,14.225l-3.457,0.023l-1.084-3.391 l-4.433,0.002l-1.083,3.389C108.559,184.289,107.431,184.324,106.307,184.277z M113.051,173.643l-1.51,4.887h2.873 C113.82,176.934,113.57,175.254,113.051,173.643z"/>
+ <path id="path10154" d="M127.924,172.535h-3.867v-2.48c3.799,0,7.6-0.004,11.4,0c-0.002,0-0.529,2.479-0.529,2.479l-3.748,0.002 v11.768h-3.256V172.535z"/>
+ <path id="path11126" d="M142.326,170.053h3.471v14.242h-3.471V170.053z"/>
+ <path id="path14040" d="M159.949,184.227c-2.24-0.338-3.854-1.586-4.748-3.68c-0.512-1.191-0.656-2.105-0.598-3.748 c0.061-1.664,0.254-2.451,0.885-3.605c0.816-1.488,1.883-2.344,3.557-2.846c0.877-0.266,1.064-0.291,2.275-0.293 c1.094-0.004,1.445,0.029,2.076,0.201c2.305,0.627,3.803,2.172,4.426,4.559c0.289,1.105,0.266,3.764-0.041,4.846 c-0.697,2.463-2.387,4.098-4.676,4.527C162.395,184.322,160.713,184.342,159.949,184.227z M162.293,181.963 c0.926-0.281,1.578-1.137,1.875-2.463c0.205-0.91,0.24-3.596,0.059-4.547c-0.336-1.762-1.328-2.654-2.957-2.656 c-1.59-0.002-2.457,0.793-2.912,2.67c-0.215,0.883-0.238,3.438-0.041,4.396c0.279,1.357,0.877,2.211,1.762,2.512 C160.682,182.082,161.756,182.123,162.293,181.963z"/>
+ <path id="path2224" d="M176.656,170.053l3.563,0.051c1.402,2.793,2.945,5.531,4.143,8.41l0.199,0.504l-0.049-1.01l-0.182-7.951 h2.992v14.246h-3.23l-0.369-0.758c-1.168-2.398-3.178-6.629-3.658-7.697c-0.305-0.684-0.531-1.137-0.502-1.012 c0.031,0.127,0.086,2.309,0.121,4.848l0.066,4.619h-3.094V170.053z"/>
+ <g id="g3173" transform="translate(-0.4495808,1251.722)">
+ <path id="path2320" d="M185.373-1089.163c-1.527-0.85-2.496-1.666-3.189-2.689l-0.402-0.596l-0.586,0.516 c-0.816,0.725-1.141,0.965-1.887,1.406c-1.727,1.02-4.043,1.439-6.92,1.252c-5.955-0.385-9.082-3.238-9.49-8.66 c-0.182-2.412,0.223-4.607,1.158-6.289c1.281-2.305,3.789-3.914,7.313-4.695c1.803-0.398,3.266-0.545,5.977-0.594l2.516-0.049 l-0.049-1.311c-0.027-0.723-0.09-1.543-0.137-1.824c-0.25-1.477-0.871-2.223-2.035-2.445c-0.902-0.17-1.32-0.18-2.357-0.053 c-2.006,0.246-4.098,1.066-6.678,2.623c-0.691,0.416-1.297,0.744-1.346,0.725c-0.096-0.037-3.463-5.682-3.461-5.799 c0.008-0.166,2.094-1.316,3.582-1.973c4.023-1.775,6.543-2.377,9.953-2.377c3.17,0,5.359,0.533,7.127,1.736 c0.693,0.473,1.736,1.551,2.115,2.186c0.35,0.586,0.777,1.646,1.012,2.51l0.189,0.691l-0.041,4.766 c-0.021,2.619-0.041,6.43-0.041,8.467c0,4.178,0,4.18,0.633,5.414c0.273,0.537,0.498,0.814,1.266,1.57 c0.855,0.842,0.922,0.93,0.809,1.059c-0.068,0.076-1.029,1.184-2.137,2.461c-1.109,1.275-2.057,2.328-2.107,2.338 C186.108-1088.786,185.756-1088.953,185.373-1089.163z M175.903-1095.453c1.229-0.154,2.246-0.641,3.182-1.518l0.508-0.477 l0.055-3.168l0.053-3.168l-1.316,0.057c-4.154,0.178-5.707,0.842-6.377,2.729c-0.213,0.596-0.295,2.16-0.146,2.787 c0.334,1.418,1.457,2.533,2.768,2.748c0.246,0.039,0.486,0.078,0.531,0.082C175.203-1095.374,175.539-1095.408,175.903-1095.453z M137.623-1089.718c-1.318-0.205-2.459-0.68-3.502-1.459c-1.109-0.826-1.762-1.803-2.238-3.348 c-0.445-1.441-0.436-1.025-0.49-18.475c-0.051-15.838-0.057-16.158-0.236-18.186c-0.102-1.135-0.162-2.086-0.137-2.111 c0.074-0.074,8.055-1.891,8.154-1.857c0.098,0.037,0.248,1.135,0.361,2.654c0.162,10.801,0.012,21.605,0.156,32.406 c0.063,2.619,0.086,2.936,0.24,3.373c0.379,1.078,0.902,1.469,1.963,1.465c0.336,0,0.662-0.018,0.727-0.039 c0.105-0.033,0.479,1.133,1.371,4.289l0.162,0.578l-1.271,0.334C141.034-1089.607,139.186-1089.476,137.623-1089.718z M154.555-1089.644c-1.066-0.125-1.982-0.391-2.85-0.822c-2.164-1.08-3.219-2.633-3.713-5.475 c-0.066-0.375-0.109-5.193-0.146-16.57c-0.053-15.98-0.055-16.049-0.25-18.383c-0.117-1.43-0.164-2.367-0.119-2.408 c0.102-0.088,8.1-1.91,8.152-1.857c0.088,0.088,0.268,1.711,0.348,3.148c0.213,11.125-0.035,22.258,0.162,33.383 c0.055,1.18,0.107,1.611,0.238,1.984c0.221,0.625,0.525,1.014,0.945,1.199c0.457,0.203,1.182,0.281,1.516,0.166 c0.174-0.063,0.283-0.066,0.316-0.014c0.078,0.129,1.402,4.816,1.369,4.85c-0.016,0.018-0.504,0.164-1.084,0.328 C158.041-1089.722,155.75-1089.501,154.555-1089.644z M67.676-1089.716c-0.134-0.018-0.61-0.076-1.059-0.127 c-0.447-0.053-1.237-0.215-1.756-0.363c-5.568-1.582-9.046-6.182-9.853-13.021c-0.124-1.047-0.123-3.988,0.001-5.09 c0.362-3.213,1.4-6.119,2.997-8.387c1.083-1.537,2.861-3.086,4.462-3.889c2.021-1.016,3.78-1.402,6.368-1.402 c2.15,0,3.536,0.219,5.156,0.816c3.931,1.449,7.106,5.004,8.254,9.238c0.922,3.398,0.905,8.645-0.037,12.051 c-0.744,2.691-2.024,4.861-3.966,6.725s-4.086,2.918-6.7,3.297C70.775-1089.757,68.143-1089.654,67.676-1089.716z M70.77-1096.027 c1.815-0.824,2.693-2.672,3.095-6.512c0.153-1.465,0.177-5.111,0.041-6.512c-0.375-3.879-1.335-5.748-3.356-6.531 c-1.055-0.41-2.505-0.303-3.577,0.262c-1.823,0.959-2.647,2.99-2.926,7.207c-0.158,2.404-0.013,5.633,0.343,7.572 c0.475,2.602,1.225,3.77,2.859,4.457c0.768,0.322,1.166,0.387,2.144,0.344C70.09-1095.769,70.293-1095.812,70.77-1096.027z M10.314-1116.745c-0.13-1.063-0.376-2.029-0.667-2.621c-0.147-0.301-0.224-0.547-0.182-0.584c0.09-0.078,7.021-1.965,7.22-1.965 c0.204,0,0.671,0.98,0.915,1.92c0.112,0.432,0.204,0.855,0.204,0.939c0,0.086,0.027,0.152,0.061,0.152 c0.034-0.002,0.391-0.277,0.794-0.615c1.52-1.27,3.532-2.127,5.465-2.324c2.115-0.217,4.02,0.1,5.551,0.92 c0.98,0.527,2.146,1.512,2.768,2.336l0.488,0.646l0.314-0.326c0.76-0.789,2.256-1.92,3.307-2.496 c0.898-0.494,2.17-0.893,3.413-1.074c1.114-0.16,3.312-0.063,4.384,0.197c1.185,0.287,2.204,0.719,2.971,1.26 c1.574,1.109,2.172,2.082,2.584,4.207c0.172,0.885,0.174,1.025,0.203,13.373l0.029,12.479H42.15v-10.934 c0-7.029-0.03-11.18-0.084-11.623c-0.198-1.623-0.574-2.096-1.798-2.268c-1.429-0.199-3.438,0.574-5.267,2.025l-0.667,0.529 l0,22.27h-7.729c-0.473-7.383,0.652-15.438-0.186-22.727c-0.296-1.432-0.807-1.955-2.059-2.107 c-1.462-0.178-3.452,0.498-5.153,1.75l-0.664,0.488l-0.005,22.596h-7.979C10.282-1099.062,11.051-1108.048,10.314-1116.745z M86.536-1095.492l14.459-19.949l-13.248-0.043v-5.779h23.043v5.578c-4.503,6.535-9.129,12.986-13.598,19.545l14.179,0.037 l-0.059,0.182l-1.888,5.559l-22.899,0.041L86.536-1095.492z M116.735-1120.853l0.184-0.041c2.588-0.43,5.186-0.809,7.775-1.227 l0.266-0.045v31.844h-8.225V-1120.853z M120.276-1124.939c-1.963-0.195-3.682-1.678-4.188-3.611 c-0.703-2.688,0.707-5.361,3.273-6.201c0.484-0.158,0.754-0.191,1.592-0.191c1.578,0,2.482,0.357,3.508,1.381 c0.986,0.986,1.412,2.068,1.418,3.586c0.004,1.563-0.406,2.584-1.457,3.637C123.332-1125.245,121.934-1124.773,120.276-1124.939z"/>
+ </g>
+</g>
+</svg>
diff --git a/addon-sdk/source/test/fixtures/moz.build b/addon-sdk/source/test/fixtures/moz.build
new file mode 100644
index 000000000..2c3635186
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/moz.build
@@ -0,0 +1,22 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+fixtures = [
+ 'native-addon-test',
+ 'native-overrides-test',
+]
+
+output_dir = OBJDIR_FILES._tests.testing.mochitest['jetpack-package']['addon-sdk'].source.test.fixtures
+
+for fixture in fixtures:
+ xpi = '%s.xpi' % fixture
+
+ GENERATED_FILES += [xpi]
+ f = GENERATED_FILES[xpi]
+ f.script = 'create_xpi.py'
+ f.inputs = [fixture]
+
+ output_dir += ['!%s' % xpi]
diff --git a/addon-sdk/source/test/fixtures/moz_favicon.ico b/addon-sdk/source/test/fixtures/moz_favicon.ico
new file mode 100644
index 000000000..d44438903
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/moz_favicon.ico
Binary files differ
diff --git a/addon-sdk/source/test/fixtures/native-addon-test/dir/a.js b/addon-sdk/source/test/fixtures/native-addon-test/dir/a.js
new file mode 100644
index 000000000..2f8cccb68
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-addon-test/dir/a.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+module.exports = 'dir/a';
diff --git a/addon-sdk/source/test/fixtures/native-addon-test/dir/a/index.js b/addon-sdk/source/test/fixtures/native-addon-test/dir/a/index.js
new file mode 100644
index 000000000..a86eaa184
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-addon-test/dir/a/index.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+module.exports = 'dir/a/index';
diff --git a/addon-sdk/source/test/fixtures/native-addon-test/dir/b.js b/addon-sdk/source/test/fixtures/native-addon-test/dir/b.js
new file mode 100644
index 000000000..cd3e975b2
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-addon-test/dir/b.js
@@ -0,0 +1,6 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+module.exports = require('test-math');
+
diff --git a/addon-sdk/source/test/fixtures/native-addon-test/dir/c.js b/addon-sdk/source/test/fixtures/native-addon-test/dir/c.js
new file mode 100644
index 000000000..ed5fd149e
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-addon-test/dir/c.js
@@ -0,0 +1,6 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+module.exports = require('../package.json');
+
diff --git a/addon-sdk/source/test/fixtures/native-addon-test/dir/dummy.js b/addon-sdk/source/test/fixtures/native-addon-test/dir/dummy.js
new file mode 100644
index 000000000..e1640884c
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-addon-test/dir/dummy.js
@@ -0,0 +1,6 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+module.exports = 'this is a dummy module';
+
diff --git a/addon-sdk/source/test/fixtures/native-addon-test/dir/test.jsm b/addon-sdk/source/test/fixtures/native-addon-test/dir/test.jsm
new file mode 100644
index 000000000..1e2946f94
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-addon-test/dir/test.jsm
@@ -0,0 +1,6 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+this['EXPORTED_SYMBOLS'] = ['test'];
+test = 'this is a jsm';
diff --git a/addon-sdk/source/test/fixtures/native-addon-test/expectedmap.json b/addon-sdk/source/test/fixtures/native-addon-test/expectedmap.json
new file mode 100644
index 000000000..4fa5886f7
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-addon-test/expectedmap.json
@@ -0,0 +1,25 @@
+{
+ "./index.js": {
+ "./dir/a": "./dir/a.js",
+ "./dir/b": "./dir/b.js",
+ "./dir/c": "./dir/c.js",
+ "./dir/dummy": "./dir/dummy.js",
+ "./dir/test.jsm": "./dir/test.jsm",
+ "./utils": "./utils/index.js",
+ "./newmodule": "./newmodule/lib/file.js",
+ "test-math": "./node_modules/test-math/index.js",
+ "test-custom-main": "./node_modules/test-custom-main/lib/custom-entry.js",
+ "test-custom-main-relative": "./node_modules/test-custom-main-relative/lib/custom-entry.js",
+ "test-default-main": "./node_modules/test-default-main/index.js"
+ },
+ "./dir/b.js": {
+ "test-math": "./node_modules/test-math/index.js"
+ },
+ "./dir/c.js": {
+ "../package.json": "./package.json"
+ },
+ "./node_modules/test-math/index.js": {
+ "test-add": "./node_modules/test-math/node_modules/test-add/index.js",
+ "test-subtract": "./node_modules/test-math/node_modules/test-subtract/index.js"
+ }
+}
diff --git a/addon-sdk/source/test/fixtures/native-addon-test/index.js b/addon-sdk/source/test/fixtures/native-addon-test/index.js
new file mode 100644
index 000000000..68720d401
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-addon-test/index.js
@@ -0,0 +1,37 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// Added noise to test AST walker
+for (var i = 0; i < 5; i++) {
+ square(i);
+}
+
+exports.directoryDefaults = require('./utils');
+exports.directoryMain = require('./newmodule');
+exports.resolvesJSoverDir= require('./dir/a');
+exports.math = require('test-math');
+exports.mathInRelative = require('./dir/b');
+exports.customMainModule = require('test-custom-main');
+exports.customMainModuleRelative = require('test-custom-main-relative');
+exports.defaultMain = require('test-default-main');
+exports.testJSON = require('./dir/c');
+exports.dummyModule = require('./dir/dummy');
+
+exports.eventCore = require('sdk/event/core');
+exports.promise = require('sdk/core/promise');
+
+exports.localJSM = require('./dir/test.jsm');
+exports.promisejsm = require('modules/Promise.jsm').Promise;
+exports.require = require;
+
+var math = require('test-math');
+exports.areModulesCached = (math === exports.math);
+
+// Added noise to test AST walker
+function square (x) {
+ let tmp = x;
+ tmp *= x;
+ return tmp;
+}
+
diff --git a/addon-sdk/source/test/fixtures/native-addon-test/newmodule/index.js b/addon-sdk/source/test/fixtures/native-addon-test/newmodule/index.js
new file mode 100644
index 000000000..f982e6b98
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-addon-test/newmodule/index.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+module.exports = 'index.js';
diff --git a/addon-sdk/source/test/fixtures/native-addon-test/newmodule/lib/file.js b/addon-sdk/source/test/fixtures/native-addon-test/newmodule/lib/file.js
new file mode 100644
index 000000000..fab140983
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-addon-test/newmodule/lib/file.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+module.exports = 'main from new module';
diff --git a/addon-sdk/source/test/fixtures/native-addon-test/newmodule/package.json b/addon-sdk/source/test/fixtures/native-addon-test/newmodule/package.json
new file mode 100644
index 000000000..feae8acaa
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-addon-test/newmodule/package.json
@@ -0,0 +1,3 @@
+{
+ "main":"lib/file.js"
+}
diff --git a/addon-sdk/source/test/fixtures/native-addon-test/package.json b/addon-sdk/source/test/fixtures/native-addon-test/package.json
new file mode 100644
index 000000000..f689b83f6
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-addon-test/package.json
@@ -0,0 +1,10 @@
+{
+ "main":"index.js",
+ "dependencies": {
+ "test-math": "*",
+ "test-custom-main": "*",
+ "test-custom-main-relative": "*",
+ "test-default-main": "*",
+ "test-assets": "*"
+ }
+}
diff --git a/addon-sdk/source/test/fixtures/native-addon-test/utils/index.js b/addon-sdk/source/test/fixtures/native-addon-test/utils/index.js
new file mode 100644
index 000000000..d34af25d5
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-addon-test/utils/index.js
@@ -0,0 +1,7 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+module.exports = 'utils';
+
diff --git a/addon-sdk/source/test/fixtures/native-overrides-test/ignore.js b/addon-sdk/source/test/fixtures/native-overrides-test/ignore.js
new file mode 100644
index 000000000..02539eb79
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-overrides-test/ignore.js
@@ -0,0 +1,6 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+exports.bar = "do not ignore this export";
diff --git a/addon-sdk/source/test/fixtures/native-overrides-test/index.js b/addon-sdk/source/test/fixtures/native-overrides-test/index.js
new file mode 100644
index 000000000..ef0b6bba1
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-overrides-test/index.js
@@ -0,0 +1,19 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+var foo = require("foo");
+var coolTabs = require("cool-tabs");
+
+exports.foo = foo.fs;
+exports.bar = foo.bar;
+exports.fs = require("sdk/io/fs");
+exports.extra = require("fs-extra").extra;
+exports.overload = require("overload");
+exports.overloadLib = require("overload/lib/foo.js");
+exports.internal = require("internal").internal;
+exports.Tabs = require("sdk/tabs").Tabs;
+exports.CoolTabs = coolTabs.Tabs;
+exports.CoolTabsLib = coolTabs.TabsLib;
+exports.ignore = require("./lib/ignore").foo;
diff --git a/addon-sdk/source/test/fixtures/native-overrides-test/lib/ignore.js b/addon-sdk/source/test/fixtures/native-overrides-test/lib/ignore.js
new file mode 100644
index 000000000..d37a7827a
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-overrides-test/lib/ignore.js
@@ -0,0 +1,6 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+exports.foo = require("../ignore").bar;
diff --git a/addon-sdk/source/test/fixtures/native-overrides-test/lib/internal.js b/addon-sdk/source/test/fixtures/native-overrides-test/lib/internal.js
new file mode 100644
index 000000000..2d5f419fd
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-overrides-test/lib/internal.js
@@ -0,0 +1,6 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+exports.internal = "test";
diff --git a/addon-sdk/source/test/fixtures/native-overrides-test/lib/tabs.js b/addon-sdk/source/test/fixtures/native-overrides-test/lib/tabs.js
new file mode 100644
index 000000000..20f59551e
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-overrides-test/lib/tabs.js
@@ -0,0 +1,6 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+exports.Tabs = "no tabs exist";
diff --git a/addon-sdk/source/test/fixtures/native-overrides-test/package.json b/addon-sdk/source/test/fixtures/native-overrides-test/package.json
new file mode 100644
index 000000000..346cf76f5
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/native-overrides-test/package.json
@@ -0,0 +1,18 @@
+{
+ "name": "native-overrides-test",
+ "main": "index.js",
+ "dependencies": {
+ "cool-tabs": "*",
+ "foo": "*",
+ "fs-extra": "*"
+ },
+ "jetpack": {
+ "overrides": {
+ "fs": "sdk/io/fs",
+ "overload": "foo",
+ "internal": "./lib/internal",
+ "sdk/tabs": "./lib/tabs",
+ "../ignore": "foo"
+ }
+ }
+}
diff --git a/addon-sdk/source/test/fixtures/preferences/curly-id/package.json b/addon-sdk/source/test/fixtures/preferences/curly-id/package.json
new file mode 100644
index 000000000..1d47b4d81
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/preferences/curly-id/package.json
@@ -0,0 +1,14 @@
+{
+ "id": "{34a1eae1-c20a-464f-9b0e-000000000000}",
+ "fullName": "curly ID test",
+ "author": "Tomislav Jovanovic",
+
+ "preferences": [{
+ "name": "test13",
+ "type": "integer",
+ "title": "test13",
+ "value": 26
+ }],
+
+ "preferences-branch": "invalid^branch*name"
+}
diff --git a/addon-sdk/source/test/fixtures/preferences/no-prefs/package.json b/addon-sdk/source/test/fixtures/preferences/no-prefs/package.json
new file mode 100644
index 000000000..7b243b41d
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/preferences/no-prefs/package.json
@@ -0,0 +1,6 @@
+{
+ "id": "no-prefs@jetpack",
+ "title": "No Prefs Test",
+ "author": "Erik Vold",
+ "loader": "lib/main.js"
+}
diff --git a/addon-sdk/source/test/fixtures/preferences/preferences-branch/package.json b/addon-sdk/source/test/fixtures/preferences/preferences-branch/package.json
new file mode 100644
index 000000000..b738db7d0
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/preferences/preferences-branch/package.json
@@ -0,0 +1,14 @@
+{
+ "id": "test-preferences-branch",
+ "fullName": "preferences-branch test",
+ "author": "Tomislav Jovanovic",
+
+ "preferences": [{
+ "name": "test42",
+ "type": "bool",
+ "title": "test42",
+ "value": true
+ }],
+
+ "preferences-branch": "human-readable"
+}
diff --git a/addon-sdk/source/test/fixtures/preferences/simple-prefs/package.json b/addon-sdk/source/test/fixtures/preferences/simple-prefs/package.json
new file mode 100644
index 000000000..d3b077ddf
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/preferences/simple-prefs/package.json
@@ -0,0 +1,75 @@
+{
+ "id": "simple-prefs@jetpack",
+ "title": "Simple Prefs Test",
+ "author": "Erik Vold",
+ "preferences": [{
+ "name": "test",
+ "type": "bool",
+ "title": "tëst",
+ "value": false,
+ "description": "descrïptiön"
+ },
+ {
+ "name": "test2",
+ "type": "string",
+ "title": "tëst",
+ "value": "ünicødé"
+ },
+ {
+ "name": "test3",
+ "type": "menulist",
+ "title": "\"><test",
+ "value": "1",
+ "options": [
+ {
+ "value": "0",
+ "label": "label1"
+ },
+ {
+ "value": "1",
+ "label": "label2"
+ }
+ ]
+ },
+ {
+ "name": "test4",
+ "type": "radio",
+ "title": "tëst",
+ "value": "red",
+ "options": [
+ {
+ "value": "red",
+ "label": "rouge"
+ },
+ {
+ "value": "blue",
+ "label": "bleu"
+ }
+ ]
+ },
+ {
+ "name": "test5",
+ "type": "boolint",
+ "title": "part part, particle",
+ "value": 7
+ },
+ {
+ "name": "test6",
+ "type": "color",
+ "title": "pop pop, popscicle",
+ "value": "#ff5e99"
+ },
+ {
+ "name": "test7",
+ "type": "file",
+ "title": "bike bike",
+ "value": "bicycle"
+ },
+ {
+ "name": "test8",
+ "type": "directory",
+ "title": "test test",
+ "value": "1-2-3"
+ }],
+ "loader": "lib/main.js"
+}
diff --git a/addon-sdk/source/test/fixtures/sandbox-complex-character.js b/addon-sdk/source/test/fixtures/sandbox-complex-character.js
new file mode 100644
index 000000000..6ae6769cf
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/sandbox-complex-character.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+var chars = 'გამარჯობა';
diff --git a/addon-sdk/source/test/fixtures/sandbox-normal.js b/addon-sdk/source/test/fixtures/sandbox-normal.js
new file mode 100644
index 000000000..54252985d
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/sandbox-normal.js
@@ -0,0 +1,7 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+var a = 1;
+this.b = 2;
+function f() { return 4; }
diff --git a/addon-sdk/source/test/fixtures/test-addon-extras-window.html b/addon-sdk/source/test/fixtures/test-addon-extras-window.html
new file mode 100644
index 000000000..76ce98cd3
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/test-addon-extras-window.html
@@ -0,0 +1,21 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<html>
+<head>
+ <meta charset="UTF-8">
+ <title>Worker test</title>
+</head>
+<body>
+ <p id="paragraph">Lorem ipsum dolor sit amet.</p>
+ <script>
+ if ("addon" in window) {
+ var count = 1;
+ addon.port.on("get-result", () => {
+ addon.port.emit("result" + count++, extras.test().getTestURL())
+ });
+ }
+ </script>
+</body>
+</html>
diff --git a/addon-sdk/source/test/fixtures/test-addon-extras.html b/addon-sdk/source/test/fixtures/test-addon-extras.html
new file mode 100644
index 000000000..9864495e8
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/test-addon-extras.html
@@ -0,0 +1,31 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<html>
+<head>
+ <meta charset="UTF-8">
+ <title>Worker test</title>
+</head>
+<body>
+ <p id="paragraph">Lorem ipsum dolor sit amet.</p>
+ <script>
+ if ("addon" in window) {
+ var count = 1;
+ addon.port.on("get-result", () => {
+ console.log("get-result recieved");
+ addon.port.emit("result" + count++, extras && extras.test())
+ });
+ }
+
+ window.addEventListener("message", function getMessage({ data }) {
+ if (data.name == "start") {
+ window.postMessage({
+ name: "extras",
+ result: window.extras === undefined
+ }, '*');
+ }
+ }, false);
+ </script>
+</body>
+</html>
diff --git a/addon-sdk/source/test/fixtures/test-contentScriptFile.js b/addon-sdk/source/test/fixtures/test-contentScriptFile.js
new file mode 100644
index 000000000..7dc0e3f24
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/test-contentScriptFile.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+self.postMessage("msg from contentScriptFile");
diff --git a/addon-sdk/source/test/fixtures/test-context-menu.js b/addon-sdk/source/test/fixtures/test-context-menu.js
new file mode 100644
index 000000000..1a15609b7
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/test-context-menu.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+self.on("context", () => true);
diff --git a/addon-sdk/source/test/fixtures/test-iframe-postmessage.html b/addon-sdk/source/test/fixtures/test-iframe-postmessage.html
new file mode 100644
index 000000000..b33d597dd
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/test-iframe-postmessage.html
@@ -0,0 +1,23 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+<!DOCTYPE HTML>
+<html>
+ <head>
+ <meta charset="UTF-8">
+ <script>
+ window.addEventListener("message", function(msg) {
+ parent.postMessage(msg.data, "*");
+ });
+
+ parent.postMessage({
+ first: "a string",
+ second: ["an", "array"],
+ third: {an: 'object'}
+ }, '*');
+ </script>
+ </head>
+ <body>
+ <h1>Inner iframe</h1>
+ </body>
+</html>
diff --git a/addon-sdk/source/test/fixtures/test-iframe.html b/addon-sdk/source/test/fixtures/test-iframe.html
new file mode 100644
index 000000000..56cd50c1a
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/test-iframe.html
@@ -0,0 +1,12 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+<!DOCTYPE HTML>
+<html>
+<head>
+ <meta charset="UTF-8">
+</head>
+<body>
+ <iframe id="inner" src="about:blank"></iframe>
+</body>
+</html>
diff --git a/addon-sdk/source/test/fixtures/test-iframe.js b/addon-sdk/source/test/fixtures/test-iframe.js
new file mode 100644
index 000000000..bbfadc4ff
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/test-iframe.js
@@ -0,0 +1,16 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+var count = 0;
+
+setTimeout(function() {
+ window.addEventListener("message", function(msg) {
+ if (++count > 1) {
+ self.postMessage(msg.data);
+ }
+ else msg.source.postMessage(msg.data, '*');
+ });
+
+ document.getElementById('inner').src = iframePath;
+}, 0);
diff --git a/addon-sdk/source/test/fixtures/test-message-manager.js b/addon-sdk/source/test/fixtures/test-message-manager.js
new file mode 100644
index 000000000..d647bd8fd
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/test-message-manager.js
@@ -0,0 +1,6 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const TEST_VALUE = 11;
+
diff --git a/addon-sdk/source/test/fixtures/test-net-url.txt b/addon-sdk/source/test/fixtures/test-net-url.txt
new file mode 100644
index 000000000..9f8166e61
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/test-net-url.txt
@@ -0,0 +1 @@
+Hello, ゼロ! \ No newline at end of file
diff --git a/addon-sdk/source/test/fixtures/test-page-mod.html b/addon-sdk/source/test/fixtures/test-page-mod.html
new file mode 100644
index 000000000..901abefc4
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/test-page-mod.html
@@ -0,0 +1,12 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+<html>
+<head>
+ <meta charset="UTF-8">
+ <title>Page Mod test</title>
+</head>
+<body>
+ <p id="paragraph">Lorem ipsum dolor sit amet.</p>
+</body>
+</html>
diff --git a/addon-sdk/source/test/fixtures/test-sidebar-addon-global.html b/addon-sdk/source/test/fixtures/test-sidebar-addon-global.html
new file mode 100644
index 000000000..4552e3d78
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/test-sidebar-addon-global.html
@@ -0,0 +1,15 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+<script>
+if ("addon" in window) {
+ addon.port.on('X', function(msg) {
+ // last message
+ addon.port.emit('X', msg + '3');
+ });
+
+ // start messaging chain
+ addon.port.emit('Y', '1');
+}
+</script>
+SIDEBAR TEST
diff --git a/addon-sdk/source/test/fixtures/test-trusted-document.html b/addon-sdk/source/test/fixtures/test-trusted-document.html
new file mode 100644
index 000000000..c31e055cb
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/test-trusted-document.html
@@ -0,0 +1,20 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<html>
+<head>
+ <meta charset="UTF-8">
+ <title>Worker test</title>
+</head>
+<body>
+ <p id="paragraph">Lorem ipsum dolor sit amet.</p>
+ <script>
+ if ("addon" in window) {
+ addon.port.on('addon-to-document', function (arg) {
+ addon.port.emit('document-to-addon', arg);
+ });
+ }
+ </script>
+</body>
+</html>
diff --git a/addon-sdk/source/test/fixtures/test.html b/addon-sdk/source/test/fixtures/test.html
new file mode 100644
index 000000000..70b5e31e5
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/test.html
@@ -0,0 +1,25 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<html>
+ <head>
+ <meta charset="UTF-8">
+ <title>foo</title>
+ </head>
+ <body>
+ <p>bar</p>
+ <script>
+ function getTestURL() {
+ return window.document.documentURI + "";
+ }
+ function changesInWindow() {
+ window.document.documentElement.setAttribute('test', 'changes in window');
+ return null;
+ }
+ function getAUQLUE() {
+ return "AUQLUE" in window;
+ }
+ </script>
+ </body>
+</html>
diff --git a/addon-sdk/source/test/fixtures/testLocalXhr.json b/addon-sdk/source/test/fixtures/testLocalXhr.json
new file mode 100644
index 000000000..0967ef424
--- /dev/null
+++ b/addon-sdk/source/test/fixtures/testLocalXhr.json
@@ -0,0 +1 @@
+{}
diff --git a/addon-sdk/source/test/framescript-manager/frame-script.js b/addon-sdk/source/test/framescript-manager/frame-script.js
new file mode 100644
index 000000000..de9bb8385
--- /dev/null
+++ b/addon-sdk/source/test/framescript-manager/frame-script.js
@@ -0,0 +1,13 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const {onPing} = require("./pong");
+
+exports.onPing = onPing;
+
+exports.onInit = (context) => {
+ context.sendAsyncMessage("framescript-manager/ready", {state: "ready"});
+ context.addMessageListener("framescript-manager/ping", exports.onPing);
+};
diff --git a/addon-sdk/source/test/framescript-manager/pong.js b/addon-sdk/source/test/framescript-manager/pong.js
new file mode 100644
index 000000000..b7fb7e077
--- /dev/null
+++ b/addon-sdk/source/test/framescript-manager/pong.js
@@ -0,0 +1,7 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+exports.onPing = message =>
+ message.target.sendAsyncMessage("framescript-manager/pong", message.data);
diff --git a/addon-sdk/source/test/framescript-util/frame-script.js b/addon-sdk/source/test/framescript-util/frame-script.js
new file mode 100644
index 000000000..2a4a2284c
--- /dev/null
+++ b/addon-sdk/source/test/framescript-util/frame-script.js
@@ -0,0 +1,21 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { windowToMessageManager, nodeToMessageManager } = require("framescript/util");
+
+
+const onInit = (context) => {
+ context.addMessageListener("framescript-util/window/request", _ => {
+ windowToMessageManager(context.content.window).sendAsyncMessage(
+ "framescript-util/window/response", {window: true});
+ });
+
+ context.addMessageListener("framescript-util/node/request", message => {
+ const node = context.content.document.querySelector(message.data);
+ nodeToMessageManager(node).sendAsyncMessage(
+ "framescript-util/node/response", {node: true});
+ });
+};
+exports.onInit = onInit;
diff --git a/addon-sdk/source/test/jetpack-package.ini b/addon-sdk/source/test/jetpack-package.ini
new file mode 100644
index 000000000..fa93b9c2f
--- /dev/null
+++ b/addon-sdk/source/test/jetpack-package.ini
@@ -0,0 +1,179 @@
+[DEFAULT]
+support-files =
+ buffers/**
+ commonjs-test-adapter/**
+ context-menu/**
+ event/**
+ fixtures.js
+ fixtures/**
+ fixtures/native-addon-test.xpi
+ fixtures/native-overrides-test.xpi
+ framescript-manager/**
+ framescript-util/**
+ lib/**
+ loader/**
+ modules/**
+ page-mod/**
+ path/**
+ private-browsing/**
+ querystring/**
+ sidebar/**
+ tabs/**
+ test-context-menu.html
+ traits/**
+ util.js
+ windows/**
+ zip/**
+generated-files =
+ fixtures/native-addon-test.xpi
+ fixtures/native-overrides-test.xpi
+
+[test-addon-bootstrap.js]
+[test-addon-extras.js]
+[test-addon-installer.js]
+[test-addon-window.js]
+[test-api-utils.js]
+[test-array.js]
+[test-base64.js]
+[test-bootstrap.js]
+[test-browser-events.js]
+[test-buffer.js]
+[test-byte-streams.js]
+[test-child_process.js]
+[test-chrome.js]
+[test-clipboard.js]
+subsuite = clipboard
+[test-collection.js]
+[test-commonjs-test-adapter.js]
+[test-content-events.js]
+[test-content-script.js]
+[test-content-sync-worker.js]
+[test-content-worker.js]
+[test-context-menu.js]
+[test-context-menu@2.js]
+[test-cuddlefish.js]
+# Cuddlefish loader is unsupported
+skip-if = true
+[test-deprecate.js]
+[test-dev-panel.js]
+[test-diffpatcher.js]
+[test-dispatcher.js]
+[test-disposable.js]
+[test-dom.js]
+[test-environment.js]
+[test-event-core.js]
+[test-event-dom.js]
+[test-event-target.js]
+[test-event-utils.js]
+[test-file.js]
+[test-frame-utils.js]
+[test-framescript-manager.js]
+[test-framescript-util.js]
+[test-fs.js]
+[test-functional.js]
+[test-globals.js]
+[test-heritage.js]
+[test-hidden-frame.js]
+[test-host-events.js]
+[test-hotkeys.js]
+[test-httpd.js]
+[test-indexed-db.js]
+[test-jetpack-id.js]
+[test-keyboard-observer.js]
+[test-keyboard-utils.js]
+[test-l10n-locale.js]
+[test-l10n-plural-rules.js]
+[test-lang-type.js]
+[test-libxul.js]
+[test-list.js]
+[test-loader.js]
+[test-match-pattern.js]
+[test-method.js]
+[test-module.js]
+[test-modules.js]
+[test-mozilla-toolkit-versioning.js]
+[test-mpl2-license-header.js]
+skip-if = true
+[test-namespace.js]
+[test-native-loader.js]
+[test-native-options.js]
+[test-net-url.js]
+[test-node-os.js]
+[test-notifications.js]
+[test-object.js]
+[test-observers.js]
+[test-page-mod-debug.js]
+[test-page-mod.js]
+[test-page-worker.js]
+[test-panel.js]
+[test-passwords-utils.js]
+[test-passwords.js]
+[test-path.js]
+[test-plain-text-console.js]
+[test-preferences-service.js]
+[test-preferences-target.js]
+[test-private-browsing.js]
+[test-promise.js]
+[test-querystring.js]
+[test-reference.js]
+[test-request.js]
+[test-require.js]
+[test-rules.js]
+[test-sandbox.js]
+[test-selection.js]
+[test-self.js]
+[test-sequence.js]
+[test-set-exports.js]
+[test-shared-require.js]
+[test-simple-prefs.js]
+[test-simple-storage.js]
+[test-system-events.js]
+[test-system-input-output.js]
+[test-system-runtime.js]
+[test-system-startup.js]
+[test-system.js]
+[test-tab-events.js]
+[test-tab-observer.js]
+[test-tab-utils.js]
+[test-tab.js]
+[test-tabs-common.js]
+[test-tabs.js]
+[test-test-addon-file.js]
+[test-test-assert.js]
+[test-test-loader.js]
+[test-test-memory.js]
+[test-test-utils-async.js]
+[test-test-utils-generator.js]
+[test-test-utils-sync.js]
+[test-test-utils.js]
+[test-text-streams.js]
+[test-timer.js]
+[test-traceback.js]
+[test-ui-action-button.js]
+skip-if = debug || asan # Bug 1208727
+[test-ui-frame.js]
+[test-ui-id.js]
+[test-ui-sidebar-private-browsing.js]
+[test-ui-sidebar.js]
+[test-ui-toggle-button.js]
+[test-ui-toolbar.js]
+[test-unit-test-finder.js]
+[test-unit-test.js]
+[test-unload.js]
+[test-unsupported-skip.js]
+# Bug 1037235
+skip-if = true
+[test-uri-resource.js]
+[test-url.js]
+[test-uuid.js]
+[test-weak-set.js]
+[test-window-events.js]
+[test-window-observer.js]
+[test-window-utils-private-browsing.js]
+[test-window-utils.js]
+[test-window-utils2.js]
+[test-windows-common.js]
+[test-windows.js]
+[test-xhr.js]
+[test-xpcom.js]
+[test-xul-app.js]
diff --git a/addon-sdk/source/test/leak/jetpack-package.ini b/addon-sdk/source/test/leak/jetpack-package.ini
new file mode 100644
index 000000000..0632bdc87
--- /dev/null
+++ b/addon-sdk/source/test/leak/jetpack-package.ini
@@ -0,0 +1,7 @@
+[DEFAULT]
+support-files =
+ leak-utils.js
+
+[test-leak-window-events.js]
+[test-leak-event-dom-closed-window.js]
+[test-leak-tab-events.js]
diff --git a/addon-sdk/source/test/leak/leak-utils.js b/addon-sdk/source/test/leak/leak-utils.js
new file mode 100644
index 000000000..e01255ec8
--- /dev/null
+++ b/addon-sdk/source/test/leak/leak-utils.js
@@ -0,0 +1,80 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Cu, Ci } = require("chrome");
+const { Services } = Cu.import("resource://gre/modules/Services.jsm", {});
+const { SelfSupportBackend } = Cu.import("resource:///modules/SelfSupportBackend.jsm", {});
+const Startup = Cu.import("resource://gre/modules/sdk/system/Startup.js", {}).exports;
+
+// Adapted from the SpecialPowers.exactGC() code. We don't have a
+// window to operate on so we cannot use the exact same logic. We
+// use 6 GC iterations here as that is what is needed to clean up
+// the windows we have tested with.
+function gc() {
+ return new Promise(resolve => {
+ Cu.forceGC();
+ Cu.forceCC();
+ let count = 0;
+ function genGCCallback() {
+ Cu.forceCC();
+ return function() {
+ if (++count < 5) {
+ Cu.schedulePreciseGC(genGCCallback());
+ } else {
+ resolve();
+ }
+ }
+ }
+
+ Cu.schedulePreciseGC(genGCCallback());
+ });
+}
+
+// Execute the given test function and verify that we did not leak windows
+// in the process. The test function must return a promise or be a generator.
+// If the promise is resolved, or generator completes, with an sdk loader
+// object then it will be unloaded after the memory measurements.
+exports.asyncWindowLeakTest = function*(assert, asyncTestFunc) {
+
+ // SelfSupportBackend periodically tries to open windows. This can
+ // mess up our window leak detection below, so turn it off.
+ SelfSupportBackend.uninit();
+
+ // Wait for the browser to finish loading.
+ yield Startup.onceInitialized;
+
+ // Track windows that are opened in an array of weak references.
+ let weakWindows = [];
+ function windowObserver(subject, topic) {
+ let supportsWeak = subject.QueryInterface(Ci.nsISupportsWeakReference);
+ if (supportsWeak) {
+ weakWindows.push(Cu.getWeakReference(supportsWeak));
+ }
+ }
+ Services.obs.addObserver(windowObserver, "domwindowopened", false);
+
+ // Execute the body of the test.
+ let testLoader = yield asyncTestFunc(assert);
+
+ // Stop tracking new windows and attempt to GC any resources allocated
+ // by the test body.
+ Services.obs.removeObserver(windowObserver, "domwindowopened", false);
+ yield gc();
+
+ // Check to see if any of the windows we saw survived the GC. We consider
+ // these leaks.
+ assert.ok(weakWindows.length > 0, "should see at least one new window");
+ for (let i = 0; i < weakWindows.length; ++i) {
+ assert.equal(weakWindows[i].get(), null, "window " + i + " should be GC'd");
+ }
+
+ // Finally, unload the test body's loader if it provided one. We do this
+ // after our leak detection to avoid free'ing things on unload. Users
+ // don't tend to unload their addons very often, so we want to find leaks
+ // that happen while addons are in use.
+ if (testLoader) {
+ testLoader.unload();
+ }
+}
diff --git a/addon-sdk/source/test/leak/test-leak-event-dom-closed-window.js b/addon-sdk/source/test/leak/test-leak-event-dom-closed-window.js
new file mode 100644
index 000000000..c398462ab
--- /dev/null
+++ b/addon-sdk/source/test/leak/test-leak-event-dom-closed-window.js
@@ -0,0 +1,29 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { asyncWindowLeakTest } = require("./leak-utils");
+const { Loader } = require('sdk/test/loader');
+const openWindow = require("sdk/window/utils").open;
+
+exports["test sdk/event/dom does not leak when attached to closed window"] = function*(assert) {
+ yield asyncWindowLeakTest(assert, _ => {
+ return new Promise(resolve => {
+ let loader = Loader(module);
+ let { open } = loader.require('sdk/event/dom');
+ let w = openWindow();
+ w.addEventListener("DOMWindowClose", function windowClosed(evt) {
+ w.removeEventListener("DOMWindowClose", windowClosed);
+ // The sdk/event/dom module tries to clean itself up when DOMWindowClose
+ // is fired. Verify that it doesn't leak if its attached to an
+ // already closed window either. (See bug 1268898.)
+ open(w.document, "TestEvent1");
+ resolve(loader);
+ });
+ w.close();
+ });
+ });
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/leak/test-leak-tab-events.js b/addon-sdk/source/test/leak/test-leak-tab-events.js
new file mode 100644
index 000000000..4266c04fc
--- /dev/null
+++ b/addon-sdk/source/test/leak/test-leak-tab-events.js
@@ -0,0 +1,46 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { asyncWindowLeakTest } = require("./leak-utils");
+const { Loader } = require('sdk/test/loader');
+const openWindow = require("sdk/window/utils").open;
+
+exports["test sdk/tab/events does not leak new window"] = function*(assert) {
+ yield asyncWindowLeakTest(assert, _ => {
+ return new Promise(resolve => {
+ let loader = Loader(module);
+ let { events } = loader.require('sdk/tab/events');
+ let w = openWindow();
+ w.addEventListener("load", function windowLoaded(evt) {
+ w.removeEventListener("load", windowLoaded);
+ w.addEventListener("DOMWindowClose", function windowClosed(evt) {
+ w.removeEventListener("DOMWindowClose", windowClosed);
+ resolve(loader);
+ });
+ w.close();
+ });
+ });
+ });
+}
+
+exports["test sdk/tab/events does not leak when attached to existing window"] = function*(assert) {
+ yield asyncWindowLeakTest(assert, _ => {
+ return new Promise(resolve => {
+ let loader = Loader(module);
+ let w = openWindow();
+ w.addEventListener("load", function windowLoaded(evt) {
+ w.removeEventListener("load", windowLoaded);
+ let { events } = loader.require('sdk/tab/events');
+ w.addEventListener("DOMWindowClose", function windowClosed(evt) {
+ w.removeEventListener("DOMWindowClose", windowClosed);
+ resolve(loader);
+ });
+ w.close();
+ });
+ });
+ });
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/leak/test-leak-window-events.js b/addon-sdk/source/test/leak/test-leak-window-events.js
new file mode 100644
index 000000000..ceb20f475
--- /dev/null
+++ b/addon-sdk/source/test/leak/test-leak-window-events.js
@@ -0,0 +1,65 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+// Opening new windows in Fennec causes issues
+module.metadata = {
+ engines: {
+ 'Firefox': '*'
+ }
+};
+
+const { asyncWindowLeakTest } = require("./leak-utils.js");
+const { Loader } = require("sdk/test/loader");
+const { open } = require("sdk/window/utils");
+
+exports["test window/events for leaks"] = function*(assert) {
+ yield asyncWindowLeakTest(assert, _ => {
+ return new Promise((resolve, reject) => {
+ let loader = Loader(module);
+ let { events } = loader.require("sdk/window/events");
+ let { on, off } = loader.require("sdk/event/core");
+
+ on(events, "data", function handler(e) {
+ try {
+ if (e.type === "load") {
+ e.target.close();
+ }
+ else if (e.type === "close") {
+ off(events, "data", handler);
+
+ // Let asyncWindowLeakTest call loader.unload() after the
+ // leak check.
+ resolve(loader);
+ }
+ } catch (e) {
+ reject(e);
+ }
+ });
+
+ // Open a window. This will trigger our data events.
+ open();
+ });
+ });
+};
+
+exports["test window/events for leaks with existing window"] = function*(assert) {
+ yield asyncWindowLeakTest(assert, _ => {
+ return new Promise((resolve, reject) => {
+ let loader = Loader(module);
+ let w = open();
+ w.addEventListener("load", function windowLoaded(evt) {
+ w.removeEventListener("load", windowLoaded);
+ let { events } = loader.require("sdk/window/events");
+ w.addEventListener("DOMWindowClose", function windowClosed(evt) {
+ w.removeEventListener("DOMWindowClose", windowClosed);
+ resolve(loader);
+ });
+ w.close();
+ });
+ });
+ });
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/lib/httpd.js b/addon-sdk/source/test/lib/httpd.js
new file mode 100644
index 000000000..e46ca96a0
--- /dev/null
+++ b/addon-sdk/source/test/lib/httpd.js
@@ -0,0 +1,5212 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/*
+* An implementation of an HTTP server both as a loadable script and as an XPCOM
+* component. See the accompanying README file for user documentation on
+* httpd.js.
+*/
+
+module.metadata = {
+ "stability": "experimental"
+};
+
+const { components, CC, Cc, Ci, Cr, Cu } = require("chrome");
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+
+const PR_UINT32_MAX = Math.pow(2, 32) - 1;
+
+/** True if debugging output is enabled, false otherwise. */
+var DEBUG = false; // non-const *only* so tweakable in server tests
+
+/** True if debugging output should be timestamped. */
+var DEBUG_TIMESTAMP = false; // non-const so tweakable in server tests
+
+var gGlobalObject = Cc["@mozilla.org/systemprincipal;1"].createInstance();
+
+/**
+* Asserts that the given condition holds. If it doesn't, the given message is
+* dumped, a stack trace is printed, and an exception is thrown to attempt to
+* stop execution (which unfortunately must rely upon the exception not being
+* accidentally swallowed by the code that uses it).
+*/
+function NS_ASSERT(cond, msg)
+{
+ if (DEBUG && !cond)
+ {
+ dumpn("###!!!");
+ dumpn("###!!! ASSERTION" + (msg ? ": " + msg : "!"));
+ dumpn("###!!! Stack follows:");
+
+ var stack = new Error().stack.split(/\n/);
+ dumpn(stack.map(function(val) { return "###!!! " + val; }).join("\n"));
+
+ throw Cr.NS_ERROR_ABORT;
+ }
+}
+
+/** Constructs an HTTP error object. */
+function HttpError(code, description)
+{
+ this.code = code;
+ this.description = description;
+}
+HttpError.prototype =
+{
+ toString: function()
+ {
+ return this.code + " " + this.description;
+ }
+};
+
+/**
+* Errors thrown to trigger specific HTTP server responses.
+*/
+const HTTP_400 = new HttpError(400, "Bad Request");
+const HTTP_401 = new HttpError(401, "Unauthorized");
+const HTTP_402 = new HttpError(402, "Payment Required");
+const HTTP_403 = new HttpError(403, "Forbidden");
+const HTTP_404 = new HttpError(404, "Not Found");
+const HTTP_405 = new HttpError(405, "Method Not Allowed");
+const HTTP_406 = new HttpError(406, "Not Acceptable");
+const HTTP_407 = new HttpError(407, "Proxy Authentication Required");
+const HTTP_408 = new HttpError(408, "Request Timeout");
+const HTTP_409 = new HttpError(409, "Conflict");
+const HTTP_410 = new HttpError(410, "Gone");
+const HTTP_411 = new HttpError(411, "Length Required");
+const HTTP_412 = new HttpError(412, "Precondition Failed");
+const HTTP_413 = new HttpError(413, "Request Entity Too Large");
+const HTTP_414 = new HttpError(414, "Request-URI Too Long");
+const HTTP_415 = new HttpError(415, "Unsupported Media Type");
+const HTTP_417 = new HttpError(417, "Expectation Failed");
+
+const HTTP_500 = new HttpError(500, "Internal Server Error");
+const HTTP_501 = new HttpError(501, "Not Implemented");
+const HTTP_502 = new HttpError(502, "Bad Gateway");
+const HTTP_503 = new HttpError(503, "Service Unavailable");
+const HTTP_504 = new HttpError(504, "Gateway Timeout");
+const HTTP_505 = new HttpError(505, "HTTP Version Not Supported");
+
+/** Creates a hash with fields corresponding to the values in arr. */
+function array2obj(arr)
+{
+ var obj = {};
+ for (var i = 0; i < arr.length; i++)
+ obj[arr[i]] = arr[i];
+ return obj;
+}
+
+/** Returns an array of the integers x through y, inclusive. */
+function range(x, y)
+{
+ var arr = [];
+ for (var i = x; i <= y; i++)
+ arr.push(i);
+ return arr;
+}
+
+/** An object (hash) whose fields are the numbers of all HTTP error codes. */
+const HTTP_ERROR_CODES = array2obj(range(400, 417).concat(range(500, 505)));
+
+
+/**
+* The character used to distinguish hidden files from non-hidden files, a la
+* the leading dot in Apache. Since that mechanism also hides files from
+* easy display in LXR, ls output, etc. however, we choose instead to use a
+* suffix character. If a requested file ends with it, we append another
+* when getting the file on the server. If it doesn't, we just look up that
+* file. Therefore, any file whose name ends with exactly one of the character
+* is "hidden" and available for use by the server.
+*/
+const HIDDEN_CHAR = "^";
+
+/**
+* The file name suffix indicating the file containing overridden headers for
+* a requested file.
+*/
+const HEADERS_SUFFIX = HIDDEN_CHAR + "headers" + HIDDEN_CHAR;
+
+/** Type used to denote SJS scripts for CGI-like functionality. */
+const SJS_TYPE = "sjs";
+
+/** Base for relative timestamps produced by dumpn(). */
+var firstStamp = 0;
+
+/** dump(str) with a trailing "\n" -- only outputs if DEBUG. */
+function dumpn(str)
+{
+ if (DEBUG)
+ {
+ var prefix = "HTTPD-INFO | ";
+ if (DEBUG_TIMESTAMP)
+ {
+ if (firstStamp === 0)
+ firstStamp = Date.now();
+
+ var elapsed = Date.now() - firstStamp; // milliseconds
+ var min = Math.floor(elapsed / 60000);
+ var sec = (elapsed % 60000) / 1000;
+
+ if (sec < 10)
+ prefix += min + ":0" + sec.toFixed(3) + " | ";
+ else
+ prefix += min + ":" + sec.toFixed(3) + " | ";
+ }
+
+ dump(prefix + str + "\n");
+ }
+}
+
+/** Dumps the current JS stack if DEBUG. */
+function dumpStack()
+{
+ // peel off the frames for dumpStack() and Error()
+ var stack = new Error().stack.split(/\n/).slice(2);
+ stack.forEach(dumpn);
+}
+
+
+/** The XPCOM thread manager. */
+var gThreadManager = null;
+
+/** The XPCOM prefs service. */
+var gRootPrefBranch = null;
+function getRootPrefBranch()
+{
+ if (!gRootPrefBranch)
+ {
+ gRootPrefBranch = Cc["@mozilla.org/preferences-service;1"]
+ .getService(Ci.nsIPrefBranch);
+ }
+ return gRootPrefBranch;
+}
+
+/**
+* JavaScript constructors for commonly-used classes; precreating these is a
+* speedup over doing the same from base principles. See the docs at
+* http://developer.mozilla.org/en/docs/components.Constructor for details.
+*/
+const ServerSocket = CC("@mozilla.org/network/server-socket;1",
+ "nsIServerSocket",
+ "init");
+const ScriptableInputStream = CC("@mozilla.org/scriptableinputstream;1",
+ "nsIScriptableInputStream",
+ "init");
+const Pipe = CC("@mozilla.org/pipe;1",
+ "nsIPipe",
+ "init");
+const FileInputStream = CC("@mozilla.org/network/file-input-stream;1",
+ "nsIFileInputStream",
+ "init");
+const ConverterInputStream = CC("@mozilla.org/intl/converter-input-stream;1",
+ "nsIConverterInputStream",
+ "init");
+const WritablePropertyBag = CC("@mozilla.org/hash-property-bag;1",
+ "nsIWritablePropertyBag2");
+const SupportsString = CC("@mozilla.org/supports-string;1",
+ "nsISupportsString");
+
+/* These two are non-const only so a test can overwrite them. */
+var BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
+ "nsIBinaryInputStream",
+ "setInputStream");
+var BinaryOutputStream = CC("@mozilla.org/binaryoutputstream;1",
+ "nsIBinaryOutputStream",
+ "setOutputStream");
+
+/**
+* Returns the RFC 822/1123 representation of a date.
+*
+* @param date : Number
+* the date, in milliseconds from midnight (00:00:00), January 1, 1970 GMT
+* @returns string
+* the representation of the given date
+*/
+function toDateString(date)
+{
+ //
+ // rfc1123-date = wkday "," SP date1 SP time SP "GMT"
+ // date1 = 2DIGIT SP month SP 4DIGIT
+ // ; day month year (e.g., 02 Jun 1982)
+ // time = 2DIGIT ":" 2DIGIT ":" 2DIGIT
+ // ; 00:00:00 - 23:59:59
+ // wkday = "Mon" | "Tue" | "Wed"
+ // | "Thu" | "Fri" | "Sat" | "Sun"
+ // month = "Jan" | "Feb" | "Mar" | "Apr"
+ // | "May" | "Jun" | "Jul" | "Aug"
+ // | "Sep" | "Oct" | "Nov" | "Dec"
+ //
+
+ const wkdayStrings = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
+ const monthStrings = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
+
+ /**
+* Processes a date and returns the encoded UTC time as a string according to
+* the format specified in RFC 2616.
+*
+* @param date : Date
+* the date to process
+* @returns string
+* a string of the form "HH:MM:SS", ranging from "00:00:00" to "23:59:59"
+*/
+ function toTime(date)
+ {
+ var hrs = date.getUTCHours();
+ var rv = (hrs < 10) ? "0" + hrs : hrs;
+
+ var mins = date.getUTCMinutes();
+ rv += ":";
+ rv += (mins < 10) ? "0" + mins : mins;
+
+ var secs = date.getUTCSeconds();
+ rv += ":";
+ rv += (secs < 10) ? "0" + secs : secs;
+
+ return rv;
+ }
+
+ /**
+* Processes a date and returns the encoded UTC date as a string according to
+* the date1 format specified in RFC 2616.
+*
+* @param date : Date
+* the date to process
+* @returns string
+* a string of the form "HH:MM:SS", ranging from "00:00:00" to "23:59:59"
+*/
+ function toDate1(date)
+ {
+ var day = date.getUTCDate();
+ var month = date.getUTCMonth();
+ var year = date.getUTCFullYear();
+
+ var rv = (day < 10) ? "0" + day : day;
+ rv += " " + monthStrings[month];
+ rv += " " + year;
+
+ return rv;
+ }
+
+ date = new Date(date);
+
+ const fmtString = "%wkday%, %date1% %time% GMT";
+ var rv = fmtString.replace("%wkday%", wkdayStrings[date.getUTCDay()]);
+ rv = rv.replace("%time%", toTime(date));
+ return rv.replace("%date1%", toDate1(date));
+}
+
+/**
+* Prints out a human-readable representation of the object o and its fields,
+* omitting those whose names begin with "_" if showMembers != true (to ignore
+* "private" properties exposed via getters/setters).
+*/
+function printObj(o, showMembers)
+{
+ var s = "******************************\n";
+ s += "o = {\n";
+ for (var i in o)
+ {
+ if (typeof(i) != "string" ||
+ (showMembers || (i.length > 0 && i[0] != "_")))
+ s+= " " + i + ": " + o[i] + ",\n";
+ }
+ s += " };\n";
+ s += "******************************";
+ dumpn(s);
+}
+
+/**
+* Instantiates a new HTTP server.
+*/
+function nsHttpServer()
+{
+ if (!gThreadManager)
+ gThreadManager = Cc["@mozilla.org/thread-manager;1"].getService();
+
+ /** The port on which this server listens. */
+ this._port = undefined;
+
+ /** The socket associated with this. */
+ this._socket = null;
+
+ /** The handler used to process requests to this server. */
+ this._handler = new ServerHandler(this);
+
+ /** Naming information for this server. */
+ this._identity = new ServerIdentity();
+
+ /**
+* Indicates when the server is to be shut down at the end of the request.
+*/
+ this._doQuit = false;
+
+ /**
+* True if the socket in this is closed (and closure notifications have been
+* sent and processed if the socket was ever opened), false otherwise.
+*/
+ this._socketClosed = true;
+
+ /**
+* Used for tracking existing connections and ensuring that all connections
+* are properly cleaned up before server shutdown; increases by 1 for every
+* new incoming connection.
+*/
+ this._connectionGen = 0;
+
+ /**
+* Hash of all open connections, indexed by connection number at time of
+* creation.
+*/
+ this._connections = {};
+}
+nsHttpServer.prototype =
+{
+ classID: components.ID("{54ef6f81-30af-4b1d-ac55-8ba811293e41}"),
+
+ // NSISERVERSOCKETLISTENER
+
+ /**
+* Processes an incoming request coming in on the given socket and contained
+* in the given transport.
+*
+* @param socket : nsIServerSocket
+* the socket through which the request was served
+* @param trans : nsISocketTransport
+* the transport for the request/response
+* @see nsIServerSocketListener.onSocketAccepted
+*/
+ onSocketAccepted: function(socket, trans)
+ {
+ dumpn("*** onSocketAccepted(socket=" + socket + ", trans=" + trans + ")");
+
+ dumpn(">>> new connection on " + trans.host + ":" + trans.port);
+
+ const SEGMENT_SIZE = 8192;
+ const SEGMENT_COUNT = 1024;
+ try
+ {
+ var input = trans.openInputStream(0, SEGMENT_SIZE, SEGMENT_COUNT)
+ .QueryInterface(Ci.nsIAsyncInputStream);
+ var output = trans.openOutputStream(0, 0, 0);
+ }
+ catch (e)
+ {
+ dumpn("*** error opening transport streams: " + e);
+ trans.close(Cr.NS_BINDING_ABORTED);
+ return;
+ }
+
+ var connectionNumber = ++this._connectionGen;
+
+ try
+ {
+ var conn = new Connection(input, output, this, socket.port, trans.port,
+ connectionNumber);
+ var reader = new RequestReader(conn);
+
+ // XXX add request timeout functionality here!
+
+ // Note: must use main thread here, or we might get a GC that will cause
+ // threadsafety assertions. We really need to fix XPConnect so that
+ // you can actually do things in multi-threaded JS. :-(
+ input.asyncWait(reader, 0, 0, gThreadManager.mainThread);
+ }
+ catch (e)
+ {
+ // Assume this connection can't be salvaged and bail on it completely;
+ // don't attempt to close it so that we can assert that any connection
+ // being closed is in this._connections.
+ dumpn("*** error in initial request-processing stages: " + e);
+ trans.close(Cr.NS_BINDING_ABORTED);
+ return;
+ }
+
+ this._connections[connectionNumber] = conn;
+ dumpn("*** starting connection " + connectionNumber);
+ },
+
+ /**
+* Called when the socket associated with this is closed.
+*
+* @param socket : nsIServerSocket
+* the socket being closed
+* @param status : nsresult
+* the reason the socket stopped listening (NS_BINDING_ABORTED if the server
+* was stopped using nsIHttpServer.stop)
+* @see nsIServerSocketListener.onStopListening
+*/
+ onStopListening: function(socket, status)
+ {
+ dumpn(">>> shutting down server on port " + socket.port);
+ this._socketClosed = true;
+ if (!this._hasOpenConnections())
+ {
+ dumpn("*** no open connections, notifying async from onStopListening");
+
+ // Notify asynchronously so that any pending teardown in stop() has a
+ // chance to run first.
+ var self = this;
+ var stopEvent =
+ {
+ run: function()
+ {
+ dumpn("*** _notifyStopped async callback");
+ self._notifyStopped();
+ }
+ };
+ gThreadManager.currentThread
+ .dispatch(stopEvent, Ci.nsIThread.DISPATCH_NORMAL);
+ }
+ },
+
+ // NSIHTTPSERVER
+
+ //
+ // see nsIHttpServer.start
+ //
+ start: function(port)
+ {
+ this._start(port, "localhost")
+ },
+
+ _start: function(port, host)
+ {
+ if (this._socket)
+ throw Cr.NS_ERROR_ALREADY_INITIALIZED;
+
+ this._port = port;
+ this._doQuit = this._socketClosed = false;
+
+ this._host = host;
+
+ // The listen queue needs to be long enough to handle
+ // network.http.max-persistent-connections-per-server concurrent connections,
+ // plus a safety margin in case some other process is talking to
+ // the server as well.
+ var prefs = getRootPrefBranch();
+ var maxConnections;
+ try {
+ // Bug 776860: The original pref was removed in favor of this new one:
+ maxConnections = prefs.getIntPref("network.http.max-persistent-connections-per-server") + 5;
+ }
+ catch(e) {
+ maxConnections = prefs.getIntPref("network.http.max-connections-per-server") + 5;
+ }
+
+ try
+ {
+ var loopback = true;
+ if (this._host != "127.0.0.1" && this._host != "localhost") {
+ var loopback = false;
+ }
+
+ var socket = new ServerSocket(this._port,
+ loopback, // true = localhost, false = everybody
+ maxConnections);
+ dumpn(">>> listening on port " + socket.port + ", " + maxConnections +
+ " pending connections");
+ socket.asyncListen(this);
+ this._identity._initialize(socket.port, host, true);
+ this._socket = socket;
+ }
+ catch (e)
+ {
+ dumpn("!!! could not start server on port " + port + ": " + e);
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ }
+ },
+
+ //
+ // see nsIHttpServer.stop
+ //
+ stop: function(callback)
+ {
+ if (!callback)
+ throw Cr.NS_ERROR_NULL_POINTER;
+ if (!this._socket)
+ throw Cr.NS_ERROR_UNEXPECTED;
+
+ this._stopCallback = typeof callback === "function"
+ ? callback
+ : function() { callback.onStopped(); };
+
+ dumpn(">>> stopping listening on port " + this._socket.port);
+ this._socket.close();
+ this._socket = null;
+
+ // We can't have this identity any more, and the port on which we're running
+ // this server now could be meaningless the next time around.
+ this._identity._teardown();
+
+ this._doQuit = false;
+
+ // socket-close notification and pending request completion happen async
+ },
+
+ //
+ // see nsIHttpServer.registerFile
+ //
+ registerFile: function(path, file)
+ {
+ if (file && (!file.exists() || file.isDirectory()))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ this._handler.registerFile(path, file);
+ },
+
+ //
+ // see nsIHttpServer.registerDirectory
+ //
+ registerDirectory: function(path, directory)
+ {
+ // XXX true path validation!
+ if (path.charAt(0) != "/" ||
+ path.charAt(path.length - 1) != "/" ||
+ (directory &&
+ (!directory.exists() || !directory.isDirectory())))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ // XXX determine behavior of nonexistent /foo/bar when a /foo/bar/ mapping
+ // exists!
+
+ this._handler.registerDirectory(path, directory);
+ },
+
+ //
+ // see nsIHttpServer.registerPathHandler
+ //
+ registerPathHandler: function(path, handler)
+ {
+ this._handler.registerPathHandler(path, handler);
+ },
+
+ //
+ // see nsIHttpServer.registerPrefixHandler
+ //
+ registerPrefixHandler: function(prefix, handler)
+ {
+ this._handler.registerPrefixHandler(prefix, handler);
+ },
+
+ //
+ // see nsIHttpServer.registerErrorHandler
+ //
+ registerErrorHandler: function(code, handler)
+ {
+ this._handler.registerErrorHandler(code, handler);
+ },
+
+ //
+ // see nsIHttpServer.setIndexHandler
+ //
+ setIndexHandler: function(handler)
+ {
+ this._handler.setIndexHandler(handler);
+ },
+
+ //
+ // see nsIHttpServer.registerContentType
+ //
+ registerContentType: function(ext, type)
+ {
+ this._handler.registerContentType(ext, type);
+ },
+
+ //
+ // see nsIHttpServer.serverIdentity
+ //
+ get identity()
+ {
+ return this._identity;
+ },
+
+ //
+ // see nsIHttpServer.getState
+ //
+ getState: function(path, k)
+ {
+ return this._handler._getState(path, k);
+ },
+
+ //
+ // see nsIHttpServer.setState
+ //
+ setState: function(path, k, v)
+ {
+ return this._handler._setState(path, k, v);
+ },
+
+ //
+ // see nsIHttpServer.getSharedState
+ //
+ getSharedState: function(k)
+ {
+ return this._handler._getSharedState(k);
+ },
+
+ //
+ // see nsIHttpServer.setSharedState
+ //
+ setSharedState: function(k, v)
+ {
+ return this._handler._setSharedState(k, v);
+ },
+
+ //
+ // see nsIHttpServer.getObjectState
+ //
+ getObjectState: function(k)
+ {
+ return this._handler._getObjectState(k);
+ },
+
+ //
+ // see nsIHttpServer.setObjectState
+ //
+ setObjectState: function(k, v)
+ {
+ return this._handler._setObjectState(k, v);
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIServerSocketListener) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // NON-XPCOM PUBLIC API
+
+ /**
+* Returns true iff this server is not running (and is not in the process of
+* serving any requests still to be processed when the server was last
+* stopped after being run).
+*/
+ isStopped: function()
+ {
+ return this._socketClosed && !this._hasOpenConnections();
+ },
+
+ // PRIVATE IMPLEMENTATION
+
+ /** True if this server has any open connections to it, false otherwise. */
+ _hasOpenConnections: function()
+ {
+ //
+ // If we have any open connections, they're tracked as numeric properties on
+ // |this._connections|. The non-standard __count__ property could be used
+ // to check whether there are any properties, but standard-wise, even
+ // looking forward to ES5, there's no less ugly yet still O(1) way to do
+ // this.
+ //
+ for (var n in this._connections)
+ return true;
+ return false;
+ },
+
+ /** Calls the server-stopped callback provided when stop() was called. */
+ _notifyStopped: function()
+ {
+ NS_ASSERT(this._stopCallback !== null, "double-notifying?");
+ NS_ASSERT(!this._hasOpenConnections(), "should be done serving by now");
+
+ //
+ // NB: We have to grab this now, null out the member, *then* call the
+ // callback here, or otherwise the callback could (indirectly) futz with
+ // this._stopCallback by starting and immediately stopping this, at
+ // which point we'd be nulling out a field we no longer have a right to
+ // modify.
+ //
+ var callback = this._stopCallback;
+ this._stopCallback = null;
+ try
+ {
+ callback();
+ }
+ catch (e)
+ {
+ // not throwing because this is specified as being usually (but not
+ // always) asynchronous
+ dump("!!! error running onStopped callback: " + e + "\n");
+ }
+ },
+
+ /**
+* Notifies this server that the given connection has been closed.
+*
+* @param connection : Connection
+* the connection that was closed
+*/
+ _connectionClosed: function(connection)
+ {
+ NS_ASSERT(connection.number in this._connections,
+ "closing a connection " + this + " that we never added to the " +
+ "set of open connections?");
+ NS_ASSERT(this._connections[connection.number] === connection,
+ "connection number mismatch? " +
+ this._connections[connection.number]);
+ delete this._connections[connection.number];
+
+ // Fire a pending server-stopped notification if it's our responsibility.
+ if (!this._hasOpenConnections() && this._socketClosed)
+ this._notifyStopped();
+ },
+
+ /**
+* Requests that the server be shut down when possible.
+*/
+ _requestQuit: function()
+ {
+ dumpn(">>> requesting a quit");
+ dumpStack();
+ this._doQuit = true;
+ }
+};
+
+
+//
+// RFC 2396 section 3.2.2:
+//
+// host = hostname | IPv4address
+// hostname = *( domainlabel "." ) toplabel [ "." ]
+// domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
+// toplabel = alpha | alpha *( alphanum | "-" ) alphanum
+// IPv4address = 1*digit "." 1*digit "." 1*digit "." 1*digit
+//
+
+const HOST_REGEX =
+ new RegExp("^(?:" +
+ // *( domainlabel "." )
+ "(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)*" +
+ // toplabel
+ "[a-z](?:[a-z0-9-]*[a-z0-9])?" +
+ "|" +
+ // IPv4 address
+ "\\d+\\.\\d+\\.\\d+\\.\\d+" +
+ ")$",
+ "i");
+
+
+/**
+* Represents the identity of a server. An identity consists of a set of
+* (scheme, host, port) tuples denoted as locations (allowing a single server to
+* serve multiple sites or to be used behind both HTTP and HTTPS proxies for any
+* host/port). Any incoming request must be to one of these locations, or it
+* will be rejected with an HTTP 400 error. One location, denoted as the
+* primary location, is the location assigned in contexts where a location
+* cannot otherwise be endogenously derived, such as for HTTP/1.0 requests.
+*
+* A single identity may contain at most one location per unique host/port pair;
+* other than that, no restrictions are placed upon what locations may
+* constitute an identity.
+*/
+function ServerIdentity()
+{
+ /** The scheme of the primary location. */
+ this._primaryScheme = "http";
+
+ /** The hostname of the primary location. */
+ this._primaryHost = "127.0.0.1"
+
+ /** The port number of the primary location. */
+ this._primaryPort = -1;
+
+ /**
+* The current port number for the corresponding server, stored so that a new
+* primary location can always be set if the current one is removed.
+*/
+ this._defaultPort = -1;
+
+ /**
+* Maps hosts to maps of ports to schemes, e.g. the following would represent
+* https://example.com:789/ and http://example.org/:
+*
+* {
+* "xexample.com": { 789: "https" },
+* "xexample.org": { 80: "http" }
+* }
+*
+* Note the "x" prefix on hostnames, which prevents collisions with special
+* JS names like "prototype".
+*/
+ this._locations = { "xlocalhost": {} };
+}
+ServerIdentity.prototype =
+{
+ // NSIHTTPSERVERIDENTITY
+
+ //
+ // see nsIHttpServerIdentity.primaryScheme
+ //
+ get primaryScheme()
+ {
+ if (this._primaryPort === -1)
+ throw Cr.NS_ERROR_NOT_INITIALIZED;
+ return this._primaryScheme;
+ },
+
+ //
+ // see nsIHttpServerIdentity.primaryHost
+ //
+ get primaryHost()
+ {
+ if (this._primaryPort === -1)
+ throw Cr.NS_ERROR_NOT_INITIALIZED;
+ return this._primaryHost;
+ },
+
+ //
+ // see nsIHttpServerIdentity.primaryPort
+ //
+ get primaryPort()
+ {
+ if (this._primaryPort === -1)
+ throw Cr.NS_ERROR_NOT_INITIALIZED;
+ return this._primaryPort;
+ },
+
+ //
+ // see nsIHttpServerIdentity.add
+ //
+ add: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ var entry = this._locations["x" + host];
+ if (!entry)
+ this._locations["x" + host] = entry = {};
+
+ entry[port] = scheme;
+ },
+
+ //
+ // see nsIHttpServerIdentity.remove
+ //
+ remove: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ var entry = this._locations["x" + host];
+ if (!entry)
+ return false;
+
+ var present = port in entry;
+ delete entry[port];
+
+ if (this._primaryScheme == scheme &&
+ this._primaryHost == host &&
+ this._primaryPort == port &&
+ this._defaultPort !== -1)
+ {
+ // Always keep at least one identity in existence at any time, unless
+ // we're in the process of shutting down (the last condition above).
+ this._primaryPort = -1;
+ this._initialize(this._defaultPort, host, false);
+ }
+
+ return present;
+ },
+
+ //
+ // see nsIHttpServerIdentity.has
+ //
+ has: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ return "x" + host in this._locations &&
+ scheme === this._locations["x" + host][port];
+ },
+
+ //
+ // see nsIHttpServerIdentity.has
+ //
+ getScheme: function(host, port)
+ {
+ this._validate("http", host, port);
+
+ var entry = this._locations["x" + host];
+ if (!entry)
+ return "";
+
+ return entry[port] || "";
+ },
+
+ //
+ // see nsIHttpServerIdentity.setPrimary
+ //
+ setPrimary: function(scheme, host, port)
+ {
+ this._validate(scheme, host, port);
+
+ this.add(scheme, host, port);
+
+ this._primaryScheme = scheme;
+ this._primaryHost = host;
+ this._primaryPort = port;
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIHttpServerIdentity) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // PRIVATE IMPLEMENTATION
+
+ /**
+* Initializes the primary name for the corresponding server, based on the
+* provided port number.
+*/
+ _initialize: function(port, host, addSecondaryDefault)
+ {
+ this._host = host;
+ if (this._primaryPort !== -1)
+ this.add("http", host, port);
+ else
+ this.setPrimary("http", "localhost", port);
+ this._defaultPort = port;
+
+ // Only add this if we're being called at server startup
+ if (addSecondaryDefault && host != "127.0.0.1")
+ this.add("http", "127.0.0.1", port);
+ },
+
+ /**
+* Called at server shutdown time, unsets the primary location only if it was
+* the default-assigned location and removes the default location from the
+* set of locations used.
+*/
+ _teardown: function()
+ {
+ if (this._host != "127.0.0.1") {
+ // Not the default primary location, nothing special to do here
+ this.remove("http", "127.0.0.1", this._defaultPort);
+ }
+
+ // This is a *very* tricky bit of reasoning here; make absolutely sure the
+ // tests for this code pass before you commit changes to it.
+ if (this._primaryScheme == "http" &&
+ this._primaryHost == this._host &&
+ this._primaryPort == this._defaultPort)
+ {
+ // Make sure we don't trigger the readding logic in .remove(), then remove
+ // the default location.
+ var port = this._defaultPort;
+ this._defaultPort = -1;
+ this.remove("http", this._host, port);
+
+ // Ensure a server start triggers the setPrimary() path in ._initialize()
+ this._primaryPort = -1;
+ }
+ else
+ {
+ // No reason not to remove directly as it's not our primary location
+ this.remove("http", this._host, this._defaultPort);
+ }
+ },
+
+ /**
+* Ensures scheme, host, and port are all valid with respect to RFC 2396.
+*
+* @throws NS_ERROR_ILLEGAL_VALUE
+* if any argument doesn't match the corresponding production
+*/
+ _validate: function(scheme, host, port)
+ {
+ if (scheme !== "http" && scheme !== "https")
+ {
+ dumpn("*** server only supports http/https schemes: '" + scheme + "'");
+ dumpStack();
+ throw Cr.NS_ERROR_ILLEGAL_VALUE;
+ }
+ if (!HOST_REGEX.test(host))
+ {
+ dumpn("*** unexpected host: '" + host + "'");
+ throw Cr.NS_ERROR_ILLEGAL_VALUE;
+ }
+ if (port < 0 || port > 65535)
+ {
+ dumpn("*** unexpected port: '" + port + "'");
+ throw Cr.NS_ERROR_ILLEGAL_VALUE;
+ }
+ }
+};
+
+
+/**
+* Represents a connection to the server (and possibly in the future the thread
+* on which the connection is processed).
+*
+* @param input : nsIInputStream
+* stream from which incoming data on the connection is read
+* @param output : nsIOutputStream
+* stream to write data out the connection
+* @param server : nsHttpServer
+* the server handling the connection
+* @param port : int
+* the port on which the server is running
+* @param outgoingPort : int
+* the outgoing port used by this connection
+* @param number : uint
+* a serial number used to uniquely identify this connection
+*/
+function Connection(input, output, server, port, outgoingPort, number)
+{
+ dumpn("*** opening new connection " + number + " on port " + outgoingPort);
+
+ /** Stream of incoming data. */
+ this.input = input;
+
+ /** Stream for outgoing data. */
+ this.output = output;
+
+ /** The server associated with this request. */
+ this.server = server;
+
+ /** The port on which the server is running. */
+ this.port = port;
+
+ /** The outgoing poort used by this connection. */
+ this._outgoingPort = outgoingPort;
+
+ /** The serial number of this connection. */
+ this.number = number;
+
+ /**
+* The request for which a response is being generated, null if the
+* incoming request has not been fully received or if it had errors.
+*/
+ this.request = null;
+
+ /** State variables for debugging. */
+ this._closed = this._processed = false;
+}
+Connection.prototype =
+{
+ /** Closes this connection's input/output streams. */
+ close: function()
+ {
+ dumpn("*** closing connection " + this.number +
+ " on port " + this._outgoingPort);
+
+ this.input.close();
+ this.output.close();
+ this._closed = true;
+
+ var server = this.server;
+ server._connectionClosed(this);
+
+ // If an error triggered a server shutdown, act on it now
+ if (server._doQuit)
+ server.stop(function() { /* not like we can do anything better */ });
+ },
+
+ /**
+* Initiates processing of this connection, using the data in the given
+* request.
+*
+* @param request : Request
+* the request which should be processed
+*/
+ process: function(request)
+ {
+ NS_ASSERT(!this._closed && !this._processed);
+
+ this._processed = true;
+
+ this.request = request;
+ this.server._handler.handleResponse(this);
+ },
+
+ /**
+* Initiates processing of this connection, generating a response with the
+* given HTTP error code.
+*
+* @param code : uint
+* an HTTP code, so in the range [0, 1000)
+* @param request : Request
+* incomplete data about the incoming request (since there were errors
+* during its processing
+*/
+ processError: function(code, request)
+ {
+ NS_ASSERT(!this._closed && !this._processed);
+
+ this._processed = true;
+ this.request = request;
+ this.server._handler.handleError(code, this);
+ },
+
+ /** Converts this to a string for debugging purposes. */
+ toString: function()
+ {
+ return "<Connection(" + this.number +
+ (this.request ? ", " + this.request.path : "") +"): " +
+ (this._closed ? "closed" : "open") + ">";
+ }
+};
+
+
+
+/** Returns an array of count bytes from the given input stream. */
+function readBytes(inputStream, count)
+{
+ return new BinaryInputStream(inputStream).readByteArray(count);
+}
+
+
+
+/** Request reader processing states; see RequestReader for details. */
+const READER_IN_REQUEST_LINE = 0;
+const READER_IN_HEADERS = 1;
+const READER_IN_BODY = 2;
+const READER_FINISHED = 3;
+
+
+/**
+* Reads incoming request data asynchronously, does any necessary preprocessing,
+* and forwards it to the request handler. Processing occurs in three states:
+*
+* READER_IN_REQUEST_LINE Reading the request's status line
+* READER_IN_HEADERS Reading headers in the request
+* READER_IN_BODY Reading the body of the request
+* READER_FINISHED Entire request has been read and processed
+*
+* During the first two stages, initial metadata about the request is gathered
+* into a Request object. Once the status line and headers have been processed,
+* we start processing the body of the request into the Request. Finally, when
+* the entire body has been read, we create a Response and hand it off to the
+* ServerHandler to be given to the appropriate request handler.
+*
+* @param connection : Connection
+* the connection for the request being read
+*/
+function RequestReader(connection)
+{
+ /** Connection metadata for this request. */
+ this._connection = connection;
+
+ /**
+* A container providing line-by-line access to the raw bytes that make up the
+* data which has been read from the connection but has not yet been acted
+* upon (by passing it to the request handler or by extracting request
+* metadata from it).
+*/
+ this._data = new LineData();
+
+ /**
+* The amount of data remaining to be read from the body of this request.
+* After all headers in the request have been read this is the value in the
+* Content-Length header, but as the body is read its value decreases to zero.
+*/
+ this._contentLength = 0;
+
+ /** The current state of parsing the incoming request. */
+ this._state = READER_IN_REQUEST_LINE;
+
+ /** Metadata constructed from the incoming request for the request handler. */
+ this._metadata = new Request(connection.port);
+
+ /**
+* Used to preserve state if we run out of line data midway through a
+* multi-line header. _lastHeaderName stores the name of the header, while
+* _lastHeaderValue stores the value we've seen so far for the header.
+*
+* These fields are always either both undefined or both strings.
+*/
+ this._lastHeaderName = this._lastHeaderValue = undefined;
+}
+RequestReader.prototype =
+{
+ // NSIINPUTSTREAMCALLBACK
+
+ /**
+* Called when more data from the incoming request is available. This method
+* then reads the available data from input and deals with that data as
+* necessary, depending upon the syntax of already-downloaded data.
+*
+* @param input : nsIAsyncInputStream
+* the stream of incoming data from the connection
+*/
+ onInputStreamReady: function(input)
+ {
+ dumpn("*** onInputStreamReady(input=" + input + ") on thread " +
+ gThreadManager.currentThread + " (main is " +
+ gThreadManager.mainThread + ")");
+ dumpn("*** this._state == " + this._state);
+
+ // Handle cases where we get more data after a request error has been
+ // discovered but *before* we can close the connection.
+ var data = this._data;
+ if (!data)
+ return;
+
+ try
+ {
+ data.appendBytes(readBytes(input, input.available()));
+ }
+ catch (e)
+ {
+ if (streamClosed(e))
+ {
+ dumpn("*** WARNING: unexpected error when reading from socket; will " +
+ "be treated as if the input stream had been closed");
+ dumpn("*** WARNING: actual error was: " + e);
+ }
+
+ // We've lost a race -- input has been closed, but we're still expecting
+ // to read more data. available() will throw in this case, and since
+ // we're dead in the water now, destroy the connection.
+ dumpn("*** onInputStreamReady called on a closed input, destroying " +
+ "connection");
+ this._connection.close();
+ return;
+ }
+
+ switch (this._state)
+ {
+ default:
+ NS_ASSERT(false, "invalid state: " + this._state);
+ break;
+
+ case READER_IN_REQUEST_LINE:
+ if (!this._processRequestLine())
+ break;
+ /* fall through */
+
+ case READER_IN_HEADERS:
+ if (!this._processHeaders())
+ break;
+ /* fall through */
+
+ case READER_IN_BODY:
+ this._processBody();
+ }
+
+ if (this._state != READER_FINISHED)
+ input.asyncWait(this, 0, 0, gThreadManager.currentThread);
+ },
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(aIID)
+ {
+ if (aIID.equals(Ci.nsIInputStreamCallback) ||
+ aIID.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // PRIVATE API
+
+ /**
+* Processes unprocessed, downloaded data as a request line.
+*
+* @returns boolean
+* true iff the request line has been fully processed
+*/
+ _processRequestLine: function()
+ {
+ NS_ASSERT(this._state == READER_IN_REQUEST_LINE);
+
+ // Servers SHOULD ignore any empty line(s) received where a Request-Line
+ // is expected (section 4.1).
+ var data = this._data;
+ var line = {};
+ var readSuccess;
+ while ((readSuccess = data.readLine(line)) && line.value == "")
+ dumpn("*** ignoring beginning blank line...");
+
+ // if we don't have a full line, wait until we do
+ if (!readSuccess)
+ return false;
+
+ // we have the first non-blank line
+ try
+ {
+ this._parseRequestLine(line.value);
+ this._state = READER_IN_HEADERS;
+ return true;
+ }
+ catch (e)
+ {
+ this._handleError(e);
+ return false;
+ }
+ },
+
+ /**
+* Processes stored data, assuming it is either at the beginning or in
+* the middle of processing request headers.
+*
+* @returns boolean
+* true iff header data in the request has been fully processed
+*/
+ _processHeaders: function()
+ {
+ NS_ASSERT(this._state == READER_IN_HEADERS);
+
+ // XXX things to fix here:
+ //
+ // - need to support RFC 2047-encoded non-US-ASCII characters
+
+ try
+ {
+ var done = this._parseHeaders();
+ if (done)
+ {
+ var request = this._metadata;
+
+ // XXX this is wrong for requests with transfer-encodings applied to
+ // them, particularly chunked (which by its nature can have no
+ // meaningful Content-Length header)!
+ this._contentLength = request.hasHeader("Content-Length")
+ ? parseInt(request.getHeader("Content-Length"), 10)
+ : 0;
+ dumpn("_processHeaders, Content-length=" + this._contentLength);
+
+ this._state = READER_IN_BODY;
+ }
+ return done;
+ }
+ catch (e)
+ {
+ this._handleError(e);
+ return false;
+ }
+ },
+
+ /**
+* Processes stored data, assuming it is either at the beginning or in
+* the middle of processing the request body.
+*
+* @returns boolean
+* true iff the request body has been fully processed
+*/
+ _processBody: function()
+ {
+ NS_ASSERT(this._state == READER_IN_BODY);
+
+ // XXX handle chunked transfer-coding request bodies!
+
+ try
+ {
+ if (this._contentLength > 0)
+ {
+ var data = this._data.purge();
+ var count = Math.min(data.length, this._contentLength);
+ dumpn("*** loading data=" + data + " len=" + data.length +
+ " excess=" + (data.length - count));
+
+ var bos = new BinaryOutputStream(this._metadata._bodyOutputStream);
+ bos.writeByteArray(data, count);
+ this._contentLength -= count;
+ }
+
+ dumpn("*** remaining body data len=" + this._contentLength);
+ if (this._contentLength == 0)
+ {
+ this._validateRequest();
+ this._state = READER_FINISHED;
+ this._handleResponse();
+ return true;
+ }
+
+ return false;
+ }
+ catch (e)
+ {
+ this._handleError(e);
+ return false;
+ }
+ },
+
+ /**
+* Does various post-header checks on the data in this request.
+*
+* @throws : HttpError
+* if the request was malformed in some way
+*/
+ _validateRequest: function()
+ {
+ NS_ASSERT(this._state == READER_IN_BODY);
+
+ dumpn("*** _validateRequest");
+
+ var metadata = this._metadata;
+ var headers = metadata._headers;
+
+ // 19.6.1.1 -- servers MUST report 400 to HTTP/1.1 requests w/o Host header
+ var identity = this._connection.server.identity;
+ if (metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1))
+ {
+ if (!headers.hasHeader("Host"))
+ {
+ dumpn("*** malformed HTTP/1.1 or greater request with no Host header!");
+ throw HTTP_400;
+ }
+
+ // If the Request-URI wasn't absolute, then we need to determine our host.
+ // We have to determine what scheme was used to access us based on the
+ // server identity data at this point, because the request just doesn't
+ // contain enough data on its own to do this, sadly.
+ if (!metadata._host)
+ {
+ var host, port;
+ var hostPort = headers.getHeader("Host");
+ var colon = hostPort.indexOf(":");
+ if (colon < 0)
+ {
+ host = hostPort;
+ port = "";
+ }
+ else
+ {
+ host = hostPort.substring(0, colon);
+ port = hostPort.substring(colon + 1);
+ }
+
+ // NB: We allow an empty port here because, oddly, a colon may be
+ // present even without a port number, e.g. "example.com:"; in this
+ // case the default port applies.
+ if (!HOST_REGEX.test(host) || !/^\d*$/.test(port))
+ {
+ dumpn("*** malformed hostname (" + hostPort + ") in Host " +
+ "header, 400 time");
+ throw HTTP_400;
+ }
+
+ // If we're not given a port, we're stuck, because we don't know what
+ // scheme to use to look up the correct port here, in general. Since
+ // the HTTPS case requires a tunnel/proxy and thus requires that the
+ // requested URI be absolute (and thus contain the necessary
+ // information), let's assume HTTP will prevail and use that.
+ port = +port || 80;
+
+ var scheme = identity.getScheme(host, port);
+ if (!scheme)
+ {
+ dumpn("*** unrecognized hostname (" + hostPort + ") in Host " +
+ "header, 400 time");
+ throw HTTP_400;
+ }
+
+ metadata._scheme = scheme;
+ metadata._host = host;
+ metadata._port = port;
+ }
+ }
+ else
+ {
+ NS_ASSERT(metadata._host === undefined,
+ "HTTP/1.0 doesn't allow absolute paths in the request line!");
+
+ metadata._scheme = identity.primaryScheme;
+ metadata._host = identity.primaryHost;
+ metadata._port = identity.primaryPort;
+ }
+
+ NS_ASSERT(identity.has(metadata._scheme, metadata._host, metadata._port),
+ "must have a location we recognize by now!");
+ },
+
+ /**
+* Handles responses in case of error, either in the server or in the request.
+*
+* @param e
+* the specific error encountered, which is an HttpError in the case where
+* the request is in some way invalid or cannot be fulfilled; if this isn't
+* an HttpError we're going to be paranoid and shut down, because that
+* shouldn't happen, ever
+*/
+ _handleError: function(e)
+ {
+ // Don't fall back into normal processing!
+ this._state = READER_FINISHED;
+
+ var server = this._connection.server;
+ if (e instanceof HttpError)
+ {
+ var code = e.code;
+ }
+ else
+ {
+ dumpn("!!! UNEXPECTED ERROR: " + e +
+ (e.lineNumber ? ", line " + e.lineNumber : ""));
+
+ // no idea what happened -- be paranoid and shut down
+ code = 500;
+ server._requestQuit();
+ }
+
+ // make attempted reuse of data an error
+ this._data = null;
+
+ this._connection.processError(code, this._metadata);
+ },
+
+ /**
+* Now that we've read the request line and headers, we can actually hand off
+* the request to be handled.
+*
+* This method is called once per request, after the request line and all
+* headers and the body, if any, have been received.
+*/
+ _handleResponse: function()
+ {
+ NS_ASSERT(this._state == READER_FINISHED);
+
+ // We don't need the line-based data any more, so make attempted reuse an
+ // error.
+ this._data = null;
+
+ this._connection.process(this._metadata);
+ },
+
+
+ // PARSING
+
+ /**
+* Parses the request line for the HTTP request associated with this.
+*
+* @param line : string
+* the request line
+*/
+ _parseRequestLine: function(line)
+ {
+ NS_ASSERT(this._state == READER_IN_REQUEST_LINE);
+
+ dumpn("*** _parseRequestLine('" + line + "')");
+
+ var metadata = this._metadata;
+
+ // clients and servers SHOULD accept any amount of SP or HT characters
+ // between fields, even though only a single SP is required (section 19.3)
+ var request = line.split(/[ \t]+/);
+ if (!request || request.length != 3)
+ throw HTTP_400;
+
+ metadata._method = request[0];
+
+ // get the HTTP version
+ var ver = request[2];
+ var match = ver.match(/^HTTP\/(\d+\.\d+)$/);
+ if (!match)
+ throw HTTP_400;
+
+ // determine HTTP version
+ try
+ {
+ metadata._httpVersion = new nsHttpVersion(match[1]);
+ if (!metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_0))
+ throw "unsupported HTTP version";
+ }
+ catch (e)
+ {
+ // we support HTTP/1.0 and HTTP/1.1 only
+ throw HTTP_501;
+ }
+
+
+ var fullPath = request[1];
+ var serverIdentity = this._connection.server.identity;
+
+ var scheme, host, port;
+
+ if (fullPath.charAt(0) != "/")
+ {
+ // No absolute paths in the request line in HTTP prior to 1.1
+ if (!metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1))
+ throw HTTP_400;
+
+ try
+ {
+ var uri = Cc["@mozilla.org/network/io-service;1"]
+ .getService(Ci.nsIIOService)
+ .newURI(fullPath, null, null);
+ fullPath = uri.path;
+ scheme = uri.scheme;
+ host = metadata._host = uri.asciiHost;
+ port = uri.port;
+ if (port === -1)
+ {
+ if (scheme === "http")
+ port = 80;
+ else if (scheme === "https")
+ port = 443;
+ else
+ throw HTTP_400;
+ }
+ }
+ catch (e)
+ {
+ // If the host is not a valid host on the server, the response MUST be a
+ // 400 (Bad Request) error message (section 5.2). Alternately, the URI
+ // is malformed.
+ throw HTTP_400;
+ }
+
+ if (!serverIdentity.has(scheme, host, port) || fullPath.charAt(0) != "/")
+ throw HTTP_400;
+ }
+
+ var splitter = fullPath.indexOf("?");
+ if (splitter < 0)
+ {
+ // _queryString already set in ctor
+ metadata._path = fullPath;
+ }
+ else
+ {
+ metadata._path = fullPath.substring(0, splitter);
+ metadata._queryString = fullPath.substring(splitter + 1);
+ }
+
+ metadata._scheme = scheme;
+ metadata._host = host;
+ metadata._port = port;
+ },
+
+ /**
+* Parses all available HTTP headers in this until the header-ending CRLFCRLF,
+* adding them to the store of headers in the request.
+*
+* @throws
+* HTTP_400 if the headers are malformed
+* @returns boolean
+* true if all headers have now been processed, false otherwise
+*/
+ _parseHeaders: function()
+ {
+ NS_ASSERT(this._state == READER_IN_HEADERS);
+
+ dumpn("*** _parseHeaders");
+
+ var data = this._data;
+
+ var headers = this._metadata._headers;
+ var lastName = this._lastHeaderName;
+ var lastVal = this._lastHeaderValue;
+
+ var line = {};
+ while (true)
+ {
+ NS_ASSERT(!((lastVal === undefined) ^ (lastName === undefined)),
+ lastName === undefined ?
+ "lastVal without lastName? lastVal: '" + lastVal + "'" :
+ "lastName without lastVal? lastName: '" + lastName + "'");
+
+ if (!data.readLine(line))
+ {
+ // save any data we have from the header we might still be processing
+ this._lastHeaderName = lastName;
+ this._lastHeaderValue = lastVal;
+ return false;
+ }
+
+ var lineText = line.value;
+ var firstChar = lineText.charAt(0);
+
+ // blank line means end of headers
+ if (lineText == "")
+ {
+ // we're finished with the previous header
+ if (lastName)
+ {
+ try
+ {
+ headers.setHeader(lastName, lastVal, true);
+ }
+ catch (e)
+ {
+ dumpn("*** e == " + e);
+ throw HTTP_400;
+ }
+ }
+ else
+ {
+ // no headers in request -- valid for HTTP/1.0 requests
+ }
+
+ // either way, we're done processing headers
+ this._state = READER_IN_BODY;
+ return true;
+ }
+ else if (firstChar == " " || firstChar == "\t")
+ {
+ // multi-line header if we've already seen a header line
+ if (!lastName)
+ {
+ // we don't have a header to continue!
+ throw HTTP_400;
+ }
+
+ // append this line's text to the value; starts with SP/HT, so no need
+ // for separating whitespace
+ lastVal += lineText;
+ }
+ else
+ {
+ // we have a new header, so set the old one (if one existed)
+ if (lastName)
+ {
+ try
+ {
+ headers.setHeader(lastName, lastVal, true);
+ }
+ catch (e)
+ {
+ dumpn("*** e == " + e);
+ throw HTTP_400;
+ }
+ }
+
+ var colon = lineText.indexOf(":"); // first colon must be splitter
+ if (colon < 1)
+ {
+ // no colon or missing header field-name
+ throw HTTP_400;
+ }
+
+ // set header name, value (to be set in the next loop, usually)
+ lastName = lineText.substring(0, colon);
+ lastVal = lineText.substring(colon + 1);
+ } // empty, continuation, start of header
+ } // while (true)
+ }
+};
+
+
+/** The character codes for CR and LF. */
+const CR = 0x0D, LF = 0x0A;
+
+/**
+* Calculates the number of characters before the first CRLF pair in array, or
+* -1 if the array contains no CRLF pair.
+*
+* @param array : Array
+* an array of numbers in the range [0, 256), each representing a single
+* character; the first CRLF is the lowest index i where
+* |array[i] == "\r".charCodeAt(0)| and |array[i+1] == "\n".charCodeAt(0)|,
+* if such an |i| exists, and -1 otherwise
+* @returns int
+* the index of the first CRLF if any were present, -1 otherwise
+*/
+function findCRLF(array)
+{
+ for (var i = array.indexOf(CR); i >= 0; i = array.indexOf(CR, i + 1))
+ {
+ if (array[i + 1] == LF)
+ return i;
+ }
+ return -1;
+}
+
+
+/**
+* A container which provides line-by-line access to the arrays of bytes with
+* which it is seeded.
+*/
+function LineData()
+{
+ /** An array of queued bytes from which to get line-based characters. */
+ this._data = [];
+}
+LineData.prototype =
+{
+ /**
+* Appends the bytes in the given array to the internal data cache maintained
+* by this.
+*/
+ appendBytes: function(bytes)
+ {
+ Array.prototype.push.apply(this._data, bytes);
+ },
+
+ /**
+* Removes and returns a line of data, delimited by CRLF, from this.
+*
+* @param out
+* an object whose "value" property will be set to the first line of text
+* present in this, sans CRLF, if this contains a full CRLF-delimited line
+* of text; if this doesn't contain enough data, the value of the property
+* is undefined
+* @returns boolean
+* true if a full line of data could be read from the data in this, false
+* otherwise
+*/
+ readLine: function(out)
+ {
+ var data = this._data;
+ var length = findCRLF(data);
+ if (length < 0)
+ return false;
+
+ //
+ // We have the index of the CR, so remove all the characters, including
+ // CRLF, from the array with splice, and convert the removed array into the
+ // corresponding string, from which we then strip the trailing CRLF.
+ //
+ // Getting the line in this matter acknowledges that substring is an O(1)
+ // operation in SpiderMonkey because strings are immutable, whereas two
+ // splices, both from the beginning of the data, are less likely to be as
+ // cheap as a single splice plus two extra character conversions.
+ //
+ var line = String.fromCharCode.apply(null, data.splice(0, length + 2));
+ out.value = line.substring(0, length);
+
+ return true;
+ },
+
+ /**
+* Removes the bytes currently within this and returns them in an array.
+*
+* @returns Array
+* the bytes within this when this method is called
+*/
+ purge: function()
+ {
+ var data = this._data;
+ this._data = [];
+ return data;
+ }
+};
+
+
+
+/**
+* Creates a request-handling function for an nsIHttpRequestHandler object.
+*/
+function createHandlerFunc(handler)
+{
+ return function(metadata, response) { handler.handle(metadata, response); };
+}
+
+
+/**
+* The default handler for directories; writes an HTML response containing a
+* slightly-formatted directory listing.
+*/
+function defaultIndexHandler(metadata, response)
+{
+ response.setHeader("Content-Type", "text/html", false);
+
+ var path = htmlEscape(decodeURI(metadata.path));
+
+ //
+ // Just do a very basic bit of directory listings -- no need for too much
+ // fanciness, especially since we don't have a style sheet in which we can
+ // stick rules (don't want to pollute the default path-space).
+ //
+
+ var body = '<html>\
+<head>\
+<title>' + path + '</title>\
+</head>\
+<body>\
+<h1>' + path + '</h1>\
+<ol style="list-style-type: none">';
+
+ var directory = metadata.getProperty("directory").QueryInterface(Ci.nsILocalFile);
+ NS_ASSERT(directory && directory.isDirectory());
+
+ var fileList = [];
+ var files = directory.directoryEntries;
+ while (files.hasMoreElements())
+ {
+ var f = files.getNext().QueryInterface(Ci.nsIFile);
+ var name = f.leafName;
+ if (!f.isHidden() &&
+ (name.charAt(name.length - 1) != HIDDEN_CHAR ||
+ name.charAt(name.length - 2) == HIDDEN_CHAR))
+ fileList.push(f);
+ }
+
+ fileList.sort(fileSort);
+
+ for (var i = 0; i < fileList.length; i++)
+ {
+ var file = fileList[i];
+ try
+ {
+ var name = file.leafName;
+ if (name.charAt(name.length - 1) == HIDDEN_CHAR)
+ name = name.substring(0, name.length - 1);
+ var sep = file.isDirectory() ? "/" : "";
+
+ // Note: using " to delimit the attribute here because encodeURIComponent
+ // passes through '.
+ var item = '<li><a href="' + encodeURIComponent(name) + sep + '">' +
+ htmlEscape(name) + sep +
+ '</a></li>';
+
+ body += item;
+ }
+ catch (e) { /* some file system error, ignore the file */ }
+ }
+
+ body += ' </ol>\
+</body>\
+</html>';
+
+ response.bodyOutputStream.write(body, body.length);
+}
+
+/**
+* Sorts a and b (nsIFile objects) into an aesthetically pleasing order.
+*/
+function fileSort(a, b)
+{
+ var dira = a.isDirectory(), dirb = b.isDirectory();
+
+ if (dira && !dirb)
+ return -1;
+ if (dirb && !dira)
+ return 1;
+
+ var namea = a.leafName.toLowerCase(), nameb = b.leafName.toLowerCase();
+ return nameb > namea ? -1 : 1;
+}
+
+
+/**
+* Converts an externally-provided path into an internal path for use in
+* determining file mappings.
+*
+* @param path
+* the path to convert
+* @param encoded
+* true if the given path should be passed through decodeURI prior to
+* conversion
+* @throws URIError
+* if path is incorrectly encoded
+*/
+function toInternalPath(path, encoded)
+{
+ if (encoded)
+ path = decodeURI(path);
+
+ var comps = path.split("/");
+ for (var i = 0, sz = comps.length; i < sz; i++)
+ {
+ var comp = comps[i];
+ if (comp.charAt(comp.length - 1) == HIDDEN_CHAR)
+ comps[i] = comp + HIDDEN_CHAR;
+ }
+ return comps.join("/");
+}
+
+
+/**
+* Adds custom-specified headers for the given file to the given response, if
+* any such headers are specified.
+*
+* @param file
+* the file on the disk which is to be written
+* @param metadata
+* metadata about the incoming request
+* @param response
+* the Response to which any specified headers/data should be written
+* @throws HTTP_500
+* if an error occurred while processing custom-specified headers
+*/
+function maybeAddHeaders(file, metadata, response)
+{
+ var name = file.leafName;
+ if (name.charAt(name.length - 1) == HIDDEN_CHAR)
+ name = name.substring(0, name.length - 1);
+
+ var headerFile = file.parent;
+ headerFile.append(name + HEADERS_SUFFIX);
+
+ if (!headerFile.exists())
+ return;
+
+ const PR_RDONLY = 0x01;
+ var fis = new FileInputStream(headerFile, PR_RDONLY, 0o444,
+ Ci.nsIFileInputStream.CLOSE_ON_EOF);
+
+ try
+ {
+ var lis = new ConverterInputStream(fis, "UTF-8", 1024, 0x0);
+ lis.QueryInterface(Ci.nsIUnicharLineInputStream);
+
+ var line = {value: ""};
+ var more = lis.readLine(line);
+
+ if (!more && line.value == "")
+ return;
+
+
+ // request line
+
+ var status = line.value;
+ if (status.indexOf("HTTP ") == 0)
+ {
+ status = status.substring(5);
+ var space = status.indexOf(" ");
+ var code, description;
+ if (space < 0)
+ {
+ code = status;
+ description = "";
+ }
+ else
+ {
+ code = status.substring(0, space);
+ description = status.substring(space + 1, status.length);
+ }
+
+ response.setStatusLine(metadata.httpVersion, parseInt(code, 10), description);
+
+ line.value = "";
+ more = lis.readLine(line);
+ }
+
+ // headers
+ while (more || line.value != "")
+ {
+ var header = line.value;
+ var colon = header.indexOf(":");
+
+ response.setHeader(header.substring(0, colon),
+ header.substring(colon + 1, header.length),
+ false); // allow overriding server-set headers
+
+ line.value = "";
+ more = lis.readLine(line);
+ }
+ }
+ catch (e)
+ {
+ dumpn("WARNING: error in headers for " + metadata.path + ": " + e);
+ throw HTTP_500;
+ }
+ finally
+ {
+ fis.close();
+ }
+}
+
+
+/**
+* An object which handles requests for a server, executing default and
+* overridden behaviors as instructed by the code which uses and manipulates it.
+* Default behavior includes the paths / and /trace (diagnostics), with some
+* support for HTTP error pages for various codes and fallback to HTTP 500 if
+* those codes fail for any reason.
+*
+* @param server : nsHttpServer
+* the server in which this handler is being used
+*/
+function ServerHandler(server)
+{
+ // FIELDS
+
+ /**
+* The nsHttpServer instance associated with this handler.
+*/
+ this._server = server;
+
+ /**
+* A FileMap object containing the set of path->nsILocalFile mappings for
+* all directory mappings set in the server (e.g., "/" for /var/www/html/,
+* "/foo/bar/" for /local/path/, and "/foo/bar/baz/" for /local/path2).
+*
+* Note carefully: the leading and trailing "/" in each path (not file) are
+* removed before insertion to simplify the code which uses this. You have
+* been warned!
+*/
+ this._pathDirectoryMap = new FileMap();
+
+ /**
+* Custom request handlers for the server in which this resides. Path-handler
+* pairs are stored as property-value pairs in this property.
+*
+* @see ServerHandler.prototype._defaultPaths
+*/
+ this._overridePaths = {};
+
+ /**
+* Custom request handlers for the server in which this resides. Prefix-handler
+* pairs are stored as property-value pairs in this property.
+*/
+ this._overridePrefixes = {};
+
+ /**
+* Custom request handlers for the error handlers in the server in which this
+* resides. Path-handler pairs are stored as property-value pairs in this
+* property.
+*
+* @see ServerHandler.prototype._defaultErrors
+*/
+ this._overrideErrors = {};
+
+ /**
+* Maps file extensions to their MIME types in the server, overriding any
+* mapping that might or might not exist in the MIME service.
+*/
+ this._mimeMappings = {};
+
+ /**
+* The default handler for requests for directories, used to serve directories
+* when no index file is present.
+*/
+ this._indexHandler = defaultIndexHandler;
+
+ /** Per-path state storage for the server. */
+ this._state = {};
+
+ /** Entire-server state storage. */
+ this._sharedState = {};
+
+ /** Entire-server state storage for nsISupports values. */
+ this._objectState = {};
+}
+ServerHandler.prototype =
+{
+ // PUBLIC API
+
+ /**
+* Handles a request to this server, responding to the request appropriately
+* and initiating server shutdown if necessary.
+*
+* This method never throws an exception.
+*
+* @param connection : Connection
+* the connection for this request
+*/
+ handleResponse: function(connection)
+ {
+ var request = connection.request;
+ var response = new Response(connection);
+
+ var path = request.path;
+ dumpn("*** path == " + path);
+
+ try
+ {
+ try
+ {
+ if (path in this._overridePaths)
+ {
+ // explicit paths first, then files based on existing directory mappings,
+ // then (if the file doesn't exist) built-in server default paths
+ dumpn("calling override for " + path);
+ this._overridePaths[path](request, response);
+ }
+ else
+ {
+ let longestPrefix = "";
+ for (let prefix in this._overridePrefixes)
+ {
+ if (prefix.length > longestPrefix.length && path.startsWith(prefix))
+ {
+ longestPrefix = prefix;
+ }
+ }
+ if (longestPrefix.length > 0)
+ {
+ dumpn("calling prefix override for " + longestPrefix);
+ this._overridePrefixes[longestPrefix](request, response);
+ }
+ else
+ {
+ this._handleDefault(request, response);
+ }
+ }
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort(e);
+ return;
+ }
+
+ if (!(e instanceof HttpError))
+ {
+ dumpn("*** unexpected error: e == " + e);
+ throw HTTP_500;
+ }
+ if (e.code !== 404)
+ throw e;
+
+ dumpn("*** default: " + (path in this._defaultPaths));
+
+ response = new Response(connection);
+ if (path in this._defaultPaths)
+ this._defaultPaths[path](request, response);
+ else
+ throw HTTP_404;
+ }
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort(e);
+ return;
+ }
+
+ var errorCode = "internal";
+
+ try
+ {
+ if (!(e instanceof HttpError))
+ throw e;
+
+ errorCode = e.code;
+ dumpn("*** errorCode == " + errorCode);
+
+ response = new Response(connection);
+ if (e.customErrorHandling)
+ e.customErrorHandling(response);
+ this._handleError(errorCode, request, response);
+ return;
+ }
+ catch (e2)
+ {
+ dumpn("*** error handling " + errorCode + " error: " +
+ "e2 == " + e2 + ", shutting down server");
+
+ connection.server._requestQuit();
+ response.abort(e2);
+ return;
+ }
+ }
+
+ response.complete();
+ },
+
+ //
+ // see nsIHttpServer.registerFile
+ //
+ registerFile: function(path, file)
+ {
+ if (!file)
+ {
+ dumpn("*** unregistering '" + path + "' mapping");
+ delete this._overridePaths[path];
+ return;
+ }
+
+ dumpn("*** registering '" + path + "' as mapping to " + file.path);
+ file = file.clone();
+
+ var self = this;
+ this._overridePaths[path] =
+ function(request, response)
+ {
+ if (!file.exists())
+ throw HTTP_404;
+
+ response.setStatusLine(request.httpVersion, 200, "OK");
+ self._writeFileResponse(request, file, response, 0, file.fileSize);
+ };
+ },
+
+ //
+ // see nsIHttpServer.registerPathHandler
+ //
+ registerPathHandler: function(path, handler)
+ {
+ // XXX true path validation!
+ if (path.charAt(0) != "/")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ this._handlerToField(handler, this._overridePaths, path);
+ },
+
+ //
+ // see nsIHttpServer.registerPrefixHandler
+ //
+ registerPrefixHandler: function(prefix, handler)
+ {
+ // XXX true prefix validation!
+ if (!(prefix.startsWith("/") && prefix.endsWith("/")))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ this._handlerToField(handler, this._overridePrefixes, prefix);
+ },
+
+ //
+ // see nsIHttpServer.registerDirectory
+ //
+ registerDirectory: function(path, directory)
+ {
+ // strip off leading and trailing '/' so that we can use lastIndexOf when
+ // determining exactly how a path maps onto a mapped directory --
+ // conditional is required here to deal with "/".substring(1, 0) being
+ // converted to "/".substring(0, 1) per the JS specification
+ var key = path.length == 1 ? "" : path.substring(1, path.length - 1);
+
+ // the path-to-directory mapping code requires that the first character not
+ // be "/", or it will go into an infinite loop
+ if (key.charAt(0) == "/")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ key = toInternalPath(key, false);
+
+ if (directory)
+ {
+ dumpn("*** mapping '" + path + "' to the location " + directory.path);
+ this._pathDirectoryMap.put(key, directory);
+ }
+ else
+ {
+ dumpn("*** removing mapping for '" + path + "'");
+ this._pathDirectoryMap.put(key, null);
+ }
+ },
+
+ //
+ // see nsIHttpServer.registerErrorHandler
+ //
+ registerErrorHandler: function(err, handler)
+ {
+ if (!(err in HTTP_ERROR_CODES))
+ dumpn("*** WARNING: registering non-HTTP/1.1 error code " +
+ "(" + err + ") handler -- was this intentional?");
+
+ this._handlerToField(handler, this._overrideErrors, err);
+ },
+
+ //
+ // see nsIHttpServer.setIndexHandler
+ //
+ setIndexHandler: function(handler)
+ {
+ if (!handler)
+ handler = defaultIndexHandler;
+ else if (typeof(handler) != "function")
+ handler = createHandlerFunc(handler);
+
+ this._indexHandler = handler;
+ },
+
+ //
+ // see nsIHttpServer.registerContentType
+ //
+ registerContentType: function(ext, type)
+ {
+ if (!type)
+ delete this._mimeMappings[ext];
+ else
+ this._mimeMappings[ext] = headerUtils.normalizeFieldValue(type);
+ },
+
+ // PRIVATE API
+
+ /**
+* Sets or remove (if handler is null) a handler in an object with a key.
+*
+* @param handler
+* a handler, either function or an nsIHttpRequestHandler
+* @param dict
+* The object to attach the handler to.
+* @param key
+* The field name of the handler.
+*/
+ _handlerToField: function(handler, dict, key)
+ {
+ // for convenience, handler can be a function if this is run from xpcshell
+ if (typeof(handler) == "function")
+ dict[key] = handler;
+ else if (handler)
+ dict[key] = createHandlerFunc(handler);
+ else
+ delete dict[key];
+ },
+
+ /**
+* Handles a request which maps to a file in the local filesystem (if a base
+* path has already been set; otherwise the 404 error is thrown).
+*
+* @param metadata : Request
+* metadata for the incoming request
+* @param response : Response
+* an uninitialized Response to the given request, to be initialized by a
+* request handler
+* @throws HTTP_###
+* if an HTTP error occurred (usually HTTP_404); note that in this case the
+* calling code must handle post-processing of the response
+*/
+ _handleDefault: function(metadata, response)
+ {
+ dumpn("*** _handleDefault()");
+
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+
+ var path = metadata.path;
+ NS_ASSERT(path.charAt(0) == "/", "invalid path: <" + path + ">");
+
+ // determine the actual on-disk file; this requires finding the deepest
+ // path-to-directory mapping in the requested URL
+ var file = this._getFileForPath(path);
+
+ // the "file" might be a directory, in which case we either serve the
+ // contained index.html or make the index handler write the response
+ if (file.exists() && file.isDirectory())
+ {
+ file.append("index.html"); // make configurable?
+ if (!file.exists() || file.isDirectory())
+ {
+ metadata._ensurePropertyBag();
+ metadata._bag.setPropertyAsInterface("directory", file.parent);
+ this._indexHandler(metadata, response);
+ return;
+ }
+ }
+
+ // alternately, the file might not exist
+ if (!file.exists())
+ throw HTTP_404;
+
+ var start, end;
+ if (metadata._httpVersion.atLeast(nsHttpVersion.HTTP_1_1) &&
+ metadata.hasHeader("Range") &&
+ this._getTypeFromFile(file) !== SJS_TYPE)
+ {
+ var rangeMatch = metadata.getHeader("Range").match(/^bytes=(\d+)?-(\d+)?$/);
+ if (!rangeMatch)
+ throw HTTP_400;
+
+ if (rangeMatch[1] !== undefined)
+ start = parseInt(rangeMatch[1], 10);
+
+ if (rangeMatch[2] !== undefined)
+ end = parseInt(rangeMatch[2], 10);
+
+ if (start === undefined && end === undefined)
+ throw HTTP_400;
+
+ // No start given, so the end is really the count of bytes from the
+ // end of the file.
+ if (start === undefined)
+ {
+ start = Math.max(0, file.fileSize - end);
+ end = file.fileSize - 1;
+ }
+
+ // start and end are inclusive
+ if (end === undefined || end >= file.fileSize)
+ end = file.fileSize - 1;
+
+ if (start !== undefined && start >= file.fileSize) {
+ var HTTP_416 = new HttpError(416, "Requested Range Not Satisfiable");
+ HTTP_416.customErrorHandling = function(errorResponse)
+ {
+ maybeAddHeaders(file, metadata, errorResponse);
+ };
+ throw HTTP_416;
+ }
+
+ if (end < start)
+ {
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ start = 0;
+ end = file.fileSize - 1;
+ }
+ else
+ {
+ response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
+ var contentRange = "bytes " + start + "-" + end + "/" + file.fileSize;
+ response.setHeader("Content-Range", contentRange);
+ }
+ }
+ else
+ {
+ start = 0;
+ end = file.fileSize - 1;
+ }
+
+ // finally...
+ dumpn("*** handling '" + path + "' as mapping to " + file.path + " from " +
+ start + " to " + end + " inclusive");
+ this._writeFileResponse(metadata, file, response, start, end - start + 1);
+ },
+
+ /**
+* Writes an HTTP response for the given file, including setting headers for
+* file metadata.
+*
+* @param metadata : Request
+* the Request for which a response is being generated
+* @param file : nsILocalFile
+* the file which is to be sent in the response
+* @param response : Response
+* the response to which the file should be written
+* @param offset: uint
+* the byte offset to skip to when writing
+* @param count: uint
+* the number of bytes to write
+*/
+ _writeFileResponse: function(metadata, file, response, offset, count)
+ {
+ const PR_RDONLY = 0x01;
+
+ var type = this._getTypeFromFile(file);
+ if (type === SJS_TYPE)
+ {
+ var fis = new FileInputStream(file, PR_RDONLY, 0o444,
+ Ci.nsIFileInputStream.CLOSE_ON_EOF);
+
+ try
+ {
+ var sis = new ScriptableInputStream(fis);
+ var s = Cu.Sandbox(gGlobalObject);
+ s.importFunction(dump, "dump");
+
+ // Define a basic key-value state-preservation API across requests, with
+ // keys initially corresponding to the empty string.
+ var self = this;
+ var path = metadata.path;
+ s.importFunction(function getState(k)
+ {
+ return self._getState(path, k);
+ });
+ s.importFunction(function setState(k, v)
+ {
+ self._setState(path, k, v);
+ });
+ s.importFunction(function getSharedState(k)
+ {
+ return self._getSharedState(k);
+ });
+ s.importFunction(function setSharedState(k, v)
+ {
+ self._setSharedState(k, v);
+ });
+ s.importFunction(function getObjectState(k, callback)
+ {
+ callback(self._getObjectState(k));
+ });
+ s.importFunction(function setObjectState(k, v)
+ {
+ self._setObjectState(k, v);
+ });
+ s.importFunction(function registerPathHandler(p, h)
+ {
+ self.registerPathHandler(p, h);
+ });
+
+ // Make it possible for sjs files to access their location
+ this._setState(path, "__LOCATION__", file.path);
+
+ try
+ {
+ // Alas, the line number in errors dumped to console when calling the
+ // request handler is simply an offset from where we load the SJS file.
+ // Work around this in a reasonably non-fragile way by dynamically
+ // getting the line number where we evaluate the SJS file. Don't
+ // separate these two lines!
+ var line = new Error().lineNumber;
+ Cu.evalInSandbox(sis.read(file.fileSize), s);
+ }
+ catch (e)
+ {
+ dumpn("*** syntax error in SJS at " + file.path + ": " + e);
+ throw HTTP_500;
+ }
+
+ try
+ {
+ s.handleRequest(metadata, response);
+ }
+ catch (e)
+ {
+ dump("*** error running SJS at " + file.path + ": " +
+ e + " on line " +
+ (e instanceof Error
+ ? e.lineNumber + " in httpd.js"
+ : (e.lineNumber - line)) + "\n");
+ throw HTTP_500;
+ }
+ }
+ finally
+ {
+ fis.close();
+ }
+ }
+ else
+ {
+ try
+ {
+ response.setHeader("Last-Modified",
+ toDateString(file.lastModifiedTime),
+ false);
+ }
+ catch (e) { /* lastModifiedTime threw, ignore */ }
+
+ response.setHeader("Content-Type", type, false);
+ maybeAddHeaders(file, metadata, response);
+ response.setHeader("Content-Length", "" + count, false);
+
+ var fis = new FileInputStream(file, PR_RDONLY, 0o444,
+ Ci.nsIFileInputStream.CLOSE_ON_EOF);
+
+ offset = offset || 0;
+ count = count || file.fileSize;
+ NS_ASSERT(offset === 0 || offset < file.fileSize, "bad offset");
+ NS_ASSERT(count >= 0, "bad count");
+ NS_ASSERT(offset + count <= file.fileSize, "bad total data size");
+
+ try
+ {
+ if (offset !== 0)
+ {
+ // Seek (or read, if seeking isn't supported) to the correct offset so
+ // the data sent to the client matches the requested range.
+ if (fis instanceof Ci.nsISeekableStream)
+ fis.seek(Ci.nsISeekableStream.NS_SEEK_SET, offset);
+ else
+ new ScriptableInputStream(fis).read(offset);
+ }
+ }
+ catch (e)
+ {
+ fis.close();
+ throw e;
+ }
+
+ let writeMore = function writeMore()
+ {
+ gThreadManager.currentThread
+ .dispatch(writeData, Ci.nsIThread.DISPATCH_NORMAL);
+ }
+
+ var input = new BinaryInputStream(fis);
+ var output = new BinaryOutputStream(response.bodyOutputStream);
+ var writeData =
+ {
+ run: function()
+ {
+ var chunkSize = Math.min(65536, count);
+ count -= chunkSize;
+ NS_ASSERT(count >= 0, "underflow");
+
+ try
+ {
+ var data = input.readByteArray(chunkSize);
+ NS_ASSERT(data.length === chunkSize,
+ "incorrect data returned? got " + data.length +
+ ", expected " + chunkSize);
+ output.writeByteArray(data, data.length);
+ if (count === 0)
+ {
+ fis.close();
+ response.finish();
+ }
+ else
+ {
+ writeMore();
+ }
+ }
+ catch (e)
+ {
+ try
+ {
+ fis.close();
+ }
+ finally
+ {
+ response.finish();
+ }
+ throw e;
+ }
+ }
+ };
+
+ writeMore();
+
+ // Now that we know copying will start, flag the response as async.
+ response.processAsync();
+ }
+ },
+
+ /**
+* Get the value corresponding to a given key for the given path for SJS state
+* preservation across requests.
+*
+* @param path : string
+* the path from which the given state is to be retrieved
+* @param k : string
+* the key whose corresponding value is to be returned
+* @returns string
+* the corresponding value, which is initially the empty string
+*/
+ _getState: function(path, k)
+ {
+ var state = this._state;
+ if (path in state && k in state[path])
+ return state[path][k];
+ return "";
+ },
+
+ /**
+* Set the value corresponding to a given key for the given path for SJS state
+* preservation across requests.
+*
+* @param path : string
+* the path from which the given state is to be retrieved
+* @param k : string
+* the key whose corresponding value is to be set
+* @param v : string
+* the value to be set
+*/
+ _setState: function(path, k, v)
+ {
+ if (typeof v !== "string")
+ throw new Error("non-string value passed");
+ var state = this._state;
+ if (!(path in state))
+ state[path] = {};
+ state[path][k] = v;
+ },
+
+ /**
+* Get the value corresponding to a given key for SJS state preservation
+* across requests.
+*
+* @param k : string
+* the key whose corresponding value is to be returned
+* @returns string
+* the corresponding value, which is initially the empty string
+*/
+ _getSharedState: function(k)
+ {
+ var state = this._sharedState;
+ if (k in state)
+ return state[k];
+ return "";
+ },
+
+ /**
+* Set the value corresponding to a given key for SJS state preservation
+* across requests.
+*
+* @param k : string
+* the key whose corresponding value is to be set
+* @param v : string
+* the value to be set
+*/
+ _setSharedState: function(k, v)
+ {
+ if (typeof v !== "string")
+ throw new Error("non-string value passed");
+ this._sharedState[k] = v;
+ },
+
+ /**
+* Returns the object associated with the given key in the server for SJS
+* state preservation across requests.
+*
+* @param k : string
+* the key whose corresponding object is to be returned
+* @returns nsISupports
+* the corresponding object, or null if none was present
+*/
+ _getObjectState: function(k)
+ {
+ if (typeof k !== "string")
+ throw new Error("non-string key passed");
+ return this._objectState[k] || null;
+ },
+
+ /**
+* Sets the object associated with the given key in the server for SJS
+* state preservation across requests.
+*
+* @param k : string
+* the key whose corresponding object is to be set
+* @param v : nsISupports
+* the object to be associated with the given key; may be null
+*/
+ _setObjectState: function(k, v)
+ {
+ if (typeof k !== "string")
+ throw new Error("non-string key passed");
+ if (typeof v !== "object")
+ throw new Error("non-object value passed");
+ if (v && !("QueryInterface" in v))
+ {
+ throw new Error("must pass an nsISupports; use wrappedJSObject to ease " +
+ "pain when using the server from JS");
+ }
+
+ this._objectState[k] = v;
+ },
+
+ /**
+* Gets a content-type for the given file, first by checking for any custom
+* MIME-types registered with this handler for the file's extension, second by
+* asking the global MIME service for a content-type, and finally by failing
+* over to application/octet-stream.
+*
+* @param file : nsIFile
+* the nsIFile for which to get a file type
+* @returns string
+* the best content-type which can be determined for the file
+*/
+ _getTypeFromFile: function(file)
+ {
+ try
+ {
+ var name = file.leafName;
+ var dot = name.lastIndexOf(".");
+ if (dot > 0)
+ {
+ var ext = name.slice(dot + 1);
+ if (ext in this._mimeMappings)
+ return this._mimeMappings[ext];
+ }
+ return Cc["@mozilla.org/uriloader/external-helper-app-service;1"]
+ .getService(Ci.nsIMIMEService)
+ .getTypeFromFile(file);
+ }
+ catch (e)
+ {
+ return "application/octet-stream";
+ }
+ },
+
+ /**
+* Returns the nsILocalFile which corresponds to the path, as determined using
+* all registered path->directory mappings and any paths which are explicitly
+* overridden.
+*
+* @param path : string
+* the server path for which a file should be retrieved, e.g. "/foo/bar"
+* @throws HttpError
+* when the correct action is the corresponding HTTP error (i.e., because no
+* mapping was found for a directory in path, the referenced file doesn't
+* exist, etc.)
+* @returns nsILocalFile
+* the file to be sent as the response to a request for the path
+*/
+ _getFileForPath: function(path)
+ {
+ // decode and add underscores as necessary
+ try
+ {
+ path = toInternalPath(path, true);
+ }
+ catch (e)
+ {
+ throw HTTP_400; // malformed path
+ }
+
+ // next, get the directory which contains this path
+ var pathMap = this._pathDirectoryMap;
+
+ // An example progression of tmp for a path "/foo/bar/baz/" might be:
+ // "foo/bar/baz/", "foo/bar/baz", "foo/bar", "foo", ""
+ var tmp = path.substring(1);
+ while (true)
+ {
+ // do we have a match for current head of the path?
+ var file = pathMap.get(tmp);
+ if (file)
+ {
+ // XXX hack; basically disable showing mapping for /foo/bar/ when the
+ // requested path was /foo/bar, because relative links on the page
+ // will all be incorrect -- we really need the ability to easily
+ // redirect here instead
+ if (tmp == path.substring(1) &&
+ tmp.length != 0 &&
+ tmp.charAt(tmp.length - 1) != "/")
+ file = null;
+ else
+ break;
+ }
+
+ // if we've finished trying all prefixes, exit
+ if (tmp == "")
+ break;
+
+ tmp = tmp.substring(0, tmp.lastIndexOf("/"));
+ }
+
+ // no mapping applies, so 404
+ if (!file)
+ throw HTTP_404;
+
+
+ // last, get the file for the path within the determined directory
+ var parentFolder = file.parent;
+ var dirIsRoot = (parentFolder == null);
+
+ // Strategy here is to append components individually, making sure we
+ // never move above the given directory; this allows paths such as
+ // "<file>/foo/../bar" but prevents paths such as "<file>/../base-sibling";
+ // this component-wise approach also means the code works even on platforms
+ // which don't use "/" as the directory separator, such as Windows
+ var leafPath = path.substring(tmp.length + 1);
+ var comps = leafPath.split("/");
+ for (var i = 0, sz = comps.length; i < sz; i++)
+ {
+ var comp = comps[i];
+
+ if (comp == "..")
+ file = file.parent;
+ else if (comp == "." || comp == "")
+ continue;
+ else
+ file.append(comp);
+
+ if (!dirIsRoot && file.equals(parentFolder))
+ throw HTTP_403;
+ }
+
+ return file;
+ },
+
+ /**
+* Writes the error page for the given HTTP error code over the given
+* connection.
+*
+* @param errorCode : uint
+* the HTTP error code to be used
+* @param connection : Connection
+* the connection on which the error occurred
+*/
+ handleError: function(errorCode, connection)
+ {
+ var response = new Response(connection);
+
+ dumpn("*** error in request: " + errorCode);
+
+ this._handleError(errorCode, new Request(connection.port), response);
+ },
+
+ /**
+* Handles a request which generates the given error code, using the
+* user-defined error handler if one has been set, gracefully falling back to
+* the x00 status code if the code has no handler, and failing to status code
+* 500 if all else fails.
+*
+* @param errorCode : uint
+* the HTTP error which is to be returned
+* @param metadata : Request
+* metadata for the request, which will often be incomplete since this is an
+* error
+* @param response : Response
+* an uninitialized Response should be initialized when this method
+* completes with information which represents the desired error code in the
+* ideal case or a fallback code in abnormal circumstances (i.e., 500 is a
+* fallback for 505, per HTTP specs)
+*/
+ _handleError: function(errorCode, metadata, response)
+ {
+ if (!metadata)
+ throw Cr.NS_ERROR_NULL_POINTER;
+
+ var errorX00 = errorCode - (errorCode % 100);
+
+ try
+ {
+ if (!(errorCode in HTTP_ERROR_CODES))
+ dumpn("*** WARNING: requested invalid error: " + errorCode);
+
+ // RFC 2616 says that we should try to handle an error by its class if we
+ // can't otherwise handle it -- if that fails, we revert to handling it as
+ // a 500 internal server error, and if that fails we throw and shut down
+ // the server
+
+ // actually handle the error
+ try
+ {
+ if (errorCode in this._overrideErrors)
+ this._overrideErrors[errorCode](metadata, response);
+ else
+ this._defaultErrors[errorCode](metadata, response);
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort(e);
+ return;
+ }
+
+ // don't retry the handler that threw
+ if (errorX00 == errorCode)
+ throw HTTP_500;
+
+ dumpn("*** error in handling for error code " + errorCode + ", " +
+ "falling back to " + errorX00 + "...");
+ response = new Response(response._connection);
+ if (errorX00 in this._overrideErrors)
+ this._overrideErrors[errorX00](metadata, response);
+ else if (errorX00 in this._defaultErrors)
+ this._defaultErrors[errorX00](metadata, response);
+ else
+ throw HTTP_500;
+ }
+ }
+ catch (e)
+ {
+ if (response.partiallySent())
+ {
+ response.abort();
+ return;
+ }
+
+ // we've tried everything possible for a meaningful error -- now try 500
+ dumpn("*** error in handling for error code " + errorX00 + ", falling " +
+ "back to 500...");
+
+ try
+ {
+ response = new Response(response._connection);
+ if (500 in this._overrideErrors)
+ this._overrideErrors[500](metadata, response);
+ else
+ this._defaultErrors[500](metadata, response);
+ }
+ catch (e2)
+ {
+ dumpn("*** multiple errors in default error handlers!");
+ dumpn("*** e == " + e + ", e2 == " + e2);
+ response.abort(e2);
+ return;
+ }
+ }
+
+ response.complete();
+ },
+
+ // FIELDS
+
+ /**
+* This object contains the default handlers for the various HTTP error codes.
+*/
+ _defaultErrors:
+ {
+ 400: function(metadata, response)
+ {
+ // none of the data in metadata is reliable, so hard-code everything here
+ response.setStatusLine("1.1", 400, "Bad Request");
+ response.setHeader("Content-Type", "text/plain", false);
+
+ var body = "Bad request\n";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 403: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 403, "Forbidden");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>403 Forbidden</title></head>\
+<body>\
+<h1>403 Forbidden</h1>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 404: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 404, "Not Found");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>404 Not Found</title></head>\
+<body>\
+<h1>404 Not Found</h1>\
+<p>\
+<span style='font-family: monospace;'>" +
+ htmlEscape(metadata.path) +
+ "</span> was not found.\
+</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 416: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion,
+ 416,
+ "Requested Range Not Satisfiable");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head>\
+<title>416 Requested Range Not Satisfiable</title></head>\
+<body>\
+<h1>416 Requested Range Not Satisfiable</h1>\
+<p>The byte range was not valid for the\
+requested resource.\
+</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 500: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion,
+ 500,
+ "Internal Server Error");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>500 Internal Server Error</title></head>\
+<body>\
+<h1>500 Internal Server Error</h1>\
+<p>Something's broken in this server and\
+needs to be fixed.</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 501: function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 501, "Not Implemented");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>501 Not Implemented</title></head>\
+<body>\
+<h1>501 Not Implemented</h1>\
+<p>This server is not (yet) Apache.</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ },
+ 505: function(metadata, response)
+ {
+ response.setStatusLine("1.1", 505, "HTTP Version Not Supported");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>505 HTTP Version Not Supported</title></head>\
+<body>\
+<h1>505 HTTP Version Not Supported</h1>\
+<p>This server only supports HTTP/1.0 and HTTP/1.1\
+connections.</p>\
+</body>\
+</html>";
+ response.bodyOutputStream.write(body, body.length);
+ }
+ },
+
+ /**
+* Contains handlers for the default set of URIs contained in this server.
+*/
+ _defaultPaths:
+ {
+ "/": function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ response.setHeader("Content-Type", "text/html", false);
+
+ var body = "<html>\
+<head><title>httpd.js</title></head>\
+<body>\
+<h1>httpd.js</h1>\
+<p>If you're seeing this page, httpd.js is up and\
+serving requests! Now set a base path and serve some\
+files!</p>\
+</body>\
+</html>";
+
+ response.bodyOutputStream.write(body, body.length);
+ },
+
+ "/trace": function(metadata, response)
+ {
+ response.setStatusLine(metadata.httpVersion, 200, "OK");
+ response.setHeader("Content-Type", "text/plain", false);
+
+ var body = "Request-URI: " +
+ metadata.scheme + "://" + metadata.host + ":" + metadata.port +
+ metadata.path + "\n\n";
+ body += "Request (semantically equivalent, slightly reformatted):\n\n";
+ body += metadata.method + " " + metadata.path;
+
+ if (metadata.queryString)
+ body += "?" + metadata.queryString;
+
+ body += " HTTP/" + metadata.httpVersion + "\r\n";
+
+ var headEnum = metadata.headers;
+ while (headEnum.hasMoreElements())
+ {
+ var fieldName = headEnum.getNext()
+ .QueryInterface(Ci.nsISupportsString)
+ .data;
+ body += fieldName + ": " + metadata.getHeader(fieldName) + "\r\n";
+ }
+
+ response.bodyOutputStream.write(body, body.length);
+ }
+ }
+};
+
+
+/**
+* Maps absolute paths to files on the local file system (as nsILocalFiles).
+*/
+function FileMap()
+{
+ /** Hash which will map paths to nsILocalFiles. */
+ this._map = {};
+}
+FileMap.prototype =
+{
+ // PUBLIC API
+
+ /**
+* Maps key to a clone of the nsILocalFile value if value is non-null;
+* otherwise, removes any extant mapping for key.
+*
+* @param key : string
+* string to which a clone of value is mapped
+* @param value : nsILocalFile
+* the file to map to key, or null to remove a mapping
+*/
+ put: function(key, value)
+ {
+ if (value)
+ this._map[key] = value.clone();
+ else
+ delete this._map[key];
+ },
+
+ /**
+* Returns a clone of the nsILocalFile mapped to key, or null if no such
+* mapping exists.
+*
+* @param key : string
+* key to which the returned file maps
+* @returns nsILocalFile
+* a clone of the mapped file, or null if no mapping exists
+*/
+ get: function(key)
+ {
+ var val = this._map[key];
+ return val ? val.clone() : null;
+ }
+};
+
+
+// Response CONSTANTS
+
+// token = *<any CHAR except CTLs or separators>
+// CHAR = <any US-ASCII character (0-127)>
+// CTL = <any US-ASCII control character (0-31) and DEL (127)>
+// separators = "(" | ")" | "<" | ">" | "@"
+// | "," | ";" | ":" | "\" | <">
+// | "/" | "[" | "]" | "?" | "="
+// | "{" | "}" | SP | HT
+const IS_TOKEN_ARRAY =
+ [0, 0, 0, 0, 0, 0, 0, 0, // 0
+ 0, 0, 0, 0, 0, 0, 0, 0, // 8
+ 0, 0, 0, 0, 0, 0, 0, 0, // 16
+ 0, 0, 0, 0, 0, 0, 0, 0, // 24
+
+ 0, 1, 0, 1, 1, 1, 1, 1, // 32
+ 0, 0, 1, 1, 0, 1, 1, 0, // 40
+ 1, 1, 1, 1, 1, 1, 1, 1, // 48
+ 1, 1, 0, 0, 0, 0, 0, 0, // 56
+
+ 0, 1, 1, 1, 1, 1, 1, 1, // 64
+ 1, 1, 1, 1, 1, 1, 1, 1, // 72
+ 1, 1, 1, 1, 1, 1, 1, 1, // 80
+ 1, 1, 1, 0, 0, 0, 1, 1, // 88
+
+ 1, 1, 1, 1, 1, 1, 1, 1, // 96
+ 1, 1, 1, 1, 1, 1, 1, 1, // 104
+ 1, 1, 1, 1, 1, 1, 1, 1, // 112
+ 1, 1, 1, 0, 1, 0, 1]; // 120
+
+
+/**
+* Determines whether the given character code is a CTL.
+*
+* @param code : uint
+* the character code
+* @returns boolean
+* true if code is a CTL, false otherwise
+*/
+function isCTL(code)
+{
+ return (code >= 0 && code <= 31) || (code == 127);
+}
+
+/**
+* Represents a response to an HTTP request, encapsulating all details of that
+* response. This includes all headers, the HTTP version, status code and
+* explanation, and the entity itself.
+*
+* @param connection : Connection
+* the connection over which this response is to be written
+*/
+function Response(connection)
+{
+ /** The connection over which this response will be written. */
+ this._connection = connection;
+
+ /**
+* The HTTP version of this response; defaults to 1.1 if not set by the
+* handler.
+*/
+ this._httpVersion = nsHttpVersion.HTTP_1_1;
+
+ /**
+* The HTTP code of this response; defaults to 200.
+*/
+ this._httpCode = 200;
+
+ /**
+* The description of the HTTP code in this response; defaults to "OK".
+*/
+ this._httpDescription = "OK";
+
+ /**
+* An nsIHttpHeaders object in which the headers in this response should be
+* stored. This property is null after the status line and headers have been
+* written to the network, and it may be modified up until it is cleared,
+* except if this._finished is set first (in which case headers are written
+* asynchronously in response to a finish() call not preceded by
+* flushHeaders()).
+*/
+ this._headers = new nsHttpHeaders();
+
+ /**
+* Set to true when this response is ended (completely constructed if possible
+* and the connection closed); further actions on this will then fail.
+*/
+ this._ended = false;
+
+ /**
+* A stream used to hold data written to the body of this response.
+*/
+ this._bodyOutputStream = null;
+
+ /**
+* A stream containing all data that has been written to the body of this
+* response so far. (Async handlers make the data contained in this
+* unreliable as a way of determining content length in general, but auxiliary
+* saved information can sometimes be used to guarantee reliability.)
+*/
+ this._bodyInputStream = null;
+
+ /**
+* A stream copier which copies data to the network. It is initially null
+* until replaced with a copier for response headers; when headers have been
+* fully sent it is replaced with a copier for the response body, remaining
+* so for the duration of response processing.
+*/
+ this._asyncCopier = null;
+
+ /**
+* True if this response has been designated as being processed
+* asynchronously rather than for the duration of a single call to
+* nsIHttpRequestHandler.handle.
+*/
+ this._processAsync = false;
+
+ /**
+* True iff finish() has been called on this, signaling that no more changes
+* to this may be made.
+*/
+ this._finished = false;
+
+ /**
+* True iff powerSeized() has been called on this, signaling that this
+* response is to be handled manually by the response handler (which may then
+* send arbitrary data in response, even non-HTTP responses).
+*/
+ this._powerSeized = false;
+}
+Response.prototype =
+{
+ // PUBLIC CONSTRUCTION API
+
+ //
+ // see nsIHttpResponse.bodyOutputStream
+ //
+ get bodyOutputStream()
+ {
+ if (this._finished)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+
+ if (!this._bodyOutputStream)
+ {
+ var pipe = new Pipe(true, false, Response.SEGMENT_SIZE, PR_UINT32_MAX,
+ null);
+ this._bodyOutputStream = pipe.outputStream;
+ this._bodyInputStream = pipe.inputStream;
+ if (this._processAsync || this._powerSeized)
+ this._startAsyncProcessor();
+ }
+
+ return this._bodyOutputStream;
+ },
+
+ //
+ // see nsIHttpResponse.write
+ //
+ write: function(data)
+ {
+ if (this._finished)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+
+ var dataAsString = String(data);
+ this.bodyOutputStream.write(dataAsString, dataAsString.length);
+ },
+
+ //
+ // see nsIHttpResponse.setStatusLine
+ //
+ setStatusLine: function(httpVersion, code, description)
+ {
+ if (!this._headers || this._finished || this._powerSeized)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ this._ensureAlive();
+
+ if (!(code >= 0 && code < 1000))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ try
+ {
+ var httpVer;
+ // avoid version construction for the most common cases
+ if (!httpVersion || httpVersion == "1.1")
+ httpVer = nsHttpVersion.HTTP_1_1;
+ else if (httpVersion == "1.0")
+ httpVer = nsHttpVersion.HTTP_1_0;
+ else
+ httpVer = new nsHttpVersion(httpVersion);
+ }
+ catch (e)
+ {
+ throw Cr.NS_ERROR_INVALID_ARG;
+ }
+
+ // Reason-Phrase = *<TEXT, excluding CR, LF>
+ // TEXT = <any OCTET except CTLs, but including LWS>
+ //
+ // XXX this ends up disallowing octets which aren't Unicode, I think -- not
+ // much to do if description is IDL'd as string
+ if (!description)
+ description = "";
+ for (var i = 0; i < description.length; i++)
+ if (isCTL(description.charCodeAt(i)) && description.charAt(i) != "\t")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ // set the values only after validation to preserve atomicity
+ this._httpDescription = description;
+ this._httpCode = code;
+ this._httpVersion = httpVer;
+ },
+
+ //
+ // see nsIHttpResponse.setHeader
+ //
+ setHeader: function(name, value, merge)
+ {
+ if (!this._headers || this._finished || this._powerSeized)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ this._ensureAlive();
+
+ this._headers.setHeader(name, value, merge);
+ },
+
+ //
+ // see nsIHttpResponse.processAsync
+ //
+ processAsync: function()
+ {
+ if (this._finished)
+ throw Cr.NS_ERROR_UNEXPECTED;
+ if (this._powerSeized)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ if (this._processAsync)
+ return;
+ this._ensureAlive();
+
+ dumpn("*** processing connection " + this._connection.number + " async");
+ this._processAsync = true;
+
+ /*
+* Either the bodyOutputStream getter or this method is responsible for
+* starting the asynchronous processor and catching writes of data to the
+* response body of async responses as they happen, for the purpose of
+* forwarding those writes to the actual connection's output stream.
+* If bodyOutputStream is accessed first, calling this method will create
+* the processor (when it first is clear that body data is to be written
+* immediately, not buffered). If this method is called first, accessing
+* bodyOutputStream will create the processor. If only this method is
+* called, we'll write nothing, neither headers nor the nonexistent body,
+* until finish() is called. Since that delay is easily avoided by simply
+* getting bodyOutputStream or calling write(""), we don't worry about it.
+*/
+ if (this._bodyOutputStream && !this._asyncCopier)
+ this._startAsyncProcessor();
+ },
+
+ //
+ // see nsIHttpResponse.seizePower
+ //
+ seizePower: function()
+ {
+ if (this._processAsync)
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ if (this._finished)
+ throw Cr.NS_ERROR_UNEXPECTED;
+ if (this._powerSeized)
+ return;
+ this._ensureAlive();
+
+ dumpn("*** forcefully seizing power over connection " +
+ this._connection.number + "...");
+
+ // Purge any already-written data without sending it. We could as easily
+ // swap out the streams entirely, but that makes it possible to acquire and
+ // unknowingly use a stale reference, so we require there only be one of
+ // each stream ever for any response to avoid this complication.
+ if (this._asyncCopier)
+ this._asyncCopier.cancel(Cr.NS_BINDING_ABORTED);
+ this._asyncCopier = null;
+ if (this._bodyOutputStream)
+ {
+ var input = new BinaryInputStream(this._bodyInputStream);
+ var avail;
+ while ((avail = input.available()) > 0)
+ input.readByteArray(avail);
+ }
+
+ this._powerSeized = true;
+ if (this._bodyOutputStream)
+ this._startAsyncProcessor();
+ },
+
+ //
+ // see nsIHttpResponse.finish
+ //
+ finish: function()
+ {
+ if (!this._processAsync && !this._powerSeized)
+ throw Cr.NS_ERROR_UNEXPECTED;
+ if (this._finished)
+ return;
+
+ dumpn("*** finishing connection " + this._connection.number);
+ this._startAsyncProcessor(); // in case bodyOutputStream was never accessed
+ if (this._bodyOutputStream)
+ this._bodyOutputStream.close();
+ this._finished = true;
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIHttpResponse) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // POST-CONSTRUCTION API (not exposed externally)
+
+ /**
+* The HTTP version number of this, as a string (e.g. "1.1").
+*/
+ get httpVersion()
+ {
+ this._ensureAlive();
+ return this._httpVersion.toString();
+ },
+
+ /**
+* The HTTP status code of this response, as a string of three characters per
+* RFC 2616.
+*/
+ get httpCode()
+ {
+ this._ensureAlive();
+
+ var codeString = (this._httpCode < 10 ? "0" : "") +
+ (this._httpCode < 100 ? "0" : "") +
+ this._httpCode;
+ return codeString;
+ },
+
+ /**
+* The description of the HTTP status code of this response, or "" if none is
+* set.
+*/
+ get httpDescription()
+ {
+ this._ensureAlive();
+
+ return this._httpDescription;
+ },
+
+ /**
+* The headers in this response, as an nsHttpHeaders object.
+*/
+ get headers()
+ {
+ this._ensureAlive();
+
+ return this._headers;
+ },
+
+ //
+ // see nsHttpHeaders.getHeader
+ //
+ getHeader: function(name)
+ {
+ this._ensureAlive();
+
+ return this._headers.getHeader(name);
+ },
+
+ /**
+* Determines whether this response may be abandoned in favor of a newly
+* constructed response. A response may be abandoned only if it is not being
+* sent asynchronously and if raw control over it has not been taken from the
+* server.
+*
+* @returns boolean
+* true iff no data has been written to the network
+*/
+ partiallySent: function()
+ {
+ dumpn("*** partiallySent()");
+ return this._processAsync || this._powerSeized;
+ },
+
+ /**
+* If necessary, kicks off the remaining request processing needed to be done
+* after a request handler performs its initial work upon this response.
+*/
+ complete: function()
+ {
+ dumpn("*** complete()");
+ if (this._processAsync || this._powerSeized)
+ {
+ NS_ASSERT(this._processAsync ^ this._powerSeized,
+ "can't both send async and relinquish power");
+ return;
+ }
+
+ NS_ASSERT(!this.partiallySent(), "completing a partially-sent response?");
+
+ this._startAsyncProcessor();
+
+ // Now make sure we finish processing this request!
+ if (this._bodyOutputStream)
+ this._bodyOutputStream.close();
+ },
+
+ /**
+* Abruptly ends processing of this response, usually due to an error in an
+* incoming request but potentially due to a bad error handler. Since we
+* cannot handle the error in the usual way (giving an HTTP error page in
+* response) because data may already have been sent (or because the response
+* might be expected to have been generated asynchronously or completely from
+* scratch by the handler), we stop processing this response and abruptly
+* close the connection.
+*
+* @param e : Error
+* the exception which precipitated this abort, or null if no such exception
+* was generated
+*/
+ abort: function(e)
+ {
+ dumpn("*** abort(<" + e + ">)");
+
+ // This response will be ended by the processor if one was created.
+ var copier = this._asyncCopier;
+ if (copier)
+ {
+ // We dispatch asynchronously here so that any pending writes of data to
+ // the connection will be deterministically written. This makes it easier
+ // to specify exact behavior, and it makes observable behavior more
+ // predictable for clients. Note that the correctness of this depends on
+ // callbacks in response to _waitToReadData in WriteThroughCopier
+ // happening asynchronously with respect to the actual writing of data to
+ // bodyOutputStream, as they currently do; if they happened synchronously,
+ // an event which ran before this one could write more data to the
+ // response body before we get around to canceling the copier. We have
+ // tests for this in test_seizepower.js, however, and I can't think of a
+ // way to handle both cases without removing bodyOutputStream access and
+ // moving its effective write(data, length) method onto Response, which
+ // would be slower and require more code than this anyway.
+ gThreadManager.currentThread.dispatch({
+ run: function()
+ {
+ dumpn("*** canceling copy asynchronously...");
+ copier.cancel(Cr.NS_ERROR_UNEXPECTED);
+ }
+ }, Ci.nsIThread.DISPATCH_NORMAL);
+ }
+ else
+ {
+ this.end();
+ }
+ },
+
+ /**
+* Closes this response's network connection, marks the response as finished,
+* and notifies the server handler that the request is done being processed.
+*/
+ end: function()
+ {
+ NS_ASSERT(!this._ended, "ending this response twice?!?!");
+
+ this._connection.close();
+ if (this._bodyOutputStream)
+ this._bodyOutputStream.close();
+
+ this._finished = true;
+ this._ended = true;
+ },
+
+ // PRIVATE IMPLEMENTATION
+
+ /**
+* Sends the status line and headers of this response if they haven't been
+* sent and initiates the process of copying data written to this response's
+* body to the network.
+*/
+ _startAsyncProcessor: function()
+ {
+ dumpn("*** _startAsyncProcessor()");
+
+ // Handle cases where we're being called a second time. The former case
+ // happens when this is triggered both by complete() and by processAsync(),
+ // while the latter happens when processAsync() in conjunction with sent
+ // data causes abort() to be called.
+ if (this._asyncCopier || this._ended)
+ {
+ dumpn("*** ignoring second call to _startAsyncProcessor");
+ return;
+ }
+
+ // Send headers if they haven't been sent already and should be sent, then
+ // asynchronously continue to send the body.
+ if (this._headers && !this._powerSeized)
+ {
+ this._sendHeaders();
+ return;
+ }
+
+ this._headers = null;
+ this._sendBody();
+ },
+
+ /**
+* Signals that all modifications to the response status line and headers are
+* complete and then sends that data over the network to the client. Once
+* this method completes, a different response to the request that resulted
+* in this response cannot be sent -- the only possible action in case of
+* error is to abort the response and close the connection.
+*/
+ _sendHeaders: function()
+ {
+ dumpn("*** _sendHeaders()");
+
+ NS_ASSERT(this._headers);
+ NS_ASSERT(!this._powerSeized);
+
+ // request-line
+ var statusLine = "HTTP/" + this.httpVersion + " " +
+ this.httpCode + " " +
+ this.httpDescription + "\r\n";
+
+ // header post-processing
+
+ var headers = this._headers;
+ headers.setHeader("Connection", "close", false);
+ headers.setHeader("Server", "httpd.js", false);
+ if (!headers.hasHeader("Date"))
+ headers.setHeader("Date", toDateString(Date.now()), false);
+
+ // Any response not being processed asynchronously must have an associated
+ // Content-Length header for reasons of backwards compatibility with the
+ // initial server, which fully buffered every response before sending it.
+ // Beyond that, however, it's good to do this anyway because otherwise it's
+ // impossible to test behaviors that depend on the presence or absence of a
+ // Content-Length header.
+ if (!this._processAsync)
+ {
+ dumpn("*** non-async response, set Content-Length");
+
+ var bodyStream = this._bodyInputStream;
+ var avail = bodyStream ? bodyStream.available() : 0;
+
+ // XXX assumes stream will always report the full amount of data available
+ headers.setHeader("Content-Length", "" + avail, false);
+ }
+
+
+ // construct and send response
+ dumpn("*** header post-processing completed, sending response head...");
+
+ // request-line
+ var preambleData = [statusLine];
+
+ // headers
+ var headEnum = headers.enumerator;
+ while (headEnum.hasMoreElements())
+ {
+ var fieldName = headEnum.getNext()
+ .QueryInterface(Ci.nsISupportsString)
+ .data;
+ var values = headers.getHeaderValues(fieldName);
+ for (var i = 0, sz = values.length; i < sz; i++)
+ preambleData.push(fieldName + ": " + values[i] + "\r\n");
+ }
+
+ // end request-line/headers
+ preambleData.push("\r\n");
+
+ var preamble = preambleData.join("");
+
+ var responseHeadPipe = new Pipe(true, false, 0, PR_UINT32_MAX, null);
+ responseHeadPipe.outputStream.write(preamble, preamble.length);
+
+ var response = this;
+ var copyObserver =
+ {
+ onStartRequest: function(request, cx)
+ {
+ dumpn("*** preamble copying started");
+ },
+
+ onStopRequest: function(request, cx, statusCode)
+ {
+ dumpn("*** preamble copying complete " +
+ "[status=0x" + statusCode.toString(16) + "]");
+
+ if (!components.isSuccessCode(statusCode))
+ {
+ dumpn("!!! header copying problems: non-success statusCode, " +
+ "ending response");
+
+ response.end();
+ }
+ else
+ {
+ response._sendBody();
+ }
+ },
+
+ QueryInterface: function(aIID)
+ {
+ if (aIID.equals(Ci.nsIRequestObserver) || aIID.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ }
+ };
+
+ var headerCopier = this._asyncCopier =
+ new WriteThroughCopier(responseHeadPipe.inputStream,
+ this._connection.output,
+ copyObserver, null);
+
+ responseHeadPipe.outputStream.close();
+
+ // Forbid setting any more headers or modifying the request line.
+ this._headers = null;
+ },
+
+ /**
+* Asynchronously writes the body of the response (or the entire response, if
+* seizePower() has been called) to the network.
+*/
+ _sendBody: function()
+ {
+ dumpn("*** _sendBody");
+
+ NS_ASSERT(!this._headers, "still have headers around but sending body?");
+
+ // If no body data was written, we're done
+ if (!this._bodyInputStream)
+ {
+ dumpn("*** empty body, response finished");
+ this.end();
+ return;
+ }
+
+ var response = this;
+ var copyObserver =
+ {
+ onStartRequest: function(request, context)
+ {
+ dumpn("*** onStartRequest");
+ },
+
+ onStopRequest: function(request, cx, statusCode)
+ {
+ dumpn("*** onStopRequest [status=0x" + statusCode.toString(16) + "]");
+
+ if (statusCode === Cr.NS_BINDING_ABORTED)
+ {
+ dumpn("*** terminating copy observer without ending the response");
+ }
+ else
+ {
+ if (!components.isSuccessCode(statusCode))
+ dumpn("*** WARNING: non-success statusCode in onStopRequest");
+
+ response.end();
+ }
+ },
+
+ QueryInterface: function(aIID)
+ {
+ if (aIID.equals(Ci.nsIRequestObserver) || aIID.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ }
+ };
+
+ dumpn("*** starting async copier of body data...");
+ this._asyncCopier =
+ new WriteThroughCopier(this._bodyInputStream, this._connection.output,
+ copyObserver, null);
+ },
+
+ /** Ensures that this hasn't been ended. */
+ _ensureAlive: function()
+ {
+ NS_ASSERT(!this._ended, "not handling response lifetime correctly");
+ }
+};
+
+/**
+* Size of the segments in the buffer used in storing response data and writing
+* it to the socket.
+*/
+Response.SEGMENT_SIZE = 8192;
+
+/** Serves double duty in WriteThroughCopier implementation. */
+function notImplemented()
+{
+ throw Cr.NS_ERROR_NOT_IMPLEMENTED;
+}
+
+/** Returns true iff the given exception represents stream closure. */
+function streamClosed(e)
+{
+ return e === Cr.NS_BASE_STREAM_CLOSED ||
+ (typeof e === "object" && e.result === Cr.NS_BASE_STREAM_CLOSED);
+}
+
+/** Returns true iff the given exception represents a blocked stream. */
+function wouldBlock(e)
+{
+ return e === Cr.NS_BASE_STREAM_WOULD_BLOCK ||
+ (typeof e === "object" && e.result === Cr.NS_BASE_STREAM_WOULD_BLOCK);
+}
+
+/**
+* Copies data from source to sink as it becomes available, when that data can
+* be written to sink without blocking.
+*
+* @param source : nsIAsyncInputStream
+* the stream from which data is to be read
+* @param sink : nsIAsyncOutputStream
+* the stream to which data is to be copied
+* @param observer : nsIRequestObserver
+* an observer which will be notified when the copy starts and finishes
+* @param context : nsISupports
+* context passed to observer when notified of start/stop
+* @throws NS_ERROR_NULL_POINTER
+* if source, sink, or observer are null
+*/
+function WriteThroughCopier(source, sink, observer, context)
+{
+ if (!source || !sink || !observer)
+ throw Cr.NS_ERROR_NULL_POINTER;
+
+ /** Stream from which data is being read. */
+ this._source = source;
+
+ /** Stream to which data is being written. */
+ this._sink = sink;
+
+ /** Observer watching this copy. */
+ this._observer = observer;
+
+ /** Context for the observer watching this. */
+ this._context = context;
+
+ /**
+* True iff this is currently being canceled (cancel has been called, the
+* callback may not yet have been made).
+*/
+ this._canceled = false;
+
+ /**
+* False until all data has been read from input and written to output, at
+* which point this copy is completed and cancel() is asynchronously called.
+*/
+ this._completed = false;
+
+ /** Required by nsIRequest, meaningless. */
+ this.loadFlags = 0;
+ /** Required by nsIRequest, meaningless. */
+ this.loadGroup = null;
+ /** Required by nsIRequest, meaningless. */
+ this.name = "response-body-copy";
+
+ /** Status of this request. */
+ this.status = Cr.NS_OK;
+
+ /** Arrays of byte strings waiting to be written to output. */
+ this._pendingData = [];
+
+ // start copying
+ try
+ {
+ observer.onStartRequest(this, context);
+ this._waitToReadData();
+ this._waitForSinkClosure();
+ }
+ catch (e)
+ {
+ dumpn("!!! error starting copy: " + e +
+ ("lineNumber" in e ? ", line " + e.lineNumber : ""));
+ dumpn(e.stack);
+ this.cancel(Cr.NS_ERROR_UNEXPECTED);
+ }
+}
+WriteThroughCopier.prototype =
+{
+ /* nsISupports implementation */
+
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIInputStreamCallback) ||
+ iid.equals(Ci.nsIOutputStreamCallback) ||
+ iid.equals(Ci.nsIRequest) ||
+ iid.equals(Ci.nsISupports))
+ {
+ return this;
+ }
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // NSIINPUTSTREAMCALLBACK
+
+ /**
+* Receives a more-data-in-input notification and writes the corresponding
+* data to the output.
+*
+* @param input : nsIAsyncInputStream
+* the input stream on whose data we have been waiting
+*/
+ onInputStreamReady: function(input)
+ {
+ if (this._source === null)
+ return;
+
+ dumpn("*** onInputStreamReady");
+
+ //
+ // Ordinarily we'll read a non-zero amount of data from input, queue it up
+ // to be written and then wait for further callbacks. The complications in
+ // this method are the cases where we deviate from that behavior when errors
+ // occur or when copying is drawing to a finish.
+ //
+ // The edge cases when reading data are:
+ //
+ // Zero data is read
+ // If zero data was read, we're at the end of available data, so we can
+ // should stop reading and move on to writing out what we have (or, if
+ // we've already done that, onto notifying of completion).
+ // A stream-closed exception is thrown
+ // This is effectively a less kind version of zero data being read; the
+ // only difference is that we notify of completion with that result
+ // rather than with NS_OK.
+ // Some other exception is thrown
+ // This is the least kind result. We don't know what happened, so we
+ // act as though the stream closed except that we notify of completion
+ // with the result NS_ERROR_UNEXPECTED.
+ //
+
+ var bytesWanted = 0, bytesConsumed = -1;
+ try
+ {
+ input = new BinaryInputStream(input);
+
+ bytesWanted = Math.min(input.available(), Response.SEGMENT_SIZE);
+ dumpn("*** input wanted: " + bytesWanted);
+
+ if (bytesWanted > 0)
+ {
+ var data = input.readByteArray(bytesWanted);
+ bytesConsumed = data.length;
+ this._pendingData.push(String.fromCharCode.apply(String, data));
+ }
+
+ dumpn("*** " + bytesConsumed + " bytes read");
+
+ // Handle the zero-data edge case in the same place as all other edge
+ // cases are handled.
+ if (bytesWanted === 0)
+ throw Cr.NS_BASE_STREAM_CLOSED;
+ }
+ catch (e)
+ {
+ if (streamClosed(e))
+ {
+ dumpn("*** input stream closed");
+ e = bytesWanted === 0 ? Cr.NS_OK : Cr.NS_ERROR_UNEXPECTED;
+ }
+ else
+ {
+ dumpn("!!! unexpected error reading from input, canceling: " + e);
+ e = Cr.NS_ERROR_UNEXPECTED;
+ }
+
+ this._doneReadingSource(e);
+ return;
+ }
+
+ var pendingData = this._pendingData;
+
+ NS_ASSERT(bytesConsumed > 0);
+ NS_ASSERT(pendingData.length > 0, "no pending data somehow?");
+ NS_ASSERT(pendingData[pendingData.length - 1].length > 0,
+ "buffered zero bytes of data?");
+
+ NS_ASSERT(this._source !== null);
+
+ // Reading has gone great, and we've gotten data to write now. What if we
+ // don't have a place to write that data, because output went away just
+ // before this read? Drop everything on the floor, including new data, and
+ // cancel at this point.
+ if (this._sink === null)
+ {
+ pendingData.length = 0;
+ this._doneReadingSource(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // Okay, we've read the data, and we know we have a place to write it. We
+ // need to queue up the data to be written, but *only* if none is queued
+ // already -- if data's already queued, the code that actually writes the
+ // data will make sure to wait on unconsumed pending data.
+ try
+ {
+ if (pendingData.length === 1)
+ this._waitToWriteData();
+ }
+ catch (e)
+ {
+ dumpn("!!! error waiting to write data just read, swallowing and " +
+ "writing only what we already have: " + e);
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // Whee! We successfully read some data, and it's successfully queued up to
+ // be written. All that remains now is to wait for more data to read.
+ try
+ {
+ this._waitToReadData();
+ }
+ catch (e)
+ {
+ dumpn("!!! error waiting to read more data: " + e);
+ this._doneReadingSource(Cr.NS_ERROR_UNEXPECTED);
+ }
+ },
+
+
+ // NSIOUTPUTSTREAMCALLBACK
+
+ /**
+* Callback when data may be written to the output stream without blocking, or
+* when the output stream has been closed.
+*
+* @param output : nsIAsyncOutputStream
+* the output stream on whose writability we've been waiting, also known as
+* this._sink
+*/
+ onOutputStreamReady: function(output)
+ {
+ if (this._sink === null)
+ return;
+
+ dumpn("*** onOutputStreamReady");
+
+ var pendingData = this._pendingData;
+ if (pendingData.length === 0)
+ {
+ // There's no pending data to write. The only way this can happen is if
+ // we're waiting on the output stream's closure, so we can respond to a
+ // copying failure as quickly as possible (rather than waiting for data to
+ // be available to read and then fail to be copied). Therefore, we must
+ // be done now -- don't bother to attempt to write anything and wrap
+ // things up.
+ dumpn("!!! output stream closed prematurely, ending copy");
+
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+
+ NS_ASSERT(pendingData[0].length > 0, "queued up an empty quantum?");
+
+ //
+ // Write out the first pending quantum of data. The possible errors here
+ // are:
+ //
+ // The write might fail because we can't write that much data
+ // Okay, we've written what we can now, so re-queue what's left and
+ // finish writing it out later.
+ // The write failed because the stream was closed
+ // Discard pending data that we can no longer write, stop reading, and
+ // signal that copying finished.
+ // Some other error occurred.
+ // Same as if the stream were closed, but notify with the status
+ // NS_ERROR_UNEXPECTED so the observer knows something was wonky.
+ //
+
+ try
+ {
+ var quantum = pendingData[0];
+
+ // XXX |quantum| isn't guaranteed to be ASCII, so we're relying on
+ // undefined behavior! We're only using this because writeByteArray
+ // is unusably broken for asynchronous output streams; see bug 532834
+ // for details.
+ var bytesWritten = output.write(quantum, quantum.length);
+ if (bytesWritten === quantum.length)
+ pendingData.shift();
+ else
+ pendingData[0] = quantum.substring(bytesWritten);
+
+ dumpn("*** wrote " + bytesWritten + " bytes of data");
+ }
+ catch (e)
+ {
+ if (wouldBlock(e))
+ {
+ NS_ASSERT(pendingData.length > 0,
+ "stream-blocking exception with no data to write?");
+ NS_ASSERT(pendingData[0].length > 0,
+ "stream-blocking exception with empty quantum?");
+ this._waitToWriteData();
+ return;
+ }
+
+ if (streamClosed(e))
+ dumpn("!!! output stream prematurely closed, signaling error...");
+ else
+ dumpn("!!! unknown error: " + e + ", quantum=" + quantum);
+
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // The day is ours! Quantum written, now let's see if we have more data
+ // still to write.
+ try
+ {
+ if (pendingData.length > 0)
+ {
+ this._waitToWriteData();
+ return;
+ }
+ }
+ catch (e)
+ {
+ dumpn("!!! unexpected error waiting to write pending data: " + e);
+ this._doneWritingToSink(Cr.NS_ERROR_UNEXPECTED);
+ return;
+ }
+
+ // Okay, we have no more pending data to write -- but might we get more in
+ // the future?
+ if (this._source !== null)
+ {
+ /*
+* If we might, then wait for the output stream to be closed. (We wait
+* only for closure because we have no data to write -- and if we waited
+* for a specific amount of data, we would get repeatedly notified for no
+* reason if over time the output stream permitted more and more data to
+* be written to it without blocking.)
+*/
+ this._waitForSinkClosure();
+ }
+ else
+ {
+ /*
+* On the other hand, if we can't have more data because the input
+* stream's gone away, then it's time to notify of copy completion.
+* Victory!
+*/
+ this._sink = null;
+ this._cancelOrDispatchCancelCallback(Cr.NS_OK);
+ }
+ },
+
+
+ // NSIREQUEST
+
+ /** Returns true if the cancel observer hasn't been notified yet. */
+ isPending: function()
+ {
+ return !this._completed;
+ },
+
+ /** Not implemented, don't use! */
+ suspend: notImplemented,
+ /** Not implemented, don't use! */
+ resume: notImplemented,
+
+ /**
+* Cancels data reading from input, asynchronously writes out any pending
+* data, and causes the observer to be notified with the given error code when
+* all writing has finished.
+*
+* @param status : nsresult
+* the status to pass to the observer when data copying has been canceled
+*/
+ cancel: function(status)
+ {
+ dumpn("*** cancel(" + status.toString(16) + ")");
+
+ if (this._canceled)
+ {
+ dumpn("*** suppressing a late cancel");
+ return;
+ }
+
+ this._canceled = true;
+ this.status = status;
+
+ // We could be in the middle of absolutely anything at this point. Both
+ // input and output might still be around, we might have pending data to
+ // write, and in general we know nothing about the state of the world. We
+ // therefore must assume everything's in progress and take everything to its
+ // final steady state (or so far as it can go before we need to finish
+ // writing out remaining data).
+
+ this._doneReadingSource(status);
+ },
+
+
+ // PRIVATE IMPLEMENTATION
+
+ /**
+* Stop reading input if we haven't already done so, passing e as the status
+* when closing the stream, and kick off a copy-completion notice if no more
+* data remains to be written.
+*
+* @param e : nsresult
+* the status to be used when closing the input stream
+*/
+ _doneReadingSource: function(e)
+ {
+ dumpn("*** _doneReadingSource(0x" + e.toString(16) + ")");
+
+ this._finishSource(e);
+ if (this._pendingData.length === 0)
+ this._sink = null;
+ else
+ NS_ASSERT(this._sink !== null, "null output?");
+
+ // If we've written out all data read up to this point, then it's time to
+ // signal completion.
+ if (this._sink === null)
+ {
+ NS_ASSERT(this._pendingData.length === 0, "pending data still?");
+ this._cancelOrDispatchCancelCallback(e);
+ }
+ },
+
+ /**
+* Stop writing output if we haven't already done so, discard any data that
+* remained to be sent, close off input if it wasn't already closed, and kick
+* off a copy-completion notice.
+*
+* @param e : nsresult
+* the status to be used when closing input if it wasn't already closed
+*/
+ _doneWritingToSink: function(e)
+ {
+ dumpn("*** _doneWritingToSink(0x" + e.toString(16) + ")");
+
+ this._pendingData.length = 0;
+ this._sink = null;
+ this._doneReadingSource(e);
+ },
+
+ /**
+* Completes processing of this copy: either by canceling the copy if it
+* hasn't already been canceled using the provided status, or by dispatching
+* the cancel callback event (with the originally provided status, of course)
+* if it already has been canceled.
+*
+* @param status : nsresult
+* the status code to use to cancel this, if this hasn't already been
+* canceled
+*/
+ _cancelOrDispatchCancelCallback: function(status)
+ {
+ dumpn("*** _cancelOrDispatchCancelCallback(" + status + ")");
+
+ NS_ASSERT(this._source === null, "should have finished input");
+ NS_ASSERT(this._sink === null, "should have finished output");
+ NS_ASSERT(this._pendingData.length === 0, "should have no pending data");
+
+ if (!this._canceled)
+ {
+ this.cancel(status);
+ return;
+ }
+
+ var self = this;
+ var event =
+ {
+ run: function()
+ {
+ dumpn("*** onStopRequest async callback");
+
+ self._completed = true;
+ try
+ {
+ self._observer.onStopRequest(self, self._context, self.status);
+ }
+ catch (e)
+ {
+ NS_ASSERT(false,
+ "how are we throwing an exception here? we control " +
+ "all the callers! " + e);
+ }
+ }
+ };
+
+ gThreadManager.currentThread.dispatch(event, Ci.nsIThread.DISPATCH_NORMAL);
+ },
+
+ /**
+* Kicks off another wait for more data to be available from the input stream.
+*/
+ _waitToReadData: function()
+ {
+ dumpn("*** _waitToReadData");
+ this._source.asyncWait(this, 0, Response.SEGMENT_SIZE,
+ gThreadManager.mainThread);
+ },
+
+ /**
+* Kicks off another wait until data can be written to the output stream.
+*/
+ _waitToWriteData: function()
+ {
+ dumpn("*** _waitToWriteData");
+
+ var pendingData = this._pendingData;
+ NS_ASSERT(pendingData.length > 0, "no pending data to write?");
+ NS_ASSERT(pendingData[0].length > 0, "buffered an empty write?");
+
+ this._sink.asyncWait(this, 0, pendingData[0].length,
+ gThreadManager.mainThread);
+ },
+
+ /**
+* Kicks off a wait for the sink to which data is being copied to be closed.
+* We wait for stream closure when we don't have any data to be copied, rather
+* than waiting to write a specific amount of data. We can't wait to write
+* data because the sink might be infinitely writable, and if no data appears
+* in the source for a long time we might have to spin quite a bit waiting to
+* write, waiting to write again, &c. Waiting on stream closure instead means
+* we'll get just one notification if the sink dies. Note that when data
+* starts arriving from the sink we'll resume waiting for data to be written,
+* dropping this closure-only callback entirely.
+*/
+ _waitForSinkClosure: function()
+ {
+ dumpn("*** _waitForSinkClosure");
+
+ this._sink.asyncWait(this, Ci.nsIAsyncOutputStream.WAIT_CLOSURE_ONLY, 0,
+ gThreadManager.mainThread);
+ },
+
+ /**
+* Closes input with the given status, if it hasn't already been closed;
+* otherwise a no-op.
+*
+* @param status : nsresult
+* status code use to close the source stream if necessary
+*/
+ _finishSource: function(status)
+ {
+ dumpn("*** _finishSource(" + status.toString(16) + ")");
+
+ if (this._source !== null)
+ {
+ this._source.closeWithStatus(status);
+ this._source = null;
+ }
+ }
+};
+
+
+/**
+* A container for utility functions used with HTTP headers.
+*/
+const headerUtils =
+{
+ /**
+* Normalizes fieldName (by converting it to lowercase) and ensures it is a
+* valid header field name (although not necessarily one specified in RFC
+* 2616).
+*
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not match the field-name production in RFC 2616
+* @returns string
+* fieldName converted to lowercase if it is a valid header, for characters
+* where case conversion is possible
+*/
+ normalizeFieldName: function(fieldName)
+ {
+ if (fieldName == "")
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ for (var i = 0, sz = fieldName.length; i < sz; i++)
+ {
+ if (!IS_TOKEN_ARRAY[fieldName.charCodeAt(i)])
+ {
+ dumpn(fieldName + " is not a valid header field name!");
+ throw Cr.NS_ERROR_INVALID_ARG;
+ }
+ }
+
+ return fieldName.toLowerCase();
+ },
+
+ /**
+* Ensures that fieldValue is a valid header field value (although not
+* necessarily as specified in RFC 2616 if the corresponding field name is
+* part of the HTTP protocol), normalizes the value if it is, and
+* returns the normalized value.
+*
+* @param fieldValue : string
+* a value to be normalized as an HTTP header field value
+* @throws NS_ERROR_INVALID_ARG
+* if fieldValue does not match the field-value production in RFC 2616
+* @returns string
+* fieldValue as a normalized HTTP header field value
+*/
+ normalizeFieldValue: function(fieldValue)
+ {
+ // field-value = *( field-content | LWS )
+ // field-content = <the OCTETs making up the field-value
+ // and consisting of either *TEXT or combinations
+ // of token, separators, and quoted-string>
+ // TEXT = <any OCTET except CTLs,
+ // but including LWS>
+ // LWS = [CRLF] 1*( SP | HT )
+ //
+ // quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
+ // qdtext = <any TEXT except <">>
+ // quoted-pair = "\" CHAR
+ // CHAR = <any US-ASCII character (octets 0 - 127)>
+
+ // Any LWS that occurs between field-content MAY be replaced with a single
+ // SP before interpreting the field value or forwarding the message
+ // downstream (section 4.2); we replace 1*LWS with a single SP
+ var val = fieldValue.replace(/(?:(?:\r\n)?[ \t]+)+/g, " ");
+
+ // remove leading/trailing LWS (which has been converted to SP)
+ val = val.replace(/^ +/, "").replace(/ +$/, "");
+
+ // that should have taken care of all CTLs, so val should contain no CTLs
+ for (var i = 0, len = val.length; i < len; i++)
+ if (isCTL(val.charCodeAt(i)))
+ throw Cr.NS_ERROR_INVALID_ARG;
+
+ // XXX disallows quoted-pair where CHAR is a CTL -- will not invalidly
+ // normalize, however, so this can be construed as a tightening of the
+ // spec and not entirely as a bug
+ return val;
+ }
+};
+
+
+
+/**
+* Converts the given string into a string which is safe for use in an HTML
+* context.
+*
+* @param str : string
+* the string to make HTML-safe
+* @returns string
+* an HTML-safe version of str
+*/
+function htmlEscape(str)
+{
+ // this is naive, but it'll work
+ var s = "";
+ for (var i = 0; i < str.length; i++)
+ s += "&#" + str.charCodeAt(i) + ";";
+ return s;
+}
+
+
+/**
+* Constructs an object representing an HTTP version (see section 3.1).
+*
+* @param versionString
+* a string of the form "#.#", where # is an non-negative decimal integer with
+* or without leading zeros
+* @throws
+* if versionString does not specify a valid HTTP version number
+*/
+function nsHttpVersion(versionString)
+{
+ var matches = /^(\d+)\.(\d+)$/.exec(versionString);
+ if (!matches)
+ throw "Not a valid HTTP version!";
+
+ /** The major version number of this, as a number. */
+ this.major = parseInt(matches[1], 10);
+
+ /** The minor version number of this, as a number. */
+ this.minor = parseInt(matches[2], 10);
+
+ if (isNaN(this.major) || isNaN(this.minor) ||
+ this.major < 0 || this.minor < 0)
+ throw "Not a valid HTTP version!";
+}
+nsHttpVersion.prototype =
+{
+ /**
+* Returns the standard string representation of the HTTP version represented
+* by this (e.g., "1.1").
+*/
+ toString: function ()
+ {
+ return this.major + "." + this.minor;
+ },
+
+ /**
+* Returns true if this represents the same HTTP version as otherVersion,
+* false otherwise.
+*
+* @param otherVersion : nsHttpVersion
+* the version to compare against this
+*/
+ equals: function (otherVersion)
+ {
+ return this.major == otherVersion.major &&
+ this.minor == otherVersion.minor;
+ },
+
+ /** True if this >= otherVersion, false otherwise. */
+ atLeast: function(otherVersion)
+ {
+ return this.major > otherVersion.major ||
+ (this.major == otherVersion.major &&
+ this.minor >= otherVersion.minor);
+ }
+};
+
+nsHttpVersion.HTTP_1_0 = new nsHttpVersion("1.0");
+nsHttpVersion.HTTP_1_1 = new nsHttpVersion("1.1");
+
+
+/**
+* An object which stores HTTP headers for a request or response.
+*
+* Note that since headers are case-insensitive, this object converts headers to
+* lowercase before storing them. This allows the getHeader and hasHeader
+* methods to work correctly for any case of a header, but it means that the
+* values returned by .enumerator may not be equal case-sensitively to the
+* values passed to setHeader when adding headers to this.
+*/
+function nsHttpHeaders()
+{
+ /**
+* A hash of headers, with header field names as the keys and header field
+* values as the values. Header field names are case-insensitive, but upon
+* insertion here they are converted to lowercase. Header field values are
+* normalized upon insertion to contain no leading or trailing whitespace.
+*
+* Note also that per RFC 2616, section 4.2, two headers with the same name in
+* a message may be treated as one header with the same field name and a field
+* value consisting of the separate field values joined together with a "," in
+* their original order. This hash stores multiple headers with the same name
+* in this manner.
+*/
+ this._headers = {};
+}
+nsHttpHeaders.prototype =
+{
+ /**
+* Sets the header represented by name and value in this.
+*
+* @param name : string
+* the header name
+* @param value : string
+* the header value
+* @throws NS_ERROR_INVALID_ARG
+* if name or value is not a valid header component
+*/
+ setHeader: function(fieldName, fieldValue, merge)
+ {
+ var name = headerUtils.normalizeFieldName(fieldName);
+ var value = headerUtils.normalizeFieldValue(fieldValue);
+
+ // The following three headers are stored as arrays because their real-world
+ // syntax prevents joining individual headers into a single header using
+ // ",". See also <http://hg.mozilla.org/mozilla-central/diff/9b2a99adc05e/netwerk/protocol/http/src/nsHttpHeaderArray.cpp#l77>
+ if (merge && name in this._headers)
+ {
+ if (name === "www-authenticate" ||
+ name === "proxy-authenticate" ||
+ name === "set-cookie")
+ {
+ this._headers[name].push(value);
+ }
+ else
+ {
+ this._headers[name][0] += "," + value;
+ NS_ASSERT(this._headers[name].length === 1,
+ "how'd a non-special header have multiple values?")
+ }
+ }
+ else
+ {
+ this._headers[name] = [value];
+ }
+ },
+
+ /**
+* Returns the value for the header specified by this.
+*
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not constitute a valid header field name
+* @throws NS_ERROR_NOT_AVAILABLE
+* if the given header does not exist in this
+* @returns string
+* the field value for the given header, possibly with non-semantic changes
+* (i.e., leading/trailing whitespace stripped, whitespace runs replaced
+* with spaces, etc.) at the option of the implementation; multiple
+* instances of the header will be combined with a comma, except for
+* the three headers noted in the description of getHeaderValues
+*/
+ getHeader: function(fieldName)
+ {
+ return this.getHeaderValues(fieldName).join("\n");
+ },
+
+ /**
+* Returns the value for the header specified by fieldName as an array.
+*
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not constitute a valid header field name
+* @throws NS_ERROR_NOT_AVAILABLE
+* if the given header does not exist in this
+* @returns [string]
+* an array of all the header values in this for the given
+* header name. Header values will generally be collapsed
+* into a single header by joining all header values together
+* with commas, but certain headers (Proxy-Authenticate,
+* WWW-Authenticate, and Set-Cookie) violate the HTTP spec
+* and cannot be collapsed in this manner. For these headers
+* only, the returned array may contain multiple elements if
+* that header has been added more than once.
+*/
+ getHeaderValues: function(fieldName)
+ {
+ var name = headerUtils.normalizeFieldName(fieldName);
+
+ if (name in this._headers)
+ return this._headers[name];
+ else
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+ },
+
+ /**
+* Returns true if a header with the given field name exists in this, false
+* otherwise.
+*
+* @param fieldName : string
+* the field name whose existence is to be determined in this
+* @throws NS_ERROR_INVALID_ARG
+* if fieldName does not constitute a valid header field name
+* @returns boolean
+* true if the header's present, false otherwise
+*/
+ hasHeader: function(fieldName)
+ {
+ var name = headerUtils.normalizeFieldName(fieldName);
+ return (name in this._headers);
+ },
+
+ /**
+* Returns a new enumerator over the field names of the headers in this, as
+* nsISupportsStrings. The names returned will be in lowercase, regardless of
+* how they were input using setHeader (header names are case-insensitive per
+* RFC 2616).
+*/
+ get enumerator()
+ {
+ var headers = [];
+ for (var i in this._headers)
+ {
+ var supports = new SupportsString();
+ supports.data = i;
+ headers.push(supports);
+ }
+
+ return new nsSimpleEnumerator(headers);
+ }
+};
+
+
+/**
+* Constructs an nsISimpleEnumerator for the given array of items.
+*
+* @param items : Array
+* the items, which must all implement nsISupports
+*/
+function nsSimpleEnumerator(items)
+{
+ this._items = items;
+ this._nextIndex = 0;
+}
+nsSimpleEnumerator.prototype =
+{
+ hasMoreElements: function()
+ {
+ return this._nextIndex < this._items.length;
+ },
+ getNext: function()
+ {
+ if (!this.hasMoreElements())
+ throw Cr.NS_ERROR_NOT_AVAILABLE;
+
+ return this._items[this._nextIndex++];
+ },
+ QueryInterface: function(aIID)
+ {
+ if (Ci.nsISimpleEnumerator.equals(aIID) ||
+ Ci.nsISupports.equals(aIID))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ }
+};
+
+
+/**
+* A representation of the data in an HTTP request.
+*
+* @param port : uint
+* the port on which the server receiving this request runs
+*/
+function Request(port)
+{
+ /** Method of this request, e.g. GET or POST. */
+ this._method = "";
+
+ /** Path of the requested resource; empty paths are converted to '/'. */
+ this._path = "";
+
+ /** Query string, if any, associated with this request (not including '?'). */
+ this._queryString = "";
+
+ /** Scheme of requested resource, usually http, always lowercase. */
+ this._scheme = "http";
+
+ /** Hostname on which the requested resource resides. */
+ this._host = undefined;
+
+ /** Port number over which the request was received. */
+ this._port = port;
+
+ var bodyPipe = new Pipe(false, false, 0, PR_UINT32_MAX, null);
+
+ /** Stream from which data in this request's body may be read. */
+ this._bodyInputStream = bodyPipe.inputStream;
+
+ /** Stream to which data in this request's body is written. */
+ this._bodyOutputStream = bodyPipe.outputStream;
+
+ /**
+* The headers in this request.
+*/
+ this._headers = new nsHttpHeaders();
+
+ /**
+* For the addition of ad-hoc properties and new functionality without having
+* to change nsIHttpRequest every time; currently lazily created, as its only
+* use is in directory listings.
+*/
+ this._bag = null;
+}
+Request.prototype =
+{
+ // SERVER METADATA
+
+ //
+ // see nsIHttpRequest.scheme
+ //
+ get scheme()
+ {
+ return this._scheme;
+ },
+
+ //
+ // see nsIHttpRequest.host
+ //
+ get host()
+ {
+ return this._host;
+ },
+
+ //
+ // see nsIHttpRequest.port
+ //
+ get port()
+ {
+ return this._port;
+ },
+
+ // REQUEST LINE
+
+ //
+ // see nsIHttpRequest.method
+ //
+ get method()
+ {
+ return this._method;
+ },
+
+ //
+ // see nsIHttpRequest.httpVersion
+ //
+ get httpVersion()
+ {
+ return this._httpVersion.toString();
+ },
+
+ //
+ // see nsIHttpRequest.path
+ //
+ get path()
+ {
+ return this._path;
+ },
+
+ //
+ // see nsIHttpRequest.queryString
+ //
+ get queryString()
+ {
+ return this._queryString;
+ },
+
+ // HEADERS
+
+ //
+ // see nsIHttpRequest.getHeader
+ //
+ getHeader: function(name)
+ {
+ return this._headers.getHeader(name);
+ },
+
+ //
+ // see nsIHttpRequest.hasHeader
+ //
+ hasHeader: function(name)
+ {
+ return this._headers.hasHeader(name);
+ },
+
+ //
+ // see nsIHttpRequest.headers
+ //
+ get headers()
+ {
+ return this._headers.enumerator;
+ },
+
+ //
+ // see nsIPropertyBag.enumerator
+ //
+ get enumerator()
+ {
+ this._ensurePropertyBag();
+ return this._bag.enumerator;
+ },
+
+ //
+ // see nsIHttpRequest.headers
+ //
+ get bodyInputStream()
+ {
+ return this._bodyInputStream;
+ },
+
+ //
+ // see nsIPropertyBag.getProperty
+ //
+ getProperty: function(name)
+ {
+ this._ensurePropertyBag();
+ return this._bag.getProperty(name);
+ },
+
+
+ // NSISUPPORTS
+
+ //
+ // see nsISupports.QueryInterface
+ //
+ QueryInterface: function(iid)
+ {
+ if (iid.equals(Ci.nsIHttpRequest) || iid.equals(Ci.nsISupports))
+ return this;
+
+ throw Cr.NS_ERROR_NO_INTERFACE;
+ },
+
+
+ // PRIVATE IMPLEMENTATION
+
+ /** Ensures a property bag has been created for ad-hoc behaviors. */
+ _ensurePropertyBag: function()
+ {
+ if (!this._bag)
+ this._bag = new WritablePropertyBag();
+ }
+};
+
+
+// XPCOM trappings
+if ("XPCOMUtils" in this && // Firefox 3.6 doesn't load XPCOMUtils in this scope for some reason...
+ "generateNSGetFactory" in XPCOMUtils) {
+ var NSGetFactory = XPCOMUtils.generateNSGetFactory([nsHttpServer]);
+}
+
+
+
+/**
+* Creates a new HTTP server listening for loopback traffic on the given port,
+* starts it, and runs the server until the server processes a shutdown request,
+* spinning an event loop so that events posted by the server's socket are
+* processed.
+*
+* This method is primarily intended for use in running this script from within
+* xpcshell and running a functional HTTP server without having to deal with
+* non-essential details.
+*
+* Note that running multiple servers using variants of this method probably
+* doesn't work, simply due to how the internal event loop is spun and stopped.
+*
+* @note
+* This method only works with Mozilla 1.9 (i.e., Firefox 3 or trunk code);
+* you should use this server as a component in Mozilla 1.8.
+* @param port
+* the port on which the server will run, or -1 if there exists no preference
+* for a specific port; note that attempting to use some values for this
+* parameter (particularly those below 1024) may cause this method to throw or
+* may result in the server being prematurely shut down
+* @param basePath
+* a local directory from which requests will be served (i.e., if this is
+* "/home/jwalden/" then a request to /index.html will load
+* /home/jwalden/index.html); if this is omitted, only the default URLs in
+* this server implementation will be functional
+*/
+function server(port, basePath)
+{
+ if (basePath)
+ {
+ var lp = Cc["@mozilla.org/file/local;1"]
+ .createInstance(Ci.nsILocalFile);
+ lp.initWithPath(basePath);
+ }
+
+ // if you're running this, you probably want to see debugging info
+ DEBUG = true;
+
+ var srv = new nsHttpServer();
+ if (lp)
+ srv.registerDirectory("/", lp);
+ srv.registerContentType("sjs", SJS_TYPE);
+ srv.start(port);
+
+ var thread = gThreadManager.currentThread;
+ while (!srv.isStopped())
+ thread.processNextEvent(true);
+
+ // get rid of any pending requests
+ while (thread.hasPendingEvents())
+ thread.processNextEvent(true);
+
+ DEBUG = false;
+}
+
+function startServerAsync(port, basePath)
+{
+ if (basePath)
+ {
+ var lp = Cc["@mozilla.org/file/local;1"]
+ .createInstance(Ci.nsILocalFile);
+ lp.initWithPath(basePath);
+ }
+
+ var srv = new nsHttpServer();
+ if (lp)
+ srv.registerDirectory("/", lp);
+ srv.registerContentType("sjs", "sjs");
+ srv.start(port);
+ return srv;
+}
+
+exports.nsHttpServer = nsHttpServer;
+exports.ScriptableInputStream = ScriptableInputStream;
+exports.server = server;
+exports.startServerAsync = startServerAsync;
diff --git a/addon-sdk/source/test/loader/b2g.js b/addon-sdk/source/test/loader/b2g.js
new file mode 100644
index 000000000..2982a0202
--- /dev/null
+++ b/addon-sdk/source/test/loader/b2g.js
@@ -0,0 +1,41 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const {Cc, Ci, Cu} = require("chrome");
+const {readURISync} = require("sdk/net/url");
+
+const systemPrincipal = Cc["@mozilla.org/systemprincipal;1"].
+ createInstance(Ci.nsIPrincipal);
+
+
+const FakeCu = function() {
+ const sandbox = Cu.Sandbox(systemPrincipal, {wantXrays: false});
+ sandbox.toString = function() {
+ return "[object BackstagePass]";
+ }
+ this.sandbox = sandbox;
+}
+FakeCu.prototype = {
+ ["import"](url, scope) {
+ const {sandbox} = this;
+ sandbox.__URI__ = url;
+ const target = Cu.createObjectIn(sandbox);
+ target.toString = sandbox.toString;
+ Cu.evalInSandbox(`(function(){` + readURISync(url) + `\n})`,
+ sandbox, "1.8", url).call(target);
+ // Borrowed from mozJSComponentLoader.cpp to match errors closer.
+ // https://github.com/mozilla/gecko-dev/blob/f6ca65e8672433b2ce1a0e7c31f72717930b5e27/js/xpconnect/loader/mozJSComponentLoader.cpp#L1205-L1208
+ if (!Array.isArray(target.EXPORTED_SYMBOLS)) {
+ throw Error("EXPORTED_SYMBOLS is not an array.");
+ }
+
+ for (let key of target.EXPORTED_SYMBOLS) {
+ scope[key] = target[key];
+ }
+
+ return target;
+ }
+};
+exports.FakeCu = FakeCu;
diff --git a/addon-sdk/source/test/loader/fixture.js b/addon-sdk/source/test/loader/fixture.js
new file mode 100644
index 000000000..ebf91abba
--- /dev/null
+++ b/addon-sdk/source/test/loader/fixture.js
@@ -0,0 +1,7 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+exports.foo = foo;
+exports.bar = 2;
+print('testing');
diff --git a/addon-sdk/source/test/loader/user-global.js b/addon-sdk/source/test/loader/user-global.js
new file mode 100644
index 000000000..34b233f42
--- /dev/null
+++ b/addon-sdk/source/test/loader/user-global.js
@@ -0,0 +1,11 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+// Test module to check presense of user defined globals.
+// Related to bug 827792.
+
+exports.getCom = function() {
+ return com;
+};
diff --git a/addon-sdk/source/test/modules/add.js b/addon-sdk/source/test/modules/add.js
new file mode 100644
index 000000000..54729340d
--- /dev/null
+++ b/addon-sdk/source/test/modules/add.js
@@ -0,0 +1,9 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+define('modules/add', function () {
+ return function (a, b) {
+ return a + b;
+ };
+});
diff --git a/addon-sdk/source/test/modules/async1.js b/addon-sdk/source/test/modules/async1.js
new file mode 100644
index 000000000..b7b60fdc2
--- /dev/null
+++ b/addon-sdk/source/test/modules/async1.js
@@ -0,0 +1,14 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+define(['./traditional2', './async2'], function () {
+ var traditional2 = require('./traditional2');
+ return {
+ name: 'async1',
+ traditional1Name: traditional2.traditional1Name,
+ traditional2Name: traditional2.name,
+ async2Name: require('./async2').name,
+ async2Traditional2Name: require('./async2').traditional2Name
+ };
+});
diff --git a/addon-sdk/source/test/modules/async2.js b/addon-sdk/source/test/modules/async2.js
new file mode 100644
index 000000000..802fb2504
--- /dev/null
+++ b/addon-sdk/source/test/modules/async2.js
@@ -0,0 +1,8 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+define(['./traditional2', 'exports'], function (traditional2, exports) {
+ exports.name = 'async2';
+ exports.traditional2Name = traditional2.name;
+});
diff --git a/addon-sdk/source/test/modules/badExportAndReturn.js b/addon-sdk/source/test/modules/badExportAndReturn.js
new file mode 100644
index 000000000..35a5359f6
--- /dev/null
+++ b/addon-sdk/source/test/modules/badExportAndReturn.js
@@ -0,0 +1,10 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// This is a bad module, it asks for exports but also returns a value from
+// the define defintion function.
+define(['exports'], function (exports) {
+ return 'badExportAndReturn';
+});
+
diff --git a/addon-sdk/source/test/modules/badFirst.js b/addon-sdk/source/test/modules/badFirst.js
new file mode 100644
index 000000000..fdb03bd4d
--- /dev/null
+++ b/addon-sdk/source/test/modules/badFirst.js
@@ -0,0 +1,9 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+define(['./badSecond'], function (badSecond) {
+ return {
+ name: 'badFirst'
+ };
+});
diff --git a/addon-sdk/source/test/modules/badSecond.js b/addon-sdk/source/test/modules/badSecond.js
new file mode 100644
index 000000000..8321a8542
--- /dev/null
+++ b/addon-sdk/source/test/modules/badSecond.js
@@ -0,0 +1,8 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+var first = require('./badFirst');
+
+exports.name = 'badSecond';
+exports.badFirstName = first.name;
diff --git a/addon-sdk/source/test/modules/blue.js b/addon-sdk/source/test/modules/blue.js
new file mode 100644
index 000000000..14ab14933
--- /dev/null
+++ b/addon-sdk/source/test/modules/blue.js
@@ -0,0 +1,9 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+define(function () {
+ return {
+ name: 'blue'
+ };
+});
diff --git a/addon-sdk/source/test/modules/castor.js b/addon-sdk/source/test/modules/castor.js
new file mode 100644
index 000000000..9209623b5
--- /dev/null
+++ b/addon-sdk/source/test/modules/castor.js
@@ -0,0 +1,10 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+define(['exports', './pollux'], function(exports, pollux) {
+ exports.name = 'castor';
+ exports.getPolluxName = function () {
+ return pollux.name;
+ };
+});
diff --git a/addon-sdk/source/test/modules/cheetah.js b/addon-sdk/source/test/modules/cheetah.js
new file mode 100644
index 000000000..37d12bfc1
--- /dev/null
+++ b/addon-sdk/source/test/modules/cheetah.js
@@ -0,0 +1,9 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+define(function () {
+ return function () {
+ return 'cheetah';
+ };
+});
diff --git a/addon-sdk/source/test/modules/color.js b/addon-sdk/source/test/modules/color.js
new file mode 100644
index 000000000..0d946bd99
--- /dev/null
+++ b/addon-sdk/source/test/modules/color.js
@@ -0,0 +1,7 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+define({
+ type: 'color'
+});
diff --git a/addon-sdk/source/test/modules/dupe.js b/addon-sdk/source/test/modules/dupe.js
new file mode 100644
index 000000000..f87fa555a
--- /dev/null
+++ b/addon-sdk/source/test/modules/dupe.js
@@ -0,0 +1,15 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+define({
+ name: 'dupe'
+});
+
+// This is wrong and should not be allowed. Only one call to
+// define per file.
+define([], function () {
+ return {
+ name: 'dupe2'
+ };
+});
diff --git a/addon-sdk/source/test/modules/dupeNested.js b/addon-sdk/source/test/modules/dupeNested.js
new file mode 100644
index 000000000..703948ca7
--- /dev/null
+++ b/addon-sdk/source/test/modules/dupeNested.js
@@ -0,0 +1,15 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+
+define(function () {
+ // This is wrong and should not be allowed.
+ define('dupeNested2', {
+ name: 'dupeNested2'
+ });
+
+ return {
+ name: 'dupeNested'
+ };
+});
diff --git a/addon-sdk/source/test/modules/dupeSetExports.js b/addon-sdk/source/test/modules/dupeSetExports.js
new file mode 100644
index 000000000..12a1be22b
--- /dev/null
+++ b/addon-sdk/source/test/modules/dupeSetExports.js
@@ -0,0 +1,8 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+define({name: "dupeSetExports"});
+
+// so this should cause a failure
+module.setExports("no no no");
diff --git a/addon-sdk/source/test/modules/exportsEquals.js b/addon-sdk/source/test/modules/exportsEquals.js
new file mode 100644
index 000000000..e176316f4
--- /dev/null
+++ b/addon-sdk/source/test/modules/exportsEquals.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+module.exports = 4;
diff --git a/addon-sdk/source/test/modules/green.js b/addon-sdk/source/test/modules/green.js
new file mode 100644
index 000000000..ce2d134b9
--- /dev/null
+++ b/addon-sdk/source/test/modules/green.js
@@ -0,0 +1,10 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+define('modules/green', ['./color'], function (color) {
+ return {
+ name: 'green',
+ parentType: color.type
+ };
+});
diff --git a/addon-sdk/source/test/modules/lion.js b/addon-sdk/source/test/modules/lion.js
new file mode 100644
index 000000000..9c3ac3bfe
--- /dev/null
+++ b/addon-sdk/source/test/modules/lion.js
@@ -0,0 +1,7 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+define(function(require) {
+ return 'lion';
+});
diff --git a/addon-sdk/source/test/modules/orange.js b/addon-sdk/source/test/modules/orange.js
new file mode 100644
index 000000000..900a32b08
--- /dev/null
+++ b/addon-sdk/source/test/modules/orange.js
@@ -0,0 +1,10 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+define(['./color'], function (color) {
+ return {
+ name: 'orange',
+ parentType: color.type
+ };
+});
diff --git a/addon-sdk/source/test/modules/pollux.js b/addon-sdk/source/test/modules/pollux.js
new file mode 100644
index 000000000..61cf66f9c
--- /dev/null
+++ b/addon-sdk/source/test/modules/pollux.js
@@ -0,0 +1,10 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+define(['exports', './castor'], function(exports, castor) {
+ exports.name = 'pollux';
+ exports.getCastorName = function () {
+ return castor.name;
+ };
+});
diff --git a/addon-sdk/source/test/modules/red.js b/addon-sdk/source/test/modules/red.js
new file mode 100644
index 000000000..c47b8e924
--- /dev/null
+++ b/addon-sdk/source/test/modules/red.js
@@ -0,0 +1,16 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+define(function (require) {
+ // comment fake-outs for require finding.
+ // require('bad1');
+ return {
+ name: 'red',
+ parentType: require('./color').type
+ };
+
+ /*
+ require('bad2');
+ */
+});
diff --git a/addon-sdk/source/test/modules/setExports.js b/addon-sdk/source/test/modules/setExports.js
new file mode 100644
index 000000000..495c301cd
--- /dev/null
+++ b/addon-sdk/source/test/modules/setExports.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+module.setExports(5);
diff --git a/addon-sdk/source/test/modules/subtract.js b/addon-sdk/source/test/modules/subtract.js
new file mode 100644
index 000000000..06e1053ab
--- /dev/null
+++ b/addon-sdk/source/test/modules/subtract.js
@@ -0,0 +1,9 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+define(function () {
+ return function (a, b) {
+ return a - b;
+ }
+});
diff --git a/addon-sdk/source/test/modules/tiger.js b/addon-sdk/source/test/modules/tiger.js
new file mode 100644
index 000000000..80479d019
--- /dev/null
+++ b/addon-sdk/source/test/modules/tiger.js
@@ -0,0 +1,8 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+define(function (require, exports) {
+ exports.name = 'tiger';
+ exports.type = require('./types/cat').type;
+});
diff --git a/addon-sdk/source/test/modules/traditional1.js b/addon-sdk/source/test/modules/traditional1.js
new file mode 100644
index 000000000..28af8ef30
--- /dev/null
+++ b/addon-sdk/source/test/modules/traditional1.js
@@ -0,0 +1,12 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+exports.name = 'traditional1'
+
+var async1 = require('./async1');
+
+exports.traditional2Name = async1.traditional2Name;
+exports.traditional1Name = async1.traditional1Name;
+exports.async2Name = async1.async2Name;
+exports.async2Traditional2Name = async1.async2Traditional2Name;
diff --git a/addon-sdk/source/test/modules/traditional2.js b/addon-sdk/source/test/modules/traditional2.js
new file mode 100644
index 000000000..67b64eecb
--- /dev/null
+++ b/addon-sdk/source/test/modules/traditional2.js
@@ -0,0 +1,6 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+exports.name = 'traditional2';
+exports.traditional1Name = require('./traditional1').name;
diff --git a/addon-sdk/source/test/modules/types/cat.js b/addon-sdk/source/test/modules/types/cat.js
new file mode 100644
index 000000000..41513d682
--- /dev/null
+++ b/addon-sdk/source/test/modules/types/cat.js
@@ -0,0 +1,5 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+exports.type = 'cat';
diff --git a/addon-sdk/source/test/page-mod/helpers.js b/addon-sdk/source/test/page-mod/helpers.js
new file mode 100644
index 000000000..3aa3deb0d
--- /dev/null
+++ b/addon-sdk/source/test/page-mod/helpers.js
@@ -0,0 +1,117 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Cc, Ci } = require("chrome");
+const { setTimeout } = require("sdk/timers");
+const { Loader } = require("sdk/test/loader");
+const { openTab, getBrowserForTab, closeTab } = require("sdk/tabs/utils");
+const { getMostRecentBrowserWindow } = require("sdk/window/utils");
+const { merge } = require("sdk/util/object");
+const httpd = require("../lib/httpd");
+const { cleanUI } = require("sdk/test/utils");
+
+const PORT = 8099;
+const PATH = '/test-contentScriptWhen.html';
+
+function createLoader () {
+ let options = merge({}, require('@loader/options'),
+ { id: "testloader", prefixURI: require('../fixtures').url() });
+ return Loader(module, null, options);
+}
+exports.createLoader = createLoader;
+
+function openNewTab(url) {
+ return openTab(getMostRecentBrowserWindow(), url, {
+ inBackground: false
+ });
+}
+exports.openNewTab = openNewTab;
+
+// an evil function enables the creation of tests
+// that depend on delicate event timing. do not use.
+function testPageMod(assert, done, testURL, pageModOptions,
+ testCallback, timeout) {
+ let loader = createLoader();
+ let { PageMod } = loader.require("sdk/page-mod");
+ let pageMods = pageModOptions.map(opts => new PageMod(opts));
+ let newTab = openNewTab(testURL);
+ let b = getBrowserForTab(newTab);
+
+ function onPageLoad() {
+ b.removeEventListener("load", onPageLoad, true);
+ // Delay callback execute as page-mod content scripts may be executed on
+ // load event. So page-mod actions may not be already done.
+ // If we delay even more contentScriptWhen:'end', we may want to modify
+ // this code again.
+ setTimeout(testCallback, timeout,
+ b.contentWindow.wrappedJSObject, // TODO: remove this CPOW
+ function () {
+ pageMods.forEach(mod => mod.destroy());
+ // XXX leaks reported if we don't close the tab?
+ closeTab(newTab);
+ loader.unload();
+ done();
+ }
+ );
+ }
+ b.addEventListener("load", onPageLoad, true);
+
+ return pageMods;
+}
+exports.testPageMod = testPageMod;
+
+/**
+ * helper function that creates a PageMod and calls back the appropriate handler
+ * based on the value of document.readyState at the time contentScript is attached
+ */
+exports.handleReadyState = function(url, contentScriptWhen, callbacks) {
+ const loader = Loader(module);
+ const { PageMod } = loader.require('sdk/page-mod');
+
+ let pagemod = PageMod({
+ include: url,
+ attachTo: ['existing', 'top'],
+ contentScriptWhen: contentScriptWhen,
+ contentScript: "self.postMessage(document.readyState)",
+ onAttach: worker => {
+ let { tab } = worker;
+ worker.on('message', readyState => {
+ // generate event name from `readyState`, e.g. `"loading"` becomes `onLoading`.
+ let type = 'on' + readyState[0].toUpperCase() + readyState.substr(1);
+
+ if (type in callbacks)
+ callbacks[type](tab);
+
+ pagemod.destroy();
+ loader.unload();
+ })
+ }
+ });
+}
+
+// serves a slow page which takes 1.5 seconds to load,
+// 0.5 seconds in each readyState: uninitialized, loading, interactive.
+function contentScriptWhenServer() {
+ const URL = 'http://localhost:' + PORT + PATH;
+
+ const HTML = `/* polyglot js
+ <script src="${URL}"></script>
+ delay both the "DOMContentLoaded"
+ <script async src="${URL}"></script>
+ and "load" events */`;
+
+ let srv = httpd.startServerAsync(PORT);
+
+ srv.registerPathHandler(PATH, (_, response) => {
+ response.processAsync();
+ response.setHeader('Content-Type', 'text/html', false);
+ setTimeout(_ => response.finish(), 500);
+ response.write(HTML);
+ })
+
+ srv.URL = URL;
+ return srv;
+}
+exports.contentScriptWhenServer = contentScriptWhenServer;
diff --git a/addon-sdk/source/test/path/test-path.js b/addon-sdk/source/test/path/test-path.js
new file mode 100644
index 000000000..38037af20
--- /dev/null
+++ b/addon-sdk/source/test/path/test-path.js
@@ -0,0 +1,430 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+// Adapted version of:
+// https://github.com/joyent/node/blob/v0.9.1/test/simple/test-path.js
+
+exports['test path'] = function(assert) {
+ var system = require('sdk/system');
+ var path = require('sdk/fs/path');
+
+ // Shim process global from node.
+ var process = Object.create(require('sdk/system'));
+ process.cwd = process.pathFor.bind(process, 'CurProcD');
+
+ var isWindows = require('sdk/system').platform.indexOf('win') === 0;
+
+ assert.equal(path.basename(''), '');
+ assert.equal(path.basename('/dir/basename.ext'), 'basename.ext');
+ assert.equal(path.basename('/basename.ext'), 'basename.ext');
+ assert.equal(path.basename('basename.ext'), 'basename.ext');
+ assert.equal(path.basename('basename.ext/'), 'basename.ext');
+ assert.equal(path.basename('basename.ext//'), 'basename.ext');
+
+ if (isWindows) {
+ // On Windows a backslash acts as a path separator.
+ assert.equal(path.basename('\\dir\\basename.ext'), 'basename.ext');
+ assert.equal(path.basename('\\basename.ext'), 'basename.ext');
+ assert.equal(path.basename('basename.ext'), 'basename.ext');
+ assert.equal(path.basename('basename.ext\\'), 'basename.ext');
+ assert.equal(path.basename('basename.ext\\\\'), 'basename.ext');
+
+ } else {
+ // On unix a backslash is just treated as any other character.
+ assert.equal(path.basename('\\dir\\basename.ext'), '\\dir\\basename.ext');
+ assert.equal(path.basename('\\basename.ext'), '\\basename.ext');
+ assert.equal(path.basename('basename.ext'), 'basename.ext');
+ assert.equal(path.basename('basename.ext\\'), 'basename.ext\\');
+ assert.equal(path.basename('basename.ext\\\\'), 'basename.ext\\\\');
+ }
+
+ // POSIX filenames may include control characters
+ // c.f. http://www.dwheeler.com/essays/fixing-unix-linux-filenames.html
+ if (!isWindows) {
+ var controlCharFilename = 'Icon' + String.fromCharCode(13);
+ assert.equal(path.basename('/a/b/' + controlCharFilename),
+ controlCharFilename);
+ }
+
+ assert.equal(path.dirname('/a/b/'), '/a');
+ assert.equal(path.dirname('/a/b'), '/a');
+ assert.equal(path.dirname('/a'), '/');
+ assert.equal(path.dirname(''), '.');
+ assert.equal(path.dirname('/'), '/');
+ assert.equal(path.dirname('////'), '/');
+
+ if (isWindows) {
+ assert.equal(path.dirname('c:\\'), 'c:\\');
+ assert.equal(path.dirname('c:\\foo'), 'c:\\');
+ assert.equal(path.dirname('c:\\foo\\'), 'c:\\');
+ assert.equal(path.dirname('c:\\foo\\bar'), 'c:\\foo');
+ assert.equal(path.dirname('c:\\foo\\bar\\'), 'c:\\foo');
+ assert.equal(path.dirname('c:\\foo\\bar\\baz'), 'c:\\foo\\bar');
+ assert.equal(path.dirname('\\'), '\\');
+ assert.equal(path.dirname('\\foo'), '\\');
+ assert.equal(path.dirname('\\foo\\'), '\\');
+ assert.equal(path.dirname('\\foo\\bar'), '\\foo');
+ assert.equal(path.dirname('\\foo\\bar\\'), '\\foo');
+ assert.equal(path.dirname('\\foo\\bar\\baz'), '\\foo\\bar');
+ assert.equal(path.dirname('c:'), 'c:');
+ assert.equal(path.dirname('c:foo'), 'c:');
+ assert.equal(path.dirname('c:foo\\'), 'c:');
+ assert.equal(path.dirname('c:foo\\bar'), 'c:foo');
+ assert.equal(path.dirname('c:foo\\bar\\'), 'c:foo');
+ assert.equal(path.dirname('c:foo\\bar\\baz'), 'c:foo\\bar');
+ assert.equal(path.dirname('\\\\unc\\share'), '\\\\unc\\share');
+ assert.equal(path.dirname('\\\\unc\\share\\foo'), '\\\\unc\\share\\');
+ assert.equal(path.dirname('\\\\unc\\share\\foo\\'), '\\\\unc\\share\\');
+ assert.equal(path.dirname('\\\\unc\\share\\foo\\bar'),
+ '\\\\unc\\share\\foo');
+ assert.equal(path.dirname('\\\\unc\\share\\foo\\bar\\'),
+ '\\\\unc\\share\\foo');
+ assert.equal(path.dirname('\\\\unc\\share\\foo\\bar\\baz'),
+ '\\\\unc\\share\\foo\\bar');
+ }
+
+
+ assert.equal(path.extname(''), '');
+ assert.equal(path.extname('/path/to/file'), '');
+ assert.equal(path.extname('/path/to/file.ext'), '.ext');
+ assert.equal(path.extname('/path.to/file.ext'), '.ext');
+ assert.equal(path.extname('/path.to/file'), '');
+ assert.equal(path.extname('/path.to/.file'), '');
+ assert.equal(path.extname('/path.to/.file.ext'), '.ext');
+ assert.equal(path.extname('/path/to/f.ext'), '.ext');
+ assert.equal(path.extname('/path/to/..ext'), '.ext');
+ assert.equal(path.extname('file'), '');
+ assert.equal(path.extname('file.ext'), '.ext');
+ assert.equal(path.extname('.file'), '');
+ assert.equal(path.extname('.file.ext'), '.ext');
+ assert.equal(path.extname('/file'), '');
+ assert.equal(path.extname('/file.ext'), '.ext');
+ assert.equal(path.extname('/.file'), '');
+ assert.equal(path.extname('/.file.ext'), '.ext');
+ assert.equal(path.extname('.path/file.ext'), '.ext');
+ assert.equal(path.extname('file.ext.ext'), '.ext');
+ assert.equal(path.extname('file.'), '.');
+ assert.equal(path.extname('.'), '');
+ assert.equal(path.extname('./'), '');
+ assert.equal(path.extname('.file.ext'), '.ext');
+ assert.equal(path.extname('.file'), '');
+ assert.equal(path.extname('.file.'), '.');
+ assert.equal(path.extname('.file..'), '.');
+ assert.equal(path.extname('..'), '');
+ assert.equal(path.extname('../'), '');
+ assert.equal(path.extname('..file.ext'), '.ext');
+ assert.equal(path.extname('..file'), '.file');
+ assert.equal(path.extname('..file.'), '.');
+ assert.equal(path.extname('..file..'), '.');
+ assert.equal(path.extname('...'), '.');
+ assert.equal(path.extname('...ext'), '.ext');
+ assert.equal(path.extname('....'), '.');
+ assert.equal(path.extname('file.ext/'), '.ext');
+ assert.equal(path.extname('file.ext//'), '.ext');
+ assert.equal(path.extname('file/'), '');
+ assert.equal(path.extname('file//'), '');
+ assert.equal(path.extname('file./'), '.');
+ assert.equal(path.extname('file.//'), '.');
+
+ if (isWindows) {
+ // On windows, backspace is a path separator.
+ assert.equal(path.extname('.\\'), '');
+ assert.equal(path.extname('..\\'), '');
+ assert.equal(path.extname('file.ext\\'), '.ext');
+ assert.equal(path.extname('file.ext\\\\'), '.ext');
+ assert.equal(path.extname('file\\'), '');
+ assert.equal(path.extname('file\\\\'), '');
+ assert.equal(path.extname('file.\\'), '.');
+ assert.equal(path.extname('file.\\\\'), '.');
+
+ } else {
+ // On unix, backspace is a valid name component like any other character.
+ assert.equal(path.extname('.\\'), '');
+ assert.equal(path.extname('..\\'), '.\\');
+ assert.equal(path.extname('file.ext\\'), '.ext\\');
+ assert.equal(path.extname('file.ext\\\\'), '.ext\\\\');
+ assert.equal(path.extname('file\\'), '');
+ assert.equal(path.extname('file\\\\'), '');
+ assert.equal(path.extname('file.\\'), '.\\');
+ assert.equal(path.extname('file.\\\\'), '.\\\\');
+ }
+
+ // path.join tests
+ var failures = [];
+ var joinTests =
+ // arguments result
+ [[['.', 'x/b', '..', '/b/c.js'], 'x/b/c.js'],
+ [['/.', 'x/b', '..', '/b/c.js'], '/x/b/c.js'],
+ [['/foo', '../../../bar'], '/bar'],
+ [['foo', '../../../bar'], '../../bar'],
+ [['foo/', '../../../bar'], '../../bar'],
+ [['foo/x', '../../../bar'], '../bar'],
+ [['foo/x', './bar'], 'foo/x/bar'],
+ [['foo/x/', './bar'], 'foo/x/bar'],
+ [['foo/x/', '.', 'bar'], 'foo/x/bar'],
+ [['./'], './'],
+ [['.', './'], './'],
+ [['.', '.', '.'], '.'],
+ [['.', './', '.'], '.'],
+ [['.', '/./', '.'], '.'],
+ [['.', '/////./', '.'], '.'],
+ [['.'], '.'],
+ [['', '.'], '.'],
+ [['', 'foo'], 'foo'],
+ [['foo', '/bar'], 'foo/bar'],
+ [['', '/foo'], '/foo'],
+ [['', '', '/foo'], '/foo'],
+ [['', '', 'foo'], 'foo'],
+ [['foo', ''], 'foo'],
+ [['foo/', ''], 'foo/'],
+ [['foo', '', '/bar'], 'foo/bar'],
+ [['./', '..', '/foo'], '../foo'],
+ [['./', '..', '..', '/foo'], '../../foo'],
+ [['.', '..', '..', '/foo'], '../../foo'],
+ [['', '..', '..', '/foo'], '../../foo'],
+ [['/'], '/'],
+ [['/', '.'], '/'],
+ [['/', '..'], '/'],
+ [['/', '..', '..'], '/'],
+ [[''], '.'],
+ [['', ''], '.'],
+ [[' /foo'], ' /foo'],
+ [[' ', 'foo'], ' /foo'],
+ [[' ', '.'], ' '],
+ [[' ', '/'], ' /'],
+ [[' ', ''], ' '],
+ [['/', 'foo'], '/foo'],
+ [['/', '/foo'], '/foo'],
+ [['/', '//foo'], '/foo'],
+ [['/', '', '/foo'], '/foo'],
+ [['', '/', 'foo'], '/foo'],
+ [['', '/', '/foo'], '/foo']
+ ];
+
+ // Windows-specific join tests
+ if (isWindows) {
+ joinTests = joinTests.concat(
+ [// UNC path expected
+ [['//foo/bar'], '//foo/bar/'],
+ [['\\/foo/bar'], '//foo/bar/'],
+ [['\\\\foo/bar'], '//foo/bar/'],
+ // UNC path expected - server and share separate
+ [['//foo', 'bar'], '//foo/bar/'],
+ [['//foo/', 'bar'], '//foo/bar/'],
+ [['//foo', '/bar'], '//foo/bar/'],
+ // UNC path expected - questionable
+ [['//foo', '', 'bar'], '//foo/bar/'],
+ [['//foo/', '', 'bar'], '//foo/bar/'],
+ [['//foo/', '', '/bar'], '//foo/bar/'],
+ // UNC path expected - even more questionable
+ [['', '//foo', 'bar'], '//foo/bar/'],
+ [['', '//foo/', 'bar'], '//foo/bar/'],
+ [['', '//foo/', '/bar'], '//foo/bar/'],
+ // No UNC path expected (no double slash in first component)
+ [['\\', 'foo/bar'], '/foo/bar'],
+ [['\\', '/foo/bar'], '/foo/bar'],
+ [['', '/', '/foo/bar'], '/foo/bar'],
+ // No UNC path expected (no non-slashes in first component - questionable)
+ [['//', 'foo/bar'], '/foo/bar'],
+ [['//', '/foo/bar'], '/foo/bar'],
+ [['\\\\', '/', '/foo/bar'], '/foo/bar'],
+ [['//'], '/'],
+ // No UNC path expected (share name missing - questionable).
+ [['//foo'], '/foo'],
+ [['//foo/'], '/foo/'],
+ [['//foo', '/'], '/foo/'],
+ [['//foo', '', '/'], '/foo/'],
+ // No UNC path expected (too many leading slashes - questionable)
+ [['///foo/bar'], '/foo/bar'],
+ [['////foo', 'bar'], '/foo/bar'],
+ [['\\\\\\/foo/bar'], '/foo/bar'],
+ // Drive-relative vs drive-absolute paths. This merely describes the
+ // status quo, rather than being obviously right
+ [['c:'], 'c:.'],
+ [['c:.'], 'c:.'],
+ [['c:', ''], 'c:.'],
+ [['', 'c:'], 'c:.'],
+ [['c:.', '/'], 'c:./'],
+ [['c:.', 'file'], 'c:file'],
+ [['c:', '/'], 'c:/'],
+ [['c:', 'file'], 'c:/file']
+ ]);
+ }
+
+ // Run the join tests.
+ joinTests.forEach(function(test) {
+ var actual = path.join.apply(path, test[0]);
+ var expected = isWindows ? test[1].replace(/\//g, '\\') : test[1];
+ var message = 'path.join(' + test[0].map(JSON.stringify).join(',') + ')' +
+ '\n expect=' + JSON.stringify(expected) +
+ '\n actual=' + JSON.stringify(actual);
+ if (actual !== expected) failures.push('\n' + message);
+ // assert.equal(actual, expected, message);
+ });
+ assert.equal(failures.length, 0, failures.join(''));
+ var joinThrowTests = [true, false, 7, null, {}, undefined, [], NaN];
+ joinThrowTests.forEach(function(test) {
+ assert.throws(function() {
+ path.join(test);
+ }, TypeError);
+ assert.throws(function() {
+ path.resolve(test);
+ }, TypeError);
+ });
+
+
+ // path normalize tests
+ if (isWindows) {
+ assert.equal(path.normalize('./fixtures///b/../b/c.js'),
+ 'fixtures\\b\\c.js');
+ assert.equal(path.normalize('/foo/../../../bar'), '\\bar');
+ assert.equal(path.normalize('a//b//../b'), 'a\\b');
+ assert.equal(path.normalize('a//b//./c'), 'a\\b\\c');
+ assert.equal(path.normalize('a//b//.'), 'a\\b');
+ assert.equal(path.normalize('//server/share/dir/file.ext'),
+ '\\\\server\\share\\dir\\file.ext');
+ } else {
+ assert.equal(path.normalize('./fixtures///b/../b/c.js'),
+ 'fixtures/b/c.js');
+ assert.equal(path.normalize('/foo/../../../bar'), '/bar');
+ assert.equal(path.normalize('a//b//../b'), 'a/b');
+ assert.equal(path.normalize('a//b//./c'), 'a/b/c');
+ assert.equal(path.normalize('a//b//.'), 'a/b');
+ }
+
+ // path.resolve tests
+ if (isWindows) {
+ // windows
+ var resolveTests =
+ // arguments result
+ [[['c:/blah\\blah', 'd:/games', 'c:../a'], 'c:\\blah\\a'],
+ [['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe'],
+ [['c:/ignore', 'c:/some/file'], 'c:\\some\\file'],
+ [['d:/ignore', 'd:some/dir//'], 'd:\\ignore\\some\\dir'],
+ [['.'], process.cwd()],
+ [['//server/share', '..', 'relative\\'], '\\\\server\\share\\relative'],
+ [['c:/', '//'], 'c:\\'],
+ [['c:/', '//dir'], 'c:\\dir'],
+ [['c:/', '//server/share'], '\\\\server\\share\\'],
+ [['c:/', '//server//share'], '\\\\server\\share\\'],
+ [['c:/', '///some//dir'], 'c:\\some\\dir']
+ ];
+ } else {
+ // Posix
+ var resolveTests =
+ // arguments result
+ [[['/var/lib', '../', 'file/'], '/var/file'],
+ [['/var/lib', '/../', 'file/'], '/file'],
+ // For some mysterious reasons OSX debug builds resolve incorrectly
+ // https://tbpl.mozilla.org/php/getParsedLog.php?id=25105489&tree=Mozilla-Inbound
+ // Disable this tests until Bug 891698 is fixed.
+ // [['a/b/c/', '../../..'], process.cwd()],
+ // [['.'], process.cwd()],
+ [['/some/dir', '.', '/absolute/'], '/absolute']];
+ }
+ var failures = [];
+ resolveTests.forEach(function(test) {
+ var actual = path.resolve.apply(path, test[0]);
+ var expected = test[1];
+ var message = 'path.resolve(' + test[0].map(JSON.stringify).join(',') + ')' +
+ '\n expect=' + JSON.stringify(expected) +
+ '\n actual=' + JSON.stringify(actual);
+ if (actual !== expected) failures.push('\n' + message);
+ // assert.equal(actual, expected, message);
+ });
+ assert.equal(failures.length, 0, failures.join(''));
+
+ // path.isAbsolute tests
+ if (isWindows) {
+ assert.equal(path.isAbsolute('//server/file'), true);
+ assert.equal(path.isAbsolute('\\\\server\\file'), true);
+ assert.equal(path.isAbsolute('C:/Users/'), true);
+ assert.equal(path.isAbsolute('C:\\Users\\'), true);
+ assert.equal(path.isAbsolute('C:cwd/another'), false);
+ assert.equal(path.isAbsolute('C:cwd\\another'), false);
+ assert.equal(path.isAbsolute('directory/directory'), false);
+ assert.equal(path.isAbsolute('directory\\directory'), false);
+ } else {
+ assert.equal(path.isAbsolute('/home/foo'), true);
+ assert.equal(path.isAbsolute('/home/foo/..'), true);
+ assert.equal(path.isAbsolute('bar/'), false);
+ assert.equal(path.isAbsolute('./baz'), false);
+ }
+
+ // path.relative tests
+ if (isWindows) {
+ // windows
+ var relativeTests =
+ // arguments result
+ [['c:/blah\\blah', 'd:/games', 'd:\\games'],
+ ['c:/aaaa/bbbb', 'c:/aaaa', '..'],
+ ['c:/aaaa/bbbb', 'c:/cccc', '..\\..\\cccc'],
+ ['c:/aaaa/bbbb', 'c:/aaaa/bbbb', ''],
+ ['c:/aaaa/bbbb', 'c:/aaaa/cccc', '..\\cccc'],
+ ['c:/aaaa/', 'c:/aaaa/cccc', 'cccc'],
+ ['c:/', 'c:\\aaaa\\bbbb', 'aaaa\\bbbb'],
+ ['c:/aaaa/bbbb', 'd:\\', 'd:\\']];
+ } else {
+ // posix
+ var relativeTests =
+ // arguments result
+ [['/var/lib', '/var', '..'],
+ ['/var/lib', '/bin', '../../bin'],
+ ['/var/lib', '/var/lib', ''],
+ ['/var/lib', '/var/apache', '../apache'],
+ ['/var/', '/var/lib', 'lib'],
+ ['/', '/var/lib', 'var/lib']];
+ }
+ var failures = [];
+ relativeTests.forEach(function(test) {
+ var actual = path.relative(test[0], test[1]);
+ var expected = test[2];
+ var message = 'path.relative(' +
+ test.slice(0, 2).map(JSON.stringify).join(',') +
+ ')' +
+ '\n expect=' + JSON.stringify(expected) +
+ '\n actual=' + JSON.stringify(actual);
+ if (actual !== expected) failures.push('\n' + message);
+ });
+ assert.equal(failures.length, 0, failures.join(''));
+
+ // path.sep tests
+ if (isWindows) {
+ // windows
+ assert.equal(path.sep, '\\');
+ }
+ else {
+ // posix
+ assert.equal(path.sep, '/');
+ }
+
+ // path.delimiter tests
+ if (isWindows) {
+ // windows
+ assert.equal(path.delimiter, ';');
+ }
+ else {
+ // posix
+ assert.equal(path.delimiter, ':');
+ }
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/preferences/common.json b/addon-sdk/source/test/preferences/common.json
new file mode 100644
index 000000000..c645e24d5
--- /dev/null
+++ b/addon-sdk/source/test/preferences/common.json
@@ -0,0 +1,16 @@
+{
+ "browser.dom.window.dump.enabled": true,
+ "javascript.options.showInConsole": true,
+ "devtools.debugger.remote-enabled": true,
+ "extensions.sdk.console.logLevel": "info",
+ "extensions.checkCompatibility.nightly": false,
+ "extensions.update.enabled": false,
+ "lightweightThemes.update.enabled": false,
+ "extensions.update.notifyUser": false,
+ "extensions.enabledScopes": 5,
+ "extensions.getAddons.cache.enabled": false,
+ "extensions.installDistroAddons": false,
+ "extensions.autoDisableScopes": 10,
+ "app.releaseNotesURL": "http://localhost/app-dummy/",
+ "app.vendorURL": "http://localhost/app-dummy/"
+}
diff --git a/addon-sdk/source/test/preferences/e10s-off.json b/addon-sdk/source/test/preferences/e10s-off.json
new file mode 100644
index 000000000..269d7a92a
--- /dev/null
+++ b/addon-sdk/source/test/preferences/e10s-off.json
@@ -0,0 +1,5 @@
+{
+ "browser.tabs.remote.autostart": false,
+ "browser.tabs.remote.autostart.1": false,
+ "browser.tabs.remote.autostart.2": false
+}
diff --git a/addon-sdk/source/test/preferences/e10s-on.json b/addon-sdk/source/test/preferences/e10s-on.json
new file mode 100644
index 000000000..dd5a78dbf
--- /dev/null
+++ b/addon-sdk/source/test/preferences/e10s-on.json
@@ -0,0 +1,3 @@
+{
+ "browser.tabs.remote.autostart": true
+}
diff --git a/addon-sdk/source/test/preferences/firefox.json b/addon-sdk/source/test/preferences/firefox.json
new file mode 100644
index 000000000..5b8145cec
--- /dev/null
+++ b/addon-sdk/source/test/preferences/firefox.json
@@ -0,0 +1,11 @@
+{
+ "browser.startup.homepage": "about:blank",
+ "startup.homepage_welcome_url": "about:blank",
+ "devtools.browsertoolbox.panel": "jsdebugger",
+ "devtools.chrome.enabled": true,
+ "urlclassifier.updateinterval": 172800,
+ "browser.safebrowsing.provider.google.gethashURL": "http://localhost/safebrowsing-dummy/gethash",
+ "browser.safebrowsing.provider.google.updateURL": "http://localhost/safebrowsing-dummy/update",
+ "browser.safebrowsing.provider.mozilla.gethashURL": "http://localhost/safebrowsing-dummy/gethash",
+ "browser.safebrowsing.provider.mozilla.updateURL": "http://localhost/safebrowsing-dummy/update"
+}
diff --git a/addon-sdk/source/test/preferences/no-connections.json b/addon-sdk/source/test/preferences/no-connections.json
new file mode 100644
index 000000000..370b7909c
--- /dev/null
+++ b/addon-sdk/source/test/preferences/no-connections.json
@@ -0,0 +1,41 @@
+{
+ "toolkit.telemetry.enabled": false,
+ "toolkit.telemetry.server": "https://localhost/telemetry-dummy/",
+ "app.update.auto": false,
+ "app.update.url": "http://localhost/app-dummy/update",
+ "app.update.enabled": false,
+ "app.update.staging.enabled": false,
+ "media.gmp-gmpopenh264.autoupdate": false,
+ "media.gmp-manager.cert.checkAttributes": false,
+ "media.gmp-manager.cert.requireBuiltIn": false,
+ "media.gmp-manager.url": "http://localhost/media-dummy/gmpmanager",
+ "media.gmp-manager.url.override": "http://localhost/dummy-gmp-manager.xml",
+ "media.gmp-manager.updateEnabled": false,
+ "browser.aboutHomeSnippets.updateUrl": "https://localhost/snippet-dummy",
+ "browser.newtab.url": "about:blank",
+ "browser.search.update": false,
+ "browser.search.suggest.enabled": false,
+ "browser.safebrowsing.phishing.enabled": false,
+ "browser.safebrowsing.provider.google.updateURL": "http://localhost/safebrowsing-dummy/update",
+ "browser.safebrowsing.provider.google.gethashURL": "http://localhost/safebrowsing-dummy/gethash",
+ "browser.safebrowsing.provider.google.reportURL": "http://localhost/safebrowsing-dummy/malwarereport",
+ "browser.selfsupport.url": "https://localhost/selfsupport-dummy",
+ "browser.safebrowsing.provider.mozilla.gethashURL": "http://localhost/safebrowsing-dummy/gethash",
+ "browser.safebrowsing.provider.mozilla.updateURL": "http://localhost/safebrowsing-dummy/update",
+ "browser.newtabpage.directory.source": "data:application/json,{'jetpack':1}",
+ "browser.newtabpage.directory.ping": "",
+ "extensions.update.url": "http://localhost/extensions-dummy/updateURL",
+ "extensions.update.background.url": "http://localhost/extensions-dummy/updateBackgroundURL",
+ "extensions.blocklist.url": "http://localhost/extensions-dummy/blocklistURL",
+ "extensions.webservice.discoverURL": "http://localhost/extensions-dummy/discoveryURL",
+ "extensions.getAddons.maxResults": 0,
+ "services.blocklist.base": "http://localhost/dummy-kinto/v1",
+ "geo.wifi.uri": "http://localhost/location-dummy/locationURL",
+ "browser.search.geoip.url": "http://localhost/location-dummy/locationURL",
+ "browser.search.isUS": true,
+ "browser.search.countryCode": "US",
+ "geo.wifi.uri": "http://localhost/extensions-dummy/geowifiURL",
+ "geo.wifi.scan": false,
+ "browser.webapps.checkForUpdates": 0,
+ "identity.fxaccounts.auth.uri": "http://localhost/fxa-dummy/"
+}
diff --git a/addon-sdk/source/test/preferences/test-e10s-preferences.js b/addon-sdk/source/test/preferences/test-e10s-preferences.js
new file mode 100644
index 000000000..ed09e5fa3
--- /dev/null
+++ b/addon-sdk/source/test/preferences/test-e10s-preferences.js
@@ -0,0 +1,15 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+var extend = require("lodash").extend
+
+var e10sOn = require("./e10s-on.json");
+var commonPrefs = require("./common.json");
+var testPrefs = require("./test.json");
+var fxPrefs = require("./firefox.json");
+var disconnectionPrefs = require("./no-connections.json");
+
+var prefs = extend({}, e10sOn, commonPrefs, testPrefs, disconnectionPrefs, fxPrefs);
+module.exports = prefs;
diff --git a/addon-sdk/source/test/preferences/test-preferences.js b/addon-sdk/source/test/preferences/test-preferences.js
new file mode 100644
index 000000000..5cdb4a292
--- /dev/null
+++ b/addon-sdk/source/test/preferences/test-preferences.js
@@ -0,0 +1,15 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+var extend = require("lodash").extend
+
+var e10sOff = require("./e10s-off.json");
+var commonPrefs = require("./common.json");
+var testPrefs = require("./test.json");
+var fxPrefs = require("./firefox.json");
+var disconnectionPrefs = require("./no-connections.json");
+
+var prefs = extend({}, e10sOff, commonPrefs, testPrefs, disconnectionPrefs, fxPrefs);
+module.exports = prefs;
diff --git a/addon-sdk/source/test/preferences/test.json b/addon-sdk/source/test/preferences/test.json
new file mode 100644
index 000000000..d34061fb8
--- /dev/null
+++ b/addon-sdk/source/test/preferences/test.json
@@ -0,0 +1,46 @@
+{
+ "browser.console.showInPanel": true,
+ "browser.startup.page": 0,
+ "browser.firstrun.show.localepicker": false,
+ "browser.firstrun.show.uidiscovery": false,
+ "browser.ui.layout.tablet": 0,
+ "dom.disable_open_during_load": false,
+ "dom.experimental_forms": true,
+ "dom.forms.number": true,
+ "dom.forms.color": true,
+ "dom.max_script_run_time": 0,
+ "hangmonitor.timeout": 0,
+ "dom.max_chrome_script_run_time": 0,
+ "dom.popup_maximum": -1,
+ "dom.send_after_paint_to_content": true,
+ "dom.successive_dialog_time_limit": 0,
+ "browser.shell.checkDefaultBrowser": false,
+ "shell.checkDefaultClient": false,
+ "browser.warnOnQuit": false,
+ "accessibility.typeaheadfind.autostart": false,
+ "browser.EULA.override": true,
+ "gfx.color_management.force_srgb": true,
+ "network.manage-offline-status": false,
+ "network.http.speculative-parallel-limit": 0,
+ "test.mousescroll": true,
+ "security.default_personal_cert": "Select Automatically",
+ "network.http.prompt-temp-redirect": false,
+ "security.warn_viewing_mixed": false,
+ "extensions.defaultProviders.enabled": true,
+ "datareporting.policy.dataSubmissionPolicyBypassNotification": true,
+ "layout.css.report_errors": true,
+ "layout.css.grid.enabled": true,
+ "layout.spammy_warnings.enabled": false,
+ "dom.mozSettings.enabled": true,
+ "network.http.bypass-cachelock-threshold": 200000,
+ "geo.provider.testing": true,
+ "browser.pagethumbnails.capturing_disabled": true,
+ "browser.download.panel.shown": true,
+ "general.useragent.updates.enabled": false,
+ "media.eme.enabled": true,
+ "media.eme.apiVisible": true,
+ "dom.ipc.tabs.shutdownTimeoutSecs": 0,
+ "general.useragent.locale": "en-US",
+ "intl.locale.matchOS": "en-US",
+ "dom.indexedDB.experimental": true
+}
diff --git a/addon-sdk/source/test/private-browsing/helper.js b/addon-sdk/source/test/private-browsing/helper.js
new file mode 100644
index 000000000..4a400b95b
--- /dev/null
+++ b/addon-sdk/source/test/private-browsing/helper.js
@@ -0,0 +1,58 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const xulApp = require("sdk/system/xul-app");
+const { open: openWindow, getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { openTab, getTabContentWindow, getActiveTab, setTabURL, closeTab } = require('sdk/tabs/utils');
+const promise = require("sdk/core/promise");
+const windowHelpers = require('sdk/window/helpers');
+const events = require("sdk/system/events");
+
+exports.openWebpage = function openWebpage(url, enablePrivate) {
+ if (xulApp.is("Fennec")) {
+ let chromeWindow = getMostRecentBrowserWindow();
+ let rawTab = openTab(chromeWindow, url, {
+ isPrivate: enablePrivate
+ });
+
+ return {
+ ready: promise.resolve(getTabContentWindow(rawTab)),
+ close: function () {
+ closeTab(rawTab);
+ // Returns a resolved promise as there is no need to wait
+ return promise.resolve();
+ }
+ };
+ }
+ else {
+ let win = openWindow(null, {
+ features: {
+ private: enablePrivate
+ }
+ });
+ let deferred = promise.defer();
+
+ // Wait for delayed startup code to be executed, in order to ensure
+ // that the window is really ready
+ events.on("browser-delayed-startup-finished", function onReady({subject}) {
+ if (subject == win) {
+ events.off("browser-delayed-startup-finished", onReady);
+ deferred.resolve(win);
+
+ let rawTab = getActiveTab(win);
+ setTabURL(rawTab, url);
+ deferred.resolve(getTabContentWindow(rawTab));
+ }
+ }, true);
+
+ return {
+ ready: deferred.promise,
+ close: function () {
+ return windowHelpers.close(win);
+ }
+ };
+ }
+ return null;
+}
diff --git a/addon-sdk/source/test/private-browsing/tabs.js b/addon-sdk/source/test/private-browsing/tabs.js
new file mode 100644
index 000000000..8564f0735
--- /dev/null
+++ b/addon-sdk/source/test/private-browsing/tabs.js
@@ -0,0 +1,25 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Ci } = require('chrome');
+const { openTab, closeTab } = require('sdk/tabs/utils');
+const { browserWindows } = require('sdk/windows');
+const { isPrivate } = require('sdk/private-browsing');
+
+exports.testIsPrivateOnTab = function(assert) {
+ let window = browserWindows.activeWindow;
+ assert.ok(!isPrivate(chromeWindow), 'the top level window is not private');
+
+ let rawTab = openTab(chromeWindow, 'data:text/html,<h1>Hi!</h1>', {
+ isPrivate: true
+ });
+
+ // test that the tab is private
+ assert.ok(rawTab.browser.docShell.QueryInterface(Ci.nsILoadContext).usePrivateBrowsing);
+ assert.ok(isPrivate(rawTab.browser.contentWindow));
+ assert.ok(isPrivate(rawTab.browser));
+
+ closeTab(rawTab);
+};
diff --git a/addon-sdk/source/test/private-browsing/windows.js b/addon-sdk/source/test/private-browsing/windows.js
new file mode 100644
index 000000000..e6f9c53b5
--- /dev/null
+++ b/addon-sdk/source/test/private-browsing/windows.js
@@ -0,0 +1,115 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { onFocus, openDialog, open } = require('sdk/window/utils');
+const { open: openPromise, close, focus, promise } = require('sdk/window/helpers');
+const { isPrivate } = require('sdk/private-browsing');
+const { getMode } = require('sdk/private-browsing/utils');
+const { browserWindows: windows } = require('sdk/windows');
+const { defer } = require('sdk/core/promise');
+const tabs = require('sdk/tabs');
+const { getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { cleanUI } = require("sdk/test/utils");
+
+// test openDialog() from window/utils with private option
+// test isActive state in pwpb case
+// test isPrivate on ChromeWindow
+exports.testPerWindowPrivateBrowsingGetter = function*(assert) {
+ let win = openDialog({ private: true });
+
+ yield promise(win, 'DOMContentLoaded');
+
+ assert.equal(getMode(win), true, 'Newly opened window is in PB mode');
+ assert.ok(isPrivate(win), 'isPrivate(window) is true');
+
+ yield close(win);
+}
+
+// test open() from window/utils with private feature
+// test isActive state in pwpb case
+// test isPrivate on ChromeWindow
+exports.testPerWindowPrivateBrowsingGetter = function*(assert) {
+ let win = open('chrome://browser/content/browser.xul', {
+ features: {
+ private: true
+ }
+ });
+
+ yield promise(win, 'DOMContentLoaded');
+ assert.equal(getMode(win), true, 'Newly opened window is in PB mode');
+ assert.ok(isPrivate(win), 'isPrivate(window) is true');
+ yield close(win)
+}
+
+exports.testIsPrivateOnWindowOpen = function*(assert) {
+ let window = yield new Promise(resolve => {
+ windows.open({
+ isPrivate: true,
+ onOpen: resolve
+ });
+ });
+
+ assert.equal(isPrivate(window), false, 'isPrivate for a window is true when it should be');
+ assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be');
+
+ yield cleanUI();
+}
+
+exports.testIsPrivateOnWindowOpenFromPrivate = function(assert, done) {
+ // open a private window
+ openPromise(null, {
+ features: {
+ private: true,
+ chrome: true,
+ titlebar: true,
+ toolbar: true
+ }
+ }).then(focus).then(function(window) {
+ let { promise, resolve } = defer();
+
+ assert.equal(isPrivate(window), true, 'the only open window is private');
+
+ windows.open({
+ url: 'about:blank',
+ onOpen: function(w) {
+ assert.equal(isPrivate(w), false, 'new test window is not private');
+ w.close(() => resolve(window));
+ }
+ });
+
+ return promise;
+ }).then(close).
+ then(done, assert.fail);
+};
+
+exports.testOpenTabWithPrivateWindow = function*(assert) {
+ let window = getMostRecentBrowserWindow().OpenBrowserWindow({ private: true });
+
+ assert.pass("loading new private window");
+
+ yield promise(window, 'load').then(focus);
+
+ assert.equal(isPrivate(window), true, 'the focused window is private');
+
+ yield new Promise(resolve => tabs.open({
+ url: 'about:blank',
+ onOpen: (tab) => {
+ assert.equal(isPrivate(tab), false, 'the opened tab is not private');
+ tab.close(resolve);
+ }
+ }));
+
+ yield close(window);
+};
+
+exports.testIsPrivateOnWindowOff = function(assert, done) {
+ windows.open({
+ onOpen: function(window) {
+ assert.equal(isPrivate(window), false, 'isPrivate for a window is false when it should be');
+ assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be');
+ window.close(done);
+ }
+ })
+}
diff --git a/addon-sdk/source/test/querystring/test-querystring.js b/addon-sdk/source/test/querystring/test-querystring.js
new file mode 100644
index 000000000..9a4fbe573
--- /dev/null
+++ b/addon-sdk/source/test/querystring/test-querystring.js
@@ -0,0 +1,205 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+"use strict";
+
+// test using assert
+var qs = require('sdk/querystring');
+
+// folding block, commented to pass gjslint
+// {{{
+// [ wonkyQS, canonicalQS, obj ]
+var qsTestCases = [
+ ['foo=918854443121279438895193',
+ 'foo=918854443121279438895193',
+ {'foo': '918854443121279438895193'}],
+ ['foo=bar', 'foo=bar', {'foo': 'bar'}],
+ //['foo=bar&foo=quux', 'foo=bar&foo=quux', {'foo': ['bar', 'quux']}],
+ ['foo=1&bar=2', 'foo=1&bar=2', {'foo': '1', 'bar': '2'}],
+ // ['my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F',
+ // 'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F',
+ // {'my weird field': 'q1!2"\'w$5&7/z8)?' }],
+ ['foo%3Dbaz=bar', 'foo%3Dbaz=bar', {'foo=baz': 'bar'}],
+ ['foo=baz=bar', 'foo=baz%3Dbar', {'foo': 'baz=bar'}],
+ /*
+ ['str=foo&arr=1&arr=2&arr=3&somenull=&undef=',
+ 'str=foo&arr=1&arr=2&arr=3&somenull=&undef=',
+ { 'str': 'foo',
+ 'arr': ['1', '2', '3'],
+ 'somenull': '',
+ 'undef': ''}],
+ */
+ //[' foo = bar ', '%20foo%20=%20bar%20', {' foo ': ' bar '}],
+ // disable test that fails ['foo=%zx', 'foo=%25zx', {'foo': '%zx'}],
+ ['foo=%EF%BF%BD', 'foo=%EF%BF%BD', {'foo': '\ufffd' }]
+];
+
+// [ wonkyQS, canonicalQS, obj ]
+var qsColonTestCases = [
+ ['foo:bar', 'foo:bar', {'foo': 'bar'}],
+ //['foo:bar;foo:quux', 'foo:bar;foo:quux', {'foo': ['bar', 'quux']}],
+ ['foo:1&bar:2;baz:quux',
+ 'foo:1%26bar%3A2;baz:quux',
+ {'foo': '1&bar:2', 'baz': 'quux'}],
+ ['foo%3Abaz:bar', 'foo%3Abaz:bar', {'foo:baz': 'bar'}],
+ ['foo:baz:bar', 'foo:baz%3Abar', {'foo': 'baz:bar'}]
+];
+
+// [wonkyObj, qs, canonicalObj]
+var extendedFunction = function() {};
+extendedFunction.prototype = {a: 'b'};
+var qsWeirdObjects = [
+ //[{regexp: /./g}, 'regexp=', {'regexp': ''}],
+ //[{regexp: new RegExp('.', 'g')}, 'regexp=', {'regexp': ''}],
+ //[{fn: function() {}}, 'fn=', {'fn': ''}],
+ //[{fn: new Function('')}, 'fn=', {'fn': ''}],
+ //[{math: Math}, 'math=', {'math': ''}],
+ //[{e: extendedFunction}, 'e=', {'e': ''}],
+ //[{d: new Date()}, 'd=', {'d': ''}],
+ //[{d: Date}, 'd=', {'d': ''}],
+ //[{f: new Boolean(false), t: new Boolean(true)}, 'f=&t=', {'f': '', 't': ''}],
+ [{f: false, t: true}, 'f=false&t=true', {'f': 'false', 't': 'true'}],
+ //[{n: null}, 'n=', {'n': ''}],
+ //[{nan: NaN}, 'nan=', {'nan': ''}],
+ //[{inf: Infinity}, 'inf=', {'inf': ''}]
+];
+// }}}
+
+var qsNoMungeTestCases = [
+ ['', {}],
+ //['foo=bar&foo=baz', {'foo': ['bar', 'baz']}],
+ ['blah=burp', {'blah': 'burp'}],
+ //['gragh=1&gragh=3&goo=2', {'gragh': ['1', '3'], 'goo': '2'}],
+ ['frappucino=muffin&goat%5B%5D=scone&pond=moose',
+ {'frappucino': 'muffin', 'goat[]': 'scone', 'pond': 'moose'}],
+ ['trololol=yes&lololo=no', {'trololol': 'yes', 'lololo': 'no'}]
+];
+
+exports['test basic'] = function(assert) {
+ assert.strictEqual('918854443121279438895193',
+ qs.parse('id=918854443121279438895193').id,
+ 'prase id=918854443121279438895193');
+};
+
+exports['test that the canonical qs is parsed properly'] = function(assert) {
+ qsTestCases.forEach(function(testCase) {
+ assert.deepEqual(testCase[2], qs.parse(testCase[0]),
+ 'parse ' + testCase[0]);
+ });
+};
+
+
+exports['test that the colon test cases can do the same'] = function(assert) {
+ qsColonTestCases.forEach(function(testCase) {
+ assert.deepEqual(testCase[2], qs.parse(testCase[0], ';', ':'),
+ 'parse ' + testCase[0] + ' -> ; :');
+ });
+};
+
+exports['test the weird objects, that they get parsed properly'] = function(assert) {
+ qsWeirdObjects.forEach(function(testCase) {
+ assert.deepEqual(testCase[2], qs.parse(testCase[1]),
+ 'parse ' + testCase[1]);
+ });
+};
+
+exports['test non munge test cases'] = function(assert) {
+ qsNoMungeTestCases.forEach(function(testCase) {
+ assert.deepEqual(testCase[0], qs.stringify(testCase[1], '&', '=', false),
+ 'stringify ' + JSON.stringify(testCase[1]) + ' -> & =');
+ });
+};
+
+exports['test the nested qs-in-qs case'] = function(assert) {
+ var f = qs.parse('a=b&q=x%3Dy%26y%3Dz');
+ f.q = qs.parse(f.q);
+ assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } },
+ 'parse a=b&q=x%3Dy%26y%3Dz');
+};
+
+exports['test nested in colon'] = function(assert) {
+ var f = qs.parse('a:b;q:x%3Ay%3By%3Az', ';', ':');
+ f.q = qs.parse(f.q, ';', ':');
+ assert.deepEqual(f, { a: 'b', q: { x: 'y', y: 'z' } },
+ 'parse a:b;q:x%3Ay%3By%3Az -> ; :');
+};
+
+exports['test stringifying'] = function(assert) {
+ qsTestCases.forEach(function(testCase) {
+ assert.equal(testCase[1], qs.stringify(testCase[2]),
+ 'stringify ' + JSON.stringify(testCase[2]));
+ });
+
+ qsColonTestCases.forEach(function(testCase) {
+ assert.equal(testCase[1], qs.stringify(testCase[2], ';', ':'),
+ 'stringify ' + JSON.stringify(testCase[2]) + ' -> ; :');
+ });
+
+ qsWeirdObjects.forEach(function(testCase) {
+ assert.equal(testCase[1], qs.stringify(testCase[0]),
+ 'stringify ' + JSON.stringify(testCase[0]));
+ });
+};
+
+exports['test stringifying nested'] = function(assert) {
+ var f = qs.stringify({
+ a: 'b',
+ q: qs.stringify({
+ x: 'y',
+ y: 'z'
+ })
+ });
+ assert.equal(f, 'a=b&q=x%3Dy%26y%3Dz',
+ JSON.stringify({
+ a: 'b',
+ 'qs.stringify -> q': {
+ x: 'y',
+ y: 'z'
+ }
+ }));
+
+ var threw = false;
+ try { qs.parse(undefined); } catch(error) { threw = true; }
+ assert.ok(!threw, "does not throws on undefined");
+};
+
+exports['test nested in colon'] = function(assert) {
+ var f = qs.stringify({
+ a: 'b',
+ q: qs.stringify({
+ x: 'y',
+ y: 'z'
+ }, ';', ':')
+ }, ';', ':');
+ assert.equal(f, 'a:b;q:x%3Ay%3By%3Az',
+ 'stringify ' + JSON.stringify({
+ a: 'b',
+ 'qs.stringify -> q': {
+ x: 'y',
+ y: 'z'
+ }
+ }) + ' -> ; : ');
+
+
+ assert.deepEqual({}, qs.parse(), 'parse undefined');
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/sidebar/utils.js b/addon-sdk/source/test/sidebar/utils.js
new file mode 100644
index 000000000..21002ba49
--- /dev/null
+++ b/addon-sdk/source/test/sidebar/utils.js
@@ -0,0 +1,74 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+module.metadata = {
+ 'engines': {
+ 'Firefox': '*'
+ }
+};
+
+const { Cu } = require('chrome');
+const { getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { fromIterator } = require('sdk/util/array');
+
+const BUILTIN_SIDEBAR_MENUITEMS = exports.BUILTIN_SIDEBAR_MENUITEMS = [
+ 'menu_socialSidebar',
+ 'menu_historySidebar',
+ 'menu_bookmarksSidebar',
+ 'menu_tabsSidebar',
+];
+
+function isSidebarShowing(window) {
+ window = window || getMostRecentBrowserWindow();
+ let sidebar = window.document.getElementById('sidebar-box');
+ return !sidebar.hidden;
+}
+exports.isSidebarShowing = isSidebarShowing;
+
+function getSidebarMenuitems(window) {
+ window = window || getMostRecentBrowserWindow();
+ return fromIterator(window.document.querySelectorAll('#viewSidebarMenu menuitem'));
+}
+exports.getSidebarMenuitems = getSidebarMenuitems;
+
+function getExtraSidebarMenuitems() {
+ let menuitems = getSidebarMenuitems();
+ return menuitems.filter(function(mi) {
+ return BUILTIN_SIDEBAR_MENUITEMS.indexOf(mi.getAttribute('id')) < 0;
+ });
+}
+exports.getExtraSidebarMenuitems = getExtraSidebarMenuitems;
+
+function makeID(id) {
+ return 'jetpack-sidebar-' + id;
+}
+exports.makeID = makeID;
+
+function simulateCommand(ele) {
+ let window = ele.ownerDocument.defaultView;
+ let { document } = window;
+ var evt = document.createEvent('XULCommandEvent');
+ evt.initCommandEvent('command', true, true, window,
+ 0, false, false, false, false, null);
+ ele.dispatchEvent(evt);
+}
+exports.simulateCommand = simulateCommand;
+
+function simulateClick(ele) {
+ let window = ele.ownerDocument.defaultView;
+ let { document } = window;
+ let evt = document.createEvent('MouseEvents');
+ evt.initMouseEvent('click', true, true, window,
+ 0, 0, 0, 0, 0, false, false, false, false, 0, null);
+ ele.dispatchEvent(evt);
+}
+exports.simulateClick = simulateClick;
+
+// OSX and Windows exhibit different behaviors when 'checked' is false,
+// so compare against the consistent 'true'. See bug 894809.
+function isChecked(node) {
+ return node.getAttribute('checked') === 'true';
+};
+exports.isChecked = isChecked;
diff --git a/addon-sdk/source/test/tabs/test-fennec-tabs.js b/addon-sdk/source/test/tabs/test-fennec-tabs.js
new file mode 100644
index 000000000..d7e362d41
--- /dev/null
+++ b/addon-sdk/source/test/tabs/test-fennec-tabs.js
@@ -0,0 +1,595 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Cc, Ci } = require('chrome');
+const { Loader, LoaderWithHookedConsole } = require('sdk/test/loader');
+const timer = require('sdk/timers');
+const tabs = require('sdk/tabs');
+const windows = require('sdk/windows');
+const { set: setPref } = require("sdk/preferences/service");
+const DEPRECATE_PREF = "devtools.errorconsole.deprecation_warnings";
+
+const tabsLen = tabs.length;
+const URL = 'data:text/html;charset=utf-8,<html><head><title>#title#</title></head></html>';
+
+// Fennec error message dispatched on all currently unimplement tab features,
+// that match LoaderWithHookedConsole messages object pattern
+const ERR_FENNEC_MSG = {
+ type: "error",
+ msg: "This method is not yet supported by Fennec"
+};
+
+// TEST: tab unloader
+exports.testAutomaticDestroy = function(assert, done) {
+ let called = false;
+
+ let loader2 = Loader(module);
+ let loader3 = Loader(module);
+ let tabs2 = loader2.require('sdk/tabs');
+ let tabs3 = loader3.require('sdk/tabs');
+ let tabs2Len = tabs2.length;
+
+ tabs2.on('open', function onOpen(tab) {
+ assert.fail("an onOpen listener was called that should not have been");
+ called = true;
+ });
+ tabs2.on('ready', function onReady(tab) {
+ assert.fail("an onReady listener was called that should not have been");
+ called = true;
+ });
+ tabs2.on('select', function onSelect(tab) {
+ assert.fail("an onSelect listener was called that should not have been");
+ called = true;
+ });
+ tabs2.on('close', function onClose(tab) {
+ assert.fail("an onClose listener was called that should not have been");
+ called = true;
+ });
+ loader2.unload();
+
+ tabs3.on('open', function onOpen(tab) {
+ assert.pass("an onOpen listener was called for tabs3");
+
+ tab.on('ready', function onReady(tab) {
+ assert.fail("an onReady listener was called that should not have been");
+ called = true;
+ });
+ tab.on('select', function onSelect(tab) {
+ assert.fail("an onSelect listener was called that should not have been");
+ called = true;
+ });
+ tab.on('close', function onClose(tab) {
+ assert.fail("an onClose listener was called that should not have been");
+ called = true;
+ });
+ });
+ tabs3.open(URL.replace(/#title#/, 'tabs3'));
+ loader3.unload();
+
+ // Fire a tab event and ensure that the destroyed tab is inactive
+ tabs.once('open', function(tab) {
+ assert.pass('tabs.once("open") works!');
+
+ assert.equal(tabs2Len, tabs2.length, "tabs2 length was not changed");
+ assert.equal(tabs.length, (tabs2.length+2), "tabs.length > tabs2.length");
+
+ tab.once('ready', function() {
+ assert.pass('tab.once("ready") works!');
+
+ tab.once('close', function() {
+ assert.pass('tab.once("close") works!');
+
+ timer.setTimeout(function () {
+ assert.ok(!called, "Unloaded tab module is destroyed and inactive");
+
+ // end test
+ done();
+ });
+ });
+
+ tab.close();
+ });
+ });
+
+ tabs.open('data:text/html;charset=utf-8,foo');
+};
+
+// TEST: tab properties
+exports.testTabProperties = function(assert, done) {
+ let url = "data:text/html;charset=utf-8,<html><head><title>foo</title></head><body>foo</body></html>";
+ let tabsLen = tabs.length;
+ tabs.open({
+ url: url,
+ onReady: function(tab) {
+ assert.equal(tab.title, "foo", "title of the new tab matches");
+ assert.equal(tab.url, url, "URL of the new tab matches");
+ assert.equal(tab.style, null, "style of the new tab matches");
+ assert.equal(tab.index, tabsLen, "index of the new tab matches");
+ assert.notEqual(tab.getThumbnail(), null, "thumbnail of the new tab matches");
+ assert.notEqual(tab.id, null, "a tab object always has an id property");
+
+ tab.close(function() {
+ loader.unload();
+
+ // end test
+ done();
+ });
+ }
+ });
+};
+
+// TEST: tabs iterator and length property
+exports.testTabsIteratorAndLength = function(assert, done) {
+ let newTabs = [];
+ let startCount = 0;
+ for (let t of tabs) startCount++;
+
+ assert.equal(startCount, tabs.length, "length property is correct");
+
+ let url = "data:text/html;charset=utf-8,testTabsIteratorAndLength";
+ tabs.open({url: url, onOpen: tab => newTabs.push(tab)});
+ tabs.open({url: url, onOpen: tab => newTabs.push(tab)});
+ tabs.open({
+ url: url,
+ onOpen: function(tab) {
+ let count = 0;
+ for (let t of tabs) count++;
+ assert.equal(count, startCount + 3, "iterated tab count matches");
+ assert.equal(startCount + 3, tabs.length, "iterated tab count matches length property");
+
+ let newTabsLength = newTabs.length;
+ newTabs.forEach(t => t.close(function() {
+ if (--newTabsLength > 0) return;
+
+ tab.close(done);
+ }));
+ }
+ });
+};
+
+// TEST: tab.url setter
+exports.testTabLocation = function(assert, done) {
+ let url1 = "data:text/html;charset=utf-8,foo";
+ let url2 = "data:text/html;charset=utf-8,bar";
+
+ tabs.on('ready', function onReady(tab) {
+ if (tab.url != url2)
+ return;
+
+ tabs.removeListener('ready', onReady);
+ assert.pass("tab loaded the correct url");
+
+ tab.close(done);
+ });
+
+ tabs.open({
+ url: url1,
+ onOpen: function(tab) {
+ tab.url = url2;
+ }
+ });
+};
+
+// TEST: tab.move()
+exports.testTabMove = function(assert, done) {
+ let { loader, messages } = LoaderWithHookedConsole();
+ let tabs = loader.require('sdk/tabs');
+
+ let url = "data:text/html;charset=utf-8,testTabMove";
+
+ tabs.open({
+ url: url,
+ onOpen: function(tab1) {
+ assert.ok(tab1.index >= 0, "opening a tab returns a tab w/ valid index");
+
+ tabs.open({
+ url: url,
+ onOpen: function(tab) {
+ let i = tab.index;
+ assert.ok(tab.index > tab1.index, "2nd tab has valid index");
+ tab.index = 0;
+ assert.equal(tab.index, i, "tab index after move matches");
+ assert.equal(JSON.stringify(messages),
+ JSON.stringify([ERR_FENNEC_MSG]),
+ "setting tab.index logs error");
+ // end test
+ tab1.close(() => tab.close(function() {
+ loader.unload();
+ done();
+ }));
+ }
+ });
+ }
+ });
+};
+
+// TEST: open tab with default options
+exports.testTabsOpen_alt = function(assert, done) {
+ let { loader, messages } = LoaderWithHookedConsole();
+ let tabs = loader.require('sdk/tabs');
+ let url = "data:text/html;charset=utf-8,default";
+
+ tabs.open({
+ url: url,
+ onReady: function(tab) {
+ assert.equal(tab.url, url, "URL of the new tab matches");
+ assert.equal(tabs.activeTab, tab, "URL of active tab in the current window matches");
+ assert.equal(tab.isPinned, false, "The new tab is not pinned");
+ assert.equal(messages.length, 1, "isPinned logs error");
+
+ // end test
+ tab.close(function() {
+ loader.unload();
+ done();
+ });
+ }
+ });
+};
+
+// TEST: open pinned tab
+exports.testOpenPinned_alt = function(assert, done) {
+ let { loader, messages } = LoaderWithHookedConsole();
+ let tabs = loader.require('sdk/tabs');
+ let url = "about:blank";
+
+ tabs.open({
+ url: url,
+ isPinned: true,
+ onOpen: function(tab) {
+ assert.equal(tab.isPinned, false, "The new tab is pinned");
+ // We get two error message: one for tabs.open's isPinned argument
+ // and another one for tab.isPinned
+ assert.equal(JSON.stringify(messages),
+ JSON.stringify([ERR_FENNEC_MSG, ERR_FENNEC_MSG]),
+ "isPinned logs error");
+
+ // end test
+ tab.close(function() {
+ loader.unload();
+ done();
+ });
+ }
+ });
+};
+
+// TEST: pin/unpin opened tab
+exports.testPinUnpin_alt = function(assert, done) {
+ let { loader, messages } = LoaderWithHookedConsole();
+ let tabs = loader.require('sdk/tabs');
+ let url = "data:text/html;charset=utf-8,default";
+
+ tabs.open({
+ url: url,
+ onOpen: function(tab) {
+ tab.pin();
+ assert.equal(tab.isPinned, false, "The tab was pinned correctly");
+ assert.equal(JSON.stringify(messages),
+ JSON.stringify([ERR_FENNEC_MSG, ERR_FENNEC_MSG]),
+ "tab.pin() logs error");
+
+ // Clear console messages for the following test
+ messages.length = 0;
+
+ tab.unpin();
+ assert.equal(tab.isPinned, false, "The tab was unpinned correctly");
+ assert.equal(JSON.stringify(messages),
+ JSON.stringify([ERR_FENNEC_MSG, ERR_FENNEC_MSG]),
+ "tab.unpin() logs error");
+
+ // end test
+ tab.close(function() {
+ loader.unload();
+ done();
+ });
+ }
+ });
+};
+
+// TEST: open tab in background
+exports.testInBackground = function(assert, done) {
+ let activeUrl = tabs.activeTab.url;
+ let url = "data:text/html;charset=utf-8,background";
+ let window = windows.browserWindows.activeWindow;
+ tabs.once('ready', function onReady(tab) {
+ assert.equal(tabs.activeTab.url, activeUrl, "URL of active tab has not changed");
+ assert.equal(tab.url, url, "URL of the new background tab matches");
+ assert.equal(windows.browserWindows.activeWindow, window, "a new window was not opened");
+ assert.notEqual(tabs.activeTab.url, url, "URL of active tab is not the new URL");
+
+ // end test
+ tab.close(done);
+ });
+
+ tabs.open({
+ url: url,
+ inBackground: true
+ });
+};
+
+// TEST: open tab in new window
+exports.testOpenInNewWindow = function(assert, done) {
+ let url = "data:text/html;charset=utf-8,newwindow";
+ let window = windows.browserWindows.activeWindow;
+
+ tabs.open({
+ url: url,
+ inNewWindow: true,
+ onReady: function(tab) {
+ assert.equal(windows.browserWindows.length, 1, "a new window was not opened");
+ assert.equal(windows.browserWindows.activeWindow, window, "old window is active");
+ assert.equal(tab.url, url, "URL of the new tab matches");
+ assert.equal(tabs.activeTab, tab, "tab is the activeTab");
+
+ tab.close(done);
+ }
+ });
+};
+
+// TEST: onOpen event handler
+exports.testTabsEvent_onOpen = function(assert, done) {
+ let url = URL.replace('#title#', 'testTabsEvent_onOpen');
+ let eventCount = 0;
+
+ // add listener via property assignment
+ function listener1(tab) {
+ eventCount++;
+ };
+ tabs.on('open', listener1);
+
+ // add listener via collection add
+ tabs.on('open', function listener2(tab) {
+ assert.equal(++eventCount, 2, "both listeners notified");
+ tabs.removeListener('open', listener1);
+ tabs.removeListener('open', listener2);
+
+ // ends test
+ tab.close(done);
+ });
+
+ tabs.open(url);
+};
+
+// TEST: onClose event handler
+exports.testTabsEvent_onClose = function(assert, done) {
+ let url = "data:text/html;charset=utf-8,onclose";
+ let eventCount = 0;
+
+ // add listener via property assignment
+ function listener1(tab) {
+ eventCount++;
+ }
+ tabs.on('close', listener1);
+
+ // add listener via collection add
+ tabs.on('close', function listener2(tab) {
+ assert.equal(++eventCount, 2, "both listeners notified");
+ tabs.removeListener('close', listener1);
+ tabs.removeListener('close', listener2);
+
+ // end test
+ done();
+ });
+
+ tabs.on('ready', function onReady(tab) {
+ tabs.removeListener('ready', onReady);
+ tab.close();
+ });
+
+ tabs.open(url);
+};
+
+// TEST: onClose event handler when a window is closed
+exports.testTabsEvent_onCloseWindow = function(assert, done) {
+ let closeCount = 0, individualCloseCount = 0;
+ function listener() {
+ closeCount++;
+ }
+ tabs.on('close', listener);
+
+ // One tab is already open with the window
+ let openTabs = 0;
+ function testCasePossiblyLoaded(tab) {
+ tab.close(function() {
+ if (++openTabs == 3) {
+ tabs.removeListener("close", listener);
+
+ assert.equal(closeCount, 3, "Correct number of close events received");
+ assert.equal(individualCloseCount, 3,
+ "Each tab with an attached onClose listener received a close " +
+ "event when the window was closed");
+
+ done();
+ }
+ });
+ }
+
+ tabs.open({
+ url: "data:text/html;charset=utf-8,tab2",
+ onOpen: testCasePossiblyLoaded,
+ onClose: () => individualCloseCount++
+ });
+
+ tabs.open({
+ url: "data:text/html;charset=utf-8,tab3",
+ onOpen: testCasePossiblyLoaded,
+ onClose: () => individualCloseCount++
+ });
+
+ tabs.open({
+ url: "data:text/html;charset=utf-8,tab4",
+ onOpen: testCasePossiblyLoaded,
+ onClose: () => individualCloseCount++
+ });
+};
+
+// TEST: onReady event handler
+exports.testTabsEvent_onReady = function(assert, done) {
+ let url = "data:text/html;charset=utf-8,onready";
+ let eventCount = 0;
+
+ // add listener via property assignment
+ function listener1(tab) {
+ eventCount++;
+ };
+ tabs.on('ready', listener1);
+
+ // add listener via collection add
+ tabs.on('ready', function listener2(tab) {
+ assert.equal(++eventCount, 2, "both listeners notified");
+ tabs.removeListener('ready', listener1);
+ tabs.removeListener('ready', listener2);
+
+ // end test
+ tab.close(done);
+ });
+
+ tabs.open(url);
+};
+
+// TEST: onActivate event handler
+exports.testTabsEvent_onActivate = function(assert, done) {
+ let url = "data:text/html;charset=utf-8,onactivate";
+ let eventCount = 0;
+
+ // add listener via property assignment
+ function listener1(tab) {
+ eventCount++;
+ };
+ tabs.on('activate', listener1);
+
+ // add listener via collection add
+ tabs.on('activate', function listener2(tab) {
+ assert.equal(++eventCount, 2, "both listeners notified");
+ assert.equal(tab, tabs.activeTab, 'the active tab is correct');
+ tabs.removeListener('activate', listener1);
+ tabs.removeListener('activate', listener2);
+
+ // end test
+ tab.close(done);
+ });
+
+ tabs.open(url);
+};
+
+// TEST: onDeactivate event handler
+exports.testTabsEvent_onDeactivate = function(assert, done) {
+ let url = "data:text/html;charset=utf-8,ondeactivate";
+ let eventCount = 0;
+
+ // add listener via property assignment
+ function listener1(tab) {
+ eventCount++;
+ };
+ tabs.on('deactivate', listener1);
+
+ // add listener via collection add
+ tabs.on('deactivate', function listener2(tab) {
+ assert.equal(++eventCount, 2, 'both listeners notified');
+ assert.notEqual(tab, tabs.activeTab, 'the active tab is not the deactivated tab');
+ tabs.removeListener('deactivate', listener1);
+ tabs.removeListener('deactivate', listener2);
+
+ // end test
+ tab.close(done);
+ });
+
+ tabs.on('activate', function onActivate(tab) {
+ tabs.removeListener('activate', onActivate);
+ tabs.open("data:text/html;charset=utf-8,foo");
+ tab.close();
+ });
+
+ tabs.open(url);
+};
+
+// TEST: per-tab event handlers
+exports.testPerTabEvents = function(assert, done) {
+ let eventCount = 0;
+
+ tabs.open({
+ url: "data:text/html;charset=utf-8,foo",
+ onOpen: function(tab) {
+ // add listener via property assignment
+ function listener1() {
+ eventCount++;
+ };
+ tab.on('ready', listener1);
+
+ // add listener via collection add
+ tab.on('ready', function listener2() {
+ assert.equal(eventCount, 1, "both listeners notified");
+ tab.removeListener('ready', listener1);
+ tab.removeListener('ready', listener2);
+
+ // end test
+ tab.close(done);
+ });
+ }
+ });
+};
+
+exports.testUniqueTabIds = function(assert, done) {
+ var tabs = require('sdk/tabs');
+ var tabIds = {};
+ var steps = [
+ function (index) {
+ tabs.open({
+ url: "data:text/html;charset=utf-8,foo",
+ onOpen: function(tab) {
+ tabIds['tab1'] = tab.id;
+ next(index);
+ }
+ });
+ },
+ function (index) {
+ tabs.open({
+ url: "data:text/html;charset=utf-8,bar",
+ onOpen: function(tab) {
+ tabIds['tab2'] = tab.id;
+ next(index);
+ }
+ });
+ },
+ function (index) {
+ assert.notEqual(tabIds.tab1, tabIds.tab2, "Tab ids should be unique.");
+ done();
+ }
+ ];
+
+ function next(index) {
+ if (index === steps.length) {
+ return;
+ }
+ let fn = steps[index];
+ index++;
+ fn(index);
+ }
+
+ next(0);
+}
+
+exports.testOnLoadEventWithDOM = function(assert, done) {
+ let count = 0;
+ let title = 'testOnLoadEventWithDOM';
+
+ tabs.open({
+ url: 'data:text/html;charset=utf-8,<title>' + title + '</title>',
+ inBackground: true,
+ onLoad: function(tab) {
+ assert.equal(tab.title, title, 'tab passed in as arg, load called');
+
+ if (++count > 1) {
+ assert.pass('onLoad event called on reload');
+ tab.close(done);
+ }
+ else {
+ assert.pass('first onLoad event occured');
+ tab.reload();
+ }
+ }
+ });
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/tabs/test-firefox-tabs.js b/addon-sdk/source/test/tabs/test-firefox-tabs.js
new file mode 100644
index 000000000..368ed02ba
--- /dev/null
+++ b/addon-sdk/source/test/tabs/test-firefox-tabs.js
@@ -0,0 +1,1305 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Cc, Ci } = require('chrome');
+const { Loader } = require('sdk/test/loader');
+const systemEvents = require("sdk/system/events");
+const { setTimeout, setImmediate } = require('sdk/timers');
+const { modelFor } = require('sdk/model/core');
+const { viewFor } = require('sdk/view/core');
+const { getOwnerWindow } = require('sdk/tabs/utils');
+const { windows, onFocus, getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { open, focus, close } = require('sdk/window/helpers');
+const { observer: windowObserver } = require("sdk/windows/observer");
+const tabs = require('sdk/tabs');
+const { browserWindows } = require('sdk/windows');
+const { set: setPref, get: getPref, reset: resetPref } = require("sdk/preferences/service");
+const DEPRECATE_PREF = "devtools.errorconsole.deprecation_warnings";
+const OPEN_IN_NEW_WINDOW_PREF = 'browser.link.open_newwindow';
+const DISABLE_POPUP_PREF = 'dom.disable_open_during_load';
+const fixtures = require("../fixtures");
+const { base64jpeg } = fixtures;
+const { cleanUI, before, after } = require("sdk/test/utils");
+const { wait } = require('../event/helpers');
+
+// Bug 682681 - tab.title should never be empty
+exports.testBug682681_aboutURI = function(assert, done) {
+ let url = 'chrome://browser/locale/tabbrowser.properties';
+ let stringBundle = Cc["@mozilla.org/intl/stringbundle;1"].
+ getService(Ci.nsIStringBundleService).
+ createBundle(url);
+ let emptyTabTitle = stringBundle.GetStringFromName('tabs.emptyTabTitle');
+
+ tabs.on('ready', function onReady(tab) {
+ tabs.removeListener('ready', onReady);
+
+ assert.equal(tab.title,
+ emptyTabTitle,
+ "title of about: tab is not blank");
+
+ tab.close(done);
+ });
+
+ // open a about: url
+ tabs.open({
+ url: "about:blank",
+ inBackground: true
+ });
+};
+
+// related to Bug 682681
+exports.testTitleForDataURI = function(assert, done) {
+ tabs.open({
+ url: "data:text/html;charset=utf-8,<title>tab</title>",
+ inBackground: true,
+ onReady: function(tab) {
+ assert.equal(tab.title, "tab", "data: title is not Connecting...");
+ tab.close(done);
+ }
+ });
+};
+
+// TEST: 'BrowserWindow' instance creation on tab 'activate' event
+// See bug 648244: there was a infinite loop.
+exports.testBrowserWindowCreationOnActivate = function(assert, done) {
+ let windows = require("sdk/windows").browserWindows;
+ let gotActivate = false;
+
+ tabs.once('activate', function onActivate(eventTab) {
+ assert.ok(windows.activeWindow, "Is able to fetch activeWindow");
+ gotActivate = true;
+ });
+
+ open().then(function(window) {
+ assert.ok(gotActivate, "Received activate event");
+ return close(window);
+ }).then(done).then(null, assert.fail);
+}
+
+// TEST: tab unloader
+exports.testAutomaticDestroyEventOpen = function(assert, done) {
+ let called = false;
+ let loader = Loader(module);
+ let tabs2 = loader.require("sdk/tabs");
+ tabs2.on('open', _ => called = true);
+
+ // Fire a tab event and ensure that the destroyed tab is inactive
+ tabs.once('open', tab => {
+ setTimeout(_ => {
+ assert.ok(!called, "Unloaded tab module is destroyed and inactive");
+ tab.close(done);
+ });
+ });
+
+ loader.unload();
+ tabs.open("data:text/html;charset=utf-8,testAutomaticDestroyEventOpen");
+};
+
+exports.testAutomaticDestroyEventActivate = function(assert, done) {
+ let called = false;
+ let loader = Loader(module);
+ let tabs2 = loader.require("sdk/tabs");
+ tabs2.on('activate', _ => called = true);
+
+ // Fire a tab event and ensure that the destroyed tab is inactive
+ tabs.once('activate', tab => {
+ setTimeout(_ => {
+ assert.ok(!called, "Unloaded tab module is destroyed and inactive");
+ tab.close(done);
+ });
+ });
+
+ loader.unload();
+ tabs.open("data:text/html;charset=utf-8,testAutomaticDestroyEventActivate");
+};
+
+exports.testAutomaticDestroyEventDeactivate = function(assert, done) {
+ let called = false;
+ let currentTab = tabs.activeTab;
+ let loader = Loader(module);
+ let tabs2 = loader.require("sdk/tabs");
+
+ tabs.open({
+ url: "data:text/html;charset=utf-8,testAutomaticDestroyEventDeactivate",
+ onActivate: _ => setTimeout(_ => {
+ tabs2.on('deactivate', _ => called = true);
+
+ // Fire a tab event and ensure that the destroyed tab is inactive
+ tabs.once('deactivate', tab => {
+ setTimeout(_ => {
+ assert.ok(!called, "Unloaded tab module is destroyed and inactive");
+ tab.close(done);
+ });
+ });
+
+ loader.unload();
+ currentTab.activate();
+ })
+ });
+};
+
+exports.testAutomaticDestroyEventClose = function(assert, done) {
+ let called = false;
+ let loader = Loader(module);
+ let tabs2 = loader.require("sdk/tabs");
+
+ tabs.open({
+ url: "data:text/html;charset=utf-8,testAutomaticDestroyEventClose",
+ onReady: tab => {
+ tabs2.on('close', _ => called = true);
+
+ // Fire a tab event and ensure that the destroyed tab is inactive
+ tabs.once('close', tab => {
+ setTimeout(_ => {
+ assert.ok(!called, "Unloaded tab module is destroyed and inactive");
+ done();
+ });
+ });
+
+ loader.unload();
+ tab.close();
+ }
+ });
+};
+
+exports.testTabPropertiesInNewWindow = function(assert, done) {
+ const { LoaderWithFilteredConsole } = require("sdk/test/loader");
+ let loader = LoaderWithFilteredConsole(module, function(type, message) {
+ return true;
+ });
+
+ let tabs = loader.require('sdk/tabs');
+ let { viewFor } = loader.require('sdk/view/core');
+
+ let count = 0;
+ function onReadyOrLoad (tab) {
+ if (count++) {
+ close(getOwnerWindow(viewFor(tab))).then(done).then(null, assert.fail);
+ }
+ }
+
+ let url = "data:text/html;charset=utf-8,<html><head><title>foo</title></head><body>foo</body></html>";
+ tabs.open({
+ inNewWindow: true,
+ url: url,
+ onReady: function(tab) {
+ assert.equal(tab.title, "foo", "title of the new tab matches");
+ assert.equal(tab.url, url, "URL of the new tab matches");
+ assert.equal(tab.favicon, undefined, "favicon of the new tab is undefined");
+ assert.equal(tab.style, null, "style of the new tab matches");
+ assert.equal(tab.index, 0, "index of the new tab matches");
+ assert.notEqual(tab.getThumbnail(), null, "thumbnail of the new tab matches");
+ assert.notEqual(tab.id, null, "a tab object always has an id property.");
+
+ onReadyOrLoad(tab);
+ },
+ onLoad: function(tab) {
+ assert.equal(tab.title, "foo", "title of the new tab matches");
+ assert.equal(tab.url, url, "URL of the new tab matches");
+ assert.equal(tab.favicon, undefined, "favicon of the new tab is undefined");
+ assert.equal(tab.style, null, "style of the new tab matches");
+ assert.equal(tab.index, 0, "index of the new tab matches");
+ assert.notEqual(tab.getThumbnail(), null, "thumbnail of the new tab matches");
+ assert.notEqual(tab.id, null, "a tab object always has an id property.");
+
+ onReadyOrLoad(tab);
+ }
+ });
+};
+
+exports.testTabPropertiesInSameWindow = function(assert, done) {
+ const { LoaderWithFilteredConsole } = require("sdk/test/loader");
+ let loader = LoaderWithFilteredConsole(module, function(type, message) {
+ return true;
+ });
+
+ let tabs = loader.require('sdk/tabs');
+
+ // Get current count of tabs so we know the index of the
+ // new tab, bug 893846
+ let tabCount = tabs.length;
+ let count = 0;
+ function onReadyOrLoad (tab) {
+ if (count++) {
+ tab.close(done);
+ }
+ }
+
+ let url = "data:text/html;charset=utf-8,<html><head><title>foo</title></head><body>foo</body></html>";
+ tabs.open({
+ url: url,
+ onReady: function(tab) {
+ assert.equal(tab.title, "foo", "title of the new tab matches");
+ assert.equal(tab.url, url, "URL of the new tab matches");
+ assert.equal(tab.favicon, undefined, "favicon of the new tab is undefined");
+ assert.equal(tab.style, null, "style of the new tab matches");
+ assert.equal(tab.index, tabCount, "index of the new tab matches");
+ assert.notEqual(tab.getThumbnail(), null, "thumbnail of the new tab matches");
+ assert.notEqual(tab.id, null, "a tab object always has an id property.");
+
+ onReadyOrLoad(tab);
+ },
+ onLoad: function(tab) {
+ assert.equal(tab.title, "foo", "title of the new tab matches");
+ assert.equal(tab.url, url, "URL of the new tab matches");
+ assert.equal(tab.favicon, undefined, "favicon of the new tab is undefined");
+ assert.equal(tab.style, null, "style of the new tab matches");
+ assert.equal(tab.index, tabCount, "index of the new tab matches");
+ assert.notEqual(tab.getThumbnail(), null, "thumbnail of the new tab matches");
+ assert.notEqual(tab.id, null, "a tab object always has an id property.");
+
+ onReadyOrLoad(tab);
+ }
+ });
+};
+
+// TEST: tab properties
+exports.testTabContentTypeAndReload = function(assert, done) {
+ open().then(focus).then(function(window) {
+ let url = "data:text/html;charset=utf-8,<html><head><title>foo</title></head><body>foo</body></html>";
+ let urlXML = "data:text/xml;charset=utf-8,<foo>bar</foo>";
+ tabs.open({
+ url: url,
+ onReady: function(tab) {
+ if (tab.url === url) {
+ assert.equal(tab.contentType, "text/html");
+ tab.url = urlXML;
+ }
+ else {
+ assert.equal(tab.contentType, "text/xml");
+ close(window).then(done).then(null, assert.fail);
+ }
+ }
+ });
+ });
+};
+
+// TEST: tabs iterator and length property
+exports.testTabsIteratorAndLength = function(assert, done) {
+ open(null, { features: { chrome: true, toolbar: true } }).then(focus).then(function(window) {
+ let startCount = 0;
+ for (let t of tabs) startCount++;
+ assert.equal(startCount, tabs.length, "length property is correct");
+ let url = "data:text/html;charset=utf-8,default";
+
+ tabs.open(url);
+ tabs.open(url);
+ tabs.open({
+ url: url,
+ onOpen: function(tab) {
+ let count = 0;
+ for (let t of tabs) count++;
+ assert.equal(count, startCount + 3, "iterated tab count matches");
+ assert.equal(startCount + 3, tabs.length, "iterated tab count matches length property");
+
+ close(window).then(done).then(null, assert.fail);
+ }
+ });
+ });
+};
+
+// TEST: tab.url setter
+exports.testTabLocation = function(assert, done) {
+ open().then(focus).then(function(window) {
+ let url1 = "data:text/html;charset=utf-8,foo";
+ let url2 = "data:text/html;charset=utf-8,bar";
+
+ tabs.on('ready', function onReady(tab) {
+ if (tab.url != url2)
+ return;
+ tabs.removeListener('ready', onReady);
+ assert.pass("tab.load() loaded the correct url");
+ close(window).then(done).then(null, assert.fail);
+ });
+
+ tabs.open({
+ url: url1,
+ onOpen: function(tab) {
+ tab.url = url2
+ }
+ });
+ });
+};
+
+// TEST: tab.close()
+exports.testTabClose = function(assert, done) {
+ let testName = "testTabClose";
+ let url = "data:text/html;charset=utf-8," + testName;
+
+ assert.notEqual(tabs.activeTab.url, url, "tab is not the active tab");
+ tabs.once('ready', function onReady(tab) {
+ assert.equal(tabs.activeTab.url, tab.url, "tab is now the active tab");
+ assert.equal(url, tab.url, "tab url is the test url");
+ let secondOnCloseCalled = false;
+
+ // Bug 699450: Multiple calls to tab.close should not throw
+ tab.close(() => secondOnCloseCalled = true);
+ try {
+ tab.close(function () {
+ assert.notEqual(tabs.activeTab.url, url, "tab is no longer the active tab");
+ assert.ok(secondOnCloseCalled,
+ "The immediate second call to tab.close happened");
+ assert.notEqual(tabs.activeTab.url, url, "tab is no longer the active tab");
+
+ done();
+ });
+ }
+ catch(e) {
+ assert.fail("second call to tab.close() thrown an exception: " + e);
+ }
+ });
+
+ tabs.open(url);
+};
+
+// TEST: tab.move()
+exports.testTabMove = function(assert, done) {
+ open().then(focus).then(function(window) {
+ let url = "data:text/html;charset=utf-8,foo";
+
+ tabs.open({
+ url: url,
+ onOpen: function(tab) {
+ assert.equal(tab.index, 1, "tab index before move matches");
+ tab.index = 0;
+ assert.equal(tab.index, 0, "tab index after move matches");
+ close(window).then(done).then(null, assert.fail);
+ }
+ });
+ }).then(null, assert.fail);
+};
+
+exports.testIgnoreClosing = function*(assert) {
+ let url = "data:text/html;charset=utf-8,foobar";
+ let originalWindow = getMostRecentBrowserWindow();
+
+ let window = yield open().then(focus);
+
+ assert.equal(tabs.length, 2, "should be two windows open each with one tab");
+
+ yield new Promise(resolve => {
+ tabs.once("ready", (tab) => {
+ let win = tab.window;
+ assert.equal(win.tabs.length, 2, "should be two tabs in the new window");
+ assert.equal(tabs.length, 3, "should be three tabs in total");
+
+ tab.close(() => {
+ assert.equal(win.tabs.length, 1, "should be one tab in the new window");
+ assert.equal(tabs.length, 2, "should be two tabs in total");
+ resolve();
+ });
+ });
+
+ tabs.open(url);
+ });
+};
+
+// TEST: open tab with default options
+exports.testOpen = function(assert, done) {
+ let url = "data:text/html;charset=utf-8,default";
+ tabs.open({
+ url: url,
+ onReady: function(tab) {
+ assert.equal(tab.url, url, "URL of the new tab matches");
+ assert.equal(tab.isPinned, false, "The new tab is not pinned");
+
+ tab.close(done);
+ }
+ });
+};
+
+// TEST: opening a pinned tab
+exports.testOpenPinned = function(assert, done) {
+ let url = "data:text/html;charset=utf-8,default";
+ tabs.open({
+ url: url,
+ isPinned: true,
+ onOpen: function(tab) {
+ assert.equal(tab.isPinned, true, "The new tab is pinned");
+ tab.close(done);
+ }
+ });
+};
+
+// TEST: pin/unpin opened tab
+exports.testPinUnpin = function(assert, done) {
+ let url = "data:text/html;charset=utf-8,default";
+ tabs.open({
+ url: url,
+ inBackground: true,
+ onOpen: function(tab) {
+ tab.pin();
+ assert.equal(tab.isPinned, true, "The tab was pinned correctly");
+ tab.unpin();
+ assert.equal(tab.isPinned, false, "The tab was unpinned correctly");
+ tab.close(done);
+ }
+ });
+}
+
+// TEST: open tab in background
+exports.testInBackground = function(assert, done) {
+ assert.equal(tabs.length, 1, "Should be one tab");
+
+ let window = getMostRecentBrowserWindow();
+ let activeUrl = tabs.activeTab.url;
+ let url = "data:text/html;charset=utf-8,background";
+ assert.equal(getMostRecentBrowserWindow(), window, "getMostRecentBrowserWindow() matches this window");
+ tabs.on('ready', function onReady(tab) {
+ tabs.removeListener('ready', onReady);
+ assert.equal(tabs.activeTab.url, activeUrl, "URL of active tab has not changed");
+ assert.equal(tab.url, url, "URL of the new background tab matches");
+ assert.equal(getMostRecentBrowserWindow(), window, "a new window was not opened");
+ assert.notEqual(tabs.activeTab.url, url, "URL of active tab is not the new URL");
+ tab.close(done);
+ });
+
+ tabs.open({
+ url: url,
+ inBackground: true
+ });
+}
+
+// TEST: open tab in new window
+exports.testOpenInNewWindow = function(assert, done) {
+ let startWindowCount = windows().length;
+
+ let url = "data:text/html;charset=utf-8,testOpenInNewWindow";
+ tabs.open({
+ url: url,
+ inNewWindow: true,
+ onReady: function(tab) {
+ let newWindow = getOwnerWindow(viewFor(tab));
+ assert.equal(windows().length, startWindowCount + 1, "a new window was opened");
+
+ onFocus(newWindow).then(function() {
+ assert.equal(getMostRecentBrowserWindow(), newWindow, "new window is active");
+ assert.equal(tab.url, url, "URL of the new tab matches");
+ assert.equal(newWindow.content.location, url, "URL of new tab in new window matches");
+ assert.equal(tabs.activeTab.url, url, "URL of activeTab matches");
+
+ return close(newWindow).then(done);
+ }).then(null, assert.fail);
+ }
+ });
+
+}
+
+// Test tab.open inNewWindow + onOpen combination
+exports.testOpenInNewWindowOnOpen = function(assert, done) {
+ let startWindowCount = windows().length;
+
+ let url = "data:text/html;charset=utf-8,newwindow";
+ tabs.open({
+ url: url,
+ inNewWindow: true,
+ onOpen: function(tab) {
+ let newWindow = getOwnerWindow(viewFor(tab));
+
+ onFocus(newWindow).then(function() {
+ assert.equal(windows().length, startWindowCount + 1, "a new window was opened");
+ assert.equal(getMostRecentBrowserWindow(), newWindow, "new window is active");
+
+ close(newWindow).then(done).then(null, assert.fail);
+ });
+ }
+ });
+};
+
+// TEST: onOpen event handler
+exports.testTabsEvent_onOpen = function(assert, done) {
+ open().then(focus).then(window => {
+ let url = "data:text/html;charset=utf-8,1";
+ let eventCount = 0;
+
+ // add listener via property assignment
+ function listener1(tab) {
+ eventCount++;
+ };
+ tabs.on('open', listener1);
+
+ // add listener via collection add
+ tabs.on('open', function listener2(tab) {
+ assert.equal(++eventCount, 2, "both listeners notified");
+ tabs.removeListener('open', listener1);
+ tabs.removeListener('open', listener2);
+ close(window).then(done).then(null, assert.fail);
+ });
+
+ tabs.open(url);
+ }).then(null, assert.fail);
+};
+
+// TEST: onClose event handler
+exports.testTabsEvent_onClose = function*(assert) {
+ let window = yield open().then(focus);
+ let url = "data:text/html;charset=utf-8,onclose";
+ let eventCount = 0;
+
+ // add listener via property assignment
+ function listener1(tab) {
+ eventCount++;
+ }
+ tabs.on("close", listener1);
+
+ yield new Promise(resolve => {
+ // add listener via collection add
+ tabs.on("close", function listener2(tab) {
+ assert.equal(++eventCount, 2, "both listeners notified");
+ tabs.removeListener("close", listener2);
+ resolve();
+ });
+
+ tabs.on('ready', function onReady(tab) {
+ tabs.removeListener('ready', onReady);
+ tab.close();
+ });
+
+ tabs.open(url);
+ });
+
+ tabs.removeListener("close", listener1);
+ assert.pass("done test!");
+
+ yield close(window);
+ assert.pass("window was closed!");
+};
+
+// TEST: onClose event handler when a window is closed
+exports.testTabsEvent_onCloseWindow = function(assert, done) {
+ let closeCount = 0;
+ let individualCloseCount = 0;
+
+ open().then(focus).then(window => {
+ assert.pass('opened a new window');
+
+ tabs.on("close", function listener() {
+ if (++closeCount == 4) {
+ tabs.removeListener("close", listener);
+ }
+ });
+
+ function endTest() {
+ if (++individualCloseCount < 3) {
+ assert.pass('tab closed ' + individualCloseCount);
+ return;
+ }
+
+ assert.equal(closeCount, 4, "Correct number of close events received");
+ assert.equal(individualCloseCount, 3,
+ "Each tab with an attached onClose listener received a close " +
+ "event when the window was closed");
+
+ done();
+ }
+
+ // One tab is already open with the window
+ let openTabs = 1;
+ function testCasePossiblyLoaded() {
+ if (++openTabs == 4) {
+ window.close();
+ }
+ assert.pass('tab opened ' + openTabs);
+ }
+
+ tabs.open({
+ url: "data:text/html;charset=utf-8,tab2",
+ onOpen: testCasePossiblyLoaded,
+ onClose: endTest
+ });
+
+ tabs.open({
+ url: "data:text/html;charset=utf-8,tab3",
+ onOpen: testCasePossiblyLoaded,
+ onClose: endTest
+ });
+
+ tabs.open({
+ url: "data:text/html;charset=utf-8,tab4",
+ onOpen: testCasePossiblyLoaded,
+ onClose: endTest
+ });
+ }).then(null, assert.fail);
+}
+
+// TEST: onReady event handler
+exports.testTabsEvent_onReady = function(assert, done) {
+ open().then(focus).then(window => {
+ let url = "data:text/html;charset=utf-8,onready";
+ let eventCount = 0;
+
+ // add listener via property assignment
+ function listener1(tab) {
+ eventCount++;
+ };
+ tabs.on('ready', listener1);
+
+ // add listener via collection add
+ tabs.on('ready', function listener2(tab) {
+ assert.equal(++eventCount, 2, "both listeners notified");
+ tabs.removeListener('ready', listener1);
+ tabs.removeListener('ready', listener2);
+ close(window).then(done);
+ });
+
+ tabs.open(url);
+ }).then(null, assert.fail);
+};
+
+// TEST: onActivate event handler
+exports.testTabsEvent_onActivate = function(assert, done) {
+ open().then(focus).then(window => {
+ let url = "data:text/html;charset=utf-8,onactivate";
+ let eventCount = 0;
+
+ // add listener via property assignment
+ function listener1(tab) {
+ eventCount++;
+ };
+ tabs.on('activate', listener1);
+
+ // add listener via collection add
+ tabs.on('activate', function listener2(tab) {
+ assert.equal(++eventCount, 2, "both listeners notified");
+ tabs.removeListener('activate', listener1);
+ tabs.removeListener('activate', listener2);
+ close(window).then(done).then(null, assert.fail);
+ });
+
+ tabs.open(url);
+ }).then(null, assert.fail);
+};
+
+// onDeactivate event handler
+exports.testTabsEvent_onDeactivate = function*(assert) {
+ let window = yield open().then(focus);
+
+ let url = "data:text/html;charset=utf-8,ondeactivate";
+ let eventCount = 0;
+
+ // add listener via property assignment
+ function listener1(tab) {
+ eventCount++;
+ assert.pass("listener1 was called " + eventCount);
+ };
+ tabs.on('deactivate', listener1);
+
+ yield new Promise(resolve => {
+ // add listener via collection add
+ tabs.on('deactivate', function listener2(tab) {
+ assert.equal(++eventCount, 2, "both listeners notified");
+ tabs.removeListener('deactivate', listener2);
+ resolve();
+ });
+
+ tabs.on('open', function onOpen(tab) {
+ assert.pass("tab opened");
+ tabs.removeListener('open', onOpen);
+ tabs.open("data:text/html;charset=utf-8,foo");
+ });
+
+ tabs.open(url);
+ });
+
+ tabs.removeListener('deactivate', listener1);
+ assert.pass("listeners were removed");
+};
+
+// pinning
+exports.testTabsEvent_pinning = function(assert, done) {
+ open().then(focus).then(window => {
+ let url = "data:text/html;charset=utf-8,1";
+
+ tabs.on('open', function onOpen(tab) {
+ tabs.removeListener('open', onOpen);
+ tab.pin();
+ });
+
+ tabs.on('pinned', function onPinned(tab) {
+ tabs.removeListener('pinned', onPinned);
+ assert.ok(tab.isPinned, "notified tab is pinned");
+ tab.unpin();
+ });
+
+ tabs.on('unpinned', function onUnpinned(tab) {
+ tabs.removeListener('unpinned', onUnpinned);
+ assert.ok(!tab.isPinned, "notified tab is not pinned");
+ close(window).then(done).then(null, assert.fail);
+ });
+
+ tabs.open(url);
+ }).then(null, assert.fail);
+};
+
+// TEST: per-tab event handlers
+exports.testPerTabEvents = function*(assert) {
+ let window = yield open().then(focus);
+ let eventCount = 0;
+
+ let tab = yield new Promise(resolve => {
+ tabs.open({
+ url: "data:text/html;charset=utf-8,foo",
+ onOpen: (tab) => {
+ assert.pass("the tab was opened");
+
+ // add listener via property assignment
+ function listener1() {
+ eventCount++;
+ };
+ tab.on('ready', listener1);
+
+ // add listener via collection add
+ tab.on('ready', function listener2() {
+ assert.equal(eventCount, 1, "listener1 called before listener2");
+ tab.removeListener('ready', listener1);
+ tab.removeListener('ready', listener2);
+ assert.pass("removed listeners");
+ eventCount++;
+ resolve();
+ });
+ }
+ });
+ });
+
+ assert.equal(eventCount, 2, "both listeners were notified.");
+};
+
+exports.testAttachOnMultipleDocuments = function (assert, done) {
+ // Example of attach that process multiple tab documents
+ open().then(focus).then(window => {
+ let firstLocation = "data:text/html;charset=utf-8,foobar";
+ let secondLocation = "data:text/html;charset=utf-8,bar";
+ let thirdLocation = "data:text/html;charset=utf-8,fox";
+ let onReadyCount = 0;
+ let worker1 = null;
+ let worker2 = null;
+ let detachEventCount = 0;
+
+ tabs.open({
+ url: firstLocation,
+ onReady: function (tab) {
+ onReadyCount++;
+ if (onReadyCount == 1) {
+ worker1 = tab.attach({
+ contentScript: 'self.on("message", ' +
+ ' function () { return self.postMessage(document.location.href); }' +
+ ');',
+ onMessage: function (msg) {
+ assert.equal(msg, firstLocation,
+ "Worker url is equal to the 1st document");
+ tab.url = secondLocation;
+ },
+ onDetach: function () {
+ detachEventCount++;
+ assert.pass("Got worker1 detach event");
+ assert.throws(function () {
+ worker1.postMessage("ex-1");
+ },
+ /Couldn't find the worker/,
+ "postMessage throw because worker1 is destroyed");
+ checkEnd();
+ }
+ });
+ worker1.postMessage("new-doc-1");
+ }
+ else if (onReadyCount == 2) {
+
+ worker2 = tab.attach({
+ contentScript: 'self.on("message", ' +
+ ' function () { return self.postMessage(document.location.href); }' +
+ ');',
+ onMessage: function (msg) {
+ assert.equal(msg, secondLocation,
+ "Worker url is equal to the 2nd document");
+ tab.url = thirdLocation;
+ },
+ onDetach: function () {
+ detachEventCount++;
+ assert.pass("Got worker2 detach event");
+ assert.throws(function () {
+ worker2.postMessage("ex-2");
+ },
+ /Couldn't find the worker/,
+ "postMessage throw because worker2 is destroyed");
+ checkEnd();
+ }
+ });
+ worker2.postMessage("new-doc-2");
+ }
+ else if (onReadyCount == 3) {
+ tab.close();
+ }
+ }
+ });
+
+ function checkEnd() {
+ if (detachEventCount != 2)
+ return;
+
+ assert.pass("Got all detach events");
+
+ close(window).then(done).then(null, assert.fail);
+ }
+ }).then(null, assert.fail);
+}
+
+
+exports.testAttachWrappers = function (assert, done) {
+ // Check that content script has access to wrapped values by default
+ open().then(focus).then(window => {
+ let document = "data:text/html;charset=utf-8,<script>var globalJSVar = true; " +
+ " document.getElementById = 3;</script>";
+ let count = 0;
+
+ tabs.open({
+ url: document,
+ onReady: function (tab) {
+ let worker = tab.attach({
+ contentScript: 'try {' +
+ ' self.postMessage(!("globalJSVar" in window));' +
+ ' self.postMessage(typeof window.globalJSVar == "undefined");' +
+ '} catch(e) {' +
+ ' self.postMessage(e.message);' +
+ '}',
+ onMessage: function (msg) {
+ assert.equal(msg, true, "Worker has wrapped objects ("+count+")");
+ if (count++ == 1)
+ close(window).then(done).then(null, assert.fail);
+ }
+ });
+ }
+ });
+ }).then(null, assert.fail);
+}
+
+/*
+// We do not offer unwrapped access to DOM since bug 601295 landed
+// See 660780 to track progress of unwrap feature
+exports.testAttachUnwrapped = function (assert, done) {
+ // Check that content script has access to unwrapped values through unsafeWindow
+ openBrowserWindow(function(window, browser) {
+ let document = "data:text/html;charset=utf-8,<script>var globalJSVar=true;</script>";
+ let count = 0;
+
+ tabs.open({
+ url: document,
+ onReady: function (tab) {
+ let worker = tab.attach({
+ contentScript: 'try {' +
+ ' self.postMessage(unsafeWindow.globalJSVar);' +
+ '} catch(e) {' +
+ ' self.postMessage(e.message);' +
+ '}',
+ onMessage: function (msg) {
+ assert.equal(msg, true, "Worker has access to javascript content globals ("+count+")");
+ close(window).then(done);
+ }
+ });
+ }
+ });
+
+ });
+}
+*/
+
+exports['test window focus changes active tab'] = function(assert, done) {
+ let url1 = "data:text/html;charset=utf-8," + encodeURIComponent("test window focus changes active tab</br><h1>Window #1");
+
+ let win1 = openBrowserWindow(function() {
+ assert.pass("window 1 is open");
+
+ let win2 = openBrowserWindow(function() {
+ assert.pass("window 2 is open");
+
+ focus(win2).then(function() {
+ tabs.on("activate", function onActivate(tab) {
+ tabs.removeListener("activate", onActivate);
+
+ if (tab.readyState === 'uninitialized') {
+ tab.once('ready', whenReady);
+ }
+ else {
+ whenReady(tab);
+ }
+
+ function whenReady(tab) {
+ assert.pass("activate was called on windows focus change.");
+ assert.equal(tab.url, url1, 'the activated tab url is correct');
+
+ return close(win2).then(function() {
+ assert.pass('window 2 was closed');
+ return close(win1);
+ }).then(done).then(null, assert.fail);
+ }
+ });
+
+ win1.focus();
+ });
+ }, "data:text/html;charset=utf-8,test window focus changes active tab</br><h1>Window #2");
+ }, url1);
+};
+
+exports['test ready event on new window tab'] = function(assert, done) {
+ let uri = encodeURI("data:text/html;charset=utf-8,Waiting for ready event!");
+
+ require("sdk/tabs").on("ready", function onReady(tab) {
+ if (tab.url === uri) {
+ require("sdk/tabs").removeListener("ready", onReady);
+ assert.pass("ready event was emitted");
+ close(window).then(done).then(null, assert.fail);
+ }
+ });
+
+ let window = openBrowserWindow(function(){}, uri);
+};
+
+exports['test unique tab ids'] = function(assert, done) {
+ var windows = require('sdk/windows').browserWindows;
+ var { all, defer } = require('sdk/core/promise');
+
+ function openWindow() {
+ let deferred = defer();
+ let win = windows.open({
+ url: "data:text/html;charset=utf-8,<html>foo</html>",
+ });
+
+ win.on('open', function(window) {
+ assert.ok(window.tabs.length);
+ assert.ok(window.tabs.activeTab);
+ assert.ok(window.tabs.activeTab.id);
+ deferred.resolve({
+ id: window.tabs.activeTab.id,
+ win: win
+ });
+ });
+
+ return deferred.promise;
+ }
+
+ var one = openWindow(), two = openWindow();
+ all([one, two]).then(function(results) {
+ assert.notEqual(results[0].id, results[1].id, "tab Ids should not be equal.");
+ results[0].win.close(function() {
+ results[1].win.close(function () {
+ done();
+ });
+ });
+ });
+}
+
+// related to Bug 671305
+exports.testOnLoadEventWithDOM = function(assert, done) {
+ let count = 0;
+ let title = 'testOnLoadEventWithDOM';
+
+ // open a about: url
+ tabs.open({
+ url: 'data:text/html;charset=utf-8,<title>' + title + '</title>',
+ inBackground: true,
+ onLoad: function(tab) {
+ assert.equal(tab.title, title, 'tab passed in as arg, load called');
+
+ if (++count > 1) {
+ assert.pass('onLoad event called on reload');
+ tab.close(done);
+ }
+ else {
+ assert.pass('first onLoad event occured');
+ tab.reload();
+ }
+ }
+ });
+};
+
+// related to Bug 671305
+exports.testOnLoadEventWithImage = function(assert, done) {
+ let count = 0;
+
+ tabs.open({
+ url: base64jpeg,
+ inBackground: true,
+ onLoad: function(tab) {
+ if (++count > 1) {
+ assert.pass('onLoad event called on reload with image');
+ tab.close(done);
+ }
+ else {
+ assert.pass('first onLoad event occured');
+ tab.reload();
+ }
+ }
+ });
+};
+
+exports.testNoDeadObjects = function(assert, done) {
+ let loader = Loader(module);
+ let myTabs = loader.require("sdk/tabs");
+
+ // Load a tab, unload our modules, and navigate the tab to trigger an event
+ // on it. This would throw a dead object exception if our modules didn't
+ // clean up their event handlers on unload.
+ tabs.open({
+ url: "data:text/html;charset=utf-8,one",
+ onLoad: function(tab) {
+ // 2. Arrange to nuke the sandboxes and then trigger the load event
+ // on the tab once the loader is kaput.
+ systemEvents.on("sdk:loader:destroy", function onUnload() {
+ systemEvents.off("sdk:loader:destroy", onUnload, true);
+ // Defer this carnage till the end of the event queue, to avoid nuking
+ // the sandboxes from under the modules as they're being cleaned up.
+ setTimeout(function() {
+ // 3. Arrange to close the tab once the second page loads.
+ tab.on("load", function() {
+ tab.close(function() {
+ let { viewFor } = loader.require("sdk/view/core");
+ assert.equal(viewFor(tab), undefined, "didn't retain the closed tab");
+ done();
+ });
+ });
+
+ // Trigger a load event on the tab, to give the now-unloaded
+ // myTabs a chance to choke on it.
+ tab.url = "data:text/html;charset=utf-8,two";
+ }, 0);
+ }, true);
+
+ // 1. Start unloading the modules. Defer till the end of the event
+ // queue, in case myTabs is attaching its own handlers here too.
+ // We want it to latch on before we pull the rug from under it.
+ setTimeout(function() {
+ loader.unload();
+ }, 0);
+ }
+ });
+};
+
+exports.testTabDestroy = function(assert, done) {
+ let loader = Loader(module);
+ let myTabs = loader.require("sdk/tabs");
+ let { modelFor: myModelFor } = loader.require("sdk/model/core");
+ let { viewFor: myViewFor } = loader.require("sdk/view/core");
+ let myFirstTab = myTabs.activeTab;
+
+ myTabs.open({
+ url: "data:text/html;charset=utf-8,destroy",
+ onReady: (myTab) => setImmediate(() => {
+ let tab = modelFor(myViewFor(myTab));
+
+ function badListener(event, tab) {
+ // Ignore events for the other tabs
+ if (tab != myTab)
+ return;
+ assert.fail("Should not have seen the " + event + " listener called");
+ }
+
+ assert.ok(myTab, "Should have a tab in the test loader.");
+ assert.equal(myViewFor(myTab), viewFor(tab), "Should have the right view.");
+ assert.equal(myTabs.length, 2, "Should have the right number of global tabs.");
+ assert.equal(myTab.window.tabs.length, 2, "Should have the right number of window tabs.");
+ assert.equal(myTabs.activeTab, myTab, "Globally active tab is correct.");
+ assert.equal(myTab.window.tabs.activeTab, myTab, "Window active tab is correct.");
+
+ assert.equal(myTabs[1], myTab, "Global tabs list contains tab.");
+ assert.equal(myTab.window.tabs[1], myTab, "Window tabs list contains tab.");
+
+ myTab.once("ready", badListener.bind(null, "tab ready"));
+ myTab.once("deactivate", badListener.bind(null, "tab deactivate"));
+ myTab.once("activate", badListener.bind(null, "tab activate"));
+ myTab.once("close", badListener.bind(null, "tab close"));
+
+ myTab.destroy();
+
+ myTab.once("ready", badListener.bind(null, "new tab ready"));
+ myTab.once("deactivate", badListener.bind(null, "new tab deactivate"));
+ myTab.once("activate", badListener.bind(null, "new tab activate"));
+ myTab.once("close", badListener.bind(null, "new tab close"));
+
+ myTabs.once("ready", badListener.bind(null, "tabs ready"));
+ myTabs.once("deactivate", badListener.bind(null, "tabs deactivate"));
+ myTabs.once("activate", badListener.bind(null, "tabs activate"));
+ myTabs.once("close", badListener.bind(null, "tabs close"));
+
+ assert.equal(myViewFor(myTab), viewFor(tab), "Should have the right view.");
+ assert.equal(myModelFor(viewFor(tab)), myTab, "Can still reach the tab object.");
+ assert.equal(myTabs.length, 2, "Should have the right number of global tabs.");
+ assert.equal(myTab.window.tabs.length, 2, "Should have the right number of window tabs.");
+ assert.equal(myTabs.activeTab, myTab, "Globally active tab is correct.");
+ assert.equal(myTab.window.tabs.activeTab, myTab, "Window active tab is correct.");
+
+ assert.equal(myTabs[1], myTab, "Global tabs list still contains tab.");
+ assert.equal(myTab.window.tabs[1], myTab, "Window tabs list still contains tab.");
+
+ assert.equal(myTab.url, undefined, "url property is not usable");
+ assert.equal(myTab.contentType, undefined, "contentType property is not usable");
+ assert.equal(myTab.title, undefined, "title property is not usable");
+ assert.equal(myTab.id, undefined, "id property is not usable");
+ assert.equal(myTab.index, undefined, "index property is not usable");
+
+ myTab.pin();
+ assert.ok(!tab.isPinned, "pin method shouldn't work");
+
+ tabs.once("activate", () => setImmediate(() => {
+ assert.equal(myTabs.activeTab, myFirstTab, "Globally active tab is correct.");
+ assert.equal(myTab.window.tabs.activeTab, myFirstTab, "Window active tab is correct.");
+
+ let sawActivate = false;
+ tabs.once("activate", () => setImmediate(() => {
+ sawActivate = true;
+
+ assert.equal(myTabs.activeTab, myTab, "Globally active tab is correct.");
+ assert.equal(myTab.window.tabs.activeTab, myTab, "Window active tab is correct.");
+
+ // This shouldn't have any effect
+ myTab.close();
+
+ tab.once("ready", () => setImmediate(() => {
+ tab.close(() => {
+ loader.unload();
+ done();
+ });
+ }));
+ tab.url = "data:text/html;charset=utf-8,destroy2";
+ }));
+
+ myTab.activate();
+ setImmediate(() => {
+ assert.ok(!sawActivate, "activate method shouldn't have done anything");
+
+ tab.activate();
+ });
+ }));
+ myFirstTab.activate();
+ })
+ })
+};
+
+// related to bug 942511
+// https://bugzilla.mozilla.org/show_bug.cgi?id=942511
+exports['test active tab properties defined on popup closed'] = function (assert, done) {
+ setPref(OPEN_IN_NEW_WINDOW_PREF, 2);
+ setPref(DISABLE_POPUP_PREF, false);
+
+ let tabID = "";
+ let popupClosed = false;
+
+ tabs.open({
+ url: 'about:blank',
+ onReady: function (tab) {
+ tabID = tab.id;
+ tab.attach({
+ contentScript: 'var popup = window.open("about:blank");' +
+ 'popup.close();'
+ });
+
+ windowObserver.once('close', () => {
+ popupClosed = true;
+ });
+
+ windowObserver.on('activate', () => {
+ // Only when the 'activate' event is fired after the popup was closed.
+ if (popupClosed) {
+ popupClosed = false;
+ let activeTabID = tabs.activeTab.id;
+ if (activeTabID) {
+ assert.equal(tabID, activeTabID, 'ActiveTab properties are correct');
+ }
+ else {
+ assert.fail('ActiveTab properties undefined on popup closed');
+ }
+ tab.close(done);
+ }
+ });
+ }
+ });
+};
+
+// related to bugs 922956 and 989288
+// https://bugzilla.mozilla.org/show_bug.cgi?id=922956
+// https://bugzilla.mozilla.org/show_bug.cgi?id=989288
+exports["test tabs ready and close after window.open"] = function*(assert, done) {
+ // ensure popups open in a new window and disable popup blocker
+ setPref(OPEN_IN_NEW_WINDOW_PREF, 2);
+ setPref(DISABLE_POPUP_PREF, false);
+
+ // open windows to trigger observers
+ tabs.activeTab.attach({
+ contentScript: "window.open('about:blank');" +
+ "window.open('about:blank', '', " +
+ "'width=800,height=600,resizable=no,status=no,location=no');"
+ });
+
+ let tab1 = yield wait(tabs, "ready");
+ assert.pass("first tab ready has occured");
+
+ let tab2 = yield wait(tabs, "ready");
+ assert.pass("second tab ready has occured");
+
+ tab1.close();
+ yield wait(tabs, "close");
+ assert.pass("first tab close has occured");
+
+ tab2.close();
+ yield wait(tabs, "close");
+ assert.pass("second tab close has occured");
+};
+
+// related to bug #939496
+exports["test tab open event for new window"] = function(assert, done) {
+ // ensure popups open in a new window and disable popup blocker
+ setPref(OPEN_IN_NEW_WINDOW_PREF, 2);
+ setPref(DISABLE_POPUP_PREF, false);
+
+ tabs.once('open', function onOpen(window) {
+ assert.pass("tab open has occured");
+ window.close(done);
+ });
+
+ // open window to trigger observers
+ browserWindows.open("about:logo");
+};
+
+after(exports, function*(name, assert) {
+ resetPopupPrefs();
+ yield cleanUI();
+});
+
+const resetPopupPrefs = () => {
+ resetPref(OPEN_IN_NEW_WINDOW_PREF);
+ resetPref(DISABLE_POPUP_PREF);
+};
+
+/******************* helpers *********************/
+
+// Utility function to open a new browser window.
+function openBrowserWindow(callback, url) {
+ let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
+ getService(Ci.nsIWindowWatcher);
+ let urlString = Cc["@mozilla.org/supports-string;1"].
+ createInstance(Ci.nsISupportsString);
+ urlString.data = url;
+ let window = ww.openWindow(null, "chrome://browser/content/browser.xul",
+ "_blank", "chrome,all,dialog=no", urlString);
+
+ if (callback) {
+ window.addEventListener("load", function onLoad(event) {
+ if (event.target && event.target.defaultView == window) {
+ window.removeEventListener("load", onLoad, true);
+ let browsers = window.document.getElementsByTagName("tabbrowser");
+ try {
+ setTimeout(function () {
+ callback(window, browsers[0]);
+ }, 10);
+ }
+ catch (e) {
+ console.exception(e);
+ }
+ }
+ }, true);
+ }
+
+ return window;
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/tabs/utils.js b/addon-sdk/source/test/tabs/utils.js
new file mode 100644
index 000000000..4981a4d08
--- /dev/null
+++ b/addon-sdk/source/test/tabs/utils.js
@@ -0,0 +1,24 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { openTab: makeTab, getTabContentWindow } = require("sdk/tabs/utils");
+
+function openTab(rawWindow, url) {
+ return new Promise(resolve => {
+ let tab = makeTab(rawWindow, url);
+ let window = getTabContentWindow(tab);
+ if (window.document.readyState == "complete") {
+ return resolve();
+ }
+
+ window.addEventListener("load", function onLoad() {
+ window.removeEventListener("load", onLoad, true);
+ resolve();
+ }, true);
+
+ return null;
+ })
+}
+exports.openTab = openTab;
diff --git a/addon-sdk/source/test/test-addon-bootstrap.js b/addon-sdk/source/test/test-addon-bootstrap.js
new file mode 100644
index 000000000..01e2d15fc
--- /dev/null
+++ b/addon-sdk/source/test/test-addon-bootstrap.js
@@ -0,0 +1,97 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Cu, Cc, Ci } = require("chrome");
+const { create, evaluate } = require("./fixtures/bootstrap/utils");
+
+const ROOT = require.resolve("sdk/base64").replace("/sdk/base64.js", "");
+
+// Note: much of this test code is from
+// http://dxr.mozilla.org/mozilla-central/source/toolkit/mozapps/extensions/internal/XPIProvider.jsm
+const BOOTSTRAP_REASONS = {
+ APP_STARTUP : 1,
+ APP_SHUTDOWN : 2,
+ ADDON_ENABLE : 3,
+ ADDON_DISABLE : 4,
+ ADDON_INSTALL : 5,
+ ADDON_UNINSTALL : 6,
+ ADDON_UPGRADE : 7,
+ ADDON_DOWNGRADE : 8
+};
+
+exports["test install/startup/shutdown/uninstall all return a promise"] = function(assert) {
+ let uri = require.resolve("./fixtures/addon/bootstrap.js");
+ let id = "test-min-boot@jetpack";
+ let bootstrapScope = create({
+ id: id,
+ uri: uri
+ });
+
+ // As we don't want our caller to control the JS version used for the
+ // bootstrap file, we run loadSubScript within the context of the
+ // sandbox with the latest JS version set explicitly.
+ bootstrapScope.ROOT = ROOT;
+
+ evaluate({
+ uri: uri,
+ scope: bootstrapScope
+ });
+
+ let addon = {
+ id: id,
+ version: "0.0.1",
+ resourceURI: {
+ spec: uri.replace("bootstrap.js", "")
+ }
+ };
+
+ let install = bootstrapScope.install(addon, BOOTSTRAP_REASONS.ADDON_INSTALL);
+ yield install.then(() => assert.pass("install returns a promise"));
+
+ let startup = bootstrapScope.startup(addon, BOOTSTRAP_REASONS.ADDON_INSTALL);
+ yield startup.then(() => assert.pass("startup returns a promise"));
+
+ let shutdown = bootstrapScope.shutdown(addon, BOOTSTRAP_REASONS.ADDON_DISABLE);
+ yield shutdown.then(() => assert.pass("shutdown returns a promise"));
+
+ // calling shutdown multiple times is fine
+ shutdown = bootstrapScope.shutdown(addon, BOOTSTRAP_REASONS.ADDON_DISABLE);
+ yield shutdown.then(() => assert.pass("shutdown returns working promise on multiple calls"));
+
+ let uninstall = bootstrapScope.uninstall(addon, BOOTSTRAP_REASONS.ADDON_UNINSTALL);
+ yield uninstall.then(() => assert.pass("uninstall returns a promise"));
+}
+
+exports["test minimal bootstrap.js"] = function*(assert) {
+ let uri = require.resolve("./fixtures/addon/bootstrap.js");
+ let bootstrapScope = create({
+ id: "test-min-boot@jetpack",
+ uri: uri
+ });
+
+ // As we don't want our caller to control the JS version used for the
+ // bootstrap file, we run loadSubScript within the context of the
+ // sandbox with the latest JS version set explicitly.
+ bootstrapScope.ROOT = ROOT;
+
+ assert.equal(typeof bootstrapScope.install, "undefined", "install DNE");
+ assert.equal(typeof bootstrapScope.startup, "undefined", "startup DNE");
+ assert.equal(typeof bootstrapScope.shutdown, "undefined", "shutdown DNE");
+ assert.equal(typeof bootstrapScope.uninstall, "undefined", "uninstall DNE");
+
+ evaluate({
+ uri: uri,
+ scope: bootstrapScope
+ });
+
+ assert.equal(typeof bootstrapScope.install, "function", "install exists");
+ assert.equal(typeof bootstrapScope.startup, "function", "startup exists");
+ assert.equal(typeof bootstrapScope.shutdown, "function", "shutdown exists");
+ assert.equal(typeof bootstrapScope.uninstall, "function", "uninstall exists");
+
+ bootstrapScope.shutdown(null, BOOTSTRAP_REASONS.ADDON_DISABLE);
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-addon-extras.js b/addon-sdk/source/test/test-addon-extras.js
new file mode 100644
index 000000000..1910db05e
--- /dev/null
+++ b/addon-sdk/source/test/test-addon-extras.js
@@ -0,0 +1,70 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Ci, Cu, Cc, components } = require("chrome");
+const self = require("sdk/self");
+const { before, after } = require("sdk/test/utils");
+const fixtures = require("./fixtures");
+const { Loader } = require("sdk/test/loader");
+const { merge } = require("sdk/util/object");
+
+exports["test changing result from addon extras in panel"] = function(assert, done) {
+ let loader = Loader(module, null, null, {
+ modules: {
+ "sdk/self": merge({}, self, {
+ data: merge({}, self.data, {url: fixtures.url})
+ })
+ }
+ });
+
+ const { Panel } = loader.require("sdk/panel");
+ const { events } = loader.require("sdk/content/sandbox/events");
+ const { on } = loader.require("sdk/event/core");
+ const { isAddonContent } = loader.require("sdk/content/utils");
+
+ var result = 1;
+ var extrasVal = {
+ test: function() {
+ return result;
+ }
+ };
+
+ on(events, "content-script-before-inserted", ({ window, worker }) => {
+ assert.pass("content-script-before-inserted");
+
+ if (isAddonContent({ contentURL: window.location.href })) {
+ let extraStuff = Cu.cloneInto(extrasVal, window, {
+ cloneFunctions: true
+ });
+ getUnsafeWindow(window).extras = extraStuff;
+
+ assert.pass("content-script-before-inserted done!");
+ }
+ });
+
+ let panel = Panel({
+ contentURL: "./test-addon-extras.html"
+ });
+
+ panel.port.once("result1", (result) => {
+ assert.equal(result, 1, "result is a number");
+ result = true;
+ panel.port.emit("get-result");
+ });
+
+ panel.port.once("result2", (result) => {
+ assert.equal(result, true, "result is a boolean");
+ loader.unload();
+ done();
+ });
+
+ panel.port.emit("get-result");
+}
+
+function getUnsafeWindow (win) {
+ return win.wrappedJSObject || win;
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-addon-installer.js b/addon-sdk/source/test/test-addon-installer.js
new file mode 100644
index 000000000..bb39cca2d
--- /dev/null
+++ b/addon-sdk/source/test/test-addon-installer.js
@@ -0,0 +1,230 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Cc, Ci, Cu } = require("chrome");
+const { pathFor } = require("sdk/system");
+const AddonInstaller = require("sdk/addon/installer");
+const { on, off } = require("sdk/system/events");
+const { setTimeout } = require("sdk/timers");
+const fs = require("sdk/io/fs");
+const path = require("sdk/fs/path");
+const { OS } = require("resource://gre/modules/osfile.jsm");
+const { toFilename } = require("sdk/url");
+
+// Retrieve the path to the OS temporary directory:
+const tmpDir = pathFor("TmpD");
+
+const profilePath = pathFor("ProfD");
+const corruptXPIPath = path.join(profilePath, "sdk-corrupt.xpi");
+const testFolderURL = module.uri.split('test-addon-installer.js')[0];
+const ADDON_URL = toFilename(testFolderURL + "fixtures/addon-install-unit-test@mozilla.com.xpi");
+
+exports["test Install"] = function*(assert) {
+ var ADDON_PATH = OS.Path.join(OS.Constants.Path.tmpDir, "install-test.xpi");
+
+ assert.pass("Copying test add-on " + ADDON_URL + " to " + ADDON_PATH);
+
+ yield OS.File.copy(ADDON_URL, ADDON_PATH);
+
+ assert.pass("Copied test add-on to " + ADDON_PATH);
+
+ // Save all events distpatched by bootstrap.js of the installed addon
+ let events = [];
+ function eventsObserver({ data }) {
+ events.push(data);
+ }
+ on("addon-install-unit-test", eventsObserver);
+
+ // Install the test addon
+ yield AddonInstaller.install(ADDON_PATH).then((id) => {
+ assert.equal(id, "addon-install-unit-test@mozilla.com", "`id` is valid");
+
+ // Now uninstall it
+ return AddonInstaller.uninstall(id).then(function () {
+ // Ensure that bootstrap.js methods of the addon have been called
+ // successfully and in the right order
+ let expectedEvents = ["install", "startup", "shutdown", "uninstall"];
+ assert.equal(JSON.stringify(events),
+ JSON.stringify(expectedEvents),
+ "addon's bootstrap.js functions have been called");
+
+ off("addon-install-unit-test", eventsObserver);
+ });
+ }, (code) => {
+ assert.fail("Install failed: "+code);
+ off("addon-install-unit-test", eventsObserver);
+ });
+
+ assert.pass("Add-on was uninstalled.");
+
+ yield OS.File.remove(ADDON_PATH);
+
+ assert.pass("Removed the temp file");
+};
+
+exports["test Failing Install With Invalid Path"] = function (assert, done) {
+ AddonInstaller.install("invalid-path").then(
+ function onInstalled(id) {
+ assert.fail("Unexpected success");
+ done();
+ },
+ function onFailure(code) {
+ assert.equal(code, AddonInstaller.ERROR_FILE_ACCESS,
+ "Got expected error code");
+ done();
+ }
+ );
+};
+
+exports["test Failing Install With Invalid File"] = function (assert, done) {
+ const content = "bad xpi";
+ const path = corruptXPIPath;
+
+ fs.writeFile(path, content, (error) => {
+ assert.equal(fs.readFileSync(path).toString(),
+ content,
+ "contet was written");
+
+ AddonInstaller.install(path).then(
+ () => {
+ assert.fail("Unexpected success");
+ fs.unlink(path, done);
+ },
+ (code) => {
+ assert.equal(code, AddonInstaller.ERROR_CORRUPT_FILE,
+ "Got expected error code");
+ fs.unlink(path, done);
+ }
+ );
+ });
+}
+
+exports["test Update"] = function*(assert) {
+ var ADDON_PATH = OS.Path.join(OS.Constants.Path.tmpDir, "update-test.xpi");
+
+ assert.pass("Copying test add-on " + ADDON_URL + " to " + ADDON_PATH);
+
+ yield OS.File.copy(ADDON_URL, ADDON_PATH);
+
+ assert.pass("Copied test add-on to " + ADDON_PATH);
+
+ // Save all events distpatched by bootstrap.js of the installed addon
+ let events = [];
+ let iteration = 1;
+ let eventsObserver = ({data}) => events.push(data);
+ on("addon-install-unit-test", eventsObserver);
+
+ yield new Promise(resolve => {
+ function onInstalled(id) {
+ let prefix = "[" + iteration + "] ";
+ assert.equal(id, "addon-install-unit-test@mozilla.com",
+ prefix + "`id` is valid");
+
+ // On 2nd and 3rd iteration, we receive uninstall events from the last
+ // previously installed addon
+ let expectedEvents =
+ iteration == 1
+ ? ["install", "startup"]
+ : ["shutdown", "uninstall", "install", "startup"];
+ assert.equal(JSON.stringify(events),
+ JSON.stringify(expectedEvents),
+ prefix + "addon's bootstrap.js functions have been called");
+
+ if (iteration++ < 3) {
+ next();
+ }
+ else {
+ events = [];
+ AddonInstaller.uninstall(id).then(function() {
+ let expectedEvents = ["shutdown", "uninstall"];
+ assert.equal(JSON.stringify(events),
+ JSON.stringify(expectedEvents),
+ prefix + "addon's bootstrap.js functions have been called");
+
+ off("addon-install-unit-test", eventsObserver);
+ resolve();
+ });
+ }
+ }
+ function onFailure(code) {
+ assert.fail("Install failed: "+code);
+ off("addon-install-unit-test", eventsObserver);
+ resolve();
+ }
+
+ function next() {
+ events = [];
+ AddonInstaller.install(ADDON_PATH).then(onInstalled, onFailure);
+ }
+
+ next();
+ });
+
+ assert.pass("Add-on was uninstalled.");
+
+ yield OS.File.remove(ADDON_PATH);
+
+ assert.pass("Removed the temp file");
+};
+
+exports['test Uninstall failure'] = function (assert, done) {
+ AddonInstaller.uninstall('invalid-addon-path').then(
+ () => assert.fail('Addon uninstall should not resolve successfully'),
+ () => assert.pass('Addon correctly rejected invalid uninstall')
+ ).then(done, assert.fail);
+};
+
+exports['test Addon Disable and Enable'] = function*(assert) {
+ var ADDON_PATH = OS.Path.join(OS.Constants.Path.tmpDir, "disable-enable-test.xpi");
+
+ assert.pass("Copying test add-on " + ADDON_URL + " to " + ADDON_PATH);
+
+ yield OS.File.copy(ADDON_URL, ADDON_PATH);
+
+ assert.pass("Copied test add-on to " + ADDON_PATH);
+
+ let ensureActive = (addonId) => AddonInstaller.isActive(addonId).then(state => {
+ assert.equal(state, true, 'Addon should be enabled by default');
+ return addonId;
+ });
+ let ensureInactive = (addonId) => AddonInstaller.isActive(addonId).then(state => {
+ assert.equal(state, false, 'Addon should be disabled after disabling');
+ return addonId;
+ });
+
+ yield AddonInstaller.install(ADDON_PATH)
+ .then(ensureActive)
+ .then(AddonInstaller.enable) // should do nothing, yet not fail
+ .then(ensureActive)
+ .then(AddonInstaller.disable)
+ .then(ensureInactive)
+ .then(AddonInstaller.disable) // should do nothing, yet not fail
+ .then(ensureInactive)
+ .then(AddonInstaller.enable)
+ .then(ensureActive)
+ .then(AddonInstaller.uninstall);
+
+ assert.pass("Add-on was uninstalled.");
+
+ yield OS.File.remove(ADDON_PATH);
+
+ assert.pass("Removed the temp file");
+};
+
+exports['test Disable failure'] = function (assert, done) {
+ AddonInstaller.disable('not-an-id').then(
+ () => assert.fail('Addon disable should not resolve successfully'),
+ () => assert.pass('Addon correctly rejected invalid disable')
+ ).then(done, assert.fail);
+};
+
+exports['test Enable failure'] = function (assert, done) {
+ AddonInstaller.enable('not-an-id').then(
+ () => assert.fail('Addon enable should not resolve successfully'),
+ () => assert.pass('Addon correctly rejected invalid enable')
+ ).then(done, assert.fail);
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-addon-window.js b/addon-sdk/source/test/test-addon-window.js
new file mode 100644
index 000000000..8cb07bb07
--- /dev/null
+++ b/addon-sdk/source/test/test-addon-window.js
@@ -0,0 +1,22 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+var { Loader } = require('sdk/test/loader');
+
+exports.testReady = function(assert, done) {
+ let loader = Loader(module);
+ let { ready, window } = loader.require('sdk/addon/window');
+ let windowIsReady = false;
+
+ ready.then(function() {
+ assert.equal(windowIsReady, false, 'ready promise was resolved only once');
+ windowIsReady = true;
+
+ loader.unload();
+ done();
+ }).then(null, assert.fail);
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-api-utils.js b/addon-sdk/source/test/test-api-utils.js
new file mode 100644
index 000000000..12f2bf44f
--- /dev/null
+++ b/addon-sdk/source/test/test-api-utils.js
@@ -0,0 +1,316 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const apiUtils = require("sdk/deprecated/api-utils");
+
+exports.testValidateOptionsEmpty = function (assert) {
+ let val = apiUtils.validateOptions(null, {});
+
+ assert.deepEqual(val, {});
+
+ val = apiUtils.validateOptions(null, { foo: {} });
+ assert.deepEqual(val, {});
+
+ val = apiUtils.validateOptions({}, {});
+ assert.deepEqual(val, {});
+
+ val = apiUtils.validateOptions({}, { foo: {} });
+ assert.deepEqual(val, {});
+};
+
+exports.testValidateOptionsNonempty = function (assert) {
+ let val = apiUtils.validateOptions({ foo: 123 }, {});
+ assert.deepEqual(val, {});
+
+ val = apiUtils.validateOptions({ foo: 123, bar: 456 },
+ { foo: {}, bar: {}, baz: {} });
+
+ assert.deepEqual(val, { foo: 123, bar: 456 });
+};
+
+exports.testValidateOptionsMap = function (assert) {
+ let val = apiUtils.validateOptions({ foo: 3, bar: 2 }, {
+ foo: { map: v => v * v },
+ bar: { map: v => undefined }
+ });
+ assert.deepEqual(val, { foo: 9, bar: undefined });
+};
+
+exports.testValidateOptionsMapException = function (assert) {
+ let val = apiUtils.validateOptions({ foo: 3 }, {
+ foo: { map: function () { throw new Error(); }}
+ });
+ assert.deepEqual(val, { foo: 3 });
+};
+
+exports.testValidateOptionsOk = function (assert) {
+ let val = apiUtils.validateOptions({ foo: 3, bar: 2, baz: 1 }, {
+ foo: { ok: v => v },
+ bar: { ok: v => v }
+ });
+ assert.deepEqual(val, { foo: 3, bar: 2 });
+
+ assert.throws(
+ () => apiUtils.validateOptions({ foo: 2, bar: 2 }, {
+ bar: { ok: v => v > 2 }
+ }),
+ /^The option "bar" is invalid/,
+ "ok should raise exception on invalid option"
+ );
+
+ assert.throws(
+ () => apiUtils.validateOptions(null, { foo: { ok: v => v }}),
+ /^The option "foo" is invalid/,
+ "ok should raise exception on invalid option"
+ );
+};
+
+exports.testValidateOptionsIs = function (assert) {
+ let opts = {
+ array: [],
+ boolean: true,
+ func: function () {},
+ nul: null,
+ number: 1337,
+ object: {},
+ string: "foo",
+ undef1: undefined
+ };
+ let requirements = {
+ array: { is: ["array"] },
+ boolean: { is: ["boolean"] },
+ func: { is: ["function"] },
+ nul: { is: ["null"] },
+ number: { is: ["number"] },
+ object: { is: ["object"] },
+ string: { is: ["string"] },
+ undef1: { is: ["undefined"] },
+ undef2: { is: ["undefined"] }
+ };
+ let val = apiUtils.validateOptions(opts, requirements);
+ assert.deepEqual(val, opts);
+
+ assert.throws(
+ () => apiUtils.validateOptions(null, {
+ foo: { is: ["object", "number"] }
+ }),
+ /^The option "foo" must be one of the following types: object, number/,
+ "Invalid type should raise exception"
+ );
+};
+
+exports.testValidateOptionsIsWithExportedValue = function (assert) {
+ let { string, number, boolean, object } = apiUtils;
+
+ let opts = {
+ boolean: true,
+ number: 1337,
+ object: {},
+ string: "foo"
+ };
+ let requirements = {
+ string: { is: string },
+ number: { is: number },
+ boolean: { is: boolean },
+ object: { is: object }
+ };
+ let val = apiUtils.validateOptions(opts, requirements);
+ assert.deepEqual(val, opts);
+
+ // Test the types are optional by default
+ val = apiUtils.validateOptions({foo: 'bar'}, requirements);
+ assert.deepEqual(val, {});
+};
+
+exports.testValidateOptionsIsWithEither = function (assert) {
+ let { string, number, boolean, either } = apiUtils;
+ let text = { is: either(string, number) };
+
+ let requirements = {
+ text: text,
+ boolOrText: { is: either(text, boolean) }
+ };
+
+ let val = apiUtils.validateOptions({text: 12}, requirements);
+ assert.deepEqual(val, {text: 12});
+
+ val = apiUtils.validateOptions({text: "12"}, requirements);
+ assert.deepEqual(val, {text: "12"});
+
+ val = apiUtils.validateOptions({boolOrText: true}, requirements);
+ assert.deepEqual(val, {boolOrText: true});
+
+ val = apiUtils.validateOptions({boolOrText: "true"}, requirements);
+ assert.deepEqual(val, {boolOrText: "true"});
+
+ val = apiUtils.validateOptions({boolOrText: 1}, requirements);
+ assert.deepEqual(val, {boolOrText: 1});
+
+ assert.throws(
+ () => apiUtils.validateOptions({text: true}, requirements),
+ /^The option "text" must be one of the following types/,
+ "Invalid type should raise exception"
+ );
+
+ assert.throws(
+ () => apiUtils.validateOptions({boolOrText: []}, requirements),
+ /^The option "boolOrText" must be one of the following types/,
+ "Invalid type should raise exception"
+ );
+};
+
+exports.testValidateOptionsWithRequiredAndOptional = function (assert) {
+ let { string, number, required, optional } = apiUtils;
+
+ let opts = {
+ number: 1337,
+ string: "foo"
+ };
+
+ let requirements = {
+ string: required(string),
+ number: number
+ };
+
+ let val = apiUtils.validateOptions(opts, requirements);
+ assert.deepEqual(val, opts);
+
+ val = apiUtils.validateOptions({string: "foo"}, requirements);
+ assert.deepEqual(val, {string: "foo"});
+
+ assert.throws(
+ () => apiUtils.validateOptions({number: 10}, requirements),
+ /^The option "string" must be one of the following types/,
+ "Invalid type should raise exception"
+ );
+
+ // Makes string optional
+ requirements.string = optional(requirements.string);
+
+ val = apiUtils.validateOptions({number: 10}, requirements),
+ assert.deepEqual(val, {number: 10});
+
+};
+
+
+
+exports.testValidateOptionsWithExportedValue = function (assert) {
+ let { string, number, boolean, object } = apiUtils;
+
+ let opts = {
+ boolean: true,
+ number: 1337,
+ object: {},
+ string: "foo"
+ };
+ let requirements = {
+ string: string,
+ number: number,
+ boolean: boolean,
+ object: object
+ };
+ let val = apiUtils.validateOptions(opts, requirements);
+ assert.deepEqual(val, opts);
+
+ // Test the types are optional by default
+ val = apiUtils.validateOptions({foo: 'bar'}, requirements);
+ assert.deepEqual(val, {});
+};
+
+
+exports.testValidateOptionsMapIsOk = function (assert) {
+ let [map, is, ok] = [false, false, false];
+ let val = apiUtils.validateOptions({ foo: 1337 }, {
+ foo: {
+ map: v => v.toString(),
+ is: ["string"],
+ ok: v => v.length > 0
+ }
+ });
+ assert.deepEqual(val, { foo: "1337" });
+
+ let requirements = {
+ foo: {
+ is: ["object"],
+ ok: () => assert.fail("is should have caused us to throw by now")
+ }
+ };
+ assert.throws(
+ () => apiUtils.validateOptions(null, requirements),
+ /^The option "foo" must be one of the following types: object/,
+ "is should be used before ok is called"
+ );
+};
+
+exports.testValidateOptionsErrorMsg = function (assert) {
+ assert.throws(
+ () => apiUtils.validateOptions(null, {
+ foo: { ok: v => v, msg: "foo!" }
+ }),
+ /^foo!/,
+ "ok should raise exception with customized message"
+ );
+};
+
+exports.testValidateMapWithMissingKey = function (assert) {
+ let val = apiUtils.validateOptions({ }, {
+ foo: {
+ map: v => v || "bar"
+ }
+ });
+ assert.deepEqual(val, { foo: "bar" });
+
+ val = apiUtils.validateOptions({ }, {
+ foo: {
+ map: v => { throw "bar" }
+ }
+ });
+ assert.deepEqual(val, { });
+};
+
+exports.testValidateMapWithMissingKeyAndThrown = function (assert) {
+ let val = apiUtils.validateOptions({}, {
+ bar: {
+ map: function(v) { throw "bar" }
+ },
+ baz: {
+ map: v => "foo"
+ }
+ });
+ assert.deepEqual(val, { baz: "foo" });
+};
+
+exports.testAddIterator = function testAddIterator (assert) {
+ let obj = {};
+ let keys = ["foo", "bar", "baz"];
+ let vals = [1, 2, 3];
+ let keysVals = [["foo", 1], ["bar", 2], ["baz", 3]];
+ apiUtils.addIterator(
+ obj,
+ function keysValsGen() {
+ for (let keyVal of keysVals)
+ yield keyVal;
+ }
+ );
+
+ let keysItr = [];
+ for (let key in obj)
+ keysItr.push(key);
+
+ assert.equal(keysItr.length, keys.length,
+ "the keys iterator returns the correct number of items");
+ for (let i = 0; i < keys.length; i++)
+ assert.equal(keysItr[i], keys[i], "the key is correct");
+
+ let valsItr = [];
+ for each (let val in obj)
+ valsItr.push(val);
+ assert.equal(valsItr.length, vals.length,
+ "the vals iterator returns the correct number of items");
+ for (let i = 0; i < vals.length; i++)
+ assert.equal(valsItr[i], vals[i], "the val is correct");
+
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-array.js b/addon-sdk/source/test/test-array.js
new file mode 100644
index 000000000..161d8033d
--- /dev/null
+++ b/addon-sdk/source/test/test-array.js
@@ -0,0 +1,103 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict'
+
+const array = require('sdk/util/array');
+
+exports.testHas = function(assert) {
+ var testAry = [1, 2, 3];
+ assert.equal(array.has([1, 2, 3], 1), true);
+ assert.equal(testAry.length, 3);
+ assert.equal(testAry[0], 1);
+ assert.equal(testAry[1], 2);
+ assert.equal(testAry[2], 3);
+ assert.equal(array.has(testAry, 2), true);
+ assert.equal(array.has(testAry, 3), true);
+ assert.equal(array.has(testAry, 4), false);
+ assert.equal(array.has(testAry, '1'), false);
+};
+exports.testHasAny = function(assert) {
+ var testAry = [1, 2, 3];
+ assert.equal(array.hasAny([1, 2, 3], [1]), true);
+ assert.equal(array.hasAny([1, 2, 3], [1, 5]), true);
+ assert.equal(array.hasAny([1, 2, 3], [5, 1]), true);
+ assert.equal(array.hasAny([1, 2, 3], [5, 2]), true);
+ assert.equal(array.hasAny([1, 2, 3], [5, 3]), true);
+ assert.equal(array.hasAny([1, 2, 3], [5, 4]), false);
+ assert.equal(testAry.length, 3);
+ assert.equal(testAry[0], 1);
+ assert.equal(testAry[1], 2);
+ assert.equal(testAry[2], 3);
+ assert.equal(array.hasAny(testAry, [2]), true);
+ assert.equal(array.hasAny(testAry, [3]), true);
+ assert.equal(array.hasAny(testAry, [4]), false);
+ assert.equal(array.hasAny(testAry), false);
+ assert.equal(array.hasAny(testAry, '1'), false);
+};
+
+exports.testAdd = function(assert) {
+ var testAry = [1];
+ assert.equal(array.add(testAry, 1), false);
+ assert.equal(testAry.length, 1);
+ assert.equal(testAry[0], 1);
+ assert.equal(array.add(testAry, 2), true);
+ assert.equal(testAry.length, 2);
+ assert.equal(testAry[0], 1);
+ assert.equal(testAry[1], 2);
+};
+
+exports.testRemove = function(assert) {
+ var testAry = [1, 2];
+ assert.equal(array.remove(testAry, 3), false);
+ assert.equal(testAry.length, 2);
+ assert.equal(testAry[0], 1);
+ assert.equal(testAry[1], 2);
+ assert.equal(array.remove(testAry, 2), true);
+ assert.equal(testAry.length, 1);
+ assert.equal(testAry[0], 1);
+};
+
+exports.testFlatten = function(assert) {
+ assert.equal(array.flatten([1, 2, 3]).length, 3);
+ assert.equal(array.flatten([1, [2, 3]]).length, 3);
+ assert.equal(array.flatten([1, [2, [3]]]).length, 3);
+ assert.equal(array.flatten([[1], [[2, [3]]]]).length, 3);
+};
+
+exports.testUnique = function(assert) {
+ var Class = function () {};
+ var A = {};
+ var B = new Class();
+ var C = [ 1, 2, 3 ];
+ var D = {};
+ var E = new Class();
+
+ assert.deepEqual(array.unique([1,2,3,1,2]), [1,2,3]);
+ assert.deepEqual(array.unique([1,1,1,4,9,5,5]), [1,4,9,5]);
+ assert.deepEqual(array.unique([A, A, A, B, B, D]), [A,B,D]);
+ assert.deepEqual(array.unique([A, D, A, E, E, D, A, A, C]), [A, D, E, C])
+};
+
+exports.testUnion = function(assert) {
+ var Class = function () {};
+ var A = {};
+ var B = new Class();
+ var C = [ 1, 2, 3 ];
+ var D = {};
+ var E = new Class();
+
+ assert.deepEqual(array.union([1, 2, 3],[7, 1, 2]), [1, 2, 3, 7]);
+ assert.deepEqual(array.union([1, 1, 1, 4, 9, 5, 5], [10, 1, 5]), [1, 4, 9, 5, 10]);
+ assert.deepEqual(array.union([A, B], [A, D]), [A, B, D]);
+ assert.deepEqual(array.union([A, D], [A, E], [E, D, A], [A, C]), [A, D, E, C]);
+};
+
+exports.testFind = function(assert) {
+ let isOdd = (x) => x % 2;
+ assert.equal(array.find([2, 4, 5, 7, 8, 9], isOdd), 5);
+ assert.equal(array.find([2, 4, 6, 8], isOdd), undefined);
+ assert.equal(array.find([2, 4, 6, 8], isOdd, null), null);
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-base64.js b/addon-sdk/source/test/test-base64.js
new file mode 100644
index 000000000..b969413f9
--- /dev/null
+++ b/addon-sdk/source/test/test-base64.js
@@ -0,0 +1,100 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const base64 = require("sdk/base64");
+
+const text = "Awesome!";
+const b64text = "QXdlc29tZSE=";
+
+const utf8text = "\u2713 à la mode";
+const badutf8text = "\u0013 à la mode";
+const b64utf8text = "4pyTIMOgIGxhIG1vZGU=";
+
+// 1 MB string
+const longtext = 'fff'.repeat(333333);
+const b64longtext = 'ZmZm'.repeat(333333);
+
+exports["test base64.encode"] = function (assert) {
+ assert.equal(base64.encode(text), b64text, "encode correctly")
+}
+
+exports["test base64.decode"] = function (assert) {
+ assert.equal(base64.decode(b64text), text, "decode correctly")
+}
+
+exports["test base64.encode Unicode"] = function (assert) {
+
+ assert.equal(base64.encode(utf8text, "utf-8"), b64utf8text,
+ "encode correctly Unicode strings.")
+}
+
+exports["test base64.decode Unicode"] = function (assert) {
+
+ assert.equal(base64.decode(b64utf8text, "utf-8"), utf8text,
+ "decode correctly Unicode strings.")
+}
+
+exports["test base64.encode long string"] = function (assert) {
+
+ assert.equal(base64.encode(longtext), b64longtext, "encode long strings")
+}
+
+exports["test base64.decode long string"] = function (assert) {
+
+ assert.equal(base64.decode(b64longtext), longtext, "decode long strings")
+}
+
+exports["test base64.encode treats input as octet string"] = function (assert) {
+
+ assert.equal(base64.encode("\u0066"), "Zg==",
+ "treat octet string as octet string")
+ assert.equal(base64.encode("\u0166"), "Zg==",
+ "treat non-octet string as octet string")
+ assert.equal(base64.encode("\uff66"), "Zg==",
+ "encode non-octet string as octet string")
+}
+
+exports["test base64.encode with wrong charset"] = function (assert) {
+
+ assert.throws(function() {
+ base64.encode(utf8text, "utf-16");
+ }, "The charset argument can be only 'utf-8'");
+
+ assert.throws(function() {
+ base64.encode(utf8text, "");
+ }, "The charset argument can be only 'utf-8'");
+
+ assert.throws(function() {
+ base64.encode(utf8text, 8);
+ }, "The charset argument can be only 'utf-8'");
+
+}
+
+exports["test base64.decode with wrong charset"] = function (assert) {
+
+ assert.throws(function() {
+ base64.decode(utf8text, "utf-16");
+ }, "The charset argument can be only 'utf-8'");
+
+ assert.throws(function() {
+ base64.decode(utf8text, "");
+ }, "The charset argument can be only 'utf-8'");
+
+ assert.throws(function() {
+ base64.decode(utf8text, 8);
+ }, "The charset argument can be only 'utf-8'");
+
+}
+
+exports["test encode/decode Unicode without utf-8 as charset"] = function (assert) {
+
+ assert.equal(base64.decode(base64.encode(utf8text)), badutf8text,
+ "Unicode strings needs 'utf-8' charset or will be mangled"
+ );
+
+}
+
+require("test").run(exports);
diff --git a/addon-sdk/source/test/test-bootstrap.js b/addon-sdk/source/test/test-bootstrap.js
new file mode 100644
index 000000000..52a713e61
--- /dev/null
+++ b/addon-sdk/source/test/test-bootstrap.js
@@ -0,0 +1,19 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Request } = require("sdk/request");
+
+exports.testBootstrapExists = function (assert, done) {
+ Request({
+ url: "resource://gre/modules/sdk/bootstrap.js",
+ onComplete: function (response) {
+ if (response.text)
+ assert.pass("the bootstrap file was found");
+ done();
+ }
+ }).get();
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-browser-events.js b/addon-sdk/source/test/test-browser-events.js
new file mode 100644
index 000000000..9402f1ec5
--- /dev/null
+++ b/addon-sdk/source/test/test-browser-events.js
@@ -0,0 +1,102 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+module.metadata = {
+ engines: {
+ "Firefox": "*"
+ }
+};
+
+const { Loader } = require("sdk/test/loader");
+const { open, getMostRecentBrowserWindow, getOuterId } = require("sdk/window/utils");
+const { setTimeout } = require("sdk/timers");
+
+exports["test browser events"] = function(assert, done) {
+ let loader = Loader(module);
+ let { events } = loader.require("sdk/browser/events");
+ let { on, off } = loader.require("sdk/event/core");
+ let actual = [];
+
+ on(events, "data", function handler(e) {
+ actual.push(e);
+ if (e.type === "load") window.close();
+ if (e.type === "close") {
+ // Unload the module so that all listeners set by observer are removed.
+
+ let [ ready, load, close ] = actual;
+
+ assert.equal(ready.type, "DOMContentLoaded");
+ assert.equal(ready.target, window, "window ready");
+
+ assert.equal(load.type, "load");
+ assert.equal(load.target, window, "window load");
+
+ assert.equal(close.type, "close");
+ assert.equal(close.target, window, "window load");
+
+ // Note: If window is closed right after this GC won't have time
+ // to claim loader and there for this listener, there for it's safer
+ // to remove listener.
+ off(events, "data", handler);
+ loader.unload();
+ done();
+ }
+ });
+
+ // Open window and close it to trigger observers.
+ let window = open();
+};
+
+exports["test browser events ignore other wins"] = function(assert, done) {
+ let loader = Loader(module);
+ let { events: windowEvents } = loader.require("sdk/window/events");
+ let { events: browserEvents } = loader.require("sdk/browser/events");
+ let { on, off } = loader.require("sdk/event/core");
+ let actualBrowser = [];
+ let actualWindow = [];
+
+ function browserEventHandler(e) {
+ return actualBrowser.push(e);
+ }
+ on(browserEvents, "data", browserEventHandler);
+ on(windowEvents, "data", function handler(e) {
+ actualWindow.push(e);
+ // Delay close so that if "load" is also emitted on `browserEvents`
+ // `browserEventHandler` will be invoked.
+ if (e.type === "load") setTimeout(window.close);
+ if (e.type === "close") {
+ assert.deepEqual(actualBrowser, [], "browser events were not triggered");
+ let [ open, ready, load, close ] = actualWindow;
+
+ assert.equal(open.type, "open");
+ assert.equal(open.target, window, "window is open");
+
+
+
+ assert.equal(ready.type, "DOMContentLoaded");
+ assert.equal(ready.target, window, "window ready");
+
+ assert.equal(load.type, "load");
+ assert.equal(load.target, window, "window load");
+
+ assert.equal(close.type, "close");
+ assert.equal(close.target, window, "window load");
+
+
+ // Note: If window is closed right after this GC won't have time
+ // to claim loader and there for this listener, there for it's safer
+ // to remove listener.
+ off(windowEvents, "data", handler);
+ off(browserEvents, "data", browserEventHandler);
+ loader.unload();
+ done();
+ }
+ });
+
+ // Open window and close it to trigger observers.
+ let window = open("data:text/html,not a browser");
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-buffer.js b/addon-sdk/source/test/test-buffer.js
new file mode 100644
index 000000000..e55bf2b2f
--- /dev/null
+++ b/addon-sdk/source/test/test-buffer.js
@@ -0,0 +1,563 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+
+/*
+ * Many of these tests taken from Joyent's Node
+ * https://github.com/joyent/node/blob/master/test/simple/test-buffer.js
+ */
+
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+const { Buffer, TextEncoder, TextDecoder } = require('sdk/io/buffer');
+const { safeMerge } = require('sdk/util/object');
+
+const ENCODINGS = ['utf-8'];
+
+exports.testBufferMain = function (assert) {
+ let b = Buffer('abcdef');
+
+ // try to create 0-length buffers
+ new Buffer('');
+ new Buffer(0);
+ // test encodings supported by node;
+ // this is different than what node supports, details
+ // in buffer.js
+ ENCODINGS.forEach(enc => {
+ new Buffer('', enc);
+ assert.pass('Creating a buffer with ' + enc + ' does not throw');
+ });
+
+ ENCODINGS.forEach(function(encoding) {
+ // Does not work with utf8
+ if (encoding === 'utf-8') return;
+ var b = new Buffer(10);
+ b.write('あいうえお', encoding);
+ assert.equal(b.toString(encoding), 'あいうえお',
+ 'encode and decodes buffer with ' + encoding);
+ });
+
+ // invalid encoding for Buffer.toString
+ assert.throws(() => {
+ b.toString('invalid');
+ }, RangeError, 'invalid encoding for Buffer.toString');
+
+ // try to toString() a 0-length slice of a buffer, both within and without the
+ // valid buffer range
+ assert.equal(new Buffer('abc').toString('utf8', 0, 0), '',
+ 'toString 0-length buffer, valid range');
+ assert.equal(new Buffer('abc').toString('utf8', -100, -100), '',
+ 'toString 0-length buffer, invalid range');
+ assert.equal(new Buffer('abc').toString('utf8', 100, 100), '',
+ 'toString 0-length buffer, invalid range');
+
+ // try toString() with a object as a encoding
+ assert.equal(new Buffer('abc').toString({toString: function() {
+ return 'utf8';
+ }}), 'abc', 'toString with object as an encoding');
+
+ // test for buffer overrun
+ var buf = new Buffer([0, 0, 0, 0, 0]); // length: 5
+ var sub = buf.slice(0, 4); // length: 4
+ var written = sub.write('12345', 'utf8');
+ assert.equal(written, 4, 'correct bytes written in slice');
+ assert.equal(buf[4], 0, 'correct origin buffer value');
+
+ // Check for fractional length args, junk length args, etc.
+ // https://github.com/joyent/node/issues/1758
+ Buffer(3.3).toString(); // throws bad argument error in commit 43cb4ec
+ assert.equal(Buffer(-1).length, 0);
+ assert.equal(Buffer(NaN).length, 0);
+ assert.equal(Buffer(3.3).length, 3);
+ assert.equal(Buffer({length: 3.3}).length, 3);
+ assert.equal(Buffer({length: 'BAM'}).length, 0);
+
+ // Make sure that strings are not coerced to numbers.
+ assert.equal(Buffer('99').length, 2);
+ assert.equal(Buffer('13.37').length, 5);
+};
+
+exports.testIsEncoding = function (assert) {
+ ENCODINGS.map(encoding => {
+ assert.ok(Buffer.isEncoding(encoding),
+ 'Buffer.isEncoding ' + encoding + ' truthy');
+ });
+ ['not-encoding', undefined, null, 100, {}].map(encoding => {
+ assert.ok(!Buffer.isEncoding(encoding),
+ 'Buffer.isEncoding ' + encoding + ' falsy');
+ });
+};
+
+exports.testBufferCopy = function (assert) {
+ // counter to ensure unique value is always copied
+ var cntr = 0;
+ var b = Buffer(1024); // safe constructor
+
+ assert.strictEqual(1024, b.length);
+ b[0] = -1;
+ assert.strictEqual(b[0], 255);
+
+ var shimArray = [];
+ for (var i = 0; i < 1024; i++) {
+ b[i] = i % 256;
+ shimArray[i] = i % 256;
+ }
+
+ compareBuffers(assert, b, shimArray, 'array notation');
+
+ var c = new Buffer(512);
+ assert.strictEqual(512, c.length);
+ // copy 512 bytes, from 0 to 512.
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = b.copy(c, 0, 0, 512);
+ assert.strictEqual(512, copied,
+ 'copied ' + copied + ' bytes from b into c');
+
+ compareBuffers(assert, b, c, 'copied to other buffer');
+
+ // copy c into b, without specifying sourceEnd
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = c.copy(b, 0, 0);
+ assert.strictEqual(c.length, copied,
+ 'copied ' + copied + ' bytes from c into b w/o sourceEnd');
+ compareBuffers(assert, b, c,
+ 'copied to other buffer without specifying sourceEnd');
+
+ // copy c into b, without specifying sourceStart
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = c.copy(b, 0);
+ assert.strictEqual(c.length, copied,
+ 'copied ' + copied + ' bytes from c into b w/o sourceStart');
+ compareBuffers(assert, b, c,
+ 'copied to other buffer without specifying sourceStart');
+
+ // copy longer buffer b to shorter c without targetStart
+ b.fill(++cntr);
+ c.fill(++cntr);
+
+ var copied = b.copy(c);
+ assert.strictEqual(c.length, copied,
+ 'copied ' + copied + ' bytes from b into c w/o targetStart');
+ compareBuffers(assert, b, c,
+ 'copy long buffer to shorter buffer without targetStart');
+
+ // copy starting near end of b to c
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = b.copy(c, 0, b.length - Math.floor(c.length / 2));
+ assert.strictEqual(Math.floor(c.length / 2), copied,
+ 'copied ' + copied + ' bytes from end of b into beg. of c');
+
+ let successStatus = true;
+ for (var i = 0; i < Math.floor(c.length / 2); i++) {
+ if (b[b.length - Math.floor(c.length / 2) + i] !== c[i])
+ successStatus = false;
+ }
+
+ for (var i = Math.floor(c.length /2) + 1; i < c.length; i++) {
+ if (c[c.length-1] !== c[i])
+ successStatus = false;
+ }
+ assert.ok(successStatus,
+ 'Copied bytes from end of large buffer into beginning of small buffer');
+
+ // try to copy 513 bytes, and check we don't overrun c
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = b.copy(c, 0, 0, 513);
+ assert.strictEqual(c.length, copied,
+ 'copied ' + copied + ' bytes from b trying to overrun c');
+ compareBuffers(assert, b, c,
+ 'copying to buffer that would overflow');
+
+ // copy 768 bytes from b into b
+ b.fill(++cntr);
+ b.fill(++cntr, 256);
+ var copied = b.copy(b, 0, 256, 1024);
+ assert.strictEqual(768, copied,
+ 'copied ' + copied + ' bytes from b into b');
+
+ compareBuffers(assert, b, shimArray.map(()=>cntr),
+ 'copy partial buffer to itself');
+
+ // copy string longer than buffer length (failure will segfault)
+ var bb = new Buffer(10);
+ bb.fill('hello crazy world');
+
+ // copy throws at negative sourceStart
+ assert.throws(function() {
+ Buffer(5).copy(Buffer(5), 0, -1);
+ }, RangeError, 'buffer copy throws at negative sourceStart');
+
+ // check sourceEnd resets to targetEnd if former is greater than the latter
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = b.copy(c, 0, 0, 1025);
+ assert.strictEqual(copied, c.length,
+ 'copied ' + copied + ' bytes from b into c');
+ compareBuffers(assert, b, c, 'copying should reset sourceEnd if targetEnd if sourceEnd > targetEnd');
+
+ // throw with negative sourceEnd
+ assert.throws(function() {
+ b.copy(c, 0, 0, -1);
+ }, RangeError, 'buffer copy throws at negative sourceEnd');
+
+ // when sourceStart is greater than sourceEnd, zero copied
+ assert.equal(b.copy(c, 0, 100, 10), 0);
+
+ // when targetStart > targetLength, zero copied
+ assert.equal(b.copy(c, 512, 0, 10), 0);
+
+ // try to copy 0 bytes worth of data into an empty buffer
+ b.copy(new Buffer(0), 0, 0, 0);
+
+ // try to copy 0 bytes past the end of the target buffer
+ b.copy(new Buffer(0), 1, 1, 1);
+ b.copy(new Buffer(1), 1, 1, 1);
+
+ // try to copy 0 bytes from past the end of the source buffer
+ b.copy(new Buffer(1), 0, 2048, 2048);
+};
+
+exports.testBufferWrite = function (assert) {
+ let b = Buffer(1024);
+ b.fill(0);
+
+ // try to write a 0-length string beyond the end of b
+ assert.throws(function() {
+ b.write('', 2048);
+ }, RangeError, 'writing a 0-length string beyond buffer throws');
+ // throw when writing to negative offset
+ assert.throws(function() {
+ b.write('a', -1);
+ }, RangeError, 'writing negative offset on buffer throws');
+
+ // throw when writing past bounds from the pool
+ assert.throws(function() {
+ b.write('a', 2048);
+ }, RangeError, 'writing past buffer bounds from pool throws');
+
+ // testing for smart defaults and ability to pass string values as offset
+
+ // previous write API was the following:
+ // write(string, encoding, offset, length)
+ // this is planned on being removed in node v0.13,
+ // we will not support it
+ var writeTest = new Buffer('abcdes');
+ writeTest.write('n', 'utf8');
+// writeTest.write('o', 'utf8', '1');
+ writeTest.write('d', '2', 'utf8');
+ writeTest.write('e', 3, 'utf8');
+// writeTest.write('j', 'utf8', 4);
+ assert.equal(writeTest.toString(), 'nbdees',
+ 'buffer write API alternative syntax works');
+};
+
+exports.testBufferWriteEncoding = function (assert) {
+
+ // Node #1210 Test UTF-8 string includes null character
+ var buf = new Buffer('\0');
+ assert.equal(buf.length, 1);
+ buf = new Buffer('\0\0');
+ assert.equal(buf.length, 2);
+
+ buf = new Buffer(2);
+ var written = buf.write(''); // 0byte
+ assert.equal(written, 0);
+ written = buf.write('\0'); // 1byte (v8 adds null terminator)
+ assert.equal(written, 1);
+ written = buf.write('a\0'); // 1byte * 2
+ assert.equal(written, 2);
+ // TODO, these tests write 0, possibly due to character encoding
+/*
+ written = buf.write('あ'); // 3bytes
+ assert.equal(written, 0);
+ written = buf.write('\0あ'); // 1byte + 3bytes
+ assert.equal(written, 1);
+*/
+ written = buf.write('\0\0あ'); // 1byte * 2 + 3bytes
+ buf = new Buffer(10);
+ written = buf.write('あいう'); // 3bytes * 3 (v8 adds null terminator)
+ assert.equal(written, 9);
+ written = buf.write('あいう\0'); // 3bytes * 3 + 1byte
+ assert.equal(written, 10);
+};
+
+exports.testBufferWriteWithMaxLength = function (assert) {
+ // Node #243 Test write() with maxLength
+ var buf = new Buffer(4);
+ buf.fill(0xFF);
+ var written = buf.write('abcd', 1, 2, 'utf8');
+ assert.equal(written, 2);
+ assert.equal(buf[0], 0xFF);
+ assert.equal(buf[1], 0x61);
+ assert.equal(buf[2], 0x62);
+ assert.equal(buf[3], 0xFF);
+
+ buf.fill(0xFF);
+ written = buf.write('abcd', 1, 4);
+ assert.equal(written, 3);
+ assert.equal(buf[0], 0xFF);
+ assert.equal(buf[1], 0x61);
+ assert.equal(buf[2], 0x62);
+ assert.equal(buf[3], 0x63);
+
+ buf.fill(0xFF);
+ // Ignore legacy API
+ /*
+ written = buf.write('abcd', 'utf8', 1, 2); // legacy style
+ console.log(buf);
+ assert.equal(written, 2);
+ assert.equal(buf[0], 0xFF);
+ assert.equal(buf[1], 0x61);
+ assert.equal(buf[2], 0x62);
+ assert.equal(buf[3], 0xFF);
+ */
+};
+
+exports.testBufferSlice = function (assert) {
+ var asciiString = 'hello world';
+ var offset = 100;
+ var b = Buffer(1024);
+ b.fill(0);
+
+ for (var i = 0; i < asciiString.length; i++) {
+ b[i] = asciiString.charCodeAt(i);
+ }
+ var asciiSlice = b.toString('utf8', 0, asciiString.length);
+ assert.equal(asciiString, asciiSlice);
+
+ var written = b.write(asciiString, offset, 'utf8');
+ assert.equal(asciiString.length, written);
+ asciiSlice = b.toString('utf8', offset, offset + asciiString.length);
+ assert.equal(asciiString, asciiSlice);
+
+ var sliceA = b.slice(offset, offset + asciiString.length);
+ var sliceB = b.slice(offset, offset + asciiString.length);
+ compareBuffers(assert, sliceA, sliceB,
+ 'slicing is idempotent');
+
+ let sliceTest = true;
+ for (var j = 0; j < 100; j++) {
+ var slice = b.slice(100, 150);
+ if (50 !== slice.length)
+ sliceTest = false;
+ for (var i = 0; i < 50; i++) {
+ if (b[100 + i] !== slice[i])
+ sliceTest = false;
+ }
+ }
+ assert.ok(sliceTest, 'massive slice runs do not affect buffer');
+
+ // Single argument slice
+ let testBuf = new Buffer('abcde');
+ assert.equal('bcde', testBuf.slice(1).toString(), 'single argument slice');
+
+ // slice(0,0).length === 0
+ assert.equal(0, Buffer('hello').slice(0, 0).length, 'slice(0,0) === 0');
+
+ var buf = new Buffer('0123456789');
+ assert.equal(buf.slice(-10, 10), '0123456789', 'buffer slice range correct');
+ assert.equal(buf.slice(-20, 10), '0123456789', 'buffer slice range correct');
+ assert.equal(buf.slice(-20, -10), '', 'buffer slice range correct');
+ assert.equal(buf.slice(0, -1), '012345678', 'buffer slice range correct');
+ assert.equal(buf.slice(2, -2), '234567', 'buffer slice range correct');
+ assert.equal(buf.slice(0, 65536), '0123456789', 'buffer slice range correct');
+ assert.equal(buf.slice(65536, 0), '', 'buffer slice range correct');
+
+ sliceTest = true;
+ for (var i = 0, s = buf.toString(); i < buf.length; ++i) {
+ if (buf.slice(-i) != s.slice(-i)) sliceTest = false;
+ if (buf.slice(0, -i) != s.slice(0, -i)) sliceTest = false;
+ }
+ assert.ok(sliceTest, 'buffer.slice should be consistent');
+
+ // Make sure modifying a sliced buffer, affects original and vice versa
+ b.fill(0);
+ let sliced = b.slice(0, 10);
+ let babyslice = sliced.slice(0, 5);
+
+ for (let i = 0; i < sliced.length; i++)
+ sliced[i] = 'jetpack'.charAt(i);
+
+ compareBuffers(assert, b, sliced,
+ 'modifying sliced buffer affects original');
+
+ compareBuffers(assert, b, babyslice,
+ 'modifying sliced buffer affects child-sliced buffer');
+
+ for (let i = 0; i < sliced.length; i++)
+ b[i] = 'odinmonkey'.charAt(i);
+
+ compareBuffers(assert, b, sliced,
+ 'modifying original buffer affects sliced');
+
+ compareBuffers(assert, b, babyslice,
+ 'modifying original buffer affects grandchild sliced buffer');
+};
+
+exports.testSlicingParents = function (assert) {
+ let root = Buffer(5);
+ let child = root.slice(0, 4);
+ let grandchild = child.slice(0, 3);
+
+ assert.equal(root.parent, undefined, 'a new buffer should not have a parent');
+
+ // make sure a zero length slice doesn't set the .parent attribute
+ assert.equal(root.slice(0,0).parent, undefined,
+ '0-length slice should not have a parent');
+
+ assert.equal(child.parent, root,
+ 'a valid slice\'s parent should be the original buffer (child)');
+
+ assert.equal(grandchild.parent, root,
+ 'a valid slice\'s parent should be the original buffer (grandchild)');
+};
+
+exports.testIsBuffer = function (assert) {
+ let buffer = new Buffer('content', 'utf8');
+ assert.ok(Buffer.isBuffer(buffer), 'isBuffer truthy on buffers');
+ assert.ok(!Buffer.isBuffer({}), 'isBuffer falsy on objects');
+ assert.ok(!Buffer.isBuffer(new Uint8Array()),
+ 'isBuffer falsy on Uint8Array');
+ assert.ok(Buffer.isBuffer(buffer.slice(0)), 'Buffer#slice should be a new buffer');
+};
+
+exports.testBufferConcat = function (assert) {
+ let zero = [];
+ let one = [ new Buffer('asdf') ];
+ let long = [];
+ for (let i = 0; i < 10; i++) long.push(new Buffer('asdf'));
+
+ let flatZero = Buffer.concat(zero);
+ let flatOne = Buffer.concat(one);
+ let flatLong = Buffer.concat(long);
+ let flatLongLen = Buffer.concat(long, 40);
+
+ assert.equal(flatZero.length, 0);
+ assert.equal(flatOne.toString(), 'asdf');
+ assert.equal(flatOne, one[0]);
+ assert.equal(flatLong.toString(), (new Array(10+1).join('asdf')));
+ assert.equal(flatLongLen.toString(), (new Array(10+1).join('asdf')));
+};
+
+exports.testBufferByteLength = function (assert) {
+ let str = '\u00bd + \u00bc = \u00be';
+ assert.equal(Buffer.byteLength(str), 12,
+ 'correct byteLength of string');
+
+ assert.equal(14, Buffer.byteLength('Il était tué'));
+ assert.equal(14, Buffer.byteLength('Il était tué', 'utf8'));
+ // We do not currently support these encodings
+ /*
+ ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
+ assert.equal(24, Buffer.byteLength('Il était tué', encoding));
+ });
+ assert.equal(12, Buffer.byteLength('Il était tué', 'ascii'));
+ assert.equal(12, Buffer.byteLength('Il était tué', 'binary'));
+ */
+};
+
+exports.testTextEncoderDecoder = function (assert) {
+ assert.ok(TextEncoder, 'TextEncoder exists');
+ assert.ok(TextDecoder, 'TextDecoder exists');
+};
+
+exports.testOverflowedBuffers = function (assert) {
+ assert.throws(function() {
+ new Buffer(0xFFFFFFFF);
+ }, RangeError, 'correctly throws buffer overflow');
+
+ assert.throws(function() {
+ new Buffer(0xFFFFFFFFF);
+ }, RangeError, 'correctly throws buffer overflow');
+
+ assert.throws(function() {
+ var buf = new Buffer(8);
+ buf.readFloatLE(0xffffffff);
+ }, RangeError, 'correctly throws buffer overflow with readFloatLE');
+
+ assert.throws(function() {
+ var buf = new Buffer(8);
+ buf.writeFloatLE(0.0, 0xffffffff);
+ }, RangeError, 'correctly throws buffer overflow with writeFloatLE');
+
+ //ensure negative values can't get past offset
+ assert.throws(function() {
+ var buf = new Buffer(8);
+ buf.readFloatLE(-1);
+ }, RangeError, 'correctly throws with readFloatLE negative values');
+
+ assert.throws(function() {
+ var buf = new Buffer(8);
+ buf.writeFloatLE(0.0, -1);
+ }, RangeError, 'correctly throws with writeFloatLE with negative values');
+
+ assert.throws(function() {
+ var buf = new Buffer(8);
+ buf.readFloatLE(-1);
+ }, RangeError, 'correctly throws with readFloatLE with negative values');
+};
+
+exports.testReadWriteDataTypeErrors = function (assert) {
+ var buf = new Buffer(0);
+ assert.throws(function() { buf.readUInt8(0); }, RangeError,
+ 'readUInt8(0) throws');
+ assert.throws(function() { buf.readInt8(0); }, RangeError,
+ 'readInt8(0) throws');
+
+ [16, 32].forEach(function(bits) {
+ var buf = new Buffer(bits / 8 - 1);
+ assert.throws(function() { buf['readUInt' + bits + 'BE'](0); },
+ RangeError,
+ 'readUInt' + bits + 'BE');
+
+ assert.throws(function() { buf['readUInt' + bits + 'LE'](0); },
+ RangeError,
+ 'readUInt' + bits + 'LE');
+
+ assert.throws(function() { buf['readInt' + bits + 'BE'](0); },
+ RangeError,
+ 'readInt' + bits + 'BE()');
+
+ assert.throws(function() { buf['readInt' + bits + 'LE'](0); },
+ RangeError,
+ 'readInt' + bits + 'LE()');
+ });
+};
+
+safeMerge(exports, require('./buffers/test-write-types'));
+safeMerge(exports, require('./buffers/test-read-types'));
+
+function compareBuffers (assert, buf1, buf2, message) {
+ let status = true;
+ for (let i = 0; i < Math.min(buf1.length, buf2.length); i++) {
+ if (buf1[i] !== buf2[i])
+ status = false;
+ }
+ assert.ok(status, 'buffer successfully copied: ' + message);
+}
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-byte-streams.js b/addon-sdk/source/test/test-byte-streams.js
new file mode 100644
index 000000000..7d45130aa
--- /dev/null
+++ b/addon-sdk/source/test/test-byte-streams.js
@@ -0,0 +1,169 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const byteStreams = require("sdk/io/byte-streams");
+const file = require("sdk/io/file");
+const { pathFor } = require("sdk/system");
+const { Loader } = require("sdk/test/loader");
+
+const STREAM_CLOSED_ERROR = new RegExp("The stream is closed and cannot be used.");
+
+// This should match the constant of the same name in byte-streams.js.
+const BUFFER_BYTE_LEN = 0x8000;
+
+exports.testWriteRead = function (assert) {
+ let fname = dataFileFilename();
+
+ // Write a small string less than the stream's buffer size...
+ let str = "exports.testWriteRead data!";
+ let stream = open(assert, fname, true);
+ assert.ok(!stream.closed, "stream.closed after open should be false");
+ stream.write(str);
+ stream.close();
+ assert.ok(stream.closed, "Stream should be closed after stream.close");
+ assert.throws(() => stream.write("This shouldn't be written!"),
+ STREAM_CLOSED_ERROR,
+ "stream.write after close should raise error");
+
+ // ... and read it.
+ stream = open(assert, fname);
+ assert.equal(stream.read(), str,
+ "stream.read should return string written");
+ assert.equal(stream.read(), "",
+ "stream.read at EOS should return empty string");
+ stream.close();
+ assert.ok(stream.closed, "Stream should be closed after stream.close");
+ assert.throws(() => stream.read(),
+ STREAM_CLOSED_ERROR,
+ "stream.read after close should raise error");
+
+ file.remove(fname);
+};
+
+// Write a big string many times the size of the stream's buffer and read it.
+exports.testWriteReadBig = function (assert) {
+ let str = "";
+ let bufLen = BUFFER_BYTE_LEN;
+ let fileSize = bufLen * 10;
+ for (let i = 0; i < fileSize; i++)
+ str += i % 10;
+ let fname = dataFileFilename();
+ let stream = open(assert, fname, true);
+ stream.write(str);
+ stream.close();
+ stream = open(assert, fname);
+ assert.equal(stream.read(), str,
+ "stream.read should return string written");
+ stream.close();
+ file.remove(fname);
+};
+
+// The same, but write and read in chunks.
+exports.testWriteReadChunks = function (assert) {
+ let str = "";
+ let bufLen = BUFFER_BYTE_LEN;
+ let fileSize = bufLen * 10;
+ for (let i = 0; i < fileSize; i++)
+ str += i % 10;
+ let fname = dataFileFilename();
+ let stream = open(assert, fname, true);
+ let i = 0;
+ while (i < str.length) {
+ // Use a chunk length that spans buffers.
+ let chunk = str.substr(i, bufLen + 1);
+ stream.write(chunk);
+ i += bufLen + 1;
+ }
+ stream.close();
+ stream = open(assert, fname);
+ let readStr = "";
+ bufLen = BUFFER_BYTE_LEN;
+ let readLen = bufLen + 1;
+ do {
+ var frag = stream.read(readLen);
+ readStr += frag;
+ } while (frag);
+ stream.close();
+ assert.equal(readStr, str,
+ "stream.write and read in chunks should work as expected");
+ file.remove(fname);
+};
+
+exports.testReadLengths = function (assert) {
+ let fname = dataFileFilename();
+ let str = "exports.testReadLengths data!";
+ let stream = open(assert, fname, true);
+ stream.write(str);
+ stream.close();
+
+ stream = open(assert, fname);
+ assert.equal(stream.read(str.length * 1000), str,
+ "stream.read with big byte length should return string " +
+ "written");
+ stream.close();
+
+ stream = open(assert, fname);
+ assert.equal(stream.read(0), "",
+ "string.read with zero byte length should return empty " +
+ "string");
+ stream.close();
+
+ stream = open(assert, fname);
+ assert.equal(stream.read(-1), "",
+ "string.read with negative byte length should return " +
+ "empty string");
+ stream.close();
+
+ file.remove(fname);
+};
+
+exports.testTruncate = function (assert) {
+ let fname = dataFileFilename();
+ let str = "exports.testReadLengths data!";
+ let stream = open(assert, fname, true);
+ stream.write(str);
+ stream.close();
+
+ stream = open(assert, fname);
+ assert.equal(stream.read(), str,
+ "stream.read should return string written");
+ stream.close();
+
+ stream = open(assert, fname, true);
+ stream.close();
+
+ stream = open(assert, fname);
+ assert.equal(stream.read(), "",
+ "stream.read after truncate should be empty");
+ stream.close();
+
+ file.remove(fname);
+};
+
+exports.testUnload = function (assert) {
+ let loader = Loader(module);
+ let file = loader.require("sdk/io/file");
+
+ let filename = dataFileFilename("temp-b");
+ let stream = file.open(filename, "wb");
+
+ loader.unload();
+ assert.ok(stream.closed, "Stream should be closed after module unload");
+};
+
+// Returns the name of a file that should be used to test writing and reading.
+function dataFileFilename() {
+ return file.join(pathFor("ProfD"), "test-byte-streams-data");
+}
+
+// Opens and returns the given file and ensures it's of the correct class.
+function open(assert, filename, forWriting) {
+ let stream = file.open(filename, forWriting ? "wb" : "b");
+ let klass = forWriting ? "ByteWriter" : "ByteReader";
+ assert.ok(stream instanceof byteStreams[klass],
+ "Opened stream should be a " + klass);
+ return stream;
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-child_process.js b/addon-sdk/source/test/test-child_process.js
new file mode 100644
index 000000000..4cfd9ec49
--- /dev/null
+++ b/addon-sdk/source/test/test-child_process.js
@@ -0,0 +1,545 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict';
+
+const { spawn, exec, execFile, fork } = require('sdk/system/child_process');
+const { env, platform, pathFor } = require('sdk/system');
+const { isNumber } = require('sdk/lang/type');
+const { after } = require('sdk/test/utils');
+const { emit } = require('sdk/event/core');
+const PROFILE_DIR= pathFor('ProfD');
+const isWindows = platform.toLowerCase().indexOf('win') === 0;
+const { getScript, cleanUp } = require('./fixtures/child-process-scripts');
+
+// We use direct paths to these utilities as we currently cannot
+// call non-absolute paths to utilities in subprocess.jsm
+const CAT_PATH = isWindows ? 'C:\\Windows\\System32\\more.com' : '/bin/cat';
+
+exports.testExecCallbackSuccess = function (assert, done) {
+ exec(isWindows ? 'DIR /A-D' : 'ls -al', {
+ cwd: PROFILE_DIR
+ }, function (err, stdout, stderr) {
+ assert.ok(!err, 'no errors found');
+ assert.equal(stderr, '', 'stderr is empty');
+ assert.ok(/extensions\.ini/.test(stdout), 'stdout output of `ls -al` finds files');
+
+ if (isWindows) {
+ // `DIR /A-D` does not display directories on WIN
+ assert.ok(!/<DIR>/.test(stdout),
+ 'passing arguments in `exec` works');
+ }
+ else {
+ // `ls -al` should list all the priviledge information on Unix
+ assert.ok(/d(r[-|w][-|x]){3}/.test(stdout),
+ 'passing arguments in `exec` works');
+ }
+ done();
+ });
+};
+
+exports.testExecCallbackError = function (assert, done) {
+ exec('not-real-command', { cwd: PROFILE_DIR }, function (err, stdout, stderr) {
+ assert.ok(/not-real-command/.test(err.toString()),
+ 'error contains error message');
+ assert.ok(err.lineNumber >= 0, 'error contains lineNumber');
+ assert.ok(/resource:\/\//.test(err.fileName), 'error contains fileName');
+ assert.ok(err.code && isNumber(err.code), 'non-zero error code property on error');
+ assert.equal(err.signal, null,
+ 'null signal property when not manually terminated');
+ assert.equal(stdout, '', 'stdout is empty');
+ assert.ok(/not-real-command/.test(stderr), 'stderr contains error message');
+ done();
+ });
+};
+
+exports.testExecOptionsEnvironment = function (assert, done) {
+ getScript('check-env').then(envScript => {
+ exec(envScript, {
+ cwd: PROFILE_DIR,
+ env: { CHILD_PROCESS_ENV_TEST: 'my-value-test' }
+ }, function (err, stdout, stderr) {
+ assert.equal(stderr, '', 'stderr is empty');
+ assert.ok(!err, 'received `cwd` option');
+ assert.ok(/my-value-test/.test(stdout),
+ 'receives environment option');
+ done();
+ });
+ }).then(null, assert.fail);
+};
+
+exports.testExecOptionsTimeout = function (assert, done) {
+ let count = 0;
+ getScript('wait').then(script => {
+ let child = exec(script, { timeout: 100 }, (err, stdout, stderr) => {
+ assert.equal(err.killed, true, 'error has `killed` property as true');
+ assert.equal(err.code, null, 'error has `code` as null');
+ assert.equal(err.signal, 'SIGTERM',
+ 'error has `signal` as SIGTERM by default');
+ assert.equal(stdout, '', 'stdout is empty');
+ assert.equal(stderr, '', 'stderr is empty');
+ if (++count === 3) complete();
+ });
+
+ function exitHandler (code, signal) {
+ assert.equal(code, null, 'error has `code` as null');
+ assert.equal(signal, 'SIGTERM',
+ 'error has `signal` as SIGTERM by default');
+ if (++count === 3) complete();
+ }
+
+ function closeHandler (code, signal) {
+ assert.equal(code, null, 'error has `code` as null');
+ assert.equal(signal, 'SIGTERM',
+ 'error has `signal` as SIGTERM by default');
+ if (++count === 3) complete();
+ }
+
+ child.on('exit', exitHandler);
+ child.on('close', closeHandler);
+
+ function complete () {
+ child.off('exit', exitHandler);
+ child.off('close', closeHandler);
+ done();
+ }
+ }).then(null, assert.fail);
+};
+
+exports.testExecFileCallbackSuccess = function (assert, done) {
+ getScript('args').then(script => {
+ execFile(script, ['--myargs', '-j', '-s'], { cwd: PROFILE_DIR }, function (err, stdout, stderr) {
+ assert.ok(!err, 'no errors found');
+ assert.equal(stderr, '', 'stderr is empty');
+ // Trim output since different systems have different new line output
+ assert.equal(stdout.trim(), '--myargs -j -s'.trim(), 'passes in correct arguments');
+ done();
+ });
+ }).then(null, assert.fail);
+};
+
+exports.testExecFileCallbackError = function (assert, done) {
+ execFile('not-real-command', { cwd: PROFILE_DIR }, function (err, stdout, stderr) {
+ assert.ok(/Executable not found/.test(err.message),
+ `error '${err.message}' contains error message`);
+ assert.ok(err.lineNumber >= 0, 'error contains lineNumber');
+ assert.ok(/resource:\/\//.test(err.fileName), 'error contains fileName');
+ assert.equal(stdout, '', 'stdout is empty');
+ assert.equal(stderr, '', 'stdout is empty');
+ done();
+ });
+};
+
+exports.testExecFileOptionsEnvironment = function (assert, done) {
+ getScript('check-env').then(script => {
+ execFile(script, {
+ cwd: PROFILE_DIR,
+ env: { CHILD_PROCESS_ENV_TEST: 'my-value-test' }
+ }, function (err, stdout, stderr) {
+ assert.equal(stderr, '', 'stderr is empty');
+ assert.ok(!err, 'received `cwd` option');
+ assert.ok(/my-value-test/.test(stdout),
+ 'receives environment option');
+ done();
+ });
+ }).then(null, assert.fail);
+};
+
+exports.testExecFileOptionsTimeout = function (assert, done) {
+ let count = 0;
+ getScript('wait').then(script => {
+ let child = execFile(script, { timeout: 100 }, (err, stdout, stderr) => {
+ assert.equal(err.killed, true, 'error has `killed` property as true');
+ assert.equal(err.code, null, 'error has `code` as null');
+ assert.equal(err.signal, 'SIGTERM',
+ 'error has `signal` as SIGTERM by default');
+ assert.equal(stdout, '', 'stdout is empty');
+ assert.equal(stderr, '', 'stderr is empty');
+ if (++count === 3) complete();
+ });
+
+ function exitHandler (code, signal) {
+ assert.equal(code, null, 'error has `code` as null');
+ assert.equal(signal, 'SIGTERM',
+ 'error has `signal` as SIGTERM by default');
+ if (++count === 3) complete();
+ }
+
+ function closeHandler (code, signal) {
+ assert.equal(code, null, 'error has `code` as null');
+ assert.equal(signal, 'SIGTERM',
+ 'error has `signal` as SIGTERM by default');
+ if (++count === 3) complete();
+ }
+
+ child.on('exit', exitHandler);
+ child.on('close', closeHandler);
+
+ function complete () {
+ child.off('exit', exitHandler);
+ child.off('close', closeHandler);
+ done();
+ }
+ }).then(null, assert.fail);
+};
+
+/**
+ * Not necessary to test for both `exec` and `execFile`, but
+ * it is necessary to test both when the buffer is larger
+ * and smaller than buffer size used by the subprocess library (1024)
+ */
+exports.testExecFileOptionsMaxBufferLargeStdOut = function (assert, done) {
+ let count = 0;
+ let stdoutChild;
+
+ // Creates a buffer of 2000 to stdout, greater than 1024
+ getScript('large-out').then(script => {
+ stdoutChild = execFile(script, ['10000'], { maxBuffer: 50 }, (err, stdout, stderr) => {
+ assert.ok(/stdout maxBuffer exceeded/.test(err.toString()),
+ 'error contains stdout maxBuffer exceeded message');
+ assert.ok(stdout.length >= 50, 'stdout has full buffer');
+ assert.equal(stderr, '', 'stderr is empty');
+ if (++count === 3) complete();
+ });
+ stdoutChild.on('exit', exitHandler);
+ stdoutChild.on('close', closeHandler);
+ }).then(null, assert.fail);
+
+ function exitHandler (code, signal) {
+ assert.equal(code, null, 'Exit code is null in exit handler');
+ assert.equal(signal, 'SIGTERM', 'Signal is SIGTERM in exit handler');
+ if (++count === 3) complete();
+ }
+
+ function closeHandler (code, signal) {
+ assert.equal(code, null, 'Exit code is null in close handler');
+ assert.equal(signal, 'SIGTERM', 'Signal is SIGTERM in close handler');
+ if (++count === 3) complete();
+ }
+
+ function complete () {
+ stdoutChild.off('exit', exitHandler);
+ stdoutChild.off('close', closeHandler);
+ done();
+ }
+};
+
+exports.testExecFileOptionsMaxBufferLargeStdOErr = function (assert, done) {
+ let count = 0;
+ let stderrChild;
+ // Creates a buffer of 2000 to stderr, greater than 1024
+ getScript('large-err').then(script => {
+ stderrChild = execFile(script, ['10000'], { maxBuffer: 50 }, (err, stdout, stderr) => {
+ assert.ok(/stderr maxBuffer exceeded/.test(err.toString()),
+ 'error contains stderr maxBuffer exceeded message');
+ assert.ok(stderr.length >= 50, 'stderr has full buffer');
+ assert.equal(stdout, '', 'stdout is empty');
+ if (++count === 3) complete();
+ });
+ stderrChild.on('exit', exitHandler);
+ stderrChild.on('close', closeHandler);
+ }).then(null, assert.fail);
+
+ function exitHandler (code, signal) {
+ assert.equal(code, null, 'Exit code is null in exit handler');
+ assert.equal(signal, 'SIGTERM', 'Signal is SIGTERM in exit handler');
+ if (++count === 3) complete();
+ }
+
+ function closeHandler (code, signal) {
+ assert.equal(code, null, 'Exit code is null in close handler');
+ assert.equal(signal, 'SIGTERM', 'Signal is SIGTERM in close handler');
+ if (++count === 3) complete();
+ }
+
+ function complete () {
+ stderrChild.off('exit', exitHandler);
+ stderrChild.off('close', closeHandler);
+ done();
+ }
+};
+
+/**
+ * When total buffer is < process buffer (1024), the process will exit
+ * and not get a chance to be killed for violating the maxBuffer,
+ * although the error will still be sent through (node behaviour)
+ */
+exports.testExecFileOptionsMaxBufferSmallStdOut = function (assert, done) {
+ let count = 0;
+ let stdoutChild;
+
+ // Creates a buffer of 60 to stdout, less than 1024
+ getScript('large-out').then(script => {
+ stdoutChild = execFile(script, ['60'], { maxBuffer: 50 }, (err, stdout, stderr) => {
+ assert.ok(/stdout maxBuffer exceeded/.test(err.toString()),
+ 'error contains stdout maxBuffer exceeded message');
+ assert.ok(stdout.length >= 50, 'stdout has full buffer');
+ assert.equal(stderr, '', 'stderr is empty');
+ if (++count === 3) complete();
+ });
+ stdoutChild.on('exit', exitHandler);
+ stdoutChild.on('close', closeHandler);
+ }).then(null, assert.fail);
+
+ function exitHandler (code, signal) {
+ // Sometimes the buffer limit is hit before the process closes successfully
+ // on both OSX/Windows
+ if (code === null) {
+ assert.equal(code, null, 'Exit code is null in exit handler');
+ assert.equal(signal, 'SIGTERM', 'Signal is SIGTERM in exit handler');
+ }
+ else {
+ assert.equal(code, 0, 'Exit code is 0 in exit handler');
+ assert.equal(signal, null, 'Signal is null in exit handler');
+ }
+ if (++count === 3) complete();
+ }
+
+ function closeHandler (code, signal) {
+ // Sometimes the buffer limit is hit before the process closes successfully
+ // on both OSX/Windows
+ if (code === null) {
+ assert.equal(code, null, 'Exit code is null in close handler');
+ assert.equal(signal, 'SIGTERM', 'Signal is SIGTERM in close handler');
+ }
+ else {
+ assert.equal(code, 0, 'Exit code is 0 in close handler');
+ assert.equal(signal, null, 'Signal is null in close handler');
+ }
+ if (++count === 3) complete();
+ }
+
+ function complete () {
+ stdoutChild.off('exit', exitHandler);
+ stdoutChild.off('close', closeHandler);
+ done();
+ }
+};
+
+exports.testExecFileOptionsMaxBufferSmallStdErr = function (assert, done) {
+ let count = 0;
+ let stderrChild;
+ // Creates a buffer of 60 to stderr, less than 1024
+ getScript('large-err').then(script => {
+ stderrChild = execFile(script, ['60'], { maxBuffer: 50 }, (err, stdout, stderr) => {
+ assert.ok(/stderr maxBuffer exceeded/.test(err.toString()),
+ 'error contains stderr maxBuffer exceeded message');
+ assert.ok(stderr.length >= 50, 'stderr has full buffer');
+ assert.equal(stdout, '', 'stdout is empty');
+ if (++count === 3) complete();
+ });
+ stderrChild.on('exit', exitHandler);
+ stderrChild.on('close', closeHandler);
+ }).then(null, assert.fail);
+
+ function exitHandler (code, signal) {
+ // Sometimes the buffer limit is hit before the process closes successfully
+ // on both OSX/Windows
+ if (code === null) {
+ assert.equal(code, null, 'Exit code is null in exit handler');
+ assert.equal(signal, 'SIGTERM', 'Signal is SIGTERM in exit handler');
+ }
+ else {
+ assert.equal(code, 0, 'Exit code is 0 in exit handler');
+ assert.equal(signal, null, 'Signal is null in exit handler');
+ }
+ if (++count === 3) complete();
+ }
+
+ function closeHandler (code, signal) {
+ // Sometimes the buffer limit is hit before the process closes successfully
+ // on both OSX/Windows
+ if (code === null) {
+ assert.equal(code, null, 'Exit code is null in close handler');
+ assert.equal(signal, 'SIGTERM', 'Signal is SIGTERM in close handler');
+ }
+ else {
+ assert.equal(code, 0, 'Exit code is 0 in close handler');
+ assert.equal(signal, null, 'Signal is null in close handler');
+ }
+ if (++count === 3) complete();
+ }
+
+ function complete () {
+ stderrChild.off('exit', exitHandler);
+ stderrChild.off('close', closeHandler);
+ done();
+ }
+};
+
+exports.testChildExecFileKillSignal = function (assert, done) {
+ getScript('wait').then(script => {
+ execFile(script, {
+ killSignal: 'beepbeep',
+ timeout: 10
+ }, function (err, stdout, stderr) {
+ assert.equal(err.signal, 'beepbeep', 'correctly used custom killSignal');
+ done();
+ });
+ }).then(null, assert.fail);
+};
+
+exports.testChildProperties = function (assert, done) {
+ getScript('check-env').then(script => {
+ let child = spawn(script, {
+ env: { CHILD_PROCESS_ENV_TEST: 'my-value-test' }
+ });
+
+ if (isWindows)
+ assert.ok(true, 'Windows environment does not have `pid`');
+ else
+ assert.ok(child.pid > 0, 'Child has a pid');
+ }).then(done, assert.fail);
+};
+
+exports.testChildStdinStreamLarge = function (assert, done) {
+ let REPEAT = 2000;
+ let allData = '';
+ // Use direct paths to more/cat, as we do not currently support calling non-files
+ // from subprocess.jsm
+ let child = spawn(CAT_PATH);
+
+ child.stdout.on('data', onData);
+ child.on('close', onClose);
+
+ for (let i = 0; i < REPEAT; i++)
+ emit(child.stdin, 'data', '12345\n');
+
+ emit(child.stdin, 'end');
+
+ function onData (data) {
+ allData += data;
+ }
+
+ function onClose (code, signal) {
+ child.stdout.off('data', onData);
+ child.off('close', onClose);
+ assert.equal(code, 0, 'exited succesfully');
+ assert.equal(signal, null, 'no kill signal given');
+ assert.equal(allData.replace(/\W/g, '').length, '12345'.length * REPEAT,
+ 'all data processed from stdin');
+ done();
+ }
+};
+
+exports.testChildStdinStreamSmall = function (assert, done) {
+ let allData = '';
+ let child = spawn(CAT_PATH);
+ child.stdout.on('data', onData);
+ child.on('close', onClose);
+
+ emit(child.stdin, 'data', '12345');
+ emit(child.stdin, 'end');
+
+ function onData (data) {
+ allData += data;
+ }
+
+ function onClose (code, signal) {
+ child.stdout.off('data', onData);
+ child.off('close', onClose);
+ assert.equal(code, 0, 'exited succesfully');
+ assert.equal(signal, null, 'no kill signal given');
+ assert.equal(allData.trim(), '12345', 'all data processed from stdin');
+ done();
+ }
+};
+/*
+ * This tests failures when an error is thrown attempting to
+ * spawn the process, like an invalid command
+ */
+exports.testChildEventsSpawningError= function (assert, done) {
+ let handlersCalled = 0;
+ let child = execFile('i-do-not-exist', (err, stdout, stderr) => {
+ assert.ok(err, 'error was passed into callback');
+ assert.equal(stdout, '', 'stdout is empty')
+ assert.equal(stderr, '', 'stderr is empty');
+ if (++handlersCalled === 3) complete();
+ });
+
+ child.on('error', handleError);
+ child.on('exit', handleExit);
+ child.on('close', handleClose);
+
+ function handleError (e) {
+ assert.ok(e, 'error passed into error handler');
+ if (++handlersCalled === 3) complete();
+ }
+
+ function handleClose (code, signal) {
+ assert.equal(code, -1,
+ 'process was never spawned, therefore exit code is -1');
+ assert.equal(signal, null, 'signal should be null');
+ if (++handlersCalled === 3) complete();
+ }
+
+ function handleExit (code, signal) {
+ assert.fail('Close event should not be called on init failure');
+ }
+
+ function complete () {
+ child.off('error', handleError);
+ child.off('exit', handleExit);
+ child.off('close', handleClose);
+ done();
+ }
+};
+
+exports.testSpawnOptions = function (assert, done) {
+ let count = 0;
+ let envStdout = '';
+ let cwdStdout = '';
+ let checkEnv, checkPwd, envChild, cwdChild;
+ getScript('check-env').then(script => {
+ checkEnv = script;
+ return getScript('check-pwd');
+ }).then(script => {
+ checkPwd = script;
+
+ envChild = spawn(checkEnv, {
+ env: { CHILD_PROCESS_ENV_TEST: 'my-value-test' }
+ });
+ cwdChild = spawn(checkPwd, { cwd: PROFILE_DIR });
+
+ // Do these need to be unbound?
+ envChild.stdout.on('data', data => envStdout += data);
+ cwdChild.stdout.on('data', data => cwdStdout += data);
+
+ envChild.on('close', envClose);
+ cwdChild.on('close', cwdClose);
+ }).then(null, assert.fail);
+
+ function envClose () {
+ assert.equal(envStdout.trim(), 'my-value-test', 'spawn correctly passed in ENV');
+ if (++count === 2) complete();
+ }
+
+ function cwdClose () {
+ // Check for PROFILE_DIR in the output because
+ // some systems resolve symbolic links, and on OSX
+ // /var -> /private/var
+ let isCorrectPath = ~cwdStdout.trim().indexOf(PROFILE_DIR);
+ assert.ok(isCorrectPath, 'spawn correctly passed in cwd');
+ if (++count === 2) complete();
+ }
+
+ function complete () {
+ envChild.off('close', envClose);
+ cwdChild.off('close', cwdClose);
+ done();
+ }
+};
+
+exports.testFork = function (assert) {
+ assert.throws(function () {
+ fork();
+ }, /not currently supported/, 'fork() correctly throws an unsupported error');
+};
+
+after(exports, cleanUp);
+
+require("sdk/test").run(exports);
+
+// Test disabled because of bug 979675
+module.exports = {};
diff --git a/addon-sdk/source/test/test-chrome.js b/addon-sdk/source/test/test-chrome.js
new file mode 100644
index 000000000..3d1f29dd0
--- /dev/null
+++ b/addon-sdk/source/test/test-chrome.js
@@ -0,0 +1,84 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+var chrome = require('chrome');
+
+const FIXTURES_URL = module.uri.substr(0, module.uri.lastIndexOf('/') + 1) +
+ 'fixtures/chrome-worker/'
+
+exports['test addEventListener'] = function(assert, done) {
+ let uri = FIXTURES_URL + 'addEventListener.js';
+
+ let worker = new chrome.ChromeWorker(uri);
+ worker.addEventListener('message', function(event) {
+ assert.equal(event.data, 'Hello', 'message received');
+ worker.terminate();
+ done();
+ });
+};
+
+exports['test onmessage'] = function(assert, done) {
+ let uri = FIXTURES_URL + 'onmessage.js';
+
+ let worker = new chrome.ChromeWorker(uri);
+ worker.onmessage = function(event) {
+ assert.equal(event.data, 'ok', 'message received');
+ worker.terminate();
+ done();
+ };
+ worker.postMessage('ok');
+};
+
+exports['test setTimeout'] = function(assert, done) {
+ let uri = FIXTURES_URL + 'setTimeout.js';
+
+ let worker = new chrome.ChromeWorker(uri);
+ worker.onmessage = function(event) {
+ assert.equal(event.data, 'ok', 'setTimeout fired');
+ worker.terminate();
+ done();
+ };
+};
+
+exports['test jsctypes'] = function(assert, done) {
+ let uri = FIXTURES_URL + 'jsctypes.js';
+
+ let worker = new chrome.ChromeWorker(uri);
+ worker.onmessage = function(event) {
+ assert.equal(event.data, 'function', 'ctypes.open is a function');
+ worker.terminate();
+ done();
+ };
+};
+
+exports['test XMLHttpRequest'] = function(assert, done) {
+ let uri = FIXTURES_URL + 'xhr.js';
+
+ let worker = new chrome.ChromeWorker(uri);
+ worker.onmessage = function(event) {
+ assert.equal(event.data, 'ok', 'XMLHttpRequest works');
+ worker.terminate();
+ done();
+ };
+};
+
+exports['test onerror'] = function(assert, done) {
+ let uri = FIXTURES_URL + 'onerror.js';
+
+ let worker = new chrome.ChromeWorker(uri);
+ worker.onerror = function(event) {
+ assert.equal(event.filename, uri, 'event reports the correct uri');
+ assert.equal(event.lineno, 6, 'event reports the correct line number');
+ assert.equal(event.target, worker, 'event reports the correct worker');
+ assert.ok(event.message.match(/ok/),
+ 'event contains the exception message');
+ // Call preventDefault in order to avoid being displayed in JS console.
+ event.preventDefault();
+ worker.terminate();
+ done();
+ };
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-clipboard.js b/addon-sdk/source/test/test-clipboard.js
new file mode 100644
index 000000000..f7ffd05be
--- /dev/null
+++ b/addon-sdk/source/test/test-clipboard.js
@@ -0,0 +1,170 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+require("sdk/clipboard");
+
+const { Cc, Ci } = require("chrome");
+
+const imageTools = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools);
+const io = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
+const appShellService = Cc['@mozilla.org/appshell/appShellService;1'].getService(Ci.nsIAppShellService);
+
+const XHTML_NS = "http://www.w3.org/1999/xhtml";
+const base64png = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYA" +
+ "AABzenr0AAAASUlEQVRYhe3O0QkAIAwD0eyqe3Q993AQ3cBSUKpygfsNTy" +
+ "N5ugbQpK0BAADgP0BRDWXWlwEAAAAAgPsA3rzDaAAAAHgPcGrpgAnzQ2FG" +
+ "bWRR9AAAAABJRU5ErkJggg%3D%3D";
+
+const { base64jpeg } = require("./fixtures");
+
+const { platform } = require("sdk/system");
+// For Windows, Mac and Linux, platform returns the following: winnt, darwin and linux.
+var isWindows = platform.toLowerCase().indexOf("win") == 0;
+
+// Test the typical use case, setting & getting with no flavors specified
+exports["test With No Flavor"] = function(assert) {
+ var contents = "hello there";
+ var flavor = "text";
+ var fullFlavor = "text/unicode";
+ var clip = require("sdk/clipboard");
+
+ // Confirm we set the clipboard
+ assert.ok(clip.set(contents));
+
+ // Confirm flavor is set
+ assert.equal(clip.currentFlavors[0], flavor);
+
+ // Confirm we set the clipboard
+ assert.equal(clip.get(), contents);
+
+ // Confirm we can get the clipboard using the flavor
+ assert.equal(clip.get(flavor), contents);
+
+ // Confirm we can still get the clipboard using the full flavor
+ assert.equal(clip.get(fullFlavor), contents);
+};
+
+// Test the slightly less common case where we specify the flavor
+exports["test With Flavor"] = function(assert) {
+ var contents = "<b>hello there</b>";
+ var contentsText = "hello there";
+
+ // On windows, HTML clipboard includes extra data.
+ // The values are from widget/windows/nsDataObj.cpp.
+ var contentsWindowsHtml = "<html><body>\n<!--StartFragment-->" +
+ contents +
+ "<!--EndFragment-->\n</body>\n</html>";
+
+ var flavor = "html";
+ var fullFlavor = "text/html";
+ var unicodeFlavor = "text";
+ var unicodeFullFlavor = "text/unicode";
+ var clip = require("sdk/clipboard");
+
+ assert.ok(clip.set(contents, flavor));
+
+ assert.equal(clip.currentFlavors[0], unicodeFlavor);
+ assert.equal(clip.currentFlavors[1], flavor);
+ assert.equal(clip.get(), contentsText);
+ assert.equal(clip.get(flavor), isWindows ? contentsWindowsHtml : contents);
+ assert.equal(clip.get(fullFlavor), isWindows ? contentsWindowsHtml : contents);
+ assert.equal(clip.get(unicodeFlavor), contentsText);
+ assert.equal(clip.get(unicodeFullFlavor), contentsText);
+};
+
+// Test that the typical case still works when we specify the flavor to set
+exports["test With Redundant Flavor"] = function(assert) {
+ var contents = "<b>hello there</b>";
+ var flavor = "text";
+ var fullFlavor = "text/unicode";
+ var clip = require("sdk/clipboard");
+
+ assert.ok(clip.set(contents, flavor));
+ assert.equal(clip.currentFlavors[0], flavor);
+ assert.equal(clip.get(), contents);
+ assert.equal(clip.get(flavor), contents);
+ assert.equal(clip.get(fullFlavor), contents);
+};
+
+exports["test Not In Flavor"] = function(assert) {
+ var contents = "hello there";
+ var flavor = "html";
+ var clip = require("sdk/clipboard");
+
+ assert.ok(clip.set(contents));
+ // If there's nothing on the clipboard with this flavor, should return null
+ assert.equal(clip.get(flavor), null);
+};
+
+exports["test Set Image"] = function(assert) {
+ var clip = require("sdk/clipboard");
+ var flavor = "image";
+ var fullFlavor = "image/png";
+
+ assert.ok(clip.set(base64png, flavor), "clipboard set");
+ assert.equal(clip.currentFlavors[0], flavor, "flavor is set");
+};
+
+exports["test Get Image"] = function* (assert) {
+ var clip = require("sdk/clipboard");
+
+ clip.set(base64png, "image");
+
+ var contents = clip.get();
+ const hiddenWindow = appShellService.hiddenDOMWindow;
+ const Image = hiddenWindow.Image;
+ const canvas = hiddenWindow.document.createElementNS(XHTML_NS, "canvas");
+ let context = canvas.getContext("2d");
+
+ const imageURLToPixels = (imageURL) => {
+ return new Promise((resolve) => {
+ let img = new Image();
+
+ img.onload = function() {
+ context.drawImage(this, 0, 0);
+
+ let pixels = Array.join(context.getImageData(0, 0, 32, 32).data);
+ resolve(pixels);
+ };
+
+ img.src = imageURL;
+ });
+ };
+
+ let [base64pngPixels, clipboardPixels] = yield Promise.all([
+ imageURLToPixels(base64png), imageURLToPixels(contents),
+ ]);
+
+ assert.ok(base64pngPixels === clipboardPixels,
+ "Image gets from clipboard equals to image sets to the clipboard");
+};
+
+exports["test Set Image Type Not Supported"] = function(assert) {
+ var clip = require("sdk/clipboard");
+ var flavor = "image";
+
+ assert.throws(function () {
+ clip.set(base64jpeg, flavor);
+ }, "Invalid flavor for image/jpeg");
+
+};
+
+// Notice that `imageTools.decodeImageData`, used by `clipboard.set` method for
+// images, write directly to the javascript console the error in case the image
+// is corrupt, even if the error is catched.
+//
+// See: http://mxr.mozilla.org/mozilla-central/source/image/src/Decoder.cpp#136
+exports["test Set Image Type Wrong Data"] = function(assert) {
+ var clip = require("sdk/clipboard");
+ var flavor = "image";
+
+ var wrongPNG = "data:image/png" + base64jpeg.substr(15);
+
+ assert.throws(function () {
+ clip.set(wrongPNG, flavor);
+ }, "Unable to decode data given in a valid image.");
+};
+
+require("sdk/test").run(exports)
diff --git a/addon-sdk/source/test/test-collection.js b/addon-sdk/source/test/test-collection.js
new file mode 100644
index 000000000..d723c14ce
--- /dev/null
+++ b/addon-sdk/source/test/test-collection.js
@@ -0,0 +1,128 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const collection = require("sdk/util/collection");
+
+exports.testAddRemove = function (assert) {
+ let coll = new collection.Collection();
+ compare(assert, coll, []);
+ addRemove(assert, coll, [], false);
+};
+
+exports.testAddRemoveBackingArray = function (assert) {
+ let items = ["foo"];
+ let coll = new collection.Collection(items);
+ compare(assert, coll, items);
+ addRemove(assert, coll, items, true);
+
+ items = ["foo", "bar"];
+ coll = new collection.Collection(items);
+ compare(assert, coll, items);
+ addRemove(assert, coll, items, true);
+};
+
+exports.testProperty = function (assert) {
+ let obj = makeObjWithCollProp();
+ compare(assert, obj.coll, []);
+ addRemove(assert, obj.coll, [], false);
+
+ // Test single-value set.
+ let items = ["foo"];
+ obj.coll = items[0];
+ compare(assert, obj.coll, items);
+ addRemove(assert, obj.coll, items, false);
+
+ // Test array set.
+ items = ["foo", "bar"];
+ obj.coll = items;
+ compare(assert, obj.coll, items);
+ addRemove(assert, obj.coll, items, false);
+};
+
+exports.testPropertyBackingArray = function (assert) {
+ let items = ["foo"];
+ let obj = makeObjWithCollProp(items);
+ compare(assert, obj.coll, items);
+ addRemove(assert, obj.coll, items, true);
+
+ items = ["foo", "bar"];
+ obj = makeObjWithCollProp(items);
+ compare(assert, obj.coll, items);
+ addRemove(assert, obj.coll, items, true);
+};
+
+// Adds some values to coll and then removes them. initialItems is an array
+// containing coll's initial items. isBacking is true if initialItems is coll's
+// backing array; the point is that updates to coll should affect initialItems
+// if that's the case.
+function addRemove(assert, coll, initialItems, isBacking) {
+ let items = isBacking ? initialItems : initialItems.slice(0);
+ let numInitialItems = items.length;
+
+ // Test add(val).
+ let numInsertions = 5;
+ for (let i = 0; i < numInsertions; i++) {
+ compare(assert, coll, items);
+ coll.add(i);
+ if (!isBacking)
+ items.push(i);
+ }
+ compare(assert, coll, items);
+
+ // Add the items we just added to make sure duplicates aren't added.
+ for (let i = 0; i < numInsertions; i++)
+ coll.add(i);
+ compare(assert, coll, items);
+
+ // Test remove(val). Do a kind of shuffled remove. Remove item 1, then
+ // item 0, 3, 2, 5, 4, ...
+ for (let i = 0; i < numInsertions; i++) {
+ let val = i % 2 ? i - 1 :
+ i === numInsertions - 1 ? i : i + 1;
+ coll.remove(val);
+ if (!isBacking)
+ items.splice(items.indexOf(val), 1);
+ compare(assert, coll, items);
+ }
+ assert.equal(coll.length, numInitialItems,
+ "All inserted items should be removed");
+
+ // Remove the items we just removed. coll should be unchanged.
+ for (let i = 0; i < numInsertions; i++)
+ coll.remove(i);
+ compare(assert, coll, items);
+
+ // Test add and remove([val1, val2]).
+ let newItems = [0, 1];
+ coll.add(newItems);
+ compare(assert, coll, isBacking ? items : items.concat(newItems));
+ coll.remove(newItems);
+ compare(assert, coll, items);
+ assert.equal(coll.length, numInitialItems,
+ "All inserted items should be removed");
+}
+
+// Asserts that the items in coll are the items of array.
+function compare(assert, coll, array) {
+ assert.equal(coll.length, array.length,
+ "Collection length should be correct");
+ let numItems = 0;
+ for (let item in coll) {
+ assert.equal(item, array[numItems], "Items should be equal");
+ numItems++;
+ }
+ assert.equal(numItems, array.length,
+ "Number of items in iteration should be correct");
+}
+
+// Returns a new object with a collection property named "coll". backingArray,
+// if defined, will create the collection with that backing array.
+function makeObjWithCollProp(backingArray) {
+ let obj = {};
+ collection.addCollectionProperty(obj, "coll", backingArray);
+ return obj;
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-commonjs-test-adapter.js b/addon-sdk/source/test/test-commonjs-test-adapter.js
new file mode 100644
index 000000000..936bea918
--- /dev/null
+++ b/addon-sdk/source/test/test-commonjs-test-adapter.js
@@ -0,0 +1,11 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+exports["test custom `Assert`'s"] = require("./commonjs-test-adapter/asserts");
+
+// Disabling this check since it is not yet supported by jetpack.
+// if (module == require.main)
+ require("test").run(exports);
diff --git a/addon-sdk/source/test/test-content-events.js b/addon-sdk/source/test/test-content-events.js
new file mode 100644
index 000000000..059c356c4
--- /dev/null
+++ b/addon-sdk/source/test/test-content-events.js
@@ -0,0 +1,92 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const { Loader } = require("sdk/test/loader");
+const { getMostRecentBrowserWindow, getInnerId } = require("sdk/window/utils");
+const { openTab, closeTab, getBrowserForTab } = require("sdk/tabs/utils");
+const { defer } = require("sdk/core/promise");
+const { curry, identity, partial } = require("sdk/lang/functional");
+
+const { nuke } = require("sdk/loader/sandbox");
+
+const { open: openWindow, close: closeWindow } = require('sdk/window/helpers');
+
+const openBrowserWindow = partial(openWindow, null, {features: {toolbar: true}});
+
+var when = curry(function(options, tab) {
+ let type = options.type || options;
+ let capture = options.capture || false;
+ let target = getBrowserForTab(tab);
+ let { promise, resolve } = defer();
+
+ target.addEventListener(type, function handler(event) {
+ if (!event.target.defaultView.frameElement) {
+ target.removeEventListener(type, handler, capture);
+ resolve(tab);
+ }
+ }, capture);
+
+ return promise;
+});
+
+var use = use = value => () => value;
+
+
+var open = curry((url, window) => openTab(window, url));
+var close = function(tab) {
+ let promise = when("pagehide", tab);
+ closeTab(tab);
+ return promise;
+}
+
+exports["test dead object errors"] = function(assert, done) {
+ let system = require("sdk/system/events");
+ let loader = Loader(module);
+ let { events } = loader.require("sdk/content/events");
+
+ // The dead object error is properly reported on console but
+ // doesn't raise any test's exception
+ function onMessage({ subject }) {
+ let message = subject.wrappedJSObject;
+ let { level } = message;
+ let text = String(message.arguments[0]);
+
+ if (level === "error" && text.includes("can't access dead object"))
+ fail(text);
+ }
+
+ let cleanup = () => system.off("console-api-log-event", onMessage);
+ let fail = (reason) => {
+ cleanup();
+ assert.fail(reason);
+ }
+
+ loader.unload();
+
+ nuke(loader.sharedGlobalSandbox);
+
+ system.on("console-api-log-event", onMessage, true);
+
+ openBrowserWindow().
+ then(closeWindow).
+ then(() => assert.pass("checking dead object errors")).
+ then(cleanup).
+ then(done, fail);
+};
+
+// ignore *-document-global-created events that are not very consistent.
+// only allow data uris that we create to ignore unwanted events, e.g.,
+// about:blank, http:// requests from Fennec's `about:`home page that displays
+// add-ons a user could install, local `searchplugins`, other chrome uris
+// Calls callback if passes filter
+function eventFilter (type, target, callback) {
+ if (target.URL.startsWith("data:text/html,") &&
+ type !== "chrome-document-global-created" &&
+ type !== "content-document-global-created")
+
+ callback();
+}
+require("test").run(exports);
diff --git a/addon-sdk/source/test/test-content-script.js b/addon-sdk/source/test/test-content-script.js
new file mode 100644
index 000000000..d3c7e1bbc
--- /dev/null
+++ b/addon-sdk/source/test/test-content-script.js
@@ -0,0 +1,845 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const hiddenFrames = require("sdk/frame/hidden-frame");
+const { create: makeFrame } = require("sdk/frame/utils");
+const { window } = require("sdk/addon/window");
+const { Loader } = require('sdk/test/loader');
+const { URL } = require("sdk/url");
+const testURI = require("./fixtures").url("test.html");
+const testHost = URL(testURI).scheme + '://' + URL(testURI).host;
+
+/*
+ * Utility function that allow to easily run a proxy test with a clean
+ * new HTML document. See first unit test for usage.
+ */
+function createProxyTest(html, callback) {
+ return function (assert, done) {
+ let url = 'data:text/html;charset=utf-8,' + encodeURIComponent(html);
+ let principalLoaded = false;
+
+ let element = makeFrame(window.document, {
+ nodeName: "iframe",
+ type: "content",
+ allowJavascript: true,
+ allowPlugins: true,
+ allowAuth: true,
+ uri: testURI
+ });
+
+ element.addEventListener("DOMContentLoaded", onDOMReady, false);
+
+ function onDOMReady() {
+ // Reload frame after getting principal from `testURI`
+ if (!principalLoaded) {
+ element.setAttribute("src", url);
+ principalLoaded = true;
+ return;
+ }
+
+ assert.equal(element.getAttribute("src"), url, "correct URL loaded");
+ element.removeEventListener("DOMContentLoaded", onDOMReady,
+ false);
+ let xrayWindow = element.contentWindow;
+ let rawWindow = xrayWindow.wrappedJSObject;
+
+ let isDone = false;
+ let helper = {
+ xrayWindow: xrayWindow,
+ rawWindow: rawWindow,
+ createWorker: function (contentScript) {
+ return createWorker(assert, xrayWindow, contentScript, helper.done);
+ },
+ done: function () {
+ if (isDone)
+ return;
+ isDone = true;
+ element.parentNode.removeChild(element);
+ done();
+ }
+ };
+ callback(helper, assert);
+ }
+ };
+}
+
+function createWorker(assert, xrayWindow, contentScript, done) {
+ let loader = Loader(module);
+ let Worker = loader.require("sdk/content/worker").Worker;
+ let worker = Worker({
+ window: xrayWindow,
+ contentScript: [
+ 'new ' + function () {
+ assert = function assert(v, msg) {
+ self.port.emit("assert", {assertion:v, msg:msg});
+ }
+ done = function done() {
+ self.port.emit("done");
+ }
+ },
+ contentScript
+ ]
+ });
+
+ worker.port.on("done", done);
+ worker.port.on("assert", function (data) {
+ assert.ok(data.assertion, data.msg);
+ });
+
+ return worker;
+}
+
+/* Examples for the `createProxyTest` uses */
+
+var html = "<script>var documentGlobal = true</script>";
+
+exports["test Create Proxy Test"] = createProxyTest(html, function (helper, assert) {
+ // You can get access to regular `test` object in second argument of
+ // `createProxyTest` method:
+ assert.ok(helper.rawWindow.documentGlobal,
+ "You have access to a raw window reference via `helper.rawWindow`");
+ assert.ok(!("documentGlobal" in helper.xrayWindow),
+ "You have access to an XrayWrapper reference via `helper.xrayWindow`");
+
+ // If you do not create a Worker, you have to call helper.done(),
+ // in order to say when your test is finished
+ helper.done();
+});
+
+exports["test Create Proxy Test With Worker"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ "new " + function WorkerScope() {
+ assert(true, "You can do assertions in your content script");
+ // And if you create a worker, you either have to call `done`
+ // from content script or helper.done()
+ done();
+ }
+ );
+
+});
+
+exports["test Create Proxy Test With Events"] = createProxyTest("", function (helper, assert) {
+
+ let worker = helper.createWorker(
+ "new " + function WorkerScope() {
+ self.port.emit("foo");
+ }
+ );
+
+ worker.port.on("foo", function () {
+ assert.pass("You can use events");
+ // And terminate your test with helper.done:
+ helper.done();
+ });
+
+});
+
+/* Disabled due to bug 1038432
+// Bug 714778: There was some issue around `toString` functions
+// that ended up being shared between content scripts
+exports["test Shared To String Proxies"] = createProxyTest("", function(helper) {
+
+ let worker = helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // We ensure that `toString` can't be modified so that nothing could
+ // leak to/from the document and between content scripts
+ // It only applies to JS proxies, there isn't any such issue with xrays.
+ //document.location.toString = function foo() {};
+ document.location.toString.foo = "bar";
+ assert("foo" in document.location.toString, "document.location.toString can be modified");
+ assert(document.location.toString() == "data:text/html;charset=utf-8,",
+ "First document.location.toString()");
+ self.postMessage("next");
+ }
+ );
+ worker.on("message", function () {
+ helper.createWorker(
+ 'new ' + function ContentScriptScope2() {
+ assert(!("foo" in document.location.toString),
+ "document.location.toString is different for each content script");
+ assert(document.location.toString() == "data:text/html;charset=utf-8,",
+ "Second document.location.toString()");
+ done();
+ }
+ );
+ });
+});
+*/
+
+// Ensure that postMessage is working correctly across documents with an iframe
+var html = '<iframe id="iframe" name="test" src="data:text/html;charset=utf-8," />';
+exports["test postMessage"] = createProxyTest(html, function (helper, assert) {
+ let ifWindow = helper.xrayWindow.document.getElementById("iframe").contentWindow;
+ // Listen without proxies, to check that it will work in regular case
+ // simulate listening from a web document.
+ ifWindow.addEventListener("message", function listener(event) {
+ ifWindow.removeEventListener("message", listener, false);
+ // As we are in system principal, event is an XrayWrapper
+ // xrays use current compartments when calling postMessage method.
+ // Whereas js proxies was using postMessage method compartment,
+ // not the caller one.
+ assert.strictEqual(event.source, helper.xrayWindow,
+ "event.source is the top window");
+ assert.equal(event.origin, testHost, "origin matches testHost");
+
+ assert.equal(event.data, "{\"foo\":\"bar\\n \\\"escaped\\\".\"}",
+ "message data is correct");
+
+ helper.done();
+ }, false);
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ var json = JSON.stringify({foo : "bar\n \"escaped\"."});
+
+ document.getElementById("iframe").contentWindow.postMessage(json, "*");
+ }
+ );
+});
+
+var html = '<input id="input2" type="checkbox" />';
+exports["test Object Listener"] = createProxyTest(html, function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // Test objects being given as event listener
+ let input = document.getElementById("input2");
+ let myClickListener = {
+ called: false,
+ handleEvent: function(event) {
+ assert(this === myClickListener, "`this` is the original object");
+ assert(!this.called, "called only once");
+ this.called = true;
+ assert(event.target, input, "event.target is the wrapped window");
+ done();
+ }
+ };
+
+ window.addEventListener("click", myClickListener, true);
+ input.click();
+ window.removeEventListener("click", myClickListener, true);
+ }
+ );
+
+});
+
+exports["test Object Listener 2"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ ('new ' + function ContentScriptScope() {
+ // variable replaced with `testHost`
+ let testHost = "TOKEN";
+ // Verify object as DOM event listener
+ let myMessageListener = {
+ called: false,
+ handleEvent: function(event) {
+ window.removeEventListener("message", myMessageListener, true);
+
+ assert(this == myMessageListener, "`this` is the original object");
+ assert(!this.called, "called only once");
+ this.called = true;
+ assert(event.target == document.defaultView, "event.target is the wrapped window");
+ assert(event.source == document.defaultView, "event.source is the wrapped window");
+ assert(event.origin == testHost, "origin matches testHost");
+ assert(event.data == "ok", "message data is correct");
+ done();
+ }
+ };
+
+ window.addEventListener("message", myMessageListener, true);
+ document.defaultView.postMessage("ok", '*');
+ }
+ ).replace("TOKEN", testHost));
+
+});
+
+var html = '<input id="input" type="text" /><input id="input3" type="checkbox" />' +
+ '<input id="input2" type="checkbox" />';
+
+exports.testStringOverload = createProxyTest(html, function (helper, assert) {
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // RightJS is hacking around String.prototype, and do similar thing:
+ // Pass `this` from a String prototype method.
+ // It is funny because typeof this == "object"!
+ // So that when we pass `this` to a native method,
+ // our proxy code can fail on another even more crazy thing.
+ // See following test to see what fails around proxies.
+ String.prototype.update = function () {
+ assert(typeof this == "object", "in update, `this` is an object");
+ assert(this.toString() == "input", "in update, `this.toString works");
+ return document.querySelectorAll(this);
+ };
+ assert("input".update().length == 3, "String.prototype overload works");
+ done();
+ }
+ );
+});
+
+exports["test Element.matches()"] = createProxyTest("", function (helper) {
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // Check matches XrayWrappers bug (Bug 658909):
+ // Test that Element.matches() does not return bad results when we are
+ // not calling it from the node itself.
+ assert(document.createElement( "div" ).matches("div"),
+ "matches works while being called from the node");
+ assert(document.documentElement.matches.call(
+ document.createElement( "div" ),
+ "div"
+ ),
+ "matches works while being called from a " +
+ "function reference to " +
+ "document.documentElement.matches.call");
+ done();
+ }
+ );
+});
+
+exports["test Events Overload"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // If we add a "____proxy" attribute on XrayWrappers in order to store
+ // the related proxy to create an unique proxy for each wrapper;
+ // we end up setting this attribute to prototype objects :x
+ // And so, instances created with such prototype will be considered
+ // as equal to the prototype ...
+ // // Internal method that return the proxy for a given XrayWrapper
+ // function proxify(obj) {
+ // if (obj._proxy) return obj._proxy;
+ // return obj._proxy = Proxy.create(...);
+ // }
+ //
+ // // Get a proxy of an XrayWrapper prototype object
+ // let proto = proxify(xpcProto);
+ //
+ // // Use this proxy as a prototype
+ // function Constr() {}
+ // Constr.proto = proto;
+ //
+ // // Try to create an instance using this prototype
+ // let xpcInstance = new Constr();
+ // let wrapper = proxify(xpcInstance)
+ //
+ // xpcProto._proxy = proto and as xpcInstance.__proto__ = xpcProto,
+ // xpcInstance._proxy = proto ... and profixy(xpcInstance) = proto :(
+ //
+ let proto = window.document.createEvent('HTMLEvents').__proto__;
+ window.Event.prototype = proto;
+ let event = document.createEvent('HTMLEvents');
+ assert(event !== proto, "Event should not be equal to its prototype");
+ event.initEvent('dataavailable', true, true);
+ assert(event.type === 'dataavailable', "Events are working fine");
+ done();
+ }
+ );
+
+});
+
+exports["test Nested Attributes"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // XrayWrappers has a bug when you set an attribute on it,
+ // in some cases, it creates an unnecessary wrapper that introduces
+ // a different object that refers to the same original object
+ // Check that our wrappers don't reproduce this bug
+ // SEE BUG 658560: Fix identity problem with CrossOriginWrappers
+ let o = {sandboxObject:true};
+ window.nested = o;
+ o.foo = true;
+ assert(o === window.nested, "Nested attribute to sandbox object should not be proxified");
+ window.nested = document;
+ assert(window.nested === document, "Nested attribute to proxy should not be double proxified");
+ done();
+ }
+ );
+
+});
+
+exports["test Form nodeName"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ let body = document.body;
+ // Check form[nodeName]
+ let form = document.createElement("form");
+ let input = document.createElement("input");
+ input.setAttribute("name", "test");
+ form.appendChild(input);
+ body.appendChild(form);
+ assert(form.test == input, "form[nodeName] is valid");
+ body.removeChild(form);
+ done();
+ }
+ );
+
+});
+
+exports["test localStorage"] = createProxyTest("", function (helper, assert) {
+
+ let worker = helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // Check localStorage:
+ assert(window.localStorage, "has access to localStorage");
+ window.localStorage.name = 1;
+ assert(window.localStorage.name == 1, "localStorage appears to work");
+
+ self.port.on("step2", function () {
+ window.localStorage.clear();
+ assert(window.localStorage.name == undefined, "localStorage really, really works");
+ done();
+ });
+ self.port.emit("step1");
+ }
+ );
+
+ worker.port.on("step1", function () {
+ assert.equal(helper.rawWindow.localStorage.name, 1, "localStorage really works");
+ worker.port.emit("step2");
+ });
+
+});
+
+exports["test Auto Unwrap Custom Attributes"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ let body = document.body;
+ // Setting a custom object to a proxy attribute is not wrapped when we get it afterward
+ let object = {custom: true, enumerable: false};
+ body.customAttribute = object;
+ assert(object === body.customAttribute, "custom JS attributes are not wrapped");
+ done();
+ }
+ );
+
+});
+
+exports["test Object Tag"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // <object>, <embed> and other tags return typeof 'function'
+ let flash = document.createElement("object");
+ assert(typeof flash == "function", "<object> is typeof 'function'");
+ assert(flash.toString().match(/\[object HTMLObjectElement.*\]/), "<object> is HTMLObjectElement");
+ assert("setAttribute" in flash, "<object> has a setAttribute method");
+ done();
+ }
+ );
+
+});
+
+exports["test Highlight toString Behavior"] = createProxyTest("", function (helper, assert) {
+ // We do not have any workaround this particular use of toString
+ // applied on <object> elements. So disable this test until we found one!
+ //assert.equal(helper.rawWindow.Object.prototype.toString.call(flash), "[object HTMLObjectElement]", "<object> is HTMLObjectElement");
+ function f() {};
+ let funToString = Object.prototype.toString.call(f);
+ assert.ok(/\[object Function.*\]/.test(funToString), "functions are functions 1");
+
+ // This is how jquery call toString:
+ let strToString = helper.rawWindow.Object.prototype.toString.call("");
+ assert.ok(/\[object String.*\]/.test(strToString), "strings are strings");
+
+ let o = {__exposedProps__:{}};
+ let objToString = helper.rawWindow.Object.prototype.toString.call(o);
+ assert.ok(/\[object Object.*\]/.test(objToString), "objects are objects");
+
+ // Make sure to pass a function from the same compartments
+ // or toString will return [object Object] on FF8+
+ let f2 = helper.rawWindow.eval("(function () {})");
+ let funToString2 = helper.rawWindow.Object.prototype.toString.call(f2);
+ assert.ok(/\[object Function.*\]/.test(funToString2), "functions are functions 2");
+
+ helper.done();
+});
+
+exports["test Document TagName"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ let body = document.body;
+ // Check document[tagName]
+ let div = document.createElement("div");
+ div.setAttribute("name", "test");
+ body.appendChild(div);
+ assert(!document.test, "document[divName] is undefined");
+ body.removeChild(div);
+
+ let form = document.createElement("form");
+ form.setAttribute("name", "test");
+ body.appendChild(form);
+ assert(document.test == form, "document[formName] is valid");
+ body.removeChild(form);
+
+ let img = document.createElement("img");
+ img.setAttribute("name", "test");
+ body.appendChild(img);
+ assert(document.test == img, "document[imgName] is valid");
+ body.removeChild(img);
+ done();
+ }
+ );
+
+});
+
+var html = '<iframe id="iframe" name="test" src="data:text/html;charset=utf-8," />';
+exports["test Window Frames"] = createProxyTest(html, function (helper) {
+
+ helper.createWorker(
+ 'let glob = this; new ' + function ContentScriptScope() {
+ // Check window[frameName] and window.frames[i]
+ let iframe = document.getElementById("iframe");
+ //assert(window.frames.length == 1, "The iframe is reported in window.frames check1");
+ //assert(window.frames[0] == iframe.contentWindow, "The iframe is reported in window.frames check2");
+ assert(window.test == iframe.contentWindow, "window[frameName] is valid");
+ done();
+ }
+ );
+
+});
+
+exports["test Collections"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // Highlight XPCNativeWrapper bug with HTMLCollection
+ // tds[0] is only defined on first access :o
+ let body = document.body;
+ let div = document.createElement("div");
+ body.appendChild(div);
+ div.innerHTML = "<table><tr><td style='padding:0;border:0;display:none'></td><td>t</td></tr></table>";
+ let tds = div.getElementsByTagName("td");
+ assert(tds[0] == tds[0], "We can get array element multiple times");
+ body.removeChild(div);
+ done();
+ }
+ );
+
+});
+
+var html = '<input id="input" type="text" /><input id="input3" type="checkbox" />' +
+ '<input id="input2" type="checkbox" />';
+exports["test Collections 2"] = createProxyTest(html, function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // Verify that NodeList/HTMLCollection are working fine
+ let body = document.body;
+ let inputs = body.getElementsByTagName("input");
+ assert(body.childNodes.length == 3, "body.childNodes length is correct");
+ assert(inputs.length == 3, "inputs.length is correct");
+ assert(body.childNodes[0] == inputs[0], "body.childNodes[0] is correct");
+ assert(body.childNodes[1] == inputs[1], "body.childNodes[1] is correct");
+ assert(body.childNodes[2] == inputs[2], "body.childNodes[2] is correct");
+ let count = 0;
+ for(let i in body.childNodes) {
+ count++;
+ }
+
+ assert(count >= 3, "body.childNodes is iterable");
+ done();
+ }
+ );
+
+});
+
+exports["test XMLHttpRequest"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // XMLHttpRequest doesn't support XMLHttpRequest.apply,
+ // that may break our proxy code
+ assert(new window.XMLHttpRequest(), "we are able to instantiate XMLHttpRequest object");
+ done();
+ }
+ );
+
+});
+
+exports["test XPathResult"] = createProxyTest("", function (helper, assert) {
+ const XPathResultTypes = ["ANY_TYPE",
+ "NUMBER_TYPE", "STRING_TYPE", "BOOLEAN_TYPE",
+ "UNORDERED_NODE_ITERATOR_TYPE",
+ "ORDERED_NODE_ITERATOR_TYPE",
+ "UNORDERED_NODE_SNAPSHOT_TYPE",
+ "ORDERED_NODE_SNAPSHOT_TYPE",
+ "ANY_UNORDERED_NODE_TYPE",
+ "FIRST_ORDERED_NODE_TYPE"];
+
+ // Check XPathResult bug with constants being undefined on XPCNativeWrapper
+ let xpcXPathResult = helper.xrayWindow.XPathResult;
+
+ XPathResultTypes.forEach(function(type, i) {
+ assert.equal(xpcXPathResult.wrappedJSObject[type],
+ helper.rawWindow.XPathResult[type],
+ "XPathResult's constants are valid on unwrapped node");
+
+ assert.equal(xpcXPathResult[type], i,
+ "XPathResult's constants are defined on " +
+ "XPCNativeWrapper (platform bug #)");
+ });
+
+ let value = helper.rawWindow.XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE;
+ let worker = helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ self.port.on("value", function (value) {
+ // Check that our work around is working:
+ assert(window.XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE === value,
+ "XPathResult works correctly on Proxies");
+ done();
+ });
+ }
+ );
+ worker.port.emit("value", value);
+});
+
+exports["test Prototype Inheritance"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // Verify that inherited prototype function like initEvent
+ // are handled correctly. (e2.type will return an error if it's not the case)
+ let event1 = document.createEvent( 'MouseEvents' );
+ event1.initEvent( "click", true, true );
+ let event2 = document.createEvent( 'MouseEvents' );
+ event2.initEvent( "click", true, true );
+ assert(event2.type == "click", "We are able to create an event");
+ done();
+ }
+ );
+
+});
+
+exports["test Functions"] = createProxyTest("", function (helper) {
+
+ helper.rawWindow.callFunction = function callFunction(f) {
+ return f();
+ };
+ helper.rawWindow.isEqual = function isEqual(a, b) {
+ return a == b;
+ };
+ // bug 784116: workaround in order to allow proxy code to cache proxies on
+ // these functions:
+ helper.rawWindow.callFunction.__exposedProps__ = {__proxy: 'rw'};
+ helper.rawWindow.isEqual.__exposedProps__ = {__proxy: 'rw'};
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // Check basic usage of functions
+ let closure2 = function () {return "ok";};
+ assert(window.wrappedJSObject.callFunction(closure2) == "ok", "Function references work");
+
+ // Ensure that functions are cached when being wrapped to native code
+ let closure = function () {};
+ assert(window.wrappedJSObject.isEqual(closure, closure), "Function references are cached before being wrapped to native");
+ done();
+ }
+ );
+
+});
+
+var html = '<input id="input2" type="checkbox" />';
+exports["test Listeners"] = createProxyTest(html, function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // Verify listeners:
+ let input = document.getElementById("input2");
+ assert(input, "proxy.getElementById works");
+
+ function onclick() {};
+ input.onclick = onclick;
+ assert(input.onclick === onclick, "on* attributes are equal to original function set");
+
+ let addEventListenerCalled = false;
+ let expandoCalled = false;
+ input.addEventListener("click", function onclick(event) {
+ input.removeEventListener("click", onclick, true);
+
+ assert(!addEventListenerCalled, "closure given to addEventListener is called once");
+ if (addEventListenerCalled)
+ return;
+ addEventListenerCalled = true;
+
+ assert(!event.target.ownerDocument.defaultView.documentGlobal, "event object is still wrapped and doesn't expose document globals");
+
+ let input2 = document.getElementById("input2");
+
+ input.onclick = function (event) {
+ input.onclick = null;
+ assert(!expandoCalled, "closure set to expando is called once");
+ if (expandoCalled) return;
+ expandoCalled = true;
+
+ assert(!event.target.ownerDocument.defaultView.documentGlobal, "event object is still wrapped and doesn't expose document globals");
+
+ setTimeout(function () {
+ input.click();
+ done();
+ }, 0);
+
+ }
+
+ setTimeout(function () {
+ input.click();
+ }, 0);
+
+ }, true);
+
+ input.click();
+ }
+ );
+
+});
+
+exports["test requestAnimationFrame"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ var self = (function() { return this; })();
+ window.requestAnimationFrame(function callback() {
+ assert(self == this, "self is equal to `this`");
+ done();
+ });
+ }
+ );
+
+});
+
+exports["testGlobalScope"] = createProxyTest("", function (helper) {
+
+ helper.createWorker(
+ 'var toplevelScope = true;' +
+ 'assert(window.toplevelScope, "variables in toplevel scope are set to `window` object");' +
+ 'assert(this.toplevelScope, "variables in toplevel scope are set to `this` object");' +
+ 'done();'
+ );
+
+});
+
+// Bug 715755: proxy code throw an exception on COW
+// Create an http server in order to simulate real cross domain documents
+exports["test Cross Domain Iframe"] = createProxyTest("", function (helper) {
+ let serverPort = 8099;
+ let server = require("./lib/httpd").startServerAsync(serverPort);
+ server.registerPathHandler("/", function handle(request, response) {
+ // Returns the webpage that receive a message and forward it back to its
+ // parent document by appending ' world'.
+ let content = "<html><head><meta charset='utf-8'></head>\n";
+ content += "<script>\n";
+ content += " window.addEventListener('message', function (event) {\n";
+ content += " parent.postMessage(event.data + ' world', '*');\n";
+ content += " }, true);\n";
+ content += "</script>\n";
+ content += "<body></body>\n";
+ content += "</html>\n";
+ response.write(content);
+ });
+
+ let worker = helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ // Waits for the server page url
+ self.on("message", function (url) {
+ // Creates an iframe with this page
+ let iframe = document.createElement("iframe");
+ iframe.addEventListener("load", function onload() {
+ iframe.removeEventListener("load", onload, true);
+ try {
+ // Try to communicate with iframe's content
+ window.addEventListener("message", function onmessage(event) {
+ window.removeEventListener("message", onmessage, true);
+
+ assert(event.data == "hello world", "COW works properly");
+ self.port.emit("end");
+ }, true);
+ iframe.contentWindow.postMessage("hello", "*");
+ } catch(e) {
+ assert(false, "COW fails : "+e.message);
+ }
+ }, true);
+ iframe.setAttribute("src", url);
+ document.body.appendChild(iframe);
+ });
+ }
+ );
+
+ worker.port.on("end", function () {
+ server.stop(helper.done);
+ });
+
+ worker.postMessage("http://localhost:" + serverPort + "/");
+
+});
+
+// Bug 769006: Ensure that MutationObserver works fine with proxies
+var html = '<a href="foo">link</a>';
+exports["test MutationObvserver"] = createProxyTest(html, function (helper) {
+
+ helper.createWorker(
+ 'new ' + function ContentScriptScope() {
+ if (typeof MutationObserver == "undefined") {
+ assert(true, "No MutationObserver for this FF version");
+ done();
+ return;
+ }
+ let link = document.getElementsByTagName("a")[0];
+
+ // Register a Mutation observer
+ let obs = new MutationObserver(function(mutations){
+ // Ensure that mutation data are valid
+ assert(mutations.length == 1, "only one attribute mutation");
+ let mutation = mutations[0];
+ assert(mutation.type == "attributes", "check `type`");
+ assert(mutation.target == link, "check `target`");
+ assert(mutation.attributeName == "href", "check `attributeName`");
+ assert(mutation.oldValue == "foo", "check `oldValue`");
+ obs.disconnect();
+ done();
+ });
+ obs.observe(document, {
+ subtree: true,
+ attributes: true,
+ attributeOldValue: true,
+ attributeFilter: ["href"]
+ });
+
+ // Modify the DOM
+ link.setAttribute("href", "bar");
+ }
+ );
+
+});
+
+var html = '<script>' +
+ 'var accessCheck = function() {' +
+ ' assert(true, "exporting function works");' +
+ ' try{' +
+ ' exportedObj.prop;' +
+ ' assert(false, "content should not have access to content-script");' +
+ ' } catch(e) {' +
+ ' assert(e.toString().indexOf("Permission denied") != -1,' +
+ ' "content should not have access to content-script");' +
+ ' }' +
+ '}</script>';
+exports["test nsEp for content-script"] = createProxyTest(html, function (helper) {
+
+ helper.createWorker(
+ 'let glob = this; new ' + function ContentScriptScope() {
+
+ exportFunction(assert, unsafeWindow, { defineAs: "assert" });
+ window.wrappedJSObject.assert(true, "assert exported");
+ window.wrappedJSObject.exportedObj = { prop: 42 };
+ window.wrappedJSObject.accessCheck();
+ done();
+ }
+ );
+
+});
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-content-sync-worker.js b/addon-sdk/source/test/test-content-sync-worker.js
new file mode 100644
index 000000000..aa93f4d7c
--- /dev/null
+++ b/addon-sdk/source/test/test-content-sync-worker.js
@@ -0,0 +1,965 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+// Skipping due to window creation being unsupported in Fennec
+module.metadata = {
+ engines: {
+ 'Firefox': '*'
+ }
+};
+
+const { Cc, Ci } = require("chrome");
+const { on } = require("sdk/event/core");
+const { setTimeout } = require("sdk/timers");
+const { LoaderWithHookedConsole } = require("sdk/test/loader");
+const { Worker } = require("sdk/deprecated/sync-worker");
+const { close } = require("sdk/window/helpers");
+const { set: setPref } = require("sdk/preferences/service");
+const { isArray } = require("sdk/lang/type");
+const { URL } = require('sdk/url');
+const fixtures = require("./fixtures");
+
+const DEPRECATE_PREF = "devtools.errorconsole.deprecation_warnings";
+
+const DEFAULT_CONTENT_URL = "data:text/html;charset=utf-8,foo";
+
+const WINDOW_SCRIPT_URL = "data:text/html;charset=utf-8," +
+ "<script>window.addEventListener('message', function (e) {" +
+ " if (e.data === 'from -> content-script')" +
+ " window.postMessage('from -> window', '*');" +
+ "});</script>";
+
+function makeWindow() {
+ let content =
+ "<?xml version=\"1.0\"?>" +
+ "<window " +
+ "xmlns=\"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul\">" +
+ "<script>var documentValue=true;</script>" +
+ "</window>";
+ var url = "data:application/vnd.mozilla.xul+xml;charset=utf-8," +
+ encodeURIComponent(content);
+ var features = ["chrome", "width=10", "height=10"];
+
+ return Cc["@mozilla.org/embedcomp/window-watcher;1"].
+ getService(Ci.nsIWindowWatcher).
+ openWindow(null, url, null, features.join(","), null);
+}
+
+// Listen for only first one occurence of DOM event
+function listenOnce(node, eventName, callback) {
+ node.addEventListener(eventName, function onevent(event) {
+ node.removeEventListener(eventName, onevent, true);
+ callback(node);
+ }, true);
+}
+
+// Load a given url in a given browser and fires the callback when it is loaded
+function loadAndWait(browser, url, callback) {
+ listenOnce(browser, "load", callback);
+ // We have to wait before calling `loadURI` otherwise, if we call
+ // `loadAndWait` during browser load event, the history will be broken
+ setTimeout(function () {
+ browser.loadURI(url);
+ }, 0);
+}
+
+// Returns a test function that will automatically open a new chrome window
+// with a <browser> element loaded on a given content URL
+// The callback receive 3 arguments:
+// - test: reference to the jetpack test object
+// - browser: a reference to the <browser> xul node
+// - done: a callback to call when test is over
+function WorkerTest(url, callback) {
+ return function testFunction(assert, done) {
+ let chromeWindow = makeWindow();
+ chromeWindow.addEventListener("load", function onload() {
+ chromeWindow.removeEventListener("load", onload, true);
+ let browser = chromeWindow.document.createElement("browser");
+ browser.setAttribute("type", "content");
+ chromeWindow.document.documentElement.appendChild(browser);
+ // Wait for about:blank load event ...
+ listenOnce(browser, "load", function onAboutBlankLoad() {
+ // ... before loading the expected doc and waiting for its load event
+ loadAndWait(browser, url, function onDocumentLoaded() {
+ callback(assert, browser, function onTestDone() {
+
+ close(chromeWindow).then(done);
+ });
+ });
+ });
+ }, true);
+ };
+}
+
+exports["test:sample"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ assert.notEqual(browser.contentWindow.location.href, "about:blank",
+ "window is now on the right document");
+
+ let window = browser.contentWindow
+ let worker = Worker({
+ window: window,
+ contentScript: "new " + function WorkerScope() {
+ // window is accessible
+ let myLocation = window.location.toString();
+ self.on("message", function(data) {
+ if (data == "hi!")
+ self.postMessage("bye!");
+ });
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(msg) {
+ assert.equal("bye!", msg);
+ assert.equal(worker.url, window.location.href,
+ "worker.url still works");
+ done();
+ }
+ });
+
+ assert.equal(worker.url, window.location.href,
+ "worker.url works");
+ assert.equal(worker.contentURL, window.location.href,
+ "worker.contentURL works");
+ worker.postMessage("hi!");
+ }
+);
+
+exports["test:emit"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ // Validate self.on and self.emit
+ self.port.on("addon-to-content", function (data) {
+ self.port.emit("content-to-addon", data);
+ });
+
+ // Check for global pollution
+ //if (typeof on != "undefined")
+ // self.postMessage("`on` is in globals");
+ if (typeof once != "undefined")
+ self.postMessage("`once` is in globals");
+ if (typeof emit != "undefined")
+ self.postMessage("`emit` is in globals");
+
+ },
+ onMessage: function(msg) {
+ assert.fail("Got an unexpected message : "+msg);
+ }
+ });
+
+ // Validate worker.port
+ worker.port.on("content-to-addon", function (data) {
+ assert.equal(data, "event data");
+ done();
+ });
+ worker.port.emit("addon-to-content", "event data");
+ }
+);
+
+exports["test:emit hack message"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ // Validate self.port
+ self.port.on("message", function (data) {
+ self.port.emit("message", data);
+ });
+ // We should not receive message on self, but only on self.port
+ self.on("message", function (data) {
+ self.postMessage("message", data);
+ });
+ },
+ onError: function(e) {
+ assert.fail("Got exception: "+e);
+ }
+ });
+
+ worker.port.on("message", function (data) {
+ assert.equal(data, "event data");
+ done();
+ });
+ worker.on("message", function (data) {
+ assert.fail("Got an unexpected message : "+msg);
+ });
+ worker.port.emit("message", "event data");
+ }
+);
+
+exports["test:n-arguments emit"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let repeat = 0;
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ // Validate self.on and self.emit
+ self.port.on("addon-to-content", function (a1, a2, a3) {
+ self.port.emit("content-to-addon", a1, a2, a3);
+ });
+ }
+ });
+
+ // Validate worker.port
+ worker.port.on("content-to-addon", function (arg1, arg2, arg3) {
+ if (!repeat++) {
+ this.emit("addon-to-content", "first argument", "second", "third");
+ } else {
+ assert.equal(arg1, "first argument");
+ assert.equal(arg2, "second");
+ assert.equal(arg3, "third");
+ done();
+ }
+ });
+ worker.port.emit("addon-to-content", "first argument", "second", "third");
+ }
+);
+
+exports["test:post-json-values-only"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ self.on("message", function (message) {
+ self.postMessage([ message.fun === undefined,
+ typeof message.w,
+ message.w && "port" in message.w,
+ message.w._url,
+ Array.isArray(message.array),
+ JSON.stringify(message.array)]);
+ });
+ }
+ });
+
+ // Validate worker.onMessage
+ let array = [1, 2, 3];
+ worker.on("message", function (message) {
+ assert.ok(message[0], "function becomes undefined");
+ assert.equal(message[1], "object", "object stays object");
+ assert.ok(message[2], "object's attributes are enumerable");
+ assert.equal(message[3], DEFAULT_CONTENT_URL,
+ "jsonable attributes are accessible");
+ // See bug 714891, Arrays may be broken over compartements:
+ assert.ok(message[4], "Array keeps being an array");
+ assert.equal(message[5], JSON.stringify(array),
+ "Array is correctly serialized");
+ done();
+ });
+ // Add a new url property sa the Class function used by
+ // Worker doesn't set enumerables to true for non-functions
+ worker._url = DEFAULT_CONTENT_URL;
+
+ worker.postMessage({ fun: function () {}, w: worker, array: array });
+ }
+);
+
+exports["test:emit-json-values-only"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ // Validate self.on and self.emit
+ self.port.on("addon-to-content", function (fun, w, obj, array) {
+ self.port.emit("content-to-addon", [
+ fun === null,
+ typeof w,
+ "port" in w,
+ w._url,
+ "fun" in obj,
+ Object.keys(obj.dom).length,
+ Array.isArray(array),
+ JSON.stringify(array)
+ ]);
+ });
+ }
+ });
+
+ // Validate worker.port
+ let array = [1, 2, 3];
+ worker.port.on("content-to-addon", function (result) {
+ assert.ok(result[0], "functions become null");
+ assert.equal(result[1], "object", "objects stay objects");
+ assert.ok(result[2], "object's attributes are enumerable");
+ assert.equal(result[3], DEFAULT_CONTENT_URL,
+ "json attribute is accessible");
+ assert.ok(!result[4], "function as object attribute is removed");
+ assert.equal(result[5], 0, "DOM nodes are converted into empty object");
+ // See bug 714891, Arrays may be broken over compartments:
+ assert.ok(result[6], "Array keeps being an array");
+ assert.equal(result[7], JSON.stringify(array),
+ "Array is correctly serialized");
+ done();
+ });
+
+ let obj = {
+ fun: function () {},
+ dom: browser.contentWindow.document.createElement("div")
+ };
+ // Add a new url property sa the Class function used by
+ // Worker doesn't set enumerables to true for non-functions
+ worker._url = DEFAULT_CONTENT_URL;
+ worker.port.emit("addon-to-content", function () {}, worker, obj, array);
+ }
+);
+
+exports["test:content is wrapped"] = WorkerTest(
+ "data:text/html;charset=utf-8,<script>var documentValue=true;</script>",
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ self.postMessage(!window.documentValue);
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(msg) {
+ assert.ok(msg,
+ "content script has a wrapped access to content document");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:chrome is unwrapped"] = function(assert, done) {
+ let window = makeWindow();
+
+ listenOnce(window, "load", function onload() {
+
+ let worker = Worker({
+ window: window,
+ contentScript: "new " + function WorkerScope() {
+ self.postMessage(window.documentValue);
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(msg) {
+ assert.ok(msg,
+ "content script has an unwrapped access to chrome document");
+ close(window).then(done);
+ }
+ });
+
+ });
+}
+
+exports["test:nothing is leaked to content script"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ self.postMessage([
+ "ContentWorker" in window,
+ "UNWRAP_ACCESS_KEY" in window,
+ "getProxyForObject" in window
+ ]);
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(list) {
+ assert.ok(!list[0], "worker API constructor isn't leaked");
+ assert.ok(!list[1], "Proxy API stuff isn't leaked 1/2");
+ assert.ok(!list[2], "Proxy API stuff isn't leaked 2/2");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:ensure console.xxx works in cs"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let { loader } = LoaderWithHookedConsole(module, onMessage);
+
+ // Intercept all console method calls
+ let calls = [];
+ function onMessage(type, msg) {
+ assert.equal(type, msg,
+ "console.xxx(\"xxx\"), i.e. message is equal to the " +
+ "console method name we are calling");
+ calls.push(msg);
+ }
+
+ // Finally, create a worker that will call all console methods
+ let worker = loader.require("sdk/deprecated/sync-worker").Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ console.time("time");
+ console.log("log");
+ console.info("info");
+ console.warn("warn");
+ console.error("error");
+ console.debug("debug");
+ console.exception("exception");
+ console.timeEnd("timeEnd");
+ self.postMessage();
+ },
+ onMessage: function() {
+ // Ensure that console methods are called in the same execution order
+ const EXPECTED_CALLS = ["time", "log", "info", "warn", "error",
+ "debug", "exception", "timeEnd"];
+ assert.equal(JSON.stringify(calls),
+ JSON.stringify(EXPECTED_CALLS),
+ "console methods have been called successfully, in expected order");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:setTimeout works with string argument"] = WorkerTest(
+ "data:text/html;charset=utf-8,<script>var docVal=5;</script>",
+ function(assert, browser, done) {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function ContentScriptScope() {
+ // must use "window.scVal" instead of "var csVal"
+ // since we are inside ContentScriptScope function.
+ // i'm NOT putting code-in-string inside code-in-string </YO DAWG>
+ window.csVal = 13;
+ setTimeout("self.postMessage([" +
+ "csVal, " +
+ "window.docVal, " +
+ "'ContentWorker' in window, " +
+ "'UNWRAP_ACCESS_KEY' in window, " +
+ "'getProxyForObject' in window, " +
+ "])", 1);
+ },
+ contentScriptWhen: "ready",
+ onMessage: function([csVal, docVal, chrome1, chrome2, chrome3]) {
+ // test timer code is executed in the correct context
+ assert.equal(csVal, 13, "accessing content-script values");
+ assert.notEqual(docVal, 5, "can't access document values (directly)");
+ assert.ok(!chrome1 && !chrome2 && !chrome3, "nothing is leaked from chrome");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:setInterval works with string argument"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let count = 0;
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "setInterval('self.postMessage(1)', 50)",
+ contentScriptWhen: "ready",
+ onMessage: function(one) {
+ count++;
+ assert.equal(one, 1, "got " + count + " message(s) from setInterval");
+ if (count >= 3) done();
+ }
+ });
+ }
+);
+
+exports["test:setInterval async Errors passed to .onError"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let count = 0;
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "setInterval(() => { throw Error('ubik') }, 50)",
+ contentScriptWhen: "ready",
+ onError: function(err) {
+ count++;
+ assert.equal(err.message, "ubik",
+ "error (correctly) propagated " + count + " time(s)");
+ if (count >= 3) done();
+ }
+ });
+ }
+);
+
+exports["test:setTimeout throws array, passed to .onError"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "setTimeout(function() { throw ['array', 42] }, 1)",
+ contentScriptWhen: "ready",
+ onError: function(arr) {
+ assert.ok(isArray(arr),
+ "the type of thrown/propagated object is array");
+ assert.ok(arr.length==2,
+ "the propagated thrown array is the right length");
+ assert.equal(arr[1], 42,
+ "element inside the thrown array correctly propagated");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:setTimeout string arg with SyntaxError to .onError"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "setTimeout('syntax 123 error', 1)",
+ contentScriptWhen: "ready",
+ onError: function(err) {
+ assert.equal(err.name, "SyntaxError",
+ "received SyntaxError thrown from bad code in string argument to setTimeout");
+ assert.ok('fileName' in err,
+ "propagated SyntaxError contains a fileName property");
+ assert.ok('stack' in err,
+ "propagated SyntaxError contains a stack property");
+ assert.equal(err.message, "missing ; before statement",
+ "propagated SyntaxError has the correct (helpful) message");
+ assert.equal(err.lineNumber, 1,
+ "propagated SyntaxError was thrown on the right lineNumber");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:setTimeout can't be cancelled by content"] = WorkerTest(
+ "data:text/html;charset=utf-8,<script>var documentValue=true;</script>",
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ let id = setTimeout(function () {
+ self.postMessage("timeout");
+ }, 100);
+ unsafeWindow.eval("clearTimeout("+id+");");
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(msg) {
+ assert.ok(msg,
+ "content didn't managed to cancel our setTimeout");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:clearTimeout"] = WorkerTest(
+ "data:text/html;charset=utf-8,clear timeout",
+ function(assert, browser, done) {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ let id1 = setTimeout(function() {
+ self.postMessage("failed");
+ }, 10);
+ let id2 = setTimeout(function() {
+ self.postMessage("done");
+ }, 100);
+ clearTimeout(id1);
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(msg) {
+ if (msg === "failed") {
+ assert.fail("failed to cancel timer");
+ } else {
+ assert.pass("timer cancelled");
+ done();
+ }
+ }
+ });
+ }
+);
+
+exports["test:clearInterval"] = WorkerTest(
+ "data:text/html;charset=utf-8,clear timeout",
+ function(assert, browser, done) {
+ let called = 0;
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ let id = setInterval(function() {
+ self.postMessage("intreval")
+ clearInterval(id)
+ setTimeout(function() {
+ self.postMessage("done")
+ }, 100)
+ }, 10);
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(msg) {
+ if (msg === "intreval") {
+ called = called + 1;
+ if (called > 1) assert.fail("failed to cancel timer");
+ } else {
+ assert.pass("interval cancelled");
+ done();
+ }
+ }
+ });
+ }
+)
+
+exports["test:setTimeout are unregistered on content unload"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let originalWindow = browser.contentWindow;
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ document.title = "ok";
+ let i = 0;
+ setInterval(function () {
+ document.title = i++;
+ }, 10);
+ },
+ contentScriptWhen: "ready"
+ });
+
+ // Change location so that content script is destroyed,
+ // and all setTimeout/setInterval should be unregistered.
+ // Wait some cycles in order to execute some intervals.
+ setTimeout(function () {
+ // Bug 689621: Wait for the new document load so that we are sure that
+ // previous document cancelled its intervals
+ let url2 = "data:text/html;charset=utf-8,<title>final</title>";
+ loadAndWait(browser, url2, function onload() {
+ let titleAfterLoad = originalWindow.document.title;
+ // Wait additional cycles to verify that intervals are really cancelled
+ setTimeout(function () {
+ assert.equal(browser.contentDocument.title, "final",
+ "New document has not been modified");
+ assert.equal(originalWindow.document.title, titleAfterLoad,
+ "Nor previous one");
+
+ done();
+ }, 100);
+ });
+ }, 100);
+ }
+);
+
+exports['test:check window attribute in iframes'] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ // Create a first iframe and wait for its loading
+ let contentWin = browser.contentWindow;
+ let contentDoc = contentWin.document;
+ let iframe = contentDoc.createElement("iframe");
+ contentDoc.body.appendChild(iframe);
+
+ listenOnce(iframe, "load", function onload() {
+
+ // Create a second iframe inside the first one and wait for its loading
+ let iframeDoc = iframe.contentWindow.document;
+ let subIframe = iframeDoc.createElement("iframe");
+ iframeDoc.body.appendChild(subIframe);
+
+ listenOnce(subIframe, "load", function onload() {
+ subIframe.removeEventListener("load", onload, true);
+
+ // And finally create a worker against this second iframe
+ let worker = Worker({
+ window: subIframe.contentWindow,
+ contentScript: 'new ' + function WorkerScope() {
+ self.postMessage([
+ window.top !== window,
+ frameElement,
+ window.parent !== window,
+ top.location.href,
+ parent.location.href,
+ ]);
+ },
+ onMessage: function(msg) {
+ assert.ok(msg[0], "window.top != window");
+ assert.ok(msg[1], "window.frameElement is defined");
+ assert.ok(msg[2], "window.parent != window");
+ assert.equal(msg[3], contentWin.location.href,
+ "top.location refers to the toplevel content doc");
+ assert.equal(msg[4], iframe.contentWindow.location.href,
+ "parent.location refers to the first iframe doc");
+ done();
+ }
+ });
+
+ });
+ subIframe.setAttribute("src", "data:text/html;charset=utf-8,bar");
+
+ });
+ iframe.setAttribute("src", "data:text/html;charset=utf-8,foo");
+ }
+);
+
+exports['test:check window attribute in toplevel documents'] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: 'new ' + function WorkerScope() {
+ self.postMessage([
+ window.top === window,
+ frameElement,
+ window.parent === window
+ ]);
+ },
+ onMessage: function(msg) {
+ assert.ok(msg[0], "window.top == window");
+ assert.ok(!msg[1], "window.frameElement is null");
+ assert.ok(msg[2], "window.parent == window");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:check worker API with page history"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let url2 = "data:text/html;charset=utf-8,bar";
+
+ loadAndWait(browser, url2, function () {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ // Just before the content script is disable, we register a timeout
+ // that will be disable until the page gets visible again
+ self.on("pagehide", function () {
+ setTimeout(function () {
+ self.postMessage("timeout restored");
+ }, 0);
+ });
+ },
+ contentScriptWhen: "start"
+ });
+
+ // postMessage works correctly when the page is visible
+ worker.postMessage("ok");
+
+ // We have to wait before going back into history,
+ // otherwise `goBack` won't do anything.
+ setTimeout(function () {
+ browser.goBack();
+ }, 0);
+
+ // Wait for the document to be hidden
+ browser.addEventListener("pagehide", function onpagehide() {
+ browser.removeEventListener("pagehide", onpagehide, false);
+ // Now any event sent to this worker should throw
+
+ assert.throws(
+ function () { worker.postMessage("data"); },
+ /The page is currently hidden and can no longer be used/,
+ "postMessage should throw when the page is hidden in history"
+ );
+
+ assert.throws(
+ function () { worker.port.emit("event"); },
+ /The page is currently hidden and can no longer be used/,
+ "port.emit should throw when the page is hidden in history"
+ );
+
+ // Display the page with attached content script back in order to resume
+ // its timeout and receive the expected message.
+ // We have to delay this in order to not break the history.
+ // We delay for a non-zero amount of time in order to ensure that we
+ // do not receive the message immediatly, so that the timeout is
+ // actually disabled
+ setTimeout(function () {
+ worker.on("message", function (data) {
+ assert.ok(data, "timeout restored");
+ done();
+ });
+ browser.goForward();
+ }, 500);
+
+ }, false);
+ });
+
+ }
+);
+
+exports['test:conentScriptFile as URL instance'] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let url = new URL(fixtures.url("test-contentScriptFile.js"));
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScriptFile: url,
+ onMessage: function(msg) {
+ assert.equal(msg, "msg from contentScriptFile",
+ "received a wrong message from contentScriptFile");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:worker events"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function (assert, browser, done) {
+ let window = browser.contentWindow;
+ let events = [];
+ let worker = Worker({
+ window: window,
+ contentScript: 'new ' + function WorkerScope() {
+ self.postMessage('start');
+ },
+ onAttach: win => {
+ events.push('attach');
+ assert.pass('attach event called when attached');
+ assert.equal(window, win, 'attach event passes in attached window');
+ },
+ onError: err => {
+ assert.equal(err.message, 'Custom',
+ 'Error passed into error event');
+ worker.detach();
+ },
+ onMessage: msg => {
+ assert.pass('`onMessage` handles postMessage')
+ throw new Error('Custom');
+ },
+ onDetach: _ => {
+ assert.pass('`onDetach` called when worker detached');
+ done();
+ }
+ });
+ // `attach` event is called synchronously during instantiation,
+ // so we can't listen to that, TODO FIX?
+ // worker.on('attach', obj => console.log('attach', obj));
+ }
+);
+
+exports["test:onDetach in contentScript on destroy"] = WorkerTest(
+ "data:text/html;charset=utf-8,foo#detach",
+ function(assert, browser, done) {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: 'new ' + function WorkerScope() {
+ self.port.on('detach', function(reason) {
+ window.location.hash += '!' + reason;
+ })
+ },
+ });
+ browser.contentWindow.addEventListener('hashchange', _ => {
+ assert.equal(browser.contentWindow.location.hash, '#detach!',
+ "location.href is as expected");
+ done();
+ })
+ worker.destroy();
+ }
+);
+
+exports["test:onDetach in contentScript on unload"] = WorkerTest(
+ "data:text/html;charset=utf-8,foo#detach",
+ function(assert, browser, done) {
+ let { loader } = LoaderWithHookedConsole(module);
+ let worker = loader.require("sdk/deprecated/sync-worker").Worker({
+ window: browser.contentWindow,
+ contentScript: 'new ' + function WorkerScope() {
+ self.port.on('detach', function(reason) {
+ window.location.hash += '!' + reason;
+ })
+ },
+ });
+ browser.contentWindow.addEventListener('hashchange', _ => {
+ assert.equal(browser.contentWindow.location.hash, '#detach!shutdown',
+ "location.href is as expected");
+ done();
+ })
+ loader.unload('shutdown');
+ }
+);
+
+exports["test:console method log functions properly"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let logs = [];
+
+ let clean = message =>
+ message.trim().
+ replace(/[\r\n]/g, " ").
+ replace(/ +/g, " ");
+
+ let onMessage = (type, message) => logs.push(clean(message));
+ let { loader } = LoaderWithHookedConsole(module, onMessage);
+
+ let worker = loader.require("sdk/deprecated/sync-worker").Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ console.log(Function);
+ console.log((foo) => foo * foo);
+ console.log(function foo(bar) { return bar + bar });
+
+ self.postMessage();
+ },
+ onMessage: () => {
+ assert.deepEqual(logs, [
+ "function Function() { [native code] }",
+ "(foo) => foo * foo",
+ "function foo(bar) { return bar + bar }"
+ ]);
+
+ done();
+ }
+ });
+ }
+);
+
+exports["test:global postMessage"] = WorkerTest(
+ WINDOW_SCRIPT_URL,
+ function(assert, browser, done) {
+ let contentScript = "window.addEventListener('message', function (e) {" +
+ " if (e.data === 'from -> window')" +
+ " self.port.emit('response', e.data, e.origin);" +
+ "});" +
+ "postMessage('from -> content-script', '*');";
+ let { loader } = LoaderWithHookedConsole(module);
+ let worker = loader.require("sdk/deprecated/sync-worker").Worker({
+ window: browser.contentWindow,
+ contentScriptWhen: "ready",
+ contentScript: contentScript
+ });
+
+ worker.port.on("response", (data, origin) => {
+ assert.equal(data, "from -> window", "Communication from content-script to window completed");
+ done();
+ });
+});
+
+exports["test:destroy unbinds listeners from port"] = WorkerTest(
+ "data:text/html;charset=utf-8,portdestroyer",
+ function(assert, browser, done) {
+ let destroyed = false;
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ self.port.emit("destroy");
+ setInterval(self.port.emit, 10, "ping");
+ },
+ onDestroy: done
+ });
+ worker.port.on("ping", () => {
+ if (destroyed) {
+ assert.fail("Should not call events on port after destroy.");
+ }
+ });
+ worker.port.on("destroy", () => {
+ destroyed = true;
+ worker.destroy();
+ assert.pass("Worker destroyed, waiting for no future listeners handling events.");
+ setTimeout(done, 500);
+ });
+ }
+);
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-content-worker.js b/addon-sdk/source/test/test-content-worker.js
new file mode 100644
index 000000000..16c9436b3
--- /dev/null
+++ b/addon-sdk/source/test/test-content-worker.js
@@ -0,0 +1,1129 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+// Skipping due to window creation being unsupported in Fennec
+module.metadata = {
+ engines: {
+ 'Firefox': '*'
+ }
+};
+
+const { Cc, Ci } = require("chrome");
+const { on } = require("sdk/event/core");
+const { setTimeout } = require("sdk/timers");
+const { LoaderWithHookedConsole, Loader } = require("sdk/test/loader");
+const { Worker } = require("sdk/content/worker");
+const { close } = require("sdk/window/helpers");
+const { set: setPref } = require("sdk/preferences/service");
+const { isArray } = require("sdk/lang/type");
+const { URL } = require('sdk/url');
+const fixtures = require("./fixtures");
+const system = require("sdk/system/events");
+
+const DEPRECATE_PREF = "devtools.errorconsole.deprecation_warnings";
+
+const DEFAULT_CONTENT_URL = "data:text/html;charset=utf-8,foo";
+
+const WINDOW_SCRIPT_URL = "data:text/html;charset=utf-8," +
+ "<script>window.addEventListener('message', function (e) {" +
+ " if (e.data === 'from -> content-script')" +
+ " window.postMessage('from -> window', '*');" +
+ "});</script>";
+
+function makeWindow() {
+ let content =
+ "<?xml version=\"1.0\"?>" +
+ "<window " +
+ "xmlns=\"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul\">" +
+ "<script>var documentValue=true;</script>" +
+ "</window>";
+ var url = "data:application/vnd.mozilla.xul+xml;charset=utf-8," +
+ encodeURIComponent(content);
+ var features = ["chrome", "width=10", "height=10"];
+
+ return Cc["@mozilla.org/embedcomp/window-watcher;1"].
+ getService(Ci.nsIWindowWatcher).
+ openWindow(null, url, null, features.join(","), null);
+}
+
+// Listen for only first one occurence of DOM event
+function listenOnce(node, eventName, callback) {
+ node.addEventListener(eventName, function onevent(event) {
+ node.removeEventListener(eventName, onevent, true);
+ callback(node);
+ }, true);
+}
+
+// Load a given url in a given browser and fires the callback when it is loaded
+function loadAndWait(browser, url, callback) {
+ listenOnce(browser, "load", callback);
+ // We have to wait before calling `loadURI` otherwise, if we call
+ // `loadAndWait` during browser load event, the history will be broken
+ setTimeout(function () {
+ browser.loadURI(url);
+ }, 0);
+}
+
+// Returns a test function that will automatically open a new chrome window
+// with a <browser> element loaded on a given content URL
+// The callback receive 3 arguments:
+// - test: reference to the jetpack test object
+// - browser: a reference to the <browser> xul node
+// - done: a callback to call when test is over
+function WorkerTest(url, callback) {
+ return function testFunction(assert, done) {
+ let chromeWindow = makeWindow();
+ chromeWindow.addEventListener("load", function onload() {
+ chromeWindow.removeEventListener("load", onload, true);
+ let browser = chromeWindow.document.createElement("browser");
+ browser.setAttribute("type", "content");
+ chromeWindow.document.documentElement.appendChild(browser);
+ // Wait for about:blank load event ...
+ listenOnce(browser, "load", function onAboutBlankLoad() {
+ // ... before loading the expected doc and waiting for its load event
+ loadAndWait(browser, url, function onDocumentLoaded() {
+ callback(assert, browser, function onTestDone() {
+
+ close(chromeWindow).then(done);
+ });
+ });
+ });
+ }, true);
+ };
+}
+
+exports["test:sample"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ assert.notEqual(browser.contentWindow.location.href, "about:blank",
+ "window is now on the right document");
+
+ let window = browser.contentWindow
+ let worker = Worker({
+ window: window,
+ contentScript: "new " + function WorkerScope() {
+ // window is accessible
+ let myLocation = window.location.toString();
+ self.on("message", function(data) {
+ if (data == "hi!")
+ self.postMessage("bye!");
+ });
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(msg) {
+ assert.equal("bye!", msg);
+ assert.equal(worker.url, window.location.href,
+ "worker.url still works");
+ done();
+ },
+ onAttach: function() {
+ assert.equal(worker.url, window.location.href,
+ "worker.url works");
+ assert.equal(worker.contentURL, window.location.href,
+ "worker.contentURL works");
+ worker.postMessage("hi!");
+ }
+ });
+ }
+);
+
+exports["test:emit"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ // Validate self.on and self.emit
+ self.port.on("addon-to-content", function (data) {
+ self.port.emit("content-to-addon", data);
+ });
+
+ // Check for global pollution
+ //if (typeof on != "undefined")
+ // self.postMessage("`on` is in globals");
+ if (typeof once != "undefined")
+ self.postMessage("`once` is in globals");
+ if (typeof emit != "undefined")
+ self.postMessage("`emit` is in globals");
+
+ },
+ onMessage: function(msg) {
+ assert.fail("Got an unexpected message : "+msg);
+ }
+ });
+
+ // Validate worker.port
+ worker.port.on("content-to-addon", function (data) {
+ assert.equal(data, "event data");
+ done();
+ });
+ worker.port.emit("addon-to-content", "event data");
+ }
+);
+
+exports["test:emit hack message"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ // Validate self.port
+ self.port.on("message", function (data) {
+ self.port.emit("message", data);
+ });
+ // We should not receive message on self, but only on self.port
+ self.on("message", function (data) {
+ self.postMessage("message", data);
+ });
+ },
+ onError: function(e) {
+ assert.fail("Got exception: "+e);
+ }
+ });
+
+ worker.port.on("message", function (data) {
+ assert.equal(data, "event data");
+ done();
+ });
+ worker.on("message", function (data) {
+ assert.fail("Got an unexpected message : "+msg);
+ });
+ worker.port.emit("message", "event data");
+ }
+);
+
+exports["test:n-arguments emit"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let repeat = 0;
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ // Validate self.on and self.emit
+ self.port.on("addon-to-content", function (a1, a2, a3) {
+ self.port.emit("content-to-addon", a1, a2, a3);
+ });
+ }
+ });
+
+ // Validate worker.port
+ worker.port.on("content-to-addon", function (arg1, arg2, arg3) {
+ if (!repeat++) {
+ this.emit("addon-to-content", "first argument", "second", "third");
+ } else {
+ assert.equal(arg1, "first argument");
+ assert.equal(arg2, "second");
+ assert.equal(arg3, "third");
+ done();
+ }
+ });
+ worker.port.emit("addon-to-content", "first argument", "second", "third");
+ }
+);
+
+exports["test:post-json-values-only"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ self.on("message", function (message) {
+ self.postMessage([ message.fun === undefined,
+ typeof message.w,
+ message.w && "port" in message.w,
+ message.w._url,
+ Array.isArray(message.array),
+ JSON.stringify(message.array)]);
+ });
+ }
+ });
+
+ // Validate worker.onMessage
+ let array = [1, 2, 3];
+ worker.on("message", function (message) {
+ assert.ok(message[0], "function becomes undefined");
+ assert.equal(message[1], "object", "object stays object");
+ assert.ok(message[2], "object's attributes are enumerable");
+ assert.equal(message[3], DEFAULT_CONTENT_URL,
+ "jsonable attributes are accessible");
+ // See bug 714891, Arrays may be broken over compartements:
+ assert.ok(message[4], "Array keeps being an array");
+ assert.equal(message[5], JSON.stringify(array),
+ "Array is correctly serialized");
+ done();
+ });
+ // Add a new url property sa the Class function used by
+ // Worker doesn't set enumerables to true for non-functions
+ worker._url = DEFAULT_CONTENT_URL;
+
+ worker.postMessage({ fun: function () {}, w: worker, array: array });
+ }
+);
+
+exports["test:emit-json-values-only"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ // Validate self.on and self.emit
+ self.port.on("addon-to-content", function (fun, w, obj, array) {
+ self.port.emit("content-to-addon", [
+ fun === null,
+ typeof w,
+ "port" in w,
+ w._url,
+ "fun" in obj,
+ Object.keys(obj.dom).length,
+ Array.isArray(array),
+ JSON.stringify(array)
+ ]);
+ });
+ }
+ });
+
+ // Validate worker.port
+ let array = [1, 2, 3];
+ worker.port.on("content-to-addon", function (result) {
+ assert.ok(result[0], "functions become null");
+ assert.equal(result[1], "object", "objects stay objects");
+ assert.ok(result[2], "object's attributes are enumerable");
+ assert.equal(result[3], DEFAULT_CONTENT_URL,
+ "json attribute is accessible");
+ assert.ok(!result[4], "function as object attribute is removed");
+ assert.equal(result[5], 0, "DOM nodes are converted into empty object");
+ // See bug 714891, Arrays may be broken over compartments:
+ assert.ok(result[6], "Array keeps being an array");
+ assert.equal(result[7], JSON.stringify(array),
+ "Array is correctly serialized");
+ done();
+ });
+
+ let obj = {
+ fun: function () {},
+ dom: browser.contentWindow.document.createElement("div")
+ };
+ // Add a new url property sa the Class function used by
+ // Worker doesn't set enumerables to true for non-functions
+ worker._url = DEFAULT_CONTENT_URL;
+ worker.port.emit("addon-to-content", function () {}, worker, obj, array);
+ }
+);
+
+exports["test:content is wrapped"] = WorkerTest(
+ "data:text/html;charset=utf-8,<script>var documentValue=true;</script>",
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ self.postMessage(!window.documentValue);
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(msg) {
+ assert.ok(msg,
+ "content script has a wrapped access to content document");
+ done();
+ }
+ });
+ }
+);
+
+// ContentWorker is not for chrome
+/*
+exports["test:chrome is unwrapped"] = function(assert, done) {
+ let window = makeWindow();
+
+ listenOnce(window, "load", function onload() {
+
+ let worker = Worker({
+ window: window,
+ contentScript: "new " + function WorkerScope() {
+ self.postMessage(window.documentValue);
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(msg) {
+ assert.ok(msg,
+ "content script has an unwrapped access to chrome document");
+ close(window).then(done);
+ }
+ });
+
+ });
+}
+*/
+
+exports["test:nothing is leaked to content script"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ self.postMessage([
+ "ContentWorker" in window,
+ "UNWRAP_ACCESS_KEY" in window,
+ "getProxyForObject" in window
+ ]);
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(list) {
+ assert.ok(!list[0], "worker API contrustor isn't leaked");
+ assert.ok(!list[1], "Proxy API stuff isn't leaked 1/2");
+ assert.ok(!list[2], "Proxy API stuff isn't leaked 2/2");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:ensure console.xxx works in cs"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ const EXPECTED = ["time", "log", "info", "warn", "error", "error", "timeEnd"];
+
+ let calls = [];
+ let levels = [];
+
+ system.on('console-api-log-event', onMessage);
+
+ function onMessage({ subject }) {
+ calls.push(subject.wrappedJSObject.arguments[0]);
+ levels.push(subject.wrappedJSObject.level);
+ }
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ console.time("time");
+ console.log("log");
+ console.info("info");
+ console.warn("warn");
+ console.error("error");
+ console.debug("debug");
+ console.exception("error");
+ console.timeEnd("timeEnd");
+ self.postMessage();
+ },
+ onMessage: function() {
+ system.off('console-api-log-event', onMessage);
+
+ assert.equal(JSON.stringify(calls),
+ JSON.stringify(EXPECTED),
+ "console methods have been called successfully, in expected order");
+
+ assert.equal(JSON.stringify(levels),
+ JSON.stringify(EXPECTED),
+ "console messages have correct log levels, in expected order");
+
+ done();
+ }
+ });
+ }
+);
+
+exports["test:setTimeout works with string argument"] = WorkerTest(
+ "data:text/html;charset=utf-8,<script>var docVal=5;</script>",
+ function(assert, browser, done) {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function ContentScriptScope() {
+ // must use "window.scVal" instead of "var csVal"
+ // since we are inside ContentScriptScope function.
+ // i'm NOT putting code-in-string inside code-in-string </YO DAWG>
+ window.csVal = 13;
+ setTimeout("self.postMessage([" +
+ "csVal, " +
+ "window.docVal, " +
+ "'ContentWorker' in window, " +
+ "'UNWRAP_ACCESS_KEY' in window, " +
+ "'getProxyForObject' in window, " +
+ "])", 1);
+ },
+ contentScriptWhen: "ready",
+ onMessage: function([csVal, docVal, chrome1, chrome2, chrome3]) {
+ // test timer code is executed in the correct context
+ assert.equal(csVal, 13, "accessing content-script values");
+ assert.notEqual(docVal, 5, "can't access document values (directly)");
+ assert.ok(!chrome1 && !chrome2 && !chrome3, "nothing is leaked from chrome");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:setInterval works with string argument"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let count = 0;
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "setInterval('self.postMessage(1)', 50)",
+ contentScriptWhen: "ready",
+ onMessage: function(one) {
+ count++;
+ assert.equal(one, 1, "got " + count + " message(s) from setInterval");
+ if (count >= 3) done();
+ }
+ });
+ }
+);
+
+exports["test:setInterval async Errors passed to .onError"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let count = 0;
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "setInterval(() => { throw Error('ubik') }, 50)",
+ contentScriptWhen: "ready",
+ onError: function(err) {
+ count++;
+ assert.equal(err.message, "ubik",
+ "error (correctly) propagated " + count + " time(s)");
+ if (count >= 3) done();
+ }
+ });
+ }
+);
+
+exports["test:setTimeout throws array, passed to .onError"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "setTimeout(function() { throw ['array', 42] }, 1)",
+ contentScriptWhen: "ready",
+ onError: function(arr) {
+ assert.ok(isArray(arr),
+ "the type of thrown/propagated object is array");
+ assert.ok(arr.length==2,
+ "the propagated thrown array is the right length");
+ assert.equal(arr[1], 42,
+ "element inside the thrown array correctly propagated");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:setTimeout string arg with SyntaxError to .onError"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "setTimeout('syntax 123 error', 1)",
+ contentScriptWhen: "ready",
+ onError: function(err) {
+ assert.equal(err.name, "SyntaxError",
+ "received SyntaxError thrown from bad code in string argument to setTimeout");
+ assert.ok('fileName' in err,
+ "propagated SyntaxError contains a fileName property");
+ assert.ok('stack' in err,
+ "propagated SyntaxError contains a stack property");
+ assert.equal(err.message, "missing ; before statement",
+ "propagated SyntaxError has the correct (helpful) message");
+ assert.equal(err.lineNumber, 1,
+ "propagated SyntaxError was thrown on the right lineNumber");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:setTimeout can't be cancelled by content"] = WorkerTest(
+ "data:text/html;charset=utf-8,<script>var documentValue=true;</script>",
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ let id = setTimeout(function () {
+ self.postMessage("timeout");
+ }, 100);
+ unsafeWindow.eval("clearTimeout("+id+");");
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(msg) {
+ assert.ok(msg,
+ "content didn't managed to cancel our setTimeout");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:clearTimeout"] = WorkerTest(
+ "data:text/html;charset=utf-8,clear timeout",
+ function(assert, browser, done) {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ let id1 = setTimeout(function() {
+ self.postMessage("failed");
+ }, 10);
+ let id2 = setTimeout(function() {
+ self.postMessage("done");
+ }, 100);
+ clearTimeout(id1);
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(msg) {
+ if (msg === "failed") {
+ assert.fail("failed to cancel timer");
+ } else {
+ assert.pass("timer cancelled");
+ done();
+ }
+ }
+ });
+ }
+);
+
+exports["test:clearInterval"] = WorkerTest(
+ "data:text/html;charset=utf-8,clear timeout",
+ function(assert, browser, done) {
+ let called = 0;
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ let id = setInterval(function() {
+ self.postMessage("intreval")
+ clearInterval(id)
+ setTimeout(function() {
+ self.postMessage("done")
+ }, 100)
+ }, 10);
+ },
+ contentScriptWhen: "ready",
+ onMessage: function(msg) {
+ if (msg === "intreval") {
+ called = called + 1;
+ if (called > 1) assert.fail("failed to cancel timer");
+ } else {
+ assert.pass("interval cancelled");
+ done();
+ }
+ }
+ });
+ }
+)
+
+exports["test:setTimeout are unregistered on content unload"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let originalWindow = browser.contentWindow;
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ document.title = "ok";
+ let i = 0;
+ setInterval(function () {
+ document.title = i++;
+ }, 10);
+ },
+ contentScriptWhen: "ready"
+ });
+
+ // Change location so that content script is destroyed,
+ // and all setTimeout/setInterval should be unregistered.
+ // Wait some cycles in order to execute some intervals.
+ setTimeout(function () {
+ // Bug 689621: Wait for the new document load so that we are sure that
+ // previous document cancelled its intervals
+ let url2 = "data:text/html;charset=utf-8,<title>final</title>";
+ loadAndWait(browser, url2, function onload() {
+ let titleAfterLoad = originalWindow.document.title;
+ // Wait additional cycles to verify that intervals are really cancelled
+ setTimeout(function () {
+ assert.equal(browser.contentDocument.title, "final",
+ "New document has not been modified");
+ assert.equal(originalWindow.document.title, titleAfterLoad,
+ "Nor previous one");
+
+ done();
+ }, 100);
+ });
+ }, 100);
+ }
+);
+
+exports['test:check window attribute in iframes'] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ // Create a first iframe and wait for its loading
+ let contentWin = browser.contentWindow;
+ let contentDoc = contentWin.document;
+ let iframe = contentDoc.createElement("iframe");
+ contentDoc.body.appendChild(iframe);
+
+ listenOnce(iframe, "load", function onload() {
+
+ // Create a second iframe inside the first one and wait for its loading
+ let iframeDoc = iframe.contentWindow.document;
+ let subIframe = iframeDoc.createElement("iframe");
+ iframeDoc.body.appendChild(subIframe);
+
+ listenOnce(subIframe, "load", function onload() {
+ subIframe.removeEventListener("load", onload, true);
+
+ // And finally create a worker against this second iframe
+ let worker = Worker({
+ window: subIframe.contentWindow,
+ contentScript: 'new ' + function WorkerScope() {
+ self.postMessage([
+ window.top !== window,
+ frameElement,
+ window.parent !== window,
+ top.location.href,
+ parent.location.href,
+ ]);
+ },
+ onMessage: function(msg) {
+ assert.ok(msg[0], "window.top != window");
+ assert.ok(msg[1], "window.frameElement is defined");
+ assert.ok(msg[2], "window.parent != window");
+ assert.equal(msg[3], contentWin.location.href,
+ "top.location refers to the toplevel content doc");
+ assert.equal(msg[4], iframe.contentWindow.location.href,
+ "parent.location refers to the first iframe doc");
+ done();
+ }
+ });
+
+ });
+ subIframe.setAttribute("src", "data:text/html;charset=utf-8,bar");
+
+ });
+ iframe.setAttribute("src", "data:text/html;charset=utf-8,foo");
+ }
+);
+
+exports['test:check window attribute in toplevel documents'] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: 'new ' + function WorkerScope() {
+ self.postMessage([
+ window.top === window,
+ frameElement,
+ window.parent === window
+ ]);
+ },
+ onMessage: function(msg) {
+ assert.ok(msg[0], "window.top == window");
+ assert.ok(!msg[1], "window.frameElement is null");
+ assert.ok(msg[2], "window.parent == window");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:check worker API with page history"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let url2 = "data:text/html;charset=utf-8,bar";
+
+ loadAndWait(browser, url2, function () {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ // Just before the content script is disable, we register a timeout
+ // that will be disable until the page gets visible again
+ self.on("pagehide", function () {
+ setTimeout(function () {
+ self.port.emit("timeout");
+ }, 0);
+ });
+
+ self.on("message", function() {
+ self.postMessage("saw message");
+ });
+
+ self.on("event", function() {
+ self.port.emit("event", "saw event");
+ });
+ },
+ contentScriptWhen: "start"
+ });
+
+ // postMessage works correctly when the page is visible
+ worker.postMessage("ok");
+
+ // We have to wait before going back into history,
+ // otherwise `goBack` won't do anything.
+ setTimeout(function () {
+ browser.goBack();
+ }, 0);
+
+ // Wait for the document to be hidden
+ browser.addEventListener("pagehide", function onpagehide() {
+ browser.removeEventListener("pagehide", onpagehide, false);
+ // Now any event sent to this worker should be cached
+
+ worker.postMessage("message");
+ worker.port.emit("event");
+
+ // Display the page with attached content script back in order to resume
+ // its timeout and receive the expected message.
+ // We have to delay this in order to not break the history.
+ // We delay for a non-zero amount of time in order to ensure that we
+ // do not receive the message immediatly, so that the timeout is
+ // actually disabled
+ setTimeout(function () {
+ worker.on("pageshow", function() {
+ let promise = Promise.all([
+ new Promise(resolve => {
+ worker.port.on("event", () => {
+ assert.pass("Saw event");
+ resolve();
+ });
+ }),
+ new Promise(resolve => {
+ worker.on("message", () => {
+ assert.pass("Saw message");
+ resolve();
+ });
+ }),
+ new Promise(resolve => {
+ worker.port.on("timeout", () => {
+ assert.pass("Timer fired");
+ resolve();
+ });
+ })
+ ]);
+ promise.then(done);
+ });
+
+ browser.goForward();
+ }, 500);
+
+ }, false);
+ });
+
+ }
+);
+
+exports['test:conentScriptFile as URL instance'] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+
+ let url = new URL(fixtures.url("test-contentScriptFile.js"));
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScriptFile: url,
+ onMessage: function(msg) {
+ assert.equal(msg, "msg from contentScriptFile",
+ "received a wrong message from contentScriptFile");
+ done();
+ }
+ });
+ }
+);
+
+exports["test:worker events"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function (assert, browser, done) {
+ let window = browser.contentWindow;
+ let events = [];
+ let worker = Worker({
+ window: window,
+ contentScript: 'new ' + function WorkerScope() {
+ self.postMessage('start');
+ },
+ onAttach: () => {
+ events.push('attach');
+ assert.pass('attach event called when attached');
+ },
+ onError: err => {
+ assert.equal(err.message, 'Custom',
+ 'Error passed into error event');
+ worker.detach();
+ },
+ onMessage: msg => {
+ assert.pass('`onMessage` handles postMessage')
+ throw new Error('Custom');
+ },
+ onDetach: _ => {
+ assert.pass('`onDetach` called when worker detached');
+ done();
+ }
+ });
+ // `attach` event is called synchronously during instantiation,
+ // so we can't listen to that, TODO FIX?
+ // worker.on('attach', obj => console.log('attach', obj));
+ }
+);
+
+exports["test:onDetach in contentScript on destroy"] = WorkerTest(
+ "data:text/html;charset=utf-8,foo#detach",
+ function(assert, browser, done) {
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: 'new ' + function WorkerScope() {
+ self.port.on('detach', function(reason) {
+ window.location.hash += '!' + reason;
+ })
+ },
+
+ onAttach: function() {
+ browser.contentWindow.addEventListener('hashchange', _ => {
+ assert.equal(browser.contentWindow.location.hash, '#detach!',
+ "location.href is as expected");
+ done();
+ })
+ worker.destroy();
+ }
+ });
+ }
+);
+
+exports["test:onDetach in contentScript on unload"] = WorkerTest(
+ "data:text/html;charset=utf-8,foo#detach",
+ function(assert, browser, done) {
+ let { loader } = LoaderWithHookedConsole(module);
+ let worker = loader.require("sdk/content/worker").Worker({
+ window: browser.contentWindow,
+ contentScript: 'new ' + function WorkerScope() {
+ self.port.on('detach', function(reason) {
+ window.location.hash += '!' + reason;
+ })
+ },
+ });
+ browser.contentWindow.addEventListener('hashchange', _ => {
+ assert.equal(browser.contentWindow.location.hash, '#detach!shutdown',
+ "location.href is as expected");
+ done();
+ })
+ loader.unload('shutdown');
+ }
+);
+
+exports["test:console method log functions properly"] = WorkerTest(
+ DEFAULT_CONTENT_URL,
+ function(assert, browser, done) {
+ let logs = [];
+
+ system.on('console-api-log-event', onMessage);
+
+ function onMessage({ subject }) {
+ logs.push(clean(subject.wrappedJSObject.arguments[0]));
+ }
+
+ let clean = message =>
+ message.trim().
+ replace(/[\r\n]/g, " ").
+ replace(/ +/g, " ");
+
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ console.log(Function);
+ console.log((foo) => foo * foo);
+ console.log(function foo(bar) { return bar + bar });
+
+ self.postMessage();
+ },
+ onMessage: () => {
+ system.off('console-api-log-event', onMessage);
+
+ assert.deepEqual(logs, [
+ "function Function() { [native code] }",
+ "(foo) => foo * foo",
+ "function foo(bar) { return bar + bar }"
+ ]);
+
+ done();
+ }
+ });
+ }
+);
+
+exports["test:global postMessage"] = WorkerTest(
+ WINDOW_SCRIPT_URL,
+ function(assert, browser, done) {
+ let contentScript = "window.addEventListener('message', function (e) {" +
+ " if (e.data === 'from -> window')" +
+ " self.port.emit('response', e.data, e.origin);" +
+ "});" +
+ "postMessage('from -> content-script', '*');";
+ let { loader } = LoaderWithHookedConsole(module);
+ let worker = loader.require("sdk/content/worker").Worker({
+ window: browser.contentWindow,
+ contentScriptWhen: "ready",
+ contentScript: contentScript
+ });
+
+ worker.port.on("response", (data, origin) => {
+ assert.equal(data, "from -> window", "Communication from content-script to window completed");
+ done();
+ });
+});
+
+exports["test:destroy unbinds listeners from port"] = WorkerTest(
+ "data:text/html;charset=utf-8,portdestroyer",
+ function(assert, browser, done) {
+ let destroyed = false;
+ let worker = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ self.port.emit("destroy");
+ setInterval(self.port.emit, 10, "ping");
+ },
+ onDestroy: done
+ });
+ worker.port.on("ping", () => {
+ if (destroyed) {
+ assert.fail("Should not call events on port after destroy.");
+ }
+ });
+ worker.port.on("destroy", () => {
+ destroyed = true;
+ worker.destroy();
+ assert.pass("Worker destroyed, waiting for no future listeners handling events.");
+ setTimeout(done, 500);
+ });
+ }
+);
+
+exports["test:destroy kills child worker"] = WorkerTest(
+ "data:text/html;charset=utf-8,<html><body><p id='detail'></p></body></html>",
+ function(assert, browser, done) {
+ let worker1 = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ self.port.on("ping", detail => {
+ let event = document.createEvent("CustomEvent");
+ event.initCustomEvent("Test:Ping", true, true, detail);
+ document.dispatchEvent(event);
+ self.port.emit("pingsent");
+ });
+
+ let listener = function(event) {
+ self.port.emit("pong", event.detail);
+ };
+
+ self.port.on("detach", () => {
+ window.removeEventListener("Test:Pong", listener);
+ });
+ window.addEventListener("Test:Pong", listener);
+ },
+ onAttach: function() {
+ let worker2 = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ let listener = function(event) {
+ let newEvent = document.createEvent("CustomEvent");
+ newEvent.initCustomEvent("Test:Pong", true, true, event.detail);
+ document.dispatchEvent(newEvent);
+ };
+ self.port.on("detach", () => {
+ window.removeEventListener("Test:Ping", listener);
+ })
+ window.addEventListener("Test:Ping", listener);
+ self.postMessage();
+ },
+ onMessage: function() {
+ worker1.port.emit("ping", "test1");
+ worker1.port.once("pong", detail => {
+ assert.equal(detail, "test1", "Saw the right message");
+ worker1.port.once("pingsent", () => {
+ assert.pass("The message was sent");
+
+ worker2.destroy();
+
+ worker1.port.emit("ping", "test2");
+ worker1.port.once("pong", detail => {
+ assert.fail("worker2 shouldn't have responded");
+ })
+ worker1.port.once("pingsent", () => {
+ assert.pass("The message was sent");
+ worker1.destroy();
+ done();
+ });
+ });
+ })
+ }
+ });
+ }
+ });
+ }
+);
+
+exports["test:unload kills child worker"] = WorkerTest(
+ "data:text/html;charset=utf-8,<html><body><p id='detail'></p></body></html>",
+ function(assert, browser, done) {
+ let loader = Loader(module);
+ let worker1 = Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ self.port.on("ping", detail => {
+ let event = document.createEvent("CustomEvent");
+ event.initCustomEvent("Test:Ping", true, true, detail);
+ document.dispatchEvent(event);
+ self.port.emit("pingsent");
+ });
+
+ let listener = function(event) {
+ self.port.emit("pong", event.detail);
+ };
+
+ self.port.on("detach", () => {
+ window.removeEventListener("Test:Pong", listener);
+ });
+ window.addEventListener("Test:Pong", listener);
+ },
+ onAttach: function() {
+ let worker2 = loader.require("sdk/content/worker").Worker({
+ window: browser.contentWindow,
+ contentScript: "new " + function WorkerScope() {
+ let listener = function(event) {
+ let newEvent = document.createEvent("CustomEvent");
+ newEvent.initCustomEvent("Test:Pong", true, true, event.detail);
+ document.dispatchEvent(newEvent);
+ };
+ self.port.on("detach", () => {
+ window.removeEventListener("Test:Ping", listener);
+ })
+ window.addEventListener("Test:Ping", listener);
+ self.postMessage();
+ },
+ onMessage: function() {
+ worker1.port.emit("ping", "test1");
+ worker1.port.once("pong", detail => {
+ assert.equal(detail, "test1", "Saw the right message");
+ worker1.port.once("pingsent", () => {
+ assert.pass("The message was sent");
+
+ loader.unload();
+
+ worker1.port.emit("ping", "test2");
+ worker1.port.once("pong", detail => {
+ assert.fail("worker2 shouldn't have responded");
+ })
+ worker1.port.once("pingsent", () => {
+ assert.pass("The message was sent");
+ worker1.destroy();
+ done();
+ });
+ });
+ })
+ }
+ });
+ }
+ });
+ }
+);
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-context-menu.html b/addon-sdk/source/test/test-context-menu.html
new file mode 100644
index 000000000..63a365ae7
--- /dev/null
+++ b/addon-sdk/source/test/test-context-menu.html
@@ -0,0 +1,98 @@
+<!-- This Source Code Form is subject to the terms of the Mozilla Public
+ - License, v. 2.0. If a copy of the MPL was not distributed with this
+ - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
+
+<html>
+ <head>
+ <meta charset="UTF-8">
+ <title>Context menu test</title>
+ <style>
+ p { display: inline-block; }
+ </style>
+ </head>
+ <body>
+ <p>
+ <img id="image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHWSURBVHjaYvz//z8DJQAggJiQOe/fv2fv7Oz8rays/N+VkfG/iYnJfyD/1+rVq7ffu3dPFpsBAAHEAHIBCJ85c8bN2Nj4vwsDw/8zQLwKiO8CcRoQu0DxqlWrdsHUwzBAAIGJmTNnPgYa9j8UqhFElwPxf2MIDeIrKSn9FwSJoRkAEEAM0DD4DzMAyPi/G+QKY4hh5WAXGf8PDQ0FGwJ22d27CjADAAIIrLmjo+MXA9R2kAHvGBA2wwx6B8W7od6CeQcggKCmCEL8bgwxYCbUIGTDVkHDBia+CuotgACCueD3TDQN75D4xmAvCoK9ARMHBzAw0AECiBHkAlC0Mdy7x9ABNA3obAZXIAa6iKEcGlMVQHwWyjYuL2d4v2cPg8vZswx7gHyAAAK7AOif7SAbOqCmn4Ha3AHFsIDtgPq/vLz8P4MSkJ2W9h8ggBjevXvHDo4FQUQg/kdypqCg4H8lUIACnQ/SOBMYI8bAsAJFPcj1AAEEjwVQqLpAbXmH5BJjqI0gi9DTAAgDBBCcAVLkgmQ7yKCZxpCQxqUZhAECCJ4XgMl493ug21ZD+aDAXH0WLM4A9MZPXJkJIIAwTAR5pQMalaCABQUULttBGCCAGCnNzgABBgAMJ5THwGvJLAAAAABJRU5ErkJggg==">
+ </p>
+
+ <p>
+ <a id="link" href="">
+ A simple link.
+ </a>
+ </p>
+
+ <p>
+ <a href="">
+ <span id="span-link">
+ A span inside a link.
+ </span>
+ </a>
+ </p>
+
+ <p id="text">
+ Some text.
+ </p>
+
+ <p>
+ <textarea id="textfield">
+ A text field,
+ with some text.
+ </textarea>
+ </p>
+
+ <p>
+ <iframe id="iframe" src="data:text/html;charset=utf-8,<p id='text'>An iframe</p>."
+ width="200" height="100">
+ </iframe>
+ </p>
+
+ <p>
+ <a id="targetlink" target="_blank" href="">
+ A targetted link.
+ </a>
+ </p>
+
+ <p>
+ <input type="submit" id="button">
+ </p>
+
+ <p>
+ <a class="predicate-test-a" href="#test">
+ A link with no ID and an anchor, used by PredicateContext tests.
+ </a>
+ </p>
+
+ <p>
+ <a class="predicate-test-b" href="#nested-image">
+ <img id="predicate-test-nested-image" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHWSURBVHjaYvz//z8DJQAggJiQOe/fv2fv7Oz8rays/N+VkfG/iYnJfyD/1+rVq7ffu3dPFpsBAAHEAHIBCJ85c8bN2Nj4vwsDw/8zQLwKiO8CcRoQu0DxqlWrdsHUwzBAAIGJmTNnPgYa9j8UqhFElwPxf2MIDeIrKSn9FwSJoRkAEEAM0DD4DzMAyPi/G+QKY4hh5WAXGf8PDQ0FGwJ22d27CjADAAIIrLmjo+MXA9R2kAHvGBA2wwx6B8W7od6CeQcggKCmCEL8bgwxYCbUIGTDVkHDBia+CuotgACCueD3TDQN75D4xmAvCoK9ARMHBzAw0AECiBHkAlC0Mdy7x9ABNA3obAZXIAa6iKEcGlMVQHwWyjYuL2d4v2cPg8vZswx7gHyAAAK7AOif7SAbOqCmn4Ha3AHFsIDtgPq/vLz8P4MSkJ2W9h8ggBjevXvHDo4FQUQg/kdypqCg4H8lUIACnQ/SOBMYI8bAsAJFPcj1AAEEjwVQqLpAbXmH5BJjqI0gi9DTAAgDBBCcAVLkgmQ7yKCZxpCQxqUZhAECCJ4XgMl493ug21ZD+aDAXH0WLM4A9MZPXJkJIIAwTAR5pQMalaCABQUULttBGCCAGCnNzgABBgAMJ5THwGvJLAAAAABJRU5ErkJggg==">
+ </a>
+ </p>
+ <p>
+ <a class="predicate-test-c" href="#nested-structure">
+ <span>
+ <span>
+ <span id="predicate-test-nested-structure">
+ A complex nested structure.
+ </span>
+ </span>
+ </span>
+ </a>
+ </p>
+
+ <p>
+ <input type="text" id="textbox" value="test value">
+ </p>
+
+ <p>
+ <input type="text" id="readonly-textbox" readonly="true" value="readonly value">
+ </p>
+
+ <p>
+ <input type="text" id="disabled-textbox" disabled="true" value="disabled value">
+ </p>
+
+ <p>
+ <p contenteditable="true" id="editable">This content is editable.</p>
+ </p>
+ </body>
+</html>
diff --git a/addon-sdk/source/test/test-context-menu.js b/addon-sdk/source/test/test-context-menu.js
new file mode 100644
index 000000000..f1a955545
--- /dev/null
+++ b/addon-sdk/source/test/test-context-menu.js
@@ -0,0 +1,3763 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+ 'use strict';
+
+require("sdk/context-menu");
+
+const { defer } = require("sdk/core/promise");
+const { isTravisCI } = require("sdk/test/utils");
+const packaging = require('@loader/options');
+
+// These should match the same constants in the module.
+const OVERFLOW_THRESH_DEFAULT = 10;
+const OVERFLOW_THRESH_PREF =
+ "extensions.addon-sdk.context-menu.overflowThreshold";
+
+const TEST_DOC_URL = module.uri.replace(/\.js$/, ".html");
+const data = require("./fixtures");
+
+const { TestHelper } = require("./context-menu/test-helper.js")
+
+// Tests that when present the separator is placed before the separator from
+// the old context-menu module
+exports.testSeparatorPosition = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ // Create the old separator
+ let oldSeparator = test.contextMenuPopup.ownerDocument.createElement("menuseparator");
+ oldSeparator.id = "jetpack-context-menu-separator";
+ test.contextMenuPopup.appendChild(oldSeparator);
+
+ // Create an item.
+ let item = new loader.cm.Item({ label: "item" });
+
+ test.showMenu(null, function (popup) {
+ assert.equal(test.contextMenuSeparator.nextSibling.nextSibling, oldSeparator,
+ "New separator should appear before the old one");
+ test.contextMenuPopup.removeChild(oldSeparator);
+ test.done();
+ });
+};
+
+// Destroying items that were previously created should cause them to be absent
+// from the menu.
+exports.testConstructDestroy = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ // Create an item.
+ let item = new loader.cm.Item({ label: "item" });
+ assert.equal(item.parentMenu, loader.cm.contentContextMenu,
+ "item's parent menu should be correct");
+
+ test.showMenu(null, function (popup) {
+
+ // It should be present when the menu is shown.
+ test.checkMenu([item], [], []);
+ popup.hidePopup();
+
+ // Destroy the item. Multiple destroys should be harmless.
+ item.destroy();
+ item.destroy();
+ test.showMenu(null, function (popup) {
+
+ // It should be removed from the menu.
+ test.checkMenu([item], [], [item]);
+ test.done();
+ });
+ });
+};
+
+
+// Destroying an item twice should not cause an error.
+exports.testDestroyTwice = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({ label: "item" });
+ item.destroy();
+ item.destroy();
+
+ test.pass("Destroying an item twice should not cause an error.");
+ test.done();
+};
+
+
+// CSS selector contexts should cause their items to be present in the menu
+// when the menu is invoked on nodes that match the selectors.
+exports.testSelectorContextMatch = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "item",
+ data: "item",
+ context: loader.cm.SelectorContext("img")
+ });
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#image", function (popup) {
+ test.checkMenu([item], [], []);
+ test.done();
+ });
+ });
+};
+
+
+// CSS selector contexts should cause their items to be present in the menu
+// when the menu is invoked on nodes that have ancestors that match the
+// selectors.
+exports.testSelectorAncestorContextMatch = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "item",
+ data: "item",
+ context: loader.cm.SelectorContext("a[href]")
+ });
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#span-link", function (popup) {
+ test.checkMenu([item], [], []);
+ test.done();
+ });
+ });
+};
+
+
+// CSS selector contexts should cause their items to be absent from the menu
+// when the menu is not invoked on nodes that match or have ancestors that
+// match the selectors.
+exports.testSelectorContextNoMatch = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "item",
+ data: "item",
+ context: loader.cm.SelectorContext("img")
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [item], []);
+ test.done();
+ });
+};
+
+
+// Page contexts should cause their items to be present in the menu when the
+// menu is not invoked on an active element.
+exports.testPageContextMatch = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [
+ new loader.cm.Item({
+ label: "item 0"
+ }),
+ new loader.cm.Item({
+ label: "item 1",
+ context: undefined
+ }),
+ new loader.cm.Item({
+ label: "item 2",
+ context: loader.cm.PageContext()
+ }),
+ new loader.cm.Item({
+ label: "item 3",
+ context: [loader.cm.PageContext()]
+ })
+ ];
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+};
+
+
+// Page contexts should cause their items to be absent from the menu when the
+// menu is invoked on an active element.
+exports.testPageContextNoMatch = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [
+ new loader.cm.Item({
+ label: "item 0"
+ }),
+ new loader.cm.Item({
+ label: "item 1",
+ context: undefined
+ }),
+ new loader.cm.Item({
+ label: "item 2",
+ context: loader.cm.PageContext()
+ }),
+ new loader.cm.Item({
+ label: "item 3",
+ context: [loader.cm.PageContext()]
+ })
+ ];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#image", function (popup) {
+ test.checkMenu(items, items, []);
+ test.done();
+ });
+ });
+};
+
+
+// Selection contexts should cause items to appear when a selection exists.
+exports.testSelectionContextMatch = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = loader.cm.Item({
+ label: "item",
+ context: loader.cm.SelectionContext()
+ });
+
+ test.withTestDoc(function (window, doc) {
+ window.getSelection().selectAllChildren(doc.body);
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ test.done();
+ });
+ });
+};
+
+
+// Selection contexts should cause items to appear when a selection exists in
+// a text field.
+exports.testSelectionContextMatchInTextField = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = loader.cm.Item({
+ label: "item",
+ context: loader.cm.SelectionContext()
+ });
+
+ test.withTestDoc(function (window, doc) {
+ test.selectRange("#textfield", 0, null);
+ test.showMenu("#textfield", function (popup) {
+ test.checkMenu([item], [], []);
+ test.done();
+ });
+ });
+};
+
+
+// Selection contexts should not cause items to appear when a selection does
+// not exist in a text field.
+exports.testSelectionContextNoMatchInTextField = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = loader.cm.Item({
+ label: "item",
+ context: loader.cm.SelectionContext()
+ });
+
+ test.withTestDoc(function (window, doc) {
+ test.selectRange("#textfield", 0, 0);
+ test.showMenu("#textfield", function (popup) {
+ test.checkMenu([item], [item], []);
+ test.done();
+ });
+ });
+};
+
+
+// Selection contexts should not cause items to appear when a selection does
+// not exist.
+exports.testSelectionContextNoMatch = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = loader.cm.Item({
+ label: "item",
+ context: loader.cm.SelectionContext()
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [item], []);
+ test.done();
+ });
+};
+
+
+// Selection contexts should cause items to appear when a selection exists even
+// for newly opened pages
+exports.testSelectionContextInNewTab = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = loader.cm.Item({
+ label: "item",
+ context: loader.cm.SelectionContext()
+ });
+
+ test.withTestDoc(function (window, doc) {
+ let link = doc.getElementById("targetlink");
+ link.click();
+
+ let tablistener = event => {
+ this.tabBrowser.tabContainer.removeEventListener("TabOpen", tablistener, false);
+ let tab = event.target;
+ let browser = tab.linkedBrowser;
+ this.loadFrameScript(browser);
+ this.delayedEventListener(browser, "load", () => {
+ let window = browser.contentWindow;
+ let doc = browser.contentDocument;
+ window.getSelection().selectAllChildren(doc.body);
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ popup.hidePopup();
+
+ test.tabBrowser.removeTab(test.tabBrowser.selectedTab);
+ test.tabBrowser.selectedTab = test.tab;
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [item], []);
+ test.done();
+ });
+ });
+ }, true);
+ };
+ this.tabBrowser.tabContainer.addEventListener("TabOpen", tablistener, false);
+ });
+};
+
+
+// Selection contexts should work when right clicking a form button
+exports.testSelectionContextButtonMatch = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = loader.cm.Item({
+ label: "item",
+ context: loader.cm.SelectionContext()
+ });
+
+ test.withTestDoc(function (window, doc) {
+ window.getSelection().selectAllChildren(doc.body);
+ test.showMenu("#button", function (popup) {
+ test.checkMenu([item], [], []);
+ test.done();
+ });
+ });
+};
+
+
+//Selection contexts should work when right clicking a form button
+exports.testSelectionContextButtonNoMatch = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = loader.cm.Item({
+ label: "item",
+ context: loader.cm.SelectionContext()
+ });
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#button", function (popup) {
+ test.checkMenu([item], [item], []);
+ test.done();
+ });
+ });
+};
+
+
+// URL contexts should cause items to appear on pages that match.
+exports.testURLContextMatch = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [
+ loader.cm.Item({
+ label: "item 0",
+ context: loader.cm.URLContext(TEST_DOC_URL)
+ }),
+ loader.cm.Item({
+ label: "item 1",
+ context: loader.cm.URLContext([TEST_DOC_URL, "*.bogus.com"])
+ }),
+ loader.cm.Item({
+ label: "item 2",
+ context: loader.cm.URLContext([new RegExp(".*\\.html")])
+ })
+ ];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+
+// URL contexts should not cause items to appear on pages that do not match.
+exports.testURLContextNoMatch = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [
+ loader.cm.Item({
+ label: "item 0",
+ context: loader.cm.URLContext("*.bogus.com")
+ }),
+ loader.cm.Item({
+ label: "item 1",
+ context: loader.cm.URLContext(["*.bogus.com", "*.gnarly.com"])
+ }),
+ loader.cm.Item({
+ label: "item 2",
+ context: loader.cm.URLContext([new RegExp(".*\\.js")])
+ })
+ ];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, items, []);
+ test.done();
+ });
+ });
+};
+
+
+// Loading a new page in the same tab should correctly start a new worker for
+// any content scripts
+exports.testPageReload = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = loader.cm.Item({
+ label: "Item",
+ contentScript: "var doc = document; self.on('context', node => doc.body.getAttribute('showItem') == 'true');"
+ });
+
+ test.withTestDoc(function (window, doc) {
+ // Set a flag on the document that the item uses
+ doc.body.setAttribute("showItem", "true");
+
+ test.showMenu(null, function (popup) {
+ // With the attribute true the item should be visible in the menu
+ test.checkMenu([item], [], []);
+ test.hideMenu(function() {
+ let browser = this.tabBrowser.getBrowserForTab(this.tab)
+ test.delayedEventListener(browser, "load", function() {
+ test.delayedEventListener(browser, "load", function() {
+ window = browser.contentWindow;
+ doc = window.document;
+
+ // Set a flag on the document that the item uses
+ doc.body.setAttribute("showItem", "false");
+
+ test.showMenu(null, function (popup) {
+ // In the new document with the attribute false the item should be
+ // hidden, but if the contentScript hasn't been reloaded it will
+ // still see the old value
+ test.checkMenu([item], [item], []);
+
+ test.done();
+ });
+ }, true);
+ browser.loadURI(TEST_DOC_URL, null, null);
+ }, true);
+ // Required to make sure we load a new page in history rather than
+ // just reloading the current page which would unload it
+ browser.loadURI("about:blank", null, null);
+ });
+ });
+ });
+};
+
+// Closing a page after it's been used with a worker should cause the worker
+// to be destroyed
+/*exports.testWorkerDestroy = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let loadExpected = false;
+
+ let item = loader.cm.Item({
+ label: "item",
+ contentScript: 'self.postMessage("loaded"); self.on("detach", function () { console.log("saw detach"); self.postMessage("detach") });',
+ onMessage: function (msg) {
+ switch (msg) {
+ case "loaded":
+ assert.ok(loadExpected, "Should have seen the load event at the right time");
+ loadExpected = false;
+ break;
+ case "detach":
+ test.done();
+ break;
+ }
+ }
+ });
+
+ test.withTestDoc(function (window, doc) {
+ loadExpected = true;
+ test.showMenu(null, function (popup) {
+ assert.ok(!loadExpected, "Should have seen a message");
+
+ test.checkMenu([item], [], []);
+
+ test.closeTab();
+ });
+ });
+};*/
+
+
+// Content contexts that return true should cause their items to be present
+// in the menu.
+exports.testContentContextMatch = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "item",
+ contentScript: 'self.on("context", () => true);'
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ test.done();
+ });
+};
+
+
+// Content contexts that return false should cause their items to be absent
+// from the menu.
+exports.testContentContextNoMatch = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "item",
+ contentScript: 'self.on("context", () => false);'
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [item], []);
+ test.done();
+ });
+};
+
+
+// Content contexts that return undefined should cause their items to be absent
+// from the menu.
+exports.testContentContextUndefined = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "item",
+ contentScript: 'self.on("context", function () {});'
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [item], []);
+ test.done();
+ });
+};
+
+
+// Content contexts that return an empty string should cause their items to be
+// absent from the menu and shouldn't wipe the label
+exports.testContentContextEmptyString = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "item",
+ contentScript: 'self.on("context", () => "");'
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [item], []);
+ assert.equal(item.label, "item", "Label should still be correct");
+ test.done();
+ });
+};
+
+
+// If any content contexts returns true then their items should be present in
+// the menu.
+exports.testMultipleContentContextMatch1 = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "item",
+ contentScript: 'self.on("context", () => true); ' +
+ 'self.on("context", () => false);',
+ onMessage: function() {
+ test.fail("Should not have called the second context listener");
+ }
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ test.done();
+ });
+};
+
+
+// If any content contexts returns true then their items should be present in
+// the menu.
+exports.testMultipleContentContextMatch2 = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "item",
+ contentScript: 'self.on("context", () => false); ' +
+ 'self.on("context", () => true);'
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ test.done();
+ });
+};
+
+
+// If any content contexts returns a string then their items should be present
+// in the menu.
+exports.testMultipleContentContextString1 = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "item",
+ contentScript: 'self.on("context", () => "new label"); ' +
+ 'self.on("context", () => false);'
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ assert.equal(item.label, "new label", "Label should have changed");
+ test.done();
+ });
+};
+
+
+// If any content contexts returns a string then their items should be present
+// in the menu.
+exports.testMultipleContentContextString2 = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "item",
+ contentScript: 'self.on("context", () => false); ' +
+ 'self.on("context", () => "new label");'
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ assert.equal(item.label, "new label", "Label should have changed");
+ test.done();
+ });
+};
+
+
+// If many content contexts returns a string then the first should take effect
+exports.testMultipleContentContextString3 = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "item",
+ contentScript: 'self.on("context", () => "new label 1"); ' +
+ 'self.on("context", () => "new label 2");'
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ assert.equal(item.label, "new label 1", "Label should have changed");
+ test.done();
+ });
+};
+
+
+// Content contexts that return true should cause their items to be present
+// in the menu when context clicking an active element.
+exports.testContentContextMatchActiveElement = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [
+ new loader.cm.Item({
+ label: "item 1",
+ contentScript: 'self.on("context", () => true);'
+ }),
+ new loader.cm.Item({
+ label: "item 2",
+ context: undefined,
+ contentScript: 'self.on("context", () => true);'
+ }),
+ // These items will always be hidden by the declarative usage of PageContext
+ new loader.cm.Item({
+ label: "item 3",
+ context: loader.cm.PageContext(),
+ contentScript: 'self.on("context", () => true);'
+ }),
+ new loader.cm.Item({
+ label: "item 4",
+ context: [loader.cm.PageContext()],
+ contentScript: 'self.on("context", () => true);'
+ })
+ ];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#image", function (popup) {
+ test.checkMenu(items, [items[2], items[3]], []);
+ test.done();
+ });
+ });
+};
+
+
+// Content contexts that return false should cause their items to be absent
+// from the menu when context clicking an active element.
+exports.testContentContextNoMatchActiveElement = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [
+ new loader.cm.Item({
+ label: "item 1",
+ contentScript: 'self.on("context", () => false);'
+ }),
+ new loader.cm.Item({
+ label: "item 2",
+ context: undefined,
+ contentScript: 'self.on("context", () => false);'
+ }),
+ // These items will always be hidden by the declarative usage of PageContext
+ new loader.cm.Item({
+ label: "item 3",
+ context: loader.cm.PageContext(),
+ contentScript: 'self.on("context", () => false);'
+ }),
+ new loader.cm.Item({
+ label: "item 4",
+ context: [loader.cm.PageContext()],
+ contentScript: 'self.on("context", () => false);'
+ })
+ ];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#image", function (popup) {
+ test.checkMenu(items, items, []);
+ test.done();
+ });
+ });
+};
+
+
+// Content contexts that return undefined should cause their items to be absent
+// from the menu when context clicking an active element.
+exports.testContentContextNoMatchActiveElement = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [
+ new loader.cm.Item({
+ label: "item 1",
+ contentScript: 'self.on("context", () => {});'
+ }),
+ new loader.cm.Item({
+ label: "item 2",
+ context: undefined,
+ contentScript: 'self.on("context", () => {});'
+ }),
+ // These items will always be hidden by the declarative usage of PageContext
+ new loader.cm.Item({
+ label: "item 3",
+ context: loader.cm.PageContext(),
+ contentScript: 'self.on("context", () => {});'
+ }),
+ new loader.cm.Item({
+ label: "item 4",
+ context: [loader.cm.PageContext()],
+ contentScript: 'self.on("context", () => {});'
+ })
+ ];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#image", function (popup) {
+ test.checkMenu(items, items, []);
+ test.done();
+ });
+ });
+};
+
+
+// Content contexts that return a string should cause their items to be present
+// in the menu and the items' labels to be updated.
+exports.testContentContextMatchString = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "first label",
+ contentScript: 'self.on("context", () => "second label");'
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ assert.equal(item.label, "second label",
+ "item's label should be updated");
+ test.done();
+ });
+};
+
+
+// Ensure that contentScriptFile is working correctly
+exports.testContentScriptFile = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+ let { defer, all } = require("sdk/core/promise");
+ let itemScript = [defer(), defer()];
+ let menuShown = defer();
+ let menuPromises = itemScript.concat(menuShown).map(({promise}) => promise);
+
+ // Reject remote files
+ assert.throws(function() {
+ new loader.cm.Item({
+ label: "item",
+ contentScriptFile: "http://mozilla.com/context-menu.js"
+ });
+ },
+ /The `contentScriptFile` option must be a local URL or an array of URLs/,
+ "Item throws when contentScriptFile is a remote URL");
+
+ // But accept files from data folder
+ let item = new loader.cm.Item({
+ label: "item",
+ contentScriptFile: data.url("test-contentScriptFile.js"),
+ onMessage: (message) => {
+ assert.equal(message, "msg from contentScriptFile",
+ "contentScriptFile loaded with absolute url");
+ itemScript[0].resolve();
+ }
+ });
+
+ let item2 = new loader.cm.Item({
+ label: "item2",
+ contentScriptFile: "./test-contentScriptFile.js",
+ onMessage: (message) => {
+ assert.equal(message, "msg from contentScriptFile",
+ "contentScriptFile loaded with relative url");
+ itemScript[1].resolve();
+ }
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item, item2], [], []);
+ menuShown.resolve();
+ });
+
+ all(menuPromises).then(() => test.done());
+};
+
+
+// The args passed to context listeners should be correct.
+exports.testContentContextArgs = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+ let callbacks = 0;
+
+ let item = new loader.cm.Item({
+ label: "item",
+ contentScript: 'self.on("context", function (node) {' +
+ ' self.postMessage(node.tagName);' +
+ ' return false;' +
+ '});',
+ onMessage: function (tagName) {
+ assert.equal(tagName, "HTML", "node should be an HTML element");
+ if (++callbacks == 2) test.done();
+ }
+ });
+
+ test.showMenu(null, function () {
+ if (++callbacks == 2) test.done();
+ });
+};
+
+// Multiple contexts imply intersection, not union.
+exports.testMultipleContexts = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "item",
+ context: [loader.cm.SelectorContext("a[href]"), loader.cm.PageContext()],
+ });
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#span-link", function (popup) {
+ test.checkMenu([item], [item], []);
+ test.done();
+ });
+ });
+};
+
+// Once a context is removed, it should no longer cause its item to appear.
+exports.testRemoveContext = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let ctxt = loader.cm.SelectorContext("img");
+ let item = new loader.cm.Item({
+ label: "item",
+ context: ctxt
+ });
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#image", function (popup) {
+
+ // The item should be present at first.
+ test.checkMenu([item], [], []);
+ popup.hidePopup();
+
+ // Remove the img context and check again.
+ item.context.remove(ctxt);
+ test.showMenu("#image", function (popup) {
+ test.checkMenu([item], [item], []);
+ test.done();
+ });
+ });
+ });
+};
+
+// Once a context is removed, it should no longer cause its item to appear.
+exports.testSetContextRemove = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let ctxt = loader.cm.SelectorContext("img");
+ let item = new loader.cm.Item({
+ label: "item",
+ context: ctxt
+ });
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#image", function (popup) {
+
+ // The item should be present at first.
+ test.checkMenu([item], [], []);
+ popup.hidePopup();
+
+ // Remove the img context and check again.
+ item.context = [];
+ test.showMenu("#image", function (popup) {
+ test.checkMenu([item], [item], []);
+ test.done();
+ });
+ });
+ });
+};
+
+// Once a context is added, it should affect whether the item appears.
+exports.testAddContext = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let ctxt = loader.cm.SelectorContext("img");
+ let item = new loader.cm.Item({
+ label: "item"
+ });
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#image", function (popup) {
+
+ // The item should not be present at first.
+ test.checkMenu([item], [item], []);
+ popup.hidePopup();
+
+ // Add the img context and check again.
+ item.context.add(ctxt);
+ test.showMenu("#image", function (popup) {
+ test.checkMenu([item], [], []);
+ test.done();
+ });
+ });
+ });
+};
+
+// Once a context is added, it should affect whether the item appears.
+exports.testSetContextAdd = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let ctxt = loader.cm.SelectorContext("img");
+ let item = new loader.cm.Item({
+ label: "item"
+ });
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#image", function (popup) {
+
+ // The item should not be present at first.
+ test.checkMenu([item], [item], []);
+ popup.hidePopup();
+
+ // Add the img context and check again.
+ item.context = [ctxt];
+ test.showMenu("#image", function (popup) {
+ test.checkMenu([item], [], []);
+ test.done();
+ });
+ });
+ });
+};
+
+// Lots of items should overflow into the overflow submenu.
+exports.testOverflow = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [];
+ for (let i = 0; i < OVERFLOW_THRESH_DEFAULT + 1; i++) {
+ let item = new loader.cm.Item({ label: "item " + i });
+ items.push(item);
+ }
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+};
+
+
+// Module unload should cause all items to be removed.
+exports.testUnload = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({ label: "item" });
+
+ test.showMenu(null, function (popup) {
+
+ // The menu should contain the item.
+ test.checkMenu([item], [], []);
+ popup.hidePopup();
+
+ // Unload the module.
+ loader.unload();
+ test.showMenu(null, function (popup) {
+
+ // The item should be removed from the menu.
+ test.checkMenu([item], [], [item]);
+ test.done();
+ });
+ });
+};
+
+
+// Using multiple module instances to add items without causing overflow should
+// work OK. Assumes OVERFLOW_THRESH_DEFAULT >= 2.
+exports.testMultipleModulesAdd = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader0 = test.newLoader();
+ let loader1 = test.newLoader();
+
+ // Use each module to add an item, then unload each module in turn.
+ let item0 = new loader0.cm.Item({ label: "item 0" });
+ let item1 = new loader1.cm.Item({ label: "item 1" });
+
+ test.showMenu(null, function (popup) {
+
+ // The menu should contain both items.
+ test.checkMenu([item0, item1], [], []);
+ popup.hidePopup();
+
+ // Unload the first module.
+ loader0.unload();
+ test.showMenu(null, function (popup) {
+
+ // The first item should be removed from the menu.
+ test.checkMenu([item0, item1], [], [item0]);
+ popup.hidePopup();
+
+ // Unload the second module.
+ loader1.unload();
+ test.showMenu(null, function (popup) {
+
+ // Both items should be removed from the menu.
+ test.checkMenu([item0, item1], [], [item0, item1]);
+ test.done();
+ });
+ });
+ });
+};
+
+
+// Using multiple module instances to add items causing overflow should work OK.
+exports.testMultipleModulesAddOverflow = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader0 = test.newLoader();
+ let loader1 = test.newLoader();
+
+ // Use module 0 to add OVERFLOW_THRESH_DEFAULT items.
+ let items0 = [];
+ for (let i = 0; i < OVERFLOW_THRESH_DEFAULT; i++) {
+ let item = new loader0.cm.Item({ label: "item 0 " + i });
+ items0.push(item);
+ }
+
+ // Use module 1 to add one item.
+ let item1 = new loader1.cm.Item({ label: "item 1" });
+
+ let allItems = items0.concat(item1);
+
+ test.showMenu(null, function (popup) {
+
+ // The menu should contain all items in overflow.
+ test.checkMenu(allItems, [], []);
+ popup.hidePopup();
+
+ // Unload the first module.
+ loader0.unload();
+ test.showMenu(null, function (popup) {
+
+ // The first items should be removed from the menu, which should not
+ // overflow.
+ test.checkMenu(allItems, [], items0);
+ popup.hidePopup();
+
+ // Unload the second module.
+ loader1.unload();
+ test.showMenu(null, function (popup) {
+
+ // All items should be removed from the menu.
+ test.checkMenu(allItems, [], allItems);
+ test.done();
+ });
+ });
+ });
+};
+
+
+// Using multiple module instances to modify the menu without causing overflow
+// should work OK. This test creates two loaders and:
+// loader0 create item -> loader1 create item -> loader0.unload ->
+// loader1.unload
+exports.testMultipleModulesDiffContexts1 = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader0 = test.newLoader();
+ let loader1 = test.newLoader();
+
+ let item0 = new loader0.cm.Item({
+ label: "item 0",
+ context: loader0.cm.SelectorContext("img")
+ });
+
+ let item1 = new loader1.cm.Item({ label: "item 1" });
+
+ test.showMenu(null, function (popup) {
+
+ // The menu should contain item1.
+ test.checkMenu([item0, item1], [item0], []);
+ popup.hidePopup();
+
+ // Unload module 0.
+ loader0.unload();
+ test.showMenu(null, function (popup) {
+
+ // item0 should be removed from the menu.
+ test.checkMenu([item0, item1], [], [item0]);
+ popup.hidePopup();
+
+ // Unload module 1.
+ loader1.unload();
+ test.showMenu(null, function (popup) {
+
+ // Both items should be removed from the menu.
+ test.checkMenu([item0, item1], [], [item0, item1]);
+ test.done();
+ });
+ });
+ });
+};
+
+
+// Using multiple module instances to modify the menu without causing overflow
+// should work OK. This test creates two loaders and:
+// loader1 create item -> loader0 create item -> loader0.unload ->
+// loader1.unload
+exports.testMultipleModulesDiffContexts2 = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader0 = test.newLoader();
+ let loader1 = test.newLoader();
+
+ let item1 = new loader1.cm.Item({ label: "item 1" });
+
+ let item0 = new loader0.cm.Item({
+ label: "item 0",
+ context: loader0.cm.SelectorContext("img")
+ });
+
+ test.showMenu(null, function (popup) {
+
+ // The menu should contain item1.
+ test.checkMenu([item0, item1], [item0], []);
+ popup.hidePopup();
+
+ // Unload module 0.
+ loader0.unload();
+ test.showMenu(null, function (popup) {
+
+ // item0 should be removed from the menu.
+ test.checkMenu([item0, item1], [], [item0]);
+ popup.hidePopup();
+
+ // Unload module 1.
+ loader1.unload();
+ test.showMenu(null, function (popup) {
+
+ // Both items should be removed from the menu.
+ test.checkMenu([item0, item1], [], [item0, item1]);
+ test.done();
+ });
+ });
+ });
+};
+
+
+// Using multiple module instances to modify the menu without causing overflow
+// should work OK. This test creates two loaders and:
+// loader0 create item -> loader1 create item -> loader1.unload ->
+// loader0.unload
+exports.testMultipleModulesDiffContexts3 = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader0 = test.newLoader();
+ let loader1 = test.newLoader();
+
+ let item0 = new loader0.cm.Item({
+ label: "item 0",
+ context: loader0.cm.SelectorContext("img")
+ });
+
+ let item1 = new loader1.cm.Item({ label: "item 1" });
+
+ test.showMenu(null, function (popup) {
+
+ // The menu should contain item1.
+ test.checkMenu([item0, item1], [item0], []);
+ popup.hidePopup();
+
+ // Unload module 1.
+ loader1.unload();
+ test.showMenu(null, function (popup) {
+
+ // item1 should be removed from the menu.
+ test.checkMenu([item0, item1], [item0], [item1]);
+ popup.hidePopup();
+
+ // Unload module 0.
+ loader0.unload();
+ test.showMenu(null, function (popup) {
+
+ // Both items should be removed from the menu.
+ test.checkMenu([item0, item1], [], [item0, item1]);
+ test.done();
+ });
+ });
+ });
+};
+
+
+// Using multiple module instances to modify the menu without causing overflow
+// should work OK. This test creates two loaders and:
+// loader1 create item -> loader0 create item -> loader1.unload ->
+// loader0.unload
+exports.testMultipleModulesDiffContexts4 = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader0 = test.newLoader();
+ let loader1 = test.newLoader();
+
+ let item1 = new loader1.cm.Item({ label: "item 1" });
+
+ let item0 = new loader0.cm.Item({
+ label: "item 0",
+ context: loader0.cm.SelectorContext("img")
+ });
+
+ test.showMenu(null, function (popup) {
+
+ // The menu should contain item1.
+ test.checkMenu([item0, item1], [item0], []);
+ popup.hidePopup();
+
+ // Unload module 1.
+ loader1.unload();
+ test.showMenu(null, function (popup) {
+
+ // item1 should be removed from the menu.
+ test.checkMenu([item0, item1], [item0], [item1]);
+ popup.hidePopup();
+
+ // Unload module 0.
+ loader0.unload();
+ test.showMenu(null, function (popup) {
+
+ // Both items should be removed from the menu.
+ test.checkMenu([item0, item1], [], [item0, item1]);
+ test.done();
+ });
+ });
+ });
+};
+
+
+// Test interactions between a loaded module, unloading another module, and the
+// menu separator and overflow submenu.
+exports.testMultipleModulesAddRemove = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader0 = test.newLoader();
+ let loader1 = test.newLoader();
+
+ let item = new loader0.cm.Item({ label: "item" });
+
+ test.showMenu(null, function (popup) {
+
+ // The menu should contain the item.
+ test.checkMenu([item], [], []);
+ popup.hidePopup();
+
+ // Remove the item.
+ item.destroy();
+ test.showMenu(null, function (popup) {
+
+ // The item should be removed from the menu.
+ test.checkMenu([item], [], [item]);
+ popup.hidePopup();
+
+ // Unload module 1.
+ loader1.unload();
+ test.showMenu(null, function (popup) {
+
+ // There shouldn't be any errors involving the menu separator or
+ // overflow submenu.
+ test.checkMenu([item], [], [item]);
+ test.done();
+ });
+ });
+ });
+};
+
+
+// Checks that the order of menu items is correct when adding/removing across
+// multiple modules. All items from a single module should remain in a group
+exports.testMultipleModulesOrder = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader0 = test.newLoader();
+ let loader1 = test.newLoader();
+
+ // Use each module to add an item, then unload each module in turn.
+ let item0 = new loader0.cm.Item({ label: "item 0" });
+ let item1 = new loader1.cm.Item({ label: "item 1" });
+
+ test.showMenu(null, function (popup) {
+
+ // The menu should contain both items.
+ test.checkMenu([item0, item1], [], []);
+ popup.hidePopup();
+
+ let item2 = new loader0.cm.Item({ label: "item 2" });
+
+ test.showMenu(null, function (popup) {
+
+ // The new item should be grouped with the same items from loader0.
+ test.checkMenu([item0, item2, item1], [], []);
+ popup.hidePopup();
+
+ let item3 = new loader1.cm.Item({ label: "item 3" });
+
+ test.showMenu(null, function (popup) {
+
+ // Same again
+ test.checkMenu([item0, item2, item1, item3], [], []);
+ test.done();
+ });
+ });
+ });
+};
+
+
+// Checks that the order of menu items is correct when adding/removing across
+// multiple modules when overflowing. All items from a single module should
+// remain in a group
+exports.testMultipleModulesOrderOverflow = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader0 = test.newLoader();
+ let loader1 = test.newLoader();
+
+ let prefs = loader0.loader.require("sdk/preferences/service");
+ prefs.set(OVERFLOW_THRESH_PREF, 0);
+
+ // Use each module to add an item, then unload each module in turn.
+ let item0 = new loader0.cm.Item({ label: "item 0" });
+ let item1 = new loader1.cm.Item({ label: "item 1" });
+
+ test.showMenu(null, function (popup) {
+
+ // The menu should contain both items.
+ test.checkMenu([item0, item1], [], []);
+ popup.hidePopup();
+
+ let item2 = new loader0.cm.Item({ label: "item 2" });
+
+ test.showMenu(null, function (popup) {
+
+ // The new item should be grouped with the same items from loader0.
+ test.checkMenu([item0, item2, item1], [], []);
+ popup.hidePopup();
+
+ let item3 = new loader1.cm.Item({ label: "item 3" });
+
+ test.showMenu(null, function (popup) {
+
+ // Same again
+ test.checkMenu([item0, item2, item1, item3], [], []);
+ test.done();
+ });
+ });
+ });
+};
+
+
+// Checks that if a module's items are all hidden then the overflow menu doesn't
+// get hidden
+exports.testMultipleModulesOverflowHidden = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader0 = test.newLoader();
+ let loader1 = test.newLoader();
+
+ let prefs = loader0.loader.require("sdk/preferences/service");
+ prefs.set(OVERFLOW_THRESH_PREF, 0);
+
+ // Use each module to add an item, then unload each module in turn.
+ let item0 = new loader0.cm.Item({ label: "item 0" });
+ let item1 = new loader1.cm.Item({
+ label: "item 1",
+ context: loader1.cm.SelectorContext("a")
+ });
+
+ test.showMenu(null, function (popup) {
+ // One should be hidden
+ test.checkMenu([item0, item1], [item1], []);
+ test.done();
+ });
+};
+
+
+// Checks that if a module's items are all hidden then the overflow menu doesn't
+// get hidden (reverse order to above)
+exports.testMultipleModulesOverflowHidden2 = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader0 = test.newLoader();
+ let loader1 = test.newLoader();
+
+ let prefs = loader0.loader.require("sdk/preferences/service");
+ prefs.set(OVERFLOW_THRESH_PREF, 0);
+
+ // Use each module to add an item, then unload each module in turn.
+ let item0 = new loader0.cm.Item({
+ label: "item 0",
+ context: loader0.cm.SelectorContext("a")
+ });
+ let item1 = new loader1.cm.Item({ label: "item 1" });
+
+ test.showMenu(null, function (popup) {
+ // One should be hidden
+ test.checkMenu([item0, item1], [item0], []);
+ test.done();
+ });
+};
+
+
+// Checks that we don't overflow if there are more items than the overflow
+// threshold but not all of them are visible
+exports.testOverflowIgnoresHidden = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let prefs = loader.loader.require("sdk/preferences/service");
+ prefs.set(OVERFLOW_THRESH_PREF, 2);
+
+ let allItems = [
+ new loader.cm.Item({
+ label: "item 0"
+ }),
+ new loader.cm.Item({
+ label: "item 1"
+ }),
+ new loader.cm.Item({
+ label: "item 2",
+ context: loader.cm.SelectorContext("a")
+ })
+ ];
+
+ test.showMenu(null, function (popup) {
+ // One should be hidden
+ test.checkMenu(allItems, [allItems[2]], []);
+ test.done();
+ });
+};
+
+
+// Checks that we don't overflow if there are more items than the overflow
+// threshold but not all of them are visible
+exports.testOverflowIgnoresHiddenMultipleModules1 = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader0 = test.newLoader();
+ let loader1 = test.newLoader();
+
+ let prefs = loader0.loader.require("sdk/preferences/service");
+ prefs.set(OVERFLOW_THRESH_PREF, 2);
+
+ let allItems = [
+ new loader0.cm.Item({
+ label: "item 0"
+ }),
+ new loader0.cm.Item({
+ label: "item 1"
+ }),
+ new loader1.cm.Item({
+ label: "item 2",
+ context: loader1.cm.SelectorContext("a")
+ }),
+ new loader1.cm.Item({
+ label: "item 3",
+ context: loader1.cm.SelectorContext("a")
+ })
+ ];
+
+ test.showMenu(null, function (popup) {
+ // One should be hidden
+ test.checkMenu(allItems, [allItems[2], allItems[3]], []);
+ test.done();
+ });
+};
+
+
+// Checks that we don't overflow if there are more items than the overflow
+// threshold but not all of them are visible
+exports.testOverflowIgnoresHiddenMultipleModules2 = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader0 = test.newLoader();
+ let loader1 = test.newLoader();
+
+ let prefs = loader0.loader.require("sdk/preferences/service");
+ prefs.set(OVERFLOW_THRESH_PREF, 2);
+
+ let allItems = [
+ new loader0.cm.Item({
+ label: "item 0"
+ }),
+ new loader0.cm.Item({
+ label: "item 1",
+ context: loader0.cm.SelectorContext("a")
+ }),
+ new loader1.cm.Item({
+ label: "item 2"
+ }),
+ new loader1.cm.Item({
+ label: "item 3",
+ context: loader1.cm.SelectorContext("a")
+ })
+ ];
+
+ test.showMenu(null, function (popup) {
+ // One should be hidden
+ test.checkMenu(allItems, [allItems[1], allItems[3]], []);
+ test.done();
+ });
+};
+
+
+// Checks that we don't overflow if there are more items than the overflow
+// threshold but not all of them are visible
+exports.testOverflowIgnoresHiddenMultipleModules3 = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader0 = test.newLoader();
+ let loader1 = test.newLoader();
+
+ let prefs = loader0.loader.require("sdk/preferences/service");
+ prefs.set(OVERFLOW_THRESH_PREF, 2);
+
+ let allItems = [
+ new loader0.cm.Item({
+ label: "item 0",
+ context: loader0.cm.SelectorContext("a")
+ }),
+ new loader0.cm.Item({
+ label: "item 1",
+ context: loader0.cm.SelectorContext("a")
+ }),
+ new loader1.cm.Item({
+ label: "item 2"
+ }),
+ new loader1.cm.Item({
+ label: "item 3"
+ })
+ ];
+
+ test.showMenu(null, function (popup) {
+ // One should be hidden
+ test.checkMenu(allItems, [allItems[0], allItems[1]], []);
+ test.done();
+ });
+};
+
+
+// Tests that we transition between overflowing to non-overflowing to no items
+// and back again
+exports.testOverflowTransition = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let prefs = loader.loader.require("sdk/preferences/service");
+ prefs.set(OVERFLOW_THRESH_PREF, 2);
+
+ let pItems = [
+ new loader.cm.Item({
+ label: "item 0",
+ context: loader.cm.SelectorContext("p")
+ }),
+ new loader.cm.Item({
+ label: "item 1",
+ context: loader.cm.SelectorContext("p")
+ })
+ ];
+
+ let aItems = [
+ new loader.cm.Item({
+ label: "item 2",
+ context: loader.cm.SelectorContext("a")
+ }),
+ new loader.cm.Item({
+ label: "item 3",
+ context: loader.cm.SelectorContext("a")
+ })
+ ];
+
+ let allItems = pItems.concat(aItems);
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#link", function (popup) {
+ // The menu should contain all items and will overflow
+ test.checkMenu(allItems, [], []);
+ popup.hidePopup();
+
+ test.showMenu("#text", function (popup) {
+ // Only contains hald the items and will not overflow
+ test.checkMenu(allItems, aItems, []);
+ popup.hidePopup();
+
+ test.showMenu(null, function (popup) {
+ // None of the items will be visible
+ test.checkMenu(allItems, allItems, []);
+ popup.hidePopup();
+
+ test.showMenu("#text", function (popup) {
+ // Only contains hald the items and will not overflow
+ test.checkMenu(allItems, aItems, []);
+ popup.hidePopup();
+
+ test.showMenu("#link", function (popup) {
+ // The menu should contain all items and will overflow
+ test.checkMenu(allItems, [], []);
+ popup.hidePopup();
+
+ test.showMenu(null, function (popup) {
+ // None of the items will be visible
+ test.checkMenu(allItems, allItems, []);
+ popup.hidePopup();
+
+ test.showMenu("#link", function (popup) {
+ // The menu should contain all items and will overflow
+ test.checkMenu(allItems, [], []);
+ test.done();
+ });
+ });
+ });
+ });
+ });
+ });
+ });
+ });
+};
+
+
+// An item's command listener should work.
+exports.testItemCommand = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "item",
+ data: "item data",
+ contentScript: 'self.on("click", function (node, data) {' +
+ ' self.postMessage({' +
+ ' tagName: node.tagName,' +
+ ' data: data' +
+ ' });' +
+ '});',
+ onMessage: function (data) {
+ assert.equal(this, item, "`this` inside onMessage should be item");
+ assert.equal(data.tagName, "HTML", "node should be an HTML element");
+ assert.equal(data.data, item.data, "data should be item data");
+ test.done();
+ }
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ let elt = test.getItemElt(popup, item);
+
+ // create a command event
+ let evt = elt.ownerDocument.createEvent('Event');
+ evt.initEvent('command', true, true);
+ elt.dispatchEvent(evt);
+ });
+};
+
+
+// A menu's click listener should work and receive bubbling 'command' events from
+// sub-items appropriately. This also tests menus and ensures that when a CSS
+// selector context matches the clicked node's ancestor, the matching ancestor
+// is passed to listeners as the clicked node.
+exports.testMenuCommand = function (assert, done) {
+ // Create a top-level menu, submenu, and item, like this:
+ // topMenu -> submenu -> item
+ // Click the item and make sure the click bubbles.
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "submenu item",
+ data: "submenu item data",
+ context: loader.cm.SelectorContext("a"),
+ });
+
+ let submenu = new loader.cm.Menu({
+ label: "submenu",
+ context: loader.cm.SelectorContext("a"),
+ items: [item]
+ });
+
+ let topMenu = new loader.cm.Menu({
+ label: "top menu",
+ contentScript: 'self.on("click", function (node, data) {' +
+ ' self.postMessage({' +
+ ' tagName: node.tagName,' +
+ ' data: data' +
+ ' });' +
+ '});',
+ onMessage: function (data) {
+ assert.equal(this, topMenu, "`this` inside top menu should be menu");
+ assert.equal(data.tagName, "A", "Clicked node should be anchor");
+ assert.equal(data.data, item.data,
+ "Clicked item data should be correct");
+ test.done();
+ },
+ items: [submenu],
+ context: loader.cm.SelectorContext("a")
+ });
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#span-link", function (popup) {
+ test.checkMenu([topMenu], [], []);
+ let topMenuElt = test.getItemElt(popup, topMenu);
+ let topMenuPopup = topMenuElt.firstChild;
+ let submenuElt = test.getItemElt(topMenuPopup, submenu);
+ let submenuPopup = submenuElt.firstChild;
+ let itemElt = test.getItemElt(submenuPopup, item);
+
+ // create a command event
+ let evt = itemElt.ownerDocument.createEvent('Event');
+ evt.initEvent('command', true, true);
+ itemElt.dispatchEvent(evt);
+ });
+ });
+};
+
+
+// Click listeners should work when multiple modules are loaded.
+exports.testItemCommandMultipleModules = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader0 = test.newLoader();
+ let loader1 = test.newLoader();
+
+ let item0 = loader0.cm.Item({
+ label: "loader 0 item",
+ contentScript: 'self.on("click", self.postMessage);',
+ onMessage: function () {
+ test.fail("loader 0 item should not emit click event");
+ }
+ });
+ let item1 = loader1.cm.Item({
+ label: "loader 1 item",
+ contentScript: 'self.on("click", self.postMessage);',
+ onMessage: function () {
+ test.pass("loader 1 item clicked as expected");
+ test.done();
+ }
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item0, item1], [], []);
+ let item1Elt = test.getItemElt(popup, item1);
+
+ // create a command event
+ let evt = item1Elt.ownerDocument.createEvent('Event');
+ evt.initEvent('command', true, true);
+ item1Elt.dispatchEvent(evt);
+ });
+};
+
+
+
+
+// An item's click listener should work.
+exports.testItemClick = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "item",
+ data: "item data",
+ contentScript: 'self.on("click", function (node, data) {' +
+ ' self.postMessage({' +
+ ' tagName: node.tagName,' +
+ ' data: data' +
+ ' });' +
+ '});',
+ onMessage: function (data) {
+ assert.equal(this, item, "`this` inside onMessage should be item");
+ assert.equal(data.tagName, "HTML", "node should be an HTML element");
+ assert.equal(data.data, item.data, "data should be item data");
+ test.done();
+ }
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ let elt = test.getItemElt(popup, item);
+ elt.click();
+ });
+};
+
+
+// A menu's click listener should work and receive bubbling clicks from
+// sub-items appropriately. This also tests menus and ensures that when a CSS
+// selector context matches the clicked node's ancestor, the matching ancestor
+// is passed to listeners as the clicked node.
+exports.testMenuClick = function (assert, done) {
+ // Create a top-level menu, submenu, and item, like this:
+ // topMenu -> submenu -> item
+ // Click the item and make sure the click bubbles.
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "submenu item",
+ data: "submenu item data",
+ context: loader.cm.SelectorContext("a"),
+ });
+
+ let submenu = new loader.cm.Menu({
+ label: "submenu",
+ context: loader.cm.SelectorContext("a"),
+ items: [item]
+ });
+
+ let topMenu = new loader.cm.Menu({
+ label: "top menu",
+ contentScript: 'self.on("click", function (node, data) {' +
+ ' self.postMessage({' +
+ ' tagName: node.tagName,' +
+ ' data: data' +
+ ' });' +
+ '});',
+ onMessage: function (data) {
+ assert.equal(this, topMenu, "`this` inside top menu should be menu");
+ assert.equal(data.tagName, "A", "Clicked node should be anchor");
+ assert.equal(data.data, item.data,
+ "Clicked item data should be correct");
+ test.done();
+ },
+ items: [submenu],
+ context: loader.cm.SelectorContext("a")
+ });
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#span-link", function (popup) {
+ test.checkMenu([topMenu], [], []);
+ let topMenuElt = test.getItemElt(popup, topMenu);
+ let topMenuPopup = topMenuElt.firstChild;
+ let submenuElt = test.getItemElt(topMenuPopup, submenu);
+ let submenuPopup = submenuElt.firstChild;
+ let itemElt = test.getItemElt(submenuPopup, item);
+ itemElt.click();
+ });
+ });
+};
+
+// Click listeners should work when multiple modules are loaded.
+exports.testItemClickMultipleModules = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader0 = test.newLoader();
+ let loader1 = test.newLoader();
+
+ let item0 = loader0.cm.Item({
+ label: "loader 0 item",
+ contentScript: 'self.on("click", self.postMessage);',
+ onMessage: function () {
+ test.fail("loader 0 item should not emit click event");
+ }
+ });
+ let item1 = loader1.cm.Item({
+ label: "loader 1 item",
+ contentScript: 'self.on("click", self.postMessage);',
+ onMessage: function () {
+ test.pass("loader 1 item clicked as expected");
+ test.done();
+ }
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item0, item1], [], []);
+ let item1Elt = test.getItemElt(popup, item1);
+ item1Elt.click();
+ });
+};
+
+
+// Adding a separator to a submenu should work OK.
+exports.testSeparator = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let menu = new loader.cm.Menu({
+ label: "submenu",
+ items: [new loader.cm.Separator()]
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([menu], [], []);
+ test.done();
+ });
+};
+
+
+// The parentMenu option should work
+exports.testParentMenu = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let menu = new loader.cm.Menu({
+ label: "submenu",
+ items: [loader.cm.Item({ label: "item 1" })],
+ parentMenu: loader.cm.contentContextMenu
+ });
+
+ let item = loader.cm.Item({
+ label: "item 2",
+ parentMenu: menu,
+ });
+
+ assert.equal(menu.items[1], item, "Item should be in the sub menu");
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([menu], [], []);
+ test.done();
+ });
+};
+
+
+// Existing context menu modifications should apply to new windows.
+exports.testNewWindow = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({ label: "item" });
+
+ test.withNewWindow(function () {
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ test.done();
+ });
+ });
+};
+
+
+// When a new window is opened, items added by an unloaded module should not
+// be present in the menu.
+exports.testNewWindowMultipleModules = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+ let item = new loader.cm.Item({ label: "item" });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ popup.hidePopup();
+ loader.unload();
+ test.withNewWindow(function () {
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], [item]);
+ test.done();
+ });
+ });
+ });
+};
+
+
+// Existing context menu modifications should not apply to new private windows.
+exports.testNewPrivateWindow = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({ label: "item" });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ popup.hidePopup();
+
+ test.withNewPrivateWindow(function () {
+ test.showMenu(null, function (popup) {
+ test.checkMenu([], [], []);
+ test.done();
+ });
+ });
+ });
+};
+
+
+// Existing context menu modifications should apply to new private windows when
+// private browsing support is enabled.
+exports.testNewPrivateEnabledWindow = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newPrivateLoader();
+
+ let item = new loader.cm.Item({ label: "item" });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ popup.hidePopup();
+
+ test.withNewPrivateWindow(function () {
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ test.done();
+ });
+ });
+ });
+};
+
+
+// Existing context menu modifications should apply to new private windows when
+// private browsing support is enabled unless unloaded.
+exports.testNewPrivateEnabledWindowUnloaded = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newPrivateLoader();
+
+ let item = new loader.cm.Item({ label: "item" });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ popup.hidePopup();
+
+ loader.unload();
+
+ test.withNewPrivateWindow(function () {
+ test.showMenu(null, function (popup) {
+ test.checkMenu([], [], []);
+ test.done();
+ });
+ });
+ });
+};
+
+
+// Items in the context menu should be sorted according to locale.
+exports.testSorting = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ // Make an unsorted items list. It'll look like this:
+ // item 1, item 0, item 3, item 2, item 5, item 4, ...
+ let items = [];
+ for (let i = 0; i < OVERFLOW_THRESH_DEFAULT; i += 2) {
+ items.push(new loader.cm.Item({ label: "item " + (i + 1) }));
+ items.push(new loader.cm.Item({ label: "item " + i }));
+ }
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+};
+
+
+// Items in the overflow menu should be sorted according to locale.
+exports.testSortingOverflow = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ // Make an unsorted items list. It'll look like this:
+ // item 1, item 0, item 3, item 2, item 5, item 4, ...
+ let items = [];
+ for (let i = 0; i < OVERFLOW_THRESH_DEFAULT * 2; i += 2) {
+ items.push(new loader.cm.Item({ label: "item " + (i + 1) }));
+ items.push(new loader.cm.Item({ label: "item " + i }));
+ }
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+};
+
+
+// Multiple modules shouldn't interfere with sorting.
+exports.testSortingMultipleModules = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader0 = test.newLoader();
+ let loader1 = test.newLoader();
+
+ let items0 = [];
+ let items1 = [];
+ for (let i = 0; i < OVERFLOW_THRESH_DEFAULT; i++) {
+ if (i % 2) {
+ let item = new loader0.cm.Item({ label: "item " + i });
+ items0.push(item);
+ }
+ else {
+ let item = new loader1.cm.Item({ label: "item " + i });
+ items1.push(item);
+ }
+ }
+ let allItems = items0.concat(items1);
+
+ test.showMenu(null, function (popup) {
+
+ // All items should be present and sorted.
+ test.checkMenu(allItems, [], []);
+ popup.hidePopup();
+ loader0.unload();
+ loader1.unload();
+ test.showMenu(null, function (popup) {
+
+ // All items should be removed.
+ test.checkMenu(allItems, [], allItems);
+ test.done();
+ });
+ });
+};
+
+
+// Content click handlers and context handlers should be able to communicate,
+// i.e., they're eval'ed in the same worker and sandbox.
+exports.testContentCommunication = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({
+ label: "item",
+ contentScript: 'var potato;' +
+ 'self.on("context", function () {' +
+ ' potato = "potato";' +
+ ' return true;' +
+ '});' +
+ 'self.on("click", function () {' +
+ ' self.postMessage(potato);' +
+ '});',
+ });
+
+ item.on("message", function (data) {
+ assert.equal(data, "potato", "That's a lot of potatoes!");
+ test.done();
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ let elt = test.getItemElt(popup, item);
+ elt.click();
+ });
+};
+
+
+// When the context menu is invoked on a tab that was already open when the
+// module was loaded, it should contain the expected items and content workers
+// should function as expected.
+exports.testLoadWithOpenTab = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ test.withTestDoc(function (window, doc) {
+ let loader = test.newLoader();
+ let item = new loader.cm.Item({
+ label: "item",
+ contentScript:
+ 'self.on("click", () => self.postMessage("click"));',
+ onMessage: function (msg) {
+ if (msg === "click")
+ test.done();
+ }
+ });
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ test.getItemElt(popup, item).click();
+ });
+ });
+};
+
+// Bug 732716: Ensure that the node given in `click` event works fine
+// (i.e. is correctly wrapped)
+exports.testDrawImageOnClickNode = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ test.withTestDoc(function (window, doc) {
+ let loader = test.newLoader();
+ let item = new loader.cm.Item({
+ label: "item",
+ context: loader.cm.SelectorContext("img"),
+ contentScript: "new " + function() {
+ self.on("click", function (img, data) {
+ let ctx = document.createElement("canvas").getContext("2d");
+ ctx.drawImage(img, 1, 1, 1, 1);
+ self.postMessage("done");
+ });
+ },
+ onMessage: function (msg) {
+ if (msg === "done")
+ test.done();
+ }
+ });
+ test.showMenu("#image", function (popup) {
+ test.checkMenu([item], [], []);
+ test.getItemElt(popup, item).click();
+ });
+ });
+};
+
+
+// Setting an item's label before the menu is ever shown should correctly change
+// its label.
+exports.testSetLabelBeforeShow = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [
+ new loader.cm.Item({ label: "a" }),
+ new loader.cm.Item({ label: "b" })
+ ]
+ items[0].label = "z";
+ assert.equal(items[0].label, "z");
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+};
+
+
+// Setting an item's label after the menu is shown should correctly change its
+// label.
+exports.testSetLabelAfterShow = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [
+ new loader.cm.Item({ label: "a" }),
+ new loader.cm.Item({ label: "b" })
+ ];
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, [], []);
+ popup.hidePopup();
+
+ items[0].label = "z";
+ assert.equal(items[0].label, "z");
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+
+// Setting an item's label before the menu is ever shown should correctly change
+// its label.
+exports.testSetLabelBeforeShowOverflow = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let prefs = loader.loader.require("sdk/preferences/service");
+ prefs.set(OVERFLOW_THRESH_PREF, 0);
+
+ let items = [
+ new loader.cm.Item({ label: "a" }),
+ new loader.cm.Item({ label: "b" })
+ ]
+ items[0].label = "z";
+ assert.equal(items[0].label, "z");
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+};
+
+
+// Setting an item's label after the menu is shown should correctly change its
+// label.
+exports.testSetLabelAfterShowOverflow = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let prefs = loader.loader.require("sdk/preferences/service");
+ prefs.set(OVERFLOW_THRESH_PREF, 0);
+
+ let items = [
+ new loader.cm.Item({ label: "a" }),
+ new loader.cm.Item({ label: "b" })
+ ];
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, [], []);
+ popup.hidePopup();
+
+ items[0].label = "z";
+ assert.equal(items[0].label, "z");
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+
+// Setting the label of an item in a Menu should work.
+exports.testSetLabelMenuItem = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let menu = loader.cm.Menu({
+ label: "menu",
+ items: [loader.cm.Item({ label: "a" })]
+ });
+ menu.items[0].label = "z";
+
+ assert.equal(menu.items[0].label, "z");
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([menu], [], []);
+ test.done();
+ });
+};
+
+
+// Menu.addItem() should work.
+exports.testMenuAddItem = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let menu = loader.cm.Menu({
+ label: "menu",
+ items: [
+ loader.cm.Item({ label: "item 0" })
+ ]
+ });
+ menu.addItem(loader.cm.Item({ label: "item 1" }));
+ menu.addItem(loader.cm.Item({ label: "item 2" }));
+
+ assert.equal(menu.items.length, 3,
+ "menu should have correct number of items");
+ for (let i = 0; i < 3; i++) {
+ assert.equal(menu.items[i].label, "item " + i,
+ "item label should be correct");
+ assert.equal(menu.items[i].parentMenu, menu,
+ "item's parent menu should be correct");
+ }
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([menu], [], []);
+ test.done();
+ });
+};
+
+
+// Adding the same item twice to a menu should work as expected.
+exports.testMenuAddItemTwice = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let menu = loader.cm.Menu({
+ label: "menu",
+ items: []
+ });
+ let subitem = loader.cm.Item({ label: "item 1" })
+ menu.addItem(subitem);
+ menu.addItem(loader.cm.Item({ label: "item 0" }));
+ menu.addItem(subitem);
+
+ assert.equal(menu.items.length, 2,
+ "menu should have correct number of items");
+ for (let i = 0; i < 2; i++) {
+ assert.equal(menu.items[i].label, "item " + i,
+ "item label should be correct");
+ }
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([menu], [], []);
+ test.done();
+ });
+};
+
+
+// Menu.removeItem() should work.
+exports.testMenuRemoveItem = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let subitem = loader.cm.Item({ label: "item 1" });
+ let menu = loader.cm.Menu({
+ label: "menu",
+ items: [
+ loader.cm.Item({ label: "item 0" }),
+ subitem,
+ loader.cm.Item({ label: "item 2" })
+ ]
+ });
+
+ // Removing twice should be harmless.
+ menu.removeItem(subitem);
+ menu.removeItem(subitem);
+
+ assert.equal(subitem.parentMenu, null,
+ "item's parent menu should be correct");
+
+ assert.equal(menu.items.length, 2,
+ "menu should have correct number of items");
+ assert.equal(menu.items[0].label, "item 0",
+ "item label should be correct");
+ assert.equal(menu.items[1].label, "item 2",
+ "item label should be correct");
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([menu], [], []);
+ test.done();
+ });
+};
+
+
+// Adding an item currently contained in one menu to another menu should work.
+exports.testMenuItemSwap = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let subitem = loader.cm.Item({ label: "item" });
+ let menu0 = loader.cm.Menu({
+ label: "menu 0",
+ items: [subitem]
+ });
+ let menu1 = loader.cm.Menu({
+ label: "menu 1",
+ items: []
+ });
+ menu1.addItem(subitem);
+
+ assert.equal(menu0.items.length, 0,
+ "menu should have correct number of items");
+
+ assert.equal(menu1.items.length, 1,
+ "menu should have correct number of items");
+ assert.equal(menu1.items[0].label, "item",
+ "item label should be correct");
+
+ assert.equal(subitem.parentMenu, menu1,
+ "item's parent menu should be correct");
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([menu0, menu1], [menu0], []);
+ test.done();
+ });
+};
+
+
+// Destroying an item should remove it from its parent menu.
+exports.testMenuItemDestroy = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let subitem = loader.cm.Item({ label: "item" });
+ let menu = loader.cm.Menu({
+ label: "menu",
+ items: [subitem]
+ });
+ subitem.destroy();
+
+ assert.equal(menu.items.length, 0,
+ "menu should have correct number of items");
+ assert.equal(subitem.parentMenu, null,
+ "item's parent menu should be correct");
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([menu], [menu], []);
+ test.done();
+ });
+};
+
+
+// Setting Menu.items should work.
+exports.testMenuItemsSetter = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let menu = loader.cm.Menu({
+ label: "menu",
+ items: [
+ loader.cm.Item({ label: "old item 0" }),
+ loader.cm.Item({ label: "old item 1" })
+ ]
+ });
+ menu.items = [
+ loader.cm.Item({ label: "new item 0" }),
+ loader.cm.Item({ label: "new item 1" }),
+ loader.cm.Item({ label: "new item 2" })
+ ];
+
+ assert.equal(menu.items.length, 3,
+ "menu should have correct number of items");
+ for (let i = 0; i < 3; i++) {
+ assert.equal(menu.items[i].label, "new item " + i,
+ "item label should be correct");
+ assert.equal(menu.items[i].parentMenu, menu,
+ "item's parent menu should be correct");
+ }
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([menu], [], []);
+ test.done();
+ });
+};
+
+
+// Setting Item.data should work.
+exports.testItemDataSetter = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = loader.cm.Item({ label: "old item 0", data: "old" });
+ item.data = "new";
+
+ assert.equal(item.data, "new", "item should have correct data");
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item], [], []);
+ test.done();
+ });
+};
+
+
+// Open the test doc, load the module, make sure items appear when context-
+// clicking the iframe.
+exports.testAlreadyOpenIframe = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ test.withTestDoc(function (window, doc) {
+ let loader = test.newLoader();
+ let item = new loader.cm.Item({
+ label: "item"
+ });
+ test.showMenu("#iframe", function (popup) {
+ test.checkMenu([item], [], []);
+ test.done();
+ });
+ });
+};
+
+
+// Tests that a missing label throws an exception
+exports.testItemNoLabel = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ try {
+ new loader.cm.Item({});
+ assert.ok(false, "Should have seen exception");
+ }
+ catch (e) {
+ assert.ok(true, "Should have seen exception");
+ }
+
+ try {
+ new loader.cm.Item({ label: null });
+ assert.ok(false, "Should have seen exception");
+ }
+ catch (e) {
+ assert.ok(true, "Should have seen exception");
+ }
+
+ try {
+ new loader.cm.Item({ label: undefined });
+ assert.ok(false, "Should have seen exception");
+ }
+ catch (e) {
+ assert.ok(true, "Should have seen exception");
+ }
+
+ try {
+ new loader.cm.Item({ label: "" });
+ assert.ok(false, "Should have seen exception");
+ }
+ catch (e) {
+ assert.ok(true, "Should have seen exception");
+ }
+
+ test.done();
+}
+
+/* bug 1302854 - disabled this subtest as it is intermittent
+// Tests that items can have an empty data property
+exports.testItemNoData = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ function checkData(data) {
+ assert.equal(data, undefined, "Data should be undefined");
+ }
+
+ let item1 = new loader.cm.Item({
+ label: "item 1",
+ contentScript: 'self.on("click", (node, data) => self.postMessage(data))',
+ onMessage: checkData
+ });
+ let item2 = new loader.cm.Item({
+ label: "item 2",
+ data: null,
+ contentScript: 'self.on("click", (node, data) => self.postMessage(data))',
+ onMessage: checkData
+ });
+ let item3 = new loader.cm.Item({
+ label: "item 3",
+ data: undefined,
+ contentScript: 'self.on("click", (node, data) => self.postMessage(data))',
+ onMessage: checkData
+ });
+
+ assert.equal(item1.data, undefined, "Should be no defined data");
+ assert.equal(item2.data, null, "Should be no defined data");
+ assert.equal(item3.data, undefined, "Should be no defined data");
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item1, item2, item3], [], []);
+
+ let itemElt = test.getItemElt(popup, item1);
+ itemElt.click();
+
+ test.hideMenu(function() {
+ test.showMenu(null, function (popup) {
+ let itemElt = test.getItemElt(popup, item2);
+ itemElt.click();
+
+ test.hideMenu(function() {
+ test.showMenu(null, function (popup) {
+ let itemElt = test.getItemElt(popup, item3);
+ itemElt.click();
+
+ test.done();
+ });
+ });
+ });
+ });
+ });
+}
+*/
+
+
+exports.testItemNoAccessKey = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item1 = new loader.cm.Item({ label: "item 1" });
+ let item2 = new loader.cm.Item({ label: "item 2", accesskey: null });
+ let item3 = new loader.cm.Item({ label: "item 3", accesskey: undefined });
+
+ assert.equal(item1.accesskey, undefined, "Should be no defined image");
+ assert.equal(item2.accesskey, null, "Should be no defined image");
+ assert.equal(item3.accesskey, undefined, "Should be no defined image");
+
+ test.showMenu().
+ then((popup) => test.checkMenu([item1, item2, item3], [], [])).
+ then(test.done).
+ catch(assert.fail);
+}
+
+
+// Test accesskey support.
+exports.testItemAccessKey = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item = new loader.cm.Item({ label: "item", accesskey: "i" });
+ assert.equal(item.accesskey, "i", "Should have set the image to i");
+
+ let menu = new loader.cm.Menu({ label: "menu", accesskey: "m", items: [
+ loader.cm.Item({ label: "subitem" })
+ ]});
+ assert.equal(menu.accesskey, "m", "Should have set the accesskey to m");
+
+ test.showMenu().then((popup) => {
+ test.checkMenu([item, menu], [], []);
+
+ let accesskey = "e";
+ menu.accesskey = item.accesskey = accesskey;
+ assert.equal(item.accesskey, accesskey, "Should have set the accesskey to " + accesskey);
+ assert.equal(menu.accesskey, accesskey, "Should have set the accesskey to " + accesskey);
+ test.checkMenu([item, menu], [], []);
+
+ item.accesskey = null;
+ menu.accesskey = null;
+ assert.equal(item.accesskey, null, "Should have set the accesskey to " + accesskey);
+ assert.equal(menu.accesskey, null, "Should have set the accesskey to " + accesskey);
+ test.checkMenu([item, menu], [], []);
+ }).
+ then(test.done).
+ catch(assert.fail);
+};
+
+
+// Tests that items without an image don't attempt to show one
+exports.testItemNoImage = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let item1 = new loader.cm.Item({ label: "item 1" });
+ let item2 = new loader.cm.Item({ label: "item 2", image: null });
+ let item3 = new loader.cm.Item({ label: "item 3", image: undefined });
+
+ assert.equal(item1.image, undefined, "Should be no defined image");
+ assert.equal(item2.image, null, "Should be no defined image");
+ assert.equal(item3.image, undefined, "Should be no defined image");
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item1, item2, item3], [], []);
+
+ test.done();
+ });
+}
+
+
+// Test image support.
+exports.testItemImage = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let imageURL = data.url("moz_favicon.ico");
+ let item = new loader.cm.Item({ label: "item", image: imageURL });
+ let menu = new loader.cm.Menu({ label: "menu", image: imageURL, items: [
+ loader.cm.Item({ label: "subitem" })
+ ]});
+ assert.equal(item.image, imageURL, "Should have set the image correctly");
+ assert.equal(menu.image, imageURL, "Should have set the image correctly");
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([item, menu], [], []);
+
+ let imageURL2 = data.url("dummy.ico");
+ item.image = imageURL2;
+ menu.image = imageURL2;
+ assert.equal(item.image, imageURL2, "Should have set the image correctly");
+ assert.equal(menu.image, imageURL2, "Should have set the image correctly");
+ test.checkMenu([item, menu], [], []);
+
+ item.image = null;
+ menu.image = null;
+ assert.equal(item.image, null, "Should have set the image correctly");
+ assert.equal(menu.image, null, "Should have set the image correctly");
+ test.checkMenu([item, menu], [], []);
+
+ test.done();
+ });
+};
+
+// Test image URL validation.
+exports.testItemImageValidURL = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ assert.throws(function(){
+ new loader.cm.Item({
+ label: "item 1",
+ image: "foo"
+ })
+ }, /Image URL validation failed/
+ );
+
+ assert.throws(function(){
+ new loader.cm.Item({
+ label: "item 2",
+ image: false
+ })
+ }, /Image URL validation failed/
+ );
+
+ assert.throws(function(){
+ new loader.cm.Item({
+ label: "item 3",
+ image: 0
+ })
+ }, /Image URL validation failed/
+ );
+
+ let imageURL = data.url("moz_favicon.ico");
+ let item4 = new loader.cm.Item({ label: "item 4", image: imageURL });
+ let item5 = new loader.cm.Item({ label: "item 5", image: null });
+ let item6 = new loader.cm.Item({ label: "item 6", image: undefined });
+
+ assert.equal(item4.image, imageURL, "Should be proper image URL");
+ assert.equal(item5.image, null, "Should be null image");
+ assert.equal(item6.image, undefined, "Should be undefined image");
+
+ test.done();
+};
+
+
+// Menu.destroy should destroy the item tree rooted at that menu.
+exports.testMenuDestroy = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let menu = loader.cm.Menu({
+ label: "menu",
+ items: [
+ loader.cm.Item({ label: "item 0" }),
+ loader.cm.Menu({
+ label: "item 1",
+ items: [
+ loader.cm.Item({ label: "subitem 0" }),
+ loader.cm.Item({ label: "subitem 1" }),
+ loader.cm.Item({ label: "subitem 2" })
+ ]
+ }),
+ loader.cm.Item({ label: "item 2" })
+ ]
+ });
+ menu.destroy();
+
+ /*let numRegistryEntries = 0;
+ loader.globalScope.browserManager.browserWins.forEach(function (bwin) {
+ for (let itemID in bwin.items)
+ numRegistryEntries++;
+ });
+ assert.equal(numRegistryEntries, 0, "All items should be unregistered.");*/
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([menu], [], [menu]);
+ test.done();
+ });
+};
+
+// Checks that if a menu contains sub items that are hidden then the menu is
+// hidden too. Also checks that content scripts and contexts work for sub items.
+exports.testSubItemContextNoMatchHideMenu = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [
+ loader.cm.Menu({
+ label: "menu 1",
+ items: [
+ loader.cm.Item({
+ label: "subitem 1",
+ context: loader.cm.SelectorContext(".foo")
+ })
+ ]
+ }),
+ loader.cm.Menu({
+ label: "menu 2",
+ items: [
+ loader.cm.Item({
+ label: "subitem 2",
+ contentScript: 'self.on("context", () => false);'
+ })
+ ]
+ }),
+ loader.cm.Menu({
+ label: "menu 3",
+ items: [
+ loader.cm.Item({
+ label: "subitem 3",
+ context: loader.cm.SelectorContext(".foo")
+ }),
+ loader.cm.Item({
+ label: "subitem 4",
+ contentScript: 'self.on("context", () => false);'
+ })
+ ]
+ })
+ ];
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, items, []);
+ test.done();
+ });
+};
+
+
+// Checks that if a menu contains a combination of hidden and visible sub items
+// then the menu is still visible too.
+exports.testSubItemContextMatch = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let hiddenItems = [
+ loader.cm.Item({
+ label: "subitem 3",
+ context: loader.cm.SelectorContext(".foo")
+ }),
+ loader.cm.Item({
+ label: "subitem 6",
+ contentScript: 'self.on("context", () => false);'
+ })
+ ];
+
+ let items = [
+ loader.cm.Menu({
+ label: "menu 1",
+ items: [
+ loader.cm.Item({
+ label: "subitem 1",
+ context: loader.cm.URLContext(TEST_DOC_URL)
+ })
+ ]
+ }),
+ loader.cm.Menu({
+ label: "menu 2",
+ items: [
+ loader.cm.Item({
+ label: "subitem 2",
+ contentScript: 'self.on("context", () => true);'
+ })
+ ]
+ }),
+ loader.cm.Menu({
+ label: "menu 3",
+ items: [
+ hiddenItems[0],
+ loader.cm.Item({
+ label: "subitem 4",
+ contentScript: 'self.on("context", () => true);'
+ })
+ ]
+ }),
+ loader.cm.Menu({
+ label: "menu 4",
+ items: [
+ loader.cm.Item({
+ label: "subitem 5",
+ context: loader.cm.URLContext(TEST_DOC_URL)
+ }),
+ hiddenItems[1]
+ ]
+ }),
+ loader.cm.Menu({
+ label: "menu 5",
+ items: [
+ loader.cm.Item({
+ label: "subitem 7",
+ context: loader.cm.URLContext(TEST_DOC_URL)
+ }),
+ loader.cm.Item({
+ label: "subitem 8",
+ contentScript: 'self.on("context", () => true);'
+ })
+ ]
+ })
+ ];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, hiddenItems, []);
+ test.done();
+ });
+ });
+};
+
+
+// Child items should default to visible, not to PageContext
+exports.testSubItemDefaultVisible = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [
+ loader.cm.Menu({
+ label: "menu 1",
+ context: loader.cm.SelectorContext("img"),
+ items: [
+ loader.cm.Item({
+ label: "subitem 1"
+ }),
+ loader.cm.Item({
+ label: "subitem 2",
+ context: loader.cm.SelectorContext("img")
+ }),
+ loader.cm.Item({
+ label: "subitem 3",
+ context: loader.cm.SelectorContext("a")
+ })
+ ]
+ })
+ ];
+
+ // subitem 3 will be hidden
+ let hiddenItems = [items[0].items[2]];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#image", function (popup) {
+ test.checkMenu(items, hiddenItems, []);
+ test.done();
+ });
+ });
+};
+
+// Tests that the click event on sub menuitem
+// tiggers the click event for the sub menuitem and the parent menu
+exports.testSubItemClick = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let state = 0;
+
+ let items = [
+ loader.cm.Menu({
+ label: "menu 1",
+ items: [
+ loader.cm.Item({
+ label: "subitem 1",
+ data: "foobar",
+ contentScript: 'self.on("click", function (node, data) {' +
+ ' self.postMessage({' +
+ ' tagName: node.tagName,' +
+ ' data: data' +
+ ' });' +
+ '});',
+ onMessage: function(msg) {
+ assert.equal(msg.tagName, "HTML", "should have seen the right node");
+ assert.equal(msg.data, "foobar", "should have seen the right data");
+ assert.equal(state, 0, "should have seen the event at the right time");
+ state++;
+ }
+ })
+ ],
+ contentScript: 'self.on("click", function (node, data) {' +
+ ' self.postMessage({' +
+ ' tagName: node.tagName,' +
+ ' data: data' +
+ ' });' +
+ '});',
+ onMessage: function(msg) {
+ assert.equal(msg.tagName, "HTML", "should have seen the right node");
+ assert.equal(msg.data, "foobar", "should have seen the right data");
+ assert.equal(state, 1, "should have seen the event at the right time");
+
+ test.done();
+ }
+ })
+ ];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, [], []);
+
+ let topMenuElt = test.getItemElt(popup, items[0]);
+ let topMenuPopup = topMenuElt.firstChild;
+ let itemElt = test.getItemElt(topMenuPopup, items[0].items[0]);
+ itemElt.click();
+ });
+ });
+};
+
+// Tests that the command event on sub menuitem
+// tiggers the click event for the sub menuitem and the parent menu
+exports.testSubItemCommand = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let state = 0;
+
+ let items = [
+ loader.cm.Menu({
+ label: "menu 1",
+ items: [
+ loader.cm.Item({
+ label: "subitem 1",
+ data: "foobar",
+ contentScript: 'self.on("click", function (node, data) {' +
+ ' self.postMessage({' +
+ ' tagName: node.tagName,' +
+ ' data: data' +
+ ' });' +
+ '});',
+ onMessage: function(msg) {
+ assert.equal(msg.tagName, "HTML", "should have seen the right node");
+ assert.equal(msg.data, "foobar", "should have seen the right data");
+ assert.equal(state, 0, "should have seen the event at the right time");
+ state++;
+ }
+ })
+ ],
+ contentScript: 'self.on("click", function (node, data) {' +
+ ' self.postMessage({' +
+ ' tagName: node.tagName,' +
+ ' data: data' +
+ ' });' +
+ '});',
+ onMessage: function(msg) {
+ assert.equal(msg.tagName, "HTML", "should have seen the right node");
+ assert.equal(msg.data, "foobar", "should have seen the right data");
+ assert.equal(state, 1, "should have seen the event at the right time");
+ state++
+
+ test.done();
+ }
+ })
+ ];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, [], []);
+
+ let topMenuElt = test.getItemElt(popup, items[0]);
+ let topMenuPopup = topMenuElt.firstChild;
+ let itemElt = test.getItemElt(topMenuPopup, items[0].items[0]);
+
+ // create a command event
+ let evt = itemElt.ownerDocument.createEvent('Event');
+ evt.initEvent('command', true, true);
+ itemElt.dispatchEvent(evt);
+ });
+ });
+};
+
+// Tests that opening a context menu for an outer frame when an inner frame
+// has a selection doesn't activate the SelectionContext
+exports.testSelectionInInnerFrameNoMatch = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let state = 0;
+
+ let items = [
+ loader.cm.Item({
+ label: "test item",
+ context: loader.cm.SelectionContext()
+ })
+ ];
+
+ test.withTestDoc(function (window, doc) {
+ let frame = doc.getElementById("iframe");
+ frame.contentWindow.getSelection().selectAllChildren(frame.contentDocument.body);
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, items, []);
+ test.done();
+ });
+ });
+};
+
+// Tests that opening a context menu for an inner frame when the inner frame
+// has a selection does activate the SelectionContext
+exports.testSelectionInInnerFrameMatch = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let state = 0;
+
+ let items = [
+ loader.cm.Item({
+ label: "test item",
+ context: loader.cm.SelectionContext()
+ })
+ ];
+
+ test.withTestDoc(function (window, doc) {
+ let frame = doc.getElementById("iframe");
+ frame.contentWindow.getSelection().selectAllChildren(frame.contentDocument.body);
+
+ test.showMenu(["#iframe", "#text"], function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+// Tests that opening a context menu for an inner frame when the outer frame
+// has a selection doesn't activate the SelectionContext
+exports.testSelectionInOuterFrameNoMatch = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let state = 0;
+
+ let items = [
+ loader.cm.Item({
+ label: "test item",
+ context: loader.cm.SelectionContext()
+ })
+ ];
+
+ test.withTestDoc(function (window, doc) {
+ let frame = doc.getElementById("iframe");
+ window.getSelection().selectAllChildren(doc.body);
+
+ test.showMenu(["#iframe", "#text"], function (popup) {
+ test.checkMenu(items, items, []);
+ test.done();
+ });
+ });
+};
+
+
+// Test that the return value of the predicate function determines if
+// item is shown
+exports.testPredicateContextControl = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let itemTrue = loader.cm.Item({
+ label: "visible",
+ context: loader.cm.PredicateContext(function () { return true; })
+ });
+
+ let itemFalse = loader.cm.Item({
+ label: "hidden",
+ context: loader.cm.PredicateContext(function () { return false; })
+ });
+
+ test.showMenu(null, function (popup) {
+ test.checkMenu([itemTrue, itemFalse], [itemFalse], []);
+ test.done();
+ });
+};
+
+// Test that the data object has the correct document type
+exports.testPredicateContextDocumentType = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.equal(data.documentType, 'text/html');
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+// Test that the data object has the correct document URL
+exports.testPredicateContextDocumentURL = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.equal(data.documentURL, TEST_DOC_URL);
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+
+// Test that the data object has the correct element name
+exports.testPredicateContextTargetName = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.targetName, "input");
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#button", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+
+// Test that the data object has the correct ID
+exports.testPredicateContextTargetIDSet = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.targetID, "button");
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#button", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+// Test that the data object has the correct ID
+exports.testPredicateContextTargetIDNotSet = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.targetID, null);
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu(".predicate-test-a", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+// Test that the data object is showing editable correctly for regular text inputs
+exports.testPredicateContextTextBoxIsEditable = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.isEditable, true);
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#textbox", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+// Test that the data object is showing editable correctly for readonly text inputs
+exports.testPredicateContextReadonlyTextBoxIsNotEditable = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.isEditable, false);
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#readonly-textbox", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+// Test that the data object is showing editable correctly for disabled text inputs
+exports.testPredicateContextDisabledTextBoxIsNotEditable = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.isEditable, false);
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#disabled-textbox", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+// Test that the data object is showing editable correctly for text areas
+exports.testPredicateContextTextAreaIsEditable = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.isEditable, true);
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#textfield", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+// Test that non-text inputs are not considered editable
+exports.testPredicateContextButtonIsNotEditable = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.isEditable, false);
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#button", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+
+// Test that the data object is showing editable correctly
+exports.testPredicateContextNonInputIsNotEditable = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.isEditable, false);
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#image", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+
+// Test that the data object is showing editable correctly for HTML contenteditable elements
+exports.testPredicateContextEditableElement = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.isEditable, true);
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#editable", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+
+// Test that the data object does not have a selection when there is none
+exports.testPredicateContextNoSelectionInPage = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.selectionText, null);
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+// Test that the data object includes the selected page text
+exports.testPredicateContextSelectionInPage = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ // since we might get whitespace
+ assert.ok(data.selectionText && data.selectionText.search(/^\s*Some text.\s*$/) != -1,
+ 'Expected "Some text.", got "' + data.selectionText + '"');
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ window.getSelection().selectAllChildren(doc.getElementById("text"));
+ test.showMenu(null, function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+// Test that the data object includes the selected input text
+exports.testPredicateContextSelectionInTextBox = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ // since we might get whitespace
+ assert.strictEqual(data.selectionText, "t v");
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ let textbox = doc.getElementById("textbox");
+ test.selectRange("#textbox", 3, 6);
+ test.showMenu("#textbox", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+// Test that the data object has the correct src for an image
+exports.testPredicateContextTargetSrcSet = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+ let image;
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.srcURL, image.src);
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ image = doc.getElementById("image");
+ test.showMenu("#image", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+// Test that the data object has no src for a link
+exports.testPredicateContextTargetSrcNotSet = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.srcURL, null);
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#link", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+
+// Test that the data object has the correct link set
+exports.testPredicateContextTargetLinkSet = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+ let image;
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.linkURL, TEST_DOC_URL + "#test");
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu(".predicate-test-a", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+// Test that the data object has no link for an image
+exports.testPredicateContextTargetLinkNotSet = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.linkURL, null);
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#image", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+// Test that the data object has the correct link for a nested image
+exports.testPredicateContextTargetLinkSetNestedImage = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.linkURL, TEST_DOC_URL + "#nested-image");
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#predicate-test-nested-image", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+// Test that the data object has the correct link for a complex nested structure
+exports.testPredicateContextTargetLinkSetNestedStructure = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.linkURL, TEST_DOC_URL + "#nested-structure");
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#predicate-test-nested-structure", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+// Test that the data object has the value for an input textbox
+exports.testPredicateContextTargetValueSet = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+ let image;
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.value, "test value");
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#textbox", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+// Test that the data object has no value for an image
+exports.testPredicateContextTargetValueNotSet = function (assert, done) {
+ let test = new TestHelper(assert, done);
+ let loader = test.newLoader();
+
+ let items = [loader.cm.Item({
+ label: "item",
+ context: loader.cm.PredicateContext(function (data) {
+ assert.strictEqual(data.value, null);
+ return true;
+ })
+ })];
+
+ test.withTestDoc(function (window, doc) {
+ test.showMenu("#image", function (popup) {
+ test.checkMenu(items, [], []);
+ test.done();
+ });
+ });
+};
+
+if (isTravisCI) {
+ module.exports = {
+ "test skip on jpm": (assert) => assert.pass("skipping this file with jpm")
+ };
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-context-menu@2.js b/addon-sdk/source/test/test-context-menu@2.js
new file mode 100644
index 000000000..78c496220
--- /dev/null
+++ b/addon-sdk/source/test/test-context-menu@2.js
@@ -0,0 +1,1350 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Cc, Ci } = require("chrome");
+const {openWindow, closeWindow, openTab, closeTab,
+ openContextMenu, closeContextMenu, select,
+ readNode, captureContextMenu, withTab, withItems } = require("./context-menu/util");
+const {when} = require("sdk/dom/events");
+const {Item, Menu, Separator, Contexts, Readers } = require("sdk/context-menu@2");
+const prefs = require("sdk/preferences/service");
+const { before, after } = require('sdk/test/utils');
+
+const testPageURI = require.resolve("./test-context-menu").replace(".js", ".html");
+
+const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
+
+const data = input =>
+ `data:text/html;charset=utf-8,${encodeURIComponent(input)}`
+
+const menugroup = (...children) => Object.assign({
+ tagName: "menugroup",
+ namespaceURI: XUL_NS,
+ style: "-moz-box-orient: vertical;",
+ className: "sdk-context-menu-extension"
+}, children.length ? {children} : {});
+
+const menuseparator = () => ({
+ tagName: "menuseparator",
+ namespaceURI: XUL_NS,
+ className: "sdk-context-menu-separator"
+})
+
+const menuitem = properties => Object.assign({
+ tagName: "menuitem",
+ namespaceURI: XUL_NS,
+ className: "sdk-context-menu-item menuitem-iconic"
+}, properties);
+
+const menu = (properties, ...children) => Object.assign({
+ tagName: "menu",
+ namespaceURI: XUL_NS,
+ className: "sdk-context-menu menu-iconic"
+}, properties, {
+ children: [Object.assign({tagName: "menupopup", namespaceURI: XUL_NS},
+ children.length ? {children} : {})]
+});
+
+// Destroying items that were previously created should cause them to be absent
+// from the menu.
+exports["test create / destroy menu item"] = withTab(function*(assert) {
+ const item = new Item({
+ label: "test-1"
+ });
+
+ const before = yield captureContextMenu("h1");
+
+ assert.deepEqual(before,
+ menugroup(menuseparator(),
+ menuitem({label: "test-1"})),
+ "context menu contains separator & added item");
+
+ item.destroy();
+
+ const after = yield captureContextMenu("h1");
+ assert.deepEqual(after, menugroup(),
+ "all items were removed children are present");
+}, data`<h1>hello</h1>`);
+
+
+/* Bug 1115419 - Disable occasionally failing test until we
+ figure out why it fails.
+// Items created should be present on all browser windows.
+exports["test menu item in new window"] = function*(assert) {
+ const isMenuPopulated = function*(tab) {
+ const state = yield captureContextMenu("h1", tab);
+ assert.deepEqual(state,
+ menugroup(menuseparator(),
+ menuitem({label: "multi-window"})),
+ "created menu item is present")
+ };
+
+ const isMenuEmpty = function*(tab) {
+ const state = yield captureContextMenu("h1", tab);
+ assert.deepEqual(state, menugroup(), "no sdk items present");
+ };
+
+ const item = new Item({ label: "multi-window" });
+
+ const tab1 = yield openTab(`data:text/html,<h1>hello</h1>`);
+ yield* isMenuPopulated(tab1);
+
+ const window2 = yield openWindow();
+ assert.pass("window is ready");
+
+ const tab2 = yield openTab(`data:text/html,<h1>hello window-2</h1>`, window2);
+ assert.pass("tab is ready");
+
+ yield* isMenuPopulated(tab2);
+
+ item.destroy();
+
+ yield* isMenuEmpty(tab2);
+ yield closeWindow(window2);
+
+ yield* isMenuEmpty(tab1);
+
+ yield closeTab(tab1);
+};
+*/
+
+
+// Multilpe items can be created and destroyed at different points
+// in time & they should not affect each other.
+exports["test multiple items"] = withTab(function*(assert) {
+ const item1 = new Item({ label: "one" });
+
+ const step1 = yield captureContextMenu("h1");
+ assert.deepEqual(step1,
+ menugroup(menuseparator(),
+ menuitem({label: "one"})),
+ "item1 is present");
+
+ const item2 = new Item({ label: "two" });
+ const step2 = yield captureContextMenu("h1");
+
+ assert.deepEqual(step2,
+ menugroup(menuseparator(),
+ menuitem({label: "one"}),
+ menuitem({label: "two"})),
+ "both items where present");
+
+ item1.destroy();
+
+ const step3 = yield captureContextMenu("h1");
+ assert.deepEqual(step3,
+ menugroup(menuseparator(),
+ menuitem({label: "two"})),
+ "one items left");
+
+ item2.destroy();
+
+ const step4 = yield captureContextMenu("h1");
+ assert.deepEqual(step4, menugroup(), "no items left");
+}, data`<h1>Multiple Items</h1>`);
+
+// Destroying an item twice should not cause an error.
+exports["test destroy twice"] = withTab(function*(assert) {
+ const item = new Item({ label: "destroy" });
+ const withItem = yield captureContextMenu("h2");
+ assert.deepEqual(withItem,
+ menugroup(menuseparator(),
+ menuitem({label:"destroy"})),
+ "Item is added");
+
+ item.destroy();
+
+ const withoutItem = yield captureContextMenu("h2");
+ assert.deepEqual(withoutItem, menugroup(), "Item was removed");
+
+ item.destroy();
+ assert.pass("Destroying an item twice should not cause an error.");
+}, "data:text/html,<h2>item destroy</h2>");
+
+// CSS selector contexts should cause their items to be absent from the menu
+// when the menu is not invoked on nodes that match selectors.
+exports["test selector context"] = withTab(function*(assert) {
+ const item = new Item({
+ context: [new Contexts.Selector("body b")],
+ label: "bold"
+ });
+
+ const match = yield captureContextMenu("b");
+ assert.deepEqual(match,
+ menugroup(menuseparator(),
+ menuitem({label: "bold"})),
+ "item mathched context");
+
+ const noMatch = yield captureContextMenu("i");
+ assert.deepEqual(noMatch, menugroup(), "item did not match context");
+
+ item.destroy();
+
+ const cleared = yield captureContextMenu("b");
+ assert.deepEqual(cleared, menugroup(), "item was removed");
+}, data`<body><i>one</i><b>two</b></body>`);
+
+// CSS selector contexts should cause their items to be absent in the menu
+// when the menu is invoked even on nodes that have ancestors that match the
+// selectors.
+exports["test parent selector don't match children"] = withTab(function*(assert) {
+ const item = new Item({
+ label: "parent match",
+ context: [new Contexts.Selector("a[href]")]
+ });
+
+ const match = yield captureContextMenu("a");
+ assert.deepEqual(match,
+ menugroup(menuseparator(),
+ menuitem({label: "parent match"})),
+ "item mathched context");
+
+ const noMatch = yield captureContextMenu("strong");
+ assert.deepEqual(noMatch, menugroup(), "item did not mathch context");
+
+ item.destroy();
+
+ const destroyed = yield captureContextMenu("a");
+ assert.deepEqual(destroyed, menugroup(), "no items left");
+}, data`<a href='/foo'>This text must be long & <strong>bold!</strong></a>`);
+
+// Page contexts should cause their items to be present in the menu when the
+// menu is not invoked on an active element.
+exports["test page context match"] = withTab(function*(assert) {
+ const isPageMatch = (tree, description="page context matched") =>
+ assert.deepEqual(tree,
+ menugroup(menuseparator(),
+ menuitem({label: "page match"}),
+ menuitem({label: "any match"})),
+ description);
+
+ const isntPageMatch = (tree, description="page context did not match") =>
+ assert.deepEqual(tree,
+ menugroup(menuseparator(),
+ menuitem({label: "any match"})),
+ description);
+
+ yield* withItems({
+ pageMatch: new Item({
+ label: "page match",
+ context: [new Contexts.Page()],
+ }),
+ anyMatch: new Item({
+ label: "any match"
+ })
+ }, function*({pageMatch, anyMatch}) {
+ for (let tagName of [null, "p", "h3"]) {
+ isPageMatch((yield captureContextMenu(tagName)),
+ `Page context matches ${tagName} passive element`);
+ }
+
+ for (let tagName of ["button", "canvas", "img", "input", "textarea",
+ "select", "menu", "embed" ,"object", "video", "audio",
+ "applet"])
+ {
+ isntPageMatch((yield captureContextMenu(tagName)),
+ `Page context does not match <${tagName}/> active element`);
+ }
+
+ for (let selector of ["span"])
+ {
+ isntPageMatch((yield captureContextMenu(selector)),
+ `Page context does not match decedents of active element`);
+ }
+ });
+},
+data`<head>
+ <style>
+ p, object, embed { display: inline-block; }
+ </style>
+</head>
+<body>
+ <div><p>paragraph</p></div>
+ <div><a href=./link><span>link</span></a></div>
+ <h3>hi</h3>
+ <div><button>button</button></div>
+ <div><canvas height=10 /></div>
+ <div><img height=10 width=10 /></div>
+ <div><input value=input /></div>
+ <div><textarea>text</textarea></div>
+ <div><select><option>one</option><option>two</option></select></div>
+ <div><menu><button>item</button></menu></div>
+ <div><object width=10 height=10><param name=foo value=bar /></object></div>
+ <div><embed width=10 height=10/></div>
+ <div><video width=10 height=10 controls /></div>
+ <div><audio width=10 height=10 controls /></div>
+ <div><applet width=10 height=10 /></div>
+</body>`);
+
+// Page context does not match if if there is a selection.
+exports["test page context doesn't match on selection"] = withTab(function*(assert) {
+ const isPageMatch = (tree, description="page context matched") =>
+ assert.deepEqual(tree,
+ menugroup(menuseparator(),
+ menuitem({label: "page match"}),
+ menuitem({label: "any match"})),
+ description);
+
+ const isntPageMatch = (tree, description="page context did not match") =>
+ assert.deepEqual(tree,
+ menugroup(menuseparator(),
+ menuitem({label: "any match"})),
+ description);
+
+ yield* withItems({
+ pageMatch: new Item({
+ label: "page match",
+ context: [new Contexts.Page()],
+ }),
+ anyMatch: new Item({
+ label: "any match"
+ })
+ }, function*({pageMatch, anyMatch}) {
+ yield select("b");
+ isntPageMatch((yield captureContextMenu("i")),
+ "page context does not match if there is a selection");
+
+ yield select(null);
+ isPageMatch((yield captureContextMenu("i")),
+ "page context match if there is no selection");
+ });
+}, data`<body><i>one</i><b>two</b></body>`);
+
+exports["test selection context"] = withTab(function*(assert) {
+ yield* withItems({
+ item: new Item({
+ label: "selection",
+ context: [new Contexts.Selection()]
+ })
+ }, function*({item}) {
+ assert.deepEqual((yield captureContextMenu()),
+ menugroup(),
+ "item does not match if there is no selection");
+
+ yield select("b");
+
+ assert.deepEqual((yield captureContextMenu()),
+ menugroup(menuseparator(),
+ menuitem({label: "selection"})),
+ "item matches if there is a selection");
+ });
+}, data`<i>one</i><b>two</b>`);
+
+exports["test selection context in textarea"] = withTab(function*(assert) {
+ yield* withItems({
+ item: new Item({
+ label: "selection",
+ context: [new Contexts.Selection()]
+ })
+ }, function*({item}) {
+ assert.deepEqual((yield captureContextMenu()),
+ menugroup(),
+ "does not match if there's no selection");
+
+ yield select({target:"textarea", start:0, end:5});
+
+ assert.deepEqual((yield captureContextMenu("b")),
+ menugroup(),
+ "does not match if target isn't input with selection");
+
+ assert.deepEqual((yield captureContextMenu("textarea")),
+ menugroup(menuseparator(),
+ menuitem({label: "selection"})),
+ "matches if target is input with selected text");
+
+ yield select({target: "textarea", start: 0, end: 0});
+
+ assert.deepEqual((yield captureContextMenu("textarea")),
+ menugroup(),
+ "does not match when selection is cleared");
+ });
+}, data`<textarea>Hello World</textarea><b>!!</b>`);
+
+exports["test url contexts"] = withTab(function*(assert) {
+ yield* withItems({
+ a: new Item({
+ label: "a",
+ context: [new Contexts.URL(testPageURI)]
+ }),
+ b: new Item({
+ label: "b",
+ context: [new Contexts.URL("*.bogus.com")]
+ }),
+ c: new Item({
+ label: "c",
+ context: [new Contexts.URL("*.bogus.com"),
+ new Contexts.URL(testPageURI)]
+ }),
+ d: new Item({
+ label: "d",
+ context: [new Contexts.URL(/.*\.html/)]
+ }),
+ e: new Item({
+ label: "e",
+ context: [new Contexts.URL("http://*"),
+ new Contexts.URL(testPageURI)]
+ }),
+ f: new Item({
+ label: "f",
+ context: [new Contexts.URL("http://*").required,
+ new Contexts.URL(testPageURI)]
+ }),
+ }, function*(_) {
+ assert.deepEqual((yield captureContextMenu()),
+ menugroup(menuseparator(),
+ menuitem({label: "a"}),
+ menuitem({label: "c"}),
+ menuitem({label: "d"}),
+ menuitem({label: "e"})),
+ "shows only matching items");
+ });
+}, testPageURI);
+
+exports["test iframe context"] = withTab(function*(assert) {
+ yield* withItems({
+ page: new Item({
+ label: "page",
+ context: [new Contexts.Page()]
+ }),
+ iframe: new Item({
+ label: "iframe",
+ context: [new Contexts.Frame()]
+ }),
+ h2: new Item({
+ label: "element",
+ context: [new Contexts.Selector("*")]
+ })
+ }, function(_) {
+ assert.deepEqual((yield captureContextMenu("iframe")),
+ menugroup(menuseparator(),
+ menuitem({label: "page"}),
+ menuitem({label: "iframe"}),
+ menuitem({label: "element"})),
+ "matching items are present");
+
+ assert.deepEqual((yield captureContextMenu("h1")),
+ menugroup(menuseparator(),
+ menuitem({label: "page"}),
+ menuitem({label: "element"})),
+ "only matching items are present");
+
+ });
+
+},
+data`<h1>hello</h1>
+<iframe src='data:text/html,<body>Bye</body>' />`);
+
+exports["test link context"] = withTab(function*(assert) {
+ yield* withItems({
+ item: new Item({
+ label: "link",
+ context: [new Contexts.Link()]
+ })
+ }, function*(_) {
+ assert.deepEqual((yield captureContextMenu("h1")),
+ menugroup(menuseparator(),
+ menuitem({label: "link"})),
+ "matches anchor child");
+
+ assert.deepEqual((yield captureContextMenu("i")),
+ menugroup(menuseparator(),
+ menuitem({label: "link"})),
+ "matches anchor decedent");
+ assert.deepEqual((yield captureContextMenu("h2")),
+ menugroup(),
+ "does not match if not under anchor");
+ });
+}, data`<a href="/link"><h1>Hello <i>World</i></h1></a><h2>miss</h2>`);
+
+
+exports["test editable context"] = withTab(function*(assert) {
+ const isntEditable = function*(selector) {
+ assert.deepEqual((yield captureContextMenu(selector)),
+ menugroup(),
+ `${selector} isn't editable`);
+ };
+
+ const isEditable = function*(selector) {
+ assert.deepEqual((yield captureContextMenu(selector)),
+ menugroup(menuseparator(),
+ menuitem({label: "editable"})),
+ `${selector} is editable`);
+ };
+
+ yield* withItems({
+ item: new Item({
+ label: "editable",
+ context: [new Contexts.Editable()]
+ })
+ }, function*(_) {
+ yield* isntEditable("h1");
+ yield* isEditable("input[id=text]");
+ yield* isntEditable("input[disabled=true]");
+ yield* isntEditable("input[readonly=true]");
+ yield* isntEditable("input[type=submit]");
+ yield* isntEditable("input[type=radio]");
+ yield* isntEditable("input[type=checkbox]");
+ yield* isEditable("input[type=foo]");
+ yield* isEditable("textarea");
+ yield* isEditable("[contenteditable=true]");
+ });
+}, data`<body>
+<h1>examles</h1>
+<pre contenteditable="true">This content is editable.</pre>
+<input type="text" readonly="true" value="readonly value">
+<input type="text" disabled="true" value="disabled value">
+<input type="text" id=text value="test value">
+<input type="submit" />
+<input type="radio" />
+<input type="foo" />
+<input type="checkbox" />
+<textarea>A text field,
+with some text.</textarea>
+</body>`);
+
+exports["test image context"] = withTab(function*(assert) {
+ yield withItems({
+ item: new Item({
+ label: "image",
+ context: [new Contexts.Image()]
+ })
+ }, function*(_) {
+ assert.deepEqual((yield captureContextMenu("img")),
+ menugroup(menuseparator(), menuitem({label: "image"})),
+ `<img/> matches image context`);
+
+ assert.deepEqual((yield captureContextMenu("p image")),
+ menugroup(),
+ `<image/> does not image context`);
+
+ assert.deepEqual((yield captureContextMenu("svg image")),
+ menugroup(menuseparator(), menuitem({label: "image"})),
+ `<svg:image/> matches image context`);
+ });
+}, data`<body>
+<p><image style="width: 50px; height: 50px" /></p>
+<img src='' />
+<div>
+<svg xmlns="http://www.w3.org/2000/svg"
+ xmlns:xlink= "http://www.w3.org/1999/xlink">
+ <image x="0" y="0" height="50px" width="50px" xlink:href=""/>
+</svg>
+<div>
+</body>`);
+
+
+exports["test audiot & video contexts"] = withTab(function*(assert) {
+ yield withItems({
+ audio: new Item({
+ label: "audio",
+ context: [new Contexts.Audio()]
+ }),
+ video: new Item({
+ label: "video",
+ context: [new Contexts.Video()]
+ }),
+ media: new Item({
+ label: "media",
+ context: [new Contexts.Audio(),
+ new Contexts.Video()]
+ })
+ }, function*(_) {
+ assert.deepEqual((yield captureContextMenu("img")),
+ menugroup(),
+ `<img/> does not match video or audio context`);
+
+ assert.deepEqual((yield captureContextMenu("audio")),
+ menugroup(menuseparator(),
+ menuitem({label: "audio"}),
+ menuitem({label: "media"})),
+ `<audio/> matches audio context`);
+
+ assert.deepEqual((yield captureContextMenu("video")),
+ menugroup(menuseparator(),
+ menuitem({label: "video"}),
+ menuitem({label: "media"})),
+ `<video/> matches video context`);
+ })
+}, data`<body>
+<div><video width=10 height=10 controls /></div>
+<div><audio width=10 height=10 controls /></div>
+<div><image style="width: 50px; height: 50px" /></div>
+</body>`);
+
+const predicateTestURL = data`<html>
+ <head>
+ <style>
+ p, object, embed { display: inline-block; }
+ </style>
+ </head>
+ <body>
+ <strong><p>paragraph</p></strong>
+ <p><a href=./link><span>link</span></a></p>
+ <p><h3>hi</h3></p>
+ <p><button>button</button></p>
+ <p><canvas height=50 width=50 /></p>
+ <p><img height=50 width=50 src="./no.png" /></p>
+ <p><code contenteditable="true">This content is editable.</code></p>
+ <p><input type="text" readonly="true" value="readonly value"></p>
+ <p><input type="text" disabled="true" value="disabled value"></p>
+ <p><input type="text" id=text value="test value" /></p>
+ <p><input type="submit" /></p>
+ <p><input type="radio" /></p>
+ <p><input type="foo" /></p>
+ <p><input type="checkbox" /></p>
+ <p><textarea>A text field,
+ with some text.</textarea></p>
+ <p><iframe src='data:text/html,<body style="height:100%">Bye</body>'></iframe></p>
+ <p><select><option>one</option><option>two</option></select></p>
+ <p><menu><button>item</button></menu></p>
+ <p><object width=10 height=10><param name=foo value=bar /></object></p>
+ <p><embed width=10 height=10/></p>
+ <p><video width=50 height=50 controls /></p>
+ <p><audio width=10 height=10 controls /></p>
+ <p><applet width=30 height=30 /></p>
+ </body>
+</html>`;
+exports["test predicate context"] = withTab(function*(assert) {
+ const test = function*(selector, expect) {
+ var isMatch = false;
+ test.return = (target) => {
+ return isMatch = expect(target);
+ }
+ assert.deepEqual((yield captureContextMenu(selector)),
+ isMatch ? menugroup(menuseparator(),
+ menuitem({label:"predicate"})) :
+ menugroup(),
+ isMatch ? `predicate item matches ${selector}` :
+ `predicate item doesn't match ${selector}`);
+ };
+ test.predicate = target => test.return(target);
+
+ yield* withItems({
+ item: new Item({
+ label: "predicate",
+ read: {
+ mediaType: new Readers.MediaType(),
+ link: new Readers.LinkURL(),
+ isPage: new Readers.isPage(),
+ isFrame: new Readers.isFrame(),
+ isEditable: new Readers.isEditable(),
+ tagName: new Readers.Query("tagName"),
+ appCodeName: new Readers.Query("ownerDocument.defaultView.navigator.appCodeName"),
+ width: new Readers.Attribute("width"),
+ src: new Readers.SrcURL(),
+ url: new Readers.PageURL(),
+ selection: new Readers.Selection()
+ },
+ context: [Contexts.Predicate(test.predicate)]
+ })
+ }, function*(items) {
+ yield* test("strong p", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: true,
+ isFrame: false,
+ isEditable: false,
+ tagName: "P",
+ appCodeName: "Mozilla",
+ width: null,
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "pagraph read test");
+ return true;
+ });
+
+ yield* test("a span", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: "./link",
+ isPage: false,
+ isFrame: false,
+ isEditable: false,
+ tagName: "SPAN",
+ appCodeName: "Mozilla",
+ width: null,
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "video tag test");
+ return false;
+ });
+
+ yield* test("h3", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: true,
+ isFrame: false,
+ isEditable: false,
+ tagName: "H3",
+ appCodeName: "Mozilla",
+ width: null,
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "video tag test");
+ return false;
+ });
+
+ yield select("h3");
+
+ yield* test("a span", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: "./link",
+ isPage: false,
+ isFrame: false,
+ isEditable: false,
+ tagName: "SPAN",
+ appCodeName: "Mozilla",
+ width: null,
+ src: null,
+ url: predicateTestURL,
+ selection: "hi",
+ }, "test selection with link");
+ return true;
+ });
+
+ yield select(null);
+
+
+ yield* test("button", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: false,
+ tagName: "BUTTON",
+ appCodeName: "Mozilla",
+ width: null,
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "test button");
+ return true;
+ });
+
+ yield* test("canvas", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: false,
+ tagName: "CANVAS",
+ appCodeName: "Mozilla",
+ width: "50",
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "test button");
+ return true;
+ });
+
+ yield* test("img", target => {
+ assert.deepEqual(target, {
+ mediaType: "image",
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: false,
+ tagName: "IMG",
+ appCodeName: "Mozilla",
+ width: "50",
+ src: "./no.png",
+ url: predicateTestURL,
+ selection: null,
+ }, "test image");
+ return true;
+ });
+
+ yield* test("code", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: true,
+ tagName: "CODE",
+ appCodeName: "Mozilla",
+ width: null,
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "test content editable");
+ return false;
+ });
+
+ yield* test("input[readonly=true]", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: false,
+ tagName: "INPUT",
+ appCodeName: "Mozilla",
+ width: null,
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "test readonly input");
+ return false;
+ });
+
+ yield* test("input[disabled=true]", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: false,
+ tagName: "INPUT",
+ appCodeName: "Mozilla",
+ width: null,
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "test disabled input");
+ return false;
+ });
+
+ yield select({target: "input#text", start: 0, end: 5 });
+
+ yield* test("input#text", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: true,
+ tagName: "INPUT",
+ appCodeName: "Mozilla",
+ width: null,
+ src: null,
+ url: predicateTestURL,
+ selection: "test ",
+ }, "test editable input");
+ return false;
+ });
+
+ yield select({target: "input#text", start:0, end: 0});
+
+ yield* test("input[type=submit]", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: false,
+ tagName: "INPUT",
+ appCodeName: "Mozilla",
+ width: null,
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "test submit input");
+ return false;
+ });
+
+ yield* test("input[type=radio]", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: false,
+ tagName: "INPUT",
+ appCodeName: "Mozilla",
+ width: null,
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "test radio input");
+ return false;
+ });
+
+ yield* test("input[type=checkbox]", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: false,
+ tagName: "INPUT",
+ appCodeName: "Mozilla",
+ width: null,
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "test checkbox input");
+ return false;
+ });
+
+ yield* test("input[type=foo]", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: true,
+ tagName: "INPUT",
+ appCodeName: "Mozilla",
+ width: null,
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "test unrecognized input");
+ return false;
+ });
+
+ yield* test("textarea", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: true,
+ tagName: "TEXTAREA",
+ appCodeName: "Mozilla",
+ width: null,
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "test textarea");
+ return false;
+ });
+
+
+ yield* test("iframe", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: true,
+ isFrame: true,
+ isEditable: false,
+ tagName: "BODY",
+ appCodeName: "Mozilla",
+ width: null,
+ src: null,
+ url: `data:text/html,<body%20style="height:100%">Bye</body>`,
+ selection: null,
+ }, "test iframe");
+ return true;
+ });
+
+ yield* test("select", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: false,
+ tagName: "SELECT",
+ appCodeName: "Mozilla",
+ width: null,
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "test select");
+ return true;
+ });
+
+ yield* test("menu", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: false,
+ tagName: "MENU",
+ appCodeName: "Mozilla",
+ width: null,
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "test menu");
+ return false;
+ });
+
+ yield* test("video", target => {
+ assert.deepEqual(target, {
+ mediaType: "video",
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: false,
+ tagName: "VIDEO",
+ appCodeName: "Mozilla",
+ width: "50",
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "test video");
+ return true;
+ });
+
+ yield* test("audio", target => {
+ assert.deepEqual(target, {
+ mediaType: "audio",
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: false,
+ tagName: "AUDIO",
+ appCodeName: "Mozilla",
+ width: "10",
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "test audio");
+ return true;
+ });
+
+ yield* test("object", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: false,
+ tagName: "OBJECT",
+ appCodeName: "Mozilla",
+ width: "10",
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "test object");
+ return true;
+ });
+
+ yield* test("embed", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: false,
+ tagName: "EMBED",
+ appCodeName: "Mozilla",
+ width: "10",
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "test embed");
+ return true;
+ });
+
+ yield* test("applet", target => {
+ assert.deepEqual(target, {
+ mediaType: null,
+ link: null,
+ isPage: false,
+ isFrame: false,
+ isEditable: false,
+ tagName: "APPLET",
+ appCodeName: "Mozilla",
+ width: "30",
+ src: null,
+ url: predicateTestURL,
+ selection: null,
+ }, "test applet");
+ return false;
+ });
+
+ });
+}, predicateTestURL);
+
+exports["test extractor reader"] = withTab(function*(assert) {
+ const test = function*(selector, expect) {
+ var isMatch = false;
+ test.return = (target) => {
+ return isMatch = expect(target);
+ }
+ assert.deepEqual((yield captureContextMenu(selector)),
+ isMatch ? menugroup(menuseparator(),
+ menuitem({label:"extractor"})) :
+ menugroup(),
+ isMatch ? `predicate item matches ${selector}` :
+ `predicate item doesn't match ${selector}`);
+ };
+ test.predicate = target => test.return(target);
+
+
+ yield* withItems({
+ item: new Item({
+ label: "extractor",
+ context: [Contexts.Predicate(test.predicate)],
+ read: {
+ tagName: Readers.Query("tagName"),
+ selector: Readers.Extractor(target => {
+ let node = target;
+ let path = [];
+ while (node) {
+ if (node.id) {
+ path.unshift(`#${node.id}`);
+ node = null;
+ }
+ else {
+ path.unshift(node.localName);
+ node = node.parentElement;
+ }
+ }
+ return path.join(" > ");
+ })
+ }
+ })
+ }, function*(_) {
+ yield* test("footer", target => {
+ assert.deepEqual(target, {
+ tagName: "FOOTER",
+ selector: "html > body > nav > footer"
+ }, "test footer");
+ return false;
+ });
+
+
+ });
+}, data`<html>
+ <body>
+ <nav>
+ <header>begin</header>
+ <footer>end</footer>
+ </nav>
+ <article data-index=1>
+ <header>First title</header>
+ <div>
+ <p>First paragraph</p>
+ <p>Second paragraph</p>
+ </div>
+ </article>
+ <article data-index=2>
+ <header>Second title</header>
+ <div>
+ <p>First <strong id=foo>paragraph</strong></p>
+ <p>Second paragraph</p>
+ </div>
+ </article>
+ </body>
+</html>`);
+
+exports["test items overflow"] = withTab(function*(assert) {
+ yield* withItems({
+ i1: new Item({label: "item-1"}),
+ i2: new Item({label: "item-2"}),
+ i3: new Item({label: "item-3"}),
+ i4: new Item({label: "item-4"}),
+ i5: new Item({label: "item-5"}),
+ i6: new Item({label: "item-6"}),
+ i7: new Item({label: "item-7"}),
+ i8: new Item({label: "item-8"}),
+ i9: new Item({label: "item-9"}),
+ i10: new Item({label: "item-10"}),
+ }, function*(_) {
+ assert.deepEqual((yield captureContextMenu("p")),
+ menugroup(menu({
+ className: "sdk-context-menu-overflow-menu",
+ label: "Add-ons",
+ accesskey: "A",
+ }, menuitem({label: "item-1"}),
+ menuitem({label: "item-2"}),
+ menuitem({label: "item-3"}),
+ menuitem({label: "item-4"}),
+ menuitem({label: "item-5"}),
+ menuitem({label: "item-6"}),
+ menuitem({label: "item-7"}),
+ menuitem({label: "item-8"}),
+ menuitem({label: "item-9"}),
+ menuitem({label: "item-10"}))),
+ "context menu has an overflow");
+ });
+
+ prefs.set("extensions.addon-sdk.context-menu.overflowThreshold", 3);
+
+ yield* withItems({
+ i1: new Item({label: "item-1"}),
+ i2: new Item({label: "item-2"}),
+ }, function*(_) {
+ assert.deepEqual((yield captureContextMenu("p")),
+ menugroup(menuseparator(),
+ menuitem({label: "item-1"}),
+ menuitem({label: "item-2"})),
+ "two items do not overflow");
+ });
+
+ yield* withItems({
+ one: new Item({label: "one"}),
+ two: new Item({label: "two"}),
+ three: new Item({label: "three"})
+ }, function*(_) {
+ assert.deepEqual((yield captureContextMenu("p")),
+ menugroup(menu({className: "sdk-context-menu-overflow-menu",
+ label: "Add-ons",
+ accesskey: "A"},
+ menuitem({label: "one"}),
+ menuitem({label: "two"}),
+ menuitem({label: "three"}))),
+ "three items overflow");
+ });
+
+ prefs.reset("extensions.addon-sdk.context-menu.overflowThreshold");
+
+ yield* withItems({
+ one: new Item({label: "one"}),
+ two: new Item({label: "two"}),
+ three: new Item({label: "three"})
+ }, function*(_) {
+ assert.deepEqual((yield captureContextMenu("p")),
+ menugroup(menuseparator(),
+ menuitem({label: "one"}),
+ menuitem({label: "two"}),
+ menuitem({label: "three"})),
+ "three items no longer overflow");
+ });
+}, data`<p>Hello</p>`);
+
+
+exports["test context menus"] = withTab(function*(assert) {
+ const one = new Item({
+ label: "one",
+ context: [Contexts.Selector("p")],
+ read: {tagName: Readers.Query("tagName")}
+ });
+
+ assert.deepEqual((yield captureContextMenu("p")),
+ menugroup(menuseparator(),
+ menuitem({label: "one"})),
+ "item is present");
+
+ const two = new Item({
+ label: "two",
+ read: {tagName: Readers.Query("tagName")}
+ });
+
+
+ assert.deepEqual((yield captureContextMenu("p")),
+ menugroup(menuseparator(),
+ menuitem({label: "one"}),
+ menuitem({label: "two"})),
+ "both items are present");
+
+ const groupLevel1 = new Menu({label: "Level 1"},
+ [one]);
+
+ assert.deepEqual((yield captureContextMenu("p")),
+ menugroup(menuseparator(),
+ menuitem({label: "two"}),
+ menu({label: "Level 1"},
+ menuitem({label: "one"}))),
+ "first item moved to group");
+
+ assert.deepEqual((yield captureContextMenu("h1")),
+ menugroup(menuseparator(),
+ menuitem({label: "two"})),
+ "menu is hidden since only item does not match");
+
+
+ const groupLevel2 = new Menu({label: "Level 2" }, [groupLevel1]);
+
+ assert.deepEqual((yield captureContextMenu("p")),
+ menugroup(menuseparator(),
+ menuitem({label: "two"}),
+ menu({label: "Level 2"},
+ menu({label: "Level 1"},
+ menuitem({label: "one"})))),
+ "top level menu moved to submenu");
+
+ assert.deepEqual((yield captureContextMenu("h1")),
+ menugroup(menuseparator(),
+ menuitem({label: "two"})),
+ "menu is hidden since only item does not match");
+
+
+ const contextGroup = new Menu({
+ label: "H1 Group",
+ context: [Contexts.Selector("h1")]
+ }, [
+ two,
+ new Separator(),
+ new Item({ label: "three" })
+ ]);
+
+
+ assert.deepEqual((yield captureContextMenu("p")),
+ menugroup(menuseparator(),
+ menu({label: "Level 2"},
+ menu({label: "Level 1"},
+ menuitem({label: "one"})))),
+ "nested menu is rendered");
+
+ assert.deepEqual((yield captureContextMenu("h1")),
+ menugroup(menuseparator(),
+ menu({label: "H1 Group"},
+ menuitem({label: "two"}),
+ menuseparator(),
+ menuitem({label: "three"}))),
+ "new contextual menu rendered");
+
+ yield* withItems({one, two,
+ groupLevel1, groupLevel2, contextGroup}, function*() {
+
+ });
+
+ assert.deepEqual((yield captureContextMenu("p")),
+ menugroup(),
+ "everyhing matching p was desposed");
+
+ assert.deepEqual((yield captureContextMenu("h1")),
+ menugroup(),
+ "everyhing matching h1 was desposed");
+
+}, data`<body><h1>Title</h1><p>Content</p></body>`);
+
+exports["test unloading"] = withTab(function*(assert) {
+ const { Loader } = require("sdk/test/loader");
+ const loader = Loader(module);
+
+ const {Item, Menu, Separator, Contexts, Readers } = loader.require("sdk/context-menu@2");
+
+ const item = new Item({label: "item"});
+ const group = new Menu({label: "menu"},
+ [new Separator(),
+ new Item({label: "sub-item"})]);
+ assert.deepEqual((yield captureContextMenu()),
+ menugroup(menuseparator(),
+ menuitem({label: "item"}),
+ menu({label: "menu"},
+ menuseparator(),
+ menuitem({label: "sub-item"}))),
+ "all items rendered");
+
+
+ loader.unload();
+
+ assert.deepEqual((yield captureContextMenu()),
+ menugroup(),
+ "all items disposed");
+}, data`<body></body>`);
+
+if (require("@loader/options").isNative) {
+ module.exports = {
+ "test skip on jpm": (assert) => assert.pass("skipping this file with jpm")
+ };
+}
+
+before(exports, (name, assert) => {
+ // Make sure Java doesn't activate
+ prefs.set("plugin.state.java", 0);
+});
+
+after(exports, (name, assert) => {
+ prefs.reset("plugin.state.java");
+});
+
+require("sdk/test").run(module.exports);
diff --git a/addon-sdk/source/test/test-cuddlefish.js b/addon-sdk/source/test/test-cuddlefish.js
new file mode 100644
index 000000000..c92eaa624
--- /dev/null
+++ b/addon-sdk/source/test/test-cuddlefish.js
@@ -0,0 +1,78 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Cc, Ci, Cu, CC, Cr, Cm, ChromeWorker, components } = require("chrome");
+
+const packaging = require("@loader/options");
+const app = require('sdk/system/xul-app');
+const { resolve } = require;
+
+const scriptLoader = Cc['@mozilla.org/moz/jssubscript-loader;1'].
+ getService(Ci.mozIJSSubScriptLoader);
+const systemPrincipal = CC('@mozilla.org/systemprincipal;1', 'nsIPrincipal')();
+
+function loadSandbox(uri) {
+ let proto = {
+ sandboxPrototype: {
+ loadSandbox: loadSandbox,
+ ChromeWorker: ChromeWorker
+ }
+ };
+ let sandbox = Cu.Sandbox(systemPrincipal, proto);
+ // Create a fake commonjs environnement just to enable loading loader.js
+ // correctly
+ sandbox.exports = {};
+ sandbox.module = { uri: uri, exports: sandbox.exports };
+ sandbox.require = function (id) {
+ if (id !== "chrome")
+ throw new Error("Bootstrap sandbox `require` method isn't implemented.");
+
+ return Object.freeze({ Cc: Cc, Ci: Ci, Cu: Cu, Cr: Cr, Cm: Cm,
+ CC: CC, components: components,
+ ChromeWorker: ChromeWorker });
+ };
+ scriptLoader.loadSubScript(uri, sandbox, 'UTF-8');
+ return sandbox;
+}
+
+exports['test loader'] = function(assert) {
+ let { Loader, Require, unload, override } = loadSandbox(resolve('sdk/loader/cuddlefish.js')).exports;
+ var prints = [];
+ function print(message) {
+ prints.push(message);
+ }
+
+ let loader = Loader(override(packaging, {
+ globals: {
+ print: print,
+ foo: 1
+ }
+ }));
+ let require = Require(loader, module);
+
+ var fixture = require('./loader/fixture');
+
+ assert.equal(fixture.foo, 1, 'custom globals must work.');
+ assert.equal(fixture.bar, 2, 'exports are set');
+
+ assert.equal(prints[0], 'testing', 'global print must be injected.');
+
+ var unloadsCalled = '';
+
+ require("sdk/system/unload").when(function(reason) {
+ assert.equal(reason, 'test', 'unload reason is passed');
+ unloadsCalled += 'a';
+ });
+ require('sdk/system/unload.js').when(function() {
+ unloadsCalled += 'b';
+ });
+
+ unload(loader, 'test');
+
+ assert.equal(unloadsCalled, 'ba',
+ 'loader.unload() must call listeners in LIFO order.');
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-deprecate.js b/addon-sdk/source/test/test-deprecate.js
new file mode 100644
index 000000000..c1bd443c6
--- /dev/null
+++ b/addon-sdk/source/test/test-deprecate.js
@@ -0,0 +1,160 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const deprecate = require("sdk/util/deprecate");
+const { LoaderWithHookedConsole } = require("sdk/test/loader");
+const { get, set } = require("sdk/preferences/service");
+const PREFERENCE = "devtools.errorconsole.deprecation_warnings";
+
+exports["test Deprecate Usage"] = function testDeprecateUsage(assert) {
+ set(PREFERENCE, true);
+ let { loader, messages } = LoaderWithHookedConsole(module);
+ let deprecate = loader.require("sdk/util/deprecate");
+
+ function functionIsDeprecated() {
+ deprecate.deprecateUsage("foo");
+ }
+
+ functionIsDeprecated();
+
+ assert.equal(messages.length, 1, "only one error is dispatched");
+ assert.equal(messages[0].type, "error", "the console message is an error");
+
+ let msg = messages[0].msg;
+
+ assert.ok(msg.indexOf("foo") !== -1,
+ "message contains the given message");
+ assert.ok(msg.indexOf("functionIsDeprecated") !== -1,
+ "message contains name of the caller function");
+ assert.ok(msg.indexOf(module.uri) !== -1,
+ "message contains URI of the caller module");
+
+ loader.unload();
+}
+
+exports["test Deprecate Function"] = function testDeprecateFunction(assert) {
+ set(PREFERENCE, true);
+ let { loader, messages } = LoaderWithHookedConsole(module);
+ let deprecate = loader.require("sdk/util/deprecate");
+
+ let self = {};
+ let arg1 = "foo";
+ let arg2 = {};
+
+ function originalFunction(a1, a2) {
+ assert.equal(this, self);
+ assert.equal(a1, arg1);
+ assert.equal(a2, arg2);
+ };
+
+ let deprecateFunction = deprecate.deprecateFunction(originalFunction,
+ "bar");
+
+ deprecateFunction.call(self, arg1, arg2);
+
+ assert.equal(messages.length, 1, "only one error is dispatched");
+ assert.equal(messages[0].type, "error", "the console message is an error");
+
+ let msg = messages[0].msg;
+ assert.ok(msg.indexOf("bar") !== -1, "message contains the given message");
+ assert.ok(msg.indexOf("testDeprecateFunction") !== -1,
+ "message contains name of the caller function");
+ assert.ok(msg.indexOf(module.uri) !== -1,
+ "message contains URI of the caller module");
+
+ loader.unload();
+}
+
+exports.testDeprecateEvent = function(assert, done) {
+ set(PREFERENCE, true);
+ let { loader, messages } = LoaderWithHookedConsole(module);
+ let deprecate = loader.require("sdk/util/deprecate");
+
+ let { on, emit } = loader.require('sdk/event/core');
+ let testObj = {};
+ testObj.on = deprecate.deprecateEvent(on.bind(null, testObj), 'BAD', ['fire']);
+
+ testObj.on('fire', function() {
+ testObj.on('water', function() {
+ assert.equal(messages.length, 1, "only one error is dispatched");
+ loader.unload();
+ done();
+ })
+ assert.equal(messages.length, 1, "only one error is dispatched");
+ emit(testObj, 'water');
+ });
+
+ assert.equal(messages.length, 1, "only one error is dispatched");
+ assert.equal(messages[0].type, "error", "the console message is an error");
+ let msg = messages[0].msg;
+ assert.ok(msg.indexOf("BAD") !== -1, "message contains the given message");
+ assert.ok(msg.indexOf("deprecateEvent") !== -1,
+ "message contains name of the caller function");
+ assert.ok(msg.indexOf(module.uri) !== -1,
+ "message contains URI of the caller module");
+
+ emit(testObj, 'fire');
+}
+
+exports.testDeprecateSettingToggle = function (assert) {
+ let { loader, messages } = LoaderWithHookedConsole(module);
+ let deprecate = loader.require("sdk/util/deprecate");
+
+ function fn () { deprecate.deprecateUsage("foo"); }
+
+ set(PREFERENCE, false);
+ fn();
+ assert.equal(messages.length, 0, 'no deprecation warnings');
+
+ set(PREFERENCE, true);
+ fn();
+ assert.equal(messages.length, 1, 'deprecation warnings when toggled');
+
+ set(PREFERENCE, false);
+ fn();
+ assert.equal(messages.length, 1, 'no new deprecation warnings');
+};
+
+exports.testDeprecateSetting = function (assert, done) {
+ // Set devtools.errorconsole.deprecation_warnings to false
+ set(PREFERENCE, false);
+
+ let { loader, messages } = LoaderWithHookedConsole(module);
+ let deprecate = loader.require("sdk/util/deprecate");
+
+ // deprecateUsage test
+ function functionIsDeprecated() {
+ deprecate.deprecateUsage("foo");
+ }
+ functionIsDeprecated();
+
+ assert.equal(messages.length, 0,
+ "no errors dispatched on deprecateUsage");
+
+ // deprecateFunction test
+ function originalFunction() {};
+
+ let deprecateFunction = deprecate.deprecateFunction(originalFunction,
+ "bar");
+ deprecateFunction();
+
+ assert.equal(messages.length, 0,
+ "no errors dispatched on deprecateFunction");
+
+ // deprecateEvent
+ let { on, emit } = loader.require('sdk/event/core');
+ let testObj = {};
+ testObj.on = deprecate.deprecateEvent(on.bind(null, testObj), 'BAD', ['fire']);
+
+ testObj.on('fire', () => {
+ assert.equal(messages.length, 0,
+ "no errors dispatched on deprecateEvent");
+ done();
+ });
+
+ emit(testObj, 'fire');
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-dev-panel.js b/addon-sdk/source/test/test-dev-panel.js
new file mode 100644
index 000000000..fb786b043
--- /dev/null
+++ b/addon-sdk/source/test/test-dev-panel.js
@@ -0,0 +1,426 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+module.metadata = {
+ "engines": {
+ "Firefox": "*"
+ }
+};
+
+const { Tool } = require("dev/toolbox");
+const { Panel } = require("dev/panel");
+const { Class } = require("sdk/core/heritage");
+const { openToolbox, closeToolbox, getCurrentPanel } = require("dev/utils");
+const { MessageChannel } = require("sdk/messaging");
+const { when } = require("sdk/dom/events-shimmed");
+const { viewFor } = require("sdk/view/core");
+const { createView } = require("dev/panel/view");
+
+const iconURI = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAaCAYAAACpSkzOAAAKQWlDQ1BJQ0MgUHJvZmlsZQAASA2dlndUU9kWh8+9N73QEiIgJfQaegkg0jtIFQRRiUmAUAKGhCZ2RAVGFBEpVmRUwAFHhyJjRRQLg4Ji1wnyEFDGwVFEReXdjGsJ7601896a/cdZ39nnt9fZZ+9917oAUPyCBMJ0WAGANKFYFO7rwVwSE8vE9wIYEAEOWAHA4WZmBEf4RALU/L09mZmoSMaz9u4ugGS72yy/UCZz1v9/kSI3QyQGAApF1TY8fiYX5QKUU7PFGTL/BMr0lSkyhjEyFqEJoqwi48SvbPan5iu7yZiXJuShGlnOGbw0noy7UN6aJeGjjAShXJgl4GejfAdlvVRJmgDl9yjT0/icTAAwFJlfzOcmoWyJMkUUGe6J8gIACJTEObxyDov5OWieAHimZ+SKBIlJYqYR15hp5ejIZvrxs1P5YjErlMNN4Yh4TM/0tAyOMBeAr2+WRQElWW2ZaJHtrRzt7VnW5mj5v9nfHn5T/T3IevtV8Sbsz55BjJ5Z32zsrC+9FgD2JFqbHbO+lVUAtG0GQOXhrE/vIADyBQC03pzzHoZsXpLE4gwnC4vs7GxzAZ9rLivoN/ufgm/Kv4Y595nL7vtWO6YXP4EjSRUzZUXlpqemS0TMzAwOl89k/fcQ/+PAOWnNycMsnJ/AF/GF6FVR6JQJhIlou4U8gViQLmQKhH/V4X8YNicHGX6daxRodV8AfYU5ULhJB8hvPQBDIwMkbj96An3rWxAxCsi+vGitka9zjzJ6/uf6Hwtcim7hTEEiU+b2DI9kciWiLBmj34RswQISkAd0oAo0gS4wAixgDRyAM3AD3iAAhIBIEAOWAy5IAmlABLJBPtgACkEx2AF2g2pwANSBetAEToI2cAZcBFfADXALDIBHQAqGwUswAd6BaQiC8BAVokGqkBakD5lC1hAbWgh5Q0FQOBQDxUOJkBCSQPnQJqgYKoOqoUNQPfQjdBq6CF2D+qAH0CA0Bv0BfYQRmALTYQ3YALaA2bA7HAhHwsvgRHgVnAcXwNvhSrgWPg63whfhG/AALIVfwpMIQMgIA9FGWAgb8URCkFgkAREha5EipAKpRZqQDqQbuY1IkXHkAwaHoWGYGBbGGeOHWYzhYlZh1mJKMNWYY5hWTBfmNmYQM4H5gqVi1bGmWCesP3YJNhGbjS3EVmCPYFuwl7ED2GHsOxwOx8AZ4hxwfrgYXDJuNa4Etw/XjLuA68MN4SbxeLwq3hTvgg/Bc/BifCG+Cn8cfx7fjx/GvyeQCVoEa4IPIZYgJGwkVBAaCOcI/YQRwjRRgahPdCKGEHnEXGIpsY7YQbxJHCZOkxRJhiQXUiQpmbSBVElqIl0mPSa9IZPJOmRHchhZQF5PriSfIF8lD5I/UJQoJhRPShxFQtlOOUq5QHlAeUOlUg2obtRYqpi6nVpPvUR9Sn0vR5Mzl/OX48mtk6uRa5Xrl3slT5TXl3eXXy6fJ18hf0r+pvy4AlHBQMFTgaOwVqFG4bTCPYVJRZqilWKIYppiiWKD4jXFUSW8koGStxJPqUDpsNIlpSEaQtOledK4tE20Otpl2jAdRzek+9OT6cX0H+i99AllJWVb5SjlHOUa5bPKUgbCMGD4M1IZpYyTjLuMj/M05rnP48/bNq9pXv+8KZX5Km4qfJUilWaVAZWPqkxVb9UU1Z2qbapP1DBqJmphatlq+9Uuq43Pp893ns+dXzT/5PyH6rC6iXq4+mr1w+o96pMamhq+GhkaVRqXNMY1GZpumsma5ZrnNMe0aFoLtQRa5VrntV4wlZnuzFRmJbOLOaGtru2nLdE+pN2rPa1jqLNYZ6NOs84TXZIuWzdBt1y3U3dCT0svWC9fr1HvoT5Rn62fpL9Hv1t/ysDQINpgi0GbwaihiqG/YZ5ho+FjI6qRq9Eqo1qjO8Y4Y7ZxivE+41smsImdSZJJjclNU9jU3lRgus+0zwxr5mgmNKs1u8eisNxZWaxG1qA5wzzIfKN5m/krCz2LWIudFt0WXyztLFMt6ywfWSlZBVhttOqw+sPaxJprXWN9x4Zq42Ozzqbd5rWtqS3fdr/tfTuaXbDdFrtOu8/2DvYi+yb7MQc9h3iHvQ732HR2KLuEfdUR6+jhuM7xjOMHJ3snsdNJp9+dWc4pzg3OowsMF/AX1C0YctFx4bgccpEuZC6MX3hwodRV25XjWuv6zE3Xjed2xG3E3dg92f24+ysPSw+RR4vHlKeT5xrPC16Il69XkVevt5L3Yu9q76c+Oj6JPo0+E752vqt9L/hh/QL9dvrd89fw5/rX+08EOASsCegKpARGBFYHPgsyCRIFdQTDwQHBu4IfL9JfJFzUFgJC/EN2hTwJNQxdFfpzGC4sNKwm7Hm4VXh+eHcELWJFREPEu0iPyNLIR4uNFksWd0bJR8VF1UdNRXtFl0VLl1gsWbPkRoxajCCmPRYfGxV7JHZyqffS3UuH4+ziCuPuLjNclrPs2nK15anLz66QX8FZcSoeGx8d3xD/iRPCqeVMrvRfuXflBNeTu4f7kufGK+eN8V34ZfyRBJeEsoTRRJfEXYljSa5JFUnjAk9BteB1sl/ygeSplJCUoykzqdGpzWmEtPi000IlYYqwK10zPSe9L8M0ozBDuspp1e5VE6JA0ZFMKHNZZruYjv5M9UiMJJslg1kLs2qy3mdHZZ/KUcwR5vTkmuRuyx3J88n7fjVmNXd1Z752/ob8wTXuaw6thdauXNu5Tnddwbrh9b7rj20gbUjZ8MtGy41lG99uit7UUaBRsL5gaLPv5sZCuUJR4b0tzlsObMVsFWzt3WazrWrblyJe0fViy+KK4k8l3JLr31l9V/ndzPaE7b2l9qX7d+B2CHfc3em681iZYlle2dCu4F2t5czyovK3u1fsvlZhW3FgD2mPZI+0MqiyvUqvakfVp+qk6oEaj5rmvep7t+2d2sfb17/fbX/TAY0DxQc+HhQcvH/I91BrrUFtxWHc4azDz+ui6rq/Z39ff0TtSPGRz0eFR6XHwo911TvU1zeoN5Q2wo2SxrHjccdv/eD1Q3sTq+lQM6O5+AQ4ITnx4sf4H++eDDzZeYp9qukn/Z/2ttBailqh1tzWibakNml7THvf6YDTnR3OHS0/m/989Iz2mZqzymdLz5HOFZybOZ93fvJCxoXxi4kXhzpXdD66tOTSna6wrt7LgZevXvG5cqnbvfv8VZerZ645XTt9nX297Yb9jdYeu56WX+x+aem172296XCz/ZbjrY6+BX3n+l37L972un3ljv+dGwOLBvruLr57/17cPel93v3RB6kPXj/Mejj9aP1j7OOiJwpPKp6qP6391fjXZqm99Oyg12DPs4hnj4a4Qy//lfmvT8MFz6nPK0a0RupHrUfPjPmM3Xqx9MXwy4yX0+OFvyn+tveV0auffnf7vWdiycTwa9HrmT9K3qi+OfrW9m3nZOjk03dp76anit6rvj/2gf2h+2P0x5Hp7E/4T5WfjT93fAn88ngmbWbm3/eE8/syOll+AAAACXBIWXMAAAsTAAALEwEAmpwYAAADqmlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iWE1QIENvcmUgNS40LjAiPgogICA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPgogICAgICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIgogICAgICAgICAgICB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIKICAgICAgICAgICAgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiCiAgICAgICAgICAgIHhtbG5zOnhtcD0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLyI+CiAgICAgICAgIDx4bXBNTTpEb2N1bWVudElEPnhtcC5kaWQ6M0ExNEY4NjZBNkU1MTFFMTlGMkFGQ0QyNTUyN0VDRjY8L3htcE1NOkRvY3VtZW50SUQ+CiAgICAgICAgIDx4bXBNTTpEZXJpdmVkRnJvbSByZGY6cGFyc2VUeXBlPSJSZXNvdXJjZSI+CiAgICAgICAgICAgIDxzdFJlZjppbnN0YW5jZUlEPnhtcC5paWQ6M0ExNEY4NjNBNkU1MTFFMTlGMkFGQ0QyNTUyN0VDRjY8L3N0UmVmOmluc3RhbmNlSUQ+CiAgICAgICAgICAgIDxzdFJlZjpkb2N1bWVudElEPnhtcC5kaWQ6M0ExNEY4NjRBNkU1MTFFMTlGMkFGQ0QyNTUyN0VDRjY8L3N0UmVmOmRvY3VtZW50SUQ+CiAgICAgICAgIDwveG1wTU06RGVyaXZlZEZyb20+CiAgICAgICAgIDx4bXBNTTpJbnN0YW5jZUlEPnhtcC5paWQ6M0ExNEY4NjVBNkU1MTFFMTlGMkFGQ0QyNTUyN0VDRjY8L3htcE1NOkluc3RhbmNlSUQ+CiAgICAgICAgIDx4bXA6Q3JlYXRvclRvb2w+QWRvYmUgUGhvdG9zaG9wIENTNSBNYWNpbnRvc2g8L3htcDpDcmVhdG9yVG9vbD4KICAgICAgPC9yZGY6RGVzY3JpcHRpb24+CiAgIDwvcmRmOlJERj4KPC94OnhtcG1ldGE+ChetDDYAAACaSURBVEgNY2AYboAR3UNnzpx5DxQTQBP/YGJiIogmBuYSq54Ji2Z0S0BKsInBtGKTwxDDZhHMAKrSdLOIqq7GZxjj+fPnBf7+/bseqMgBn0IK5A4wMzMHMtHYEpD7HEB2gOLIAcSjMXCgW2IYtYjsqBwNutGgg4fAaGKABwWpDLoG3QFSXUeG+gNMoEoJqJGWloErPjIcR54WALqPHeiJgl15AAAAAElFTkSuQmCC";
+const makeHTML = fn =>
+ "data:text/html;charset=utf-8,<script>(" + fn + ")();</script>";
+
+
+const test = function(unit) {
+ return function*(assert) {
+ assert.isRendered = (panel, toolbox) => {
+ const doc = toolbox.doc;
+ assert.ok(doc.querySelector("[value='" + panel.label + "']"),
+ "panel.label is found in the developer toolbox DOM");
+ assert.ok(doc.querySelector("[tooltiptext='" + panel.tooltip + "']"),
+ "panel.tooltip is found in the developer toolbox DOM");
+
+ assert.ok(doc.querySelector("#toolbox-panel-" + panel.id),
+ "toolbar panel with a matching id is present");
+ };
+
+
+ yield* unit(assert);
+ };
+};
+
+exports["test Panel API"] = test(function*(assert) {
+ const MyPanel = Class({
+ extends: Panel,
+ label: "test panel",
+ tooltip: "my test panel",
+ icon: iconURI,
+ url: makeHTML(() => {
+ document.documentElement.innerHTML = "hello world";
+ }),
+ setup: function({debuggee}) {
+ this.debuggee = debuggee;
+ assert.equal(this.readyState, "uninitialized",
+ "at construction time panel document is not inited");
+ },
+ dispose: function() {
+ delete this.debuggee;
+ }
+ });
+ assert.ok(MyPanel, "panel is defined");
+
+ const myTool = new Tool({
+ panels: {
+ myPanel: MyPanel
+ }
+ });
+ assert.ok(myTool, "tool is defined");
+
+
+ var toolbox = yield openToolbox(MyPanel);
+ var panel = yield getCurrentPanel(toolbox);
+ assert.ok(panel instanceof MyPanel, "is instance of MyPanel");
+
+ assert.isRendered(panel, toolbox);
+
+ if (panel.readyState === "uninitialized") {
+ yield panel.ready();
+ assert.equal(panel.readyState, "interactive", "panel is ready");
+ }
+
+ yield panel.loaded();
+ assert.equal(panel.readyState, "complete", "panel is loaded");
+
+ yield closeToolbox();
+
+ assert.equal(panel.readyState, "destroyed", "panel is destroyed");
+
+ myTool.destroy();
+});
+
+exports["test forbid remote https docs"] = test(function*(assert) {
+ const MyPanel = Class({
+ extends: Panel,
+ label: "test https panel",
+ tooltip: "my test panel",
+ icon: iconURI,
+ url: "https://mozilla.org",
+ });
+
+ assert.throws(() => {
+ new Tool({ panels: { myPanel: MyPanel } });
+ },
+ /The `options.url` must be a valid local URI/,
+ "can't use panel with remote URI");
+});
+
+exports["test forbid remote http docs"] = test(function*(assert) {
+ const MyPanel = Class({
+ extends: Panel,
+ label: "test http panel",
+ tooltip: "my test panel",
+ icon: iconURI,
+ url: "http://arewefastyet.com/",
+ });
+
+ assert.throws(() => {
+ new Tool({ panels: { myPanel: MyPanel } });
+ },
+ /The `options.url` must be a valid local URI/,
+ "can't use panel with remote URI");
+});
+
+exports["test forbid remote ftp docs"] = test(function*(assert) {
+ const MyPanel = Class({
+ extends: Panel,
+ label: "test ftp panel",
+ tooltip: "my test panel",
+ icon: iconURI,
+ url: "ftp://ftp.mozilla.org/",
+ });
+
+ assert.throws(() => {
+ new Tool({ panels: { myPanel: MyPanel } });
+ },
+ /The `options.url` must be a valid local URI/,
+ "can't use panel with remote URI");
+});
+
+
+exports["test Panel communication"] = test(function*(assert) {
+ const MyPanel = Class({
+ extends: Panel,
+ label: "communication",
+ tooltip: "test palen communication",
+ icon: iconURI,
+ url: makeHTML(() => {
+ window.addEventListener("message", event => {
+ if (event.source === window) {
+ var port = event.ports[0];
+ port.start();
+ port.postMessage("ping");
+ port.onmessage = (event) => {
+ if (event.data === "pong") {
+ port.postMessage("bye");
+ port.close();
+ }
+ };
+ }
+ });
+ }),
+ dispose: function() {
+ delete this.port;
+ }
+ });
+
+
+ const myTool = new Tool({
+ panels: {
+ myPanel: MyPanel
+ }
+ });
+
+
+ const toolbox = yield openToolbox(MyPanel);
+ const panel = yield getCurrentPanel(toolbox);
+ assert.ok(panel instanceof MyPanel, "is instance of MyPanel");
+
+ assert.isRendered(panel, toolbox);
+
+ yield panel.ready();
+ const { port1, port2 } = new MessageChannel();
+ panel.port = port1;
+ panel.postMessage("connect", [port2]);
+ panel.port.start();
+
+ const ping = yield when(panel.port, "message");
+
+ assert.equal(ping.data, "ping", "received ping from panel doc");
+
+ panel.port.postMessage("pong");
+
+ const bye = yield when(panel.port, "message");
+
+ assert.equal(bye.data, "bye", "received bye from panel doc");
+
+ panel.port.close();
+
+ yield closeToolbox();
+
+ assert.equal(panel.readyState, "destroyed", "panel is destroyed");
+ myTool.destroy();
+});
+
+exports["test communication with debuggee"] = test(function*(assert) {
+ const MyPanel = Class({
+ extends: Panel,
+ label: "debuggee",
+ tooltip: "test debuggee",
+ icon: iconURI,
+ url: makeHTML(() => {
+ window.addEventListener("message", event => {
+ if (event.source === window) {
+ var debuggee = event.ports[0];
+ var port = event.ports[1];
+ debuggee.start();
+ port.start();
+
+
+ debuggee.onmessage = (event) => {
+ port.postMessage(event.data);
+ };
+ port.onmessage = (event) => {
+ debuggee.postMessage(event.data);
+ };
+ }
+ });
+ }),
+ setup: function({debuggee}) {
+ this.debuggee = debuggee;
+ },
+ onReady: function() {
+ const { port1, port2 } = new MessageChannel();
+ this.port = port1;
+ this.port.start();
+ this.debuggee.start();
+
+ this.postMessage("connect", [this.debuggee, port2]);
+ },
+ dispose: function() {
+ this.port.close();
+ this.debuggee.close();
+
+ delete this.port;
+ delete this.debuggee;
+ }
+ });
+
+
+ const myTool = new Tool({
+ panels: {
+ myPanel: MyPanel
+ }
+ });
+
+
+ const toolbox = yield openToolbox(MyPanel);
+ const panel = yield getCurrentPanel(toolbox);
+ assert.ok(panel instanceof MyPanel, "is instance of MyPanel");
+
+ assert.isRendered(panel, toolbox);
+
+ yield panel.ready();
+ const intro = yield when(panel.port, "message");
+
+ assert.equal(intro.data.from, "root", "intro message from root");
+
+ panel.port.postMessage({
+ to: "root",
+ type: "echo",
+ text: "ping"
+ });
+
+ const pong = yield when(panel.port, "message");
+
+ assert.deepEqual(pong.data, {
+ to: "root",
+ from: "root",
+ type: "echo",
+ text: "ping"
+ }, "received message back from root");
+
+ yield closeToolbox();
+
+ assert.equal(panel.readyState, "destroyed", "panel is destroyed");
+
+ myTool.destroy();
+});
+
+
+exports["test viewFor panel"] = test(function*(assert) {
+ const url = "data:text/html;charset=utf-8,viewFor";
+ const MyPanel = Class({
+ extends: Panel,
+ label: "view for panel",
+ tooltip: "my panel view",
+ icon: iconURI,
+ url: url
+ });
+
+ const myTool = new Tool({
+ panels: {
+ myPanel: MyPanel
+ }
+ });
+
+
+ const toolbox = yield openToolbox(MyPanel);
+ const panel = yield getCurrentPanel(toolbox);
+ assert.ok(panel instanceof MyPanel, "is instance of MyPanel");
+
+ const frame = viewFor(panel);
+
+ assert.equal(frame.nodeName.toLowerCase(), "iframe",
+ "viewFor(panel) returns associated iframe");
+
+ yield panel.loaded();
+
+ assert.equal(frame.contentDocument.URL, url, "is expected iframe");
+
+ yield closeToolbox();
+
+ myTool.destroy();
+});
+
+
+exports["test createView panel"] = test(function*(assert) {
+ var frame = null;
+ var panel = null;
+
+ const url = "data:text/html;charset=utf-8,createView";
+ const id = Math.random().toString(16).substr(2);
+ const MyPanel = Class({
+ extends: Panel,
+ label: "create view",
+ tooltip: "panel creator",
+ icon: iconURI,
+ url: url
+ });
+
+ createView.define(MyPanel, (instance, document) => {
+ var view = document.createElement("iframe");
+ view.setAttribute("type", "content");
+
+ // save instances for later asserts
+ frame = view;
+ panel = instance;
+
+ return view;
+ });
+
+ const myTool = new Tool({
+ panels: {
+ myPanel: MyPanel
+ }
+ });
+
+ const toolbox = yield openToolbox(MyPanel);
+ const myPanel = yield getCurrentPanel(toolbox);
+
+ assert.equal(myPanel, panel,
+ "panel passed to createView is one instantiated");
+ assert.equal(viewFor(panel), frame,
+ "createView has created an iframe");
+
+ yield panel.loaded();
+
+ assert.equal(frame.contentDocument.URL, url, "is expected iframe");
+
+ yield closeToolbox();
+
+ myTool.destroy();
+});
+
+
+exports["test ports is an optional"] = test(function*(assert) {
+ const MyPanel = Class({
+ extends: Panel,
+ label: "no-port",
+ icon: iconURI,
+ url: makeHTML(() => {
+ window.addEventListener("message", event => {
+ if (event.ports.length) {
+ event.ports[0].postMessage(window.firstPacket);
+ } else {
+ window.firstPacket = event.data;
+ }
+ });
+ })
+ });
+
+
+ const myTool = new Tool({
+ panels: {
+ myPanel: MyPanel
+ }
+ });
+
+
+ const toolbox = yield openToolbox(MyPanel);
+ const panel = yield getCurrentPanel(toolbox);
+ assert.ok(panel instanceof MyPanel, "is instance of MyPanel");
+
+ assert.isRendered(panel, toolbox);
+
+ yield panel.ready();
+
+ const { port1, port2 } = new MessageChannel();
+ port1.start();
+
+ panel.postMessage("hi");
+ panel.postMessage("bye", [port2]);
+
+ const packet = yield when(port1, "message");
+
+ assert.equal(packet.data, "hi", "got first packet back");
+
+ yield closeToolbox();
+
+ assert.equal(panel.readyState, "destroyed", "panel is destroyed");
+
+ myTool.destroy();
+});
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-diffpatcher.js b/addon-sdk/source/test/test-diffpatcher.js
new file mode 100644
index 000000000..47126c930
--- /dev/null
+++ b/addon-sdk/source/test/test-diffpatcher.js
@@ -0,0 +1,8 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+exports["test diffpatcher"] = require("diffpatcher/test/index");
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-dispatcher.js b/addon-sdk/source/test/test-dispatcher.js
new file mode 100644
index 000000000..437d75176
--- /dev/null
+++ b/addon-sdk/source/test/test-dispatcher.js
@@ -0,0 +1,76 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { dispatcher } = require("sdk/util/dispatcher");
+
+exports["test dispatcher API"] = assert => {
+ const dispatch = dispatcher();
+
+ assert.equal(typeof(dispatch), "function",
+ "dispatch is a function");
+
+ assert.equal(typeof(dispatch.define), "function",
+ "dispatch.define is a function");
+
+ assert.equal(typeof(dispatch.implement), "function",
+ "dispatch.implement is a function");
+
+ assert.equal(typeof(dispatch.when), "function",
+ "dispatch.when is a function");
+};
+
+exports["test dispatcher"] = assert => {
+ const isDuck = dispatcher();
+
+ const quacks = x => x && typeof(x.quack) === "function";
+
+ const Duck = function() {};
+ const Goose = function() {};
+
+ const True = _ => true;
+ const False = _ => false;
+
+
+
+ isDuck.define(Goose, False);
+ isDuck.define(Duck, True);
+ isDuck.when(quacks, True);
+
+ assert.equal(isDuck(new Goose()), false,
+ "Goose ain't duck");
+
+ assert.equal(isDuck(new Duck()), true,
+ "Ducks are ducks");
+
+ assert.equal(isDuck({ quack: () => "Quaaaaaack!" }), true,
+ "It's a duck if it quacks");
+
+
+ assert.throws(() => isDuck({}), /Type does not implements method/, "not implemneted");
+
+ isDuck.define(Object, False);
+
+ assert.equal(isDuck({}), false,
+ "Ain't duck if it does not quacks!");
+};
+
+exports["test redefining fails"] = assert => {
+ const isPM = dispatcher();
+ const isAfternoon = time => time.getHours() > 12;
+
+ isPM.when(isAfternoon, _ => true);
+
+ assert.equal(isPM(new Date(Date.parse("Jan 23, 1985, 13:20:00"))), true,
+ "yeap afternoon");
+ assert.equal(isPM({ getHours: _ => 17 }), true,
+ "seems like afternoon");
+
+ assert.throws(() => isPM.when(isAfternoon, x => x > 12 && x < 24),
+ /Already implemented for the given predicate/,
+ "can't redefine on same predicate");
+
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-disposable.js b/addon-sdk/source/test/test-disposable.js
new file mode 100644
index 000000000..3204c2479
--- /dev/null
+++ b/addon-sdk/source/test/test-disposable.js
@@ -0,0 +1,393 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Loader } = require("sdk/test/loader");
+const { Class } = require("sdk/core/heritage");
+const { Disposable } = require("sdk/core/disposable");
+const { Cc, Ci, Cu } = require("chrome");
+const { setTimeout } = require("sdk/timers");
+
+exports["test disposeDisposable"] = assert => {
+ let loader = Loader(module);
+
+ const { Disposable, disposeDisposable } = loader.require("sdk/core/disposable");
+ const { isWeak, WeakReference } = loader.require("sdk/core/reference");
+
+ let disposals = 0;
+
+ const Foo = Class({
+ extends: Disposable,
+ implements: [WeakReference],
+ dispose(...params) {
+ disposeDisposable(this);
+ disposals = disposals + 1;
+ }
+ });
+
+ const f1 = new Foo();
+ assert.equal(isWeak(f1), true, "f1 has WeakReference support");
+
+ f1.dispose();
+ assert.equal(disposals, 1, "disposed on dispose");
+
+ loader.unload("uninstall");
+ assert.equal(disposals, 1, "after disposeDisposable, dispose is not called anymore");
+};
+
+exports["test destroy reasons"] = assert => {
+ let disposals = 0;
+
+ const Foo = Class({
+ extends: Disposable,
+ dispose: function() {
+ disposals = disposals + 1;
+ }
+ });
+
+ const f1 = new Foo();
+
+ f1.destroy();
+ assert.equal(disposals, 1, "disposed on destroy");
+ f1.destroy();
+ assert.equal(disposals, 1, "second destroy is ignored");
+
+ disposals = 0;
+ const f2 = new Foo();
+
+ f2.destroy("uninstall");
+ assert.equal(disposals, 1, "uninstall invokes disposal");
+ f2.destroy("uninstall")
+ f2.destroy();
+ assert.equal(disposals, 1, "disposal happens just once");
+
+ disposals = 0;
+ const f3 = new Foo();
+
+ f3.destroy("shutdown");
+ assert.equal(disposals, 0, "shutdown invoke disposal");
+ f3.destroy("shutdown");
+ f3.destroy();
+ assert.equal(disposals, 0, "shutdown disposal happens just once");
+
+ disposals = 0;
+ const f4 = new Foo();
+
+ f4.destroy("disable");
+ assert.equal(disposals, 1, "disable invokes disposal");
+ f4.destroy("disable")
+ f4.destroy();
+ assert.equal(disposals, 1, "destroy happens just once");
+
+ disposals = 0;
+ const f5 = new Foo();
+
+ f5.destroy("disable");
+ assert.equal(disposals, 1, "disable invokes disposal");
+ f5.destroy("disable")
+ f5.destroy();
+ assert.equal(disposals, 1, "destroy happens just once");
+
+ disposals = 0;
+ const f6 = new Foo();
+
+ f6.destroy("upgrade");
+ assert.equal(disposals, 1, "upgrade invokes disposal");
+ f6.destroy("upgrade")
+ f6.destroy();
+ assert.equal(disposals, 1, "destroy happens just once");
+
+ disposals = 0;
+ const f7 = new Foo();
+
+ f7.destroy("downgrade");
+ assert.equal(disposals, 1, "downgrade invokes disposal");
+ f7.destroy("downgrade")
+ f7.destroy();
+ assert.equal(disposals, 1, "destroy happens just once");
+
+
+ disposals = 0;
+ const f8 = new Foo();
+
+ f8.destroy("whatever");
+ assert.equal(disposals, 1, "unrecognized reason invokes disposal");
+ f8.destroy("meh")
+ f8.destroy();
+ assert.equal(disposals, 1, "destroy happens just once");
+};
+
+exports["test different unload hooks"] = assert => {
+ const { uninstall, shutdown, disable, upgrade,
+ downgrade, dispose } = require("sdk/core/disposable");
+ const UberUnload = Class({
+ extends: Disposable,
+ setup: function() {
+ this.log = [];
+ }
+ });
+
+ uninstall.define(UberUnload, x => x.log.push("uninstall"));
+ shutdown.define(UberUnload, x => x.log.push("shutdown"));
+ disable.define(UberUnload, x => x.log.push("disable"));
+ upgrade.define(UberUnload, x => x.log.push("upgrade"));
+ downgrade.define(UberUnload, x => x.log.push("downgrade"));
+ dispose.define(UberUnload, x => x.log.push("dispose"));
+
+ const u1 = new UberUnload();
+ u1.destroy("uninstall");
+ u1.destroy();
+ u1.destroy("shutdown");
+ assert.deepEqual(u1.log, ["uninstall"], "uninstall hook invoked");
+
+ const u2 = new UberUnload();
+ u2.destroy("shutdown");
+ u2.destroy();
+ u2.destroy("uninstall");
+ assert.deepEqual(u2.log, ["shutdown"], "shutdown hook invoked");
+
+ const u3 = new UberUnload();
+ u3.destroy("disable");
+ u3.destroy();
+ u3.destroy("uninstall");
+ assert.deepEqual(u3.log, ["disable"], "disable hook invoked");
+
+ const u4 = new UberUnload();
+ u4.destroy("upgrade");
+ u4.destroy();
+ u4.destroy("uninstall");
+ assert.deepEqual(u4.log, ["upgrade"], "upgrade hook invoked");
+
+ const u5 = new UberUnload();
+ u5.destroy("downgrade");
+ u5.destroy();
+ u5.destroy("uninstall");
+ assert.deepEqual(u5.log, ["downgrade"], "downgrade hook invoked");
+
+ const u6 = new UberUnload();
+ u6.destroy();
+ u6.destroy();
+ u6.destroy("uninstall");
+ assert.deepEqual(u6.log, ["dispose"], "dispose hook invoked");
+
+ const u7 = new UberUnload();
+ u7.destroy("whatever");
+ u7.destroy();
+ u7.destroy("uninstall");
+ assert.deepEqual(u7.log, ["dispose"], "dispose hook invoked");
+};
+
+exports["test disposables are disposed on unload"] = function(assert) {
+ let loader = Loader(module);
+ let { Disposable } = loader.require("sdk/core/disposable");
+
+ let arg1 = {}
+ let arg2 = 2
+ let disposals = 0
+
+ let Foo = Class({
+ extends: Disposable,
+ setup: function setup(a, b) {
+ assert.equal(a, arg1,
+ "arguments passed to constructur is passed to setup");
+ assert.equal(b, arg2,
+ "second argument is also passed to a setup");
+ assert.ok(this instanceof Foo,
+ "this is an instance in the scope of the setup method");
+
+ this.fooed = true
+ },
+ dispose: function dispose() {
+ assert.equal(this.fooed, true, "attribute was set")
+ this.fooed = false
+ disposals = disposals + 1
+ }
+ })
+
+ let foo1 = Foo(arg1, arg2)
+ let foo2 = Foo(arg1, arg2)
+
+ loader.unload();
+
+ assert.equal(disposals, 2, "both instances were disposed")
+}
+
+exports["test destroyed windows dispose before unload"] = function(assert) {
+ let loader = Loader(module);
+ let { Disposable } = loader.require("sdk/core/disposable");
+
+ let arg1 = {}
+ let arg2 = 2
+ let disposals = 0
+
+ let Foo = Class({
+ extends: Disposable,
+ setup: function setup(a, b) {
+ assert.equal(a, arg1,
+ "arguments passed to constructur is passed to setup");
+ assert.equal(b, arg2,
+ "second argument is also passed to a setup");
+ assert.ok(this instanceof Foo,
+ "this is an instance in the scope of the setup method");
+
+ this.fooed = true
+ },
+ dispose: function dispose() {
+ assert.equal(this.fooed, true, "attribute was set")
+ this.fooed = false
+ disposals = disposals + 1
+ }
+ })
+
+ let foo1 = Foo(arg1, arg2)
+ let foo2 = Foo(arg1, arg2)
+
+ foo1.destroy();
+ assert.equal(disposals, 1, "destroy disposes instance")
+
+ loader.unload();
+
+ assert.equal(disposals, 2, "unload disposes alive instances")
+}
+
+exports["test disposables are GC-able"] = function(assert, done) {
+ let loader = Loader(module);
+ let { Disposable } = loader.require("sdk/core/disposable");
+ let { WeakReference } = loader.require("sdk/core/reference");
+
+ let arg1 = {}
+ let arg2 = 2
+ let disposals = 0
+
+ let Foo = Class({
+ extends: Disposable,
+ implements: [WeakReference],
+ setup: function setup(a, b) {
+ assert.equal(a, arg1,
+ "arguments passed to constructur is passed to setup");
+ assert.equal(b, arg2,
+ "second argument is also passed to a setup");
+ assert.ok(this instanceof Foo,
+ "this is an instance in the scope of the setup method");
+
+ this.fooed = true
+ },
+ dispose: function dispose() {
+ assert.equal(this.fooed, true, "attribute was set")
+ this.fooed = false
+ disposals = disposals + 1
+ }
+ });
+
+ let foo1 = Foo(arg1, arg2)
+ let foo2 = Foo(arg1, arg2)
+
+ foo1 = foo2 = null;
+
+ Cu.schedulePreciseGC(function() {
+ loader.unload();
+ assert.equal(disposals, 0, "GC removed dispose listeners");
+ done();
+ });
+}
+
+
+exports["test loader unloads do not affect other loaders"] = function(assert) {
+ let loader1 = Loader(module);
+ let loader2 = Loader(module);
+ let { Disposable: Disposable1 } = loader1.require("sdk/core/disposable");
+ let { Disposable: Disposable2 } = loader2.require("sdk/core/disposable");
+
+ let arg1 = {}
+ let arg2 = 2
+ let disposals = 0
+
+ let Foo1 = Class({
+ extends: Disposable1,
+ dispose: function dispose() {
+ disposals = disposals + 1;
+ }
+ });
+
+ let Foo2 = Class({
+ extends: Disposable2,
+ dispose: function dispose() {
+ disposals = disposals + 1;
+ }
+ });
+
+ let foo1 = Foo1(arg1, arg2);
+ let foo2 = Foo2(arg1, arg2);
+
+ assert.equal(disposals, 0, "no destroy calls");
+
+ loader1.unload();
+
+ assert.equal(disposals, 1, "1 destroy calls");
+
+ loader2.unload();
+
+ assert.equal(disposals, 2, "2 destroy calls");
+}
+
+exports["test disposables that throw"] = function(assert) {
+ let loader = Loader(module);
+ let { Disposable } = loader.require("sdk/core/disposable");
+
+ let disposals = 0
+
+ let Foo = Class({
+ extends: Disposable,
+ setup: function setup(a, b) {
+ throw Error("Boom!")
+ },
+ dispose: function dispose() {
+ disposals = disposals + 1
+ }
+ })
+
+ assert.throws(function() {
+ let foo1 = Foo()
+ }, /Boom/, "disposable constructors may throw");
+
+ loader.unload();
+
+ assert.equal(disposals, 0, "no disposal if constructor threw");
+}
+
+exports["test multiple destroy"] = function(assert) {
+ let loader = Loader(module);
+ let { Disposable } = loader.require("sdk/core/disposable");
+
+ let disposals = 0
+
+ let Foo = Class({
+ extends: Disposable,
+ dispose: function dispose() {
+ disposals = disposals + 1
+ }
+ })
+
+ let foo1 = Foo();
+ let foo2 = Foo();
+ let foo3 = Foo();
+
+ assert.equal(disposals, 0, "no disposals yet");
+
+ foo1.destroy();
+ assert.equal(disposals, 1, "disposed properly");
+ foo1.destroy();
+ assert.equal(disposals, 1, "didn't attempt to dispose twice");
+
+ foo2.destroy();
+ assert.equal(disposals, 2, "other instances still dispose fine");
+ foo2.destroy();
+ assert.equal(disposals, 2, "but not twice");
+
+ loader.unload();
+
+ assert.equal(disposals, 3, "unload only disposed the remaining instance");
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-dom.js b/addon-sdk/source/test/test-dom.js
new file mode 100644
index 000000000..0b50a7639
--- /dev/null
+++ b/addon-sdk/source/test/test-dom.js
@@ -0,0 +1,88 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const events = require("sdk/dom/events");
+const { activeBrowserWindow: { document } } = require("sdk/deprecated/window-utils");
+const window = document.window;
+/*
+exports["test on / emit"] = function (assert, done) {
+ let element = document.createElement("div");
+ events.on(element, "click", function listener(event) {
+ assert.equal(event.target, element, "event has correct target");
+ events.removeListener(element, "click", listener);
+ done();
+ });
+
+ events.emit(element, "click", {
+ category: "MouseEvents",
+ settings: [
+ true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null
+ ]
+ });
+};
+
+exports["test remove"] = function (assert, done) {
+ let element = document.createElement("span");
+ let l1 = 0;
+ let l2 = 0;
+ let options = {
+ category: "MouseEvents",
+ settings: [
+ true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null
+ ]
+ };
+
+ events.on(element, "click", function listener1(event) {
+ l1 ++;
+ assert.equal(event.target, element, "event has correct target");
+ events.removeListener(element, "click", listener1);
+ });
+
+ events.on(element, "click", function listener2(event) {
+ l2 ++;
+ if (l1 < l2) {
+ assert.equal(l1, 1, "firs listener was called and then romeved");
+ events.removeListener(element, "click", listener2);
+ done();
+ }
+ events.emit(element, "click", options);
+ });
+
+ events.emit(element, "click", options);
+};
+
+exports["test once"] = function (assert, done) {
+ let element = document.createElement("h1");
+ let l1 = 0;
+ let l2 = 0;
+ let options = {
+ category: "MouseEvents",
+ settings: [
+ true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null
+ ]
+ };
+
+
+ events.once(element, "click", function listener(event) {
+ assert.equal(event.target, element, "event target is a correct element");
+ l1 ++;
+ });
+
+ events.on(element, "click", function listener(event) {
+ l2 ++;
+ if (l2 > 3) {
+ events.removeListener(element, "click", listener);
+ assert.equal(event.target, element, "event has correct target");
+ assert.equal(l1, 1, "once was called only once");
+ done();
+ }
+ events.emit(element, "click", options);
+ });
+
+ events.emit(element, "click", options);
+}
+*/
+require("test").run(exports);
diff --git a/addon-sdk/source/test/test-environment.js b/addon-sdk/source/test/test-environment.js
new file mode 100644
index 000000000..9fec6f83d
--- /dev/null
+++ b/addon-sdk/source/test/test-environment.js
@@ -0,0 +1,49 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { env } = require('sdk/system/environment');
+const { Cc, Ci } = require('chrome');
+const { get, set, exists } = Cc['@mozilla.org/process/environment;1'].
+ getService(Ci.nsIEnvironment);
+
+exports['test exists'] = function(assert) {
+ assert.equal('PATH' in env, exists('PATH'),
+ 'PATH environment variable is defined');
+ assert.equal('FOO1' in env, exists('FOO1'),
+ 'FOO1 environment variable is not defined');
+ set('FOO1', 'foo');
+ assert.equal('FOO1' in env, true,
+ 'FOO1 environment variable was set');
+ set('FOO1', null);
+ assert.equal('FOO1' in env, false,
+ 'FOO1 environment variable was unset');
+};
+
+exports['test get'] = function(assert) {
+ assert.equal(env.PATH, get('PATH'), 'PATH env variable matches');
+ assert.equal(env.BAR2, undefined, 'BAR2 env variable is not defined');
+ set('BAR2', 'bar');
+ assert.equal(env.BAR2, 'bar', 'BAR2 env variable was set');
+ set('BAR2', null);
+ assert.equal(env.BAR2, undefined, 'BAR2 env variable was unset');
+};
+
+exports['test set'] = function(assert) {
+ assert.equal(get('BAZ3'), '', 'BAZ3 env variable is not set');
+ assert.equal(env.BAZ3, undefined, 'BAZ3 is not set');
+ env.BAZ3 = 'baz';
+ assert.equal(env.BAZ3, get('BAZ3'), 'BAZ3 env variable is set');
+ assert.equal(get('BAZ3'), 'baz', 'BAZ3 env variable was set to "baz"');
+};
+
+exports['test unset'] = function(assert) {
+ env.BLA4 = 'bla';
+ assert.equal(env.BLA4, 'bla', 'BLA4 env variable is set');
+ assert.equal(delete env.BLA4, true, 'BLA4 env variable is removed');
+ assert.equal(env.BLA4, undefined, 'BLA4 env variable is unset');
+ assert.equal('BLA4' in env, false, 'BLA4 env variable no longer exists' );
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-event-core.js b/addon-sdk/source/test/test-event-core.js
new file mode 100644
index 000000000..ca937b259
--- /dev/null
+++ b/addon-sdk/source/test/test-event-core.js
@@ -0,0 +1,347 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { on, once, off, emit, count } = require('sdk/event/core');
+const { LoaderWithHookedConsole } = require("sdk/test/loader");
+const { defer } = require("sdk/core/promise");
+const { gc } = require("sdk/test/memory");
+
+exports['test add a listener'] = function(assert) {
+ let events = [ { name: 'event#1' }, 'event#2' ];
+ let target = { name: 'target' };
+
+ on(target, 'message', function(message) {
+ assert.equal(this, target, 'this is a target object');
+ assert.equal(message, events.shift(), 'message is emitted event');
+ });
+ emit(target, 'message', events[0]);
+ emit(target, 'message', events[0]);
+};
+
+exports['test that listener is unique per type'] = function(assert) {
+ let actual = []
+ let target = {}
+ function listener() { actual.push(1) }
+ on(target, 'message', listener);
+ on(target, 'message', listener);
+ on(target, 'message', listener);
+ on(target, 'foo', listener);
+ on(target, 'foo', listener);
+
+ emit(target, 'message');
+ assert.deepEqual([ 1 ], actual, 'only one message listener added');
+ emit(target, 'foo');
+ assert.deepEqual([ 1, 1 ], actual, 'same listener added for other event');
+};
+
+exports['test event type matters'] = function(assert) {
+ let target = { name: 'target' }
+ on(target, 'message', function() {
+ assert.fail('no event is expected');
+ });
+ on(target, 'done', function() {
+ assert.pass('event is emitted');
+ });
+ emit(target, 'foo')
+ emit(target, 'done');
+};
+
+exports['test all arguments are pasesd'] = function(assert) {
+ let foo = { name: 'foo' }, bar = 'bar';
+ let target = { name: 'target' };
+ on(target, 'message', function(a, b) {
+ assert.equal(a, foo, 'first argument passed');
+ assert.equal(b, bar, 'second argument passed');
+ });
+ emit(target, 'message', foo, bar);
+};
+
+exports['test no side-effects in emit'] = function(assert) {
+ let target = { name: 'target' };
+ on(target, 'message', function() {
+ assert.pass('first listener is called');
+ on(target, 'message', function() {
+ assert.fail('second listener is called');
+ });
+ });
+ emit(target, 'message');
+};
+
+exports['test can remove next listener'] = function(assert) {
+ let target = { name: 'target' };
+ function fail() {
+ return assert.fail('Listener should be removed');
+ };
+
+ on(target, 'data', function() {
+ assert.pass('first litener called');
+ off(target, 'data', fail);
+ });
+ on(target, 'data', fail);
+
+ emit(target, 'data', 'hello');
+};
+
+exports['test order of propagation'] = function(assert) {
+ let actual = [];
+ let target = { name: 'target' };
+ on(target, 'message', function() { actual.push(1); });
+ on(target, 'message', function() { actual.push(2); });
+ on(target, 'message', function() { actual.push(3); });
+ emit(target, 'message');
+ assert.deepEqual([ 1, 2, 3 ], actual, 'called in order they were added');
+};
+
+exports['test remove a listener'] = function(assert) {
+ let target = { name: 'target' };
+ let actual = [];
+ on(target, 'message', function listener() {
+ actual.push(1);
+ on(target, 'message', function() {
+ off(target, 'message', listener);
+ actual.push(2);
+ })
+ });
+
+ emit(target, 'message');
+ assert.deepEqual([ 1 ], actual, 'first listener called');
+ emit(target, 'message');
+ assert.deepEqual([ 1, 1, 2 ], actual, 'second listener called');
+
+ emit(target, 'message');
+ assert.deepEqual([ 1, 1, 2, 2, 2 ], actual, 'first listener removed');
+};
+
+exports['test remove all listeners for type'] = function(assert) {
+ let actual = [];
+ let target = { name: 'target' }
+ on(target, 'message', function() { actual.push(1); });
+ on(target, 'message', function() { actual.push(2); });
+ on(target, 'message', function() { actual.push(3); });
+ on(target, 'bar', function() { actual.push('b') });
+ off(target, 'message');
+
+ emit(target, 'message');
+ emit(target, 'bar');
+
+ assert.deepEqual([ 'b' ], actual, 'all message listeners were removed');
+};
+
+exports['test remove all listeners'] = function(assert) {
+ let actual = [];
+ let target = { name: 'target' }
+ on(target, 'message', function() { actual.push(1); });
+ on(target, 'message', function() { actual.push(2); });
+ on(target, 'message', function() { actual.push(3); });
+ on(target, 'bar', function() { actual.push('b') });
+ off(target);
+
+ emit(target, 'message');
+ emit(target, 'bar');
+
+ assert.deepEqual([], actual, 'all listeners events were removed');
+};
+
+exports['test falsy arguments are fine'] = function(assert) {
+ let type, listener, actual = [];
+ let target = { name: 'target' }
+ on(target, 'bar', function() { actual.push(0) });
+
+ off(target, 'bar', listener);
+ emit(target, 'bar');
+ assert.deepEqual([ 0 ], actual, '3rd bad ard will keep listeners');
+
+ off(target, type);
+ emit(target, 'bar');
+ assert.deepEqual([ 0, 0 ], actual, '2nd bad arg will keep listener');
+
+ off(target, type, listener);
+ emit(target, 'bar');
+ assert.deepEqual([ 0, 0, 0 ], actual, '2nd&3rd bad args will keep listener');
+};
+
+exports['test error handling'] = function(assert) {
+ let target = Object.create(null);
+ let error = Error('boom!');
+
+ on(target, 'message', function() { throw error; })
+ on(target, 'error', function(boom) {
+ assert.equal(boom, error, 'thrown exception causes error event');
+ });
+ emit(target, 'message');
+};
+
+exports['test unhandled exceptions'] = function(assert) {
+ let exceptions = [];
+ let { loader, messages } = LoaderWithHookedConsole(module);
+
+ let { emit, on } = loader.require('sdk/event/core');
+ let target = {};
+ let boom = Error('Boom!');
+ let drax = Error('Draax!!');
+
+ on(target, 'message', function() { throw boom; });
+
+ emit(target, 'message');
+ assert.equal(messages.length, 1, 'Got the first exception');
+ assert.equal(messages[0].type, 'exception', 'The console message is exception');
+ assert.ok(~String(messages[0].msg).indexOf('Boom!'),
+ 'unhandled exception is logged');
+
+ on(target, 'error', function() { throw drax; });
+ emit(target, 'message');
+ assert.equal(messages.length, 2, 'Got the second exception');
+ assert.equal(messages[1].type, 'exception', 'The console message is exception');
+ assert.ok(~String(messages[1].msg).indexOf('Draax!'),
+ 'error in error handler is logged');
+};
+
+exports['test unhandled errors'] = function(assert) {
+ let exceptions = [];
+ let { loader, messages } = LoaderWithHookedConsole(module);
+
+ let { emit } = loader.require('sdk/event/core');
+ let target = {};
+ let boom = Error('Boom!');
+
+ emit(target, 'error', boom);
+ assert.equal(messages.length, 1, 'Error was logged');
+ assert.equal(messages[0].type, 'exception', 'The console message is exception');
+ assert.ok(~String(messages[0].msg).indexOf('Boom!'),
+ 'unhandled exception is logged');
+};
+
+exports['test piped errors'] = function(assert) {
+ let exceptions = [];
+ let { loader, messages } = LoaderWithHookedConsole(module);
+
+ let { emit } = loader.require('sdk/event/core');
+ let { pipe } = loader.require('sdk/event/utils');
+ let target = {};
+ let second = {};
+
+ pipe(target, second);
+ emit(target, 'error', 'piped!');
+
+ assert.equal(messages.length, 1, 'error logged only once, ' +
+ 'considered "handled" on `target` by the catch-all pipe');
+ assert.equal(messages[0].type, 'exception', 'The console message is exception');
+ assert.ok(~String(messages[0].msg).indexOf('piped!'),
+ 'unhandled (piped) exception is logged on `second` target');
+};
+
+exports['test count'] = function(assert) {
+ let target = {};
+
+ assert.equal(count(target, 'foo'), 0, 'no listeners for "foo" events');
+ on(target, 'foo', function() {});
+ assert.equal(count(target, 'foo'), 1, 'listener registered');
+ on(target, 'foo', function() {}, 2, 'another listener registered');
+ off(target)
+ assert.equal(count(target, 'foo'), 0, 'listeners unregistered');
+};
+
+exports['test listen to all events'] = function(assert) {
+ let actual = [];
+ let target = {};
+
+ on(target, 'foo', message => actual.push(message));
+ on(target, '*', (type, ...message) => {
+ actual.push([type].concat(message));
+ });
+
+ emit(target, 'foo', 'hello');
+ assert.equal(actual[0], 'hello',
+ 'non-wildcard listeners still work');
+ assert.deepEqual(actual[1], ['foo', 'hello'],
+ 'wildcard listener called');
+
+ emit(target, 'bar', 'goodbye');
+ assert.deepEqual(actual[2], ['bar', 'goodbye'],
+ 'wildcard listener called for unbound event name');
+};
+
+exports['test once'] = function(assert, done) {
+ let target = {};
+ let called = false;
+ let { resolve, promise } = defer();
+
+ once(target, 'foo', function(value) {
+ assert.ok(!called, "listener called only once");
+ assert.equal(value, "bar", "correct argument was passed");
+ });
+ once(target, 'done', resolve);
+
+ emit(target, 'foo', 'bar');
+ emit(target, 'foo', 'baz');
+ emit(target, 'done', "");
+
+ yield promise;
+};
+
+exports['test once with gc'] = function*(assert) {
+ let target = {};
+ let called = false;
+ let { resolve, promise } = defer();
+
+ once(target, 'foo', function(value) {
+ assert.ok(!called, "listener called only once");
+ assert.equal(value, "bar", "correct argument was passed");
+ });
+ once(target, 'done', resolve);
+
+ yield gc();
+
+ emit(target, 'foo', 'bar');
+ emit(target, 'foo', 'baz');
+ emit(target, 'done', "");
+
+ yield promise;
+};
+
+exports["test removing once"] = function(assert, done) {
+ let target = {};
+
+ function test() {
+ assert.fail("listener was called");
+ }
+
+ once(target, "foo", test);
+ once(target, "done", done);
+
+ off(target, "foo", test);
+
+ assert.pass("emit foo");
+ emit(target, "foo", "bar");
+
+ assert.pass("emit done");
+ emit(target, "done", "");
+};
+
+exports["test removing once with gc"] = function*(assert) {
+ let target = {};
+ let { resolve, promise } = defer();
+
+ function test() {
+ assert.fail("listener was called");
+ }
+
+ once(target, "foo", test);
+ once(target, "done", resolve);
+
+ yield gc();
+
+ off(target, "foo", test);
+
+ assert.pass("emit foo");
+ emit(target, "foo", "bar");
+
+ assert.pass("emit done");
+ emit(target, "done", "");
+
+ yield promise;
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-event-dom.js b/addon-sdk/source/test/test-event-dom.js
new file mode 100644
index 000000000..fbbc6825b
--- /dev/null
+++ b/addon-sdk/source/test/test-event-dom.js
@@ -0,0 +1,92 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { openWindow, closeWindow } = require('./util');
+const { Loader } = require('sdk/test/loader');
+const { getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { Cc, Ci } = require('chrome');
+const els = Cc["@mozilla.org/eventlistenerservice;1"].
+ getService(Ci.nsIEventListenerService);
+
+function countListeners(target, type) {
+ let listeners = els.getListenerInfoFor(target, {});
+ return listeners.filter(listener => listener.type == type).length;
+}
+
+exports['test window close clears listeners'] = function(assert) {
+ let window = yield openWindow();
+ let loader = Loader(module);
+
+ // Any element will do here
+ let gBrowser = window.gBrowser;
+
+ // Other parts of the app may be listening for this
+ let windowListeners = countListeners(window, "DOMWindowClose");
+
+ // We can assume we're the only ones using the test events
+ assert.equal(countListeners(gBrowser, "TestEvent1"), 0, "Should be no listener for test event 1");
+ assert.equal(countListeners(gBrowser, "TestEvent2"), 0, "Should be no listener for test event 2");
+
+ let { open } = loader.require('sdk/event/dom');
+
+ open(gBrowser, "TestEvent1");
+ assert.equal(countListeners(window, "DOMWindowClose"), windowListeners + 1,
+ "Should have added a DOMWindowClose listener");
+ assert.equal(countListeners(gBrowser, "TestEvent1"), 1, "Should be a listener for test event 1");
+ assert.equal(countListeners(gBrowser, "TestEvent2"), 0, "Should be no listener for test event 2");
+
+ open(gBrowser, "TestEvent2");
+ assert.equal(countListeners(window, "DOMWindowClose"), windowListeners + 1,
+ "Should not have added another DOMWindowClose listener");
+ assert.equal(countListeners(gBrowser, "TestEvent1"), 1, "Should be a listener for test event 1");
+ assert.equal(countListeners(gBrowser, "TestEvent2"), 1, "Should be a listener for test event 2");
+
+ window = yield closeWindow(window);
+
+ assert.equal(countListeners(window, "DOMWindowClose"), windowListeners,
+ "Should have removed a DOMWindowClose listener");
+ assert.equal(countListeners(gBrowser, "TestEvent1"), 0, "Should be no listener for test event 1");
+ assert.equal(countListeners(gBrowser, "TestEvent2"), 0, "Should be no listener for test event 2");
+
+ loader.unload();
+};
+
+exports['test unload clears listeners'] = function(assert) {
+ let window = getMostRecentBrowserWindow();
+ let loader = Loader(module);
+
+ // Any element will do here
+ let gBrowser = window.gBrowser;
+
+ // Other parts of the app may be listening for this
+ let windowListeners = countListeners(window, "DOMWindowClose");
+
+ // We can assume we're the only ones using the test events
+ assert.equal(countListeners(gBrowser, "TestEvent1"), 0, "Should be no listener for test event 1");
+ assert.equal(countListeners(gBrowser, "TestEvent2"), 0, "Should be no listener for test event 2");
+
+ let { open } = loader.require('sdk/event/dom');
+
+ open(gBrowser, "TestEvent1");
+ assert.equal(countListeners(window, "DOMWindowClose"), windowListeners + 1,
+ "Should have added a DOMWindowClose listener");
+ assert.equal(countListeners(gBrowser, "TestEvent1"), 1, "Should be a listener for test event 1");
+ assert.equal(countListeners(gBrowser, "TestEvent2"), 0, "Should be no listener for test event 2");
+
+ open(gBrowser, "TestEvent2");
+ assert.equal(countListeners(window, "DOMWindowClose"), windowListeners + 1,
+ "Should not have added another DOMWindowClose listener");
+ assert.equal(countListeners(gBrowser, "TestEvent1"), 1, "Should be a listener for test event 1");
+ assert.equal(countListeners(gBrowser, "TestEvent2"), 1, "Should be a listener for test event 2");
+
+ loader.unload();
+
+ assert.equal(countListeners(window, "DOMWindowClose"), windowListeners,
+ "Should have removed a DOMWindowClose listener");
+ assert.equal(countListeners(gBrowser, "TestEvent1"), 0, "Should be no listener for test event 1");
+ assert.equal(countListeners(gBrowser, "TestEvent2"), 0, "Should be no listener for test event 2");
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-event-target.js b/addon-sdk/source/test/test-event-target.js
new file mode 100644
index 000000000..d51314aa5
--- /dev/null
+++ b/addon-sdk/source/test/test-event-target.js
@@ -0,0 +1,222 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { emit } = require('sdk/event/core');
+const { EventTarget } = require('sdk/event/target');
+const { Loader } = require('sdk/test/loader');
+
+exports['test add a listener'] = function(assert) {
+ let events = [ { name: 'event#1' }, 'event#2' ];
+ let target = EventTarget();
+
+ target.on('message', function(message) {
+ assert.equal(this, target, 'this is a target object');
+ assert.equal(message, events.shift(), 'message is emitted event');
+ });
+
+ emit(target, 'message', events[0]);
+ emit(target, 'message', events[0]);
+};
+
+exports['test pass in listeners'] = function(assert) {
+ let actual = [ ];
+ let target = EventTarget({
+ onMessage: function onMessage(message) {
+ assert.equal(this, target, 'this is an event target');
+ actual.push(1);
+ },
+ onFoo: null,
+ onbla: function() {
+ assert.fail('`onbla` is not supposed to be called');
+ }
+ });
+ target.on('message', function(message) {
+ assert.equal(this, target, 'this is an event target');
+ actual.push(2);
+ });
+
+ emit(target, 'message');
+ emit(target, 'missing');
+
+ assert.deepEqual([ 1, 2 ], actual, 'all listeners trigerred in right order');
+};
+
+exports['test that listener is unique per type'] = function(assert) {
+ let actual = []
+ let target = EventTarget();
+ function listener() { actual.push(1) }
+ target.on('message', listener);
+ target.on('message', listener);
+ target.on('message', listener);
+ target.on('foo', listener);
+ target.on('foo', listener);
+
+ emit(target, 'message');
+ assert.deepEqual([ 1 ], actual, 'only one message listener added');
+ emit(target, 'foo');
+ assert.deepEqual([ 1, 1 ], actual, 'same listener added for other event');
+};
+
+exports['test event type matters'] = function(assert) {
+ let target = EventTarget();
+ target.on('message', function() {
+ assert.fail('no event is expected');
+ });
+ target.on('done', function() {
+ assert.pass('event is emitted');
+ });
+
+ emit(target, 'foo');
+ emit(target, 'done');
+};
+
+exports['test all arguments are pasesd'] = function(assert) {
+ let foo = { name: 'foo' }, bar = 'bar';
+ let target = EventTarget();
+ target.on('message', function(a, b) {
+ assert.equal(a, foo, 'first argument passed');
+ assert.equal(b, bar, 'second argument passed');
+ });
+ emit(target, 'message', foo, bar);
+};
+
+exports['test no side-effects in emit'] = function(assert) {
+ let target = EventTarget();
+ target.on('message', function() {
+ assert.pass('first listener is called');
+ target.on('message', function() {
+ assert.fail('second listener is called');
+ });
+ });
+ emit(target, 'message');
+};
+
+exports['test order of propagation'] = function(assert) {
+ let actual = [];
+ let target = EventTarget();
+ target.on('message', function() { actual.push(1); });
+ target.on('message', function() { actual.push(2); });
+ target.on('message', function() { actual.push(3); });
+ emit(target, 'message');
+ assert.deepEqual([ 1, 2, 3 ], actual, 'called in order they were added');
+};
+
+exports['test remove a listener'] = function(assert) {
+ let target = EventTarget();
+ let actual = [];
+ target.on('message', function listener() {
+ actual.push(1);
+ target.on('message', function() {
+ target.removeListener('message', listener);
+ actual.push(2);
+ })
+ });
+
+ emit(target, 'message');
+ assert.deepEqual([ 1 ], actual, 'first listener called');
+ emit(target, 'message');
+ assert.deepEqual([ 1, 1, 2 ], actual, 'second listener called');
+ emit(target, 'message');
+ assert.deepEqual([ 1, 1, 2, 2, 2 ], actual, 'first listener removed');
+};
+
+exports['test .off() removes all listeners'] = function(assert) {
+ let target = EventTarget();
+ let actual = [];
+ target.on('message', function listener() {
+ actual.push(1);
+ target.on('message', function() {
+ target.removeListener('message', listener);
+ actual.push(2);
+ })
+ });
+
+ emit(target, 'message');
+ assert.deepEqual([ 1 ], actual, 'first listener called');
+ emit(target, 'message');
+ assert.deepEqual([ 1, 1, 2 ], actual, 'second listener called');
+ target.off();
+ emit(target, 'message');
+ assert.deepEqual([ 1, 1, 2 ], actual, 'target.off() removed all listeners');
+};
+
+exports['test error handling'] = function(assert) {
+ let target = EventTarget();
+ let error = Error('boom!');
+
+ target.on('message', function() { throw error; })
+ target.on('error', function(boom) {
+ assert.equal(boom, error, 'thrown exception causes error event');
+ });
+ emit(target, 'message');
+};
+
+exports['test unhandled errors'] = function(assert) {
+ let exceptions = [];
+ let loader = Loader(module);
+ let { emit } = loader.require('sdk/event/core');
+ let { EventTarget } = loader.require('sdk/event/target');
+ Object.defineProperties(loader.sandbox('sdk/event/core'), {
+ console: { value: {
+ exception: function(e) {
+ exceptions.push(e);
+ }
+ }}
+ });
+ let target = EventTarget();
+ let boom = Error('Boom!');
+ let drax = Error('Draax!!');
+
+ target.on('message', function() { throw boom; });
+
+ emit(target, 'message');
+ assert.ok(~String(exceptions[0]).indexOf('Boom!'),
+ 'unhandled exception is logged');
+
+ target.on('error', function() { throw drax; });
+ emit(target, 'message');
+ assert.ok(~String(exceptions[1]).indexOf('Draax!'),
+ 'error in error handler is logged');
+};
+
+exports['test target is chainable'] = function (assert, done) {
+ let loader = Loader(module);
+ let exceptions = [];
+ let { EventTarget } = loader.require('sdk/event/target');
+ let { emit } = loader.require('sdk/event/core');
+ Object.defineProperties(loader.sandbox('sdk/event/core'), {
+ console: { value: {
+ exception: function(e) {
+ exceptions.push(e);
+ }
+ }}
+ });
+
+ let emitter = EventTarget();
+ let boom = Error('Boom');
+ let onceCalled = 0;
+
+ emitter.once('oneTime', function () {
+ assert.equal(++onceCalled, 1, 'once event called only once');
+ }).on('data', function (message) {
+ assert.equal(message, 'message', 'handles event');
+ emit(emitter, 'oneTime');
+ emit(emitter, 'data2', 'message2');
+ }).on('phony', function () {
+ assert.fail('removeListener does not remove chained event');
+ }).removeListener('phony')
+ .on('data2', function (message) {
+ assert.equal(message, 'message2', 'handle chained event');
+ emit(emitter, 'oneTime');
+ throw boom;
+ }).on('error', function (error) {
+ assert.equal(error, boom, 'error handled in chained event');
+ done();
+ });
+
+ emit(emitter, 'data', 'message');
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-event-utils.js b/addon-sdk/source/test/test-event-utils.js
new file mode 100644
index 000000000..ea69e7677
--- /dev/null
+++ b/addon-sdk/source/test/test-event-utils.js
@@ -0,0 +1,285 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { on, emit } = require("sdk/event/core");
+const { filter, map, merge, expand, pipe, stripListeners } = require("sdk/event/utils");
+const $ = require("./event/helpers");
+
+function isEven(x) {
+ return !(x % 2);
+}
+function inc(x) {
+ return x + 1;
+}
+
+exports["test filter events"] = function(assert) {
+ let input = {};
+ let evens = filter(input, isEven);
+ let actual = [];
+ on(evens, "data", e => actual.push(e));
+
+ [1, 2, 3, 4, 5, 6, 7].forEach(x => emit(input, "data", x));
+
+ assert.deepEqual(actual, [2, 4, 6], "only even numbers passed through");
+};
+
+exports["test filter emits"] = $.emits(function(input, assert) {
+ let output = filter(input, isEven);
+ assert(output, [1, 2, 3, 4, 5], [2, 4], "this is `output` & evens passed");
+});;
+
+exports["test filter reg once"] = $.registerOnce(function(input, assert) {
+ assert(filter(input, isEven), [1, 2, 3, 4, 5, 6], [2, 4, 6],
+ "listener can be registered only once");
+});
+
+exports["test filter ignores new"] = $.ignoreNew(function(input, assert) {
+ assert(filter(input, isEven), [1, 2, 3], [2],
+ "new listener is ignored")
+});
+
+exports["test filter is FIFO"] = $.FIFO(function(input, assert) {
+ assert(filter(input, isEven), [1, 2, 3, 4], [2, 4],
+ "listeners are invoked in fifo order")
+});
+
+exports["test map events"] = function(assert) {
+ let input = {};
+ let incs = map(input, inc);
+ let actual = [];
+ on(incs, "data", e => actual.push(e));
+
+ [1, 2, 3, 4].forEach(x => emit(input, "data", x));
+
+ assert.deepEqual(actual, [2, 3, 4, 5], "all numbers were incremented");
+};
+
+exports["test map emits"] = $.emits(function(input, assert) {
+ let output = map(input, inc);
+ assert(output, [1, 2, 3], [2, 3, 4], "this is `output` & evens passed");
+});
+
+exports["test map reg once"] = $.registerOnce(function(input, assert) {
+ assert(map(input, inc), [1, 2, 3], [2, 3, 4],
+ "listener can be registered only once");
+});
+
+exports["test map ignores new"] = $.ignoreNew(function(input, assert) {
+ assert(map(input, inc), [1], [2],
+ "new listener is ignored")
+});
+
+exports["test map is FIFO"] = $.FIFO(function(input, assert) {
+ assert(map(input, inc), [1, 2, 3, 4], [2, 3, 4, 5],
+ "listeners are invoked in fifo order")
+});
+
+exports["test merge stream[stream]"] = function(assert) {
+ let a = {}, b = {}, c = {};
+ let inputs = {};
+ let actual = [];
+
+ on(merge(inputs), "data", $ => actual.push($))
+
+ emit(inputs, "data", a);
+ emit(a, "data", "a1");
+ emit(inputs, "data", b);
+ emit(b, "data", "b1");
+ emit(a, "data", "a2");
+ emit(inputs, "data", c);
+ emit(c, "data", "c1");
+ emit(c, "data", "c2");
+ emit(b, "data", "b2");
+ emit(a, "data", "a3");
+
+ assert.deepEqual(actual, ["a1", "b1", "a2", "c1", "c2", "b2", "a3"],
+ "all inputs data merged into one");
+};
+
+exports["test merge array[stream]"] = function(assert) {
+ let a = {}, b = {}, c = {};
+ let inputs = {};
+ let actual = [];
+
+ on(merge([a, b, c]), "data", $ => actual.push($))
+
+ emit(a, "data", "a1");
+ emit(b, "data", "b1");
+ emit(a, "data", "a2");
+ emit(c, "data", "c1");
+ emit(c, "data", "c2");
+ emit(b, "data", "b2");
+ emit(a, "data", "a3");
+
+ assert.deepEqual(actual, ["a1", "b1", "a2", "c1", "c2", "b2", "a3"],
+ "all inputs data merged into one");
+};
+
+exports["test merge emits"] = $.emits(function(input, assert) {
+ let evens = filter(input, isEven)
+ let output = merge([evens, input]);
+ assert(output, [1, 2, 3], [1, 2, 2, 3], "this is `output` & evens passed");
+});
+
+
+exports["test merge reg once"] = $.registerOnce(function(input, assert) {
+ let evens = filter(input, isEven)
+ let output = merge([input, evens]);
+ assert(output, [1, 2, 3, 4], [1, 2, 2, 3, 4, 4],
+ "listener can be registered only once");
+});
+
+exports["test merge ignores new"] = $.ignoreNew(function(input, assert) {
+ let evens = filter(input, isEven)
+ let output = merge([input, evens])
+ assert(output, [1], [1],
+ "new listener is ignored")
+});
+
+exports["test marge is FIFO"] = $.FIFO(function(input, assert) {
+ let evens = filter(input, isEven)
+ let output = merge([input, evens])
+
+ assert(output, [1, 2, 3, 4], [1, 2, 2, 3, 4, 4],
+ "listeners are invoked in fifo order")
+});
+
+exports["test expand"] = function(assert) {
+ let a = {}, b = {}, c = {};
+ let inputs = {};
+ let actual = [];
+
+ on(expand(inputs, $ => $()), "data", $ => actual.push($))
+
+ emit(inputs, "data", () => a);
+ emit(a, "data", "a1");
+ emit(inputs, "data", () => b);
+ emit(b, "data", "b1");
+ emit(a, "data", "a2");
+ emit(inputs, "data", () => c);
+ emit(c, "data", "c1");
+ emit(c, "data", "c2");
+ emit(b, "data", "b2");
+ emit(a, "data", "a3");
+
+ assert.deepEqual(actual, ["a1", "b1", "a2", "c1", "c2", "b2", "a3"],
+ "all inputs data merged into one");
+};
+
+exports["test pipe"] = function (assert, done) {
+ let src = {};
+ let dest = {};
+
+ let aneventCount = 0, multiargsCount = 0;
+ let wildcardCount = {};
+
+ on(dest, 'an-event', arg => {
+ assert.equal(arg, 'my-arg', 'piped argument to event');
+ ++aneventCount;
+ check();
+ });
+ on(dest, 'multiargs', (...data) => {
+ assert.equal(data[0], 'a', 'multiple arguments passed via pipe');
+ assert.equal(data[1], 'b', 'multiple arguments passed via pipe');
+ assert.equal(data[2], 'c', 'multiple arguments passed via pipe');
+ ++multiargsCount;
+ check();
+ });
+
+ on(dest, '*', (name, ...data) => {
+ wildcardCount[name] = (wildcardCount[name] || 0) + 1;
+ if (name === 'multiargs') {
+ assert.equal(data[0], 'a', 'multiple arguments passed via pipe, wildcard');
+ assert.equal(data[1], 'b', 'multiple arguments passed via pipe, wildcard');
+ assert.equal(data[2], 'c', 'multiple arguments passed via pipe, wildcard');
+ }
+ if (name === 'an-event')
+ assert.equal(data[0], 'my-arg', 'argument passed via pipe, wildcard');
+ check();
+ });
+
+ pipe(src, dest);
+
+ for (let i = 0; i < 3; i++)
+ emit(src, 'an-event', 'my-arg');
+
+ emit(src, 'multiargs', 'a', 'b', 'c');
+
+ function check () {
+ if (aneventCount === 3 && multiargsCount === 1 &&
+ wildcardCount['an-event'] === 3 &&
+ wildcardCount['multiargs'] === 1)
+ done();
+ }
+};
+
+exports["test pipe multiple targets"] = function (assert) {
+ let src1 = {};
+ let src2 = {};
+ let middle = {};
+ let dest = {};
+
+ pipe(src1, middle);
+ pipe(src2, middle);
+ pipe(middle, dest);
+
+ let middleFired = 0;
+ let destFired = 0;
+ let src1Fired = 0;
+ let src2Fired = 0;
+
+ on(src1, '*', () => src1Fired++);
+ on(src2, '*', () => src2Fired++);
+ on(middle, '*', () => middleFired++);
+ on(dest, '*', () => destFired++);
+
+ emit(src1, 'ev');
+ assert.equal(src1Fired, 1, 'event triggers in source in pipe chain');
+ assert.equal(middleFired, 1, 'event passes through the middle of pipe chain');
+ assert.equal(destFired, 1, 'event propagates to end of pipe chain');
+ assert.equal(src2Fired, 0, 'event does not fire on alternative chain routes');
+
+ emit(src2, 'ev');
+ assert.equal(src2Fired, 1, 'event triggers in source in pipe chain');
+ assert.equal(middleFired, 2,
+ 'event passes through the middle of pipe chain from different src');
+ assert.equal(destFired, 2,
+ 'event propagates to end of pipe chain from different src');
+ assert.equal(src1Fired, 1, 'event does not fire on alternative chain routes');
+
+ emit(middle, 'ev');
+ assert.equal(middleFired, 3,
+ 'event triggers in source of pipe chain');
+ assert.equal(destFired, 3,
+ 'event propagates to end of pipe chain from middle src');
+ assert.equal(src1Fired, 1, 'event does not fire on alternative chain routes');
+ assert.equal(src2Fired, 1, 'event does not fire on alternative chain routes');
+};
+
+exports['test stripListeners'] = function (assert) {
+ var options = {
+ onAnEvent: noop1,
+ onMessage: noop2,
+ verb: noop1,
+ value: 100
+ };
+
+ var stripped = stripListeners(options);
+ assert.ok(stripped !== options, 'stripListeners should return a new object');
+ assert.equal(options.onAnEvent, noop1, 'stripListeners does not affect original');
+ assert.equal(options.onMessage, noop2, 'stripListeners does not affect original');
+ assert.equal(options.verb, noop1, 'stripListeners does not affect original');
+ assert.equal(options.value, 100, 'stripListeners does not affect original');
+
+ assert.ok(!stripped.onAnEvent, 'stripListeners removes `on*` values');
+ assert.ok(!stripped.onMessage, 'stripListeners removes `on*` values');
+ assert.equal(stripped.verb, noop1, 'stripListeners leaves not `on*` values');
+ assert.equal(stripped.value, 100, 'stripListeners leaves not `on*` values');
+
+ function noop1 () {}
+ function noop2 () {}
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-file.js b/addon-sdk/source/test/test-file.js
new file mode 100644
index 000000000..268c7f791
--- /dev/null
+++ b/addon-sdk/source/test/test-file.js
@@ -0,0 +1,271 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { pathFor } = require('sdk/system');
+const file = require("sdk/io/file");
+const url = require("sdk/url");
+
+const byteStreams = require("sdk/io/byte-streams");
+const textStreams = require("sdk/io/text-streams");
+
+const ERRORS = {
+ FILE_NOT_FOUND: /^path does not exist: .+$/,
+ NOT_A_DIRECTORY: /^path is not a directory: .+$/,
+ NOT_A_FILE: /^path is not a file: .+$/,
+};
+
+// Use profile directory to list / read / write files.
+const profilePath = pathFor('ProfD');
+const fileNameInProfile = 'compatibility.ini';
+const dirNameInProfile = 'extensions';
+const filePathInProfile = file.join(profilePath, fileNameInProfile);
+const dirPathInProfile = file.join(profilePath, dirNameInProfile);
+
+exports.testDirName = function(assert) {
+ assert.equal(file.dirname(dirPathInProfile), profilePath,
+ "file.dirname() of dir should return parent dir");
+
+ assert.equal(file.dirname(filePathInProfile), profilePath,
+ "file.dirname() of file should return its dir");
+
+ let dir = profilePath;
+ while (dir)
+ dir = file.dirname(dir);
+
+ assert.equal(dir, "",
+ "dirname should return empty string when dir has no parent");
+};
+
+exports.testBasename = function(assert) {
+ // Get the top-most path -- the path with no basename. E.g., on Unix-like
+ // systems this will be /. We'll use it below to build up some test paths.
+ // We have to go to this trouble because file.join() needs a legal path as a
+ // base case; join("foo", "bar") doesn't work unfortunately.
+ let topPath = profilePath;
+ let parentPath = file.dirname(topPath);
+ while (parentPath) {
+ topPath = parentPath;
+ parentPath = file.dirname(topPath);
+ }
+
+ let path = topPath;
+ assert.equal(file.basename(path), "",
+ "basename should work on paths with no components");
+
+ path = file.join(path, "foo");
+ assert.equal(file.basename(path), "foo",
+ "basename should work on paths with a single component");
+
+ path = file.join(path, "bar");
+ assert.equal(file.basename(path), "bar",
+ "basename should work on paths with multiple components");
+};
+
+exports.testList = function(assert) {
+ let list = file.list(profilePath);
+ let found = list.filter(name => name === fileNameInProfile);
+
+ assert.equal(found.length, 1, "file.list() should work");
+
+ assert.throws(function() {
+ file.list(filePathInProfile);
+ }, ERRORS.NOT_A_DIRECTORY, "file.list() on non-dir should raise error");
+
+ assert.throws(function() {
+ file.list(file.join(dirPathInProfile, "does-not-exist"));
+ }, ERRORS.FILE_NOT_FOUND, "file.list() on nonexistent should raise error");
+};
+
+exports.testRead = function(assert) {
+ let contents = file.read(filePathInProfile);
+ assert.ok(/Compatibility/.test(contents),
+ "file.read() should work");
+
+ assert.throws(function() {
+ file.read(file.join(dirPathInProfile, "does-not-exists"));
+ }, ERRORS.FILE_NOT_FOUND, "file.read() on nonexistent file should throw");
+
+ assert.throws(function() {
+ file.read(dirPathInProfile);
+ }, ERRORS.NOT_A_FILE, "file.read() on dir should throw");
+};
+
+exports.testJoin = function(assert) {
+ let baseDir = file.dirname(filePathInProfile);
+
+ assert.equal(file.join(baseDir, fileNameInProfile),
+ filePathInProfile, "file.join() should work");
+};
+
+exports.testOpenNonexistentForRead = function (assert) {
+ var filename = file.join(profilePath, 'does-not-exists');
+ assert.throws(function() {
+ file.open(filename);
+ }, ERRORS.FILE_NOT_FOUND, "file.open() on nonexistent file should throw");
+
+ assert.throws(function() {
+ file.open(filename, "r");
+ }, ERRORS.FILE_NOT_FOUND, "file.open('r') on nonexistent file should throw");
+
+ assert.throws(function() {
+ file.open(filename, "zz");
+ }, ERRORS.FILE_NOT_FOUND, "file.open('zz') on nonexistent file should throw");
+};
+
+exports.testOpenNonexistentForWrite = function (assert) {
+ let filename = file.join(profilePath, 'open.txt');
+
+ let stream = file.open(filename, "w");
+ stream.close();
+
+ assert.ok(file.exists(filename),
+ "file.exists() should return true after file.open('w')");
+ file.remove(filename);
+ assert.ok(!file.exists(filename),
+ "file.exists() should return false after file.remove()");
+
+ stream = file.open(filename, "rw");
+ stream.close();
+
+ assert.ok(file.exists(filename),
+ "file.exists() should return true after file.open('rw')");
+ file.remove(filename);
+ assert.ok(!file.exists(filename),
+ "file.exists() should return false after file.remove()");
+};
+
+exports.testOpenDirectory = function (assert) {
+ let dir = dirPathInProfile;
+ assert.throws(function() {
+ file.open(dir);
+ }, ERRORS.NOT_A_FILE, "file.open() on directory should throw");
+
+ assert.throws(function() {
+ file.open(dir, "w");
+ }, ERRORS.NOT_A_FILE, "file.open('w') on directory should throw");
+};
+
+exports.testOpenTypes = function (assert) {
+ let filename = file.join(profilePath, 'open-types.txt');
+
+
+ // Do the opens first to create the data file.
+ var stream = file.open(filename, "w");
+ assert.ok(stream instanceof textStreams.TextWriter,
+ "open(w) should return a TextWriter");
+ stream.close();
+
+ stream = file.open(filename, "wb");
+ assert.ok(stream instanceof byteStreams.ByteWriter,
+ "open(wb) should return a ByteWriter");
+ stream.close();
+
+ stream = file.open(filename);
+ assert.ok(stream instanceof textStreams.TextReader,
+ "open() should return a TextReader");
+ stream.close();
+
+ stream = file.open(filename, "r");
+ assert.ok(stream instanceof textStreams.TextReader,
+ "open(r) should return a TextReader");
+ stream.close();
+
+ stream = file.open(filename, "b");
+ assert.ok(stream instanceof byteStreams.ByteReader,
+ "open(b) should return a ByteReader");
+ stream.close();
+
+ stream = file.open(filename, "rb");
+ assert.ok(stream instanceof byteStreams.ByteReader,
+ "open(rb) should return a ByteReader");
+ stream.close();
+
+ file.remove(filename);
+};
+
+exports.testMkpathRmdir = function (assert) {
+ let basePath = profilePath;
+ let dirs = [];
+ for (let i = 0; i < 3; i++)
+ dirs.push("test-file-dir");
+
+ let paths = [];
+ for (let i = 0; i < dirs.length; i++) {
+ let args = [basePath].concat(dirs.slice(0, i + 1));
+ paths.unshift(file.join.apply(null, args));
+ }
+
+ for (let i = 0; i < paths.length; i++) {
+ assert.ok(!file.exists(paths[i]),
+ "Sanity check: path should not exist: " + paths[i]);
+ }
+
+ file.mkpath(paths[0]);
+ assert.ok(file.exists(paths[0]), "mkpath should create path: " + paths[0]);
+
+ for (let i = 0; i < paths.length; i++) {
+ file.rmdir(paths[i]);
+ assert.ok(!file.exists(paths[i]),
+ "rmdir should remove path: " + paths[i]);
+ }
+};
+
+exports.testMkpathTwice = function (assert) {
+ let dir = profilePath;
+ let path = file.join(dir, "test-file-dir");
+ assert.ok(!file.exists(path),
+ "Sanity check: path should not exist: " + path);
+ file.mkpath(path);
+ assert.ok(file.exists(path), "mkpath should create path: " + path);
+ file.mkpath(path);
+ assert.ok(file.exists(path),
+ "After second mkpath, path should still exist: " + path);
+ file.rmdir(path);
+ assert.ok(!file.exists(path), "rmdir should remove path: " + path);
+};
+
+exports.testMkpathExistingNondirectory = function (assert) {
+ var fname = file.join(profilePath, 'conflict.txt');
+ file.open(fname, "w").close();
+ assert.ok(file.exists(fname), "File should exist");
+ assert.throws(() => file.mkpath(fname),
+ /^The path already exists and is not a directory: .+$/,
+ "mkpath on file should raise error");
+ file.remove(fname);
+};
+
+exports.testRmdirNondirectory = function (assert) {
+ var fname = file.join(profilePath, 'not-a-dir')
+ file.open(fname, "w").close();
+ assert.ok(file.exists(fname), "File should exist");
+ assert.throws(function() {
+ file.rmdir(fname);
+ }, ERRORS.NOT_A_DIRECTORY, "rmdir on file should raise error");
+ file.remove(fname);
+ assert.ok(!file.exists(fname), "File should not exist");
+ assert.throws(() => file.rmdir(fname),
+ ERRORS.FILE_NOT_FOUND,
+ "rmdir on non-existing file should raise error");
+};
+
+exports.testRmdirNonempty = function (assert) {
+ let dir = profilePath;
+ let path = file.join(dir, "test-file-dir");
+ assert.ok(!file.exists(path),
+ "Sanity check: path should not exist: " + path);
+ file.mkpath(path);
+ let filePath = file.join(path, "file");
+ file.open(filePath, "w").close();
+ assert.ok(file.exists(filePath),
+ "Sanity check: path should exist: " + filePath);
+ assert.throws(() => file.rmdir(path),
+ /^The directory is not empty: .+$/,
+ "rmdir on non-empty directory should raise error");
+ file.remove(filePath);
+ file.rmdir(path);
+ assert.ok(!file.exists(path), "Path should not exist");
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-frame-utils.js b/addon-sdk/source/test/test-frame-utils.js
new file mode 100644
index 000000000..501f93c87
--- /dev/null
+++ b/addon-sdk/source/test/test-frame-utils.js
@@ -0,0 +1,59 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { create } = require('sdk/frame/utils');
+const { open, close } = require('sdk/window/helpers');
+
+exports['test frame creation'] = function(assert, done) {
+ open('data:text/html;charset=utf-8,Window').then(function (window) {
+ let frame = create(window.document);
+
+ assert.equal(frame.getAttribute('type'), 'content',
+ 'frame type is content');
+ assert.ok(frame.contentWindow, 'frame has contentWindow');
+ assert.equal(frame.contentWindow.location.href, 'about:blank',
+ 'by default "about:blank" is loaded');
+ assert.equal(frame.docShell.allowAuth, false, 'auth disabled by default');
+ assert.equal(frame.docShell.allowJavascript, false, 'js disabled by default');
+ assert.equal(frame.docShell.allowPlugins, false,
+ 'plugins disabled by default');
+ close(window).then(done);
+ });
+};
+
+exports['test fram has js disabled by default'] = function(assert, done) {
+ open('data:text/html;charset=utf-8,window').then(function (window) {
+ let frame = create(window.document, {
+ uri: 'data:text/html;charset=utf-8,<script>document.documentElement.innerHTML' +
+ '= "J" + "S"</script>',
+ });
+ frame.contentWindow.addEventListener('DOMContentLoaded', function ready() {
+ frame.contentWindow.removeEventListener('DOMContentLoaded', ready, false);
+ assert.ok(!~frame.contentDocument.documentElement.innerHTML.indexOf('JS'),
+ 'JS was executed');
+
+ close(window).then(done);
+ }, false);
+ });
+};
+
+exports['test frame with js enabled'] = function(assert, done) {
+ open('data:text/html;charset=utf-8,window').then(function (window) {
+ let frame = create(window.document, {
+ uri: 'data:text/html;charset=utf-8,<script>document.documentElement.innerHTML' +
+ '= "J" + "S"</script>',
+ allowJavascript: true
+ });
+ frame.contentWindow.addEventListener('DOMContentLoaded', function ready() {
+ frame.contentWindow.removeEventListener('DOMContentLoaded', ready, false);
+ assert.ok(~frame.contentDocument.documentElement.innerHTML.indexOf('JS'),
+ 'JS was executed');
+
+ close(window).then(done);
+ }, false);
+ });
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-framescript-manager.js b/addon-sdk/source/test/test-framescript-manager.js
new file mode 100644
index 000000000..442f71eda
--- /dev/null
+++ b/addon-sdk/source/test/test-framescript-manager.js
@@ -0,0 +1,32 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const {loadModule} = require("framescript/manager");
+const {withTab, receiveMessage} = require("./util");
+const {getBrowserForTab} = require("sdk/tabs/utils");
+
+exports.testLoadModule = withTab(function*(assert, tab) {
+ const {messageManager} = getBrowserForTab(tab);
+
+ loadModule(messageManager,
+ require.resolve("./framescript-manager/frame-script"),
+ true,
+ "onInit");
+
+ const message = yield receiveMessage(messageManager, "framescript-manager/ready");
+
+ assert.deepEqual(message.data, {state: "ready"},
+ "received ready message from the loaded module");
+
+ messageManager.sendAsyncMessage("framescript-manager/ping", {x: 1});
+
+ const pong = yield receiveMessage(messageManager, "framescript-manager/pong");
+
+ assert.deepEqual(pong.data, {x: 1},
+ "received pong back");
+}, "data:text/html,<h1>Message Manager</h1>");
+
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-framescript-util.js b/addon-sdk/source/test/test-framescript-util.js
new file mode 100644
index 000000000..0a55bcbf6
--- /dev/null
+++ b/addon-sdk/source/test/test-framescript-util.js
@@ -0,0 +1,45 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const {loadModule} = require("framescript/manager");
+const {withTab, receiveMessage} = require("./util");
+const {getBrowserForTab} = require("sdk/tabs/utils");
+
+exports["test windowToMessageManager"] = withTab(function*(assert, tab) {
+ const {messageManager} = getBrowserForTab(tab);
+
+ loadModule(messageManager,
+ require.resolve("./framescript-util/frame-script"),
+ true,
+ "onInit");
+
+ messageManager.sendAsyncMessage("framescript-util/window/request");
+
+ const response = yield receiveMessage(messageManager,
+ "framescript-util/window/response");
+
+ assert.deepEqual(response.data, {window: true},
+ "got response");
+}, "data:text/html,<h1>Window to Message Manager</h1>");
+
+
+exports["test nodeToMessageManager"] = withTab(function*(assert, tab) {
+ const {messageManager} = getBrowserForTab(tab);
+
+ loadModule(messageManager,
+ require.resolve("./framescript-util/frame-script"),
+ true,
+ "onInit");
+
+ messageManager.sendAsyncMessage("framescript-util/node/request", "h1");
+
+ const response = yield receiveMessage(messageManager,
+ "framescript-util/node/response");
+
+ assert.deepEqual(response.data, {node: true},
+ "got response");
+}, "data:text/html,<h1>Node to Message Manager</h1>");
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-fs.js b/addon-sdk/source/test/test-fs.js
new file mode 100644
index 000000000..ed26ca3e3
--- /dev/null
+++ b/addon-sdk/source/test/test-fs.js
@@ -0,0 +1,621 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const { pathFor, platform } = require("sdk/system");
+const fs = require("sdk/io/fs");
+const url = require("sdk/url");
+const path = require("sdk/fs/path");
+const { defer } = require("sdk/core/promise");
+const { Buffer } = require("sdk/io/buffer");
+const { is } = require("sdk/system/xul-app");
+
+// Use profile directory to list / read / write files.
+const profilePath = pathFor("ProfD");
+const fileNameInProfile = "compatibility.ini";
+const dirNameInProfile = "extensions";
+const filePathInProfile = path.join(profilePath, fileNameInProfile);
+const dirPathInProfile = path.join(profilePath, dirNameInProfile);
+const mkdirPath = path.join(profilePath, "sdk-fixture-mkdir");
+const writePath = path.join(profilePath, "sdk-fixture-writeFile");
+const unlinkPath = path.join(profilePath, "sdk-fixture-unlink");
+const truncatePath = path.join(profilePath, "sdk-fixture-truncate");
+const renameFromPath = path.join(profilePath, "sdk-fixture-rename-from");
+const renameToPath = path.join(profilePath, "sdk-fixture-rename-to");
+const chmodPath = path.join(profilePath, "sdk-fixture-chmod");
+
+const profileEntries = [
+ "compatibility.ini",
+ "extensions",
+ "prefs.js"
+ // There are likely to be a lot more files but we can"t really
+ // on consistent list so we limit to this.
+];
+
+const isWindows = platform.indexOf('win') === 0;
+
+exports["test readdir"] = function(assert, end) {
+ var async = false;
+ fs.readdir(profilePath, function(error, entries) {
+ assert.ok(async, "readdir is async");
+ assert.ok(!error, "there is no error when reading directory");
+ assert.ok(profileEntries.length <= entries.length,
+ "got at least number of entries we expect");
+ assert.ok(profileEntries.every(function(entry) {
+ return entries.indexOf(entry) >= 0;
+ }), "all profiles are present");
+ end();
+ });
+
+ async = true;
+};
+
+exports["test readdir error"] = function(assert, end) {
+ var async = false;
+ var path = profilePath + "-does-not-exists";
+ fs.readdir(path, function(error, entries) {
+ assert.ok(async, "readdir is async");
+ assert.equal(error.message, "ENOENT, readdir " + path);
+ assert.equal(error.code, "ENOENT", "error has a code");
+ assert.equal(error.path, path, "error has a path");
+ assert.equal(error.errno, 34, "error has a errno");
+ end();
+ });
+
+ async = true;
+};
+
+exports["test readdirSync"] = function(assert) {
+ var async = false;
+ var entries = fs.readdirSync(profilePath);
+ assert.ok(profileEntries.length <= entries.length,
+ "got at least number of entries we expect");
+ assert.ok(profileEntries.every(function(entry) {
+ return entries.indexOf(entry) >= 0;
+ }), "all profiles are present");
+};
+
+exports["test readdirSync error"] = function(assert) {
+ var async = false;
+ var path = profilePath + "-does-not-exists";
+ try {
+ fs.readdirSync(path);
+ assert.fail(Error("No error was thrown"));
+ } catch (error) {
+ assert.equal(error.message, "ENOENT, readdir " + path);
+ assert.equal(error.code, "ENOENT", "error has a code");
+ assert.equal(error.path, path, "error has a path");
+ assert.equal(error.errno, 34, "error has a errno");
+ }
+};
+
+exports["test readFile"] = function(assert, end) {
+ let async = false;
+ fs.readFile(filePathInProfile, function(error, content) {
+ assert.ok(async, "readFile is async");
+ assert.ok(!error, "error is falsy");
+
+ assert.ok(Buffer.isBuffer(content), "readFile returns buffer");
+ assert.ok(typeof(content.length) === "number", "buffer has length");
+ assert.ok(content.toString().indexOf("[Compatibility]") >= 0,
+ "content contains expected data");
+ end();
+ });
+ async = true;
+};
+
+exports["test readFile error"] = function(assert, end) {
+ let async = false;
+ let path = filePathInProfile + "-does-not-exists";
+ fs.readFile(path, function(error, content) {
+ assert.ok(async, "readFile is async");
+ assert.equal(error.message, "ENOENT, open " + path);
+ assert.equal(error.code, "ENOENT", "error has a code");
+ assert.equal(error.path, path, "error has a path");
+ assert.equal(error.errno, 34, "error has a errno");
+
+ end();
+ });
+ async = true;
+};
+
+exports["test readFileSync not implemented"] = function(assert) {
+ let buffer = fs.readFileSync(filePathInProfile);
+ assert.ok(buffer.toString().indexOf("[Compatibility]") >= 0,
+ "read content");
+};
+
+exports["test fs.stat file"] = function(assert, end) {
+ let async = false;
+ let path = filePathInProfile;
+ fs.stat(path, function(error, stat) {
+ assert.ok(async, "fs.stat is async");
+ assert.ok(!error, "error is falsy");
+ assert.ok(!stat.isDirectory(), "not a dir");
+ assert.ok(stat.isFile(), "is a file");
+ assert.ok(!stat.isSymbolicLink(), "isn't a symlink");
+ assert.ok(typeof(stat.size) === "number", "size is a number");
+ assert.ok(stat.exists === true, "file exists");
+ assert.ok(typeof(stat.isBlockDevice()) === "boolean");
+ assert.ok(typeof(stat.isCharacterDevice()) === "boolean");
+ assert.ok(typeof(stat.isFIFO()) === "boolean");
+ assert.ok(typeof(stat.isSocket()) === "boolean");
+ assert.ok(typeof(stat.hidden) === "boolean");
+ assert.ok(typeof(stat.writable) === "boolean")
+ assert.ok(stat.readable === true);
+
+ end();
+ });
+ async = true;
+};
+
+exports["test fs.stat dir"] = function(assert, end) {
+ let async = false;
+ let path = dirPathInProfile;
+ fs.stat(path, function(error, stat) {
+ assert.ok(async, "fs.stat is async");
+ assert.ok(!error, "error is falsy");
+ assert.ok(stat.isDirectory(), "is a dir");
+ assert.ok(!stat.isFile(), "not a file");
+ assert.ok(!stat.isSymbolicLink(), "isn't a symlink");
+ assert.ok(typeof(stat.size) === "number", "size is a number");
+ assert.ok(stat.exists === true, "file exists");
+ assert.ok(typeof(stat.isBlockDevice()) === "boolean");
+ assert.ok(typeof(stat.isCharacterDevice()) === "boolean");
+ assert.ok(typeof(stat.isFIFO()) === "boolean");
+ assert.ok(typeof(stat.isSocket()) === "boolean");
+ assert.ok(typeof(stat.hidden) === "boolean");
+ assert.ok(typeof(stat.writable) === "boolean")
+ assert.ok(typeof(stat.readable) === "boolean");
+
+ end();
+ });
+ async = true;
+};
+
+exports["test fs.stat error"] = function(assert, end) {
+ let async = false;
+ let path = filePathInProfile + "-does-not-exists";
+ fs.stat(path, function(error, stat) {
+ assert.ok(async, "fs.stat is async");
+ assert.equal(error.message, "ENOENT, stat " + path);
+ assert.equal(error.code, "ENOENT", "error has a code");
+ assert.equal(error.path, path, "error has a path");
+ assert.equal(error.errno, 34, "error has a errno");
+
+ end();
+ });
+ async = true;
+};
+
+exports["test fs.exists NO"] = function(assert, end) {
+ let async = false
+ let path = filePathInProfile + "-does-not-exists";
+ fs.exists(path, function(error, value) {
+ assert.ok(async, "fs.exists is async");
+ assert.ok(!error, "error is falsy");
+ assert.ok(!value, "file does not exists");
+ end();
+ });
+ async = true;
+};
+
+exports["test fs.exists YES"] = function(assert, end) {
+ let async = false
+ let path = filePathInProfile
+ fs.exists(path, function(error, value) {
+ assert.ok(async, "fs.exists is async");
+ assert.ok(!error, "error is falsy");
+ assert.ok(value, "file exists");
+ end();
+ });
+ async = true;
+};
+
+exports["test fs.exists NO"] = function(assert, end) {
+ let async = false
+ let path = filePathInProfile + "-does-not-exists";
+ fs.exists(path, function(error, value) {
+ assert.ok(async, "fs.exists is async");
+ assert.ok(!error, "error is falsy");
+ assert.ok(!value, "file does not exists");
+ end();
+ });
+ async = true;
+};
+
+exports["test fs.existsSync"] = function(assert) {
+ let path = filePathInProfile
+ assert.equal(fs.existsSync(path), true, "exists");
+ assert.equal(fs.existsSync(path + "-does-not-exists"), false, "exists");
+};
+
+exports["test fs.mkdirSync fs.rmdirSync"] = function(assert) {
+ let path = mkdirPath;
+
+ assert.equal(fs.existsSync(path), false, "does not exists");
+ fs.mkdirSync(path);
+ assert.equal(fs.existsSync(path), true, "dir was created");
+ try {
+ fs.mkdirSync(path);
+ assert.fail(Error("mkdir on existing should throw"));
+ } catch (error) {
+ assert.equal(error.message, "EEXIST, mkdir " + path);
+ assert.equal(error.code, "EEXIST", "error has a code");
+ assert.equal(error.path, path, "error has a path");
+ assert.equal(error.errno, 47, "error has a errno");
+ }
+ fs.rmdirSync(path);
+ assert.equal(fs.existsSync(path), false, "dir was removed");
+};
+
+exports["test fs.mkdir"] = function(assert, end) {
+ let path = mkdirPath;
+
+ if (!fs.existsSync(path)) {
+ let async = false;
+ fs.mkdir(path, function(error) {
+ assert.ok(async, "mkdir is async");
+ assert.ok(!error, "no error");
+ assert.equal(fs.existsSync(path), true, "dir was created");
+ fs.rmdirSync(path);
+ assert.equal(fs.existsSync(path), false, "dir was removed");
+ end();
+ });
+ async = true;
+ }
+};
+
+exports["test fs.mkdir error"] = function(assert, end) {
+ let path = mkdirPath;
+
+ if (!fs.existsSync(path)) {
+ fs.mkdirSync(path);
+ let async = false;
+ fs.mkdir(path, function(error) {
+ assert.ok(async, "mkdir is async");
+ assert.equal(error.message, "EEXIST, mkdir " + path);
+ assert.equal(error.code, "EEXIST", "error has a code");
+ assert.equal(error.path, path, "error has a path");
+ assert.equal(error.errno, 47, "error has a errno");
+ fs.rmdirSync(path);
+ assert.equal(fs.existsSync(path), false, "dir was removed");
+ end();
+ });
+ async = true;
+ }
+};
+
+exports["test fs.rmdir"] = function(assert, end) {
+ let path = mkdirPath;
+
+ if (!fs.existsSync(path)) {
+ fs.mkdirSync(path);
+ assert.equal(fs.existsSync(path), true, "dir exists");
+ let async = false;
+ fs.rmdir(path, function(error) {
+ assert.ok(async, "mkdir is async");
+ assert.ok(!error, "no error");
+ assert.equal(fs.existsSync(path), false, "dir was removed");
+ end();
+ });
+ async = true;
+ }
+};
+
+
+exports["test fs.rmdir error"] = function(assert, end) {
+ let path = mkdirPath;
+
+ if (!fs.existsSync(path)) {
+ assert.equal(fs.existsSync(path), false, "dir doesn't exists");
+ let async = false;
+ fs.rmdir(path, function(error) {
+ assert.ok(async, "mkdir is async");
+ assert.equal(error.message, "ENOENT, remove " + path);
+ assert.equal(error.code, "ENOENT", "error has a code");
+ assert.equal(error.path, path, "error has a path");
+ assert.equal(error.errno, 34, "error has a errno");
+ assert.equal(fs.existsSync(path), false, "dir is removed");
+ end();
+ });
+ async = true;
+ }
+};
+
+exports["test fs.truncateSync fs.unlinkSync"] = function(assert) {
+ let path = truncatePath;
+
+ assert.equal(fs.existsSync(path), false, "does not exists");
+ fs.truncateSync(path);
+ assert.equal(fs.existsSync(path), true, "file was created");
+ fs.truncateSync(path);
+ fs.unlinkSync(path);
+ assert.equal(fs.existsSync(path), false, "file was removed");
+};
+
+
+exports["test fs.truncate"] = function(assert, end) {
+ let path = truncatePath;
+ if (!fs.existsSync(path)) {
+ let async = false;
+ fs.truncate(path, 0, function(error) {
+ assert.ok(async, "truncate is async");
+ assert.ok(!error, "no error");
+ assert.equal(fs.existsSync(path), true, "file was created");
+ fs.unlinkSync(path);
+ assert.equal(fs.existsSync(path), false, "file was removed");
+ end();
+ })
+ async = true;
+ }
+};
+
+exports["test fs.unlink"] = function(assert, end) {
+ let path = unlinkPath;
+ let async = false;
+ assert.ok(!fs.existsSync(path), "file doesn't exists yet");
+ fs.truncateSync(path, 0);
+ assert.ok(fs.existsSync(path), "file was created");
+ fs.unlink(path, function(error) {
+ assert.ok(async, "fs.unlink is async");
+ assert.ok(!error, "error is falsy");
+ assert.ok(!fs.existsSync(path), "file was removed");
+ end();
+ });
+ async = true;
+};
+
+exports["test fs.unlink error"] = function(assert, end) {
+ let path = unlinkPath;
+ let async = false;
+ assert.ok(!fs.existsSync(path), "file doesn't exists yet");
+ fs.unlink(path, function(error) {
+ assert.ok(async, "fs.unlink is async");
+ assert.equal(error.message, "ENOENT, remove " + path);
+ assert.equal(error.code, "ENOENT", "error has a code");
+ assert.equal(error.path, path, "error has a path");
+ assert.equal(error.errno, 34, "error has a errno");
+ end();
+ });
+ async = true;
+};
+
+exports["test fs.rename"] = function(assert, end) {
+ let fromPath = renameFromPath;
+ let toPath = renameToPath;
+
+ fs.truncateSync(fromPath);
+ assert.ok(fs.existsSync(fromPath), "source file exists");
+ assert.ok(!fs.existsSync(toPath), "destination doesn't exists");
+ var async = false;
+ fs.rename(fromPath, toPath, function(error) {
+ assert.ok(async, "fs.rename is async");
+ assert.ok(!error, "error is falsy");
+ assert.ok(!fs.existsSync(fromPath), "source path no longer exists");
+ assert.ok(fs.existsSync(toPath), "destination file exists");
+ fs.unlinkSync(toPath);
+ assert.ok(!fs.existsSync(toPath), "cleaned up properly");
+ end();
+ });
+ async = true;
+};
+
+exports["test fs.rename (missing source file)"] = function(assert, end) {
+ let fromPath = renameFromPath;
+ let toPath = renameToPath;
+
+ assert.ok(!fs.existsSync(fromPath), "source file doesn't exists");
+ assert.ok(!fs.existsSync(toPath), "destination doesn't exists");
+ var async = false;
+ fs.rename(fromPath, toPath, function(error) {
+ assert.ok(async, "fs.rename is async");
+ assert.equal(error.message, "ENOENT, rename " + fromPath);
+ assert.equal(error.code, "ENOENT", "error has a code");
+ assert.equal(error.path, fromPath, "error has a path");
+ assert.equal(error.errno, 34, "error has a errno");
+ end();
+ });
+ async = true;
+};
+
+exports["test fs.rename (existing target file)"] = function(assert, end) {
+ let fromPath = renameFromPath;
+ let toPath = renameToPath;
+
+ fs.truncateSync(fromPath);
+ fs.truncateSync(toPath);
+ assert.ok(fs.existsSync(fromPath), "source file exists");
+ assert.ok(fs.existsSync(toPath), "destination file exists");
+ var async = false;
+ fs.rename(fromPath, toPath, function(error) {
+ assert.ok(async, "fs.rename is async");
+ assert.ok(!error, "error is falsy");
+ assert.ok(!fs.existsSync(fromPath), "source path no longer exists");
+ assert.ok(fs.existsSync(toPath), "destination file exists");
+ fs.unlinkSync(toPath);
+ assert.ok(!fs.existsSync(toPath), "cleaned up properly");
+ end();
+ });
+ async = true;
+};
+
+exports["test fs.writeFile"] = function(assert, end) {
+ let path = writePath;
+ let content = ["hello world",
+ "this is some text"].join("\n");
+
+ var async = false;
+ fs.writeFile(path, content, function(error) {
+ assert.ok(async, "fs write is async");
+ assert.ok(!error, "error is falsy");
+ assert.ok(fs.existsSync(path), "file was created");
+ assert.equal(fs.readFileSync(path).toString(),
+ content,
+ "contet was written");
+ fs.unlinkSync(path);
+ assert.ok(!fs.exists(path), "file was removed");
+
+ end();
+ });
+ async = true;
+};
+
+exports["test fs.writeFile (with large files)"] = function(assert, end) {
+ let path = writePath;
+ let content = "";
+
+ for (var i = 0; i < 100000; i++) {
+ content += "buffer\n";
+ }
+
+ var async = false;
+ fs.writeFile(path, content, function(error) {
+ assert.ok(async, "fs write is async");
+ assert.ok(!error, "error is falsy");
+ assert.ok(fs.existsSync(path), "file was created");
+ assert.equal(fs.readFileSync(path).toString(),
+ content,
+ "contet was written");
+ fs.unlinkSync(path);
+ assert.ok(!fs.exists(path), "file was removed");
+
+ end();
+ });
+ async = true;
+};
+
+exports["test fs.writeFile error"] = function (assert, done) {
+ try {
+ fs.writeFile({}, 'content', function (err) {
+ assert.fail('Error thrown from TypeError should not be caught');
+ });
+ } catch (e) {
+ assert.ok(e,
+ 'writeFile with a non-string error should not be caught');
+ assert.equal(e.name, 'TypeError', 'error should be TypeError');
+ }
+ fs.writeFile('not/a/valid/path', 'content', function (err) {
+ assert.ok(err, 'error caught and handled in callback');
+ done();
+ });
+};
+
+exports["test fs.chmod"] = function (assert, done) {
+ let content = ["hej från sverige"];
+
+ fs.writeFile(chmodPath, content, function (err) {
+ testPerm("0755")()
+ .then(testPerm("0777"))
+ .then(testPerm("0666"))
+ .then(testPerm(0o511))
+ .then(testPerm(0o200))
+ .then(testPerm("0040"))
+ .then(testPerm("0000"))
+ .then(testPermSync(0o777))
+ .then(testPermSync(0o666))
+ .then(testPermSync("0511"))
+ .then(testPermSync("0200"))
+ .then(testPermSync("0040"))
+ .then(testPermSync("0000"))
+ .then(() => {
+ assert.pass("Successful chmod passes");
+ }, assert.fail)
+ // Test invalid paths
+ .then(() => chmod("not-a-valid-file", 0o755))
+ .then(assert.fail, (err) => {
+ checkPermError(err, "not-a-valid-file");
+ })
+ .then(() => chmod("not-a-valid-file", 0o755, "sync"))
+ .then(assert.fail, (err) => {
+ checkPermError(err, "not-a-valid-file");
+ })
+ // Test invalid files
+ .then(() => chmod("resource://not-a-real-file", 0o755))
+ .then(assert.fail, (err) => {
+ checkPermError(err, "resource://not-a-real-file");
+ })
+ .then(() => chmod("resource://not-a-real-file", 0o755, 'sync'))
+ .then(assert.fail, (err) => {
+ checkPermError(err, "resource://not-a-real-file");
+ })
+ .then(done, assert.fail);
+ });
+
+ function checkPermError (err, path) {
+ assert.equal(err.message, "ENOENT, chmod " + path);
+ assert.equal(err.code, "ENOENT", "error has a code");
+ assert.equal(err.path, path, "error has a path");
+ assert.equal(err.errno, 34, "error has a errno");
+ }
+
+ function chmod (path, mode, sync) {
+ let { promise, resolve, reject } = defer();
+ if (!sync) {
+ fs.chmod(path, mode, (err) => {
+ if (err) reject(err);
+ else resolve();
+ });
+ } else {
+ fs.chmodSync(path, mode);
+ resolve();
+ }
+ return promise;
+ }
+
+ function testPerm (mode, sync) {
+ return function () {
+ return chmod(chmodPath, mode, sync)
+ .then(() => getPerm(chmodPath))
+ .then(perm => {
+ let nMode = normalizeMode(mode);
+ if (isWindows)
+ assert.equal(perm, nMode,
+ "mode correctly set to " + mode + " (" + nMode + " on windows)");
+ else
+ assert.equal(perm, nMode, "mode correctly set to " + nMode);
+ });
+ };
+ }
+
+ function testPermSync (mode) {
+ return testPerm(mode, true);
+ }
+
+ function getPerm (path) {
+ let { promise, resolve, reject } = defer();
+ fs.stat(path, function (err, stat) {
+ if (err) reject(err);
+ else resolve(stat.mode);
+ });
+ return promise;
+ }
+
+ /*
+ * Converts a unix mode `0755` into a Windows version of unix permissions
+ */
+ function normalizeMode (mode) {
+ if (typeof mode === "string")
+ mode = parseInt(mode, 8);
+
+ if (!isWindows)
+ return mode;
+
+ var ANY_READ = 0o444;
+ var ANY_WRITE = 0o222;
+ var winMode = 0;
+
+ // On Windows, if WRITE is true, then READ is also true
+ if (mode & ANY_WRITE)
+ winMode |= ANY_WRITE | ANY_READ;
+ // Minimum permissions are READ for Windows
+ else
+ winMode |= ANY_READ;
+
+ return winMode;
+ }
+};
+
+require("test").run(exports);
diff --git a/addon-sdk/source/test/test-functional.js b/addon-sdk/source/test/test-functional.js
new file mode 100644
index 000000000..02ae15fc6
--- /dev/null
+++ b/addon-sdk/source/test/test-functional.js
@@ -0,0 +1,463 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { setTimeout } = require('sdk/timers');
+const utils = require('sdk/lang/functional');
+const { invoke, defer, partial, compose, memoize, once, is, isnt,
+ delay, wrap, curry, chainable, field, query, isInstance, debounce, throttle
+} = utils;
+const { LoaderWithHookedConsole } = require('sdk/test/loader');
+
+exports['test forwardApply'] = function(assert) {
+ function sum(b, c) { return this.a + b + c; }
+ assert.equal(invoke(sum, [2, 3], { a: 1 }), 6,
+ 'passed arguments and pseoude-variable are used');
+
+ assert.equal(invoke(sum.bind({ a: 2 }), [2, 3], { a: 1 }), 7,
+ 'bounded `this` pseoudo variable is used');
+};
+
+exports['test deferred function'] = function(assert, done) {
+ let nextTurn = false;
+ function sum(b, c) {
+ assert.ok(nextTurn, 'enqueued is called in next turn of event loop');
+ assert.equal(this.a + b + c, 6,
+ 'passed arguments an pseoude-variable are used');
+ done();
+ }
+
+ let fixture = { a: 1, method: defer(sum) };
+ fixture.method(2, 3);
+ nextTurn = true;
+};
+
+exports['test partial function'] = function(assert) {
+ function sum(b, c) { return this.a + b + c; }
+
+ let foo = { a : 5 };
+
+ foo.sum7 = partial(sum, 7);
+ foo.sum8and4 = partial(sum, 8, 4);
+
+ assert.equal(foo.sum7(2), 14, 'partial one arguments works');
+
+ assert.equal(foo.sum8and4(), 17, 'partial both arguments works');
+};
+
+exports["test curry defined numeber of arguments"] = function(assert) {
+ var sum = curry(function(a, b, c) {
+ return a + b + c;
+ });
+
+ assert.equal(sum(2, 2, 1), 5, "sum(2, 2, 1) => 5");
+ assert.equal(sum(2, 4)(1), 7, "sum(2, 4)(1) => 7");
+ assert.equal(sum(2)(4, 2), 8, "sum(2)(4, 2) => 8");
+ assert.equal(sum(2)(4)(3), 9, "sum(2)(4)(3) => 9");
+};
+
+exports['test compose'] = function(assert) {
+ let greet = function(name) { return 'hi: ' + name; };
+ let exclaim = function(sentence) { return sentence + '!'; };
+
+ assert.equal(compose(exclaim, greet)('moe'), 'hi: moe!',
+ 'can compose a function that takes another');
+
+ assert.equal(compose(greet, exclaim)('moe'), 'hi: moe!',
+ 'in this case, the functions are also commutative');
+
+ let target = {
+ name: 'Joe',
+ greet: compose(function exclaim(sentence) {
+ return sentence + '!';
+ }, function(title) {
+ return 'hi : ' + title + ' ' + this.name;
+ })
+ };
+
+ assert.equal(target.greet('Mr'), 'hi : Mr Joe!',
+ 'this can be passed in');
+ assert.equal(target.greet.call({ name: 'Alex' }, 'Dr'), 'hi : Dr Alex!',
+ 'this can be applied');
+
+ let single = compose(function(value) {
+ return value + ':suffix';
+ });
+
+ assert.equal(single('text'), 'text:suffix', 'works with single function');
+
+ let identity = compose();
+ assert.equal(identity('bla'), 'bla', 'works with zero functions');
+};
+
+exports['test wrap'] = function(assert) {
+ let greet = function(name) { return 'hi: ' + name; };
+ let backwards = wrap(greet, function(f, name) {
+ return f(name) + ' ' + name.split('').reverse().join('');
+ });
+
+ assert.equal(backwards('moe'), 'hi: moe eom',
+ 'wrapped the saluation function');
+
+ let inner = function () { return 'Hello '; };
+ let target = {
+ name: 'Matteo',
+ hi: wrap(inner, function(f) { return f() + this.name; })
+ };
+
+ assert.equal(target.hi(), 'Hello Matteo', 'works with this');
+
+ function noop() { }
+ let wrapped = wrap(noop, function(f) {
+ return Array.slice(arguments);
+ });
+
+ let actual = wrapped([ 'whats', 'your' ], 'vector', 'victor');
+ assert.deepEqual(actual, [ noop, ['whats', 'your'], 'vector', 'victor' ],
+ 'works with fancy stuff');
+};
+
+exports['test memoize'] = function(assert) {
+ const fib = n => n < 2 ? n : fib(n - 1) + fib(n - 2);
+ let fibnitro = memoize(fib);
+
+ assert.equal(fib(10), 55,
+ 'a memoized version of fibonacci produces identical results');
+ assert.equal(fibnitro(10), 55,
+ 'a memoized version of fibonacci produces identical results');
+
+ function o(key, value) { return value; }
+ let oo = memoize(o), v1 = {}, v2 = {};
+
+
+ assert.equal(oo(1, v1), v1, 'returns value back');
+ assert.equal(oo(1, v2), v1, 'memoized by a first argument');
+ assert.equal(oo(2, v2), v2, 'returns back value if not memoized');
+ assert.equal(oo(2), v2, 'memoized new value');
+ assert.notEqual(oo(1), oo(2), 'values do not override');
+ assert.equal(o(3, v2), oo(2, 3), 'returns same value as un-memoized');
+
+ let get = memoize(function(attribute) { return this[attribute]; });
+ let target = { name: 'Bob', get: get };
+
+ assert.equal(target.get('name'), 'Bob', 'has correct `this`');
+ assert.equal(target.get.call({ name: 'Jack' }, 'name'), 'Bob',
+ 'name is memoized');
+ assert.equal(get('name'), 'Bob', 'once memoized can be called without this');
+};
+
+exports['test delay'] = function(assert, done) {
+ let delayed = false;
+ delay(function() {
+ assert.ok(delayed, 'delayed the function');
+ done();
+ }, 1);
+ delayed = true;
+};
+
+exports['test delay with this'] = function(assert, done) {
+ let context = {};
+ delay.call(context, function(name) {
+ assert.equal(this, context, 'this was passed in');
+ assert.equal(name, 'Tom', 'argument was passed in');
+ done();
+ }, 10, 'Tom');
+};
+
+exports['test once'] = function(assert) {
+ let n = 0;
+ let increment = once(function() { n ++; });
+
+ increment();
+ increment();
+
+ assert.equal(n, 1, 'only incremented once');
+
+ let target = {
+ state: 0,
+ update: once(function() {
+ return this.state ++;
+ })
+ };
+
+ target.update();
+ target.update();
+
+ assert.equal(target.state, 1, 'this was passed in and called only once');
+};
+
+exports['test once with argument'] = function(assert) {
+ let n = 0;
+ let increment = once(a => n++);
+
+ increment();
+ increment('foo');
+
+ assert.equal(n, 1, 'only incremented once');
+
+ increment();
+ increment('foo');
+
+ assert.equal(n, 1, 'only incremented once');
+};
+
+exports['test complement'] = assert => {
+ let { complement } = require("sdk/lang/functional");
+
+ let isOdd = x => Boolean(x % 2);
+
+ assert.equal(isOdd(1), true);
+ assert.equal(isOdd(2), false);
+
+ let isEven = complement(isOdd);
+
+ assert.equal(isEven(1), false);
+ assert.equal(isEven(2), true);
+
+ let foo = {};
+ let isFoo = function() { return this === foo; };
+ let insntFoo = complement(isFoo);
+
+ assert.equal(insntFoo.call(foo), false);
+ assert.equal(insntFoo.call({}), true);
+};
+
+exports['test constant'] = assert => {
+ let { constant } = require("sdk/lang/functional");
+
+ let one = constant(1);
+
+ assert.equal(one(1), 1);
+ assert.equal(one(2), 1);
+};
+
+exports['test apply'] = assert => {
+ let { apply } = require("sdk/lang/functional");
+
+ let dashify = (...args) => args.join("-");
+
+ assert.equal(apply(dashify, 1, [2, 3]), "1-2-3");
+ assert.equal(apply(dashify, "a"), "a");
+ assert.equal(apply(dashify, ["a", "b"]), "a-b");
+ assert.equal(apply(dashify, ["a", "b"], "c"), "a,b-c");
+ assert.equal(apply(dashify, [1, 2], [3, 4]), "1,2-3-4");
+};
+
+exports['test flip'] = assert => {
+ let { flip } = require("sdk/lang/functional");
+
+ let append = (left, right) => left + " " + right;
+ let prepend = flip(append);
+
+ assert.equal(append("hello", "world"), "hello world");
+ assert.equal(prepend("hello", "world"), "world hello");
+
+ let wrap = function(left, right) {
+ return left + " " + this + " " + right;
+ };
+ let invertWrap = flip(wrap);
+
+ assert.equal(wrap.call("@", "hello", "world"), "hello @ world");
+ assert.equal(invertWrap.call("@", "hello", "world"), "world @ hello");
+
+ let reverse = flip((...args) => args);
+
+ assert.deepEqual(reverse(1, 2, 3, 4), [4, 3, 2, 1]);
+ assert.deepEqual(reverse(1), [1]);
+ assert.deepEqual(reverse(), []);
+
+ // currying still works
+ let prependr = curry(prepend);
+
+ assert.equal(prependr("hello", "world"), "world hello");
+ assert.equal(prependr("hello")("world"), "world hello");
+};
+
+exports["test when"] = assert => {
+ let { when } = require("sdk/lang/functional");
+
+ let areNums = (...xs) => xs.every(x => typeof(x) === "number");
+
+ let sum = when(areNums, (...xs) => xs.reduce((y, x) => x + y, 0));
+
+ assert.equal(sum(1, 2, 3), 6);
+ assert.equal(sum(1, 2, "3"), undefined);
+
+ let multiply = when(areNums,
+ (...xs) => xs.reduce((y, x) => x * y, 1),
+ (...xs) => xs);
+
+ assert.equal(multiply(2), 2);
+ assert.equal(multiply(2, 3), 6);
+ assert.deepEqual(multiply(2, "4"), [2, "4"]);
+
+ function Point(x, y) {
+ this.x = x;
+ this.y = y;
+ }
+
+ let isPoint = x => x instanceof Point;
+
+ let inc = when(isPoint, ({x, y}) => new Point(x + 1, y + 1));
+
+ assert.equal(inc({}), undefined);
+ assert.deepEqual(inc(new Point(0, 0)), { x: 1, y: 1 });
+
+ let axis = when(isPoint,
+ ({ x, y }) => [x, y],
+ _ => [0, 0]);
+
+ assert.deepEqual(axis(new Point(1, 4)), [1, 4]);
+ assert.deepEqual(axis({ foo: "bar" }), [0, 0]);
+};
+
+exports["test chainable"] = function(assert) {
+ let Player = function () { this.volume = 5; };
+ Player.prototype = {
+ setBand: chainable(function (band) { return (this.band = band); }),
+ incVolume: chainable(function () { return this.volume++; })
+ };
+ let player = new Player();
+ player
+ .setBand('Animals As Leaders')
+ .incVolume().incVolume().incVolume().incVolume().incVolume().incVolume();
+
+ assert.equal(player.band, 'Animals As Leaders', 'passes arguments into chained');
+ assert.equal(player.volume, 11, 'accepts no arguments in chain');
+};
+
+exports["test field"] = assert => {
+ let Num = field("constructor", 0);
+ assert.equal(Num.name, Number.name);
+ assert.ok(typeof(Num), "function");
+
+ let x = field("x");
+
+ [
+ [field("foo", { foo: 1 }), 1],
+ [field("foo")({ foo: 1 }), 1],
+ [field("bar", {}), undefined],
+ [field("bar")({}), undefined],
+ [field("hey", undefined), undefined],
+ [field("hey")(undefined), undefined],
+ [field("how", null), null],
+ [field("how")(null), null],
+ [x(1), undefined],
+ [x(undefined), undefined],
+ [x(null), null],
+ [x({ x: 1 }), 1],
+ [x({ x: 2 }), 2],
+ ].forEach(([actual, expected]) => assert.equal(actual, expected));
+};
+
+exports["test query"] = assert => {
+ let Num = query("constructor", 0);
+ assert.equal(Num.name, Number.name);
+ assert.ok(typeof(Num), "function");
+
+ let x = query("x");
+ let xy = query("x.y");
+
+ [
+ [query("foo", { foo: 1 }), 1],
+ [query("foo")({ foo: 1 }), 1],
+ [query("foo.bar", { foo: { bar: 2 } }), 2],
+ [query("foo.bar")({ foo: { bar: 2 } }), 2],
+ [query("foo.bar", { foo: 1 }), undefined],
+ [query("foo.bar")({ foo: 1 }), undefined],
+ [x(1), undefined],
+ [x(undefined), undefined],
+ [x(null), null],
+ [x({ x: 1 }), 1],
+ [x({ x: 2 }), 2],
+ [xy(1), undefined],
+ [xy(undefined), undefined],
+ [xy(null), null],
+ [xy({ x: 1 }), undefined],
+ [xy({ x: 2 }), undefined],
+ [xy({ x: { y: 1 } }), 1],
+ [xy({ x: { y: 2 } }), 2]
+ ].forEach(([actual, expected]) => assert.equal(actual, expected));
+};
+
+exports["test isInstance"] = assert => {
+ function X() {}
+ function Y() {}
+ let isX = isInstance(X);
+
+ [
+ isInstance(X, new X()),
+ isInstance(X)(new X()),
+ !isInstance(X, new Y()),
+ !isInstance(X)(new Y()),
+ isX(new X()),
+ !isX(new Y())
+ ].forEach(x => assert.ok(x));
+};
+
+exports["test is"] = assert => {
+
+ assert.deepEqual([ 1, 0, 1, 0, 1 ].map(is(1)),
+ [ true, false, true, false, true ],
+ "is can be partially applied");
+
+ assert.ok(is(1, 1));
+ assert.ok(!is({}, {}));
+ assert.ok(is()(1)()(1), "is is curried");
+ assert.ok(!is()(1)()(2));
+};
+
+exports["test isnt"] = assert => {
+
+ assert.deepEqual([ 1, 0, 1, 0, 1 ].map(isnt(0)),
+ [ true, false, true, false, true ],
+ "is can be partially applied");
+
+ assert.ok(!isnt(1, 1));
+ assert.ok(isnt({}, {}));
+ assert.ok(!isnt()(1)()(1));
+ assert.ok(isnt()(1)()(2));
+};
+
+exports["test debounce"] = (assert, done) => {
+ let counter = 0;
+ let fn = debounce(() => counter++, 100);
+
+ new Array(10).join(0).split("").forEach(fn);
+
+ assert.equal(counter, 0, "debounce does not fire immediately");
+ setTimeout(() => {
+ assert.equal(counter, 1, "function called after wait time");
+ fn();
+ setTimeout(() => {
+ assert.equal(counter, 2, "function able to be called again after wait");
+ done();
+ }, 150);
+ }, 200);
+};
+
+exports["test throttle"] = (assert, done) => {
+ let called = 0;
+ let attempt = 0;
+ let atleast100ms = false;
+ let throttledFn = throttle(() => {
+ called++;
+ if (called === 2) {
+ assert.equal(attempt, 10, "called twice, but attempted 10 times");
+ fn();
+ }
+ if (called === 3) {
+ assert.ok(atleast100ms, "atleast 100ms have passed");
+ assert.equal(attempt, 11, "called third, waits for delay to happen");
+ done();
+ }
+ }, 200);
+ let fn = () => ++attempt && throttledFn();
+
+ setTimeout(() => atleast100ms = true, 100);
+
+ new Array(11).join(0).split("").forEach(fn);
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-globals.js b/addon-sdk/source/test/test-globals.js
new file mode 100644
index 000000000..bc3364367
--- /dev/null
+++ b/addon-sdk/source/test/test-globals.js
@@ -0,0 +1,30 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+Object.defineProperty(this, 'global', { value: this });
+
+exports.testGlobals = function(assert) {
+ // the only globals in module scope should be:
+ // module, exports, require, dump, console
+ assert.equal(typeof module, 'object', 'have "module", good');
+ assert.equal(typeof exports, 'object', 'have "exports", good');
+ assert.equal(typeof require, 'function', 'have "require", good');
+ assert.equal(typeof dump, 'function', 'have "dump", good');
+ assert.equal(typeof console, 'object', 'have "console", good');
+
+ // in particular, these old globals should no longer be present
+ assert.ok(!('packaging' in global), "no 'packaging', good");
+ assert.ok(!('memory' in global), "no 'memory', good");
+ assert.ok(/test-globals\.js$/.test(module.uri),
+ 'should contain filename');
+};
+
+exports.testComponent = function (assert) {
+ assert.throws(() => {
+ Components;
+ }, /`Components` is not available/, 'using `Components` throws');
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-heritage.js b/addon-sdk/source/test/test-heritage.js
new file mode 100644
index 000000000..e087f3e4d
--- /dev/null
+++ b/addon-sdk/source/test/test-heritage.js
@@ -0,0 +1,301 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Class, extend, mix, obscure } = require('sdk/core/heritage');
+
+exports['test extend'] = function(assert) {
+ let ancestor = { a: 1 };
+ let descendant = extend(ancestor, {
+ b: 2,
+ get c() { return 3 },
+ d: function() { return 4 }
+ });
+
+ assert.ok(ancestor.isPrototypeOf(descendant),
+ 'descendant inherits from ancestor');
+ assert.ok(descendant.b, 2, 'proprety was implemented');
+ assert.ok(descendant.c, 3, 'getter was implemented');
+ assert.ok(descendant.d(), 4, 'method was implemented');
+
+ /* Will be fixed once Bug 674195 is shipped.
+ assert.ok(Object.isFrozen(descendant),
+ 'extend returns frozen objects');
+ */
+};
+
+exports['test mix'] = function(assert) {
+ let ancestor = { a: 1 }
+ let mixed = mix(extend(ancestor, { b: 1, c: 1 }), { c: 2 }, { d: 3 });
+
+ assert.deepEqual(JSON.parse(JSON.stringify(mixed)), { b: 1, c: 2, d: 3 },
+ 'properties mixed as expected');
+ assert.ok(ancestor.isPrototypeOf(mixed),
+ 'first arguments ancestor is ancestor of result');
+};
+
+exports['test obscure'] = function(assert) {
+ let fixture = mix({ a: 1 }, obscure({ b: 2 }));
+
+ assert.equal(fixture.a, 1, 'a property is included');
+ assert.equal(fixture.b, 2, 'b proprety is included');
+ assert.ok(!Object.getOwnPropertyDescriptor(fixture, 'b').enumerable,
+ 'obscured properties are non-enumerable');
+};
+
+exports['test inheritance'] = function(assert) {
+ let Ancestor = Class({
+ name: 'ancestor',
+ method: function () {
+ return 'hello ' + this.name;
+ }
+ });
+
+ assert.ok(Ancestor() instanceof Ancestor,
+ 'can be instantiated without new');
+ assert.ok(new Ancestor() instanceof Ancestor,
+ 'can also be instantiated with new');
+ assert.ok(Ancestor() instanceof Class,
+ 'if ancestor not specified than defaults to Class');
+ assert.ok(Ancestor.prototype.extends, Class.prototype,
+ 'extends of prototype points to ancestors prototype');
+
+
+ assert.equal(Ancestor().method(), 'hello ancestor',
+ 'instance inherits defined properties');
+
+ let Descendant = Class({
+ extends: Ancestor,
+ name: 'descendant'
+ });
+
+ assert.ok(Descendant() instanceof Descendant,
+ 'instantiates correctly');
+ assert.ok(Descendant() instanceof Ancestor,
+ 'Inherits for passed `extends`');
+ assert.equal(Descendant().method(), 'hello descendant',
+ 'propreties inherited');
+};
+
+exports['test immunity against __proto__'] = function(assert) {
+ let Foo = Class({ name: 'foo', hacked: false });
+
+ let Bar = Class({ extends: Foo, name: 'bar' });
+
+ assert.throws(function() {
+ Foo.prototype.__proto__ = { hacked: true };
+ if (Foo() instanceof Base && !Foo().hacked)
+ throw Error('can not change prototype chain');
+ }, 'prototype chain is immune to __proto__ hacks');
+
+ assert.throws(function() {
+ Foo.prototype.__proto__ = { hacked: true };
+ if (Bar() instanceof Foo && !Bar().hacked)
+ throw Error('can not change prototype chain');
+ }, 'prototype chain of decedants immune to __proto__ hacks');
+};
+
+exports['test super'] = function(assert) {
+ var Foo = Class({
+ initialize: function initialize(options) {
+ this.name = options.name;
+ }
+ });
+
+ var Bar = Class({
+ extends: Foo,
+ initialize: function Bar(options) {
+ Foo.prototype.initialize.call(this, options);
+ this.type = 'bar';
+ }
+ });
+
+ var bar = Bar({ name: 'test' });
+
+ assert.equal(bar.type, 'bar', 'bar initializer was called');
+ assert.equal(bar.name, 'test', 'bar initializer called Foo initializer');
+};
+
+exports['test initialize'] = function(assert) {
+ var Dog = Class({
+ initialize: function initialize(name) {
+ this.name = name;
+ },
+ type: 'dog',
+ bark: function bark() {
+ return 'Ruff! Ruff!'
+ }
+ });
+
+ var fluffy = Dog('Fluffy'); // instatiation
+ assert.ok(fluffy instanceof Dog,
+ 'instanceof works as expected');
+ assert.ok(fluffy instanceof Class,
+ 'inherits form Class if not specified otherwise');
+ assert.ok(fluffy.name, 'fluffy',
+ 'initialize unless specified otherwise');
+};
+
+exports['test complements regular inheritace'] = function(assert) {
+ let Base = Class({ name: 'base' });
+
+ function Type() {
+ // ...
+ }
+ Type.prototype = Object.create(Base.prototype);
+ Type.prototype.run = function() {
+ // ...
+ };
+
+ let value = new Type();
+
+ assert.ok(value instanceof Type, 'creates instance of Type');
+ assert.ok(value instanceof Base, 'inherits from Base');
+ assert.equal(value.name, 'base', 'inherits properties from Base');
+
+
+ let SubType = Class({
+ extends: Type,
+ sub: 'type'
+ });
+
+ let fixture = SubType();
+
+ assert.ok(fixture instanceof Base, 'is instance of Base');
+ assert.ok(fixture instanceof Type, 'is instance of Type');
+ assert.ok(fixture instanceof SubType, 'is instance of SubType');
+
+ assert.equal(fixture.sub, 'type', 'proprety is defined');
+ assert.equal(fixture.run, Type.prototype.run, 'proprety is inherited');
+ assert.equal(fixture.name, 'base', 'inherits base properties');
+};
+
+exports['test extends object'] = function(assert) {
+ let prototype = { constructor: function() { return this; }, name: 'me' };
+ let Foo = Class({
+ extends: prototype,
+ value: 2
+ });
+ let foo = new Foo();
+
+ assert.ok(foo instanceof Foo, 'instance of Foo');
+ assert.ok(!(foo instanceof Class), 'is not instance of Class');
+ assert.ok(prototype.isPrototypeOf(foo), 'inherits from given prototype');
+ assert.equal(Object.getPrototypeOf(Foo.prototype), prototype,
+ 'contsructor prototype inherits from extends option');
+ assert.equal(foo.value, 2, 'property is defined');
+ assert.equal(foo.name, 'me', 'prototype proprety is inherited');
+};
+
+
+var HEX = Class({
+ hex: function hex() {
+ return '#' + this.color;
+ }
+});
+
+var RGB = Class({
+ red: function red() {
+ return parseInt(this.color.substr(0, 2), 16);
+ },
+ green: function green() {
+ return parseInt(this.color.substr(2, 2), 16);
+ },
+ blue: function blue() {
+ return parseInt(this.color.substr(4, 2), 16);
+ }
+});
+
+var CMYK = Class({
+ black: function black() {
+ var color = Math.max(Math.max(this.red(), this.green()), this.blue());
+ return (1 - color / 255).toFixed(4);
+ },
+ magenta: function magenta() {
+ var K = this.black();
+ return (((1 - this.green() / 255).toFixed(4) - K) / (1 - K)).toFixed(4);
+ },
+ yellow: function yellow() {
+ var K = this.black();
+ return (((1 - this.blue() / 255).toFixed(4) - K) / (1 - K)).toFixed(4);
+ },
+ cyan: function cyan() {
+ var K = this.black();
+ return (((1 - this.red() / 255).toFixed(4) - K) / (1 - K)).toFixed(4);
+ }
+});
+
+var Color = Class({
+ implements: [ HEX, RGB, CMYK ],
+ initialize: function initialize(color) {
+ this.color = color;
+ }
+});
+
+exports['test composition'] = function(assert) {
+ var pink = Color('FFC0CB');
+
+ assert.equal(pink.red(), 255, 'red() works');
+ assert.equal(pink.green(), 192, 'green() works');
+ assert.equal(pink.blue(), 203, 'blue() works');
+
+ assert.equal(pink.magenta(), 0.2471, 'magenta() works');
+ assert.equal(pink.yellow(), 0.2039, 'yellow() works');
+ assert.equal(pink.cyan(), 0.0000, 'cyan() works');
+
+ assert.ok(pink instanceof Color, 'is instance of Color');
+ assert.ok(pink instanceof Class, 'is instance of Class');
+};
+
+var Point = Class({
+ initialize: function initialize(x, y) {
+ this.x = x;
+ this.y = y;
+ },
+ toString: function toString() {
+ return this.x + ':' + this.y;
+ }
+})
+
+var Pixel = Class({
+ extends: Point,
+ implements: [ Color ],
+ initialize: function initialize(x, y, color) {
+ Color.prototype.initialize.call(this, color);
+ Point.prototype.initialize.call(this, x, y);
+ },
+ toString: function toString() {
+ return this.hex() + '@' + Point.prototype.toString.call(this)
+ }
+});
+
+exports['test compostion with inheritance'] = function(assert) {
+ var pixel = Pixel(11, 23, 'CC3399');
+
+ assert.equal(pixel.toString(), '#CC3399@11:23', 'stringifies correctly');
+ assert.ok(pixel instanceof Pixel, 'instance of Pixel');
+ assert.ok(pixel instanceof Point, 'instance of Point');
+};
+
+exports['test composition with objects'] = function(assert) {
+ var A = { a: 1, b: 1 };
+ var B = Class({ b: 2, c: 2 });
+ var C = { c: 3 };
+ var D = { d: 4 };
+
+ var ABCD = Class({
+ implements: [ A, B, C, D ],
+ e: 5
+ });
+
+ var f = ABCD();
+
+ assert.equal(f.a, 1, 'inherits A.a');
+ assert.equal(f.b, 2, 'inherits B.b overrides A.b');
+ assert.equal(f.c, 3, 'inherits C.c overrides B.c');
+ assert.equal(f.d, 4, 'inherits D.d');
+ assert.equal(f.e, 5, 'implements e');
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-hidden-frame.js b/addon-sdk/source/test/test-hidden-frame.js
new file mode 100644
index 000000000..945c2413f
--- /dev/null
+++ b/addon-sdk/source/test/test-hidden-frame.js
@@ -0,0 +1,71 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Loader } = require("sdk/test/loader");
+const hiddenFrames = require("sdk/frame/hidden-frame");
+const { HiddenFrame } = hiddenFrames;
+
+exports["test Frame"] = function(assert, done) {
+ let url = "data:text/html;charset=utf-8,<!DOCTYPE%20html>";
+
+ let hiddenFrame = hiddenFrames.add(HiddenFrame({
+ onReady: function () {
+ assert.equal(this.element.contentWindow.location, "about:blank",
+ "HiddenFrame loads about:blank by default.");
+
+ function onDOMReady() {
+ hiddenFrame.element.removeEventListener("DOMContentLoaded", onDOMReady,
+ false);
+ assert.equal(hiddenFrame.element.contentWindow.location, url,
+ "HiddenFrame loads the specified content.");
+ done();
+ }
+
+ this.element.addEventListener("DOMContentLoaded", onDOMReady, false);
+ this.element.setAttribute("src", url);
+ }
+ }));
+};
+
+exports["test frame removed properly"] = function(assert, done) {
+ let url = "data:text/html;charset=utf-8,<!DOCTYPE%20html>";
+
+ let hiddenFrame = hiddenFrames.add(HiddenFrame({
+ onReady: function () {
+ let frame = this.element;
+ assert.ok(frame.parentNode, "frame has a parent node");
+ hiddenFrames.remove(hiddenFrame);
+ assert.ok(!frame.parentNode, "frame no longer has a parent node");
+ done();
+ }
+ }));
+};
+
+exports["test unload detaches panels"] = function(assert, done) {
+ let loader = Loader(module);
+ let { add, remove, HiddenFrame } = loader.require("sdk/frame/hidden-frame");
+ let frames = []
+
+ function ready() {
+ frames.push(this.element);
+ if (frames.length === 2) complete();
+ }
+
+ add(HiddenFrame({ onReady: ready }));
+ add(HiddenFrame({ onReady: ready }));
+
+ function complete() {
+ frames.forEach(function(frame) {
+ assert.ok(frame.parentNode, "frame is in the document");
+ })
+ loader.unload();
+ frames.forEach(function(frame) {
+ assert.ok(!frame.parentNode, "frame isn't in the document'");
+ });
+ done();
+ }
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-host-events.js b/addon-sdk/source/test/test-host-events.js
new file mode 100644
index 000000000..1c6664534
--- /dev/null
+++ b/addon-sdk/source/test/test-host-events.js
@@ -0,0 +1,99 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Cc, Ci } = require('chrome');
+const { defer, all } = require('sdk/core/promise');
+const { setTimeout } = require('sdk/timers');
+const { request, response } = require('sdk/addon/host');
+const { send } = require('sdk/addon/events');
+const { filter } = require('sdk/event/utils');
+const { on, emit, off } = require('sdk/event/core');
+
+var stream = filter(request, (data) => /sdk-x-test/.test(data.event));
+
+exports.testSend = function (assert, done) {
+ on(stream, 'data', handle);
+ send('sdk-x-test-simple', { title: 'my test data' }).then((data) => {
+ assert.equal(data.title, 'my response', 'response is handled');
+ off(stream, 'data', handle);
+ done();
+ }, (reason) => {
+ assert.fail('should not call reject');
+ });
+ function handle (e) {
+ assert.equal(e.event, 'sdk-x-test-simple', 'correct event name');
+ assert.ok(e.id != null, 'message has an ID');
+ assert.equal(e.data.title, 'my test data', 'serialized data passes');
+ e.data.title = 'my response';
+ emit(response, 'data', e);
+ }
+};
+
+exports.testSendError = function (assert, done) {
+ on(stream, 'data', handle);
+ send('sdk-x-test-error', { title: 'my test data' }).then((data) => {
+ assert.fail('should not call success');
+ }, (reason) => {
+ assert.equal(reason, 'ErrorInfo', 'should reject with error/reason');
+ off(stream, 'data', handle);
+ done();
+ });
+ function handle (e) {
+ e.error = 'ErrorInfo';
+ emit(response, 'data', e);
+ }
+};
+
+exports.testMultipleSends = function (assert, done) {
+ let count = 0;
+ let ids = [];
+ on(stream, 'data', handle);
+ ['firefox', 'thunderbird', 'rust'].map(data =>
+ send('sdk-x-test-multi', { data: data }).then(val => {
+ assert.ok(val === 'firefox' || val === 'rust', 'successful calls resolve correctly');
+ if (++count === 3) {
+ off(stream, 'data', handle);
+ done();
+ }
+ }, reason => {
+ assert.equal(reason.error, 'too blue', 'rejected calls are rejected');
+ if (++count === 3) {
+ off(stream, 'data', handle);
+ done();
+ }
+ }));
+
+ function handle (e) {
+ if (e.data !== 'firefox' || e.data !== 'rust')
+ e.error = { data: e.data, error: 'too blue' };
+ assert.ok(!~ids.indexOf(e.id), 'ID should be unique');
+ assert.equal(e.event, 'sdk-x-test-multi', 'has event name');
+ ids.push(e.id);
+ emit(response, 'data', e);
+ }
+};
+
+exports.testSerialization = function (assert, done) {
+ on(stream, 'data', handle);
+ let object = { title: 'my test data' };
+ let resObject;
+ send('sdk-x-test-serialize', object).then(data => {
+ data.title = 'another title';
+ assert.equal(object.title, 'my test data', 'original object not modified');
+ assert.equal(resObject.title, 'new title', 'object passed by value from host');
+ off(stream, 'data', handle);
+ done();
+ }, (reason) => {
+ assert.fail('should not call reject');
+ });
+ function handle (e) {
+ e.data.title = 'new title';
+ assert.equal(object.title, 'my test data', 'object passed by value to host');
+ resObject = e.data;
+ emit(response, 'data', e);
+ }
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-hotkeys.js b/addon-sdk/source/test/test-hotkeys.js
new file mode 100644
index 000000000..ba460ee45
--- /dev/null
+++ b/addon-sdk/source/test/test-hotkeys.js
@@ -0,0 +1,183 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Hotkey } = require("sdk/hotkeys");
+const { keyDown } = require("sdk/dom/events/keys");
+const { Loader } = require('sdk/test/loader');
+const { getMostRecentBrowserWindow } = require("sdk/window/utils");
+
+const element = getMostRecentBrowserWindow().document.documentElement;
+
+exports["test hotkey: function key"] = function(assert, done) {
+ var showHotKey = Hotkey({
+ combo: "f1",
+ onPress: function() {
+ assert.pass("first callback is called");
+ assert.equal(this, showHotKey,
+ 'Context `this` in `onPress` should be the hotkey object');
+ keyDown(element, "f2");
+ showHotKey.destroy();
+ }
+ });
+
+ var hideHotKey = Hotkey({
+ combo: "f2",
+ onPress: function() {
+ assert.pass("second callback is called");
+ hideHotKey.destroy();
+ done();
+ }
+ });
+
+ keyDown(element, "f1");
+};
+
+exports["test hotkey: accel alt shift"] = function(assert, done) {
+ var showHotKey = Hotkey({
+ combo: "accel-shift-6",
+ onPress: function() {
+ assert.pass("first callback is called");
+ keyDown(element, "accel-alt-shift-6");
+ showHotKey.destroy();
+ }
+ });
+
+ var hideHotKey = Hotkey({
+ combo: "accel-alt-shift-6",
+ onPress: function() {
+ assert.pass("second callback is called");
+ hideHotKey.destroy();
+ done();
+ }
+ });
+
+ keyDown(element, "accel-shift-6");
+};
+
+exports["test hotkey meta & control"] = function(assert, done) {
+ var showHotKey = Hotkey({
+ combo: "meta-3",
+ onPress: function() {
+ assert.pass("first callback is called");
+ keyDown(element, "alt-control-shift-b");
+ showHotKey.destroy();
+ }
+ });
+
+ var hideHotKey = Hotkey({
+ combo: "Ctrl-Alt-Shift-B",
+ onPress: function() {
+ assert.pass("second callback is called");
+ hideHotKey.destroy();
+ done();
+ }
+ });
+
+ keyDown(element, "meta-3");
+};
+
+exports["test hotkey: control-1 / meta--"] = function(assert, done) {
+ var showHotKey = Hotkey({
+ combo: "control-1",
+ onPress: function() {
+ assert.pass("first callback is called");
+ keyDown(element, "meta--");
+ showHotKey.destroy();
+ }
+ });
+
+ var hideHotKey = Hotkey({
+ combo: "meta--",
+ onPress: function() {
+ assert.pass("second callback is called");
+ hideHotKey.destroy();
+ done();
+ }
+ });
+
+ keyDown(element, "control-1");
+};
+
+exports["test invalid combos"] = function(assert) {
+ assert.throws(function() {
+ Hotkey({
+ combo: "d",
+ onPress: function() {}
+ });
+ }, "throws if no modifier is present");
+ assert.throws(function() {
+ Hotkey({
+ combo: "alt",
+ onPress: function() {}
+ });
+ }, "throws if no key is present");
+ assert.throws(function() {
+ Hotkey({
+ combo: "alt p b",
+ onPress: function() {}
+ });
+ }, "throws if more then one key is present");
+};
+
+exports["test no exception on unmodified keypress"] = function(assert) {
+ var someHotkey = Hotkey({
+ combo: "control-alt-1",
+ onPress: () => {}
+ });
+ keyDown(element, "a");
+ assert.pass("No exception throw, unmodified keypress passed");
+ someHotkey.destroy();
+};
+
+exports["test hotkey: automatic destroy"] = function*(assert) {
+ // Hacky way to be able to create unloadable modules via makeSandboxedLoader.
+ let loader = Loader(module);
+
+ var called = false;
+ var hotkey = loader.require("sdk/hotkeys").Hotkey({
+ combo: "accel-shift-x",
+ onPress: () => called = true
+ });
+
+ // Unload the module so that previous hotkey is automatically destroyed
+ loader.unload();
+
+ // Ensure that the hotkey is really destroyed
+ keyDown(element, "accel-shift-x");
+
+ assert.ok(!called, "Hotkey is destroyed and not called.");
+
+ // create a new hotkey for a different set
+ yield new Promise(resolve => {
+ let key = Hotkey({
+ combo: "accel-shift-y",
+ onPress: () => {
+ key.destroy();
+ assert.pass("accel-shift-y was pressed.");
+ resolve();
+ }
+ });
+ keyDown(element, "accel-shift-y");
+ });
+
+ assert.ok(!called, "Hotkey is still not called, in time it would take.");
+
+ // create a new hotkey for the same set
+ yield new Promise(resolve => {
+ let key = Hotkey({
+ combo: "accel-shift-x",
+ onPress: () => {
+ key.destroy();
+ assert.pass("accel-shift-x was pressed.");
+ resolve();
+ }
+ });
+ keyDown(element, "accel-shift-x");
+ });
+
+ assert.ok(!called, "Hotkey is still not called, and reusing is ok.");
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-httpd.js b/addon-sdk/source/test/test-httpd.js
new file mode 100644
index 000000000..78740f1bf
--- /dev/null
+++ b/addon-sdk/source/test/test-httpd.js
@@ -0,0 +1,73 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const port = 8099;
+const file = require("sdk/io/file");
+const { pathFor } = require("sdk/system");
+const { Loader } = require("sdk/test/loader");
+const options = require("sdk/test/options");
+
+const loader = Loader(module);
+const httpd = loader.require("./lib/httpd");
+if (options.parseable || options.verbose)
+ loader.sandbox("./lib/httpd").DEBUG = true;
+
+exports.testBasicHTTPServer = function(assert, done) {
+ // Use the profile directory for the temporary file as that will be deleted
+ // when tests are complete
+ let basePath = pathFor("ProfD");
+ let filePath = file.join(basePath, 'test-httpd.txt');
+ let content = "This is the HTTPD test file.\n";
+ let fileStream = file.open(filePath, 'w');
+ fileStream.write(content);
+ fileStream.close();
+
+ let srv = httpd.startServerAsync(port, basePath);
+
+ // Request this very file.
+ let Request = require('sdk/request').Request;
+ Request({
+ url: "http://localhost:" + port + "/test-httpd.txt",
+ onComplete: function (response) {
+ assert.equal(response.text, content);
+ srv.stop(done);
+ }
+ }).get();
+};
+
+exports.testDynamicServer = function (assert, done) {
+ let content = "This is the HTTPD test file.\n";
+
+ let srv = httpd.startServerAsync(port);
+
+ // See documentation here:
+ //http://doxygen.db48x.net/mozilla/html/interfacensIHttpServer.html#a81fc7e7e29d82aac5ce7d56d0bedfb3a
+ //http://doxygen.db48x.net/mozilla/html/interfacensIHttpRequestHandler.html
+ srv.registerPathHandler("/test-httpd.txt", function handle(request, response) {
+ // Add text content type, only to avoid error in `Request` API
+ response.setHeader("Content-Type", "text/plain", false);
+ response.write(content);
+ });
+
+ // Request this very file.
+ let Request = require('sdk/request').Request;
+ Request({
+ url: "http://localhost:" + port + "/test-httpd.txt",
+ onComplete: function (response) {
+ assert.equal(response.text, content);
+ srv.stop(done);
+ }
+ }).get();
+};
+
+exports.testAutomaticPortSelection = function (assert, done) {
+ const srv = httpd.startServerAsync(-1);
+
+ const port = srv.identity.primaryPort;
+ assert.ok(0 <= port && port <= 65535);
+
+ srv.stop(done);
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-indexed-db.js b/addon-sdk/source/test/test-indexed-db.js
new file mode 100644
index 000000000..ea53a3e72
--- /dev/null
+++ b/addon-sdk/source/test/test-indexed-db.js
@@ -0,0 +1,182 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { indexedDB, IDBKeyRange, DOMException
+ } = require("sdk/indexed-db");
+
+exports["test indexedDB is frozen"] = function(assert){
+ let original = indexedDB.open;
+ let f = function(){};
+ assert.throws(function(){indexedDB.open = f});
+ assert.equal(indexedDB.open,original);
+ assert.notEqual(indexedDB.open,f);
+
+};
+
+exports["test db variables"] = function(assert) {
+ [ indexedDB, IDBKeyRange, DOMException
+ ].forEach(function(value) {
+ assert.notEqual(typeof(value), "undefined", "variable is defined");
+ });
+}
+
+exports["test open"] = function(assert, done) {
+ testOpen(0, assert, done);
+}
+
+function testOpen(step, assert, done) {
+ const dbName = "MyTestDatabase";
+ const openParams = [
+ { dbName: "MyTestDatabase", dbVersion: 10 },
+ { dbName: "MyTestDatabase" },
+ { dbName: "MyTestDatabase", dbOptions: { storage: "temporary" } },
+ { dbName: "MyTestDatabase", dbOptions: { version: 20, storage: "default" } }
+ ];
+
+ let params = openParams[step];
+
+ let request;
+ let expectedStorage;
+ let expectedVersion;
+ let upgradeNeededCalled = false;
+ if ("dbVersion" in params) {
+ request = indexedDB.open(params.dbName, params.dbVersion);
+ expectedVersion = params.dbVersion;
+ expectedStorage = "persistent";
+ } else if ("dbOptions" in params) {
+ request = indexedDB.open(params.dbName, params.dbOptions);
+ if ("version" in params.dbOptions) {
+ expectedVersion = params.dbOptions.version;
+ } else {
+ expectedVersion = 1;
+ }
+ if ("storage" in params.dbOptions) {
+ expectedStorage = params.dbOptions.storage;
+ } else {
+ expectedStorage = "persistent";
+ }
+ } else {
+ request = indexedDB.open(params.dbName);
+ expectedVersion = 1;
+ expectedStorage = "persistent";
+ }
+ request.onerror = function(event) {
+ assert.fail("Failed to open indexedDB")
+ done();
+ }
+ request.onupgradeneeded = function(event) {
+ upgradeNeededCalled = true;
+ assert.equal(event.oldVersion, 0, "Correct old version");
+ }
+ request.onsuccess = function(event) {
+ assert.pass("IndexedDB was open");
+ assert.equal(upgradeNeededCalled, true, "Upgrade needed called");
+ let db = request.result;
+ assert.equal(db.storage, expectedStorage, "Storage is correct");
+ db.onversionchange = function(event) {
+ assert.equal(event.oldVersion, expectedVersion, "Old version is correct");
+ db.close();
+ }
+ if ("dbOptions" in params) {
+ request = indexedDB.deleteDatabase(params.dbName, params.dbOptions);
+ } else {
+ request = indexedDB.deleteDatabase(params.dbName);
+ }
+ request.onerror = function(event) {
+ assert.fail("Failed to delete indexedDB")
+ done();
+ }
+ request.onsuccess = function(event) {
+ assert.pass("IndexedDB was deleted");
+
+ if (++step == openParams.length) {
+ done();
+ } else {
+ testOpen(step, assert, done);
+ }
+ }
+ }
+}
+
+exports["test dbname is unprefixed"] = function(assert, done) {
+ // verify fixes in https://bugzilla.mozilla.org/show_bug.cgi?id=786688
+ let dbName = "dbname-unprefixed";
+ let request = indexedDB.open(dbName);
+ request.onerror = function(event) {
+ assert.fail("Failed to open db");
+ done();
+ };
+ request.onsuccess = function(event) {
+ assert.equal(request.result.name, dbName);
+ done();
+ };
+};
+
+exports["test structuring the database"] = function(assert, done) {
+ // This is what our customer data looks like.
+ let customerData = [
+ { ssn: "444-44-4444", name: "Bill", age: 35, email: "bill@company.com" },
+ { ssn: "555-55-5555", name: "Donna", age: 32, email: "donna@home.org" }
+ ];
+ let dbName = "the_name";
+ let request = indexedDB.open(dbName, 2);
+ request.onerror = function(event) {
+ assert.fail("Failed to open db");
+ done();
+ };
+ request.onsuccess = function(event) {
+ assert.pass("transaction is complete");
+ testRead(assert, done);
+ }
+ request.onupgradeneeded = function(event) {
+ assert.pass("data base upgrade")
+
+ var db = event.target.result;
+
+ // Create an objectStore to hold information about our customers. We"re
+ // going to use "ssn" as our key path because it"s guaranteed to be
+ // unique.
+ var objectStore = db.createObjectStore("customers", { keyPath: "ssn" });
+
+ // Create an index to search customers by name. We may have duplicates
+ // so we can"t use a unique index.
+ objectStore.createIndex("name", "name", { unique: false });
+
+ // Create an index to search customers by email. We want to ensure that
+ // no two customers have the same email, so use a unique index.
+ objectStore.createIndex("email", "email", { unique: true });
+
+ // Store values in the newly created objectStore.
+ customerData.forEach(function(data) {
+ objectStore.add(data);
+ });
+ assert.pass("data added to object store");
+ };
+};
+
+function testRead(assert, done) {
+ let dbName = "the_name";
+ let request = indexedDB.open(dbName, 2);
+ request.onsuccess = function(event) {
+ assert.pass("data opened")
+ var db = event.target.result;
+ let transaction = db.transaction(["customers"]);
+ var objectStore = transaction.objectStore("customers");
+ var request = objectStore.get("444-44-4444");
+ request.onerror = function(event) {
+ assert.fail("Failed to retrive data")
+ };
+ request.onsuccess = function(event) {
+ // Do something with the request.result!
+ assert.equal(request.result.name, "Bill", "Name is correct");
+ done();
+ };
+ };
+ request.onerror = function() {
+ assert.fail("failed to open db");
+ };
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-jetpack-id.js b/addon-sdk/source/test/test-jetpack-id.js
new file mode 100644
index 000000000..99479e32d
--- /dev/null
+++ b/addon-sdk/source/test/test-jetpack-id.js
@@ -0,0 +1,64 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+var getID = require("jetpack-id/index");
+
+exports["test Returns GUID when `id` GUID"] = assert => {
+ var guid = "{8490ae4f-93bc-13af-80b3-39adf9e7b243}";
+ assert.equal(getID({ id: guid }), guid);
+};
+
+exports["test Returns domain id when `id` domain id"] = assert => {
+ var id = "my-addon@jetpack";
+ assert.equal(getID({ id: id }), id);
+};
+
+exports["test allows underscores in name"] = assert => {
+ var name = "my_addon";
+ assert.equal(getID({ name: name }), `@${name}`);
+};
+
+exports["test allows underscores in id"] = assert => {
+ var id = "my_addon@jetpack";
+ assert.equal(getID({ id: id }), id);
+};
+
+exports["test Returns valid name when `name` exists"] = assert => {
+ var id = "my-addon";
+ assert.equal(getID({ name: id }), `@${id}`);
+};
+
+
+exports["test Returns null when `id` and `name` do not exist"] = assert => {
+ assert.equal(getID({}), null)
+}
+
+exports["test Returns null when no object passed in"] = assert => {
+ assert.equal(getID(), null)
+}
+
+exports["test Returns null when `id` exists but not GUID/domain"] = assert => {
+ var id = "my-addon";
+ assert.equal(getID({ id: id }), null);
+}
+
+exports["test Returns null when `id` contains multiple @"] = assert => {
+ assert.equal(getID({ id: "my@addon@yeah" }), null);
+};
+
+exports["test Returns null when `id` or `name` specified in domain format but has invalid characters"] = assert => {
+ [" ", "!", "/", "$", " ", "~", "("].forEach(sym => {
+ assert.equal(getID({ id: "my" + sym + "addon@domain" }), null);
+ assert.equal(getID({ name: "my" + sym + "addon" }), null);
+ });
+};
+
+exports["test Returns null, does not crash, when providing non-string properties for `name` and `id`"] = assert => {
+ assert.equal(getID({ id: 5 }), null);
+ assert.equal(getID({ name: 5 }), null);
+ assert.equal(getID({ name: {} }), null);
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-keyboard-observer.js b/addon-sdk/source/test/test-keyboard-observer.js
new file mode 100644
index 000000000..18f32eab3
--- /dev/null
+++ b/addon-sdk/source/test/test-keyboard-observer.js
@@ -0,0 +1,36 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { keyPress } = require("sdk/dom/events/keys");
+const { Loader } = require("sdk/test/loader");
+const timer = require("sdk/timers");
+
+exports["test unload keyboard observer"] = function(assert, done) {
+ let loader = Loader(module);
+ let element = loader.require("sdk/deprecated/window-utils").
+ activeBrowserWindow.document.documentElement;
+ let observer = loader.require("sdk/keyboard/observer").
+ observer;
+ let called = 0;
+
+ observer.on("keypress", function () { called++; });
+
+ // dispatching "keypress" event to trigger observer listeners.
+ keyPress(element, "accel-%");
+
+ // Unload the module.
+ loader.unload();
+
+ // dispatching "keypress" even once again.
+ keyPress(element, "accel-%");
+
+ // Enqueuing asserts to make sure that assertion is not performed early.
+ timer.setTimeout(function () {
+ assert.equal(called, 1, "observer was called before unload only.");
+ done();
+ }, 0);
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-keyboard-utils.js b/addon-sdk/source/test/test-keyboard-utils.js
new file mode 100644
index 000000000..00fc841ee
--- /dev/null
+++ b/addon-sdk/source/test/test-keyboard-utils.js
@@ -0,0 +1,61 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const utils = require("sdk/keyboard/utils");
+const runtime = require("sdk/system/runtime");
+
+const isMac = runtime.OS === "Darwin";
+
+exports["test toString"] = function(assert) {
+ assert.equal(utils.toString({
+ key: "B",
+ modifiers: [ "Shift", "Ctrl" ]
+ }), "Shift-Ctrl-B", "toString does not normalizes JSON");
+
+ assert.equal(utils.toString({
+ key: "C",
+ modifiers: [],
+ }), "C", "Works with objects with empty array of modifiers");
+
+ assert.equal(utils.toString(Object.create((function Type() {}).prototype, {
+ key: { value: "d" },
+ modifiers: { value: [ "alt" ] },
+ method: { value: function() {} }
+ })), "alt-d", "Works with non-json objects");
+
+ assert.equal(utils.toString({
+ modifiers: [ "shift", "alt" ]
+ }), "shift-alt-", "works with only modifiers");
+};
+
+exports["test toJSON"] = function(assert) {
+ assert.deepEqual(utils.toJSON("Shift-Ctrl-B"), {
+ key: "b",
+ modifiers: [ "control", "shift" ]
+ }, "toJSON normalizes input");
+
+ assert.deepEqual(utils.toJSON("Meta-Alt-option-C"), {
+ key: "c",
+ modifiers: [ "alt", "meta" ]
+ }, "removes dublicates");
+
+ assert.deepEqual(utils.toJSON("AccEl+sHiFt+Z", "+"), {
+ key: "z",
+ modifiers: isMac ? [ "meta", "shift" ] : [ "control", "shift" ]
+ }, "normalizes OS specific keys and adjustes seperator");
+};
+
+exports["test normalize"] = function assert(assert) {
+ assert.equal(utils.normalize("Shift Ctrl A control ctrl", " "),
+ "control shift a", "removes reapeted modifiers");
+ assert.equal(utils.normalize("shift-ctrl-left"), "control-shift-left",
+ "normilizes non printed characters");
+
+ assert.throws(function() {
+ utils.normalize("shift-alt-b-z");
+ }, "throws if contains more then on non-modifier key");
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-l10n-locale.js b/addon-sdk/source/test/test-l10n-locale.js
new file mode 100644
index 000000000..564abbf1b
--- /dev/null
+++ b/addon-sdk/source/test/test-l10n-locale.js
@@ -0,0 +1,169 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const { getPreferedLocales, findClosestLocale } = require("sdk/l10n/locale");
+const prefs = require("sdk/preferences/service");
+const { Cc, Ci, Cu } = require("chrome");
+const { Services } = Cu.import("resource://gre/modules/Services.jsm");
+const BundleService = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService);
+
+const PREF_MATCH_OS_LOCALE = "intl.locale.matchOS";
+const PREF_SELECTED_LOCALE = "general.useragent.locale";
+const PREF_ACCEPT_LANGUAGES = "intl.accept_languages";
+
+function assertPrefered(assert, expected, msg) {
+ assert.equal(JSON.stringify(getPreferedLocales()), JSON.stringify(expected),
+ msg);
+}
+
+exports.testGetPreferedLocales = function(assert) {
+ prefs.set(PREF_MATCH_OS_LOCALE, false);
+ prefs.set(PREF_SELECTED_LOCALE, "");
+ prefs.set(PREF_ACCEPT_LANGUAGES, "");
+ assertPrefered(assert, ["en-us"],
+ "When all preferences are empty, we only have en-us");
+
+ prefs.set(PREF_SELECTED_LOCALE, "fr");
+ prefs.set(PREF_ACCEPT_LANGUAGES, "jp");
+ assertPrefered(assert, ["fr", "jp", "en-us"],
+ "We first have useragent locale, then web one and finally en-US");
+
+ prefs.set(PREF_SELECTED_LOCALE, "en-US");
+ prefs.set(PREF_ACCEPT_LANGUAGES, "en-US");
+ assertPrefered(assert, ["en-us"],
+ "We do not have duplicates");
+
+ prefs.set(PREF_SELECTED_LOCALE, "en-US");
+ prefs.set(PREF_ACCEPT_LANGUAGES, "fr");
+ assertPrefered(assert, ["en-us", "fr"],
+ "en-US can be first if specified by higher priority preference");
+
+ // Reset what we changed
+ prefs.reset(PREF_MATCH_OS_LOCALE);
+ prefs.reset(PREF_SELECTED_LOCALE);
+ prefs.reset(PREF_ACCEPT_LANGUAGES);
+}
+
+// In some cases, mainly on Fennec and on Linux version,
+// `general.useragent.locale` is a special 'localized' value, like:
+// "chrome://global/locale/intl.properties"
+exports.testPreferedLocalizedLocale = function(assert) {
+ prefs.set(PREF_MATCH_OS_LOCALE, false);
+ let bundleURL = "chrome://global/locale/intl.properties";
+ prefs.setLocalized(PREF_SELECTED_LOCALE, bundleURL);
+ let contentLocale = "ja";
+ prefs.set(PREF_ACCEPT_LANGUAGES, contentLocale);
+
+ // Read manually the expected locale value from the property file
+ let expectedLocale = BundleService.createBundle(bundleURL).
+ GetStringFromName(PREF_SELECTED_LOCALE).
+ toLowerCase();
+
+ // First add the useragent locale
+ let expectedLocaleList = [expectedLocale];
+
+ // Then the content locale
+ if (expectedLocaleList.indexOf(contentLocale) == -1)
+ expectedLocaleList.push(contentLocale);
+
+ // Add default "en-us" fallback if the main language is not already en-us
+ if (expectedLocaleList.indexOf("en-us") == -1)
+ expectedLocaleList.push("en-us");
+
+ assertPrefered(assert, expectedLocaleList, "test localized pref value");
+
+ // Reset what we have changed
+ prefs.reset(PREF_MATCH_OS_LOCALE);
+ prefs.reset(PREF_SELECTED_LOCALE);
+ prefs.reset(PREF_ACCEPT_LANGUAGES);
+}
+
+// On Linux the PREF_ACCEPT_LANGUAGES pref can be a localized pref.
+exports.testPreferedContentLocale = function(assert) {
+ prefs.set(PREF_MATCH_OS_LOCALE, false);
+ let noLocale = "",
+ bundleURL = "chrome://global/locale/intl.properties";
+ prefs.set(PREF_SELECTED_LOCALE, noLocale);
+ prefs.setLocalized(PREF_ACCEPT_LANGUAGES, bundleURL);
+
+ // Read the expected locale values from the property file
+ let expectedLocaleList = BundleService.createBundle(bundleURL).
+ GetStringFromName(PREF_ACCEPT_LANGUAGES).
+ split(",").
+ map(locale => locale.trim().toLowerCase());
+
+ // Add default "en-us" fallback if the main language is not already en-us
+ if (expectedLocaleList.indexOf("en-us") == -1)
+ expectedLocaleList.push("en-us");
+
+ assertPrefered(assert, expectedLocaleList, "test localized content locale pref value");
+
+ // Reset what we have changed
+ prefs.reset(PREF_MATCH_OS_LOCALE);
+ prefs.reset(PREF_SELECTED_LOCALE);
+ prefs.reset(PREF_ACCEPT_LANGUAGES);
+}
+
+exports.testPreferedOsLocale = function(assert) {
+ prefs.set(PREF_MATCH_OS_LOCALE, true);
+ prefs.set(PREF_SELECTED_LOCALE, "");
+ prefs.set(PREF_ACCEPT_LANGUAGES, "");
+
+ let expectedLocale = Services.locale.getLocaleComponentForUserAgent().
+ toLowerCase();
+ let expectedLocaleList = [expectedLocale];
+
+ // Add default "en-us" fallback if the main language is not already en-us
+ if (expectedLocale != "en-us")
+ expectedLocaleList.push("en-us");
+
+ assertPrefered(assert, expectedLocaleList, "Ensure that we select OS locale when related preference is set");
+
+ // Reset what we have changed
+ prefs.reset(PREF_MATCH_OS_LOCALE);
+ prefs.reset(PREF_SELECTED_LOCALE);
+ prefs.reset(PREF_ACCEPT_LANGUAGES);
+}
+
+exports.testFindClosestLocale = function(assert) {
+ // Second param of findClosestLocale (aMatchLocales) have to be in lowercase
+ assert.equal(findClosestLocale([], []), null,
+ "When everything is empty we get null");
+
+ assert.equal(findClosestLocale(["en", "en-US"], ["en"]),
+ "en", "We always accept exact match first 1/5");
+ assert.equal(findClosestLocale(["en-US", "en"], ["en"]),
+ "en", "We always accept exact match first 2/5");
+ assert.equal(findClosestLocale(["en", "en-US"], ["en-us"]),
+ "en-US", "We always accept exact match first 3/5");
+ assert.equal(findClosestLocale(["ja-JP-mac", "ja", "ja-JP"], ["ja-jp"]),
+ "ja-JP", "We always accept exact match first 4/5");
+ assert.equal(findClosestLocale(["ja-JP-mac", "ja", "ja-JP"], ["ja-jp-mac"]),
+ "ja-JP-mac", "We always accept exact match first 5/5");
+
+ assert.equal(findClosestLocale(["en", "en-GB"], ["en-us"]),
+ "en", "We accept more generic locale, when there is no exact match 1/2");
+ assert.equal(findClosestLocale(["en-ZA", "en"], ["en-gb"]),
+ "en", "We accept more generic locale, when there is no exact match 2/2");
+
+ assert.equal(findClosestLocale(["ja-JP"], ["ja"]),
+ "ja-JP", "We accept more specialized locale, when there is no exact match 1/2");
+ // Better to select "ja" in this case but behave same as current AddonManager
+ assert.equal(findClosestLocale(["ja-JP-mac", "ja"], ["ja-jp"]),
+ "ja-JP-mac", "We accept more specialized locale, when there is no exact match 2/2");
+
+ assert.equal(findClosestLocale(["en-US"], ["en-us"]),
+ "en-US", "We keep the original one as result 1/2");
+ assert.equal(findClosestLocale(["en-us"], ["en-us"]),
+ "en-us", "We keep the original one as result 2/2");
+
+ assert.equal(findClosestLocale(["ja-JP-mac"], ["ja-jp-mac"]),
+ "ja-JP-mac", "We accept locale with 3 parts");
+ assert.equal(findClosestLocale(["ja-JP"], ["ja-jp-mac"]),
+ "ja-JP", "We accept locale with 2 parts from locale with 3 parts");
+ assert.equal(findClosestLocale(["ja"], ["ja-jp-mac"]),
+ "ja", "We accept locale with 1 part from locale with 3 parts");
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-l10n-plural-rules.js b/addon-sdk/source/test/test-l10n-plural-rules.js
new file mode 100644
index 000000000..953d977a4
--- /dev/null
+++ b/addon-sdk/source/test/test-l10n-plural-rules.js
@@ -0,0 +1,85 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+ "use strict";
+
+const { getRulesForLocale } = require("sdk/l10n/plural-rules");
+
+// For more information, please visit unicode website:
+// http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
+
+function map(assert, f, n, form) {
+ assert.equal(f(n), form, n + " maps to '" + form + "'");
+}
+
+exports.testFrench = function(assert) {
+ let f = getRulesForLocale("fr");
+ map(assert, f, -1, "other");
+ map(assert, f, 0, "one");
+ map(assert, f, 1, "one");
+ map(assert, f, 1.5, "one");
+ map(assert, f, 2, "other");
+ map(assert, f, 100, "other");
+}
+
+exports.testEnglish = function(assert) {
+ let f = getRulesForLocale("en");
+ map(assert, f, -1, "other");
+ map(assert, f, 0, "other");
+ map(assert, f, 1, "one");
+ map(assert, f, 1.5, "other");
+ map(assert, f, 2, "other");
+ map(assert, f, 100, "other");
+}
+
+exports.testArabic = function(assert) {
+ let f = getRulesForLocale("ar");
+ map(assert, f, -1, "other");
+ map(assert, f, 0, "zero");
+ map(assert, f, 0.5, "other");
+
+ map(assert, f, 1, "one");
+ map(assert, f, 1.5, "other");
+
+ map(assert, f, 2, "two");
+ map(assert, f, 2.5, "other");
+
+ map(assert, f, 3, "few");
+ map(assert, f, 3.5, "few"); // I'd expect it to be 'other', but the unicode.org
+ // algorithm computes 'few'.
+ map(assert, f, 5, "few");
+ map(assert, f, 10, "few");
+ map(assert, f, 103, "few");
+ map(assert, f, 105, "few");
+ map(assert, f, 110, "few");
+ map(assert, f, 203, "few");
+ map(assert, f, 205, "few");
+ map(assert, f, 210, "few");
+
+ map(assert, f, 11, "many");
+ map(assert, f, 50, "many");
+ map(assert, f, 99, "many");
+ map(assert, f, 111, "many");
+ map(assert, f, 150, "many");
+ map(assert, f, 199, "many");
+
+ map(assert, f, 100, "other");
+ map(assert, f, 101, "other");
+ map(assert, f, 102, "other");
+ map(assert, f, 200, "other");
+ map(assert, f, 201, "other");
+ map(assert, f, 202, "other");
+}
+
+exports.testJapanese = function(assert) {
+ // Japanese doesn't have plural forms.
+ let f = getRulesForLocale("ja");
+ map(assert, f, -1, "other");
+ map(assert, f, 0, "other");
+ map(assert, f, 1, "other");
+ map(assert, f, 1.5, "other");
+ map(assert, f, 2, "other");
+ map(assert, f, 100, "other");
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-lang-type.js b/addon-sdk/source/test/test-lang-type.js
new file mode 100644
index 000000000..c0e510076
--- /dev/null
+++ b/addon-sdk/source/test/test-lang-type.js
@@ -0,0 +1,166 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict"
+
+var utils = require("sdk/lang/type");
+
+exports["test function"] = function (assert) {
+ assert.equal(utils.isFunction(function(){}), true, "value is a function");
+ assert.equal(utils.isFunction(Object), true, "Object is a function");
+ assert.equal(utils.isFunction(new Function("")), true, "Genertaed value is a function");
+ assert.equal(utils.isFunction({}), false, "object is not a function");
+ assert.equal(utils.isFunction(4), false, "number is not a function");
+};
+
+exports["test atoms"] = function (assert) {
+ assert.equal(utils.isPrimitive(2), true, "number is a primitive");
+ assert.equal(utils.isPrimitive(NaN), true, "`NaN` is a primitve");
+ assert.equal(utils.isPrimitive(undefined), true, "`undefined` is a primitive");
+ assert.equal(utils.isPrimitive(null), true, "`null` is a primitive");
+ assert.equal(utils.isPrimitive(Infinity), true, "`Infinity` is a primitive");
+ assert.equal(utils.isPrimitive("foo"), true, "strings are a primitive");
+ assert.ok(utils.isPrimitive(true) && utils.isPrimitive(false),
+ "booleans are primitive");
+};
+
+exports["test object"] = function (assert) {
+ assert.equal(utils.isObject({}), true, "`{}` is an object");
+ assert.ok(!utils.isObject(null), "`null` is not an object");
+ assert.ok(!utils.isObject(Object), "functions is not an object");
+};
+
+exports["test generator"] = function (assert) {
+ assert.equal(utils.isGenerator(function*(){}), true, "`function*(){}` is a generator");
+ assert.equal(utils.isGenerator(function(){}), false, "`function(){}` is not a generator");
+ assert.equal(utils.isGenerator(() => {}), false, "`() => {}` is not a generator");
+ assert.equal(utils.isGenerator({}), false, "`{}` is not a generator");
+ assert.equal(utils.isGenerator(1), false, "`1` is not a generator");
+ assert.equal(utils.isGenerator([]), false, "`[]` is not a generator");
+ assert.equal(utils.isGenerator(null), false, "`null` is not a generator");
+ assert.equal(utils.isGenerator(undefined), false, "`undefined` is not a generator");
+};
+
+exports["test array"] = function (assert) {
+ assert.equal(utils.isArray([]), true, "[] is an array");
+ assert.equal(utils.isArray([1]), true, "[1] is an array");
+ assert.equal(utils.isArray(new Array()), true, "new Array() is an array");
+ assert.equal(utils.isArray(new Array(10)), true, "new Array(10) is an array");
+ assert.equal(utils.isArray(Array.prototype), true, "Array.prototype is an array");
+
+ assert.equal(utils.isArray(), false, "implicit undefined is not an array");
+ assert.equal(utils.isArray(null), false, "null is not an array");
+ assert.equal(utils.isArray(undefined), false, "undefined is not an array");
+ assert.equal(utils.isArray(1), false, "1 is not an array");
+ assert.equal(utils.isArray(true), false, "true is not an array");
+ assert.equal(utils.isArray('foo'), false, "'foo' is not an array");
+ assert.equal(utils.isArray({}), false, "{} is not an array");
+ assert.equal(utils.isArray(Symbol.iterator), false, "Symbol.iterator is not an array");
+};
+
+exports["test arguments"] = function (assert) {
+ assert.equal(utils.isArguments(arguments), true, "arguments is an arguments");
+ (function() {
+ assert.equal(utils.isArguments(arguments), true, "arguments in nested function is an arguments");
+ })();
+ (function*() {
+ assert.equal(utils.isArguments(arguments), true, "arguments in nested generator is an arguments");
+ })();
+ (() => {
+ assert.equal(utils.isArguments(arguments), true, "arguments in arrow function is an arguments");
+ })();
+
+ assert.equal(utils.isArguments(), false, "implicit undefined is not an arguments");
+ assert.equal(utils.isArguments(null), false, "null is not an arguments");
+ assert.equal(utils.isArguments(undefined), false, "undefined is not an arguments");
+ assert.equal(utils.isArguments(1), false, "1 is not an arguments");
+ assert.equal(utils.isArguments(true), false, "true is not an arguments");
+ assert.equal(utils.isArguments('foo'), false, "'foo' is not an arguments");
+ assert.equal(utils.isArguments([]), false, "[] is not an arguments");
+ assert.equal(utils.isArguments({}), false, "{} is not an arguments");
+ assert.equal(utils.isArguments(Symbol.iterator), false, "Symbol.iterator is not an arguments");
+ (function(...args) {
+ assert.equal(utils.isArguments(args), false, "rest arguments is not an arguments");
+ })();
+};
+
+exports["test flat objects"] = function (assert) {
+ assert.ok(utils.isFlat({}), "`{}` is a flat object");
+ assert.ok(!utils.isFlat([]), "`[]` is not a flat object");
+ assert.ok(!utils.isFlat(new function() {}), "derived objects are not flat");
+ assert.ok(utils.isFlat(Object.prototype), "Object.prototype is flat");
+};
+
+exports["test json atoms"] = function (assert) {
+ assert.ok(utils.isJSON(null), "`null` is JSON");
+ assert.ok(utils.isJSON(undefined), "`undefined` is JSON");
+ assert.ok(utils.isJSON(NaN), "`NaN` is JSON");
+ assert.ok(utils.isJSON(Infinity), "`Infinity` is JSON");
+ assert.ok(utils.isJSON(true) && utils.isJSON(false), "booleans are JSON");
+ assert.ok(utils.isJSON(4), utils.isJSON(0), "numbers are JSON");
+ assert.ok(utils.isJSON("foo bar"), "strings are JSON");
+};
+
+exports["test jsonable values"] = function (assert) {
+ assert.ok(utils.isJSONable(null), "`null` is JSONable");
+ assert.ok(!utils.isJSONable(undefined), "`undefined` is not JSONable");
+ assert.ok(utils.isJSONable(NaN), "`NaN` is JSONable");
+ assert.ok(utils.isJSONable(Infinity), "`Infinity` is JSONable");
+ assert.ok(utils.isJSONable(true) && utils.isJSONable(false), "booleans are JSONable");
+ assert.ok(utils.isJSONable(0), "numbers are JSONable");
+ assert.ok(utils.isJSONable("foo bar"), "strings are JSONable");
+ assert.ok(!utils.isJSONable(function(){}), "functions are not JSONable");
+
+ const functionWithToJSON = function(){};
+ functionWithToJSON.toJSON = function() { return "foo bar"; };
+ assert.ok(utils.isJSONable(functionWithToJSON), "functions with toJSON() are JSONable");
+
+ assert.ok(utils.isJSONable({}), "`{}` is JSONable");
+
+ const foo = {};
+ foo.bar = foo;
+ assert.ok(!utils.isJSONable(foo), "recursive objects are not JSONable");
+};
+
+exports["test instanceOf"] = function (assert) {
+ assert.ok(utils.instanceOf(assert, Object),
+ "assert is object from other sandbox");
+ assert.ok(utils.instanceOf(new Date(), Date), "instance of date");
+ assert.ok(!utils.instanceOf(null, Object), "null is not an instance");
+};
+
+exports["test json"] = function (assert) {
+ assert.ok(!utils.isJSON(function(){}), "functions are not json");
+ assert.ok(utils.isJSON({}), "`{}` is JSON");
+ assert.ok(utils.isJSON({
+ a: "foo",
+ b: 3,
+ c: undefined,
+ d: null,
+ e: {
+ f: {
+ g: "bar",
+ p: [{}, "oueou", 56]
+ },
+ q: { nan: NaN, infinity: Infinity },
+ "non standard name": "still works"
+ }
+ }), "JSON can contain nested objects");
+
+ var foo = {};
+ var bar = { foo: foo };
+ foo.bar = bar;
+ assert.ok(!utils.isJSON(foo), "recursive objects are not json");
+
+
+ assert.ok(!utils.isJSON({ get foo() { return 5 } }),
+ "json can not have getter");
+
+ assert.ok(!utils.isJSON({ foo: "bar", baz: function () {} }),
+ "json can not contain functions");
+
+ assert.ok(!utils.isJSON(Object.create({})),
+ "json must be direct descendant of `Object.prototype`");
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-libxul.js b/addon-sdk/source/test/test-libxul.js
new file mode 100644
index 000000000..7a11a69cb
--- /dev/null
+++ b/addon-sdk/source/test/test-libxul.js
@@ -0,0 +1,18 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// Test that we can link with libxul using js-ctypes
+
+const {Cu} = require("chrome");
+const {ctypes} = Cu.import("resource://gre/modules/ctypes.jsm", {});
+const {OS} = Cu.import("resource://gre/modules/osfile.jsm", {});
+
+exports.test = function(assert) {
+ let path = OS.Constants.Path.libxul;
+ assert.pass("libxul is at " + path);
+ let lib = ctypes.open(path);
+ assert.ok(lib != null, "linked to libxul successfully");
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-list.js b/addon-sdk/source/test/test-list.js
new file mode 100644
index 000000000..9b03a9513
--- /dev/null
+++ b/addon-sdk/source/test/test-list.js
@@ -0,0 +1,58 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { List, addListItem, removeListItem } = require('sdk/util/list');
+const { Class } = require('sdk/core/heritage');
+
+exports.testList = function(assert) {
+ let list = List();
+ addListItem(list, 1);
+
+ for (let key in list) {
+ assert.equal(key, 0, 'key is correct');
+ assert.equal(list[key], 1, 'value is correct');
+ }
+
+ let count = 0;
+ for (let ele of list) {
+ assert.equal(ele, 1, 'ele is correct');
+ assert.equal(++count, 1, 'count is correct');
+ }
+
+ count = 0;
+ for (let ele of list) {
+ assert.equal(ele, 1, 'ele is correct');
+ assert.equal(++count, 1, 'count is correct');
+ }
+
+ removeListItem(list, 1);
+ assert.equal(list.length, 0, 'remove worked');
+};
+
+exports.testImplementsList = function(assert) {
+ let List2 = Class({
+ implements: [List],
+ initialize: function() {
+ List.prototype.initialize.apply(this, [0, 1, 2]);
+ }
+ });
+ let list2 = List2();
+ let count = 0;
+
+ for (let ele of list2) {
+ assert.equal(ele, count++, 'ele is correct');
+ }
+
+ count = 0;
+ for (let ele of list2) {
+ assert.equal(ele, count++, 'ele is correct');
+ }
+
+ addListItem(list2, 3);
+ assert.equal(list2.length, 4, '3 was added');
+ assert.equal(list2[list2.length-1], 3, '3 was added');
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-loader.js b/addon-sdk/source/test/test-loader.js
new file mode 100644
index 000000000..3ee3e34f0
--- /dev/null
+++ b/addon-sdk/source/test/test-loader.js
@@ -0,0 +1,657 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+var {
+ Loader, main, unload, parseStack, resolve, join,
+ Require, Module
+} = require('toolkit/loader');
+var { readURI } = require('sdk/net/url');
+
+var root = module.uri.substr(0, module.uri.lastIndexOf('/'));
+
+const app = require('sdk/system/xul-app');
+
+// The following adds Debugger constructor to the global namespace.
+const { Cu } = require('chrome');
+const { addDebuggerToGlobal } = Cu.import('resource://gre/modules/jsdebugger.jsm', {});
+addDebuggerToGlobal(this);
+
+exports['test resolve'] = function (assert) {
+ let cuddlefish_id = 'sdk/loader/cuddlefish';
+ assert.equal(resolve('../index.js', './dir/c.js'), './index.js');
+ assert.equal(resolve('./index.js', './dir/c.js'), './dir/index.js');
+ assert.equal(resolve('./dir/c.js', './index.js'), './dir/c.js');
+ assert.equal(resolve('../utils/file.js', './dir/b.js'), './utils/file.js');
+
+ assert.equal(resolve('../utils/./file.js', './dir/b.js'), './utils/file.js');
+ assert.equal(resolve('../utils/file.js', './'), './../utils/file.js');
+ assert.equal(resolve('./utils/file.js', './'), './utils/file.js');
+ assert.equal(resolve('./utils/file.js', './index.js'), './utils/file.js');
+
+ assert.equal(resolve('../utils/./file.js', cuddlefish_id), 'sdk/utils/file.js');
+ assert.equal(resolve('../utils/file.js', cuddlefish_id), 'sdk/utils/file.js');
+ assert.equal(resolve('./utils/file.js', cuddlefish_id), 'sdk/loader/utils/file.js');
+
+ assert.equal(resolve('..//index.js', './dir/c.js'), './index.js');
+ assert.equal(resolve('../modules/XPCOMUtils.jsm', 'resource://gre/utils/file.js'), 'resource://gre/modules/XPCOMUtils.jsm');
+ assert.equal(resolve('../modules/XPCOMUtils.jsm', 'chrome://gre/utils/file.js'), 'chrome://gre/modules/XPCOMUtils.jsm');
+ assert.equal(resolve('../../a/b/c.json', 'file:///thing/utils/file.js'), 'file:///a/b/c.json');
+
+ // Does not change absolute paths
+ assert.equal(resolve('resource://gre/modules/file.js', './dir/b.js'),
+ 'resource://gre/modules/file.js');
+ assert.equal(resolve('file:///gre/modules/file.js', './dir/b.js'),
+ 'file:///gre/modules/file.js');
+ assert.equal(resolve('/root.js', './dir/b.js'),
+ '/root.js');
+};
+
+exports['test join'] = function (assert) {
+ assert.equal(join('a/path', '../../../module'), '../module');
+ assert.equal(join('a/path/to', '../module'), 'a/path/module');
+ assert.equal(join('a/path/to', './module'), 'a/path/to/module');
+ assert.equal(join('a/path/to', '././../module'), 'a/path/module');
+ assert.equal(join('resource://my/path/yeah/yuh', '../whoa'),
+ 'resource://my/path/yeah/whoa');
+ assert.equal(join('resource://my/path/yeah/yuh', './whoa'),
+ 'resource://my/path/yeah/yuh/whoa');
+ assert.equal(join('resource:///my/path/yeah/yuh', '../whoa'),
+ 'resource:///my/path/yeah/whoa');
+ assert.equal(join('resource:///my/path/yeah/yuh', './whoa'),
+ 'resource:///my/path/yeah/yuh/whoa');
+ assert.equal(join('file:///my/path/yeah/yuh', '../whoa'),
+ 'file:///my/path/yeah/whoa');
+ assert.equal(join('file:///my/path/yeah/yuh', './whoa'),
+ 'file:///my/path/yeah/yuh/whoa');
+ assert.equal(join('a/path/to', '..//module'), 'a/path/module');
+};
+
+exports['test dependency cycles'] = function(assert) {
+ let uri = root + '/fixtures/loader/cycles/';
+ let loader = Loader({ paths: { '': uri } });
+
+ let program = main(loader, 'main');
+
+ assert.equal(program.a.b, program.b, 'module `a` gets correct `b`');
+ assert.equal(program.b.a, program.a, 'module `b` gets correct `a`');
+ assert.equal(program.c.main, program, 'module `c` gets correct `main`');
+
+ unload(loader);
+}
+
+exports['test syntax errors'] = function(assert) {
+ let uri = root + '/fixtures/loader/syntax-error/';
+ let loader = Loader({ paths: { '': uri } });
+
+ try {
+ let program = main(loader, 'main');
+ } catch (error) {
+ assert.equal(error.name, "SyntaxError", "throws syntax error");
+ assert.equal(error.fileName.split("/").pop(), "error.js",
+ "Error contains filename");
+ assert.equal(error.lineNumber, 11, "error is on line 11");
+ let stack = parseStack(error.stack);
+
+ assert.equal(stack.pop().fileName, uri + "error.js",
+ "last frame file containing syntax error");
+ assert.equal(stack.pop().fileName, uri + "main.js",
+ "previous frame is a requirer module");
+ assert.equal(stack.pop().fileName, module.uri,
+ "previous to it is a test module");
+
+ } finally {
+ unload(loader);
+ }
+}
+
+exports['test sandboxes are not added if error'] = function (assert) {
+ let uri = root + '/fixtures/loader/missing-twice/';
+ let loader = Loader({ paths: { '': uri } });
+ let program = main(loader, 'main');
+ assert.ok(!(uri + 'not-found.js' in loader.sandboxes), 'not-found.js not in loader.sandboxes');
+}
+
+exports['test missing module'] = function(assert) {
+ let uri = root + '/fixtures/loader/missing/'
+ let loader = Loader({ paths: { '': uri } });
+
+ try {
+ let program = main(loader, 'main')
+ } catch (error) {
+ assert.equal(error.message, "Module `not-found` is not found at " +
+ uri + "not-found.js", "throws if error not found");
+
+ assert.equal(error.fileName.split("/").pop(), "main.js",
+ "Error fileName is requirer module");
+
+ assert.equal(error.lineNumber, 7, "error is on line 7");
+
+ let stack = parseStack(error.stack);
+
+ assert.equal(stack.pop().fileName, uri + "main.js",
+ "loader stack is omitted");
+
+ assert.equal(stack.pop().fileName, module.uri,
+ "previous in the stack is test module");
+ } finally {
+ unload(loader);
+ }
+}
+
+exports["test invalid module not cached and throws everytime"] = function(assert) {
+ let uri = root + "/fixtures/loader/missing-twice/";
+ let loader = Loader({ paths: { "": uri } });
+
+ let { firstError, secondError, invalidJSON1, invalidJSON2 } = main(loader, "main");
+ assert.equal(firstError.message, "Module `not-found` is not found at " +
+ uri + "not-found.js", "throws on first invalid require");
+ assert.equal(firstError.lineNumber, 8, "first error is on line 7");
+ assert.equal(secondError.message, "Module `not-found` is not found at " +
+ uri + "not-found.js", "throws on second invalid require");
+ assert.equal(secondError.lineNumber, 14, "second error is on line 14");
+
+ assert.equal(invalidJSON1.message,
+ "JSON.parse: unexpected character at line 1 column 1 of the JSON data",
+ "throws on invalid JSON");
+ assert.equal(invalidJSON2.message,
+ "JSON.parse: unexpected character at line 1 column 1 of the JSON data",
+ "throws on invalid JSON second time");
+};
+
+exports['test exceptions in modules'] = function(assert) {
+ let uri = root + '/fixtures/loader/exceptions/'
+
+ let loader = Loader({ paths: { '': uri } });
+
+ try {
+ let program = main(loader, 'main')
+ } catch (error) {
+ assert.equal(error.message, "Boom!", "thrown errors propagate");
+
+ assert.equal(error.fileName.split("/").pop(), "boomer.js",
+ "Error comes from the module that threw it");
+
+ assert.equal(error.lineNumber, 8, "error is on line 8");
+
+ let stack = parseStack(error.stack);
+
+ let frame = stack.pop()
+ assert.equal(frame.fileName, uri + "boomer.js",
+ "module that threw is first in the stack");
+ assert.equal(frame.name, "exports.boom",
+ "name is in the stack");
+
+ frame = stack.pop()
+ assert.equal(frame.fileName, uri + "main.js",
+ "module that called it is next in the stack");
+ assert.equal(frame.lineNumber, 9, "caller line is in the stack");
+
+
+ assert.equal(stack.pop().fileName, module.uri,
+ "this test module is next in the stack");
+ } finally {
+ unload(loader);
+ }
+}
+
+exports['test early errors in module'] = function(assert) {
+ let uri = root + '/fixtures/loader/errors/';
+ let loader = Loader({ paths: { '': uri } });
+
+ try {
+ let program = main(loader, 'main')
+ } catch (error) {
+ assert.equal(String(error),
+ "Error: opening input stream (invalid filename?)",
+ "thrown errors propagate");
+
+ assert.equal(error.fileName.split("/").pop(), "boomer.js",
+ "Error comes from the module that threw it");
+
+ assert.equal(error.lineNumber, 7, "error is on line 7");
+
+ let stack = parseStack(error.stack);
+
+ let frame = stack.pop()
+ assert.equal(frame.fileName, uri + "boomer.js",
+ "module that threw is first in the stack");
+
+ frame = stack.pop()
+ assert.equal(frame.fileName, uri + "main.js",
+ "module that called it is next in the stack");
+ assert.equal(frame.lineNumber, 7, "caller line is in the stack");
+
+
+ assert.equal(stack.pop().fileName, module.uri,
+ "this test module is next in the stack");
+ } finally {
+ unload(loader);
+ }
+};
+
+exports['test require json'] = function (assert) {
+ let data = require('./fixtures/loader/json/manifest.json');
+ assert.equal(data.name, 'Jetpack Loader Test', 'loads json with strings');
+ assert.equal(data.version, '1.0.1', 'loads json with strings');
+ assert.equal(data.dependencies.async, '*', 'loads json with objects');
+ assert.equal(data.dependencies.underscore, '*', 'loads json with objects');
+ assert.equal(data.contributors.length, 4, 'loads json with arrays');
+ assert.ok(Array.isArray(data.contributors), 'loads json with arrays');
+ data.version = '2.0.0';
+ let newdata = require('./fixtures/loader/json/manifest.json');
+ assert.equal(newdata.version, '2.0.0',
+ 'JSON objects returned should be cached and the same instance');
+
+ try {
+ require('./fixtures/loader/json/invalid.json');
+ assert.fail('Error not thrown when loading invalid json');
+ } catch (err) {
+ assert.ok(err, 'error thrown when loading invalid json');
+ assert.ok(/JSON\.parse/.test(err.message),
+ 'should thrown an error from JSON.parse, not attempt to load .json.js');
+ }
+
+ // Try again to ensure an empty module isn't loaded from cache
+ try {
+ require('./fixtures/loader/json/invalid.json');
+ assert.fail('Error not thrown when loading invalid json a second time');
+ } catch (err) {
+ assert.ok(err,
+ 'error thrown when loading invalid json a second time');
+ assert.ok(/JSON\.parse/.test(err.message),
+ 'should thrown an error from JSON.parse a second time, not attempt to load .json.js');
+ }
+};
+
+exports['test setting metadata for newly created sandboxes'] = function(assert) {
+ let addonID = 'random-addon-id';
+ let uri = root + '/fixtures/loader/cycles/';
+ let loader = Loader({ paths: { '': uri }, id: addonID });
+
+ let dbg = new Debugger();
+ dbg.onNewGlobalObject = function(global) {
+ dbg.onNewGlobalObject = undefined;
+
+ let metadata = Cu.getSandboxMetadata(global.unsafeDereference());
+ assert.ok(metadata, 'this global has attached metadata');
+ assert.equal(metadata.URI, uri + 'main.js', 'URI is set properly');
+ assert.equal(metadata.addonID, addonID, 'addon ID is set');
+ }
+
+ let program = main(loader, 'main');
+};
+
+exports['test require .json, .json.js'] = function (assert) {
+ let testjson = require('./fixtures/loader/json/test.json');
+ assert.equal(testjson.filename, 'test.json',
+ 'require("./x.json") should load x.json, not x.json.js');
+
+ let nodotjson = require('./fixtures/loader/json/nodotjson.json');
+ assert.equal(nodotjson.filename, 'nodotjson.json.js',
+ 'require("./x.json") should load x.json.js when x.json does not exist');
+ nodotjson.data.prop = 'hydralisk';
+
+ // require('nodotjson.json') and require('nodotjson.json.js')
+ // should resolve to the same file
+ let nodotjsonjs = require('./fixtures/loader/json/nodotjson.json.js');
+ assert.equal(nodotjsonjs.data.prop, 'hydralisk',
+ 'js modules are cached whether access via .json.js or .json');
+};
+
+exports['test invisibleToDebugger: false'] = function (assert) {
+ let uri = root + '/fixtures/loader/cycles/';
+ let loader = Loader({ paths: { '': uri } });
+ main(loader, 'main');
+
+ let dbg = new Debugger();
+ let sandbox = loader.sandboxes[uri + 'main.js'];
+
+ try {
+ dbg.addDebuggee(sandbox);
+ assert.ok(true, 'debugger added visible value');
+ } catch(e) {
+ assert.fail('debugger could not add visible value');
+ }
+};
+
+exports['test invisibleToDebugger: true'] = function (assert) {
+ let uri = root + '/fixtures/loader/cycles/';
+ let loader = Loader({ paths: { '': uri }, invisibleToDebugger: true });
+ main(loader, 'main');
+
+ let dbg = new Debugger();
+ let sandbox = loader.sandboxes[uri + 'main.js'];
+
+ try {
+ dbg.addDebuggee(sandbox);
+ assert.fail('debugger added invisible value');
+ } catch(e) {
+ assert.ok(true, 'debugger did not add invisible value');
+ }
+};
+
+exports['test console global by default'] = function (assert) {
+ let uri = root + '/fixtures/loader/globals/';
+ let loader = Loader({ paths: { '': uri }});
+ let program = main(loader, 'main');
+
+ assert.ok(typeof program.console === 'object', 'global `console` exists');
+ assert.ok(typeof program.console.log === 'function', 'global `console.log` exists');
+
+ let loader2 = Loader({ paths: { '': uri }, globals: { console: fakeConsole }});
+ let program2 = main(loader2, 'main');
+
+ assert.equal(program2.console, fakeConsole,
+ 'global console can be overridden with Loader options');
+ function fakeConsole () {};
+};
+
+exports['test shared globals'] = function(assert) {
+ let uri = root + '/fixtures/loader/cycles/';
+ let loader = Loader({ paths: { '': uri }, sharedGlobal: true,
+ sharedGlobalBlocklist: ['b'] });
+
+ let program = main(loader, 'main');
+
+ // As it is hard to verify what is the global of an object
+ // (due to wrappers) we check that we see the `foo` symbol
+ // being manually injected into the shared global object
+ loader.sharedGlobalSandbox.foo = true;
+
+ let m = loader.sandboxes[uri + 'main.js'];
+ let a = loader.sandboxes[uri + 'a.js'];
+ let b = loader.sandboxes[uri + 'b.js'];
+
+ assert.ok(Cu.getGlobalForObject(m).foo, "main is shared");
+ assert.ok(Cu.getGlobalForObject(a).foo, "a is shared");
+ assert.ok(!Cu.getGlobalForObject(b).foo, "b isn't shared");
+
+ unload(loader);
+}
+
+exports['test deprecated shared globals exception name'] = function(assert) {
+ let uri = root + '/fixtures/loader/cycles/';
+ let loader = Loader({ paths: { '': uri }, sharedGlobal: true,
+ sharedGlobalBlacklist: ['b'] });
+
+ let program = main(loader, 'main');
+
+ assert.ok(loader.sharedGlobalBlocklist.includes("b"), "b should be in the blocklist");
+ assert.equal(loader.sharedGlobalBlocklist.length, loader.sharedGlobalBlacklist.length,
+ "both blocklists should have the same number of items.");
+ assert.equal(loader.sharedGlobalBlocklist.join(","), loader.sharedGlobalBlacklist.join(","),
+ "both blocklists should have the same items.");
+
+ // As it is hard to verify what is the global of an object
+ // (due to wrappers) we check that we see the `foo` symbol
+ // being manually injected into the shared global object
+ loader.sharedGlobalSandbox.foo = true;
+
+ let m = loader.sandboxes[uri + 'main.js'];
+ let a = loader.sandboxes[uri + 'a.js'];
+ let b = loader.sandboxes[uri + 'b.js'];
+
+ assert.ok(Cu.getGlobalForObject(m).foo, "main is shared");
+ assert.ok(Cu.getGlobalForObject(a).foo, "a is shared");
+ assert.ok(!Cu.getGlobalForObject(b).foo, "b isn't shared");
+
+ unload(loader);
+}
+
+exports['test prototype of global'] = function (assert) {
+ let uri = root + '/fixtures/loader/globals/';
+ let loader = Loader({ paths: { '': uri }, sharedGlobal: true,
+ sandboxPrototype: { globalFoo: 5 }});
+
+ let program = main(loader, 'main');
+
+ assert.ok(program.globalFoo === 5, '`globalFoo` exists');
+};
+
+exports["test require#resolve"] = function(assert) {
+ let foundRoot = require.resolve("sdk/tabs").replace(/sdk\/tabs.js$/, "");
+ assert.ok(root, foundRoot, "correct resolution root");
+
+ assert.equal(foundRoot + "sdk/tabs.js", require.resolve("sdk/tabs"), "correct resolution of sdk module");
+ assert.equal(foundRoot + "toolkit/loader.js", require.resolve("toolkit/loader"), "correct resolution of sdk module");
+
+ const localLoader = Loader({
+ paths: { "foo/bar": "bizzle",
+ "foo/bar2/": "bizzle2",
+ // Just to make sure this doesn't match the first entry,
+ // let use resolve this module
+ "foo/bar-bar": "foo/bar-bar" }
+ });
+ const localRequire = Require(localLoader, module);
+ assert.equal(localRequire.resolve("foo/bar"), "bizzle.js");
+ assert.equal(localRequire.resolve("foo/bar/baz"), "bizzle/baz.js");
+ assert.equal(localRequire.resolve("foo/bar-bar"), "foo/bar-bar.js");
+ assert.equal(localRequire.resolve("foo/bar2/"), "bizzle2.js");
+};
+
+const modulesURI = require.resolve("toolkit/loader").replace("toolkit/loader.js", "");
+exports["test loading a loader"] = function(assert) {
+ const loader = Loader({ paths: { "": modulesURI } });
+
+ const require = Require(loader, module);
+
+ const requiredLoader = require("toolkit/loader");
+
+ assert.equal(requiredLoader.Loader, Loader,
+ "got the same Loader instance");
+
+ const jsmLoader = Cu.import(require.resolve("toolkit/loader"), {}).Loader;
+
+ assert.equal(jsmLoader.Loader, requiredLoader.Loader,
+ "loading loader via jsm returns same loader");
+
+ unload(loader);
+};
+
+exports['test loader on unsupported modules with checkCompatibility true'] = function(assert) {
+ let loader = Loader({
+ paths: { '': root + "/" },
+ checkCompatibility: true
+ });
+ let require = Require(loader, module);
+
+ assert.throws(() => {
+ if (!app.is('Firefox')) {
+ require('fixtures/loader/unsupported/firefox');
+ }
+ else {
+ require('fixtures/loader/unsupported/fennec');
+ }
+ }, /^Unsupported Application/, "throws Unsupported Application");
+
+ unload(loader);
+};
+
+exports['test loader on unsupported modules with checkCompatibility false'] = function(assert) {
+ let loader = Loader({
+ paths: { '': root + "/" },
+ checkCompatibility: false
+ });
+ let require = Require(loader, module);
+
+ try {
+ if (!app.is('Firefox')) {
+ require('fixtures/loader/unsupported/firefox');
+ }
+ else {
+ require('fixtures/loader/unsupported/fennec');
+ }
+ assert.pass("loaded unsupported module without an error");
+ }
+ catch(e) {
+ assert.fail(e);
+ }
+
+ unload(loader);
+};
+
+exports['test loader on unsupported modules with checkCompatibility default'] = function(assert) {
+ let loader = Loader({ paths: { '': root + "/" } });
+ let require = Require(loader, module);
+
+ try {
+ if (!app.is('Firefox')) {
+ require('fixtures/loader/unsupported/firefox');
+ }
+ else {
+ require('fixtures/loader/unsupported/fennec');
+ }
+ assert.pass("loaded unsupported module without an error");
+ }
+ catch(e) {
+ assert.fail(e);
+ }
+
+ unload(loader);
+};
+
+exports["test Cu.import of toolkit/loader"] = (assert) => {
+ const toolkitLoaderURI = require.resolve("toolkit/loader");
+ const loaderModule = Cu.import(toolkitLoaderURI).Loader;
+ const { Loader, Require, Main } = loaderModule;
+ const version = "0.1.0";
+ const id = `fxos_${version.replace(".", "_")}_simulator@mozilla.org`;
+ const uri = `resource://${encodeURIComponent(id.replace("@", "at"))}/`;
+
+ const loader = Loader({
+ paths: {
+ "./": uri + "lib/",
+ // Can't just put `resource://gre/modules/commonjs/` as it
+ // won't take module overriding into account.
+ "": toolkitLoaderURI.replace("toolkit/loader.js", "")
+ },
+ globals: {
+ console: console
+ },
+ modules: {
+ "toolkit/loader": loaderModule,
+ addon: {
+ id: "simulator",
+ version: "0.1",
+ uri: uri
+ }
+ }
+ });
+
+ let require_ = Require(loader, { id: "./addon" });
+ assert.equal(typeof(loaderModule),
+ typeof(require_("toolkit/loader")),
+ "module returned is whatever was mapped to it");
+};
+
+exports["test Cu.import in b2g style"] = (assert) => {
+ const {FakeCu} = require("./loader/b2g");
+ const toolkitLoaderURI = require.resolve("toolkit/loader");
+ const b2g = new FakeCu();
+
+ const exported = {};
+ const loader = b2g.import(toolkitLoaderURI, exported);
+
+ assert.equal(typeof(exported.Loader),
+ "function",
+ "loader is a function");
+ assert.equal(typeof(exported.Loader.Loader),
+ "function",
+ "Loader.Loader is a funciton");
+};
+
+exports['test lazy globals'] = function (assert) {
+ let uri = root + '/fixtures/loader/lazy/';
+ let gotFoo = false;
+ let foo = {};
+ let modules = {
+ get foo() {
+ gotFoo = true;
+ return foo;
+ }
+ };
+ let loader = Loader({ paths: { '': uri }, modules: modules});
+ assert.ok(!gotFoo, "foo hasn't been accessed during loader instanciation");
+ let program = main(loader, 'main');
+ assert.ok(!gotFoo, "foo hasn't been accessed during module loading");
+ assert.equal(program.useFoo(), foo, "foo mock works");
+ assert.ok(gotFoo, "foo has been accessed only when we first try to use it");
+};
+
+exports['test user global'] = function(assert) {
+ // Test case for bug 827792
+ let com = {};
+ let loader = require('toolkit/loader');
+ let loadOptions = require('@loader/options');
+ let options = loader.override(loadOptions,
+ {globals: loader.override(loadOptions.globals,
+ {com: com,
+ console: console,
+ dump: dump})});
+ let subloader = loader.Loader(options);
+ let userRequire = loader.Require(subloader, module);
+ let userModule = userRequire("./loader/user-global");
+
+ assert.equal(userModule.getCom(), com,
+ "user module returns expected `com` global");
+};
+
+exports['test custom require caching'] = function(assert) {
+ const loader = Loader({
+ paths: { '': root + "/" },
+ requireHook: (id, require) => {
+ // Just load it normally
+ return require(id);
+ }
+ });
+ const require = Require(loader, module);
+
+ let data = require('fixtures/loader/json/mutation.json');
+ assert.equal(data.value, 1, 'has initial value');
+ data.value = 2;
+ let newdata = require('fixtures/loader/json/mutation.json');
+ assert.equal(
+ newdata.value,
+ 2,
+ 'JSON objects returned should be cached and the same instance'
+ );
+};
+
+exports['test caching when proxying a loader'] = function(assert) {
+ const parentRequire = require;
+ const loader = Loader({
+ paths: { '': root + "/" },
+ requireHook: (id, childRequire) => {
+ if(id === 'gimmejson') {
+ return childRequire('fixtures/loader/json/mutation.json')
+ }
+ // Load it with the original (global) require
+ return parentRequire(id);
+ }
+ });
+ const childRequire = Require(loader, module);
+
+ let data = childRequire('./fixtures/loader/json/mutation.json');
+ assert.equal(data.value, 1, 'data has initial value');
+ data.value = 2;
+
+ let newdata = childRequire('./fixtures/loader/json/mutation.json');
+ assert.equal(newdata.value, 2, 'data has changed');
+
+ let childData = childRequire('gimmejson');
+ assert.equal(childData.value, 1, 'data from child loader has initial value');
+ childData.value = 3;
+ let newChildData = childRequire('gimmejson');
+ assert.equal(newChildData.value, 3, 'data from child loader has changed');
+
+ data = childRequire('./fixtures/loader/json/mutation.json');
+ assert.equal(data.value, 2, 'data from parent loader has not changed');
+
+ // Set it back to the original value just in case (this instance
+ // will be shared across tests)
+ data.value = 1;
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-match-pattern.js b/addon-sdk/source/test/test-match-pattern.js
new file mode 100644
index 000000000..651ad3148
--- /dev/null
+++ b/addon-sdk/source/test/test-match-pattern.js
@@ -0,0 +1,137 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { MatchPattern } = require("sdk/util/match-pattern");
+
+exports.testMatchPatternTestTrue = function(assert) {
+ function ok(pattern, url) {
+ let mp = new MatchPattern(pattern);
+ assert.ok(mp.test(url), pattern + " should match " + url);
+ }
+
+ ok("*", "http://example.com");
+ ok("*", "https://example.com");
+ ok("*", "ftp://example.com");
+
+ ok("*.example.com", "http://example.com");
+ ok("*.example.com", "http://hamburger.example.com");
+ ok("*.example.com", "http://hotdog.hamburger.example.com");
+
+ ok("http://example.com*", "http://example.com");
+ ok("http://example.com*", "http://example.com/");
+ ok("http://example.com/*", "http://example.com/");
+ ok("http://example.com/*", "http://example.com/potato-salad");
+ ok("http://example.com/pickles/*", "http://example.com/pickles/");
+ ok("http://example.com/pickles/*", "http://example.com/pickles/lemonade");
+
+ ok("http://example.com", "http://example.com");
+ ok("http://example.com/ice-cream", "http://example.com/ice-cream");
+
+ ok(/.*zilla.*/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753");
+ ok(/.*A.*/i, "http://A.com");
+ ok(/.*A.*/i, "http://a.com");
+ ok(/https:.*zilla.*/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753");
+ ok('*.sample.com', 'http://ex.sample.com/foo.html');
+ ok('*.amp.le.com', 'http://ex.amp.le.com');
+
+ ok('data:*', 'data:text/html;charset=utf-8,');
+};
+
+exports.testMatchPatternTestFalse = function(assert) {
+ function ok(pattern, url) {
+ let mp = new MatchPattern(pattern);
+ assert.ok(!mp.test(url), pattern + " should not match " + url);
+ }
+
+ ok("*", null);
+ ok("*", "");
+ ok("*", "bogus");
+ ok("*", "chrome://browser/content/browser.xul");
+ ok("*", "nttp://example.com");
+
+ ok("*.example.com", null);
+ ok("*.example.com", "");
+ ok("*.example.com", "bogus");
+ ok("*.example.com", "http://example.net");
+ ok("*.example.com", "http://foo.com");
+ ok("*.example.com", "http://example.com.foo");
+ ok("*.example2.com", "http://example.com");
+
+ ok("http://example.com/*", null);
+ ok("http://example.com/*", "");
+ ok("http://example.com/*", "bogus");
+ ok("http://example.com/*", "http://example.com");
+ ok("http://example.com/*", "http://foo.com/");
+
+ ok("http://example.com", null);
+ ok("http://example.com", "");
+ ok("http://example.com", "bogus");
+ ok("http://example.com", "http://example.com/");
+
+ ok(/zilla.*/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753");
+ ok(/.*zilla/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753");
+ ok(/.*Zilla.*/, "https://bugzilla.redhat.com/show_bug.cgi?id=655464"); // bug 655464
+ ok(/https:.*zilla/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753");
+
+ // bug 856913
+ ok('*.ign.com', 'http://www.design.com');
+ ok('*.ign.com', 'http://design.com');
+ ok('*.zilla.com', 'http://bugzilla.mozilla.com');
+ ok('*.zilla.com', 'http://mo-zilla.com');
+ ok('*.amp.le.com', 'http://amp-le.com');
+ ok('*.amp.le.com', 'http://examp.le.com');
+};
+
+exports.testMatchPatternErrors = function(assert) {
+ assert.throws(
+ () => new MatchPattern("*.google.com/*"),
+ /There can be at most one/,
+ "MatchPattern throws when supplied multiple '*'"
+ );
+
+ assert.throws(
+ () => new MatchPattern("google.com"),
+ /expected to be either an exact URL/,
+ "MatchPattern throws when the wildcard doesn't use '*' and doesn't " +
+ "look like a URL"
+ );
+
+ assert.throws(
+ () => new MatchPattern("http://google*.com"),
+ /expected to be the first or the last/,
+ "MatchPattern throws when a '*' is in the middle of the wildcard"
+ );
+
+ assert.throws(
+ () => new MatchPattern(/ /g),
+ /^A RegExp match pattern cannot be set to `global` \(i\.e\. \/\/g\)\.$/,
+ "MatchPattern throws on a RegExp set to `global` (i.e. //g)."
+ );
+
+ assert.throws(
+ () => new MatchPattern( / /m ),
+ /^A RegExp match pattern cannot be set to `multiline` \(i\.e\. \/\/m\)\.$/,
+ "MatchPattern throws on a RegExp set to `multiline` (i.e. //m)."
+ );
+};
+
+exports.testMatchPatternInternals = function(assert) {
+ assert.equal(
+ new MatchPattern("http://google.com/test").exactURL,
+ "http://google.com/test"
+ );
+
+ assert.equal(
+ new MatchPattern("http://google.com/test/*").urlPrefix,
+ "http://google.com/test/"
+ );
+
+ assert.equal(
+ new MatchPattern("*.example.com").domain,
+ "example.com"
+ );
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-method.js b/addon-sdk/source/test/test-method.js
new file mode 100644
index 000000000..0478593c1
--- /dev/null
+++ b/addon-sdk/source/test/test-method.js
@@ -0,0 +1,7 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+module.exports = require("method/test/common");
diff --git a/addon-sdk/source/test/test-module.js b/addon-sdk/source/test/test-module.js
new file mode 100644
index 000000000..1f9979e4b
--- /dev/null
+++ b/addon-sdk/source/test/test-module.js
@@ -0,0 +1,36 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+/** Disabled because of Bug 672199
+exports["test module exports are frozen"] = function(assert) {
+ assert.ok(Object.isFrozen(require("sdk/hotkeys")),
+ "module exports are frozen");
+};
+
+exports["test redefine exported property"] = function(assert) {
+ let hotkeys = require("sdk/hotkeys");
+ let { Hotkey } = hotkeys;
+ try { Object.defineProperty(hotkeys, 'Hotkey', { value: {} }); } catch(e) {}
+ assert.equal(hotkeys.Hotkey, Hotkey, "exports can't be redefined");
+};
+*/
+
+exports["test can't delete exported property"] = function(assert) {
+ let hotkeys = require("sdk/hotkeys");
+ let { Hotkey } = hotkeys;
+
+ try { delete hotkeys.Hotkey; } catch(e) {}
+ assert.equal(hotkeys.Hotkey, Hotkey, "exports can't be deleted");
+};
+
+exports["test can't override exported property"] = function(assert) {
+ let hotkeys = require("sdk/hotkeys");
+ let { Hotkey } = hotkeys;
+
+ try { hotkeys.Hotkey = Object } catch(e) {}
+ assert.equal(hotkeys.Hotkey, Hotkey, "exports can't be overriden");
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-modules.js b/addon-sdk/source/test/test-modules.js
new file mode 100644
index 000000000..ee9d3d9b5
--- /dev/null
+++ b/addon-sdk/source/test/test-modules.js
@@ -0,0 +1,150 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+exports.testDefine = function(assert) {
+ let tiger = require('./modules/tiger');
+ assert.equal(tiger.name, 'tiger', 'name proprety was exported properly');
+ assert.equal(tiger.type, 'cat', 'property form other module exported');
+};
+
+exports.testDefineInoresNonFactory = function(assert) {
+ let mod = require('./modules/async2');
+ assert.equal(mod.name, 'async2', 'name proprety was exported properly');
+ assert.ok(mod.traditional2Name !== 'traditional2', '1st is ignored');
+};
+/* Disable test that require AMD specific functionality:
+
+// define() that exports a function as the module value,
+// specifying a module name.
+exports.testDefExport = function(assert) {
+ var add = require('modules/add');
+ assert.equal(add(1, 1), 2, 'Named define() exporting a function');
+};
+
+// define() that exports function as a value, but is anonymous
+exports.testAnonDefExport = function (assert) {
+ var subtract = require('modules/subtract');
+ assert.equal(subtract(4, 2), 2,
+ 'Anonymous define() exporting a function');
+}
+
+// using require([], function () {}) to load modules.
+exports.testSimpleRequire = function (assert) {
+ require(['modules/blue', 'modules/orange'], function (blue, orange) {
+ assert.equal(blue.name, 'blue', 'Simple require for blue');
+ assert.equal(orange.name, 'orange', 'Simple require for orange');
+ assert.equal(orange.parentType, 'color',
+ 'Simple require dependency check for orange');
+ });
+}
+
+// using nested require([]) calls.
+exports.testSimpleRequireNested = function (assert) {
+ require(['modules/blue', 'modules/orange', 'modules/green'],
+ function (blue, orange, green) {
+
+ require(['modules/orange', 'modules/red'], function (orange, red) {
+ assert.equal(red.name, 'red', 'Simple require for red');
+ assert.equal(red.parentType, 'color',
+ 'Simple require dependency check for red');
+ assert.equal(blue.name, 'blue', 'Simple require for blue');
+ assert.equal(orange.name, 'orange', 'Simple require for orange');
+ assert.equal(orange.parentType, 'color',
+ 'Simple require dependency check for orange');
+ assert.equal(green.name, 'green', 'Simple require for green');
+ assert.equal(green.parentType, 'color',
+ 'Simple require dependency check for green');
+ });
+
+ });
+}
+
+// requiring a traditional module, that uses async, that use traditional and
+// async, with a circular reference
+exports.testMixedCircular = function (assert) {
+ var t = require('modules/traditional1');
+ assert.equal(t.name, 'traditional1', 'Testing name');
+ assert.equal(t.traditional2Name, 'traditional2',
+ 'Testing dependent name');
+ assert.equal(t.traditional1Name, 'traditional1', 'Testing circular name');
+ assert.equal(t.async2Name, 'async2', 'Testing async2 name');
+ assert.equal(t.async2Traditional2Name, 'traditional2',
+ 'Testing nested traditional2 name');
+}
+
+// Testing define()(function(require) {}) with some that use exports,
+// some that use return.
+exports.testAnonExportsReturn = function (assert) {
+ var lion = require('modules/lion');
+ require(['modules/tiger', 'modules/cheetah'], function (tiger, cheetah) {
+ assert.equal('lion', lion, 'Check lion name');
+ assert.equal('tiger', tiger.name, 'Check tiger name');
+ assert.equal('cat', tiger.type, 'Check tiger type');
+ assert.equal('cheetah', cheetah(), 'Check cheetah name');
+ });
+}
+
+// circular dependency
+exports.testCircular = function (assert) {
+ var pollux = require('modules/pollux'),
+ castor = require('modules/castor');
+
+ assert.equal(pollux.name, 'pollux', 'Pollux\'s name');
+ assert.equal(pollux.getCastorName(),
+ 'castor', 'Castor\'s name from Pollux.');
+ assert.equal(castor.name, 'castor', 'Castor\'s name');
+ assert.equal(castor.getPolluxName(), 'pollux',
+ 'Pollux\'s name from Castor.');
+}
+
+// test a bad module that asks for exports but also does a define() return
+exports.testBadExportAndReturn = function (assert) {
+ var passed = false;
+ try {
+ var bad = require('modules/badExportAndReturn');
+ } catch(e) {
+ passed = /cannot use exports and also return/.test(e.toString());
+ }
+ assert.equal(passed, true, 'Make sure exports and return fail');
+}
+
+// test a bad circular dependency, where an exported value is needed, but
+// the return value happens too late, a module already asked for the exported
+// value.
+exports.testBadExportAndReturnCircular = function (assert) {
+ var passed = false;
+ try {
+ var bad = require('modules/badFirst');
+ } catch(e) {
+ passed = /after another module has referenced its exported value/
+ .test(e.toString());
+ }
+ assert.equal(passed, true, 'Make sure return after an exported ' +
+ 'value is grabbed by another module fails.');
+}
+
+// only allow one define call per file.
+exports.testOneDefine = function (assert) {
+ var passed = false;
+ try {
+ var dupe = require('modules/dupe');
+ } catch(e) {
+ passed = /Only one call to define/.test(e.toString());
+ }
+ assert.equal(passed, true, 'Only allow one define call per module');
+}
+
+// only allow one define call per file, testing a bad nested define call.
+exports.testOneDefineNested = function (assert) {
+ var passed = false;
+ try {
+ var dupe = require('modules/dupeNested');
+ } catch(e) {
+ passed = /Only one call to define/.test(e.toString());
+ }
+ assert.equal(passed, true, 'Only allow one define call per module');
+}
+*/
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-mozilla-toolkit-versioning.js b/addon-sdk/source/test/test-mozilla-toolkit-versioning.js
new file mode 100644
index 000000000..148919595
--- /dev/null
+++ b/addon-sdk/source/test/test-mozilla-toolkit-versioning.js
@@ -0,0 +1,59 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const {parse, increment} = require("mozilla-toolkit-versioning/index")
+
+const TestParse = assert => (version, min, max) => {
+ const actual = parse(version);
+ assert.equal(actual.min, min);
+ assert.equal(actual.max, max);
+}
+
+const TestInc = assert => (version, expected) => {
+ assert.equal(increment(version), expected,
+ `increment: ${version} should be equal ${expected}`)
+}
+
+
+
+exports['test parse(version) single value'] = assert => {
+ const testParse = TestParse(assert)
+ testParse('1.2.3', '1.2.3', '1.2.3');
+ testParse('>=1.2.3', '1.2.3', undefined);
+ testParse('<=1.2.3', undefined, '1.2.3');
+ testParse('>1.2.3', '1.2.3.1', undefined);
+ testParse('<1.2.3', undefined, '1.2.3.-1');
+ testParse('*', undefined, undefined);
+};
+
+exports['test parse(version) range'] = assert => {
+ const testParse = TestParse(assert);
+ testParse('>=1.2.3 <=2.3.4', '1.2.3', '2.3.4');
+ testParse('>1.2.3 <=2.3.4', '1.2.3.1', '2.3.4');
+ testParse('>=1.2.3 <2.3.4', '1.2.3', '2.3.4.-1');
+ testParse('>1.2.3 <2.3.4', '1.2.3.1', '2.3.4.-1');
+
+ testParse('<=2.3.4 >=1.2.3', '1.2.3', '2.3.4');
+ testParse('<=2.3.4 >1.2.3', '1.2.3.1', '2.3.4');
+ testParse('<2.3.4 >=1.2.3', '1.2.3', '2.3.4.-1');
+ testParse('<2.3.4 >1.2.3', '1.2.3.1', '2.3.4.-1');
+
+ testParse('1.2.3pre1 - 2.3.4', '1.2.3pre1', '2.3.4');
+};
+
+exports['test increment(version)'] = assert => {
+ const testInc = TestInc(assert);
+
+ testInc('1.2.3', '1.2.3.1');
+ testInc('1.2.3a', '1.2.3a1');
+ testInc('1.2.3pre', '1.2.3pre1');
+ testInc('1.2.3pre1', '1.2.3pre2');
+ testInc('1.2', '1.2.1');
+ testInc('1.2pre1a', '1.2pre1b');
+ testInc('1.2pre1pre', '1.2pre1prf');
+};
+
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-mpl2-license-header.js b/addon-sdk/source/test/test-mpl2-license-header.js
new file mode 100644
index 000000000..22a2cf0ea
--- /dev/null
+++ b/addon-sdk/source/test/test-mpl2-license-header.js
@@ -0,0 +1,105 @@
+// Note: This line is here intentionally, to break MPL2_LICENSE_TEST
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Cc, Ci, Cu } = require("chrome");
+const options = require('@loader/options');
+const { id } = require("sdk/self");
+const { getAddonByID } = require("sdk/addon/manager");
+const { mapcat, map, filter, fromEnumerator } = require("sdk/util/sequence");
+const { readURISync } = require('sdk/net/url');
+const { Request } = require('sdk/request');
+const { defer } = require("sdk/core/promise");
+
+const ios = Cc['@mozilla.org/network/io-service;1'].
+ getService(Ci.nsIIOService);
+
+const MIT_LICENSE_HEADER = [];
+
+const MPL2_LICENSE_TEST = new RegExp([
+ "^\\/\\* This Source Code Form is subject to the terms of the Mozilla Public",
+ " \\* License, v\\. 2\\.0\\. If a copy of the MPL was not distributed with this",
+ " \\* file, You can obtain one at http:\\/\\/mozilla\\.org\\/MPL\\/2\\.0\\/\\. \\*\\/"
+].join("\n"));
+
+// Note: Using regular expressions because the paths a different for cfx vs jpm
+const IGNORES = [
+ /lib[\/\\](diffpatcher|method)[\/\\].+$/, // MIT
+ /lib[\/\\]sdk[\/\\]fs[\/\\]path\.js$/, // MIT
+ /lib[\/\\]sdk[\/\\]system[\/\\]child_process[\/\\].*/,
+ /tests?[\/\\]buffers[\/\\].+$/, // MIT
+ /tests?[\/\\]path[\/\\]test-path\.js$/,
+ /tests?[\/\\]querystring[\/\\]test-querystring\.js$/,
+];
+
+const ignoreFile = file => !!IGNORES.find(regex => regex.test(file));
+
+const baseURI = "resource://test-sdk-addon/";
+
+const uri = (path="") => baseURI + path;
+
+const toFile = x => x.QueryInterface(Ci.nsIFile);
+const isTestFile = ({ path, leafName }) => {
+ return !ignoreFile(path) && /\.jsm?$/.test(leafName)
+};
+const getFileURI = x => ios.newFileURI(x).spec;
+
+const getDirectoryEntries = file => map(toFile, fromEnumerator(_ => file.directoryEntries));
+
+const isDirectory = x => x.isDirectory();
+const getEntries = directory => mapcat(entry => {
+ if (isDirectory(entry)) {
+ return getEntries(entry);
+ }
+ else if (isTestFile(entry)) {
+ return [ entry ];
+ }
+ return [];
+}, filter(() => true, getDirectoryEntries(directory)));
+
+function readURL(url) {
+ let { promise, resolve } = defer();
+
+ Request({
+ url: url,
+ overrideMimeType: "text/plain",
+ onComplete: (response) => resolve(response.text)
+ }).get();
+
+ return promise;
+}
+
+exports["test MPL2 license header"] = function*(assert) {
+ let addon = yield getAddonByID(id);
+ let xpiURI = addon.getResourceURI();
+ let rootURL = xpiURI.spec;
+ assert.ok(rootURL, rootURL);
+ let files = [...getEntries(xpiURI.QueryInterface(Ci.nsIFileURL).file)];
+
+ assert.ok(files.length > 1, files.length + " files found.");
+ let failures = [];
+ let success = 0;
+
+ for (let i = 0, len = files.length; i < len; i++) {
+ let file = files[i];
+ assert.ok(file.path, "Trying " + file.path);
+
+ const URI = ios.newFileURI(file);
+
+ let leafName = URI.spec.replace(rootURL, "");
+
+ let contents = yield readURL(URI.spec);
+ if (!MPL2_LICENSE_TEST.test(contents)) {
+ failures.push(leafName);
+ }
+ }
+
+ assert.equal(1, failures.length, "we expect one failure");
+ assert.ok(/test-mpl2-license-header\.js$/.test(failures[0]), "the only failure is this file");
+ failures.shift();
+ assert.equal("", failures.join(",\n"), failures.length + " files found missing the required mpl 2 header");
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-namespace.js b/addon-sdk/source/test/test-namespace.js
new file mode 100644
index 000000000..636682e9e
--- /dev/null
+++ b/addon-sdk/source/test/test-namespace.js
@@ -0,0 +1,120 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { ns } = require("sdk/core/namespace");
+const { Cc, Ci, Cu } = require("chrome");
+const { setTimeout } = require("sdk/timers")
+
+exports["test post GC references"] = function (assert, done) {
+ var target = {}, local = ns()
+ local(target).there = true
+
+ assert.equal(local(target).there, true, "namespaced preserved");
+
+ Cu.schedulePreciseGC(function() {
+ assert.equal(local(target).there, true, "namespace is preserved post GC");
+ done();
+ });
+};
+
+exports["test namsepace basics"] = function(assert) {
+ var privates = ns();
+ var object = { foo: function foo() { return "hello foo"; } };
+
+ assert.notEqual(privates(object), object,
+ "namespaced object is not the same");
+ assert.ok(!('foo' in privates(object)),
+ "public properties are not in the namespace");
+
+ assert.equal(privates(object), privates(object),
+ "same namespaced object is returned on each call");
+};
+
+exports["test namespace overlays"] = function(assert) {
+ var _ = ns();
+ var object = { foo: 'foo' };
+
+ _(object).foo = 'bar';
+
+ assert.equal(_(object).foo, "bar",
+ "namespaced property `foo` changed value");
+
+ assert.equal(object.foo, "foo",
+ "public property `foo` has original value");
+
+ object.foo = "baz";
+ assert.equal(_(object).foo, "bar",
+ "property changes do not affect namespaced properties");
+
+ object.bar = "foo";
+ assert.ok(!("bar" in _(object)),
+ "new public properties are not reflected in namespace");
+};
+
+exports["test shared namespaces"] = function(assert) {
+ var _ = ns();
+
+ var f1 = { hello: 1 };
+ var f2 = { foo: 'foo', hello: 2 };
+ _(f1).foo = _(f2).foo = 'bar';
+
+ assert.equal(_(f1).hello, _(f2).hello, "namespace can be shared");
+ assert.notEqual(f1.hello, _(f1).hello, "shared namespace can overlay");
+ assert.notEqual(f2.hello, _(f2).hello, "target is not affected");
+
+ _(f1).hello = 3;
+
+ assert.notEqual(_(f1).hello, _(f2).hello,
+ "namespaced property can be overided");
+ assert.equal(_(f2).hello, _({}).hello, "namespace does not change");
+};
+
+exports["test multi namespace"] = function(assert) {
+ var n1 = ns();
+ var n2 = ns();
+ var object = { baz: 1 };
+ n1(object).foo = 1;
+ n2(object).foo = 2;
+ n1(object).bar = n2(object).bar = 3;
+
+ assert.notEqual(n1(object).foo, n2(object).foo,
+ "object can have multiple namespaces");
+ assert.equal(n1(object).bar, n2(object).bar,
+ "object can have matching props in diff namespaces");
+};
+
+exports["test ns alias"] = function(assert) {
+ assert.strictEqual(ns, require('sdk/core/namespace').Namespace,
+ "ns is an alias of Namespace");
+};
+
+exports["test ns inheritance"] = function(assert) {
+ let _ = ns();
+
+ let prototype = { level: 1 };
+ let object = Object.create(prototype);
+ let delegee = Object.create(object);
+
+ _(prototype).foo = {};
+
+ assert.ok(!Object.prototype.hasOwnProperty.call(_(delegee), "foo"),
+ "namespaced property is not copied to descendants");
+ assert.equal(_(delegee).foo, _(prototype).foo,
+ "namespaced properties are inherited by descendants");
+
+ _(object).foo = {};
+ assert.notEqual(_(object).foo, _(prototype).foo,
+ "namespaced properties may be shadowed");
+ assert.equal(_(object).foo, _(delegee).foo,
+ "shadwed properties are inherited by descendants");
+
+ _(object).bar = {};
+ assert.ok(!("bar" in _(prototype)),
+ "descendants properties are not copied to ancestors");
+ assert.ok(_(object).bar, _(delegee).bar,
+ "descendants properties are inherited");
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-native-loader.js b/addon-sdk/source/test/test-native-loader.js
new file mode 100644
index 000000000..cc7185522
--- /dev/null
+++ b/addon-sdk/source/test/test-native-loader.js
@@ -0,0 +1,423 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+var {
+ Loader, main, unload, parseStack, resolve, nodeResolve
+} = require('toolkit/loader');
+var { readURI } = require('sdk/net/url');
+var { all } = require('sdk/core/promise');
+var { before, after } = require('sdk/test/utils');
+var testOptions = require('@test/options');
+
+var root = module.uri.substr(0, module.uri.lastIndexOf('/'))
+// The following adds Debugger constructor to the global namespace.
+const { Cc, Ci, Cu } = require('chrome');
+const { addDebuggerToGlobal } = Cu.import('resource://gre/modules/jsdebugger.jsm', {});
+addDebuggerToGlobal(this);
+
+const { NetUtil } = Cu.import('resource://gre/modules/NetUtil.jsm', {});
+
+const resProto = Cc["@mozilla.org/network/protocol;1?name=resource"]
+ .getService(Ci.nsIResProtocolHandler);
+
+const fileRoot = resProto.resolveURI(NetUtil.newURI(root));
+
+let variants = [
+ {
+ description: "unpacked resource:",
+ getRootURI(fixture) {
+ return `${root}/fixtures/${fixture}/`;
+ },
+ },
+ {
+ description: "unpacked file:",
+ getRootURI(fixture) {
+ return `${fileRoot}/fixtures/${fixture}/`;
+ },
+ },
+ {
+ description: "packed resource:",
+ getRootURI(fixture) {
+ return `resource://${fixture}/`;
+ },
+ },
+ {
+ description: "packed jar:",
+ getRootURI(fixture) {
+ return `jar:${fileRoot}/fixtures/${fixture}.xpi!/`;
+ },
+ },
+];
+
+let fixtures = [
+ 'native-addon-test',
+ 'native-overrides-test',
+];
+
+for (let variant of variants) {
+ exports[`test nodeResolve (${variant.description})`] = function (assert) {
+ let rootURI = variant.getRootURI('native-addon-test');
+ let manifest = {};
+ manifest.dependencies = {};
+
+ // Handles extensions
+ resolveTest('../package.json', './dir/c.js', './package.json');
+ resolveTest('../dir/b.js', './dir/c.js', './dir/b.js');
+
+ resolveTest('./dir/b', './index.js', './dir/b.js');
+ resolveTest('../index', './dir/b.js', './index.js');
+ resolveTest('../', './dir/b.js', './index.js');
+ resolveTest('./dir/a', './index.js', './dir/a.js', 'Precedence dir/a.js over dir/a/');
+ resolveTest('../utils', './dir/a.js', './utils/index.js', 'Requiring a directory defaults to dir/index.js');
+ resolveTest('../newmodule', './dir/c.js', './newmodule/lib/file.js', 'Uses package.json main in dir to load appropriate "main"');
+ resolveTest('test-math', './utils/index.js', './node_modules/test-math/index.js',
+ 'Dependencies default to their index.js');
+ resolveTest('test-custom-main', './utils/index.js', './node_modules/test-custom-main/lib/custom-entry.js',
+ 'Dependencies use "main" entry');
+ resolveTest('test-math/lib/sqrt', './utils/index.js', './node_modules/test-math/lib/sqrt.js',
+ 'Dependencies\' files can be consumed via "/"');
+
+ resolveTest('sdk/tabs/utils', './index.js', undefined,
+ 'correctly ignores SDK references in paths');
+ resolveTest('fs', './index.js', undefined,
+ 'correctly ignores built in node modules in paths');
+
+ resolveTest('test-add', './node_modules/test-math/index.js',
+ './node_modules/test-math/node_modules/test-add/index.js',
+ 'Dependencies\' dependencies can be found');
+
+ resolveTest('resource://gre/modules/commonjs/sdk/tabs.js', './index.js', undefined,
+ 'correctly ignores absolute URIs.');
+
+ resolveTest('../tabs', 'resource://gre/modules/commonjs/sdk/addon/bootstrap.js', undefined,
+ 'correctly ignores attempts to resolve from a module at an absolute URI.');
+
+ resolveTest('sdk/tabs', 'resource://gre/modules/commonjs/sdk/addon/bootstrap.js', undefined,
+ 'correctly ignores attempts to resolve from a module at an absolute URI.');
+
+ function resolveTest (id, requirer, expected, msg) {
+ let result = nodeResolve(id, requirer, { manifest: manifest, rootURI: rootURI });
+ assert.equal(result, expected, 'nodeResolve ' + id + ' from ' + requirer + ' ' +msg);
+ }
+ }
+
+ /*
+ // TODO not working in current env
+ exports[`test bundle (${variant.description`] = function (assert, done) {
+ loadAddon('/native-addons/native-addon-test/')
+ };
+ */
+
+ exports[`test native Loader with mappings (${variant.description})`] = function (assert, done) {
+ all([
+ getJSON('/fixtures/native-addon-test/expectedmap.json'),
+ getJSON('/fixtures/native-addon-test/package.json')
+ ]).then(([expectedMap, manifest]) => {
+
+ // Override dummy module and point it to `test-math` to see if the
+ // require is pulling from the mapping
+ expectedMap['./index.js']['./dir/dummy'] = './dir/a.js';
+
+ let rootURI = variant.getRootURI('native-addon-test');
+ let loader = Loader({
+ paths: makePaths(rootURI),
+ rootURI: rootURI,
+ manifest: manifest,
+ requireMap: expectedMap,
+ isNative: true
+ });
+
+ let program = main(loader);
+ assert.equal(program.dummyModule, 'dir/a',
+ 'The lookup uses the information given in the mapping');
+
+ testLoader(program, assert);
+ unload(loader);
+ done();
+ }).then(null, (reason) => console.error(reason));
+ };
+
+ exports[`test native Loader overrides (${variant.description})`] = function*(assert) {
+ const expectedKeys = Object.keys(require("sdk/io/fs")).join(", ");
+ const manifest = yield getJSON('/fixtures/native-overrides-test/package.json');
+ const rootURI = variant.getRootURI('native-overrides-test');
+
+ let loader = Loader({
+ paths: makePaths(rootURI),
+ rootURI: rootURI,
+ manifest: manifest,
+ metadata: manifest,
+ isNative: true
+ });
+
+ let program = main(loader);
+ let fooKeys = Object.keys(program.foo).join(", ");
+ let barKeys = Object.keys(program.foo).join(", ");
+ let fsKeys = Object.keys(program.fs).join(", ");
+ let overloadKeys = Object.keys(program.overload.fs).join(", ");
+ let overloadLibKeys = Object.keys(program.overloadLib.fs).join(", ");
+
+ assert.equal(fooKeys, expectedKeys, "foo exports sdk/io/fs");
+ assert.equal(barKeys, expectedKeys, "bar exports sdk/io/fs");
+ assert.equal(fsKeys, expectedKeys, "sdk/io/fs exports sdk/io/fs");
+ assert.equal(overloadKeys, expectedKeys, "overload exports foo which exports sdk/io/fs");
+ assert.equal(overloadLibKeys, expectedKeys, "overload/lib/foo exports foo/lib/foo");
+ assert.equal(program.internal, "test", "internal exports ./lib/internal");
+ assert.equal(program.extra, true, "fs-extra was exported properly");
+
+ assert.equal(program.Tabs, "no tabs exist", "sdk/tabs exports ./lib/tabs from the add-on");
+ assert.equal(program.CoolTabs, "no tabs exist", "sdk/tabs exports ./lib/tabs from the node_modules");
+ assert.equal(program.CoolTabsLib, "a cool tabs implementation", "./lib/tabs true relative path from the node_modules");
+
+ assert.equal(program.ignore, "do not ignore this export", "../ignore override was ignored.");
+
+ unload(loader);
+ };
+
+ exports[`test invalid native Loader overrides cause no errors (${variant.description})`] = function*(assert) {
+ const manifest = yield getJSON('/fixtures/native-overrides-test/package.json');
+ const rootURI = variant.getRootURI('native-overrides-test');
+ const EXPECTED = JSON.stringify({});
+
+ let makeLoader = (rootURI, manifest) => Loader({
+ paths: makePaths(rootURI),
+ rootURI: rootURI,
+ manifest: manifest,
+ metadata: manifest,
+ isNative: true
+ });
+
+ manifest.jetpack.overrides = "string";
+ let loader = makeLoader(rootURI, manifest);
+ assert.equal(JSON.stringify(loader.manifest.jetpack.overrides), EXPECTED,
+ "setting jetpack.overrides to a string caused no errors making the loader");
+ unload(loader);
+
+ manifest.jetpack.overrides = true;
+ loader = makeLoader(rootURI, manifest);
+ assert.equal(JSON.stringify(loader.manifest.jetpack.overrides), EXPECTED,
+ "setting jetpack.overrides to a boolean caused no errors making the loader");
+ unload(loader);
+
+ manifest.jetpack.overrides = 5;
+ loader = makeLoader(rootURI, manifest);
+ assert.equal(JSON.stringify(loader.manifest.jetpack.overrides), EXPECTED,
+ "setting jetpack.overrides to a number caused no errors making the loader");
+ unload(loader);
+
+ manifest.jetpack.overrides = null;
+ loader = makeLoader(rootURI, manifest);
+ assert.equal(JSON.stringify(loader.manifest.jetpack.overrides), EXPECTED,
+ "setting jetpack.overrides to null caused no errors making the loader");
+ unload(loader);
+ };
+
+ exports[`test invalid native Loader jetpack key cause no errors (${variant.description})`] = function*(assert) {
+ const manifest = yield getJSON('/fixtures/native-overrides-test/package.json');
+ const rootURI = variant.getRootURI('native-overrides-test');
+ const EXPECTED = JSON.stringify({});
+
+ let makeLoader = (rootURI, manifest) => Loader({
+ paths: makePaths(rootURI),
+ rootURI: rootURI,
+ manifest: manifest,
+ metadata: manifest,
+ isNative: true
+ });
+
+ manifest.jetpack = "string";
+ let loader = makeLoader(rootURI, manifest);
+ assert.equal(JSON.stringify(loader.manifest.jetpack.overrides), EXPECTED,
+ "setting jetpack.overrides to a string caused no errors making the loader");
+ unload(loader);
+
+ manifest.jetpack = true;
+ loader = makeLoader(rootURI, manifest);
+ assert.equal(JSON.stringify(loader.manifest.jetpack.overrides), EXPECTED,
+ "setting jetpack.overrides to a boolean caused no errors making the loader");
+ unload(loader);
+
+ manifest.jetpack = 5;
+ loader = makeLoader(rootURI, manifest);
+ assert.equal(JSON.stringify(loader.manifest.jetpack.overrides), EXPECTED,
+ "setting jetpack.overrides to a number caused no errors making the loader");
+ unload(loader);
+
+ manifest.jetpack = null;
+ loader = makeLoader(rootURI, manifest);
+ assert.equal(JSON.stringify(loader.manifest.jetpack.overrides), EXPECTED,
+ "setting jetpack.overrides to null caused no errors making the loader");
+ unload(loader);
+ };
+
+ exports[`test native Loader without mappings (${variant.description})`] = function (assert, done) {
+ getJSON('/fixtures/native-addon-test/package.json').then(manifest => {
+ let rootURI = variant.getRootURI('native-addon-test');
+ let loader = Loader({
+ paths: makePaths(rootURI),
+ rootURI: rootURI,
+ manifest: manifest,
+ isNative: true
+ });
+
+ let program = main(loader);
+ testLoader(program, assert);
+ unload(loader);
+ done();
+ }).then(null, (reason) => console.error(reason));
+ };
+
+ exports[`test require#resolve with relative, dependencies (${variant.description})`] = function(assert, done) {
+ getJSON('/fixtures/native-addon-test/package.json').then(manifest => {
+ let rootURI = variant.getRootURI('native-addon-test');
+ let loader = Loader({
+ paths: makePaths(rootURI),
+ rootURI: rootURI,
+ manifest: manifest,
+ isNative: true
+ });
+
+ let program = main(loader);
+ let fixtureRoot = program.require.resolve("./").replace(/index\.js$/, "");
+
+ assert.equal(variant.getRootURI("native-addon-test"), fixtureRoot, "correct resolution root");
+ assert.equal(program.require.resolve("test-math"), fixtureRoot + "node_modules/test-math/index.js", "works with node_modules");
+ assert.equal(program.require.resolve("./newmodule"), fixtureRoot + "newmodule/lib/file.js", "works with directory mains");
+ assert.equal(program.require.resolve("./dir/a"), fixtureRoot + "dir/a.js", "works with normal relative module lookups");
+ assert.equal(program.require.resolve("modules/Promise.jsm"), "resource://gre/modules/Promise.jsm", "works with path lookups");
+
+ // TODO bug 1050422, handle loading non JS/JSM file paths
+ // assert.equal(program.require.resolve("test-assets/styles.css"), fixtureRoot + "node_modules/test-assets/styles.css",
+ // "works with different file extension lookups in dependencies");
+
+ unload(loader);
+ done();
+ }).then(null, (reason) => console.error(reason));
+ };
+}
+
+before(exports, () => {
+ for (let fixture of fixtures) {
+ let url = `jar:${root}/fixtures/${fixture}.xpi!/`;
+
+ resProto.setSubstitution(fixture, NetUtil.newURI(url));
+ }
+});
+
+after(exports, () => {
+ for (let fixture of fixtures)
+ resProto.setSubstitution(fixture, null);
+});
+
+exports['test JSM loading'] = function (assert, done) {
+ getJSON('/fixtures/jsm-package/package.json').then(manifest => {
+ let rootURI = root + '/fixtures/jsm-package/';
+ let loader = Loader({
+ paths: makePaths(rootURI),
+ rootURI: rootURI,
+ manifest: manifest,
+ isNative: true
+ });
+
+ let program = main(loader);
+ assert.ok(program.localJSMCached, 'local relative JSMs are cached');
+ assert.ok(program.isCachedJSAbsolute , 'absolute resource:// js are cached');
+ assert.ok(program.isCachedPath, 'JSMs resolved in paths are cached');
+ assert.ok(program.isCachedAbsolute, 'absolute resource:// JSMs are cached');
+
+ assert.ok(program.localJSM, 'able to load local relative JSMs');
+ all([
+ program.isLoadedPath(10),
+ program.isLoadedAbsolute(20),
+ program.isLoadedJSAbsolute(30)
+ ]).then(([path, absolute, jsabsolute]) => {
+ assert.equal(path, 10, 'JSM files resolved from path work');
+ assert.equal(absolute, 20, 'JSM files resolved from full resource:// work');
+ assert.equal(jsabsolute, 30, 'JS files resolved from full resource:// work');
+ }).then(done, console.error);
+
+ }).then(null, console.error);
+};
+
+function testLoader (program, assert) {
+ // Test 'main' entries
+ // no relative custom main `lib/index.js`
+ assert.equal(program.customMainModule, 'custom entry file',
+ 'a node_module dependency correctly uses its `main` entry in manifest');
+ // relative custom main `./lib/index.js`
+ assert.equal(program.customMainModuleRelative, 'custom entry file relative',
+ 'a node_module dependency correctly uses its `main` entry in manifest with relative ./');
+ // implicit './index.js'
+ assert.equal(program.defaultMain, 'default main',
+ 'a node_module dependency correctly defautls to index.js for main');
+
+ // Test directory exports
+ assert.equal(program.directoryDefaults, 'utils',
+ '`require`ing a directory defaults to dir/index.js');
+ assert.equal(program.directoryMain, 'main from new module',
+ '`require`ing a directory correctly loads the `main` entry and not index.js');
+ assert.equal(program.resolvesJSoverDir, 'dir/a',
+ '`require`ing "a" resolves "a.js" over "a/index.js"');
+
+ // Test dependency's dependencies
+ assert.ok(program.math.add,
+ 'correctly defaults to index.js of a module');
+ assert.equal(program.math.add(10, 5), 15,
+ 'node dependencies correctly include their own dependencies');
+ assert.equal(program.math.subtract(10, 5), 5,
+ 'node dependencies correctly include their own dependencies');
+ assert.equal(program.mathInRelative.subtract(10, 5), 5,
+ 'relative modules can also include node dependencies');
+
+ // Test SDK natives
+ assert.ok(program.promise.defer, 'main entry can include SDK modules with no deps');
+ assert.ok(program.promise.resolve, 'main entry can include SDK modules with no deps');
+ assert.ok(program.eventCore.on, 'main entry can include SDK modules that have dependencies');
+ assert.ok(program.eventCore.off, 'main entry can include SDK modules that have dependencies');
+
+ // Test JSMs
+ assert.ok(program.promisejsm.defer, 'can require JSM files in path');
+ assert.equal(program.localJSM.test, 'this is a jsm',
+ 'can require relative JSM files');
+
+ // Other tests
+ assert.equal(program.areModulesCached, true,
+ 'modules are correctly cached');
+ assert.equal(program.testJSON.dependencies['test-math'], '*',
+ 'correctly requires JSON files');
+}
+
+function getJSON (uri) {
+ return readURI(root + uri).then(manifest => JSON.parse(manifest));
+}
+
+function makePaths (uri) {
+ // Uses development SDK modules if overloaded in loader
+ let sdkPaths = testOptions.paths ? testOptions.paths[''] : 'resource://gre/modules/commonjs/';
+ return {
+ './': uri,
+ 'sdk/': sdkPaths + 'sdk/',
+ 'toolkit/': sdkPaths + 'toolkit/',
+ 'modules/': 'resource://gre/modules/'
+ };
+}
+
+function loadAddon (uri, map) {
+ let rootURI = root + uri;
+ getJSON(uri + '/package.json').then(manifest => {
+ let loader = Loader({
+ paths: makePaths(rootURI),
+ rootURI: rootURI,
+ manifest: manifest,
+ isNative: true,
+ modules: {
+ '@test/options': testOptions
+ }
+ });
+ let program = main(loader);
+ }).then(null, console.error);
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-native-options.js b/addon-sdk/source/test/test-native-options.js
new file mode 100644
index 000000000..84212757a
--- /dev/null
+++ b/addon-sdk/source/test/test-native-options.js
@@ -0,0 +1,183 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { setDefaults, injectOptions: inject, validate } = require('sdk/preferences/native-options');
+const { activeBrowserWindow: { document } } = require("sdk/deprecated/window-utils");
+const { preferencesBranch, id } = require('sdk/self');
+const { get } = require('sdk/preferences/service');
+const { setTimeout } = require('sdk/timers');
+const simple = require('sdk/simple-prefs');
+const fixtures = require('./fixtures');
+const { Cc, Ci } = require('chrome');
+
+const prefsrv = Cc['@mozilla.org/preferences-service;1'].
+ getService(Ci.nsIPrefService);
+
+function injectOptions(preferences, preferencesBranch, document, parent) {
+ inject({
+ id: id,
+ preferences: preferences,
+ preferencesBranch: preferencesBranch,
+ document: document,
+ parent: parent
+ });
+}
+
+exports.testValidate = function(assert) {
+ let { preferences } = packageJSON('simple-prefs');
+
+ let block = () => validate(preferences);
+
+ delete preferences[3].options[0].value;
+ assert.throws(block, /option requires both a value/, "option missing value error");
+
+ delete preferences[2].options;
+ assert.throws(block, /'test3' pref requires options/, "menulist missing options error");
+
+ preferences[1].type = 'control';
+ assert.throws(block, /'test2' control requires a label/, "control missing label error");
+
+ preferences[1].type = 'nonvalid';
+ assert.throws(block, /'test2' pref must be of valid type/, "invalid pref type error");
+
+ delete preferences[0].title;
+ assert.throws(block, /'test' pref requires a title/, "pref missing title error");
+}
+
+exports.testNoPrefs = function(assert, done) {
+ let { preferences } = packageJSON('no-prefs');
+
+ let parent = document.createDocumentFragment();
+ injectOptions(preferences || [], preferencesBranch, document, parent);
+ assert.equal(parent.children.length, 0, "No setting elements injected");
+
+ // must test with events because we can't reset default prefs
+ function onPrefChange(name) {
+ assert.fail("No preferences should be defined");
+ }
+
+ simple.on('', onPrefChange);
+ setDefaults(preferences || [], preferencesBranch);
+ setTimeout(function() {
+ assert.pass("No preferences were defined");
+ simple.off('', onPrefChange);
+ done();
+ }, 100);
+}
+
+exports.testCurlyID = function(assert) {
+ let { preferences, id } = packageJSON('curly-id');
+ let branch = prefsrv.getDefaultBranch('extensions.' + id);
+
+ let parent = document.createDocumentFragment();
+ injectOptions(preferences, id, document, parent);
+ assert.equal(parent.children.length, 1, "One setting elements injected");
+ assert.equal(parent.firstElementChild.attributes.pref.value,
+ "extensions.{34a1eae1-c20a-464f-9b0e-000000000000}.test13",
+ "Setting pref attribute is set properly");
+
+ setDefaults(preferences, id);
+ assert.equal(get('extensions.{34a1eae1-c20a-464f-9b0e-000000000000}.test13'),
+ 26, "test13 is 26");
+
+ branch.deleteBranch('');
+ assert.equal(get('extensions.{34a1eae1-c20a-464f-9b0e-000000000000}.test13'),
+ undefined, "test13 is undefined");
+}
+
+exports.testPreferencesBranch = function(assert) {
+ let { preferences, 'preferences-branch': prefsBranch } = packageJSON('preferences-branch');
+ let branch = prefsrv.getDefaultBranch('extensions.' + prefsBranch);
+
+ let parent = document.createDocumentFragment();
+ injectOptions(preferences, prefsBranch, document, parent);
+ assert.equal(parent.children.length, 1, "One setting elements injected");
+ assert.equal(parent.firstElementChild.attributes.pref.value,
+ "extensions.human-readable.test42",
+ "Setting pref attribute is set properly");
+
+ setDefaults(preferences, prefsBranch);
+ assert.equal(get('extensions.human-readable.test42'), true, "test42 is true");
+
+ branch.deleteBranch('');
+ assert.equal(get('extensions.human-readable.test42'), undefined, "test42 is undefined");
+}
+
+exports.testSimplePrefs = function(assert) {
+ let { preferences } = packageJSON('simple-prefs');
+ let branch = prefsrv.getDefaultBranch('extensions.' + preferencesBranch);
+
+ function assertPref(setting, name, type, title, description = null) {
+ assert.equal(setting.getAttribute('data-jetpack-id'), id,
+ "setting 'data-jetpack-id' attribute correct");
+ assert.equal(setting.getAttribute('pref'), 'extensions.' + id + '.' + name,
+ "setting 'pref' attribute correct");
+ assert.equal(setting.getAttribute('pref-name'), name,
+ "setting 'pref-name' attribute correct");
+ assert.equal(setting.getAttribute('type'), type,
+ "setting 'type' attribute correct");
+ assert.equal(setting.getAttribute('title'), title,
+ "setting 'title' attribute correct");
+ if (description) {
+ assert.equal(setting.getAttribute('desc'), description,
+ "setting 'desc' attribute correct");
+ }
+ else {
+ assert.ok(!setting.hasAttribute('desc'),
+ "setting 'desc' attribute is not present");
+ }
+ }
+
+ function assertOption(option, value, label) {
+ assert.equal(option.getAttribute('value'), value, "value attribute correct");
+ assert.equal(option.getAttribute('label'), label, "label attribute correct");
+ }
+
+ let parent = document.createDocumentFragment();
+ injectOptions(preferences, preferencesBranch, document, parent);
+ assert.equal(parent.children.length, 8, "Eight setting elements injected");
+
+ assertPref(parent.children[0], 'test', 'bool', 't\u00EBst', 'descr\u00EFpti\u00F6n');
+ assertPref(parent.children[1], 'test2', 'string', 't\u00EBst');
+ assertPref(parent.children[2], 'test3', 'menulist', '"><test');
+ assertPref(parent.children[3], 'test4', 'radio', 't\u00EBst');
+
+ assertPref(parent.children[4], 'test5', 'boolint', 'part part, particle');
+ assertPref(parent.children[5], 'test6', 'color', 'pop pop, popscicle');
+ assertPref(parent.children[6], 'test7', 'file', 'bike bike');
+ assertPref(parent.children[7], 'test8', 'directory', 'test test');
+
+ let menuItems = parent.children[2].querySelectorAll('menupopup>menuitem');
+ let radios = parent.children[3].querySelectorAll('radiogroup>radio');
+
+ assertOption(menuItems[0], '0', 'label1');
+ assertOption(menuItems[1], '1', 'label2');
+ assertOption(radios[0], 'red', 'rouge');
+ assertOption(radios[1], 'blue', 'bleu');
+
+ setDefaults(preferences, preferencesBranch);
+ assert.strictEqual(simple.prefs.test, false, "test is false");
+ assert.strictEqual(simple.prefs.test2, "\u00FCnic\u00F8d\u00E9", "test2 is unicode");
+ assert.strictEqual(simple.prefs.test3, "1", "test3 is '1'");
+ assert.strictEqual(simple.prefs.test4, "red", "test4 is 'red'");
+
+ // Only delete the test preferences to avoid unsetting any test harness
+ // preferences.
+ for (let setting of parent.children) {
+ let name = setting.getAttribute('pref-name');
+ branch.deleteBranch("." + name);
+ }
+
+ assert.strictEqual(simple.prefs.test, undefined, "test is undefined");
+ assert.strictEqual(simple.prefs.test2, undefined, "test2 is undefined");
+ assert.strictEqual(simple.prefs.test3, undefined, "test3 is undefined");
+ assert.strictEqual(simple.prefs.test4, undefined, "test4 is undefined");
+}
+
+function packageJSON(dir) {
+ return require(fixtures.url('preferences/' + dir + '/package.json'));
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-net-url.js b/addon-sdk/source/test/test-net-url.js
new file mode 100644
index 000000000..9e463b798
--- /dev/null
+++ b/addon-sdk/source/test/test-net-url.js
@@ -0,0 +1,137 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { readURI, readURISync } = require("sdk/net/url");
+const data = require("./fixtures");
+
+const utf8text = "Hello, ゼロ!";
+const latin1text = "Hello, ゼロ!";
+
+const dataURIutf8 = "data:text/plain;charset=utf-8," + encodeURIComponent(utf8text);
+const dataURIlatin1 = "data:text/plain;charset=ISO-8859-1," + escape(latin1text);
+const chromeURI = "chrome://global-platform/locale/accessible.properties";
+
+exports["test async readURI"] = function(assert, done) {
+ let content = "";
+
+ readURI(data.url("test-net-url.txt")).then(function(data) {
+ content = data;
+ assert.equal(content, utf8text, "The URL content is loaded properly");
+ done();
+ }, function() {
+ assert.fail("should not reject");
+ done();
+ })
+
+ assert.equal(content, "", "The URL content is not load yet");
+}
+
+exports["test readURISync"] = function(assert) {
+ let content = readURISync(data.url("test-net-url.txt"));
+
+ assert.equal(content, utf8text, "The URL content is loaded properly");
+}
+
+exports["test async readURI with ISO-8859-1 charset"] = function(assert, done) {
+ let content = "";
+
+ readURI(data.url("test-net-url.txt"), { charset : "ISO-8859-1"}).then(function(data) {
+ content = data;
+ assert.equal(content, latin1text, "The URL content is loaded properly");
+ done();
+ }, function() {
+ assert.fail("should not reject");
+ done();
+ })
+
+ assert.equal(content, "", "The URL content is not load yet");
+}
+
+exports["test readURISync with ISO-8859-1 charset"] = function(assert) {
+ let content = readURISync(data.url("test-net-url.txt"), "ISO-8859-1");
+
+ assert.equal(content, latin1text, "The URL content is loaded properly");
+}
+
+exports["test async readURI with not existing file"] = function(assert, done) {
+ readURI(data.url("test-net-url-fake.txt")).then(function(data) {
+ assert.fail("should not resolve");
+ done();
+ }, function(reason) {
+ assert.ok(reason.indexOf("Failed to read:") === 0);
+ done();
+ })
+}
+
+exports["test readURISync with not existing file"] = function(assert) {
+ assert.throws(function() {
+ readURISync(data.url("test-net-url-fake.txt"));
+ }, /NS_ERROR_FILE_NOT_FOUND/);
+}
+
+exports["test async readURI with data URI"] = function(assert, done) {
+ let content = "";
+
+ readURI(dataURIutf8).then(function(data) {
+ content = data;
+ assert.equal(content, utf8text, "The URL content is loaded properly");
+ done();
+ }, function() {
+ assert.fail("should not reject");
+ done();
+ })
+
+ assert.equal(content, "", "The URL content is not load yet");
+}
+
+exports["test readURISync with data URI"] = function(assert) {
+ let content = readURISync(dataURIutf8);
+
+ assert.equal(content, utf8text, "The URL content is loaded properly");
+}
+
+exports["test async readURI with data URI and ISO-8859-1 charset"] = function(assert, done) {
+ let content = "";
+
+ readURI(dataURIlatin1, { charset : "ISO-8859-1"}).then(function(data) {
+ content = unescape(data);
+ assert.equal(content, latin1text, "The URL content is loaded properly");
+ done();
+ }, function() {
+ assert.fail("should not reject");
+ done();
+ })
+
+ assert.equal(content, "", "The URL content is not load yet");
+}
+
+exports["test readURISync with data URI and ISO-8859-1 charset"] = function(assert) {
+ let content = unescape(readURISync(dataURIlatin1, "ISO-8859-1"));
+
+ assert.equal(content, latin1text, "The URL content is loaded properly");
+}
+
+exports["test readURISync with chrome URI"] = function(assert) {
+ let content = readURISync(chromeURI);
+
+ assert.ok(content, "The URL content is loaded properly");
+}
+
+exports["test async readURI with chrome URI"] = function(assert, done) {
+ let content = "";
+
+ readURI(chromeURI).then(function(data) {
+ content = data;
+ assert.equal(content, readURISync(chromeURI), "The URL content is loaded properly");
+ done();
+ }, function() {
+ assert.fail("should not reject");
+ done();
+ })
+
+ assert.equal(content, "", "The URL content is not load yet");
+}
+
+require("sdk/test").run(exports)
diff --git a/addon-sdk/source/test/test-node-os.js b/addon-sdk/source/test/test-node-os.js
new file mode 100644
index 000000000..e8316d7d9
--- /dev/null
+++ b/addon-sdk/source/test/test-node-os.js
@@ -0,0 +1,33 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+var os = require("node/os");
+var system = require("sdk/system");
+
+exports["test os"] = function (assert) {
+ assert.equal(os.tmpdir(), system.pathFor("TmpD"), "os.tmpdir() matches temp dir");
+ assert.ok(os.endianness() === "BE" || os.endianness() === "LE", "os.endianness is BE or LE");
+
+ assert.ok(os.arch().length > 0, "os.arch() returns a value");
+ assert.equal(typeof os.arch(), "string", "os.arch() returns a string");
+ assert.ok(os.type().length > 0, "os.type() returns a value");
+ assert.equal(typeof os.type(), "string", "os.type() returns a string");
+ assert.ok(os.platform().length > 0, "os.platform() returns a value");
+ assert.equal(typeof os.platform(), "string", "os.platform() returns a string");
+
+ assert.ok(os.release().length > 0, "os.release() returns a value");
+ assert.equal(typeof os.release(), "string", "os.release() returns a string");
+ assert.ok(os.hostname().length > 0, "os.hostname() returns a value");
+ assert.equal(typeof os.hostname(), "string", "os.hostname() returns a string");
+ assert.ok(os.EOL === "\n" || os.EOL === "\r\n", "os.EOL returns a correct EOL char");
+
+ assert.deepEqual(os.loadavg(), [0, 0, 0], "os.loadavg() returns an array of 0s");
+
+ ["uptime", "totalmem", "freemem", "cpus"].forEach(method => {
+ assert.throws(() => os[method](), "os." + method + " throws");
+ });
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-notifications.js b/addon-sdk/source/test/test-notifications.js
new file mode 100644
index 000000000..9a4d6cea0
--- /dev/null
+++ b/addon-sdk/source/test/test-notifications.js
@@ -0,0 +1,94 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+ "use strict";
+
+const { Loader } = require('sdk/test/loader');
+
+exports["test onClick"] = function(assert) {
+ let [loader, mockAlertServ] = makeLoader(module);
+ let notifs = loader.require("sdk/notifications");
+ let data = "test data";
+ let opts = {
+ onClick: function (clickedData) {
+ assert.equal(this, notifs, "|this| should be notifications module");
+ assert.equal(clickedData, data,
+ "data passed to onClick should be correct");
+ },
+ data: data,
+ title: "test title",
+ text: "test text",
+ iconURL: "test icon URL"
+ };
+ notifs.notify(opts);
+ mockAlertServ.click();
+ loader.unload();
+};
+
+exports['test numbers and URLs in options'] = function(assert) {
+ let [loader] = makeLoader(module);
+ let notifs = loader.require('sdk/notifications');
+ let opts = {
+ title: 123,
+ text: 45678,
+ // must use in-loader `sdk/url` module for the validation type check to work
+ iconURL: loader.require('sdk/url').URL('data:image/png,blah')
+ };
+ try {
+ notifs.notify(opts);
+ assert.pass('using numbers and URLs in options works');
+ } catch (e) {
+ assert.fail('using numbers and URLs in options must not throw');
+ }
+ loader.unload();
+}
+
+exports['test new tag, dir and lang options'] = function(assert) {
+ let [loader] = makeLoader(module);
+ let notifs = loader.require('sdk/notifications');
+ let opts = {
+ title: 'best',
+ tag: 'tagging',
+ lang: 'en'
+ };
+
+ try {
+ opts.dir = 'ttb';
+ notifs.notify(opts);
+ assert.fail('`dir` option must not accept TopToBottom direction.');
+ } catch (e) {
+ assert.equal(e.message,
+ '`dir` option must be one of: "auto", "ltr" or "rtl".');
+ }
+
+ try {
+ opts.dir = 'rtl';
+ notifs.notify(opts);
+ assert.pass('`dir` option accepts "rtl" direction.');
+ } catch (e) {
+ assert.fail('`dir` option must accept "rtl" direction.');
+ }
+
+ loader.unload();
+}
+
+// Returns [loader, mockAlertService].
+function makeLoader(module) {
+ let loader = Loader(module);
+ let mockAlertServ = {
+ showAlertNotification: function (imageUrl, title, text, textClickable,
+ cookie, alertListener, name) {
+ this._cookie = cookie;
+ this._alertListener = alertListener;
+ },
+ click: function () {
+ this._alertListener.observe(null, "alertclickcallback", this._cookie);
+ }
+ };
+ loader.require("sdk/notifications");
+ let scope = loader.sandbox("sdk/notifications");
+ scope.notify = mockAlertServ.showAlertNotification.bind(mockAlertServ);
+ return [loader, mockAlertServ];
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-object.js b/addon-sdk/source/test/test-object.js
new file mode 100644
index 000000000..a380fac8a
--- /dev/null
+++ b/addon-sdk/source/test/test-object.js
@@ -0,0 +1,36 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { merge, extend, has, each } = require('sdk/util/object');
+
+var o = {
+ 'paper': 0,
+ 'rock': 1,
+ 'scissors': 2
+};
+
+//exports.testMerge = function(assert) {}
+//exports.testExtend = function(assert) {}
+
+exports.testHas = function(assert) {
+ assert.equal(has(o, 'paper'), true, 'has correctly finds key');
+ assert.equal(has(o, 'rock'), true, 'has correctly finds key');
+ assert.equal(has(o, 'scissors'), true, 'has correctly finds key');
+ assert.equal(has(o, 'nope'), false, 'has correctly does not find key');
+ assert.equal(has(o, '__proto__'), false, 'has correctly does not find key');
+ assert.equal(has(o, 'isPrototypeOf'), false, 'has correctly does not find key');
+};
+
+exports.testEach = function(assert) {
+ var keys = new Set();
+ each(o, function (value, key, object) {
+ keys.add(key);
+ assert.equal(o[key], value, 'Key and value pairs passed in');
+ assert.equal(o, object, 'Object passed in');
+ });
+ assert.equal(keys.size, 3, 'All keys have been iterated upon');
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-observers.js b/addon-sdk/source/test/test-observers.js
new file mode 100644
index 000000000..4f15a87f1
--- /dev/null
+++ b/addon-sdk/source/test/test-observers.js
@@ -0,0 +1,183 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Loader } = require("sdk/test/loader");
+const { isWeak, WeakReference } = require("sdk/core/reference");
+const { subscribe, unsubscribe,
+ observe, Observer } = require("sdk/core/observer");
+const { Class } = require("sdk/core/heritage");
+
+const { Cc, Ci, Cu } = require("chrome");
+const { notifyObservers } = Cc["@mozilla.org/observer-service;1"].
+ getService(Ci.nsIObserverService);
+const { defer } = require("sdk/core/promise");
+
+const message = x => ({wrappedJSObject: x});
+
+exports["test subscribe unsubscribe"] = assert => {
+ const topic = Date.now().toString(32);
+ const Subscriber = Class({
+ extends: Observer,
+ initialize: function(observe) {
+ this.observe = observe;
+ }
+ });
+ observe.define(Subscriber, (x, subject, _, data) =>
+ x.observe(subject.wrappedJSObject.x));
+
+ let xs = [];
+ const x = Subscriber((...rest) => xs.push(...rest));
+
+ let ys = [];
+ const y = Subscriber((...rest) => ys.push(...rest));
+
+ const publish = (topic, data) =>
+ notifyObservers(message(data), topic, null);
+
+ publish({x:0});
+
+ subscribe(x, topic);
+
+ publish(topic, {x:1});
+
+ subscribe(y, topic);
+
+ publish(topic, {x:2});
+ publish(topic + "!", {x: 2.5});
+
+ unsubscribe(x, topic);
+
+ publish(topic, {x:3});
+
+ subscribe(y, topic);
+
+ publish(topic, {x:4});
+
+ subscribe(x, topic);
+
+ publish(topic, {x:5});
+
+ unsubscribe(x, topic);
+ unsubscribe(y, topic);
+
+ publish(topic, {x:6});
+
+ assert.deepEqual(xs, [1, 2, 5]);
+ assert.deepEqual(ys, [2, 3, 4, 5]);
+}
+
+exports["test weak observers are GC-ed on unload"] = (assert, end) => {
+ const topic = Date.now().toString(32);
+ const loader = Loader(module);
+ const { Observer, observe,
+ subscribe, unsubscribe } = loader.require("sdk/core/observer");
+ const { isWeak, WeakReference } = loader.require("sdk/core/reference");
+
+ const MyObserver = Class({
+ extends: Observer,
+ initialize: function(observe) {
+ this.observe = observe;
+ }
+ });
+ observe.define(MyObserver, (x, ...rest) => x.observe(...rest));
+
+ const MyWeakObserver = Class({
+ extends: MyObserver,
+ implements: [WeakReference]
+ });
+
+ let xs = [];
+ let ys = [];
+ let x = new MyObserver((subject, topic, data) => {
+ xs.push(subject.wrappedJSObject, topic, data);
+ });
+ let y = new MyWeakObserver((subject, topic, data) => {
+ ys.push(subject.wrappedJSObject, topic, data);
+ });
+
+ subscribe(x, topic);
+ subscribe(y, topic);
+
+
+ notifyObservers(message({ foo: 1 }), topic, null);
+ x = null;
+ y = null;
+ loader.unload();
+
+ Cu.schedulePreciseGC(() => {
+
+ notifyObservers(message({ bar: 2 }), topic, ":)");
+
+ assert.deepEqual(xs, [{ foo: 1 }, topic, null,
+ { bar: 2 }, topic, ":)"],
+ "non week observer is kept");
+
+ assert.deepEqual(ys, [{ foo: 1 }, topic, null],
+ "week observer was GC-ed");
+
+ end();
+ });
+};
+
+exports["test weak observer unsubscribe"] = function*(assert) {
+ const loader = Loader(module);
+ const { Observer, observe, subscribe, unsubscribe } = loader.require("sdk/core/observer");
+ const { WeakReference } = loader.require("sdk/core/reference");
+
+ let sawNotification = false;
+ let firstWait = defer();
+ let secondWait = defer();
+
+ const WeakObserver = Class({
+ extends: Observer,
+ implements: [WeakReference],
+ observe: function() {
+ sawNotification = true;
+ firstWait.resolve();
+ }
+ });
+
+ const StrongObserver = Class({
+ extends: Observer,
+ observe: function() {
+ secondWait.resolve();
+ }
+ });
+
+ observe.define(Observer, (x, ...rest) => x.observe(...rest));
+
+ let weakObserver = new WeakObserver;
+ let strongObserver = new StrongObserver();
+ subscribe(weakObserver, "test-topic");
+ subscribe(strongObserver, "test-wait");
+
+ notifyObservers(null, "test-topic", null);
+ yield firstWait.promise;
+
+ assert.ok(sawNotification, "Should have seen notification before GC");
+ sawNotification = false;
+
+ yield loader.require("sdk/test/memory").gc();
+
+ notifyObservers(null, "test-topic", null);
+ notifyObservers(null, "test-wait", null);
+ yield secondWait.promise;
+
+ assert.ok(sawNotification, "Should have seen notification after GC");
+ sawNotification = false;
+
+ try {
+ unsubscribe(weakObserver, "test-topic");
+ unsubscribe(strongObserver, "test-wait");
+ assert.pass("Should not have seen an exception");
+ }
+ catch (e) {
+ assert.fail("Should not have seen an exception");
+ }
+
+ loader.unload();
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-page-mod-debug.js b/addon-sdk/source/test/test-page-mod-debug.js
new file mode 100644
index 000000000..86f491149
--- /dev/null
+++ b/addon-sdk/source/test/test-page-mod-debug.js
@@ -0,0 +1,66 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Cc, Ci, Cu } = require("chrome");
+const { PageMod } = require("sdk/page-mod");
+const { testPageMod, handleReadyState, openNewTab,
+ contentScriptWhenServer, createLoader } = require("./page-mod/helpers");
+const { cleanUI, after } = require("sdk/test/utils");
+const { open, getFrames, getMostRecentBrowserWindow, getInnerId } = require("sdk/window/utils");
+
+const { devtools } = Cu.import("resource://devtools/shared/Loader.jsm", {});
+const { require: devtoolsRequire } = devtools;
+const contentGlobals = devtoolsRequire("devtools/server/content-globals");
+
+// The following adds Debugger constructor to the global namespace.
+const { addDebuggerToGlobal } = require('resource://gre/modules/jsdebugger.jsm');
+addDebuggerToGlobal(this);
+
+exports.testDebugMetadata = function(assert, done) {
+ let dbg = new Debugger;
+ let globalDebuggees = [];
+ dbg.onNewGlobalObject = function(global) {
+ globalDebuggees.push(global);
+ }
+
+ let mods = testPageMod(assert, done, "about:", [{
+ include: "about:",
+ contentScriptWhen: "start",
+ contentScript: "null;",
+ }], function(win, done) {
+ assert.ok(globalDebuggees.some(function(global) {
+ try {
+ let metadata = Cu.getSandboxMetadata(global.unsafeDereference());
+ return metadata && metadata.addonID && metadata.SDKContentScript &&
+ metadata['inner-window-id'] == getInnerId(win);
+ } catch(e) {
+ // Some of the globals might not be Sandbox instances and thus
+ // will cause getSandboxMetadata to fail.
+ return false;
+ }
+ }), "one of the globals is a content script");
+ done();
+ }
+ );
+};
+
+exports.testDevToolsExtensionsGetContentGlobals = function(assert, done) {
+ let mods = testPageMod(assert, done, "about:", [{
+ include: "about:",
+ contentScriptWhen: "start",
+ contentScript: "null;",
+ }], function(win, done) {
+ assert.equal(contentGlobals.getContentGlobals({ 'inner-window-id': getInnerId(win) }).length, 1);
+ done();
+ }
+ );
+};
+
+after(exports, function*(name, assert) {
+ assert.pass("cleaning ui.");
+ yield cleanUI();
+});
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-page-mod.js b/addon-sdk/source/test/test-page-mod.js
new file mode 100644
index 000000000..d03463d2d
--- /dev/null
+++ b/addon-sdk/source/test/test-page-mod.js
@@ -0,0 +1,2214 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Cc, Ci, Cu } = require("chrome");
+const { PageMod } = require("sdk/page-mod");
+const { testPageMod, handleReadyState, openNewTab,
+ contentScriptWhenServer, createLoader } = require("./page-mod/helpers");
+const { Loader } = require("sdk/test/loader");
+const tabs = require("sdk/tabs");
+const { setTimeout } = require("sdk/timers");
+const system = require("sdk/system/events");
+const { open, getFrames, getMostRecentBrowserWindow, getInnerId } = require("sdk/window/utils");
+const { getTabContentWindow, getActiveTab, setTabURL, openTab, closeTab,
+ getBrowserForTab } = require("sdk/tabs/utils");
+const xulApp = require("sdk/system/xul-app");
+const { isPrivateBrowsingSupported } = require("sdk/self");
+const { isPrivate } = require("sdk/private-browsing");
+const { openWebpage } = require("./private-browsing/helper");
+const { isTabPBSupported, isWindowPBSupported } = require("sdk/private-browsing/utils");
+const promise = require("sdk/core/promise");
+const { pb } = require("./private-browsing/helper");
+const { URL } = require("sdk/url");
+const { defer, all, resolve } = require("sdk/core/promise");
+const { waitUntil } = require("sdk/test/utils");
+const data = require("./fixtures");
+const { cleanUI, after } = require("sdk/test/utils");
+
+const testPageURI = data.url("test.html");
+
+function Isolate(worker) {
+ return "(" + worker + ")()";
+}
+
+/* Tests for the PageMod APIs */
+
+exports.testPageMod1 = function*(assert) {
+ let modAttached = defer();
+ let mod = PageMod({
+ include: /about:/,
+ contentScriptWhen: "end",
+ contentScript: "new " + function WorkerScope() {
+ window.document.body.setAttribute("JEP-107", "worked");
+
+ self.port.once("done", () => {
+ self.port.emit("results", window.document.body.getAttribute("JEP-107"))
+ });
+ },
+ onAttach: function(worker) {
+ assert.equal(this, mod, "The 'this' object is the page mod.");
+ mod.port.once("results", modAttached.resolve)
+ mod.port.emit("done");
+ }
+ });
+
+ let tab = yield new Promise(resolve => {
+ tabs.open({
+ url: "about:",
+ inBackground: true,
+ onReady: resolve
+ })
+ });
+ assert.pass("test tab was opened.");
+
+ let worked = yield modAttached.promise;
+ assert.pass("test mod was attached.");
+
+ mod.destroy();
+ assert.pass("test mod was destroyed.");
+
+ assert.equal(worked, "worked", "PageMod.onReady test");
+};
+
+exports.testPageMod2 = function*(assert) {
+ let modAttached = defer();
+ let mod = PageMod({
+ include: testPageURI,
+ contentScriptWhen: "end",
+ contentScript: [
+ 'new ' + function contentScript() {
+ window.AUQLUE = function() { return 42; }
+ try {
+ window.AUQLUE()
+ }
+ catch(e) {
+ throw new Error("PageMod scripts executed in order");
+ }
+ document.documentElement.setAttribute("first", "true");
+ },
+ 'new ' + function contentScript() {
+ document.documentElement.setAttribute("second", "true");
+
+ self.port.once("done", () => {
+ self.port.emit("results", {
+ "first": window.document.documentElement.getAttribute("first"),
+ "second": window.document.documentElement.getAttribute("second"),
+ "AUQLUE": unsafeWindow.getAUQLUE()
+ });
+ });
+ }
+ ],
+ onAttach: modAttached.resolve
+ });
+
+ let tab = yield new Promise(resolve => {
+ tabs.open({
+ url: testPageURI,
+ inBackground: true,
+ onReady: resolve
+ })
+ });
+ assert.pass("test tab was opened.");
+
+ let worker = yield modAttached.promise;
+ assert.pass("test mod was attached.");
+
+ let results = yield new Promise(resolve => {
+ worker.port.once("results", resolve)
+ worker.port.emit("done");
+ });
+
+ mod.destroy();
+ assert.pass("test mod was destroyed.");
+
+ assert.equal(results["first"],
+ "true",
+ "PageMod test #2: first script has run");
+ assert.equal(results["second"],
+ "true",
+ "PageMod test #2: second script has run");
+ assert.equal(results["AUQLUE"], false,
+ "PageMod test #2: scripts get a wrapped window");
+};
+
+exports.testPageModIncludes = function*(assert) {
+ var modsAttached = [];
+ var modNumber = 0;
+ var modAttached = defer();
+ let includes = [
+ "*",
+ "*.google.com",
+ "resource:*",
+ "resource:",
+ testPageURI
+ ];
+ let expected = [
+ false,
+ false,
+ true,
+ false,
+ true
+ ]
+
+ let mod = PageMod({
+ include: testPageURI,
+ contentScript: 'new ' + function() {
+ self.port.on("get-local-storage", () => {
+ let result = {};
+ self.options.forEach(include => {
+ result[include] = !!window.localStorage[include]
+ });
+
+ self.port.emit("got-local-storage", result);
+
+ window.localStorage.clear();
+ });
+ },
+ contentScriptOptions: includes,
+ onAttach: modAttached.resolve
+ });
+
+ function createPageModTest(include, expectedMatch) {
+ var modIndex = modNumber++;
+
+ let attached = defer();
+ modsAttached.push(expectedMatch ? attached.promise : resolve());
+
+ // ...and corresponding PageMod options
+ return PageMod({
+ include: include,
+ contentScript: 'new ' + function() {
+ self.on("message", function(msg) {
+ window.localStorage[msg] = true
+ self.port.emit('done');
+ });
+ },
+ // The testPageMod callback with test assertions is called on 'end',
+ // and we want this page mod to be attached before it gets called,
+ // so we attach it on 'start'.
+ contentScriptWhen: 'start',
+ onAttach: function(worker) {
+ assert.pass("mod " + modIndex + " was attached");
+
+ worker.port.once("done", () => {
+ assert.pass("mod " + modIndex + " is done");
+ attached.resolve(worker);
+ });
+ worker.postMessage(this.include[0]);
+ }
+ });
+ }
+
+ let mods = [
+ createPageModTest("*", false),
+ createPageModTest("*.google.com", false),
+ createPageModTest("resource:*", true),
+ createPageModTest("resource:", false),
+ createPageModTest(testPageURI, true)
+ ];
+
+ let tab = yield new Promise(resolve => {
+ tabs.open({
+ url: testPageURI,
+ inBackground: true,
+ onReady: resolve
+ });
+ });
+ assert.pass("tab was opened");
+
+ yield all(modsAttached);
+ assert.pass("all mods were attached.");
+
+ mods.forEach(mod => mod.destroy());
+ assert.pass("all mods were destroyed.");
+
+ yield modAttached.promise;
+ assert.pass("final test mod was attached.");
+
+ yield new Promise(resolve => {
+ mod.port.on("got-local-storage", (storage) => {
+ includes.forEach((include, i) => {
+ assert.equal(storage[include], expected[i], "localStorage is correct for " + include);
+ });
+ resolve();
+ });
+ mod.port.emit("get-local-storage");
+ });
+ assert.pass("final test of localStorage is complete.");
+
+ mod.destroy();
+ assert.pass("final test mod was destroyed.");
+};
+
+exports.testPageModExcludes = function(assert, done) {
+ var asserts = [];
+ function createPageModTest(include, exclude, expectedMatch) {
+ // Create an 'onload' test function...
+ asserts.push(function(test, win) {
+ var matches = JSON.stringify([include, exclude]) in win.localStorage;
+ assert.ok(expectedMatch ? matches : !matches,
+ "[include, exclude] = [" + include + ", " + exclude +
+ "] match test, expected: " + expectedMatch);
+ });
+ // ...and corresponding PageMod options
+ return {
+ include: include,
+ exclude: exclude,
+ contentScript: 'new ' + function() {
+ self.on("message", function(msg) {
+ // The key in localStorage is "[<include>, <exclude>]".
+ window.localStorage[JSON.stringify(msg)] = true;
+ });
+ },
+ // The testPageMod callback with test assertions is called on 'end',
+ // and we want this page mod to be attached before it gets called,
+ // so we attach it on 'start'.
+ contentScriptWhen: 'start',
+ onAttach: function(worker) {
+ worker.postMessage([this.include[0], this.exclude[0]]);
+ }
+ };
+ }
+
+ testPageMod(assert, done, testPageURI, [
+ createPageModTest("*", testPageURI, false),
+ createPageModTest(testPageURI, testPageURI, false),
+ createPageModTest(testPageURI, "resource://*", false),
+ createPageModTest(testPageURI, "*.google.com", true)
+ ],
+ function (win, done) {
+ waitUntil(() => win.localStorage[JSON.stringify([testPageURI, "*.google.com"])],
+ testPageURI + " page-mod to be executed")
+ .then(() => {
+ asserts.forEach(fn => fn(assert, win));
+ win.localStorage.clear();
+ done();
+ });
+ });
+};
+
+exports.testPageModValidationAttachTo = function(assert) {
+ [{ val: 'top', type: 'string "top"' },
+ { val: 'frame', type: 'string "frame"' },
+ { val: ['top', 'existing'], type: 'array with "top" and "existing"' },
+ { val: ['frame', 'existing'], type: 'array with "frame" and "existing"' },
+ { val: ['top'], type: 'array with "top"' },
+ { val: ['frame'], type: 'array with "frame"' },
+ { val: undefined, type: 'undefined' }].forEach((attachTo) => {
+ new PageMod({ attachTo: attachTo.val, include: '*.validation111' });
+ assert.pass("PageMod() does not throw when attachTo is " + attachTo.type);
+ });
+
+ [{ val: 'existing', type: 'string "existing"' },
+ { val: ['existing'], type: 'array with "existing"' },
+ { val: 'not-legit', type: 'string with "not-legit"' },
+ { val: ['not-legit'], type: 'array with "not-legit"' },
+ { val: {}, type: 'object' }].forEach((attachTo) => {
+ assert.throws(() =>
+ new PageMod({ attachTo: attachTo.val, include: '*.validation111' }),
+ /The `attachTo` option/,
+ "PageMod() throws when 'attachTo' option is " + attachTo.type + ".");
+ });
+};
+
+exports.testPageModValidationInclude = function(assert) {
+ [{ val: undefined, type: 'undefined' },
+ { val: {}, type: 'object' },
+ { val: [], type: 'empty array'},
+ { val: [/regexp/, 1], type: 'array with non string/regexp' },
+ { val: 1, type: 'number' }].forEach((include) => {
+ assert.throws(() => new PageMod({ include: include.val }),
+ /The `include` option must always contain atleast one rule/,
+ "PageMod() throws when 'include' option is " + include.type + ".");
+ });
+
+ [{ val: '*.validation111', type: 'string' },
+ { val: /validation111/, type: 'regexp' },
+ { val: ['*.validation111'], type: 'array with length > 0'}].forEach((include) => {
+ new PageMod({ include: include.val });
+ assert.pass("PageMod() does not throw when include option is " + include.type);
+ });
+};
+
+exports.testPageModValidationExclude = function(assert) {
+ let includeVal = '*.validation111';
+
+ [{ val: {}, type: 'object' },
+ { val: [], type: 'empty array'},
+ { val: [/regexp/, 1], type: 'array with non string/regexp' },
+ { val: 1, type: 'number' }].forEach((exclude) => {
+ assert.throws(() => new PageMod({ include: includeVal, exclude: exclude.val }),
+ /If set, the `exclude` option must always contain at least one rule as a string, regular expression, or an array of strings and regular expressions./,
+ "PageMod() throws when 'exclude' option is " + exclude.type + ".");
+ });
+
+ [{ val: undefined, type: 'undefined' },
+ { val: '*.validation111', type: 'string' },
+ { val: /validation111/, type: 'regexp' },
+ { val: ['*.validation111'], type: 'array with length > 0'}].forEach((exclude) => {
+ new PageMod({ include: includeVal, exclude: exclude.val });
+ assert.pass("PageMod() does not throw when exclude option is " + exclude.type);
+ });
+};
+
+/* Tests for internal functions. */
+exports.testCommunication1 = function*(assert) {
+ let workerDone = defer();
+
+ let mod = PageMod({
+ include: "about:*",
+ contentScriptWhen: "end",
+ contentScript: 'new ' + function WorkerScope() {
+ self.on('message', function(msg) {
+ document.body.setAttribute('JEP-107', 'worked');
+ self.postMessage(document.body.getAttribute('JEP-107'));
+ });
+ self.port.on('get-jep-107', () => {
+ self.port.emit('got-jep-107', document.body.getAttribute('JEP-107'));
+ });
+ },
+ onAttach: function(worker) {
+ worker.on('error', function(e) {
+ assert.fail('Errors where reported');
+ });
+ worker.on('message', function(value) {
+ assert.equal(
+ "worked",
+ value,
+ "test comunication"
+ );
+ workerDone.resolve();
+ });
+ worker.postMessage("do it!")
+ }
+ });
+
+ let tab = yield new Promise(resolve => {
+ tabs.open({
+ url: "about:",
+ onReady: resolve
+ });
+ });
+ assert.pass("opened tab");
+
+ yield workerDone.promise;
+ assert.pass("the worker has made a change");
+
+ let value = yield new Promise(resolve => {
+ mod.port.once("got-jep-107", resolve);
+ mod.port.emit("get-jep-107");
+ });
+
+ assert.equal("worked", value, "attribute should be modified");
+
+ mod.destroy();
+ assert.pass("the worker was destroyed");
+};
+
+exports.testCommunication2 = function*(assert) {
+ let workerDone = defer();
+ let url = data.url("test.html");
+
+ let mod = PageMod({
+ include: url,
+ contentScriptWhen: 'start',
+ contentScript: 'new ' + function WorkerScope() {
+ document.documentElement.setAttribute('AUQLUE', 42);
+
+ window.addEventListener('load', function listener() {
+ self.postMessage({
+ msg: 'onload',
+ AUQLUE: document.documentElement.getAttribute('AUQLUE')
+ });
+ }, false);
+
+ self.on("message", function(msg) {
+ if (msg == "get window.test") {
+ unsafeWindow.changesInWindow();
+ }
+
+ self.postMessage({
+ msg: document.documentElement.getAttribute("test")
+ });
+ });
+ },
+ onAttach: function(worker) {
+ worker.on('error', function(e) {
+ assert.fail('Errors where reported');
+ });
+ worker.on('message', function({ msg, AUQLUE }) {
+ if ('onload' == msg) {
+ assert.equal('42', AUQLUE, 'PageMod scripts executed in order');
+ worker.postMessage('get window.test');
+ }
+ else {
+ assert.equal('changes in window', msg, 'PageMod test #2: second script has run');
+ workerDone.resolve();
+ }
+ });
+ }
+ });
+
+ let tab = yield new Promise(resolve => {
+ tabs.open({
+ url: url,
+ inBackground: true,
+ onReady: resolve
+ });
+ });
+ assert.pass("opened tab");
+
+ yield workerDone.promise;
+
+ mod.destroy();
+ assert.pass("the worker was destroyed");
+};
+
+exports.testEventEmitter = function(assert, done) {
+ let workerDone = false,
+ callbackDone = null;
+
+ testPageMod(assert, done, "about:", [{
+ include: "about:*",
+ contentScript: 'new ' + function WorkerScope() {
+ self.port.on('addon-to-content', function(data) {
+ self.port.emit('content-to-addon', data);
+ });
+ },
+ onAttach: function(worker) {
+ worker.on('error', function(e) {
+ assert.fail('Errors were reported : '+e);
+ });
+ worker.port.on('content-to-addon', function(value) {
+ assert.equal(
+ "worked",
+ value,
+ "EventEmitter API works!"
+ );
+ if (callbackDone)
+ callbackDone();
+ else
+ workerDone = true;
+ });
+ worker.port.emit('addon-to-content', 'worked');
+ }
+ }],
+ function(win, done) {
+ if (workerDone)
+ done();
+ else
+ callbackDone = done;
+ }
+ );
+};
+
+// Execute two concurrent page mods on same document to ensure that their
+// JS contexts are different
+exports.testMixedContext = function(assert, done) {
+ let doneCallback = null;
+ let messages = 0;
+ let modObject = {
+ include: "data:text/html;charset=utf-8,",
+ contentScript: 'new ' + function WorkerScope() {
+ // Both scripts will execute this,
+ // context is shared if one script see the other one modification.
+ let isContextShared = "sharedAttribute" in document;
+ self.postMessage(isContextShared);
+ document.sharedAttribute = true;
+ },
+ onAttach: function(w) {
+ w.on("message", function (isContextShared) {
+ if (isContextShared) {
+ assert.fail("Page mod contexts are mixed.");
+ doneCallback();
+ }
+ else if (++messages == 2) {
+ assert.pass("Page mod contexts are different.");
+ doneCallback();
+ }
+ });
+ }
+ };
+ testPageMod(assert, done, "data:text/html;charset=utf-8,", [modObject, modObject],
+ function(win, done) {
+ doneCallback = done;
+ }
+ );
+};
+
+exports.testHistory = function(assert, done) {
+ // We need a valid url in order to have a working History API.
+ // (i.e do not work on data: or about: pages)
+ // Test bug 679054.
+ let url = data.url("test-page-mod.html");
+ let callbackDone = null;
+ testPageMod(assert, done, url, [{
+ include: url,
+ contentScriptWhen: 'end',
+ contentScript: 'new ' + function WorkerScope() {
+ history.pushState({}, "", "#");
+ history.replaceState({foo: "bar"}, "", "#");
+ self.postMessage(history.state);
+ },
+ onAttach: function(worker) {
+ worker.on('message', function (data) {
+ assert.equal(JSON.stringify(data), JSON.stringify({foo: "bar"}),
+ "History API works!");
+ callbackDone();
+ });
+ }
+ }],
+ function(win, done) {
+ callbackDone = done;
+ }
+ );
+};
+
+exports.testRelatedTab = function(assert, done) {
+ let tab;
+ let pageMod = new PageMod({
+ include: "about:*",
+ onAttach: function(worker) {
+ assert.ok(!!worker.tab, "Worker.tab exists");
+ assert.equal(tab, worker.tab, "Worker.tab is valid");
+ pageMod.destroy();
+ tab.close(done);
+ }
+ });
+
+ tabs.open({
+ url: "about:",
+ onOpen: function onOpen(t) {
+ tab = t;
+ }
+ });
+};
+
+// related to bug #989288
+// https://bugzilla.mozilla.org/show_bug.cgi?id=989288
+exports.testRelatedTabNewWindow = function(assert, done) {
+ let url = "about:logo"
+ let pageMod = new PageMod({
+ include: url,
+ onAttach: function(worker) {
+ assert.equal(worker.tab.url, url, "Worker.tab.url is valid");
+ worker.tab.close(done);
+ }
+ });
+
+ tabs.activeTab.attach({
+ contentScript: "window.open('about:logo', '', " +
+ "'width=800,height=600,resizable=no,status=no,location=no');"
+ });
+
+};
+
+exports.testRelatedTabNoRequireTab = function(assert, done) {
+ let loader = Loader(module);
+ let tab;
+ let url = "data:text/html;charset=utf-8," + encodeURI("Test related worker tab 2");
+ let { PageMod } = loader.require("sdk/page-mod");
+ let pageMod = new PageMod({
+ include: url,
+ onAttach: function(worker) {
+ assert.equal(worker.tab.url, url, "Worker.tab.url is valid");
+ worker.tab.close(function() {
+ pageMod.destroy();
+ loader.unload();
+ done();
+ });
+ }
+ });
+
+ tabs.open(url);
+};
+
+exports.testRelatedTabNoOtherReqs = function(assert, done) {
+ let loader = Loader(module);
+ let { PageMod } = loader.require("sdk/page-mod");
+ let pageMod = new PageMod({
+ include: "about:blank?testRelatedTabNoOtherReqs",
+ onAttach: function(worker) {
+ assert.ok(!!worker.tab, "Worker.tab exists");
+ pageMod.destroy();
+ worker.tab.close(function() {
+ worker.destroy();
+ loader.unload();
+ done();
+ });
+ }
+ });
+
+ tabs.open({
+ url: "about:blank?testRelatedTabNoOtherReqs"
+ });
+};
+
+exports.testWorksWithExistingTabs = function(assert, done) {
+ let url = "data:text/html;charset=utf-8," + encodeURI("Test unique document");
+ let { PageMod } = require("sdk/page-mod");
+ tabs.open({
+ url: url,
+ onReady: function onReady(tab) {
+ let pageModOnExisting = new PageMod({
+ include: url,
+ attachTo: ["existing", "top", "frame"],
+ onAttach: function(worker) {
+ assert.ok(!!worker.tab, "Worker.tab exists");
+ assert.equal(tab, worker.tab, "A worker has been created on this existing tab");
+
+ worker.on('pageshow', () => {
+ assert.fail("Should not have seen pageshow for an already loaded page");
+ });
+
+ setTimeout(function() {
+ pageModOnExisting.destroy();
+ pageModOffExisting.destroy();
+ tab.close(done);
+ }, 0);
+ }
+ });
+
+ let pageModOffExisting = new PageMod({
+ include: url,
+ onAttach: function(worker) {
+ assert.fail("pageModOffExisting page-mod should not have attached to anything");
+ }
+ });
+ }
+ });
+};
+
+exports.testExistingFrameDoesntMatchInclude = function(assert, done) {
+ let iframeURL = 'data:text/html;charset=utf-8,UNIQUE-TEST-STRING-42';
+ let iframe = '<iframe src="' + iframeURL + '" />';
+ let url = 'data:text/html;charset=utf-8,' + encodeURIComponent(iframe);
+ tabs.open({
+ url: url,
+ onReady: function onReady(tab) {
+ let pagemod = new PageMod({
+ include: url,
+ attachTo: ['existing', 'frame'],
+ onAttach: function() {
+ assert.fail("Existing iframe URL doesn't match include, must not attach to anything");
+ }
+ });
+ setTimeout(function() {
+ assert.pass("PageMod didn't attach to anything")
+ pagemod.destroy();
+ tab.close(done);
+ }, 250);
+ }
+ });
+};
+
+exports.testExistingOnlyFrameMatchesInclude = function(assert, done) {
+ let iframeURL = 'data:text/html;charset=utf-8,UNIQUE-TEST-STRING-43';
+ let iframe = '<iframe src="' + iframeURL + '" />';
+ let url = 'data:text/html;charset=utf-8,' + encodeURIComponent(iframe);
+ tabs.open({
+ url: url,
+ onReady: function onReady(tab) {
+ let pagemod = new PageMod({
+ include: iframeURL,
+ attachTo: ['existing', 'frame'],
+ onAttach: function(worker) {
+ assert.equal(iframeURL, worker.url,
+ "PageMod attached to existing iframe when only it matches include rules");
+ pagemod.destroy();
+ tab.close(done);
+ }
+ });
+ }
+ });
+};
+
+exports.testAttachOnlyOncePerDocument = function(assert, done) {
+ let iframeURL = 'data:text/html;charset=utf-8,testAttachOnlyOncePerDocument';
+ let iframe = '<iframe src="' + iframeURL + '" />';
+ let url = 'data:text/html;charset=utf-8,' + encodeURIComponent(iframe);
+ let count = 0;
+
+ tabs.open({
+ url: url,
+ onReady: function onReady(tab) {
+ let pagemod = new PageMod({
+ include: iframeURL,
+ attachTo: ['existing', 'frame'],
+ onAttach: (worker) => {
+ count++;
+ assert.equal(iframeURL, worker.url,
+ "PageMod attached to existing iframe");
+ assert.equal(count, 1, "PageMod attached only once");
+ setTimeout(_ => {
+ assert.equal(count, 1, "PageMod attached only once");
+ pagemod.destroy();
+ tab.close(done);
+ }, 1);
+ }
+ });
+ }
+ });
+}
+
+exports.testContentScriptWhenDefault = function(assert) {
+ let pagemod = PageMod({include: '*'});
+
+ assert.equal(pagemod.contentScriptWhen, 'end', "Default contentScriptWhen is 'end'");
+ pagemod.destroy();
+}
+
+// test timing for all 3 contentScriptWhen options (start, ready, end)
+// for new pages, or tabs opened after PageMod is created
+exports.testContentScriptWhenForNewTabs = function(assert, done) {
+ let srv = contentScriptWhenServer();
+ let url = srv.URL + '?ForNewTabs';
+ let count = 0;
+
+ handleReadyState(url, 'start', {
+ onLoading: (tab) => {
+ assert.pass("PageMod is attached while document is loading");
+ checkDone(++count, tab, srv, done);
+ },
+ onInteractive: () => assert.fail("onInteractive should not be called with 'start'."),
+ onComplete: () => assert.fail("onComplete should not be called with 'start'."),
+ });
+
+ handleReadyState(url, 'ready', {
+ onInteractive: (tab) => {
+ assert.pass("PageMod is attached while document is interactive");
+ checkDone(++count, tab, srv, done);
+ },
+ onLoading: () => assert.fail("onLoading should not be called with 'ready'."),
+ onComplete: () => assert.fail("onComplete should not be called with 'ready'."),
+ });
+
+ handleReadyState(url, 'end', {
+ onComplete: (tab) => {
+ assert.pass("PageMod is attached when document is complete");
+ checkDone(++count, tab, srv, done);
+ },
+ onLoading: () => assert.fail("onLoading should not be called with 'end'."),
+ onInteractive: () => assert.fail("onInteractive should not be called with 'end'."),
+ });
+
+ tabs.open(url);
+}
+
+// test timing for all 3 contentScriptWhen options (start, ready, end)
+// for PageMods created right as the tab is created (in tab.onOpen)
+exports.testContentScriptWhenOnTabOpen = function(assert, done) {
+ let srv = contentScriptWhenServer();
+ let url = srv.URL + '?OnTabOpen';
+ let count = 0;
+
+ tabs.open({
+ url: url,
+ onOpen: function(tab) {
+
+ handleReadyState(url, 'start', {
+ onLoading: () => {
+ assert.pass("PageMod is attached while document is loading");
+ checkDone(++count, tab, srv, done);
+ },
+ onInteractive: () => assert.fail("onInteractive should not be called with 'start'."),
+ onComplete: () => assert.fail("onComplete should not be called with 'start'."),
+ });
+
+ handleReadyState(url, 'ready', {
+ onInteractive: () => {
+ assert.pass("PageMod is attached while document is interactive");
+ checkDone(++count, tab, srv, done);
+ },
+ onLoading: () => assert.fail("onLoading should not be called with 'ready'."),
+ onComplete: () => assert.fail("onComplete should not be called with 'ready'."),
+ });
+
+ handleReadyState(url, 'end', {
+ onComplete: () => {
+ assert.pass("PageMod is attached when document is complete");
+ checkDone(++count, tab, srv, done);
+ },
+ onLoading: () => assert.fail("onLoading should not be called with 'end'."),
+ onInteractive: () => assert.fail("onInteractive should not be called with 'end'."),
+ });
+
+ }
+ });
+}
+
+// test timing for all 3 contentScriptWhen options (start, ready, end)
+// for PageMods created while the tab is interactive (in tab.onReady)
+exports.testContentScriptWhenOnTabReady = function(assert, done) {
+ let srv = contentScriptWhenServer();
+ let url = srv.URL + '?OnTabReady';
+ let count = 0;
+
+ tabs.open({
+ url: url,
+ onReady: function(tab) {
+
+ handleReadyState(url, 'start', {
+ onInteractive: () => {
+ assert.pass("PageMod is attached while document is interactive");
+ checkDone(++count, tab, srv, done);
+ },
+ onLoading: () => assert.fail("onLoading should not be called with 'start'."),
+ onComplete: () => assert.fail("onComplete should not be called with 'start'."),
+ });
+
+ handleReadyState(url, 'ready', {
+ onInteractive: () => {
+ assert.pass("PageMod is attached while document is interactive");
+ checkDone(++count, tab, srv, done);
+ },
+ onLoading: () => assert.fail("onLoading should not be called with 'ready'."),
+ onComplete: () => assert.fail("onComplete should not be called with 'ready'."),
+ });
+
+ handleReadyState(url, 'end', {
+ onComplete: () => {
+ assert.pass("PageMod is attached when document is complete");
+ checkDone(++count, tab, srv, done);
+ },
+ onLoading: () => assert.fail("onLoading should not be called with 'end'."),
+ onInteractive: () => assert.fail("onInteractive should not be called with 'end'."),
+ });
+
+ }
+ });
+}
+
+// test timing for all 3 contentScriptWhen options (start, ready, end)
+// for PageMods created after a tab has completed loading (in tab.onLoad)
+exports.testContentScriptWhenOnTabLoad = function(assert, done) {
+ let srv = contentScriptWhenServer();
+ let url = srv.URL + '?OnTabLoad';
+ let count = 0;
+
+ tabs.open({
+ url: url,
+ onLoad: function(tab) {
+
+ handleReadyState(url, 'start', {
+ onComplete: () => {
+ assert.pass("PageMod is attached when document is complete");
+ checkDone(++count, tab, srv, done);
+ },
+ onLoading: () => assert.fail("onLoading should not be called with 'start'."),
+ onInteractive: () => assert.fail("onInteractive should not be called with 'start'."),
+ });
+
+ handleReadyState(url, 'ready', {
+ onComplete: () => {
+ assert.pass("PageMod is attached when document is complete");
+ checkDone(++count, tab, srv, done);
+ },
+ onLoading: () => assert.fail("onLoading should not be called with 'ready'."),
+ onInteractive: () => assert.fail("onInteractive should not be called with 'ready'."),
+ });
+
+ handleReadyState(url, 'end', {
+ onComplete: () => {
+ assert.pass("PageMod is attached when document is complete");
+ checkDone(++count, tab, srv, done);
+ },
+ onLoading: () => assert.fail("onLoading should not be called with 'end'."),
+ onInteractive: () => assert.fail("onInteractive should not be called with 'end'."),
+ });
+
+ }
+ });
+}
+
+function checkDone(count, tab, srv, done) {
+ if (count === 3)
+ tab.close(_ => srv.stop(done));
+}
+
+exports.testTabWorkerOnMessage = function(assert, done) {
+ let { browserWindows } = require("sdk/windows");
+ let tabs = require("sdk/tabs");
+ let { PageMod } = require("sdk/page-mod");
+
+ let url1 = "data:text/html;charset=utf-8,<title>tab1</title><h1>worker1.tab</h1>";
+ let url2 = "data:text/html;charset=utf-8,<title>tab2</title><h1>worker2.tab</h1>";
+ let worker1 = null;
+
+ let mod = PageMod({
+ include: "data:text/html*",
+ contentScriptWhen: "ready",
+ contentScript: "self.postMessage('#1');",
+ onAttach: function onAttach(worker) {
+ worker.on("message", function onMessage() {
+ this.tab.attach({
+ contentScriptWhen: "ready",
+ contentScript: "self.postMessage({ url: window.location.href, title: document.title });",
+ onMessage: function onMessage(data) {
+ assert.equal(this.tab.url, data.url, "location is correct");
+ assert.equal(this.tab.title, data.title, "title is correct");
+ if (this.tab.url === url1) {
+ worker1 = this;
+ tabs.open({ url: url2, inBackground: true });
+ }
+ else if (this.tab.url === url2) {
+ mod.destroy();
+ worker1.tab.close(function() {
+ worker1.destroy();
+ worker.tab.close(function() {
+ worker.destroy();
+ done();
+ });
+ });
+ }
+ }
+ });
+ });
+ }
+ });
+
+ tabs.open(url1);
+};
+
+exports.testAutomaticDestroy = function(assert, done) {
+ let loader = Loader(module);
+
+ let pageMod = loader.require("sdk/page-mod").PageMod({
+ include: "about:*",
+ contentScriptWhen: "start",
+ onAttach: function(w) {
+ assert.fail("Page-mod should have been detroyed during module unload");
+ }
+ });
+
+ // Unload the page-mod module so that our page mod is destroyed
+ loader.unload();
+
+ // Then create a second tab to ensure that it is correctly destroyed
+ let tabs = require("sdk/tabs");
+ tabs.open({
+ url: "about:",
+ onReady: function onReady(tab) {
+ assert.pass("check automatic destroy");
+ tab.close(done);
+ }
+ });
+};
+
+exports.testAttachToTabsOnly = function(assert, done) {
+ let { PageMod } = require('sdk/page-mod');
+ let openedTab = null; // Tab opened in openTabWithIframe()
+ let workerCount = 0;
+
+ let mod = PageMod({
+ include: 'data:text/html*',
+ contentScriptWhen: 'start',
+ contentScript: '',
+ onAttach: function onAttach(worker) {
+ if (worker.tab === openedTab) {
+ if (++workerCount == 3) {
+ assert.pass('Succesfully applied to tab documents and its iframe');
+ worker.destroy();
+ mod.destroy();
+ openedTab.close(done);
+ }
+ }
+ else {
+ assert.fail('page-mod attached to a non-tab document');
+ }
+ }
+ });
+
+ function openHiddenFrame() {
+ assert.pass('Open iframe in hidden window');
+ let hiddenFrames = require('sdk/frame/hidden-frame');
+ let hiddenFrame = hiddenFrames.add(hiddenFrames.HiddenFrame({
+ onReady: function () {
+ let element = this.element;
+ element.addEventListener('DOMContentLoaded', function onload() {
+ element.removeEventListener('DOMContentLoaded', onload, false);
+ hiddenFrames.remove(hiddenFrame);
+
+ if (!xulApp.is("Fennec")) {
+ openToplevelWindow();
+ }
+ else {
+ openBrowserIframe();
+ }
+ }, false);
+ element.setAttribute('src', 'data:text/html;charset=utf-8,foo');
+ }
+ }));
+ }
+
+ function openToplevelWindow() {
+ assert.pass('Open toplevel window');
+ let win = open('data:text/html;charset=utf-8,bar');
+ win.addEventListener('DOMContentLoaded', function onload() {
+ win.removeEventListener('DOMContentLoaded', onload, false);
+ win.close();
+ openBrowserIframe();
+ }, false);
+ }
+
+ function openBrowserIframe() {
+ assert.pass('Open iframe in browser window');
+ let window = require('sdk/deprecated/window-utils').activeBrowserWindow;
+ let document = window.document;
+ let iframe = document.createElement('iframe');
+ iframe.setAttribute('type', 'content');
+ iframe.setAttribute('src', 'data:text/html;charset=utf-8,foobar');
+ iframe.addEventListener('DOMContentLoaded', function onload() {
+ iframe.removeEventListener('DOMContentLoaded', onload, false);
+ iframe.parentNode.removeChild(iframe);
+ openTabWithIframes();
+ }, false);
+ document.documentElement.appendChild(iframe);
+ }
+
+ // Only these three documents will be accepted by the page-mod
+ function openTabWithIframes() {
+ assert.pass('Open iframes in a tab');
+ let subContent = '<iframe src="data:text/html;charset=utf-8,sub frame" />'
+ let content = '<iframe src="data:text/html;charset=utf-8,' +
+ encodeURIComponent(subContent) + '" />';
+ require('sdk/tabs').open({
+ url: 'data:text/html;charset=utf-8,' + encodeURIComponent(content),
+ onOpen: function onOpen(tab) {
+ openedTab = tab;
+ }
+ });
+ }
+
+ openHiddenFrame();
+};
+
+exports['test111 attachTo [top]'] = function(assert, done) {
+ let { PageMod } = require('sdk/page-mod');
+
+ let subContent = '<iframe src="data:text/html;charset=utf-8,sub frame" />'
+ let content = '<iframe src="data:text/html;charset=utf-8,' +
+ encodeURIComponent(subContent) + '" />';
+ let topDocumentURL = 'data:text/html;charset=utf-8,' + encodeURIComponent(content)
+
+ let workerCount = 0;
+
+ let mod = PageMod({
+ include: 'data:text/html*',
+ contentScriptWhen: 'start',
+ contentScript: 'self.postMessage(document.location.href);',
+ attachTo: ['top'],
+ onAttach: function onAttach(worker) {
+ if (++workerCount == 1) {
+ worker.on('message', function (href) {
+ assert.equal(href, topDocumentURL,
+ "worker on top level document only");
+ let tab = worker.tab;
+ worker.destroy();
+ mod.destroy();
+ tab.close(done);
+ });
+ }
+ else {
+ assert.fail('page-mod attached to a non-top document');
+ }
+ }
+ });
+
+ require('sdk/tabs').open(topDocumentURL);
+};
+
+exports['test111 attachTo [frame]'] = function(assert, done) {
+ let { PageMod } = require('sdk/page-mod');
+
+ let subFrameURL = 'data:text/html;charset=utf-8,subframe';
+ let subContent = '<iframe src="' + subFrameURL + '" />';
+ let frameURL = 'data:text/html;charset=utf-8,' + encodeURIComponent(subContent);
+ let content = '<iframe src="' + frameURL + '" />';
+ let topDocumentURL = 'data:text/html;charset=utf-8,' + encodeURIComponent(content)
+
+ let workerCount = 0, messageCount = 0;
+
+ function onMessage(href) {
+ if (href == frameURL)
+ assert.pass("worker on first frame");
+ else if (href == subFrameURL)
+ assert.pass("worker on second frame");
+ else
+ assert.fail("worker on unexpected document: " + href);
+ this.destroy();
+ if (++messageCount == 2) {
+ mod.destroy();
+ require('sdk/tabs').activeTab.close(done);
+ }
+ }
+ let mod = PageMod({
+ include: 'data:text/html*',
+ contentScriptWhen: 'start',
+ contentScript: 'self.postMessage(document.location.href);',
+ attachTo: ['frame'],
+ onAttach: function onAttach(worker) {
+ if (++workerCount <= 2) {
+ worker.on('message', onMessage);
+ }
+ else {
+ assert.fail('page-mod attached to a non-frame document');
+ }
+ }
+ });
+
+ require('sdk/tabs').open(topDocumentURL);
+};
+
+exports.testContentScriptOptionsOption = function(assert, done) {
+ let callbackDone = null;
+ testPageMod(assert, done, "about:", [{
+ include: "about:*",
+ contentScript: "self.postMessage( [typeof self.options.d, self.options] );",
+ contentScriptWhen: "end",
+ contentScriptOptions: {a: true, b: [1,2,3], c: "string", d: function(){ return 'test'}},
+ onAttach: function(worker) {
+ worker.on('message', function(msg) {
+ assert.equal( msg[0], 'undefined', 'functions are stripped from contentScriptOptions' );
+ assert.equal( typeof msg[1], 'object', 'object as contentScriptOptions' );
+ assert.equal( msg[1].a, true, 'boolean in contentScriptOptions' );
+ assert.equal( msg[1].b.join(), '1,2,3', 'array and numbers in contentScriptOptions' );
+ assert.equal( msg[1].c, 'string', 'string in contentScriptOptions' );
+ callbackDone();
+ });
+ }
+ }],
+ function(win, done) {
+ callbackDone = done;
+ }
+ );
+};
+
+exports.testPageModCss = function(assert, done) {
+ let [pageMod] = testPageMod(assert, done,
+ 'data:text/html;charset=utf-8,<div style="background: silver">css test</div>', [{
+ include: ["*", "data:*"],
+ contentStyle: "div { height: 100px; }",
+ contentStyleFile: [data.url("include-file.css"), "./border-style.css"]
+ }],
+ function(win, done) {
+ let div = win.document.querySelector("div");
+
+ assert.equal(div.clientHeight, 100,
+ "PageMod contentStyle worked");
+
+ assert.equal(div.offsetHeight, 120,
+ "PageMod contentStyleFile worked");
+
+ assert.equal(win.getComputedStyle(div).borderTopStyle, "dashed",
+ "PageMod contentStyleFile with relative path worked");
+
+ done();
+ }
+ );
+};
+
+exports.testPageModCssList = function*(assert) {
+ const URL = 'data:text/html;charset=utf-8,<div style="width:320px; max-width: 480px!important">css test</div>';
+ let modAttached = defer();
+
+ let pageMod = PageMod({
+ include: "data:*",
+ contentStyleFile: [
+ // Highlight evaluation order in this list
+ "data:text/css;charset=utf-8,div { border: 1px solid black; }",
+ "data:text/css;charset=utf-8,div { border: 10px solid black; }",
+ // Highlight evaluation order between contentStylesheet & contentStylesheetFile
+ "data:text/css;charset=utf-8s,div { height: 1000px; }",
+ // Highlight precedence between the author and user style sheet
+ "data:text/css;charset=utf-8,div { width: 200px; max-width: 640px!important}",
+ ],
+ contentStyle: [
+ "div { height: 10px; }",
+ "div { height: 100px; }"
+ ],
+ contentScript: 'new ' + function WorkerScope() {
+ self.port.on('get-results', () => {
+ let div = window.document.querySelector('div');
+ let style = window.getComputedStyle(div);
+
+ self.port.emit("results", {
+ clientHeight: div.clientHeight,
+ offsetHeight: div.offsetHeight,
+ width: style.width,
+ maxWidth: style.maxWidth
+ });
+ })
+ },
+ onAttach: modAttached.resolve
+ });
+
+ let tab = yield new Promise(resolve => {
+ tabs.open({
+ url: URL,
+ onReady: resolve
+ });
+ });
+ assert.pass("the tab was opened");
+
+ yield modAttached.promise;
+ assert.pass("the mod has been attached");
+
+ let results = yield new Promise(resolve => {
+ pageMod.port.on("results", resolve);
+ pageMod.port.emit("get-results");
+ })
+
+ assert.equal(
+ results.clientHeight,
+ 100,
+ "PageMod contentStyle list works and is evaluated after contentStyleFile"
+ );
+
+ assert.equal(
+ results.offsetHeight,
+ 120,
+ "PageMod contentStyleFile list works"
+ );
+
+ assert.equal(
+ results.width,
+ "320px",
+ "PageMod add-on author/page author style sheet precedence works"
+ );
+
+ assert.equal(
+ results.maxWidth,
+ "480px",
+ "PageMod add-on author/page author style sheet precedence with !important works"
+ );
+
+ pageMod.destroy();
+ assert.pass("the page mod was destroyed");
+};
+
+exports.testPageModCssDestroy = function(assert, done) {
+ let loader = Loader(module);
+
+ tabs.open({
+ url: "data:text/html;charset=utf-8,<div style='width:200px'>css test</div>",
+
+ onReady: function onReady(tab) {
+ let browserWindow = getMostRecentBrowserWindow();
+ let win = getTabContentWindow(getActiveTab(browserWindow));
+
+ let div = win.document.querySelector("div");
+ let style = win.getComputedStyle(div);
+
+ assert.equal(
+ style.width,
+ "200px",
+ "PageMod contentStyle is current before page-mod applies"
+ );
+
+ let pageMod = loader.require("sdk/page-mod").PageMod({
+ include: "data:*",
+ contentStyle: "div { width: 100px!important; }",
+ attachTo: ["top", "existing"],
+ onAttach: function(worker) {
+ assert.equal(
+ style.width,
+ "100px",
+ "PageMod contentStyle worked"
+ );
+
+ worker.once('detach', () => {
+ assert.equal(
+ style.width,
+ "200px",
+ "PageMod contentStyle is removed after page-mod destroy"
+ );
+
+ tab.close(done);
+ });
+
+ pageMod.destroy();
+ }
+ });
+ }
+ });
+};
+
+exports.testPageModCssAutomaticDestroy = function(assert, done) {
+ let loader = Loader(module);
+
+ tabs.open({
+ url: "data:text/html;charset=utf-8,<div style='width:200px'>css test</div>",
+
+ onReady: function onReady(tab) {
+ let browserWindow = getMostRecentBrowserWindow();
+ let win = getTabContentWindow(getActiveTab(browserWindow));
+
+ let div = win.document.querySelector("div");
+ let style = win.getComputedStyle(div);
+
+ assert.equal(
+ style.width,
+ "200px",
+ "PageMod contentStyle is current before page-mod applies"
+ );
+
+ let pageMod = loader.require("sdk/page-mod").PageMod({
+ include: "data:*",
+ contentStyle: "div { width: 100px!important; }",
+ attachTo: ["top", "existing"],
+ onAttach: function(worker) {
+ assert.equal(
+ style.width,
+ "100px",
+ "PageMod contentStyle worked"
+ );
+
+ // Wait for a second page-mod to attach to be sure the unload
+ // message has made it to the child
+ let pageMod2 = PageMod({
+ include: "data:*",
+ contentStyle: "div { width: 100px!important; }",
+ attachTo: ["top", "existing"],
+ onAttach: function(worker) {
+ assert.equal(
+ style.width,
+ "200px",
+ "PageMod contentStyle is removed after page-mod destroy"
+ );
+
+ pageMod2.destroy();
+ tab.close(done);
+ }
+ });
+
+ loader.unload();
+ }
+ });
+ }
+ });
+};
+
+exports.testPageModContentScriptFile = function(assert, done) {
+ let loader = createLoader();
+ let { PageMod } = loader.require("sdk/page-mod");
+
+ tabs.open({
+ url: "about:license",
+ onReady: function(tab) {
+ let mod = PageMod({
+ include: "about:*",
+ attachTo: ["existing", "top"],
+ contentScriptFile: "./test-contentScriptFile.js",
+ onMessage: message => {
+ assert.equal(message, "msg from contentScriptFile",
+ "PageMod contentScriptFile with relative path worked");
+ tab.close(function() {
+ mod.destroy();
+ loader.unload();
+ done();
+ });
+ }
+ });
+ }
+ })
+};
+
+exports.testPageModTimeout = function(assert, done) {
+ let tab = null
+ let loader = Loader(module);
+ let { PageMod } = loader.require("sdk/page-mod");
+
+ let mod = PageMod({
+ include: "data:*",
+ contentScript: Isolate(function() {
+ var id = setTimeout(function() {
+ self.port.emit("fired", id)
+ }, 10)
+ self.port.emit("scheduled", id);
+ }),
+ onAttach: function(worker) {
+ worker.port.on("scheduled", function(id) {
+ assert.pass("timer was scheduled")
+ worker.port.on("fired", function(data) {
+ assert.equal(id, data, "timer was fired")
+ tab.close(function() {
+ worker.destroy()
+ loader.unload()
+ done()
+ });
+ })
+ })
+ }
+ });
+
+ tabs.open({
+ url: "data:text/html;charset=utf-8,timeout",
+ onReady: function($) { tab = $ }
+ })
+}
+
+
+exports.testPageModcancelTimeout = function(assert, done) {
+ let tab = null
+ let loader = Loader(module);
+ let { PageMod } = loader.require("sdk/page-mod");
+
+ let mod = PageMod({
+ include: "data:*",
+ contentScript: Isolate(function() {
+ var id1 = setTimeout(function() {
+ self.port.emit("failed")
+ }, 10)
+ var id2 = setTimeout(function() {
+ self.port.emit("timeout")
+ }, 100)
+ clearTimeout(id1)
+ }),
+ onAttach: function(worker) {
+ worker.port.on("failed", function() {
+ assert.fail("cancelled timeout fired")
+ })
+ worker.port.on("timeout", function(id) {
+ assert.pass("timer was scheduled")
+ tab.close(function() {
+ worker.destroy();
+ mod.destroy();
+ loader.unload();
+ done();
+ });
+ })
+ }
+ });
+
+ tabs.open({
+ url: "data:text/html;charset=utf-8,cancell timeout",
+ onReady: function($) { tab = $ }
+ })
+}
+
+exports.testExistingOnFrames = function(assert, done) {
+ let subFrameURL = 'data:text/html;charset=utf-8,testExistingOnFrames-sub-frame';
+ let subIFrame = '<iframe src="' + subFrameURL + '" />'
+ let iFrameURL = 'data:text/html;charset=utf-8,' + encodeURIComponent(subIFrame)
+ let iFrame = '<iframe src="' + iFrameURL + '" />';
+ let url = 'data:text/html;charset=utf-8,' + encodeURIComponent(iFrame);
+
+ // we want all urls related to the test here, and not just the iframe urls
+ // because we need to fail if the test is applied to the top window url.
+ let urls = [url, iFrameURL, subFrameURL];
+
+ let counter = 0;
+ let tab = openTab(getMostRecentBrowserWindow(), url);
+
+ function wait4Iframes() {
+ let window = getTabContentWindow(tab);
+ if (window.document.readyState != "complete" ||
+ getFrames(window).length != 2) {
+ return;
+ }
+
+ let pagemodOnExisting = PageMod({
+ include: ["*", "data:*"],
+ attachTo: ["existing", "frame"],
+ contentScriptWhen: 'ready',
+ onAttach: function(worker) {
+ // need to ignore urls that are not part of the test, because other
+ // tests are not closing their tabs when they complete..
+ if (urls.indexOf(worker.url) == -1)
+ return;
+
+ assert.notEqual(url,
+ worker.url,
+ 'worker should not be attached to the top window');
+
+ if (++counter < 2) {
+ // we can rely on this order in this case because we are sure that
+ // the frames being tested have completely loaded
+ assert.equal(iFrameURL, worker.url, '1st attach is for top frame');
+ }
+ else if (counter > 2) {
+ assert.fail('applied page mod too many times');
+ }
+ else {
+ assert.equal(subFrameURL, worker.url, '2nd attach is for sub frame');
+ // need timeout because onAttach is called before the constructor returns
+ setTimeout(function() {
+ pagemodOnExisting.destroy();
+ pagemodOffExisting.destroy();
+ closeTab(tab);
+ done();
+ }, 0);
+ }
+ }
+ });
+
+ let pagemodOffExisting = PageMod({
+ include: ["*", "data:*"],
+ attachTo: ["frame"],
+ contentScriptWhen: 'ready',
+ onAttach: function(mod) {
+ assert.fail('pagemodOffExisting page-mod should not have been attached');
+ }
+ });
+ }
+
+ getBrowserForTab(tab).addEventListener("load", wait4Iframes, true);
+};
+
+exports.testIFramePostMessage = function(assert, done) {
+ let count = 0;
+
+ tabs.open({
+ url: data.url("test-iframe.html"),
+ onReady: function(tab) {
+ var worker = tab.attach({
+ contentScriptFile: data.url('test-iframe.js'),
+ contentScript: 'var iframePath = \'' + data.url('test-iframe-postmessage.html') + '\'',
+ onMessage: function(msg) {
+ assert.equal(++count, 1);
+ assert.equal(msg.first, 'a string');
+ assert.ok(msg.second[1], "array");
+ assert.equal(typeof msg.third, 'object');
+
+ worker.destroy();
+ tab.close(done);
+ }
+ });
+ }
+ });
+};
+
+exports.testEvents = function*(assert) {
+ let modAttached = defer();
+ let content = "<script>\n new " + function DocumentScope() {
+ window.addEventListener("ContentScriptEvent", function () {
+ window.document.body.setAttribute("receivedEvent", "ok");
+ }, false);
+ } + "\n</script>";
+ let url = "data:text/html;charset=utf-8," + encodeURIComponent(content);
+
+ let mod = PageMod({
+ include: "data:*",
+ contentScript: 'new ' + function WorkerScope() {
+ let evt = document.createEvent("Event");
+ evt.initEvent("ContentScriptEvent", true, true);
+ document.body.dispatchEvent(evt);
+
+ self.port.on("get-result", () => {
+ self.port.emit("result", {
+ receivedEvent: window.document.body.getAttribute("receivedEvent")
+ });
+ });
+ },
+ onAttach: modAttached.resolve
+ });
+
+ let tab = yield new Promise(resolve => {
+ tabs.open({
+ url: url,
+ onReady: resolve
+ });
+ });
+ assert.pass("the tab is ready");
+
+ yield modAttached.promise;
+ assert.pass("the mod was attached")
+
+ let result = yield new Promise(resolve => {
+ mod.port.once("result", resolve);
+ mod.port.emit("get-result");
+ });
+
+ assert.equal(result.receivedEvent, "ok",
+ "Content script sent an event and document received it");
+};
+
+exports["test page-mod on private tab"] = function (assert, done) {
+ let fail = assert.fail.bind(assert);
+
+ let privateUri = "data:text/html;charset=utf-8," +
+ "<iframe src=\"data:text/html;charset=utf-8,frame\" />";
+ let nonPrivateUri = "data:text/html;charset=utf-8,non-private";
+
+ let pageMod = new PageMod({
+ include: "data:*",
+ onAttach: function(worker) {
+ if (isTabPBSupported || isWindowPBSupported) {
+ // When PB isn't supported, the page-mod will apply to all document
+ // as all of them will be non-private
+ assert.equal(worker.tab.url,
+ nonPrivateUri,
+ "page-mod should only attach to the non-private tab");
+ }
+
+ assert.ok(!isPrivate(worker),
+ "The worker is really non-private");
+ assert.ok(!isPrivate(worker.tab),
+ "The document is really non-private");
+ pageMod.destroy();
+
+ page1.close().
+ then(page2.close).
+ then(done, fail);
+ }
+ });
+
+ let page1, page2;
+ page1 = openWebpage(privateUri, true);
+ page1.ready.then(function() {
+ page2 = openWebpage(nonPrivateUri, false);
+ }, fail);
+}
+
+// Bug 699450: Calling worker.tab.close() should not lead to exception
+exports.testWorkerTabClose = function(assert, done) {
+ let callbackDone;
+ testPageMod(assert, done, "about:", [{
+ include: "about:",
+ contentScript: '',
+ onAttach: function(worker) {
+ assert.pass("The page-mod was attached");
+
+ worker.tab.close(function () {
+ // On Fennec, tab is completely destroyed right after close event is
+ // dispatch, so we need to wait for the next event loop cycle to
+ // check for tab nulliness.
+ setTimeout(function () {
+ assert.ok(!worker.tab,
+ "worker.tab should be null right after tab.close()");
+ callbackDone();
+ }, 0);
+ });
+ }
+ }],
+ function(win, done) {
+ callbackDone = done;
+ }
+ );
+};
+
+exports.testDetachOnDestroy = function(assert, done) {
+ let tab;
+ const TEST_URL = 'data:text/html;charset=utf-8,detach';
+ const loader = Loader(module);
+ const { PageMod } = loader.require('sdk/page-mod');
+
+ let mod1 = PageMod({
+ include: TEST_URL,
+ contentScript: Isolate(function() {
+ self.port.on('detach', function(reason) {
+ window.document.body.innerHTML += '!' + reason;
+ });
+ }),
+ onAttach: worker => {
+ assert.pass('attach[1] happened');
+
+ worker.on('detach', _ => setTimeout(_ => {
+ assert.pass('detach happened');
+
+ let mod2 = PageMod({
+ attachTo: [ 'existing', 'top' ],
+ include: TEST_URL,
+ contentScript: Isolate(function() {
+ self.port.on('test', _ => {
+ self.port.emit('result', { result: window.document.body.innerHTML});
+ });
+ }),
+ onAttach: worker => {
+ assert.pass('attach[2] happened');
+ worker.port.once('result', ({ result }) => {
+ assert.equal(result, 'detach!', 'the body.innerHTML is as expected');
+ mod1.destroy();
+ mod2.destroy();
+ loader.unload();
+ tab.close(done);
+ });
+ worker.port.emit('test');
+ }
+ });
+ }));
+
+ worker.destroy();
+ }
+ });
+
+ tabs.open({
+ url: TEST_URL,
+ onOpen: t => tab = t
+ })
+}
+
+exports.testDetachOnUnload = function(assert, done) {
+ let tab;
+ const TEST_URL = 'data:text/html;charset=utf-8,detach';
+ const loader = Loader(module);
+ const { PageMod } = loader.require('sdk/page-mod');
+
+ let mod1 = PageMod({
+ include: TEST_URL,
+ contentScript: Isolate(function() {
+ self.port.on('detach', function(reason) {
+ window.document.body.innerHTML += '!' + reason;
+ });
+ }),
+ onAttach: worker => {
+ assert.pass('attach[1] happened');
+
+ worker.on('detach', _ => setTimeout(_ => {
+ assert.pass('detach happened');
+
+ let mod2 = require('sdk/page-mod').PageMod({
+ attachTo: [ 'existing', 'top' ],
+ include: TEST_URL,
+ contentScript: Isolate(function() {
+ self.port.on('test', _ => {
+ self.port.emit('result', { result: window.document.body.innerHTML});
+ });
+ }),
+ onAttach: worker => {
+ assert.pass('attach[2] happened');
+ worker.port.once('result', ({ result }) => {
+ assert.equal(result, 'detach!shutdown', 'the body.innerHTML is as expected');
+ mod2.destroy();
+ tab.close(done);
+ });
+ worker.port.emit('test');
+ }
+ });
+ }));
+
+ loader.unload('shutdown');
+ }
+ });
+
+ tabs.open({
+ url: TEST_URL,
+ onOpen: t => tab = t
+ })
+}
+
+exports.testConsole = function(assert, done) {
+ let innerID;
+ const TEST_URL = 'data:text/html;charset=utf-8,console';
+
+ let seenMessage = false;
+
+ system.on('console-api-log-event', onMessage);
+
+ function onMessage({ subject: { wrappedJSObject: msg }}) {
+ if (msg.arguments[0] !== "Hello from the page mod")
+ return;
+ seenMessage = true;
+ innerID = msg.innerID;
+ }
+
+ let mod = PageMod({
+ include: TEST_URL,
+ contentScriptWhen: "ready",
+ contentScript: Isolate(function() {
+ console.log("Hello from the page mod");
+ self.port.emit("done");
+ }),
+ onAttach: function(worker) {
+ worker.port.on("done", function() {
+ let window = getTabContentWindow(tab);
+ let id = getInnerId(window);
+ assert.ok(seenMessage, "Should have seen the console message");
+ assert.equal(innerID, id, "Should have seen the right inner ID");
+
+ system.off('console-api-log-event', onMessage);
+ mod.destroy();
+ closeTab(tab);
+ done();
+ });
+ },
+ });
+
+ let tab = openTab(getMostRecentBrowserWindow(), TEST_URL);
+}
+
+exports.testSyntaxErrorInContentScript = function *(assert) {
+ const url = "data:text/html;charset=utf-8,testSyntaxErrorInContentScript";
+ const loader = createLoader();
+ const { PageMod } = loader.require("sdk/page-mod");
+ let attached = defer();
+ let errored = defer();
+
+ let mod = PageMod({
+ include: url,
+ contentScript: 'console.log(23',
+ onAttach: attached.resolve,
+ onError: errored.resolve
+ });
+ openNewTab(url);
+
+ yield attached.promise;
+ let hitError = yield errored.promise;
+
+ assert.notStrictEqual(hitError, null, "The syntax error was reported.");
+ assert.equal(hitError.name, "SyntaxError", "The error thrown should be a SyntaxError");
+
+ loader.unload();
+ yield cleanUI();
+};
+
+exports.testPageShowWhenStart = function(assert, done) {
+ const TEST_URL = 'data:text/html;charset=utf-8,detach';
+ let sawWorkerPageShow = false;
+ let sawInjected = false;
+ let sawContentScriptPageShow = false;
+
+ let mod = PageMod({
+ include: TEST_URL,
+ contentScriptWhen: 'start',
+ contentScript: Isolate(function() {
+ self.port.emit("injected");
+ self.on("pageshow", () => {
+ self.port.emit("pageshow");
+ });
+ }),
+ onAttach: worker => {
+ worker.port.on("injected", () => {
+ sawInjected = true;
+ });
+
+ worker.port.on("pageshow", () => {
+ sawContentScriptPageShow = true;
+ closeTab(tab);
+ });
+
+ worker.on("pageshow", () => {
+ sawWorkerPageShow = true;
+ });
+
+ worker.on("detach", () => {
+ assert.ok(sawWorkerPageShow, "Worker emitted pageshow");
+ assert.ok(sawInjected, "Content script ran");
+ assert.ok(sawContentScriptPageShow, "Content script saw pageshow");
+ mod.destroy();
+ done();
+ });
+ }
+ });
+
+ let tab = openTab(getMostRecentBrowserWindow(), TEST_URL);
+};
+
+exports.testPageShowWhenReady = function(assert, done) {
+ const TEST_URL = 'data:text/html;charset=utf-8,detach';
+ let sawWorkerPageShow = false;
+ let sawInjected = false;
+ let sawContentScriptPageShow = false;
+
+ let mod = PageMod({
+ include: TEST_URL,
+ contentScriptWhen: 'ready',
+ contentScript: Isolate(function() {
+ self.port.emit("injected");
+ self.on("pageshow", () => {
+ self.port.emit("pageshow");
+ });
+ }),
+ onAttach: worker => {
+ worker.port.on("injected", () => {
+ sawInjected = true;
+ });
+
+ worker.port.on("pageshow", () => {
+ sawContentScriptPageShow = true;
+ closeTab(tab);
+ });
+
+ worker.on("pageshow", () => {
+ sawWorkerPageShow = true;
+ });
+
+ worker.on("detach", () => {
+ assert.ok(sawWorkerPageShow, "Worker emitted pageshow");
+ assert.ok(sawInjected, "Content script ran");
+ assert.ok(sawContentScriptPageShow, "Content script saw pageshow");
+ mod.destroy();
+ done();
+ });
+ }
+ });
+
+ let tab = openTab(getMostRecentBrowserWindow(), TEST_URL);
+};
+
+exports.testPageShowWhenEnd = function(assert, done) {
+ const TEST_URL = 'data:text/html;charset=utf-8,detach';
+ let sawWorkerPageShow = false;
+ let sawInjected = false;
+ let sawContentScriptPageShow = false;
+
+ let mod = PageMod({
+ include: TEST_URL,
+ contentScriptWhen: 'end',
+ contentScript: Isolate(function() {
+ self.port.emit("injected");
+ self.on("pageshow", () => {
+ self.port.emit("pageshow");
+ });
+ }),
+ onAttach: worker => {
+ worker.port.on("injected", () => {
+ sawInjected = true;
+ });
+
+ worker.port.on("pageshow", () => {
+ sawContentScriptPageShow = true;
+ closeTab(tab);
+ });
+
+ worker.on("pageshow", () => {
+ sawWorkerPageShow = true;
+ });
+
+ worker.on("detach", () => {
+ assert.ok(sawWorkerPageShow, "Worker emitted pageshow");
+ assert.ok(sawInjected, "Content script ran");
+ assert.ok(sawContentScriptPageShow, "Content script saw pageshow");
+ mod.destroy();
+ done();
+ });
+ }
+ });
+
+ let tab = openTab(getMostRecentBrowserWindow(), TEST_URL);
+};
+
+// Tests that after destroy existing workers have been destroyed
+exports.testDestroyKillsChild = function(assert, done) {
+ const TEST_URL = 'data:text/html;charset=utf-8,detach';
+
+ let mod1 = PageMod({
+ include: TEST_URL,
+ contentScriptWhen: 'end',
+ contentScript: Isolate(function() {
+ self.port.on("ping", detail => {
+ let event = document.createEvent("CustomEvent");
+ event.initCustomEvent("Test:Ping", true, true, detail);
+ document.dispatchEvent(event);
+ self.port.emit("pingsent");
+ });
+
+ let listener = function(event) {
+ self.port.emit("pong", event.detail);
+ };
+
+ self.port.on("detach", () => {
+ window.removeEventListener("Test:Pong", listener);
+ });
+ window.addEventListener("Test:Pong", listener);
+ }),
+ onAttach: worker1 => {
+ let mod2 = PageMod({
+ include: TEST_URL,
+ attachTo: ["top", "existing"],
+ contentScriptWhen: 'end',
+ contentScript: Isolate(function() {
+ let listener = function(event) {
+ let newEvent = document.createEvent("CustomEvent");
+ newEvent.initCustomEvent("Test:Pong", true, true, event.detail);
+ document.dispatchEvent(newEvent);
+ };
+ self.port.on("detach", () => {
+ window.removeEventListener("Test:Ping", listener);
+ })
+ window.addEventListener("Test:Ping", listener);
+ self.postMessage();
+ }),
+ onAttach: worker2 => {
+ worker1.port.emit("ping", "test1");
+ worker1.port.once("pong", detail => {
+ assert.equal(detail, "test1", "Saw the right message");
+ worker1.port.once("pingsent", () => {
+ assert.pass("The message was sent");
+
+ mod2.destroy();
+
+ worker1.port.emit("ping", "test2");
+ worker1.port.once("pong", detail => {
+ assert.fail("worker2 shouldn't have responded");
+ })
+ worker1.port.once("pingsent", () => {
+ assert.pass("The message was sent");
+ mod1.destroy();
+ closeTab(tab);
+ done();
+ });
+ });
+ })
+ }
+ });
+ }
+ });
+
+ let tab = openTab(getMostRecentBrowserWindow(), TEST_URL);
+}
+
+// Tests that after destroy child page-mod won't attach
+exports.testDestroyWontAttach = function(assert, done) {
+ const TEST_URL = 'data:text/html;charset=utf-8,detach';
+
+ let badMod = PageMod({
+ include: TEST_URL,
+ contentScriptWhen: 'start',
+ contentScript: Isolate(function() {
+ unsafeWindow.testProperty = "attached";
+ })
+ });
+ badMod.destroy();
+
+ let mod = PageMod({
+ include: TEST_URL,
+ contentScriptWhen: 'end',
+ contentScript: Isolate(function() {
+ self.postMessage(unsafeWindow.testProperty);
+ }),
+ onMessage: property => {
+ assert.equal(property, undefined, "Shouldn't have seen the test property set.");
+ mod.destroy();
+ closeTab(tab);
+ done();
+ }
+ });
+
+ let tab = openTab(getMostRecentBrowserWindow(), TEST_URL);
+}
+
+// Tests that after unload existing workers have been destroyed
+exports.testUnloadKillsChild = function(assert, done) {
+ const TEST_URL = 'data:text/html;charset=utf-8,detach';
+
+ let mod1 = PageMod({
+ include: TEST_URL,
+ contentScriptWhen: 'end',
+ contentScript: Isolate(function() {
+ self.port.on("ping", detail => {
+ let event = document.createEvent("CustomEvent");
+ event.initCustomEvent("Test:Ping", true, true, detail);
+ document.dispatchEvent(event);
+ self.port.emit("pingsent");
+ });
+
+ let listener = function(event) {
+ self.port.emit("pong", event.detail);
+ };
+
+ self.port.on("detach", () => {
+ window.removeEventListener("Test:Pong", listener);
+ });
+ window.addEventListener("Test:Pong", listener);
+ }),
+ onAttach: worker1 => {
+ let loader = Loader(module);
+ let mod2 = loader.require('sdk/page-mod').PageMod({
+ include: TEST_URL,
+ attachTo: ["top", "existing"],
+ contentScriptWhen: 'end',
+ contentScript: Isolate(function() {
+ let listener = function(event) {
+ let newEvent = document.createEvent("CustomEvent");
+ newEvent.initCustomEvent("Test:Pong", true, true, event.detail);
+ document.dispatchEvent(newEvent);
+ };
+ self.port.on("detach", () => {
+ window.removeEventListener("Test:Ping", listener);
+ })
+ window.addEventListener("Test:Ping", listener);
+ self.postMessage();
+ }),
+ onAttach: worker2 => {
+ worker1.port.emit("ping", "test1");
+ worker1.port.once("pong", detail => {
+ assert.equal(detail, "test1", "Saw the right message");
+ worker1.port.once("pingsent", () => {
+ assert.pass("The message was sent");
+
+ loader.unload();
+
+ worker1.port.emit("ping", "test2");
+ worker1.port.once("pong", detail => {
+ assert.fail("worker2 shouldn't have responded");
+ })
+ worker1.port.once("pingsent", () => {
+ assert.pass("The message was sent");
+ mod1.destroy();
+ closeTab(tab);
+ done();
+ });
+ });
+ })
+ }
+ });
+ }
+ });
+
+ let tab = openTab(getMostRecentBrowserWindow(), TEST_URL);
+}
+
+// Tests that after unload child page-mod won't attach
+exports.testUnloadWontAttach = function(assert, done) {
+ const TEST_URL = 'data:text/html;charset=utf-8,detach';
+
+ let loader = Loader(module);
+ let badMod = loader.require('sdk/page-mod').PageMod({
+ include: TEST_URL,
+ contentScriptWhen: 'start',
+ contentScript: Isolate(function() {
+ unsafeWindow.testProperty = "attached";
+ })
+ });
+ loader.unload();
+
+ let mod = PageMod({
+ include: TEST_URL,
+ contentScriptWhen: 'end',
+ contentScript: Isolate(function() {
+ self.postMessage(unsafeWindow.testProperty);
+ }),
+ onMessage: property => {
+ assert.equal(property, undefined, "Shouldn't have seen the test property set.");
+ mod.destroy();
+ closeTab(tab);
+ done();
+ }
+ });
+
+ let tab = openTab(getMostRecentBrowserWindow(), TEST_URL);
+}
+
+// Tests that the SDK console isn't injected into documents loaded in tabs
+exports.testDontInjectConsole = function(assert, done) {
+ const TEST_URL = 'data:text/html;charset=utf-8,consoleinject';
+
+ let loader = Loader(module);
+
+ let mod = PageMod({
+ include: TEST_URL,
+ contentScript: Isolate(function() {
+ // This relies on the fact that the SDK console doesn't have assert defined
+ self.postMessage((typeof unsafeWindow.console.assert) == "function");
+ }),
+ onMessage: isNativeConsole => {
+ assert.ok(isNativeConsole, "Shouldn't have injected the SDK console.");
+ mod.destroy();
+ closeTab(tab);
+ done();
+ }
+ });
+
+ let tab = openTab(getMostRecentBrowserWindow(), TEST_URL);
+}
+
+after(exports, function*(name, assert) {
+ assert.pass("cleaning ui.");
+ yield cleanUI();
+});
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-page-worker.js b/addon-sdk/source/test/test-page-worker.js
new file mode 100644
index 000000000..a8cd2492f
--- /dev/null
+++ b/addon-sdk/source/test/test-page-worker.js
@@ -0,0 +1,558 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Loader } = require('sdk/test/loader');
+const { Page } = require("sdk/page-worker");
+const { URL } = require("sdk/url");
+const fixtures = require("./fixtures");
+const testURI = fixtures.url("test.html");
+const { getActiveView } = require("sdk/view/core");
+const { getDocShell } = require('sdk/frame/utils');
+
+const ERR_DESTROYED =
+ "Couldn't find the worker to receive this message. " +
+ "The script may not be initialized yet, or may already have been unloaded.";
+
+const Isolate = fn => "(" + fn + ")()";
+
+exports.testSimplePageCreation = function(assert, done) {
+ let page = new Page({
+ contentScript: "self.postMessage(window.location.href)",
+ contentScriptWhen: "end",
+ onMessage: function (message) {
+ assert.equal(message, "about:blank",
+ "Page Worker should start with a blank page by default");
+ assert.equal(this, page, "The 'this' object is the page itself.");
+ done();
+ }
+ });
+}
+
+/*
+ * Tests that we can't be tricked by document overloads as we have access
+ * to wrapped nodes
+ */
+exports.testWrappedDOM = function(assert, done) {
+ let page = Page({
+ allow: { script: true },
+ contentURL: "data:text/html;charset=utf-8,<script>document.getElementById=3;window.scrollTo=3;</script>",
+ contentScript: 'new ' + function() {
+ function send() {
+ self.postMessage([typeof(document.getElementById), typeof(window.scrollTo)]);
+ }
+ if (document.readyState !== 'complete')
+ window.addEventListener('load', send, true)
+ else
+ send();
+ },
+ onMessage: function (message) {
+ assert.equal(message[0],
+ "function",
+ "getElementById from content script is the native one");
+
+ assert.equal(message[1],
+ "function",
+ "scrollTo from content script is the native one");
+
+ done();
+ }
+ });
+}
+
+/*
+// We do not offer unwrapped access to DOM since bug 601295 landed
+// See 660780 to track progress of unwrap feature
+exports.testUnwrappedDOM = function(assert, done) {
+ let page = Page({
+ allow: { script: true },
+ contentURL: "data:text/html;charset=utf-8,<script>document.getElementById=3;window.scrollTo=3;</script>",
+ contentScript: "window.addEventListener('load', function () {" +
+ "return self.postMessage([typeof(unsafeWindow.document.getElementById), " +
+ "typeof(unsafeWindow.scrollTo)]); }, true)",
+ onMessage: function (message) {
+ assert.equal(message[0],
+ "number",
+ "document inside page is free to be changed");
+
+ assert.equal(message[1],
+ "number",
+ "window inside page is free to be changed");
+
+ done();
+ }
+ });
+}
+*/
+
+exports.testPageProperties = function(assert) {
+ let page = new Page();
+
+ for (let prop of ['contentURL', 'allow', 'contentScriptFile',
+ 'contentScript', 'contentScriptWhen', 'on',
+ 'postMessage', 'removeListener']) {
+ assert.ok(prop in page, prop + " property is defined on page.");
+ }
+
+ assert.ok(() => page.postMessage("foo") || true,
+ "postMessage doesn't throw exception on page.");
+}
+
+exports.testConstructorAndDestructor = function(assert, done) {
+ let loader = Loader(module);
+ let { Page } = loader.require("sdk/page-worker");
+ let global = loader.sandbox("sdk/page-worker");
+
+ let pagesReady = 0;
+
+ let page1 = Page({
+ contentScript: "self.postMessage('')",
+ contentScriptWhen: "end",
+ onMessage: pageReady
+ });
+ let page2 = Page({
+ contentScript: "self.postMessage('')",
+ contentScriptWhen: "end",
+ onMessage: pageReady
+ });
+
+ assert.notEqual(page1, page2,
+ "Page 1 and page 2 should be different objects.");
+
+ function pageReady() {
+ if (++pagesReady == 2) {
+ page1.destroy();
+ page2.destroy();
+
+ assert.ok(isDestroyed(page1), "page1 correctly unloaded.");
+ assert.ok(isDestroyed(page2), "page2 correctly unloaded.");
+
+ loader.unload();
+ done();
+ }
+ }
+}
+
+exports.testAutoDestructor = function(assert, done) {
+ let loader = Loader(module);
+ let { Page } = loader.require("sdk/page-worker");
+
+ let page = Page({
+ contentScript: "self.postMessage('')",
+ contentScriptWhen: "end",
+ onMessage: function() {
+ loader.unload();
+ assert.ok(isDestroyed(page), "Page correctly unloaded.");
+ done();
+ }
+ });
+}
+
+exports.testValidateOptions = function(assert) {
+ assert.throws(
+ () => Page({ contentURL: 'home' }),
+ /The `contentURL` option must be a valid URL\./,
+ "Validation correctly denied a non-URL contentURL"
+ );
+
+ assert.throws(
+ () => Page({ onMessage: "This is not a function."}),
+ /The option "onMessage" must be one of the following types: function/,
+ "Validation correctly denied a non-function onMessage."
+ );
+
+ assert.pass("Options validation is working.");
+}
+
+exports.testContentAndAllowGettersAndSetters = function(assert, done) {
+ let content = "data:text/html;charset=utf-8,<script>window.localStorage.allowScript=3;</script>";
+
+ // Load up the page with testURI initially for the resource:// principal,
+ // then load the actual data:* content, as data:* URIs no longer
+ // have localStorage
+ let page = Page({
+ contentURL: testURI,
+ contentScript: "if (window.location.href==='"+testURI+"')" +
+ " self.postMessage('reload');" +
+ "else " +
+ " self.postMessage(window.localStorage.allowScript)",
+ contentScriptWhen: "end",
+ onMessage: step0
+ });
+
+ function step0(message) {
+ if (message === 'reload')
+ return page.contentURL = content;
+ assert.equal(message, "3",
+ "Correct value expected for allowScript - 3");
+ assert.equal(page.contentURL, content,
+ "Correct content expected");
+ page.removeListener('message', step0);
+ page.on('message', step1);
+ page.allow = { script: false };
+ page.contentURL = content =
+ "data:text/html;charset=utf-8,<script>window.localStorage.allowScript='f'</script>";
+ }
+
+ function step1(message) {
+ assert.equal(message, "3",
+ "Correct value expected for allowScript - 3");
+ assert.equal(page.contentURL, content, "Correct content expected");
+ page.removeListener('message', step1);
+ page.on('message', step2);
+ page.allow = { script: true };
+ page.contentURL = content =
+ "data:text/html;charset=utf-8,<script>window.localStorage.allowScript='g'</script>";
+ }
+
+ function step2(message) {
+ assert.equal(message, "g",
+ "Correct value expected for allowScript - g");
+ assert.equal(page.contentURL, content, "Correct content expected");
+ page.removeListener('message', step2);
+ page.on('message', step3);
+ page.allow.script = false;
+ page.contentURL = content =
+ "data:text/html;charset=utf-8,<script>window.localStorage.allowScript=3</script>";
+ }
+
+ function step3(message) {
+ assert.equal(message, "g",
+ "Correct value expected for allowScript - g");
+ assert.equal(page.contentURL, content, "Correct content expected");
+ page.removeListener('message', step3);
+ page.on('message', step4);
+ page.allow.script = true;
+ page.contentURL = content =
+ "data:text/html;charset=utf-8,<script>window.localStorage.allowScript=4</script>";
+ }
+
+ function step4(message) {
+ assert.equal(message, "4",
+ "Correct value expected for allowScript - 4");
+ assert.equal(page.contentURL, content, "Correct content expected");
+ done();
+ }
+
+}
+
+exports.testOnMessageCallback = function(assert, done) {
+ Page({
+ contentScript: "self.postMessage('')",
+ contentScriptWhen: "end",
+ onMessage: function() {
+ assert.pass("onMessage callback called");
+ done();
+ }
+ });
+}
+
+exports.testMultipleOnMessageCallbacks = function(assert, done) {
+ let count = 0;
+ let page = Page({
+ contentScript: "self.postMessage('')",
+ contentScriptWhen: "end",
+ onMessage: () => count += 1
+ });
+ page.on('message', () => count += 2);
+ page.on('message', () => count *= 3);
+ page.on('message', () =>
+ assert.equal(count, 9, "All callbacks were called, in order."));
+ page.on('message', done);
+};
+
+exports.testLoadContentPage = function(assert, done) {
+ let page = Page({
+ onMessage: function(message) {
+ // The message is an array whose first item is the test method to call
+ // and the rest of whose items are arguments to pass it.
+ let msg = message.shift();
+ if (msg == "done")
+ return done();
+ assert[msg].apply(assert, message);
+ },
+ contentURL: fixtures.url("addon-sdk/data/test-page-worker.html"),
+ contentScriptFile: fixtures.url("addon-sdk/data/test-page-worker.js"),
+ contentScriptWhen: "ready"
+ });
+}
+
+exports.testLoadContentPageRelativePath = function(assert, done) {
+ const self = require("sdk/self");
+ const { merge } = require("sdk/util/object");
+
+ const options = merge({}, require('@loader/options'),
+ { id: "testloader", prefixURI: require('./fixtures').url() });
+
+ let loader = Loader(module, null, options);
+
+ let page = loader.require("sdk/page-worker").Page({
+ onMessage: function(message) {
+ // The message is an array whose first item is the test method to call
+ // and the rest of whose items are arguments to pass it.
+ let msg = message.shift();
+ if (msg == "done")
+ return done();
+ assert[msg].apply(assert, message);
+ },
+ contentURL: "./test-page-worker.html",
+ contentScriptFile: "./test-page-worker.js",
+ contentScriptWhen: "ready"
+ });
+}
+
+exports.testAllowScriptDefault = function(assert, done) {
+ let page = Page({
+ onMessage: function(message) {
+ assert.ok(message, "Script is allowed to run by default.");
+ done();
+ },
+ contentURL: "data:text/html;charset=utf-8,<script>document.documentElement.setAttribute('foo', 3);</script>",
+ contentScript: "self.postMessage(document.documentElement.getAttribute('foo'))",
+ contentScriptWhen: "ready"
+ });
+}
+
+exports.testAllowScript = function(assert, done) {
+ let page = Page({
+ onMessage: function(message) {
+ assert.ok(message, "Script runs when allowed to do so.");
+ page.destroy();
+ done();
+ },
+ allow: { script: true },
+ contentURL: "data:text/html;charset=utf-8,<script>document.documentElement.setAttribute('foo', 3);</script>",
+ contentScript: "self.postMessage(document.documentElement.hasAttribute('foo') && " +
+ " document.documentElement.getAttribute('foo') == 3)",
+ contentScriptWhen: "ready"
+ });
+}
+
+exports.testPingPong = function(assert, done) {
+ let page = Page({
+ contentURL: 'data:text/html;charset=utf-8,ping-pong',
+ contentScript: 'self.on("message", message => self.postMessage("pong"));'
+ + 'self.postMessage("ready");',
+ onMessage: function(message) {
+ if ('ready' == message) {
+ page.postMessage('ping');
+ }
+ else {
+ assert.ok(message, 'pong', 'Callback from contentScript');
+ done();
+ }
+ }
+ });
+};
+
+exports.testRedirect = function (assert, done) {
+ let page = Page({
+ contentURL: 'data:text/html;charset=utf-8,first-page',
+ contentScriptWhen: "end",
+ contentScript: '' +
+ 'if (/first-page/.test(document.location.href)) ' +
+ ' document.location.href = "data:text/html;charset=utf-8,redirect";' +
+ 'else ' +
+ ' self.port.emit("redirect", document.location.href);'
+ });
+
+ page.port.on('redirect', function (url) {
+ assert.equal(url, 'data:text/html;charset=utf-8,redirect', 'Reinjects contentScript on reload');
+ done();
+ });
+};
+
+exports.testRedirectIncludeArrays = function (assert, done) {
+ let firstURL = 'data:text/html;charset=utf-8,first-page';
+ let page = Page({
+ contentURL: firstURL,
+ contentScript: '(function () {' +
+ 'self.port.emit("load", document.location.href);' +
+ ' self.port.on("redirect", function (url) {' +
+ ' document.location.href = url;' +
+ ' })' +
+ '})();',
+ include: ['about:blank', 'data:*']
+ });
+
+ page.port.on('load', function (url) {
+ if (url === firstURL) {
+ page.port.emit('redirect', 'about:blank');
+ } else if (url === 'about:blank') {
+ page.port.emit('redirect', 'about:mozilla');
+ assert.ok('`include` property handles arrays');
+ assert.equal(url, 'about:blank', 'Redirects work with accepted domains');
+ done();
+ } else if (url === 'about:mozilla') {
+ assert.fail('Should not redirect to restricted domain');
+ }
+ });
+};
+
+exports.testRedirectFromWorker = function (assert, done) {
+ let firstURL = 'data:text/html;charset=utf-8,first-page';
+ let secondURL = 'data:text/html;charset=utf-8,second-page';
+ let thirdURL = 'data:text/html;charset=utf-8,third-page';
+ let page = Page({
+ contentURL: firstURL,
+ contentScript: '(function () {' +
+ 'self.port.emit("load", document.location.href);' +
+ ' self.port.on("redirect", function (url) {' +
+ ' document.location.href = url;' +
+ ' })' +
+ '})();',
+ include: 'data:*'
+ });
+
+ page.port.on('load', function (url) {
+ if (url === firstURL) {
+ page.port.emit('redirect', secondURL);
+ } else if (url === secondURL) {
+ page.port.emit('redirect', thirdURL);
+ } else if (url === thirdURL) {
+ page.port.emit('redirect', 'about:mozilla');
+ assert.equal(url, thirdURL, 'Redirects work with accepted domains on include strings');
+ done();
+ } else {
+ assert.fail('Should not redirect to unauthorized domains');
+ }
+ });
+};
+
+exports.testRedirectWithContentURL = function (assert, done) {
+ let firstURL = 'data:text/html;charset=utf-8,first-page';
+ let secondURL = 'data:text/html;charset=utf-8,second-page';
+ let thirdURL = 'data:text/html;charset=utf-8,third-page';
+ let page = Page({
+ contentURL: firstURL,
+ contentScript: '(function () {' +
+ 'self.port.emit("load", document.location.href);' +
+ '})();',
+ include: 'data:*'
+ });
+
+ page.port.on('load', function (url) {
+ if (url === firstURL) {
+ page.contentURL = secondURL;
+ } else if (url === secondURL) {
+ page.contentURL = thirdURL;
+ } else if (url === thirdURL) {
+ page.contentURL = 'about:mozilla';
+ assert.equal(url, thirdURL, 'Redirects work with accepted domains on include strings');
+ done();
+ } else {
+ assert.fail('Should not redirect to unauthorized domains');
+ }
+ });
+};
+
+
+exports.testMultipleDestroys = function(assert) {
+ let page = Page();
+ page.destroy();
+ page.destroy();
+ assert.pass("Multiple destroys should not cause an error");
+};
+
+exports.testContentScriptOptionsOption = function(assert, done) {
+ let page = new Page({
+ contentScript: "self.postMessage( [typeof self.options.d, self.options] );",
+ contentScriptWhen: "end",
+ contentScriptOptions: {a: true, b: [1,2,3], c: "string", d: function(){ return 'test'}},
+ onMessage: function(msg) {
+ assert.equal(msg[0], 'undefined', 'functions are stripped from contentScriptOptions');
+ assert.equal(typeof msg[1], 'object', 'object as contentScriptOptions');
+ assert.equal(msg[1].a, true, 'boolean in contentScriptOptions');
+ assert.equal(msg[1].b.join(), '1,2,3', 'array and numbers in contentScriptOptions');
+ assert.equal(msg[1].c, 'string', 'string in contentScriptOptions');
+ done();
+ }
+ });
+};
+
+exports.testMessageQueue = function (assert, done) {
+ let page = new Page({
+ contentScript: 'self.on("message", function (m) {' +
+ 'self.postMessage(m);' +
+ '});',
+ contentURL: 'data:text/html;charset=utf-8,',
+ });
+ page.postMessage('ping');
+ page.on('message', function (m) {
+ assert.equal(m, 'ping', 'postMessage should queue messages');
+ done();
+ });
+};
+
+exports.testWindowStopDontBreak = function (assert, done) {
+ const { Ci, Cc } = require('chrome');
+ const consoleService = Cc['@mozilla.org/consoleservice;1'].
+ getService(Ci.nsIConsoleService);
+ const listener = {
+ observe: ({message}) => {
+ if (message.includes('contentWorker is null'))
+ assert.fail('contentWorker is null');
+ }
+ };
+ consoleService.registerListener(listener)
+
+ let page = new Page({
+ contentURL: 'data:text/html;charset=utf-8,testWindowStopDontBreak',
+ contentScriptWhen: 'ready',
+ contentScript: Isolate(() => {
+ window.stop();
+ self.port.on('ping', () => self.port.emit('pong'));
+ })
+ });
+
+ page.port.on('pong', () => {
+ assert.pass('page-worker works after window.stop');
+ page.destroy();
+ consoleService.unregisterListener(listener);
+ done();
+ });
+
+ page.port.emit("ping");
+};
+
+/**
+ * bug 1138545 - the docs claim you can pass in a bare regexp.
+ */
+exports.testRegexArgument = function (assert, done) {
+ let url = 'data:text/html;charset=utf-8,testWindowStopDontBreak';
+
+ let page = new Page({
+ contentURL: url,
+ contentScriptWhen: 'ready',
+ contentScript: Isolate(() => {
+ self.port.emit("pong", document.location.href);
+ }),
+ include: /^data\:text\/html;.*/
+ });
+
+ assert.pass("We can pass in a RegExp into page-worker's include option.");
+
+ page.port.on("pong", (href) => {
+ assert.equal(href, url, "we get back the same url from the content script.");
+ page.destroy();
+ done();
+ });
+};
+
+function isDestroyed(page) {
+ try {
+ page.postMessage("foo");
+ }
+ catch (err) {
+ if (err.message == ERR_DESTROYED) {
+ return true;
+ }
+ else {
+ throw err;
+ }
+ }
+ return false;
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-panel.js b/addon-sdk/source/test/test-panel.js
new file mode 100644
index 000000000..13776e9db
--- /dev/null
+++ b/addon-sdk/source/test/test-panel.js
@@ -0,0 +1,1426 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+module.metadata = {
+ 'engines': {
+ 'Firefox': '*'
+ }
+};
+
+const { Cc, Ci } = require("chrome");
+const { Loader } = require('sdk/test/loader');
+const { LoaderWithHookedConsole } = require("sdk/test/loader");
+const { setTimeout } = require("sdk/timers");
+const self = require('sdk/self');
+const { open, close, focus, ready } = require('sdk/window/helpers');
+const { isPrivate } = require('sdk/private-browsing');
+const { isWindowPBSupported } = require('sdk/private-browsing/utils');
+const { defer, all } = require('sdk/core/promise');
+const { getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { URL } = require('sdk/url');
+const { wait } = require('./event/helpers');
+const packaging = require('@loader/options');
+const { cleanUI, after, isTravisCI } = require("sdk/test/utils");
+const { platform } = require('sdk/system');
+
+const fixtures = require('./fixtures')
+
+const SVG_URL = fixtures.url('mofo_logo.SVG');
+
+const Isolate = fn => '(' + fn + ')()';
+
+function ignorePassingDOMNodeWarning(type, message) {
+ if (type !== 'warn' || !message.startsWith('Passing a DOM node'))
+ console[type](message);
+}
+
+function makeEmptyPrivateBrowserWindow(options) {
+ options = options || {};
+ return open('chrome://browser/content/browser.xul', {
+ features: {
+ chrome: true,
+ toolbar: true,
+ private: true
+ }
+ });
+}
+
+exports["test Panel"] = function(assert, done) {
+ const { Panel } = require('sdk/panel');
+
+ let panel = Panel({
+ contentURL: "about:buildconfig",
+ contentScript: "self.postMessage(1); self.on('message', () => self.postMessage(2));",
+ onMessage: function (message) {
+ assert.equal(this, panel, "The 'this' object is the panel.");
+ switch(message) {
+ case 1:
+ assert.pass("The panel was loaded.");
+ panel.postMessage('');
+ break;
+ case 2:
+ assert.pass("The panel posted a message and received a response.");
+ panel.destroy();
+ done();
+ break;
+ }
+ }
+ });
+};
+
+exports["test Panel Emit"] = function(assert, done) {
+ const { Panel } = require('sdk/panel');
+
+ let panel = Panel({
+ contentURL: "about:buildconfig",
+ contentScript: "self.port.emit('loaded');" +
+ "self.port.on('addon-to-content', " +
+ " () => self.port.emit('received'));",
+ });
+ panel.port.on("loaded", function () {
+ assert.pass("The panel was loaded and sent a first event.");
+ panel.port.emit("addon-to-content");
+ });
+ panel.port.on("received", function () {
+ assert.pass("The panel posted a message and received a response.");
+ panel.destroy();
+ done();
+ });
+};
+
+exports["test Panel Emit Early"] = function(assert, done) {
+ const { Panel } = require('sdk/panel');
+
+ let panel = Panel({
+ contentURL: "about:buildconfig",
+ contentScript: "self.port.on('addon-to-content', " +
+ " () => self.port.emit('received'));",
+ });
+ panel.port.on("received", function () {
+ assert.pass("The panel posted a message early and received a response.");
+ panel.destroy();
+ done();
+ });
+ panel.port.emit("addon-to-content");
+};
+
+exports["test Show Hide Panel"] = function(assert, done) {
+ const { Panel } = require('sdk/panel');
+
+ let panel = Panel({
+ contentScript: "self.postMessage('')",
+ contentScriptWhen: "end",
+ contentURL: "data:text/html;charset=utf-8,",
+ onMessage: function (message) {
+ panel.show();
+ },
+ onShow: function () {
+ assert.pass("The panel was shown.");
+ assert.equal(this, panel, "The 'this' object is the panel.");
+ assert.equal(this.isShowing, true, "panel.isShowing == true.");
+ panel.hide();
+ },
+ onHide: function () {
+ assert.pass("The panel was hidden.");
+ assert.equal(this, panel, "The 'this' object is the panel.");
+ assert.equal(this.isShowing, false, "panel.isShowing == false.");
+ panel.destroy();
+ done();
+ }
+ });
+};
+
+exports["test Document Reload"] = function(assert, done) {
+ const { Panel } = require('sdk/panel');
+
+ let url2 = "data:text/html;charset=utf-8,page2";
+ let content =
+ "<script>" +
+ "window.addEventListener('message', function({ data }) {"+
+ " if (data == 'move') window.location = '" + url2 + "';" +
+ '}, false);' +
+ "</script>";
+ let messageCount = 0;
+ let panel = Panel({
+ // using URL here is intentional, see bug 859009
+ contentURL: URL("data:text/html;charset=utf-8," + encodeURIComponent(content)),
+ contentScript: "self.postMessage(window.location.href);" +
+ // initiate change to url2
+ "self.port.once('move', () => document.defaultView.postMessage('move', '*'));",
+ onMessage: function (message) {
+ messageCount++;
+ assert.notEqual(message, "about:blank", "about:blank is not a message " + messageCount);
+
+ if (messageCount == 1) {
+ assert.ok(/data:text\/html/.test(message), "First document had a content script; " + message);
+ panel.port.emit('move');
+ assert.pass('move message was sent');
+ return;
+ }
+ else if (messageCount == 2) {
+ assert.equal(message, url2, "Second document too; " + message);
+ panel.destroy();
+ done();
+ }
+ }
+ });
+ assert.pass('Panel was created');
+};
+
+// Test disabled because of bug 910230
+/*
+exports["test Parent Resize Hack"] = function(assert, done) {
+ const { Panel } = require('sdk/panel');
+
+ let browserWindow = getMostRecentBrowserWindow();
+
+ let previousWidth = browserWindow.outerWidth;
+ let previousHeight = browserWindow.outerHeight;
+
+ let content = "<script>" +
+ "function contentResize() {" +
+ " resizeTo(200,200);" +
+ " resizeBy(200,200);" +
+ " window.postMessage('resize-attempt', '*');" +
+ "}" +
+ "</script>" +
+ "Try to resize browser window";
+
+ let panel = Panel({
+ contentURL: "data:text/html;charset=utf-8," + encodeURIComponent(content),
+ contentScriptWhen: "ready",
+ contentScript: Isolate(() => {
+ self.on('message', message => {
+ if (message === 'resize') unsafeWindow.contentResize();
+ });
+
+ window.addEventListener('message', ({ data }) => self.postMessage(data));
+ }),
+ onMessage: function (message) {
+ if (message !== "resize-attempt") return;
+
+ assert.equal(browserWindow, getMostRecentBrowserWindow(),
+ "The browser window is still the same");
+ assert.equal(previousWidth, browserWindow.outerWidth,
+ "Size doesn't change by calling resizeTo/By/...");
+ assert.equal(previousHeight, browserWindow.outerHeight,
+ "Size doesn't change by calling resizeTo/By/...");
+
+ try {
+ panel.destroy();
+ }
+ catch (e) {
+ assert.fail(e);
+ throw e;
+ }
+
+ done();
+ },
+ onShow: () => panel.postMessage('resize')
+ });
+
+ panel.show();
+}
+*/
+
+exports["test Resize Panel"] = function(assert, done) {
+ const { Panel } = require('sdk/panel');
+
+ // These tests fail on Linux if the browser window in which the panel
+ // is displayed is not active. And depending on what other tests have run
+ // before this one, it might not be (the untitled window in which the test
+ // runner executes is often active). So we make sure the browser window
+ // is focused by focusing it before running the tests. Then, to be the best
+ // possible test citizen, we refocus whatever window was focused before we
+ // started running these tests.
+
+ let activeWindow = Cc["@mozilla.org/embedcomp/window-watcher;1"].
+ getService(Ci.nsIWindowWatcher).
+ activeWindow;
+ let browserWindow = Cc["@mozilla.org/appshell/window-mediator;1"].
+ getService(Ci.nsIWindowMediator).
+ getMostRecentWindow("navigator:browser");
+
+
+ function onFocus() {
+ browserWindow.removeEventListener("focus", onFocus, true);
+
+ let panel = Panel({
+ contentScript: "self.postMessage('')",
+ contentScriptWhen: "end",
+ contentURL: "data:text/html;charset=utf-8,",
+ height: 10,
+ width: 10,
+ onMessage: function (message) {
+ // Make sure that attempting to resize a panel while it isn't
+ // visible doesn't cause an error.
+ panel.resize(1, 1);
+
+ panel.show();
+ },
+ onShow: function () {
+ panel.resize(100,100);
+ panel.hide();
+ },
+ onHide: function () {
+ assert.ok((panel.width == 100) && (panel.height == 100),
+ "The panel was resized.");
+ if (activeWindow)
+ activeWindow.focus();
+ done();
+ }
+ });
+ }
+
+ if (browserWindow === activeWindow) {
+ onFocus();
+ }
+ else {
+ browserWindow.addEventListener("focus", onFocus, true);
+ browserWindow.focus();
+ }
+};
+
+exports["test Hide Before Show"] = function(assert, done) {
+ const { Panel } = require('sdk/panel');
+
+ let showCalled = false;
+ let hideCalled = false;
+ let panel1 = Panel({
+ onShow: function () {
+ showCalled = true;
+ },
+ onHide: function () {
+ hideCalled = true;
+ }
+ });
+ panel1.show();
+ panel1.hide();
+
+ let panel2 = Panel({
+ onShow: function () {
+ if (showCalled) {
+ assert.ok(hideCalled, 'should not emit show without also emitting hide');
+ } else {
+ assert.ok(!hideCalled, 'should not emit hide without also emitting show');
+ }
+ panel1.destroy();
+ panel2.destroy();
+ done();
+ },
+ });
+ panel2.show();
+};
+
+exports["test Several Show Hides"] = function(assert, done) {
+ const { Panel } = require('sdk/panel');
+
+ let hideCalled = 0;
+ let panel = Panel({
+ contentURL: "about:buildconfig",
+ onShow: function () {
+ panel.hide();
+ },
+ onHide: function () {
+ hideCalled++;
+ if (hideCalled < 3)
+ panel.show();
+ else {
+ assert.pass("onHide called three times as expected");
+ done();
+ }
+ }
+ });
+ panel.on('error', function(e) {
+ assert.fail('error was emitted:' + e.message + '\n' + e.stack);
+ });
+ panel.show();
+};
+
+exports["test Anchor And Arrow"] = function*(assert, done) {
+ let { loader } = LoaderWithHookedConsole(module, ignorePassingDOMNodeWarning);
+ let { Panel } = loader.require('sdk/panel');
+
+ let count = 0;
+ let url = 'data:text/html;charset=utf-8,' +
+ '<html><head><title>foo</title></head><body>' +
+ '</body></html>';
+
+ let panel = yield new Promise(resolve => {
+ let browserWindow = getMostRecentBrowserWindow();
+ let anchor = browserWindow.document.getElementById("identity-box");
+ let panel = Panel({
+ contentURL: "data:text/html;charset=utf-8,<html><body style='padding: 0; margin: 0; " +
+ "background: gray; text-align: center;'>Anchor: " +
+ anchor.id + "</body></html>",
+ width: 200,
+ height: 100,
+ onShow: () => resolve(panel)
+ });
+ panel.show(null, anchor);
+ });
+ assert.pass("All anchored panel test displayed");
+
+ panel.destroy();
+ assert.pass("panel was destroyed.");
+};
+
+exports["test Panel Focus True"] = function(assert, done) {
+ const { Panel } = require('sdk/panel');
+
+ const FM = Cc["@mozilla.org/focus-manager;1"].
+ getService(Ci.nsIFocusManager);
+
+ let browserWindow = Cc["@mozilla.org/appshell/window-mediator;1"].
+ getService(Ci.nsIWindowMediator).
+ getMostRecentWindow("navigator:browser");
+
+ // Make sure there is a focused element
+ browserWindow.document.documentElement.focus();
+
+ // Get the current focused element
+ let focusedElement = FM.focusedElement;
+
+ let panel = Panel({
+ contentURL: "about:buildconfig",
+ focus: true,
+ onShow: function () {
+ assert.ok(focusedElement !== FM.focusedElement,
+ "The panel takes the focus away.");
+ done();
+ }
+ });
+ panel.show();
+};
+
+exports["test Panel Focus False"] = function(assert, done) {
+ const { Panel } = require('sdk/panel');
+
+ const FM = Cc["@mozilla.org/focus-manager;1"].
+ getService(Ci.nsIFocusManager);
+
+ let browserWindow = Cc["@mozilla.org/appshell/window-mediator;1"].
+ getService(Ci.nsIWindowMediator).
+ getMostRecentWindow("navigator:browser");
+
+ // Make sure there is a focused element
+ browserWindow.document.documentElement.focus();
+
+ // Get the current focused element
+ let focusedElement = FM.focusedElement;
+
+ let panel = Panel({
+ contentURL: "about:buildconfig",
+ focus: false,
+ onShow: function () {
+ assert.ok(focusedElement === FM.focusedElement,
+ "The panel does not take the focus away.");
+ done();
+ }
+ });
+ panel.show();
+};
+
+exports["test Panel Focus Not Set"] = function(assert, done) {
+ const { Panel } = require('sdk/panel');
+
+ const FM = Cc["@mozilla.org/focus-manager;1"].
+ getService(Ci.nsIFocusManager);
+
+ let browserWindow = Cc["@mozilla.org/appshell/window-mediator;1"].
+ getService(Ci.nsIWindowMediator).
+ getMostRecentWindow("navigator:browser");
+
+ // Make sure there is a focused element
+ browserWindow.document.documentElement.focus();
+
+ // Get the current focused element
+ let focusedElement = FM.focusedElement;
+
+ let panel = Panel({
+ contentURL: "about:buildconfig",
+ onShow: function () {
+ assert.ok(focusedElement !== FM.focusedElement,
+ "The panel takes the focus away.");
+ done();
+ }
+ });
+ panel.show();
+};
+
+exports["test Panel Text Color"] = function(assert, done) {
+ const { Panel } = require('sdk/panel');
+
+ let html = "<html><head><style>body {color: yellow}</style></head>" +
+ "<body><p>Foo</p></body></html>";
+ let panel = Panel({
+ contentURL: "data:text/html;charset=utf-8," + encodeURI(html),
+ contentScript: "self.port.emit('color', " +
+ "window.getComputedStyle(document.body.firstChild, null). " +
+ " getPropertyValue('color'));"
+ });
+ panel.port.on("color", function (color) {
+ assert.equal(color, "rgb(255, 255, 0)",
+ "The panel text color style is preserved when a style exists.");
+ panel.destroy();
+ done();
+ });
+};
+
+// Bug 866333
+exports["test watch event name"] = function(assert, done) {
+ const { Panel } = require('sdk/panel');
+
+ let html = "<html><head><style>body {color: yellow}</style></head>" +
+ "<body><p>Foo</p></body></html>";
+
+ let panel = Panel({
+ contentURL: "data:text/html;charset=utf-8," + encodeURI(html),
+ contentScript: "self.port.emit('watch', 'test');"
+ });
+ panel.port.on("watch", function (msg) {
+ assert.equal(msg, "test", 'watch event name works');
+ panel.destroy();
+ done();
+ });
+}
+
+// Bug 696552: Ensure panel.contentURL modification support
+exports["test Change Content URL"] = function(assert, done) {
+ const { Panel } = require('sdk/panel');
+
+ let panel = Panel({
+ contentURL: "about:blank",
+ contentScript: "self.port.emit('ready', document.location.href);"
+ });
+
+ let count = 0;
+ panel.port.on("ready", function (location) {
+ count++;
+ if (count == 1) {
+ assert.equal(location, "about:blank");
+ assert.equal(panel.contentURL, "about:blank");
+ panel.contentURL = "about:buildconfig";
+ }
+ else {
+ assert.equal(location, "about:buildconfig");
+ assert.equal(panel.contentURL, "about:buildconfig");
+ panel.destroy();
+ done();
+ }
+ });
+};
+
+function makeEventOrderTest(options) {
+ let expectedEvents = [];
+
+ return function(assert, done) {
+ const { Panel } = require('sdk/panel');
+
+ let panel = Panel({ contentURL: "about:buildconfig" });
+
+ function expect(event, cb) {
+ expectedEvents.push(event);
+ panel.on(event, function() {
+ assert.equal(event, expectedEvents.shift());
+ if (cb)
+ setTimeout(cb, 1);
+ });
+ return {then: expect};
+ }
+
+ options.test(assert, done, expect, panel);
+ }
+}
+
+exports["test Automatic Destroy"] = function(assert) {
+ let loader = Loader(module);
+ let panel = loader.require("sdk/panel").Panel({
+ contentURL: "about:buildconfig",
+ contentScript:
+ "self.port.on('event', () => self.port.emit('event-back'));"
+ });
+
+ loader.unload();
+
+ assert.throws(() => {
+ panel.port.emit("event");
+ }, /already have been unloaded/, "check automatic destroy");
+};
+
+exports["test Show Then Destroy"] = makeEventOrderTest({
+ test: function(assert, done, expect, panel) {
+ panel.show();
+ expect('show', function() { panel.destroy(); }).
+ then('hide', function() { done(); });
+ }
+});
+
+
+// TODO: Re-enable and fix this intermittent test
+// See Bug 1111695 https://bugzilla.mozilla.org/show_bug.cgi?id=1111695
+/*
+exports["test Show Then Hide Then Destroy"] = makeEventOrderTest({
+ test: function(assert, done, expect, panel) {
+ panel.show();
+ expect('show', function() { panel.hide(); }).
+ then('hide', function() { panel.destroy(); done(); });
+ }
+});
+*/
+
+exports["test Content URL Option"] = function(assert) {
+ const { Panel } = require('sdk/panel');
+
+ const URL_STRING = "about:buildconfig";
+ const HTML_CONTENT = "<html><title>Test</title><p>This is a test.</p></html>";
+ let dataURL = "data:text/html;charset=utf-8," + encodeURIComponent(HTML_CONTENT);
+
+ let panel = Panel({ contentURL: URL_STRING });
+ assert.pass("contentURL accepts a string URL.");
+ assert.equal(panel.contentURL, URL_STRING,
+ "contentURL is the string to which it was set.");
+ panel.destroy();
+
+ panel = Panel({ contentURL: dataURL });
+ assert.pass("contentURL accepts a data: URL.");
+ panel.destroy();
+
+ panel = Panel({});
+ assert.ok(panel.contentURL == null, "contentURL is undefined.");
+ panel.destroy();
+
+ assert.throws(() => Panel({ contentURL: "foo" }),
+ /The `contentURL` option must be a valid URL./,
+ "Panel throws an exception if contentURL is not a URL.");
+};
+
+exports["test SVG Document"] = function(assert) {
+ let panel = require("sdk/panel").Panel({ contentURL: SVG_URL });
+
+ panel.show();
+ panel.hide();
+ panel.destroy();
+
+ assert.pass("contentURL accepts a svg document");
+ assert.equal(panel.contentURL, SVG_URL,
+ "contentURL is the string to which it was set.");
+};
+
+exports["test ContentScriptOptions Option"] = function(assert, done) {
+ let loader = Loader(module);
+ let panel = loader.require("sdk/panel").Panel({
+ contentScript: "self.postMessage( [typeof self.options.d, self.options] );",
+ contentScriptWhen: "end",
+ contentScriptOptions: {a: true, b: [1,2,3], c: "string", d: function(){ return 'test'}},
+ contentURL: "data:text/html;charset=utf-8,",
+ onMessage: function(msg) {
+ assert.equal( msg[0], 'undefined', 'functions are stripped from contentScriptOptions' );
+ assert.equal( typeof msg[1], 'object', 'object as contentScriptOptions' );
+ assert.equal( msg[1].a, true, 'boolean in contentScriptOptions' );
+ assert.equal( msg[1].b.join(), '1,2,3', 'array and numbers in contentScriptOptions' );
+ assert.equal( msg[1].c, 'string', 'string in contentScriptOptions' );
+ done();
+ }
+ });
+};
+
+exports["test console.log in Panel"] = function(assert, done) {
+ let text = 'console.log() in Panel works!';
+ let html = '<script>onload = function log(){\
+ console.log("' + text + '");\
+ }</script>';
+
+ let { loader } = LoaderWithHookedConsole(module, onMessage);
+ let { Panel } = loader.require('sdk/panel');
+
+ let panel = Panel({
+ contentURL: 'data:text/html;charset=utf-8,' + encodeURIComponent(html)
+ });
+
+ panel.show();
+
+ function onMessage(type, message) {
+ assert.equal(type, 'log', 'console.log() works');
+ assert.equal(message, text, 'console.log() works');
+ panel.destroy();
+ done();
+ }
+};
+
+/*if (isWindowPBSupported) {
+ exports.testPanelDoesNotShowInPrivateWindowNoAnchor = function(assert, done) {
+ let { loader } = LoaderWithHookedConsole(module, ignorePassingDOMNodeWarning);
+ let { Panel } = loader.require("sdk/panel");
+ let browserWindow = getMostRecentBrowserWindow();
+
+ assert.equal(isPrivate(browserWindow), false, 'open window is not private');
+
+ let panel = Panel({
+ contentURL: SVG_URL
+ });
+
+ testShowPanel(assert, panel).
+ then(makeEmptyPrivateBrowserWindow).
+ then(focus).
+ then(function(window) {
+ assert.equal(isPrivate(window), true, 'opened window is private');
+ assert.pass('private window was focused');
+ return window;
+ }).
+ then(function(window) {
+ let { promise, resolve } = defer();
+ let showTries = 0;
+ let showCount = 0;
+
+ panel.on('show', function runTests() {
+ showCount++;
+
+ if (showTries == 2) {
+ panel.removeListener('show', runTests);
+ assert.equal(showCount, 1, 'show count is correct - 1');
+ resolve(window);
+ }
+ });
+ showTries++;
+ panel.show();
+ showTries++;
+ panel.show(null, browserWindow.gBrowser);
+
+ return promise;
+ }).
+ then(function(window) {
+ assert.equal(panel.isShowing, true, 'panel is still showing');
+ panel.hide();
+ assert.equal(panel.isShowing, false, 'panel is hidden');
+ return window;
+ }).
+ then(close).
+ then(function() {
+ assert.pass('private window was closed');
+ }).
+ then(testShowPanel.bind(null, assert, panel)).
+ then(done, assert.fail.bind(assert));
+ }
+
+ exports.testPanelDoesNotShowInPrivateWindowWithAnchor = function(assert, done) {
+ let { loader } = LoaderWithHookedConsole(module, ignorePassingDOMNodeWarning);
+ let { Panel } = loader.require("sdk/panel");
+ let browserWindow = getMostRecentBrowserWindow();
+
+ assert.equal(isPrivate(browserWindow), false, 'open window is not private');
+
+ let panel = Panel({
+ contentURL: SVG_URL
+ });
+
+ testShowPanel(assert, panel).
+ then(makeEmptyPrivateBrowserWindow).
+ then(focus).
+ then(function(window) {
+ assert.equal(isPrivate(window), true, 'opened window is private');
+ assert.pass('private window was focused');
+ return window;
+ }).
+ then(function(window) {
+ let { promise, resolve } = defer();
+ let showTries = 0;
+ let showCount = 0;
+
+ panel.on('show', function runTests() {
+ showCount++;
+
+ if (showTries == 2) {
+ panel.removeListener('show', runTests);
+ assert.equal(showCount, 1, 'show count is correct - 1');
+ resolve(window);
+ }
+ });
+ showTries++;
+ panel.show(null, window.gBrowser);
+ showTries++;
+ panel.show(null, browserWindow.gBrowser);
+
+ return promise;
+ }).
+ then(function(window) {
+ assert.equal(panel.isShowing, true, 'panel is still showing');
+ panel.hide();
+ assert.equal(panel.isShowing, false, 'panel is hidden');
+ return window;
+ }).
+ then(close).
+ then(function() {
+ assert.pass('private window was closed');
+ }).
+ then(testShowPanel.bind(null, assert, panel)).
+ then(done, assert.fail.bind(assert));
+ }
+}*/
+
+function testShowPanel(assert, panel) {
+ let { promise, resolve } = defer();
+
+ assert.ok(!panel.isShowing, 'the panel is not showing [1]');
+
+ panel.once('show', function() {
+ assert.ok(panel.isShowing, 'the panel is showing');
+
+ panel.once('hide', function() {
+ assert.ok(!panel.isShowing, 'the panel is not showing [2]');
+
+ resolve(null);
+ });
+
+ panel.hide();
+ })
+ panel.show();
+
+ return promise;
+}
+
+exports['test Style Applied Only Once'] = function (assert, done) {
+ let loader = Loader(module);
+ let panel = loader.require("sdk/panel").Panel({
+ contentURL: "data:text/html;charset=utf-8,",
+ contentScript:
+ 'self.port.on("check",function() { self.port.emit("count", document.getElementsByTagName("style").length); });' +
+ 'self.port.on("ping", function (count) { self.port.emit("pong", count); });'
+ });
+
+ panel.port.on('count', function (styleCount) {
+ assert.equal(styleCount, 1, 'should only have one style');
+ done();
+ });
+
+ panel.port.on('pong', function (counter) {
+ panel[--counter % 2 ? 'hide' : 'show']();
+ panel.port.emit(!counter ? 'check' : 'ping', counter);
+ });
+
+ panel.on('show', init);
+ panel.show();
+
+ function init () {
+ panel.removeListener('show', init);
+ panel.port.emit('ping', 10);
+ }
+};
+
+exports['test Only One Panel Open Concurrently'] = function (assert, done) {
+ const loader = Loader(module);
+ const { Panel } = loader.require('sdk/panel')
+
+ let panelA = Panel({
+ contentURL: 'about:buildconfig'
+ });
+
+ let panelB = Panel({
+ contentURL: 'about:buildconfig',
+ onShow: function () {
+ // When loading two panels simulataneously, only the second
+ // should be shown, never showing the first
+ assert.equal(panelA.isShowing, false, 'First panel is hidden');
+ assert.equal(panelB.isShowing, true, 'Second panel is showing');
+ panelC.show();
+ }
+ });
+
+ let panelC = Panel({
+ contentURL: 'about:buildconfig',
+ onShow: function () {
+ assert.equal(panelA.isShowing, false, 'First panel is hidden');
+ assert.equal(panelB.isShowing, false, 'Second panel is hidden');
+ assert.equal(panelC.isShowing, true, 'Third panel is showing');
+ done();
+ }
+ });
+
+ panelA.show();
+ panelB.show();
+};
+
+exports['test passing DOM node as first argument'] = function (assert, done) {
+ let warned = defer();
+ let shown = defer();
+
+ function onMessage(type, message) {
+ if (type != 'warn') return;
+
+ let warning = 'Passing a DOM node to Panel.show() method is an unsupported ' +
+ 'feature that will be soon replaced. ' +
+ 'See: https://bugzilla.mozilla.org/show_bug.cgi?id=878877';
+
+ assert.equal(type, 'warn',
+ 'the message logged is a warning');
+
+ assert.equal(message, warning,
+ 'the warning content is correct');
+
+ warned.resolve();
+ }
+
+ let { loader } = LoaderWithHookedConsole(module, onMessage);
+ let { Panel } = loader.require('sdk/panel');
+ let { ActionButton } = loader.require('sdk/ui/button/action');
+ let { getNodeView } = loader.require('sdk/view/core');
+ let { document } = getMostRecentBrowserWindow();
+
+ let panel = Panel({
+ onShow: function() {
+ let panelNode = document.getElementById('mainPopupSet').lastChild;
+
+ assert.equal(panelNode.anchorNode, buttonNode,
+ 'the panel is properly anchored to the button');
+
+ shown.resolve();
+ }
+ });
+
+ let button = ActionButton({
+ id: 'panel-button',
+ label: 'panel button',
+ icon: './icon.png'
+ });
+
+ let buttonNode = getNodeView(button);
+
+ all([warned.promise, shown.promise]).
+ then(loader.unload).
+ then(done, assert.fail)
+
+ panel.show(buttonNode);
+};
+
+// This test is checking that `onpupshowing` events emitted by panel's children
+// are not considered.
+// See Bug 886329
+exports['test nested popups'] = function (assert, done) {
+ let loader = Loader(module);
+ let { Panel } = loader.require('sdk/panel');
+ let { getActiveView } = loader.require('sdk/view/core');
+ let url = '<select><option>1<option>2<option>3</select>';
+
+ let getContentWindow = panel => {
+ return getActiveView(panel).querySelector('iframe').contentWindow;
+ }
+
+ let panel = Panel({
+ contentURL: 'data:text/html;charset=utf-8,' + encodeURIComponent(url),
+ onShow: () => {
+ ready(getContentWindow(panel)).then(({ window, document }) => {
+ let select = document.querySelector('select');
+ let event = document.createEvent('UIEvent');
+
+ event.initUIEvent('popupshowing', true, true, window, null);
+ select.dispatchEvent(event);
+
+ assert.equal(
+ select,
+ getContentWindow(panel).document.querySelector('select'),
+ 'select is still loaded in panel'
+ );
+
+ done();
+ });
+ }
+ });
+
+ panel.show();
+};
+
+exports['test emits on url changes'] = function (assert, done) {
+ let loader = Loader(module);
+ let { Panel } = loader.require('sdk/panel');
+ let uriA = 'data:text/html;charset=utf-8,A';
+ let uriB = 'data:text/html;charset=utf-8,B';
+
+ let panel = Panel({
+ contentURL: uriA,
+ contentScript: 'new ' + function() {
+ self.port.on('hi', function() {
+ self.port.emit('bye', document.URL);
+ });
+ }
+ });
+
+ panel.contentURL = uriB;
+ panel.port.emit('hi', 'hi')
+ panel.port.on('bye', function(uri) {
+ assert.equal(uri, uriB, 'message was delivered to new uri');
+ loader.unload();
+ done();
+ });
+};
+
+exports['test panel can be constructed without any arguments'] = function (assert) {
+ const { Panel } = require('sdk/panel');
+
+ let panel = Panel();
+ assert.ok(true, "Creating a panel with no arguments does not throw");
+};
+
+exports['test panel CSS'] = function(assert, done) {
+ const { merge } = require("sdk/util/object");
+
+ let loader = Loader(module, null, null, {
+ modules: {
+ "sdk/self": merge({}, self, {
+ data: merge({}, self.data, fixtures)
+ })
+ }
+ });
+
+ const { Panel } = loader.require('sdk/panel');
+
+ const { getActiveView } = loader.require('sdk/view/core');
+
+ const getContentWindow = panel =>
+ getActiveView(panel).querySelector('iframe').contentWindow;
+
+ let panel = Panel({
+ contentURL: 'data:text/html;charset=utf-8,' +
+ '<div style="background: silver">css test</div>',
+ contentStyle: 'div { height: 100px; }',
+ contentStyleFile: [fixtures.url("include-file.css"), "./border-style.css"],
+ onShow: () => {
+ ready(getContentWindow(panel)).then(({ window, document }) => {
+ let div = document.querySelector('div');
+
+ assert.equal(div.clientHeight, 100,
+ "Panel contentStyle worked");
+
+ assert.equal(div.offsetHeight, 120,
+ "Panel contentStyleFile worked");
+
+ assert.equal(window.getComputedStyle(div).borderTopStyle, "dashed",
+ "Panel contentStyleFile with relative path worked");
+
+ loader.unload();
+ done();
+ }).then(null, assert.fail);
+ }
+ });
+
+ panel.show();
+};
+
+exports['test panel contentScriptFile'] = function(assert, done) {
+ const { merge } = require("sdk/util/object");
+
+ let loader = Loader(module, null, null, {
+ modules: {
+ "sdk/self": merge({}, self, {
+ data: merge({}, self.data, {url: fixtures.url})
+ })
+ }
+ });
+
+ const { Panel } = loader.require('sdk/panel');
+ const { getActiveView } = loader.require('sdk/view/core');
+
+ const getContentWindow = panel =>
+ getActiveView(panel).querySelector('iframe').contentWindow;
+
+ let whenMessage = defer();
+ let whenShown = defer();
+
+ let panel = Panel({
+ contentURL: './test.html',
+ contentScriptFile: "./test-contentScriptFile.js",
+ onMessage: (message) => {
+ assert.equal(message, "msg from contentScriptFile",
+ "Panel contentScriptFile with relative path worked");
+
+ whenMessage.resolve();
+ },
+ onShow: () => {
+ ready(getContentWindow(panel)).then(({ document }) => {
+ assert.equal(document.title, 'foo',
+ "Panel contentURL with relative path worked");
+
+ whenShown.resolve();
+ });
+ }
+ });
+
+ all([whenMessage.promise, whenShown.promise]).
+ then(loader.unload).
+ then(done, assert.fail);
+
+ panel.show();
+};
+
+
+exports['test panel CSS list'] = function(assert, done) {
+ const loader = Loader(module);
+ const { Panel } = loader.require('sdk/panel');
+
+ const { getActiveView } = loader.require('sdk/view/core');
+
+ const getContentWindow = panel =>
+ getActiveView(panel).querySelector('iframe').contentWindow;
+
+ let panel = Panel({
+ contentURL: 'data:text/html;charset=utf-8,' +
+ '<div style="width:320px; max-width: 480px!important">css test</div>',
+ contentStyleFile: [
+ // Highlight evaluation order in this list
+ "data:text/css;charset=utf-8,div { border: 1px solid black; }",
+ "data:text/css;charset=utf-8,div { border: 10px solid black; }",
+ // Highlight evaluation order between contentStylesheet & contentStylesheetFile
+ "data:text/css;charset=utf-8s,div { height: 1000px; }",
+ // Highlight precedence between the author and user style sheet
+ "data:text/css;charset=utf-8,div { width: 200px; max-width: 640px!important}",
+ ],
+ contentStyle: [
+ "div { height: 10px; }",
+ "div { height: 100px; }"
+ ],
+ onShow: () => {
+ ready(getContentWindow(panel)).then(({ window, document }) => {
+ let div = document.querySelector('div');
+ let style = window.getComputedStyle(div);
+
+ assert.equal(div.clientHeight, 100,
+ 'Panel contentStyle list is evaluated after contentStyleFile');
+
+ assert.equal(div.offsetHeight, 120,
+ 'Panel contentStyleFile list works');
+
+ assert.equal(style.width, '320px',
+ 'add-on author/page author stylesheet precedence works');
+
+ assert.equal(style.maxWidth, '480px',
+ 'add-on author/page author stylesheet !important precedence works');
+
+ loader.unload();
+ }).then(done, assert.fail);
+ }
+ });
+
+ panel.show();
+};
+
+exports['test panel contextmenu validation'] = function(assert) {
+ const loader = Loader(module);
+ const { Panel } = loader.require('sdk/panel');
+
+ let panel = Panel({});
+
+ assert.equal(panel.contextMenu, false,
+ 'contextMenu option is `false` by default');
+
+ panel.destroy();
+
+ panel = Panel({
+ contextMenu: false
+ });
+
+ assert.equal(panel.contextMenu, false,
+ 'contextMenu option is `false`');
+
+ panel.contextMenu = true;
+
+ assert.equal(panel.contextMenu, true,
+ 'contextMenu option accepts boolean values');
+
+ panel.destroy();
+
+ panel = Panel({
+ contextMenu: true
+ });
+
+ assert.equal(panel.contextMenu, true,
+ 'contextMenu option is `true`');
+
+ panel.contextMenu = false;
+
+ assert.equal(panel.contextMenu, false,
+ 'contextMenu option accepts boolean values');
+
+ assert.throws(() =>
+ Panel({contextMenu: 1}),
+ /The option "contextMenu" must be one of the following types: boolean, undefined, null/,
+ 'contextMenu only accepts boolean or nil values');
+
+ panel = Panel();
+
+ assert.throws(() =>
+ panel.contextMenu = 1,
+ /The option "contextMenu" must be one of the following types: boolean, undefined, null/,
+ 'contextMenu only accepts boolean or nil values');
+
+ loader.unload();
+}
+
+exports['test panel contextmenu enabled'] = function*(assert) {
+ const loader = Loader(module);
+ const { Panel } = loader.require('sdk/panel');
+ const { getActiveView } = loader.require('sdk/view/core');
+ const { getContentDocument } = loader.require('sdk/panel/utils');
+
+ let contextmenu = getMostRecentBrowserWindow().
+ document.getElementById("contentAreaContextMenu");
+
+ let panel = Panel({contextMenu: true});
+
+ panel.show();
+
+ yield wait(panel, 'show');
+
+ let view = getActiveView(panel);
+ let window = getContentDocument(view).defaultView;
+
+ let { sendMouseEvent } = window.QueryInterface(Ci.nsIInterfaceRequestor).
+ getInterface(Ci.nsIDOMWindowUtils);
+
+ yield ready(window);
+
+ assert.equal(contextmenu.state, 'closed',
+ 'contextmenu must be closed');
+
+ sendMouseEvent('contextmenu', 20, 20, 2, 1, 0);
+
+ yield wait(contextmenu, 'popupshown');
+
+ assert.equal(contextmenu.state, 'open',
+ 'contextmenu is opened');
+
+ contextmenu.hidePopup();
+
+ loader.unload();
+}
+
+exports['test panel contextmenu disabled'] = function*(assert) {
+ const loader = Loader(module);
+ const { Panel } = loader.require('sdk/panel');
+ const { getActiveView } = loader.require('sdk/view/core');
+ const { getContentDocument } = loader.require('sdk/panel/utils');
+
+ let contextmenu = getMostRecentBrowserWindow().
+ document.getElementById("contentAreaContextMenu");
+ let listener = () => assert.fail('popupshown should never be called');
+
+ let panel = Panel();
+
+ panel.show();
+
+ yield wait(panel, 'show');
+
+ let view = getActiveView(panel);
+ let window = getContentDocument(view).defaultView;
+
+ let { sendMouseEvent } = window.QueryInterface(Ci.nsIInterfaceRequestor).
+ getInterface(Ci.nsIDOMWindowUtils);
+
+ yield ready(window);
+
+ assert.equal(contextmenu.state, 'closed',
+ 'contextmenu must be closed');
+
+ sendMouseEvent('contextmenu', 20, 20, 2, 1, 0);
+
+ contextmenu.addEventListener('popupshown', listener);
+
+ yield wait(1000);
+
+ contextmenu.removeEventListener('popupshown', listener);
+
+ assert.equal(contextmenu.state, 'closed',
+ 'contextmenu was never open');
+
+ loader.unload();
+}
+
+exports["test panel addon global object"] = function*(assert) {
+ const { merge } = require("sdk/util/object");
+
+ let loader = Loader(module, null, null, {
+ modules: {
+ "sdk/self": merge({}, self, {
+ data: merge({}, self.data, {url: fixtures.url})
+ })
+ }
+ });
+
+ const { Panel } = loader.require('sdk/panel');
+
+ let panel = Panel({
+ contentURL: "./test-trusted-document.html"
+ });
+
+ panel.show();
+
+ yield wait(panel, "show");
+
+ panel.port.emit('addon-to-document', 'ok');
+
+ yield wait(panel.port, "document-to-addon");
+
+ assert.pass("Received an event from the document");
+
+ loader.unload();
+}
+
+exports["test panel load doesn't show"] = function*(assert) {
+ let loader = Loader(module);
+
+ let panel = loader.require("sdk/panel").Panel({
+ contentScript: "addEventListener('load', function(event) { self.postMessage('load'); });",
+ contentScriptWhen: "start",
+ contentURL: "data:text/html;charset=utf-8,",
+ });
+
+ let shown = defer();
+ let messaged = defer();
+
+ panel.once("show", function() {
+ shown.resolve();
+ });
+
+ panel.once("message", function() {
+ messaged.resolve();
+ });
+
+ panel.show();
+ yield all([shown.promise, messaged.promise]);
+ assert.ok(true, "Saw panel display");
+
+ panel.on("show", function() {
+ assert.fail("Should not have seen another show event")
+ });
+
+ messaged = defer();
+ panel.once("message", function() {
+ assert.ok(true, "Saw panel reload");
+ messaged.resolve();
+ });
+
+ panel.contentURL = "data:text/html;charset=utf-8,<html/>";
+
+ yield messaged.promise;
+ loader.unload();
+}
+
+exports["test Panel without contentURL and contentScriptWhen=start should show"] = function*(assert) {
+ let loader = Loader(module);
+
+ let panel = loader.require("sdk/panel").Panel({
+ contentScriptWhen: "start",
+ // No contentURL, the bug only shows up when contentURL is not explicitly set.
+ });
+
+ yield new Promise(resolve => {
+ panel.once("show", resolve);
+ panel.show();
+ });
+
+ assert.pass("Received show event");
+
+ loader.unload();
+}
+
+exports["test Panel links"] = function*(assert) {
+ const loader = Loader(module);
+
+ const { Panel } = loader.require('sdk/panel');
+ const { getActiveView } = loader.require('sdk/view/core');
+ const tabs = loader.require('sdk/tabs');
+
+ const synthesizeClick = (panel, options) => {
+ let { contentWindow } = getActiveView(panel).querySelector('iframe');
+ let event = new contentWindow.MouseEvent('click', options);
+
+ contentWindow.document.querySelector('a').dispatchEvent(event);
+ }
+
+ const linkURL = 'data:text/html;charset=utf-8,' +
+ encodeURIComponent('<html><a href="#">foo</a></html>');
+
+ const contentURL = 'data:text/html;charset=utf-8,' +
+ encodeURIComponent(`<html><a href="${linkURL}">page</a></html>`);
+
+ let panel = Panel({
+ contentURL,
+ contentScript: Isolate(() => self.postMessage(document.URL))
+ });
+
+ panel.show();
+
+ let url = yield wait(panel, 'message');
+
+ assert.equal(url, contentURL,
+ 'content URL loaded');
+
+ synthesizeClick(panel, { bubbles: true });
+
+ url = yield wait(panel, 'message');
+
+ assert.equal(url, linkURL,
+ 'link URL loaded in the panel after click');
+
+ synthesizeClick(panel, {
+ bubbles: true,
+ [platform === 'darwin' ? 'metaKey' : 'ctrlKey']: true
+ });
+
+ let tab = yield wait(tabs, 'ready');
+
+ assert.equal(tab.url, linkURL + '#',
+ 'link URL loaded in a new tab after click + accel');
+
+ loader.unload();
+}
+
+exports["test Panel script allow property"] = function*(assert) {
+ const loader = Loader(module);
+ const { Panel } = loader.require('sdk/panel');
+ const { getActiveView } = loader.require('sdk/view/core');
+
+ const contentURL = 'data:text/html;charset=utf-8,' +
+ encodeURIComponent(`<body onclick='postMessage("got script click", "*")'>
+ <script>
+ document.body.appendChild(document.createElement('unwanted'))
+ </script>
+ </body>`);
+ let panel = Panel({
+ contentURL,
+ allow: {script: false},
+ });
+
+ panel.show();
+
+ yield wait(panel, 'show');
+
+ let { contentWindow } = getActiveView(panel).querySelector('iframe');
+
+ assert.equal(contentWindow.document.body.lastElementChild.localName, "script",
+ "Script should not have executed");
+
+ panel.allow.script = true;
+
+ let p = wait(contentWindow, "message");
+ let event = new contentWindow.MouseEvent('click', {});
+ contentWindow.document.body.dispatchEvent(event);
+
+ let msg = yield p;
+ assert.equal(msg.data, "got script click", "Should have seen script click");
+
+ loader.unload();
+};
+
+after(exports, function*(name, assert) {
+ yield cleanUI();
+ assert.pass("ui was cleaned.");
+});
+
+if (isTravisCI) {
+ module.exports = {
+ "test skip on jpm": (assert) => assert.pass("skipping this file with jpm")
+ };
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-passwords-utils.js b/addon-sdk/source/test/test-passwords-utils.js
new file mode 100644
index 000000000..72b25c78b
--- /dev/null
+++ b/addon-sdk/source/test/test-passwords-utils.js
@@ -0,0 +1,141 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { store, search, remove } = require("sdk/passwords/utils");
+
+exports["test store requires `password` field"] = function(assert) {
+ assert.throws(function() {
+ store({ username: "foo", realm: "bar" });
+ }, "`password` is required");
+};
+
+exports["test store requires `username` field"] = function(assert) {
+ assert.throws(function() {
+ store({ password: "foo", realm: "bar" });
+ }, "`username` is required");
+};
+
+exports["test store requires `realm` field"] = function(assert) {
+ assert.throws(function() {
+ store({ username: "foo", password: "bar" });
+ }, "`password` is required");
+};
+
+exports["test can't store same login twice"] = function(assert) {
+ let options = { username: "user", password: "pass", realm: "realm" };
+ store(options);
+ assert.throws(function() {
+ store(options);
+ }, "can't store same pass twice");
+ remove(options);
+};
+
+exports["test remove throws if no login found"] = function(assert) {
+ assert.throws(function() {
+ remove({ username: "foo", password: "bar", realm: "baz" });
+ }, "can't remove unstored credentials");
+};
+
+exports["test addon associated credentials"] = function(assert) {
+ let options = { username: "foo", password: "bar", realm: "baz" };
+ store(options);
+
+ assert.ok(search().length, "credential was stored");
+ assert.ok(search(options).length, "stored credential found");
+ assert.ok(search({ username: options.username }).length, "found by username");
+ assert.ok(search({ password: options.password }).length, "found by password");
+ assert.ok(search({ realm: options.realm }).length, "found by realm");
+
+ let credential = search(options)[0];
+ assert.equal(credential.url.indexOf("addon:"), 0,
+ "`addon:` uri is used for add-on associated credentials");
+ assert.equal(credential.username, options.username, "username matches");
+ assert.equal(credential.password, options.password, "password matches");
+ assert.equal(credential.realm, options.realm, "realm matches");
+ assert.equal(credential.formSubmitURL, null,
+ "`formSubmitURL` is `null` for add-on associated credentials");
+ assert.equal(credential.usernameField, "", "usernameField is empty");
+ assert.equal(credential.passwordField, "", "passwordField is empty");
+
+ remove(search(options)[0]);
+ assert.ok(!search(options).length, "remove worked");
+};
+
+exports["test web page associated credentials"] = function(assert) {
+ let options = {
+ url: "http://www.example.com",
+ formSubmitURL: "http://login.example.com",
+ username: "user",
+ password: "pass",
+ usernameField: "user-f",
+ passwordField: "pass-f"
+ };
+ store({
+ url: "http://www.example.com/login",
+ formSubmitURL: "http://login.example.com/foo/authenticate.cgi",
+ username: options.username,
+ password: options.password,
+ usernameField: options.usernameField,
+ passwordField: options.passwordField
+ });
+
+ assert.ok(search().length, "credential was stored");
+ assert.ok(search(options).length, "stored credential found");
+ assert.ok(search({ username: options.username }).length, "found by username");
+ assert.ok(search({ password: options.password }).length, "found by password");
+ assert.ok(search({ formSubmitURL: options.formSubmitURL }).length,
+ "found by formSubmitURL");
+ assert.ok(search({ usernameField: options.usernameField }).length,
+ "found by usernameField");
+ assert.ok(search({ passwordField: options.passwordField }).length,
+ "found by passwordField");
+
+ let credential = search(options)[0];
+ assert.equal(credential.url, options.url, "url matches");
+ assert.equal(credential.username, options.username, "username matches");
+ assert.equal(credential.password, options.password, "password matches");
+ assert.equal(credential.realm, null, "realm is ");
+ assert.equal(credential.formSubmitURL, options.formSubmitURL,
+ "`formSubmitURL` matches");
+ assert.equal(credential.usernameField, options.usernameField,
+ "usernameField matches");
+ assert.equal(credential.passwordField, options.passwordField,
+ "passwordField matches");
+
+ remove(search(options)[0]);
+ assert.ok(!search(options).length, "remove worked");
+};
+
+exports["test site authentication credentials"] = function(assert) {
+ let options = {
+ url: "http://test.authentication.com",
+ username: "u",
+ password: "p",
+ realm: "r"
+ };
+
+ store(options);
+ assert.ok(search().length, "credential was stored");
+ assert.ok(search(options).length, "stored credential found");
+ assert.ok(search({ username: options.username }).length, "found by username");
+ assert.ok(search({ password: options.password }).length, "found by password");
+ assert.ok(search({ realm: options.realm }).length, "found by realm");
+ assert.ok(search({ url: options.url }).length, "found by url");
+
+ let credential = search(options)[0];
+ assert.equal(credential.url, options.url, "url matches");
+ assert.equal(credential.username, options.username, "username matches");
+ assert.equal(credential.password, options.password, "password matches");
+ assert.equal(credential.realm, options.realm, "realm matches");
+ assert.equal(credential.formSubmitURL, null,
+ "`formSubmitURL` is `null` for site authentication credentials");
+ assert.equal(credential.usernameField, "", "usernameField is empty");
+ assert.equal(credential.passwordField, "", "passwordField is empty");
+
+ remove(search(options)[0]);
+ assert.ok(!search(options).length, "remove worked");
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-passwords.js b/addon-sdk/source/test/test-passwords.js
new file mode 100644
index 000000000..dc33cfbb7
--- /dev/null
+++ b/addon-sdk/source/test/test-passwords.js
@@ -0,0 +1,280 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { store, search, remove } = require("sdk/passwords");
+
+exports["test store requires `password` field"] = function(assert, done) {
+ store({
+ username: "foo",
+ realm: "bar",
+ onComplete: function onComplete() {
+ assert.fail("onComplete should not be called");
+ },
+ onError: function onError() {
+ assert.pass("'`password` is required");
+ done();
+ }
+ });
+};
+
+exports["test store requires `username` field"] = function(assert, done) {
+ store({
+ password: "foo",
+ realm: "bar",
+ onComplete: function onComplete() {
+ assert.fail("onComplete should not be called");
+ },
+ onError: function onError() {
+ assert.pass("'`username` is required");
+ done();
+ }
+ });
+};
+
+exports["test onComplete is optional"] = function(assert, done) {
+ store({
+ realm: "bla",
+ username: "bla",
+ password: "bla",
+ onError: function onError() {
+ assert.fail("onError was called");
+ }
+ });
+ assert.pass("exception is not thrown if `onComplete is missing")
+ done();
+};
+
+exports["test exceptions in onComplete are reported"] = function(assert, done) {
+ store({
+ realm: "throws",
+ username: "error",
+ password: "boom!",
+ onComplete: function onComplete(error) {
+ throw new Error("Boom!")
+ },
+ onError: function onError(error) {
+ assert.equal(error.message, "Boom!", "Error thrown is reported");
+ done();
+ }
+ });
+};
+
+exports["test store requires `realm` field"] = function(assert, done) {
+ store({
+ username: "foo",
+ password: "bar",
+ onComplete: function onComplete() {
+ assert.fail("onComplete should not be called");
+ },
+ onError: function onError() {
+ assert.pass("'`realm` is required");
+ done();
+ }
+ });
+};
+
+exports["test can't store same login twice"] = function(assert, done) {
+ store({
+ username: "user",
+ password: "pass",
+ realm: "realm",
+ onComplete: function onComplete() {
+ assert.pass("credential saved");
+
+ store({
+ username: "user",
+ password: "pass",
+ realm: "realm",
+ onComplete: function onComplete() {
+ assert.fail("onComplete should not be called");
+ },
+ onError: function onError() {
+ assert.pass("re-saving credential failed");
+
+ remove({
+ username: "user",
+ password: "pass",
+ realm: "realm",
+ onComplete: function onComplete() {
+ assert.pass("credential was removed");
+ done();
+ },
+ onError: function onError() {
+ assert.fail("remove should not fail");
+ }
+ });
+ }
+ });
+ },
+ onError: function onError() {
+ assert.fail("onError should not be called");
+ }
+ });
+};
+
+exports["test remove fails if no login found"] = function(assert, done) {
+ remove({
+ username: "foo",
+ password: "bar",
+ realm: "baz",
+ onComplete: function onComplete() {
+ assert.fail("should not be able to remove unstored credentials");
+ },
+ onError: function onError() {
+ assert.pass("can't remove unstored credentials");
+ done();
+ }
+ });
+};
+
+exports["test addon associated credentials"] = function(assert, done) {
+ store({
+ username: "foo",
+ password: "bar",
+ realm: "baz",
+ onComplete: function onComplete() {
+ search({
+ username: "foo",
+ password: "bar",
+ realm: "baz",
+ onComplete: function onComplete([credential]) {
+ assert.equal(credential.url.indexOf("addon:"), 0,
+ "`addon:` uri is used for add-on credentials");
+ assert.equal(credential.username, "foo",
+ "username matches");
+ assert.equal(credential.password, "bar",
+ "password matches");
+ assert.equal(credential.realm, "baz", "realm matches");
+ assert.equal(credential.formSubmitURL, null,
+ "`formSubmitURL` is `null` for add-on credentials");
+ assert.equal(credential.usernameField, "", "usernameField is empty");
+ assert.equal(credential.passwordField, "", "passwordField is empty");
+
+ remove({
+ username: credential.username,
+ password: credential.password,
+ realm: credential.realm,
+ onComplete: function onComplete() {
+ assert.pass("credential is removed");
+ done();
+ },
+ onError: function onError() {
+ assert.fail("onError should not be called");
+ }
+ });
+ },
+ onError: function onError() {
+ assert.fail("onError should not be called");
+ }
+ });
+ },
+ onError: function onError() {
+ assert.fail("onError should not be called");
+ }
+ });
+};
+
+exports["test web page associated credentials"] = function(assert, done) {
+ store({
+ url: "http://bar.foo.com/authentication/?login",
+ formSubmitURL: "http://login.foo.com/authenticate.cgi",
+ username: "user",
+ password: "pass",
+ usernameField: "user-f",
+ passwordField: "pass-f",
+ onComplete: function onComplete() {
+ search({
+ username: "user",
+ password: "pass",
+ url: "http://bar.foo.com",
+ formSubmitURL: "http://login.foo.com",
+ onComplete: function onComplete([credential]) {
+ assert.equal(credential.url, "http://bar.foo.com", "url matches");
+ assert.equal(credential.username, "user", "username matches");
+ assert.equal(credential.password, "pass", "password matches");
+ assert.equal(credential.realm, null, "realm is null");
+ assert.equal(credential.formSubmitURL, "http://login.foo.com",
+ "formSubmitURL matches");
+ assert.equal(credential.usernameField, "user-f",
+ "usernameField is matches");
+ assert.equal(credential.passwordField, "pass-f",
+ "passwordField matches");
+
+ remove({
+ url: credential.url,
+ formSubmitURL: credential.formSubmitURL,
+ username: credential.username,
+ password: credential.password,
+ usernameField: credential.usernameField,
+ passwordField: credential.passwordField,
+
+ onComplete: function onComplete() {
+ assert.pass("credential is removed");
+ done();
+ },
+ onError: function onError(e) {
+ assert.fail("onError should not be called");
+ }
+ });
+ },
+ onError: function onError() {
+ assert.fail("onError should not be called");
+ }
+ });
+ },
+ onError: function onError() {
+ assert.fail("onError should not be called");
+ }
+ });
+};
+
+exports["test site authentication credentials"] = function(assert, done) {
+ store({
+ url: "http://authentication.com",
+ username: "U",
+ password: "P",
+ realm: "R",
+ onComplete: function onComplete() {
+ search({
+ url: "http://authentication.com",
+ username: "U",
+ password: "P",
+ realm: "R",
+ onComplete: function onComplete([credential]) {
+ assert.equal(credential.url,"http://authentication.com",
+ "url matches");
+ assert.equal(credential.username, "U", "username matches");
+ assert.equal(credential.password, "P", "password matches");
+ assert.equal(credential.realm, "R", "realm matches");
+ assert.equal(credential.formSubmitURL, null, "formSubmitURL is null");
+ assert.equal(credential.usernameField, "", "usernameField is empty");
+ assert.equal(credential.passwordField, "", "passwordField is empty");
+
+ remove({
+ url: credential.url,
+ username: credential.username,
+ password: credential.password,
+ realm: credential.realm,
+ onComplete: function onComplete() {
+ assert.pass("credential is removed");
+ done();
+ },
+ onError: function onError() {
+ assert.fail("onError should not be called");
+ }
+ });
+ },
+ onError: function onError() {
+ assert.fail("onError should not be called");
+ }
+ });
+ },
+ onError: function onError() {
+ assert.fail("onError should not be called");
+ }
+ });
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-path.js b/addon-sdk/source/test/test-path.js
new file mode 100644
index 000000000..7211def46
--- /dev/null
+++ b/addon-sdk/source/test/test-path.js
@@ -0,0 +1,6 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+module.exports = require("./path/test-path.js");
diff --git a/addon-sdk/source/test/test-plain-text-console.js b/addon-sdk/source/test/test-plain-text-console.js
new file mode 100644
index 000000000..d55328e34
--- /dev/null
+++ b/addon-sdk/source/test/test-plain-text-console.js
@@ -0,0 +1,278 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const prefs = require("sdk/preferences/service");
+const { id, name } = require("sdk/self");
+const { Cc, Cu, Ci } = require("chrome");
+const { loadSubScript } = Cc['@mozilla.org/moz/jssubscript-loader;1'].
+ getService(Ci.mozIJSSubScriptLoader);
+
+const ADDON_LOG_LEVEL_PREF = "extensions." + id + ".sdk.console.logLevel";
+const SDK_LOG_LEVEL_PREF = "extensions.sdk.console.logLevel";
+
+const HAS_ORIGINAL_ADDON_LOG_LEVEL = prefs.has(ADDON_LOG_LEVEL_PREF);
+const ORIGINAL_ADDON_LOG_LEVEL = prefs.get(ADDON_LOG_LEVEL_PREF);
+const HAS_ORIGINAL_SDK_LOG_LEVEL = prefs.has(SDK_LOG_LEVEL_PREF);
+const ORIGINAL_SDK_LOG_LEVEL = prefs.get(SDK_LOG_LEVEL_PREF);
+
+exports.testPlainTextConsole = function(assert) {
+ let prints = [];
+ let tbLines;
+ function print(message) {
+ prints.push(message);
+ }
+ function lastPrint() {
+ let last = prints.slice(-1)[0];
+ prints = [];
+ return last;
+ }
+
+ prefs.set(SDK_LOG_LEVEL_PREF, "all");
+ prefs.reset(ADDON_LOG_LEVEL_PREF);
+
+ let Console = require("sdk/console/plain-text").PlainTextConsole;
+ let con = new Console(print);
+
+ assert.ok("PlainTextConsole instantiates");
+
+ con.log('testing', 1, [2, 3, 4]);
+ assert.equal(lastPrint(), "console.log: " + name + ": testing 1 Array [2,3,4]\n",
+ "PlainTextConsole.log() must work.");
+
+ con.info('testing', 1, [2, 3, 4]);
+ assert.equal(lastPrint(), "console.info: " + name + ": testing 1 Array [2,3,4]\n",
+ "PlainTextConsole.info() must work.");
+
+ con.warn('testing', 1, [2, 3, 4]);
+ assert.equal(lastPrint(), "console.warn: " + name + ": testing 1 Array [2,3,4]\n",
+ "PlainTextConsole.warn() must work.");
+
+ con.error('testing', 1, [2, 3, 4]);
+ assert.equal(prints[0], "console.error: " + name + ": \n",
+ "PlainTextConsole.error() must work.");
+ assert.equal(prints[1], " testing\n")
+ assert.equal(prints[2], " 1\n")
+ assert.equal(prints[3], "Array\n - 0 = 2\n - 1 = 3\n - 2 = 4\n - length = 3\n");
+ prints = [];
+
+ con.debug('testing', 1, [2, 3, 4]);
+ assert.equal(prints[0], "console.debug: " + name + ": \n",
+ "PlainTextConsole.debug() must work.");
+ assert.equal(prints[1], " testing\n")
+ assert.equal(prints[2], " 1\n")
+ assert.equal(prints[3], "Array\n - 0 = 2\n - 1 = 3\n - 2 = 4\n - length = 3\n");
+ prints = [];
+
+ con.log('testing', undefined);
+ assert.equal(lastPrint(), "console.log: " + name + ": testing undefined\n",
+ "PlainTextConsole.log() must stringify undefined.");
+
+ con.log('testing', null);
+ assert.equal(lastPrint(), "console.log: " + name + ": testing null\n",
+ "PlainTextConsole.log() must stringify null.");
+
+ // TODO: Fix console.jsm to detect custom toString.
+ con.log("testing", { toString: () => "obj.toString()" });
+ assert.equal(lastPrint(), "console.log: " + name + ": testing {}\n",
+ "PlainTextConsole.log() doesn't printify custom toString.");
+
+ con.log("testing", { toString: function() { throw "fail!"; } });
+ assert.equal(lastPrint(), "console.log: " + name + ": testing {}\n",
+ "PlainTextConsole.log() must stringify custom bad toString.");
+
+ con.exception(new Error("blah"));
+
+ assert.equal(prints[0], "console.error: " + name + ": \n", "prints[0] is correct");
+ tbLines = prints[1].split("\n");
+ assert.equal(tbLines[0], " Message: Error: blah", "tbLines[0] is correct");
+ assert.equal(tbLines[1], " Stack:", "tbLines[1] is correct");
+ let lineNumber = prints[1].match(module.uri + ":(\\d+)")[1];
+ assert.equal(lineNumber, "84", "line number is correct")
+ assert.ok(prints[1].indexOf(module.uri + ":84") !== -1, "line number is correct");
+ prints = []
+
+ try {
+ loadSubScript("invalid-url", {});
+ assert.fail("successed in calling loadSubScript with invalid-url");
+ }
+ catch(e) {
+ con.exception(e);
+ }
+ assert.equal(prints[0], "console.error: " + name + ": \n", "prints[0] is correct");
+ assert.equal(prints[1], " Error creating URI (invalid URL scheme?)\n", "prints[1] is correct");
+ prints = [];
+
+ con.trace();
+ tbLines = prints[0].split("\n");
+ assert.equal(tbLines[0], "console.trace: " + name + ": ", "contains correct console.trace");
+ assert.ok(tbLines[1].indexOf("_ain-text-console.js 106") == 0);
+ prints = [];
+
+ // Whether or not console methods should print at the various log levels,
+ // structured as a hash of levels, each of which contains a hash of methods,
+ // each of whose value is whether or not it should print, i.e.:
+ // { [level]: { [method]: [prints?], ... }, ... }.
+ let levels = {
+ all: { debug: true, log: true, info: true, warn: true, error: true },
+ debug: { debug: true, log: true, info: true, warn: true, error: true },
+ info: { debug: false, log: true, info: true, warn: true, error: true },
+ warn: { debug: false, log: false, info: false, warn: true, error: true },
+ error: { debug: false, log: false, info: false, warn: false, error: true },
+ off: { debug: false, log: false, info: false, warn: false, error: false },
+ };
+
+ // The messages we use to test the various methods, as a hash of methods.
+ let messages = {
+ debug: "console.debug: " + name + ": \n \n",
+ log: "console.log: " + name + ": \n",
+ info: "console.info: " + name + ": \n",
+ warn: "console.warn: " + name + ": \n",
+ error: "console.error: " + name + ": \n \n",
+ };
+
+ for (let level in levels) {
+ let methods = levels[level];
+ for (let method in methods) {
+ // We have to reset the log level pref each time we run the test
+ // because the test runner relies on the console to print test output,
+ // and test results would not get printed to the console for some
+ // values of the pref.
+ prefs.set(SDK_LOG_LEVEL_PREF, level);
+ con[method]("");
+ prefs.set(SDK_LOG_LEVEL_PREF, "all");
+ assert.equal(prints.join(""),
+ (methods[method] ? messages[method] : ""),
+ "at log level '" + level + "', " + method + "() " +
+ (methods[method] ? "prints" : "doesn't print"));
+ prints = [];
+ }
+ }
+
+ prefs.set(SDK_LOG_LEVEL_PREF, "off");
+ prefs.set(ADDON_LOG_LEVEL_PREF, "all");
+ con.debug("");
+ assert.equal(prints.join(""), messages["debug"],
+ "addon log level 'all' overrides SDK log level 'off'");
+ prints = [];
+
+ prefs.set(SDK_LOG_LEVEL_PREF, "all");
+ prefs.set(ADDON_LOG_LEVEL_PREF, "off");
+ con.error("");
+ prefs.reset(ADDON_LOG_LEVEL_PREF);
+ assert.equal(lastPrint(), null,
+ "addon log level 'off' overrides SDK log level 'all'");
+
+ restorePrefs();
+};
+
+exports.testPlainTextConsoleBoundMethods = function(assert) {
+ let prints = [];
+ let tbLines;
+ function print(message) {
+ prints.push(message);
+ }
+ function lastPrint() {
+ let last = prints.slice(-1)[0];
+ prints = [];
+ return last;
+ }
+
+ prefs.set(SDK_LOG_LEVEL_PREF, "all");
+ prefs.reset(ADDON_LOG_LEVEL_PREF);
+
+ let Console = require("sdk/console/plain-text").PlainTextConsole;
+ let { log, info, warn, error, debug, exception, trace } = new Console(print);
+
+ assert.ok("PlainTextConsole instantiates");
+
+ log('testing', 1, [2, 3, 4]);
+ assert.equal(lastPrint(), "console.log: " + name + ": testing 1 Array [2,3,4]\n",
+ "PlainTextConsole.log() must work.");
+
+ info('testing', 1, [2, 3, 4]);
+ assert.equal(lastPrint(), "console.info: " + name + ": testing 1 Array [2,3,4]\n",
+ "PlainTextConsole.info() must work.");
+
+ warn('testing', 1, [2, 3, 4]);
+ assert.equal(lastPrint(), "console.warn: " + name + ": testing 1 Array [2,3,4]\n",
+ "PlainTextConsole.warn() must work.");
+
+ error('testing', 1, [2, 3, 4]);
+ assert.equal(prints[0], "console.error: " + name + ": \n",
+ "PlainTextConsole.error() must work.");
+ assert.equal(prints[1], " testing\n")
+ assert.equal(prints[2], " 1\n")
+ assert.equal(prints[3], "Array\n - 0 = 2\n - 1 = 3\n - 2 = 4\n - length = 3\n");
+ prints = [];
+
+ debug('testing', 1, [2, 3, 4]);
+ assert.equal(prints[0], "console.debug: " + name + ": \n",
+ "PlainTextConsole.debug() must work.");
+ assert.equal(prints[1], " testing\n", "prints[1] is correct");
+ assert.equal(prints[2], " 1\n", "prints[2] is correct");
+ assert.equal(prints[3],
+ "Array\n - 0 = 2\n - 1 = 3\n - 2 = 4\n - length = 3\n",
+ "prints[3] is correct");
+ prints = [];
+
+ exception(new Error("blah"));
+
+ assert.equal(prints[0], "console.error: " + name + ": \n", "prints[0] is correct");
+ tbLines = prints[1].split("\n");
+ assert.equal(tbLines[0], " Message: Error: blah", "tbLines[0] is correct");
+ assert.equal(tbLines[1], " Stack:", "tbLines[1] is correct");
+ assert.ok(prints[1].indexOf(module.uri + ":219") !== -1, "correct line number");
+ prints = []
+
+ trace();
+ tbLines = prints[0].split("\n");
+ assert.equal(tbLines[0], "console.trace: " + name + ": ", "console.trace is correct");
+ assert.ok(tbLines[1].indexOf("_ain-text-console.js 228") === 0, "correct line number");
+ prints = [];
+
+ restorePrefs();
+};
+
+exports.testConsoleInnerID = function(assert) {
+ let Console = require("sdk/console/plain-text").PlainTextConsole;
+ let { log, info, warn, error, debug, exception, trace } = new Console(function() {}, "test ID");
+
+ prefs.set(SDK_LOG_LEVEL_PREF, "all");
+
+ let messages = [];
+ function onMessage({ subject }) {
+ let message = subject.wrappedJSObject;
+ messages.push({ msg: message.arguments[0], type: message.level, innerID: message.innerID });
+ }
+
+ const system = require("sdk/system/events");
+ system.on("console-api-log-event", onMessage);
+
+ log("Test log");
+ warn("Test warning");
+ error("Test error");
+
+ assert.equal(messages.length, 3, "Should see 3 log events");
+ assert.deepEqual(messages[0], { msg: "Test log", type: "log", innerID: "test ID" }, "Should see the right event");
+ assert.deepEqual(messages[1], { msg: "Test warning", type: "warn", innerID: "test ID" }, "Should see the right event");
+ assert.deepEqual(messages[2], { msg: "Test error", type: "error", innerID: "test ID" }, "Should see the right event");
+
+ system.off("console-api-log-event", onMessage);
+
+ restorePrefs();
+};
+
+function restorePrefs() {
+ if (HAS_ORIGINAL_ADDON_LOG_LEVEL)
+ prefs.set(ADDON_LOG_LEVEL_PREF, ORIGINAL_ADDON_LOG_LEVEL);
+ else
+ prefs.reset(ADDON_LOG_LEVEL_PREF);
+
+ if (HAS_ORIGINAL_SDK_LOG_LEVEL)
+ prefs.set(SDK_LOG_LEVEL_PREF, ORIGINAL_SDK_LOG_LEVEL);
+ else
+ prefs.reset(SDK_LOG_LEVEL_PREF);
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-preferences-service.js b/addon-sdk/source/test/test-preferences-service.js
new file mode 100644
index 000000000..08227eafc
--- /dev/null
+++ b/addon-sdk/source/test/test-preferences-service.js
@@ -0,0 +1,155 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Cc, Ci, Cu } = require("chrome");
+const prefs = require("sdk/preferences/service");
+const Branch = prefs.Branch;
+const BundleService = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService);
+
+const specialChars = "!@#$%^&*()_-=+[]{}~`\'\"<>,./?;:";
+
+exports.testReset = function(assert) {
+ prefs.reset("test_reset_pref");
+ assert.equal(prefs.has("test_reset_pref"), false);
+ assert.equal(prefs.isSet("test_reset_pref"), false);
+ prefs.set("test_reset_pref", 5);
+ assert.equal(prefs.has("test_reset_pref"), true);
+ assert.equal(prefs.isSet("test_reset_pref"), true);
+ assert.equal(prefs.keys("test_reset_pref").toString(), "test_reset_pref");
+};
+
+exports.testGetAndSet = function(assert) {
+ let svc = Cc["@mozilla.org/preferences-service;1"].
+ getService(Ci.nsIPrefService).
+ getBranch(null);
+ svc.setCharPref("test_set_get_pref", "a normal string");
+ assert.equal(prefs.get("test_set_get_pref"), "a normal string",
+ "preferences-service should read from " +
+ "application-wide preferences service");
+
+ // test getting a pref that does not exist,
+ // and where we provide no default
+ assert.equal(
+ prefs.get("test_dne_get_pref", "default"),
+ "default",
+ "default was used for a pref that does not exist");
+ assert.equal(
+ prefs.get("test_dne_get_pref"),
+ undefined,
+ "undefined was returned for a pref that does not exist with no default");
+
+ prefs.set("test_set_get_pref.integer", 1);
+ assert.equal(prefs.get("test_set_get_pref.integer"), 1,
+ "set/get integer preference should work");
+
+ assert.equal(
+ prefs.keys("test_set_get_pref").sort().toString(),
+ ["test_set_get_pref.integer","test_set_get_pref"].sort().toString(),
+ "the key list is correct");
+
+ prefs.set("test_set_get_number_pref", 42);
+ assert.throws(
+ () => prefs.set("test_set_get_number_pref", 3.14159),
+ /cannot store non-integer number: 3.14159/,
+ "setting a float preference should raise an error"
+ );
+ assert.equal(prefs.get("test_set_get_number_pref"),
+ 42,
+ "bad-type write attempt should not overwrite");
+
+ // 0x80000000 (bad), 0x7fffffff (ok), -0x80000000 (ok), -0x80000001 (bad)
+ assert.throws(
+ () => prefs.set("test_set_get_number_pref", 0x80000000),
+ /32\-bit/,
+ "setting an int pref above 2^31-1 shouldn't work"
+ );
+
+ assert.equal(prefs.get("test_set_get_number_pref"), 42,
+ "out-of-range write attempt should not overwrite 1");
+
+ prefs.set("test_set_get_number_pref", 0x7fffffff);
+ assert.equal(prefs.get("test_set_get_number_pref"),
+ 0x7fffffff,
+ "in-range write attempt should work 1");
+
+ prefs.set("test_set_get_number_pref", -0x80000000);
+ assert.equal(prefs.get("test_set_get_number_pref"),
+ -0x80000000,
+ "in-range write attempt should work 2");
+ assert.throws(
+ () => prefs.set("test_set_get_number_pref", -0x80000001),
+ /32\-bit/,
+ "setting an int pref below -(2^31) shouldn't work"
+ );
+ assert.equal(prefs.get("test_set_get_number_pref"), -0x80000000,
+ "out-of-range write attempt should not overwrite 2");
+
+
+ prefs.set("test_set_get_pref.string", "foo");
+ assert.equal(prefs.get("test_set_get_pref.string"), "foo",
+ "set/get string preference should work");
+
+ prefs.set("test_set_get_pref.boolean", true);
+ assert.equal(prefs.get("test_set_get_pref.boolean"), true,
+ "set/get boolean preference should work");
+
+ prefs.set("test_set_get_unicode_pref", String.fromCharCode(960));
+ assert.equal(prefs.get("test_set_get_unicode_pref"),
+ String.fromCharCode(960),
+ "set/get unicode preference should work");
+
+ [ null, [], undefined ].forEach((value) => {
+ assert.throws(
+ () => prefs.set("test_set_pref", value),
+ new RegExp("can't set pref test_set_pref to value '" + value + "'; " +
+ "it isn't a string, number, or boolean", "i"),
+ "Setting a pref to " + uneval(value) + " should raise error"
+ );
+ });
+};
+
+exports.testPrefClass = function(assert) {
+ var branch = Branch("test_foo");
+
+ assert.equal(branch.test, undefined, "test_foo.test is undefined");
+ branch.test = true;
+ assert.equal(branch.test, true, "test_foo.test is true");
+ delete branch.test;
+ assert.equal(branch.test, undefined, "test_foo.test is undefined");
+};
+
+exports.testGetSetLocalized = function(assert) {
+ let prefName = "general.useragent.locale";
+
+ // Ensure that "general.useragent.locale" is a 'localized' pref
+ let bundleURL = "chrome://global/locale/intl.properties";
+ prefs.setLocalized(prefName, bundleURL);
+
+ // Fetch the expected value directly from the property file
+ let expectedValue = BundleService.createBundle(bundleURL).
+ GetStringFromName(prefName).
+ toLowerCase();
+
+ assert.equal(prefs.getLocalized(prefName).toLowerCase(),
+ expectedValue,
+ "get localized preference");
+
+ // Undo our modification
+ prefs.reset(prefName);
+}
+
+// TEST: setting and getting preferences with special characters work
+exports.testSpecialChars = function(assert) {
+ let chars = specialChars.split('');
+ const ROOT = "test.";
+
+ chars.forEach((char) => {
+ let rand = Math.random() + "";
+ prefs.set(ROOT + char, rand);
+ assert.equal(prefs.get(ROOT+char), rand, "setting pref with a name that is a special char, " + char + ", worked!");
+ });
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-preferences-target.js b/addon-sdk/source/test/test-preferences-target.js
new file mode 100644
index 000000000..a534bc7ab
--- /dev/null
+++ b/addon-sdk/source/test/test-preferences-target.js
@@ -0,0 +1,42 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { PrefsTarget } = require('sdk/preferences/event-target');
+const { get, set, reset } = require('sdk/preferences/service');
+const { Loader } = require('sdk/test/loader');
+const { setTimeout } = require('sdk/timers');
+
+const root = PrefsTarget();
+
+exports.testPrefsTarget = function(assert, done) {
+ let loader = Loader(module);
+ let pt = loader.require('sdk/preferences/event-target').PrefsTarget({});
+ let name = 'test';
+
+ assert.equal(get(name, ''), '', 'test pref is blank');
+
+ pt.once(name, function() {
+ assert.equal(pt.prefs[name], 2, 'test pref is 2');
+
+ pt.once(name, function() {
+ assert.fail('should not have heard a pref change');
+ });
+ loader.unload();
+ root.once(name, function() {
+ assert.pass('test pref was changed');
+ reset(name);
+
+ // NOTE: using setTimeout to make sure that the other listener had
+ // a chance to fail
+ // end test
+ setTimeout(done);
+ });
+ set(name, 3);
+ });
+
+ pt.prefs[name] = 2;
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-private-browsing.js b/addon-sdk/source/test/test-private-browsing.js
new file mode 100644
index 000000000..3af89afe2
--- /dev/null
+++ b/addon-sdk/source/test/test-private-browsing.js
@@ -0,0 +1,88 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Ci, Cu } = require('chrome');
+const { safeMerge } = require('sdk/util/object');
+const windows = require('sdk/windows').browserWindows;
+const tabs = require('sdk/tabs');
+const winUtils = require('sdk/window/utils');
+const { isWindowPrivate } = winUtils;
+const { isPrivateBrowsingSupported } = require('sdk/self');
+const { is } = require('sdk/system/xul-app');
+const { isPrivate } = require('sdk/private-browsing');
+const { LoaderWithHookedConsole } = require("sdk/test/loader");
+const { getMode, isWindowPBSupported, isTabPBSupported } = require('sdk/private-browsing/utils');
+const { pb } = require('./private-browsing/helper');
+const prefs = require('sdk/preferences/service');
+
+const { NetUtil } = Cu.import("resource://gre/modules/NetUtil.jsm", {});
+
+const kAutoStartPref = "browser.privatebrowsing.autostart";
+
+if (isWindowPBSupported) {
+ safeMerge(module.exports, require('./private-browsing/windows'));
+
+ exports.testPWOnlyOnFirefox = function(assert) {
+ assert.ok(is("Firefox"), "isWindowPBSupported is only true on Firefox");
+ }
+}
+// only on Fennec
+else if (isTabPBSupported) {
+ safeMerge(module.exports, require('./private-browsing/tabs'));
+
+ exports.testPTOnlyOnFennec = function(assert) {
+ assert.ok(is("Fennec"), "isTabPBSupported is only true on Fennec");
+ }
+}
+
+exports.testIsPrivateDefaults = function(assert) {
+ assert.equal(isPrivate(), false, 'undefined is not private');
+ assert.equal(isPrivate('test'), false, 'strings are not private');
+ assert.equal(isPrivate({}), false, 'random objects are not private');
+ assert.equal(isPrivate(4), false, 'numbers are not private');
+ assert.equal(isPrivate(/abc/), false, 'regex are not private');
+ assert.equal(isPrivate(function() {}), false, 'functions are not private');
+};
+
+exports.testWindowDefaults = function(assert) {
+ // Ensure that browserWindow still works while being deprecated
+ let { loader, messages } = LoaderWithHookedConsole(module);
+ let windows = loader.require("sdk/windows").browserWindows;
+ assert.equal(windows.activeWindow.isPrivateBrowsing, undefined,
+ 'window.isPrivateBrowsing is undefined');
+ assert.equal(undefined, messages[0],
+ 'isPrivateBrowsing is deprecated');
+
+ let chromeWin = winUtils.getMostRecentBrowserWindow();
+ assert.equal(getMode(chromeWin), false);
+ assert.equal(isWindowPrivate(chromeWin), false);
+};
+
+exports.testIsPrivateBrowsingFalseDefault = function(assert) {
+ assert.equal(isPrivateBrowsingSupported, false,
+ 'isPrivateBrowsingSupported property is false by default');
+};
+
+exports.testNSIPrivateBrowsingChannel = function(assert) {
+ let channel = NetUtil.newChannel({
+ uri: "about:blank",
+ loadUsingSystemPrincipal: true
+ });
+ channel.QueryInterface(Ci.nsIPrivateBrowsingChannel);
+ assert.equal(isPrivate(channel), false, 'isPrivate detects non-private channels');
+ channel.setPrivate(true);
+ assert.ok(isPrivate(channel), 'isPrivate detects private channels');
+}
+
+exports.testNewGlobalPBService = function(assert) {
+ assert.equal(isPrivate(), false, 'isPrivate() is false by default');
+ prefs.set(kAutoStartPref, true);
+ assert.equal(prefs.get(kAutoStartPref, false), true, kAutoStartPref + ' is true now');
+ assert.equal(isPrivate(), true, 'isPrivate() is true now');
+ prefs.set(kAutoStartPref, false);
+ assert.equal(isPrivate(), false, 'isPrivate() is false again');
+};
+
+require('sdk/test').run(module.exports);
diff --git a/addon-sdk/source/test/test-promise.js b/addon-sdk/source/test/test-promise.js
new file mode 100644
index 000000000..1348e6cbf
--- /dev/null
+++ b/addon-sdk/source/test/test-promise.js
@@ -0,0 +1,461 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Cc, Cu, Ci } = require('chrome');
+const { setTimeout } = require('sdk/timers');
+const { prefixURI, name } = require('@loader/options');
+const addonPromiseURI = prefixURI + name + '/lib/sdk/core/promise.js';
+const builtPromiseURI = 'resource://gre/modules/commonjs/sdk/core/promise.js';
+var { Promise, defer, resolve, reject, all, promised } = require('sdk/core/promise');
+
+exports['test all observers are notified'] = function(assert, done) {
+ let expected = 'Taram pam param!';
+ let deferred = defer();
+ let pending = 10, i = 0;
+
+ function resolved(value) {
+ assert.equal(value, expected, 'value resolved as expected: #' + pending);
+ if (!--pending) done();
+ }
+
+ while (i++ < pending) deferred.promise.then(resolved);
+
+ deferred.resolve(expected);
+};
+
+exports['test exceptions dont stop notifications'] = function(assert, done) {
+ let threw = false, boom = Error('Boom!');
+ let deferred = defer();
+
+ let promise2 = deferred.promise.then(function() {
+ threw = true;
+ throw boom;
+ });
+
+ deferred.promise.then(function() {
+ assert.ok(threw, 'observer is called even though previos one threw');
+ promise2.then(function() {
+ assert.fail('should not resolve');
+ }, function(reason) {
+ assert.equal(reason, boom, 'rejects to thrown error');
+ done();
+ });
+ });
+
+ deferred.resolve('go!');
+};
+
+exports['test subsequent resolves are ignored'] = function(assert, done) {
+ let deferred = defer();
+ deferred.resolve(1);
+ deferred.resolve(2);
+ deferred.reject(3);
+
+ deferred.promise.then(function(actual) {
+ assert.equal(actual, 1, 'resolves to first value');
+ }, function() {
+ assert.fail('must not reject');
+ });
+ deferred.promise.then(function(actual) {
+ assert.equal(actual, 1, 'subsequent resolutions are ignored');
+ done();
+ }, function() {
+ assert.fail('must not reject');
+ });
+};
+
+exports['test subsequent rejections are ignored'] = function(assert, done) {
+ let deferred = defer();
+ deferred.reject(1);
+ deferred.resolve(2);
+ deferred.reject(3);
+
+ deferred.promise.then(function(actual) {
+ assert.fail('must not resolve');
+ }, function(actual) {
+ assert.equal(actual, 1, 'must reject to first');
+ });
+ deferred.promise.then(function(actual) {
+ assert.fail('must not resolve');
+ }, function(actual) {
+ assert.equal(actual, 1, 'must reject to first');
+ done();
+ });
+};
+
+exports['test error recovery'] = function(assert, done) {
+ let boom = Error('Boom!');
+ let deferred = defer();
+
+ deferred.promise.then(function() {
+ assert.fail('rejected promise should not resolve');
+ }, function(reason) {
+ assert.equal(reason, boom, 'rejection reason delivered');
+ return 'recovery';
+ }).then(function(value) {
+ assert.equal(value, 'recovery', 'error handled by a handler');
+ done();
+ });
+
+ deferred.reject(boom);
+};
+
+exports['test error recovery with promise'] = function(assert, done) {
+ let deferred = defer();
+
+ deferred.promise.then(function() {
+ assert.fail('must reject');
+ }, function(actual) {
+ assert.equal(actual, 'reason', 'rejected');
+ let deferred = defer();
+ deferred.resolve('recovery');
+ return deferred.promise;
+ }).then(function(actual) {
+ assert.equal(actual, 'recovery', 'recorvered via promise');
+ let deferred = defer();
+ deferred.reject('error');
+ return deferred.promise;
+ }).then(null, function(actual) {
+ assert.equal(actual, 'error', 'rejected via promise');
+ let deferred = defer();
+ deferred.reject('end');
+ return deferred.promise;
+ }).then(null, function(actual) {
+ assert.equal(actual, 'end', 'rejeced via promise');
+ done();
+ });
+
+ deferred.reject('reason');
+};
+
+exports['test propagation'] = function(assert, done) {
+ let d1 = defer(), d2 = defer(), d3 = defer();
+
+ d1.promise.then(function(actual) {
+ assert.equal(actual, 'expected', 'resolves to expected value');
+ done();
+ });
+
+ d1.resolve(d2.promise);
+ d2.resolve(d3.promise);
+ d3.resolve('expected');
+};
+
+exports['test chaining'] = function(assert, done) {
+ let boom = Error('boom'), brax = Error('braxXXx');
+ let deferred = defer();
+
+ deferred.promise.then().then().then(function(actual) {
+ assert.equal(actual, 2, 'value propagates unchanged');
+ return actual + 2;
+ }).then(null, function(reason) {
+ assert.fail('should not reject');
+ }).then(function(actual) {
+ assert.equal(actual, 4, 'value propagates through if not handled');
+ throw boom;
+ }).then(function(actual) {
+ assert.fail('exception must reject promise');
+ }).then().then(null, function(actual) {
+ assert.equal(actual, boom, 'reason propagates unchanged');
+ throw brax;
+ }).then().then(null, function(actual) {
+ assert.equal(actual, brax, 'reason changed becase of exception');
+ return 'recovery';
+ }).then(function(actual) {
+ assert.equal(actual, 'recovery', 'recovered from error');
+ done();
+ });
+
+ deferred.resolve(2);
+};
+
+exports['test reject'] = function(assert, done) {
+ let expected = Error('boom');
+
+ reject(expected).then(function() {
+ assert.fail('should reject');
+ }, function(actual) {
+ assert.equal(actual, expected, 'rejected with expected reason');
+ }).then(done, assert.fail);
+};
+
+exports['test resolve to rejected'] = function(assert, done) {
+ let expected = Error('boom');
+ let deferred = defer();
+
+ deferred.promise.then(function() {
+ assert.fail('should reject');
+ }, function(actual) {
+ assert.equal(actual, expected, 'rejected with expected failure');
+ }).then(done, assert.fail);
+
+ deferred.resolve(reject(expected));
+};
+
+exports['test resolve'] = function(assert, done) {
+ let expected = 'value';
+ resolve(expected).then(function(actual) {
+ assert.equal(actual, expected, 'resolved as expected');
+ }).catch(assert.fail).then(done);
+};
+
+exports['test promised with normal args'] = function(assert, done) {
+ let sum = promised((x, y) => x + y );
+
+ sum(7, 8).then(function(actual) {
+ assert.equal(actual, 7 + 8, 'resolves as expected');
+ }).catch(assert.fail).then(done);
+};
+
+exports['test promised with promise args'] = function(assert, done) {
+ let sum = promised((x, y) => x + y );
+ let deferred = defer();
+
+ sum(11, deferred.promise).then(function(actual) {
+ assert.equal(actual, 11 + 24, 'resolved as expected');
+ }).catch(assert.fail).then(done);
+
+ deferred.resolve(24);
+};
+
+exports['test promised error handling'] = function(assert, done) {
+ let expected = Error('boom');
+ let f = promised(function() {
+ throw expected;
+ });
+
+ f().then(function() {
+ assert.fail('should reject');
+ }, function(actual) {
+ assert.equal(actual, expected, 'rejected as expected');
+ }).catch(assert.fail).then(done);
+};
+
+exports['test errors in promise resolution handlers are propagated'] = function(assert, done) {
+ var expected = Error('Boom');
+ var { promise, resolve } = defer();
+
+ promise.then(function() {
+ throw expected;
+ }).then(function() {
+ return undefined;
+ }).then(null, function(actual) {
+ assert.equal(actual, expected, 'rejected as expected');
+ }).then(done, assert.fail);
+
+ resolve({});
+};
+
+exports['test return promise form promised'] = function(assert, done) {
+ let f = promised(function() {
+ return resolve(17);
+ });
+
+ f().then(function(actual) {
+ assert.equal(actual, 17, 'resolves to a promise resolution');
+ }).catch(assert.fail).then(done);
+};
+
+exports['test promised returning failure'] = function(assert, done) {
+ let expected = Error('boom');
+ let f = promised(function() {
+ return reject(expected);
+ });
+
+ f().then(function() {
+ assert.fail('must reject');
+ }, function(actual) {
+ assert.equal(actual, expected, 'rejects with expected reason');
+ }).catch(assert.fail).then(done);
+};
+
+/*
+ * Changed for compliance in Bug 881047, promises are now always async
+ */
+exports['test promises are always async'] = function (assert, done) {
+ let runs = 0;
+ resolve(1)
+ .then(val => ++runs)
+ .catch(assert.fail).then(done);
+ assert.equal(runs, 0, 'resolutions are called in following tick');
+};
+
+/*
+ * Changed for compliance in Bug 881047, promised's are now non greedy
+ */
+exports['test promised are not greedy'] = function(assert, done) {
+ let runs = 0;
+ promised(() => ++runs)()
+ .catch(assert.fail).then(done);
+ assert.equal(runs, 0, 'promised does not run task right away');
+};
+
+exports['test promised does not flatten arrays'] = function(assert, done) {
+ let p = promised(function(empty, one, two, nested) {
+ assert.equal(empty.length, 0, "first argument is empty");
+ assert.deepEqual(one, ['one'], "second has one");
+ assert.deepEqual(two, ['two', 'more'], "third has two more");
+ assert.deepEqual(nested, [[]], "forth is properly nested");
+ done();
+ });
+
+ p([], ['one'], ['two', 'more'], [[]]);
+};
+
+exports['test arrays should not flatten'] = function(assert, done) {
+ let a = defer();
+ let b = defer();
+
+ let combine = promised(function(str, arr) {
+ assert.equal(str, 'Hello', 'Array was not flattened');
+ assert.deepEqual(arr, [ 'my', 'friend' ]);
+ });
+
+ combine(a.promise, b.promise).catch(assert.fail).then(done);
+
+
+ a.resolve('Hello');
+ b.resolve([ 'my', 'friend' ]);
+};
+
+exports['test `all` for all promises'] = function (assert, done) {
+ all([
+ resolve(5), resolve(7), resolve(10)
+ ]).then(function (val) {
+ assert.equal(
+ val[0] === 5 &&
+ val[1] === 7 &&
+ val[2] === 10
+ , true, 'return value contains resolved promises values');
+ done();
+ }, function () {
+ assert.fail('should not call reject function');
+ });
+};
+
+exports['test `all` aborts upon first reject'] = function (assert, done) {
+ all([
+ resolve(5), reject('error'), delayedResolve()
+ ]).then(function (val) {
+ assert.fail('Successful resolve function should not be called');
+ }, function (reason) {
+ assert.equal(reason, 'error', 'should reject the `all` promise');
+ done();
+ });
+
+ function delayedResolve () {
+ let deferred = defer();
+ setTimeout(deferred.resolve, 50);
+ return deferred.promise;
+ }
+};
+
+exports['test `all` with array containing non-promise'] = function (assert, done) {
+ all([
+ resolve(5), resolve(10), 925
+ ]).then(function (val) {
+ assert.equal(val[2], 925, 'non-promises should pass-through value');
+ done();
+ }, function () {
+ assert.fail('should not be rejected');
+ });
+};
+
+exports['test `all` should resolve with an empty array'] = function (assert, done) {
+ all([]).then(function (val) {
+ assert.equal(Array.isArray(val), true, 'should return array in resolved');
+ assert.equal(val.length, 0, 'array should be empty in resolved');
+ done();
+ }, function () {
+ assert.fail('should not be rejected');
+ });
+};
+
+exports['test `all` with multiple rejected'] = function (assert, done) {
+ all([
+ reject('error1'), reject('error2'), reject('error3')
+ ]).then(function (value) {
+ assert.fail('should not be successful');
+ }, function (reason) {
+ assert.equal(reason, 'error1', 'should reject on first promise reject');
+ done();
+ });
+};
+
+exports['test Promise constructor resolve'] = function (assert, done) {
+ var isAsync = true;
+ new Promise(function (resolve, reject) {
+ resolve(5);
+ }).then(x => {
+ isAsync = false;
+ assert.equal(x, 5, 'Promise constructor resolves correctly');
+ }).catch(assert.fail).then(done);
+ assert.ok(isAsync, 'Promise constructor runs async');
+};
+
+exports['test Promise constructor reject'] = function (assert, done) {
+ new Promise(function (resolve, reject) {
+ reject(new Error('deferred4life'));
+ }).then(assert.fail, (err) => {
+ assert.equal(err.message, 'deferred4life', 'Promise constructor rejects correctly');
+ }).catch(assert.fail).then(done);
+};
+
+exports['test JSM Load and API'] = function (assert, done) {
+ // Use addon URL when loading from cfx/local:
+ // resource://90111c90-c31e-4dc7-ac35-b65947434435-at-jetpack/addon-sdk/lib/sdk/core/promise.js
+ // Use built URL when testing on try, etc.
+ // resource://gre/modules/commonjs/sdk/core/promise.js
+ try {
+ var { Promise } = Cu.import(addonPromiseURI, {});
+ } catch (e) {
+ var { Promise } = Cu.import(builtPromiseURI, {});
+ }
+ testEnvironment(Promise, assert, done, 'JSM');
+};
+
+exports['test mozIJSSubScriptLoader exporting'] = function (assert, done) {
+ let { Services } = Cu.import('resource://gre/modules/Services.jsm', {});
+ let systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
+ let Promise = new Cu.Sandbox(systemPrincipal);
+ let loader = Cc['@mozilla.org/moz/jssubscript-loader;1']
+ .getService(Ci.mozIJSSubScriptLoader);
+
+ // Use addon URL when loading from cfx/local:
+ // resource://90111c90-c31e-4dc7-ac35-b65947434435-at-jetpack/addon-sdk/lib/sdk/core/promise.js
+ // Use built URL when testing on try, etc.
+ // resource://gre/modules/commonjs/sdk/core/promise.js
+ try {
+ loader.loadSubScript(addonPromiseURI, Promise);
+ } catch (e) {
+ loader.loadSubScript(builtPromiseURI, Promise);
+ }
+
+ testEnvironment(Promise, assert, done, 'mozIJSSubScript');
+};
+
+function testEnvironment ({all, resolve, defer, reject, promised}, assert, done, type) {
+ all([resolve(5), resolve(10), 925]).then(val => {
+ assert.equal(val[0], 5, 'promise#all works ' + type);
+ assert.equal(val[1], 10, 'promise#all works ' + type);
+ assert.equal(val[2], 925, 'promise#all works ' + type);
+ return resolve(1000);
+ }).then(value => {
+ assert.equal(value, 1000, 'promise#resolve works ' + type);
+ return reject('testing reject');
+ }).then(null, reason => {
+ assert.equal(reason, 'testing reject', 'promise#reject works ' + type);
+ let deferred = defer();
+ setTimeout(() => deferred.resolve('\\m/'), 10);
+ return deferred.promise;
+ }).then(value => {
+ assert.equal(value, '\\m/', 'promise#defer works ' + type);
+ return promised(x => x * x)(5);
+ }).then(value => {
+ assert.equal(value, 25, 'promise#promised works ' + type);
+ }).then(done, assert.fail);
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-querystring.js b/addon-sdk/source/test/test-querystring.js
new file mode 100644
index 000000000..b108cd081
--- /dev/null
+++ b/addon-sdk/source/test/test-querystring.js
@@ -0,0 +1,6 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+module.exports = require("./querystring/test-querystring.js");
diff --git a/addon-sdk/source/test/test-reference.js b/addon-sdk/source/test/test-reference.js
new file mode 100644
index 000000000..1fe08004c
--- /dev/null
+++ b/addon-sdk/source/test/test-reference.js
@@ -0,0 +1,99 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { isWeak, WeakReference } = require("sdk/core/reference");
+const { Class } = require("sdk/core/heritage");
+
+exports["test non WeakReferences"] = assert => {
+ assert.equal(isWeak({}), false,
+ "regular objects aren't weak");
+
+ assert.equal(isWeak([]), false,
+ "arrays aren't weak");
+};
+
+exports["test a WeakReference"] = assert => {
+ assert.equal(isWeak(new WeakReference()), true,
+ "instances of WeakReferences are weak");
+};
+
+exports["test a subclass"] = assert => {
+ const SubType = Class({
+ extends: WeakReference,
+ foo: function() {
+ }
+ });
+
+ const x = new SubType();
+ assert.equal(isWeak(x), true,
+ "subtypes of WeakReferences are weak");
+
+ const SubSubType = Class({
+ extends: SubType
+ });
+
+ const y = new SubSubType();
+ assert.equal(isWeak(y), true,
+ "sub subtypes of WeakReferences are weak");
+};
+
+exports["test a mixin"] = assert => {
+ const Mixer = Class({
+ implements: [WeakReference]
+ });
+
+ assert.equal(isWeak(new Mixer()), true,
+ "instances with mixed WeakReference are weak");
+
+ const Mux = Class({});
+ const MixMux = Class({
+ extends: Mux,
+ implements: [WeakReference]
+ });
+
+ assert.equal(isWeak(new MixMux()), true,
+ "instances with mixed WeakReference that " +
+ "inheret from other are weak");
+
+
+ const Base = Class({});
+ const ReMix = Class({
+ extends: Base,
+ implements: [MixMux]
+ });
+
+ assert.equal(isWeak(new ReMix()), true,
+ "subtime of mix is still weak");
+};
+
+exports["test an override"] = assert => {
+ const SubType = Class({ extends: WeakReference });
+ isWeak.define(SubType, x => false);
+
+ assert.equal(isWeak(new SubType()), false,
+ "behavior of isWeak can still be overrided on subtype");
+
+ const Mixer = Class({ implements: [WeakReference] });
+ const SubMixer = Class({ extends: Mixer });
+ isWeak.define(SubMixer, x => false);
+ assert.equal(isWeak(new SubMixer()), false,
+ "behavior of isWeak can still be overrided on mixed type");
+};
+
+exports["test an opt-in"] = assert => {
+ var BaseType = Class({});
+ var SubType = Class({ extends: BaseType });
+
+ isWeak.define(SubType, x => true);
+ assert.equal(isWeak(new SubType()), true,
+ "isWeak can be just implemented");
+
+ var x = {};
+ isWeak.implement(x, x => true);
+ assert.equal(isWeak(x), true,
+ "isWeak can be implemented per instance");
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-request.js b/addon-sdk/source/test/test-request.js
new file mode 100644
index 000000000..3c65d7684
--- /dev/null
+++ b/addon-sdk/source/test/test-request.js
@@ -0,0 +1,548 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const { Request } = require("sdk/request");
+const { pathFor } = require("sdk/system");
+const file = require("sdk/io/file");
+const { URL } = require("sdk/url");
+const { extend } = require("sdk/util/object");
+const { Loader } = require("sdk/test/loader");
+const options = require("sdk/test/options");
+
+const loader = Loader(module);
+const httpd = loader.require("./lib/httpd");
+if (options.parseable || options.verbose)
+ loader.sandbox("./lib/httpd").DEBUG = true;
+const { startServerAsync } = httpd;
+
+const { Cc, Ci, Cu } = require("chrome");
+const { Services } = Cu.import("resource://gre/modules/Services.jsm");
+
+// Use the profile directory for the temporary files as that will be deleted
+// when tests are complete
+const basePath = pathFor("ProfD");
+const port = 8099;
+
+
+exports.testOptionsValidator = function(assert) {
+ // First, a simple test to make sure we didn't break normal functionality.
+ assert.throws(function () {
+ Request({
+ url: null
+ });
+ }, /The option "url" is invalid./);
+
+ // Next we'll have a Request that doesn't throw from c'tor, but from a setter.
+ let req = Request({
+ url: "http://playground.zpao.com/jetpack/request/text.php",
+ onComplete: function () {}
+ });
+ assert.throws(function () {
+ req.url = 'www.mozilla.org';
+ }, /The option "url" is invalid/);
+ // The url shouldn't have changed, so check that
+ assert.equal(req.url, "http://playground.zpao.com/jetpack/request/text.php");
+
+ // Test default anonymous parameter value
+ assert.equal(req.anonymous, false);
+ // Test set anonymous parameter value
+ req = Request({
+ url: "http://playground.zpao.com/jetpack/request/text.php",
+ anonymous: true,
+ onComplete: function () {}
+ });
+ assert.equal(req.anonymous, true);
+ // Test wrong value as anonymous parameter value
+ assert.throws(function() {
+ Request({
+ url: "http://playground.zpao.com/jetpack/request/text.php",
+ anonymous: "invalidvalue"
+ });
+ }, /The option "anonymous" must be one of the following types/);
+};
+
+exports.testContentValidator = function(assert, done) {
+ runMultipleURLs(null, assert, done, {
+ url: "data:text/html;charset=utf-8,response",
+ content: { 'key1' : null, 'key2' : 'some value' },
+ onComplete: function(response) {
+ assert.equal(response.text, "response?key1=null&key2=some+value");
+ }
+ });
+};
+
+// This is a request to a file that exists.
+exports.testStatus200 = function (assert, done) {
+ let srv = startServerAsync(port, basePath);
+ let content = "Look ma, no hands!\n";
+ let basename = "test-request.txt"
+ let requestURL = "http://localhost:" + port + "/" + basename;
+
+ prepareFile(basename, content);
+
+ var req = Request({
+ url: requestURL,
+ onComplete: function (response) {
+ assert.equal(this, req, "`this` should be request");
+ assert.equal(response.status, 200);
+ assert.equal(response.statusText, "OK");
+ assert.equal(response.headers["Content-Type"], "text/plain");
+ assert.equal(response.text, content);
+ assert.strictEqual(response.url, requestURL);
+ srv.stop(done);
+ }
+ }).get();
+};
+
+// Should return the location to which have been automatically redirected
+exports.testRedirection = function (assert, done) {
+ let srv = startServerAsync(port, basePath);
+
+ let moveLocation = "test-request-302-located.txt";
+ let movedContent = "here now\n";
+ let movedUrl = "http://localhost:" + port + "/" + moveLocation;
+ prepareFile(moveLocation, movedContent);
+
+ let content = "The document has moved!\n";
+ let contentHeaders = "HTTP 302 Found\nLocation: "+moveLocation+"\n";
+ let basename = "test-request-302.txt"
+ let requestURL = "http://localhost:" + port + "/" + basename;
+ prepareFile(basename, content);
+ prepareFile(basename+"^headers^", contentHeaders);
+
+ var req = Request({
+ url: requestURL,
+ onComplete: function (response) {
+ assert.equal(this, req, "`this` should be request");
+ assert.equal(response.status, 200);
+ assert.equal(response.statusText, "OK");
+ assert.equal(response.headers["Content-Type"], "text/plain");
+ assert.equal(response.text, movedContent);
+ assert.strictEqual(response.url, movedUrl);
+ srv.stop(done);
+ }
+ }).get();
+};
+
+// This tries to get a file that doesn't exist
+exports.testStatus404 = function (assert, done) {
+ var srv = startServerAsync(port, basePath);
+ let requestURL = "http://localhost:" + port + "/test-request-404.txt";
+
+ runMultipleURLs(srv, assert, done, {
+ // the following URL doesn't exist
+ url: requestURL,
+ onComplete: function (response) {
+ assert.equal(response.status, 404);
+ assert.equal(response.statusText, "Not Found");
+ assert.strictEqual(response.url, requestURL);
+ }
+ });
+};
+
+// a simple file with a known header
+exports.testKnownHeader = function (assert, done) {
+ var srv = startServerAsync(port, basePath);
+
+ // Create the file that will be requested with the associated headers file
+ let content = "This tests adding headers to the server's response.\n";
+ let basename = "test-request-headers.txt";
+ let headerContent = "x-jetpack-header: Jamba Juice\n";
+ let headerBasename = "test-request-headers.txt^headers^";
+ prepareFile(basename, content);
+ prepareFile(headerBasename, headerContent);
+
+ runMultipleURLs(srv, assert, done, {
+ url: "http://localhost:" + port + "/test-request-headers.txt",
+ onComplete: function (response) {
+ assert.equal(response.headers["x-jetpack-header"], "Jamba Juice");
+ }
+ });
+};
+
+// complex headers
+exports.testComplexHeader = function (assert, done) {
+ let srv = startServerAsync(port, basePath);
+
+ let basename = "test-request-complex-headers.sjs";
+ let content = handleRequest.toString();
+ prepareFile(basename, content);
+
+ let headers = {
+ "x-jetpack-header": "Jamba Juice is: delicious",
+ "x-jetpack-header-2": "foo,bar",
+ "x-jetpack-header-3": "sup dawg, i heard you like x, so we put a x in " +
+ "yo x so you can y while you y",
+ "Set-Cookie": "foo=bar\nbaz=foo"
+ };
+
+ runMultipleURLs(srv, assert, done, {
+ url: "http://localhost:" + port + "/test-request-complex-headers.sjs",
+ onComplete: function (response) {
+ for (k in headers) {
+ assert.equal(response.headers[k], headers[k]);
+ }
+ }
+ });
+};
+
+// Force Allow Third Party cookies
+exports.test3rdPartyCookies = function (assert, done) {
+ let srv = startServerAsync(port, basePath);
+
+ let basename = "test-request-3rd-party-cookies.sjs";
+
+ // Function to handle the requests in the server
+ let content = function handleRequest(request, response) {
+ var cookiePresent = request.hasHeader("Cookie");
+ // If no cookie, set it
+ if(!cookiePresent) {
+ response.setHeader("Set-Cookie", "cookie=monster;", "true");
+ response.setHeader("x-jetpack-3rd-party", "false", "true");
+ } else {
+ // We got the cookie, say so
+ response.setHeader("x-jetpack-3rd-party", "true", "true");
+ }
+
+ response.write("<html><body>This tests 3rd party cookies.</body></html>");
+ }.toString();
+
+ prepareFile(basename, content);
+
+ // Disable the 3rd party cookies
+ Services.prefs.setIntPref("network.cookie.cookieBehavior", 1);
+
+ Request({
+ url: "http://localhost:" + port + "/test-request-3rd-party-cookies.sjs",
+ onComplete: function (response) {
+ // Check that the server created the cookie
+ assert.equal(response.headers['Set-Cookie'], 'cookie=monster;');
+
+ // Check it wasn't there before
+ assert.equal(response.headers['x-jetpack-3rd-party'], 'false');
+
+ // Make a second request, and check that the server this time
+ // got the cookie
+ Request({
+ url: "http://localhost:" + port + "/test-request-3rd-party-cookies.sjs",
+ onComplete: function (response) {
+ assert.equal(response.headers['x-jetpack-3rd-party'], 'true');
+ srv.stop(done);
+ }
+ }).get();
+ }
+ }).get();
+};
+
+// Test anonymous request behavior
+exports.testAnonymousRequest = function(assert, done) {
+ let srv = startServerAsync(port, basePath);
+ let basename = "test-anonymous-request.sjs";
+ let testUrl = "http://localhost:" + port + "/" + basename;
+ // Function to handle the requests in the server
+ let content = function handleRequest(request, response) {
+ // Request to store cookie
+ response.setHeader("Set-Cookie", "anonymousKey=anonymousValue;", "true");
+ // Set response content type
+ response.setHeader("Content-Type", "application/json");
+ // Check if cookie was send during request
+ var cookiePresent = request.hasHeader("Cookie");
+ // Create server respone content
+ response.write(JSON.stringify({ "hasCookie": cookiePresent }));
+ }.toString();
+ prepareFile(basename, content);
+ // Create request callbacks
+ var checkCookieCreated = function (response) {
+ // Check that the server created the cookie
+ assert.equal(response.headers['Set-Cookie'], 'anonymousKey=anonymousValue;');
+ // Make an other request and check that the server this time got the cookie
+ Request({
+ url: testUrl,
+ onComplete: checkCookieSend
+ }).get();
+ },
+ checkCookieSend = function (response) {
+ // Check the response sent headers and cookies
+ assert.equal(response.anonymous, false);
+ // Check the server got the created cookie
+ assert.equal(response.json.hasCookie, true);
+ // Make a anonymous request and check the server did not get the cookie
+ Request({
+ url: testUrl,
+ anonymous: true,
+ onComplete: checkCookieNotSend
+ }).get();
+ },
+ checkCookieNotSend = function (response) {
+ // Check the response is anonymous
+ assert.equal(response.anonymous, true);
+ // Check the server did not get the cookie
+ assert.equal(response.json.hasCookie, false);
+ // Stop the server
+ srv.stop(done);
+ };
+ // Make the first request to create cookie
+ Request({
+ url: testUrl,
+ onComplete: checkCookieCreated
+ }).get();
+};
+
+exports.testSimpleJSON = function (assert, done) {
+ let srv = startServerAsync(port, basePath);
+ let json = { foo: "bar" };
+ let basename = "test-request.json";
+ prepareFile(basename, JSON.stringify(json));
+
+ runMultipleURLs(srv, assert, done, {
+ url: "http://localhost:" + port + "/" + basename,
+ onComplete: function (response) {
+ assert.deepEqual(response.json, json);
+ }
+ });
+};
+
+exports.testInvalidJSON = function (assert, done) {
+ let srv = startServerAsync(port, basePath);
+ let basename = "test-request-invalid.json";
+ prepareFile(basename, '"this": "isn\'t JSON"');
+
+ runMultipleURLs(srv, assert, done, {
+ url: "http://localhost:" + port + "/" + basename,
+ onComplete: function (response) {
+ assert.equal(response.json, null);
+ }
+ });
+};
+
+exports.testDelete = function (assert, done) {
+ let srv = startServerAsync(port, basePath);
+
+ srv.registerPathHandler("/test-delete",
+ function handle(request, response) {
+ response.setHeader("Content-Type", "text/plain", false);
+ });
+
+ Request({
+ url: "http://localhost:" + port + "/test-delete",
+ onComplete: function (response) {
+ // We cannot access the METHOD of the request to verify it's set
+ // correctly.
+ assert.equal(response.text, "");
+ assert.equal(response.statusText, "OK");
+ assert.equal(response.headers["Content-Type"], "text/plain");
+ srv.stop(done);
+ }
+ }).delete();
+};
+
+exports.testHead = function (assert, done) {
+ let srv = startServerAsync(port, basePath);
+
+ srv.registerPathHandler("/test-head",
+ function handle(request, response) {
+ response.setHeader("Content-Type", "text/plain", false);
+ });
+
+ Request({
+ url: "http://localhost:" + port + "/test-head",
+ onComplete: function (response) {
+ assert.equal(response.text, "");
+ assert.equal(response.statusText, "OK");
+ assert.equal(response.headers["Content-Type"], "text/plain");
+ srv.stop(done);
+ }
+ }).head();
+};
+
+function runMultipleURLs (srv, assert, done, options) {
+ let urls = [options.url, URL(options.url)];
+ let cb = options.onComplete;
+ let ran = 0;
+ let onComplete = function (res) {
+ cb(res);
+ if (++ran === urls.length)
+ srv ? srv.stop(done) : done();
+ };
+ urls.forEach(function (url) {
+ Request(extend(options, { url: url, onComplete: onComplete })).get();
+ });
+}
+
+// All tests below here require a network connection. They will be commented out
+// when checked in. If you'd like to run them, simply uncomment them.
+//
+// When we have the means, these tests will be converted so that they don't
+// require an external server nor a network connection.
+
+/*
+exports.testGetWithParamsNotContent = function (assert, done) {
+ Request({
+ url: "http://playground.zpao.com/jetpack/request/getpost.php?foo=bar",
+ onComplete: function (response) {
+ let expected = {
+ "POST": [],
+ "GET" : { foo: "bar" }
+ };
+ assert.deepEqual(response.json, expected);
+ done();
+ }
+ }).get();
+}
+
+exports.testGetWithContent = function (assert, done) {
+ Request({
+ url: "http://playground.zpao.com/jetpack/request/getpost.php",
+ content: { foo: "bar" },
+ onComplete: function (response) {
+ let expected = {
+ "POST": [],
+ "GET" : { foo: "bar" }
+ };
+ assert.deepEqual(response.json, expected);
+ done();
+ }
+ }).get();
+}
+
+exports.testGetWithParamsAndContent = function (assert, done) {
+ Request({
+ url: "http://playground.zpao.com/jetpack/request/getpost.php?foo=bar",
+ content: { baz: "foo" },
+ onComplete: function (response) {
+ let expected = {
+ "POST": [],
+ "GET" : { foo: "bar", baz: "foo" }
+ };
+ assert.deepEqual(response.json, expected);
+ done();
+ }
+ }).get();
+}
+
+exports.testSimplePost = function (assert, done) {
+ Request({
+ url: "http://playground.zpao.com/jetpack/request/getpost.php",
+ content: { foo: "bar" },
+ onComplete: function (response) {
+ let expected = {
+ "POST": { foo: "bar" },
+ "GET" : []
+ };
+ assert.deepEqual(response.json, expected);
+ done();
+ }
+ }).post();
+}
+
+exports.testEncodedContent = function (assert, done) {
+ Request({
+ url: "http://playground.zpao.com/jetpack/request/getpost.php",
+ content: "foo=bar&baz=foo",
+ onComplete: function (response) {
+ let expected = {
+ "POST": [],
+ "GET" : { foo: "bar", baz: "foo" }
+ };
+ assert.deepEqual(response.json, expected);
+ done();
+ }
+ }).get();
+}
+
+exports.testEncodedContentWithSpaces = function (assert, done) {
+ Request({
+ url: "http://playground.zpao.com/jetpack/request/getpost.php",
+ content: "foo=bar+hop!&baz=foo",
+ onComplete: function (response) {
+ let expected = {
+ "POST": [],
+ "GET" : { foo: "bar hop!", baz: "foo" }
+ };
+ assert.deepEqual(response.json, expected);
+ done();
+ }
+ }).get();
+}
+
+exports.testGetWithArray = function (assert, done) {
+ Request({
+ url: "http://playground.zpao.com/jetpack/request/getpost.php",
+ content: { foo: [1, 2], baz: "foo" },
+ onComplete: function (response) {
+ let expected = {
+ "POST": [],
+ "GET" : { foo: [1, 2], baz: "foo" }
+ };
+ assert.deepEqual(response.json, expected);
+ done();
+ }
+ }).get();
+}
+
+exports.testGetWithNestedArray = function (assert, done) {
+ Request({
+ url: "http://playground.zpao.com/jetpack/request/getpost.php",
+ content: { foo: [1, 2, [3, 4]], bar: "baz" },
+ onComplete: function (response) {
+ let expected = {
+ "POST": [],
+ "GET" : this.content
+ };
+ assert.deepEqual(response.json, expected);
+ done();
+ }
+ }).get();
+}
+
+exports.testGetWithNestedArray = function (assert, done) {
+ let request = Request({
+ url: "http://playground.zpao.com/jetpack/request/getpost.php",
+ content: {
+ foo: [1, 2, {
+ omg: "bbq",
+ "all your base!": "are belong to us"
+ }],
+ bar: "baz"
+ },
+ onComplete: function (response) {
+ let expected = {
+ "POST": [],
+ "GET" : request.content
+ };
+ assert.deepEqual(response.json, expected);
+ done();
+ }
+ }).get();
+}
+*/
+
+function prepareFile(basename, content) {
+ let filePath = file.join(basePath, basename);
+ let fileStream = file.open(filePath, 'w');
+ fileStream.write(content);
+ fileStream.close();
+}
+
+// Helper function for testComplexHeaders
+function handleRequest(request, response) {
+ // Test header with an extra colon
+ response.setHeader("x-jetpack-header", "Jamba Juice is: delicious", "true");
+
+ // Test that multiple headers with the same name coalesce
+ response.setHeader("x-jetpack-header-2", "foo", "true");
+ response.setHeader("x-jetpack-header-2", "bar", "true");
+
+ // Test that headers with commas work
+ response.setHeader("x-jetpack-header-3", "sup dawg, i heard you like x, " +
+ "so we put a x in yo x so you can y while you y", "true");
+
+ // Test that multiple cookies work
+ response.setHeader("Set-Cookie", "foo=bar", "true");
+ response.setHeader("Set-Cookie", "baz=foo", "true");
+
+ response.write("<html><body>This file tests more complex headers.</body></html>");
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-require.js b/addon-sdk/source/test/test-require.js
new file mode 100644
index 000000000..9dcfd31a7
--- /dev/null
+++ b/addon-sdk/source/test/test-require.js
@@ -0,0 +1,67 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const traceback = require('sdk/console/traceback');
+const REQUIRE_LINE_NO = 29;
+
+exports.test_no_args = function(assert) {
+ let passed = tryRequireModule(assert);
+ assert.ok(passed, 'require() with no args should raise helpful error');
+};
+
+exports.test_invalid_sdk_module = function (assert) {
+ let passed = tryRequireModule(assert, 'sdk/does-not-exist');
+ assert.ok(passed, 'require() with an invalid sdk module should raise');
+};
+
+exports.test_invalid_relative_module = function (assert) {
+ let passed = tryRequireModule(assert, './does-not-exist');
+ assert.ok(passed, 'require() with an invalid relative module should raise');
+};
+
+
+function tryRequireModule(assert, module) {
+ let passed = false;
+ try {
+ // This line number is important, referenced in REQUIRE_LINE_NO
+ let doesNotExist = require(module);
+ }
+ catch(e) {
+ checkError(assert, module, e);
+ passed = true;
+ }
+ return passed;
+}
+
+function checkError (assert, name, e) {
+ let msg = e.toString();
+ if (name) {
+ assert.ok(/is not found at/.test(msg),
+ 'Error message indicates module not found');
+ assert.ok(msg.indexOf(name.replace(/\./g,'')) > -1,
+ 'Error message has the invalid module name in the message');
+ }
+ else {
+ assert.equal(msg.indexOf('Error: You must provide a module name when calling require() from '), 0);
+ assert.ok(msg.indexOf("test-require") !== -1, msg);
+ }
+
+ // we'd also like to assert that the right filename
+ // and linenumber is in the stacktrace
+ let tb = traceback.fromException(e);
+
+ // The last frame may be inside a loader
+ let lastFrame = tb[tb.length - 1];
+ if (lastFrame.fileName.indexOf("toolkit/loader.js") !== -1 ||
+ lastFrame.fileName.indexOf("sdk/loader/cuddlefish.js") !== -1)
+ lastFrame = tb[tb.length - 2];
+
+ assert.ok(lastFrame.fileName.indexOf("test-require.js") !== -1,
+ 'Filename found in stacktrace');
+ assert.equal(lastFrame.lineNumber, REQUIRE_LINE_NO,
+ 'stacktrace has correct line number');
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-rules.js b/addon-sdk/source/test/test-rules.js
new file mode 100644
index 000000000..705e909cb
--- /dev/null
+++ b/addon-sdk/source/test/test-rules.js
@@ -0,0 +1,79 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Rules } = require('sdk/util/rules');
+const { on, off, emit } = require('sdk/event/core');
+
+exports.testAdd = function (test, done) {
+ let rules = Rules();
+ let urls = [
+ 'http://www.firefox.com',
+ '*.mozilla.org',
+ '*.html5audio.org'
+ ];
+ let count = 0;
+ on(rules, 'add', function (rule) {
+ if (count < urls.length) {
+ test.ok(rules.get(rule), 'rule added to internal registry');
+ test.equal(rule, urls[count], 'add event fired with proper params');
+ if (++count < urls.length) rules.add(urls[count]);
+ else done();
+ }
+ });
+ rules.add(urls[0]);
+};
+
+exports.testRemove = function (test, done) {
+ let rules = Rules();
+ let urls = [
+ 'http://www.firefox.com',
+ '*.mozilla.org',
+ '*.html5audio.org'
+ ];
+ let count = 0;
+ on(rules, 'remove', function (rule) {
+ if (count < urls.length) {
+ test.ok(!rules.get(rule), 'rule removed to internal registry');
+ test.equal(rule, urls[count], 'remove event fired with proper params');
+ if (++count < urls.length) rules.remove(urls[count]);
+ else done();
+ }
+ });
+ urls.forEach(url => rules.add(url));
+ rules.remove(urls[0]);
+};
+
+exports.testMatchesAny = function(test) {
+ let rules = Rules();
+ rules.add('*.mozilla.org');
+ rules.add('data:*');
+ matchTest('http://mozilla.org', true);
+ matchTest('http://www.mozilla.org', true);
+ matchTest('http://www.google.com', false);
+ matchTest('data:text/html;charset=utf-8,', true);
+
+ function matchTest(string, expected) {
+ test.equal(rules.matchesAny(string), expected,
+ 'Expected to find ' + string + ' in rules');
+ }
+};
+
+exports.testIterable = function(test) {
+ let rules = Rules();
+ rules.add('*.mozilla.org');
+ rules.add('data:*');
+ rules.add('http://google.com');
+ rules.add('http://addons.mozilla.org');
+ rules.remove('http://google.com');
+
+ test.equal(rules.length, 3, 'has correct length of keys');
+ Array.forEach(rules, function (rule, i) {
+ test.equal(rule, ['*.mozilla.org', 'data:*', 'http://addons.mozilla.org'][i]);
+ });
+ for (let i in rules)
+ test.equal(rules[i], ['*.mozilla.org', 'data:*', 'http://addons.mozilla.org'][i]);
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-sandbox.js b/addon-sdk/source/test/test-sandbox.js
new file mode 100644
index 000000000..8592e9fbc
--- /dev/null
+++ b/addon-sdk/source/test/test-sandbox.js
@@ -0,0 +1,161 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const { sandbox, load, evaluate, nuke } = require('sdk/loader/sandbox');
+const xulApp = require("sdk/system/xul-app");
+const fixturesURI = module.uri.split('test-sandbox.js')[0] + 'fixtures/';
+
+// The following adds Debugger constructor to the global namespace.
+const { Cu } = require('chrome');
+const { addDebuggerToGlobal } =
+ Cu.import('resource://gre/modules/jsdebugger.jsm', {});
+addDebuggerToGlobal(this);
+
+exports['test basics'] = function(assert) {
+ let fixture = sandbox('http://example.com');
+ assert.equal(evaluate(fixture, 'var a = 1;'), undefined,
+ 'returns expression value');
+ assert.equal(evaluate(fixture, 'b = 2;'), 2,
+ 'returns expression value');
+ assert.equal(fixture.b, 2, 'global is defined as property');
+ assert.equal(fixture.a, 1, 'global is defined as property');
+ assert.equal(evaluate(fixture, 'a + b;'), 3, 'returns correct sum');
+};
+
+exports['test non-privileged'] = function(assert) {
+ let fixture = sandbox('http://example.com');
+ if (xulApp.versionInRange(xulApp.platformVersion, "15.0a1", "18.*")) {
+ let rv = evaluate(fixture, 'Compo' + 'nents.utils');
+ assert.equal(rv, undefined,
+ "Components's attributes are undefined in content sandboxes");
+ }
+ else {
+ assert.throws(function() {
+ evaluate(fixture, 'Compo' + 'nents.utils');
+ }, 'Access to components is restricted');
+ }
+ fixture.sandbox = sandbox;
+ assert.throws(function() {
+ evaluate(fixture, sandbox('http://foo.com'));
+ }, 'Can not call privileged code');
+};
+
+exports['test injection'] = function(assert) {
+ let fixture = sandbox();
+ fixture.hi = name => 'Hi ' + name;
+ assert.equal(evaluate(fixture, 'hi("sandbox");'), 'Hi sandbox',
+ 'injected functions are callable');
+};
+
+exports['test exceptions'] = function(assert) {
+ let fixture = sandbox();
+ try {
+ evaluate(fixture, '!' + function() {
+ var message = 'boom';
+ throw Error(message);
+ } + '();');
+ }
+ catch (error) {
+ assert.equal(error.fileName, '[System Principal]', 'No specific fileName reported');
+ assert.equal(error.lineNumber, 3, 'reports correct line number');
+ }
+
+ try {
+ evaluate(fixture, '!' + function() {
+ var message = 'boom';
+ throw Error(message);
+ } + '();', 'foo.js');
+ }
+ catch (error) {
+ assert.equal(error.fileName, 'foo.js', 'correct fileName reported');
+ assert.equal(error.lineNumber, 3, 'reports correct line number');
+ }
+
+ try {
+ evaluate(fixture, '!' + function() {
+ var message = 'boom';
+ throw Error(message);
+ } + '();', 'foo.js', 2);
+ }
+ catch (error) {
+ assert.equal(error.fileName, 'foo.js', 'correct fileName reported');
+ assert.equal(error.lineNumber, 4, 'line number was opted');
+ }
+};
+
+exports['test load'] = function(assert) {
+ let fixture = sandbox();
+ load(fixture, fixturesURI + 'sandbox-normal.js');
+ assert.equal(fixture.a, 1, 'global variable defined');
+ assert.equal(fixture.b, 2, 'global via `this` property was set');
+ assert.equal(fixture.f(), 4, 'function was defined');
+};
+
+exports['test load with data: URL'] = function(assert) {
+ let code = "var a = 1; this.b = 2; function f() { return 4; }";
+ let fixture = sandbox();
+ load(fixture, "data:," + encodeURIComponent(code));
+
+ assert.equal(fixture.a, 1, 'global variable defined');
+ assert.equal(fixture.b, 2, 'global via `this` property was set');
+ assert.equal(fixture.f(), 4, 'function was defined');
+};
+
+exports['test load script with complex char'] = function(assert) {
+ let fixture = sandbox();
+ load(fixture, fixturesURI + 'sandbox-complex-character.js');
+ assert.equal(fixture.chars, 'გამარჯობა', 'complex chars were loaded correctly');
+};
+
+exports['test load script with data: URL and complex char'] = function(assert) {
+ let code = "var chars = 'გამარჯობა';";
+ let fixture = sandbox();
+ load(fixture, "data:," + encodeURIComponent(code));
+
+ assert.equal(fixture.chars, 'გამარჯობა', 'complex chars were loaded correctly');
+};
+
+exports['test metadata'] = function(assert) {
+ let self = require('sdk/self');
+
+ let dbg = new Debugger();
+ dbg.onNewGlobalObject = function(global) {
+ let metadata = Cu.getSandboxMetadata(global.unsafeDereference());
+ assert.ok(metadata, 'this global has attached metadata');
+ assert.equal(metadata.addonID, self.id, 'addon ID is set');
+
+ dbg.onNewGlobalObject = undefined;
+ }
+
+ let fixture = sandbox();
+ assert.equal(dbg.onNewGlobalObject, undefined, 'Should have reset the handler');
+}
+
+exports['test nuke sandbox'] = function(assert) {
+
+ let fixture = sandbox('http://example.com');
+ fixture.foo = 'foo';
+
+ let ref = evaluate(fixture, 'let a = {bar: "bar"}; a');
+
+ nuke(fixture);
+
+ assert.ok(Cu.isDeadWrapper(fixture), 'sandbox should be dead');
+
+ assert.throws(
+ () => fixture.foo,
+ /can't access dead object/,
+ 'property of nuked sandbox should not be accessible'
+ );
+
+ assert.ok(Cu.isDeadWrapper(ref), 'ref to object from sandbox should be dead');
+
+ assert.throws(
+ () => ref.bar,
+ /can't access dead object/,
+ 'object from nuked sandbox should not be alive'
+ );
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-selection.js b/addon-sdk/source/test/test-selection.js
new file mode 100644
index 000000000..a0d5ff894
--- /dev/null
+++ b/addon-sdk/source/test/test-selection.js
@@ -0,0 +1,985 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+module.metadata = {
+ 'engines': {
+ 'Firefox': '*'
+ }
+};
+
+const HTML = "<html>\
+ <body>\
+ <div>foo</div>\
+ <div>and</div>\
+ <textarea>noodles</textarea>\
+ </body>\
+</html>";
+
+const URL = "data:text/html;charset=utf-8," + encodeURIComponent(HTML);
+
+const FRAME_HTML = "<iframe src='" + URL + "'><iframe>";
+const FRAME_URL = "data:text/html;charset=utf-8," + encodeURIComponent(FRAME_HTML);
+
+const { Cu } = require("chrome");
+const { defer } = require("sdk/core/promise");
+const tabs = require("sdk/tabs");
+const { getActiveTab, getTabContentWindow, setTabURL } = require("sdk/tabs/utils")
+const { getMostRecentBrowserWindow } = require("sdk/window/utils");
+const { open: openNewWindow } = require("sdk/window/helpers");
+const { Loader } = require("sdk/test/loader");
+const { setTimeout } = require("sdk/timers");
+const { merge } = require("sdk/util/object");
+const { isPrivate } = require("sdk/private-browsing");
+const events = require("sdk/system/events");
+const { viewFor } = require("sdk/view/core");
+const { cleanUI } = require("sdk/test/utils");
+// General purpose utility functions
+
+/**
+ * Opens the url given and return a promise, that will be resolved with the
+ * content window when the document is ready.
+ *
+ * I believe this approach could be useful in most of our unit test, that
+ * requires to open a tab and need to access to its content.
+ */
+function open(url, options) {
+ let { promise, resolve } = defer();
+
+ if (options && typeof(options) === "object") {
+ openNewWindow("", {
+ features: merge({ toolbar: true }, options)
+ }).then(function(chromeWindow) {
+ if (isPrivate(chromeWindow) !== !!options.private)
+ throw new Error("Window should have Private set to " + !!options.private);
+
+ let tab = getActiveTab(chromeWindow);
+
+ tab.linkedBrowser.addEventListener("load", function ready(event) {
+ let { document } = getTabContentWindow(tab);
+
+ if (document.readyState === "complete" && document.URL === url) {
+ this.removeEventListener(event.type, ready);
+
+ resolve(document.defaultView);
+ }
+ }, true);
+
+ setTabURL(tab, url);
+ });
+
+ return promise;
+ };
+
+ tabs.open({
+ url: url,
+ onReady: (tab) => {
+ let window = getTabContentWindow(viewFor(tab));
+ resolve(window);
+ }
+ });
+
+ return promise;
+};
+
+/**
+ * Reload the window given and return a promise, that will be resolved with the
+ * content window after a small delay.
+ */
+function reload(window) {
+ let { promise, resolve } = defer();
+
+ // Here we assuming that the most recent browser window is the one we're
+ // doing the test, and the active tab is the one we just opened.
+ let tab = tabs.activeTab;
+
+ tab.once("ready", function () {
+ resolve(window);
+ });
+
+ window.location.reload(true);
+
+ return promise;
+}
+
+// Selection's unit test utility function
+
+/**
+ * Returns the frame's window once the document is loaded
+ */
+function getFrameWindow(window) {
+ let { promise, resolve } = defer();
+
+ let frame = window.frames[0];
+ let { document } = frame;
+
+ frame.focus();
+
+ if (document.readyState === "complete")
+ return frame;
+
+ document.addEventListener("readystatechange", function readystate() {
+ if (this.readyState === "complete") {
+ this.removeEventListener("readystatechange", readystate);
+ frame.focus();
+ resolve(frame);
+ }
+ });
+
+ return promise;
+}
+
+/**
+ * Hide the frame in order to destroy the selection object, and show it again
+ * after ~500 msec, to give time to attach the code on `document-shown`
+ * notification.
+ * In the process, call `Cu.forgeGC` to ensure that the `document-shown` code
+ * is not garbaged.
+ */
+function hideAndShowFrame(window) {
+ let { promise, resolve } = defer();
+ let iframe = window.document.querySelector("iframe");
+
+ iframe.style.display = "none";
+
+ Cu.schedulePreciseGC(function() {
+ events.on("document-shown", function shown(event) {
+ if (iframe.contentWindow !== event.subject.defaultView)
+ return;
+
+ events.off("document-shown", shown);
+ setTimeout(resolve, 0, window);
+ }, true);
+
+ iframe.style.display = "";
+ });
+
+ return promise;
+}
+
+/**
+ * Select the first div in the page, adding the range to the selection.
+ */
+function selectFirstDiv(window) {
+ let div = window.document.querySelector("div");
+ let selection = window.getSelection();
+ let range = window.document.createRange();
+
+ if (selection.rangeCount > 0)
+ selection.removeAllRanges();
+
+ range.selectNode(div);
+ selection.addRange(range);
+
+ return window;
+}
+
+/**
+ * Select all divs in the page, adding the ranges to the selection.
+ */
+function selectAllDivs(window) {
+ let divs = window.document.getElementsByTagName("div");
+ let selection = window.getSelection();
+
+ if (selection.rangeCount > 0)
+ selection.removeAllRanges();
+
+ for (let i = 0; i < divs.length; i++) {
+ let range = window.document.createRange();
+
+ range.selectNode(divs[i]);
+ selection.addRange(range);
+ }
+
+ return window;
+}
+
+/**
+ * Select the textarea content
+ */
+function selectTextarea(window) {
+ let selection = window.getSelection();
+ let textarea = window.document.querySelector("textarea");
+
+ if (selection.rangeCount > 0)
+ selection.removeAllRanges();
+
+ textarea.setSelectionRange(0, textarea.value.length);
+ textarea.focus();
+
+ return window;
+}
+
+/**
+ * Select the content of the first div
+ */
+function selectContentFirstDiv(window) {
+ let div = window.document.querySelector("div");
+ let selection = window.getSelection();
+ let range = window.document.createRange();
+
+ if (selection.rangeCount > 0)
+ selection.removeAllRanges();
+
+ range.selectNodeContents(div);
+ selection.addRange(range);
+
+ return window;
+}
+
+/**
+ * Dispatch the selection event for the selection listener added by
+ * `nsISelectionPrivate.addSelectionListener`
+ */
+function dispatchSelectionEvent(window) {
+ // We modify the selection in order to dispatch the selection's event, by
+ // contract the selection by one character. So if the text selected is "foo"
+ // will be "fo".
+ window.getSelection().modify("extend", "backward", "character");
+
+ return window;
+}
+
+/**
+ * Dispatch the selection event for the selection listener added by
+ * `window.onselect` / `window.addEventListener`
+ */
+function dispatchOnSelectEvent(window) {
+ let { document } = window;
+ let textarea = document.querySelector("textarea");
+ let event = document.createEvent("UIEvents");
+
+ event.initUIEvent("select", true, true, window, 1);
+
+ textarea.dispatchEvent(event);
+
+ return window;
+}
+
+/**
+ * Creates empty ranges and add them to selections
+ */
+function createEmptySelections(window) {
+ selectAllDivs(window);
+
+ let selection = window.getSelection();
+
+ for (let i = 0; i < selection.rangeCount; i++)
+ selection.getRangeAt(i).collapse(true);
+}
+
+// Test cases
+
+exports["test No Selection"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let window = yield open(URL);
+
+ assert.equal(selection.isContiguous, false,
+ "selection.isContiguous without selection works.");
+
+ assert.strictEqual(selection.text, null,
+ "selection.text without selection works.");
+
+ assert.strictEqual(selection.html, null,
+ "selection.html without selection works.");
+
+ let selectionCount = 0;
+ for (let sel of selection)
+ selectionCount++;
+
+ assert.equal(selectionCount, 0, "No iterable selections");
+
+ yield cleanUI();
+ loader.unload();
+};
+
+exports["test Single DOM Selection"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let window = yield open(URL);
+
+ selectFirstDiv(window)
+
+ assert.equal(selection.isContiguous, true,
+ "selection.isContiguous with single DOM Selection works.");
+
+ assert.equal(selection.text, "foo",
+ "selection.text with single DOM Selection works.");
+
+ assert.equal(selection.html, "<div>foo</div>",
+ "selection.html with single DOM Selection works.");
+
+ let selectionCount = 0;
+ for (let sel of selection) {
+ selectionCount++;
+
+ assert.equal(sel.text, "foo",
+ "iterable selection.text with single DOM Selection works.");
+
+ assert.equal(sel.html, "<div>foo</div>",
+ "iterable selection.html with single DOM Selection works.");
+ }
+
+ assert.equal(selectionCount, 1, "One iterable selection");
+
+ yield cleanUI();
+ loader.unload();
+};
+
+exports["test Multiple DOM Selection"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let expectedText = ["foo", "and"];
+ let expectedHTML = ["<div>foo</div>", "<div>and</div>"];
+ let window = yield open(URL);
+
+ selectAllDivs(window);
+
+ assert.equal(selection.isContiguous, false,
+ "selection.isContiguous with multiple DOM Selection works.");
+
+ assert.equal(selection.text, expectedText[0],
+ "selection.text with multiple DOM Selection works.");
+
+ assert.equal(selection.html, expectedHTML[0],
+ "selection.html with multiple DOM Selection works.");
+
+ let selectionCount = 0;
+ for (let sel of selection) {
+ assert.equal(sel.text, expectedText[selectionCount],
+ "iterable selection.text with multiple DOM Selection works.");
+
+ assert.equal(sel.html, expectedHTML[selectionCount],
+ "iterable selection.text with multiple DOM Selection works.");
+
+ selectionCount++;
+ }
+
+ assert.equal(selectionCount, 2, "Two iterable selections");
+
+ yield cleanUI();
+ loader.unload();
+};
+
+exports["test Textarea Selection"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let window = yield open(URL);
+
+ selectTextarea(window);
+
+ assert.equal(selection.isContiguous, true,
+ "selection.isContiguous with Textarea Selection works.");
+
+ assert.equal(selection.text, "noodles",
+ "selection.text with Textarea Selection works.");
+
+ assert.strictEqual(selection.html, null,
+ "selection.html with Textarea Selection works.");
+
+ let selectionCount = 0;
+ for (let sel of selection) {
+ selectionCount++;
+
+ assert.equal(sel.text, "noodles",
+ "iterable selection.text with Textarea Selection works.");
+
+ assert.strictEqual(sel.html, null,
+ "iterable selection.html with Textarea Selection works.");
+ }
+
+ assert.equal(selectionCount, 1, "One iterable selection");
+
+ yield cleanUI();
+ loader.unload();
+};
+
+exports["test Set Text in Multiple DOM Selection"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let expectedText = ["bar", "and"];
+ let expectedHTML = ["bar", "<div>and</div>"];
+ let window = yield open(URL);
+
+ selectAllDivs(window);
+
+ selection.text = "bar";
+
+ assert.equal(selection.text, expectedText[0],
+ "set selection.text with single DOM Selection works.");
+
+ assert.equal(selection.html, expectedHTML[0],
+ "selection.html with single DOM Selection works.");
+
+ let selectionCount = 0;
+ for (let sel of selection) {
+ assert.equal(sel.text, expectedText[selectionCount],
+ "iterable selection.text with multiple DOM Selection works.");
+
+ assert.equal(sel.html, expectedHTML[selectionCount],
+ "iterable selection.html with multiple DOM Selection works.");
+
+ selectionCount++;
+ }
+
+ assert.equal(selectionCount, 2, "Two iterable selections");
+
+ yield cleanUI();
+ loader.unload();
+};
+
+exports["test Set HTML in Multiple DOM Selection"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let html = "<span>b<b>a</b>r</span>";
+ let expectedText = ["bar", "and"];
+ let expectedHTML = [html, "<div>and</div>"];
+ let window = yield open(URL);
+
+ selectAllDivs(window);
+
+ selection.html = html;
+
+ assert.equal(selection.text, expectedText[0],
+ "set selection.text with DOM Selection works.");
+
+ assert.equal(selection.html, expectedHTML[0],
+ "selection.html with DOM Selection works.");
+
+ let selectionCount = 0;
+ for (let sel of selection) {
+ assert.equal(sel.text, expectedText[selectionCount],
+ "iterable selection.text with multiple DOM Selection works.");
+
+ assert.equal(sel.html, expectedHTML[selectionCount],
+ "iterable selection.html with multiple DOM Selection works.");
+
+ selectionCount++;
+ }
+
+ assert.equal(selectionCount, 2, "Two iterable selections");
+
+ yield cleanUI();
+ loader.unload();
+};
+
+exports["test Set HTML as text in Multiple DOM Selection"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let text = "<span>b<b>a</b>r</span>";
+ let html = "&lt;span&gt;b&lt;b&gt;a&lt;/b&gt;r&lt;/span&gt;";
+ let expectedText = [text, "and"];
+ let expectedHTML = [html, "<div>and</div>"];
+ let window = yield open(URL);
+
+ selectAllDivs(window);
+
+ selection.text = text;
+
+ assert.equal(selection.text, expectedText[0],
+ "set selection.text with DOM Selection works.");
+
+ assert.equal(selection.html, expectedHTML[0],
+ "selection.html with DOM Selection works.");
+
+ let selectionCount = 0;
+ for (let sel of selection) {
+ assert.equal(sel.text, expectedText[selectionCount],
+ "iterable selection.text with multiple DOM Selection works.");
+
+ assert.equal(sel.html, expectedHTML[selectionCount],
+ "iterable selection.html with multiple DOM Selection works.");
+
+ selectionCount++;
+ }
+
+ assert.equal(selectionCount, 2, "Two iterable selections");
+
+ yield cleanUI();
+ loader.unload();
+};
+
+exports["test Set Text in Textarea Selection"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let text = "bar";
+ let window = yield open(URL);
+
+ selectTextarea(window);
+
+ selection.text = text;
+
+ assert.equal(selection.text, text,
+ "set selection.text with Textarea Selection works.");
+
+ assert.strictEqual(selection.html, null,
+ "selection.html with Textarea Selection works.");
+
+ let selectionCount = 0;
+ for (let sel of selection) {
+ selectionCount++;
+
+ assert.equal(sel.text, text,
+ "iterable selection.text with Textarea Selection works.");
+
+ assert.strictEqual(sel.html, null,
+ "iterable selection.html with Textarea Selection works.");
+ }
+
+ assert.equal(selectionCount, 1, "One iterable selection");
+
+ yield cleanUI();
+ loader.unload();
+};
+
+exports["test Set HTML in Textarea Selection"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let html = "<span>b<b>a</b>r</span>";
+ let window = yield open(URL);
+
+ selectTextarea(window);
+
+ // Textarea can't have HTML so set `html` property is equals to set `text`
+ // property
+ selection.html = html;
+
+ assert.equal(selection.text, html,
+ "set selection.text with Textarea Selection works.");
+
+ assert.strictEqual(selection.html, null,
+ "selection.html with Textarea Selection works.");
+
+ let selectionCount = 0;
+ for (let sel of selection) {
+ selectionCount++;
+
+ assert.equal(sel.text, html,
+ "iterable selection.text with Textarea Selection works.");
+
+ assert.strictEqual(sel.html, null,
+ "iterable selection.html with Textarea Selection works.");
+ }
+
+ assert.equal(selectionCount, 1, "One iterable selection");
+
+ yield cleanUI();
+ loader.unload();
+};
+
+exports["test Empty Selections"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let window = yield open(URL);
+
+ createEmptySelections(window);
+
+ assert.equal(selection.isContiguous, false,
+ "selection.isContiguous with empty selections works.");
+
+ assert.strictEqual(selection.text, null,
+ "selection.text with empty selections works.");
+
+ assert.strictEqual(selection.html, null,
+ "selection.html with empty selections works.");
+
+ let selectionCount = 0;
+ for (let sel of selection)
+ selectionCount++;
+
+ assert.equal(selectionCount, 0, "No iterable selections");
+
+ yield cleanUI();
+ loader.unload();
+}
+
+
+exports["test No Selection Exception"] = function*(assert) {
+ const NO_SELECTION = /It isn't possible to change the selection/;
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let window = yield open(URL);
+
+ // We're trying to change a selection when there is no selection
+ assert.throws(function() {
+ selection.text = "bar";
+ }, NO_SELECTION);
+
+ assert.throws(function() {
+ selection.html = "bar";
+ }, NO_SELECTION);
+
+ yield cleanUI();
+ loader.unload();
+};
+
+exports["test for...of without selections"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let window = yield open(URL);
+ let selectionCount = 0;
+
+ for (let sel of selection)
+ selectionCount++;
+
+ assert.equal(selectionCount, 0, "No iterable selections");
+
+ yield cleanUI();
+ loader.unload();
+}
+
+exports["test for...of with selections"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let expectedText = ["foo", "and"];
+ let expectedHTML = ["<div>foo</div>", "<div>and</div>"];
+ let window = yield open(URL);
+
+ selectAllDivs(window);
+
+ let selectionCount = 0;
+
+ for (let sel of selection) {
+ assert.equal(sel.text, expectedText[selectionCount],
+ "iterable selection.text with for...of works.");
+
+ assert.equal(sel.html, expectedHTML[selectionCount],
+ "iterable selection.text with for...of works.");
+
+ selectionCount++;
+ }
+
+ assert.equal(selectionCount, 2, "Two iterable selections");
+
+ yield cleanUI();
+ loader.unload();
+}
+
+exports["test Selection Listener"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let selected = defer();
+
+ selection.once("select", selected.resolve);
+
+ yield open(URL).
+ then(selectContentFirstDiv).
+ then(dispatchSelectionEvent);
+
+ yield selected.promise;
+
+ assert.equal(selection.text, "fo");
+
+ yield cleanUI();
+ loader.unload();
+};
+
+exports["test Textarea OnSelect Listener"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let selected = defer();
+
+ selection.once("select", selected.resolve);
+
+ yield open(URL).
+ then(selectTextarea).
+ then(dispatchOnSelectEvent);
+
+ yield selected.promise;
+
+ assert.equal(selection.text, "noodles", "selection is noodles");
+
+ yield cleanUI();
+ loader.unload();
+};
+
+exports["test Selection listener removed on unload"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+
+ selection.once("select", function() {
+ assert.fail("Shouldn't be never called");
+ });
+
+ loader.unload();
+ assert.pass("unload was a success");
+
+ yield open(URL).
+ then(selectContentFirstDiv).
+ then(dispatchSelectionEvent).
+ then(cleanUI);
+};
+
+exports["test Textarea onSelect Listener removed on unload"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+
+ selection.once("select", function() {
+ assert.fail("Shouldn't be never called");
+ });
+
+ loader.unload();
+ assert.pass("unload was a success");
+
+ yield open(URL).
+ then(selectTextarea).
+ then(dispatchOnSelectEvent).
+ then(cleanUI);
+};
+
+
+exports["test Selection Listener on existing document"] = function*(assert) {
+ let loader = Loader(module);
+ let selected = defer();
+
+ let window = yield open(URL);
+ let selection = loader.require("sdk/selection");
+
+ selection.once("select", selected.resolve);
+
+ selectContentFirstDiv(window);
+ dispatchSelectionEvent(window);
+
+ yield selected.promise;
+
+ assert.equal(selection.text, "fo");
+
+ yield cleanUI();
+ loader.unload();
+};
+
+
+exports["test Textarea OnSelect Listener on existing document"] = function*(assert) {
+ let loader = Loader(module);
+ let selected = defer();
+
+ let selection = loader.require("sdk/selection");
+
+ let window = yield open(URL);
+
+ selection.once("select", selected.resolve);
+ selectTextarea(window)
+ dispatchOnSelectEvent(window);
+
+ yield selected.promise;
+
+ assert.equal(selection.text, "noodles");
+
+ yield cleanUI();
+ loader.unload();
+};
+
+exports["test Selection Listener on document reload"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let selected = defer();
+
+ selection.once("select", selected.resolve);
+
+ yield open(URL).
+ then(reload).
+ then(selectContentFirstDiv).
+ then(dispatchSelectionEvent);
+
+ yield selected.promise;
+
+ assert.equal(selection.text, "fo");
+
+ yield cleanUI();
+ loader.unload();
+};
+
+exports["test Textarea OnSelect Listener on document reload"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let selected = defer();
+
+ selection.once("select", selected.resolve);
+
+ yield open(URL).
+ then(reload).
+ then(selectTextarea).
+ then(dispatchOnSelectEvent);
+
+ yield selected.promise;
+
+ assert.equal(selection.text, "noodles");
+
+ yield cleanUI();
+ loader.unload();
+};
+
+exports["test Selection Listener on frame"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let selected = defer();
+
+ selection.once("select", selected.resolve);
+
+ let window = yield open(FRAME_URL);
+ yield hideAndShowFrame(window);
+ let frame = yield getFrameWindow(window);
+ yield selectContentFirstDiv(frame);
+ yield dispatchSelectionEvent(frame);
+
+ yield selected.promise;
+
+ assert.equal(selection.text, "fo");
+
+ yield cleanUI();
+ loader.unload();
+};
+
+
+// TODO: re-enable and fix intermittent test below
+// See Bug 970062 https://bugzilla.mozilla.org/show_bug.cgi?id=970062
+/*
+exports["test Textarea onSelect Listener on frame"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let selected = defer();
+
+ selection.once("select", selected.resolve);
+
+ yield open(FRAME_URL).
+ then(hideAndShowFrame).
+ then(getFrameWindow).
+ then(selectTextarea).
+ then(dispatchOnSelectEvent);
+
+ yield selected.promise;
+
+ assert.equal(selection.text, "noodles");
+
+ yield cleanUI();
+ loader.unload();
+};
+*/
+
+
+exports["test PBPW Selection Listener"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+
+ selection.once("select", function() {
+ assert.fail("Shouldn't be never called");
+ });
+
+ assert.pass();
+
+ yield open(URL, { private: true }).
+ then(selectContentFirstDiv).
+ then(dispatchSelectionEvent).
+ then(cleanUI);
+
+ loader.unload();
+};
+
+exports["test PBPW Textarea OnSelect Listener"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+
+ selection.once("select", () => {
+ assert.fail("Shouldn't be never called");
+ });
+
+ assert.pass("opening private test content window");
+
+ yield open(URL, { private: true }).
+ then(selectTextarea).
+ then(dispatchOnSelectEvent).
+ then(cleanUI);
+
+ loader.unload();
+};
+
+
+exports["test PBPW Single DOM Selection"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let window = yield open(URL, { private: true });
+
+ selectFirstDiv(window);
+
+ assert.equal(selection.isContiguous, false,
+ "selection.isContiguous with single DOM Selection in PBPW works.");
+
+ assert.equal(selection.text, null,
+ "selection.text with single DOM Selection in PBPW works.");
+
+ assert.equal(selection.html, null,
+ "selection.html with single DOM Selection in PBPW works.");
+
+ let selectionCount = 0;
+ for (let sel of selection)
+ selectionCount++;
+
+ assert.equal(selectionCount, 0, "No iterable selection in PBPW");
+
+ yield cleanUI();
+ loader.unload();
+};
+
+exports["test PBPW Textarea Selection"] = function*(assert) {
+ let loader = Loader(module);
+ let selection = loader.require("sdk/selection");
+ let window = yield open(URL, { private: true });
+
+ selectTextarea(window);
+
+ assert.equal(selection.isContiguous, false,
+ "selection.isContiguous with Textarea Selection in PBPW works.");
+
+ assert.equal(selection.text, null,
+ "selection.text with Textarea Selection in PBPW works.");
+
+ assert.strictEqual(selection.html, null,
+ "selection.html with Textarea Selection in PBPW works.");
+
+ let selectionCount = 0;
+ for (let sel of selection) {
+ selectionCount++;
+
+ assert.equal(sel.text, null,
+ "iterable selection.text with Textarea Selection in PBPW works.");
+
+ assert.strictEqual(sel.html, null,
+ "iterable selection.html with Textarea Selection in PBPW works.");
+ }
+
+ assert.equal(selectionCount, 0, "No iterable selection in PBPW");
+
+ yield cleanUI();
+ loader.unload();
+};
+
+// TODO: test Selection Listener on long-held connection (Bug 661884)
+//
+// I didn't find a way to do so with httpd, using `processAsync` I'm able to
+// Keep the connection but not to flush the buffer to the client in two steps,
+// that is what I need for this test (e.g. flush "Hello" to the client, makes
+// selection when the connection is still hold, and check that the listener
+// is executed before the server send "World" and close the connection).
+//
+// Because this test is needed to the refactoring of context-menu as well, I
+// believe we will find a proper solution quickly.
+/*
+exports["test Selection Listener on long-held connection"] = function(assert, done) {
+
+};
+*/
+
+// If the platform doesn't support the PBPW, we're replacing PBPW tests
+if (!require("sdk/private-browsing/utils").isWindowPBSupported) {
+ Object.keys(module.exports).forEach((key) => {
+ if (key.indexOf("test PBPW") === 0) {
+ module.exports[key] = function Unsupported (assert) {
+ assert.pass("Private Window Per Browsing is not supported on this platform.");
+ }
+ }
+ });
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-self.js b/addon-sdk/source/test/test-self.js
new file mode 100644
index 000000000..8c258e122
--- /dev/null
+++ b/addon-sdk/source/test/test-self.js
@@ -0,0 +1,79 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const xulApp = require("sdk/system/xul-app");
+const self = require("sdk/self");
+const { Loader, main, unload, override } = require("toolkit/loader");
+const { PlainTextConsole } = require("sdk/console/plain-text");
+const { Loader: CustomLoader } = require("sdk/test/loader");
+const loaderOptions = require("@loader/options");
+
+exports.testSelf = function(assert) {
+ // Likewise, we can't assert anything about the full URL, because that
+ // depends on self.id . We can only assert that it ends in the right
+ // thing.
+ var url = self.data.url("test.html");
+ assert.equal(typeof(url), "string", "self.data.url('x') returns string");
+ assert.equal(/\/test\.html$/.test(url), true);
+
+ // Make sure 'undefined' is not in url when no string is provided.
+ url = self.data.url();
+ assert.equal(typeof(url), "string", "self.data.url() returns string");
+ assert.equal(/\/undefined$/.test(url), false);
+
+ // When tests are run on just the api-utils package, self.name is
+ // api-utils. When they're run as 'cfx testall', self.name is testpkgs.
+ assert.equal(self.name, "addon-sdk", "self.name is addon-sdk");
+
+ // loadReason may change here, as we change the way tests addons are installed
+ // Bug 854937 fixed loadReason and is now install
+ let testLoadReason = xulApp.versionInRange(xulApp.platformVersion,
+ "23.0a1", "*") ? "install"
+ : "startup";
+ assert.equal(self.loadReason, testLoadReason,
+ "self.loadReason is either startup or install on test runs");
+
+ assert.equal(self.isPrivateBrowsingSupported, false,
+ 'usePrivateBrowsing property is false by default');
+};
+
+exports.testSelfHandlesLackingLoaderOptions = function (assert) {
+ let root = module.uri.substr(0, module.uri.lastIndexOf('/'));
+ let uri = root + '/fixtures/loader/self/';
+ let sdkPath = loaderOptions.paths[''] + 'sdk';
+ let loader = Loader({ paths: { '': uri, 'sdk': sdkPath }});
+ let program = main(loader, 'main');
+ let self = program.self;
+ assert.pass("No errors thrown when including sdk/self without loader options");
+ assert.equal(self.isPrivateBrowsingSupported, false,
+ "safely checks sdk/self.isPrivateBrowsingSupported");
+ assert.equal(self.packed, false,
+ "safely checks sdk/self.packed");
+ unload(loader);
+};
+
+exports.testPreferencesBranch = function (assert) {
+ let options = override(loaderOptions, {
+ preferencesBranch: 'human-readable',
+ });
+ let loader = CustomLoader(module, { }, options);
+ let { preferencesBranch } = loader.require('sdk/self');
+ assert.equal(preferencesBranch, 'human-readable',
+ 'preferencesBranch is human-readable');
+}
+
+exports.testInvalidPreferencesBranch = function (assert) {
+ let console = new PlainTextConsole(_ => void _);
+ let options = override(loaderOptions, {
+ preferencesBranch: 'invalid^branch*name',
+ id: 'simple@jetpack'
+ });
+ let loader = CustomLoader(module, { console }, options);
+ let { preferencesBranch } = loader.require('sdk/self');
+ assert.equal(preferencesBranch, 'simple@jetpack',
+ 'invalid preferencesBranch value ignored');
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-sequence.js b/addon-sdk/source/test/test-sequence.js
new file mode 100644
index 000000000..90a12edd1
--- /dev/null
+++ b/addon-sdk/source/test/test-sequence.js
@@ -0,0 +1,1245 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+var { seq, iterate, filter, map, reductions, reduce, count,
+ isEmpty, every, isEvery, some, take, takeWhile, drop,
+ dropWhile, concat, first, rest, nth, last, dropLast,
+ distinct, remove, mapcat, fromEnumerator, string,
+ object, pairs, keys, values, each, names, symbols
+ } = require("sdk/util/sequence");
+
+const boom = () => { throw new Error("Boom!"); };
+const broken = seq(function*() {
+ yield 1;
+ throw new Error("Boom!");
+});
+
+exports["test seq"] = assert => {
+ let xs = seq(function*() {
+ yield 1;
+ yield 2;
+ yield 3;
+ });
+
+ assert.deepEqual([...seq(null)], [], "seq of null is empty");
+ assert.deepEqual([...seq(void(0))], [], "seq of void is empty");
+ assert.deepEqual([...xs], [1, 2, 3], "seq of 1 2 3");
+ assert.deepEqual([...seq(xs)], [1, 2, 3], "seq of seq is seq");
+
+ assert.deepEqual([...seq([])], [], "seq of emtpy array is empty");
+ assert.deepEqual([...seq([1])], [1], "seq of lonly array is single element");
+ assert.deepEqual([...seq([1, 2, 3])], [1, 2, 3], "seq of array is it's elements");
+
+ assert.deepEqual([...seq("")], [], "seq of emtpy string is empty");
+ assert.deepEqual([...seq("o")], ["o"], "seq of char is single char seq");
+ assert.deepEqual([...seq("hello")], ["h", "e", "l", "l", "o"],
+ "seq of string are chars");
+
+ assert.deepEqual([...seq(new Set())], [], "seq of emtpy set is empty");
+ assert.deepEqual([...seq(new Set([1]))], [1], "seq of lonely set is single");
+ assert.deepEqual([...seq(new Set([1, 2, 3]))], [1, 2, 3], "seq of lonely set is single");
+
+ assert.deepEqual([...seq(new Map())], [], "seq of emtpy map is empty");
+ assert.deepEqual([...seq(new Map([[1, 2]]))], [[1, 2]], "seq single mapping is that mapping");
+ assert.deepEqual([...seq(new Map([[1, 2], [3, 4], [5, 6]]))],
+ [[1, 2], [3, 4], [5, 6]],
+ "seq of map is key value mappings");
+
+ [function(){}, 1, /foo/, true].forEach(x => {
+ assert.throws(() => seq(x), "Type is not seq-able");
+ });
+
+ assert.throws(() => [...broken],
+ /Boom/,
+ "broken sequence errors propagate");
+};
+
+exports["test seq casting"] = assert => {
+ const xs = seq(function*() { yield 1; yield 2; yield 3; });
+ const ys = seq(function*() { yield 1; });
+ const zs = seq(function*() {});
+ const kvs = seq(function*() { yield ["a", 1]; yield ["b", 2]; });
+ const kv = seq(function*() { yield ["a", 1]; });
+
+ assert.deepEqual([...xs], [1, 2, 3], "cast to array");
+ assert.deepEqual([...ys], [1], "cast to of one element");
+ assert.deepEqual([...zs], [], "cast empty array");
+
+ assert.deepEqual(string(...xs), "123", "cast to string");
+ assert.deepEqual(string(...ys), "1", "cast to char");
+ assert.deepEqual(string(...zs), "", "cast to empty string");
+
+ assert.deepEqual(new Set(xs), new Set([1, 2, 3]),
+ "cast to set of items");
+ assert.deepEqual(new Set(ys), new Set([1]),
+ "cast to set of one item");
+ assert.deepEqual(new Set(zs), new Set(),
+ "cast to set of one item");
+
+ assert.deepEqual(new Map(kvs), new Map([["a", 1], ["b", 2]]),
+ "cast to map");
+ assert.deepEqual(new Map(kv), new Map([["a", 1]]),
+ "cast to single mapping");
+ assert.deepEqual(new Map(zs), new Map(),
+ "cast to empty map");
+
+ assert.deepEqual(object(...kvs), {a: 1, b: 2},
+ "cast to object");
+ assert.deepEqual(object(...kv), {a: 1},
+ "cast to single pair");
+ assert.deepEqual(object(...zs), {},
+ "cast to empty object");
+};
+
+exports["test pairs"] = assert => {
+ assert.deepEqual([...pairs(null)], [], "pairs on null is empty");
+ assert.deepEqual([...pairs(void(0))], [], "pairs on void is empty");
+ assert.deepEqual([...pairs({})], [], "empty sequence");
+ assert.deepEqual([...pairs({a: 1})], [["a", 1]], "single pair");
+ assert.deepEqual([...pairs({a: 1, b: 2, c: 3})].sort(),
+ [["a", 1], ["b", 2], ["c", 3]],
+ "creates pairs");
+ let items = [];
+ for (let [key, value] of pairs({a: 1, b: 2, c: 3}))
+ items.push([key, value]);
+
+ assert.deepEqual(items.sort(),
+ [["a", 1], ["b", 2], ["c", 3]],
+ "for of works on pairs");
+
+
+ assert.deepEqual([...pairs([])], [], "pairs on empty array is empty");
+ assert.deepEqual([...pairs([1])], [[0, 1]], "pairs on array is [index, element]");
+ assert.deepEqual([...pairs([1, 2, 3])],
+ [[0, 1], [1, 2], [2, 3]],
+ "for arrays it pair of [index, element]");
+
+ assert.deepEqual([...pairs("")], [], "pairs on empty string is empty");
+ assert.deepEqual([...pairs("a")], [[0, "a"]], "pairs on char is [0, char]");
+ assert.deepEqual([...pairs("hello")],
+ [[0, "h"], [1, "e"], [2, "l"], [3, "l"], [4, "o"]],
+ "for strings it's pair of [index, char]");
+
+ assert.deepEqual([...pairs(new Map())],
+ [],
+ "pairs on empty map is empty");
+ assert.deepEqual([...pairs(new Map([[1, 3]]))],
+ [[1, 3]],
+ "pairs on single mapping single mapping");
+ assert.deepEqual([...pairs(new Map([[1, 2], [3, 4]]))],
+ [[1, 2], [3, 4]],
+ "pairs on map returs key vaule pairs");
+
+ assert.throws(() => pairs(new Set()),
+ "can't pair set");
+
+ assert.throws(() => pairs(4),
+ "can't pair number");
+
+ assert.throws(() => pairs(true),
+ "can't pair boolean");
+};
+
+exports["test keys"] = assert => {
+ assert.deepEqual([...keys(null)], [], "keys on null is empty");
+ assert.deepEqual([...keys(void(0))], [], "keys on void is empty");
+ assert.deepEqual([...keys({})], [], "empty sequence");
+ assert.deepEqual([...keys({a: 1})], ["a"], "single key");
+ assert.deepEqual([...keys({a: 1, b: 2, c: 3})].sort(),
+ ["a", "b", "c"],
+ "all keys");
+ assert.deepEqual([...keys(Object.create({}, {
+ a: {value: 1, enumerable: true},
+ b: {value: 2, enumerable: false},
+ c: {value: 3, enumerable: false}
+ }))].sort(),
+ ["a"],
+ "only enumerable keys");
+
+ let items = [];
+ for (let key of keys({a: 1, b: 2, c: 3}))
+ items.push(key);
+
+ assert.deepEqual(items.sort(),
+ ["a", "b", "c"],
+ "for of works on keys");
+
+
+ assert.deepEqual([...keys([])], [], "keys on empty array is empty");
+ assert.deepEqual([...keys([1])], [0], "keys on array is indexes");
+ assert.deepEqual([...keys([1, 2, 3])],
+ [0, 1, 2],
+ "keys on arrays returns indexes");
+
+ assert.deepEqual([...keys("")], [], "keys on empty string is empty");
+ assert.deepEqual([...keys("a")], [0], "keys on char is 0");
+ assert.deepEqual([...keys("hello")],
+ [0, 1, 2, 3, 4],
+ "keys on strings is char indexes");
+
+ assert.deepEqual([...keys(new Map())],
+ [],
+ "keys on empty map is empty");
+ assert.deepEqual([...keys(new Map([[1, 3]]))],
+ [1],
+ "keys on single mapping single mapping is single key");
+ assert.deepEqual([...keys(new Map([[1, 2], [3, 4]]))],
+ [1, 3],
+ "keys on map is keys from map");
+
+ assert.throws(() => keys(new Set()),
+ "can't keys set");
+
+ assert.throws(() => keys(4),
+ "can't keys number");
+
+ assert.throws(() => keys(true),
+ "can't keys boolean");
+};
+
+exports["test names"] = assert => {
+ assert.deepEqual([...names(null)], [], "names on null is empty");
+ assert.deepEqual([...names(void(0))], [], "names on void is empty");
+ assert.deepEqual([...names({})], [], "empty sequence");
+ assert.deepEqual([...names({a: 1})], ["a"], "single key");
+ assert.deepEqual([...names({a: 1, b: 2, c: 3})].sort(),
+ ["a", "b", "c"],
+ "all keys");
+ assert.deepEqual([...names(Object.create({}, {
+ a: {value: 1, enumerable: true},
+ b: {value: 2, enumerable: false},
+ c: {value: 3, enumerable: false}
+ }))].sort(),
+ ["a", "b", "c"],
+ "all enumerable & non-enumerable keys");
+
+
+ assert.throws(() => names([1, 2, 3]),
+ "can't names array");
+
+ assert.throws(() => names(""),
+ "can't names string");
+
+ assert.throws(() => names(new Map()),
+ "can't names map");
+
+ assert.throws(() => names(new Set()),
+ "can't names set");
+
+ assert.throws(() => names(4),
+ "can't names number");
+
+ assert.throws(() => names(true),
+ "can't names boolean");
+};
+
+exports["test symbols"] = assert => {
+ assert.deepEqual([...symbols(null)], [], "symbols on null is empty");
+ assert.deepEqual([...symbols(void(0))], [], "symbols on void is empty");
+ assert.deepEqual([...symbols({})], [], "symbols on empty is empty");
+ assert.deepEqual([...symbols({a: 1})], [], "symbols is empty if not owned");
+
+ const b = Symbol("b");
+ assert.deepEqual([...symbols({a: 1, [b]: 2, c: 3})].sort(),
+ [b],
+ "only symbols");
+
+ assert.deepEqual([...symbols(Object.create({}, {
+ a: {value: 1, enumerable: true},
+ [b]: {value: 2, enumerable: false},
+ c: {value: 3, enumerable: false}
+ }))].sort(),
+ [b],
+ "includes non-enumerable symbols");
+
+
+ assert.throws(() => symbols([1, 2, 3]),
+ "can't symbols array");
+
+ assert.throws(() => symbols(""),
+ "can't symbols string");
+
+ assert.throws(() => symbols(new Map()),
+ "can't symbols map");
+
+ assert.throws(() => symbols(new Set()),
+ "can't symbols set");
+
+ assert.throws(() => symbols(4),
+ "can't symbols number");
+
+ assert.throws(() => symbols(true),
+ "can't symbols boolean");
+};
+
+exports["test values"] = assert => {
+ assert.deepEqual([...values({})], [], "empty sequence");
+ assert.deepEqual([...values({a: 1})], [1], "single value");
+ assert.deepEqual([...values({a: 1, b: 2, c: 3})].sort(),
+ [1, 2, 3],
+ "all values");
+
+ let items = [];
+ for (let value of values({a: 1, b: 2, c: 3}))
+ items.push(value);
+
+ assert.deepEqual(items.sort(),
+ [1, 2, 3],
+ "for of works on values");
+
+ assert.deepEqual([...values([])], [], "values on empty array is empty");
+ assert.deepEqual([...values([1])], [1], "values on array elements");
+ assert.deepEqual([...values([1, 2, 3])],
+ [1, 2, 3],
+ "values on arrays returns elements");
+
+ assert.deepEqual([...values("")], [], "values on empty string is empty");
+ assert.deepEqual([...values("a")], ["a"], "values on char is char");
+ assert.deepEqual([...values("hello")],
+ ["h", "e", "l", "l", "o"],
+ "values on strings is chars");
+
+ assert.deepEqual([...values(new Map())],
+ [],
+ "values on empty map is empty");
+ assert.deepEqual([...values(new Map([[1, 3]]))],
+ [3],
+ "keys on single mapping single mapping is single key");
+ assert.deepEqual([...values(new Map([[1, 2], [3, 4]]))],
+ [2, 4],
+ "values on map is values from map");
+
+ assert.deepEqual([...values(new Set())], [], "values on empty set is empty");
+ assert.deepEqual([...values(new Set([1]))], [1], "values on set is it's items");
+ assert.deepEqual([...values(new Set([1, 2, 3]))],
+ [1, 2, 3],
+ "values on set is it's items");
+
+
+ assert.throws(() => values(4),
+ "can't values number");
+
+ assert.throws(() => values(true),
+ "can't values boolean");
+};
+
+exports["test fromEnumerator"] = assert => {
+ const { Cc, Ci } = require("chrome");
+ const { enumerateObservers,
+ addObserver,
+ removeObserver } = Cc["@mozilla.org/observer-service;1"].
+ getService(Ci.nsIObserverService);
+
+
+ const topic = "sec:" + Math.random().toString(32).substr(2);
+ const [a, b, c] = [{wrappedJSObject: {}},
+ {wrappedJSObject: {}},
+ {wrappedJSObject: {}}];
+ const unwrap = x => x.wrappedJSObject;
+
+ [a, b, c].forEach(x => addObserver(x, topic, false));
+
+ const xs = fromEnumerator(() => enumerateObservers(topic));
+ const ys = map(unwrap, xs);
+
+ assert.deepEqual([...ys], [a, b, c].map(unwrap),
+ "all observers are there");
+
+ removeObserver(b, topic);
+
+ assert.deepEqual([...ys], [a, c].map(unwrap),
+ "b was removed");
+
+ removeObserver(a, topic);
+
+ assert.deepEqual([...ys], [c].map(unwrap),
+ "a was removed");
+
+ removeObserver(c, topic);
+
+ assert.deepEqual([...ys], [],
+ "c was removed, now empty");
+
+ addObserver(a, topic, false);
+
+ assert.deepEqual([...ys], [a].map(unwrap),
+ "a was added");
+
+ removeObserver(a, topic);
+
+ assert.deepEqual([...ys], [].map(unwrap),
+ "a was removed, now empty");
+
+};
+
+exports["test filter"] = assert => {
+ const isOdd = x => x % 2;
+ const odds = seq(function*() { yield 1; yield 3; yield 5; });
+ const evens = seq(function*() { yield 2; yield 4; yield 6; });
+ const mixed = seq(function*() {
+ yield 1;
+ yield 2;
+ yield 3;
+ yield 4;
+ });
+
+ assert.deepEqual([...filter(isOdd, mixed)], [1, 3],
+ "filtered odds");
+ assert.deepEqual([...filter(isOdd, odds)], [1, 3, 5],
+ "kept all");
+ assert.deepEqual([...filter(isOdd, evens)], [],
+ "kept none");
+
+
+ let xs = filter(boom, mixed);
+ assert.throws(() => [...xs], /Boom/, "errors propagate");
+
+ assert.throws(() => [...filter(isOdd, broken)], /Boom/,
+ "sequence errors propagate");
+};
+
+exports["test filter array"] = assert => {
+ let isOdd = x => x % 2;
+ let xs = filter(isOdd, [1, 2, 3, 4]);
+ let ys = filter(isOdd, [1, 3, 5]);
+ let zs = filter(isOdd, [2, 4, 6]);
+
+ assert.deepEqual([...xs], [1, 3], "filteres odds");
+ assert.deepEqual([...ys], [1, 3, 5], "kept all");
+ assert.deepEqual([...zs], [], "kept none");
+ assert.ok(!Array.isArray(xs));
+};
+
+exports["test filter set"] = assert => {
+ let isOdd = x => x % 2;
+ let xs = filter(isOdd, new Set([1, 2, 3, 4]));
+ let ys = filter(isOdd, new Set([1, 3, 5]));
+ let zs = filter(isOdd, new Set([2, 4, 6]));
+
+ assert.deepEqual([...xs], [1, 3], "filteres odds");
+ assert.deepEqual([...ys], [1, 3, 5], "kept all");
+ assert.deepEqual([...zs], [], "kept none");
+};
+
+exports["test filter string"] = assert => {
+ let isUpperCase = x => x.toUpperCase() === x;
+ let xs = filter(isUpperCase, "aBcDe");
+ let ys = filter(isUpperCase, "ABC");
+ let zs = filter(isUpperCase, "abcd");
+
+ assert.deepEqual([...xs], ["B", "D"], "filteres odds");
+ assert.deepEqual([...ys], ["A", "B", "C"], "kept all");
+ assert.deepEqual([...zs], [], "kept none");
+};
+
+exports["test filter lazy"] = assert => {
+ const x = 1;
+ let y = 2;
+
+ const xy = seq(function*() { yield x; yield y; });
+ const isOdd = x => x % 2;
+ const actual = filter(isOdd, xy);
+
+ assert.deepEqual([...actual], [1], "only one odd number");
+ y = 3;
+ assert.deepEqual([...actual], [1, 3], "filter is lazy");
+};
+
+exports["test filter non sequences"] = assert => {
+ const False = _ => false;
+ assert.throws(() => [...filter(False, 1)],
+ "can't iterate number");
+ assert.throws(() => [...filter(False, {a: 1, b:2})],
+ "can't iterate object");
+};
+
+exports["test map"] = assert => {
+ let inc = x => x + 1;
+ let xs = seq(function*() { yield 1; yield 2; yield 3; });
+ let ys = map(inc, xs);
+
+ assert.deepEqual([...ys], [2, 3, 4], "incremented each item");
+
+ assert.deepEqual([...map(inc, null)], [], "mapping null is empty");
+ assert.deepEqual([...map(inc, void(0))], [], "mapping void is empty");
+ assert.deepEqual([...map(inc, new Set([1, 2, 3]))], [2, 3, 4], "maps set items");
+};
+
+exports["test map two inputs"] = assert => {
+ let sum = (x, y) => x + y;
+ let xs = seq(function*() { yield 1; yield 2; yield 3; });
+ let ys = seq(function*() { yield 4; yield 5; yield 6; });
+
+ let zs = map(sum, xs, ys);
+
+ assert.deepEqual([...zs], [5, 7, 9], "summed numbers");
+};
+
+exports["test map diff sized inputs"] = assert => {
+ let sum = (x, y) => x + y;
+ let xs = seq(function*() { yield 1; yield 2; yield 3; });
+ let ys = seq(function*() { yield 4; yield 5; yield 6; yield 7; yield 8; });
+
+ let zs = map(sum, xs, ys);
+
+ assert.deepEqual([...zs], [5, 7, 9], "summed numbers");
+ assert.deepEqual([...map(sum, ys, xs)], [5, 7, 9],
+ "index of exhasting input is irrelevant");
+};
+
+exports["test map multi"] = assert => {
+ let sum = (x, y, z, w) => x + y + z + w;
+ let xs = seq(function*() { yield 1; yield 2; yield 3; yield 4; });
+ let ys = seq(function*() { yield 4; yield 5; yield 6; yield 7; yield 8; });
+ let zs = seq(function*() { yield 10; yield 11; yield 12; });
+ let ws = seq(function*() { yield 0; yield 20; yield 40; yield 60; });
+
+ let actual = map(sum, xs, ys, zs, ws);
+
+ assert.deepEqual([...actual], [15, 38, 61], "summed numbers");
+};
+
+exports["test map errors"] = assert => {
+ assert.deepEqual([...map(boom, [])], [],
+ "won't throw if empty");
+
+ const xs = map(boom, [1, 2, 4]);
+
+ assert.throws(() => [...xs], /Boom/, "propagates errors");
+
+ assert.throws(() => [...map(x => x, broken)], /Boom/,
+ "sequence errors propagate");
+};
+
+exports["test reductions"] = assert => {
+ let sum = (...xs) => xs.reduce((x, y) => x + y, 0);
+
+ assert.deepEqual([...reductions(sum, [1, 1, 1, 1])],
+ [1, 2, 3, 4],
+ "works with arrays");
+ assert.deepEqual([...reductions(sum, 5, [1, 1, 1, 1])],
+ [5, 6, 7, 8, 9],
+ "array with initial");
+
+ assert.deepEqual([...reductions(sum, seq(function*() {
+ yield 1;
+ yield 2;
+ yield 3;
+ }))],
+ [1, 3, 6],
+ "works with sequences");
+
+ assert.deepEqual([...reductions(sum, 10, seq(function*() {
+ yield 1;
+ yield 2;
+ yield 3;
+ }))],
+ [10, 11, 13, 16],
+ "works with sequences");
+
+ assert.deepEqual([...reductions(sum, [])], [0],
+ "invokes accumulator with no args");
+
+ assert.throws(() => [...reductions(boom, 1, [1])],
+ /Boom/,
+ "arg errors errors propagate");
+ assert.throws(() => [...reductions(sum, 1, broken)],
+ /Boom/,
+ "sequence errors propagate");
+};
+
+exports["test reduce"] = assert => {
+ let sum = (...xs) => xs.reduce((x, y) => x + y, 0);
+
+ assert.deepEqual(reduce(sum, [1, 2, 3, 4, 5]),
+ 15,
+ "works with arrays");
+
+ assert.deepEqual(reduce(sum, seq(function*() {
+ yield 1;
+ yield 2;
+ yield 3;
+ })),
+ 6,
+ "works with sequences");
+
+ assert.deepEqual(reduce(sum, 10, [1, 2, 3, 4, 5]),
+ 25,
+ "works with array & initial");
+
+ assert.deepEqual(reduce(sum, 5, seq(function*() {
+ yield 1;
+ yield 2;
+ yield 3;
+ })),
+ 11,
+ "works with sequences & initial");
+
+ assert.deepEqual(reduce(sum, []), 0, "reduce with no args");
+ assert.deepEqual(reduce(sum, "a", []), "a", "reduce with initial");
+ assert.deepEqual(reduce(sum, 1, [1]), 2, "reduce with single & initial");
+
+ assert.throws(() => [...reduce(boom, 1, [1])],
+ /Boom/,
+ "arg errors errors propagate");
+ assert.throws(() => [...reduce(sum, 1, broken)],
+ /Boom/,
+ "sequence errors propagate");
+};
+
+exports["test each"] = assert => {
+ const collect = xs => {
+ let result = [];
+ each((...etc) => result.push(...etc), xs);
+ return result;
+ };
+
+ assert.deepEqual(collect(null), [], "each ignores null");
+ assert.deepEqual(collect(void(0)), [], "each ignores void");
+
+ assert.deepEqual(collect([]), [], "each ignores empty");
+ assert.deepEqual(collect([1]), [1], "each works on single item arrays");
+ assert.deepEqual(collect([1, 2, 3, 4, 5]),
+ [1, 2, 3, 4, 5],
+ "works with arrays");
+
+ assert.deepEqual(collect(seq(function*() {
+ yield 1;
+ yield 2;
+ yield 3;
+ })),
+ [1, 2, 3],
+ "works with sequences");
+
+ assert.deepEqual(collect(""), [], "ignores empty strings");
+ assert.deepEqual(collect("a"), ["a"], "works on chars");
+ assert.deepEqual(collect("hello"), ["h", "e", "l", "l", "o"],
+ "works on strings");
+
+ assert.deepEqual(collect(new Set()), [], "ignores empty sets");
+ assert.deepEqual(collect(new Set(["a"])), ["a"],
+ "works on single item sets");
+ assert.deepEqual(collect(new Set([1, 2, 3])), [1, 2, 3],
+ "works on muti item tests");
+
+ assert.deepEqual(collect(new Map()), [], "ignores empty maps");
+ assert.deepEqual(collect(new Map([["a", 1]])), [["a", 1]],
+ "works on single mapping maps");
+ assert.deepEqual(collect(new Map([[1, 2], [3, 4], [5, 6]])),
+ [[1, 2], [3, 4], [5, 6]],
+ "works on muti mapping maps");
+
+ assert.throws(() => collect({}), "objects arn't supported");
+ assert.throws(() => collect(1), "numbers arn't supported");
+ assert.throws(() => collect(true), "booleas arn't supported");
+};
+
+exports["test count"] = assert => {
+ assert.equal(count(null), 0, "null counts to 0");
+ assert.equal(count(), 0, "undefined counts to 0");
+ assert.equal(count([]), 0, "empty array");
+ assert.equal(count([1, 2, 3]), 3, "non-empty array");
+ assert.equal(count(""), 0, "empty string");
+ assert.equal(count("hello"), 5, "non-empty string");
+ assert.equal(count(new Map()), 0, "empty map");
+ assert.equal(count(new Map([[1, 2], [2, 3]])), 2, "non-empty map");
+ assert.equal(count(new Set()), 0, "empty set");
+ assert.equal(count(new Set([1, 2, 3, 4])), 4, "non-empty set");
+ assert.equal(count(seq(function*() {})), 0, "empty sequence");
+ assert.equal(count(seq(function*() { yield 1; yield 2; })), 2,
+ "non-empty sequence");
+
+ assert.throws(() => count(broken),
+ /Boom/,
+ "sequence errors propagate");
+};
+
+exports["test isEmpty"] = assert => {
+ assert.equal(isEmpty(null), true, "null is empty");
+ assert.equal(isEmpty(), true, "undefined is empty");
+ assert.equal(isEmpty([]), true, "array is array");
+ assert.equal(isEmpty([1, 2, 3]), false, "array isn't empty");
+ assert.equal(isEmpty(""), true, "string is empty");
+ assert.equal(isEmpty("hello"), false, "non-empty string");
+ assert.equal(isEmpty(new Map()), true, "empty map");
+ assert.equal(isEmpty(new Map([[1, 2], [2, 3]])), false, "non-empty map");
+ assert.equal(isEmpty(new Set()), true, "empty set");
+ assert.equal(isEmpty(new Set([1, 2, 3, 4])), false , "non-empty set");
+ assert.equal(isEmpty(seq(function*() {})), true, "empty sequence");
+ assert.equal(isEmpty(seq(function*() { yield 1; yield 2; })), false,
+ "non-empty sequence");
+
+ assert.equal(isEmpty(broken), false, "hasn't reached error");
+};
+
+exports["test isEvery"] = assert => {
+ let isOdd = x => x % 2;
+ let isTrue = x => x === true;
+ let isFalse = x => x === false;
+
+ assert.equal(isEvery(isOdd, seq(function*() {
+ yield 1;
+ yield 3;
+ yield 5;
+ })), true, "all are odds");
+
+ assert.equal(isEvery(isOdd, seq(function*() {
+ yield 1;
+ yield 2;
+ yield 3;
+ })), false, "contains even");
+
+ assert.equal(isEvery(isTrue, seq(function*() {})), true, "true if empty");
+ assert.equal(isEvery(isFalse, seq(function*() {})), true, "true if empty");
+
+ assert.equal(isEvery(isTrue, null), true, "true for null");
+ assert.equal(isEvery(isTrue, undefined), true, "true for undefined");
+
+ assert.throws(() => isEvery(boom, [1, 2]),
+ /Boom/,
+ "arg errors errors propagate");
+ assert.throws(() => isEvery(x => true, broken),
+ /Boom/,
+ "sequence errors propagate");
+
+ assert.equal(isEvery(x => false, broken), false,
+ "hasn't reached error");
+};
+
+exports["test some"] = assert => {
+ let isOdd = x => x % 2;
+ let isTrue = x => x === true;
+ let isFalse = x => x === false;
+
+ assert.equal(some(isOdd, seq(function*() {
+ yield 2;
+ yield 4;
+ yield 6;
+ })), null, "all are even");
+
+ assert.equal(some(isOdd, seq(function*() {
+ yield 2;
+ yield 3;
+ yield 4;
+ })), true, "contains odd");
+
+ assert.equal(some(isTrue, seq(function*() {})), null,
+ "null if empty")
+ assert.equal(some(isFalse, seq(function*() {})), null,
+ "null if empty")
+
+ assert.equal(some(isTrue, null), null, "null for null");
+ assert.equal(some(isTrue, undefined), null, "null for undefined");
+
+ assert.throws(() => some(boom, [1, 2]),
+ /Boom/,
+ "arg errors errors propagate");
+ assert.throws(() => some(x => false, broken),
+ /Boom/,
+ "sequence errors propagate");
+
+ assert.equal(some(x => true, broken), true,
+ "hasn't reached error");
+};
+
+exports["test take"] = assert => {
+ let xs = seq(function*() {
+ yield 1;
+ yield 2;
+ yield 3;
+ yield 4;
+ yield 5;
+ yield 6;
+ });
+
+ assert.deepEqual([...take(3, xs)], [1, 2, 3], "took 3 items");
+ assert.deepEqual([...take(3, [1, 2, 3, 4, 5])], [1, 2, 3],
+ "took 3 from array");
+
+ let ys = seq(function*() { yield 1; yield 2; });
+ assert.deepEqual([...take(3, ys)], [1, 2], "takes at max n");
+ assert.deepEqual([...take(3, [1, 2])], [1, 2],
+ "takes at max n from arary");
+
+ let empty = seq(function*() {});
+ assert.deepEqual([...take(5, empty)], [], "nothing to take");
+
+ assert.throws(() => [...take(3, broken)],
+ /Boom/,
+ "sequence errors propagate");
+
+ assert.deepEqual([...take(1, broken)], [1],
+ "hasn't reached error");
+};
+
+exports["test iterate"] = assert => {
+ let inc = x => x + 1;
+ let nums = iterate(inc, 0);
+
+ assert.deepEqual([...take(5, nums)], [0, 1, 2, 3, 4], "took 5");
+ assert.deepEqual([...take(10, nums)], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], "took 10");
+
+ let xs = iterate(x => x * 3, 2);
+ assert.deepEqual([...take(4, xs)], [2, 6, 18, 54], "took 4");
+
+ assert.throws(() => [...iterate(boom, 0)],
+ /Boom/,
+ "function exceptions propagate");
+};
+
+exports["test takeWhile"] = assert => {
+ let isNegative = x => x < 0;
+ let xs = seq(function*() {
+ yield -2;
+ yield -1;
+ yield 0;
+ yield 1;
+ yield 2;
+ yield 3;
+ });
+
+ assert.deepEqual([...takeWhile(isNegative, xs)], [-2, -1],
+ "took until 0");
+
+ let ys = seq(function*() {});
+ assert.deepEqual([...takeWhile(isNegative, ys)], [],
+ "took none");
+
+ let zs = seq(function*() {
+ yield 0;
+ yield 1;
+ yield 2;
+ yield 3;
+ });
+
+ assert.deepEqual([...takeWhile(isNegative, zs)], [],
+ "took none");
+
+ assert.throws(() => [...takeWhile(boom, zs)],
+ /Boom/,
+ "function errors errors propagate");
+ assert.throws(() => [...takeWhile(x => true, broken)],
+ /Boom/,
+ "sequence errors propagate");
+
+ assert.deepEqual([...takeWhile(x => false, broken)],
+ [],
+ "hasn't reached error");
+};
+
+exports["test drop"] = assert => {
+ let testDrop = xs => {
+ assert.deepEqual([...drop(2, xs)],
+ [3, 4],
+ "dropped two elements");
+
+ assert.deepEqual([...drop(1, xs)],
+ [2, 3, 4],
+ "dropped one");
+
+ assert.deepEqual([...drop(0, xs)],
+ [1, 2, 3, 4],
+ "dropped 0");
+
+ assert.deepEqual([...drop(-2, xs)],
+ [1, 2, 3, 4],
+ "dropped 0 on negative `n`");
+
+ assert.deepEqual([...drop(5, xs)],
+ [],
+ "dropped all items");
+ };
+
+ testDrop([1, 2, 3, 4]);
+ testDrop(seq(function*() {
+ yield 1;
+ yield 2;
+ yield 3;
+ yield 4;
+ }));
+
+ assert.throws(() => [...drop(1, broken)],
+ /Boom/,
+ "sequence errors propagate");
+};
+
+
+exports["test dropWhile"] = assert => {
+ let isNegative = x => x < 0;
+ let True = _ => true;
+ let False = _ => false;
+
+ let test = xs => {
+ assert.deepEqual([...dropWhile(isNegative, xs)],
+ [0, 1, 2],
+ "dropped negative");
+
+ assert.deepEqual([...dropWhile(True, xs)],
+ [],
+ "drop all");
+
+ assert.deepEqual([...dropWhile(False, xs)],
+ [-2, -1, 0, 1, 2],
+ "keep all");
+ };
+
+ test([-2, -1, 0, 1, 2]);
+ test(seq(function*() {
+ yield -2;
+ yield -1;
+ yield 0;
+ yield 1;
+ yield 2;
+ }));
+
+ assert.throws(() => [...dropWhile(boom, [1, 2, 3])],
+ /Boom/,
+ "function errors errors propagate");
+ assert.throws(() => [...dropWhile(x => true, broken)],
+ /Boom/,
+ "sequence errors propagate");
+};
+
+
+exports["test concat"] = assert => {
+ let test = (a, b, c, d) => {
+ assert.deepEqual([...concat()],
+ [],
+ "nothing to concat");
+ assert.deepEqual([...concat(a)],
+ [1, 2, 3],
+ "concat with nothing returns same as first");
+ assert.deepEqual([...concat(a, b)],
+ [1, 2, 3, 4, 5],
+ "concat items from both");
+ assert.deepEqual([...concat(a, b, a)],
+ [1, 2, 3, 4, 5, 1, 2, 3],
+ "concat itself");
+ assert.deepEqual([...concat(c)],
+ [],
+ "concat of empty is empty");
+ assert.deepEqual([...concat(a, c)],
+ [1, 2, 3],
+ "concat with empty");
+ assert.deepEqual([...concat(c, c, c)],
+ [],
+ "concat of empties is empty");
+ assert.deepEqual([...concat(c, b)],
+ [4, 5],
+ "empty can be in front");
+ assert.deepEqual([...concat(d)],
+ [7],
+ "concat singular");
+ assert.deepEqual([...concat(d, d)],
+ [7, 7],
+ "concat singulars");
+
+ assert.deepEqual([...concat(a, a, b, c, d, c, d, d)],
+ [1, 2, 3, 1, 2, 3, 4, 5, 7, 7, 7],
+ "many concats");
+
+ let ab = concat(a, b);
+ let abcd = concat(ab, concat(c, d));
+ let cdabcd = concat(c, d, abcd);
+
+ assert.deepEqual([...cdabcd],
+ [7, 1, 2, 3, 4, 5, 7],
+ "nested concats");
+ };
+
+ test([1, 2, 3],
+ [4, 5],
+ [],
+ [7]);
+
+ test(seq(function*() { yield 1; yield 2; yield 3; }),
+ seq(function*() { yield 4; yield 5; }),
+ seq(function*() { }),
+ seq(function*() { yield 7; }));
+
+ assert.throws(() => [...concat(broken, [1, 2, 3])],
+ /Boom/,
+ "function errors errors propagate");
+};
+
+
+exports["test first"] = assert => {
+ let test = (xs, empty) => {
+ assert.equal(first(xs), 1, "returns first");
+ assert.equal(first(empty), null, "returns null empty");
+ };
+
+ test("1234", "");
+ test([1, 2, 3], []);
+ test([1, 2, 3], null);
+ test([1, 2, 3], undefined);
+ test(seq(function*() { yield 1; yield 2; yield 3; }),
+ seq(function*() { }));
+ assert.equal(first(broken), 1, "did not reached error");
+};
+
+exports["test rest"] = assert => {
+ let test = (xs, x, nil) => {
+ assert.deepEqual([...rest(xs)], ["b", "c"],
+ "rest items");
+ assert.deepEqual([...rest(x)], [],
+ "empty when singular");
+ assert.deepEqual([...rest(nil)], [],
+ "empty when empty");
+ };
+
+ test("abc", "a", "");
+ test(["a", "b", "c"], ["d"], []);
+ test(seq(function*() { yield "a"; yield "b"; yield "c"; }),
+ seq(function*() { yield "d"; }),
+ seq(function*() {}));
+ test(["a", "b", "c"], ["d"], null);
+ test(["a", "b", "c"], ["d"], undefined);
+
+ assert.throws(() => [...rest(broken)],
+ /Boom/,
+ "sequence errors propagate");
+};
+
+
+exports["test nth"] = assert => {
+ let notFound = {};
+ let test = xs => {
+ assert.equal(nth(xs, 0), "h", "first");
+ assert.equal(nth(xs, 1), "e", "second");
+ assert.equal(nth(xs, 5), void(0), "out of bound");
+ assert.equal(nth(xs, 5, notFound), notFound, "out of bound");
+ assert.equal(nth(xs, -1), void(0), "out of bound");
+ assert.equal(nth(xs, -1, notFound), notFound, "out of bound");
+ assert.equal(nth(xs, 4), "o", "5th");
+ };
+
+ let testEmpty = xs => {
+ assert.equal(nth(xs, 0), void(0), "no first in empty");
+ assert.equal(nth(xs, 5), void(0), "no 5th in empty");
+ assert.equal(nth(xs, 0, notFound), notFound, "notFound on out of bound");
+ };
+
+ test("hello");
+ test(["h", "e", "l", "l", "o"]);
+ test(seq(function*() {
+ yield "h";
+ yield "e";
+ yield "l";
+ yield "l";
+ yield "o";
+ }));
+ testEmpty(null);
+ testEmpty(undefined);
+ testEmpty([]);
+ testEmpty("");
+ testEmpty(seq(function*() {}));
+
+
+ assert.throws(() => nth(broken, 1),
+ /Boom/,
+ "sequence errors propagate");
+ assert.equal(nth(broken, 0), 1, "have not reached error");
+};
+
+
+exports["test last"] = assert => {
+ assert.equal(last(null), null, "no last in null");
+ assert.equal(last(void(0)), null, "no last in undefined");
+ assert.equal(last([]), null, "no last in []");
+ assert.equal(last(""), null, "no last in ''");
+ assert.equal(last(seq(function*() { })), null, "no last in empty");
+
+ assert.equal(last("hello"), "o", "last from string");
+ assert.equal(last([1, 2, 3]), 3, "last from array");
+ assert.equal(last([1]), 1, "last from singular");
+ assert.equal(last(seq(function*() {
+ yield 1;
+ yield 2;
+ yield 3;
+ })), 3, "last from sequence");
+
+ assert.throws(() => last(broken),
+ /Boom/,
+ "sequence errors propagate");
+};
+
+
+exports["test dropLast"] = assert => {
+ let test = xs => {
+ assert.deepEqual([...dropLast(xs)],
+ [1, 2, 3, 4],
+ "dropped last");
+ assert.deepEqual([...dropLast(0, xs)],
+ [1, 2, 3, 4, 5],
+ "dropped none on 0");
+ assert.deepEqual([...dropLast(-3, xs)],
+ [1, 2, 3, 4, 5],
+ "drop none on negative");
+ assert.deepEqual([...dropLast(3, xs)],
+ [1, 2],
+ "dropped given number");
+ assert.deepEqual([...dropLast(5, xs)],
+ [],
+ "dropped all");
+ };
+
+ let testEmpty = xs => {
+ assert.deepEqual([...dropLast(xs)],
+ [],
+ "nothing to drop");
+ assert.deepEqual([...dropLast(0, xs)],
+ [],
+ "dropped none on 0");
+ assert.deepEqual([...dropLast(-3, xs)],
+ [],
+ "drop none on negative");
+ assert.deepEqual([...dropLast(3, xs)],
+ [],
+ "nothing to drop");
+ };
+
+ test([1, 2, 3, 4, 5]);
+ test(seq(function*() {
+ yield 1;
+ yield 2;
+ yield 3;
+ yield 4;
+ yield 5;
+ }));
+ testEmpty([]);
+ testEmpty("");
+ testEmpty(seq(function*() {}));
+
+ assert.throws(() => [...dropLast(broken)],
+ /Boom/,
+ "sequence errors propagate");
+};
+
+
+exports["test distinct"] = assert => {
+ let test = (xs, message) => {
+ assert.deepEqual([...distinct(xs)],
+ [1, 2, 3, 4, 5],
+ message);
+ };
+
+ test([1, 2, 1, 3, 1, 4, 1, 5], "works with arrays");
+ test(seq(function*() {
+ yield 1;
+ yield 2;
+ yield 1;
+ yield 3;
+ yield 1;
+ yield 4;
+ yield 1;
+ yield 5;
+ }), "works with sequences");
+ test(new Set([1, 2, 1, 3, 1, 4, 1, 5]),
+ "works with sets");
+ test(seq(function*() {
+ yield 1;
+ yield 2;
+ yield 2;
+ yield 2;
+ yield 1;
+ yield 3;
+ yield 1;
+ yield 4;
+ yield 4;
+ yield 4;
+ yield 1;
+ yield 5;
+ }), "works with multiple repeatitions");
+ test([1, 2, 3, 4, 5], "work with distinct arrays");
+ test(seq(function*() {
+ yield 1;
+ yield 2;
+ yield 3;
+ yield 4;
+ yield 5;
+ }), "works with distinct seqs");
+};
+
+
+exports["test remove"] = assert => {
+ let isPositive = x => x > 0;
+ let test = xs => {
+ assert.deepEqual([...remove(isPositive, xs)],
+ [-2, -1, 0],
+ "removed positives");
+ };
+
+ test([1, -2, 2, -1, 3, 7, 0]);
+ test(seq(function*() {
+ yield 1;
+ yield -2;
+ yield 2;
+ yield -1;
+ yield 3;
+ yield 7;
+ yield 0;
+ }));
+
+ assert.throws(() => [...distinct(broken)],
+ /Boom/,
+ "sequence errors propagate");
+};
+
+
+exports["test mapcat"] = assert => {
+ let upto = n => seq(function* () {
+ let index = 0;
+ while (index < n) {
+ yield index;
+ index = index + 1;
+ }
+ });
+
+ assert.deepEqual([...mapcat(upto, [1, 2, 3, 4])],
+ [0, 0, 1, 0, 1, 2, 0, 1, 2, 3],
+ "expands given sequence");
+
+ assert.deepEqual([...mapcat(upto, [0, 1, 2, 0])],
+ [0, 0, 1],
+ "expands given sequence");
+
+ assert.deepEqual([...mapcat(upto, [0, 0, 0])],
+ [],
+ "expands given sequence");
+
+ assert.deepEqual([...mapcat(upto, [])],
+ [],
+ "nothing to expand");
+
+ assert.deepEqual([...mapcat(upto, null)],
+ [],
+ "nothing to expand");
+
+ assert.deepEqual([...mapcat(upto, void(0))],
+ [],
+ "nothing to expand");
+
+ let xs = seq(function*() {
+ yield 0;
+ yield 1;
+ yield 0;
+ yield 2;
+ yield 0;
+ });
+
+ assert.deepEqual([...mapcat(upto, xs)],
+ [0, 0, 1],
+ "expands given sequence");
+
+ assert.throws(() => [...mapcat(boom, xs)],
+ /Boom/,
+ "function errors errors propagate");
+ assert.throws(() => [...mapcat(upto, broken)],
+ /Boom/,
+ "sequence errors propagate");
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-set-exports.js b/addon-sdk/source/test/test-set-exports.js
new file mode 100644
index 000000000..f70bbd34b
--- /dev/null
+++ b/addon-sdk/source/test/test-set-exports.js
@@ -0,0 +1,37 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+var four = require("./modules/exportsEquals");
+exports.testExportsEquals = function(assert) {
+ assert.equal(four, 4);
+};
+
+/* TODO: Discuss idea of dropping support for this feature that was alternative
+ to `module.exports = ..` that failed.
+var five = require("./modules/setExports");
+exports.testSetExports = function(assert) {
+ assert.equal(five, 5);
+}
+
+exports.testDupeSetExports = function(assert) {
+ var passed = false;
+ try {
+ var dupe = require('./modules/dupeSetExports');
+ } catch(e) {
+ passed = /define\(\) was used, so module\.exports= and module\.setExports\(\) may not be used/.test(e.toString());
+ }
+ assert.equal(passed, true, 'define() or setExports(), not both');
+}
+*/
+
+exports.testModule = function(assert) {
+ // module.id is not cast in stone yet. In the future, it may include the
+ // package name, or may possibly be a/ URL of some sort. For now, it's a
+ // URL that starts with resource: and ends with this module name, but the
+ // part in between varies depending upon how the test is run.
+ var found = /test-set-exports$/.test(module.id);
+ assert.equal(found, true, module.id+" ends with test-set-exports.js");
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-shared-require.js b/addon-sdk/source/test/test-shared-require.js
new file mode 100644
index 000000000..c5a1bf88b
--- /dev/null
+++ b/addon-sdk/source/test/test-shared-require.js
@@ -0,0 +1,36 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Cu } = require("chrome");
+
+const requireURI = require.resolve("toolkit/require.js");
+
+const jsm = Cu.import(requireURI, {});
+
+exports.testRequire = assert => {
+ assert.equal(typeof(jsm.require), "function",
+ "require is a function");
+ assert.equal(typeof(jsm.require.resolve), "function",
+ "require.resolve is a function");
+
+ assert.equal(typeof(jsm.require("method/core")), "function",
+ "can import modules that aren't under sdk");
+
+ assert.equal(typeof(jsm.require("sdk/base64").encode), "function",
+ "can import module from sdk");
+};
+
+const required = require("toolkit/require")
+
+exports.testSameRequire = (assert) => {
+ assert.equal(jsm.require("method/core"),
+ required.require("method/core"),
+ "jsm and module return same instance");
+
+ assert.equal(jsm.require, required.require,
+ "require function is same in both contexts");
+};
+
+require("sdk/test").run(exports)
diff --git a/addon-sdk/source/test/test-simple-prefs.js b/addon-sdk/source/test/test-simple-prefs.js
new file mode 100644
index 000000000..05b548a3c
--- /dev/null
+++ b/addon-sdk/source/test/test-simple-prefs.js
@@ -0,0 +1,331 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Cc, Ci, Cu } = require('chrome');
+const { Loader } = require("sdk/test/loader");
+const { setTimeout } = require("sdk/timers");
+const { emit } = require("sdk/system/events");
+const simplePrefs = require("sdk/simple-prefs");
+const { prefs: sp } = simplePrefs;
+const { defer, resolve, reject, all } = require("sdk/core/promise");
+const AddonInstaller = require("sdk/addon/installer");
+const fixtures = require("./fixtures");
+const { pathFor } = require("sdk/system");
+const file = require("sdk/io/file");
+const { install, uninstall } = require("sdk/addon/installer");
+const { open } = require('sdk/preferences/utils');
+const { toFilename } = require('sdk/url');
+const { getAddonByID } = require('sdk/addon/manager');
+const { ZipWriter } = require('./zip/utils');
+const { getTabForId } = require('sdk/tabs/utils');
+const { preferencesBranch, id } = require('sdk/self');
+const { modelFor } = require('sdk/model/core');
+require('sdk/tabs');
+
+const prefsrv = Cc['@mozilla.org/preferences-service;1'].
+ getService(Ci.nsIPrefService);
+
+const specialChars = "!@#$%^&*()_-=+[]{}~`\'\"<>,./?;:";
+
+exports.testIterations = function(assert) {
+ sp["test"] = true;
+ sp["test.test"] = true;
+ let prefAry = [];
+ for (var name in sp ) {
+ prefAry.push(name);
+ }
+ assert.ok("test" in sp);
+ assert.ok(!sp.getPropertyDescriptor);
+ assert.ok(Object.prototype.hasOwnProperty.call(sp, "test"));
+ assert.equal(["test", "test.test"].toString(), prefAry.filter((i) => !/^sdk\./.test(i)).sort().toString(), "for (x in y) part 1/2 works");
+ assert.equal(["test", "test.test"].toString(), Object.keys(sp).filter((i) => !/^sdk\./.test(i)).sort().toString(), "Object.keys works");
+
+ delete sp["test"];
+ delete sp["test.test"];
+ prefAry = [];
+ for (var name in sp ) {
+ prefAry.push(name);
+ }
+ assert.equal([].toString(), prefAry.filter((i) => !/^sdk\./.test(i)).toString(), "for (x in y) part 2/2 works");
+}
+
+exports.testSetGetBool = function(assert) {
+ assert.equal(sp.test, undefined, "Value should not exist");
+ sp.test = true;
+ assert.ok(sp.test, "Value read should be the value previously set");
+ delete sp.test;
+};
+
+// TEST: setting and getting preferences with special characters work
+exports.testSpecialChars = function(assert, done) {
+ let chars = specialChars.split("");
+ let len = chars.length;
+
+ let count = 0;
+ chars.forEach(function(char) {
+ let rand = Math.random() + "";
+ simplePrefs.on(char, function onPrefChanged() {
+ simplePrefs.removeListener(char, onPrefChanged);
+ assert.equal(sp[char], rand, "setting pref with a name that is a special char, " + char + ", worked!");
+ delete sp[char];
+
+ // end test
+ if (++count == len)
+ done();
+ })
+ sp[char] = rand;
+ });
+};
+
+exports.testSetGetInt = function(assert) {
+ assert.equal(sp["test-int"], undefined, "Value should not exist");
+ sp["test-int"] = 1;
+ assert.equal(sp["test-int"], 1, "Value read should be the value previously set");
+ delete sp["test-int"];
+};
+
+exports.testSetComplex = function(assert) {
+ try {
+ sp["test-complex"] = {test: true};
+ assert.fail("Complex values are not allowed");
+ }
+ catch (e) {
+ assert.pass("Complex values are not allowed");
+ }
+};
+
+exports.testSetGetString = function(assert) {
+ assert.equal(sp["test-string"], undefined, "Value should not exist");
+ sp["test-string"] = "test";
+ assert.equal(sp["test-string"], "test", "Value read should be the value previously set");
+ delete sp["test-string"];
+};
+
+exports.testHasAndRemove = function(assert) {
+ sp.test = true;
+ assert.ok(("test" in sp), "Value exists");
+ delete sp.test;
+ assert.equal(sp.test, undefined, "Value should be undefined");
+};
+
+exports.testPrefListener = function(assert, done) {
+ let listener = function(prefName) {
+ simplePrefs.removeListener('test-listener', listener);
+ assert.equal(prefName, "test-listen", "The prefs listener heard the right event");
+ delete sp["test-listen"];
+ done();
+ };
+
+ simplePrefs.on("test-listen", listener);
+
+ sp["test-listen"] = true;
+
+ // Wildcard listen
+ let toSet = ['wildcard1','wildcard.pref2','wildcard.even.longer.test'];
+ let observed = [];
+
+ let wildlistener = function(prefName) {
+ if (toSet.indexOf(prefName) > -1) observed.push(prefName);
+ };
+
+ simplePrefs.on('',wildlistener);
+
+ toSet.forEach(function(pref) {
+ sp[pref] = true;
+ delete sp[pref];
+ });
+
+ assert.ok((observed.length === 6 && toSet.length === 3),
+ "Wildcard lengths inconsistent" + JSON.stringify([observed.length, toSet.length]));
+
+ toSet.forEach(function(pref,ii) {
+ assert.equal(observed[2*ii], pref, "Wildcard observed " + pref);
+ });
+
+ simplePrefs.removeListener('',wildlistener);
+
+};
+
+exports.testBtnListener = function(assert, done) {
+ let name = "test-btn-listen";
+ simplePrefs.on(name, function listener() {
+ simplePrefs.removeListener(name, listener);
+ assert.pass("Button press event was heard");
+ done();
+ });
+ emit((id + "-cmdPressed"), { subject: "", data: name });
+};
+
+exports.testPrefRemoveListener = function(assert, done) {
+ let counter = 0;
+
+ let listener = function() {
+ assert.pass("The prefs listener was not removed yet");
+
+ if (++counter > 1)
+ assert.fail("The prefs listener was not removed");
+
+ simplePrefs.removeListener("test-listen2", listener);
+
+ sp["test-listen2"] = false;
+
+ setTimeout(function() {
+ assert.pass("The prefs listener was removed");
+ delete sp["test-listen2"];
+ done();
+ }, 250);
+ };
+
+ simplePrefs.on("test-listen2", listener);
+
+ // emit change
+ sp["test-listen2"] = true;
+};
+
+// Bug 710117: Test that simple-pref listeners are removed on unload
+exports.testPrefUnloadListener = function(assert, done) {
+ let loader = Loader(module);
+ let sp = loader.require("sdk/simple-prefs");
+ let counter = 0;
+
+ let listener = function() {
+ assert.equal(++counter, 1, "This listener should only be called once");
+
+ loader.unload();
+
+ // this may not execute after unload, but definitely shouldn't fire listener
+ sp.prefs["test-listen3"] = false;
+ // this should execute, but also definitely shouldn't fire listener
+ require("sdk/simple-prefs").prefs["test-listen3"] = false;
+
+ delete sp.prefs["test-listen3"];
+ done();
+ };
+
+ sp.on("test-listen3", listener);
+
+ // emit change
+ sp.prefs["test-listen3"] = true;
+};
+
+
+// Bug 710117: Test that simple-pref listeners are removed on unload
+exports.testPrefUnloadWildcardListener = function(assert, done) {
+ let testpref = "test-wildcard-unload-listener";
+ let loader = Loader(module);
+ let sp = loader.require("sdk/simple-prefs");
+ let counter = 0;
+
+ let listener = function() {
+ assert.equal(++counter, 1, "This listener should only be called once");
+
+ loader.unload();
+
+ // this may not execute after unload, but definitely shouldn't fire listener
+ sp.prefs[testpref] = false;
+ // this should execute, but also definitely shouldn't fire listener
+ require("sdk/simple-prefs").prefs[testpref] = false;
+
+ delete sp.prefs[testpref];
+ done();
+ };
+
+ sp.on("", listener);
+ // emit change
+ sp.prefs[testpref] = true;
+};
+
+
+// Bug 732919 - JSON.stringify() fails on simple-prefs.prefs
+exports.testPrefJSONStringification = function(assert) {
+ var sp = require("sdk/simple-prefs").prefs;
+ assert.equal(
+ Object.keys(sp).join(),
+ Object.keys(JSON.parse(JSON.stringify(sp))).join(),
+ "JSON stringification should work.");
+};
+
+exports.testUnloadOfDynamicPrefGeneration = function*(assert) {
+ let loader = Loader(module);
+ let branch = prefsrv.getDefaultBranch('extensions.' + preferencesBranch);
+
+ let { enable } = loader.require("sdk/preferences/native-options");
+
+ let addon_id = "test-bootstrap-addon@mozilla.com";
+ let xpi_path = file.join(pathFor("ProfD"), addon_id + ".xpi");
+
+ // zip the add-on
+ let zip = new ZipWriter(xpi_path);
+ assert.pass("start creating the blank xpi");
+ zip.addFile("", toFilename(fixtures.url("bootstrap-addon/")));
+ yield zip.close();
+
+ // insatll the add-on
+ let id = yield install(xpi_path);
+ assert.pass('installed');
+
+ // get the addon
+ let addon = yield getAddonByID(id);
+ assert.equal(id, addon.id, 'addon id: ' + id);
+
+ addon.userDisabled = false;
+ assert.ok(!addon.userDisabled, 'the add-on is enabled');
+ assert.ok(addon.isActive, 'the add-on is active');
+
+ // setup dynamic prefs
+ yield enable({
+ id: addon.id,
+ preferences: [{
+ "name": "test",
+ "description": "test",
+ "title": "Test",
+ "type": "string",
+ "value": "default"
+ }, {
+ "name": "test-int",
+ "description": "test",
+ "type": "integer",
+ "value": 5,
+ "title": "How Many?"
+ }]
+ });
+
+ assert.pass('enabled prefs');
+
+ // show inline prefs
+ let { tabId, document } = yield open(addon);
+ assert.pass('opened');
+
+ // confirm dynamic pref generation did occur
+ let results = document.querySelectorAll("*[data-jetpack-id=\"" + id + "\"]");
+ assert.ok(results.length > 0, "the prefs were setup");
+
+ // unload dynamic prefs
+ loader.unload();
+ assert.pass('unload');
+
+ // hide and show the inline prefs
+ let { promise, resolve } = defer();
+ modelFor(getTabForId(tabId)).close(resolve);
+ yield promise;
+
+ // reopen the add-on prefs page
+ ({ tabId, document } = yield open(addon));
+
+ // confirm dynamic pref generation did not occur
+ ({ promise, resolve } = defer());
+ results = document.querySelectorAll("*[data-jetpack-id=\"" + id + "\"]");
+ assert.equal(0, results.length, "the prefs were not setup after unload");
+ modelFor(getTabForId(tabId)).close(resolve);
+ yield promise;
+
+ // uninstall the add-on
+ yield uninstall(id);
+
+ // delete the pref branch
+ branch.deleteBranch('');
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-simple-storage.js b/addon-sdk/source/test/test-simple-storage.js
new file mode 100644
index 000000000..2eb449bb6
--- /dev/null
+++ b/addon-sdk/source/test/test-simple-storage.js
@@ -0,0 +1,322 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const file = require("sdk/io/file");
+const prefs = require("sdk/preferences/service");
+
+const QUOTA_PREF = "extensions.addon-sdk.simple-storage.quota";
+
+var {Cc,Ci} = require("chrome");
+
+const { Loader } = require("sdk/test/loader");
+const { id } = require("sdk/self");
+
+var storeFile = Cc["@mozilla.org/file/directory_service;1"].
+ getService(Ci.nsIProperties).
+ get("ProfD", Ci.nsIFile);
+storeFile.append("jetpack");
+storeFile.append(id);
+storeFile.append("simple-storage");
+file.mkpath(storeFile.path);
+storeFile.append("store.json");
+var storeFilename = storeFile.path;
+
+function manager(loader) {
+ return loader.sandbox("sdk/simple-storage").manager;
+}
+
+exports.testSetGet = function (assert, done) {
+ // Load the module once, set a value.
+ let loader = Loader(module);
+ let ss = loader.require("sdk/simple-storage");
+ manager(loader).jsonStore.onWrite = function (storage) {
+ assert.ok(file.exists(storeFilename), "Store file should exist");
+
+ // Load the module again and make sure the value stuck.
+ loader = Loader(module);
+ ss = loader.require("sdk/simple-storage");
+ manager(loader).jsonStore.onWrite = function (storage) {
+ file.remove(storeFilename);
+ done();
+ };
+ assert.equal(ss.storage.foo, val, "Value should persist");
+ loader.unload();
+ };
+ let val = "foo";
+ ss.storage.foo = val;
+ assert.equal(ss.storage.foo, val, "Value read should be value set");
+ loader.unload();
+};
+
+exports.testSetGetRootArray = function (assert, done) {
+ setGetRoot(assert, done, [1, 2, 3], function (arr1, arr2) {
+ if (arr1.length !== arr2.length)
+ return false;
+ for (let i = 0; i < arr1.length; i++) {
+ if (arr1[i] !== arr2[i])
+ return false;
+ }
+ return true;
+ });
+};
+
+exports.testSetGetRootBool = function (assert, done) {
+ setGetRoot(assert, done, true);
+};
+
+exports.testSetGetRootFunction = function (assert, done) {
+ setGetRootError(assert, done, function () {},
+ "Setting storage to a function should fail");
+};
+
+exports.testSetGetRootNull = function (assert, done) {
+ setGetRoot(assert, done, null);
+};
+
+exports.testSetGetRootNumber = function (assert, done) {
+ setGetRoot(assert, done, 3.14);
+};
+
+exports.testSetGetRootObject = function (assert, done) {
+ setGetRoot(assert, done, { foo: 1, bar: 2 }, function (obj1, obj2) {
+ for (let prop in obj1) {
+ if (!(prop in obj2) || obj2[prop] !== obj1[prop])
+ return false;
+ }
+ for (let prop in obj2) {
+ if (!(prop in obj1) || obj1[prop] !== obj2[prop])
+ return false;
+ }
+ return true;
+ });
+};
+
+exports.testSetGetRootString = function (assert, done) {
+ setGetRoot(assert, done, "sho' 'nuff");
+};
+
+exports.testSetGetRootUndefined = function (assert, done) {
+ setGetRootError(assert, done, undefined, "Setting storage to undefined should fail");
+};
+
+exports.testEmpty = function (assert) {
+ let loader = Loader(module);
+ let ss = loader.require("sdk/simple-storage");
+ loader.unload();
+ assert.ok(!file.exists(storeFilename), "Store file should not exist");
+};
+
+exports.testStorageDataRecovery = function(assert) {
+ const data = {
+ a: true,
+ b: [3, 13],
+ c: "guilty!",
+ d: { e: 1, f: 2 }
+ };
+ let stream = file.open(storeFilename, "w");
+ stream.write(JSON.stringify(data));
+ stream.close();
+ let loader = Loader(module);
+ let ss = loader.require("sdk/simple-storage");
+ assert.deepEqual(ss.storage, data, "Recovered data should be the same as written");
+ file.remove(storeFilename);
+ loader.unload();
+}
+
+exports.testMalformed = function (assert) {
+ let stream = file.open(storeFilename, "w");
+ stream.write("i'm not json");
+ stream.close();
+ let loader = Loader(module);
+ let ss = loader.require("sdk/simple-storage");
+ let empty = true;
+ for (let key in ss.storage) {
+ empty = false;
+ break;
+ }
+ assert.ok(empty, "Malformed storage should cause root to be empty");
+ file.remove(storeFilename);
+ loader.unload();
+};
+
+// Go over quota and handle it by listener.
+exports.testQuotaExceededHandle = function (assert, done) {
+ prefs.set(QUOTA_PREF, 18);
+
+ let loader = Loader(module);
+ let ss = loader.require("sdk/simple-storage");
+ ss.on("OverQuota", function () {
+ assert.pass("OverQuota was emitted as expected");
+ assert.equal(this, ss, "`this` should be simple storage");
+ ss.storage = { x: 4, y: 5 };
+
+ manager(loader).jsonStore.onWrite = function () {
+ loader = Loader(module);
+ ss = loader.require("sdk/simple-storage");
+ let numProps = 0;
+ for (let prop in ss.storage)
+ numProps++;
+ assert.ok(numProps, 2,
+ "Store should contain 2 values: " + ss.storage.toSource());
+ assert.equal(ss.storage.x, 4, "x value should be correct");
+ assert.equal(ss.storage.y, 5, "y value should be correct");
+ manager(loader).jsonStore.onWrite = function (storage) {
+ prefs.reset(QUOTA_PREF);
+ done();
+ };
+ loader.unload();
+ };
+ loader.unload();
+ });
+ // This will be JSON.stringify()ed to: {"a":1,"b":2,"c":3} (19 bytes)
+ ss.storage = { a: 1, b: 2, c: 3 };
+ manager(loader).jsonStore.write();
+};
+
+// Go over quota but don't handle it. The last good state should still persist.
+exports.testQuotaExceededNoHandle = function (assert, done) {
+ prefs.set(QUOTA_PREF, 5);
+
+ let loader = Loader(module);
+ let ss = loader.require("sdk/simple-storage");
+
+ manager(loader).jsonStore.onWrite = function (storage) {
+ loader = Loader(module);
+ ss = loader.require("sdk/simple-storage");
+ assert.equal(ss.storage, val,
+ "Value should have persisted: " + ss.storage);
+ ss.storage = "some very long string that is very long";
+ ss.on("OverQuota", function () {
+ assert.pass("OverQuota emitted as expected");
+ manager(loader).jsonStore.onWrite = function () {
+ assert.fail("Over-quota value should not have been written");
+ };
+ loader.unload();
+
+ loader = Loader(module);
+ ss = loader.require("sdk/simple-storage");
+ assert.equal(ss.storage, val,
+ "Over-quota value should not have been written, " +
+ "old value should have persisted: " + ss.storage);
+ loader.unload();
+ prefs.reset(QUOTA_PREF);
+ done();
+ });
+ manager(loader).jsonStore.write();
+ };
+
+ let val = "foo";
+ ss.storage = val;
+ loader.unload();
+};
+
+exports.testQuotaUsage = function (assert, done) {
+ let quota = 21;
+ prefs.set(QUOTA_PREF, quota);
+
+ let loader = Loader(module);
+ let ss = loader.require("sdk/simple-storage");
+
+ // {"a":1} (7 bytes)
+ ss.storage = { a: 1 };
+ assert.equal(ss.quotaUsage, 7 / quota, "quotaUsage should be correct");
+
+ // {"a":1,"bb":2} (14 bytes)
+ ss.storage = { a: 1, bb: 2 };
+ assert.equal(ss.quotaUsage, 14 / quota, "quotaUsage should be correct");
+
+ // {"a":1,"bb":2,"cc":3} (21 bytes)
+ ss.storage = { a: 1, bb: 2, cc: 3 };
+ assert.equal(ss.quotaUsage, 21 / quota, "quotaUsage should be correct");
+
+ manager(loader).jsonStore.onWrite = function () {
+ prefs.reset(QUOTA_PREF);
+ done();
+ };
+ loader.unload();
+};
+
+exports.testUninstall = function (assert, done) {
+ let loader = Loader(module);
+ let ss = loader.require("sdk/simple-storage");
+ manager(loader).jsonStore.onWrite = function () {
+ assert.ok(file.exists(storeFilename), "Store file should exist");
+
+ loader = Loader(module);
+ ss = loader.require("sdk/simple-storage");
+ loader.unload("uninstall");
+ assert.ok(!file.exists(storeFilename), "Store file should be removed");
+ done();
+ };
+ ss.storage.foo = "foo";
+ loader.unload();
+};
+
+exports.testSetNoSetRead = function (assert, done) {
+ // Load the module, set a value.
+ let loader = Loader(module);
+ let ss = loader.require("sdk/simple-storage");
+ manager(loader).jsonStore.onWrite = function (storage) {
+ assert.ok(file.exists(storeFilename), "Store file should exist");
+
+ // Load the module again but don't access ss.storage.
+ loader = Loader(module);
+ ss = loader.require("sdk/simple-storage");
+ manager(loader).jsonStore.onWrite = function (storage) {
+ assert.fail("Nothing should be written since `storage` was not accessed.");
+ };
+ loader.unload();
+
+ // Load the module a third time and make sure the value stuck.
+ loader = Loader(module);
+ ss = loader.require("sdk/simple-storage");
+ manager(loader).jsonStore.onWrite = function (storage) {
+ file.remove(storeFilename);
+ done();
+ };
+ assert.equal(ss.storage.foo, val, "Value should persist");
+ loader.unload();
+ };
+ let val = "foo";
+ ss.storage.foo = val;
+ assert.equal(ss.storage.foo, val, "Value read should be value set");
+ loader.unload();
+};
+
+
+function setGetRoot(assert, done, val, compare) {
+ compare = compare || ((a, b) => a === b);
+
+ // Load the module once, set a value.
+ let loader = Loader(module);
+ let ss = loader.require("sdk/simple-storage");
+ manager(loader).jsonStore.onWrite = function () {
+ assert.ok(file.exists(storeFilename), "Store file should exist");
+
+ // Load the module again and make sure the value stuck.
+ loader = Loader(module);
+ ss = loader.require("sdk/simple-storage");
+ manager(loader).jsonStore.onWrite = function () {
+ file.remove(storeFilename);
+ done();
+ };
+ assert.ok(compare(ss.storage, val), "Value should persist");
+ loader.unload();
+ };
+ ss.storage = val;
+ assert.ok(compare(ss.storage, val), "Value read should be value set");
+ loader.unload();
+}
+
+function setGetRootError(assert, done, val, msg) {
+ let pred = new RegExp("storage must be one of the following types: " +
+ "array, boolean, null, number, object, string");
+ let loader = Loader(module);
+ let ss = loader.require("sdk/simple-storage");
+ assert.throws(() => ss.storage = val, pred, msg);
+ done();
+ loader.unload();
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-system-events.js b/addon-sdk/source/test/test-system-events.js
new file mode 100644
index 000000000..9b216ecc9
--- /dev/null
+++ b/addon-sdk/source/test/test-system-events.js
@@ -0,0 +1,278 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const events = require("sdk/system/events");
+const self = require("sdk/self");
+const { Cc, Ci, Cu } = require("chrome");
+const { setTimeout } = require("sdk/timers");
+const { Loader, LoaderWithHookedConsole2 } = require("sdk/test/loader");
+const nsIObserverService = Cc["@mozilla.org/observer-service;1"].
+ getService(Ci.nsIObserverService);
+
+var isConsoleEvent = (topic) =>
+ !!~["console-api-log-event", "console-storage-cache-event"].indexOf(topic)
+
+exports["test basic"] = function(assert) {
+ let type = Date.now().toString(32);
+
+ let timesCalled = 0;
+ function handler({subject, data}) { timesCalled++; };
+
+ events.on(type, handler);
+ events.emit(type, { data: "yo yo" });
+
+ assert.equal(timesCalled, 1, "event handler was called");
+
+ events.off(type, handler);
+ events.emit(type, { data: "no way" });
+
+ assert.equal(timesCalled, 1, "removed handler is no longer called");
+
+ events.once(type, handler);
+ events.emit(type, { data: "and we meet again" });
+ events.emit(type, { data: "it's always hard to say bye" });
+
+ assert.equal(timesCalled, 2, "handlers added via once are triggered once");
+}
+
+exports["test simple argument passing"] = function (assert) {
+ let type = Date.now().toString(32);
+
+ let lastArg;
+ function handler({data}) { lastArg = data; }
+ events.on(type, handler);
+
+ [true, false, 100, 0, 'a string', ''].forEach(arg => {
+ events.emit(type, arg);
+ assert.strictEqual(lastArg, arg + '',
+ 'event emitted for ' + arg + ' has correct data value');
+
+ events.emit(type, { data: arg });
+ assert.strictEqual(lastArg, arg + '',
+ 'event emitted for ' + arg + ' has correct data value when a property on an object');
+ });
+
+ [null, undefined, {}].forEach(arg => {
+ events.emit(type, arg);
+ assert.strictEqual(lastArg, null,
+ 'emitting ' + arg + ' gets null data');
+ });
+
+ events.off(type, handler);
+};
+
+exports["test error reporting"] = function(assert) {
+ let { loader, messages } = LoaderWithHookedConsole2(module);
+
+ let events = loader.require("sdk/system/events");
+ function brokenHandler(subject, data) { throw new Error("foo"); };
+
+ let lineNumber;
+ try { brokenHandler() } catch (error) { lineNumber = error.lineNumber }
+
+ let errorType = Date.now().toString(32);
+
+ events.on(errorType, brokenHandler);
+ events.emit(errorType, { data: "yo yo" });
+
+ assert.equal(messages.length, 2, "Got an exception");
+ assert.equal(messages[0], "console.error: " + self.name + ": \n",
+ "error is logged");
+ let text = messages[1];
+ assert.ok(text.indexOf("Error: foo") >= 0, "error message is logged");
+ assert.ok(text.indexOf(module.uri) >= 0, "module uri is logged");
+ assert.ok(text.indexOf(lineNumber) >= 0, "error line is logged");
+
+ events.off(errorType, brokenHandler);
+
+ loader.unload();
+};
+
+exports["test listeners are GC-ed"] = function(assert, done) {
+ let receivedFromWeak = [];
+ let receivedFromStrong = [];
+ let loader = Loader(module);
+ let events = loader.require('sdk/system/events');
+
+ let type = 'test-listeners-are-garbage-collected';
+ function handler(event) { receivedFromStrong.push(event); }
+ function weakHandler(event) { receivedFromWeak.push(event); }
+
+ events.on(type, handler, true);
+ events.on(type, weakHandler);
+
+ events.emit(type, { data: 1 });
+ assert.equal(receivedFromStrong.length, 1, "strong listener invoked");
+ assert.equal(receivedFromWeak.length, 1, "weak listener invoked");
+
+ handler = weakHandler = null;
+
+ Cu.schedulePreciseGC(function() {
+ events.emit(type, { data: 2 });
+
+ assert.equal(receivedFromWeak.length, 1, "weak listener was GC-ed");
+ assert.equal(receivedFromStrong.length, 2, "strong listener was invoked");
+
+ loader.unload();
+ done();
+ });
+};
+
+exports["test alive listeners are removed on unload"] = function(assert) {
+ let receivedFromWeak = [];
+ let receivedFromStrong = [];
+ let loader = Loader(module);
+ let events = loader.require('sdk/system/events');
+
+ let type = 'test-alive-listeners-are-removed';
+ const handler = (event) => receivedFromStrong.push(event);
+ const weakHandler = (event) => receivedFromWeak.push(event);
+
+ events.on(type, handler, true);
+ events.on(type, weakHandler);
+
+ events.emit(type, { data: 1 });
+ assert.equal(receivedFromStrong.length, 1, "strong listener invoked");
+ assert.equal(receivedFromWeak.length, 1, "weak listener invoked");
+
+ loader.unload();
+ events.emit(type, { data: 2 });
+
+ assert.equal(receivedFromWeak.length, 1, "weak listener was removed");
+ assert.equal(receivedFromStrong.length, 1, "strong listener was removed");
+};
+
+exports["test handle nsIObserverService notifications"] = function(assert) {
+ let ios = Cc['@mozilla.org/network/io-service;1']
+ .getService(Ci.nsIIOService);
+
+ let uri = ios.newURI("http://www.foo.com", null, null);
+
+ let type = Date.now().toString(32);
+ let timesCalled = 0;
+ let lastSubject = null;
+ let lastData = null;
+ let lastType = null;
+
+ function handler({ subject, data, type }) {
+ // Ignores internal console events
+ if (isConsoleEvent(type))
+ return;
+ timesCalled++;
+ lastSubject = subject;
+ lastData = data;
+ lastType = type;
+ };
+
+ events.on(type, handler);
+ nsIObserverService.notifyObservers(uri, type, "some data");
+
+ assert.equal(timesCalled, 1, "notification invokes handler");
+ assert.equal(lastType, type, "event.type is notification topic");
+ assert.equal(lastSubject, uri, "event.subject is notification subject");
+ assert.equal(lastData, "some data", "event.data is notification data");
+
+ function customSubject() {}
+ function customData() {}
+
+ events.emit(type, { data: customData, subject: customSubject });
+
+ assert.equal(timesCalled, 2, "notification invokes handler");
+ assert.equal(lastType, type, "event.type is notification topic");
+ assert.equal(lastSubject, customSubject,
+ "event.subject is wrapped & unwrapped");
+ assert.equal(lastData, customData, "event.data is wrapped & unwrapped");
+
+ events.off(type, handler);
+
+ nsIObserverService.notifyObservers(null, type, "some data");
+
+ assert.equal(timesCalled, 2, "event handler is removed");
+
+ events.on("*", handler);
+
+ nsIObserverService.notifyObservers(null, type, "more data");
+
+ assert.equal(timesCalled, 3, "notification invokes * handler");
+ assert.equal(lastType, type, "event.type is notification topic");
+ assert.equal(lastSubject, null,
+ "event.subject is notification subject");
+ assert.equal(lastData, "more data", "event.data is notification data");
+
+ events.off("*", handler);
+
+ nsIObserverService.notifyObservers(null, type, "last data");
+
+ assert.equal(timesCalled, 3, "* event handler is removed");
+};
+
+exports["test emit to nsIObserverService observers"] = function(assert) {
+ let ios = Cc['@mozilla.org/network/io-service;1']
+ .getService(Ci.nsIIOService);
+
+ let uri = ios.newURI("http://www.foo.com", null, null);
+ let timesCalled = 0;
+ let lastSubject = null;
+ let lastData = null;
+ let lastTopic = null;
+
+ var topic = Date.now().toString(32)
+ let nsIObserver = {
+ QueryInterface: function() {
+ return nsIObserver;
+ },
+ observe: function(subject, topic, data) {
+ // Ignores internal console events
+ if (isConsoleEvent(topic))
+ return;
+ timesCalled = timesCalled + 1;
+ lastSubject = subject;
+ lastData = data;
+ lastTopic = topic;
+ }
+ };
+
+ nsIObserverService.addObserver(nsIObserver, topic, false);
+
+ events.emit(topic, { subject: uri, data: "some data" });
+
+ assert.equal(timesCalled, 1, "emit notifies observers");
+ assert.equal(lastTopic, topic, "event type is notification topic");
+ assert.equal(lastSubject.wrappedJSObject.object, uri,
+ "event.subject is notification subject");
+ assert.equal(lastData, "some data", "event.data is notification data");
+ function customSubject() {}
+ function customData() {}
+ events.emit(topic, { subject: customSubject, data: customData });
+
+ assert.equal(timesCalled, 2, "emit notifies observers");
+ assert.equal(lastTopic, topic, "event.type is notification");
+ assert.equal(lastSubject.wrappedJSObject.object, customSubject,
+ "event.subject is notification subject");
+ assert.equal(lastData, customData, "event.data is notification data");
+
+ nsIObserverService.removeObserver(nsIObserver, topic);
+
+ events.emit(topic, { data: "more data" });
+
+ assert.equal(timesCalled, 2, "removed observers no longer invoked");
+
+ nsIObserverService.addObserver(nsIObserver, "*", false);
+
+ events.emit(topic, { data: "data again" });
+
+ assert.equal(timesCalled, 3, "emit notifies * observers");
+
+ assert.equal(lastTopic, topic, "event.type is notification");
+ assert.equal(lastSubject, null,
+ "event.subject is notification subject");
+ assert.equal(lastData, "data again", "event.data is notification data");
+
+ nsIObserverService.removeObserver(nsIObserver, "*");
+
+ events.emit(topic, { data: "last data" });
+ assert.equal(timesCalled, 3, "removed observers no longer invoked");
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-system-input-output.js b/addon-sdk/source/test/test-system-input-output.js
new file mode 100644
index 000000000..9a4d6ebb9
--- /dev/null
+++ b/addon-sdk/source/test/test-system-input-output.js
@@ -0,0 +1,319 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+
+const { id: addonID, name: addonName } = require("sdk/self");
+const { Cc, Ci, Cu } = require("chrome");
+const { Loader, LoaderWithHookedConsole2 } = require("sdk/test/loader");
+const { InputPort } = require("sdk/input/system");
+const { OutputPort } = require("sdk/output/system");
+
+const { removeObserver, addObserver,
+ notifyObservers } = Cc["@mozilla.org/observer-service;1"].
+ getService(Ci.nsIObserverService);
+
+const { lift, start, stop, send } = require("sdk/event/utils");
+
+const isConsoleEvent = topic =>
+ ["console-api-log-event",
+ "console-storage-cache-event"].indexOf(topic) >= 0;
+
+const message = x => ({wrappedJSObject: {data: x}});
+
+exports["test start / stop ports"] = assert => {
+ const input = new InputPort({ id: Date.now().toString(32), initial: {data:0} });
+ const topic = input.topic;
+
+ assert.ok(topic.includes(addonID), "topics are namespaced to add-on");
+
+ const xs = lift(({data}) => "x:" + data, input);
+ const ys = lift(({data}) => "y:" + data, input);
+
+ assert.deepEqual(input.value, {data:0}, "initila value is set");
+ assert.deepEqual(xs.value, "x:0", "initial value is mapped");
+ assert.deepEqual(ys.value, "y:0", "initial value is mapped");
+
+ notifyObservers(message(1), topic, null);
+
+ assert.deepEqual(input.value, {data:0}, "no message received on input port");
+ assert.deepEqual(xs.value, "x:0", "no message received on xs");
+ assert.deepEqual(ys.value, "y:0", "no message received on ys");
+
+ start(xs);
+
+
+ notifyObservers(message(2), topic, null);
+
+ assert.deepEqual(input.value, {data:2}, "message received on input port");
+ assert.deepEqual(xs.value, "x:2", "message received on xs");
+ assert.deepEqual(ys.value, "y:2", "no message received on (not started) ys");
+
+
+ notifyObservers(message(3), topic, null);
+
+
+ assert.deepEqual(input.value, {data:3}, "message received on input port");
+ assert.deepEqual(xs.value, "x:3", "message received on xs");
+ assert.deepEqual(ys.value, "y:3", "message received on ys");
+
+
+ notifyObservers(message(4), topic, null);
+
+ assert.deepEqual(input.value, {data:4}, "message received on input port");
+ assert.deepEqual(xs.value, "x:4", "message not received on (stopped) xs");
+ assert.deepEqual(ys.value, "y:4", "message received on ys");
+
+
+ stop(input);
+
+ notifyObservers(message(5), topic, null);
+
+ assert.deepEqual(input.value, {data:4}, "message note received on input port");
+ assert.deepEqual(xs.value, "x:4", "message not received on (stopped) xs");
+ assert.deepEqual(ys.value, "y:4", "message not received on (stopped) ys");
+};
+
+exports["test send messages to nsIObserverService"] = assert => {
+ let messages = [];
+
+ const { newURI } = Cc['@mozilla.org/network/io-service;1'].
+ getService(Ci.nsIIOService);
+
+ const output = new OutputPort({ id: Date.now().toString(32), sync: true });
+ const topic = output.topic;
+
+ const observer = {
+ QueryInterface: function() {
+ return this;
+ },
+ observe: (subject, topic, data) => {
+ // Ignores internal console events
+ if (!isConsoleEvent(topic)) {
+ messages.push({
+ topic: topic,
+ subject: subject
+ });
+ }
+ }
+ };
+
+ addObserver(observer, topic, false);
+
+ send(output, null);
+ assert.deepEqual(messages.shift(), { topic: topic, subject: null },
+ "null message received");
+
+
+ const uri = newURI("http://www.foo.com", null, null);
+ send(output, uri);
+
+ assert.deepEqual(messages.shift(), { topic: topic, subject: uri },
+ "message received");
+
+
+ function customSubject() {}
+ send(output, customSubject);
+
+ let message = messages.shift();
+ assert.equal(message.topic, topic, "topic was received");
+ assert.equal(message.subject.wrappedJSObject, customSubject,
+ "custom subject is received");
+
+ removeObserver(observer, topic);
+
+ send(output, { data: "more data" });
+
+ assert.deepEqual(messages, [],
+ "no more data received");
+
+ addObserver(observer, "*", false);
+
+ send(output, { data: "data again" });
+
+ message = messages.shift();
+ assert.equal(message.topic, topic, "topic was received");
+ assert.deepEqual(message.subject.wrappedJSObject,
+ { data: "data again" },
+ "wrapped message received");
+
+ removeObserver(observer, "*");
+
+ send(output, { data: "last data" });
+ assert.deepEqual(messages, [],
+ "no more data received");
+
+ assert.throws(() => send(output, "hi"),
+ /Unsupproted message type: `string`/,
+ "strings can't be send");
+
+ assert.throws(() => send(output, 4),
+ /Unsupproted message type: `number`/,
+ "numbers can't be send");
+
+ assert.throws(() => send(output, void(0)),
+ /Unsupproted message type: `undefined`/,
+ "undefineds can't be send");
+
+ assert.throws(() => send(output, true),
+ /Unsupproted message type: `boolean`/,
+ "booleans can't be send");
+};
+
+exports["test async OutputPort"] = (assert, done) => {
+ let async = false;
+ const output = new OutputPort({ id: Date.now().toString(32) });
+ const observer = {
+ observe: (subject, topic, data) => {
+ removeObserver(observer, topic);
+ assert.equal(topic, output.topic, "correct topic");
+ assert.deepEqual(subject.wrappedJSObject, {foo: "bar"}, "message received");
+ assert.ok(async, "message received async");
+ done();
+ }
+ };
+ addObserver(observer, output.topic, false);
+ send(output, {foo: "bar"});
+
+ assert.throws(() => send(output, "boom"), "can only send object");
+ async = true;
+};
+
+exports["test explicit output topic"] = (assert, done) => {
+ const topic = Date.now().toString(32);
+ const output = new OutputPort({ topic: topic });
+ const observer = {
+ observe: (subject, topic, data) => {
+ removeObserver(observer, topic);
+ assert.deepEqual(subject.wrappedJSObject, {foo: "bar"}, "message received");
+ done();
+ }
+ };
+
+ assert.equal(output.topic, topic, "given topic is used");
+
+ addObserver(observer, topic, false);
+ send(output, {foo: "bar"});
+};
+
+exports["test explicit input topic"] = (assert) => {
+ const topic = Date.now().toString(32);
+ const input = new InputPort({ topic: topic });
+
+ start(input);
+ assert.equal(input.topic, topic, "given topic is used");
+
+
+ notifyObservers({wrappedJSObject: {foo: "bar"}}, topic, null);
+
+ assert.deepEqual(input.value, {foo: "bar"}, "message received");
+};
+
+
+exports["test receive what was send"] = assert => {
+ const id = Date.now().toString(32);
+ const input = new InputPort({ id: id, initial: 0});
+ const output = new OutputPort({ id: id, sync: true });
+
+ assert.ok(input.topic.includes(addonID),
+ "input topic is namespaced to addon");
+ assert.equal(input.topic, output.topic,
+ "input & output get same topics from id.");
+
+ start(input);
+
+ assert.equal(input.value, 0, "initial value is set");
+
+ send(output, { data: 1 });
+ assert.deepEqual(input.value, {data: 1}, "message unwrapped");
+
+ send(output, []);
+ assert.deepEqual(input.value, [], "array message unwrapped");
+
+ send(output, null);
+ assert.deepEqual(input.value, null, "null message received");
+
+ send(output, new String("message"));
+ assert.deepEqual(input.value, new String("message"),
+ "string instance received");
+
+ send(output, /pattern/);
+ assert.deepEqual(input.value, /pattern/, "regexp received");
+
+ assert.throws(() => send(output, "hi"),
+ /Unsupproted message type: `string`/,
+ "strings can't be send");
+
+ assert.throws(() => send(output, 4),
+ /Unsupproted message type: `number`/,
+ "numbers can't be send");
+
+ assert.throws(() => send(output, void(0)),
+ /Unsupproted message type: `undefined`/,
+ "undefineds can't be send");
+
+ assert.throws(() => send(output, true),
+ /Unsupproted message type: `boolean`/,
+ "booleans can't be send");
+
+ stop(input);
+};
+
+
+exports["-test error reporting"] = function(assert) {
+ let { loader, messages } = LoaderWithHookedConsole2(module);
+ const { start, stop, lift } = loader.require("sdk/event/utils");
+ const { InputPort } = loader.require("sdk/input/system");
+ const { OutputPort } = loader.require("sdk/output/system");
+ const id = "error:" + Date.now().toString(32);
+
+ const raise = x => { if (x) throw new Error("foo"); };
+
+ const input = new InputPort({ id: id });
+ const output = new OutputPort({ id: id, sync: true });
+ const xs = lift(raise, input);
+
+ assert.equal(input.value, null, "initial inherited");
+
+ send(output, { data: "yo yo" });
+
+ assert.deepEqual(messages, [], "nothing happend yet");
+
+ start(xs);
+
+ send(output, { data: "first" });
+
+ assert.equal(messages.length, 4, "Got an exception");
+
+
+ assert.equal(messages[0], "console.error: " + addonName + ": \n",
+ "error is logged");
+
+ assert.ok(/Unhandled error/.test(messages[1]),
+ "expected error message");
+
+ loader.unload();
+};
+
+exports["test unload ends input port"] = assert => {
+ const loader = Loader(module);
+ const { start, stop, lift } = loader.require("sdk/event/utils");
+ const { InputPort } = loader.require("sdk/input/system");
+
+ const id = "unload!" + Date.now().toString(32);
+ const input = new InputPort({ id: id });
+
+ start(input);
+ notifyObservers(message(1), input.topic, null);
+ assert.deepEqual(input.value, {data: 1}, "message received");
+
+ notifyObservers(message(2), input.topic, null);
+ assert.deepEqual(input.value, {data: 2}, "message received");
+
+ loader.unload();
+ notifyObservers(message(3), input.topic, null);
+ assert.deepEqual(input.value, {data: 2}, "message wasn't received");
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-system-runtime.js b/addon-sdk/source/test/test-system-runtime.js
new file mode 100644
index 000000000..f841777c7
--- /dev/null
+++ b/addon-sdk/source/test/test-system-runtime.js
@@ -0,0 +1,25 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const runtime = require("sdk/system/runtime");
+
+exports["test system runtime"] = function(assert) {
+ assert.equal(typeof(runtime.inSafeMode), "boolean",
+ "inSafeMode is boolean");
+ assert.equal(typeof(runtime.OS), "string",
+ "runtime.OS is string");
+ assert.equal(typeof(runtime.processType), "number",
+ "runtime.processType is a number");
+ assert.equal(typeof(runtime.processID), "number",
+ "runtime.processID is a number");
+ assert.equal(typeof(runtime.widgetToolkit), "string",
+ "runtime.widgetToolkit is string");
+ const XPCOMABI = runtime.XPCOMABI;
+ assert.ok(XPCOMABI === null || typeof(XPCOMABI) === "string",
+ "runtime.XPCOMABI is string or null if not supported by platform");
+};
+
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-system-startup.js b/addon-sdk/source/test/test-system-startup.js
new file mode 100644
index 000000000..e7a4487f4
--- /dev/null
+++ b/addon-sdk/source/test/test-system-startup.js
@@ -0,0 +1,19 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Cu } = require("chrome");
+const Startup = Cu.import("resource://gre/modules/sdk/system/Startup.js", {}).exports;
+
+exports["test startup initialized"] = function(assert) {
+ assert.ok(Startup.initialized, "Startup.initialized is true");
+}
+
+exports["test startup onceInitialized"] = function*(assert) {
+ yield Startup.onceInitialized.then(() => {
+ assert.pass("onceInitialized promise was resolved");
+ }).catch(assert.fail);
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-system.js b/addon-sdk/source/test/test-system.js
new file mode 100644
index 000000000..8fe86725a
--- /dev/null
+++ b/addon-sdk/source/test/test-system.js
@@ -0,0 +1,37 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const runtime = require("sdk/system/runtime");
+const system = require("sdk/system");
+
+exports["test system architecture and compiler"] = function(assert) {
+
+ if (system.architecture !== null) {
+ assert.equal(
+ runtime.XPCOMABI.indexOf(system.architecture), 0,
+ "system.architecture is starting substring of runtime.XPCOMABI"
+ );
+ }
+
+ if (system.compiler !== null) {
+ assert.equal(
+ runtime.XPCOMABI.indexOf(system.compiler),
+ runtime.XPCOMABI.length - system.compiler.length,
+ "system.compiler is trailing substring of runtime.XPCOMABI"
+ );
+ }
+
+ assert.ok(
+ system.architecture === null || typeof(system.architecture) === "string",
+ "system.architecture is string or null if not supported by platform"
+ );
+
+ assert.ok(
+ system.compiler === null || typeof(system.compiler) === "string",
+ "system.compiler is string or null if not supported by platform"
+ );
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-tab-events.js b/addon-sdk/source/test/test-tab-events.js
new file mode 100644
index 000000000..32089fff8
--- /dev/null
+++ b/addon-sdk/source/test/test-tab-events.js
@@ -0,0 +1,238 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const { Loader } = require("sdk/test/loader");
+const utils = require("sdk/tabs/utils");
+const { open, close } = require("sdk/window/helpers");
+const { getMostRecentBrowserWindow } = require("sdk/window/utils");
+const { events } = require("sdk/tab/events");
+const { on, off } = require("sdk/event/core");
+const { resolve, defer } = require("sdk/core/promise");
+
+var isFennec = require("sdk/system/xul-app").is("Fennec");
+
+function test(options) {
+ return function(assert, done) {
+ let tabEvents = [];
+ let tabs = [];
+ let { promise, resolve: resolveP } = defer();
+ let win = isFennec ? resolve(getMostRecentBrowserWindow()) :
+ open(null, {
+ features: { private: true, toolbar:true, chrome: true }
+ });
+ let window = null;
+
+ // Firefox events are fired sync; Fennec events async
+ // this normalizes the tests
+ function handler (event) {
+ tabEvents.push(event);
+ runIfReady();
+ }
+
+ function runIfReady () {
+ let releventEvents = getRelatedEvents(tabEvents, tabs);
+ if (options.readyWhen(releventEvents))
+ options.end({
+ tabs: tabs,
+ events: releventEvents,
+ assert: assert,
+ done: resolveP
+ });
+ }
+
+ win.then(function(w) {
+ window = w;
+ on(events, "data", handler);
+ options.start({ tabs: tabs, window: window });
+
+ // Execute here for synchronous FF events, as the handlers
+ // were called before tabs were pushed to `tabs`
+ runIfReady();
+ return promise;
+ }).then(function() {
+ off(events, "data", handler);
+ return isFennec ? null : close(window);
+ }).then(done, assert.fail);
+ };
+}
+
+// Just making sure that tab events work for already opened tabs not only
+// for new windows.
+exports["test current window"] = test({
+ readyWhen: events => events.length === 3,
+ start: ({ tabs, window }) => {
+ let tab = utils.openTab(window, 'data:text/plain,open');
+ tabs.push(tab);
+ utils.closeTab(tab);
+ },
+ end: ({ tabs, events, assert, done }) => {
+ let [open, select, close] = events;
+ let tab = tabs[0];
+
+ assert.equal(open.type, "TabOpen");
+ assert.equal(open.target, tab);
+
+ assert.equal(select.type, "TabSelect");
+ assert.equal(select.target, tab);
+
+ assert.equal(close.type, "TabClose");
+ assert.equal(close.target, tab);
+ done();
+ }
+});
+
+exports["test open"] = test({
+ readyWhen: events => events.length === 2,
+ start: ({ tabs, window }) => {
+ tabs.push(utils.openTab(window, 'data:text/plain,open'));
+ },
+ end: ({ tabs, events, assert, done }) => {
+ let [open, select] = events;
+ let tab = tabs[0];
+
+ assert.equal(open.type, "TabOpen");
+ assert.equal(open.target, tab);
+
+ assert.equal(select.type, "TabSelect");
+ assert.equal(select.target, tab);
+ done();
+ }
+});
+
+exports["test open -> close"] = test({
+ readyWhen: events => events.length === 3,
+ start: ({ tabs, window }) => {
+ // First tab is useless we just open it so that closing second tab won't
+ // close window on some platforms.
+ utils.openTab(window, 'data:text/plain,ignore');
+ let tab = utils.openTab(window, 'data:text/plain,open-close');
+ tabs.push(tab);
+ utils.closeTab(tab);
+ },
+ end: ({ tabs, events, assert, done }) => {
+ let [open, select, close] = events;
+ let tab = tabs[0];
+
+ assert.equal(open.type, "TabOpen");
+ assert.equal(open.target, tab);
+
+ assert.equal(select.type, "TabSelect");
+ assert.equal(select.target, tab);
+
+ assert.equal(close.type, "TabClose");
+ assert.equal(close.target, tab);
+ done();
+ }
+});
+
+exports["test open -> open -> select"] = test({
+ readyWhen: events => events.length === 5,
+ start: ({tabs, window}) => {
+ tabs.push(utils.openTab(window, 'data:text/plain,Tab-1'));
+ tabs.push(utils.openTab(window, 'data:text/plain,Tab-2'));
+ utils.activateTab(tabs[0], window);
+ },
+ end: ({ tabs, events, assert, done }) => {
+ let [ tab1, tab2 ] = tabs;
+ let tab1Events = 0;
+ getRelatedEvents(events, tab1).map(event => {
+ tab1Events++;
+ if (tab1Events === 1)
+ assert.equal(event.type, "TabOpen", "first tab opened");
+ else
+ assert.equal(event.type, "TabSelect", "first tab selected");
+ assert.equal(event.target, tab1);
+ });
+ assert.equal(tab1Events, 3, "first tab has 3 events");
+
+ let tab2Opened;
+ getRelatedEvents(events, tab2).map(event => {
+ if (!tab2Opened)
+ assert.equal(event.type, "TabOpen", "second tab opened");
+ else
+ assert.equal(event.type, "TabSelect", "second tab selected");
+ tab2Opened = true;
+ assert.equal(event.target, tab2);
+ });
+ done();
+ }
+});
+
+exports["test open -> pin -> unpin"] = test({
+ readyWhen: events => events.length === (isFennec ? 2 : 5),
+ start: ({ tabs, window }) => {
+ tabs.push(utils.openTab(window, 'data:text/plain,pin-unpin'));
+ utils.pin(tabs[0]);
+ utils.unpin(tabs[0]);
+ },
+ end: ({ tabs, events, assert, done }) => {
+ let [open, select, move, pin, unpin] = events;
+ let tab = tabs[0];
+
+ assert.equal(open.type, "TabOpen");
+ assert.equal(open.target, tab);
+
+ assert.equal(select.type, "TabSelect");
+ assert.equal(select.target, tab);
+
+ if (isFennec) {
+ assert.pass("Tab pin / unpin is not supported by Fennec");
+ }
+ else {
+ assert.equal(move.type, "TabMove");
+ assert.equal(move.target, tab);
+
+ assert.equal(pin.type, "TabPinned");
+ assert.equal(pin.target, tab);
+
+ assert.equal(unpin.type, "TabUnpinned");
+ assert.equal(unpin.target, tab);
+ }
+ done();
+ }
+});
+
+exports["test open -> open -> move "] = test({
+ readyWhen: events => events.length === (isFennec ? 4 : 5),
+ start: ({tabs, window}) => {
+ tabs.push(utils.openTab(window, 'data:text/plain,Tab-1'));
+ tabs.push(utils.openTab(window, 'data:text/plain,Tab-2'));
+ utils.move(tabs[0], 2);
+ },
+ end: ({ tabs, events, assert, done }) => {
+ let [ tab1, tab2 ] = tabs;
+ let tab1Events = 0;
+ getRelatedEvents(events, tab1).map(event => {
+ tab1Events++;
+ if (tab1Events === 1)
+ assert.equal(event.type, "TabOpen", "first tab opened");
+ else if (tab1Events === 2)
+ assert.equal(event.type, "TabSelect", "first tab selected");
+ else if (tab1Events === 3 && isFennec)
+ assert.equal(event.type, "TabMove", "first tab moved");
+ assert.equal(event.target, tab1);
+ });
+ assert.equal(tab1Events, isFennec ? 2 : 3,
+ "correct number of events for first tab");
+
+ let tab2Events = 0;
+ getRelatedEvents(events, tab2).map(event => {
+ tab2Events++;
+ if (tab2Events === 1)
+ assert.equal(event.type, "TabOpen", "second tab opened");
+ else
+ assert.equal(event.type, "TabSelect", "second tab selected");
+ assert.equal(event.target, tab2);
+ });
+ done();
+ }
+});
+
+function getRelatedEvents (events, tabs) {
+ return events.filter(({target}) => ~([].concat(tabs)).indexOf(target));
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-tab-observer.js b/addon-sdk/source/test/test-tab-observer.js
new file mode 100644
index 000000000..49f0e3957
--- /dev/null
+++ b/addon-sdk/source/test/test-tab-observer.js
@@ -0,0 +1,45 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+// TODO Fennec support in Bug #894525
+module.metadata = {
+ "engines": {
+ "Firefox": "*"
+ }
+}
+
+const { openTab, closeTab } = require("sdk/tabs/utils");
+const { Loader } = require("sdk/test/loader");
+const { setTimeout } = require("sdk/timers");
+
+exports["test unload tab observer"] = function(assert, done) {
+ let loader = Loader(module);
+
+ let window = loader.require("sdk/deprecated/window-utils").activeBrowserWindow;
+ let observer = loader.require("sdk/tabs/observer").observer;
+ let opened = 0;
+ let closed = 0;
+
+ observer.on("open", function onOpen(window) { opened++; });
+ observer.on("close", function onClose(window) { closed++; });
+
+ // Open and close tab to trigger observers.
+ closeTab(openTab(window, "data:text/html;charset=utf-8,tab-1"));
+
+ // Unload the module so that all listeners set by observer are removed.
+ loader.unload();
+
+ // Open and close tab once again.
+ closeTab(openTab(window, "data:text/html;charset=utf-8,tab-2"));
+
+ // Enqueuing asserts to make sure that assertion is not performed early.
+ setTimeout(function () {
+ assert.equal(1, opened, "observer open was called before unload only");
+ assert.equal(1, closed, "observer close was called before unload only");
+ done();
+ }, 0);
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-tab-utils.js b/addon-sdk/source/test/test-tab-utils.js
new file mode 100644
index 000000000..1b631a105
--- /dev/null
+++ b/addon-sdk/source/test/test-tab-utils.js
@@ -0,0 +1,69 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { getTabs } = require('sdk/tabs/utils');
+const { isWindowPBSupported, isTabPBSupported } = require('sdk/private-browsing/utils');
+const { browserWindows } = require('sdk/windows');
+const tabs = require('sdk/tabs');
+const { isPrivate } = require('sdk/private-browsing');
+const { openTab, closeTab, getTabContentWindow, getOwnerWindow } = require('sdk/tabs/utils');
+const { open, close } = require('sdk/window/helpers');
+const { windows } = require('sdk/window/utils');
+const { getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { fromIterator } = require('sdk/util/array');
+
+if (isWindowPBSupported) {
+ exports.testGetTabs = function(assert, done) {
+ let tabCount = getTabs().length;
+ let windowCount = browserWindows.length;
+
+ open(null, {
+ features: {
+ private: true,
+ toolbar: true,
+ chrome: true
+ }
+ }).then(function(window) {
+ assert.ok(isPrivate(window), 'new tab is private');
+
+ assert.equal(getTabs().length, tabCount, 'there are no new tabs found');
+ getTabs().forEach(function(tab) {
+ assert.equal(isPrivate(tab), false, 'all found tabs are not private');
+ assert.equal(isPrivate(getOwnerWindow(tab)), false, 'all found tabs are not private');
+ assert.equal(isPrivate(getTabContentWindow(tab)), false, 'all found tabs are not private');
+ });
+
+ assert.equal(browserWindows.length, windowCount, 'there are no new windows found');
+ fromIterator(browserWindows).forEach(function(window) {
+ assert.equal(isPrivate(window), false, 'all found windows are not private');
+ });
+
+ assert.equal(windows(null, {includePrivate: true}).length, 2, 'there are really two windows');
+
+ close(window).then(done);
+ });
+ };
+}
+else if (isTabPBSupported) {
+ exports.testGetTabs = function(assert, done) {
+ let startTabCount = getTabs().length;
+ let tab = openTab(getMostRecentBrowserWindow(), 'about:blank', {
+ isPrivate: true
+ });
+
+ assert.ok(isPrivate(getTabContentWindow(tab)), 'new tab is private');
+ let utils_tabs = getTabs();
+ assert.equal(utils_tabs.length, startTabCount + 1,
+ 'there are two tabs found');
+ assert.equal(utils_tabs[utils_tabs.length-1], tab,
+ 'the last tab is the opened tab');
+ assert.equal(browserWindows.length, 1, 'there is only one window');
+ closeTab(tab);
+
+ done();
+ };
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-tab.js b/addon-sdk/source/test/test-tab.js
new file mode 100644
index 000000000..1bf4ef20f
--- /dev/null
+++ b/addon-sdk/source/test/test-tab.js
@@ -0,0 +1,228 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const tabs = require("sdk/tabs");
+const windowUtils = require("sdk/deprecated/window-utils");
+const windows = require("sdk/windows").browserWindows;
+const app = require("sdk/system/xul-app");
+const { viewFor } = require("sdk/view/core");
+const { modelFor } = require("sdk/model/core");
+const { getBrowserForTab, getTabId, isTab } = require("sdk/tabs/utils");
+const { defer } = require("sdk/lang/functional");
+
+function tabExistenceInTabs(assert, found, tab, tabs) {
+ let tabFound = false;
+
+ for (let t of tabs) {
+ assert.notEqual(t.title, undefined, 'tab title is not undefined');
+ assert.notEqual(t.url, undefined, 'tab url is not undefined');
+ assert.notEqual(t.index, undefined, 'tab index is not undefined');
+
+ if (t === tab) {
+ tabFound = true;
+ break;
+ }
+ }
+
+ // check for the tab's existence
+ if (found)
+ assert.ok(tabFound, 'the tab was found as expected');
+ else
+ assert.ok(!tabFound, 'the tab was not found as expected');
+}
+
+exports.testBehaviorOnCloseAfterReady = function(assert, done) {
+ tabs.open({
+ url: "about:mozilla",
+ onReady: function(tab) {
+ assert.equal(tab.url, "about:mozilla", "Tab has the expected url");
+ // if another test ends before closing a tab then index != 1 here
+ assert.ok(tab.index >= 1, "Tab has the expected index, a value greater than 0");
+
+ let testTabFound = tabExistenceInTabs.bind(null, assert, true, tab);
+ let testTabNotFound = tabExistenceInTabs.bind(null, assert, false, tab);
+
+ // tab in require("sdk/tabs") ?
+ testTabFound(tabs);
+ // tab was found in require("sdk/windows").windows.activeWindow.tabs ?
+ let activeWindowTabs = windows.activeWindow.tabs;
+ testTabFound(activeWindowTabs);
+
+ tab.close(function () {
+ assert.equal(tab.url, undefined,
+ "After being closed, tab attributes are undefined (url)");
+ assert.equal(tab.index, undefined,
+ "After being closed, tab attributes are undefined (index)");
+
+ tab.destroy();
+
+ testTabNotFound(tabs);
+ testTabNotFound(windows.activeWindow.tabs);
+ testTabNotFound(activeWindowTabs);
+
+ // Ensure that we can call destroy multiple times without throwing
+ tab.destroy();
+
+ testTabNotFound(tabs);
+ testTabNotFound(windows.activeWindow.tabs);
+ testTabNotFound(activeWindowTabs);
+
+ done();
+ });
+ }
+ });
+};
+
+exports.testBug844492 = function(assert, done) {
+ const activeWindowTabs = windows.activeWindow.tabs;
+ let openedTabs = 0;
+
+ tabs.on('open', function onOpenTab(tab) {
+ openedTabs++;
+
+ let testTabFound = tabExistenceInTabs.bind(null, assert, true, tab);
+ let testTabNotFound = tabExistenceInTabs.bind(null, assert, false, tab);
+
+ testTabFound(tabs);
+ testTabFound(windows.activeWindow.tabs);
+ testTabFound(activeWindowTabs);
+
+ tab.close();
+
+ testTabNotFound(tabs);
+ testTabNotFound(windows.activeWindow.tabs);
+ testTabNotFound(activeWindowTabs);
+
+ if (openedTabs >= 2) {
+ tabs.removeListener('open', onOpenTab);
+ done();
+ }
+ });
+
+ tabs.open({ url: 'about:mozilla' });
+ tabs.open({ url: 'about:mozilla' });
+};
+
+exports.testBehaviorOnCloseAfterOpen = function(assert, done) {
+ tabs.open({
+ url: "about:mozilla",
+ onOpen: function(tab) {
+ assert.notEqual(tab.url, undefined, "Tab has a url");
+ assert.ok(tab.index >= 1, "Tab has the expected index");
+
+ let testTabFound = tabExistenceInTabs.bind(null, assert, true, tab);
+ let testTabNotFound = tabExistenceInTabs.bind(null, assert, false, tab);
+
+ // tab in require("sdk/tabs") ?
+ testTabFound(tabs);
+ // tab was found in require("sdk/windows").windows.activeWindow.tabs ?
+ let activeWindowTabs = windows.activeWindow.tabs;
+ testTabFound(activeWindowTabs);
+
+ tab.close(function () {
+ assert.equal(tab.url, undefined,
+ "After being closed, tab attributes are undefined (url)");
+ assert.equal(tab.index, undefined,
+ "After being closed, tab attributes are undefined (index)");
+
+ tab.destroy();
+
+ if (app.is("Firefox")) {
+ // Ensure that we can call destroy multiple times without throwing;
+ // Fennec doesn't use this internal utility
+ tab.destroy();
+ tab.destroy();
+ }
+
+ testTabNotFound(tabs);
+ testTabNotFound(windows.activeWindow.tabs);
+ testTabNotFound(activeWindowTabs);
+
+ // Ensure that we can call destroy multiple times without throwing
+ tab.destroy();
+
+ testTabNotFound(tabs);
+ testTabNotFound(windows.activeWindow.tabs);
+ testTabNotFound(activeWindowTabs);
+
+ done();
+ });
+ }
+ });
+};
+
+exports["test viewFor(tab)"] = (assert, done) => {
+ // Note we defer handlers as length collection is updated after
+ // handler is invoked, so if test is finished before counnts are
+ // updated wrong length will show up in followup tests.
+ tabs.once("open", defer(tab => {
+ const view = viewFor(tab);
+ assert.ok(view, "view is returned");
+ assert.equal(getTabId(view), tab.id, "tab has a same id");
+
+ tab.close(defer(done));
+ }));
+
+ tabs.open({ url: "about:mozilla" });
+};
+
+
+exports["test modelFor(xulTab)"] = (assert, done) => {
+ tabs.open({
+ url: "about:mozilla",
+ onReady: tab => {
+ const view = viewFor(tab);
+ assert.ok(view, "view is returned");
+ assert.ok(isTab(view), "view is underlaying tab");
+ assert.equal(getTabId(view), tab.id, "tab has a same id");
+ assert.equal(modelFor(view), tab, "modelFor(view) is SDK tab");
+
+ tab.close(defer(done));
+ }
+ });
+};
+
+exports["test tab.readyState"] = (assert, done) => {
+ tabs.open({
+ url: "data:text/html;charset=utf-8,test_readyState",
+ onOpen: (tab) => {
+ assert.notEqual(["uninitialized", "loading"].indexOf(tab.readyState), -1,
+ "tab is either uninitialized or loading when onOpen");
+ },
+ onReady: (tab) => {
+ assert.notEqual(["interactive", "complete"].indexOf(tab.readyState), -1,
+ "tab is either interactive or complete when onReady");
+ },
+ onLoad: (tab) => {
+ assert.equal(tab.readyState, "complete", "tab is complete onLoad");
+ tab.close(defer(done));
+ }
+ });
+}
+
+exports["test tab.readyState for existent tabs"] = function* (assert) {
+ assert.equal(tabs.length, 1, "tabs contains an existent tab");
+
+ function frameScript() {
+ sendAsyncMessage("test:contentDocument.readyState", content.document.readyState);
+ }
+
+ for (let tab of tabs) {
+ let browserForTab = getBrowserForTab(viewFor(tab));
+ let mm = browserForTab.messageManager;
+
+ yield new Promise((resolve) => {
+ mm.addMessageListener("test:contentDocument.readyState", function listener(evt) {
+ mm.removeMessageListener("test:contentDocument.readyState", listener);
+ assert.equal(evt.data, tab.readyState,
+ "tab.readyState has the same value of the associated contentDocument.readyState CPOW");
+ resolve();
+ });
+ mm.loadFrameScript(`data:,new ${frameScript};`, false);
+ });
+ }
+}
+
+require("sdk/test").run(module.exports);
diff --git a/addon-sdk/source/test/test-tabs-common.js b/addon-sdk/source/test/test-tabs-common.js
new file mode 100644
index 000000000..7ab8b3c76
--- /dev/null
+++ b/addon-sdk/source/test/test-tabs-common.js
@@ -0,0 +1,654 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Loader, LoaderWithHookedConsole } = require("sdk/test/loader");
+const { browserWindows } = require('sdk/windows');
+const tabs = require('sdk/tabs');
+const { isPrivate } = require('sdk/private-browsing');
+const { openDialog } = require('sdk/window/utils');
+const { isWindowPrivate } = require('sdk/window/utils');
+const { setTimeout } = require('sdk/timers');
+const { openWebpage } = require('./private-browsing/helper');
+const { isTabPBSupported, isWindowPBSupported } = require('sdk/private-browsing/utils');
+const { getTabContentWindow } = require('sdk/tabs/utils');
+const { attach, detach } = require('sdk/content/mod');
+const { Style } = require('sdk/stylesheet/style');
+const fixtures = require('./fixtures');
+const { viewFor } = require('sdk/view/core');
+const app = require("sdk/system/xul-app");
+const { cleanUI } = require('sdk/test/utils');
+
+const URL = 'data:text/html;charset=utf-8,<html><head><title>#title#</title></head></html>';
+
+// TEST: tab count
+exports.testTabCounts = function(assert, done) {
+ tabs.open({
+ url: 'about:blank',
+ onReady: function(tab) {
+ let count1 = 0,
+ count2 = 0;
+ for (let window of browserWindows) {
+ count1 += window.tabs.length;
+ for (let tab of window.tabs) {
+ count2 += 1;
+ }
+ }
+
+ assert.ok(tabs.length > 1, 'tab count is > 1');
+ assert.equal(count1, tabs.length, 'tab count by length is correct');
+ assert.equal(count2, tabs.length, 'tab count by iteration is correct');
+
+ // end test
+ tab.close(done);
+ }
+ });
+};
+
+exports.testTabRelativePath = function(assert, done) {
+ const { merge } = require("sdk/util/object");
+ const self = require("sdk/self");
+
+ const options = merge({}, require('@loader/options'),
+ { prefixURI: require('./fixtures').url() });
+
+ let loader = Loader(module, null, options);
+
+ let tabs = loader.require("sdk/tabs");
+
+ tabs.open({
+ url: "./test.html",
+ onReady: (tab) => {
+ assert.equal(tab.title, "foo",
+ "tab opened a document with relative path");
+
+ tab.attach({
+ contentScriptFile: "./test-contentScriptFile.js",
+ onMessage: (message) => {
+ assert.equal(message, "msg from contentScriptFile",
+ "Tab attach a contentScriptFile with relative path worked");
+
+ tab.close(done);
+ loader.unload();
+ }
+ });
+ }
+ });
+};
+
+// TEST: tabs.activeTab getter
+exports.testActiveTab_getter = function(assert, done) {
+ let evtCount = 0;
+ let activeTab = null;
+
+ function endTest(type, tab) {
+ if (type == 'activate') {
+ assert.strictEqual(tabs.activeTab, tab, 'the active tab is the opened tab');
+ activeTab = tabs.activeTab;
+ }
+ else {
+ assert.equal(tab.url, url, 'the opened tab has the correct url');
+ }
+
+ if (++evtCount != 2)
+ return;
+
+ assert.strictEqual(activeTab, tab, 'the active tab is the ready tab');
+ assert.strictEqual(tabs.activeTab, tab, 'the active tab is the ready tab');
+
+ tab.close(done);
+ }
+
+ let url = URL.replace("#title#", "testActiveTab_getter");
+ tabs.open({
+ url: url,
+ onReady: endTest.bind(null, 'ready'),
+ onActivate: endTest.bind(null, 'activate')
+ });
+};
+
+// TEST: tab.activate()
+exports.testActiveTab_setter = function(assert, done) {
+ let url = URL.replace("#title#", "testActiveTab_setter");
+ let tab1URL = URL.replace("#title#", "tab1");
+
+ tabs.open({
+ url: tab1URL,
+ onReady: function(activeTab) {
+ let activeTabURL = tabs.activeTab.url;
+
+ tabs.open({
+ url: url,
+ inBackground: true,
+ onReady: function onReady(tab) {
+ assert.equal(tabs.activeTab.url, activeTabURL, "activeTab url has not changed");
+ assert.equal(tab.url, url, "url of new background tab matches");
+
+ tab.once('activate', function onActivate(eventTab) {
+ assert.equal(tabs.activeTab.url, url, "url after activeTab setter matches");
+ assert.equal(eventTab, tab, "event argument is the activated tab");
+ assert.equal(eventTab, tabs.activeTab, "the tab is the active one");
+
+ activeTab.close(function() {
+ tab.close(done);
+ });
+ });
+
+ tab.activate();
+ }
+ });
+ }
+ });
+};
+
+// TEST: tab.close()
+exports.testTabClose_alt = function(assert, done) {
+ let url = URL.replace('#title#', 'TabClose_alt');
+ let tab1URL = URL.replace('#title#', 'tab1');
+
+ tabs.open({
+ url: tab1URL,
+ onReady: function(tab1) {
+ // make sure that our tab is not active first
+ assert.notEqual(tabs.activeTab.url, url, "tab is not the active tab");
+
+ tabs.open({
+ url: url,
+ onReady: function(tab) {
+ assert.equal(tab.url, url, "tab is now the active tab");
+ assert.equal(tabs.activeTab.url, url, "tab is now the active tab");
+
+ // another tab should be activated on close
+ tabs.once('activate', function() {
+ assert.notEqual(tabs.activeTab.url, url, "tab is no longer the active tab");
+
+ // end test
+ tab1.close(done);
+ });
+
+ tab.close();
+ }
+ });
+ }
+ });
+};
+
+exports.testAttachOnMultipleDocuments_alt = function (assert, done) {
+ // Example of attach that process multiple tab documents
+ let firstLocation = "data:text/html;charset=utf-8,foobar";
+ let secondLocation = "data:text/html;charset=utf-8,bar";
+ let thirdLocation = "data:text/html;charset=utf-8,fox";
+ let onReadyCount = 0;
+ let worker1 = null;
+ let worker2 = null;
+ let detachEventCount = 0;
+
+ tabs.open({
+ url: firstLocation,
+ onReady: function (tab) {
+ onReadyCount++;
+ if (onReadyCount == 1) {
+ worker1 = tab.attach({
+ contentScript: 'self.on("message", ' +
+ ' () => self.postMessage(document.location.href)' +
+ ');',
+ onMessage: function (msg) {
+ assert.equal(msg, firstLocation,
+ "Worker url is equal to the 1st document");
+ tab.url = secondLocation;
+ },
+ onDetach: function () {
+ detachEventCount++;
+ assert.pass("Got worker1 detach event");
+ assert.throws(function () {
+ worker1.postMessage("ex-1");
+ },
+ /Couldn't find the worker/,
+ "postMessage throw because worker1 is destroyed");
+ checkEnd();
+ }
+ });
+ worker1.postMessage("new-doc-1");
+ }
+ else if (onReadyCount == 2) {
+ worker2 = tab.attach({
+ contentScript: 'self.on("message", ' +
+ ' () => self.postMessage(document.location.href)' +
+ ');',
+ onMessage: function (msg) {
+ assert.equal(msg, secondLocation,
+ "Worker url is equal to the 2nd document");
+ tab.url = thirdLocation;
+ },
+ onDetach: function () {
+ detachEventCount++;
+ assert.pass("Got worker2 detach event");
+ assert.throws(function () {
+ worker2.postMessage("ex-2");
+ },
+ /Couldn't find the worker/,
+ "postMessage throw because worker2 is destroyed");
+ checkEnd(tab);
+ }
+ });
+ worker2.postMessage("new-doc-2");
+ }
+ else if (onReadyCount == 3) {
+ tab.close();
+ }
+ }
+ });
+
+ function checkEnd(tab) {
+ if (detachEventCount != 2)
+ return;
+
+ assert.pass("Got all detach events");
+
+ done();
+ }
+};
+
+exports.testAttachWrappers_alt = function (assert, done) {
+ // Check that content script has access to wrapped values by default
+
+ let document = "data:text/html;charset=utf-8,<script>var globalJSVar = true; " +
+ " document.getElementById = 3;</script>";
+ let count = 0;
+
+ tabs.open({
+ url: document,
+ onReady: function (tab) {
+ let worker = tab.attach({
+ contentScript: 'try {' +
+ ' self.postMessage(!("globalJSVar" in window));' +
+ ' self.postMessage(typeof window.globalJSVar == "undefined");' +
+ '} catch(e) {' +
+ ' self.postMessage(e.message);' +
+ '}',
+ onMessage: function (msg) {
+ assert.equal(msg, true, "Worker has wrapped objects ("+count+")");
+ if (count++ == 1)
+ tab.close(() => done());
+ }
+ });
+ }
+ });
+};
+
+// TEST: activeWindow getter and activeTab getter on tab 'activate' event
+exports.testActiveWindowActiveTabOnActivate_alt = function(assert, done) {
+
+ let activateCount = 0;
+ let newTabs = [];
+ let tabs = browserWindows.activeWindow.tabs;
+
+ tabs.on('activate', function onActivate(tab) {
+ assert.equal(tabs.activeTab, tab,
+ "the active window's active tab is the tab provided");
+
+ if (++activateCount == 2) {
+ assert.equal(newTabs.length, activateCount, "Should have seen the right number of tabs open");
+ tabs.removeListener('activate', onActivate);
+
+ newTabs.forEach(function(tab) {
+ tab.close(function() {
+ if (--activateCount == 0) {
+ done();
+ }
+ });
+ });
+ }
+ else if (activateCount > 2) {
+ assert.fail("activateCount is greater than 2 for some reason..");
+ }
+ });
+
+ tabs.open({
+ url: URL.replace("#title#", "tabs.open1"),
+ onOpen: tab => newTabs.push(tab)
+ });
+ tabs.open({
+ url: URL.replace("#title#", "tabs.open2"),
+ onOpen: tab => newTabs.push(tab)
+ });
+};
+
+// TEST: tab properties
+exports.testTabContentTypeAndReload = function(assert, done) {
+
+ let url = "data:text/html;charset=utf-8,<html><head><title>foo</title></head><body>foo</body></html>";
+ let urlXML = "data:text/xml;charset=utf-8,<foo>bar</foo>";
+ tabs.open({
+ url: url,
+ onReady: function(tab) {
+ if (tab.url === url) {
+ assert.equal(tab.contentType, "text/html");
+ tab.url = urlXML;
+ }
+ else {
+ assert.equal(tab.contentType, "text/xml");
+ tab.close(done);
+ }
+ }
+ });
+};
+
+// test that it isn't possible to open a private tab without the private permission
+exports.testTabOpenPrivate = function(assert, done) {
+
+ let url = 'about:blank';
+ tabs.open({
+ url: url,
+ isPrivate: true,
+ onReady: function(tab) {
+ assert.equal(tab.url, url, 'opened correct tab');
+ assert.equal(isPrivate(tab), false, 'private tabs are not supported by default');
+
+ tab.close(done);
+ }
+ });
+}
+
+// We need permission flag in order to see private window's tabs
+exports.testPrivateAreNotListed = function (assert, done) {
+ let originalTabCount = tabs.length;
+
+ let page = openWebpage("about:blank", true);
+ if (!page) {
+ assert.pass("Private browsing isn't supported in this release");
+ return;
+ }
+
+ page.ready.then(function (win) {
+ if (isTabPBSupported || isWindowPBSupported) {
+ assert.ok(isWindowPrivate(win), "the window is private");
+ assert.equal(tabs.length, originalTabCount,
+ 'but the tab is *not* visible in tabs list');
+ }
+ else {
+ assert.ok(!isWindowPrivate(win), "the window isn't private");
+ assert.equal(tabs.length, originalTabCount + 1,
+ 'so that the tab is visible is tabs list');
+ }
+ page.close().then(done);
+ });
+}
+
+// If we close the tab while being in `onOpen` listener,
+// we end up synchronously consuming TabOpen, closing the tab and still
+// synchronously consuming the related TabClose event before the second
+// loader have a change to process the first TabOpen event!
+exports.testImmediateClosing = function (assert, done) {
+ let tabURL = 'data:text/html,foo';
+
+ let { loader, messages } = LoaderWithHookedConsole(module, onMessage);
+ let concurrentTabs = loader.require("sdk/tabs");
+ concurrentTabs.on("open", function (tab) {
+ // On Firefox, It shouldn't receive such event as the other loader will just
+ // open and destroy the tab without giving a chance to other loader to even
+ // know about the existance of this tab.
+ if (app.is("Firefox")) {
+ assert.fail("Concurrent loader received a tabs `open` event");
+ }
+ else {
+ // On mobile, we can still receive an open event,
+ // but not the related ready event
+ tab.on("ready", function () {
+ assert.fail("Concurrent loader received a tabs `ready` event");
+ });
+ }
+ });
+ function onMessage(type, msg) {
+ assert.fail("Unexpected mesage on concurrent loader: " + msg);
+ }
+
+ tabs.open({
+ url: tabURL,
+ onOpen: function(tab) {
+ tab.close(function () {
+ assert.pass("Tab succesfully removed");
+ // Let a chance to the concurrent loader to receive a TabOpen event
+ // on the next event loop turn
+ setTimeout(function () {
+ loader.unload();
+ done();
+ }, 0);
+ });
+ }
+ });
+}
+
+// TEST: tab.reload()
+exports.testTabReload = function(assert, done) {
+
+ let url = "data:text/html;charset=utf-8,<!doctype%20html><title></title>";
+
+ tabs.open({
+ url: url,
+ onReady: function onReady(tab) {
+ tab.removeListener('ready', onReady);
+
+ tab.once(
+ 'ready',
+ function onReload() {
+ assert.pass("the tab was loaded again");
+ assert.equal(tab.url, url, "the tab has the same URL");
+
+ tab.close(() => done());
+ }
+ );
+
+ tab.reload();
+ }
+ });
+};
+
+exports.testOnPageShowEvent = function (assert, done) {
+ let events = [];
+ let firstUrl = 'data:text/html;charset=utf-8,First';
+ let secondUrl = 'data:text/html;charset=utf-8,Second';
+
+ let counter = 0;
+ function onPageShow (tab, persisted) {
+ events.push('pageshow');
+ counter++;
+ if (counter === 1) {
+ assert.equal(persisted, false, 'page should not be cached on initial load');
+ tab.url = secondUrl;
+ }
+ else if (counter === 2) {
+ assert.equal(persisted, false, 'second test page should not be cached either');
+ tab.attach({
+ contentScript: 'setTimeout(function () { window.history.back(); }, 0)'
+ });
+ }
+ else {
+ assert.equal(persisted, true, 'when we get back to the fist page, it has to' +
+ 'come from cache');
+ tabs.removeListener('pageshow', onPageShow);
+ tabs.removeListener('open', onOpen);
+ tabs.removeListener('ready', onReady);
+ tab.close(() => {
+ ['open', 'ready', 'pageshow', 'ready',
+ 'pageshow', 'pageshow'].map((type, i) => {
+ assert.equal(type, events[i], 'correct ordering of events');
+ });
+ done()
+ });
+ }
+ }
+
+ function onOpen () {
+ return events.push('open');
+ }
+ function onReady () {
+ return events.push('ready');
+ }
+
+ tabs.on('pageshow', onPageShow);
+ tabs.on('open', onOpen);
+ tabs.on('ready', onReady);
+ tabs.open({
+ url: firstUrl
+ });
+};
+
+exports.testOnPageShowEventDeclarative = function (assert, done) {
+ let events = [];
+ let firstUrl = 'data:text/html;charset=utf-8,First';
+ let secondUrl = 'data:text/html;charset=utf-8,Second';
+
+ let counter = 0;
+ function onPageShow (tab, persisted) {
+ events.push('pageshow');
+ counter++;
+ if (counter === 1) {
+ assert.equal(persisted, false, 'page should not be cached on initial load');
+ tab.url = secondUrl;
+ }
+ else if (counter === 2) {
+ assert.equal(persisted, false, 'second test page should not be cached either');
+ tab.attach({
+ contentScript: 'setTimeout(function () { window.history.back(); }, 0)'
+ });
+ }
+ else {
+ assert.equal(persisted, true, 'when we get back to the fist page, it has to' +
+ 'come from cache');
+ tabs.removeListener('pageshow', onPageShow);
+ tabs.removeListener('open', onOpen);
+ tabs.removeListener('ready', onReady);
+ tab.close(() => {
+ ['open', 'ready', 'pageshow', 'ready',
+ 'pageshow', 'pageshow'].map((type, i) => {
+ assert.equal(type, events[i], 'correct ordering of events');
+ });
+ done()
+ });
+ }
+ }
+
+ function onOpen () {
+ return events.push('open');
+ }
+ function onReady () {
+ return events.push('ready');
+ }
+
+ tabs.open({
+ url: firstUrl,
+ onPageShow: onPageShow,
+ onOpen: onOpen,
+ onReady: onReady
+ });
+};
+
+exports.testAttachStyleToTab = function(assert, done) {
+ let style = Style({
+ source: "div { height: 100px; }",
+ uri: fixtures.url("include-file.css")
+ });
+
+ tabs.open({
+ url: "data:text/html;charset=utf-8,<div style='background: silver'>css test</div>",
+ onReady: (tab) => {
+ let xulTab = viewFor(tab);
+
+ attach(style, tab)
+
+ let { document } = getTabContentWindow(xulTab);
+ let div = document.querySelector("div");
+
+ assert.equal(div.clientHeight, 100,
+ "Style.source properly attached to tab");
+
+ assert.equal(div.offsetHeight, 120,
+ "Style.uri properly attached to tab");
+
+ detach(style, tab);
+
+ assert.notEqual(div.clientHeight, 100,
+ "Style.source properly detached from tab");
+
+ assert.notEqual(div.offsetHeight, 120,
+ "Style.uri properly detached from tab");
+
+ attach(style, xulTab);
+
+ assert.equal(div.clientHeight, 100,
+ "Style.source properly attached to xul tab");
+
+ assert.equal(div.offsetHeight, 120,
+ "Style.uri properly attached to xul tab");
+
+ detach(style, tab);
+
+ assert.notEqual(div.clientHeight, 100,
+ "Style.source properly detached from xul tab");
+
+ assert.notEqual(div.offsetHeight, 120,
+ "Style.uri properly detached from xul tab");
+
+ tab.close(done);
+ }
+ });
+};
+
+// Tests that the this property is correct in event listeners called from
+// the tabs API.
+exports.testTabEventBindings = function(assert, done) {
+ let loader = Loader(module);
+ let tabs = loader.require("sdk/tabs");
+ let firstTab = tabs.activeTab;
+
+ let EVENTS = ["open", "close", "activate", "deactivate",
+ "load", "ready", "pageshow"];
+
+ let tabBoundEventHandler = (event) => function(tab) {
+ assert.equal(this, tab, "tab listener for " + event + " event should be bound to the tab object.");
+ };
+
+ let tabsBoundEventHandler = (event) => function(tab) {
+ assert.equal(this, tabs, "tabs listener for " + event + " event should be bound to the tabs object.");
+ }
+ for (let event of EVENTS)
+ tabs.on(event, tabsBoundEventHandler(event));
+
+ let tabsOpenEventHandler = (event) => function(tab) {
+ assert.equal(this, tab, "tabs open listener for " + event + " event should be bound to the tab object.");
+ }
+
+ let openArgs = {
+ url: "data:text/html;charset=utf-8,binding-test",
+ onOpen: function(tab) {
+ tabsOpenEventHandler("open").call(this, tab);
+
+ for (let event of EVENTS)
+ tab.on(event, tabBoundEventHandler(event));
+
+ tab.once("pageshow", () => {
+ tab.once("deactivate", () => {
+ tab.once("close", () => {
+ loader.unload();
+ done();
+ });
+
+ tab.close();
+ });
+
+ firstTab.activate();
+ });
+ }
+ };
+ // Listen to everything except onOpen
+ for (let event of EVENTS.slice(1)) {
+ let eventProperty = "on" + event.slice(0, 1).toUpperCase() + event.slice(1);
+ openArgs[eventProperty] = tabsOpenEventHandler(event);
+ }
+
+ tabs.open(openArgs);
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-tabs.js b/addon-sdk/source/test/test-tabs.js
new file mode 100644
index 000000000..84fbdb228
--- /dev/null
+++ b/addon-sdk/source/test/test-tabs.js
@@ -0,0 +1,20 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+module.metadata = {
+ 'engines': {
+ 'Firefox': '*',
+ 'Fennec': '*'
+ }
+};
+
+const app = require('sdk/system/xul-app');
+
+if (app.is('Fennec')) {
+ module.exports = require('./tabs/test-fennec-tabs');
+}
+else {
+ module.exports = require('./tabs/test-firefox-tabs');
+}
diff --git a/addon-sdk/source/test/test-test-addon-file.js b/addon-sdk/source/test/test-test-addon-file.js
new file mode 100644
index 000000000..6d09d7534
--- /dev/null
+++ b/addon-sdk/source/test/test-test-addon-file.js
@@ -0,0 +1,16 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+// demonstrates that requiring a test file from a test addon works
+
+exports["test part 1"] = function(assert) {
+ let test = require("./addons/addon-manager/lib/test-main.js");
+ assert.equal(Object.keys(test).length, 1, "there is only one test");
+ assert.ok("test getAddonByID" in test, "the test is corret");
+};
+
+exports["test getAddonByID"] = require("./addons/addon-manager/lib/test-main.js")["test getAddonByID"];
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-test-assert.js b/addon-sdk/source/test/test-test-assert.js
new file mode 100644
index 000000000..8f6449653
--- /dev/null
+++ b/addon-sdk/source/test/test-test-assert.js
@@ -0,0 +1,218 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Assert } = require("sdk/test/assert");
+
+function createAssertTest() {
+ let failures = [], successes = [], exceptions = [];
+ return {
+ test: new Assert({
+ fail: (m) => failures.push(m),
+ pass: (m) => successes.push(m),
+ exception: (e) => exceptions.push(e)
+ }),
+ failures: failures,
+ successes: successes,
+ exceptions: exceptions
+ };
+}
+
+exports["test createAssertTest initial state"] = function(assert) {
+ let { failures, successes, exceptions } = createAssertTest();
+
+ assert.equal(successes.length, 0, "0 success log");
+ assert.equal(failures.length, 0, "0 failure logs");
+ assert.equal(exceptions.length, 0, "0 exception logs");
+}
+
+exports["test assert.ok(true)"] = (assert) => {
+ let { test, failures, successes, exceptions } = createAssertTest();
+
+ assert.strictEqual(test.ok(true), true, "assert ok(true) strictEquals true");
+ assert.equal(successes.length, 1, "1 success log");
+
+ assert.equal(test.ok(true), true, "assert ok(true) equals true");
+ assert.equal(successes.length, 2, "2 success logs");
+
+ assert.ok(test.ok(true), "assert ok(true) is ok");
+ assert.equal(successes.length, 3, "3 success logs");
+
+ assert.equal(failures.length, 0, "0 failure logs");
+ assert.equal(exceptions.length, 0, "0 exception logs");
+}
+
+exports["test assert.ok(false)"] = (assert) => {
+ let { test, failures, successes, exceptions } = createAssertTest();
+
+ assert.strictEqual(test.ok(false), false, "assert ok(false) strictEquals false");
+ assert.equal(failures.length, 1, "1 failure log");
+
+ assert.equal(test.ok(false), false, "assert ok(false) equals false");
+ assert.equal(failures.length, 2, "2 failure logs");
+
+ assert.ok(!test.ok(false), "assert ok(false) is not ok");
+ assert.equal(failures.length, 3, "3 failure logs");
+
+ assert.equal(successes.length, 0, "0 success log");
+ assert.equal(exceptions.length, 0, "0 exception logs");
+}
+
+exports["test assert.ok(false) failure message"] = (assert) => {
+ let { test, failures, successes, exceptions } = createAssertTest();
+
+ test.ok(false, "XYZ");
+
+ assert.equal(successes.length, 0, "0 success log");
+ assert.equal(failures.length, 1, "1 failure logs");
+ assert.equal(exceptions.length, 0, "0 exception logs");
+
+ assert.equal(failures[0], "XYZ - false == true");
+}
+
+exports["test assert.equal"] = (assert) => {
+ let { test, failures, successes, exceptions } = createAssertTest();
+
+ assert.strictEqual(test.equal(true, true), true, "assert equal(true, true) strictEquals true");
+ assert.equal(test.equal(true, true), true, "assert equal(true, true) equals true");
+ assert.ok(test.equal(true, true), "assert equal(true, true) is ok");
+
+ assert.equal(successes.length, 3, "3 success log");
+ assert.equal(failures.length, 0, "0 failure logs");
+ assert.equal(exceptions.length, 0, "0 exception logs");
+}
+
+exports["test assert.equal failure message"] = (assert) => {
+ let { test, failures, successes, exceptions } = createAssertTest();
+
+ test.equal("foo", "bar", "XYZ");
+
+ assert.equal(successes.length, 0, "0 success log");
+ assert.equal(failures.length, 1, "1 failure logs");
+ assert.equal(exceptions.length, 0, "0 exception logs");
+
+ assert.equal(failures[0], "XYZ - \"foo\" == \"bar\"");
+}
+
+exports["test assert.strictEqual"] = (assert) => {
+ let { test, failures, successes, exceptions } = createAssertTest();
+
+ assert.strictEqual(test.strictEqual(true, true), true, "assert strictEqual(true, true) strictEquals true");
+ assert.equal(successes.length, 1, "1 success logs");
+
+ assert.equal(test.strictEqual(true, true), true, "assert strictEqual(true, true) equals true");
+ assert.equal(successes.length, 2, "2 success logs");
+
+ assert.ok(test.strictEqual(true, true), "assert strictEqual(true, true) is ok");
+ assert.equal(successes.length, 3, "3 success logs");
+
+ assert.equal(failures.length, 0, "0 failure logs");
+ assert.equal(exceptions.length, 0, "0 exception logs");
+}
+
+exports["test assert.strictEqual failure message"] = (assert) => {
+ let { test, failures, successes, exceptions } = createAssertTest();
+
+ test.strictEqual("foo", "bar", "XYZ");
+
+ assert.equal(successes.length, 0, "0 success log");
+ assert.equal(failures.length, 1, "1 failure logs");
+ assert.equal(exceptions.length, 0, "0 exception logs");
+
+ assert.equal(failures[0], "XYZ - \"foo\" === \"bar\"");
+}
+
+exports["test assert.throws(func, string, string) matches"] = (assert) => {
+ let { test, failures, successes, exceptions } = createAssertTest();
+
+ assert.ok(
+ test.throws(
+ () => { throw new Error("this is a thrown error") },
+ "this is a thrown error"
+ ),
+ "throwing an new Error works");
+ assert.equal(successes.length, 1, "1 success log");
+
+ assert.ok(
+ test.throws(
+ () => { throw Error("this is a thrown error") },
+ "this is a thrown error"
+ ),
+ "throwing an Error works");
+ assert.equal(successes.length, 2, "2 success log");
+
+ assert.ok(
+ test.throws(
+ () => { throw "this is a thrown string" },
+ "this is a thrown string"
+ ),
+ "throwing a String works");
+ assert.equal(successes.length, 3, "3 success logs");
+
+ assert.equal(failures.length, 0, "0 failure logs");
+ assert.equal(exceptions.length, 0, "0 exception logs");
+}
+
+exports["test assert.throws(func, string, string) failure message"] = (assert) => {
+ let { test, failures, successes, exceptions } = createAssertTest();
+
+ test.throws(
+ () => { throw new Error("foo") },
+ "bar",
+ "XYZ");
+
+ assert.equal(successes.length, 0, "0 success log");
+ assert.equal(failures.length, 1, "1 failure logs");
+ assert.equal(exceptions.length, 0, "0 exception logs");
+
+ assert.equal(failures[0], "XYZ - \"foo\" matches \"bar\"");
+}
+
+exports["test assert.throws(func, regex, string) matches"] = (assert) => {
+ let { test, failures, successes, exceptions } = createAssertTest();
+
+ assert.ok(
+ test.throws(
+ () => { throw new Error("this is a thrown error") },
+ /this is a thrown error/
+ ),
+ "throwing an new Error works");
+ assert.equal(successes.length, 1, "1 success log");
+
+ assert.ok(
+ test.throws(
+ () => { throw Error("this is a thrown error") },
+ /this is a thrown error/
+ ),
+ "throwing an Error works");
+ assert.equal(successes.length, 2, "2 success log");
+
+ assert.ok(
+ test.throws(
+ () => { throw "this is a thrown string" },
+ /this is a thrown string/
+ ),
+ "throwing a String works");
+ assert.equal(successes.length, 3, "3 success logs");
+
+ assert.equal(failures.length, 0, "0 failure logs");
+ assert.equal(exceptions.length, 0, "0 exception logs");
+}
+
+exports["test assert.throws(func, regex, string) failure message"] = (assert) => {
+ let { test, failures, successes, exceptions } = createAssertTest();
+
+ test.throws(
+ () => { throw new Error("foo") },
+ /bar/i,
+ "XYZ");
+
+ assert.equal(successes.length, 0, "0 success log");
+ assert.equal(failures.length, 1, "1 failure logs");
+ assert.equal(exceptions.length, 0, "0 exception logs");
+
+ assert.equal(failures[0], "XYZ - \"foo\" matches \"/bar/i\"");
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-test-loader.js b/addon-sdk/source/test/test-test-loader.js
new file mode 100644
index 000000000..6b47e4d0d
--- /dev/null
+++ b/addon-sdk/source/test/test-test-loader.js
@@ -0,0 +1,59 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict"
+
+const { LoaderWithHookedConsole } = require("sdk/test/loader");
+
+exports["test LoaderWithHookedConsole"] = function (assert) {
+ let count = 0;
+ function onMessage(type, message) {
+ switch (count++) {
+ case 0:
+ assert.equal(type, "log", "got log type");
+ assert.equal(message, "1st", "got log msg");
+ break;
+ case 1:
+ assert.equal(type, "error", "got error type");
+ assert.equal(message, "2nd", "got error msg");
+ break;
+ case 2:
+ assert.equal(type, "warn", "got warn type");
+ assert.equal(message, "3rd", "got warn msg");
+ break;
+ case 3:
+ assert.equal(type, "info", "got info type");
+ assert.equal(message, "4th", "got info msg");
+ break;
+ case 4:
+ assert.equal(type, "debug", "got debug type");
+ assert.equal(message, "5th", "got debug msg");
+ break;
+ case 5:
+ assert.equal(type, "exception", "got exception type");
+ assert.equal(message, "6th", "got exception msg");
+ break;
+ default:
+ assert.fail("Got unexception message: " + i);
+ }
+ }
+
+ let { loader, messages } = LoaderWithHookedConsole(module, onMessage);
+ let console = loader.globals.console;
+ console.log("1st");
+ console.error("2nd");
+ console.warn("3rd");
+ console.info("4th");
+ console.debug("5th");
+ console.exception("6th");
+ assert.equal(messages.length, 6, "Got all console messages");
+ assert.deepEqual(messages[0], {type: "log", msg: "1st", innerID: null}, "Got log");
+ assert.deepEqual(messages[1], {type: "error", msg: "2nd", innerID: null}, "Got error");
+ assert.deepEqual(messages[2], {type: "warn", msg: "3rd", innerID: null}, "Got warn");
+ assert.deepEqual(messages[3], {type: "info", msg: "4th", innerID: null}, "Got info");
+ assert.deepEqual(messages[4], {type: "debug", msg: "5th", innerID: null}, "Got debug");
+ assert.deepEqual(messages[5], {type: "exception", msg: "6th", innerID: null}, "Got exception");
+ assert.equal(count, 6, "Called for all messages");
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-test-memory.js b/addon-sdk/source/test/test-test-memory.js
new file mode 100644
index 000000000..0f53fae8c
--- /dev/null
+++ b/addon-sdk/source/test/test-test-memory.js
@@ -0,0 +1,25 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Cc, Ci, Cu, components } = require('chrome');
+const { gc } = require('sdk/test/memory');
+
+exports.testGC = function*(assert) {
+ let weakref;
+
+ if (true) {
+ let tempObj = {};
+ weakref = Cu.getWeakReference(tempObj);
+ assert.equal(weakref.get(), tempObj, 'the weakref returned the tempObj');
+ }
+
+ let arg = yield gc();
+
+ assert.equal(arg, undefined, 'there is no argument');
+ assert.pass('gc() returns a promise which eventually resolves');
+ assert.equal(weakref.get(), undefined, 'the weakref returned undefined');
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-test-utils-async.js b/addon-sdk/source/test/test-test-utils-async.js
new file mode 100644
index 000000000..1465ce58c
--- /dev/null
+++ b/addon-sdk/source/test/test-test-utils-async.js
@@ -0,0 +1,86 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { defer: async } = require('sdk/lang/functional');
+const { before, after } = require('sdk/test/utils');
+const { resolve } = require('sdk/core/promise');
+
+var AFTER_RUN = 0;
+var BEFORE_RUN = 0;
+
+/*
+ * Tests are dependent on ordering, as the before and after functions
+ * are called outside of each test, and sometimes checked in the next test
+ * (like in the `after` tests)
+ */
+exports.testABeforeAsync = function (assert, done) {
+ assert.equal(BEFORE_RUN, 1, 'before function was called');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+ async(done)();
+};
+
+exports.testABeforeNameAsync = function (assert, done) {
+ assert.equal(BEFORE_RUN, 2, 'before function was called with name');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+ async(done)();
+};
+
+exports.testAfterAsync = function (assert, done) {
+ assert.equal(AFTER_RUN, 1, 'after function was called previously');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+ async(done)();
+};
+
+exports.testAfterNameAsync = function (assert, done) {
+ assert.equal(AFTER_RUN, 2, 'after function was called with name');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+ async(done)();
+};
+
+exports.testSyncABefore = function (assert) {
+ assert.equal(BEFORE_RUN, 1, 'before function was called for sync test');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+};
+
+exports.testSyncAfter = function (assert) {
+ assert.equal(AFTER_RUN, 1, 'after function was called for sync test');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+};
+
+exports.testGeneratorBefore = function*(assert) {
+ assert.equal(BEFORE_RUN, 1, 'before function was called for generator test');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+ yield resolve();
+}
+
+exports.testGeneratorAfter = function*(assert) {
+ assert.equal(AFTER_RUN, 1, 'after function was called for generator test');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+ yield resolve();
+};
+
+before(exports, (name, assert, done) => {
+ BEFORE_RUN = (name === 'testABeforeNameAsync') ? 2 : 1;
+ assert.pass('assert passed into before function');
+ async(done)();
+});
+
+after(exports, (name, assert, done) => {
+ // testAfterName runs after testAfter, which is where this
+ // check occurs in the assertation
+ AFTER_RUN = (name === 'testAfterAsync') ? 2 : 1;
+ assert.pass('assert passed into after function');
+ async(done)();
+});
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-test-utils-generator.js b/addon-sdk/source/test/test-test-utils-generator.js
new file mode 100644
index 000000000..da3477900
--- /dev/null
+++ b/addon-sdk/source/test/test-test-utils-generator.js
@@ -0,0 +1,76 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+// NOTE: this test is explictly testing non-normal,
+// generator functions.
+
+
+const { defer: async } = require('sdk/lang/functional');
+const { before, after } = require('sdk/test/utils');
+const { resolve } = require("sdk/core/promise");
+
+var AFTER_RUN = 0;
+var BEFORE_RUN = 0;
+
+/*
+ * Tests are dependent on ordering, as the before and after functions
+ * are called outside of each test, and sometimes checked in the next test
+ * (like in the `after` tests)
+ */
+exports.testABeforeAsync = function (assert, done) {
+ assert.equal(BEFORE_RUN, 1, 'before function was called');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+ async(done)();
+};
+
+exports.testABeforeNameAsync = function (assert, done) {
+ assert.equal(BEFORE_RUN, 2, 'before function was called with name');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+ async(done)();
+};
+
+exports.testAfterAsync = function (assert, done) {
+ assert.equal(AFTER_RUN, 1, 'after function was called previously');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+ async(done)();
+};
+
+exports.testAfterNameAsync = function (assert, done) {
+ assert.equal(AFTER_RUN, 2, 'after function was called with name');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+ async(done)();
+};
+
+exports.testSyncABefore = function (assert) {
+ assert.equal(BEFORE_RUN, 1, 'before function was called for sync test');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+};
+
+exports.testSyncAfter = function (assert) {
+ assert.equal(AFTER_RUN, 1, 'after function was called for sync test');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+};
+
+before(exports, function*(name, assert) {
+ yield resolve();
+ BEFORE_RUN = (name === 'testABeforeNameAsync') ? 2 : 1;
+ assert.pass('assert passed into before function');
+});
+
+after(exports, function*(name, assert) {
+ yield resolve();
+ // testAfterName runs after testAfter, which is where this
+ // check occurs in the assertation
+ AFTER_RUN = (name === 'testAfterAsync') ? 2 : 1;
+ assert.pass('assert passed into after function');
+});
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-test-utils-sync.js b/addon-sdk/source/test/test-test-utils-sync.js
new file mode 100644
index 000000000..5c0fef56e
--- /dev/null
+++ b/addon-sdk/source/test/test-test-utils-sync.js
@@ -0,0 +1,84 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { defer: async } = require('sdk/lang/functional');
+const { before, after } = require('sdk/test/utils');
+const { resolve } = require('sdk/core/promise');
+
+var AFTER_RUN = 0;
+var BEFORE_RUN = 0;
+
+/*
+ * Tests are dependent on ordering, as the before and after functions
+ * are called outside of each test, and sometimes checked in the next test
+ * (like in the `after` tests)
+ */
+exports.testABeforeAsync = function (assert, done) {
+ assert.equal(BEFORE_RUN, 1, 'before function was called');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+ async(done)();
+};
+
+exports.testABeforeNameAsync = function (assert, done) {
+ assert.equal(BEFORE_RUN, 2, 'before function was called with name');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+ async(done)();
+};
+
+exports.testAfterAsync = function (assert, done) {
+ assert.equal(AFTER_RUN, 1, 'after function was called previously');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+ async(done)();
+};
+
+exports.testAfterNameAsync = function (assert, done) {
+ assert.equal(AFTER_RUN, 2, 'after function was called with name');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+ async(done)();
+};
+
+exports.testSyncABefore = function (assert) {
+ assert.equal(BEFORE_RUN, 1, 'before function was called for sync test');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+};
+
+exports.testSyncAfter = function (assert) {
+ assert.equal(AFTER_RUN, 1, 'after function was called for sync test');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+};
+
+exports.testGeneratorBefore = function*(assert) {
+ assert.equal(BEFORE_RUN, 1, 'before function was called for generator test');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+ yield resolve();
+}
+
+exports.testGeneratorAfter = function*(assert) {
+ assert.equal(AFTER_RUN, 1, 'after function was called for generator test');
+ BEFORE_RUN = 0;
+ AFTER_RUN = 0;
+ yield resolve();
+};
+
+before(exports, (name, assert) => {
+ BEFORE_RUN = (name === 'testABeforeNameAsync') ? 2 : 1;
+ assert.pass('assert passed into before function');
+});
+
+after(exports, (name, assert) => {
+ // testAfterName runs after testAfter, which is where this
+ // check occurs in the assertation
+ AFTER_RUN = (name === 'testAfterAsync') ? 2 : 1;
+ assert.pass('assert passed into after function');
+});
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-test-utils.js b/addon-sdk/source/test/test-test-utils.js
new file mode 100644
index 000000000..fe30d6756
--- /dev/null
+++ b/addon-sdk/source/test/test-test-utils.js
@@ -0,0 +1,81 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+'use strict'
+
+const { setTimeout } = require('sdk/timers');
+const { waitUntil, cleanUI } = require('sdk/test/utils');
+const tabs = require('sdk/tabs');
+const fixtures = require("./fixtures");
+const testURI = fixtures.url("test.html");
+
+exports.testWaitUntil = function (assert, done) {
+ let bool = false;
+ let finished = false;
+ waitUntil(() => {
+ if (finished)
+ assert.fail('interval should be cleared after predicate is truthy');
+ return bool;
+ }).then(function () {
+ assert.ok(bool,
+ 'waitUntil shouldn\'t call until predicate is truthy');
+ finished = true;
+ done();
+ });
+ setTimeout(() => { bool = true; }, 20);
+};
+
+exports.testWaitUntilInterval = function (assert, done) {
+ let bool = false;
+ let finished = false;
+ let counter = 0;
+ waitUntil(() => {
+ if (finished)
+ assert.fail('interval should be cleared after predicate is truthy');
+ counter++;
+ return bool;
+ }, 50).then(function () {
+ assert.ok(bool,
+ 'waitUntil shouldn\'t call until predicate is truthy');
+ assert.equal(counter, 1,
+ 'predicate should only be called once with a higher interval');
+ finished = true;
+ done();
+ });
+ setTimeout(() => { bool = true; }, 10);
+};
+
+exports.testCleanUIWithExtraTabAndWindow = function*(assert) {
+ let tab = yield new Promise(resolve => {
+ tabs.open({
+ url: testURI,
+ inNewWindow: true,
+ onReady: resolve
+ });
+ });
+
+ assert.equal(tabs.length, 2, 'there are two tabs open');
+
+ yield cleanUI()
+ assert.pass("the ui was cleaned");
+ assert.equal(tabs.length, 1, 'there is only one tab open');
+}
+
+exports.testCleanUIWithOnlyExtraTab = function*(assert) {
+ let tab = yield new Promise(resolve => {
+ tabs.open({
+ url: testURI,
+ inBackground: true,
+ onReady: resolve
+ });
+ });
+
+ assert.equal(tabs.length, 2, 'there are two tabs open');
+
+ yield cleanUI();
+ assert.pass("the ui was cleaned.");
+ assert.equal(tabs.length, 1, 'there is only one tab open');
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-text-streams.js b/addon-sdk/source/test/test-text-streams.js
new file mode 100644
index 000000000..c7691fd54
--- /dev/null
+++ b/addon-sdk/source/test/test-text-streams.js
@@ -0,0 +1,154 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const file = require("sdk/io/file");
+const { pathFor } = require("sdk/system");
+const { Loader } = require("sdk/test/loader");
+
+const STREAM_CLOSED_ERROR = new RegExp("The stream is closed and cannot be used.");
+
+// This should match the constant of the same name in text-streams.js.
+const BUFFER_BYTE_LEN = 0x8000;
+
+exports.testWriteRead = function (assert) {
+ let fname = dataFileFilename();
+
+ // Write a small string less than the stream's buffer size...
+ let str = "exports.testWriteRead data!";
+ let stream = file.open(fname, "w");
+ assert.ok(!stream.closed, "stream.closed after open should be false");
+ stream.write(str);
+ stream.close();
+ assert.ok(stream.closed, "stream.closed after close should be true");
+ assert.throws(() => stream.close(),
+ STREAM_CLOSED_ERROR,
+ "stream.close after already closed should raise error");
+ assert.throws(() => stream.write("This shouldn't be written!"),
+ STREAM_CLOSED_ERROR,
+ "stream.write after close should raise error");
+
+ // ... and read it.
+ stream = file.open(fname);
+ assert.ok(!stream.closed, "stream.closed after open should be false");
+ assert.equal(stream.read(), str,
+ "stream.read should return string written");
+ assert.equal(stream.read(), "",
+ "stream.read at EOS should return empty string");
+ stream.close();
+ assert.ok(stream.closed, "stream.closed after close should be true");
+ assert.throws(() => stream.close(),
+ STREAM_CLOSED_ERROR,
+ "stream.close after already closed should raise error");
+ assert.throws(() => stream.read(),
+ STREAM_CLOSED_ERROR,
+ "stream.read after close should raise error");
+
+ // Write a big string many times the size of the stream's buffer and read it.
+ // Since it comes after the previous test, this also ensures that the file is
+ // truncated when it's opened for writing.
+ str = "";
+ let bufLen = BUFFER_BYTE_LEN;
+ let fileSize = bufLen * 10;
+ for (let i = 0; i < fileSize; i++)
+ str += i % 10;
+ stream = file.open(fname, "w");
+ stream.write(str);
+ stream.close();
+ stream = file.open(fname);
+ assert.equal(stream.read(), str,
+ "stream.read should return string written");
+ stream.close();
+
+ // The same, but write and read in chunks.
+ stream = file.open(fname, "w");
+ let i = 0;
+ while (i < str.length) {
+ // Use a chunk length that spans buffers.
+ let chunk = str.substr(i, bufLen + 1);
+ stream.write(chunk);
+ i += bufLen + 1;
+ }
+ stream.close();
+ stream = file.open(fname);
+ let readStr = "";
+ bufLen = BUFFER_BYTE_LEN;
+ let readLen = bufLen + 1;
+ do {
+ var frag = stream.read(readLen);
+ readStr += frag;
+ } while (frag);
+ stream.close();
+ assert.equal(readStr, str,
+ "stream.write and read in chunks should work as expected");
+
+ // Read the same file, passing in strange numbers of bytes to read.
+ stream = file.open(fname);
+ assert.equal(stream.read(fileSize * 100), str,
+ "stream.read with big byte length should return string " +
+ "written");
+ stream.close();
+
+ stream = file.open(fname);
+ assert.equal(stream.read(0), "",
+ "string.read with zero byte length should return empty " +
+ "string");
+ stream.close();
+
+ stream = file.open(fname);
+ assert.equal(stream.read(-1), "",
+ "string.read with negative byte length should return " +
+ "empty string");
+ stream.close();
+
+ file.remove(fname);
+};
+
+exports.testWriteAsync = function (assert, done) {
+ let fname = dataFileFilename();
+ let str = "exports.testWriteAsync data!";
+ let stream = file.open(fname, "w");
+ assert.ok(!stream.closed, "stream.closed after open should be false");
+
+ // Write.
+ stream.writeAsync(str, function (err) {
+ assert.equal(this, stream, "|this| should be the stream object");
+ assert.equal(err, undefined,
+ "stream.writeAsync should not cause error");
+ assert.ok(stream.closed, "stream.closed after write should be true");
+ assert.throws(() => stream.close(),
+ STREAM_CLOSED_ERROR,
+ "stream.close after already closed should raise error");
+ assert.throws(() => stream.writeAsync("This shouldn't work!"),
+ STREAM_CLOSED_ERROR,
+ "stream.writeAsync after close should raise error");
+
+ // Read.
+ stream = file.open(fname, "r");
+ assert.ok(!stream.closed, "stream.closed after open should be false");
+ let readStr = stream.read();
+ assert.equal(readStr, str,
+ "string.read should yield string written");
+ stream.close();
+ file.remove(fname);
+ done();
+ });
+};
+
+exports.testUnload = function (assert) {
+ let loader = Loader(module);
+ let file = loader.require("sdk/io/file");
+
+ let filename = dataFileFilename("temp");
+ let stream = file.open(filename, "w");
+
+ loader.unload();
+ assert.ok(stream.closed, "stream should be closed after module unload");
+};
+
+// Returns the name of a file that should be used to test writing and reading.
+function dataFileFilename() {
+ return file.join(pathFor("ProfD"), "test-text-streams-data");
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-timer.js b/addon-sdk/source/test/test-timer.js
new file mode 100644
index 000000000..eed0fd047
--- /dev/null
+++ b/addon-sdk/source/test/test-timer.js
@@ -0,0 +1,229 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+var timer = require("sdk/timers");
+const { Loader } = require("sdk/test/loader");
+const { gc } = require("sdk/test/memory");
+const { all, defer } = require("sdk/core/promise");
+
+exports.testSetTimeout = function(assert, end) {
+ timer.setTimeout(function() {
+ assert.pass("testSetTimeout passed");
+ end();
+ }, 1);
+};
+
+exports.testSetTimeoutGC = function*(assert) {
+ let called = defer();
+ let gcDone = defer();
+
+ timer.setTimeout(called.resolve, 300);
+ gc().then(gcDone.resolve);
+
+ yield called.promise;
+ yield gcDone.promise;
+
+ assert.pass("setTimeout passed!");
+};
+
+exports.testParamedSetTimeout = function(assert, end) {
+ let params = [1, 'foo', { bar: 'test' }, null, undefined];
+ timer.setTimeout.apply(null, [function() {
+ assert.equal(arguments.length, params.length);
+ for (let i = 0, ii = params.length; i < ii; i++)
+ assert.equal(params[i], arguments[i]);
+ end();
+ }, 1].concat(params));
+};
+
+exports.testClearTimeout = function(assert, end) {
+ var myFunc = function myFunc() {
+ assert.fail("myFunc() should not be called in testClearTimeout");
+ };
+ var id = timer.setTimeout(myFunc, 1);
+ timer.setTimeout(function() {
+ assert.pass("testClearTimeout passed");
+ end();
+ }, 2);
+ timer.clearTimeout(id);
+};
+
+exports.testParamedClearTimeout = function(assert, end) {
+ let params = [1, 'foo', { bar: 'test' }, null, undefined];
+ var myFunc = function myFunc() {
+ assert.fail("myFunc() should not be called in testClearTimeout");
+ };
+ var id = timer.setTimeout(myFunc, 1);
+ timer.setTimeout.apply(null, [function() {
+ assert.equal(arguments.length, params.length);
+ for (let i = 0, ii = params.length; i < ii; i++)
+ assert.equal(params[i], arguments[i]);
+ end();
+ }, 1].concat(params));
+ timer.clearTimeout(id);
+};
+
+exports.testSetInterval = function (assert, end) {
+ var count = 0;
+ var id = timer.setInterval(function () {
+ count++;
+ if (count >= 5) {
+ timer.clearInterval(id);
+ assert.pass("testSetInterval passed");
+ end();
+ }
+ }, 1);
+};
+
+exports.testSetIntervalGC = function*(assert) {
+ let called = defer();
+ let gcDone = defer();
+ let done = false;
+ let count = 0;
+
+ let id = timer.setInterval(() => {
+ assert.pass("call count is " + count);
+
+ if (done) {
+ timer.clearInterval(id);
+ called.resolve();
+ return null;
+ }
+
+ if (count++ == 0) {
+ assert.pass("first call to setInterval worked!");
+
+ gc().then(() => {
+ assert.pass("gc is complete!");
+ done = true;
+ gcDone.resolve();
+ });
+
+ assert.pass("called gc()!");
+ }
+
+ return null;
+ }, 1);
+
+ yield gcDone.promise;
+ yield called.promise;
+
+ assert.pass("setInterval was called after the gc!");
+};
+
+exports.testParamedSetInerval = function(assert, end) {
+ let params = [1, 'foo', { bar: 'test' }, null, undefined];
+ let count = 0;
+ let id = timer.setInterval.apply(null, [function() {
+ count ++;
+ if (count < 5) {
+ assert.equal(arguments.length, params.length);
+ for (let i = 0, ii = params.length; i < ii; i++)
+ assert.equal(params[i], arguments[i]);
+ } else {
+ timer.clearInterval(id);
+ end();
+ }
+ }, 1].concat(params));
+};
+
+exports.testClearInterval = function (assert, end) {
+ timer.clearInterval(timer.setInterval(function () {
+ assert.fail("setInterval callback should not be called");
+ }, 1));
+ var id = timer.setInterval(function () {
+ timer.clearInterval(id);
+ assert.pass("testClearInterval passed");
+ end();
+ }, 2);
+};
+
+exports.testParamedClearInterval = function(assert, end) {
+ timer.clearInterval(timer.setInterval(function () {
+ assert.fail("setInterval callback should not be called");
+ }, 1, timer, {}, null));
+
+ let id = timer.setInterval(function() {
+ timer.clearInterval(id);
+ assert.equal(3, arguments.length);
+ end();
+ }, 2, undefined, 'test', {});
+};
+
+
+exports.testImmediate = function(assert, end) {
+ let actual = [];
+ let ticks = 0;
+ timer.setImmediate(function(...params) {
+ actual.push(params);
+ assert.equal(ticks, 1, "is a next tick");
+ assert.deepEqual(actual, [["start", "immediates"]]);
+ }, "start", "immediates");
+
+ timer.setImmediate(function(...params) {
+ actual.push(params);
+ assert.deepEqual(actual, [["start", "immediates"],
+ ["added"]]);
+ assert.equal(ticks, 1, "is a next tick");
+ timer.setImmediate(function(...params) {
+ actual.push(params);
+ assert.equal(ticks, 2, "is second tick");
+ assert.deepEqual(actual, [["start", "immediates"],
+ ["added"],
+ [],
+ ["last", "immediate", "handler"],
+ ["side-effect"]]);
+ end();
+ }, "side-effect");
+ }, "added");
+
+ timer.setImmediate(function(...params) {
+ actual.push(params);
+ assert.equal(ticks, 1, "is a next tick");
+ assert.deepEqual(actual, [["start", "immediates"],
+ ["added"],
+ []]);
+ timer.clearImmediate(removeID);
+ });
+
+ function removed() {
+ assert.fail("should be removed");
+ }
+ let removeID = timer.setImmediate(removed);
+
+ timer.setImmediate(function(...params) {
+ actual.push(params);
+ assert.equal(ticks, 1, "is a next tick");
+ assert.deepEqual(actual, [["start", "immediates"],
+ ["added"],
+ [],
+ ["last", "immediate", "handler"]]);
+ ticks = ticks + 1;
+ }, "last", "immediate", "handler");
+
+
+ ticks = ticks + 1;
+};
+
+exports.testUnload = function(assert, end) {
+ var loader = Loader(module);
+ var sbtimer = loader.require("sdk/timers");
+
+ var myFunc = function myFunc() {
+ assert.fail("myFunc() should not be called in testUnload");
+ };
+
+ sbtimer.setTimeout(myFunc, 1);
+ sbtimer.setTimeout(myFunc, 1, 'foo', 4, {}, undefined);
+ sbtimer.setInterval(myFunc, 1);
+ sbtimer.setInterval(myFunc, 1, {}, null, 'bar', undefined, 87);
+ loader.unload();
+ timer.setTimeout(function() {
+ assert.pass("timer testUnload passed");
+ end();
+ }, 2);
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-traceback.js b/addon-sdk/source/test/test-traceback.js
new file mode 100644
index 000000000..e6548b94e
--- /dev/null
+++ b/addon-sdk/source/test/test-traceback.js
@@ -0,0 +1,139 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+ "use strict";
+
+var traceback = require("sdk/console/traceback");
+var {Cc,Ci,Cr,Cu} = require("chrome");
+const { on, off } = require("sdk/system/events");
+
+function throwNsIException() {
+ var ios = Cc['@mozilla.org/network/io-service;1']
+ .getService(Ci.nsIIOService);
+ ios.newURI("i'm a malformed URI", null, null);
+}
+
+function throwError() {
+ throw new Error("foob");
+}
+
+exports.testFormatDoesNotFetchRemoteFiles = function(assert) {
+ ["http", "https"].forEach(
+ function(scheme) {
+ var httpRequests = 0;
+ function onHttp() {
+ httpRequests++;
+ }
+
+ on("http-on-modify-request", onHttp);
+
+ try {
+ var tb = [{filename: scheme + "://www.mozilla.org/",
+ lineNumber: 1,
+ name: "blah"}];
+ traceback.format(tb);
+ } catch (e) {
+ assert.fail(e);
+ }
+
+ off("http-on-modify-request", onHttp);
+
+ assert.equal(httpRequests, 0,
+ "traceback.format() does not make " +
+ scheme + " request");
+ });
+};
+
+exports.testFromExceptionWithString = function(assert) {
+ try {
+ throw "foob";
+ assert.fail("an exception should've been thrown");
+ } catch (e) {
+ if (e == "foob") {
+ var tb = traceback.fromException(e);
+ assert.equal(tb.length, 0);
+ }
+ else {
+ throw e;
+ }
+ }
+};
+
+exports.testFormatWithString = function(assert) {
+ // This can happen if e.g. a thrown exception was
+ // a string instead of an Error instance.
+ assert.equal(traceback.format("blah"),
+ "Traceback (most recent call last):");
+};
+
+exports.testFromExceptionWithError = function(assert) {
+ try {
+ throwError();
+ assert.fail("an exception should've been thrown");
+ } catch (e) {
+ if (e instanceof Error) {
+ var tb = traceback.fromException(e);
+
+ var xulApp = require("sdk/system/xul-app");
+ assert.equal(tb.slice(-1)[0].name, "throwError");
+ }
+ else {
+ throw e;
+ }
+ }
+};
+
+exports.testFromExceptionWithNsIException = function(assert) {
+ try {
+ throwNsIException();
+ assert.fail("an exception should've been thrown");
+ } catch (e) {
+ if (e.result == Cr.NS_ERROR_MALFORMED_URI) {
+ var tb = traceback.fromException(e);
+ assert.equal(tb[tb.length - 1].name, "throwNsIException");
+ }
+ else {
+ throw e;
+ }
+ }
+};
+
+exports.testFormat = function(assert) {
+ function getTraceback() {
+ return traceback.format();
+ }
+
+ var formatted = getTraceback();
+ assert.equal(typeof(formatted), "string");
+ var lines = formatted.split("\n");
+
+ assert.equal(lines[lines.length - 2].indexOf("getTraceback") > 0,
+ true,
+ "formatted traceback should include function name");
+
+ assert.equal(lines[lines.length - 1].trim(),
+ "return traceback.format();",
+ "formatted traceback should include source code");
+};
+
+exports.testExceptionsWithEmptyStacksAreLogged = function(assert) {
+ // Ensures that our fix to bug 550368 works.
+ var sandbox = Cu.Sandbox("http://www.foo.com");
+ var excRaised = false;
+ try {
+ Cu.evalInSandbox("returns 1 + 2;", sandbox, "1.8",
+ "blah.js", 25);
+ } catch (e) {
+ excRaised = true;
+ var stack = traceback.fromException(e);
+ assert.equal(stack.length, 1, "stack should have one frame");
+
+ assert.ok(stack[0].fileName, "blah.js", "frame should have filename");
+ assert.ok(stack[0].lineNumber, 25, "frame should have line no");
+ assert.equal(stack[0].name, null, "frame should have null function name");
+ }
+ if (!excRaised)
+ assert.fail("Exception should have been raised.");
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-ui-action-button.js b/addon-sdk/source/test/test-ui-action-button.js
new file mode 100644
index 000000000..264d546fb
--- /dev/null
+++ b/addon-sdk/source/test/test-ui-action-button.js
@@ -0,0 +1,1182 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+module.metadata = {
+ 'engines': {
+ 'Firefox': '> 28'
+ }
+};
+
+const { Cu } = require('chrome');
+const { Loader } = require('sdk/test/loader');
+const { data } = require('sdk/self');
+const { open, focus, close } = require('sdk/window/helpers');
+const { setTimeout } = require('sdk/timers');
+const { getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { partial } = require('sdk/lang/functional');
+const { wait } = require('./event/helpers');
+const { gc } = require('sdk/test/memory');
+const { emit, once } = require("sdk/event/core");
+
+const openBrowserWindow = partial(open, null, {features: {toolbar: true}});
+const openPrivateBrowserWindow = partial(open, null,
+ {features: {toolbar: true, private: true}});
+
+const badgeNodeFor = (node) =>
+ node.ownerDocument.getAnonymousElementByAttribute(node,
+ 'class', 'toolbarbutton-badge');
+
+function getWidget(buttonId, window = getMostRecentBrowserWindow()) {
+ const { CustomizableUI } = Cu.import('resource:///modules/CustomizableUI.jsm', {});
+ const { AREA_NAVBAR } = CustomizableUI;
+
+ let widgets = CustomizableUI.getWidgetIdsInArea(AREA_NAVBAR).
+ filter((id) => id.startsWith('action-button--') && id.endsWith(buttonId));
+
+ if (widgets.length === 0)
+ throw new Error('Widget with id `' + id +'` not found.');
+
+ if (widgets.length > 1)
+ throw new Error('Unexpected number of widgets: ' + widgets.length)
+
+ return CustomizableUI.getWidget(widgets[0]).forWindow(window);
+};
+
+exports['test basic constructor validation'] = function(assert) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+
+ assert.throws(
+ () => ActionButton({}),
+ /^The option/,
+ 'throws on no option given');
+
+ // Test no label
+ assert.throws(
+ () => ActionButton({ id: 'my-button', icon: './icon.png'}),
+ /^The option "label"/,
+ 'throws on no label given');
+
+ // Test no id
+ assert.throws(
+ () => ActionButton({ label: 'my button', icon: './icon.png' }),
+ /^The option "id"/,
+ 'throws on no id given');
+
+ // Test no icon
+ assert.throws(
+ () => ActionButton({ id: 'my-button', label: 'my button' }),
+ /^The option "icon"/,
+ 'throws on no icon given');
+
+
+ // Test empty label
+ assert.throws(
+ () => ActionButton({ id: 'my-button', label: '', icon: './icon.png' }),
+ /^The option "label"/,
+ 'throws on no valid label given');
+
+ // Test invalid id
+ assert.throws(
+ () => ActionButton({ id: 'my button', label: 'my button', icon: './icon.png' }),
+ /^The option "id"/,
+ 'throws on no valid id given');
+
+ // Test empty id
+ assert.throws(
+ () => ActionButton({ id: '', label: 'my button', icon: './icon.png' }),
+ /^The option "id"/,
+ 'throws on no valid id given');
+
+ // Test remote icon
+ assert.throws(
+ () => ActionButton({ id: 'my-button', label: 'my button', icon: 'http://www.mozilla.org/favicon.ico'}),
+ /^The option "icon"/,
+ 'throws on no valid icon given');
+
+ // Test wrong icon: no absolute URI to local resource, neither relative './'
+ assert.throws(
+ () => ActionButton({ id: 'my-button', label: 'my button', icon: 'icon.png'}),
+ /^The option "icon"/,
+ 'throws on no valid icon given');
+
+ // Test wrong icon: no absolute URI to local resource, neither relative './'
+ assert.throws(
+ () => ActionButton({ id: 'my-button', label: 'my button', icon: 'foo and bar'}),
+ /^The option "icon"/,
+ 'throws on no valid icon given');
+
+ // Test wrong icon: '../' is not allowed
+ assert.throws(
+ () => ActionButton({ id: 'my-button', label: 'my button', icon: '../icon.png'}),
+ /^The option "icon"/,
+ 'throws on no valid icon given');
+
+ assert.throws(
+ () => ActionButton({ id: 'my-button', label: 'button', icon: './i.png', badge: true}),
+ /^The option "badge"/,
+ 'throws on no valid badge given');
+
+ assert.throws(
+ () => ActionButton({ id: 'my-button', label: 'button', icon: './i.png', badgeColor: true}),
+ /^The option "badgeColor"/,
+ 'throws on no valid badge given');
+
+ loader.unload();
+};
+
+exports['test button added'] = function(assert) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+ let { data } = loader.require('sdk/self');
+
+ let button = ActionButton({
+ id: 'my-button-1',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ // check defaults
+ assert.equal(button.disabled, false,
+ 'disabled is set to default `false` value');
+
+ let { node } = getWidget(button.id);
+
+ assert.ok(!!node, 'The button is in the navbar');
+
+ assert.equal(button.label, node.getAttribute('label'),
+ 'label is set');
+
+ assert.equal(button.label, node.getAttribute('tooltiptext'),
+ 'tooltip is set');
+
+ assert.equal(data.url(button.icon.substr(2)), node.getAttribute('image'),
+ 'icon is set');
+
+ assert.equal("", node.getAttribute('badge'),
+ 'badge attribute is empty');
+
+ loader.unload();
+}
+
+exports['test button is not garbaged'] = function (assert, done) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+ let { data } = loader.require('sdk/self');
+
+ ActionButton({
+ id: 'my-button-1',
+ label: 'my button',
+ icon: './icon.png',
+ onClick: () => {
+ loader.unload();
+ done();
+ }
+ });
+
+ gc().then(() => {
+ let { node } = getWidget('my-button-1');
+
+ assert.ok(!!node, 'The button is in the navbar');
+
+ assert.equal('my button', node.getAttribute('label'),
+ 'label is set');
+
+ assert.equal(data.url('icon.png'), node.getAttribute('image'),
+ 'icon is set');
+
+ // ensure the listener is not gc'ed too
+ node.click();
+ }).catch(assert.fail);
+}
+
+exports['test button added with resource URI'] = function(assert) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+ let { data } = loader.require('sdk/self');
+
+ let button = ActionButton({
+ id: 'my-button-1',
+ label: 'my button',
+ icon: data.url('icon.png')
+ });
+
+ assert.equal(button.icon, data.url('icon.png'),
+ 'icon is set');
+
+ let { node } = getWidget(button.id);
+
+ assert.equal(button.icon, node.getAttribute('image'),
+ 'icon on node is set');
+
+ loader.unload();
+}
+
+exports['test button duplicate id'] = function(assert) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+
+ let button = ActionButton({
+ id: 'my-button-2',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ assert.throws(() => {
+ let doppelganger = ActionButton({
+ id: 'my-button-2',
+ label: 'my button',
+ icon: './icon.png'
+ });
+ },
+ /^The ID/,
+ 'No duplicates allowed');
+
+ loader.unload();
+}
+
+exports['test button multiple destroy'] = function(assert) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+
+ let button = ActionButton({
+ id: 'my-button-2',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ button.destroy();
+ button.destroy();
+ button.destroy();
+
+ assert.pass('multiple destroy doesn\'t matter');
+
+ loader.unload();
+}
+
+exports['test button removed on dispose'] = function(assert, done) {
+ const { CustomizableUI } = Cu.import('resource:///modules/CustomizableUI.jsm', {});
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+
+ let widgetId;
+
+ CustomizableUI.addListener({
+ onWidgetDestroyed: function(id) {
+ if (id === widgetId) {
+ CustomizableUI.removeListener(this);
+
+ assert.pass('button properly removed');
+ loader.unload();
+ done();
+ }
+ }
+ });
+
+ let button = ActionButton({
+ id: 'my-button-3',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ // Tried to use `getWidgetIdsInArea` but seems undefined, not sure if it
+ // was removed or it's not in the UX build yet
+ widgetId = getWidget(button.id).id;
+
+ button.destroy();
+};
+
+exports['test button global state updated'] = function(assert) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+ let { data } = loader.require("sdk/self");
+
+ let button = ActionButton({
+ id: 'my-button-4',
+ label: 'my button',
+ icon: './icon.png',
+ });
+ assert.pass('the button was created.');
+
+ // Tried to use `getWidgetIdsInArea` but seems undefined, not sure if it
+ // was removed or it's not in the UX build yet
+
+ let { node, id: widgetId } = getWidget(button.id);
+
+ // check read-only properties
+
+ assert.throws(() => button.id = 'another-id',
+ /^setting a property that has only a getter/,
+ 'id cannot be set at runtime');
+
+ assert.equal(button.id, 'my-button-4',
+ 'id is unchanged');
+ assert.equal(node.id, widgetId,
+ 'node id is unchanged');
+
+ // check writable properties
+
+ button.label = 'New label';
+ assert.equal(button.label, 'New label',
+ 'label is updated');
+ assert.equal(node.getAttribute('label'), 'New label',
+ 'node label is updated');
+ assert.equal(node.getAttribute('tooltiptext'), 'New label',
+ 'node tooltip is updated');
+
+ button.icon = './new-icon.png';
+ assert.equal(button.icon, './new-icon.png',
+ 'icon is updated');
+ assert.equal(node.getAttribute('image'), data.url('new-icon.png'),
+ 'node image is updated');
+
+ button.disabled = true;
+ assert.equal(button.disabled, true,
+ 'disabled is updated');
+ assert.equal(node.getAttribute('disabled'), 'true',
+ 'node disabled is updated');
+
+ button.badge = '+2';
+ button.badgeColor = 'blue';
+
+ assert.equal(button.badge, '+2',
+ 'badge is updated');
+ assert.equal(node.getAttribute('bagde'), '',
+ 'node badge is updated');
+
+ assert.equal(button.badgeColor, 'blue',
+ 'badgeColor is updated');
+ assert.equal(badgeNodeFor(node).style.backgroundColor, 'blue',
+ 'badge color is updated');
+
+ // TODO: test validation on update
+
+ loader.unload();
+}
+
+exports['test button global state set and get with state method'] = function(assert) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+
+ let button = ActionButton({
+ id: 'my-button-16',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ // read the button's state
+ let state = button.state(button);
+
+ assert.equal(state.label, 'my button',
+ 'label is correct');
+ assert.equal(state.icon, './icon.png',
+ 'icon is correct');
+ assert.equal(state.disabled, false,
+ 'disabled is correct');
+
+ // set the new button's state
+ button.state(button, {
+ label: 'New label',
+ icon: './new-icon.png',
+ disabled: true,
+ badge: '+2',
+ badgeColor: 'blue'
+ });
+
+ assert.equal(button.label, 'New label',
+ 'label is updated');
+ assert.equal(button.icon, './new-icon.png',
+ 'icon is updated');
+ assert.equal(button.disabled, true,
+ 'disabled is updated');
+ assert.equal(button.badge, '+2',
+ 'badge is updated');
+ assert.equal(button.badgeColor, 'blue',
+ 'badgeColor is updated');
+
+ loader.unload();
+}
+
+exports['test button global state updated on multiple windows'] = function*(assert) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+ let { data } = loader.require('sdk/self');
+
+ let button = ActionButton({
+ id: 'my-button-5',
+ label: 'my button',
+ icon: './icon.png'
+ });
+ assert.pass('the button was created');
+
+ let nodes = [ getWidget(button.id).node ];
+
+ let window = yield openBrowserWindow();
+ assert.pass('the window was created');
+
+ nodes.push(getWidget(button.id, window).node);
+
+ button.label = 'New label';
+ button.icon = './new-icon.png';
+ button.disabled = true;
+ button.badge = '+10';
+ button.badgeColor = 'green';
+
+ for (let node of nodes) {
+ assert.equal(node.getAttribute('label'), 'New label',
+ 'node label is updated');
+ assert.equal(node.getAttribute('tooltiptext'), 'New label',
+ 'node tooltip is updated');
+
+ assert.equal(button.icon, './new-icon.png',
+ 'icon is updated');
+ assert.equal(node.getAttribute('image'), data.url('new-icon.png'),
+ 'node image is updated');
+
+ assert.equal(button.disabled, true,
+ 'disabled is updated');
+ assert.equal(node.getAttribute('disabled'), 'true',
+ 'node disabled is updated');
+
+ assert.equal(button.badge, '+10',
+ 'badge is updated')
+ assert.equal(button.badgeColor, 'green',
+ 'badgeColor is updated')
+ assert.equal(node.getAttribute('badge'), '+10',
+ 'node badge is updated')
+ assert.equal(badgeNodeFor(node).style.backgroundColor, 'green',
+ 'node badge color is updated')
+ };
+
+ yield close(window);
+
+ loader.unload();
+};
+
+exports['test button window state'] = function*(assert) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+ let { browserWindows } = loader.require('sdk/windows');
+ let { data } = loader.require('sdk/self');
+
+ let button = ActionButton({
+ id: 'my-button-6',
+ label: 'my button',
+ icon: './icon.png',
+ badge: '+1',
+ badgeColor: 'red'
+ });
+
+ let mainWindow = browserWindows.activeWindow;
+ let nodes = [getWidget(button.id).node];
+
+ let window = yield openBrowserWindow().then(focus);
+
+ nodes.push(getWidget(button.id, window).node);
+
+ let { activeWindow } = browserWindows;
+
+ button.state(activeWindow, {
+ label: 'New label',
+ icon: './new-icon.png',
+ disabled: true,
+ badge: '+2',
+ badgeColor : 'green'
+ });
+
+ // check the states
+
+ assert.equal(button.label, 'my button',
+ 'global label unchanged');
+ assert.equal(button.icon, './icon.png',
+ 'global icon unchanged');
+ assert.equal(button.disabled, false,
+ 'global disabled unchanged');
+ assert.equal(button.badge, '+1',
+ 'global badge unchanged');
+ assert.equal(button.badgeColor, 'red',
+ 'global badgeColor unchanged');
+
+ let state = button.state(mainWindow);
+
+ assert.equal(state.label, 'my button',
+ 'previous window label unchanged');
+ assert.equal(state.icon, './icon.png',
+ 'previous window icon unchanged');
+ assert.equal(state.disabled, false,
+ 'previous window disabled unchanged');
+ assert.deepEqual(button.badge, '+1',
+ 'previouswindow badge unchanged');
+ assert.deepEqual(button.badgeColor, 'red',
+ 'previous window badgeColor unchanged');
+
+ state = button.state(activeWindow);
+
+ assert.equal(state.label, 'New label',
+ 'active window label updated');
+ assert.equal(state.icon, './new-icon.png',
+ 'active window icon updated');
+ assert.equal(state.disabled, true,
+ 'active disabled updated');
+ assert.equal(state.badge, '+2',
+ 'active badge updated');
+ assert.equal(state.badgeColor, 'green',
+ 'active badgeColor updated');
+
+ // change the global state, only the windows without a state are affected
+
+ button.label = 'A good label';
+ button.badge = '+3';
+
+ assert.equal(button.label, 'A good label',
+ 'global label updated');
+ assert.equal(button.state(mainWindow).label, 'A good label',
+ 'previous window label updated');
+ assert.equal(button.state(activeWindow).label, 'New label',
+ 'active window label unchanged');
+ assert.equal(button.state(activeWindow).badge, '+2',
+ 'active badge unchanged');
+ assert.equal(button.state(activeWindow).badgeColor, 'green',
+ 'active badgeColor unchanged');
+ assert.equal(button.state(mainWindow).badge, '+3',
+ 'previous window badge updated');
+ assert.equal(button.state(mainWindow).badgeColor, 'red',
+ 'previous window badgeColor unchanged');
+
+ // delete the window state will inherits the global state again
+
+ button.state(activeWindow, null);
+
+ state = button.state(activeWindow);
+
+ assert.equal(state.label, 'A good label',
+ 'active window label inherited');
+ assert.equal(state.badge, '+3',
+ 'previous window badge inherited');
+ assert.equal(button.badgeColor, 'red',
+ 'previous window badgeColor inherited');
+
+ // check the nodes properties
+ let node = nodes[0];
+
+ state = button.state(mainWindow);
+
+ assert.equal(node.getAttribute('label'), state.label,
+ 'node label is correct');
+ assert.equal(node.getAttribute('tooltiptext'), state.label,
+ 'node tooltip is correct');
+
+ assert.equal(node.getAttribute('image'), data.url(state.icon.substr(2)),
+ 'node image is correct');
+ assert.equal(node.hasAttribute('disabled'), state.disabled,
+ 'disabled is correct');
+ assert.equal(node.getAttribute("badge"), state.badge,
+ 'badge is correct');
+
+ assert.equal(badgeNodeFor(node).style.backgroundColor, state.badgeColor,
+ 'badge color is correct');
+
+ node = nodes[1];
+ state = button.state(activeWindow);
+
+ assert.equal(node.getAttribute('label'), state.label,
+ 'node label is correct');
+ assert.equal(node.getAttribute('tooltiptext'), state.label,
+ 'node tooltip is correct');
+
+ assert.equal(node.getAttribute('image'), data.url(state.icon.substr(2)),
+ 'node image is correct');
+ assert.equal(node.hasAttribute('disabled'), state.disabled,
+ 'disabled is correct');
+ assert.equal(node.getAttribute('badge'), state.badge,
+ 'badge is correct');
+
+ assert.equal(badgeNodeFor(node).style.backgroundColor, state.badgeColor,
+ 'badge color is correct');
+
+ yield close(window);
+
+ loader.unload();
+};
+
+
+exports['test button tab state'] = function*(assert) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+ let { browserWindows } = loader.require('sdk/windows');
+ let { data } = loader.require('sdk/self');
+ let tabs = loader.require('sdk/tabs');
+
+ let button = ActionButton({
+ id: 'my-button-7',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ let mainTab = tabs.activeTab;
+ let node = getWidget(button.id).node;
+
+ tabs.open('about:blank');
+
+ yield wait(tabs, 'ready');
+
+ let tab = tabs.activeTab;
+ let { activeWindow } = browserWindows;
+
+ // set window state
+ button.state(activeWindow, {
+ label: 'Window label',
+ icon: './window-icon.png',
+ badge: 'win',
+ badgeColor: 'blue'
+ });
+
+ // set previous active tab state
+ button.state(mainTab, {
+ label: 'Tab label',
+ icon: './tab-icon.png',
+ badge: 'tab',
+ badgeColor: 'red'
+ });
+
+ // set current active tab state
+ button.state(tab, {
+ icon: './another-tab-icon.png',
+ disabled: true,
+ badge: 't1',
+ badgeColor: 'green'
+ });
+
+ // check the states, be sure they won't be gc'ed
+ yield gc();
+
+ assert.equal(button.label, 'my button',
+ 'global label unchanged');
+ assert.equal(button.icon, './icon.png',
+ 'global icon unchanged');
+ assert.equal(button.disabled, false,
+ 'global disabled unchanged');
+ assert.equal(button.badge, undefined,
+ 'global badge unchanged')
+
+ let state = button.state(mainTab);
+
+ assert.equal(state.label, 'Tab label',
+ 'previous tab label updated');
+ assert.equal(state.icon, './tab-icon.png',
+ 'previous tab icon updated');
+ assert.equal(state.disabled, false,
+ 'previous tab disabled unchanged');
+ assert.equal(state.badge, 'tab',
+ 'previous tab badge unchanged')
+ assert.equal(state.badgeColor, 'red',
+ 'previous tab badgeColor unchanged')
+
+ state = button.state(tab);
+
+ assert.equal(state.label, 'Window label',
+ 'active tab inherited from window state');
+ assert.equal(state.icon, './another-tab-icon.png',
+ 'active tab icon updated');
+ assert.equal(state.disabled, true,
+ 'active disabled updated');
+ assert.equal(state.badge, 't1',
+ 'active badge updated');
+ assert.equal(state.badgeColor, 'green',
+ 'active badgeColor updated');
+
+ // change the global state
+ button.icon = './good-icon.png';
+
+ // delete the tab state
+ button.state(tab, null);
+
+ assert.equal(button.icon, './good-icon.png',
+ 'global icon updated');
+ assert.equal(button.state(mainTab).icon, './tab-icon.png',
+ 'previous tab icon unchanged');
+ assert.equal(button.state(tab).icon, './window-icon.png',
+ 'tab icon inherited from window');
+ assert.equal(button.state(mainTab).badge, 'tab',
+ 'previous tab badge is unchaged');
+ assert.equal(button.state(tab).badge, 'win',
+ 'tab badge is inherited from window');
+
+ // delete the window state
+ button.state(activeWindow, null);
+
+ state = button.state(tab);
+
+ assert.equal(state.icon, './good-icon.png',
+ 'tab icon inherited from global');
+ assert.equal(state.badge, undefined,
+ 'tab badge inherited from global');
+ assert.equal(state.badgeColor, undefined,
+ 'tab badgeColor inherited from global');
+
+ // check the node properties
+ yield new Promise(resolve => {
+ let target = {};
+ once(target, "ready", resolve);
+ emit(target, "ready");
+ });
+
+ assert.equal(node.getAttribute('label'), state.label,
+ 'node label is correct');
+ assert.equal(node.getAttribute('tooltiptext'), state.label,
+ 'node tooltip is correct');
+ assert.equal(node.getAttribute('image'), data.url(state.icon.substr(2)),
+ 'node image is correct');
+ assert.equal(node.hasAttribute('disabled'), state.disabled,
+ 'node disabled is correct');
+ assert.equal(node.getAttribute('badge'), '',
+ 'badge text is correct');
+ assert.equal(badgeNodeFor(node).style.backgroundColor, '',
+ 'badge color is correct');
+
+ mainTab.activate();
+
+ yield wait(tabs, 'activate');
+
+ // This is made in order to avoid to check the node before it
+ // is updated, need a better check
+ yield new Promise(resolve => {
+ let target = {};
+ once(target, "ready", resolve);
+ emit(target, "ready");
+ });
+
+ state = button.state(mainTab);
+
+ assert.equal(node.getAttribute('label'), state.label,
+ 'node label is correct');
+ assert.equal(node.getAttribute('tooltiptext'), state.label,
+ 'node tooltip is correct');
+ assert.equal(node.getAttribute('image'), data.url(state.icon.substr(2)),
+ 'node image is correct');
+ assert.equal(node.hasAttribute('disabled'), state.disabled,
+ 'disabled is correct');
+ assert.equal(node.getAttribute('badge'), state.badge,
+ 'badge text is correct');
+ assert.equal(badgeNodeFor(node).style.backgroundColor, state.badgeColor,
+ 'badge color is correct');
+
+ tab.close(loader.unload);
+
+ loader.unload();
+};
+
+exports['test button click'] = function*(assert) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+ let { browserWindows } = loader.require('sdk/windows');
+
+ let labels = [];
+
+ let button = ActionButton({
+ id: 'my-button-8',
+ label: 'my button',
+ icon: './icon.png',
+ onClick: ({label}) => labels.push(label)
+ });
+
+ let mainWindow = browserWindows.activeWindow;
+ let chromeWindow = getMostRecentBrowserWindow();
+
+ let window = yield openBrowserWindow().then(focus);
+
+ button.state(mainWindow, { label: 'nothing' });
+ button.state(mainWindow.tabs.activeTab, { label: 'foo'})
+ button.state(browserWindows.activeWindow, { label: 'bar' });
+
+ button.click();
+
+ yield focus(chromeWindow);
+
+ button.click();
+
+ assert.deepEqual(labels, ['bar', 'foo'],
+ 'button click works');
+
+ yield close(window);
+
+ loader.unload();
+}
+
+exports['test button icon set'] = function(assert) {
+ const { CustomizableUI } = Cu.import('resource:///modules/CustomizableUI.jsm', {});
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+ let { data } = loader.require('sdk/self');
+
+ // Test remote icon set
+ assert.throws(
+ () => ActionButton({
+ id: 'my-button-10',
+ label: 'my button',
+ icon: {
+ '16': 'http://www.mozilla.org/favicon.ico'
+ }
+ }),
+ /^The option "icon"/,
+ 'throws on no valid icon given');
+
+ let button = ActionButton({
+ id: 'my-button-11',
+ label: 'my button',
+ icon: {
+ '5': './icon5.png',
+ '16': './icon16.png',
+ '32': './icon32.png',
+ '64': './icon64.png'
+ }
+ });
+
+ let { node, id: widgetId } = getWidget(button.id);
+ let { devicePixelRatio } = node.ownerDocument.defaultView;
+
+ let size = 16 * devicePixelRatio;
+
+ assert.equal(node.getAttribute('image'), data.url(button.icon[size].substr(2)),
+ 'the icon is set properly in navbar');
+
+ size = 32 * devicePixelRatio;
+
+ CustomizableUI.addWidgetToArea(widgetId, CustomizableUI.AREA_PANEL);
+
+ assert.equal(node.getAttribute('image'), data.url(button.icon[size].substr(2)),
+ 'the icon is set properly in panel');
+
+ // Using `loader.unload` without move back the button to the original area
+ // raises an error in the CustomizableUI. This is doesn't happen if the
+ // button is moved manually from navbar to panel. I believe it has to do
+ // with `addWidgetToArea` method, because even with a `timeout` the issue
+ // persist.
+ CustomizableUI.addWidgetToArea(widgetId, CustomizableUI.AREA_NAVBAR);
+
+ loader.unload();
+}
+
+exports['test button icon set with only one option'] = function(assert) {
+ const { CustomizableUI } = Cu.import('resource:///modules/CustomizableUI.jsm', {});
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+ let { data } = loader.require('sdk/self');
+
+ // Test remote icon set
+ assert.throws(
+ () => ActionButton({
+ id: 'my-button-10',
+ label: 'my button',
+ icon: {
+ '16': 'http://www.mozilla.org/favicon.ico'
+ }
+ }),
+ /^The option "icon"/,
+ 'throws on no valid icon given');
+
+ let button = ActionButton({
+ id: 'my-button-11',
+ label: 'my button',
+ icon: {
+ '5': './icon5.png'
+ }
+ });
+
+ let { node, id: widgetId } = getWidget(button.id);
+
+ assert.equal(node.getAttribute('image'), data.url(button.icon['5'].substr(2)),
+ 'the icon is set properly in navbar');
+
+ CustomizableUI.addWidgetToArea(widgetId, CustomizableUI.AREA_PANEL);
+
+ assert.equal(node.getAttribute('image'), data.url(button.icon['5'].substr(2)),
+ 'the icon is set properly in panel');
+
+ // Using `loader.unload` without move back the button to the original area
+ // raises an error in the CustomizableUI. This is doesn't happen if the
+ // button is moved manually from navbar to panel. I believe it has to do
+ // with `addWidgetToArea` method, because even with a `timeout` the issue
+ // persist.
+ CustomizableUI.addWidgetToArea(widgetId, CustomizableUI.AREA_NAVBAR);
+
+ loader.unload();
+}
+
+exports['test button state validation'] = function(assert) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+ let { browserWindows } = loader.require('sdk/windows');
+
+ let button = ActionButton({
+ id: 'my-button-12',
+ label: 'my button',
+ icon: './icon.png'
+ })
+
+ let state = button.state(button);
+
+ assert.throws(
+ () => button.state(button, { icon: 'http://www.mozilla.org/favicon.ico' }),
+ /^The option "icon"/,
+ 'throws on remote icon given');
+
+ assert.throws(
+ () => button.state(button, { badge: true } ),
+ /^The option "badge"/,
+ 'throws on wrong badge value given');
+
+ loader.unload();
+};
+
+exports['test button are not in private windows'] = function(assert, done) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+ let{ isPrivate } = loader.require('sdk/private-browsing');
+ let { browserWindows } = loader.require('sdk/windows');
+
+ let button = ActionButton({
+ id: 'my-button-13',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ openPrivateBrowserWindow().then(window => {
+ assert.ok(isPrivate(window),
+ 'the new window is private');
+
+ let { node } = getWidget(button.id, window);
+
+ assert.ok(!node || node.style.display === 'none',
+ 'the button is not added / is not visible on private window');
+
+ return window;
+ }).
+ then(close).
+ then(loader.unload).
+ then(done, assert.fail)
+}
+
+exports['test button state are snapshot'] = function(assert) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+ let { browserWindows } = loader.require('sdk/windows');
+ let tabs = loader.require('sdk/tabs');
+
+ let button = ActionButton({
+ id: 'my-button-14',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ let state = button.state(button);
+ let windowState = button.state(browserWindows.activeWindow);
+ let tabState = button.state(tabs.activeTab);
+
+ assert.deepEqual(windowState, state,
+ 'window state has the same properties of button state');
+
+ assert.deepEqual(tabState, state,
+ 'tab state has the same properties of button state');
+
+ assert.notEqual(windowState, state,
+ 'window state is not the same object of button state');
+
+ assert.notEqual(tabState, state,
+ 'tab state is not the same object of button state');
+
+ assert.deepEqual(button.state(button), state,
+ 'button state has the same content of previous button state');
+
+ assert.deepEqual(button.state(browserWindows.activeWindow), windowState,
+ 'window state has the same content of previous window state');
+
+ assert.deepEqual(button.state(tabs.activeTab), tabState,
+ 'tab state has the same content of previous tab state');
+
+ assert.notEqual(button.state(button), state,
+ 'button state is not the same object of previous button state');
+
+ assert.notEqual(button.state(browserWindows.activeWindow), windowState,
+ 'window state is not the same object of previous window state');
+
+ assert.notEqual(button.state(tabs.activeTab), tabState,
+ 'tab state is not the same object of previous tab state');
+
+ loader.unload();
+}
+
+exports['test button icon object is a snapshot'] = function(assert) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+
+ let icon = {
+ '16': './foo.png'
+ };
+
+ let button = ActionButton({
+ id: 'my-button-17',
+ label: 'my button',
+ icon: icon
+ });
+
+ assert.deepEqual(button.icon, icon,
+ 'button.icon has the same properties of the object set in the constructor');
+
+ assert.notEqual(button.icon, icon,
+ 'button.icon is not the same object of the object set in the constructor');
+
+ assert.throws(
+ () => button.icon[16] = './bar.png',
+ /16 is read-only/,
+ 'properties of button.icon are ready-only'
+ );
+
+ let newIcon = {'16': './bar.png'};
+ button.icon = newIcon;
+
+ assert.deepEqual(button.icon, newIcon,
+ 'button.icon has the same properties of the object set');
+
+ assert.notEqual(button.icon, newIcon,
+ 'button.icon is not the same object of the object set');
+
+ loader.unload();
+}
+
+exports['test button after destroy'] = function(assert) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+ let { browserWindows } = loader.require('sdk/windows');
+ let { activeTab } = loader.require('sdk/tabs');
+
+ let button = ActionButton({
+ id: 'my-button-15',
+ label: 'my button',
+ icon: './icon.png',
+ onClick: () => assert.fail('onClick should not be called')
+ });
+
+ button.destroy();
+
+ assert.throws(
+ () => button.click(),
+ /^The state cannot be set or get/,
+ 'button.click() not executed');
+
+ assert.throws(
+ () => button.label,
+ /^The state cannot be set or get/,
+ 'button.label cannot be get after destroy');
+
+ assert.throws(
+ () => button.label = 'my label',
+ /^The state cannot be set or get/,
+ 'button.label cannot be set after destroy');
+
+ assert.throws(
+ () => {
+ button.state(browserWindows.activeWindow, {
+ label: 'window label'
+ });
+ },
+ /^The state cannot be set or get/,
+ 'window state label cannot be set after destroy');
+
+ assert.throws(
+ () => button.state(browserWindows.activeWindow).label,
+ /^The state cannot be set or get/,
+ 'window state label cannot be get after destroy');
+
+ assert.throws(
+ () => {
+ button.state(activeTab, {
+ label: 'tab label'
+ });
+ },
+ /^The state cannot be set or get/,
+ 'tab state label cannot be set after destroy');
+
+ assert.throws(
+ () => button.state(activeTab).label,
+ /^The state cannot be set or get/,
+ 'window state label cannot se get after destroy');
+
+ loader.unload();
+};
+
+exports['test button badge property'] = function(assert) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+
+ let button = ActionButton({
+ id: 'my-button-18',
+ label: 'my button',
+ icon: './icon.png',
+ badge: 123456
+ });
+
+ assert.equal(button.badge, 123456,
+ 'badge is set');
+
+ assert.equal(button.badgeColor, undefined,
+ 'badge color is not set');
+
+ let { node } = getWidget(button.id);
+ let { getComputedStyle } = node.ownerDocument.defaultView;
+ let badgeNode = badgeNodeFor(node);
+
+ assert.equal('1234', node.getAttribute('badge'),
+ 'badge text is displayed up to four characters');
+
+ assert.equal(getComputedStyle(badgeNode).backgroundColor, 'rgb(217, 0, 0)',
+ 'badge color is the default one');
+
+ button.badge = '危機';
+
+ assert.equal(button.badge, '危機',
+ 'badge is properly set');
+
+ assert.equal('危機', node.getAttribute('badge'),
+ 'badge text is displayed');
+
+ button.badge = '🐶🐰🐹';
+
+ assert.equal(button.badge, '🐶🐰🐹',
+ 'badge is properly set');
+
+ assert.equal('🐶🐰🐹', node.getAttribute('badge'),
+ 'badge text is displayed');
+
+ loader.unload();
+}
+exports['test button badge color'] = function(assert) {
+ let loader = Loader(module);
+ let { ActionButton } = loader.require('sdk/ui');
+
+ let button = ActionButton({
+ id: 'my-button-19',
+ label: 'my button',
+ icon: './icon.png',
+ badge: '+1',
+ badgeColor: 'blue'
+ });
+
+ assert.equal(button.badgeColor, 'blue',
+ 'badge color is set');
+
+ let { node } = getWidget(button.id);
+ let { getComputedStyle } = node.ownerDocument.defaultView;
+ let badgeNode = badgeNodeFor(node);
+
+ assert.equal(badgeNodeFor(node).style.backgroundColor, 'blue',
+ 'badge color is displayed properly');
+ assert.equal(getComputedStyle(badgeNode).backgroundColor, 'rgb(0, 0, 255)',
+ 'badge color overrides the default one');
+
+ loader.unload();
+}
+
+require("sdk/test").run(module.exports);
diff --git a/addon-sdk/source/test/test-ui-frame.js b/addon-sdk/source/test/test-ui-frame.js
new file mode 100644
index 000000000..05b40edd5
--- /dev/null
+++ b/addon-sdk/source/test/test-ui-frame.js
@@ -0,0 +1,252 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+module.metadata = {
+ "engines": {
+ "Firefox": "*"
+ }
+};
+
+const { Cu } = require("chrome");
+const { Frame } = require("sdk/ui/frame");
+const { Toolbar } = require("sdk/ui/toolbar");
+const { Loader } = require("sdk/test/loader");
+const { identify } = require("sdk/ui/id");
+const { setTimeout } = require("sdk/timers");
+const { getMostRecentBrowserWindow, open } = require("sdk/window/utils");
+const { ready, loaded, close } = require("sdk/window/helpers");
+const { defer, all } = require("sdk/core/promise");
+const { send } = require("sdk/event/utils");
+const { object } = require("sdk/util/sequence");
+const { OutputPort } = require("sdk/output/system");
+const { Task } = Cu.import("resource://gre/modules/Task.jsm", {});
+const output = new OutputPort({ id: "toolbar-change" });
+
+const wait = (toolbar, event, times) => {
+ let { promise, resolve } = defer();
+ if (times) {
+ let resolveArray = [];
+ let counter = 0;
+ toolbar.on(event, function onEvent (e) {
+ resolveArray.push(e);
+ if (++counter === times) {
+ toolbar.off(event, onEvent);
+ resolve(resolveArray);
+ }
+ });
+ }
+ else {
+ toolbar.once(event, resolve);
+ }
+ return promise;
+};
+
+const stateEventsFor = frame =>
+ [wait(frame, "attach"), wait(frame, "ready"),
+ wait(frame, "load"), wait(frame, "detach")];
+
+
+const isAttached = ({id}, window=getMostRecentBrowserWindow()) =>
+ !!window.document.getElementById(id);
+
+// Use `Task.spawn` instead of `Task.async` because the returned function does not contain
+// a length for the test harness to determine whether the test should be executed
+exports["test frame API"] = function* (assert) {
+ const url = "data:text/html,frame-api";
+ assert.throws(() => new Frame(),
+ /The `options.url`/,
+ "must provide url");
+
+ assert.throws(() => new Frame({ url: "http://mozilla.org/" }),
+ /The `options.url`/,
+ "options.url must be local url");
+
+ assert.throws(() => new Frame({url: url, name: "4you" }),
+ /The `option.name` must be a valid/,
+ "can only take valid names");
+
+ const f1 = new Frame({ url: url });
+
+ assert.ok(f1.id, "frame has an id");
+ assert.equal(f1.url, void(0), "frame has no url until it's loaded");
+ assert.equal(typeof(f1.postMessage), "function",
+ "frames can postMessages");
+
+ const p1 = wait(f1, "register");
+
+ assert.throws(() => new Frame({ url: url }),
+ /Frame with this id already exists/,
+ "can't have two identical frames");
+
+
+ const f2 = new Frame({ name: "frame-2", url: url });
+ assert.pass("can create frame with same url but diff name");
+ const p2 = wait(f2, "register");
+
+ yield p1;
+ assert.pass("frame#1 was registered");
+ assert.equal(f1.url, url, "once registered it get's url");
+
+ yield p2;
+ assert.pass("frame#2 was registered");
+ assert.equal(f2.url, url, "once registered it get's url");
+
+ f1.destroy();
+ const f3 = new Frame({ url: url });
+ assert.pass("frame identical to destroyed one can be created");
+
+ yield wait(f3, "register");
+ assert.equal(f3.url, url, "url is set");
+ f2.destroy();
+ f3.destroy();
+};
+
+exports["test frame in toolbar"] = function* (assert) {
+ const assertEvent = (event, type) => {
+ assert.ok(event, "`" + type + "` event was dispatched");
+ assert.equal(event.type, type, "event.type is: " + type);
+ assert.equal(typeof(event.source), "object",
+ "event.source is an object");
+ assert.equal(typeof(event.source.postMessage), "function",
+ "messages can be posted to event.source");
+ };
+
+
+ const url = "data:text/html,toolbar-frame";
+ const f1 = new Frame({ url: url });
+ const t1 = new Toolbar({
+ title: "frame toolbar",
+ items: [f1]
+ });
+
+ const w1 = getMostRecentBrowserWindow();
+ const [a1, r1, l1] = stateEventsFor(f1);
+
+ assertEvent((yield a1), "attach");
+ assert.ok(isAttached(f1, w1), "frame is in the window#1");
+ assertEvent((yield r1), "ready");
+ assertEvent((yield l1), "load");
+
+ const [a2, r2, l2] = stateEventsFor(f1);
+ const w2 = open();
+
+ assertEvent((yield a2), "attach");
+ assert.ok(isAttached(f1, w2), "frame is in the window#2");
+ assertEvent((yield r2), "ready");
+ assertEvent((yield l2), "load");
+ assert.pass("frame attached to window#2");
+
+
+ const d1 = wait(f1, "detach");
+ yield close(w2);
+ assertEvent((yield d1), "detach");
+ assert.pass("frame detached when window is closed");
+
+ t1.destroy();
+
+ assertEvent((yield wait(f1, "detach")), "detach");
+ assert.ok(!isAttached(f1, w1), "frame was removed from window#1");
+ assert.pass("toolbar destroy detaches frame");
+};
+
+
+exports["test host to content messaging"] = function* (assert) {
+ const url = "data:text/html,<script>new " + function() {
+ window.addEventListener("message", (event) => {
+ if (event.data === "ping!")
+ event.source.postMessage("pong!", event.origin);
+ });
+ } + "</script>";
+ const f1 = new Frame({ name: "mailbox", url: url });
+ const t1 = new Toolbar({ title: "mailbox", items: [f1] });
+
+ const e1 = yield wait(f1, "ready");
+ e1.source.postMessage("ping!", e1.origin);
+
+ const pong = yield wait(f1, "message");
+ assert.equal(pong.data, "pong!", "received ping back");
+ t1.destroy();
+
+ yield wait(t1, "detach");
+};
+
+
+exports["test content to host messaging"] = function* (assert) {
+ const url = "data:text/html,<script>new " + function() {
+ window.addEventListener("message", (event) => {
+ if (event.data === "pong!")
+ event.source.postMessage("end", event.origin);
+ });
+
+ window.parent.postMessage("ping!", "*");
+ } + "</script>";
+
+ const f1 = new Frame({ name: "inbox", url: url });
+ const t1 = new Toolbar({ title: "inbox", items: [f1] });
+
+ const e1 = yield wait(f1, "message");
+ assert.equal(e1.data, "ping!", "received ping from content");
+
+ e1.source.postMessage("pong!", e1.origin);
+
+ const e2 = yield wait(f1, "message");
+ assert.equal(e2.data, "end", "received end message");
+
+ t1.destroy();
+ yield wait(t1, "detach");
+
+};
+
+
+exports["test direct messaging"] = function* (assert) {
+ let message;
+ const url = "data:text/html,<script>new " + function() {
+ var n = 0;
+ window.addEventListener("message", (event) => {
+ if (event.data === "inc")
+ n = n + 1;
+ if (event.data === "print")
+ event.source.postMessage({ n: n }, event.origin);
+ });
+ } + "</script>";
+
+ const w1 = getMostRecentBrowserWindow();
+ const f1 = new Frame({ url: url, name: "mail-cluster" });
+ const t1 = new Toolbar({ title: "claster", items: [f1] });
+
+ yield wait(f1, "ready");
+ assert.pass("document loaded in window#1");
+
+ const w2 = open();
+
+ yield wait(f1, "ready");
+ assert.pass("document loaded in window#2");
+
+ let messages = wait(f1, "message", 2);
+ f1.postMessage("inc", f1.origin);
+ f1.postMessage("print", f1.origin);
+
+ const [e1, e2] = yield messages;
+ assert.deepEqual(e1.data, {n: 1}, "received message from window#1");
+ assert.deepEqual(e2.data, {n: 1}, "received message from window#2");
+
+ message = wait(f1, "message");
+ e1.source.postMessage("inc", e1.origin);
+ e1.source.postMessage("print", e1.origin);
+ const e3 = yield message;
+ assert.deepEqual(e3.data, {n: 2}, "state changed in window#1");
+
+ message = wait(f1, "message");
+ e2.source.postMessage("print", e2.origin);
+ yield message;
+ assert.deepEqual(e2.data, {n:1}, "window#2 didn't received inc message");
+
+ yield close(w2);
+ t1.destroy();
+
+ yield wait(t1, "detach");
+};
+
+require("sdk/test").run(module.exports);
diff --git a/addon-sdk/source/test/test-ui-id.js b/addon-sdk/source/test/test-ui-id.js
new file mode 100644
index 000000000..881259af6
--- /dev/null
+++ b/addon-sdk/source/test/test-ui-id.js
@@ -0,0 +1,43 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { identify } = require('sdk/ui/id');
+const { Class } = require('sdk/core/heritage');
+
+const signature = /{[0-9a-f\-]+}/;
+
+exports['test generate id'] = function(assert) {
+ let first = identify({});
+ let second = identify({});
+
+ assert.ok(signature.test(first), 'first id has a correct signature');
+ assert.ok(signature.test(second), 'second id has a correct signature');
+
+ assert.notEqual(first, identify({}), 'identify generated new uuid [1]');
+ assert.notEqual(first, second, 'identify generated new uuid [2]');
+};
+
+exports['test get id'] = function(assert) {
+ let thing = {};
+ let thingID = identify(thing);
+
+ assert.equal(thingID, identify(thing), 'ids for things are cached by default');
+ assert.notEqual(identify(thing), identify({}), 'new ids for new things');
+};
+
+exports['test custom id definition for classes'] = function(assert) {
+ let Thing = Class({});
+ let thing = Thing();
+ let counter = 0;
+
+ identify.define(Thing, function(thing) {
+ return ++counter;
+ });
+
+ assert.equal(identify(thing), counter, 'it is possible to define custom identifications');
+ assert.ok(signature.test(identify({})), 'defining a custom identification does not affect the default behavior');
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-ui-sidebar-private-browsing.js b/addon-sdk/source/test/test-ui-sidebar-private-browsing.js
new file mode 100644
index 000000000..52c4ec4b5
--- /dev/null
+++ b/addon-sdk/source/test/test-ui-sidebar-private-browsing.js
@@ -0,0 +1,203 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+module.metadata = {
+ 'engines': {
+ 'Firefox': '*'
+ }
+};
+
+const { Loader } = require('sdk/test/loader');
+const { show, hide } = require('sdk/ui/sidebar/actions');
+const { isShowing } = require('sdk/ui/sidebar/utils');
+const { getMostRecentBrowserWindow, isWindowPrivate } = require('sdk/window/utils');
+const { open, close, focus, promise: windowPromise } = require('sdk/window/helpers');
+const { setTimeout } = require('sdk/timers');
+const { isPrivate } = require('sdk/private-browsing');
+const { data } = require('sdk/self');
+const { URL } = require('sdk/url');
+
+const { BUILTIN_SIDEBAR_MENUITEMS, isSidebarShowing,
+ getSidebarMenuitems, getExtraSidebarMenuitems, makeID, simulateCommand,
+ simulateClick, isChecked } = require('./sidebar/utils');
+
+exports.testSideBarIsNotInNewPrivateWindows = function(assert, done) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testSideBarIsNotInNewPrivateWindows';
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+testName
+ });
+
+ let startWindow = getMostRecentBrowserWindow();
+ let ele = startWindow.document.getElementById(makeID(testName));
+ assert.ok(ele, 'sidebar element was added');
+
+ open(null, { features: { private: true } }).then(function(window) {
+ let ele = window.document.getElementById(makeID(testName));
+ assert.ok(isPrivate(window), 'the new window is private');
+ assert.equal(ele, null, 'sidebar element was not added');
+
+ sidebar.destroy();
+ assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE');
+ assert.ok(!startWindow.document.getElementById(makeID(testName)), 'sidebar id DNE');
+
+ close(window).then(done, assert.fail);
+ })
+}
+
+exports.testSidebarIsNotOpenInNewPrivateWindow = function(assert, done) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testSidebarIsNotOpenInNewPrivateWindow';
+ let window = getMostRecentBrowserWindow();
+
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+testName
+ });
+
+ sidebar.on('show', function() {
+ assert.equal(isPrivate(window), false, 'the new window is not private');
+ assert.equal(isSidebarShowing(window), true, 'the sidebar is showing');
+ assert.equal(isShowing(sidebar), true, 'the sidebar is showing');
+
+ let window2 = window.OpenBrowserWindow({ private: true });
+ windowPromise(window2, 'load').then(focus).then(function() {
+ // TODO: find better alt to setTimeout...
+ setTimeout(function() {
+ assert.equal(isPrivate(window2), true, 'the new window is private');
+ assert.equal(isSidebarShowing(window), true, 'the sidebar is showing in old window still');
+ assert.equal(isSidebarShowing(window2), false, 'the sidebar is not showing in the new private window');
+ assert.equal(isShowing(sidebar), false, 'the sidebar is not showing');
+
+ sidebar.destroy();
+ close(window2).then(done);
+ }, 500);
+ })
+ });
+
+ sidebar.show();
+}
+
+// TEST: edge case where web panel is destroyed while loading
+exports.testDestroyEdgeCaseBugWithPrivateWindow = function(assert, done) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testDestroyEdgeCaseBug';
+ let window = getMostRecentBrowserWindow();
+
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+testName
+ });
+
+ // NOTE: purposely not listening to show event b/c the event happens
+ // between now and then.
+ sidebar.show();
+
+ assert.equal(isPrivate(window), false, 'the new window is not private');
+ assert.equal(isSidebarShowing(window), true, 'the sidebar is showing');
+
+ //assert.equal(isShowing(sidebar), true, 'the sidebar is showing');
+
+ open(null, { features: { private: true } }).then(focus).then(function(window2) {
+ assert.equal(isPrivate(window2), true, 'the new window is private');
+ assert.equal(isSidebarShowing(window2), false, 'the sidebar is not showing');
+ assert.equal(isShowing(sidebar), false, 'the sidebar is not showing');
+
+ sidebar.destroy();
+ assert.pass('destroying the sidebar');
+
+ close(window2).then(function() {
+ let loader = Loader(module);
+
+ assert.equal(isPrivate(window), false, 'the current window is not private');
+
+ let sidebar = loader.require('sdk/ui/sidebar').Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+ testName,
+ onShow: function() {
+ assert.pass('onShow works for Sidebar');
+ loader.unload();
+
+ let sidebarMI = getSidebarMenuitems();
+ for (let mi of sidebarMI) {
+ assert.ok(BUILTIN_SIDEBAR_MENUITEMS.indexOf(mi.getAttribute('id')) >= 0, 'the menuitem is for a built-in sidebar')
+ assert.ok(!isChecked(mi), 'no sidebar menuitem is checked');
+ }
+ assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE');
+ assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing');
+
+ done();
+ }
+ })
+
+ sidebar.show();
+ assert.pass('showing the sidebar');
+
+ });
+ });
+}
+
+exports.testShowInPrivateWindow = function(assert, done) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testShowInPrivateWindow';
+ let window = getMostRecentBrowserWindow();
+ let { document } = window;
+ let url = 'data:text/html;charset=utf-8,'+testName;
+
+ let sidebar1 = Sidebar({
+ id: testName,
+ title: testName,
+ url: url
+ });
+
+ assert.equal(sidebar1.url, url, 'url getter works');
+ assert.equal(isShowing(sidebar1), false, 'the sidebar is not showing');
+ assert.ok(!isChecked(document.getElementById(makeID(sidebar1.id))),
+ 'the menuitem is not checked');
+ assert.equal(isSidebarShowing(window), false, 'the new window sidebar is not showing');
+
+ windowPromise(window.OpenBrowserWindow({ private: true }), 'load').then(function(window) {
+ let { document } = window;
+ assert.equal(isWindowPrivate(window), true, 'new window is private');
+ assert.equal(isPrivate(window), true, 'new window is private');
+
+ sidebar1.show().then(
+ function bad() {
+ assert.fail('a successful show should not happen here..');
+ },
+ function good() {
+ assert.equal(isShowing(sidebar1), false, 'the sidebar is still not showing');
+ assert.equal(document.getElementById(makeID(sidebar1.id)),
+ null,
+ 'the menuitem dne on the private window');
+ assert.equal(isSidebarShowing(window), false, 'the new window sidebar is not showing');
+
+ sidebar1.destroy();
+ close(window).then(done);
+ });
+ }, assert.fail);
+}
+
+// If the module doesn't support the app we're being run in, require() will
+// throw. In that case, remove all tests above from exports, and add one dummy
+// test that passes.
+try {
+ require('sdk/ui/sidebar');
+}
+catch (err) {
+ if (!/^Unsupported Application/.test(err.message))
+ throw err;
+
+ module.exports = {
+ 'test Unsupported Application': assert => assert.pass(err.message)
+ }
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-ui-sidebar.js b/addon-sdk/source/test/test-ui-sidebar.js
new file mode 100644
index 000000000..916a43e0b
--- /dev/null
+++ b/addon-sdk/source/test/test-ui-sidebar.js
@@ -0,0 +1,1579 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+module.metadata = {
+ 'engines': {
+ 'Firefox': '*'
+ }
+};
+
+const { Cu } = require('chrome');
+const { Loader } = require('sdk/test/loader');
+const { show, hide } = require('sdk/ui/sidebar/actions');
+const { isShowing } = require('sdk/ui/sidebar/utils');
+const { getMostRecentBrowserWindow, isFocused } = require('sdk/window/utils');
+const { open, close, focus, promise: windowPromise } = require('sdk/window/helpers');
+const { setTimeout, setImmediate } = require('sdk/timers');
+const { isPrivate } = require('sdk/private-browsing');
+const data = require('./fixtures');
+const { URL } = require('sdk/url');
+const { once, off, emit } = require('sdk/event/core');
+const { defer, all } = require('sdk/core/promise');
+const { modelFor } = require('sdk/model/core');
+const { cleanUI } = require("sdk/test/utils");
+const { before, after } = require('sdk/test/utils');
+
+require('sdk/windows');
+
+const { BUILTIN_SIDEBAR_MENUITEMS, isSidebarShowing,
+ getSidebarMenuitems, getExtraSidebarMenuitems, makeID, simulateCommand,
+ simulateClick, isChecked } = require('./sidebar/utils');
+
+exports.testSidebarBasicLifeCycle = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testSidebarBasicLifeCycle';
+ let window = getMostRecentBrowserWindow();
+ assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE');
+ let sidebarXUL = window.document.getElementById('sidebar');
+ assert.ok(sidebarXUL, 'sidebar xul element does exist');
+ assert.ok(!getExtraSidebarMenuitems().length, 'there are no extra sidebar menuitems');
+
+ assert.equal(isSidebarShowing(window), false, 'sidebar is not showing 1');
+ let sidebarDetails = {
+ id: testName,
+ title: 'test',
+ url: 'data:text/html;charset=utf-8,'+testName
+ };
+ let sidebar = Sidebar(sidebarDetails);
+
+ // test the sidebar attributes
+ for (let key of Object.keys(sidebarDetails)) {
+ assert.equal(sidebarDetails[key], sidebar[key], 'the attributes match the input');
+ }
+
+ assert.pass('The Sidebar constructor worked');
+
+ let extraMenuitems = getExtraSidebarMenuitems();
+ assert.equal(extraMenuitems.length, 1, 'there is one extra sidebar menuitems');
+
+ let ele = window.document.getElementById(makeID(testName));
+ assert.equal(ele, extraMenuitems[0], 'the only extra menuitem is the one for our sidebar.')
+ assert.ok(ele, 'sidebar element was added');
+ assert.ok(!isChecked(ele), 'the sidebar is not displayed');
+ assert.equal(ele.getAttribute('label'), sidebar.title, 'the sidebar title is the menuitem label')
+
+ assert.equal(isSidebarShowing(window), false, 'sidebar is not showing 2');
+
+ // explicit test of the on hide event
+ yield new Promise(resolve => {
+ sidebar.on('show', resolve);
+ sidebar.show();
+ assert.pass('showing sidebar..');
+ });
+
+ assert.pass('the show event was fired');
+ assert.equal(isSidebarShowing(window), true, 'sidebar is not showing 3');
+ assert.equal(isShowing(sidebar), true, 'the sidebar is showing');
+ assert.ok(isChecked(ele), 'the sidebar is displayed');
+
+ // explicit test of the on show event
+ yield new Promise(resolve => {
+ sidebar.on('hide', () => {
+ sidebar.once('detach', resolve);
+
+ assert.pass('the hide event was fired');
+ assert.ok(!isChecked(ele), 'the sidebar menuitem is not checked');
+ assert.equal(isShowing(sidebar), false, 'the sidebar is not showing');
+ assert.equal(isSidebarShowing(window), false, 'the sidebar elemnt is hidden');
+ });
+ sidebar.hide();
+ assert.pass('hiding sidebar..');
+ });
+
+ // calling destroy twice should not matter
+ sidebar.destroy();
+ sidebar.destroy();
+
+ for (let mi of getSidebarMenuitems()) {
+ let id = mi.getAttribute('id');
+
+ if (BUILTIN_SIDEBAR_MENUITEMS.indexOf(id) < 0) {
+ assert.fail('the menuitem "' + id + '" is not a built-in sidebar');
+ }
+ assert.ok(!isChecked(mi), 'no sidebar menuitem is checked');
+ }
+
+ assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE');
+ assert.pass('calling destroy worked without error');
+}
+
+exports.testSideBarIsInNewWindows = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testSideBarIsInNewWindows';
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+testName
+ });
+
+ let startWindow = getMostRecentBrowserWindow();
+ let ele = startWindow.document.getElementById(makeID(testName));
+ assert.ok(ele, 'sidebar element was added');
+
+ let window = yield open();
+
+ ele = window.document.getElementById(makeID(testName));
+ assert.ok(ele, 'sidebar element was added');
+
+ // calling destroy twice should not matter
+ sidebar.destroy();
+ sidebar.destroy();
+
+ assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE');
+ assert.ok(!startWindow.document.getElementById(makeID(testName)), 'sidebar id DNE');
+}
+
+exports.testSideBarIsShowingInNewWindows = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testSideBarIsShowingInNewWindows';
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: URL('data:text/html;charset=utf-8,'+testName)
+ });
+
+ let startWindow = getMostRecentBrowserWindow();
+ let ele = startWindow.document.getElementById(makeID(testName));
+ assert.ok(ele, 'sidebar element was added');
+
+ let oldEle = ele;
+
+ yield new Promise(resolve => {
+ sidebar.once('attach', function() {
+ assert.pass('attach event fired');
+
+ sidebar.once('show', function() {
+ assert.pass('show event fired');
+ resolve();
+ })
+ });
+ sidebar.show();
+ });
+
+ yield new Promise(resolve => {
+ sidebar.once('show', resolve);
+ startWindow.OpenBrowserWindow();
+ });
+
+ let window = getMostRecentBrowserWindow();
+ assert.notEqual(startWindow, window, 'window is new');
+
+ let sb = window.document.getElementById('sidebar');
+ yield new Promise(resolve => {
+ if (sb && sb.docShell && sb.contentDocument && sb.contentDocument.getElementById('web-panels-browser')) {
+ end();
+ }
+ else {
+ sb.addEventListener('DOMWindowCreated', end, false);
+ }
+ function end () {
+ sb.removeEventListener('DOMWindowCreated', end, false);
+ resolve();
+ }
+ })
+
+ ele = window.document.getElementById(makeID(testName));
+ assert.ok(ele, 'sidebar element was added 2');
+ assert.ok(isChecked(ele), 'the sidebar is checked');
+ assert.notEqual(ele, oldEle, 'there are two different sidebars');
+
+ assert.equal(isShowing(sidebar), true, 'the sidebar is showing in new window');
+
+ sidebar.destroy();
+
+ assert.equal(isShowing(sidebar), false, 'the sidebar is not showing');
+ assert.ok(!isSidebarShowing(window), 'sidebar in most recent window is not showing');
+ assert.ok(!isSidebarShowing(startWindow), 'sidebar in most start window is not showing');
+ assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE');
+ assert.ok(!startWindow.document.getElementById(makeID(testName)), 'sidebar id DNE');
+}
+
+// TODO: determine if this is acceptable..
+/*
+exports.testAddonGlobalSimple = function(assert, done) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testAddonGlobalSimple';
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: data.url('test-sidebar-addon-global.html')
+ });
+
+ sidebar.on('show', function({worker}) {
+ assert.pass('sidebar was attached');
+ assert.ok(!!worker, 'attach event has worker');
+
+ worker.port.on('X', function(msg) {
+ assert.equal(msg, '23', 'the final message is correct');
+
+ sidebar.destroy();
+
+ done();
+ });
+ worker.port.emit('X', '2');
+ });
+ show(sidebar);
+}
+*/
+
+exports.testAddonGlobalComplex = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testAddonGlobalComplex';
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: data.url('test-sidebar-addon-global.html')
+ });
+
+ let worker = yield new Promise(resolve => {
+ sidebar.on('attach', resolve);
+ show(sidebar);
+ });
+
+ assert.pass('sidebar was attached');
+ assert.ok(!!worker, 'attach event has worker');
+
+
+ let msg = yield new Promise(resolve => {
+ worker.port.once('Y', resolve);
+ });
+
+ assert.equal(msg, '1', 'got event from worker');
+
+ msg = yield new Promise(resolve => {
+ worker.port.on('X', resolve);
+ worker.port.emit('X', msg + '2');
+ });
+
+ assert.equal(msg, '123', 'the final message is correct');
+
+ sidebar.destroy();
+}
+
+exports.testAddonReady = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testAddonReady';
+ let ready = defer();
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: data.url('test-sidebar-addon-global.html'),
+ onReady: ready.resolve
+ });
+ show(sidebar);
+
+ let worker = yield ready.promise;
+ assert.pass('sidebar was attached');
+ assert.ok(!!worker, 'attach event has worker');
+
+ let msg = yield new Promise(resolve => {
+ worker.port.on('X', resolve);
+ worker.port.emit('X', '12');
+ });
+ assert.equal(msg, '123', 'the final message is correct');
+
+ sidebar.destroy();
+}
+
+exports.testShowingOneSidebarAfterAnother = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testShowingOneSidebarAfterAnother';
+
+ let sidebar1 = Sidebar({
+ id: testName + '1',
+ title: testName + '1',
+ url: 'data:text/html;charset=utf-8,'+ testName + 1
+ });
+ let sidebar2 = Sidebar({
+ id: testName + '2',
+ title: testName + '2',
+ url: 'data:text/html;charset=utf-8,'+ testName + 2
+ });
+
+ let window = getMostRecentBrowserWindow();
+ let IDs = [ sidebar1.id, sidebar2.id ];
+
+ let extraMenuitems = getExtraSidebarMenuitems(window);
+ assert.equal(extraMenuitems.length, 2, 'there are two extra sidebar menuitems');
+
+ function testShowing(sb1, sb2, sbEle) {
+ assert.equal(isShowing(sidebar1), sb1);
+ assert.equal(isShowing(sidebar2), sb2);
+ assert.equal(isSidebarShowing(window), sbEle);
+ }
+ testShowing(false, false, false);
+
+ yield show(sidebar1);
+ assert.pass('showed sidebar1');
+
+ testShowing(true, false, true);
+
+ for (let mi of getExtraSidebarMenuitems(window)) {
+ let menuitemID = mi.getAttribute('id').replace(/^jetpack-sidebar-/, '');
+ assert.ok(IDs.indexOf(menuitemID) >= 0, 'the extra menuitem is for one of our test sidebars');
+ assert.equal(isChecked(mi), menuitemID == sidebar1.id, 'the test sidebar menuitem has the correct checked value');
+ }
+
+ yield show(sidebar2);
+ assert.pass('showed sidebar2');
+
+ testShowing(false, true, true);
+
+ for (let mi of getExtraSidebarMenuitems(window)) {
+ let menuitemID = mi.getAttribute('id').replace(/^jetpack-sidebar-/, '');
+ assert.ok(IDs.indexOf(menuitemID) >= 0, 'the extra menuitem is for one of our test sidebars');
+ assert.equal(isChecked(mi), menuitemID == sidebar2.id, 'the test sidebar menuitem has the correct checked value');
+ }
+
+ sidebar1.destroy();
+ sidebar2.destroy();
+
+ testShowing(false, false, false);
+}
+
+exports.testSidebarUnload = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testSidebarUnload';
+ let loader = Loader(module);
+
+ let window = getMostRecentBrowserWindow();
+
+ assert.equal(isPrivate(window), false, 'the current window is not private');
+
+ let sidebar = loader.require('sdk/ui/sidebar').Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+ testName
+ });
+
+ yield sidebar.show();
+ assert.pass('showing the sidebar');
+
+ loader.unload();
+
+ for (let mi of getSidebarMenuitems()) {
+ assert.ok(BUILTIN_SIDEBAR_MENUITEMS.indexOf(mi.getAttribute('id')) >= 0, 'the menuitem is for a built-in sidebar')
+ assert.ok(!isChecked(mi), 'no sidebar menuitem is checked');
+ }
+
+ assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE');
+ assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing');
+}
+
+exports.testRelativeURL = function*(assert) {
+ const { merge } = require('sdk/util/object');
+ const self = require('sdk/self');
+
+ let loader = Loader(module, null, null, {
+ modules: {
+ 'sdk/self': merge({}, self, {
+ data: merge({}, self.data, require('./fixtures'))
+ })
+ }
+ });
+
+ const { Sidebar } = loader.require('sdk/ui/sidebar');
+
+ let testName = 'testRelativeURL';
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: './test-sidebar-addon-global.html'
+ });
+
+ yield new Promise(resolve => {
+ sidebar.on('attach', function(worker) {
+ assert.pass('sidebar was attached');
+ assert.ok(!!worker, 'attach event has worker');
+
+ worker.port.once('Y', function(msg) {
+ assert.equal(msg, '1', 'got event from worker');
+
+ worker.port.on('X', function(msg) {
+ assert.equal(msg, '123', 'the final message is correct');
+ resolve();
+ });
+ worker.port.emit('X', msg + '2');
+ })
+ });
+ sidebar.show();
+ });
+
+ sidebar.destroy();
+}
+
+exports.testRemoteContent = function(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testRemoteContent';
+ try {
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: 'http://dne.xyz.mozilla.org'
+ });
+ assert.fail('a bad sidebar was created..');
+ sidebar.destroy();
+ }
+ catch(e) {
+ assert.ok(/The option "url" must be a valid local URI\./.test(e), 'remote content is not acceptable');
+ }
+}
+
+exports.testInvalidURL = function(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testInvalidURL';
+ try {
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: 'http:mozilla.org'
+ });
+ assert.fail('a bad sidebar was created..');
+ sidebar.destroy();
+ }
+ catch(e) {
+ assert.ok(/The option "url" must be a valid local URI\./.test(e), 'invalid URIs are not acceptable');
+ }
+}
+
+exports.testInvalidURLType = function(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testInvalidURLType';
+ try {
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName
+ });
+ assert.fail('a bad sidebar was created..');
+ sidebar.destroy();
+ }
+ catch(e) {
+ assert.ok(/The option "url" must be a valid local URI\./.test(e), 'invalid URIs are not acceptable');
+ }
+}
+
+exports.testInvalidTitle = function(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testInvalidTitle';
+ try {
+ let sidebar = Sidebar({
+ id: testName,
+ title: '',
+ url: 'data:text/html;charset=utf-8,'+testName
+ });
+ assert.fail('a bad sidebar was created..');
+ sidebar.destroy();
+ }
+ catch(e) {
+ assert.equal('The option "title" must be one of the following types: string', e.message, 'invalid titles are not acceptable');
+ }
+}
+
+exports.testInvalidID = function(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testInvalidID';
+ try {
+ let sidebar = Sidebar({
+ id: '!',
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+testName
+ });
+ assert.fail('a bad sidebar was created..');
+ sidebar.destroy();
+ }
+ catch(e) {
+ assert.ok(/The option "id" must be a valid alphanumeric id/.test(e), 'invalid ids are not acceptable');
+ }
+}
+
+exports.testInvalidBlankID = function(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testInvalidBlankID';
+ try {
+ let sidebar = Sidebar({
+ id: '',
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+testName
+ });
+ assert.fail('a bad sidebar was created..');
+ sidebar.destroy();
+ }
+ catch(e) {
+ assert.ok(/The option "id" must be a valid alphanumeric id/.test(e), 'invalid ids are not acceptable');
+ }
+}
+
+exports.testInvalidNullID = function(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testInvalidNullID';
+ try {
+ let sidebar = Sidebar({
+ id: null,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+testName
+ });
+ assert.fail('a bad sidebar was created..');
+ sidebar.destroy();
+ }
+ catch(e) {
+ assert.ok(/The option "id" must be a valid alphanumeric id/.test(e), 'invalid ids are not acceptable');
+ }
+}
+
+exports.testUndefinedID = function(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testInvalidUndefinedID';
+
+ try {
+ let sidebar = Sidebar({
+ title: testName,
+ url: 'data:text/html;charset=utf-8,' + testName
+ });
+
+ assert.ok(sidebar.id, 'an undefined id was accepted, id was creawted: ' + sidebar.id);
+ assert.ok(getMostRecentBrowserWindow().document.getElementById(makeID(sidebar.id)), 'the sidebar element was found');
+
+ sidebar.destroy();
+ }
+ catch(e) {
+ assert.fail('undefined ids are acceptable');
+ assert.fail(e.message);
+ }
+}
+
+// TEST: edge case where web panel is destroyed while loading
+exports.testDestroyEdgeCaseBug = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testDestroyEdgeCaseBug';
+ let window = getMostRecentBrowserWindow();
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+testName
+ });
+
+ // NOTE: purposely not listening to show event b/c the event happens
+ // between now and then.
+ sidebar.show();
+
+ assert.equal(isPrivate(window), false, 'the new window is not private');
+ assert.equal(isSidebarShowing(window), true, 'the sidebar is showing');
+
+ //assert.equal(isShowing(sidebar), true, 'the sidebar is showing');
+
+ let window2 = yield open().then(focus);
+
+ assert.equal(isPrivate(window2), false, 'the new window is not private');
+ assert.equal(isSidebarShowing(window2), false, 'the sidebar is not showing');
+ assert.equal(isShowing(sidebar), false, 'the sidebar is not showing');
+
+ sidebar.destroy();
+ assert.pass('destroying the sidebar');
+
+ yield close(window2);
+
+ let loader = Loader(module);
+
+ assert.equal(isPrivate(window), false, 'the current window is not private');
+
+ sidebar = loader.require('sdk/ui/sidebar').Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+ testName,
+ onShow: function() {
+ }
+ })
+
+ assert.pass('showing the sidebar');
+ yield sidebar.show();
+ loader.unload();
+
+ for (let mi of getSidebarMenuitems()) {
+ let id = mi.getAttribute('id');
+
+ if (BUILTIN_SIDEBAR_MENUITEMS.indexOf(id) < 0) {
+ assert.fail('the menuitem "' + id + '" is not a built-in sidebar');
+ }
+ assert.ok(!isChecked(mi), 'no sidebar menuitem is checked');
+ }
+ assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE');
+ assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing');
+}
+
+exports.testClickingACheckedMenuitem = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ const testName = 'testClickingACheckedMenuitem';
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+testName,
+ });
+ assert.pass('sidebar was created');
+
+ let window = yield open().then(focus);
+ yield sidebar.show();
+ assert.pass('the show callback works');
+
+ let waitForHide = defer();
+ sidebar.once('hide', waitForHide.resolve);
+ let menuitem = window.document.getElementById(makeID(sidebar.id));
+ simulateCommand(menuitem);
+
+ yield waitForHide.promise;
+
+ assert.pass('clicking the menuitem after the sidebar has shown hides it.');
+ sidebar.destroy();
+};
+
+exports.testTitleSetter = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testTitleSetter';
+ let { document } = getMostRecentBrowserWindow();
+
+ let sidebar1 = Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+testName,
+ });
+
+ assert.equal(sidebar1.title, testName, 'title getter works');
+
+ yield sidebar1.show();
+ assert.equal(document.getElementById(makeID(sidebar1.id)).getAttribute('label'),
+ testName,
+ 'the menuitem label is correct');
+
+ assert.equal(document.getElementById('sidebar-title').value, testName, 'the menuitem label is correct');
+
+ sidebar1.title = 'foo';
+
+ assert.equal(sidebar1.title, 'foo', 'title getter works');
+
+ assert.equal(document.getElementById(makeID(sidebar1.id)).getAttribute('label'),
+ 'foo',
+ 'the menuitem label was updated');
+
+ assert.equal(document.getElementById('sidebar-title').value, 'foo', 'the sidebar title was updated');
+
+ sidebar1.destroy();
+}
+
+exports.testURLSetter = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testURLSetter';
+ let window = getMostRecentBrowserWindow();
+ let { document } = window;
+ let url = 'data:text/html;charset=utf-8,'+testName;
+
+ let sidebar1 = Sidebar({
+ id: testName,
+ title: testName,
+ url: url
+ });
+
+ assert.equal(sidebar1.url, url, 'url getter works');
+ assert.equal(isShowing(sidebar1), false, 'the sidebar is not showing');
+ assert.ok(!isChecked(document.getElementById(makeID(sidebar1.id))),
+ 'the menuitem is not checked');
+ assert.equal(isSidebarShowing(window), false, 'the new window sidebar is not showing');
+
+ window = yield windowPromise(window.OpenBrowserWindow(), 'load');
+ document = window.document;
+ assert.pass('new window was opened');
+
+ yield sidebar1.show();
+
+ assert.equal(isShowing(sidebar1), true, 'the sidebar is showing');
+ assert.ok(isChecked(document.getElementById(makeID(sidebar1.id))),
+ 'the menuitem is checked');
+ assert.ok(isSidebarShowing(window), 'the new window sidebar is showing');
+
+ yield new Promise(resolve => {
+ sidebar1.once('show', resolve);
+ sidebar1.url = (url + '1');
+ assert.equal(sidebar1.url, (url + '1'), 'url getter works');
+ assert.equal(isShowing(sidebar1), true, 'the sidebar is showing');
+ assert.ok(isSidebarShowing(window), 'the new window sidebar is showing');
+ });
+
+ assert.pass('setting the sidebar.url causes a show event');
+
+ assert.equal(isShowing(sidebar1), true, 'the sidebar is showing');
+ assert.equal(isSidebarShowing(window), true, 'the new window sidebar is still showing');
+
+ assert.ok(isChecked(document.getElementById(makeID(sidebar1.id))),
+ 'the menuitem is still checked');
+
+ sidebar1.destroy();
+ assert.equal(isSidebarShowing(window), false, 'the new window sidebar is not showing');
+}
+
+exports.testDuplicateID = function(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testDuplicateID';
+ let window = getMostRecentBrowserWindow();
+ let { document } = window;
+ let url = 'data:text/html;charset=utf-8,'+testName;
+
+ let sidebar1 = Sidebar({
+ id: testName,
+ title: testName,
+ url: url
+ });
+
+ assert.throws(function() {
+ Sidebar({
+ id: testName,
+ title: testName + 1,
+ url: url + 2
+ }).destroy();
+ }, /The ID .+ seems already used\./i, 'duplicate IDs will throw errors');
+
+ sidebar1.destroy();
+}
+
+exports.testURLSetterToSameValueReloadsSidebar = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testURLSetterToSameValueReloadsSidebar';
+ let window = getMostRecentBrowserWindow();
+ let { document } = window;
+ let url = 'data:text/html;charset=utf-8,'+testName;
+
+ let sidebar1 = Sidebar({
+ id: testName,
+ title: testName,
+ url: url
+ });
+
+ assert.equal(sidebar1.url, url, 'url getter works');
+ assert.equal(isShowing(sidebar1), false, 'the sidebar is not showing');
+ assert.ok(!isChecked(document.getElementById(makeID(sidebar1.id))),
+ 'the menuitem is not checked');
+ assert.equal(isSidebarShowing(window), false, 'the new window sidebar is not showing');
+
+ window = yield windowPromise(window.OpenBrowserWindow(), 'load');
+ document = window.document;
+ assert.pass('new window was opened');
+
+ yield focus(window);
+ assert.pass('new window was focused');
+
+ yield sidebar1.show();
+
+ assert.equal(isShowing(sidebar1), true, 'the sidebar is showing');
+ assert.ok(isChecked(document.getElementById(makeID(sidebar1.id))),
+ 'the menuitem is checked');
+ assert.ok(isSidebarShowing(window), 'the new window sidebar is showing');
+
+ let shown = defer();
+ sidebar1.once('show', shown.resolve);
+ sidebar1.url = url;
+
+ assert.equal(sidebar1.url, url, 'url getter works');
+ assert.equal(isShowing(sidebar1), true, 'the sidebar is showing');
+ assert.ok(isSidebarShowing(window), 'the new window sidebar is showing');
+
+ yield shown.promise;
+
+ assert.pass('setting the sidebar.url causes a show event');
+
+ assert.equal(isShowing(sidebar1), true, 'the sidebar is showing');
+ assert.ok(isSidebarShowing(window), 'the new window sidebar is still showing');
+
+ assert.ok(isChecked(document.getElementById(makeID(sidebar1.id))),
+ 'the menuitem is still checked');
+
+ sidebar1.destroy();
+}
+
+exports.testShowingInOneWindowDoesNotAffectOtherWindows = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testShowingInOneWindowDoesNotAffectOtherWindows';
+ let window1 = getMostRecentBrowserWindow();
+ let url = 'data:text/html;charset=utf-8,'+testName;
+
+ let sidebar1 = Sidebar({
+ id: testName,
+ title: testName,
+ url: url
+ });
+
+ assert.equal(sidebar1.url, url, 'url getter works');
+ assert.equal(isShowing(sidebar1), false, 'the sidebar is not showing');
+ let checkCount = 1;
+ function checkSidebarShowing(window, expected) {
+ assert.pass('check count ' + checkCount++);
+
+ let mi = window.document.getElementById(makeID(sidebar1.id));
+ if (mi) {
+ assert.equal(isChecked(mi), expected,
+ 'the menuitem is not checked');
+ }
+ assert.equal(isSidebarShowing(window), expected || false, 'the new window sidebar is not showing');
+ }
+ checkSidebarShowing(window1, false);
+
+ let window = yield windowPromise(window1.OpenBrowserWindow(), 'load');
+ let { document } = window;
+ assert.pass('new window was opened!');
+
+ // waiting for show
+ yield sidebar1.show();
+
+ // check state of the new window
+ assert.equal(isShowing(sidebar1), true, 'the sidebar is showing');
+ checkSidebarShowing(window, true);
+
+ // check state of old window
+ checkSidebarShowing(window1, false);
+
+ yield sidebar1.show();
+
+ // check state of the new window
+ assert.equal(isShowing(sidebar1), true, 'the sidebar is showing');
+ checkSidebarShowing(window, true);
+
+ // check state of old window
+ checkSidebarShowing(window1, false);
+
+ // calling destroy() twice should not matter
+ sidebar1.destroy();
+ sidebar1.destroy();
+
+ // check state of the new window
+ assert.equal(isShowing(sidebar1), false, 'the sidebar is not showing');
+ checkSidebarShowing(window, undefined);
+
+ // check state of old window
+ checkSidebarShowing(window1, undefined);
+}
+
+exports.testHidingAHiddenSidebarRejects = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testHidingAHiddenSidebarRejects';
+ let url = 'data:text/html;charset=utf-8,'+testName;
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: url
+ });
+
+ yield sidebar.hide().then(assert.fail, assert.pass);
+
+ sidebar.destroy();
+}
+
+exports.testGCdSidebarsOnUnload = function*(assert) {
+ const loader = Loader(module);
+ const { Sidebar } = loader.require('sdk/ui/sidebar');
+ const window = getMostRecentBrowserWindow();
+
+ let testName = 'testGCdSidebarsOnUnload';
+ let url = 'data:text/html;charset=utf-8,'+testName;
+
+ assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing');
+
+ // IMPORTANT: make no reference to the sidebar instance, so it is GC'd
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: url
+ });
+
+ yield sidebar.show();
+ sidebar = null;
+
+ assert.equal(isSidebarShowing(window), true, 'the sidebar is showing');
+
+ let menuitemID = makeID(testName);
+
+ assert.ok(window.document.getElementById(menuitemID), 'the menuitem was found');
+
+ yield new Promise(resolve => Cu.schedulePreciseGC(resolve));
+
+ loader.unload();
+
+ assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing after unload');
+ assert.ok(!window.document.getElementById(menuitemID), 'the menuitem was removed');
+}
+
+exports.testGCdShowingSidebarsOnUnload = function*(assert) {
+ const loader = Loader(module);
+ const { Sidebar } = loader.require('sdk/ui/sidebar');
+ const window = getMostRecentBrowserWindow();
+
+ let testName = 'testGCdShowingSidebarsOnUnload';
+ let url = 'data:text/html;charset=utf-8,'+testName;
+
+ assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing');
+
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: url
+ });
+
+ yield sidebar.show();
+
+ sidebar = null;
+
+ assert.equal(isSidebarShowing(window), true, 'the sidebar is showing');
+
+ let menuitemID = makeID(testName);
+
+ assert.ok(!!window.document.getElementById(menuitemID), 'the menuitem was found');
+
+ yield new Promise(resolve => Cu.schedulePreciseGC(resolve));
+
+ assert.equal(isSidebarShowing(window), true, 'the sidebar is still showing after gc');
+ assert.ok(!!window.document.getElementById(menuitemID), 'the menuitem was found after gc');
+
+ loader.unload();
+
+ assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing after unload');
+ assert.ok(!window.document.getElementById(menuitemID), 'the menuitem was removed');
+}
+
+exports.testDetachEventOnWindowClose = function*(assert) {
+ const loader = Loader(module);
+ const { Sidebar } = loader.require('sdk/ui/sidebar');
+ let window = getMostRecentBrowserWindow();
+
+ let testName = 'testDetachEventOnWindowClose';
+ let url = 'data:text/html;charset=utf-8,' + testName;
+
+ window = yield windowPromise(window.OpenBrowserWindow(), 'load').then(focus);
+
+ yield new Promise(resolve => {
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: url,
+ onAttach: function() {
+ assert.pass('the attach event is fired');
+ window.close();
+ },
+ onDetach: resolve
+ });
+
+ sidebar.show();
+ });
+
+ assert.pass('the detach event is fired when the window showing it closes');
+ loader.unload();
+}
+
+exports.testHideEventOnWindowClose = function*(assert) {
+ const loader = Loader(module);
+ const { Sidebar } = loader.require('sdk/ui/sidebar');
+ let window = getMostRecentBrowserWindow();
+
+ let testName = 'testDetachEventOnWindowClose';
+ let url = 'data:text/html;charset=utf-8,' + testName;
+
+
+ window = yield windowPromise(window.OpenBrowserWindow(), 'load').then(focus);
+ yield new Promise(resolve => {
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: url,
+ onAttach: function() {
+ assert.pass('the attach event is fired');
+ window.close();
+ },
+ onHide: resolve
+ });
+
+ sidebar.show();
+ });
+
+ assert.pass('the hide event is fired when the window showing it closes');
+ loader.unload();
+}
+
+exports.testGCdHiddenSidebarsOnUnload = function*(assert) {
+ const loader = Loader(module);
+ const { Sidebar } = loader.require('sdk/ui/sidebar');
+ const window = getMostRecentBrowserWindow();
+
+ let testName = 'testGCdHiddenSidebarsOnUnload';
+ let url = 'data:text/html;charset=utf-8,'+testName;
+
+ assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing');
+
+ // IMPORTANT: make no reference to the sidebar instance, so it is GC'd
+ Sidebar({
+ id: testName,
+ title: testName,
+ url: url
+ });
+
+ let menuitemID = makeID(testName);
+
+ assert.ok(!!window.document.getElementById(menuitemID), 'the menuitem was found');
+
+ yield new Promise(resolve => Cu.schedulePreciseGC(resolve));
+
+ assert.ok(!!window.document.getElementById(menuitemID), 'the menuitem was found after gc');
+
+ loader.unload();
+
+ assert.ok(!window.document.getElementById(menuitemID), 'the menuitem was removed');
+}
+
+exports.testSidebarGettersAndSettersAfterDestroy = function(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testSidebarGettersAndSettersAfterDestroy';
+ let url = 'data:text/html;charset=utf-8,'+testName;
+
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: url
+ });
+
+ sidebar.destroy();
+
+ assert.equal(sidebar.id, undefined, 'sidebar after destroy has no id');
+
+ assert.throws(() => sidebar.id = 'foo-tang',
+ /^setting a property that has only a getter/,
+ 'id cannot be set at runtime');
+
+ assert.equal(sidebar.id, undefined, 'sidebar after destroy has no id');
+
+ assert.equal(sidebar.title, undefined, 'sidebar after destroy has no title');
+ sidebar.title = 'boo-tang';
+ assert.equal(sidebar.title, undefined, 'sidebar after destroy has no title');
+
+ assert.equal(sidebar.url, undefined, 'sidebar after destroy has no url');
+ sidebar.url = url + 'barz';
+ assert.equal(sidebar.url, undefined, 'sidebar after destroy has no url');
+}
+
+
+exports.testSidebarLeakCheckDestroyAfterAttach = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testSidebarLeakCheckDestroyAfterAttach';
+ let window = getMostRecentBrowserWindow();
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+testName
+ });
+
+ yield new Promise(resolve => {
+ sidebar.on('attach', resolve);
+ assert.pass('showing the sidebar');
+ sidebar.show();
+ })
+
+ assert.pass('the sidebar was shown');
+
+ sidebar.on('show', () => {
+ assert.fail('the sidebar show listener should have been removed');
+ });
+ assert.pass('added a sidebar show listener');
+
+ sidebar.on('hide', () => {
+ assert.fail('the sidebar hide listener should have been removed');
+ });
+ assert.pass('added a sidebar hide listener');
+
+ yield new Promise(resolve => {
+ let panelBrowser = window.document.getElementById('sidebar').contentDocument.getElementById('web-panels-browser');
+ panelBrowser.contentWindow.addEventListener('unload', function onUnload() {
+ panelBrowser.contentWindow.removeEventListener('unload', onUnload, false);
+ resolve();
+ }, false);
+ sidebar.destroy();
+ });
+
+ assert.pass('the sidebar web panel was unloaded properly');
+}
+
+exports.testSidebarLeakCheckUnloadAfterAttach = function*(assert) {
+ const loader = Loader(module);
+ const { Sidebar } = loader.require('sdk/ui/sidebar');
+ let testName = 'testSidebarLeakCheckUnloadAfterAttach';
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+testName
+ });
+
+ let window = yield open().then(focus);
+
+ yield new Promise(resolve => {
+ sidebar.on('attach', resolve);
+ assert.pass('showing the sidebar');
+ sidebar.show();
+ });
+
+ assert.pass('the sidebar was shown');
+
+ sidebar.on('show', function() {
+ assert.fail('the sidebar show listener should have been removed');
+ });
+ assert.pass('added a sidebar show listener');
+
+ sidebar.on('hide', function() {
+ assert.fail('the sidebar hide listener should have been removed');
+ });
+ assert.pass('added a sidebar hide listener');
+
+ let panelBrowser = window.document.getElementById('sidebar').contentDocument.getElementById('web-panels-browser');
+ yield new Promise(resolve => {
+ panelBrowser.contentWindow.addEventListener('unload', function onUnload() {
+ panelBrowser.contentWindow.removeEventListener('unload', onUnload, false);
+ resolve();
+ }, false);
+ loader.unload();
+ });
+
+ assert.pass('the sidebar web panel was unloaded properly');
+}
+
+exports.testTwoSidebarsWithSameTitleAndURL = function(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testTwoSidebarsWithSameTitleAndURL';
+
+ let title = testName;
+ let url = 'data:text/html;charset=utf-8,' + testName;
+
+ let sidebar1 = Sidebar({
+ id: testName + 1,
+ title: title,
+ url: url
+ });
+
+ assert.throws(function() {
+ Sidebar({
+ id: testName + 2,
+ title: title,
+ url: url
+ }).destroy();
+ }, /title.+url.+invalid/i, 'Creating two sidebars with the same title + url is not allowed');
+
+ let sidebar2 = Sidebar({
+ id: testName + 2,
+ title: title,
+ url: 'data:text/html;charset=utf-8,X'
+ });
+
+ assert.throws(function() {
+ sidebar2.url = url;
+ }, /title.+url.+invalid/i, 'Creating two sidebars with the same title + url is not allowed');
+
+ sidebar2.title = 'foo';
+ sidebar2.url = url;
+
+ assert.throws(function() {
+ sidebar2.title = title;
+ }, /title.+url.+invalid/i, 'Creating two sidebars with the same title + url is not allowed');
+
+ sidebar1.destroy();
+ sidebar2.destroy();
+}
+
+exports.testChangingURLBackToOriginalValue = function(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testChangingURLBackToOriginalValue';
+
+ let title = testName;
+ let url = 'data:text/html;charset=utf-8,' + testName;
+ let count = 0;
+
+ let sidebar = Sidebar({
+ id: testName,
+ title: title,
+ url: url
+ });
+
+ sidebar.url = url + 2;
+ assert.equal(sidebar.url, url + 2, 'the sidebar.url is correct');
+ sidebar.url = url;
+ assert.equal(sidebar.url, url, 'the sidebar.url is correct');
+
+ sidebar.title = 'foo';
+ assert.equal(sidebar.title, 'foo', 'the sidebar.title is correct');
+ sidebar.title = title;
+ assert.equal(sidebar.title, title, 'the sidebar.title is correct');
+
+ sidebar.destroy();
+
+ assert.pass('Changing values back to originals works');
+}
+
+exports.testShowToOpenXToClose = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testShowToOpenXToClose';
+
+ let title = testName;
+ let url = 'data:text/html;charset=utf-8,' + testName;
+ let window = getMostRecentBrowserWindow();
+ let shown = defer();
+ let hidden = defer();
+
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: url,
+ onShow: shown.resolve,
+ onHide: hidden.resolve
+ });
+
+ let menuitem = window.document.getElementById(makeID(sidebar.id));
+
+ assert.ok(!isChecked(menuitem), 'menuitem is not checked');
+
+ sidebar.show();
+
+ yield shown.promise;
+
+ assert.ok(isChecked(menuitem), 'menuitem is checked');
+
+ let closeButton = window.document.querySelector('#sidebar-header > toolbarbutton.close-icon');
+ simulateCommand(closeButton);
+
+ yield hidden.promise;
+
+ assert.ok(!isChecked(menuitem), 'menuitem is not checked');
+
+ sidebar.destroy();
+}
+
+exports.testShowToOpenMenuitemToClose = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testShowToOpenMenuitemToClose';
+
+ let title = testName;
+ let url = 'data:text/html;charset=utf-8,' + testName;
+ let window = getMostRecentBrowserWindow();
+
+ let hidden = defer();
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: url,
+ onHide: hidden.resolve
+ });
+
+ let menuitem = window.document.getElementById(makeID(sidebar.id));
+
+ assert.ok(!isChecked(menuitem), 'menuitem is not checked');
+
+ yield sidebar.show();
+
+ assert.ok(isChecked(menuitem), 'menuitem is checked');
+
+ simulateCommand(menuitem);
+
+ yield hidden.promise;
+
+ assert.ok(!isChecked(menuitem), 'menuitem is not checked');
+
+ sidebar.destroy();
+}
+
+exports.testDestroyWhileNonBrowserWindowIsOpen = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testDestroyWhileNonBrowserWindowIsOpen';
+ let url = 'data:text/html;charset=utf-8,' + testName;
+
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: url
+ });
+
+ let window = yield open('chrome://browser/content/aboutDialog.xul');
+
+ yield sidebar.show();
+ assert.equal(isSidebarShowing(getMostRecentBrowserWindow()), true, 'the sidebar is showing');
+
+ sidebar.destroy();
+ assert.pass('sidebar was destroyed while a non browser window was open');
+
+ yield cleanUI();
+ assert.equal(isSidebarShowing(getMostRecentBrowserWindow()), false, 'the sidebar is not showing');
+}
+
+exports.testEventListeners = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testWhatThisIsInSidebarEventListeners';
+ let eventListenerOrder = [];
+
+ let constructorOnShow = defer();
+ let constructorOnHide = defer();
+ let constructorOnAttach = defer();
+ let constructorOnReady = defer();
+
+ let onShow = defer();
+ let onHide = defer();
+ let onAttach = defer();
+ let onReady = defer();
+
+ let onceShow = defer();
+ let onceHide = defer();
+ let onceAttach = defer();
+ let onceReady = defer();
+
+ function testThis() {
+ assert(this, sidebar, '`this` is correct');
+ }
+
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,' + testName,
+ onShow: function() {
+ assert.equal(this, sidebar, '`this` is correct in onShow');
+ eventListenerOrder.push('onShow');
+ constructorOnShow.resolve();
+ },
+ onAttach: function() {
+ assert.equal(this, sidebar, '`this` is correct in onAttach');
+ eventListenerOrder.push('onAttach');
+ constructorOnAttach.resolve();
+ },
+ onReady: function() {
+ assert.equal(this, sidebar, '`this` is correct in onReady');
+ eventListenerOrder.push('onReady');
+ constructorOnReady.resolve();
+ },
+ onHide: function() {
+ assert.equal(this, sidebar, '`this` is correct in onHide');
+ eventListenerOrder.push('onHide');
+ constructorOnHide.resolve();
+ }
+ });
+
+ sidebar.once('show', function() {
+ assert.equal(this, sidebar, '`this` is correct in once show');
+ eventListenerOrder.push('once show');
+ onceShow.resolve();
+ });
+ sidebar.once('attach', function() {
+ assert.equal(this, sidebar, '`this` is correct in once attach');
+ eventListenerOrder.push('once attach');
+ onceAttach.resolve();
+ });
+ sidebar.once('ready', function() {
+ assert.equal(this, sidebar, '`this` is correct in once ready');
+ eventListenerOrder.push('once ready');
+ onceReady.resolve();
+ });
+ sidebar.once('hide', function() {
+ assert.equal(this, sidebar, '`this` is correct in once hide');
+ eventListenerOrder.push('once hide');
+ onceHide.resolve();
+ });
+
+ sidebar.on('show', function() {
+ assert.equal(this, sidebar, '`this` is correct in on show');
+ eventListenerOrder.push('on show');
+ onShow.resolve();
+
+ sidebar.hide();
+ });
+ sidebar.on('attach', function() {
+ assert.equal(this, sidebar, '`this` is correct in on attach');
+ eventListenerOrder.push('on attach');
+ onAttach.resolve();
+ });
+ sidebar.on('ready', function() {
+ assert.equal(this, sidebar, '`this` is correct in on ready');
+ eventListenerOrder.push('on ready');
+ onReady.resolve();
+ });
+ sidebar.on('hide', function() {
+ assert.equal(this, sidebar, '`this` is correct in on hide');
+ eventListenerOrder.push('on hide');
+ onHide.resolve();
+ });
+
+ sidebar.show();
+
+ yield all([constructorOnShow.promise,
+ constructorOnAttach.promise,
+ constructorOnReady.promise,
+ constructorOnHide.promise,
+ onceShow.promise,
+ onceAttach.promise,
+ onceReady.promise,
+ onceHide.promise,
+ onShow.promise,
+ onAttach.promise,
+ onReady.promise,
+ onHide.promise]);
+
+ assert.equal(eventListenerOrder.join(), [
+ 'onAttach',
+ 'once attach',
+ 'on attach',
+ 'onReady',
+ 'once ready',
+ 'on ready',
+ 'onShow',
+ 'once show',
+ 'on show',
+ 'onHide',
+ 'once hide',
+ 'on hide'
+ ].join(), 'the event order was correct');
+
+ sidebar.destroy();
+}
+
+// For more information see Bug 920780
+exports.testAttachDoesNotEmitWhenShown = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+ let testName = 'testAttachDoesNotEmitWhenShown';
+ let count = 0;
+
+ let attached = defer();
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,'+testName,
+ onAttach: function() {
+ if (count > 2) {
+ assert.fail('sidebar was attached again..');
+ }
+ else {
+ assert.pass('sidebar was attached ' + count + ' time(s)');
+ }
+
+ count++;
+ attached.resolve();
+ }
+ });
+
+ sidebar.show();
+
+ yield attached.promise;
+
+ let shownFired = 0;
+ let onShow = () => shownFired++;
+ sidebar.on('show', onShow);
+
+ yield sidebar.show();
+ assert.equal(shownFired, 0, 'shown should not be fired again when already showing from after attach');
+
+ yield sidebar.hide();
+ assert.pass("the sidebar was hidden");
+
+ yield sidebar.show();
+ assert.equal(shownFired, 1, 'shown was emitted when `show` called after being hidden');
+
+ yield sidebar.show();
+ assert.equal(shownFired, 1, 'shown was not emitted again if already being shown');
+
+ sidebar.off('show', onShow);
+ sidebar.destroy();
+}
+
+exports.testShowHideRawWindowArg = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+
+ let testName = 'testShowHideRawWindowArg';
+
+ assert.pass("Creating sidebar");
+
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,' + testName
+ });
+
+ assert.pass("Created sidebar");
+
+ let mainWindow = getMostRecentBrowserWindow();
+ let newWindow = yield windowPromise(mainWindow.OpenBrowserWindow(), 'load');
+ assert.pass("Created the new window");
+
+ yield focus(newWindow);
+ assert.pass("Focused the new window");
+
+ let newWindow2 = yield windowPromise(mainWindow.OpenBrowserWindow(), 'load');
+ assert.pass("Created the second new window");
+
+ yield focus(newWindow2);
+ assert.pass("Focused the second new window");
+
+ yield sidebar.show(newWindow);
+
+ assert.pass('the sidebar was shown');
+ assert.equal(isSidebarShowing(mainWindow), false, 'sidebar is not showing in main window');
+ assert.equal(isSidebarShowing(newWindow2), false, 'sidebar is not showing in second window');
+ assert.equal(isSidebarShowing(newWindow), true, 'sidebar is showing in new window');
+
+ assert.ok(isFocused(newWindow2), 'main window is still focused');
+
+ yield sidebar.hide(newWindow);
+
+ assert.equal(isFocused(newWindow2), true, 'second window is still focused');
+ assert.equal(isSidebarShowing(mainWindow), false, 'sidebar is not showing in main window');
+ assert.equal(isSidebarShowing(newWindow2), false, 'sidebar is not showing in second window');
+ assert.equal(isSidebarShowing(newWindow), false, 'sidebar is not showing in new window');
+
+ sidebar.destroy();
+}
+
+exports.testShowHideSDKWindowArg = function*(assert) {
+ const { Sidebar } = require('sdk/ui/sidebar');
+
+ let testName = 'testShowHideSDKWindowArg';
+ let sidebar = Sidebar({
+ id: testName,
+ title: testName,
+ url: 'data:text/html;charset=utf-8,' + testName
+ });
+
+ let mainWindow = getMostRecentBrowserWindow();
+ let newWindow = yield open().then(focus);
+ let newSDKWindow = modelFor(newWindow);
+
+ yield focus(mainWindow);
+
+ yield sidebar.show(newSDKWindow);
+
+ assert.pass('the sidebar was shown');
+ assert.ok(!isSidebarShowing(mainWindow), 'sidebar is not showing in main window');
+ assert.ok(isSidebarShowing(newWindow), 'sidebar is showing in new window');
+
+ assert.ok(isFocused(mainWindow), 'main window is still focused');
+
+ yield sidebar.hide(newSDKWindow);
+
+ assert.ok(isFocused(mainWindow), 'main window is still focused');
+ assert.ok(!isSidebarShowing(mainWindow), 'sidebar is not showing in main window');
+ assert.ok(!isSidebarShowing(newWindow), 'sidebar is not showing in new window');
+ sidebar.destroy();
+}
+
+before(exports, (name, assert) => {
+ assert.equal(isSidebarShowing(), false, 'no sidebar is showing');
+});
+
+after(exports, function*(name, assert) {
+ assert.pass("Cleaning new windows and tabs");
+ yield cleanUI();
+ assert.equal(isSidebarShowing(), false, 'no sidebar is showing');
+});
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-ui-toggle-button.js b/addon-sdk/source/test/test-ui-toggle-button.js
new file mode 100644
index 000000000..c187ec794
--- /dev/null
+++ b/addon-sdk/source/test/test-ui-toggle-button.js
@@ -0,0 +1,1386 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+module.metadata = {
+ 'engines': {
+ 'Firefox': '> 28'
+ }
+};
+
+const { Cu } = require('chrome');
+const { Loader } = require('sdk/test/loader');
+const { data } = require('sdk/self');
+const { open, focus, close } = require('sdk/window/helpers');
+const { setTimeout } = require('sdk/timers');
+const { getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { partial } = require('sdk/lang/functional');
+const { wait } = require('./event/helpers');
+const { gc } = require('sdk/test/memory');
+const packaging = require("@loader/options");
+
+const openBrowserWindow = partial(open, null, {features: {toolbar: true}});
+const openPrivateBrowserWindow = partial(open, null,
+ {features: {toolbar: true, private: true}});
+
+const badgeNodeFor = (node) =>
+ node.ownerDocument.getAnonymousElementByAttribute(node,
+ 'class', 'toolbarbutton-badge');
+
+function getWidget(buttonId, window = getMostRecentBrowserWindow()) {
+ const { CustomizableUI } = Cu.import('resource:///modules/CustomizableUI.jsm', {});
+ const { AREA_NAVBAR } = CustomizableUI;
+
+ let widgets = CustomizableUI.getWidgetIdsInArea(AREA_NAVBAR).
+ filter((id) => id.startsWith('toggle-button--') && id.endsWith(buttonId));
+
+ if (widgets.length === 0)
+ throw new Error('Widget with id `' + id +'` not found.');
+
+ if (widgets.length > 1)
+ throw new Error('Unexpected number of widgets: ' + widgets.length)
+
+ return CustomizableUI.getWidget(widgets[0]).forWindow(window);
+};
+
+exports['test basic constructor validation'] = function(assert) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+
+ assert.throws(
+ () => ToggleButton({}),
+ /^The option/,
+ 'throws on no option given');
+
+ // Test no label
+ assert.throws(
+ () => ToggleButton({ id: 'my-button', icon: './icon.png'}),
+ /^The option "label"/,
+ 'throws on no label given');
+
+ // Test no id
+ assert.throws(
+ () => ToggleButton({ label: 'my button', icon: './icon.png' }),
+ /^The option "id"/,
+ 'throws on no id given');
+
+ // Test no icon
+ assert.throws(
+ () => ToggleButton({ id: 'my-button', label: 'my button' }),
+ /^The option "icon"/,
+ 'throws on no icon given');
+
+
+ // Test empty label
+ assert.throws(
+ () => ToggleButton({ id: 'my-button', label: '', icon: './icon.png' }),
+ /^The option "label"/,
+ 'throws on no valid label given');
+
+ // Test invalid id
+ assert.throws(
+ () => ToggleButton({ id: 'my button', label: 'my button', icon: './icon.png' }),
+ /^The option "id"/,
+ 'throws on no valid id given');
+
+ // Test empty id
+ assert.throws(
+ () => ToggleButton({ id: '', label: 'my button', icon: './icon.png' }),
+ /^The option "id"/,
+ 'throws on no valid id given');
+
+ // Test remote icon
+ assert.throws(
+ () => ToggleButton({ id: 'my-button', label: 'my button', icon: 'http://www.mozilla.org/favicon.ico'}),
+ /^The option "icon"/,
+ 'throws on no valid icon given');
+
+ // Test wrong icon: no absolute URI to local resource, neither relative './'
+ assert.throws(
+ () => ToggleButton({ id: 'my-button', label: 'my button', icon: 'icon.png'}),
+ /^The option "icon"/,
+ 'throws on no valid icon given');
+
+ // Test wrong icon: no absolute URI to local resource, neither relative './'
+ assert.throws(
+ () => ToggleButton({ id: 'my-button', label: 'my button', icon: 'foo and bar'}),
+ /^The option "icon"/,
+ 'throws on no valid icon given');
+
+ // Test wrong icon: '../' is not allowed
+ assert.throws(
+ () => ToggleButton({ id: 'my-button', label: 'my button', icon: '../icon.png'}),
+ /^The option "icon"/,
+ 'throws on no valid icon given');
+
+ assert.throws(
+ () => ToggleButton({ id: 'my-button', label: 'button', icon: './i.png', badge: true}),
+ /^The option "badge"/,
+ 'throws on no valid badge given');
+
+ assert.throws(
+ () => ToggleButton({ id: 'my-button', label: 'button', icon: './i.png', badgeColor: true}),
+ /^The option "badgeColor"/,
+ 'throws on no valid badge given');
+
+ loader.unload();
+};
+
+exports['test button added'] = function(assert) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+
+ let button = ToggleButton({
+ id: 'my-button-1',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ // check defaults
+ assert.equal(button.disabled, false,
+ 'disabled is set to default `false` value');
+
+ let { node } = getWidget(button.id);
+
+ assert.ok(!!node, 'The button is in the navbar');
+
+ assert.equal(button.label, node.getAttribute('label'),
+ 'label is set');
+
+ assert.equal(button.label, node.getAttribute('tooltiptext'),
+ 'tooltip is set');
+
+ assert.equal(data.url(button.icon.substr(2)), node.getAttribute('image'),
+ 'icon is set');
+
+ assert.equal("", node.getAttribute('badge'),
+ 'badge attribute is empty');
+
+ loader.unload();
+}
+exports['test button is not garbaged'] = function (assert, done) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+
+ ToggleButton({
+ id: 'my-button-1',
+ label: 'my button',
+ icon: './icon.png',
+ onClick: () => {
+ loader.unload();
+ done();
+ }
+ });
+
+ gc().then(() => {
+ let { node } = getWidget('my-button-1');
+
+ assert.ok(!!node, 'The button is in the navbar');
+
+ assert.equal('my button', node.getAttribute('label'),
+ 'label is set');
+
+ assert.equal(data.url('icon.png'), node.getAttribute('image'),
+ 'icon is set');
+
+ // ensure the listener is not gc'ed too
+ node.click();
+ }).catch(assert.fail);
+}
+
+exports['test button added with resource URI'] = function(assert) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+
+ let button = ToggleButton({
+ id: 'my-button-1',
+ label: 'my button',
+ icon: data.url('icon.png')
+ });
+
+ assert.equal(button.icon, data.url('icon.png'),
+ 'icon is set');
+
+ let { node } = getWidget(button.id);
+
+ assert.equal(button.icon, node.getAttribute('image'),
+ 'icon on node is set');
+
+ loader.unload();
+}
+
+exports['test button duplicate id'] = function(assert) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+
+ let button = ToggleButton({
+ id: 'my-button-2',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ assert.throws(() => {
+ let doppelganger = ToggleButton({
+ id: 'my-button-2',
+ label: 'my button',
+ icon: './icon.png'
+ });
+ },
+ /^The ID/,
+ 'No duplicates allowed');
+
+ loader.unload();
+}
+
+exports['test button multiple destroy'] = function(assert) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+
+ let button = ToggleButton({
+ id: 'my-button-2',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ button.destroy();
+ button.destroy();
+ button.destroy();
+
+ assert.pass('multiple destroy doesn\'t matter');
+
+ loader.unload();
+}
+
+exports['test button removed on dispose'] = function(assert, done) {
+ const { CustomizableUI } = Cu.import('resource:///modules/CustomizableUI.jsm', {});
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+
+ let widgetId;
+
+ CustomizableUI.addListener({
+ onWidgetDestroyed: function(id) {
+ if (id === widgetId) {
+ CustomizableUI.removeListener(this);
+
+ assert.pass('button properly removed');
+ loader.unload();
+ done();
+ }
+ }
+ });
+
+ let button = ToggleButton({
+ id: 'my-button-3',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ // Tried to use `getWidgetIdsInArea` but seems undefined, not sure if it
+ // was removed or it's not in the UX build yet
+ widgetId = getWidget(button.id).id;
+
+ button.destroy();
+};
+
+exports['test button global state updated'] = function(assert) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+
+ let button = ToggleButton({
+ id: 'my-button-4',
+ label: 'my button',
+ icon: './icon.png',
+ });
+
+ // Tried to use `getWidgetIdsInArea` but seems undefined, not sure if it
+ // was removed or it's not in the UX build yet
+
+ let { node, id: widgetId } = getWidget(button.id);
+
+ // check read-only properties
+
+ assert.throws(() => button.id = 'another-id',
+ /^setting a property that has only a getter/,
+ 'id cannot be set at runtime');
+
+ assert.equal(button.id, 'my-button-4',
+ 'id is unchanged');
+ assert.equal(node.id, widgetId,
+ 'node id is unchanged');
+
+ // check writable properties
+
+ button.label = 'New label';
+ assert.equal(button.label, 'New label',
+ 'label is updated');
+ assert.equal(node.getAttribute('label'), 'New label',
+ 'node label is updated');
+ assert.equal(node.getAttribute('tooltiptext'), 'New label',
+ 'node tooltip is updated');
+
+ button.icon = './new-icon.png';
+ assert.equal(button.icon, './new-icon.png',
+ 'icon is updated');
+ assert.equal(node.getAttribute('image'), data.url('new-icon.png'),
+ 'node image is updated');
+
+ button.disabled = true;
+ assert.equal(button.disabled, true,
+ 'disabled is updated');
+ assert.equal(node.getAttribute('disabled'), 'true',
+ 'node disabled is updated');
+
+ button.badge = '+2';
+ button.badgeColor = 'blue';
+
+ assert.equal(button.badge, '+2',
+ 'badge is updated');
+ assert.equal(node.getAttribute('bagde'), '',
+ 'node badge is updated');
+
+ assert.equal(button.badgeColor, 'blue',
+ 'badgeColor is updated');
+ assert.equal(badgeNodeFor(node).style.backgroundColor, 'blue',
+ 'badge color is updated');
+
+ // TODO: test validation on update
+
+ loader.unload();
+}
+
+exports['test button global state set and get with state method'] = function(assert) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+
+ let button = ToggleButton({
+ id: 'my-button-16',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ // read the button's state
+ let state = button.state(button);
+
+ assert.equal(state.label, 'my button',
+ 'label is correct');
+ assert.equal(state.icon, './icon.png',
+ 'icon is correct');
+ assert.equal(state.disabled, false,
+ 'disabled is correct');
+
+ // set the new button's state
+ button.state(button, {
+ label: 'New label',
+ icon: './new-icon.png',
+ disabled: true,
+ badge: '+2',
+ badgeColor: 'blue'
+ });
+
+ assert.equal(button.label, 'New label',
+ 'label is updated');
+ assert.equal(button.icon, './new-icon.png',
+ 'icon is updated');
+ assert.equal(button.disabled, true,
+ 'disabled is updated');
+ assert.equal(button.badge, '+2',
+ 'badge is updated');
+ assert.equal(button.badgeColor, 'blue',
+ 'badgeColor is updated');
+
+ loader.unload();
+}
+
+exports['test button global state updated on multiple windows'] = function*(assert) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+
+ let button = ToggleButton({
+ id: 'my-button-5',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ let nodes = [getWidget(button.id).node];
+
+ let window = yield openBrowserWindow();
+
+ nodes.push(getWidget(button.id, window).node);
+
+ button.label = 'New label';
+ button.icon = './new-icon.png';
+ button.disabled = true;
+ button.badge = '+10';
+ button.badgeColor = 'green';
+
+ for (let node of nodes) {
+ assert.equal(node.getAttribute('label'), 'New label',
+ 'node label is updated');
+ assert.equal(node.getAttribute('tooltiptext'), 'New label',
+ 'node tooltip is updated');
+
+ assert.equal(button.icon, './new-icon.png',
+ 'icon is updated');
+ assert.equal(node.getAttribute('image'), data.url('new-icon.png'),
+ 'node image is updated');
+
+ assert.equal(button.disabled, true,
+ 'disabled is updated');
+ assert.equal(node.getAttribute('disabled'), 'true',
+ 'node disabled is updated');
+
+ assert.equal(button.badge, '+10',
+ 'badge is updated')
+ assert.equal(button.badgeColor, 'green',
+ 'badgeColor is updated')
+ assert.equal(node.getAttribute('badge'), '+10',
+ 'node badge is updated')
+ assert.equal(badgeNodeFor(node).style.backgroundColor, 'green',
+ 'node badge color is updated')
+ };
+
+ yield close(window);
+
+ loader.unload();
+};
+
+exports['test button window state'] = function*(assert) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+ let { browserWindows } = loader.require('sdk/windows');
+
+ let button = ToggleButton({
+ id: 'my-button-6',
+ label: 'my button',
+ icon: './icon.png',
+ badge: '+1',
+ badgeColor: 'red'
+ });
+
+ let mainWindow = browserWindows.activeWindow;
+ let nodes = [getWidget(button.id).node];
+
+ let window = yield openBrowserWindow().then(focus);
+
+ nodes.push(getWidget(button.id, window).node);
+
+ let { activeWindow } = browserWindows;
+
+ button.state(activeWindow, {
+ label: 'New label',
+ icon: './new-icon.png',
+ disabled: true,
+ badge: '+2',
+ badgeColor : 'green'
+ });
+
+ // check the states
+
+ assert.equal(button.label, 'my button',
+ 'global label unchanged');
+ assert.equal(button.icon, './icon.png',
+ 'global icon unchanged');
+ assert.equal(button.disabled, false,
+ 'global disabled unchanged');
+ assert.equal(button.badge, '+1',
+ 'global badge unchanged');
+ assert.equal(button.badgeColor, 'red',
+ 'global badgeColor unchanged');
+
+ let state = button.state(mainWindow);
+
+ assert.equal(state.label, 'my button',
+ 'previous window label unchanged');
+ assert.equal(state.icon, './icon.png',
+ 'previous window icon unchanged');
+ assert.equal(state.disabled, false,
+ 'previous window disabled unchanged');
+ assert.deepEqual(button.badge, '+1',
+ 'previouswindow badge unchanged');
+ assert.deepEqual(button.badgeColor, 'red',
+ 'previous window badgeColor unchanged');
+
+ state = button.state(activeWindow);
+
+ assert.equal(state.label, 'New label',
+ 'active window label updated');
+ assert.equal(state.icon, './new-icon.png',
+ 'active window icon updated');
+ assert.equal(state.disabled, true,
+ 'active disabled updated');
+ assert.equal(state.badge, '+2',
+ 'active badge updated');
+ assert.equal(state.badgeColor, 'green',
+ 'active badgeColor updated');
+
+ // change the global state, only the windows without a state are affected
+
+ button.label = 'A good label';
+ button.badge = '+3';
+
+ assert.equal(button.label, 'A good label',
+ 'global label updated');
+ assert.equal(button.state(mainWindow).label, 'A good label',
+ 'previous window label updated');
+ assert.equal(button.state(activeWindow).label, 'New label',
+ 'active window label unchanged');
+ assert.equal(button.state(activeWindow).badge, '+2',
+ 'active badge unchanged');
+ assert.equal(button.state(activeWindow).badgeColor, 'green',
+ 'active badgeColor unchanged');
+ assert.equal(button.state(mainWindow).badge, '+3',
+ 'previous window badge updated');
+ assert.equal(button.state(mainWindow).badgeColor, 'red',
+ 'previous window badgeColor unchanged');
+
+ // delete the window state will inherits the global state again
+
+ button.state(activeWindow, null);
+
+ state = button.state(activeWindow);
+
+ assert.equal(state.label, 'A good label',
+ 'active window label inherited');
+ assert.equal(state.badge, '+3',
+ 'previous window badge inherited');
+ assert.equal(button.badgeColor, 'red',
+ 'previous window badgeColor inherited');
+
+ // check the nodes properties
+ let node = nodes[0];
+
+ state = button.state(mainWindow);
+
+ assert.equal(node.getAttribute('label'), state.label,
+ 'node label is correct');
+ assert.equal(node.getAttribute('tooltiptext'), state.label,
+ 'node tooltip is correct');
+
+ assert.equal(node.getAttribute('image'), data.url(state.icon.substr(2)),
+ 'node image is correct');
+ assert.equal(node.hasAttribute('disabled'), state.disabled,
+ 'disabled is correct');
+ assert.equal(node.getAttribute("badge"), state.badge,
+ 'badge is correct');
+
+ assert.equal(badgeNodeFor(node).style.backgroundColor, state.badgeColor,
+ 'badge color is correct');
+
+ node = nodes[1];
+ state = button.state(activeWindow);
+
+ assert.equal(node.getAttribute('label'), state.label,
+ 'node label is correct');
+ assert.equal(node.getAttribute('tooltiptext'), state.label,
+ 'node tooltip is correct');
+
+ assert.equal(node.getAttribute('image'), data.url(state.icon.substr(2)),
+ 'node image is correct');
+ assert.equal(node.hasAttribute('disabled'), state.disabled,
+ 'disabled is correct');
+ assert.equal(node.getAttribute('badge'), state.badge,
+ 'badge is correct');
+
+ assert.equal(badgeNodeFor(node).style.backgroundColor, state.badgeColor,
+ 'badge color is correct');
+
+ yield close(window);
+
+ loader.unload();
+};
+
+
+exports['test button tab state'] = function*(assert) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+ let { browserWindows } = loader.require('sdk/windows');
+ let tabs = loader.require('sdk/tabs');
+
+ let button = ToggleButton({
+ id: 'my-button-7',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ let mainTab = tabs.activeTab;
+ let node = getWidget(button.id).node;
+
+ tabs.open('about:blank');
+
+ yield wait(tabs, 'ready');
+
+ let tab = tabs.activeTab;
+ let { activeWindow } = browserWindows;
+
+ // set window state
+ button.state(activeWindow, {
+ label: 'Window label',
+ icon: './window-icon.png',
+ badge: 'win',
+ badgeColor: 'blue'
+ });
+
+ // set previous active tab state
+ button.state(mainTab, {
+ label: 'Tab label',
+ icon: './tab-icon.png',
+ badge: 'tab',
+ badgeColor: 'red'
+ });
+
+ // set current active tab state
+ button.state(tab, {
+ icon: './another-tab-icon.png',
+ disabled: true,
+ badge: 't1',
+ badgeColor: 'green'
+ });
+
+ // check the states, be sure they won't be gc'ed
+ yield gc();
+
+ assert.equal(button.label, 'my button',
+ 'global label unchanged');
+ assert.equal(button.icon, './icon.png',
+ 'global icon unchanged');
+ assert.equal(button.disabled, false,
+ 'global disabled unchanged');
+ assert.equal(button.badge, undefined,
+ 'global badge unchanged')
+
+ let state = button.state(mainTab);
+
+ assert.equal(state.label, 'Tab label',
+ 'previous tab label updated');
+ assert.equal(state.icon, './tab-icon.png',
+ 'previous tab icon updated');
+ assert.equal(state.disabled, false,
+ 'previous tab disabled unchanged');
+ assert.equal(state.badge, 'tab',
+ 'previous tab badge unchanged')
+ assert.equal(state.badgeColor, 'red',
+ 'previous tab badgeColor unchanged')
+
+ state = button.state(tab);
+
+ assert.equal(state.label, 'Window label',
+ 'active tab inherited from window state');
+ assert.equal(state.icon, './another-tab-icon.png',
+ 'active tab icon updated');
+ assert.equal(state.disabled, true,
+ 'active disabled updated');
+ assert.equal(state.badge, 't1',
+ 'active badge updated');
+ assert.equal(state.badgeColor, 'green',
+ 'active badgeColor updated');
+
+ // change the global state
+ button.icon = './good-icon.png';
+
+ // delete the tab state
+ button.state(tab, null);
+
+ assert.equal(button.icon, './good-icon.png',
+ 'global icon updated');
+ assert.equal(button.state(mainTab).icon, './tab-icon.png',
+ 'previous tab icon unchanged');
+ assert.equal(button.state(tab).icon, './window-icon.png',
+ 'tab icon inherited from window');
+ assert.equal(button.state(mainTab).badge, 'tab',
+ 'previous tab badge is unchaged');
+ assert.equal(button.state(tab).badge, 'win',
+ 'tab badge is inherited from window');
+
+ // delete the window state
+ button.state(activeWindow, null);
+
+ state = button.state(tab);
+
+ assert.equal(state.icon, './good-icon.png',
+ 'tab icon inherited from global');
+ assert.equal(state.badge, undefined,
+ 'tab badge inherited from global');
+ assert.equal(state.badgeColor, undefined,
+ 'tab badgeColor inherited from global');
+
+ // check the node properties
+ yield wait();
+
+ assert.equal(node.getAttribute('label'), state.label,
+ 'node label is correct');
+ assert.equal(node.getAttribute('tooltiptext'), state.label,
+ 'node tooltip is correct');
+ assert.equal(node.getAttribute('image'), data.url(state.icon.substr(2)),
+ 'node image is correct');
+ assert.equal(node.hasAttribute('disabled'), state.disabled,
+ 'node disabled is correct');
+ assert.equal(node.getAttribute('badge'), '',
+ 'badge text is correct');
+ assert.equal(badgeNodeFor(node).style.backgroundColor, '',
+ 'badge color is correct');
+
+ mainTab.activate();
+
+ yield wait(tabs, 'activate');
+
+ // This is made in order to avoid to check the node before it
+ // is updated, need a better check
+ yield wait();
+
+ state = button.state(mainTab);
+
+ assert.equal(node.getAttribute('label'), state.label,
+ 'node label is correct');
+ assert.equal(node.getAttribute('tooltiptext'), state.label,
+ 'node tooltip is correct');
+ assert.equal(node.getAttribute('image'), data.url(state.icon.substr(2)),
+ 'node image is correct');
+ assert.equal(node.hasAttribute('disabled'), state.disabled,
+ 'disabled is correct');
+ assert.equal(node.getAttribute('badge'), state.badge,
+ 'badge text is correct');
+ assert.equal(badgeNodeFor(node).style.backgroundColor, state.badgeColor,
+ 'badge color is correct');
+
+ tab.close(loader.unload);
+
+ loader.unload();
+};
+
+exports['test button click'] = function*(assert) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+ let { browserWindows } = loader.require('sdk/windows');
+
+ let labels = [];
+
+ let button = ToggleButton({
+ id: 'my-button-8',
+ label: 'my button',
+ icon: './icon.png',
+ onClick: ({label}) => labels.push(label)
+ });
+
+ let mainWindow = browserWindows.activeWindow;
+ let chromeWindow = getMostRecentBrowserWindow();
+
+ let window = yield openBrowserWindow().then(focus);
+
+ button.state(mainWindow, { label: 'nothing' });
+ button.state(mainWindow.tabs.activeTab, { label: 'foo'})
+ button.state(browserWindows.activeWindow, { label: 'bar' });
+
+ button.click();
+
+ yield focus(chromeWindow);
+
+ button.click();
+
+ assert.deepEqual(labels, ['bar', 'foo'],
+ 'button click works');
+
+ yield close(window);
+
+ loader.unload();
+}
+
+exports['test button icon set'] = function(assert) {
+ const { CustomizableUI } = Cu.import('resource:///modules/CustomizableUI.jsm', {});
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+
+ // Test remote icon set
+ assert.throws(
+ () => ToggleButton({
+ id: 'my-button-10',
+ label: 'my button',
+ icon: {
+ '16': 'http://www.mozilla.org/favicon.ico'
+ }
+ }),
+ /^The option "icon"/,
+ 'throws on no valid icon given');
+
+ let button = ToggleButton({
+ id: 'my-button-11',
+ label: 'my button',
+ icon: {
+ '5': './icon5.png',
+ '16': './icon16.png',
+ '32': './icon32.png',
+ '64': './icon64.png'
+ }
+ });
+
+ let { node, id: widgetId } = getWidget(button.id);
+ let { devicePixelRatio } = node.ownerDocument.defaultView;
+
+ let size = 16 * devicePixelRatio;
+
+ assert.equal(node.getAttribute('image'), data.url(button.icon[size].substr(2)),
+ 'the icon is set properly in navbar');
+
+ size = 32 * devicePixelRatio;
+
+ CustomizableUI.addWidgetToArea(widgetId, CustomizableUI.AREA_PANEL);
+
+ assert.equal(node.getAttribute('image'), data.url(button.icon[size].substr(2)),
+ 'the icon is set properly in panel');
+
+ // Using `loader.unload` without move back the button to the original area
+ // raises an error in the CustomizableUI. This is doesn't happen if the
+ // button is moved manually from navbar to panel. I believe it has to do
+ // with `addWidgetToArea` method, because even with a `timeout` the issue
+ // persist.
+ CustomizableUI.addWidgetToArea(widgetId, CustomizableUI.AREA_NAVBAR);
+
+ loader.unload();
+}
+
+exports['test button icon set with only one option'] = function(assert) {
+ const { CustomizableUI } = Cu.import('resource:///modules/CustomizableUI.jsm', {});
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+
+ // Test remote icon set
+ assert.throws(
+ () => ToggleButton({
+ id: 'my-button-10',
+ label: 'my button',
+ icon: {
+ '16': 'http://www.mozilla.org/favicon.ico'
+ }
+ }),
+ /^The option "icon"/,
+ 'throws on no valid icon given');
+
+ let button = ToggleButton({
+ id: 'my-button-11',
+ label: 'my button',
+ icon: {
+ '5': './icon5.png'
+ }
+ });
+
+ let { node, id: widgetId } = getWidget(button.id);
+
+ assert.equal(node.getAttribute('image'), data.url(button.icon['5'].substr(2)),
+ 'the icon is set properly in navbar');
+
+ CustomizableUI.addWidgetToArea(widgetId, CustomizableUI.AREA_PANEL);
+
+ assert.equal(node.getAttribute('image'), data.url(button.icon['5'].substr(2)),
+ 'the icon is set properly in panel');
+
+ // Using `loader.unload` without move back the button to the original area
+ // raises an error in the CustomizableUI. This is doesn't happen if the
+ // button is moved manually from navbar to panel. I believe it has to do
+ // with `addWidgetToArea` method, because even with a `timeout` the issue
+ // persist.
+ CustomizableUI.addWidgetToArea(widgetId, CustomizableUI.AREA_NAVBAR);
+
+ loader.unload();
+}
+
+exports['test button state validation'] = function(assert) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+ let { browserWindows } = loader.require('sdk/windows');
+
+ let button = ToggleButton({
+ id: 'my-button-12',
+ label: 'my button',
+ icon: './icon.png'
+ })
+
+ let state = button.state(button);
+
+ assert.throws(
+ () => button.state(button, { icon: 'http://www.mozilla.org/favicon.ico' }),
+ /^The option "icon"/,
+ 'throws on remote icon given');
+
+ assert.throws(
+ () => button.state(button, { badge: true } ),
+ /^The option "badge"/,
+ 'throws on wrong badge value given');
+
+ loader.unload();
+};
+
+exports['test button are not in private windows'] = function(assert, done) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+ let{ isPrivate } = loader.require('sdk/private-browsing');
+ let { browserWindows } = loader.require('sdk/windows');
+
+ let button = ToggleButton({
+ id: 'my-button-13',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ openPrivateBrowserWindow().then(window => {
+ assert.ok(isPrivate(window),
+ 'the new window is private');
+
+ let { node } = getWidget(button.id, window);
+
+ assert.ok(!node || node.style.display === 'none',
+ 'the button is not added / is not visible on private window');
+
+ return window;
+ }).
+ then(close).
+ then(loader.unload).
+ then(done, assert.fail)
+}
+
+exports['test button state are snapshot'] = function(assert) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+ let { browserWindows } = loader.require('sdk/windows');
+ let tabs = loader.require('sdk/tabs');
+
+ let button = ToggleButton({
+ id: 'my-button-14',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ let state = button.state(button);
+ let windowState = button.state(browserWindows.activeWindow);
+ let tabState = button.state(tabs.activeTab);
+
+ assert.deepEqual(windowState, state,
+ 'window state has the same properties of button state');
+
+ assert.deepEqual(tabState, state,
+ 'tab state has the same properties of button state');
+
+ assert.notEqual(windowState, state,
+ 'window state is not the same object of button state');
+
+ assert.notEqual(tabState, state,
+ 'tab state is not the same object of button state');
+
+ assert.deepEqual(button.state(button), state,
+ 'button state has the same content of previous button state');
+
+ assert.deepEqual(button.state(browserWindows.activeWindow), windowState,
+ 'window state has the same content of previous window state');
+
+ assert.deepEqual(button.state(tabs.activeTab), tabState,
+ 'tab state has the same content of previous tab state');
+
+ assert.notEqual(button.state(button), state,
+ 'button state is not the same object of previous button state');
+
+ assert.notEqual(button.state(browserWindows.activeWindow), windowState,
+ 'window state is not the same object of previous window state');
+
+ assert.notEqual(button.state(tabs.activeTab), tabState,
+ 'tab state is not the same object of previous tab state');
+
+ loader.unload();
+}
+
+exports['test button icon object is a snapshot'] = function(assert) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+
+ let icon = {
+ '16': './foo.png'
+ };
+
+ let button = ToggleButton({
+ id: 'my-button-17',
+ label: 'my button',
+ icon: icon
+ });
+
+ assert.deepEqual(button.icon, icon,
+ 'button.icon has the same properties of the object set in the constructor');
+
+ assert.notEqual(button.icon, icon,
+ 'button.icon is not the same object of the object set in the constructor');
+
+ assert.throws(
+ () => button.icon[16] = './bar.png',
+ /16 is read-only/,
+ 'properties of button.icon are ready-only'
+ );
+
+ let newIcon = {'16': './bar.png'};
+ button.icon = newIcon;
+
+ assert.deepEqual(button.icon, newIcon,
+ 'button.icon has the same properties of the object set');
+
+ assert.notEqual(button.icon, newIcon,
+ 'button.icon is not the same object of the object set');
+
+ loader.unload();
+}
+
+exports['test button after destroy'] = function(assert) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+ let { browserWindows } = loader.require('sdk/windows');
+ let { activeTab } = loader.require('sdk/tabs');
+
+ let button = ToggleButton({
+ id: 'my-button-15',
+ label: 'my button',
+ icon: './icon.png',
+ onClick: () => assert.fail('onClick should not be called')
+ });
+
+ button.destroy();
+
+ assert.throws(
+ () => button.click(),
+ /^The state cannot be set or get/,
+ 'button.click() not executed');
+
+ assert.throws(
+ () => button.label,
+ /^The state cannot be set or get/,
+ 'button.label cannot be get after destroy');
+
+ assert.throws(
+ () => button.label = 'my label',
+ /^The state cannot be set or get/,
+ 'button.label cannot be set after destroy');
+
+ assert.throws(
+ () => {
+ button.state(browserWindows.activeWindow, {
+ label: 'window label'
+ });
+ },
+ /^The state cannot be set or get/,
+ 'window state label cannot be set after destroy');
+
+ assert.throws(
+ () => button.state(browserWindows.activeWindow).label,
+ /^The state cannot be set or get/,
+ 'window state label cannot be get after destroy');
+
+ assert.throws(
+ () => {
+ button.state(activeTab, {
+ label: 'tab label'
+ });
+ },
+ /^The state cannot be set or get/,
+ 'tab state label cannot be set after destroy');
+
+ assert.throws(
+ () => button.state(activeTab).label,
+ /^The state cannot be set or get/,
+ 'window state label cannot se get after destroy');
+
+ loader.unload();
+};
+
+exports['test button badge property'] = function(assert) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+
+ let button = ToggleButton({
+ id: 'my-button-18',
+ label: 'my button',
+ icon: './icon.png',
+ badge: 123456
+ });
+
+ assert.equal(button.badge, 123456,
+ 'badge is set');
+
+ assert.equal(button.badgeColor, undefined,
+ 'badge color is not set');
+
+ let { node } = getWidget(button.id);
+ let { getComputedStyle } = node.ownerDocument.defaultView;
+ let badgeNode = badgeNodeFor(node);
+
+ assert.equal('1234', node.getAttribute('badge'),
+ 'badge text is displayed up to four characters');
+
+ assert.equal(getComputedStyle(badgeNode).backgroundColor, 'rgb(217, 0, 0)',
+ 'badge color is the default one');
+
+ button.badge = '危機';
+
+ assert.equal(button.badge, '危機',
+ 'badge is properly set');
+
+ assert.equal('危機', node.getAttribute('badge'),
+ 'badge text is displayed');
+
+ button.badge = '🐶🐰🐹';
+
+ assert.equal(button.badge, '🐶🐰🐹',
+ 'badge is properly set');
+
+ assert.equal('🐶🐰🐹', node.getAttribute('badge'),
+ 'badge text is displayed');
+
+ loader.unload();
+}
+exports['test button badge color'] = function(assert) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+
+ let button = ToggleButton({
+ id: 'my-button-19',
+ label: 'my button',
+ icon: './icon.png',
+ badge: '+1',
+ badgeColor: 'blue'
+ });
+
+ assert.equal(button.badgeColor, 'blue',
+ 'badge color is set');
+
+ let { node } = getWidget(button.id);
+ let { getComputedStyle } = node.ownerDocument.defaultView;
+ let badgeNode = badgeNodeFor(node);
+
+ assert.equal(badgeNodeFor(node).style.backgroundColor, 'blue',
+ 'badge color is displayed properly');
+ assert.equal(getComputedStyle(badgeNode).backgroundColor, 'rgb(0, 0, 255)',
+ 'badge color overrides the default one');
+
+ loader.unload();
+}
+
+// toggle button only
+exports['test button checked'] = function(assert, done) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+ let { browserWindows } = loader.require('sdk/windows');
+
+ let events = [];
+
+ let button = ToggleButton({
+ id: 'my-button-9',
+ label: 'my button',
+ icon: './icon.png',
+ checked: true,
+ onClick: ({label}) => events.push('clicked:' + label),
+ onChange: state => events.push('changed:' + state.label + ':' + state.checked)
+ });
+
+ let { node } = getWidget(button.id);
+
+ assert.equal(node.getAttribute('type'), 'checkbox',
+ 'node type is properly set');
+
+ let mainWindow = browserWindows.activeWindow;
+ let chromeWindow = getMostRecentBrowserWindow();
+
+ openBrowserWindow().then(focus).then(window => {
+ button.state(mainWindow, { label: 'nothing' });
+ button.state(mainWindow.tabs.activeTab, { label: 'foo'})
+ button.state(browserWindows.activeWindow, { label: 'bar' });
+
+ button.click();
+ button.click();
+
+ focus(chromeWindow).then(() => {
+ button.click();
+ button.click();
+
+ assert.deepEqual(events, [
+ 'clicked:bar', 'changed:bar:false', 'clicked:bar', 'changed:bar:true',
+ 'clicked:foo', 'changed:foo:false', 'clicked:foo', 'changed:foo:true'
+ ],
+ 'button change events works');
+
+ close(window).
+ then(loader.unload).
+ then(done, assert.fail);
+ })
+ }).then(null, assert.fail);
+}
+
+exports['test button is checked on window level'] = function(assert, done) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+ let { browserWindows } = loader.require('sdk/windows');
+ let tabs = loader.require('sdk/tabs');
+
+ let button = ToggleButton({
+ id: 'my-button-20',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ let mainWindow = browserWindows.activeWindow;
+ let mainTab = tabs.activeTab;
+
+ assert.equal(button.checked, false,
+ 'global state, checked is `false`.');
+ assert.equal(button.state(mainTab).checked, false,
+ 'tab state, checked is `false`.');
+ assert.equal(button.state(mainWindow).checked, false,
+ 'window state, checked is `false`.');
+
+ button.click();
+
+ tabs.open({
+ url: 'about:blank',
+ onActivate: function onActivate(tab) {
+ tab.removeListener('activate', onActivate);
+
+ assert.notEqual(mainTab, tab,
+ 'the current tab is not the same.');
+
+ assert.equal(button.checked, false,
+ 'global state, checked is `false`.');
+ assert.equal(button.state(mainTab).checked, true,
+ 'previous tab state, checked is `true`.');
+ assert.equal(button.state(tab).checked, true,
+ 'current tab state, checked is `true`.');
+ assert.equal(button.state(mainWindow).checked, true,
+ 'window state, checked is `true`.');
+
+ openBrowserWindow().then(focus).then(window => {
+ let { activeWindow } = browserWindows;
+ let { activeTab } = activeWindow.tabs;
+
+ assert.equal(button.checked, false,
+ 'global state, checked is `false`.');
+ assert.equal(button.state(activeTab).checked, false,
+ 'tab state, checked is `false`.');
+
+ assert.equal(button.state(activeWindow).checked, false,
+ 'window state, checked is `false`.');
+
+ tab.close(()=> {
+ close(window).
+ then(loader.unload).
+ then(done, assert.fail);
+ })
+ }).
+ then(null, assert.fail);
+ }
+ });
+
+};
+
+exports['test button click do not messing up states'] = function(assert) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+ let { browserWindows } = loader.require('sdk/windows');
+
+ let button = ToggleButton({
+ id: 'my-button-21',
+ label: 'my button',
+ icon: './icon.png'
+ });
+
+ let mainWindow = browserWindows.activeWindow;
+ let { activeTab } = mainWindow.tabs;
+
+ button.state(mainWindow, { icon: './new-icon.png' });
+ button.state(activeTab, { label: 'foo'})
+
+ assert.equal(button.state(mainWindow).label, 'my button',
+ 'label property for window state, properly derived from global state');
+
+ assert.equal(button.state(activeTab).icon, './new-icon.png',
+ 'icon property for tab state, properly derived from window state');
+
+ button.click();
+
+ button.label = 'bar';
+
+ assert.equal(button.state(mainWindow).label, 'bar',
+ 'label property for window state, properly derived from global state');
+
+ button.state(mainWindow, null);
+
+ assert.equal(button.state(activeTab).icon, './icon.png',
+ 'icon property for tab state, properly derived from window state');
+
+ loader.unload();
+}
+
+exports['test buttons can have anchored panels'] = function(assert, done) {
+ let loader = Loader(module);
+ let { ToggleButton } = loader.require('sdk/ui');
+ let { Panel } = loader.require('sdk/panel');
+ let { identify } = loader.require('sdk/ui/id');
+ let { getActiveView } = loader.require('sdk/view/core');
+
+ let b1 = ToggleButton({
+ id: 'my-button-22',
+ label: 'my button',
+ icon: './icon.png',
+ onChange: ({checked}) => checked && panel.show()
+ });
+
+ let b2 = ToggleButton({
+ id: 'my-button-23',
+ label: 'my button',
+ icon: './icon.png',
+ onChange: ({checked}) => checked && panel.show({position: b2})
+ });
+
+ let panel = Panel({
+ position: b1
+ });
+
+ let { document } = getMostRecentBrowserWindow();
+ let b1Node = document.getElementById(identify(b1));
+ let b2Node = document.getElementById(identify(b2));
+ let panelNode = getActiveView(panel);
+
+ panel.once('show', () => {
+ assert.ok(b1.state('window').checked,
+ 'button is checked');
+
+ assert.equal(panelNode.getAttribute('type'), 'arrow',
+ 'the panel is a arrow type');
+
+ assert.strictEqual(b1Node, panelNode.anchorNode,
+ 'the panel is anchored properly to the button given in costructor');
+
+ panel.hide();
+
+ panel.once('show', () => {
+ assert.ok(b2.state('window').checked,
+ 'button is checked');
+
+ assert.equal(panelNode.getAttribute('type'), 'arrow',
+ 'the panel is a arrow type');
+
+ // test also that the button passed in `show` method, takes the precedence
+ // over the button set in panel's constructor.
+ assert.strictEqual(b2Node, panelNode.anchorNode,
+ 'the panel is anchored properly to the button passed to show method');
+
+ loader.unload();
+
+ done();
+ });
+
+ b2.click();
+ });
+
+ b1.click();
+}
+
+
+if (packaging.isNative) {
+ module.exports = {
+ "test skip on jpm": (assert) => assert.pass("skipping this file with jpm")
+ };
+}
+
+require("sdk/test").run(module.exports);
diff --git a/addon-sdk/source/test/test-ui-toolbar.js b/addon-sdk/source/test/test-ui-toolbar.js
new file mode 100644
index 000000000..30217b300
--- /dev/null
+++ b/addon-sdk/source/test/test-ui-toolbar.js
@@ -0,0 +1,511 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+module.metadata = {
+ "engines": {
+ "Firefox": "*"
+ }
+};
+
+const { Toolbar } = require("sdk/ui/toolbar");
+const { Loader } = require("sdk/test/loader");
+const { identify } = require("sdk/ui/id");
+const { getMostRecentBrowserWindow, open, getOuterId } = require("sdk/window/utils");
+const { ready, close } = require("sdk/window/helpers");
+const { defer } = require("sdk/core/promise");
+const { send, stop, Reactor } = require("sdk/event/utils");
+const { object } = require("sdk/util/sequence");
+const { CustomizationInput } = require("sdk/input/customizable-ui");
+const { OutputPort } = require("sdk/output/system");
+const output = new OutputPort({ id: "toolbar-change" });
+const { cleanUI } = require('sdk/test/utils');
+const tabs = require("sdk/tabs");
+
+const wait = (toolbar, event) => {
+ let { promise, resolve } = defer();
+ toolbar.once(event, resolve);
+ return promise;
+};
+
+const show = ({id}) => send(output, object([id, {collapsed: false}]));
+const hide = ({id}) => send(output, object([id, {collapsed: true}]));
+const retitle = ({id}, title) => send(output, object([id, {title: title}]));
+
+const isAttached = ({id}, window=getMostRecentBrowserWindow()) =>
+ !!window.document.getElementById(id);
+
+const isCollapsed = ({id}, window=getMostRecentBrowserWindow()) =>
+ window.document.getElementById(id).getAttribute("collapsed") === "true";
+
+const closeViaButton = ({id}, window=getMostRecentBrowserWindow()) =>
+ window.document.getElementById("close-" + id).click();
+
+const readTitle = ({id}, window=getMostRecentBrowserWindow()) =>
+ window.document.getElementById(id).getAttribute("toolbarname");
+
+exports["test toolbar API"] = function*(assert) {
+ assert.throws(() => new Toolbar(),
+ /The `option.title`/,
+ "toolbar requires title");
+
+ assert.throws(() => new Toolbar({ hidden: false }),
+ /The `option.title`/,
+ "toolbar requires title");
+
+ const t1 = new Toolbar({ title: "foo" });
+
+ assert.throws(() => new Toolbar({ title: "foo" }),
+ /already exists/,
+ "can't create identical toolbars");
+
+ assert.ok(t1.id, "toolbar has an id");
+ assert.equal(t1.id, identify(t1), "identify returns toolbar id");
+ assert.deepEqual(t1.items, [], "toolbar items are empty");
+ assert.equal(t1.title, void(0), "title is void until attached");
+ assert.equal(t1.hidden, void(0), "hidden is void until attached");
+
+ yield wait(t1, "attach");
+
+ assert.equal(t1.title, "foo", "title is set after attach");
+ assert.equal(t1.hidden, false, "by default toolbar isn't hidden");
+
+ assert.throws(() => new Toolbar({ title: "foo" }),
+ /already exists/,
+ "still can't create identical toolbar");
+
+
+ const t2 = new Toolbar({ title: "bar", hidden: true });
+ assert.pass("can create different toolbar though");
+
+ assert.ok(t2.id, "toolbar has an id");
+ assert.equal(t2.id, identify(t2), "identify returns toolbar id");
+ assert.notEqual(t2.id, t1.id, "each toolbar has unique id");
+
+ yield wait(t2, "attach");
+
+ assert.equal(t2.title, "bar", "title is set after attach");
+ assert.equal(t2.hidden, true, "toolbar is hidden as specified");
+
+ t2.destroy();
+ t1.destroy();
+
+ yield wait(t1, "detach");
+
+ assert.equal(t1.title, void(0), "title is voided after detach");
+ assert.equal(t1.hidden, void(0), "hidden is void fater detach");
+
+
+ const t3 = new Toolbar({ title: "foo" });
+ assert.pass("Can create toolbar after identical was detached");
+
+ assert.equal(t3.id, t1.id, "toolbar has a same id");
+ assert.equal(t3.id, identify(t3), "identify returns toolbar.id");
+ assert.equal(t3.title, void(0), "title is void before attach");
+ assert.equal(t3.hidden, void(0), "hidden is void before attach");
+
+ yield wait(t3, "attach");
+
+ assert.equal(t3.title, "foo", "title is set");
+ assert.equal(t3.hidden, false, "toolbar is hidden");
+
+ t3.destroy();
+
+ yield wait(t3, "detach");
+};
+
+exports["test show / hide toolbar"] = function*(assert) {
+ const t1 = new Toolbar({ title: "foo" });
+
+ yield wait(t1, "attach");
+
+ assert.equal(t1.title, "foo", "title is set after attach");
+ assert.equal(t1.hidden, false, "by default toolbar isn't hidden");
+ assert.ok(isAttached(t1), "toolbar was actually attarched");
+ assert.ok(!isCollapsed(t1), "toolbar isn't collapsed");
+
+ hide(t1);
+ assert.equal(t1.hidden, false, "not hidden yet");
+
+ yield wait(t1, "hide");
+ assert.equal(t1.hidden, true, "toolbar got hidden");
+ assert.ok(isCollapsed(t1), "toolbar is collapsed");
+
+ show(t1);
+
+ yield wait(t1, "show");
+ assert.equal(t1.hidden, false, "toolbar got shown");
+ assert.ok(!isCollapsed(t1), "toolbar isn't collapsed");
+
+ t1.destroy();
+ yield wait(t1, "detach");
+ assert.ok(!isAttached(t1), "toolbar is no longer attached");
+};
+
+exports["test multiple windows & toolbars"] = function*(assert) {
+ const w1 = getMostRecentBrowserWindow();
+ const t1 = new Toolbar({ title: "multi window" });
+
+ yield wait(t1, "attach");
+
+ assert.equal(t1.title, "multi window", "title is set after attach");
+ assert.equal(t1.hidden, false, "by default toolbar isn't hidden");
+ assert.ok(isAttached(t1, w1), "toolbar was actually attarched");
+ assert.ok(!isCollapsed(t1, w1), "toolbar isn't collapsed");
+
+ const w2 = open();
+ yield ready(w2);
+
+ assert.ok(isAttached(t1, w2), "toolbar was attached to second window");
+ assert.ok(!isCollapsed(t1, w2), "toolbar isn't collabsed");
+
+ hide(t1);
+ yield wait(t1, "hide");
+
+ assert.ok(isCollapsed(t1, w1) && isCollapsed(t1, w2),
+ "toolbar is collabsed");
+
+
+ const w3 = open();
+ yield ready(w3);
+
+ assert.ok(isAttached(t1, w1) && isAttached(t1, w2) && isAttached(t1, w3),
+ "toolbar is attached to all windows");
+ assert.ok(isCollapsed(t1, w3) && isCollapsed(t1, w3) && isCollapsed(t1, w3),
+ "toolbar still collapsed in all windows");
+
+
+ const t2 = new Toolbar({ title: "multi hidden", hidden: true });
+
+ yield wait(t2, "attach");
+
+ assert.equal(t2.title, "multi hidden", "title is set after attach");
+ assert.equal(t2.hidden, true, "isn't hidden as specified");
+
+ assert.ok(isAttached(t1, w1) && isAttached(t1, w2) && isAttached(t1, w3),
+ "toolbar#1 is still attached");
+ assert.ok(isAttached(t2, w1) && isAttached(t2, w2) && isAttached(t2, w3),
+ "toolbar#2 was attached to all windows");
+
+ assert.ok(isCollapsed(t1, w1) && isCollapsed(t1, w2) && isCollapsed(t1, w3),
+ "toolbar#1 is still collapsed");
+
+ assert.ok(isCollapsed(t2, w1) && isCollapsed(t2, w2) && isCollapsed(t2, w3),
+ "toolbar#2 is collapsed");
+
+ t1.destroy();
+ yield wait(t1, "detach");
+
+ assert.ok(!isAttached(t1, w1) && !isAttached(t1, w2) && !isAttached(t1, w3),
+ "toolbar#1 was detached from all windows");
+ assert.ok(isAttached(t2, w1) && isAttached(t2, w2) && isAttached(t2, w3),
+ "toolbar#2 is still attached to all windows");
+
+ yield close(w2);
+
+ assert.ok(isAttached(t2, w1) && isAttached(t2, w3),
+ "toolbar#2 is still attached to remaining windows");
+ assert.ok(isCollapsed(t2, w1) && isCollapsed(t2, w3),
+ "toolbar#2 is still collapsed");
+
+ show(t2);
+ yield wait(t2, "show");
+
+ assert.ok(!isCollapsed(t2, w1) && !isCollapsed(t2, w3),
+ "toolbar#2 is not collapsed");
+
+ yield close(w3);
+
+ assert.ok(isAttached(t2, w1), "still attached to last window");
+ assert.ok(!isCollapsed(t2, w1), "still isn't collapsed");
+
+ t2.destroy();
+ yield wait(t2, "detach");
+
+ assert.ok(!isAttached(t2, w1), "toolbar was removed");
+
+ yield cleanUI();
+};
+
+exports["test toolbar persistence"] = function*(assert) {
+ const t1 = new Toolbar({ title: "per sist ence" });
+
+ yield wait(t1, "attach");
+
+ assert.equal(t1.hidden, false, "toolbar is visible");
+
+ hide(t1);
+ yield wait(t1, "hide");
+
+ assert.equal(t1.hidden, true, "toolbar is hidden");
+ assert.ok(isCollapsed(t1), "toolbar is collapsed");
+
+ t1.destroy();
+
+ yield wait(t1, "detach");
+
+ const t2 = new Toolbar({ title: "per sist ence" });
+
+ yield wait(t2, "attach");
+
+ assert.equal(t2.hidden, true, "toolbar persisted state");
+ assert.ok(isCollapsed(t2), "toolbar is collapsed");
+
+ show(t2);
+ t2.destroy();
+
+ yield wait(t2, "detach");
+
+ const t3 = new Toolbar({ title: "per sist ence", hidden: true });
+
+ yield wait(t3, "attach");
+
+ assert.equal(t3.hidden, false, "toolbar persisted state & ignored option");
+ assert.ok(!isCollapsed(t3), "toolbar isn1t collapsed");
+
+ t3.destroy();
+
+ yield wait(t3, "detach");
+
+ yield cleanUI();
+};
+
+
+exports["test toolbar unload"] = function*(assert) {
+ // We override add-on id, otherwise two instances of Toolbar host (view.js)
+ // handling same updates, cause message port is bound to add-on id.
+ const loader = Loader(module, null, null, {id: "toolbar-unload-addon"});
+ const { Toolbar } = loader.require("sdk/ui/toolbar");
+
+ const w1 = getMostRecentBrowserWindow();
+ const w2 = open();
+
+ yield ready(w2);
+
+ const t1 = new Toolbar({ title: "unload" });
+
+ yield wait(t1, "attach");
+
+ assert.ok(isAttached(t1, w1) && isAttached(t1, w2),
+ "attached to both windows");
+
+
+ loader.unload();
+
+
+ assert.ok(!isAttached(t1, w1) && !isAttached(t1, w2),
+ "detached from both windows on unload");
+
+ yield cleanUI();
+};
+
+exports["test toolbar close button"] = function*(assert) {
+ const t1 = new Toolbar({ title: "close with button" });
+
+ yield wait(t1, "attach");
+ const w1 = getMostRecentBrowserWindow();
+ const w2 = open();
+
+ yield ready(w2);
+
+ assert.ok(!isCollapsed(t1, w1) && !isCollapsed(t1, w2),
+ "toolbar isn't collapsed");
+
+ closeViaButton(t1);
+
+ yield wait(t1, "hide");
+
+ assert.ok(isCollapsed(t1, w1) && isCollapsed(t1, w2),
+ "toolbar was collapsed");
+
+ t1.destroy();
+ yield wait(t1, "detach");
+ yield cleanUI();
+};
+
+exports["test title change"] = function*(assert) {
+ const w1 = getMostRecentBrowserWindow();
+ const w2 = open();
+
+ yield ready(w2);
+
+ const t1 = new Toolbar({ title: "first title" });
+ const id = t1.id;
+
+ yield wait(t1, "attach");
+
+
+ assert.equal(t1.title, "first title",
+ "correct title is set");
+ assert.equal(readTitle(t1, w1), "first title",
+ "title set in the view of first window");
+ assert.equal(readTitle(t1, w2), "first title",
+ "title set in the view of second window");
+
+ retitle(t1, "second title");
+
+ // Hide & show so to make sure changes go through a round
+ // loop.
+ hide(t1);
+ yield wait(t1, "hide");
+ show(t1);
+ yield wait(t1, "show");
+
+ assert.equal(t1.id, id, "id remains same");
+ assert.equal(t1.title, "second title", "instance title was updated");
+ assert.equal(readTitle(t1, w1), "second title",
+ "title updated in first window");
+ assert.equal(readTitle(t1, w2), "second title",
+ "title updated in second window");
+
+ t1.destroy();
+ yield wait(t1, "detach");
+ yield cleanUI();
+};
+
+exports["test toolbar is not customizable"] = function*(assert, done) {
+ const { window, document, gCustomizeMode } = getMostRecentBrowserWindow();
+ const outerId = getOuterId(window);
+ const input = new CustomizationInput();
+ const customized = defer();
+ const customizedEnd = defer();
+
+ // open a new tab so that the customize tab replaces it
+ // and does not replace the start tab.
+ yield new Promise(resolve => {
+ tabs.open({
+ url: "about:blank",
+ onReady: resolve
+ });
+ });
+
+ new Reactor({ onStep: value => {
+ if (value[outerId] === true)
+ customized.resolve();
+ if (value[outerId] === null)
+ customizedEnd.resolve();
+ }}).run(input);
+
+ const toolbar = new Toolbar({ title: "foo" });
+ yield wait(toolbar, "attach");
+
+ let view = document.getElementById(toolbar.id);
+ let label = view.querySelector("label");
+ let inner = view.querySelector("toolbar");
+
+ assert.equal(view.getAttribute("customizable"), "false",
+ "The outer toolbar is not customizable.");
+
+ assert.ok(label.collapsed,
+ "The label is not displayed.")
+
+ assert.equal(inner.getAttribute("customizable"), "true",
+ "The inner toolbar is customizable.");
+
+ assert.equal(window.getComputedStyle(inner).visibility, "visible",
+ "The inner toolbar is visible.");
+
+ // Enter in customization mode
+ gCustomizeMode.toggle();
+
+ yield customized.promise;
+
+ assert.equal(view.getAttribute("customizable"), "false",
+ "The outer toolbar is not customizable.");
+
+ assert.equal(label.collapsed, false,
+ "The label is displayed.")
+
+ assert.equal(inner.getAttribute("customizable"), "true",
+ "The inner toolbar is customizable.");
+
+ assert.equal(window.getComputedStyle(inner).visibility, "hidden",
+ "The inner toolbar is hidden.");
+
+ // Exit from customization mode
+ gCustomizeMode.toggle();
+
+ yield customizedEnd.promise;
+
+ assert.equal(view.getAttribute("customizable"), "false",
+ "The outer toolbar is not customizable.");
+
+ assert.ok(label.collapsed,
+ "The label is not displayed.")
+
+ assert.equal(inner.getAttribute("customizable"), "true",
+ "The inner toolbar is customizable.");
+
+ assert.equal(window.getComputedStyle(inner).visibility, "visible",
+ "The inner toolbar is visible.");
+
+ toolbar.destroy();
+
+ yield cleanUI();
+};
+
+exports["test button are attached to toolbar"] = function*(assert) {
+ const { document } = getMostRecentBrowserWindow();
+ const { ActionButton, ToggleButton } = require("sdk/ui");
+ const { identify } = require("sdk/ui/id");
+
+ let action = ActionButton({
+ id: "btn-1",
+ label: "action",
+ icon: "./placeholder.png"
+ });
+
+ let toggle = ToggleButton({
+ id: "btn-2",
+ label: "toggle",
+ icon: "./placeholder.png"
+ });
+
+ const toolbar = new Toolbar({
+ title: "foo",
+ items: [action, toggle]
+ });
+
+ yield wait(toolbar, "attach");
+
+ let actionNode = document.getElementById(identify(action));
+ let toggleNode = document.getElementById(identify(toggle));
+
+ assert.notEqual(actionNode, null,
+ "action button exists in the document");
+
+ assert.notEqual(actionNode, null,
+ "action button exists in the document");
+
+ assert.notEqual(toggleNode, null,
+ "toggle button exists in the document");
+
+ assert.equal(actionNode.nextElementSibling, toggleNode,
+ "action button is placed before toggle button");
+
+ assert.equal(actionNode.parentNode.parentNode.id, toolbar.id,
+ "buttons are placed in the correct toolbar");
+
+ toolbar.destroy();
+
+ yield cleanUI();
+};
+
+exports["test toolbar are not in private windows"] = function*(assert) {
+ const w = open(null, {features: {toolbar: true, private: true}});
+
+ yield ready(w);
+
+ const t = new Toolbar({title: "foo"});
+
+ yield wait(t, "attach");
+
+ assert.ok(!isAttached(t), "toolbar wasn't actually attached");
+
+ t.destroy();
+
+ yield cleanUI();
+}
+
+require("sdk/test").run(module.exports);
diff --git a/addon-sdk/source/test/test-unit-test-finder.js b/addon-sdk/source/test/test-unit-test-finder.js
new file mode 100644
index 000000000..83db5c722
--- /dev/null
+++ b/addon-sdk/source/test/test-unit-test-finder.js
@@ -0,0 +1,57 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+ "use strict";
+
+const { makeFilters } = require("sdk/deprecated/unit-test-finder");
+
+const testFiles = [ "test-clipboard", "test-timers" ]
+const testMethods = [ "test set clipboard", "test setTimeout" ]
+
+exports["test makeFilters no options"] = (assert) => {
+ let { fileFilter, testFilter } = makeFilters();
+ testFiles.forEach(f => assert.ok(fileFilter(f), "using no options on filename " + f + " works"));
+ testMethods.forEach(m => assert.ok(testFilter(m), "using no options on method name " + m + " works"));
+}
+
+exports["test makeFilters no filter"] = (assert) => {
+ let { fileFilter, testFilter } = makeFilters({});
+ testFiles.forEach(f => assert.ok(fileFilter(f), "using no options on filename " + f + " works"));
+ testMethods.forEach(m => assert.ok(testFilter(m), "using no options on method name " + m + " works"));
+}
+
+exports["test makeFilters no method filter"] = (assert) => {
+ let { fileFilter, testFilter } = makeFilters({ filter: "i" });
+ testFiles.forEach(f => assert.ok(fileFilter(f), "using filter 'i' on filename " + f + " works"));
+ testMethods.forEach(m => assert.ok(testFilter(m), "using filter 'i' on method name " + m + " works"));
+
+ ({ fileFilter, testFilter } = makeFilters({ filter: "i:" }));
+ testFiles.forEach(f => assert.ok(fileFilter(f), "using filter 'i:' on filename " + f + " works"));
+ testMethods.forEach(m => assert.ok(testFilter(m), "using filter 'i:' on method name " + m + " works"));
+
+ ({ fileFilter, testFilter } = makeFilters({ filter: "z:" }));
+ testFiles.forEach(f => assert.ok(!fileFilter(f), "using filter 'z:' on filename " + f + " dnw"));
+ testMethods.forEach(m => assert.ok(testFilter(m), "using filter 'z:' on method name " + m + " works"));
+}
+
+exports["test makeFilters no file filter"] = (assert) => {
+ let { fileFilter, testFilter } = makeFilters({ filter: ":i" });
+ testFiles.forEach(f => assert.ok(fileFilter(f), "using filter ':i' on filename " + f + " works"));
+ testMethods.forEach(m => assert.ok(testFilter(m), "using filter ':i' on method name " + m + " works"));
+
+ ({ fileFilter, testFilter } = makeFilters({ filter: ":z" }));
+ testFiles.forEach(f => assert.ok(fileFilter(f), "using filter ':z' on filename " + f + " works"));
+ testMethods.forEach(m => assert.ok(!testFilter(m), "using filter ':z' on method name " + m + " dnw"));
+}
+
+exports["test makeFilters both filters"] = (assert) => {
+ let { fileFilter, testFilter } = makeFilters({ filter: "i:i" });
+ testFiles.forEach(f => assert.ok(fileFilter(f), "using filter 'i:i' on filename " + f + " works"));
+ testMethods.forEach(m => assert.ok(testFilter(m), "using filter 'i:i' on method name " + m + " works"));
+
+ ({ fileFilter, testFilter } = makeFilters({ filter: "z:z" }));
+ testFiles.forEach(f => assert.ok(!fileFilter(f), "using filter 'z:z' on filename " + f + " dnw"));
+ testMethods.forEach(m => assert.ok(!testFilter(m), "using filter 'z:z' on method name " + m + " dnw"));
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-unit-test.js b/addon-sdk/source/test/test-unit-test.js
new file mode 100644
index 000000000..929418447
--- /dev/null
+++ b/addon-sdk/source/test/test-unit-test.js
@@ -0,0 +1,270 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const timer = require("sdk/timers");
+
+var setupCalled = false, teardownCalled = false;
+
+exports.setup = function() {
+ setupCalled = true;
+};
+
+exports.teardown = function() {
+ teardownCalled = true;
+ setupCalled = false;
+};
+
+// Important note - unit tests are run in alphabetical order. The following
+// unit tests for setup/teardown are order dependent, sometimes the result of
+// one test is checked in the next test (testing for teardown does this). When
+// tests are cohesively a single unit, they are named <test_name> - partN where
+// N is their order in the sequence. Secondly, because these tests should be
+// run before all others, they start with an A.
+exports.testASetupTeardownSyncTestPart1 = function(test) {
+ test.assertEqual(true, setupCalled, 'setup function was called before this');
+ test.assertEqual(false, teardownCalled, 'teardown function was not called before this');
+};
+
+exports.testASetupTeardownSyncTestPart2 = function(test) {
+ test.assertEqual(true, setupCalled, 'setup was re-called before this');
+ test.assertEqual(true, teardownCalled, 'teardown was called after first function');
+};
+
+exports.testATeardownAsyncTestPart1 = function(test) {
+ teardownCalled = false;
+ timer.setTimeout(_ => {
+ test.assertEqual(false, teardownCalled, "teardown not called until done");
+ test.done();
+ }, 20);
+ test.waitUntilDone();
+};
+
+exports.testATeardownAsyncTestPart2 = function(test) {
+ test.assertEqual(true, teardownCalled, "teardown called after done");
+};
+
+exports.testWaitUntilInstant = function(test) {
+ test.waitUntilDone();
+
+ test.waitUntil(() => true, "waitUntil with instant true pass")
+ .then(() => test.done());
+}
+
+exports.testWaitUntil = function(test) {
+ test.waitUntilDone();
+ let succeed = false;
+
+ test.waitUntil(_ => succeed, "waitUntil pass")
+ .then(test.done);
+
+ timer.setTimeout(_ => {
+ test.pass("succeed");
+ succeed = true;
+ }, 20);
+}
+
+exports.testWaitUntilEqual = function(test) {
+ test.waitUntilDone();
+ let succeed = false;
+
+ test.waitUntilEqual("foo", _ => succeed ? "foo" : "bar",
+ "waitUntilEqual pass")
+ .then(test.done);
+
+ timer.setTimeout(_ => {
+ test.pass("succeed");
+ succeed = true;
+ }, 20);
+}
+
+exports.testWaitUntilNotEqual = function(test) {
+ test.waitUntilDone();
+ let succeed = false;
+
+ test.waitUntilNotEqual("foo", _ => succeed ? "bar" : "foo",
+ "waitUntilNotEqual pass")
+ .then(test.done);
+
+ timer.setTimeout(_ => {
+ test.pass("succeed");
+ succeed = true;
+ }, 20);
+}
+
+exports.testWaitUntilMatches = function(test) {
+ test.waitUntilDone();
+ let succeed = false;
+
+ test.waitUntilMatches(_ => succeed ? "foo" : "bar",
+ /foo/, "waitUntilEqual pass")
+ .then(test.done);
+
+ timer.setTimeout(_ => {
+ test.pass("succeed");
+ succeed = true;
+ }, 20);
+}
+
+exports.testWaitUntilErrorInCallback = function(test) {
+ test.waitUntilDone();
+ test.expectFail(_ => {
+ test.waitUntil(_ => { throw "oops"; }, "waitUntil pass")
+ .then(test.done);
+ });
+}
+
+exports.testWaitUntilTimeoutInCallback = function(test) {
+ test.waitUntilDone();
+
+ let expected = [];
+ let message = 0;
+ if (require("sdk/test/options").parseable) {
+ expected.push(["print", "TEST-START | wait4ever\n"]);
+ expected.push(["error", "fail:", "Timed out (after: START)"]);
+ expected.push(["error", "test assertion never became true:\n", "assertion failed, value is false\n"]);
+ }
+ else {
+ expected.push(["info", "executing 'wait4ever'"]);
+ expected.push(["error", "fail:", "Timed out (after: START)"]);
+ expected.push(["error", "test assertion never became true:\n", "assertion failed, value is false\n"]);
+ }
+
+ function checkExpected(name, args) {
+ var index = message;
+ if (message++ >= expected.length) {
+ return;
+ }
+
+ let expectedArgs = expected[index].slice(1);
+ for (let i = 0; i < expectedArgs.length; i++) {
+ test.assertEqual(args[i], expectedArgs[i], "Should have seen the right message in argument " + i + " of message " + message);
+ }
+
+ if (message >= expected.length) {
+ test.done();
+ }
+ }
+
+ let runner = new (require("sdk/deprecated/unit-test").TestRunner)({
+ console: {
+ error: function() {
+ checkExpected("error", Array.slice(arguments));
+ },
+ info: function () {
+ checkExpected("info", Array.slice(arguments));
+ },
+ trace: function () {},
+ exception: function () {},
+ print: function () {
+ checkExpected("print", Array.slice(arguments));
+ }
+ }
+ });
+
+ runner.start({
+ test: {
+ name: "wait4ever",
+ testFunction: function(test) {
+ test.waitUntilDone(100);
+ test.waitUntil(() => false);
+ }
+ },
+ onDone: function() {}
+ });
+};
+
+exports.testExpectFail = function(test) {
+ test.expectFail(function() {
+ test.fail('expectFail masking .fail');
+ });
+
+ test.expectFail(function() {
+ test.assert(false, 'expectFail masking .assert');
+ });
+
+ test.assert(true, 'assert should pass with no expectFail');
+/*
+ test.expectFail(function() {
+ test.expectFail(function() {
+ test.fail('this should blow up');
+ });
+ });
+*/
+};
+
+exports.testAssertFunction = function(test) {
+ test.assertFunction(function() {}, 'assertFunction with function');
+ test.expectFail(function() {
+ test.assertFunction(null, 'assertFunction with non-function');
+ });
+};
+
+exports.testAssertUndefined = function(test) {
+ test.assertUndefined(undefined, 'assertUndefined with undefined');
+ test.expectFail(function() {
+ test.assertUndefined(null, 'assertUndefined with null');
+ });
+ test.expectFail(function() {
+ test.assertUndefined(false, 'assertUndefined with false');
+ });
+ test.expectFail(function() {
+ test.assertUndefined(0, 'assertUndefined with 0');
+ });
+};
+
+exports.testAssertNotUndefined = function(test) {
+ test.expectFail(function() {
+ test.assertNotUndefined(undefined, 'assertNotUndefined with undefined');
+ });
+ test.assertNotUndefined(null, 'assertNotUndefined with null');
+ test.assertNotUndefined(false, 'assertNotUndefined with false');
+ test.assertNotUndefined(0, 'assertNotUndefined with 0');
+};
+
+exports.testAssertNull = function(test) {
+ test.assertNull(null, 'assertNull with null');
+ test.expectFail(function() {
+ test.assertNull(undefined, 'assertNull with undefined');
+ });
+ test.expectFail(function() {
+ test.assertNull(false, 'assertNull with false');
+ });
+ test.expectFail(function() {
+ test.assertNull(0, 'assertNull with 0');
+ });
+};
+
+exports.testAssertNotNull = function(test) {
+ test.assertNotNull(undefined, 'assertNotNull with undefined');
+ test.assertNotNull(false, 'assertNotNull with false');
+ test.assertNotNull(0, 'assertNotNull with 0');
+
+ test.expectFail(function() {
+ test.assertNotNull(null, 'testAssertNotNull with null');
+ });
+};
+
+exports.testAssertObject = function(test) {
+ test.assertObject({}, 'assertObject with {}' );
+ test.assertObject(new Object(), 'assertObject with new Object');
+ test.expectFail(function() {
+ test.assertObject('fail', 'assertObject with string');
+ });
+};
+
+exports.testAssertString = function(test) {
+ test.assertString('', 'assertString with ""');
+ test.assertString(new String(), 'assertString with new String');
+};
+
+exports.testAssertArray = function(test) {
+ test.assertArray([], 'assertArray with []');
+ test.assertArray(new Array(), 'assertArray with new Array');
+};
+
+exports.testNumber = function(test) {
+ test.assertNumber(1, 'assertNumber with 1');
+ test.assertNumber(new Number('2'), 'assertNumber with new Number("2")' );
+};
+
diff --git a/addon-sdk/source/test/test-unload.js b/addon-sdk/source/test/test-unload.js
new file mode 100644
index 000000000..9bf8b75ac
--- /dev/null
+++ b/addon-sdk/source/test/test-unload.js
@@ -0,0 +1,71 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+ "use strict";
+
+var unload = require("sdk/system/unload");
+var { Loader, LoaderWithHookedConsole } = require("sdk/test/loader");
+
+exports.testUnloading = function(assert) {
+ let { loader, messages } = LoaderWithHookedConsole(module);
+ var ul = loader.require("sdk/system/unload");
+ var unloadCalled = 0;
+ function unload() {
+ unloadCalled++;
+ throw new Error("error");
+ }
+ ul.when(unload);
+
+ // This should be ignored, as we already registered it
+ ul.when(unload);
+
+ function unload2() { unloadCalled++; }
+ ul.when(unload2);
+ loader.unload();
+ assert.equal(unloadCalled, 2,
+ "Unloader functions are called on unload.");
+ assert.equal(messages.length, 1,
+ "One unload handler threw exception 1/2");
+ assert.equal(messages[0].type, "exception",
+ "One unload handler threw exception 2/2");
+};
+
+exports.testEnsure = function(assert) {
+ assert.throws(function() { unload.ensure({}); },
+ /object has no 'unload' property/,
+ "passing obj with no unload prop should fail");
+ assert.throws(function() { unload.ensure({}, "destroy"); },
+ /object has no 'destroy' property/,
+ "passing obj with no custom unload prop should fail");
+
+ var called = 0;
+ var obj = {unload: function() { called++; }};
+
+ unload.ensure(obj);
+ obj.unload();
+ assert.equal(called, 1,
+ "unload() should be called");
+ obj.unload();
+ assert.equal(called, 1,
+ "unload() should be called only once");
+};
+
+exports.testReason = function (assert) {
+ var reason = "Reason doesn't actually have to be anything in particular.";
+ var loader = Loader(module);
+ var ul = loader.require("sdk/system/unload");
+ ul.when(function (rsn) {
+ assert.equal(rsn, reason,
+ "when() reason should be reason given to loader");
+ });
+ var obj = {
+ unload: function (rsn) {
+ assert.equal(rsn, reason,
+ "ensure() reason should be reason given to loader");
+ }
+ };
+ ul.ensure(obj);
+ loader.unload(reason);
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-unsupported-skip.js b/addon-sdk/source/test/test-unsupported-skip.js
new file mode 100644
index 000000000..b19e26cfb
--- /dev/null
+++ b/addon-sdk/source/test/test-unsupported-skip.js
@@ -0,0 +1,24 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const app = require('sdk/system/xul-app');
+
+/*
+ * Include a module that is unsupported for the current system.
+ * The `Unsupported Application` error should be caught by the test loader
+ * and no errors should occur
+ */
+if (!app.is('Firefox')) {
+ require('./fixtures/loader/unsupported/firefox');
+}
+else {
+ require('./fixtures/loader/unsupported/fennec');
+}
+
+exports.testRunning = function (assert) {
+ assert.fail('Tests should not run in unsupported applications');
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-uri-resource.js b/addon-sdk/source/test/test-uri-resource.js
new file mode 100644
index 000000000..547090561
--- /dev/null
+++ b/addon-sdk/source/test/test-uri-resource.js
@@ -0,0 +1,43 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const {mount, unmount, resolve} = require("sdk/uri/resource");
+const {tmpdir} = require("node/os");
+const {fromFilename} = require("sdk/url");
+
+const mountURI = fromFilename(tmpdir());
+
+exports.testAPI = assert => {
+ const domain = Math.random().toString(16).substr(2)
+
+ assert.equal(resolve(`resource://${domain}`),
+ null,
+ "domain isn't mounted");
+
+ mount(domain, mountURI);
+
+ assert.equal(resolve(`resource://${domain}`),
+ mountURI,
+ "domain was mounted");
+
+
+ assert.equal(resolve(`resource://${domain}/foo.js`),
+ `${mountURI}foo.js`,
+ "uri resolves to a file in mounted location");
+
+ unmount(domain);
+
+ assert.equal(resolve(`resource://${domain}`),
+ null,
+ "domain is no longer mounted");
+
+
+ assert.equal(resolve(`resource://${domain}/foo.js`),
+ null,
+ "uris under unmounted domain resolve to null");
+
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-url.js b/addon-sdk/source/test/test-url.js
new file mode 100644
index 000000000..9f2df0917
--- /dev/null
+++ b/addon-sdk/source/test/test-url.js
@@ -0,0 +1,502 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const {
+ URL,
+ toFilename,
+ fromFilename,
+ isValidURI,
+ getTLD,
+ DataURL,
+ isLocalURL } = require('sdk/url');
+
+const { pathFor } = require('sdk/system');
+const file = require('sdk/io/file');
+const tabs = require('sdk/tabs');
+const { decode } = require('sdk/base64');
+
+const httpd = require('./lib/httpd');
+const port = 8099;
+
+exports.testResolve = function(assert) {
+ assert.equal(URL('bar', 'http://www.foo.com/').toString(),
+ 'http://www.foo.com/bar');
+
+ assert.equal(URL('bar', 'http://www.foo.com'),
+ 'http://www.foo.com/bar');
+
+ assert.equal(URL('http://bar.com/', 'http://foo.com/'),
+ 'http://bar.com/',
+ 'relative should override base');
+
+ assert.throws(function() { URL('blah'); },
+ /malformed URI: blah/i,
+ 'url.resolve() should throw malformed URI on base');
+
+ assert.throws(function() { URL('chrome://global'); },
+ /invalid URI: chrome:\/\/global/i,
+ 'url.resolve() should throw invalid URI on base');
+
+ assert.throws(function() { URL('chrome://foo/bar'); },
+ /invalid URI: chrome:\/\/foo\/bar/i,
+ 'url.resolve() should throw on bad chrome URI');
+
+ assert.equal(URL('', 'http://www.foo.com'),
+ 'http://www.foo.com/',
+ 'url.resolve() should add slash to end of domain');
+};
+
+exports.testParseHttp = function(assert) {
+ var aUrl = 'http://sub.foo.com/bar?locale=en-US&otherArg=%20x%20#myhash';
+ var info = URL(aUrl);
+
+ assert.equal(info.scheme, 'http');
+ assert.equal(info.protocol, 'http:');
+ assert.equal(info.host, 'sub.foo.com');
+ assert.equal(info.hostname, 'sub.foo.com');
+ assert.equal(info.port, null);
+ assert.equal(info.userPass, null);
+ assert.equal(info.path, '/bar?locale=en-US&otherArg=%20x%20#myhash');
+ assert.equal(info.pathname, '/bar');
+ assert.equal(info.href, aUrl);
+ assert.equal(info.hash, '#myhash');
+ assert.equal(info.search, '?locale=en-US&otherArg=%20x%20');
+ assert.equal(info.fileName, 'bar');
+};
+
+exports.testParseHttpSearchAndHash = function (assert) {
+ var info = URL('https://www.moz.com/some/page.html');
+ assert.equal(info.hash, '');
+ assert.equal(info.search, '');
+
+ var hashOnly = URL('https://www.sub.moz.com/page.html#justhash');
+ assert.equal(hashOnly.search, '');
+ assert.equal(hashOnly.hash, '#justhash');
+
+ var queryOnly = URL('https://www.sub.moz.com/page.html?my=query');
+ assert.equal(queryOnly.search, '?my=query');
+ assert.equal(queryOnly.hash, '');
+
+ var qMark = URL('http://www.moz.org?');
+ assert.equal(qMark.search, '');
+ assert.equal(qMark.hash, '');
+
+ var hash = URL('http://www.moz.org#');
+ assert.equal(hash.search, '');
+ assert.equal(hash.hash, '');
+
+ var empty = URL('http://www.moz.org?#');
+ assert.equal(hash.search, '');
+ assert.equal(hash.hash, '');
+
+ var strange = URL('http://moz.org?test1#test2?test3');
+ assert.equal(strange.search, '?test1');
+ assert.equal(strange.hash, '#test2?test3');
+};
+
+exports.testParseHttpWithPort = function(assert) {
+ var info = URL('http://foo.com:5/bar');
+ assert.equal(info.port, 5);
+};
+
+exports.testParseChrome = function(assert) {
+ var info = URL('chrome://global/content/blah');
+ assert.equal(info.scheme, 'chrome');
+ assert.equal(info.host, 'global');
+ assert.equal(info.port, null);
+ assert.equal(info.userPass, null);
+ assert.equal(info.path, '/content/blah');
+ assert.equal(info.fileName, 'blah');
+};
+
+exports.testParseAbout = function(assert) {
+ var info = URL('about:boop');
+ assert.equal(info.scheme, 'about');
+ assert.equal(info.host, null);
+ assert.equal(info.port, null);
+ assert.equal(info.userPass, null);
+ assert.equal(info.path, 'boop');
+};
+
+exports.testParseFTP = function(assert) {
+ var info = URL('ftp://1.2.3.4/foo');
+ assert.equal(info.scheme, 'ftp');
+ assert.equal(info.host, '1.2.3.4');
+ assert.equal(info.port, null);
+ assert.equal(info.userPass, null);
+ assert.equal(info.path, '/foo');
+ assert.equal(info.fileName, 'foo');
+};
+
+exports.testParseFTPWithUserPass = function(assert) {
+ var info = URL('ftp://user:pass@1.2.3.4/foo');
+ assert.equal(info.userPass, 'user:pass');
+};
+
+exports.testToFilename = function(assert) {
+ assert.throws(
+ function() { toFilename('resource://nonexistent'); },
+ /resource does not exist: resource:\/\/nonexistent\//i,
+ 'toFilename() on nonexistent resources should throw'
+ );
+
+ assert.throws(
+ function() { toFilename('http://foo.com/'); },
+ /cannot map to filename: http:\/\/foo.com\//i,
+ 'toFilename() on http: URIs should raise error'
+ );
+
+ try {
+ assert.ok(
+ /.*console\.xul$/.test(toFilename('chrome://global/content/console.xul')),
+ 'toFilename() w/ console.xul works when it maps to filesystem'
+ );
+ }
+ catch (e) {
+ if (/chrome url isn\'t on filesystem/.test(e.message))
+ assert.pass('accessing console.xul in jar raises exception');
+ else
+ assert.fail('accessing console.xul raises ' + e);
+ }
+
+ // TODO: Are there any chrome URLs that we're certain exist on the
+ // filesystem?
+ // assert.ok(/.*main\.js$/.test(toFilename('chrome://myapp/content/main.js')));
+};
+
+exports.testFromFilename = function(assert) {
+ var profileDirName = require('sdk/system').pathFor('ProfD');
+ var fileUrl = fromFilename(profileDirName);
+ assert.equal(URL(fileUrl).scheme, 'file',
+ 'toFilename() should return a file: url');
+ assert.equal(fromFilename(toFilename(fileUrl)), fileUrl);
+};
+
+exports.testURL = function(assert) {
+ assert.ok(URL('h:foo') instanceof URL, 'instance is of correct type');
+ assert.throws(() => URL(),
+ /malformed URI: undefined/i,
+ 'url.URL should throw on undefined');
+ assert.throws(() => URL(''),
+ /malformed URI: /i,
+ 'url.URL should throw on empty string');
+ assert.throws(() => URL('foo'),
+ /malformed URI: foo/i,
+ 'url.URL should throw on invalid URI');
+ assert.ok(URL('h:foo').scheme, 'has scheme');
+ assert.equal(URL('h:foo').toString(),
+ 'h:foo',
+ 'toString should roundtrip');
+ // test relative + base
+ assert.equal(URL('mypath', 'http://foo').toString(),
+ 'http://foo/mypath',
+ 'relative URL resolved to base');
+ // test relative + no base
+ assert.throws(() => URL('path').toString(),
+ /malformed URI: path/i,
+ 'no base for relative URI should throw');
+
+ let a = URL('h:foo');
+ let b = URL(a);
+ assert.equal(b.toString(),
+ 'h:foo',
+ 'a URL can be initialized from another URL');
+ assert.notStrictEqual(a, b,
+ 'a URL initialized from another URL is not the same object');
+ assert.ok(a == 'h:foo',
+ 'toString is implicit when a URL is compared to a string via ==');
+ assert.strictEqual(a + '', 'h:foo',
+ 'toString is implicit when a URL is concatenated to a string');
+};
+
+exports.testStringInterface = function(assert) {
+ var EM = 'about:addons';
+ var a = URL(EM);
+
+ // make sure the standard URL properties are enumerable and not the String interface bits
+ assert.equal(Object.keys(a),
+ 'fileName,scheme,userPass,host,hostname,port,path,pathname,hash,href,origin,protocol,search',
+ 'enumerable key list check for URL.');
+ assert.equal(
+ JSON.stringify(a),
+ JSON.stringify(EM),
+ 'JSON.stringify on url should return the url as a flat string');
+ // JSON.parse(JSON.stringify(url)) wont work like an url object
+ // (missing methods). this makes it easier to re-create an url
+ // instance from the whole string, and every place that
+ // accepts an url also works with a flat string.
+
+ // make sure that the String interface exists and works as expected
+ assert.equal(a.indexOf(':'), EM.indexOf(':'), 'indexOf on URL works');
+ assert.equal(a.valueOf(), EM.valueOf(), 'valueOf on URL works.');
+ assert.equal(a.toSource(), EM.toSource(), 'toSource on URL works.');
+ assert.equal(a.lastIndexOf('a'), EM.lastIndexOf('a'), 'lastIndexOf on URL works.');
+ assert.equal(a.match('t:').toString(), EM.match('t:').toString(), 'match on URL works.');
+ assert.equal(a.toUpperCase(), EM.toUpperCase(), 'toUpperCase on URL works.');
+ assert.equal(a.toLowerCase(), EM.toLowerCase(), 'toLowerCase on URL works.');
+ assert.equal(a.split(':').toString(), EM.split(':').toString(), 'split on URL works.');
+ assert.equal(a.charAt(2), EM.charAt(2), 'charAt on URL works.');
+ assert.equal(a.charCodeAt(2), EM.charCodeAt(2), 'charCodeAt on URL works.');
+ assert.equal(a.concat(EM), EM.concat(a), 'concat on URL works.');
+ assert.equal(a.substr(2,3), EM.substr(2,3), 'substr on URL works.');
+ assert.equal(a.substring(2,3), EM.substring(2,3), 'substring on URL works.');
+ assert.equal(a.trim(), EM.trim(), 'trim on URL works.');
+ assert.equal(a.trimRight(), EM.trimRight(), 'trimRight on URL works.');
+ assert.equal(a.trimLeft(), EM.trimLeft(), 'trimLeft on URL works.');
+}
+
+exports.testDataURLwithouthURI = function (assert) {
+ let dataURL = new DataURL();
+
+ assert.equal(dataURL.base64, false, 'base64 is false for empty uri')
+ assert.equal(dataURL.data, '', 'data is an empty string for empty uri')
+ assert.equal(dataURL.mimeType, '', 'mimeType is an empty string for empty uri')
+ assert.equal(Object.keys(dataURL.parameters).length, 0, 'parameters is an empty object for empty uri');
+
+ assert.equal(dataURL.toString(), 'data:,');
+}
+
+exports.testDataURLwithMalformedURI = function (assert) {
+ assert.throws(function() {
+ let dataURL = new DataURL('http://www.mozilla.com/');
+ },
+ /Malformed Data URL: http:\/\/www.mozilla.com\//i,
+ 'DataURL raises an exception for malformed data uri'
+ );
+}
+
+exports.testDataURLparse = function (assert) {
+ let dataURL = new DataURL('data:text/html;charset=US-ASCII,%3Ch1%3EHello!%3C%2Fh1%3E');
+
+ assert.equal(dataURL.base64, false, 'base64 is false for non base64 data uri')
+ assert.equal(dataURL.data, '<h1>Hello!</h1>', 'data is properly decoded')
+ assert.equal(dataURL.mimeType, 'text/html', 'mimeType is set properly')
+ assert.equal(Object.keys(dataURL.parameters).length, 1, 'one parameters specified');
+ assert.equal(dataURL.parameters['charset'], 'US-ASCII', 'charset parsed');
+
+ assert.equal(dataURL.toString(), 'data:text/html;charset=US-ASCII,%3Ch1%3EHello!%3C%2Fh1%3E');
+}
+
+exports.testDataURLparseBase64 = function (assert) {
+ let text = 'Awesome!';
+ let b64text = 'QXdlc29tZSE=';
+ let dataURL = new DataURL('data:text/plain;base64,' + b64text);
+
+ assert.equal(dataURL.base64, true, 'base64 is true for base64 encoded data uri')
+ assert.equal(dataURL.data, text, 'data is properly decoded')
+ assert.equal(dataURL.mimeType, 'text/plain', 'mimeType is set properly')
+ assert.equal(Object.keys(dataURL.parameters).length, 1, 'one parameters specified');
+ assert.equal(dataURL.parameters['base64'], '', 'parameter set without value');
+ assert.equal(dataURL.toString(), 'data:text/plain;base64,' + encodeURIComponent(b64text));
+}
+
+exports.testIsValidURI = function (assert) {
+ validURIs().forEach(function (aUri) {
+ assert.equal(isValidURI(aUri), true, aUri + ' is a valid URL');
+ });
+};
+
+exports.testIsInvalidURI = function (assert) {
+ invalidURIs().forEach(function (aUri) {
+ assert.equal(isValidURI(aUri), false, aUri + ' is an invalid URL');
+ });
+};
+
+exports.testURLFromURL = function(assert) {
+ let aURL = URL('http://mozilla.org');
+ let bURL = URL(aURL);
+ assert.equal(aURL.toString(), bURL.toString(), 'Making a URL from a URL works');
+};
+
+exports.testTLD = function(assert) {
+ let urls = [
+ { url: 'http://my.sub.domains.mozilla.co.uk', tld: 'co.uk' },
+ { url: 'http://my.mozilla.com', tld: 'com' },
+ { url: 'http://my.domains.mozilla.org.hk', tld: 'org.hk' },
+ { url: 'chrome://global/content/blah', tld: 'global' },
+ { url: 'data:text/plain;base64,QXdlc29tZSE=', tld: null },
+ { url: 'https://1.2.3.4', tld: null }
+ ];
+
+ urls.forEach(function (uri) {
+ assert.equal(getTLD(uri.url), uri.tld);
+ assert.equal(getTLD(URL(uri.url)), uri.tld);
+ });
+}
+
+exports.testWindowLocationMatch = function (assert, done) {
+ let server = httpd.startServerAsync(port);
+ server.registerPathHandler('/index.html', function (request, response) {
+ response.write('<html><head></head><body><h1>url tests</h1></body></html>');
+ });
+
+ let aUrl = 'http://localhost:' + port + '/index.html?q=aQuery#somehash';
+ let urlObject = URL(aUrl);
+
+ tabs.open({
+ url: aUrl,
+ onReady: function (tab) {
+ tab.attach({
+ onMessage: function (loc) {
+ for (let prop in loc) {
+ assert.equal(urlObject[prop], loc[prop], prop + ' matches');
+ }
+
+ tab.close(() => server.stop(done));
+ },
+ contentScript: '(' + function () {
+ let res = {};
+ // `origin` is `null` in this context???
+ let props = 'hostname,port,pathname,hash,href,protocol,search'.split(',');
+ props.forEach(function (prop) {
+ res[prop] = window.location[prop];
+ });
+ self.postMessage(res);
+ } + ')()'
+ });
+ }
+ })
+};
+
+exports.testURLInRegExpTest = function(assert) {
+ let url = 'https://mozilla.org';
+ assert.equal((new RegExp(url).test(URL(url))), true, 'URL instances work in a RegExp test');
+}
+
+exports.testLocalURL = function(assert) {
+ [
+ 'data:text/html;charset=utf-8,foo and bar',
+ 'data:text/plain,foo and bar',
+ 'resource://gre/modules/commonjs/',
+ 'chrome://browser/content/browser.xul'
+ ].forEach(aUri => {
+ assert.ok(isLocalURL(aUri), aUri + ' is a Local URL');
+ })
+
+}
+
+exports.testLocalURLwithRemoteURL = function(assert) {
+ validURIs().filter(url => !url.startsWith('data:')).forEach(aUri => {
+ assert.ok(!isLocalURL(aUri), aUri + ' is an invalid Local URL');
+ });
+}
+
+exports.testLocalURLwithInvalidURL = function(assert) {
+ invalidURIs().concat([
+ 'data:foo and bar',
+ 'resource:// must fail',
+ 'chrome:// here too'
+ ]).forEach(aUri => {
+ assert.ok(!isLocalURL(aUri), aUri + ' is an invalid Local URL');
+ });
+}
+
+exports.testFileName = function(assert) {
+ let urls = [
+ ['https://foo/bar.js', 'bar.js'],
+ ['chrome://gaia/content/myfxosapp/file.js', 'file.js'],
+ ['http://localhost:8888/file.js', 'file.js'],
+ ['http://foo/bar.js#hash', 'bar.js'],
+ ['http://foo/bar.js?q=go&query=yeah', 'bar.js'],
+ ['chrome://browser/content/content.js', 'content.js'],
+ ['resource://gre/foo.js', 'foo.js'],
+ ];
+
+ urls.forEach(([url, fileName]) => assert.equal(URL(url).fileName, fileName, 'file names are equal'));
+};
+
+function validURIs() {
+ return [
+ 'http://foo.com/blah_blah',
+ 'http://foo.com/blah_blah/',
+ 'http://foo.com/blah_blah_(wikipedia)',
+ 'http://foo.com/blah_blah_(wikipedia)_(again)',
+ 'http://www.example.com/wpstyle/?p=364',
+ 'https://www.example.com/foo/?bar=baz&amp;inga=42&amp;quux',
+ 'http://✪df.ws/123',
+ 'http://userid:password@example.com:8080',
+ 'http://userid:password@example.com:8080/',
+ 'http://userid@example.com',
+ 'http://userid@example.com/',
+ 'http://userid@example.com:8080',
+ 'http://userid@example.com:8080/',
+ 'http://userid:password@example.com',
+ 'http://userid:password@example.com/',
+ 'http://142.42.1.1/',
+ 'http://142.42.1.1:8080/',
+ 'http://➡.ws/䨹',
+ 'http://⌘.ws',
+ 'http://⌘.ws/',
+ 'http://foo.com/blah_(wikipedia)#cite-1',
+ 'http://foo.com/blah_(wikipedia)_blah#cite-1',
+ 'http://foo.com/unicode_(✪)_in_parens',
+ 'http://foo.com/(something)?after=parens',
+ 'http://☺.damowmow.com/',
+ 'http://code.google.com/events/#&amp;product=browser',
+ 'http://j.mp',
+ 'ftp://foo.bar/baz',
+ 'http://foo.bar/?q=Test%20URL-encoded%20stuff',
+ 'http://مثال.إختبار',
+ 'http://例子.测试',
+ 'http://उदाहरण.परीक्षा',
+ 'http://-.~_!$&amp;\'()*+,;=:%40:80%2f::::::@example.com',
+ 'http://1337.net',
+ 'http://a.b-c.de',
+ 'http://223.255.255.254',
+ // Also want to validate data-uris, localhost
+ 'http://localhost:8432/some-file.js',
+ 'data:text/plain;base64,',
+ 'data:text/html;charset=US-ASCII,%3Ch1%3EHello!%3C%2Fh1%3E',
+ 'data:text/html;charset=utf-8,'
+ ];
+}
+
+// Some invalidURIs are valid according to the regex used,
+// can be improved in the future, but better to pass some
+// invalid URLs than prevent valid URLs
+
+function invalidURIs () {
+ return [
+// 'http://',
+// 'http://.',
+// 'http://..',
+// 'http://../',
+// 'http://?',
+// 'http://??',
+// 'http://??/',
+// 'http://#',
+// 'http://##',
+// 'http://##/',
+// 'http://foo.bar?q=Spaces should be encoded',
+ 'not a url',
+ '//',
+ '//a',
+ '///a',
+ '///',
+// 'http:///a',
+ 'foo.com',
+ 'http:// shouldfail.com',
+ ':// should fail',
+// 'http://foo.bar/foo(bar)baz quux',
+// 'http://-error-.invalid/',
+// 'http://a.b--c.de/',
+// 'http://-a.b.co',
+// 'http://a.b-.co',
+// 'http://0.0.0.0',
+// 'http://10.1.1.0',
+// 'http://10.1.1.255',
+// 'http://224.1.1.1',
+// 'http://1.1.1.1.1',
+// 'http://123.123.123',
+// 'http://3628126748',
+// 'http://.www.foo.bar/',
+// 'http://www.foo.bar./',
+// 'http://.www.foo.bar./',
+// 'http://10.1.1.1',
+// 'http://10.1.1.254'
+ ];
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-uuid.js b/addon-sdk/source/test/test-uuid.js
new file mode 100644
index 000000000..e5ade499e
--- /dev/null
+++ b/addon-sdk/source/test/test-uuid.js
@@ -0,0 +1,26 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { uuid } = require('sdk/util/uuid');
+
+exports['test generate uuid'] = function(assert) {
+ let signature = /{[0-9a-f\-]+}/
+ let first = String(uuid());
+ let second = String(uuid());
+
+ assert.ok(signature.test(first), 'first guid has a correct signature');
+ assert.ok(signature.test(second), 'second guid has a correct signature');
+ assert.notEqual(first, second, 'guid generates new guid on each call');
+};
+
+exports['test parse uuid'] = function(assert) {
+ let firefoxUUID = '{ec8030f7-c20a-464f-9b0e-13a3a9e97384}';
+ let actual = uuid(firefoxUUID);
+
+ assert.equal(actual.number, firefoxUUID, 'uuid parsed given string');
+ assert.equal(String(actual), firefoxUUID, 'serializes to the same value');
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-weak-set.js b/addon-sdk/source/test/test-weak-set.js
new file mode 100644
index 000000000..f7cc19e04
--- /dev/null
+++ b/addon-sdk/source/test/test-weak-set.js
@@ -0,0 +1,146 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Cu } = require("chrome");
+const memory = require("sdk/test/memory");
+const { add, remove, has, clear, iterator } = require("sdk/lang/weak-set");
+const { setInterval, clearInterval } = require("sdk/timers");
+
+function gc(assert) {
+ let wait = 1;
+ let interval = setInterval(function() {
+ assert.pass("waited " + (wait++ * 0.250) + "secs for gc()..");
+ }, 250);
+
+ return memory.gc().then(() => {
+ assert.pass("gc completed!");
+ clearInterval(interval);
+ });
+}
+
+exports['test add/remove/iterate/clear item'] = function*(assert) {
+ let addItems = {};
+ let removeItems = {};
+ let iterateItems = {};
+ let clearItems = {};
+ let nonReferencedItems = {};
+
+ let item = {};
+ let addedItems = [{}, {}];
+
+ assert.pass("adding things to items");
+ add(addItems, item);
+ add(removeItems, item);
+ add(iterateItems, addedItems[0]);
+ add(iterateItems, addedItems[1]);
+ add(iterateItems, addedItems[0]); // weak set shouldn't add this twice
+ add(clearItems, addedItems[0]);
+ add(clearItems, addedItems[1]);
+ add(nonReferencedItems, {});
+
+ assert.pass("removing things from removeItems");
+ remove(removeItems, item);
+
+ assert.pass("clear things from clearItems");
+ clear(clearItems);
+
+ assert.pass("starting gc..");
+ yield gc(assert);
+ let count = 0;
+
+ assert.equal(has(addItems, item), true, 'the addItems is in the weak set');
+ assert.equal(has(removeItems, item), false, 'the removeItems is not in weak set');
+
+ assert.pass("iterating iterateItems..");
+ for (let item of iterator(iterateItems)) {
+ assert.equal(item, addedItems[count], "item in the expected order");
+ count++;
+ }
+
+ assert.equal(count, 2, 'items in the expected number');
+
+ assert.pass("iterating clearItems..");
+ for (let item of iterator(clearItems)) {
+ assert.fail("the loop should not be executed");
+ count++
+ }
+
+ for (let item of iterator(nonReferencedItems)) {
+ assert.fail("the loop should not be executed");
+ count++
+ }
+
+ assert.equal(count, 2, 'items in the expected number');
+};
+
+exports['test adding non object or null item'] = function(assert) {
+ let items = {};
+
+ assert.throws(() => {
+ add(items, 'foo');
+ },
+ /^\w+ is not a non-null object/,
+ 'only non-null object are allowed');
+
+ assert.throws(() => {
+ add(items, 0);
+ },
+ /^\w+ is not a non-null object/,
+ 'only non-null object are allowed');
+
+ assert.throws(() => {
+ add(items, undefined);
+ },
+ /^\w+ is not a non-null object/,
+ 'only non-null object are allowed');
+
+ assert.throws(() => {
+ add(items, null);
+ },
+ /^\w+ is not a non-null object/,
+ 'only non-null object are allowed');
+
+ assert.throws(() => {
+ add(items, true);
+ },
+ /^\w+ is not a non-null object/,
+ 'only non-null object are allowed');
+};
+
+exports['test adding to non object or null item'] = function(assert) {
+ let item = {};
+
+ assert.throws(() => {
+ add('foo', item);
+ },
+ /^\w+ is not a non-null object/,
+ 'only non-null object are allowed');
+
+ assert.throws(() => {
+ add(0, item);
+ },
+ /^\w+ is not a non-null object/,
+ 'only non-null object are allowed');
+
+ assert.throws(() => {
+ add(undefined, item);
+ },
+ /^\w+ is not a non-null object/,
+ 'only non-null object are allowed');
+
+ assert.throws(() => {
+ add(null, item);
+ },
+ /^\w+ is not a non-null object/,
+ 'only non-null object are allowed');
+
+ assert.throws(() => {
+ add(true, item);
+ },
+ /^\w+ is not a non-null object/,
+ 'only non-null object are allowed');
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-window-events.js b/addon-sdk/source/test/test-window-events.js
new file mode 100644
index 000000000..19580f42a
--- /dev/null
+++ b/addon-sdk/source/test/test-window-events.js
@@ -0,0 +1,63 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+// Opening new windows in Fennec causes issues
+module.metadata = {
+ engines: {
+ 'Firefox': '*'
+ }
+};
+
+const { Loader } = require("sdk/test/loader");
+const { open, getMostRecentBrowserWindow, getOuterId } = require("sdk/window/utils");
+
+exports["test browser events"] = function(assert, done) {
+ let loader = Loader(module);
+ let { events } = loader.require("sdk/window/events");
+ let { on, off } = loader.require("sdk/event/core");
+ let actual = [];
+
+ on(events, "data", function handler(e) {
+ actual.push(e);
+
+ if (e.type === "open") {
+ assert.pass("window open has occured");
+ }
+ else if (e.type === "DOMContentLoaded") {
+ assert.pass("window DOMContentLoaded has occured");
+ }
+ else if (e.type === "load") {
+ assert.pass("window load has occured");
+ window.close();
+ }
+ else if (e.type === "close") {
+ // confirm the ordering of events
+ let [ open, ready, load, close ] = actual;
+ assert.equal(open.type, "open")
+ assert.equal(open.target, window, "window is open")
+
+ assert.equal(ready.type, "DOMContentLoaded")
+ assert.equal(ready.target, window, "window ready")
+
+ assert.equal(load.type, "load")
+ assert.equal(load.target, window, "window load")
+
+ assert.equal(close.type, "close")
+ assert.equal(close.target, window, "window load")
+
+ // Note: If window is closed right after this GC won't have time
+ // to claim loader and there for this listener. It's better to remove
+ // remove listener here to avoid race conditions.
+ off(events, "data", handler);
+ loader.unload();
+ done();
+ }
+ });
+
+ // Open window and close it to trigger observers.
+ let window = open();
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-window-observer.js b/addon-sdk/source/test/test-window-observer.js
new file mode 100644
index 000000000..7efb6993a
--- /dev/null
+++ b/addon-sdk/source/test/test-window-observer.js
@@ -0,0 +1,61 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Loader } = require("sdk/test/loader");
+const { open, close } = require("sdk/window/helpers");
+const { browserWindows: windows } = require("sdk/windows");
+const { isBrowser } = require('sdk/window/utils');
+const app = require("sdk/system/xul-app");
+
+exports["test unload window observer"] = function(assert, done) {
+ // Hacky way to be able to create unloadable modules via makeSandboxedLoader.
+ let loader = Loader(module);
+ let observer = loader.require("sdk/windows/observer").observer;
+ let opened = 0;
+ let closed = 0;
+ let windowsOpen = windows.length;
+
+ observer.on("open", onOpen);
+ observer.on("close", onClose);
+
+ // On Fennec, only test that the module does not throw an error
+ if (app.is("Fennec")) {
+ assert.pass("Windows observer did not throw on Fennec");
+ return cleanUp();
+ }
+
+ // Open window and close it to trigger observers.
+ open().
+ then(close).
+ then(loader.unload).
+ then(open).
+ then(close).
+ then(function() {
+ // Enqueuing asserts to make sure that assertion is not performed early.
+ assert.equal(1, opened, "observer open was called before unload only");
+ assert.equal(windowsOpen + 1, closed, "observer close was called before unload only");
+ }).
+ then(cleanUp, assert.fail);
+
+ function cleanUp () {
+ observer.removeListener("open", onOpen);
+ observer.removeListener("close", onClose);
+ done();
+ }
+
+ function onOpen(window) {
+ // Ignoring non-browser windows
+ if (isBrowser(window))
+ opened++;
+ }
+ function onClose(window) {
+ // Ignore non-browser windows & already opened `activeWindow` (unload will
+ // emit close on it even though it is not actually closed).
+ if (isBrowser(window))
+ closed++;
+ }
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-window-utils-private-browsing.js b/addon-sdk/source/test/test-window-utils-private-browsing.js
new file mode 100644
index 000000000..cf57b19f3
--- /dev/null
+++ b/addon-sdk/source/test/test-window-utils-private-browsing.js
@@ -0,0 +1,210 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+// Fennec support tracked in bug #809412
+module.metadata = {
+ 'engines': {
+ 'Firefox': '*'
+ }
+};
+
+const windowUtils = require('sdk/deprecated/window-utils');
+const { Cc, Ci } = require('chrome');
+const { isWindowPBSupported } = require('sdk/private-browsing/utils');
+const { getFrames, getWindowTitle, onFocus, isWindowPrivate } = require('sdk/window/utils');
+const { open, close, focus } = require('sdk/window/helpers');
+const WM = Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator);
+const { isPrivate } = require('sdk/private-browsing');
+const { fromIterator: toArray } = require('sdk/util/array');
+const { defer } = require('sdk/core/promise');
+const { setTimeout } = require('sdk/timers');
+
+function tick() {
+ let deferred = defer();
+ setTimeout(deferred.resolve);
+ return deferred.promise;
+}
+
+function makeEmptyBrowserWindow(options) {
+ options = options || {};
+ return open('chrome://browser/content/browser.xul', {
+ features: {
+ chrome: true,
+ private: !!options.private
+ }
+ });
+}
+
+exports.testWindowTrackerIgnoresPrivateWindows = function(assert, done) {
+ var myNonPrivateWindow, myPrivateWindow;
+ var finished = false;
+ var privateWindow;
+ var privateWindowClosed = false;
+
+ let wt = windowUtils.WindowTracker({
+ onTrack: function(window) {
+ assert.ok(!isWindowPrivate(window), 'private window was not tracked!');
+ },
+ onUntrack: function(window) {
+ assert.ok(!isWindowPrivate(window), 'private window was not tracked!');
+ // PWPB case
+ if (window === myPrivateWindow && isWindowPBSupported) {
+ privateWindowClosed = true;
+ }
+ if (window === myNonPrivateWindow) {
+ assert.ok(!privateWindowClosed);
+ wt.unload();
+ done();
+ }
+ }
+ });
+
+ // make a new private window
+ makeEmptyBrowserWindow({
+ private: true
+ }).then(function(window) {
+ myPrivateWindow = window;
+
+ assert.equal(isWindowPrivate(window), isWindowPBSupported);
+ assert.ok(getFrames(window).length > 1, 'there are frames for private window');
+ assert.equal(getWindowTitle(window), window.document.title,
+ 'getWindowTitle works');
+
+ return close(window).then(function() {
+ return makeEmptyBrowserWindow().then(function(window) {
+ myNonPrivateWindow = window;
+ assert.pass('opened new window');
+ return close(window);
+ });
+ });
+ }).then(null, assert.fail);
+};
+
+// Test setting activeWIndow and onFocus for private windows
+exports.testSettingActiveWindowDoesNotIgnorePrivateWindow = function(assert, done) {
+ let browserWindow = WM.getMostRecentWindow("navigator:browser");
+
+ assert.equal(windowUtils.activeBrowserWindow, browserWindow,
+ "Browser window is the active browser window.");
+ assert.ok(!isPrivate(browserWindow), "Browser window is not private.");
+
+ // make a new private window
+ makeEmptyBrowserWindow({ private: true }).then(focus).then(window => {
+ // PWPB case
+ if (isWindowPBSupported) {
+ assert.ok(isPrivate(window), "window is private");
+ assert.notStrictEqual(windowUtils.activeBrowserWindow, browserWindow);
+ }
+ // Global case
+ else {
+ assert.ok(!isPrivate(window), "window is not private");
+ }
+
+ assert.strictEqual(windowUtils.activeBrowserWindow, window,
+ "Correct active browser window pb supported");
+ assert.notStrictEqual(browserWindow, window,
+ "The window is not the old browser window");
+
+
+ return onFocus(windowUtils.activeWindow = browserWindow).then(_ => {
+ assert.strictEqual(windowUtils.activeWindow, browserWindow,
+ "Correct active window [1]");
+ assert.strictEqual(windowUtils.activeBrowserWindow, browserWindow,
+ "Correct active browser window [1]");
+
+ // test focus(window)
+ return focus(window).then(w => {
+ assert.strictEqual(w, window, 'require("sdk/window/helpers").focus on window works');
+ }).then(tick);
+ }).then(_ => {
+ assert.strictEqual(windowUtils.activeBrowserWindow, window,
+ "Correct active browser window [2]");
+ assert.strictEqual(windowUtils.activeWindow, window,
+ "Correct active window [2]");
+
+ // test setting a private window
+ return onFocus(windowUtils.activeWindow = window);
+ }).then(function() {
+ assert.strictEqual(windowUtils.activeBrowserWindow, window,
+ "Correct active browser window [3]");
+ assert.strictEqual(windowUtils.activeWindow, window,
+ "Correct active window [3]");
+
+ // just to get back to original state
+ return onFocus(windowUtils.activeWindow = browserWindow);
+ }).then(_ => {
+ assert.strictEqual(windowUtils.activeBrowserWindow, browserWindow,
+ "Correct active browser window when pb mode is supported [4]");
+ assert.strictEqual(windowUtils.activeWindow, browserWindow,
+ "Correct active window when pb mode is supported [4]");
+
+ return close(window);
+ })
+ }).then(done).then(null, assert.fail);
+};
+
+exports.testActiveWindowDoesNotIgnorePrivateWindow = function(assert, done) {
+ // make a new private window
+ makeEmptyBrowserWindow({
+ private: true
+ }).then(function(window) {
+ // PWPB case
+ if (isWindowPBSupported) {
+ assert.equal(isPrivate(windowUtils.activeWindow), true,
+ "active window is private");
+ assert.equal(isPrivate(windowUtils.activeBrowserWindow), true,
+ "active browser window is private");
+ assert.ok(isWindowPrivate(window), "window is private");
+ assert.ok(isPrivate(window), "window is private");
+
+ // pb mode is supported
+ assert.ok(
+ isWindowPrivate(windowUtils.activeWindow),
+ "active window is private when pb mode is supported");
+ assert.ok(
+ isWindowPrivate(windowUtils.activeBrowserWindow),
+ "active browser window is private when pb mode is supported");
+ assert.ok(isPrivate(windowUtils.activeWindow),
+ "active window is private when pb mode is supported");
+ assert.ok(isPrivate(windowUtils.activeBrowserWindow),
+ "active browser window is private when pb mode is supported");
+ }
+ // Global case
+ else {
+ assert.equal(isPrivate(windowUtils.activeWindow), false,
+ "active window is not private");
+ assert.equal(isPrivate(windowUtils.activeBrowserWindow), false,
+ "active browser window is not private");
+ assert.equal(isWindowPrivate(window), false, "window is not private");
+ assert.equal(isPrivate(window), false, "window is not private");
+ }
+
+ return close(window);
+ }).then(done).then(null, assert.fail);
+}
+
+exports.testWindowIteratorIgnoresPrivateWindows = function(assert, done) {
+ // make a new private window
+ makeEmptyBrowserWindow({
+ private: true
+ }).then(function(window) {
+ // PWPB case
+ if (isWindowPBSupported) {
+ assert.ok(isWindowPrivate(window), "window is private");
+ assert.equal(toArray(windowUtils.windowIterator()).indexOf(window), -1,
+ "window is not in windowIterator()");
+ }
+ // Global case
+ else {
+ assert.equal(isWindowPrivate(window), false, "window is not private");
+ assert.ok(toArray(windowUtils.windowIterator()).indexOf(window) > -1,
+ "window is in windowIterator()");
+ }
+
+ return close(window);
+ }).then(done).then(null, assert.fail);
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-window-utils.js b/addon-sdk/source/test/test-window-utils.js
new file mode 100644
index 000000000..77a9d8d6a
--- /dev/null
+++ b/addon-sdk/source/test/test-window-utils.js
@@ -0,0 +1,266 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+module.metadata = {
+ engines: {
+ 'Firefox': '*'
+ }
+};
+
+const windowUtils = require("sdk/deprecated/window-utils");
+const timer = require("sdk/timers");
+const { Cc, Ci } = require("chrome");
+const { Loader } = require("sdk/test/loader");
+const { open, getFrames, getWindowTitle, onFocus, windows } = require('sdk/window/utils');
+const { close } = require('sdk/window/helpers');
+const { fromIterator: toArray } = require('sdk/util/array');
+
+const WM = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
+
+function makeEmptyWindow(options) {
+ options = options || {};
+ var xulNs = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
+ var blankXul = ('<?xml version="1.0"?>' +
+ '<?xml-stylesheet href="chrome://global/skin/" ' +
+ ' type="text/css"?>' +
+ '<window xmlns="' + xulNs + '" windowtype="test:window">' +
+ '</window>');
+
+ return open("data:application/vnd.mozilla.xul+xml;charset=utf-8," + escape(blankXul), {
+ features: {
+ chrome: true,
+ width: 10,
+ height: 10
+ }
+ });
+}
+
+exports.testWindowTracker = function(assert, done) {
+ var myWindow = makeEmptyWindow();
+ assert.pass('window was created');
+
+ myWindow.addEventListener("load", function onload() {
+ myWindow.removeEventListener("load", onload, false);
+ assert.pass("test window has opened");
+
+ // test bug 638007 (new is optional), using new
+ var wt = new windowUtils.WindowTracker({
+ onTrack: window => {
+ if (window === myWindow) {
+ assert.pass("onTrack() called with our test window");
+ close(window);
+ }
+ },
+ onUntrack: window => {
+ if (window === myWindow) {
+ assert.pass("onUntrack() called with our test window");
+ wt.unload();
+ timer.setTimeout(done);
+ }
+ }
+ });
+ }, false);
+};
+
+exports['test window watcher untracker'] = function(assert, done) {
+ var myWindow;
+ var tracks = 0;
+ var unloadCalled = false;
+
+ var delegate = {
+ onTrack: function(window) {
+ tracks = tracks + 1;
+ if (window == myWindow) {
+ assert.pass("onTrack() called with our test window");
+ timer.setTimeout(function() {
+ myWindow.close();
+ }, 1);
+ }
+ },
+ onUntrack: function(window) {
+ tracks = tracks - 1;
+ if (window == myWindow && !unloadCalled) {
+ unloadCalled = true;
+ timer.setTimeout(function() {
+ wt.unload();
+ }, 1);
+ }
+ if (0 > tracks) {
+ assert.fail("WindowTracker onUntrack was called more times than onTrack..");
+ }
+ else if (0 == tracks) {
+ timer.setTimeout(function() {
+ myWindow = null;
+ done();
+ }, 1);
+ }
+ }
+ };
+
+ // test bug 638007 (new is optional), not using new
+ var wt = windowUtils.WindowTracker(delegate);
+ myWindow = makeEmptyWindow();
+};
+
+// test that _unregWindow calls _unregLoadingWindow
+exports['test window watcher unregs 4 loading wins'] = function(assert, done) {
+ var myWindow;
+ var finished = false;
+ let browserWindow = WM.getMostRecentWindow("navigator:browser");
+ var counter = 0;
+
+ var delegate = {
+ onTrack: function(window) {
+ var type = window.document.documentElement.getAttribute("windowtype");
+ if (type == "test:window")
+ assert.fail("onTrack shouldn't have been executed.");
+ }
+ };
+ var wt = new windowUtils.WindowTracker(delegate);
+
+ // make a new window
+ myWindow = makeEmptyWindow();
+
+ // make sure that the window hasn't loaded yet
+ assert.notEqual(
+ myWindow.document.readyState,
+ "complete",
+ "window hasn't loaded yet.");
+
+ // unload WindowTracker
+ wt.unload();
+
+ // make sure that the window still hasn't loaded, which means that the onTrack
+ // would have been removed successfully assuming that it doesn't execute.
+ assert.notEqual(
+ myWindow.document.readyState,
+ "complete",
+ "window still hasn't loaded yet.");
+
+ // wait for the window to load and then close it. onTrack wouldn't be called
+ // until the window loads, so we must let it load before closing it to be
+ // certain that onTrack was removed.
+ myWindow.addEventListener("load", function() {
+ // allow all of the load handles to execute before closing
+ myWindow.setTimeout(function() {
+ myWindow.addEventListener("unload", function() {
+ // once the window unloads test is done
+ done();
+ }, false);
+ myWindow.close();
+ }, 0);
+ }, false);
+}
+
+exports['test window watcher without untracker'] = function(assert, done) {
+ let myWindow;
+ let wt = new windowUtils.WindowTracker({
+ onTrack: function(window) {
+ if (window == myWindow) {
+ assert.pass("onTrack() called with our test window");
+
+ close(myWindow).then(function() {
+ wt.unload();
+ done();
+ }, assert.fail);
+ }
+ }
+ });
+
+ myWindow = makeEmptyWindow();
+};
+
+exports['test active window'] = function(assert, done) {
+ let browserWindow = WM.getMostRecentWindow("navigator:browser");
+ let continueAfterFocus = window => onFocus(window).then(nextTest);
+
+ assert.equal(windowUtils.activeBrowserWindow, browserWindow,
+ "Browser window is the active browser window.");
+
+
+ let testSteps = [
+ function() {
+ continueAfterFocus(windowUtils.activeWindow = browserWindow);
+ },
+ function() {
+ assert.equal(windowUtils.activeWindow, browserWindow,
+ "Correct active window [1]");
+ nextTest();
+ },
+ function() {
+ assert.equal(windowUtils.activeBrowserWindow, browserWindow,
+ "Correct active browser window [2]");
+ continueAfterFocus(windowUtils.activeWindow = browserWindow);
+ },
+ function() {
+ assert.equal(windowUtils.activeWindow, browserWindow,
+ "Correct active window [3]");
+ nextTest();
+ },
+ function() {
+ assert.equal(windowUtils.activeBrowserWindow, browserWindow,
+ "Correct active browser window [4]");
+ done();
+ }
+ ];
+
+ function nextTest() {
+ if (testSteps.length)
+ testSteps.shift()();
+ }
+ nextTest();
+};
+
+exports.testWindowIterator = function(assert, done) {
+ // make a new window
+ let window = makeEmptyWindow();
+
+ // make sure that the window hasn't loaded yet
+ assert.notEqual(
+ window.document.readyState,
+ "complete",
+ "window hasn't loaded yet.");
+
+ // this window should only appear in windowIterator() while its loading
+ assert.ok(toArray(windowUtils.windowIterator()).indexOf(window) === -1,
+ "window isn't in windowIterator()");
+
+ // Then it should be in windowIterator()
+ window.addEventListener("load", function onload() {
+ window.addEventListener("load", onload, false);
+ assert.ok(toArray(windowUtils.windowIterator()).indexOf(window) !== -1,
+ "window is now in windowIterator()");
+
+ // Wait for the window unload before ending test
+ close(window).then(done);
+ }, false);
+};
+
+exports.testIgnoreClosingWindow = function(assert, done) {
+ assert.equal(windows().length, 1, "Only one window open");
+
+ // make a new window
+ let window = makeEmptyWindow();
+
+ assert.equal(windows().length, 2, "Two windows open");
+
+ window.addEventListener("load", function onload() {
+ window.addEventListener("load", onload, false);
+
+ assert.equal(windows().length, 2, "Two windows open");
+
+ // Wait for the window unload before ending test
+ let checked = false;
+
+ close(window).then(function() {
+ assert.ok(checked, 'the test is finished');
+ }).then(done, assert.fail)
+
+ assert.equal(windows().length, 1, "Only one window open");
+ checked = true;
+ }, false);
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-window-utils2.js b/addon-sdk/source/test/test-window-utils2.js
new file mode 100644
index 000000000..214ef6ff1
--- /dev/null
+++ b/addon-sdk/source/test/test-window-utils2.js
@@ -0,0 +1,112 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+// Opening new windows in Fennec causes issues
+module.metadata = {
+ engines: {
+ 'Firefox': '*'
+ }
+};
+
+const { Ci } = require('chrome');
+const { open, windows, isBrowser,
+ getXULWindow, getBaseWindow, getToplevelWindow, getMostRecentWindow,
+ getMostRecentBrowserWindow } = require('sdk/window/utils');
+const { close } = require('sdk/window/helpers');
+const windowUtils = require('sdk/deprecated/window-utils');
+
+exports['test get nsIBaseWindow from nsIDomWindow'] = function(assert) {
+ let active = windowUtils.activeBrowserWindow;
+
+ assert.ok(!(active instanceof Ci.nsIBaseWindow),
+ 'active window is not nsIBaseWindow');
+
+ assert.ok(getBaseWindow(active) instanceof Ci.nsIBaseWindow,
+ 'base returns nsIBaseWindow');
+};
+
+exports['test get nsIXULWindow from nsIDomWindow'] = function(assert) {
+ let active = windowUtils.activeBrowserWindow;
+ assert.ok(!(active instanceof Ci.nsIXULWindow),
+ 'active window is not nsIXULWindow');
+ assert.ok(getXULWindow(active) instanceof Ci.nsIXULWindow,
+ 'base returns nsIXULWindow');
+};
+
+exports['test getToplevelWindow'] = function(assert) {
+ let active = windowUtils.activeBrowserWindow;
+ assert.equal(getToplevelWindow(active), active,
+ 'getToplevelWindow of toplevel window returns the same window');
+ assert.equal(getToplevelWindow(active.content), active,
+ 'getToplevelWindow of tab window returns the browser window');
+ assert.ok(getToplevelWindow(active) instanceof Ci.nsIDOMWindow,
+ 'getToplevelWindow returns nsIDOMWindow');
+};
+
+exports['test top window creation'] = function(assert, done) {
+ let window = open('data:text/html;charset=utf-8,Hello top window');
+ assert.ok(~windows().indexOf(window), 'window was opened');
+
+ // Wait for the window unload before ending test
+ close(window).then(done);
+};
+
+exports['test new top window with options'] = function(assert, done) {
+ let window = open('data:text/html;charset=utf-8,Hi custom top window', {
+ name: 'test',
+ features: { height: 100, width: 200, toolbar: true }
+ });
+ assert.ok(~windows().indexOf(window), 'window was opened');
+ assert.equal(window.name, 'test', 'name was set');
+ assert.equal(window.innerHeight, 100, 'height is set');
+ assert.equal(window.innerWidth, 200, 'height is set');
+ assert.equal(window.toolbar.visible, true, 'toolbar was set');
+
+ // Wait for the window unload before ending test
+ close(window).then(done);
+};
+
+exports['test new top window with various URIs'] = function(assert, done) {
+ let msg = 'only chrome, resource and data uris are allowed';
+ assert.throws(function () {
+ open('foo');
+ }, msg);
+ assert.throws(function () {
+ open('http://foo');
+ }, msg);
+ assert.throws(function () {
+ open('https://foo');
+ }, msg);
+ assert.throws(function () {
+ open('ftp://foo');
+ }, msg);
+ assert.throws(function () {
+ open('//foo');
+ }, msg);
+
+ let chromeWindow = open('chrome://foo/content/');
+ assert.ok(~windows().indexOf(chromeWindow), 'chrome URI works');
+
+ let resourceWindow = open('resource://foo');
+ assert.ok(~windows().indexOf(resourceWindow), 'resource URI works');
+
+ // Wait for the window unload before ending test
+ close(chromeWindow).then(close.bind(null, resourceWindow)).then(done);
+};
+
+exports.testIsBrowser = function(assert) {
+ // dummy window, bad type
+ assert.equal(isBrowser({ document: { documentElement: { getAttribute: function() {
+ return 'navigator:browserx';
+ }}}}), false, 'dummy object with correct stucture and bad type does not pass');
+
+ assert.ok(isBrowser(getMostRecentBrowserWindow()), 'active browser window is a browser window');
+ assert.ok(!isBrowser({}), 'non window is not a browser window');
+ assert.ok(!isBrowser({ document: {} }), 'non window is not a browser window');
+ assert.ok(!isBrowser({ document: { documentElement: {} } }), 'non window is not a browser window');
+ assert.ok(!isBrowser(), 'no argument is not a browser window');
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-windows-common.js b/addon-sdk/source/test/test-windows-common.js
new file mode 100644
index 000000000..a4ca6e1a4
--- /dev/null
+++ b/addon-sdk/source/test/test-windows-common.js
@@ -0,0 +1,104 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Loader } = require('sdk/test/loader');
+const { browserWindows } = require('sdk/windows');
+const { isFocused } = require('sdk/window/utils');
+const { viewFor } = require('sdk/view/core');
+const { modelFor } = require('sdk/model/core');
+const { Ci } = require("chrome");
+const { isBrowser, getWindowTitle } = require("sdk/window/utils");
+const { after, cleanUI } = require("sdk/test/utils");
+
+// TEST: browserWindows Iterator
+exports.testBrowserWindowsIterator = function(assert) {
+ let activeWindowCount = 0;
+ let windows = [];
+ let i = 0;
+ for (let window of browserWindows) {
+ if (window === browserWindows.activeWindow)
+ activeWindowCount++;
+
+ assert.equal(windows.indexOf(window), -1, 'window not already in iterator');
+ assert.equal(browserWindows[i++], window, 'browserWindows[x] works');
+ windows.push(window);
+ }
+ assert.equal(activeWindowCount, 1, 'activeWindow was found in the iterator');
+
+ i = 0;
+ for (let j in browserWindows) {
+ assert.equal(j, i++, 'for (x in browserWindows) works');
+ }
+};
+
+exports.testActiveWindowIsFocused = function(assert) {
+ assert.ok(isFocused(browserWindows.activeWindow), 'the active window is focused');
+}
+
+exports.testWindowTabsObject_alt = function(assert, done) {
+ let window = browserWindows.activeWindow;
+ window.tabs.open({
+ url: 'data:text/html;charset=utf-8,<title>tab 2</title>',
+ inBackground: true,
+ onReady: function onReady(tab) {
+ assert.equal(tab.title, 'tab 2', 'Correct new tab title');
+ assert.notEqual(window.tabs.activeTab, tab, 'Correct active tab');
+
+ // end test
+ tab.close(done);
+ }
+ });
+};
+
+// TEST: browserWindows.activeWindow
+exports.testWindowActivateMethod_simple = function(assert) {
+ let window = browserWindows.activeWindow;
+ let tab = window.tabs.activeTab;
+
+ window.activate();
+
+ assert.equal(browserWindows.activeWindow, window,
+ 'Active window is active after window.activate() call');
+ assert.equal(window.tabs.activeTab, tab,
+ 'Active tab is active after window.activate() call');
+};
+
+
+exports["test getView(window)"] = function*(assert) {
+ let window = yield new Promise(resolve => {
+ browserWindows.once("open", resolve);
+ browserWindows.open({ url: "data:text/html;charset=utf-8,<title>yo</title>" });
+ });
+
+ const view = viewFor(window);
+
+ assert.ok(view instanceof Ci.nsIDOMWindow, "view is a window");
+ assert.ok(isBrowser(view), "view is a browser window");
+ assert.equal(getWindowTitle(view), window.title,
+ "window has a right title");
+};
+
+
+exports["test modelFor(window)"] = function(assert, done) {
+ browserWindows.once("open", window => {
+ const view = viewFor(window);
+
+ assert.ok(view instanceof Ci.nsIDOMWindow, "view is a window");
+ assert.ok(isBrowser(view), "view is a browser window");
+ assert.ok(modelFor(view) === window, "modelFor(browserWindow) is SDK window");
+
+ window.close(done);
+ });
+
+
+ browserWindows.open({ url: "data:text/html;charset=utf-8,<title>yo</title>" });
+};
+
+after(exports, function*(name, assert) {
+ assert.pass("cleaning the ui.");
+ yield cleanUI();
+});
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-windows.js b/addon-sdk/source/test/test-windows.js
new file mode 100644
index 000000000..dd6998b0f
--- /dev/null
+++ b/addon-sdk/source/test/test-windows.js
@@ -0,0 +1,24 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const app = require("sdk/system/xul-app");
+const packaging = require("@loader/options");
+
+if (app.is("Firefox")) {
+ module.exports = require("./windows/test-firefox-windows");
+}
+else if (app.is("Fennec")) {
+ module.exports = require("./windows/test-fennec-windows");
+}
+else {
+ module.exports = {
+ "test Unsupported Application": (assert) => {
+ assert.pass(
+ "The windows module currently supports only Firefox and Fennec." +
+ "In the future we would like it to support other applications, however.");
+ }
+ };
+ require("sdk/test").run(exports);
+}
diff --git a/addon-sdk/source/test/test-xhr.js b/addon-sdk/source/test/test-xhr.js
new file mode 100644
index 000000000..30ee8e362
--- /dev/null
+++ b/addon-sdk/source/test/test-xhr.js
@@ -0,0 +1,89 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+ 'use strict'
+
+const { XMLHttpRequest } = require('sdk/net/xhr');
+const { LoaderWithHookedConsole } = require('sdk/test/loader');
+const { set: setPref } = require("sdk/preferences/service");
+const data = require("./fixtures");
+
+const DEPRECATE_PREF = "devtools.errorconsole.deprecation_warnings";
+
+exports.testAPIExtension = function(assert) {
+ let { loader, messages } = LoaderWithHookedConsole(module);
+ let { XMLHttpRequest } = loader.require("sdk/net/xhr");
+ setPref(DEPRECATE_PREF, true);
+
+ let xhr = new XMLHttpRequest();
+ assert.equal(typeof(xhr.forceAllowThirdPartyCookie), "function",
+ "forceAllowThirdPartyCookie is defined");
+ assert.equal(xhr.forceAllowThirdPartyCookie(), undefined,
+ "function can be called");
+
+ assert.ok(messages[0].msg.indexOf("`xhr.forceAllowThirdPartyCookie()` is deprecated") >= 0,
+ "deprecation warning was dumped");
+ assert.ok(xhr.mozBackgroundRequest, "is background request");
+
+ loader.unload();
+};
+
+exports.testAbortedXhr = function(assert, done) {
+ let req = new XMLHttpRequest();
+ req.open('GET', data.url('testLocalXhr.json'));
+ req.addEventListener("abort", function() {
+ assert.pass("request was aborted");
+ done();
+ });
+ req.send(null);
+ req.abort();
+};
+
+exports.testLocalXhr = function(assert, done) {
+ let req = new XMLHttpRequest();
+ let ready = false;
+
+ req.overrideMimeType('text/plain');
+ req.open('GET', data.url('testLocalXhr.json'));
+ req.onreadystatechange = function() {
+ if (req.readyState == 4 && (req.status == 0 || req.status == 200)) {
+ ready = true;
+ assert.equal(req.responseText, '{}\n', 'XMLHttpRequest should get local files');
+ }
+ };
+ req.addEventListener('load', function onload() {
+ req.removeEventListener('load', onload);
+ assert.pass('addEventListener for load event worked');
+ assert.ok(ready, 'onreadystatechange listener worked');
+ done();
+ });
+ req.send(null);
+};
+
+
+exports.testResponseHeaders = function(assert, done) {
+ let req = new XMLHttpRequest();
+
+ req.overrideMimeType('text/plain');
+ req.open('GET', module.uri);
+ req.onreadystatechange = function() {
+ if (req.readyState == 4 && (req.status == 0 || req.status == 200)) {
+ var headers = req.getAllResponseHeaders();
+ headers = headers.split("\r\n");
+ if (headers.length == 1) {
+ headers = headers[0].split("\n");
+ }
+ for (let i in headers) {
+ if (headers[i] && headers[i].search('Content-Type') >= 0) {
+ assert.equal(headers[i], 'Content-Type: text/plain',
+ 'XHR\'s headers are valid');
+ }
+ }
+
+ done();
+ }
+ };
+ req.send(null);
+}
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/test-xpcom.js b/addon-sdk/source/test/test-xpcom.js
new file mode 100644
index 000000000..113f00a96
--- /dev/null
+++ b/addon-sdk/source/test/test-xpcom.js
@@ -0,0 +1,232 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const xpcom = require("sdk/platform/xpcom");
+const { Cc, Ci, Cm, Cr } = require("chrome");
+const { isCIDRegistered } = Cm.QueryInterface(Ci.nsIComponentRegistrar);
+const { Class } = require("sdk/core/heritage");
+const { Loader } = require("sdk/test/loader");
+const { NetUtil } = require("resource://gre/modules/NetUtil.jsm");
+
+exports['test Unknown implements nsISupports'] = function(assert) {
+ let actual = xpcom.Unknown();
+ assert.equal(actual.QueryInterface(Ci.nsISupports),
+ actual,
+ 'component implements nsISupports');
+};
+
+exports['test implement xpcom interfaces'] = function(assert) {
+ let WeakReference = Class({
+ extends: xpcom.Unknown,
+ interfaces: [ 'nsIWeakReference' ],
+ QueryReferent: function() {}
+ });
+ let weakReference = WeakReference()
+
+ assert.equal(weakReference.QueryInterface(Ci.nsISupports),
+ weakReference,
+ 'component implements nsISupports');
+ assert.equal(weakReference.QueryInterface(Ci.nsIWeakReference),
+ weakReference,
+ 'component implements specified interface');
+
+ assert.throws(function() {
+ component.QueryInterface(Ci.nsIObserver);
+ }, "component does not implements interface");
+
+ let Observer = Class({
+ extends: WeakReference,
+ interfaces: [ 'nsIObserver', 'nsIRequestObserver' ],
+ observe: function() {},
+ onStartRequest: function() {},
+ onStopRequest: function() {}
+ });
+ let observer = Observer()
+
+ assert.equal(observer.QueryInterface(Ci.nsISupports),
+ observer,
+ 'derived component implements nsISupports');
+ assert.equal(observer.QueryInterface(Ci.nsIWeakReference),
+ observer,
+ 'derived component implements supers interface');
+ assert.equal(observer.QueryInterface(Ci.nsIObserver),
+ observer.QueryInterface(Ci.nsIRequestObserver),
+ 'derived component implements specified interfaces');
+};
+
+exports['test implement factory without contract'] = function(assert) {
+ let actual = xpcom.Factory({
+ get wrappedJSObject() {
+ return this;
+ },
+ });
+
+ assert.ok(isCIDRegistered(actual.id), 'factory is regiseterd');
+ xpcom.unregister(actual);
+ assert.ok(!isCIDRegistered(actual.id), 'factory is unregistered');
+};
+
+exports['test implement xpcom factory'] = function(assert) {
+ let Component = Class({
+ extends: xpcom.Unknown,
+ interfaces: [ 'nsIObserver' ],
+ get wrappedJSObject() {
+ return this;
+ },
+ observe: function() {}
+ });
+
+ let factory = xpcom.Factory({
+ register: false,
+ contract: '@jetpack/test/factory;1',
+ Component: Component
+ });
+
+ assert.ok(!isCIDRegistered(factory.id), 'factory is not registered');
+ xpcom.register(factory);
+ assert.ok(isCIDRegistered(factory.id), 'factory is registered');
+
+ let actual = Cc[factory.contract].createInstance(Ci.nsIObserver);
+
+ assert.ok(actual.wrappedJSObject instanceof Component,
+ "createInstance returnes wrapped factory instances");
+
+ assert.notEqual(Cc[factory.contract].createInstance(Ci.nsIObserver),
+ Cc[factory.contract].createInstance(Ci.nsIObserver),
+ "createInstance returns new instance each time");
+};
+
+exports['test implement xpcom service'] = function(assert) {
+ let actual = xpcom.Service({
+ contract: '@jetpack/test/service;1',
+ register: false,
+ Component: Class({
+ extends: xpcom.Unknown,
+ interfaces: [ 'nsIObserver'],
+ get wrappedJSObject() {
+ return this;
+ },
+ observe: function() {},
+ name: 'my-service'
+ })
+ });
+
+ assert.ok(!isCIDRegistered(actual.id), 'component is not registered');
+ xpcom.register(actual);
+ assert.ok(isCIDRegistered(actual.id), 'service is regiseterd');
+ assert.ok(Cc[actual.contract].getService(Ci.nsIObserver).observe,
+ 'service can be accessed via get service');
+ assert.equal(Cc[actual.contract].getService(Ci.nsIObserver).wrappedJSObject,
+ actual.component,
+ 'wrappedJSObject is an actual component');
+ xpcom.unregister(actual);
+ assert.ok(!isCIDRegistered(actual.id), 'service is unregistered');
+};
+
+
+function testRegister(assert, text) {
+
+ const service = xpcom.Service({
+ description: 'test about:boop page',
+ contract: '@mozilla.org/network/protocol/about;1?what=boop',
+ register: false,
+ Component: Class({
+ extends: xpcom.Unknown,
+ get wrappedJSObject() {
+ return this;
+ },
+ interfaces: [ 'nsIAboutModule' ],
+ newChannel : function(aURI, aLoadInfo) {
+ var ios = Cc["@mozilla.org/network/io-service;1"].
+ getService(Ci.nsIIOService);
+
+ var uri = ios.newURI("data:text/plain;charset=utf-8," + text,
+ null, null);
+ var channel = ios.newChannelFromURIWithLoadInfo(uri, aLoadInfo);
+
+ channel.originalURI = aURI;
+ return channel;
+ },
+ getURIFlags: function(aURI) {
+ return Ci.nsIAboutModule.ALLOW_SCRIPT;
+ }
+ })
+ });
+
+ xpcom.register(service);
+
+ assert.equal(isCIDRegistered(service.id), true);
+
+ var aboutFactory = xpcom.factoryByContract(service.contract);
+ var about = aboutFactory.createInstance(Ci.nsIAboutModule);
+
+ var ios = Cc["@mozilla.org/network/io-service;1"].
+ getService(Ci.nsIIOService);
+ assert.equal(
+ about.getURIFlags(ios.newURI("http://foo.com", null, null)),
+ Ci.nsIAboutModule.ALLOW_SCRIPT
+ );
+
+ var channel = NetUtil.newChannel({
+ uri: "about:boop",
+ loadUsingSystemPrincipal: true
+ });
+ var iStream = channel.open2();
+ var siStream = Cc['@mozilla.org/scriptableinputstream;1']
+ .createInstance(Ci.nsIScriptableInputStream);
+ siStream.init(iStream);
+ var data = new String();
+ data += siStream.read(-1);
+ siStream.close();
+ iStream.close();
+ assert.equal(data, text);
+
+ xpcom.unregister(service);
+ assert.equal(isCIDRegistered(service.id), false);
+}
+
+exports["test register"] = function(assert) {
+ testRegister(assert, "hai2u");
+};
+
+exports["test re-register"] = function(assert) {
+ testRegister(assert, "hai2u again");
+};
+
+exports["test unload"] = function(assert) {
+ let loader = Loader(module);
+ let sbxpcom = loader.require("sdk/platform/xpcom");
+
+ let auto = sbxpcom.Factory({
+ contract: "@mozilla.org/test/auto-unload;1",
+ description: "test auto",
+ name: "auto"
+ });
+
+ let manual = sbxpcom.Factory({
+ contract: "@mozilla.org/test/manual-unload;1",
+ description: "test manual",
+ register: false,
+ unregister: false,
+ name: "manual"
+ });
+
+ assert.equal(isCIDRegistered(auto.id), true, 'component registered');
+ assert.equal(isCIDRegistered(manual.id), false, 'component not registered');
+
+ sbxpcom.register(manual)
+ assert.equal(isCIDRegistered(manual.id), true,
+ 'component was automatically registered on first instance');
+ loader.unload();
+
+ assert.equal(isCIDRegistered(auto.id), false,
+ 'component was atumatically unregistered on unload');
+ assert.equal(isCIDRegistered(manual.id), true,
+ 'component was not automatically unregistered on unload');
+ sbxpcom.unregister(manual);
+ assert.equal(isCIDRegistered(manual.id), false,
+ 'component was manually unregistered on unload');
+};
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/test-xul-app.js b/addon-sdk/source/test/test-xul-app.js
new file mode 100644
index 000000000..a57d425d2
--- /dev/null
+++ b/addon-sdk/source/test/test-xul-app.js
@@ -0,0 +1,145 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+ "use strict";
+
+var xulApp = require("sdk/system/xul-app");
+
+exports["test xulapp"] = function (assert) {
+ assert.equal(typeof(xulApp.ID), "string",
+ "ID is a string");
+ assert.equal(typeof(xulApp.name), "string",
+ "name is a string");
+ assert.equal(typeof(xulApp.version), "string",
+ "version is a string");
+ assert.equal(typeof(xulApp.platformVersion), "string",
+ "platformVersion is a string");
+
+ assert.throws(() => xulApp.is("blargy"),
+ /Unkown Mozilla Application: blargy/,
+ "is() throws error on bad app name");
+
+ assert.throws(() => xulApp.isOneOf(["blargy"]),
+ /Unkown Mozilla Application: blargy/,
+ "isOneOf() throws error on bad app name");
+
+ function testSupport(name) {
+ var item = xulApp.is(name);
+ assert.ok(item === true || item === false,
+ "is('" + name + "') is true or false.");
+ }
+
+ var apps = ["Firefox", "Mozilla", "SeaMonkey", "Fennec", "Thunderbird"];
+
+ apps.forEach(testSupport);
+
+ assert.ok(xulApp.isOneOf(apps) == true || xulApp.isOneOf(apps) == false,
+ "isOneOf() returns true or false.");
+
+ assert.equal(xulApp.versionInRange(xulApp.platformVersion, "1.9", "*"),
+ true, "platformVersion in range [1.9, *)");
+ assert.equal(xulApp.versionInRange("3.6.4", "3.6.4", "3.6.*"),
+ true, "3.6.4 in [3.6.4, 3.6.*)");
+ assert.equal(xulApp.versionInRange("1.9.3", "1.9.2", "1.9.3"),
+ false, "1.9.3 not in [1.9.2, 1.9.3)");
+};
+
+exports["test satisfies version range"] = function (assert) {
+ [ ["1.0.0 - 2.0.0", "1.2.3"],
+ ["1.0.0", "1.0.0"],
+ [">=*", "0.2.4"],
+ ["", "1.0.0"],
+ ["*", "1.2.3"],
+ [">=1.0.0", "1.0.0"],
+ [">=1.0.0", "1.0.1"],
+ [">=1.0.0", "1.1.0"],
+ [">1.0.0", "1.0.1"],
+ [">1.0.0", "1.1.0"],
+ ["<=2.0.0", "2.0.0"],
+ ["<=2.0.0", "1.9999.9999"],
+ ["<=2.0.0", "0.2.9"],
+ ["<2.0.0", "1.9999.9999"],
+ ["<2.0.0", "0.2.9"],
+ [">= 1.0.0", "1.0.0"],
+ [">= 1.0.0", "1.0.1"],
+ [">= 1.0.0", "1.1.0"],
+ ["> 1.0.0", "1.0.1"],
+ ["> 1.0.0", "1.1.0"],
+ ["<1", "1.0.0beta"],
+ ["< 1", "1.0.0beta"],
+ ["<= 2.0.0", "2.0.0"],
+ ["<= 2.0.0", "1.9999.9999"],
+ ["<= 2.0.0", "0.2.9"],
+ ["< 2.0.0", "1.9999.9999"],
+ ["<\t2.0.0", "0.2.9"],
+ [">=0.1.97", "0.1.97"],
+ ["0.1.20 || 1.2.4", "1.2.4"],
+ [">=0.2.3 || <0.0.1", "0.0.0"],
+ [">=0.2.3 || <0.0.1", "0.2.3"],
+ [">=0.2.3 || <0.0.1", "0.2.4"],
+ ["||", "1.3.4"],
+ ["2.x.x", "2.1.3"],
+ ["1.2.x", "1.2.3"],
+ ["1.2.x || 2.x", "2.1.3"],
+ ["1.2.x || 2.x", "1.2.3"],
+ ["x", "1.2.3"],
+ ["2.*.*", "2.1.3"],
+ ["1.2.*", "1.2.3"],
+ ["1.2.* || 2.*", "2.1.3"],
+ ["1.2.* || 2.*", "1.2.3"],
+ ["*", "1.2.3"],
+ ["2.*", "2.1.2"],
+ [">=1", "1.0.0"],
+ [">= 1", "1.0.0"],
+ ["<1.2", "1.1.1"],
+ ["< 1.2", "1.1.1"],
+ ["=0.7.x", "0.7.2"],
+ [">=0.7.x", "0.7.2"],
+ ["<=0.7.x", "0.6.2"],
+ ["<=0.7.x", "0.7.2"]
+ ].forEach(function (v) {
+ assert.ok(xulApp.satisfiesVersion(v[1], v[0]), v[0] + " satisfied by " + v[1]);
+ });
+}
+exports["test not satisfies version range"] = function (assert) {
+ [ ["1.0.0 - 2.0.0", "2.2.3"],
+ ["1.0.0", "1.0.1"],
+ [">=1.0.0", "0.0.0"],
+ [">=1.0.0", "0.0.1"],
+ [">=1.0.0", "0.1.0"],
+ [">1.0.0", "0.0.1"],
+ [">1.0.0", "0.1.0"],
+ ["<=2.0.0", "3.0.0"],
+ ["<=2.0.0", "2.9999.9999"],
+ ["<=2.0.0", "2.2.9"],
+ ["<2.0.0", "2.9999.9999"],
+ ["<2.0.0", "2.2.9"],
+ [">=0.1.97", "v0.1.93"],
+ [">=0.1.97", "0.1.93"],
+ ["0.1.20 || 1.2.4", "1.2.3"],
+ [">=0.2.3 || <0.0.1", "0.0.3"],
+ [">=0.2.3 || <0.0.1", "0.2.2"],
+ ["2.x.x", "1.1.3"],
+ ["2.x.x", "3.1.3"],
+ ["1.2.x", "1.3.3"],
+ ["1.2.x || 2.x", "3.1.3"],
+ ["1.2.x || 2.x", "1.1.3"],
+ ["2.*.*", "1.1.3"],
+ ["2.*.*", "3.1.3"],
+ ["1.2.*", "1.3.3"],
+ ["1.2.* || 2.*", "3.1.3"],
+ ["1.2.* || 2.*", "1.1.3"],
+ ["2", "1.1.2"],
+ ["2.3", "2.3.1"],
+ ["2.3", "2.4.1"],
+ ["<1", "1.0.0"],
+ [">=1.2", "1.1.1"],
+ ["1", "2.0.0beta"],
+ ["=0.7.x", "0.8.2"],
+ [">=0.7.x", "0.6.2"],
+ ].forEach(function (v) {
+ assert.ok(!xulApp.satisfiesVersion(v[1], v[0]), v[0] + " not satisfied by " + v[1]);
+ });
+}
+
+require("sdk/test").run(exports);
diff --git a/addon-sdk/source/test/traits/assert.js b/addon-sdk/source/test/traits/assert.js
new file mode 100644
index 000000000..df143f1f5
--- /dev/null
+++ b/addon-sdk/source/test/traits/assert.js
@@ -0,0 +1,101 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+var BaseAssert = require("sdk/test/assert").Assert;
+
+const getOwnIdentifiers = x => [...Object.getOwnPropertyNames(x),
+ ...Object.getOwnPropertySymbols(x)];
+
+/**
+ * Whether or not given property descriptors are equivalent. They are
+ * equivalent either if both are marked as "conflict" or "required" property
+ * or if all the properties of descriptors are equal.
+ * @param {Object} actual
+ * @param {Object} expected
+ */
+function equivalentDescriptors(actual, expected) {
+ return (actual.conflict && expected.conflict) ||
+ (actual.required && expected.required) ||
+ equalDescriptors(actual, expected);
+}
+
+function equalDescriptors(actual, expected) {
+ return actual.get === expected.get &&
+ actual.set === expected.set &&
+ actual.value === expected.value &&
+ !!actual.enumerable === !!expected.enumerable &&
+ !!actual.configurable === !!expected.configurable &&
+ !!actual.writable === !!expected.writable;
+}
+
+/**
+ * Whether or not given `target` array contains all the element
+ * from a given `source` array.
+ */
+function containsSet(source, target) {
+ return source.some(function(element) {
+ return 0 > target.indexOf(element);
+ });
+}
+
+/**
+ * Whether or not given two arrays contain all elements from another.
+ */
+function equivalentSets(source, target) {
+ return containsSet(source, target) && containsSet(target, source);
+}
+
+/**
+ * Finds name of the property from `source` property descriptor map, that
+ * is not equivalent of the name named property in the `target` property
+ * descriptor map. If not found `null` is returned instead.
+ */
+function findNonEquivalentPropertyName(source, target) {
+ var value = null;
+ getOwnIdentifiers(source).some(function(key) {
+ var areEquivalent = false;
+ if (!equivalentDescriptors(source[key], target[key])) {
+ value = key;
+ areEquivalent = true;
+ }
+ return areEquivalent;
+ });
+ return value;
+}
+
+var AssertDescriptor = {
+ equalTraits: {
+ value: function equivalentTraits(actual, expected, message) {
+ var difference;
+ var actualKeys = getOwnIdentifiers(actual);
+ var expectedKeys = getOwnIdentifiers(expected);
+
+ if (equivalentSets(actualKeys, expectedKeys)) {
+ this.fail({
+ operator: "equalTraits",
+ message: "Traits define different properties",
+ actual: actualKeys.sort().join(","),
+ expected: expectedKeys.sort().join(","),
+ });
+ }
+ else if ((difference = findNonEquivalentPropertyName(actual, expected))) {
+ this.fail({
+ operator: "equalTraits",
+ message: "Traits define non-equivalent property `" + difference + "`",
+ actual: actual[difference],
+ expected: expected[difference]
+ });
+ }
+ else {
+ this.pass(message || "Traits are equivalent.");
+ }
+ }
+ }
+};
+
+exports.Assert = function Assert() {
+ return Object.create(BaseAssert.apply(null, arguments), AssertDescriptor);
+};
diff --git a/addon-sdk/source/test/traits/utils.js b/addon-sdk/source/test/traits/utils.js
new file mode 100644
index 000000000..8426af7de
--- /dev/null
+++ b/addon-sdk/source/test/traits/utils.js
@@ -0,0 +1,56 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+var ERR_CONFLICT = "Remaining conflicting property: ";
+var ERR_REQUIRED = "Missing required property: ";
+
+exports.Data = function Data(value, enumerable, configurable, writable) {
+ return ({
+ value: value,
+ enumerable: enumerable !== false,
+ configurable: configurable !== false,
+ writable: writable !== false
+ });
+};
+
+exports.Method = function Method(method, enumerable, configurable, writable) {
+ return ({
+ value: method,
+ enumerable: enumerable !== false,
+ configurable: configurable !== false,
+ writable: writable !== false
+ });
+};
+
+exports.Accessor = function Accessor(get, set, enumerable, configurable) {
+ return ({
+ get: get,
+ set: set,
+ enumerable: enumerable !== false,
+ configurable: configurable !== false
+ });
+};
+
+exports.Required = function Required(name) {
+ function required() { throw new Error(ERR_REQUIRED + name) }
+
+ return ({
+ get: required,
+ set: required,
+ required: true
+ });
+};
+
+exports.Conflict = function Conflict(name) {
+ function conflict() { throw new Error(ERR_CONFLICT + name) }
+
+ return ({
+ get: conflict,
+ set: conflict,
+ conflict: true
+ });
+};
+
diff --git a/addon-sdk/source/test/util.js b/addon-sdk/source/test/util.js
new file mode 100644
index 000000000..af6a6f564
--- /dev/null
+++ b/addon-sdk/source/test/util.js
@@ -0,0 +1,90 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const {Cc, Ci} = require("chrome");
+const {getMostRecentBrowserWindow, open} = require("sdk/window/utils");
+const tabUtils = require("sdk/tabs/utils");
+const {when} = require("sdk/dom/events");
+
+
+var observerService = Cc["@mozilla.org/observer-service;1"]
+ .getService(Ci.nsIObserverService);
+
+const { ShimWaiver } = Cu.import("resource://gre/modules/ShimWaiver.jsm");
+const addObserver = ShimWaiver.getProperty(observerService, "addObserver");
+const removeObserver = ShimWaiver.getProperty(observerService, "removeObserver");
+
+const getActiveTab = (window=getMostRecentBrowserWindow()) =>
+ tabUtils.getActiveTab(window)
+
+const openWindow = () => {
+ const window = open();
+ return new Promise((resolve) => {
+ addObserver({
+ observe(subject, topic) {
+ if (subject === window) {
+ removeObserver(this, topic);
+ resolve(subject);
+ }
+ }
+ }, "browser-delayed-startup-finished", false);
+ });
+};
+exports.openWindow = openWindow;
+
+const closeWindow = (window) => {
+ const closed = when(window, "unload", true).then(_ => window);
+ window.close();
+ return closed;
+};
+exports.closeWindow = closeWindow;
+
+const openTab = (url, window=getMostRecentBrowserWindow()) => {
+ const tab = tabUtils.openTab(window, url);
+ const browser = tabUtils.getBrowserForTab(tab);
+
+ return when(browser, "load", true).then(_ => tab);
+};
+exports.openTab = openTab;
+
+const closeTab = (tab) => {
+ const result = when(tab, "TabClose").then(_ => tab);
+ tabUtils.closeTab(tab);
+
+ return result;
+};
+exports.closeTab = closeTab;
+
+const withTab = (test, uri="about:blank") => function*(assert) {
+ const tab = yield openTab(uri);
+ try {
+ yield* test(assert, tab);
+ }
+ finally {
+ yield closeTab(tab);
+ }
+};
+exports.withTab = withTab;
+
+const withWindow = () => function*(assert) {
+ const window = yield openWindow();
+ try {
+ yield* test(assert, window);
+ }
+ finally {
+ yield closeWindow(window);
+ }
+};
+exports.withWindow = withWindow;
+
+const receiveMessage = (manager, name) => new Promise((resolve) => {
+ manager.addMessageListener(name, {
+ receiveMessage(message) {
+ manager.removeMessageListener(name, this);
+ resolve(message);
+ }
+ });
+});
+exports.receiveMessage = receiveMessage;
diff --git a/addon-sdk/source/test/windows/test-fennec-windows.js b/addon-sdk/source/test/windows/test-fennec-windows.js
new file mode 100644
index 000000000..3e1c00d73
--- /dev/null
+++ b/addon-sdk/source/test/windows/test-fennec-windows.js
@@ -0,0 +1,48 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Cc, Ci } = require('chrome');
+const { setTimeout } = require('sdk/timers');
+const { Loader } = require('sdk/test/loader');
+const WM = Cc['@mozilla.org/appshell/window-mediator;1'].
+ getService(Ci.nsIWindowMediator);
+const { browserWindows } = require('sdk/windows');
+
+const ERR_MSG = 'This method is not yet supported by Fennec, consider using require("sdk/tabs") instead';
+
+// TEST: browserWindows.length for Fennec
+exports.testBrowserWindowsLength = function(assert) {
+ assert.equal(browserWindows.length, 1, "Only one window open");
+};
+
+// TEST: open & close window
+exports.testOpenWindow = function(assert) {
+ let tabCount = browserWindows.activeWindow.tabs.length;
+ let url = "data:text/html;charset=utf-8,<title>windows%20API%20test</title>";
+
+ try {
+ browserWindows.open({url: url});
+ assert.fail('Error was not thrown');
+ }
+ catch(e) {
+ assert.equal(e.message, ERR_MSG, 'Error is thrown on windows.open');
+ assert.equal(browserWindows.length, 1, "Only one window open");
+ }
+};
+
+exports.testCloseWindow = function(assert) {
+ let window = browserWindows.activeWindow;
+
+ try {
+ window.close();
+ assert.fail('Error was not thrown');
+ }
+ catch(e) {
+ assert.equal(e.message, ERR_MSG, 'Error is thrown on windows.close');
+ assert.equal(browserWindows.length, 1, "Only one window open");
+ }
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/windows/test-firefox-windows.js b/addon-sdk/source/test/windows/test-firefox-windows.js
new file mode 100644
index 000000000..4db9953eb
--- /dev/null
+++ b/addon-sdk/source/test/windows/test-firefox-windows.js
@@ -0,0 +1,621 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+'use strict';
+
+const { Cc, Ci } = require('chrome');
+const { setTimeout } = require('sdk/timers');
+const { Loader } = require('sdk/test/loader');
+const { onFocus, getMostRecentWindow, windows, isBrowser, getWindowTitle, isFocused } = require('sdk/window/utils');
+const { open, close, focus, promise: windowPromise } = require('sdk/window/helpers');
+const { browserWindows } = require("sdk/windows");
+const tabs = require("sdk/tabs");
+const winUtils = require("sdk/deprecated/window-utils");
+const { isPrivate } = require('sdk/private-browsing');
+const { isWindowPBSupported } = require('sdk/private-browsing/utils');
+const { viewFor } = require("sdk/view/core");
+const { defer } = require("sdk/lang/functional");
+const { cleanUI } = require("sdk/test/utils");
+const { after } = require("sdk/test/utils");
+const { merge } = require("sdk/util/object");
+const self = require("sdk/self");
+const { openTab } = require("../tabs/utils");
+const { set: setPref, reset: resetPref } = require("sdk/preferences/service");
+const OPEN_IN_NEW_WINDOW_PREF = 'browser.link.open_newwindow';
+const DISABLE_POPUP_PREF = 'dom.disable_open_during_load';
+const { wait } = require('../event/helpers');
+
+// TEST: open & close window
+exports.testOpenAndCloseWindow = function(assert, done) {
+ assert.equal(browserWindows.length, 1, "Only one window open");
+ let title = 'testOpenAndCloseWindow';
+
+ browserWindows.open({
+ url: "data:text/html;charset=utf-8,<title>" + title + "</title>",
+ onOpen: function(window) {
+ assert.equal(this, browserWindows, "The 'this' object is the windows object.");
+ assert.equal(window.tabs.length, 1, "Only one tab open");
+ assert.equal(browserWindows.length, 2, "Two windows open");
+
+ window.tabs.activeTab.once('ready', function onReady(tab) {
+ assert.pass(RegExp(title).test(window.title), "URL correctly loaded");
+ window.close();
+ });
+ },
+ onClose: function(window) {
+ assert.equal(window.tabs.length, 0, "Tabs were cleared");
+ assert.equal(browserWindows.length, 1, "Only one window open");
+ done();
+ }
+ });
+};
+
+exports.testNeWindowIsFocused = function(assert, done) {
+ let mainWindow = browserWindows.activeWindow;
+
+ browserWindows.open({
+ url: "about:blank",
+ onOpen: function(window) {
+ focus(viewFor(window)).then((window) => {
+ assert.ok(isFocused(window), 'the new window is focused');
+ assert.ok(isFocused(browserWindows.activeWindow), 'the active window is focused');
+ assert.ok(!isFocused(mainWindow), 'the main window is not focused');
+ done();
+ })
+ }
+ });
+}
+
+exports.testOpenRelativePathWindow = function*(assert) {
+ assert.equal(browserWindows.length, 1, "Only one window open");
+
+ let loader = Loader(module, null, null, {
+ modules: {
+ "sdk/self": merge({}, self, {
+ data: merge({}, self.data, require("./../fixtures"))
+ })
+ }
+ });
+ assert.pass("Created a new loader");
+
+ let tabReady = new Promise(resolve => {
+ loader.require("sdk/tabs").on("ready", (tab) => {
+ if (!/test\.html$/.test(tab.url))
+ return;
+ assert.equal(tab.title, "foo",
+ "tab opened a document with relative path");
+ resolve();
+ });
+ });
+
+
+ yield new Promise(resolve => {
+ loader.require("sdk/windows").browserWindows.open({
+ url: "./test.html",
+ onOpen: (window) => {
+ assert.pass("Created a new window");
+ resolve();
+ }
+ })
+ });
+
+ yield tabReady;
+ loader.unload();
+}
+
+exports.testAutomaticDestroy = function(assert, done) {
+ let windows = browserWindows;
+
+ // Create a second windows instance that we will unload
+ let called = false;
+ let loader = Loader(module);
+ let windows2 = loader.require("sdk/windows").browserWindows;
+
+ windows2.on("open", function() {
+ called = true;
+ });
+
+ loader.unload();
+
+ // Fire a windows event and check that this unloaded instance is inactive
+ windows.open({
+ url: "data:text/html;charset=utf-8,foo",
+ onOpen: function(window) {
+ setTimeout(function () {
+ assert.ok(!called, "Unloaded windows instance is destroyed and inactive");
+ done();
+ });
+ }
+ });
+};
+
+exports.testWindowTabsObject = function(assert, done) {
+ let window, count = 0;
+ function runTest() {
+ if (++count != 2)
+ return;
+
+ assert.equal(window.tabs.length, 1, "Only 1 tab open");
+ assert.equal(window.tabs.activeTab.title, "tab 1", "Correct active tab");
+
+ window.tabs.open({
+ url: "data:text/html;charset=utf-8,<title>tab 2</title>",
+ inBackground: true,
+ onReady: function onReady(newTab) {
+ assert.equal(window.tabs.length, 2, "New tab open");
+ assert.equal(newTab.title, "tab 2", "Correct new tab title");
+ assert.equal(window.tabs.activeTab.title, "tab 1", "Correct active tab");
+
+ let i = 1;
+ for (let tab of window.tabs)
+ assert.equal(tab.title, "tab " + i++, "Correct title");
+
+ window.close();
+ }
+ });
+ }
+
+ tabs.once("ready", runTest);
+
+ browserWindows.open({
+ url: "data:text/html;charset=utf-8,<title>tab 1</title>",
+ onActivate: function onActivate(win) {
+ window = win;
+ runTest();
+ },
+ onClose: function onClose(window) {
+ assert.equal(window.tabs.length, 0, "No more tabs on closed window");
+ done();
+ }
+ });
+};
+
+exports.testOnOpenOnCloseListeners = function(assert, done) {
+ let windows = browserWindows;
+
+ assert.equal(browserWindows.length, 1, "Only one window open");
+
+ let received = {
+ listener1: false,
+ listener2: false,
+ listener3: false,
+ listener4: false
+ }
+
+ function listener1() {
+ assert.equal(this, windows, "The 'this' object is the windows object.");
+
+ if (received.listener1)
+ assert.fail("Event received twice");
+ received.listener1 = true;
+ }
+
+ function listener2() {
+ if (received.listener2)
+ assert.fail("Event received twice");
+ received.listener2 = true;
+ }
+
+ function listener3() {
+ assert.equal(this, windows, "The 'this' object is the windows object.");
+ if (received.listener3)
+ assert.fail("Event received twice");
+ received.listener3 = true;
+ }
+
+ function listener4() {
+ if (received.listener4)
+ assert.fail("Event received twice");
+ received.listener4 = true;
+ }
+
+ windows.on('open', listener1);
+ windows.on('open', listener2);
+ windows.on('close', listener3);
+ windows.on('close', listener4);
+
+ windows.open({
+ url: "data:text/html;charset=utf-8,foo",
+ onOpen: function(window) {
+ window.close(function() {
+ assert.ok(received.listener1, "onOpen handler called");
+ assert.ok(received.listener2, "onOpen handler called");
+ assert.ok(received.listener3, "onClose handler called");
+ assert.ok(received.listener4, "onClose handler called");
+
+ windows.removeListener('open', listener1);
+ windows.removeListener('open', listener2);
+ windows.removeListener('close', listener3);
+ windows.removeListener('close', listener4);
+
+ done();
+ });
+ }
+ });
+};
+
+exports.testActiveWindow = function*(assert) {
+ let windows = browserWindows;
+ let window = getMostRecentWindow();
+
+ // Raw window objects
+ let rawWindow2 = yield windowPromise(window.OpenBrowserWindow(), "load").then(focus);
+ assert.pass("Window 2 was created");
+
+ // open a tab in window 2
+ yield openTab(rawWindow2, "data:text/html;charset=utf-8,<title>window 2</title>");
+
+ assert.equal(rawWindow2.content.document.title, "window 2", "Got correct raw window 2");
+ assert.equal(rawWindow2.document.title, windows[1].title, "Saw correct title on window 2");
+
+ let rawWindow3 = yield windowPromise(window.OpenBrowserWindow(), "load").then(focus);;
+ assert.pass("Window 3 was created");
+
+ // open a tab in window 3
+ yield openTab(rawWindow3, "data:text/html;charset=utf-8,<title>window 3</title>");
+
+ assert.equal(rawWindow3.content.document.title, "window 3", "Got correct raw window 3");
+ assert.equal(rawWindow3.document.title, windows[2].title, "Saw correct title on window 3");
+
+ assert.equal(windows.length, 3, "Correct number of browser windows");
+
+ let count = 0;
+ for (let window in windows) {
+ count++;
+ }
+ assert.equal(count, 3, "Correct number of windows returned by iterator");
+ assert.equal(windows.activeWindow.title, windows[2].title, "Correct active window title - 3");
+ let window3 = windows[2];
+
+ yield focus(rawWindow2);
+
+ assert.equal(windows.activeWindow.title, windows[1].title, "Correct active window title - 2");
+ let window2 = windows[1];
+
+ yield new Promise(resolve => {
+ onFocus(rawWindow2).then(resolve);
+ window2.activate();
+ assert.pass("activating window2");
+ });
+
+ assert.equal(windows.activeWindow.title, windows[1].title, "Correct active window - 2");
+
+ yield new Promise(resolve => {
+ onFocus(rawWindow3).then(resolve);
+ window3.activate();
+ assert.pass("activating window3");
+ });
+
+ assert.equal(windows.activeWindow.title, window3.title, "Correct active window - 3");
+
+ yield close(rawWindow2);
+ yield close(rawWindow3);
+
+ assert.equal(windows.length, 1, "Correct number of browser windows");
+ assert.equal(window.closed, false, "Original window is still open");
+};
+
+exports.testTrackWindows = function(assert, done) {
+ let windows = [];
+ let actions = [];
+
+ let expects = [
+ "activate 0", "global activate 0", "deactivate 0", "global deactivate 0",
+ "activate 1", "global activate 1", "deactivate 1", "global deactivate 1",
+ "activate 2", "global activate 2"
+ ];
+
+ function windowsActivation(window) {
+ let index = windows.indexOf(window);
+ // only concerned with windows opened for this test
+ if (index < 0)
+ return;
+
+ assert.equal(actions.join(), expects.slice(0, index*4 + 1).join(), expects[index*4 + 1]);
+ actions.push("global activate " + index)
+ }
+
+ function windowsDeactivation(window) {
+ let index = windows.indexOf(window);
+ // only concerned with windows opened for this test
+ if (index < 0)
+ return;
+
+ assert.equal(actions.join(), expects.slice(0, index*4 + 3).join(), expects[index*4 + 3]);
+ actions.push("global deactivate " + index)
+ }
+
+ // listen to global activate events
+ browserWindows.on("activate", windowsActivation);
+
+ // listen to global deactivate events
+ browserWindows.on("deactivate", windowsDeactivation);
+
+ function openWindow() {
+ windows.push(browserWindows.open({
+ url: "data:text/html;charset=utf-8,<i>testTrackWindows</i>",
+ onActivate: function(window) {
+ let index = windows.indexOf(window);
+
+ // Guard against windows that have already been removed.
+ // See bug 874502 comment 32.
+ if (index == -1)
+ return;
+
+ assert.equal(actions.join(),
+ expects.slice(0, index*4).join(),
+ "expecting " + expects[index*4]);
+ actions.push("activate " + index);
+
+ if (windows.length < 3) {
+ openWindow()
+ }
+ else {
+ (function closeWindows(windows) {
+ if (!windows.length) {
+ assert.pass('the last window was closed');
+ browserWindows.removeListener("activate", windowsActivation);
+ browserWindows.removeListener("deactivate", windowsDeactivation);
+ assert.pass('removed listeners');
+ return done();
+ }
+
+ return windows.pop().close(function() {
+ assert.pass('window was closed');
+ closeWindows(windows);
+ });
+ })(windows)
+ }
+ },
+ onDeactivate: function(window) {
+ let index = windows.indexOf(window);
+
+ // Guard against windows that have already been removed.
+ // See bug 874502 comment 32.
+ if (index == -1)
+ return;
+
+ assert.equal(actions.join(),
+ expects.slice(0, index*4 + 2).join(),
+ "expecting " + expects[index*4 + 2]);
+ actions.push("deactivate " + index)
+ }
+ }));
+ }
+ openWindow();
+}
+
+// test that it is not possible to open a private window by default
+exports.testWindowOpenPrivateDefault = function*(assert) {
+ const TITLE = "yo";
+ const URL = "data:text/html,<title>" + TITLE + "</title>";
+
+ let tabReady = new Promise(resolve => {
+ tabs.on('ready', function onTabReady(tab) {
+ if (tab.url != URL)
+ return;
+
+ tabs.removeListener('ready', onTabReady);
+ assert.equal(tab.title, TITLE, 'opened correct tab');
+ assert.equal(isPrivate(tab), false, 'tab is not private');
+ resolve();
+ });
+ })
+
+ yield new Promise(resolve => browserWindows.open({
+ url: URL,
+ isPrivate: true,
+ onOpen: function(window) {
+ assert.pass("the new window was opened");
+ resolve();
+ }
+ }));
+
+ yield tabReady;
+}
+
+// test that it is not possible to find a private window in
+// windows module's iterator
+exports.testWindowIteratorPrivateDefault = function*(assert) {
+ assert.equal(browserWindows.length, 1, 'only one window open');
+
+ let window = yield open('chrome://browser/content/browser.xul', {
+ features: {
+ private: true,
+ chrome: true
+ }
+ });
+
+ yield focus(window);
+
+ // test that there is a private window opened
+ assert.equal(isPrivate(window), true, 'there is a private window open');
+ assert.strictEqual(window, winUtils.activeWindow);
+ assert.strictEqual(window, getMostRecentWindow());
+
+ assert.ok(!isPrivate(browserWindows.activeWindow));
+
+ assert.equal(browserWindows.length, 1, 'only one window in browserWindows');
+ assert.equal(windows().length, 1, 'only one window in windows()');
+
+ assert.equal(windows(null, { includePrivate: true }).length, 2);
+
+ // test that all windows in iterator are not private
+ for (let window of browserWindows) {
+ assert.ok(!isPrivate(window), 'no window in browserWindows is private');
+ }
+};
+
+exports["test getView(window)"] = function(assert, done) {
+ browserWindows.once("open", window => {
+ const view = viewFor(window);
+
+ assert.ok(view instanceof Ci.nsIDOMWindow, "view is a window");
+ assert.ok(isBrowser(view), "view is a browser window");
+ assert.equal(getWindowTitle(view), window.title,
+ "window has a right title");
+
+ window.close();
+ // Defer handler cause window is destroyed after event is dispatched.
+ browserWindows.once("close", defer(_ => {
+ assert.equal(viewFor(window), null, "window view is gone");
+ done();
+ }));
+ });
+
+ browserWindows.open({ url: "data:text/html,<title>yo</title>" });
+};
+
+// Tests that the this property is correct in event listeners called from
+// the windows API.
+exports.testWindowEventBindings = function(assert, done) {
+ let loader = Loader(module);
+ let { browserWindows } = loader.require("sdk/windows");
+ let firstWindow = browserWindows.activeWindow;
+ let { viewFor } = loader.require("sdk/view/core");
+
+ let EVENTS = ["open", "close", "activate", "deactivate"];
+
+ let windowBoundEventHandler = (event) => function(window) {
+ // Close event listeners are always bound to browserWindows.
+ if (event == "close")
+ assert.equal(this, browserWindows, "window listener for " + event + " event should be bound to the browserWindows object.");
+ else
+ assert.equal(this, window, "window listener for " + event + " event should be bound to the window object.");
+ };
+
+ let windowsBoundEventHandler = (event) => function(window) {
+ assert.equal(this, browserWindows, "windows listener for " + event + " event should be bound to the browserWindows object.");
+ }
+ for (let event of EVENTS)
+ browserWindows.on(event, windowsBoundEventHandler(event));
+
+ let windowsOpenEventHandler = (event) => function(window) {
+ // Open and close event listeners are always bound to browserWindows.
+ if (event == "open" || event == "close")
+ assert.equal(this, browserWindows, "windows open listener for " + event + " event should be bound to the browserWindows object.");
+ else
+ assert.equal(this, window, "windows open listener for " + event + " event should be bound to the window object.");
+ }
+
+ let openArgs = {
+ url: "data:text/html;charset=utf-8,binding-test",
+ onOpen: function(window) {
+ windowsOpenEventHandler("open").call(this, window);
+
+ for (let event of EVENTS)
+ window.on(event, windowBoundEventHandler(event));
+
+ let rawWindow = viewFor(window);
+ onFocus(rawWindow).then(() => {
+ window.once("deactivate", () => {
+ window.once("close", () => {
+ loader.unload();
+ done();
+ });
+
+ window.close();
+ });
+
+ firstWindow.activate();
+ });
+ }
+ };
+ // Listen to everything except onOpen
+ for (let event of EVENTS.slice(1)) {
+ openArgs["on" + event.slice(0, 1).toUpperCase() + event.slice(1)] = windowsOpenEventHandler(event);
+ }
+
+ browserWindows.open(openArgs);
+}
+
+// Tests that the this property is correct in event listeners called from
+// manipulating window.tabs.
+exports.testWindowTabEventBindings = function(assert, done) {
+ let loader = Loader(module);
+ let { browserWindows } = loader.require("sdk/windows");
+ let firstWindow = browserWindows.activeWindow;
+ let tabs = loader.require("sdk/tabs");
+ let windowTabs = firstWindow.tabs;
+ let firstTab = firstWindow.tabs.activeTab;
+
+ let EVENTS = ["open", "close", "activate", "deactivate",
+ "load", "ready", "pageshow"];
+
+ let tabBoundEventHandler = (event) => function(tab) {
+ assert.equal(this, tab, "tab listener for " + event + " event should be bound to the tab object.");
+ };
+
+ let windowTabsBoundEventHandler = (event) => function(tab) {
+ assert.equal(this, windowTabs, "window tabs listener for " + event + " event should be bound to the windowTabs object.");
+ }
+ for (let event of EVENTS)
+ windowTabs.on(event, windowTabsBoundEventHandler(event));
+
+ let windowTabsOpenEventHandler = (event) => function(tab) {
+ assert.equal(this, tab, "window tabs open listener for " + event + " event should be bound to the tab object.");
+ }
+
+ let openArgs = {
+ url: "data:text/html;charset=utf-8,binding-test",
+ onOpen: function(tab) {
+ windowTabsOpenEventHandler("open").call(this, tab);
+
+ for (let event of EVENTS)
+ tab.on(event, tabBoundEventHandler(event));
+
+ tab.once("pageshow", () => {
+ tab.once("deactivate", () => {
+ tab.once("close", () => {
+ loader.unload();
+ done();
+ });
+
+ tab.close();
+ });
+
+ firstTab.activate();
+ });
+ }
+ };
+ // Listen to everything except onOpen
+ for (let event of EVENTS.slice(1)) {
+ let eventProperty = "on" + event.slice(0, 1).toUpperCase() + event.slice(1);
+ openArgs[eventProperty] = windowTabsOpenEventHandler(event);
+ }
+
+ windowTabs.open(openArgs);
+}
+
+// related to bug #989288
+// https://bugzilla.mozilla.org/show_bug.cgi?id=989288
+exports["test window events after window.open"] = function*(assert, done) {
+ // ensure popups open in a new windows and disable popup blocker
+ setPref(OPEN_IN_NEW_WINDOW_PREF, 2);
+ setPref(DISABLE_POPUP_PREF, false);
+
+ // open window to trigger observers
+ tabs.activeTab.attach({
+ contentScript: "window.open('about:blank', '', " +
+ "'width=800,height=600,resizable=no,status=no,location=no');"
+ });
+
+ let window = yield wait(browserWindows, "open");
+ assert.pass("tab open has occured");
+ window.close();
+
+ yield wait(browserWindows,"close");
+ assert.pass("tab close has occured");
+};
+
+after(exports, function*(name, assert) {
+ resetPopupPrefs();
+ yield cleanUI();
+});
+
+const resetPopupPrefs = () => {
+ resetPref(OPEN_IN_NEW_WINDOW_PREF);
+ resetPref(DISABLE_POPUP_PREF);
+};
+
+require('sdk/test').run(exports);
diff --git a/addon-sdk/source/test/zip/utils.js b/addon-sdk/source/test/zip/utils.js
new file mode 100644
index 000000000..8d192b9fb
--- /dev/null
+++ b/addon-sdk/source/test/zip/utils.js
@@ -0,0 +1,126 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const { Ci, Cc } = require("chrome");
+const { defer, all } = require("sdk/core/promise");
+
+const PR_RDONLY = 0x01;
+const PR_WRONLY = 0x02;
+const PR_RDWR = 0x04;
+const PR_CREATE_FILE = 0x08;
+const PR_APPEND = 0x10;
+const PR_TRUNCATE = 0x20;
+const PR_SYNC = 0x40;
+const PR_EXCL = 0x80;
+
+// Default compression level:
+const { COMPRESSION_DEFAULT } = Ci.nsIZipWriter;
+
+function createNsFile(path) {
+ let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
+ try {
+ file.initWithPath(path);
+ } catch(e) {
+ throw new Error("This zip file path is not valid : " + path + "\n" + e);
+ }
+ return file;
+}
+exports.createNsFile = createNsFile;
+
+const converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
+ createInstance(Ci.nsIScriptableUnicodeConverter);
+converter.charset = "UTF-8";
+function streamForData(data) {
+ return converter.convertToInputStream(data);
+}
+
+exports.ZipWriter = function (zipPath, mode) {
+ mode = mode ? mode : PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE;
+
+ let zw = Cc["@mozilla.org/zipwriter;1"].createInstance(Ci.nsIZipWriter);
+ zw.open(createNsFile(zipPath), mode);
+
+ // Create a directory entry.
+ // Returns true if the entry was created, or false if the entry already exists
+ this.mkdir = function mkdir(pathInZip) {
+ try {
+ zw.addEntryDirectory(pathInZip, 0, false);
+ }
+ catch(e) {
+ if (e.name === "NS_ERROR_FILE_ALREADY_EXISTS")
+ return false;
+ throw e
+ }
+ return true;
+ }
+
+ this.addFile = function addFile(pathInZip, filePath) {
+ let { promise, reject, resolve } = defer();
+
+ let nsfile = createNsFile(filePath);
+ if (!nsfile.exists()) {
+ reject(new Error("This file doesn't exist: " + nsfile.path));
+ return promise;
+ }
+
+ // Case 1/ Regular file
+ if (!nsfile.isDirectory()) {
+ try {
+ zw.addEntryFile(pathInZip, COMPRESSION_DEFAULT, nsfile, false);
+ resolve();
+ }
+ catch (e) {
+ reject(new Error("Unable to add following file in zip: " + nsfile.path + "\n" + e));
+ }
+ return promise;
+ }
+
+ // Case 2/ Directory
+ if (pathInZip.substr(-1) !== '/')
+ pathInZip += "/";
+ let entries = nsfile.directoryEntries;
+ let array = [];
+
+ zw.addEntryDirectory(pathInZip, nsfile.lastModifiedTime, false);
+
+ while(entries.hasMoreElements()) {
+ let file = entries.getNext().QueryInterface(Ci.nsIFile);
+ if (file.leafName === "." || file.leafName === "..")
+ continue;
+ let path = pathInZip + file.leafName;
+ if (path.substr(0, 1) == '/') {
+ path = path.substr(1);
+ }
+ this.addFile(path, file.path);
+ }
+
+ resolve();
+ return promise;
+ }
+
+ this.addData = function (pathInZip, data) {
+ let deferred = defer();
+
+ try {
+ let stream = streamForData(data);
+ zw.addEntryStream(pathInZip, 0, COMPRESSION_DEFAULT, stream, false);
+ } catch(e) {
+ throw new Error("Unable to add following data in zip: " +
+ data.substr(0, 20) + "\n" + e);
+ }
+
+ deferred.resolve();
+ return deferred.promise;
+ }
+
+ this.close = function close() {
+ let deferred = defer();
+
+ zw.close();
+
+ deferred.resolve();
+ return deferred.promise;
+ }
+}